blob: 28fbac8d265103c0ceec2abbce3dcdd843ee2412 [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
283 str = "hello/x\t"; /* slash is not an invalid character */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200284 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200285 assert_int_equal(7, len);
286 assert_string_equal("hello/x\t", word);
287
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200288 assert_null(buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200289
290 /* different quoting */
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200291 str = "hello ";
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 Krejciefd22f62018-09-27 11:47:58 +0200293 assert_null(buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200294 assert_int_equal(5, len);
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200295 assert_string_equal("hello ", word);
Radek Krejciefd22f62018-09-27 11:47:58 +0200296
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200297 str = "hello/*comment*/\n";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200298 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200299 assert_null(buf);
300 assert_int_equal(5, len);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200301 assert_false(strncmp("hello", word, len));
302
303
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200304 str = "\"hello\\n\\t\\\"\\\\\";";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200305 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200306 assert_null(buf);
307 assert_int_equal(9, len);
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200308 assert_string_equal("hello\\n\\t\\\"\\\\\";", word);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200309
310 ctx.indent = 14;
311 str = "\"hello \t\n\t\t world!\"";
312 /* - space and tabs before newline are stripped out
313 * - space and tabs after newline (indentation) are stripped out
314 */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200315 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200316 assert_non_null(buf);
317 assert_ptr_equal(word, buf);
318 assert_int_equal(14, len);
319 assert_string_equal("hello\n world!", word);
320 free(buf);
321
322 ctx.indent = 14;
323 str = "\"hello\n \tworld!\"";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200324 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200325 assert_non_null(buf);
326 assert_ptr_equal(word, buf);
327 assert_int_equal(12, len);
328 assert_string_equal("hello\nworld!", word);
329 free(buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200330
331 str = "\'hello\'";
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 Krejciefd22f62018-09-27 11:47:58 +0200333 assert_null(buf);
334 assert_int_equal(5, len);
335 assert_false(strncmp("hello", word, 5));
336
337 str = "\"hel\" +\t\n\"lo\"";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200338 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200339 assert_ptr_equal(word, buf);
340 assert_int_equal(5, len);
341 assert_string_equal("hello", word);
342 free(buf);
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200343 str = "\"hel\" +\t\nlo"; /* unquoted the second part */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200344 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200345 logbuf_assert("Both string parts divided by '+' must be quoted. Line number 5.");
Radek Krejciefd22f62018-09-27 11:47:58 +0200346
347 str = "\'he\'\t\n+ \"llo\"";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200348 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200349 assert_ptr_equal(word, buf);
350 assert_int_equal(5, len);
351 assert_string_equal("hello", word);
352 free(buf);
353
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200354 str = " \t\n\"he\"+\'llo\'";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200355 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200356 assert_ptr_equal(word, buf);
357 assert_int_equal(5, len);
358 assert_string_equal("hello", word);
359 free(buf);
360
Radek Krejci44ceedc2018-10-02 15:54:31 +0200361 /* missing argument */
362 str = ";";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200363 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200364 logbuf_assert("Invalid character sequence \";\", expected an argument. Line number 7.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200365}
366
367static void
368test_stmts(void **state)
369{
370 (void) state; /* unused */
371
Radek Krejcie7b95092019-05-15 11:03:07 +0200372 struct lys_parser_ctx ctx;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200373 const char *str, *p;
374 enum yang_keyword kw;
375 char *word;
376 size_t len;
377
378 ctx.ctx = NULL;
379 ctx.line = 1;
380
381 str = "\n// comment\n\tinput\t{";
382 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
383 assert_int_equal(YANG_INPUT, kw);
384 assert_int_equal(5, len);
385 assert_string_equal("input\t{", word);
386 assert_string_equal("\t{", str);
387
388 str = "\t /* comment */\t output\n\t{";
389 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
390 assert_int_equal(YANG_OUTPUT, kw);
391 assert_int_equal(6, len);
392 assert_string_equal("output\n\t{", word);
393 assert_string_equal("\n\t{", str);
394
395 str = "/input { "; /* invalid slash */
396 assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len));
397 logbuf_assert("Invalid identifier first character '/'. Line number 4.");
398
399 str = "not-a-statement-nor-extension { "; /* invalid identifier */
400 assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len));
401 logbuf_assert("Invalid character sequence \"not-a-statement-nor-extension\", expected a keyword. Line number 4.");
402
403 str = "path;"; /* missing sep after the keyword */
404 assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len));
405 logbuf_assert("Invalid character sequence \"path;\", expected a keyword followed by a separator. Line number 4.");
406
407 str = "action ";
408 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
409 assert_int_equal(YANG_ACTION, kw);
410 assert_int_equal(6, len);
411 str = "anydata ";
412 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
413 assert_int_equal(YANG_ANYDATA, kw);
414 assert_int_equal(7, len);
415 str = "anyxml ";
416 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
417 assert_int_equal(YANG_ANYXML, kw);
418 assert_int_equal(6, len);
419 str = "argument ";
420 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
421 assert_int_equal(YANG_ARGUMENT, kw);
422 assert_int_equal(8, len);
423 str = "augment ";
424 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
425 assert_int_equal(YANG_AUGMENT, kw);
426 assert_int_equal(7, len);
427 str = "base ";
428 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
429 assert_int_equal(YANG_BASE, kw);
430 assert_int_equal(4, len);
431 str = "belongs-to ";
432 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
433 assert_int_equal(YANG_BELONGS_TO, kw);
434 assert_int_equal(10, len);
435 str = "bit ";
436 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
437 assert_int_equal(YANG_BIT, kw);
438 assert_int_equal(3, len);
439 str = "case ";
440 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
441 assert_int_equal(YANG_CASE, kw);
442 assert_int_equal(4, len);
443 str = "choice ";
444 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
445 assert_int_equal(YANG_CHOICE, kw);
446 assert_int_equal(6, len);
447 str = "config ";
448 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
449 assert_int_equal(YANG_CONFIG, kw);
450 assert_int_equal(6, len);
451 str = "contact ";
452 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
453 assert_int_equal(YANG_CONTACT, kw);
454 assert_int_equal(7, len);
455 str = "container ";
456 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
457 assert_int_equal(YANG_CONTAINER, kw);
458 assert_int_equal(9, len);
459 str = "default ";
460 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
461 assert_int_equal(YANG_DEFAULT, kw);
462 assert_int_equal(7, len);
463 str = "description ";
464 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
465 assert_int_equal(YANG_DESCRIPTION, kw);
466 assert_int_equal(11, len);
467 str = "deviate ";
468 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
469 assert_int_equal(YANG_DEVIATE, kw);
470 assert_int_equal(7, len);
471 str = "deviation ";
472 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
473 assert_int_equal(YANG_DEVIATION, kw);
474 assert_int_equal(9, len);
475 str = "enum ";
476 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
477 assert_int_equal(YANG_ENUM, kw);
478 assert_int_equal(4, len);
479 str = "error-app-tag ";
480 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
481 assert_int_equal(YANG_ERROR_APP_TAG, kw);
482 assert_int_equal(13, len);
483 str = "error-message ";
484 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
485 assert_int_equal(YANG_ERROR_MESSAGE, kw);
486 assert_int_equal(13, len);
487 str = "extension ";
488 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
489 assert_int_equal(YANG_EXTENSION, kw);
490 assert_int_equal(9, len);
491 str = "feature ";
492 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
493 assert_int_equal(YANG_FEATURE, kw);
494 assert_int_equal(7, len);
495 str = "fraction-digits ";
496 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
497 assert_int_equal(YANG_FRACTION_DIGITS, kw);
498 assert_int_equal(15, len);
499 str = "grouping ";
500 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
501 assert_int_equal(YANG_GROUPING, kw);
502 assert_int_equal(8, len);
503 str = "identity ";
504 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
505 assert_int_equal(YANG_IDENTITY, kw);
506 assert_int_equal(8, len);
507 str = "if-feature ";
508 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
509 assert_int_equal(YANG_IF_FEATURE, kw);
510 assert_int_equal(10, len);
511 str = "import ";
512 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
513 assert_int_equal(YANG_IMPORT, kw);
514 assert_int_equal(6, len);
515 str = "include ";
516 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
517 assert_int_equal(YANG_INCLUDE, kw);
518 assert_int_equal(7, len);
519 str = "input{";
520 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
521 assert_int_equal(YANG_INPUT, kw);
522 assert_int_equal(5, len);
523 str = "key ";
524 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
525 assert_int_equal(YANG_KEY, kw);
526 assert_int_equal(3, len);
527 str = "leaf ";
528 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
529 assert_int_equal(YANG_LEAF, kw);
530 assert_int_equal(4, len);
531 str = "leaf-list ";
532 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
533 assert_int_equal(YANG_LEAF_LIST, kw);
534 assert_int_equal(9, len);
535 str = "length ";
536 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
537 assert_int_equal(YANG_LENGTH, kw);
538 assert_int_equal(6, len);
539 str = "list ";
540 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
541 assert_int_equal(YANG_LIST, kw);
542 assert_int_equal(4, len);
543 str = "mandatory ";
544 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
545 assert_int_equal(YANG_MANDATORY, kw);
546 assert_int_equal(9, len);
547 str = "max-elements ";
548 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
549 assert_int_equal(YANG_MAX_ELEMENTS, kw);
550 assert_int_equal(12, len);
551 str = "min-elements ";
552 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
553 assert_int_equal(YANG_MIN_ELEMENTS, kw);
554 assert_int_equal(12, len);
555 str = "modifier ";
556 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
557 assert_int_equal(YANG_MODIFIER, kw);
558 assert_int_equal(8, len);
559 str = "module ";
560 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
561 assert_int_equal(YANG_MODULE, kw);
562 assert_int_equal(6, len);
563 str = "must ";
564 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
565 assert_int_equal(YANG_MUST, kw);
566 assert_int_equal(4, len);
567 str = "namespace ";
568 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
569 assert_int_equal(YANG_NAMESPACE, kw);
570 assert_int_equal(9, len);
571 str = "notification ";
572 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
573 assert_int_equal(YANG_NOTIFICATION, kw);
574 assert_int_equal(12, len);
575 str = "ordered-by ";
576 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
577 assert_int_equal(YANG_ORDERED_BY, kw);
578 assert_int_equal(10, len);
579 str = "organization ";
580 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
581 assert_int_equal(YANG_ORGANIZATION, kw);
582 assert_int_equal(12, len);
583 str = "output ";
584 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
585 assert_int_equal(YANG_OUTPUT, kw);
586 assert_int_equal(6, len);
587 str = "path ";
588 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
589 assert_int_equal(YANG_PATH, kw);
590 assert_int_equal(4, len);
591 str = "pattern ";
592 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
593 assert_int_equal(YANG_PATTERN, kw);
594 assert_int_equal(7, len);
595 str = "position ";
596 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
597 assert_int_equal(YANG_POSITION, kw);
598 assert_int_equal(8, len);
599 str = "prefix ";
600 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
601 assert_int_equal(YANG_PREFIX, kw);
602 assert_int_equal(6, len);
603 str = "presence ";
604 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
605 assert_int_equal(YANG_PRESENCE, kw);
606 assert_int_equal(8, len);
607 str = "range ";
608 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
609 assert_int_equal(YANG_RANGE, kw);
610 assert_int_equal(5, len);
611 str = "reference ";
612 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
613 assert_int_equal(YANG_REFERENCE, kw);
614 assert_int_equal(9, len);
615 str = "refine ";
616 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
617 assert_int_equal(YANG_REFINE, kw);
618 assert_int_equal(6, len);
619 str = "require-instance ";
620 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
621 assert_int_equal(YANG_REQUIRE_INSTANCE, kw);
622 assert_int_equal(16, len);
623 str = "revision ";
624 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
625 assert_int_equal(YANG_REVISION, kw);
626 assert_int_equal(8, len);
627 str = "revision-date ";
628 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
629 assert_int_equal(YANG_REVISION_DATE, kw);
630 assert_int_equal(13, len);
631 str = "rpc ";
632 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
633 assert_int_equal(YANG_RPC, kw);
634 assert_int_equal(3, len);
635 str = "status ";
636 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
637 assert_int_equal(YANG_STATUS, kw);
638 assert_int_equal(6, len);
639 str = "submodule ";
640 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
641 assert_int_equal(YANG_SUBMODULE, kw);
642 assert_int_equal(9, len);
643 str = "type ";
644 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
645 assert_int_equal(YANG_TYPE, kw);
646 assert_int_equal(4, len);
647 str = "typedef ";
648 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
649 assert_int_equal(YANG_TYPEDEF, kw);
650 assert_int_equal(7, len);
651 str = "unique ";
652 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
653 assert_int_equal(YANG_UNIQUE, kw);
654 assert_int_equal(6, len);
655 str = "units ";
656 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
657 assert_int_equal(YANG_UNITS, kw);
658 assert_int_equal(5, len);
659 str = "uses ";
660 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
661 assert_int_equal(YANG_USES, kw);
662 assert_int_equal(4, len);
663 str = "value ";
664 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
665 assert_int_equal(YANG_VALUE, kw);
666 assert_int_equal(5, len);
667 str = "when ";
668 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
669 assert_int_equal(YANG_WHEN, kw);
670 assert_int_equal(4, len);
671 str = "yang-version ";
672 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
673 assert_int_equal(YANG_YANG_VERSION, kw);
674 assert_int_equal(12, len);
675 str = "yin-element ";
676 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
677 assert_int_equal(YANG_YIN_ELEMENT, kw);
678 assert_int_equal(11, len);
Radek Krejci626df482018-10-11 15:06:31 +0200679 str = ";config false;";
680 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
681 assert_int_equal(YANG_SEMICOLON, kw);
682 assert_int_equal(1, len);
683 assert_string_equal("config false;", str);
684 str = "{ config false;";
685 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
686 assert_int_equal(YANG_LEFT_BRACE, kw);
687 assert_int_equal(1, len);
688 assert_string_equal(" config false;", str);
689 str = "}";
690 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
691 assert_int_equal(YANG_RIGHT_BRACE, kw);
692 assert_int_equal(1, len);
693 assert_string_equal("", str);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200694
695 /* geenric extension */
696 str = p = "nacm:default-deny-write;";
697 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
698 assert_int_equal(YANG_CUSTOM, kw);
699 assert_int_equal(23, len);
700 assert_ptr_equal(p, word);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200701}
Radek Krejci44ceedc2018-10-02 15:54:31 +0200702
Radek Krejci05b13982018-11-28 16:22:07 +0100703static void
704test_minmax(void **state)
705{
706 *state = test_minmax;
707
Radek Krejcie7b95092019-05-15 11:03:07 +0200708 struct lys_parser_ctx ctx = {0};
Radek Krejci05b13982018-11-28 16:22:07 +0100709 uint16_t flags = 0;
710 uint32_t value = 0;
711 struct lysp_ext_instance *ext = NULL;
712 const char *str;
713
714 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
715 assert_non_null(ctx.ctx);
716 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100717 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci05b13982018-11-28 16:22:07 +0100718
Radek Krejcidf6cad12018-11-28 17:10:55 +0100719 str = " 1invalid; ...";
Radek Krejci05b13982018-11-28 16:22:07 +0100720 assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext));
Radek Krejcidf6cad12018-11-28 17:10:55 +0100721 logbuf_assert("Invalid value \"1invalid\" of \"min-elements\". Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100722
723 flags = value = 0;
724 str = " -1; ...";
725 assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext));
726 logbuf_assert("Invalid value \"-1\" of \"min-elements\". Line number 1.");
727
Radek Krejcidf6cad12018-11-28 17:10:55 +0100728 /* implementation limit */
729 flags = value = 0;
730 str = " 4294967296; ...";
731 assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext));
732 logbuf_assert("Value \"4294967296\" is out of \"min-elements\" bounds. Line number 1.");
733
Radek Krejci05b13982018-11-28 16:22:07 +0100734 flags = value = 0;
735 str = " 1; ...";
736 assert_int_equal(LY_SUCCESS, parse_minelements(&ctx, &str, &value, &flags, &ext));
737 assert_int_equal(LYS_SET_MIN, flags);
738 assert_int_equal(1, value);
739
740 flags = value = 0;
741 str = " 1 {m:ext;} ...";
742 assert_int_equal(LY_SUCCESS, parse_minelements(&ctx, &str, &value, &flags, &ext));
743 assert_int_equal(LYS_SET_MIN, flags);
744 assert_int_equal(1, value);
745 assert_non_null(ext);
746 FREE_ARRAY(ctx.ctx, ext, lysp_ext_instance_free);
747 ext = NULL;
748
749 flags = value = 0;
750 str = " 1 {config true;} ...";
751 assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext));
752 logbuf_assert("Invalid keyword \"config\" as a child of \"min-elements\". Line number 1.");
753
Radek Krejcidf6cad12018-11-28 17:10:55 +0100754 str = " 1invalid; ...";
Radek Krejci05b13982018-11-28 16:22:07 +0100755 assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext));
Radek Krejcidf6cad12018-11-28 17:10:55 +0100756 logbuf_assert("Invalid value \"1invalid\" of \"max-elements\". Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100757
758 flags = value = 0;
759 str = " -1; ...";
760 assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext));
761 logbuf_assert("Invalid value \"-1\" of \"max-elements\". Line number 1.");
762
Radek Krejcidf6cad12018-11-28 17:10:55 +0100763 /* implementation limit */
764 flags = value = 0;
765 str = " 4294967296; ...";
766 assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext));
767 logbuf_assert("Value \"4294967296\" is out of \"max-elements\" bounds. Line number 1.");
768
Radek Krejci05b13982018-11-28 16:22:07 +0100769 flags = value = 0;
770 str = " 1; ...";
771 assert_int_equal(LY_SUCCESS, parse_maxelements(&ctx, &str, &value, &flags, &ext));
772 assert_int_equal(LYS_SET_MAX, flags);
773 assert_int_equal(1, value);
774
775 flags = value = 0;
776 str = " unbounded; ...";
777 assert_int_equal(LY_SUCCESS, parse_maxelements(&ctx, &str, &value, &flags, &ext));
778 assert_int_equal(LYS_SET_MAX, flags);
779 assert_int_equal(0, value);
780
781 flags = value = 0;
782 str = " 1 {m:ext;} ...";
783 assert_int_equal(LY_SUCCESS, parse_maxelements(&ctx, &str, &value, &flags, &ext));
784 assert_int_equal(LYS_SET_MAX, flags);
785 assert_int_equal(1, value);
786 assert_non_null(ext);
787 FREE_ARRAY(ctx.ctx, ext, lysp_ext_instance_free);
788 ext = NULL;
789
790 flags = value = 0;
791 str = " 1 {config true;} ...";
792 assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext));
793 logbuf_assert("Invalid keyword \"config\" as a child of \"max-elements\". Line number 1.");
794
795 *state = NULL;
796 ly_ctx_destroy(ctx.ctx, NULL);
797}
798
Radek Krejci9fcacc12018-10-11 15:59:11 +0200799static struct lysp_module *
Radek Krejcie7b95092019-05-15 11:03:07 +0200800mod_renew(struct lys_parser_ctx *ctx)
Radek Krejci9fcacc12018-10-11 15:59:11 +0200801{
Radek Krejci40544fa2019-01-11 09:38:37 +0100802 struct lysp_module *mod_p;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100803 static struct lys_module mod = {0};
804
805 lysc_module_free(mod.compiled, NULL);
806 lysp_module_free(mod.parsed);
807 FREE_STRING(mod.ctx, mod.name);
808 FREE_STRING(mod.ctx, mod.ns);
809 FREE_STRING(mod.ctx, mod.prefix);
810 FREE_STRING(mod.ctx, mod.filepath);
811 FREE_STRING(mod.ctx, mod.org);
812 FREE_STRING(mod.ctx, mod.contact);
813 FREE_STRING(mod.ctx, mod.dsc);
814 FREE_STRING(mod.ctx, mod.ref);
815 memset(&mod, 0, sizeof mod);
816 mod.ctx = ctx->ctx;
817
818 mod_p = calloc(1, sizeof *mod_p);
819 mod.parsed = mod_p;
820 mod_p->mod = &mod;
821 assert_non_null(mod_p);
822 return mod_p;
823}
824
825static struct lysp_submodule *
Radek Krejcie7b95092019-05-15 11:03:07 +0200826submod_renew(struct lys_parser_ctx *ctx, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100827{
828 lysp_submodule_free(ctx->ctx, submod);
829 submod = calloc(1, sizeof *submod);
830 assert_non_null(submod);
831 return submod;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200832}
833
Radek Krejcid33273d2018-10-25 14:55:52 +0200834static LY_ERR test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
835 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
836 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
837{
838 *module_data = user_data;
839 *format = LYS_IN_YANG;
840 *free_module_data = NULL;
841 return LY_SUCCESS;
842}
843
Radek Krejci9fcacc12018-10-11 15:59:11 +0200844static void
845test_module(void **state)
846{
Radek Krejci40544fa2019-01-11 09:38:37 +0100847 *state = test_module;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200848
Radek Krejcie7b95092019-05-15 11:03:07 +0200849 struct lys_parser_ctx ctx;
Radek Krejci40544fa2019-01-11 09:38:37 +0100850 struct lysp_module *mod = NULL;
851 struct lysp_submodule *submod = NULL;
852 struct lys_module *m;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200853 const char *str;
854
855 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
856 assert_non_null(ctx.ctx);
857 ctx.line = 1;
Radek Krejcia042ea12018-10-13 07:52:15 +0200858 ctx.indent = 0;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200859
Radek Krejci40544fa2019-01-11 09:38:37 +0100860 mod = mod_renew(&ctx);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200861
862 /* missing mandatory substatements */
863 str = " name {}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100864 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
865 assert_string_equal("name", mod->mod->name);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200866 logbuf_assert("Missing mandatory keyword \"namespace\" as a child of \"module\". Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100867 mod = mod_renew(&ctx);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200868
869 str = " name {namespace urn:x;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100870 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
871 assert_string_equal("urn:x", mod->mod->ns);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200872 logbuf_assert("Missing mandatory keyword \"prefix\" as a child of \"module\". Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100873 mod = mod_renew(&ctx);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200874
875 str = " name {namespace urn:x;prefix \"x\";}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100876 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod));
877 assert_string_equal("x", mod->mod->prefix);
Radek Krejci40544fa2019-01-11 09:38:37 +0100878 mod = mod_renew(&ctx);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200879
Radek Krejci027d5802018-11-14 16:57:28 +0100880#define SCHEMA_BEGINNING " name {yang-version 1.1;namespace urn:x;prefix \"x\";"
881#define SCHEMA_BEGINNING2 " name {namespace urn:x;prefix \"x\";"
Radek Krejcia042ea12018-10-13 07:52:15 +0200882#define TEST_NODE(NODETYPE, INPUT, NAME) \
883 str = SCHEMA_BEGINNING INPUT; \
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100884 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod)); \
Radek Krejcia042ea12018-10-13 07:52:15 +0200885 assert_non_null(mod->data); \
886 assert_int_equal(NODETYPE, mod->data->nodetype); \
887 assert_string_equal(NAME, mod->data->name); \
Radek Krejci40544fa2019-01-11 09:38:37 +0100888 mod = mod_renew(&ctx);
Radek Krejcia042ea12018-10-13 07:52:15 +0200889#define TEST_GENERIC(INPUT, TARGET, TEST) \
890 str = SCHEMA_BEGINNING INPUT; \
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100891 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod)); \
Radek Krejcia042ea12018-10-13 07:52:15 +0200892 assert_non_null(TARGET); \
893 TEST; \
Radek Krejci40544fa2019-01-11 09:38:37 +0100894 mod = mod_renew(&ctx);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100895#define TEST_DUP(MEMBER, VALUE1, VALUE2, LINE) \
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200896 TEST_DUP_GENERIC(SCHEMA_BEGINNING, MEMBER, VALUE1, VALUE2, \
Radek Krejci40544fa2019-01-11 09:38:37 +0100897 parse_module, mod, LINE, mod = mod_renew(&ctx))
Radek Krejcia042ea12018-10-13 07:52:15 +0200898
899 /* duplicated namespace, prefix */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100900 TEST_DUP("namespace", "y", "z", "1");
901 TEST_DUP("prefix", "y", "z", "1");
902 TEST_DUP("contact", "a", "b", "1");
903 TEST_DUP("description", "a", "b", "1");
904 TEST_DUP("organization", "a", "b", "1");
905 TEST_DUP("reference", "a", "b", "1");
Radek Krejcia042ea12018-10-13 07:52:15 +0200906
Radek Krejci70853c52018-10-15 14:46:16 +0200907 /* not allowed in module (submodule-specific) */
908 str = SCHEMA_BEGINNING "belongs-to master {prefix m;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100909 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejci70853c52018-10-15 14:46:16 +0200910 logbuf_assert("Invalid keyword \"belongs-to\" as a child of \"module\". Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100911 mod = mod_renew(&ctx);
Radek Krejci70853c52018-10-15 14:46:16 +0200912
Radek Krejcia042ea12018-10-13 07:52:15 +0200913 /* anydata */
914 TEST_NODE(LYS_ANYDATA, "anydata test;}", "test");
915 /* anyxml */
916 TEST_NODE(LYS_ANYXML, "anyxml test;}", "test");
917 /* augment */
918 TEST_GENERIC("augment /somepath;}", mod->augments,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200919 assert_string_equal("/somepath", mod->augments[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200920 /* choice */
921 TEST_NODE(LYS_CHOICE, "choice test;}", "test");
922 /* contact 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100923 TEST_GENERIC("contact \"firstname\" + \n\t\" surname\";}", mod->mod->contact,
924 assert_string_equal("firstname surname", mod->mod->contact));
Radek Krejcia042ea12018-10-13 07:52:15 +0200925 /* container */
926 TEST_NODE(LYS_CONTAINER, "container test;}", "test");
927 /* description 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100928 TEST_GENERIC("description \'some description\';}", mod->mod->dsc,
929 assert_string_equal("some description", mod->mod->dsc));
Radek Krejcia042ea12018-10-13 07:52:15 +0200930 /* deviation */
931 TEST_GENERIC("deviation /somepath {deviate not-supported;}}", mod->deviations,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200932 assert_string_equal("/somepath", mod->deviations[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200933 /* extension */
934 TEST_GENERIC("extension test;}", mod->extensions,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200935 assert_string_equal("test", mod->extensions[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200936 /* feature */
937 TEST_GENERIC("feature test;}", mod->features,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200938 assert_string_equal("test", mod->features[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200939 /* grouping */
940 TEST_GENERIC("grouping grp;}", mod->groupings,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200941 assert_string_equal("grp", mod->groupings[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200942 /* identity */
943 TEST_GENERIC("identity test;}", mod->identities,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200944 assert_string_equal("test", mod->identities[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200945 /* import */
Radek Krejci086c7132018-10-26 15:29:04 +0200946 ly_ctx_set_module_imp_clb(ctx.ctx, test_imp_clb, "module zzz { namespace urn:zzz; prefix z;}");
947 TEST_GENERIC("import zzz {prefix z;}}", mod->imports,
948 assert_string_equal("zzz", mod->imports[0].name));
Radek Krejci70853c52018-10-15 14:46:16 +0200949
Radek Krejcia042ea12018-10-13 07:52:15 +0200950 /* import - prefix collision */
Radek Krejci086c7132018-10-26 15:29:04 +0200951 str = SCHEMA_BEGINNING "import zzz {prefix x;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100952 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejci70853c52018-10-15 14:46:16 +0200953 logbuf_assert("Prefix \"x\" already used as module prefix. Line number 2.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100954 mod = mod_renew(&ctx);
Radek Krejci086c7132018-10-26 15:29:04 +0200955 str = SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix y;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100956 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejci086c7132018-10-26 15:29:04 +0200957 logbuf_assert("Prefix \"y\" already used to import \"zzz\" module. Line number 2.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100958 mod = mod_renew(&ctx);
Radek Krejci086c7132018-10-26 15:29:04 +0200959 str = "module" SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix z;}}";
960 assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
961 assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx));
962 logbuf_assert("Single revision of the module \"zzz\" referred twice.");
Radek Krejci70853c52018-10-15 14:46:16 +0200963
Radek Krejcia042ea12018-10-13 07:52:15 +0200964 /* include */
Radek Krejci9ed7a192018-10-31 16:23:51 +0100965 store = 1;
Radek Krejcid33273d2018-10-25 14:55:52 +0200966 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 +0200967 str = "module" SCHEMA_BEGINNING "include xxx;}";
968 assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
969 assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx));
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100970 logbuf_assert("Input data contains module in situation when a submodule is expected.");
Radek Krejci9ed7a192018-10-31 16:23:51 +0100971 store = -1;
Radek Krejcid33273d2018-10-25 14:55:52 +0200972
Radek Krejci9ed7a192018-10-31 16:23:51 +0100973 store = 1;
Radek Krejci313d9902018-11-08 09:42:58 +0100974 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 +0200975 str = "module" SCHEMA_BEGINNING "include xxx;}";
976 assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
977 assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx));
978 logbuf_assert("Included \"xxx\" submodule from \"name\" belongs-to a different module \"wrong-name\".");
Radek Krejci9ed7a192018-10-31 16:23:51 +0100979 store = -1;
Radek Krejcid33273d2018-10-25 14:55:52 +0200980
Radek Krejci313d9902018-11-08 09:42:58 +0100981 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 +0200982 TEST_GENERIC("include xxx;}", mod->includes,
Radek Krejci086c7132018-10-26 15:29:04 +0200983 assert_string_equal("xxx", mod->includes[0].name));
Radek Krejcid33273d2018-10-25 14:55:52 +0200984
Radek Krejcia042ea12018-10-13 07:52:15 +0200985 /* leaf */
986 TEST_NODE(LYS_LEAF, "leaf test {type string;}}", "test");
987 /* leaf-list */
988 TEST_NODE(LYS_LEAFLIST, "leaf-list test {type string;}}", "test");
989 /* list */
990 TEST_NODE(LYS_LIST, "list test {key a;leaf a {type string;}}}", "test");
991 /* notification */
992 TEST_GENERIC("notification test;}", mod->notifs,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200993 assert_string_equal("test", mod->notifs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200994 /* organization 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100995 TEST_GENERIC("organization \"CESNET a.l.e.\";}", mod->mod->org,
996 assert_string_equal("CESNET a.l.e.", mod->mod->org));
Radek Krejcia042ea12018-10-13 07:52:15 +0200997 /* reference 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100998 TEST_GENERIC("reference RFC7950;}", mod->mod->ref,
999 assert_string_equal("RFC7950", mod->mod->ref));
Radek Krejcia042ea12018-10-13 07:52:15 +02001000 /* revision */
1001 TEST_GENERIC("revision 2018-10-12;}", mod->revs,
Radek Krejcib7db73a2018-10-24 14:18:40 +02001002 assert_string_equal("2018-10-12", mod->revs[0].date));
Radek Krejcia042ea12018-10-13 07:52:15 +02001003 /* rpc */
1004 TEST_GENERIC("rpc test;}", mod->rpcs,
Radek Krejci2c4e7172018-10-19 15:56:26 +02001005 assert_string_equal("test", mod->rpcs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +02001006 /* typedef */
1007 TEST_GENERIC("typedef test{type string;}}", mod->typedefs,
Radek Krejci2c4e7172018-10-19 15:56:26 +02001008 assert_string_equal("test", mod->typedefs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +02001009 /* uses */
1010 TEST_NODE(LYS_USES, "uses test;}", "test");
1011 /* yang-version */
Radek Krejci027d5802018-11-14 16:57:28 +01001012 str = SCHEMA_BEGINNING2 "\n\tyang-version 10;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001013 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejcia042ea12018-10-13 07:52:15 +02001014 logbuf_assert("Invalid value \"10\" of \"yang-version\". Line number 3.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001015 mod = mod_renew(&ctx);
Radek Krejci027d5802018-11-14 16:57:28 +01001016 str = SCHEMA_BEGINNING2 "yang-version 1.0;yang-version 1.1;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001017 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejcia042ea12018-10-13 07:52:15 +02001018 logbuf_assert("Duplicate keyword \"yang-version\". Line number 3.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001019 mod = mod_renew(&ctx);
Radek Krejci027d5802018-11-14 16:57:28 +01001020 str = SCHEMA_BEGINNING2 "yang-version 1.0;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001021 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod));
1022 assert_int_equal(1, mod->mod->version);
Radek Krejci40544fa2019-01-11 09:38:37 +01001023 mod = mod_renew(&ctx);
Radek Krejci027d5802018-11-14 16:57:28 +01001024 str = SCHEMA_BEGINNING2 "yang-version \"1.1\";}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001025 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod));
1026 assert_int_equal(2, mod->mod->version);
Radek Krejci40544fa2019-01-11 09:38:37 +01001027 mod = mod_renew(&ctx);
1028
1029 str = "module " SCHEMA_BEGINNING "} module q {namespace urn:q;prefixq;}";
1030 m = mod->mod;
1031 free(mod);
1032 m->parsed = NULL;
1033 assert_int_equal(LY_EVALID, yang_parse_module(&ctx, str, m));
Radek Krejci0a1d0d42019-05-16 15:14:51 +02001034 logbuf_assert("Trailing garbage \"module q {names...\" after module, expected end-of-input. Line number 3.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001035 mod = mod_renew(&ctx);
1036
1037 str = "prefix " SCHEMA_BEGINNING "}";
1038 m = mod->mod;
1039 free(mod);
1040 m->parsed = NULL;
1041 assert_int_equal(LY_EVALID, yang_parse_module(&ctx, str, m));
1042 logbuf_assert("Invalid keyword \"prefix\", expected \"module\" or \"submodule\". Line number 3.");
1043 mod = mod_renew(&ctx);
Radek Krejci09306362018-10-15 15:26:01 +02001044
Radek Krejci156ccaf2018-10-15 15:49:17 +02001045 /* extensions */
1046 TEST_GENERIC("prefix:test;}", mod->exts,
Radek Krejci2c4e7172018-10-19 15:56:26 +02001047 assert_string_equal("prefix:test", mod->exts[0].name);
1048 assert_int_equal(LYEXT_SUBSTMT_SELF, mod->exts[0].insubstmt));
Radek Krejci40544fa2019-01-11 09:38:37 +01001049 mod = mod_renew(&ctx);
Radek Krejci156ccaf2018-10-15 15:49:17 +02001050
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001051 /* invalid substatement */
1052 str = SCHEMA_BEGINNING "must false;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001053 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001054 logbuf_assert("Invalid keyword \"must\" as a child of \"module\". Line number 3.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001055 mod = mod_renew(&ctx);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001056
Radek Krejci09306362018-10-15 15:26:01 +02001057 /* submodule */
Radek Krejci40544fa2019-01-11 09:38:37 +01001058 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001059
1060 /* missing mandatory substatements */
1061 str = " subname {}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001062 lydict_remove(ctx.ctx, submod->name);
1063 assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod));
1064 assert_string_equal("subname", submod->name);
Radek Krejci09306362018-10-15 15:26:01 +02001065 logbuf_assert("Missing mandatory keyword \"belongs-to\" as a child of \"submodule\". Line number 3.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001066 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001067
Radek Krejci313d9902018-11-08 09:42:58 +01001068 str = " subname {belongs-to name {prefix x;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001069 lydict_remove(ctx.ctx, submod->name);
1070 assert_int_equal(LY_SUCCESS, parse_submodule(&ctx, &str, submod));
1071 assert_string_equal("name", submod->belongsto);
1072 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001073
1074#undef SCHEMA_BEGINNING
Radek Krejci313d9902018-11-08 09:42:58 +01001075#define SCHEMA_BEGINNING " subname {belongs-to name {prefix x;}"
Radek Krejci09306362018-10-15 15:26:01 +02001076
1077 /* duplicated namespace, prefix */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001078 str = " subname {belongs-to name {prefix x;}belongs-to module1;belongs-to module2;} ...";
1079 assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod)); \
1080 logbuf_assert("Duplicate keyword \"belongs-to\". Line number 3."); \
1081 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001082
1083 /* not allowed in submodule (module-specific) */
1084 str = SCHEMA_BEGINNING "namespace \"urn:z\";}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001085 assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod));
Radek Krejci09306362018-10-15 15:26:01 +02001086 logbuf_assert("Invalid keyword \"namespace\" as a child of \"submodule\". Line number 3.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001087 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001088 str = SCHEMA_BEGINNING "prefix m;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001089 assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod));
Radek Krejci09306362018-10-15 15:26:01 +02001090 logbuf_assert("Invalid keyword \"prefix\" as a child of \"submodule\". Line number 3.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001091 submod = submod_renew(&ctx, submod);
Radek Krejcia042ea12018-10-13 07:52:15 +02001092
Radek Krejci40544fa2019-01-11 09:38:37 +01001093 str = "submodule " SCHEMA_BEGINNING "} module q {namespace urn:q;prefixq;}";
1094 lysp_submodule_free(ctx.ctx, submod);
1095 submod = NULL;
1096 assert_int_equal(LY_EVALID, yang_parse_submodule(&ctx, str, &submod));
Radek Krejci0a1d0d42019-05-16 15:14:51 +02001097 logbuf_assert("Trailing garbage \"module q {names...\" after submodule, expected end-of-input. Line number 3.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001098
1099 str = "prefix " SCHEMA_BEGINNING "}";
1100 assert_int_equal(LY_EVALID, yang_parse_submodule(&ctx, str, &submod));
1101 logbuf_assert("Invalid keyword \"prefix\", expected \"module\" or \"submodule\". Line number 3.");
1102 submod = submod_renew(&ctx, submod);
1103
Radek Krejcia042ea12018-10-13 07:52:15 +02001104#undef TEST_GENERIC
1105#undef TEST_NODE
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001106#undef TEST_DUP
Radek Krejcia042ea12018-10-13 07:52:15 +02001107#undef SCHEMA_BEGINNING
1108
Radek Krejci9fcacc12018-10-11 15:59:11 +02001109 lysp_module_free(mod);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001110 lysp_submodule_free(ctx.ctx, submod);
Radek Krejci9fcacc12018-10-11 15:59:11 +02001111 ly_ctx_destroy(ctx.ctx, NULL);
Radek Krejci40544fa2019-01-11 09:38:37 +01001112
1113 *state = NULL;
Radek Krejciefd22f62018-09-27 11:47:58 +02001114}
1115
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001116static void
1117test_identity(void **state)
1118{
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001119 *state = test_identity;
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001120
Radek Krejcie7b95092019-05-15 11:03:07 +02001121 struct lys_parser_ctx ctx;
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001122 struct lysp_ident *ident = NULL;
1123 const char *str;
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001124
1125 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1126 assert_non_null(ctx.ctx);
1127 ctx.line = 1;
1128 ctx.indent = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001129 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001130
1131 /* invalid cardinality */
1132#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001133 TEST_DUP_GENERIC(" test {", MEMBER, VALUE1, VALUE2, parse_identity, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001134 &ident, "1", FREE_ARRAY(ctx.ctx, ident, lysp_ident_free); ident = NULL)
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001135
Radek Krejci38222632019-02-12 16:55:05 +01001136 TEST_DUP("description", "a", "b");
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001137 TEST_DUP("reference", "a", "b");
1138 TEST_DUP("status", "current", "obsolete");
1139
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001140 /* full content */
1141 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 +02001142 assert_int_equal(LY_SUCCESS, parse_identity(&ctx, &str, &ident));
1143 assert_non_null(ident);
1144 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001145 FREE_ARRAY(ctx.ctx, ident, lysp_ident_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001146 ident = NULL;
1147
1148 /* invalid substatement */
1149 str = " test {organization XXX;}";
1150 assert_int_equal(LY_EVALID, parse_identity(&ctx, &str, &ident));
1151 logbuf_assert("Invalid keyword \"organization\" as a child of \"identity\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001152 FREE_ARRAY(ctx.ctx, ident, lysp_ident_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001153 ident = NULL;
1154
1155#undef TEST_DUP
1156
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001157 *state = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001158 ly_ctx_destroy(ctx.ctx, NULL);
1159}
1160
1161static void
1162test_feature(void **state)
1163{
1164 (void) state; /* unused */
1165
Radek Krejcie7b95092019-05-15 11:03:07 +02001166 struct lys_parser_ctx ctx;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001167 struct lysp_feature *features = NULL;
1168 const char *str;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001169
1170 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1171 assert_non_null(ctx.ctx);
1172 ctx.line = 1;
1173 ctx.indent = 0;
1174
1175 /* invalid cardinality */
1176#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1177 TEST_DUP_GENERIC(" test {", MEMBER, VALUE1, VALUE2, parse_feature, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001178 &features, "1", FREE_ARRAY(ctx.ctx, features, lysp_feature_free); features = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001179
1180 TEST_DUP("description", "a", "b");
1181 TEST_DUP("reference", "a", "b");
1182 TEST_DUP("status", "current", "obsolete");
1183
1184 /* full content */
1185 str = " test {description text;reference \'another text\';status current; if-feature x;if-feature y;prefix:ext;} ...";
1186 assert_int_equal(LY_SUCCESS, parse_feature(&ctx, &str, &features));
1187 assert_non_null(features);
1188 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001189 FREE_ARRAY(ctx.ctx, features, lysp_feature_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001190 features = NULL;
1191
1192 /* invalid substatement */
1193 str = " test {organization XXX;}";
1194 assert_int_equal(LY_EVALID, parse_feature(&ctx, &str, &features));
1195 logbuf_assert("Invalid keyword \"organization\" as a child of \"feature\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001196 FREE_ARRAY(ctx.ctx, features, lysp_feature_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001197 features = NULL;
1198
1199#undef TEST_DUP
1200
1201 ly_ctx_destroy(ctx.ctx, NULL);
1202}
1203
1204static void
1205test_deviation(void **state)
1206{
1207 (void) state; /* unused */
1208
Radek Krejcie7b95092019-05-15 11:03:07 +02001209 struct lys_parser_ctx ctx;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001210 struct lysp_deviation *d = NULL;
1211 const char *str;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001212
1213 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1214 assert_non_null(ctx.ctx);
1215 ctx.line = 1;
1216 ctx.indent = 0;
1217
1218 /* invalid cardinality */
1219#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1220 TEST_DUP_GENERIC(" test {deviate not-supported;", MEMBER, VALUE1, VALUE2, parse_deviation, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001221 &d, "1", FREE_ARRAY(ctx.ctx, d, lysp_deviation_free); d = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001222
1223 TEST_DUP("description", "a", "b");
1224 TEST_DUP("reference", "a", "b");
1225
1226 /* full content */
1227 str = " test {deviate not-supported;description text;reference \'another text\';prefix:ext;} ...";
1228 assert_int_equal(LY_SUCCESS, parse_deviation(&ctx, &str, &d));
1229 assert_non_null(d);
1230 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001231 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001232 d = NULL;
1233
1234 /* missing mandatory substatement */
1235 str = " test {description text;}";
1236 assert_int_equal(LY_EVALID, parse_deviation(&ctx, &str, &d));
1237 logbuf_assert("Missing mandatory keyword \"deviate\" as a child of \"deviation\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001238 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001239 d = NULL;
1240
1241 /* invalid substatement */
1242 str = " test {deviate not-supported; status obsolete;}";
1243 assert_int_equal(LY_EVALID, parse_deviation(&ctx, &str, &d));
1244 logbuf_assert("Invalid keyword \"status\" as a child of \"deviation\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001245 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001246 d = NULL;
1247
1248#undef TEST_DUP
1249
1250 ly_ctx_destroy(ctx.ctx, NULL);
1251}
1252
1253static void
1254test_deviate(void **state)
1255{
1256 (void) state; /* unused */
1257
Radek Krejcie7b95092019-05-15 11:03:07 +02001258 struct lys_parser_ctx ctx;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001259 struct lysp_deviate *d = NULL;
1260 const char *str;
1261
1262 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1263 assert_non_null(ctx.ctx);
1264 ctx.line = 1;
1265 ctx.indent = 0;
1266
1267 /* invalid cardinality */
1268#define TEST_DUP(TYPE, MEMBER, VALUE1, VALUE2) \
1269 TEST_DUP_GENERIC(TYPE" {", MEMBER, VALUE1, VALUE2, parse_deviate, \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001270 &d, "1", lysp_deviate_free(ctx.ctx, d); free(d); d = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001271
1272 TEST_DUP("add", "config", "true", "false");
1273 TEST_DUP("replace", "default", "int8", "uint8");
1274 TEST_DUP("add", "mandatory", "true", "false");
1275 TEST_DUP("add", "max-elements", "1", "2");
1276 TEST_DUP("add", "min-elements", "1", "2");
1277 TEST_DUP("replace", "type", "int8", "uint8");
1278 TEST_DUP("add", "units", "kilometers", "miles");
1279
1280 /* full contents */
1281 str = " not-supported {prefix:ext;} ...";
1282 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1283 assert_non_null(d);
1284 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001285 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001286 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;} ...";
1287 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1288 assert_non_null(d);
1289 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001290 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001291 str = " delete {units meters; must 1; must 2; unique x; unique y; default a; default b; prefix:ext;} ...";
1292 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1293 assert_non_null(d);
1294 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001295 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001296 str = " replace {type string; units meters; default a; config true; mandatory true; min-elements 1; max-elements 2; prefix:ext;} ...";
1297 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1298 assert_non_null(d);
1299 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001300 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001301
1302 /* invalid substatements */
1303#define TEST_NOT_SUP(DEV, STMT, VALUE) \
1304 str = " "DEV" {"STMT" "VALUE";}..."; \
1305 assert_int_equal(LY_EVALID, parse_deviate(&ctx, &str, &d)); \
1306 logbuf_assert("Deviate \""DEV"\" does not support keyword \""STMT"\". Line number 1."); \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001307 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001308
1309 TEST_NOT_SUP("not-supported", "units", "meters");
1310 TEST_NOT_SUP("not-supported", "must", "1");
1311 TEST_NOT_SUP("not-supported", "unique", "x");
1312 TEST_NOT_SUP("not-supported", "default", "a");
1313 TEST_NOT_SUP("not-supported", "config", "true");
1314 TEST_NOT_SUP("not-supported", "mandatory", "true");
1315 TEST_NOT_SUP("not-supported", "min-elements", "1");
1316 TEST_NOT_SUP("not-supported", "max-elements", "2");
1317 TEST_NOT_SUP("not-supported", "type", "string");
1318 TEST_NOT_SUP("add", "type", "string");
1319 TEST_NOT_SUP("delete", "config", "true");
1320 TEST_NOT_SUP("delete", "mandatory", "true");
1321 TEST_NOT_SUP("delete", "min-elements", "1");
1322 TEST_NOT_SUP("delete", "max-elements", "2");
1323 TEST_NOT_SUP("delete", "type", "string");
1324 TEST_NOT_SUP("replace", "must", "1");
1325 TEST_NOT_SUP("replace", "unique", "a");
1326
1327 str = " nonsence; ...";
1328 assert_int_equal(LY_EVALID, parse_deviate(&ctx, &str, &d));
1329 logbuf_assert("Invalid value \"nonsence\" of \"deviate\". Line number 1.");
1330 assert_null(d);
1331
1332#undef TEST_NOT_SUP
1333#undef TEST_DUP
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001334
1335 ly_ctx_destroy(ctx.ctx, NULL);
1336}
1337
Radek Krejci8c370832018-11-02 15:10:03 +01001338static void
1339test_container(void **state)
1340{
1341 (void) state; /* unused */
1342
Radek Krejcie7b95092019-05-15 11:03:07 +02001343 struct lys_parser_ctx ctx = {0};
Radek Krejci8c370832018-11-02 15:10:03 +01001344 struct lysp_node_container *c = NULL;
1345 const char *str;
1346
1347 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1348 assert_non_null(ctx.ctx);
1349 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001350 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci8c370832018-11-02 15:10:03 +01001351
1352 /* invalid cardinality */
1353#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1354 str = "cont {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1355 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c)); \
1356 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001357 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001358
1359 TEST_DUP("config", "true", "false");
1360 TEST_DUP("description", "text1", "text2");
1361 TEST_DUP("presence", "true", "false");
1362 TEST_DUP("reference", "1", "2");
1363 TEST_DUP("status", "current", "obsolete");
1364 TEST_DUP("when", "true", "false");
1365#undef TEST_DUP
1366
1367 /* full content */
Radek Krejci313d9902018-11-08 09:42:58 +01001368 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;}"
1369 "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 +01001370 assert_int_equal(LY_SUCCESS, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1371 assert_non_null(c);
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001372 assert_int_equal(LYS_CONTAINER, c->nodetype);
1373 assert_string_equal("cont", c->name);
Radek Krejci8c370832018-11-02 15:10:03 +01001374 assert_non_null(c->actions);
1375 assert_non_null(c->child);
1376 assert_string_equal("test", c->dsc);
1377 assert_non_null(c->exts);
1378 assert_non_null(c->groupings);
1379 assert_non_null(c->iffeatures);
1380 assert_non_null(c->musts);
1381 assert_non_null(c->notifs);
1382 assert_string_equal("true", c->presence);
1383 assert_string_equal("test", c->ref);
1384 assert_non_null(c->typedefs);
1385 assert_non_null(c->when);
1386 assert_null(c->parent);
1387 assert_null(c->next);
1388 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR, c->flags);
Radek Krejci313d9902018-11-08 09:42:58 +01001389 ly_set_erase(&ctx.tpdfs_nodes, NULL);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001390 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001391
1392 /* invalid */
1393 str = " cont {augment /root;} ...";
1394 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1395 logbuf_assert("Invalid keyword \"augment\" as a child of \"container\". Line number 1.");
Radek Krejci4f28eda2018-11-12 11:46:16 +01001396 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001397 str = " cont {nonsence true;} ...";
1398 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1399 logbuf_assert("Invalid character sequence \"nonsence\", expected a keyword. Line number 1.");
Radek Krejci4f28eda2018-11-12 11:46:16 +01001400 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001401
Radek Krejcif538ce52019-03-05 10:46:14 +01001402 ctx.mod_version = 1; /* simulate YANG 1.0 */
1403 str = " cont {action x;} ...";
1404 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1405 logbuf_assert("Invalid keyword \"action\" as a child of \"container\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
1406 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
1407
Radek Krejci8c370832018-11-02 15:10:03 +01001408 ly_ctx_destroy(ctx.ctx, NULL);
1409}
1410
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001411static void
1412test_leaf(void **state)
1413{
1414 *state = test_leaf;
1415
Radek Krejcie7b95092019-05-15 11:03:07 +02001416 struct lys_parser_ctx ctx = {0};
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001417 struct lysp_node_leaf *l = NULL;
1418 const char *str;
1419
1420 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1421 assert_non_null(ctx.ctx);
1422 ctx.line = 1;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001423 //ctx.mod->version = 2; /* simulate YANG 1.1 */
1424
1425 /* invalid cardinality */
1426#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1427 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1428 assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l)); \
1429 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1430 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1431
1432 TEST_DUP("config", "true", "false");
1433 TEST_DUP("default", "x", "y");
1434 TEST_DUP("description", "text1", "text2");
1435 TEST_DUP("mandatory", "true", "false");
1436 TEST_DUP("reference", "1", "2");
1437 TEST_DUP("status", "current", "obsolete");
Radek Krejci0e5d8382018-11-28 16:37:53 +01001438 TEST_DUP("type", "int8", "uint8");
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001439 TEST_DUP("units", "text1", "text2");
1440 TEST_DUP("when", "true", "false");
1441#undef TEST_DUP
1442
1443 /* full content - without mandatory which is mutual exclusive with default */
1444 str = "l {config false;default \"xxx\";description test;if-feature f;"
1445 "must 'expr';reference test;status current;type string; units yyy;when true;m:ext;} ...";
1446 assert_int_equal(LY_SUCCESS, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
1447 assert_non_null(l);
1448 assert_int_equal(LYS_LEAF, l->nodetype);
1449 assert_string_equal("l", l->name);
1450 assert_string_equal("test", l->dsc);
1451 assert_string_equal("xxx", l->dflt);
1452 assert_string_equal("yyy", l->units);
1453 assert_string_equal("string", l->type.name);
1454 assert_non_null(l->exts);
1455 assert_non_null(l->iffeatures);
1456 assert_non_null(l->musts);
1457 assert_string_equal("test", l->ref);
1458 assert_non_null(l->when);
1459 assert_null(l->parent);
1460 assert_null(l->next);
1461 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR, l->flags);
1462 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1463
1464 /* full content - now with mandatory */
1465 str = "l {mandatory true; type string;} ...";
1466 assert_int_equal(LY_SUCCESS, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
1467 assert_non_null(l);
1468 assert_int_equal(LYS_LEAF, l->nodetype);
1469 assert_string_equal("l", l->name);
1470 assert_string_equal("string", l->type.name);
1471 assert_int_equal(LYS_MAND_TRUE, l->flags);
1472 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1473
1474 /* invalid */
1475 str = " l {mandatory true; default xx; type string;} ...";
1476 assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
Radek Krejcia9026eb2018-12-12 16:04:47 +01001477 logbuf_assert("Invalid combination of keywords \"mandatory\" and \"default\" as substatements of \"leaf\". Line number 1.");
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001478 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1479
1480 str = " l {description \"missing type\";} ...";
1481 assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
1482 logbuf_assert("Missing mandatory keyword \"type\" as a child of \"leaf\". Line number 1.");
1483 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1484
1485 *state = NULL;
1486 ly_ctx_destroy(ctx.ctx, NULL);
1487}
1488
Radek Krejci0e5d8382018-11-28 16:37:53 +01001489static void
1490test_leaflist(void **state)
1491{
1492 *state = test_leaf;
1493
Radek Krejcie7b95092019-05-15 11:03:07 +02001494 struct lys_parser_ctx ctx = {0};
Radek Krejci0e5d8382018-11-28 16:37:53 +01001495 struct lysp_node_leaflist *ll = NULL;
1496 const char *str;
1497
1498 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1499 assert_non_null(ctx.ctx);
1500 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001501 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci0e5d8382018-11-28 16:37:53 +01001502
1503 /* invalid cardinality */
1504#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1505 str = "ll {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1506 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll)); \
1507 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1508 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1509
1510 TEST_DUP("config", "true", "false");
1511 TEST_DUP("description", "text1", "text2");
1512 TEST_DUP("max-elements", "10", "20");
1513 TEST_DUP("min-elements", "10", "20");
1514 TEST_DUP("ordered-by", "user", "system");
1515 TEST_DUP("reference", "1", "2");
1516 TEST_DUP("status", "current", "obsolete");
1517 TEST_DUP("type", "int8", "uint8");
1518 TEST_DUP("units", "text1", "text2");
1519 TEST_DUP("when", "true", "false");
1520#undef TEST_DUP
1521
1522 /* full content - without min-elements which is mutual exclusive with default */
1523 str = "ll {config false;default \"xxx\"; default \"yyy\";description test;if-feature f;"
1524 "max-elements 10;must 'expr';ordered-by user;reference test;"
1525 "status current;type string; units zzz;when true;m:ext;} ...";
1526 assert_int_equal(LY_SUCCESS, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1527 assert_non_null(ll);
1528 assert_int_equal(LYS_LEAFLIST, ll->nodetype);
1529 assert_string_equal("ll", ll->name);
1530 assert_string_equal("test", ll->dsc);
1531 assert_non_null(ll->dflts);
1532 assert_int_equal(2, LY_ARRAY_SIZE(ll->dflts));
1533 assert_string_equal("xxx", ll->dflts[0]);
1534 assert_string_equal("yyy", ll->dflts[1]);
1535 assert_string_equal("zzz", ll->units);
1536 assert_int_equal(10, ll->max);
1537 assert_int_equal(0, ll->min);
1538 assert_string_equal("string", ll->type.name);
1539 assert_non_null(ll->exts);
1540 assert_non_null(ll->iffeatures);
1541 assert_non_null(ll->musts);
1542 assert_string_equal("test", ll->ref);
1543 assert_non_null(ll->when);
1544 assert_null(ll->parent);
1545 assert_null(ll->next);
1546 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_USER | LYS_SET_MAX, ll->flags);
1547 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1548
1549 /* full content - now with min-elements */
1550 str = "ll {min-elements 10; type string;} ...";
1551 assert_int_equal(LY_SUCCESS, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1552 assert_non_null(ll);
1553 assert_int_equal(LYS_LEAFLIST, ll->nodetype);
1554 assert_string_equal("ll", ll->name);
1555 assert_string_equal("string", ll->type.name);
1556 assert_int_equal(0, ll->max);
1557 assert_int_equal(10, ll->min);
1558 assert_int_equal(LYS_SET_MIN, ll->flags);
1559 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1560
1561 /* invalid */
1562 str = " ll {min-elements 1; default xx; type string;} ...";
1563 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
Radek Krejcia9026eb2018-12-12 16:04:47 +01001564 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 +01001565 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1566
1567 str = " ll {description \"missing type\";} ...";
1568 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1569 logbuf_assert("Missing mandatory keyword \"type\" as a child of \"leaf-list\". Line number 1.");
1570 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1571
Radek Krejcidf6cad12018-11-28 17:10:55 +01001572 str = " ll {type string; min-elements 10; max-elements 1;} ..."; /* invalid combination of min/max */
1573 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1574 logbuf_assert("Invalid combination of min-elements and max-elements: min value 10 is bigger than the max value 1. Line number 1.");
1575 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1576
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001577 ctx.mod_version = 1; /* simulate YANG 1.0 - default statement is not allowed */
Radek Krejci0e5d8382018-11-28 16:37:53 +01001578 str = " ll {default xx; type string;} ...";
1579 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1580 logbuf_assert("Invalid keyword \"default\" as a child of \"leaf-list\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
1581 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1582
1583 *state = NULL;
1584 ly_ctx_destroy(ctx.ctx, NULL);
1585}
1586
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001587static void
1588test_list(void **state)
1589{
1590 *state = test_list;
1591
Radek Krejcie7b95092019-05-15 11:03:07 +02001592 struct lys_parser_ctx ctx = {0};
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001593 struct lysp_node_list *l = NULL;
1594 const char *str;
1595
1596 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1597 assert_non_null(ctx.ctx);
1598 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001599 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001600
1601 /* invalid cardinality */
1602#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1603 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1604 assert_int_equal(LY_EVALID, parse_list(&ctx, &str, NULL, (struct lysp_node**)&l)); \
1605 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1606 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1607
1608 TEST_DUP("config", "true", "false");
1609 TEST_DUP("description", "text1", "text2");
1610 TEST_DUP("key", "one", "two");
1611 TEST_DUP("max-elements", "10", "20");
1612 TEST_DUP("min-elements", "10", "20");
1613 TEST_DUP("ordered-by", "user", "system");
1614 TEST_DUP("reference", "1", "2");
1615 TEST_DUP("status", "current", "obsolete");
1616 TEST_DUP("when", "true", "false");
1617#undef TEST_DUP
1618
1619 /* full content */
1620 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;}"
1621 "leaf-list ll {type string;} list li;max-elements 10; min-elements 1;must 'expr';notification not; ordered-by system; reference test;"
1622 "status current;typedef t {type int8;}unique xxx;unique yyy;uses g;when true;m:ext;} ...";
1623 assert_int_equal(LY_SUCCESS, parse_list(&ctx, &str, NULL, (struct lysp_node**)&l));
1624 assert_non_null(l);
1625 assert_int_equal(LYS_LIST, l->nodetype);
1626 assert_string_equal("l", l->name);
1627 assert_string_equal("test", l->dsc);
1628 assert_string_equal("l", l->key);
1629 assert_non_null(l->uniques);
1630 assert_int_equal(2, LY_ARRAY_SIZE(l->uniques));
1631 assert_string_equal("xxx", l->uniques[0]);
1632 assert_string_equal("yyy", l->uniques[1]);
1633 assert_int_equal(10, l->max);
1634 assert_int_equal(1, l->min);
1635 assert_non_null(l->exts);
1636 assert_non_null(l->iffeatures);
1637 assert_non_null(l->musts);
1638 assert_string_equal("test", l->ref);
1639 assert_non_null(l->when);
1640 assert_null(l->parent);
1641 assert_null(l->next);
1642 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM | LYS_SET_MAX | LYS_SET_MIN, l->flags);
1643 ly_set_erase(&ctx.tpdfs_nodes, NULL);
1644 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1645
Radek Krejcif538ce52019-03-05 10:46:14 +01001646 /* invalid content */
1647 ctx.mod_version = 1; /* simulate YANG 1.0 */
1648 str = "l {action x;} ...";
1649 assert_int_equal(LY_EVALID, parse_list(&ctx, &str, NULL, (struct lysp_node**)&l));
1650 logbuf_assert("Invalid keyword \"action\" as a child of \"list\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
1651 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1652
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001653 *state = NULL;
1654 ly_ctx_destroy(ctx.ctx, NULL);
1655}
1656
Radek Krejci056d0a82018-12-06 16:57:25 +01001657static void
1658test_choice(void **state)
1659{
1660 *state = test_choice;
1661
Radek Krejcie7b95092019-05-15 11:03:07 +02001662 struct lys_parser_ctx ctx = {0};
Radek Krejci056d0a82018-12-06 16:57:25 +01001663 struct lysp_node_choice *ch = NULL;
1664 const char *str;
1665
1666 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1667 assert_non_null(ctx.ctx);
1668 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001669 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci056d0a82018-12-06 16:57:25 +01001670
1671 /* invalid cardinality */
1672#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1673 str = "ch {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1674 assert_int_equal(LY_EVALID, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch)); \
1675 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1676 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1677
1678 TEST_DUP("config", "true", "false");
1679 TEST_DUP("default", "a", "b");
1680 TEST_DUP("description", "text1", "text2");
1681 TEST_DUP("mandatory", "true", "false");
1682 TEST_DUP("reference", "1", "2");
1683 TEST_DUP("status", "current", "obsolete");
1684 TEST_DUP("when", "true", "false");
1685#undef TEST_DUP
1686
Radek Krejcia9026eb2018-12-12 16:04:47 +01001687 /* full content - without default due to a collision with mandatory */
1688 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 +01001689 "leaf-list ll {type string;} list li;mandatory true;reference test;status current;when true;m:ext;} ...";
1690 assert_int_equal(LY_SUCCESS, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch));
1691 assert_non_null(ch);
1692 assert_int_equal(LYS_CHOICE, ch->nodetype);
1693 assert_string_equal("ch", ch->name);
1694 assert_string_equal("test", ch->dsc);
1695 assert_non_null(ch->exts);
1696 assert_non_null(ch->iffeatures);
1697 assert_string_equal("test", ch->ref);
1698 assert_non_null(ch->when);
1699 assert_null(ch->parent);
1700 assert_null(ch->next);
1701 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_MAND_TRUE, ch->flags);
1702 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1703
Radek Krejcia9026eb2018-12-12 16:04:47 +01001704 /* full content - the default missing from the previous node */
1705 str = "ch {default c;case c;} ...";
1706 assert_int_equal(LY_SUCCESS, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch));
1707 assert_non_null(ch);
1708 assert_int_equal(LYS_CHOICE, ch->nodetype);
1709 assert_string_equal("ch", ch->name);
1710 assert_string_equal("c", ch->dflt);
1711 assert_int_equal(0, ch->flags);
1712 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1713
1714 /* invalid content */
1715 str = "ch {mandatory true; default c1; case c1 {leaf x{type string;}}} ...";
1716 assert_int_equal(LY_EVALID, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch));
1717 logbuf_assert("Invalid combination of keywords \"mandatory\" and \"default\" as substatements of \"choice\". Line number 1.");
1718 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1719
1720 *state = NULL;
1721 ly_ctx_destroy(ctx.ctx, NULL);
1722}
1723
1724static void
1725test_case(void **state)
1726{
1727 *state = test_case;
1728
Radek Krejcie7b95092019-05-15 11:03:07 +02001729 struct lys_parser_ctx ctx = {0};
Radek Krejcia9026eb2018-12-12 16:04:47 +01001730 struct lysp_node_case *cs = NULL;
1731 const char *str;
1732
1733 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1734 assert_non_null(ctx.ctx);
1735 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001736 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejcia9026eb2018-12-12 16:04:47 +01001737
1738 /* invalid cardinality */
1739#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1740 str = "cs {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1741 assert_int_equal(LY_EVALID, parse_case(&ctx, &str, NULL, (struct lysp_node**)&cs)); \
1742 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1743 lysp_node_free(ctx.ctx, (struct lysp_node*)cs); cs = NULL;
1744
1745 TEST_DUP("description", "text1", "text2");
1746 TEST_DUP("reference", "1", "2");
1747 TEST_DUP("status", "current", "obsolete");
1748 TEST_DUP("when", "true", "false");
1749#undef TEST_DUP
1750
1751 /* full content */
1752 str = "cs {anydata any;anyxml anyxml; choice ch;container c;description test;if-feature f;leaf l {type string;}"
1753 "leaf-list ll {type string;} list li;reference test;status current;uses grp;when true;m:ext;} ...";
1754 assert_int_equal(LY_SUCCESS, parse_case(&ctx, &str, NULL, (struct lysp_node**)&cs));
1755 assert_non_null(cs);
1756 assert_int_equal(LYS_CASE, cs->nodetype);
1757 assert_string_equal("cs", cs->name);
1758 assert_string_equal("test", cs->dsc);
1759 assert_non_null(cs->exts);
1760 assert_non_null(cs->iffeatures);
1761 assert_string_equal("test", cs->ref);
1762 assert_non_null(cs->when);
1763 assert_null(cs->parent);
1764 assert_null(cs->next);
1765 assert_int_equal(LYS_STATUS_CURR, cs->flags);
1766 lysp_node_free(ctx.ctx, (struct lysp_node*)cs); cs = NULL;
1767
1768 /* invalid content */
1769 str = "cs {config true} ...";
1770 assert_int_equal(LY_EVALID, parse_case(&ctx, &str, NULL, (struct lysp_node**)&cs));
1771 logbuf_assert("Invalid keyword \"config\" as a child of \"case\". Line number 1.");
1772 lysp_node_free(ctx.ctx, (struct lysp_node*)cs); cs = NULL;
1773
Radek Krejci056d0a82018-12-06 16:57:25 +01001774 *state = NULL;
1775 ly_ctx_destroy(ctx.ctx, NULL);
1776}
1777
Radek Krejci9800fb82018-12-13 14:26:23 +01001778static void
1779test_any(void **state, enum yang_keyword kw)
1780{
1781 *state = test_any;
1782
Radek Krejcie7b95092019-05-15 11:03:07 +02001783 struct lys_parser_ctx ctx = {0};
Radek Krejci9800fb82018-12-13 14:26:23 +01001784 struct lysp_node_anydata *any = NULL;
1785 const char *str;
1786
1787 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1788 assert_non_null(ctx.ctx);
1789 ctx.line = 1;
Radek Krejci9800fb82018-12-13 14:26:23 +01001790 if (kw == YANG_ANYDATA) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001791 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci9800fb82018-12-13 14:26:23 +01001792 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001793 ctx.mod_version = 1; /* simulate YANG 1.0 */
Radek Krejci9800fb82018-12-13 14:26:23 +01001794 }
1795
1796 /* invalid cardinality */
1797#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1798 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1799 assert_int_equal(LY_EVALID, parse_any(&ctx, &str, kw, NULL, (struct lysp_node**)&any)); \
1800 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1801 lysp_node_free(ctx.ctx, (struct lysp_node*)any); any = NULL;
1802
1803 TEST_DUP("config", "true", "false");
1804 TEST_DUP("description", "text1", "text2");
1805 TEST_DUP("mandatory", "true", "false");
1806 TEST_DUP("reference", "1", "2");
1807 TEST_DUP("status", "current", "obsolete");
1808 TEST_DUP("when", "true", "false");
1809#undef TEST_DUP
1810
1811 /* full content */
1812 str = "any {config true;description test;if-feature f;mandatory true;must 'expr';reference test;status current;when true;m:ext;} ...";
1813 assert_int_equal(LY_SUCCESS, parse_any(&ctx, &str, kw, NULL, (struct lysp_node**)&any));
1814 assert_non_null(any);
1815 assert_int_equal(kw == YANG_ANYDATA ? LYS_ANYDATA : LYS_ANYXML, any->nodetype);
1816 assert_string_equal("any", any->name);
1817 assert_string_equal("test", any->dsc);
1818 assert_non_null(any->exts);
1819 assert_non_null(any->iffeatures);
1820 assert_non_null(any->musts);
1821 assert_string_equal("test", any->ref);
1822 assert_non_null(any->when);
1823 assert_null(any->parent);
1824 assert_null(any->next);
1825 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR | LYS_MAND_TRUE, any->flags);
1826 lysp_node_free(ctx.ctx, (struct lysp_node*)any); any = NULL;
1827
1828 *state = NULL;
1829 ly_ctx_destroy(ctx.ctx, NULL);
1830}
1831
1832static void
1833test_anydata(void **state)
1834{
1835 return test_any(state, YANG_ANYDATA);
1836}
1837
1838static void
1839test_anyxml(void **state)
1840{
1841 return test_any(state, YANG_ANYXML);
1842}
1843
Radek Krejcie86bf772018-12-14 11:39:53 +01001844static void
1845test_grouping(void **state)
1846{
1847 *state = test_grouping;
1848
Radek Krejcie7b95092019-05-15 11:03:07 +02001849 struct lys_parser_ctx ctx = {0};
Radek Krejcie86bf772018-12-14 11:39:53 +01001850 struct lysp_grp *grp = NULL;
1851 const char *str;
1852
1853 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1854 assert_non_null(ctx.ctx);
1855 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001856 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejcie86bf772018-12-14 11:39:53 +01001857
1858 /* invalid cardinality */
1859#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1860 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1861 assert_int_equal(LY_EVALID, parse_grouping(&ctx, &str, NULL, &grp)); \
1862 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1863 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1864
1865 TEST_DUP("description", "text1", "text2");
1866 TEST_DUP("reference", "1", "2");
1867 TEST_DUP("status", "current", "obsolete");
1868#undef TEST_DUP
1869
1870 /* full content */
1871 str = "grp {action x;anydata any;anyxml anyxml; choice ch;container c;description test;grouping g;leaf l {type string;}"
1872 "leaf-list ll {type string;} list li;notification not;reference test;status current;typedef t {type int8;}uses g;m:ext;} ...";
1873 assert_int_equal(LY_SUCCESS, parse_grouping(&ctx, &str, NULL, &grp));
1874 assert_non_null(grp);
1875 assert_int_equal(LYS_GROUPING, grp->nodetype);
1876 assert_string_equal("grp", grp->name);
1877 assert_string_equal("test", grp->dsc);
1878 assert_non_null(grp->exts);
1879 assert_string_equal("test", grp->ref);
1880 assert_null(grp->parent);
1881 assert_int_equal( LYS_STATUS_CURR, grp->flags);
1882 ly_set_erase(&ctx.tpdfs_nodes, NULL);
1883 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1884
1885 /* invalid content */
1886 str = "grp {config true} ...";
1887 assert_int_equal(LY_EVALID, parse_grouping(&ctx, &str, NULL, &grp));
1888 logbuf_assert("Invalid keyword \"config\" as a child of \"grouping\". Line number 1.");
1889 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1890
1891 str = "grp {must 'expr'} ...";
1892 assert_int_equal(LY_EVALID, parse_grouping(&ctx, &str, NULL, &grp));
1893 logbuf_assert("Invalid keyword \"must\" as a child of \"grouping\". Line number 1.");
1894 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1895
1896 *state = NULL;
1897 ly_ctx_destroy(ctx.ctx, NULL);
1898}
1899
1900static void
Radek Krejcif538ce52019-03-05 10:46:14 +01001901test_action(void **state)
1902{
1903 *state = test_action;
1904
Radek Krejcie7b95092019-05-15 11:03:07 +02001905 struct lys_parser_ctx ctx = {0};
Radek Krejcif538ce52019-03-05 10:46:14 +01001906 struct lysp_action *rpcs = NULL;
1907 struct lysp_node_container *c = NULL;
1908 const char *str;
1909
1910 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1911 assert_non_null(ctx.ctx);
1912 ctx.line = 1;
1913 ctx.mod_version = 2; /* simulate YANG 1.1 */
1914
1915 /* invalid cardinality */
1916#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1917 str = "func {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1918 assert_int_equal(LY_EVALID, parse_action(&ctx, &str, NULL, &rpcs)); \
1919 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1920 FREE_ARRAY(ctx.ctx, rpcs, lysp_action_free); rpcs = NULL;
1921
1922 TEST_DUP("description", "text1", "text2");
1923 TEST_DUP("input", "", "");
1924 TEST_DUP("output", "", "");
1925 TEST_DUP("reference", "1", "2");
1926 TEST_DUP("status", "current", "obsolete");
1927#undef TEST_DUP
1928
1929 /* full content */
1930 str = "top;";
1931 assert_int_equal(LY_SUCCESS, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1932 str = "func {description test;grouping grp;if-feature f;reference test;status current;typedef mytype {type int8;} m:ext;"
1933 "input {anydata a1; anyxml a2; choice ch; container c; grouping grp; leaf l {type int8;} leaf-list ll {type int8;}"
1934 " list li; must 1; typedef mytypei {type int8;} uses grp; m:ext;}"
1935 "output {anydata a1; anyxml a2; choice ch; container c; grouping grp; leaf l {type int8;} leaf-list ll {type int8;}"
1936 " list li; must 1; typedef mytypeo {type int8;} uses grp; m:ext;}} ...";
1937 assert_int_equal(LY_SUCCESS, parse_action(&ctx, &str, (struct lysp_node*)c, &rpcs));
1938 assert_non_null(rpcs);
1939 assert_int_equal(LYS_ACTION, rpcs->nodetype);
1940 assert_string_equal("func", rpcs->name);
1941 assert_string_equal("test", rpcs->dsc);
1942 assert_non_null(rpcs->exts);
1943 assert_non_null(rpcs->iffeatures);
1944 assert_string_equal("test", rpcs->ref);
1945 assert_non_null(rpcs->groupings);
1946 assert_non_null(rpcs->typedefs);
1947 assert_int_equal(LYS_STATUS_CURR, rpcs->flags);
1948 /* input */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001949 assert_int_equal(rpcs->input.nodetype, LYS_INPUT);
Radek Krejcif538ce52019-03-05 10:46:14 +01001950 assert_non_null(rpcs->input.groupings);
1951 assert_non_null(rpcs->input.exts);
1952 assert_non_null(rpcs->input.musts);
1953 assert_non_null(rpcs->input.typedefs);
1954 assert_non_null(rpcs->input.data);
1955 /* output */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001956 assert_int_equal(rpcs->output.nodetype, LYS_OUTPUT);
Radek Krejcif538ce52019-03-05 10:46:14 +01001957 assert_non_null(rpcs->output.groupings);
1958 assert_non_null(rpcs->output.exts);
1959 assert_non_null(rpcs->output.musts);
1960 assert_non_null(rpcs->output.typedefs);
1961 assert_non_null(rpcs->output.data);
1962
1963 ly_set_erase(&ctx.tpdfs_nodes, NULL);
1964 FREE_ARRAY(ctx.ctx, rpcs, lysp_action_free); rpcs = NULL;
1965
1966 /* invalid content */
1967 str = "func {config true} ...";
1968 assert_int_equal(LY_EVALID, parse_action(&ctx, &str, NULL, &rpcs));
1969 logbuf_assert("Invalid keyword \"config\" as a child of \"rpc\". Line number 1.");
1970 FREE_ARRAY(ctx.ctx, rpcs, lysp_action_free); rpcs = NULL;
1971
1972 *state = NULL;
1973 lysp_node_free(ctx.ctx, (struct lysp_node*)c);
1974 ly_ctx_destroy(ctx.ctx, NULL);
1975}
1976
1977static void
Radek Krejcifc11bd72019-04-11 16:00:05 +02001978test_notification(void **state)
1979{
1980 *state = test_notification;
1981
Radek Krejcie7b95092019-05-15 11:03:07 +02001982 struct lys_parser_ctx ctx = {0};
Radek Krejcifc11bd72019-04-11 16:00:05 +02001983 struct lysp_notif *notifs = NULL;
1984 struct lysp_node_container *c = NULL;
1985 const char *str;
1986
1987 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1988 assert_non_null(ctx.ctx);
1989 ctx.line = 1;
1990 ctx.mod_version = 2; /* simulate YANG 1.1 */
1991
1992 /* invalid cardinality */
1993#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1994 str = "func {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1995 assert_int_equal(LY_EVALID, parse_notif(&ctx, &str, NULL, &notifs)); \
1996 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1997 FREE_ARRAY(ctx.ctx, notifs, lysp_notif_free); notifs = NULL;
1998
1999 TEST_DUP("description", "text1", "text2");
2000 TEST_DUP("reference", "1", "2");
2001 TEST_DUP("status", "current", "obsolete");
2002#undef TEST_DUP
2003
2004 /* full content */
2005 str = "top;";
2006 assert_int_equal(LY_SUCCESS, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
2007 str = "ntf {anydata a1; anyxml a2; choice ch; container c; description test; grouping grp; if-feature f; leaf l {type int8;}"
2008 "leaf-list ll {type int8;} list li; must 1; reference test; status current; typedef mytype {type int8;} uses grp; m:ext;}";
2009 assert_int_equal(LY_SUCCESS, parse_notif(&ctx, &str, (struct lysp_node*)c, &notifs));
2010 assert_non_null(notifs);
2011 assert_int_equal(LYS_NOTIF, notifs->nodetype);
2012 assert_string_equal("ntf", notifs->name);
2013 assert_string_equal("test", notifs->dsc);
2014 assert_non_null(notifs->exts);
2015 assert_non_null(notifs->iffeatures);
2016 assert_string_equal("test", notifs->ref);
2017 assert_non_null(notifs->groupings);
2018 assert_non_null(notifs->typedefs);
2019 assert_non_null(notifs->musts);
2020 assert_non_null(notifs->data);
2021 assert_int_equal(LYS_STATUS_CURR, notifs->flags);
2022
2023 ly_set_erase(&ctx.tpdfs_nodes, NULL);
2024 FREE_ARRAY(ctx.ctx, notifs, lysp_notif_free); notifs = NULL;
2025
2026 /* invalid content */
2027 str = "ntf {config true} ...";
2028 assert_int_equal(LY_EVALID, parse_notif(&ctx, &str, NULL, &notifs));
2029 logbuf_assert("Invalid keyword \"config\" as a child of \"notification\". Line number 1.");
2030 FREE_ARRAY(ctx.ctx, notifs, lysp_notif_free); notifs = NULL;
2031
2032 *state = NULL;
2033 lysp_node_free(ctx.ctx, (struct lysp_node*)c);
2034 ly_ctx_destroy(ctx.ctx, NULL);
2035}
2036
2037static void
Radek Krejcie86bf772018-12-14 11:39:53 +01002038test_uses(void **state)
2039{
2040 *state = test_uses;
2041
Radek Krejcie7b95092019-05-15 11:03:07 +02002042 struct lys_parser_ctx ctx = {0};
Radek Krejcie86bf772018-12-14 11:39:53 +01002043 struct lysp_node_uses *u = NULL;
2044 const char *str;
2045
2046 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
2047 assert_non_null(ctx.ctx);
2048 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002049 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejcie86bf772018-12-14 11:39:53 +01002050
2051 /* invalid cardinality */
2052#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
2053 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
2054 assert_int_equal(LY_EVALID, parse_uses(&ctx, &str, NULL, (struct lysp_node**)&u)); \
2055 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
2056 lysp_node_free(ctx.ctx, (struct lysp_node*)u); u = NULL;
2057
2058 TEST_DUP("description", "text1", "text2");
2059 TEST_DUP("reference", "1", "2");
2060 TEST_DUP("status", "current", "obsolete");
2061 TEST_DUP("when", "true", "false");
2062#undef TEST_DUP
2063
2064 /* full content */
2065 str = "grpref {augment some/node;description test;if-feature f;reference test;refine some/other/node;status current;when true;m:ext;} ...";
2066 assert_int_equal(LY_SUCCESS, parse_uses(&ctx, &str, NULL, (struct lysp_node**)&u));
2067 assert_non_null(u);
2068 assert_int_equal(LYS_USES, u->nodetype);
2069 assert_string_equal("grpref", u->name);
2070 assert_string_equal("test", u->dsc);
2071 assert_non_null(u->exts);
2072 assert_non_null(u->iffeatures);
2073 assert_string_equal("test", u->ref);
2074 assert_non_null(u->augments);
2075 assert_non_null(u->refines);
2076 assert_non_null(u->when);
2077 assert_null(u->parent);
2078 assert_null(u->next);
2079 assert_int_equal(LYS_STATUS_CURR, u->flags);
2080 lysp_node_free(ctx.ctx, (struct lysp_node*)u); u = NULL;
2081
2082 *state = NULL;
2083 ly_ctx_destroy(ctx.ctx, NULL);
2084}
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002085
2086
2087static void
2088test_augment(void **state)
2089{
2090 *state = test_augment;
2091
Radek Krejcie7b95092019-05-15 11:03:07 +02002092 struct lys_parser_ctx ctx = {0};
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002093 struct lysp_augment *a = NULL;
2094 const char *str;
2095
2096 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
2097 assert_non_null(ctx.ctx);
2098 ctx.line = 1;
Radek Krejcib4adbf12019-02-25 13:35:22 +01002099 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002100
2101 /* invalid cardinality */
2102#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
2103 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
2104 assert_int_equal(LY_EVALID, parse_augment(&ctx, &str, NULL, &a)); \
2105 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
Radek Krejcib4adbf12019-02-25 13:35:22 +01002106 FREE_ARRAY(ctx.ctx, a, lysp_augment_free); a = NULL;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002107
2108 TEST_DUP("description", "text1", "text2");
2109 TEST_DUP("reference", "1", "2");
2110 TEST_DUP("status", "current", "obsolete");
2111 TEST_DUP("when", "true", "false");
2112#undef TEST_DUP
2113
2114 /* full content */
2115 str = "/target/nodeid {action x; anydata any;anyxml anyxml; case cs; choice ch;container c;description test;if-feature f;leaf l {type string;}"
2116 "leaf-list ll {type string;} list li;notification not;reference test;status current;uses g;when true;m:ext;} ...";
2117 assert_int_equal(LY_SUCCESS, parse_augment(&ctx, &str, NULL, &a));
2118 assert_non_null(a);
2119 assert_int_equal(LYS_AUGMENT, a->nodetype);
2120 assert_string_equal("/target/nodeid", a->nodeid);
2121 assert_string_equal("test", a->dsc);
2122 assert_non_null(a->exts);
2123 assert_non_null(a->iffeatures);
2124 assert_string_equal("test", a->ref);
2125 assert_non_null(a->when);
2126 assert_null(a->parent);
2127 assert_int_equal(LYS_STATUS_CURR, a->flags);
Radek Krejcib4adbf12019-02-25 13:35:22 +01002128 FREE_ARRAY(ctx.ctx, a, lysp_augment_free); a = NULL;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002129
2130 *state = NULL;
2131 ly_ctx_destroy(ctx.ctx, NULL);
2132}
2133
Radek Krejci80dd33e2018-09-26 15:57:18 +02002134int main(void)
2135{
2136 const struct CMUnitTest tests[] = {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002137 cmocka_unit_test_setup(test_helpers, logger_setup),
Radek Krejci80dd33e2018-09-26 15:57:18 +02002138 cmocka_unit_test_setup(test_comments, logger_setup),
Radek Krejciefd22f62018-09-27 11:47:58 +02002139 cmocka_unit_test_setup(test_arg, logger_setup),
Radek Krejcidcc7b322018-10-11 14:24:02 +02002140 cmocka_unit_test_setup(test_stmts, logger_setup),
Radek Krejci05b13982018-11-28 16:22:07 +01002141 cmocka_unit_test_setup_teardown(test_minmax, logger_setup, logger_teardown),
Radek Krejci40544fa2019-01-11 09:38:37 +01002142 cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown),
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002143 cmocka_unit_test_setup_teardown(test_identity, logger_setup, logger_teardown),
Radek Krejci2c02f3e2018-10-16 10:54:38 +02002144 cmocka_unit_test_setup(test_feature, logger_setup),
2145 cmocka_unit_test_setup(test_deviation, logger_setup),
2146 cmocka_unit_test_setup(test_deviate, logger_setup),
Radek Krejci8c370832018-11-02 15:10:03 +01002147 cmocka_unit_test_setup(test_container, logger_setup),
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002148 cmocka_unit_test_setup_teardown(test_leaf, logger_setup, logger_teardown),
Radek Krejci0e5d8382018-11-28 16:37:53 +01002149 cmocka_unit_test_setup_teardown(test_leaflist, logger_setup, logger_teardown),
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002150 cmocka_unit_test_setup_teardown(test_list, logger_setup, logger_teardown),
Radek Krejci056d0a82018-12-06 16:57:25 +01002151 cmocka_unit_test_setup_teardown(test_choice, logger_setup, logger_teardown),
Radek Krejcia9026eb2018-12-12 16:04:47 +01002152 cmocka_unit_test_setup_teardown(test_case, logger_setup, logger_teardown),
Radek Krejci9800fb82018-12-13 14:26:23 +01002153 cmocka_unit_test_setup_teardown(test_anydata, logger_setup, logger_teardown),
2154 cmocka_unit_test_setup_teardown(test_anyxml, logger_setup, logger_teardown),
Radek Krejcif538ce52019-03-05 10:46:14 +01002155 cmocka_unit_test_setup_teardown(test_action, logger_setup, logger_teardown),
Radek Krejcifc11bd72019-04-11 16:00:05 +02002156 cmocka_unit_test_setup_teardown(test_notification, logger_setup, logger_teardown),
Radek Krejcie86bf772018-12-14 11:39:53 +01002157 cmocka_unit_test_setup_teardown(test_grouping, logger_setup, logger_teardown),
2158 cmocka_unit_test_setup_teardown(test_uses, logger_setup, logger_teardown),
Radek Krejcib4adbf12019-02-25 13:35:22 +01002159 cmocka_unit_test_setup_teardown(test_augment, logger_setup, logger_teardown),
Radek Krejci80dd33e2018-09-26 15:57:18 +02002160 };
2161
2162 return cmocka_run_group_tests(tests, NULL, NULL);
2163}