blob: e4adfd736e6d6e3b9ed2969f4ed9335dd751f908 [file] [log] [blame]
Radek Krejci80dd33e2018-09-26 15:57:18 +02001/*
2 * @file test_parser_yang.c
3 * @author: Radek Krejci <rkrejci@cesnet.cz>
4 * @brief unit tests for functions from parser_yang.c
5 *
6 * Copyright (c) 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Radek Krejci80dd33e2018-09-26 15:57:18 +020015#include <stdarg.h>
16#include <stddef.h>
17#include <setjmp.h>
18#include <cmocka.h>
19
20#include <stdio.h>
21#include <string.h>
22
Radek Krejci2d7a47b2019-05-16 13:34:10 +020023#include "../../src/common.h"
24#include "../../src/tree_schema.h"
25#include "../../src/tree_schema_internal.h"
26
27/* originally static functions from tree_schema_free.c and parser_yang.c */
28void lysp_ext_instance_free(struct ly_ctx *ctx, struct lysp_ext_instance *ext);
29void lysp_ident_free(struct ly_ctx *ctx, struct lysp_ident *ident);
30void lysp_feature_free(struct ly_ctx *ctx, struct lysp_feature *feat);
31void lysp_deviation_free(struct ly_ctx *ctx, struct lysp_deviation *dev);
32void lysp_grp_free(struct ly_ctx *ctx, struct lysp_grp *grp);
33void lysp_action_free(struct ly_ctx *ctx, struct lysp_action *action);
34void lysp_notif_free(struct ly_ctx *ctx, struct lysp_notif *notif);
35void lysp_augment_free(struct ly_ctx *ctx, struct lysp_augment *augment);
36void lysp_deviate_free(struct ly_ctx *ctx, struct lysp_deviate *d);
37void lysp_node_free(struct ly_ctx *ctx, struct lysp_node *node);
Radek Krejcif09e4e82019-06-14 15:08:11 +020038void lysp_when_free(struct ly_ctx *ctx, struct lysp_when *when);
Radek Krejci2d7a47b2019-05-16 13:34:10 +020039
40LY_ERR buf_add_char(struct ly_ctx *ctx, const char **input, size_t len, char **buf, size_t *buf_len, size_t *buf_used);
David Sedlák40bb13b2019-07-10 14:34:18 +020041LY_ERR buf_store_char(struct lys_parser_ctx *ctx, const char **input, enum yang_arg arg, char **word_p,
42 size_t *word_len, char **word_b, size_t *buf_len, int need_buf, int *prefix);
Radek Krejci2d7a47b2019-05-16 13:34:10 +020043LY_ERR get_keyword(struct lys_parser_ctx *ctx, const char **data, enum yang_keyword *kw, char **word_p, size_t *word_len);
44LY_ERR get_argument(struct lys_parser_ctx *ctx, const char **data, enum yang_arg arg,
45 uint16_t *flags, char **word_p, char **word_b, size_t *word_len);
46LY_ERR skip_comment(struct lys_parser_ctx *ctx, const char **data, int comment);
Radek Krejci2d7a47b2019-05-16 13:34:10 +020047
48LY_ERR parse_action(struct lys_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_action **actions);
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 Krejcif09e4e82019-06-14 15:08:11 +020068LY_ERR parse_when(struct lys_parser_ctx *ctx, const char **data, struct lysp_when **when_p);
David Sedlákd6ce6d72019-07-16 17:30:18 +020069LY_ERR parse_type_enum_value_pos(struct lys_parser_ctx *ctx, const char **data, enum yang_keyword val_kw, int64_t *value, uint16_t *flags, struct lysp_ext_instance **exts);
Radek Krejci80dd33e2018-09-26 15:57:18 +020070
71#define BUFSIZE 1024
72char logbuf[BUFSIZE] = {0};
Radek Krejci9ed7a192018-10-31 16:23:51 +010073int store = -1; /* negative for infinite logging, positive for limited logging */
Radek Krejci80dd33e2018-09-26 15:57:18 +020074
75/* set to 0 to printing error messages to stderr instead of checking them in code */
Radek Krejci70853c52018-10-15 14:46:16 +020076#define ENABLE_LOGGER_CHECKING 1
Radek Krejci80dd33e2018-09-26 15:57:18 +020077
Radek Krejcid5f2b5f2018-10-11 10:54:36 +020078#if ENABLE_LOGGER_CHECKING
Radek Krejci80dd33e2018-09-26 15:57:18 +020079static void
80logger(LY_LOG_LEVEL level, const char *msg, const char *path)
81{
82 (void) level; /* unused */
Radek Krejci9ed7a192018-10-31 16:23:51 +010083 if (store) {
84 if (path && path[0]) {
85 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
86 } else {
87 strncpy(logbuf, msg, BUFSIZE - 1);
88 }
89 if (store > 0) {
90 --store;
91 }
Radek Krejci80dd33e2018-09-26 15:57:18 +020092 }
93}
Radek Krejcid5f2b5f2018-10-11 10:54:36 +020094#endif
Radek Krejci80dd33e2018-09-26 15:57:18 +020095
96static int
97logger_setup(void **state)
98{
99 (void) state; /* unused */
100#if ENABLE_LOGGER_CHECKING
101 ly_set_log_clb(logger, 1);
102#endif
103 return 0;
104}
105
Radek Krejcib1a5dcc2018-11-26 14:50:05 +0100106static int
107logger_teardown(void **state)
108{
109 (void) state; /* unused */
110#if ENABLE_LOGGER_CHECKING
111 if (*state) {
112 fprintf(stderr, "%s\n", logbuf);
113 }
114#endif
115 return 0;
116}
117
Radek Krejci80dd33e2018-09-26 15:57:18 +0200118void
119logbuf_clean(void)
120{
121 logbuf[0] = '\0';
122}
123
124#if ENABLE_LOGGER_CHECKING
125# define logbuf_assert(str) assert_string_equal(logbuf, str)
126#else
127# define logbuf_assert(str)
128#endif
129
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200130#define TEST_DUP_GENERIC(PREFIX, MEMBER, VALUE1, VALUE2, FUNC, RESULT, LINE, CLEANUP) \
131 str = PREFIX MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
132 assert_int_equal(LY_EVALID, FUNC(&ctx, &str, RESULT)); \
133 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number "LINE"."); \
134 CLEANUP
135
Radek Krejci44ceedc2018-10-02 15:54:31 +0200136static void
137test_helpers(void **state)
138{
139 (void) state; /* unused */
140
141 const char *str;
Radek Krejci404251e2018-10-09 12:06:44 +0200142 char *buf, *p;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200143 size_t len, size;
Radek Krejcie7b95092019-05-15 11:03:07 +0200144 struct lys_parser_ctx ctx;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200145 ctx.ctx = NULL;
146 ctx.line = 1;
David Sedlák40bb13b2019-07-10 14:34:18 +0200147 int prefix = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200148
149 /* storing into buffer */
150 str = "abcd";
151 buf = NULL;
152 size = len = 0;
153 assert_int_equal(LY_SUCCESS, buf_add_char(NULL, &str, 2, &buf, &size, &len));
154 assert_int_not_equal(0, size);
155 assert_int_equal(2, len);
156 assert_string_equal("cd", str);
157 assert_false(strncmp("ab", buf, 2));
158 free(buf);
Radek Krejci404251e2018-10-09 12:06:44 +0200159 buf = NULL;
160
161 /* invalid first characters */
162 len = 0;
163 str = "2invalid";
David Sedlák40bb13b2019-07-10 14:34:18 +0200164 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
Radek Krejci404251e2018-10-09 12:06:44 +0200165 str = ".invalid";
David Sedlák40bb13b2019-07-10 14:34:18 +0200166 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
Radek Krejci404251e2018-10-09 12:06:44 +0200167 str = "-invalid";
David Sedlák40bb13b2019-07-10 14:34:18 +0200168 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
Radek Krejci404251e2018-10-09 12:06:44 +0200169 /* invalid following characters */
170 len = 3; /* number of characters read before the str content */
171 str = "!";
David Sedlák40bb13b2019-07-10 14:34:18 +0200172 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
Radek Krejci404251e2018-10-09 12:06:44 +0200173 str = ":";
David Sedlák40bb13b2019-07-10 14:34:18 +0200174 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
Radek Krejci404251e2018-10-09 12:06:44 +0200175 /* valid colon for prefixed identifiers */
176 len = size = 0;
177 p = NULL;
David Sedlák40bb13b2019-07-10 14:34:18 +0200178 prefix = 0;
Radek Krejci404251e2018-10-09 12:06:44 +0200179 str = "x:id";
David Sedlák40bb13b2019-07-10 14:34:18 +0200180 assert_int_equal(LY_SUCCESS, buf_store_char(&ctx, &str, Y_PREF_IDENTIF_ARG, &p, &len, &buf, &size, 0, &prefix));
Radek Krejci404251e2018-10-09 12:06:44 +0200181 assert_int_equal(1, len);
182 assert_null(buf);
183 assert_string_equal(":id", str);
184 assert_int_equal('x', p[len - 1]);
David Sedlák40bb13b2019-07-10 14:34:18 +0200185 assert_int_equal(LY_SUCCESS, buf_store_char(&ctx, &str, Y_PREF_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
Radek Krejci404251e2018-10-09 12:06:44 +0200186 assert_int_equal(2, len);
187 assert_string_equal("id", str);
188 assert_int_equal(':', p[len - 1]);
189 free(buf);
David Sedlák40bb13b2019-07-10 14:34:18 +0200190 prefix = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200191
192 /* checking identifiers */
David Sedlák4ccd7c32019-07-10 12:02:04 +0200193 assert_int_equal(LY_EVALID, lysp_check_identifierchar(&ctx, ':', 0, NULL));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200194 logbuf_assert("Invalid identifier character ':'. Line number 1.");
David Sedlák4ccd7c32019-07-10 12:02:04 +0200195 assert_int_equal(LY_EVALID, lysp_check_identifierchar(&ctx, '#', 1, NULL));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200196 logbuf_assert("Invalid identifier first character '#'. Line number 1.");
197
David Sedlák4ccd7c32019-07-10 12:02:04 +0200198 assert_int_equal(LY_SUCCESS, lysp_check_identifierchar(&ctx, 'a', 1, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200199 assert_int_equal(0, prefix);
David Sedlák4ccd7c32019-07-10 12:02:04 +0200200 assert_int_equal(LY_SUCCESS, lysp_check_identifierchar(&ctx, ':', 0, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200201 assert_int_equal(1, prefix);
David Sedlák4ccd7c32019-07-10 12:02:04 +0200202 assert_int_equal(LY_EVALID, lysp_check_identifierchar(&ctx, ':', 0, &prefix));
Radek Krejcidcc7b322018-10-11 14:24:02 +0200203 assert_int_equal(1, prefix);
David Sedlák4ccd7c32019-07-10 12:02:04 +0200204 assert_int_equal(LY_SUCCESS, lysp_check_identifierchar(&ctx, 'b', 0, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200205 assert_int_equal(2, prefix);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200206 /* second colon is invalid */
David Sedlák4ccd7c32019-07-10 12:02:04 +0200207 assert_int_equal(LY_EVALID, lysp_check_identifierchar(&ctx, ':', 0, &prefix));
Radek Krejcidcc7b322018-10-11 14:24:02 +0200208 logbuf_assert("Invalid identifier character ':'. Line number 1.");
Radek Krejci44ceedc2018-10-02 15:54:31 +0200209}
Radek Krejci80dd33e2018-09-26 15:57:18 +0200210
211static void
212test_comments(void **state)
213{
214 (void) state; /* unused */
215
Radek Krejcie7b95092019-05-15 11:03:07 +0200216 struct lys_parser_ctx ctx;
Radek Krejci80dd33e2018-09-26 15:57:18 +0200217 const char *str, *p;
Radek Krejciefd22f62018-09-27 11:47:58 +0200218 char *word, *buf;
219 size_t len;
Radek Krejci80dd33e2018-09-26 15:57:18 +0200220
Radek Krejci44ceedc2018-10-02 15:54:31 +0200221 ctx.ctx = NULL;
222 ctx.line = 1;
223
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200224 str = " // this is a text of / one * line */ comment\nargument;";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200225 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200226 assert_string_equal("argument;", word);
Radek Krejciefd22f62018-09-27 11:47:58 +0200227 assert_null(buf);
228 assert_int_equal(8, len);
Radek Krejci80dd33e2018-09-26 15:57:18 +0200229
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200230 str = "/* this is a \n * text // of / block * comment */\"arg\" + \"ume\" \n + \n \"nt\";";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200231 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200232 assert_string_equal("argument", word);
233 assert_ptr_equal(buf, word);
234 assert_int_equal(8, len);
235 free(word);
Radek Krejci80dd33e2018-09-26 15:57:18 +0200236
237 str = p = " this is one line comment on last line";
Radek Krejci44ceedc2018-10-02 15:54:31 +0200238 assert_int_equal(LY_SUCCESS, skip_comment(&ctx, &str, 1));
Radek Krejci80dd33e2018-09-26 15:57:18 +0200239 assert_true(str[0] == '\0');
240
241 str = p = " this is a not terminated comment x";
Radek Krejci44ceedc2018-10-02 15:54:31 +0200242 assert_int_equal(LY_EVALID, skip_comment(&ctx, &str, 2));
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200243 logbuf_assert("Unexpected end-of-input, non-terminated comment. Line number 5.");
Radek Krejci80dd33e2018-09-26 15:57:18 +0200244 assert_true(str[0] == '\0');
245}
246
Radek Krejciefd22f62018-09-27 11:47:58 +0200247static void
248test_arg(void **state)
249{
250 (void) state; /* unused */
251
Radek Krejcie7b95092019-05-15 11:03:07 +0200252 struct lys_parser_ctx ctx;
Radek Krejciefd22f62018-09-27 11:47:58 +0200253 const char *str;
254 char *word, *buf;
255 size_t len;
256
Radek Krejci44ceedc2018-10-02 15:54:31 +0200257 ctx.ctx = NULL;
258 ctx.line = 1;
259
Radek Krejciefd22f62018-09-27 11:47:58 +0200260 /* missing argument */
261 str = ";";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200262 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_MAYBE_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200263 assert_null(word);
264
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200265 str = "{";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200266 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200267 logbuf_assert("Invalid character sequence \"{\", expected an argument. Line number 1.");
268
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200269 /* invalid escape sequence */
270 str = "\"\\s\"";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200271 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200272 logbuf_assert("Double-quoted string unknown special character \'\\s\'. Line number 1.");
273 str = "\'\\s\'"; /* valid, since it is not an escape sequence in single quoted string */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200274 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200275 assert_int_equal(2, len);
276 assert_string_equal("\\s\'", word);
277 assert_int_equal('\0', str[0]); /* input has been eaten */
278
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200279 /* invalid character after the argument */
280 str = "hello\"";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200281 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200282 logbuf_assert("Invalid character sequence \"\"\", expected unquoted string character, optsep, semicolon or opening brace. Line number 1.");
283 str = "hello}";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200284 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200285 logbuf_assert("Invalid character sequence \"}\", expected unquoted string character, optsep, semicolon or opening brace. Line number 1.");
286
David Sedlák40bb13b2019-07-10 14:34:18 +0200287 /* invalid identifier-ref-arg-str */
288 str = "pre:pre:value";
289 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &len));
290
Radek Krejci4e199f52019-05-28 09:09:28 +0200291 str = "\"\";"; /* empty identifier is not allowed */
292 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_IDENTIF_ARG, NULL, &word, &buf, &len));
293 logbuf_assert("Statement argument is required. Line number 1.");
294 logbuf_clean();
295 str = "\"\";"; /* empty reference identifier is not allowed */
296 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &len));
297 logbuf_assert("Statement argument is required. Line number 1.");
298
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200299 str = "hello/x\t"; /* slash is not an invalid character */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200300 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200301 assert_int_equal(7, len);
302 assert_string_equal("hello/x\t", word);
303
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200304 assert_null(buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200305
306 /* different quoting */
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200307 str = "hello ";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200308 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200309 assert_null(buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200310 assert_int_equal(5, len);
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200311 assert_string_equal("hello ", word);
Radek Krejciefd22f62018-09-27 11:47:58 +0200312
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200313 str = "hello/*comment*/\n";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200314 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200315 assert_null(buf);
316 assert_int_equal(5, len);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200317 assert_false(strncmp("hello", word, len));
318
319
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200320 str = "\"hello\\n\\t\\\"\\\\\";";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200321 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200322 assert_null(buf);
323 assert_int_equal(9, len);
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200324 assert_string_equal("hello\\n\\t\\\"\\\\\";", word);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200325
326 ctx.indent = 14;
327 str = "\"hello \t\n\t\t world!\"";
328 /* - space and tabs before newline are stripped out
329 * - space and tabs after newline (indentation) are stripped out
330 */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200331 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200332 assert_non_null(buf);
333 assert_ptr_equal(word, buf);
334 assert_int_equal(14, len);
335 assert_string_equal("hello\n world!", word);
336 free(buf);
337
338 ctx.indent = 14;
339 str = "\"hello\n \tworld!\"";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200340 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200341 assert_non_null(buf);
342 assert_ptr_equal(word, buf);
343 assert_int_equal(12, len);
344 assert_string_equal("hello\nworld!", word);
345 free(buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200346
347 str = "\'hello\'";
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_null(buf);
350 assert_int_equal(5, len);
351 assert_false(strncmp("hello", word, 5));
352
353 str = "\"hel\" +\t\n\"lo\"";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200354 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200355 assert_ptr_equal(word, buf);
356 assert_int_equal(5, len);
357 assert_string_equal("hello", word);
358 free(buf);
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200359 str = "\"hel\" +\t\nlo"; /* unquoted the second part */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200360 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200361 logbuf_assert("Both string parts divided by '+' must be quoted. Line number 5.");
Radek Krejciefd22f62018-09-27 11:47:58 +0200362
363 str = "\'he\'\t\n+ \"llo\"";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200364 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200365 assert_ptr_equal(word, buf);
366 assert_int_equal(5, len);
367 assert_string_equal("hello", word);
368 free(buf);
369
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200370 str = " \t\n\"he\"+\'llo\'";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200371 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200372 assert_ptr_equal(word, buf);
373 assert_int_equal(5, len);
374 assert_string_equal("hello", word);
375 free(buf);
376
Radek Krejci44ceedc2018-10-02 15:54:31 +0200377 /* missing argument */
378 str = ";";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200379 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200380 logbuf_assert("Invalid character sequence \";\", expected an argument. Line number 7.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200381}
382
383static void
384test_stmts(void **state)
385{
386 (void) state; /* unused */
387
Radek Krejcie7b95092019-05-15 11:03:07 +0200388 struct lys_parser_ctx ctx;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200389 const char *str, *p;
390 enum yang_keyword kw;
391 char *word;
392 size_t len;
393
394 ctx.ctx = NULL;
395 ctx.line = 1;
396
397 str = "\n// comment\n\tinput\t{";
398 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
399 assert_int_equal(YANG_INPUT, kw);
400 assert_int_equal(5, len);
401 assert_string_equal("input\t{", word);
402 assert_string_equal("\t{", str);
403
404 str = "\t /* comment */\t output\n\t{";
405 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
406 assert_int_equal(YANG_OUTPUT, kw);
407 assert_int_equal(6, len);
408 assert_string_equal("output\n\t{", word);
409 assert_string_equal("\n\t{", str);
Radek Krejciabdd8062019-06-11 16:44:19 +0200410 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
411 assert_int_equal(YANG_LEFT_BRACE, kw);
412 assert_int_equal(1, len);
413 assert_string_equal("{", word);
414 assert_string_equal("", str);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200415
416 str = "/input { "; /* invalid slash */
417 assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len));
418 logbuf_assert("Invalid identifier first character '/'. Line number 4.");
419
420 str = "not-a-statement-nor-extension { "; /* invalid identifier */
421 assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len));
422 logbuf_assert("Invalid character sequence \"not-a-statement-nor-extension\", expected a keyword. Line number 4.");
423
424 str = "path;"; /* missing sep after the keyword */
425 assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len));
426 logbuf_assert("Invalid character sequence \"path;\", expected a keyword followed by a separator. Line number 4.");
427
428 str = "action ";
429 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
430 assert_int_equal(YANG_ACTION, kw);
431 assert_int_equal(6, len);
432 str = "anydata ";
433 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
434 assert_int_equal(YANG_ANYDATA, kw);
435 assert_int_equal(7, len);
436 str = "anyxml ";
437 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
438 assert_int_equal(YANG_ANYXML, kw);
439 assert_int_equal(6, len);
440 str = "argument ";
441 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
442 assert_int_equal(YANG_ARGUMENT, kw);
443 assert_int_equal(8, len);
444 str = "augment ";
445 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
446 assert_int_equal(YANG_AUGMENT, kw);
447 assert_int_equal(7, len);
448 str = "base ";
449 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
450 assert_int_equal(YANG_BASE, kw);
451 assert_int_equal(4, len);
452 str = "belongs-to ";
453 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
454 assert_int_equal(YANG_BELONGS_TO, kw);
455 assert_int_equal(10, len);
456 str = "bit ";
457 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
458 assert_int_equal(YANG_BIT, kw);
459 assert_int_equal(3, len);
460 str = "case ";
461 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
462 assert_int_equal(YANG_CASE, kw);
463 assert_int_equal(4, len);
464 str = "choice ";
465 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
466 assert_int_equal(YANG_CHOICE, kw);
467 assert_int_equal(6, len);
468 str = "config ";
469 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
470 assert_int_equal(YANG_CONFIG, kw);
471 assert_int_equal(6, len);
472 str = "contact ";
473 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
474 assert_int_equal(YANG_CONTACT, kw);
475 assert_int_equal(7, len);
476 str = "container ";
477 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
478 assert_int_equal(YANG_CONTAINER, kw);
479 assert_int_equal(9, len);
480 str = "default ";
481 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
482 assert_int_equal(YANG_DEFAULT, kw);
483 assert_int_equal(7, len);
484 str = "description ";
485 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
486 assert_int_equal(YANG_DESCRIPTION, kw);
487 assert_int_equal(11, len);
488 str = "deviate ";
489 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
490 assert_int_equal(YANG_DEVIATE, kw);
491 assert_int_equal(7, len);
492 str = "deviation ";
493 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
494 assert_int_equal(YANG_DEVIATION, kw);
495 assert_int_equal(9, len);
496 str = "enum ";
497 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
498 assert_int_equal(YANG_ENUM, kw);
499 assert_int_equal(4, len);
500 str = "error-app-tag ";
501 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
502 assert_int_equal(YANG_ERROR_APP_TAG, kw);
503 assert_int_equal(13, len);
504 str = "error-message ";
505 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
506 assert_int_equal(YANG_ERROR_MESSAGE, kw);
507 assert_int_equal(13, len);
508 str = "extension ";
509 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
510 assert_int_equal(YANG_EXTENSION, kw);
511 assert_int_equal(9, len);
512 str = "feature ";
513 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
514 assert_int_equal(YANG_FEATURE, kw);
515 assert_int_equal(7, len);
516 str = "fraction-digits ";
517 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
518 assert_int_equal(YANG_FRACTION_DIGITS, kw);
519 assert_int_equal(15, len);
520 str = "grouping ";
521 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
522 assert_int_equal(YANG_GROUPING, kw);
523 assert_int_equal(8, len);
524 str = "identity ";
525 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
526 assert_int_equal(YANG_IDENTITY, kw);
527 assert_int_equal(8, len);
528 str = "if-feature ";
529 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
530 assert_int_equal(YANG_IF_FEATURE, kw);
531 assert_int_equal(10, len);
532 str = "import ";
533 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
534 assert_int_equal(YANG_IMPORT, kw);
535 assert_int_equal(6, len);
536 str = "include ";
537 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
538 assert_int_equal(YANG_INCLUDE, kw);
539 assert_int_equal(7, len);
540 str = "input{";
541 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
542 assert_int_equal(YANG_INPUT, kw);
543 assert_int_equal(5, len);
544 str = "key ";
545 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
546 assert_int_equal(YANG_KEY, kw);
547 assert_int_equal(3, len);
548 str = "leaf ";
549 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
550 assert_int_equal(YANG_LEAF, kw);
551 assert_int_equal(4, len);
552 str = "leaf-list ";
553 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
554 assert_int_equal(YANG_LEAF_LIST, kw);
555 assert_int_equal(9, len);
556 str = "length ";
557 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
558 assert_int_equal(YANG_LENGTH, kw);
559 assert_int_equal(6, len);
560 str = "list ";
561 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
562 assert_int_equal(YANG_LIST, kw);
563 assert_int_equal(4, len);
564 str = "mandatory ";
565 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
566 assert_int_equal(YANG_MANDATORY, kw);
567 assert_int_equal(9, len);
568 str = "max-elements ";
569 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
570 assert_int_equal(YANG_MAX_ELEMENTS, kw);
571 assert_int_equal(12, len);
572 str = "min-elements ";
573 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
574 assert_int_equal(YANG_MIN_ELEMENTS, kw);
575 assert_int_equal(12, len);
576 str = "modifier ";
577 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
578 assert_int_equal(YANG_MODIFIER, kw);
579 assert_int_equal(8, len);
580 str = "module ";
581 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
582 assert_int_equal(YANG_MODULE, kw);
583 assert_int_equal(6, len);
584 str = "must ";
585 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
586 assert_int_equal(YANG_MUST, kw);
587 assert_int_equal(4, len);
588 str = "namespace ";
589 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
590 assert_int_equal(YANG_NAMESPACE, kw);
591 assert_int_equal(9, len);
592 str = "notification ";
593 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
594 assert_int_equal(YANG_NOTIFICATION, kw);
595 assert_int_equal(12, len);
596 str = "ordered-by ";
597 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
598 assert_int_equal(YANG_ORDERED_BY, kw);
599 assert_int_equal(10, len);
600 str = "organization ";
601 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
602 assert_int_equal(YANG_ORGANIZATION, kw);
603 assert_int_equal(12, len);
604 str = "output ";
605 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
606 assert_int_equal(YANG_OUTPUT, kw);
607 assert_int_equal(6, len);
608 str = "path ";
609 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
610 assert_int_equal(YANG_PATH, kw);
611 assert_int_equal(4, len);
612 str = "pattern ";
613 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
614 assert_int_equal(YANG_PATTERN, kw);
615 assert_int_equal(7, len);
616 str = "position ";
617 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
618 assert_int_equal(YANG_POSITION, kw);
619 assert_int_equal(8, len);
620 str = "prefix ";
621 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
622 assert_int_equal(YANG_PREFIX, kw);
623 assert_int_equal(6, len);
624 str = "presence ";
625 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
626 assert_int_equal(YANG_PRESENCE, kw);
627 assert_int_equal(8, len);
628 str = "range ";
629 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
630 assert_int_equal(YANG_RANGE, kw);
631 assert_int_equal(5, len);
632 str = "reference ";
633 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
634 assert_int_equal(YANG_REFERENCE, kw);
635 assert_int_equal(9, len);
636 str = "refine ";
637 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
638 assert_int_equal(YANG_REFINE, kw);
639 assert_int_equal(6, len);
640 str = "require-instance ";
641 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
642 assert_int_equal(YANG_REQUIRE_INSTANCE, kw);
643 assert_int_equal(16, len);
644 str = "revision ";
645 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
646 assert_int_equal(YANG_REVISION, kw);
647 assert_int_equal(8, len);
648 str = "revision-date ";
649 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
650 assert_int_equal(YANG_REVISION_DATE, kw);
651 assert_int_equal(13, len);
652 str = "rpc ";
653 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
654 assert_int_equal(YANG_RPC, kw);
655 assert_int_equal(3, len);
656 str = "status ";
657 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
658 assert_int_equal(YANG_STATUS, kw);
659 assert_int_equal(6, len);
660 str = "submodule ";
661 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
662 assert_int_equal(YANG_SUBMODULE, kw);
663 assert_int_equal(9, len);
664 str = "type ";
665 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
666 assert_int_equal(YANG_TYPE, kw);
667 assert_int_equal(4, len);
668 str = "typedef ";
669 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
670 assert_int_equal(YANG_TYPEDEF, kw);
671 assert_int_equal(7, len);
672 str = "unique ";
673 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
674 assert_int_equal(YANG_UNIQUE, kw);
675 assert_int_equal(6, len);
676 str = "units ";
677 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
678 assert_int_equal(YANG_UNITS, kw);
679 assert_int_equal(5, len);
680 str = "uses ";
681 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
682 assert_int_equal(YANG_USES, kw);
683 assert_int_equal(4, len);
684 str = "value ";
685 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
686 assert_int_equal(YANG_VALUE, kw);
687 assert_int_equal(5, len);
688 str = "when ";
689 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
690 assert_int_equal(YANG_WHEN, kw);
691 assert_int_equal(4, len);
692 str = "yang-version ";
693 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
694 assert_int_equal(YANG_YANG_VERSION, kw);
695 assert_int_equal(12, len);
696 str = "yin-element ";
697 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
698 assert_int_equal(YANG_YIN_ELEMENT, kw);
699 assert_int_equal(11, len);
Radek Krejci626df482018-10-11 15:06:31 +0200700 str = ";config false;";
701 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
702 assert_int_equal(YANG_SEMICOLON, kw);
703 assert_int_equal(1, len);
704 assert_string_equal("config false;", str);
705 str = "{ config false;";
706 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
707 assert_int_equal(YANG_LEFT_BRACE, kw);
708 assert_int_equal(1, len);
709 assert_string_equal(" config false;", str);
710 str = "}";
711 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
712 assert_int_equal(YANG_RIGHT_BRACE, kw);
713 assert_int_equal(1, len);
714 assert_string_equal("", str);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200715
716 /* geenric extension */
717 str = p = "nacm:default-deny-write;";
718 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
719 assert_int_equal(YANG_CUSTOM, kw);
720 assert_int_equal(23, len);
721 assert_ptr_equal(p, word);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200722}
Radek Krejci44ceedc2018-10-02 15:54:31 +0200723
Radek Krejci05b13982018-11-28 16:22:07 +0100724static void
725test_minmax(void **state)
726{
727 *state = test_minmax;
728
Radek Krejcie7b95092019-05-15 11:03:07 +0200729 struct lys_parser_ctx ctx = {0};
Radek Krejci05b13982018-11-28 16:22:07 +0100730 uint16_t flags = 0;
731 uint32_t value = 0;
732 struct lysp_ext_instance *ext = NULL;
733 const char *str;
734
735 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
736 assert_non_null(ctx.ctx);
737 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100738 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci05b13982018-11-28 16:22:07 +0100739
Radek Krejcidf6cad12018-11-28 17:10:55 +0100740 str = " 1invalid; ...";
Radek Krejci05b13982018-11-28 16:22:07 +0100741 assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext));
Radek Krejcidf6cad12018-11-28 17:10:55 +0100742 logbuf_assert("Invalid value \"1invalid\" of \"min-elements\". Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100743
744 flags = value = 0;
745 str = " -1; ...";
746 assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext));
747 logbuf_assert("Invalid value \"-1\" of \"min-elements\". Line number 1.");
748
Radek Krejcidf6cad12018-11-28 17:10:55 +0100749 /* implementation limit */
750 flags = value = 0;
751 str = " 4294967296; ...";
752 assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext));
753 logbuf_assert("Value \"4294967296\" is out of \"min-elements\" bounds. Line number 1.");
754
Radek Krejci05b13982018-11-28 16:22:07 +0100755 flags = value = 0;
756 str = " 1; ...";
757 assert_int_equal(LY_SUCCESS, parse_minelements(&ctx, &str, &value, &flags, &ext));
758 assert_int_equal(LYS_SET_MIN, flags);
759 assert_int_equal(1, value);
760
761 flags = value = 0;
762 str = " 1 {m:ext;} ...";
763 assert_int_equal(LY_SUCCESS, parse_minelements(&ctx, &str, &value, &flags, &ext));
764 assert_int_equal(LYS_SET_MIN, flags);
765 assert_int_equal(1, value);
766 assert_non_null(ext);
767 FREE_ARRAY(ctx.ctx, ext, lysp_ext_instance_free);
768 ext = NULL;
769
770 flags = value = 0;
771 str = " 1 {config true;} ...";
772 assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext));
773 logbuf_assert("Invalid keyword \"config\" as a child of \"min-elements\". Line number 1.");
774
Radek Krejcidf6cad12018-11-28 17:10:55 +0100775 str = " 1invalid; ...";
Radek Krejci05b13982018-11-28 16:22:07 +0100776 assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext));
Radek Krejcidf6cad12018-11-28 17:10:55 +0100777 logbuf_assert("Invalid value \"1invalid\" of \"max-elements\". Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100778
779 flags = value = 0;
780 str = " -1; ...";
781 assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext));
782 logbuf_assert("Invalid value \"-1\" of \"max-elements\". Line number 1.");
783
Radek Krejcidf6cad12018-11-28 17:10:55 +0100784 /* implementation limit */
785 flags = value = 0;
786 str = " 4294967296; ...";
787 assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext));
788 logbuf_assert("Value \"4294967296\" is out of \"max-elements\" bounds. Line number 1.");
789
Radek Krejci05b13982018-11-28 16:22:07 +0100790 flags = value = 0;
791 str = " 1; ...";
792 assert_int_equal(LY_SUCCESS, parse_maxelements(&ctx, &str, &value, &flags, &ext));
793 assert_int_equal(LYS_SET_MAX, flags);
794 assert_int_equal(1, value);
795
796 flags = value = 0;
797 str = " unbounded; ...";
798 assert_int_equal(LY_SUCCESS, parse_maxelements(&ctx, &str, &value, &flags, &ext));
799 assert_int_equal(LYS_SET_MAX, flags);
800 assert_int_equal(0, value);
801
802 flags = value = 0;
803 str = " 1 {m:ext;} ...";
804 assert_int_equal(LY_SUCCESS, parse_maxelements(&ctx, &str, &value, &flags, &ext));
805 assert_int_equal(LYS_SET_MAX, flags);
806 assert_int_equal(1, value);
807 assert_non_null(ext);
808 FREE_ARRAY(ctx.ctx, ext, lysp_ext_instance_free);
809 ext = NULL;
810
811 flags = value = 0;
812 str = " 1 {config true;} ...";
813 assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext));
814 logbuf_assert("Invalid keyword \"config\" as a child of \"max-elements\". Line number 1.");
815
816 *state = NULL;
817 ly_ctx_destroy(ctx.ctx, NULL);
818}
819
Radek Krejci9fcacc12018-10-11 15:59:11 +0200820static struct lysp_module *
Radek Krejcie7b95092019-05-15 11:03:07 +0200821mod_renew(struct lys_parser_ctx *ctx)
Radek Krejci9fcacc12018-10-11 15:59:11 +0200822{
Radek Krejci40544fa2019-01-11 09:38:37 +0100823 struct lysp_module *mod_p;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100824 static struct lys_module mod = {0};
825
826 lysc_module_free(mod.compiled, NULL);
827 lysp_module_free(mod.parsed);
828 FREE_STRING(mod.ctx, mod.name);
829 FREE_STRING(mod.ctx, mod.ns);
830 FREE_STRING(mod.ctx, mod.prefix);
831 FREE_STRING(mod.ctx, mod.filepath);
832 FREE_STRING(mod.ctx, mod.org);
833 FREE_STRING(mod.ctx, mod.contact);
834 FREE_STRING(mod.ctx, mod.dsc);
835 FREE_STRING(mod.ctx, mod.ref);
836 memset(&mod, 0, sizeof mod);
837 mod.ctx = ctx->ctx;
838
839 mod_p = calloc(1, sizeof *mod_p);
840 mod.parsed = mod_p;
841 mod_p->mod = &mod;
842 assert_non_null(mod_p);
843 return mod_p;
844}
845
846static struct lysp_submodule *
Radek Krejcie7b95092019-05-15 11:03:07 +0200847submod_renew(struct lys_parser_ctx *ctx, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100848{
849 lysp_submodule_free(ctx->ctx, submod);
850 submod = calloc(1, sizeof *submod);
851 assert_non_null(submod);
852 return submod;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200853}
854
Radek Krejcid33273d2018-10-25 14:55:52 +0200855static LY_ERR test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
856 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
857 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
858{
859 *module_data = user_data;
860 *format = LYS_IN_YANG;
861 *free_module_data = NULL;
862 return LY_SUCCESS;
863}
864
Radek Krejci9fcacc12018-10-11 15:59:11 +0200865static void
866test_module(void **state)
867{
Radek Krejci40544fa2019-01-11 09:38:37 +0100868 *state = test_module;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200869
Radek Krejcie7b95092019-05-15 11:03:07 +0200870 struct lys_parser_ctx ctx;
Radek Krejci40544fa2019-01-11 09:38:37 +0100871 struct lysp_module *mod = NULL;
872 struct lysp_submodule *submod = NULL;
873 struct lys_module *m;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200874 const char *str;
875
876 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
877 assert_non_null(ctx.ctx);
878 ctx.line = 1;
Radek Krejcia042ea12018-10-13 07:52:15 +0200879 ctx.indent = 0;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200880
Radek Krejci40544fa2019-01-11 09:38:37 +0100881 mod = mod_renew(&ctx);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200882
883 /* missing mandatory substatements */
884 str = " name {}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100885 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
886 assert_string_equal("name", mod->mod->name);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200887 logbuf_assert("Missing mandatory keyword \"namespace\" as a child of \"module\". Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100888 mod = mod_renew(&ctx);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200889
890 str = " name {namespace urn:x;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100891 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
892 assert_string_equal("urn:x", mod->mod->ns);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200893 logbuf_assert("Missing mandatory keyword \"prefix\" as a child of \"module\". Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100894 mod = mod_renew(&ctx);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200895
896 str = " name {namespace urn:x;prefix \"x\";}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100897 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod));
898 assert_string_equal("x", mod->mod->prefix);
Radek Krejci40544fa2019-01-11 09:38:37 +0100899 mod = mod_renew(&ctx);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200900
Radek Krejci027d5802018-11-14 16:57:28 +0100901#define SCHEMA_BEGINNING " name {yang-version 1.1;namespace urn:x;prefix \"x\";"
902#define SCHEMA_BEGINNING2 " name {namespace urn:x;prefix \"x\";"
Radek Krejcia042ea12018-10-13 07:52:15 +0200903#define TEST_NODE(NODETYPE, INPUT, NAME) \
904 str = SCHEMA_BEGINNING INPUT; \
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100905 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod)); \
Radek Krejcia042ea12018-10-13 07:52:15 +0200906 assert_non_null(mod->data); \
907 assert_int_equal(NODETYPE, mod->data->nodetype); \
908 assert_string_equal(NAME, mod->data->name); \
Radek Krejci40544fa2019-01-11 09:38:37 +0100909 mod = mod_renew(&ctx);
Radek Krejcia042ea12018-10-13 07:52:15 +0200910#define TEST_GENERIC(INPUT, TARGET, TEST) \
911 str = SCHEMA_BEGINNING INPUT; \
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100912 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod)); \
Radek Krejcia042ea12018-10-13 07:52:15 +0200913 assert_non_null(TARGET); \
914 TEST; \
Radek Krejci40544fa2019-01-11 09:38:37 +0100915 mod = mod_renew(&ctx);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100916#define TEST_DUP(MEMBER, VALUE1, VALUE2, LINE) \
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200917 TEST_DUP_GENERIC(SCHEMA_BEGINNING, MEMBER, VALUE1, VALUE2, \
Radek Krejci40544fa2019-01-11 09:38:37 +0100918 parse_module, mod, LINE, mod = mod_renew(&ctx))
Radek Krejcia042ea12018-10-13 07:52:15 +0200919
920 /* duplicated namespace, prefix */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100921 TEST_DUP("namespace", "y", "z", "1");
922 TEST_DUP("prefix", "y", "z", "1");
923 TEST_DUP("contact", "a", "b", "1");
924 TEST_DUP("description", "a", "b", "1");
925 TEST_DUP("organization", "a", "b", "1");
926 TEST_DUP("reference", "a", "b", "1");
Radek Krejcia042ea12018-10-13 07:52:15 +0200927
Radek Krejci70853c52018-10-15 14:46:16 +0200928 /* not allowed in module (submodule-specific) */
929 str = SCHEMA_BEGINNING "belongs-to master {prefix m;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100930 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejci70853c52018-10-15 14:46:16 +0200931 logbuf_assert("Invalid keyword \"belongs-to\" as a child of \"module\". Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100932 mod = mod_renew(&ctx);
Radek Krejci70853c52018-10-15 14:46:16 +0200933
Radek Krejcia042ea12018-10-13 07:52:15 +0200934 /* anydata */
935 TEST_NODE(LYS_ANYDATA, "anydata test;}", "test");
936 /* anyxml */
937 TEST_NODE(LYS_ANYXML, "anyxml test;}", "test");
938 /* augment */
939 TEST_GENERIC("augment /somepath;}", mod->augments,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200940 assert_string_equal("/somepath", mod->augments[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200941 /* choice */
942 TEST_NODE(LYS_CHOICE, "choice test;}", "test");
943 /* contact 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100944 TEST_GENERIC("contact \"firstname\" + \n\t\" surname\";}", mod->mod->contact,
945 assert_string_equal("firstname surname", mod->mod->contact));
Radek Krejcia042ea12018-10-13 07:52:15 +0200946 /* container */
947 TEST_NODE(LYS_CONTAINER, "container test;}", "test");
948 /* description 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100949 TEST_GENERIC("description \'some description\';}", mod->mod->dsc,
950 assert_string_equal("some description", mod->mod->dsc));
Radek Krejcia042ea12018-10-13 07:52:15 +0200951 /* deviation */
952 TEST_GENERIC("deviation /somepath {deviate not-supported;}}", mod->deviations,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200953 assert_string_equal("/somepath", mod->deviations[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200954 /* extension */
955 TEST_GENERIC("extension test;}", mod->extensions,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200956 assert_string_equal("test", mod->extensions[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200957 /* feature */
958 TEST_GENERIC("feature test;}", mod->features,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200959 assert_string_equal("test", mod->features[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200960 /* grouping */
961 TEST_GENERIC("grouping grp;}", mod->groupings,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200962 assert_string_equal("grp", mod->groupings[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200963 /* identity */
964 TEST_GENERIC("identity test;}", mod->identities,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200965 assert_string_equal("test", mod->identities[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200966 /* import */
Radek Krejci086c7132018-10-26 15:29:04 +0200967 ly_ctx_set_module_imp_clb(ctx.ctx, test_imp_clb, "module zzz { namespace urn:zzz; prefix z;}");
968 TEST_GENERIC("import zzz {prefix z;}}", mod->imports,
969 assert_string_equal("zzz", mod->imports[0].name));
Radek Krejci70853c52018-10-15 14:46:16 +0200970
Radek Krejcia042ea12018-10-13 07:52:15 +0200971 /* import - prefix collision */
Radek Krejci086c7132018-10-26 15:29:04 +0200972 str = SCHEMA_BEGINNING "import zzz {prefix x;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100973 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejci70853c52018-10-15 14:46:16 +0200974 logbuf_assert("Prefix \"x\" already used as module prefix. Line number 2.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100975 mod = mod_renew(&ctx);
Radek Krejci086c7132018-10-26 15:29:04 +0200976 str = SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix y;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100977 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejci086c7132018-10-26 15:29:04 +0200978 logbuf_assert("Prefix \"y\" already used to import \"zzz\" module. Line number 2.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100979 mod = mod_renew(&ctx);
Radek Krejci086c7132018-10-26 15:29:04 +0200980 str = "module" SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix z;}}";
981 assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
982 assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx));
983 logbuf_assert("Single revision of the module \"zzz\" referred twice.");
Radek Krejci70853c52018-10-15 14:46:16 +0200984
Radek Krejcia042ea12018-10-13 07:52:15 +0200985 /* include */
Radek Krejci9ed7a192018-10-31 16:23:51 +0100986 store = 1;
Radek Krejcid33273d2018-10-25 14:55:52 +0200987 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 +0200988 str = "module" SCHEMA_BEGINNING "include xxx;}";
989 assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
990 assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx));
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100991 logbuf_assert("Input data contains module in situation when a submodule is expected.");
Radek Krejci9ed7a192018-10-31 16:23:51 +0100992 store = -1;
Radek Krejcid33273d2018-10-25 14:55:52 +0200993
Radek Krejci9ed7a192018-10-31 16:23:51 +0100994 store = 1;
Radek Krejci313d9902018-11-08 09:42:58 +0100995 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 +0200996 str = "module" SCHEMA_BEGINNING "include xxx;}";
997 assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
998 assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx));
999 logbuf_assert("Included \"xxx\" submodule from \"name\" belongs-to a different module \"wrong-name\".");
Radek Krejci9ed7a192018-10-31 16:23:51 +01001000 store = -1;
Radek Krejcid33273d2018-10-25 14:55:52 +02001001
Radek Krejci313d9902018-11-08 09:42:58 +01001002 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 +02001003 TEST_GENERIC("include xxx;}", mod->includes,
Radek Krejci086c7132018-10-26 15:29:04 +02001004 assert_string_equal("xxx", mod->includes[0].name));
Radek Krejcid33273d2018-10-25 14:55:52 +02001005
Radek Krejcia042ea12018-10-13 07:52:15 +02001006 /* leaf */
1007 TEST_NODE(LYS_LEAF, "leaf test {type string;}}", "test");
1008 /* leaf-list */
1009 TEST_NODE(LYS_LEAFLIST, "leaf-list test {type string;}}", "test");
1010 /* list */
1011 TEST_NODE(LYS_LIST, "list test {key a;leaf a {type string;}}}", "test");
1012 /* notification */
1013 TEST_GENERIC("notification test;}", mod->notifs,
Radek Krejci2c4e7172018-10-19 15:56:26 +02001014 assert_string_equal("test", mod->notifs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +02001015 /* organization 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001016 TEST_GENERIC("organization \"CESNET a.l.e.\";}", mod->mod->org,
1017 assert_string_equal("CESNET a.l.e.", mod->mod->org));
Radek Krejcia042ea12018-10-13 07:52:15 +02001018 /* reference 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001019 TEST_GENERIC("reference RFC7950;}", mod->mod->ref,
1020 assert_string_equal("RFC7950", mod->mod->ref));
Radek Krejcia042ea12018-10-13 07:52:15 +02001021 /* revision */
1022 TEST_GENERIC("revision 2018-10-12;}", mod->revs,
Radek Krejcib7db73a2018-10-24 14:18:40 +02001023 assert_string_equal("2018-10-12", mod->revs[0].date));
Radek Krejcia042ea12018-10-13 07:52:15 +02001024 /* rpc */
1025 TEST_GENERIC("rpc test;}", mod->rpcs,
Radek Krejci2c4e7172018-10-19 15:56:26 +02001026 assert_string_equal("test", mod->rpcs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +02001027 /* typedef */
1028 TEST_GENERIC("typedef test{type string;}}", mod->typedefs,
Radek Krejci2c4e7172018-10-19 15:56:26 +02001029 assert_string_equal("test", mod->typedefs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +02001030 /* uses */
1031 TEST_NODE(LYS_USES, "uses test;}", "test");
1032 /* yang-version */
Radek Krejci027d5802018-11-14 16:57:28 +01001033 str = SCHEMA_BEGINNING2 "\n\tyang-version 10;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001034 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejcia042ea12018-10-13 07:52:15 +02001035 logbuf_assert("Invalid value \"10\" of \"yang-version\". Line number 3.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001036 mod = mod_renew(&ctx);
Radek Krejci027d5802018-11-14 16:57:28 +01001037 str = SCHEMA_BEGINNING2 "yang-version 1.0;yang-version 1.1;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001038 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejcia042ea12018-10-13 07:52:15 +02001039 logbuf_assert("Duplicate keyword \"yang-version\". Line number 3.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001040 mod = mod_renew(&ctx);
Radek Krejci027d5802018-11-14 16:57:28 +01001041 str = SCHEMA_BEGINNING2 "yang-version 1.0;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001042 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod));
1043 assert_int_equal(1, mod->mod->version);
Radek Krejci40544fa2019-01-11 09:38:37 +01001044 mod = mod_renew(&ctx);
Radek Krejci027d5802018-11-14 16:57:28 +01001045 str = SCHEMA_BEGINNING2 "yang-version \"1.1\";}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001046 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod));
1047 assert_int_equal(2, mod->mod->version);
Radek Krejci40544fa2019-01-11 09:38:37 +01001048 mod = mod_renew(&ctx);
1049
David Sedlák1b623122019-08-05 15:27:49 +02001050 struct lys_parser_ctx *ctx_p = NULL;
Radek Krejci40544fa2019-01-11 09:38:37 +01001051 str = "module " SCHEMA_BEGINNING "} module q {namespace urn:q;prefixq;}";
1052 m = mod->mod;
1053 free(mod);
1054 m->parsed = NULL;
David Sedlák1b623122019-08-05 15:27:49 +02001055 assert_int_equal(LY_EVALID, yang_parse_module(&ctx_p, str, m));
1056 logbuf_assert("Trailing garbage \"module q {names...\" after module, expected end-of-input. Line number 1.");
1057 lys_parser_ctx_free(ctx_p);
Radek Krejci40544fa2019-01-11 09:38:37 +01001058 mod = mod_renew(&ctx);
1059
1060 str = "prefix " SCHEMA_BEGINNING "}";
1061 m = mod->mod;
1062 free(mod);
1063 m->parsed = NULL;
David Sedlák1b623122019-08-05 15:27:49 +02001064 assert_int_equal(LY_EVALID, yang_parse_module(&ctx_p, str, m));
1065 lys_parser_ctx_free(ctx_p);
1066 logbuf_assert("Invalid keyword \"prefix\", expected \"module\" or \"submodule\". Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001067 mod = mod_renew(&ctx);
Radek Krejci09306362018-10-15 15:26:01 +02001068
David Sedlák9fb515f2019-07-11 10:33:58 +02001069 str = "module " SCHEMA_BEGINNING "}";
1070 str = "module " SCHEMA_BEGINNING "leaf enum {type enumeration {enum seven { position 7;}}}}";
1071 m = mod->mod;
1072 free(mod);
1073 m->parsed = NULL;
David Sedlák1b623122019-08-05 15:27:49 +02001074 assert_int_equal(LY_EVALID, yang_parse_module(&ctx_p, str, m));
1075 lys_parser_ctx_free(ctx_p);
1076 logbuf_assert("Invalid keyword \"position\" as a child of \"enum\". Line number 1.");
David Sedlák9fb515f2019-07-11 10:33:58 +02001077 mod = mod_renew(&ctx);
1078
Radek Krejci156ccaf2018-10-15 15:49:17 +02001079 /* extensions */
1080 TEST_GENERIC("prefix:test;}", mod->exts,
Radek Krejci2c4e7172018-10-19 15:56:26 +02001081 assert_string_equal("prefix:test", mod->exts[0].name);
1082 assert_int_equal(LYEXT_SUBSTMT_SELF, mod->exts[0].insubstmt));
Radek Krejci40544fa2019-01-11 09:38:37 +01001083 mod = mod_renew(&ctx);
Radek Krejci156ccaf2018-10-15 15:49:17 +02001084
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001085 /* invalid substatement */
1086 str = SCHEMA_BEGINNING "must false;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001087 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001088 logbuf_assert("Invalid keyword \"must\" as a child of \"module\". Line number 3.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001089 mod = mod_renew(&ctx);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001090
Radek Krejci09306362018-10-15 15:26:01 +02001091 /* submodule */
Radek Krejci40544fa2019-01-11 09:38:37 +01001092 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001093
1094 /* missing mandatory substatements */
1095 str = " subname {}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001096 lydict_remove(ctx.ctx, submod->name);
1097 assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod));
1098 assert_string_equal("subname", submod->name);
Radek Krejci09306362018-10-15 15:26:01 +02001099 logbuf_assert("Missing mandatory keyword \"belongs-to\" as a child of \"submodule\". Line number 3.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001100 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001101
Radek Krejci313d9902018-11-08 09:42:58 +01001102 str = " subname {belongs-to name {prefix x;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001103 lydict_remove(ctx.ctx, submod->name);
1104 assert_int_equal(LY_SUCCESS, parse_submodule(&ctx, &str, submod));
1105 assert_string_equal("name", submod->belongsto);
1106 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001107
1108#undef SCHEMA_BEGINNING
Radek Krejci313d9902018-11-08 09:42:58 +01001109#define SCHEMA_BEGINNING " subname {belongs-to name {prefix x;}"
Radek Krejci09306362018-10-15 15:26:01 +02001110
1111 /* duplicated namespace, prefix */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001112 str = " subname {belongs-to name {prefix x;}belongs-to module1;belongs-to module2;} ...";
1113 assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod)); \
1114 logbuf_assert("Duplicate keyword \"belongs-to\". Line number 3."); \
1115 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001116
1117 /* not allowed in submodule (module-specific) */
1118 str = SCHEMA_BEGINNING "namespace \"urn:z\";}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001119 assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod));
Radek Krejci09306362018-10-15 15:26:01 +02001120 logbuf_assert("Invalid keyword \"namespace\" as a child of \"submodule\". Line number 3.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001121 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001122 str = SCHEMA_BEGINNING "prefix m;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001123 assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod));
Radek Krejci09306362018-10-15 15:26:01 +02001124 logbuf_assert("Invalid keyword \"prefix\" as a child of \"submodule\". Line number 3.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001125 submod = submod_renew(&ctx, submod);
Radek Krejcia042ea12018-10-13 07:52:15 +02001126
Radek Krejci40544fa2019-01-11 09:38:37 +01001127 str = "submodule " SCHEMA_BEGINNING "} module q {namespace urn:q;prefixq;}";
1128 lysp_submodule_free(ctx.ctx, submod);
1129 submod = NULL;
David Sedlák1b623122019-08-05 15:27:49 +02001130 assert_int_equal(LY_EVALID, yang_parse_submodule(&ctx_p, ctx.ctx, &ctx, str, &submod));
1131 lys_parser_ctx_free(ctx_p);
1132 logbuf_assert("Trailing garbage \"module q {names...\" after submodule, expected end-of-input. Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001133
1134 str = "prefix " SCHEMA_BEGINNING "}";
David Sedlák1b623122019-08-05 15:27:49 +02001135 assert_int_equal(LY_EVALID, yang_parse_submodule(&ctx_p, ctx.ctx, &ctx, str, &submod));
1136 lys_parser_ctx_free(ctx_p);
1137 logbuf_assert("Invalid keyword \"prefix\", expected \"module\" or \"submodule\". Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001138 submod = submod_renew(&ctx, submod);
1139
Radek Krejcia042ea12018-10-13 07:52:15 +02001140#undef TEST_GENERIC
1141#undef TEST_NODE
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001142#undef TEST_DUP
Radek Krejcia042ea12018-10-13 07:52:15 +02001143#undef SCHEMA_BEGINNING
1144
Radek Krejci9fcacc12018-10-11 15:59:11 +02001145 lysp_module_free(mod);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001146 lysp_submodule_free(ctx.ctx, submod);
Radek Krejci9fcacc12018-10-11 15:59:11 +02001147 ly_ctx_destroy(ctx.ctx, NULL);
Radek Krejci40544fa2019-01-11 09:38:37 +01001148
1149 *state = NULL;
Radek Krejciefd22f62018-09-27 11:47:58 +02001150}
1151
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001152static void
1153test_identity(void **state)
1154{
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001155 *state = test_identity;
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001156
Radek Krejcie7b95092019-05-15 11:03:07 +02001157 struct lys_parser_ctx ctx;
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001158 struct lysp_ident *ident = NULL;
1159 const char *str;
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001160
1161 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1162 assert_non_null(ctx.ctx);
1163 ctx.line = 1;
1164 ctx.indent = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001165 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001166
1167 /* invalid cardinality */
1168#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001169 TEST_DUP_GENERIC(" test {", MEMBER, VALUE1, VALUE2, parse_identity, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001170 &ident, "1", FREE_ARRAY(ctx.ctx, ident, lysp_ident_free); ident = NULL)
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001171
Radek Krejci38222632019-02-12 16:55:05 +01001172 TEST_DUP("description", "a", "b");
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001173 TEST_DUP("reference", "a", "b");
1174 TEST_DUP("status", "current", "obsolete");
1175
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001176 /* full content */
David Sedlák40bb13b2019-07-10 14:34:18 +02001177 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 +02001178 assert_int_equal(LY_SUCCESS, parse_identity(&ctx, &str, &ident));
1179 assert_non_null(ident);
1180 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001181 FREE_ARRAY(ctx.ctx, ident, lysp_ident_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001182 ident = NULL;
1183
1184 /* invalid substatement */
1185 str = " test {organization XXX;}";
1186 assert_int_equal(LY_EVALID, parse_identity(&ctx, &str, &ident));
1187 logbuf_assert("Invalid keyword \"organization\" as a child of \"identity\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001188 FREE_ARRAY(ctx.ctx, ident, lysp_ident_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001189 ident = NULL;
1190
1191#undef TEST_DUP
1192
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001193 *state = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001194 ly_ctx_destroy(ctx.ctx, NULL);
1195}
1196
1197static void
1198test_feature(void **state)
1199{
1200 (void) state; /* unused */
1201
Radek Krejcie7b95092019-05-15 11:03:07 +02001202 struct lys_parser_ctx ctx;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001203 struct lysp_feature *features = NULL;
1204 const char *str;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001205
1206 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1207 assert_non_null(ctx.ctx);
1208 ctx.line = 1;
1209 ctx.indent = 0;
1210
1211 /* invalid cardinality */
1212#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1213 TEST_DUP_GENERIC(" test {", MEMBER, VALUE1, VALUE2, parse_feature, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001214 &features, "1", FREE_ARRAY(ctx.ctx, features, lysp_feature_free); features = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001215
1216 TEST_DUP("description", "a", "b");
1217 TEST_DUP("reference", "a", "b");
1218 TEST_DUP("status", "current", "obsolete");
1219
1220 /* full content */
1221 str = " test {description text;reference \'another text\';status current; if-feature x;if-feature y;prefix:ext;} ...";
1222 assert_int_equal(LY_SUCCESS, parse_feature(&ctx, &str, &features));
1223 assert_non_null(features);
1224 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001225 FREE_ARRAY(ctx.ctx, features, lysp_feature_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001226 features = NULL;
1227
1228 /* invalid substatement */
1229 str = " test {organization XXX;}";
1230 assert_int_equal(LY_EVALID, parse_feature(&ctx, &str, &features));
1231 logbuf_assert("Invalid keyword \"organization\" as a child of \"feature\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001232 FREE_ARRAY(ctx.ctx, features, lysp_feature_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001233 features = NULL;
1234
1235#undef TEST_DUP
1236
1237 ly_ctx_destroy(ctx.ctx, NULL);
1238}
1239
1240static void
1241test_deviation(void **state)
1242{
1243 (void) state; /* unused */
1244
Radek Krejcie7b95092019-05-15 11:03:07 +02001245 struct lys_parser_ctx ctx;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001246 struct lysp_deviation *d = NULL;
1247 const char *str;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001248
1249 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1250 assert_non_null(ctx.ctx);
1251 ctx.line = 1;
1252 ctx.indent = 0;
1253
1254 /* invalid cardinality */
1255#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1256 TEST_DUP_GENERIC(" test {deviate not-supported;", MEMBER, VALUE1, VALUE2, parse_deviation, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001257 &d, "1", FREE_ARRAY(ctx.ctx, d, lysp_deviation_free); d = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001258
1259 TEST_DUP("description", "a", "b");
1260 TEST_DUP("reference", "a", "b");
1261
1262 /* full content */
1263 str = " test {deviate not-supported;description text;reference \'another text\';prefix:ext;} ...";
1264 assert_int_equal(LY_SUCCESS, parse_deviation(&ctx, &str, &d));
1265 assert_non_null(d);
1266 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001267 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001268 d = NULL;
1269
1270 /* missing mandatory substatement */
1271 str = " test {description text;}";
1272 assert_int_equal(LY_EVALID, parse_deviation(&ctx, &str, &d));
1273 logbuf_assert("Missing mandatory keyword \"deviate\" as a child of \"deviation\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001274 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001275 d = NULL;
1276
1277 /* invalid substatement */
1278 str = " test {deviate not-supported; status obsolete;}";
1279 assert_int_equal(LY_EVALID, parse_deviation(&ctx, &str, &d));
1280 logbuf_assert("Invalid keyword \"status\" as a child of \"deviation\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001281 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001282 d = NULL;
1283
1284#undef TEST_DUP
1285
1286 ly_ctx_destroy(ctx.ctx, NULL);
1287}
1288
1289static void
1290test_deviate(void **state)
1291{
1292 (void) state; /* unused */
1293
Radek Krejcie7b95092019-05-15 11:03:07 +02001294 struct lys_parser_ctx ctx;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001295 struct lysp_deviate *d = NULL;
1296 const char *str;
1297
1298 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1299 assert_non_null(ctx.ctx);
1300 ctx.line = 1;
1301 ctx.indent = 0;
1302
1303 /* invalid cardinality */
1304#define TEST_DUP(TYPE, MEMBER, VALUE1, VALUE2) \
1305 TEST_DUP_GENERIC(TYPE" {", MEMBER, VALUE1, VALUE2, parse_deviate, \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001306 &d, "1", lysp_deviate_free(ctx.ctx, d); free(d); d = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001307
1308 TEST_DUP("add", "config", "true", "false");
1309 TEST_DUP("replace", "default", "int8", "uint8");
1310 TEST_DUP("add", "mandatory", "true", "false");
1311 TEST_DUP("add", "max-elements", "1", "2");
1312 TEST_DUP("add", "min-elements", "1", "2");
1313 TEST_DUP("replace", "type", "int8", "uint8");
1314 TEST_DUP("add", "units", "kilometers", "miles");
1315
1316 /* full contents */
1317 str = " not-supported {prefix:ext;} ...";
1318 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1319 assert_non_null(d);
1320 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001321 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001322 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;} ...";
1323 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1324 assert_non_null(d);
1325 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001326 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001327 str = " delete {units meters; must 1; must 2; unique x; unique y; default a; default b; prefix:ext;} ...";
1328 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1329 assert_non_null(d);
1330 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001331 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001332 str = " replace {type string; units meters; default a; config true; mandatory true; min-elements 1; max-elements 2; prefix:ext;} ...";
1333 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1334 assert_non_null(d);
1335 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001336 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001337
1338 /* invalid substatements */
1339#define TEST_NOT_SUP(DEV, STMT, VALUE) \
1340 str = " "DEV" {"STMT" "VALUE";}..."; \
1341 assert_int_equal(LY_EVALID, parse_deviate(&ctx, &str, &d)); \
1342 logbuf_assert("Deviate \""DEV"\" does not support keyword \""STMT"\". Line number 1."); \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001343 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001344
1345 TEST_NOT_SUP("not-supported", "units", "meters");
1346 TEST_NOT_SUP("not-supported", "must", "1");
1347 TEST_NOT_SUP("not-supported", "unique", "x");
1348 TEST_NOT_SUP("not-supported", "default", "a");
1349 TEST_NOT_SUP("not-supported", "config", "true");
1350 TEST_NOT_SUP("not-supported", "mandatory", "true");
1351 TEST_NOT_SUP("not-supported", "min-elements", "1");
1352 TEST_NOT_SUP("not-supported", "max-elements", "2");
1353 TEST_NOT_SUP("not-supported", "type", "string");
1354 TEST_NOT_SUP("add", "type", "string");
1355 TEST_NOT_SUP("delete", "config", "true");
1356 TEST_NOT_SUP("delete", "mandatory", "true");
1357 TEST_NOT_SUP("delete", "min-elements", "1");
1358 TEST_NOT_SUP("delete", "max-elements", "2");
1359 TEST_NOT_SUP("delete", "type", "string");
1360 TEST_NOT_SUP("replace", "must", "1");
1361 TEST_NOT_SUP("replace", "unique", "a");
1362
1363 str = " nonsence; ...";
1364 assert_int_equal(LY_EVALID, parse_deviate(&ctx, &str, &d));
1365 logbuf_assert("Invalid value \"nonsence\" of \"deviate\". Line number 1.");
1366 assert_null(d);
1367
1368#undef TEST_NOT_SUP
1369#undef TEST_DUP
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001370
1371 ly_ctx_destroy(ctx.ctx, NULL);
1372}
1373
Radek Krejci8c370832018-11-02 15:10:03 +01001374static void
1375test_container(void **state)
1376{
1377 (void) state; /* unused */
1378
Radek Krejcie7b95092019-05-15 11:03:07 +02001379 struct lys_parser_ctx ctx = {0};
Radek Krejci8c370832018-11-02 15:10:03 +01001380 struct lysp_node_container *c = NULL;
1381 const char *str;
1382
1383 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1384 assert_non_null(ctx.ctx);
1385 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001386 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci8c370832018-11-02 15:10:03 +01001387
1388 /* invalid cardinality */
1389#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1390 str = "cont {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1391 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c)); \
1392 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001393 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001394
1395 TEST_DUP("config", "true", "false");
1396 TEST_DUP("description", "text1", "text2");
1397 TEST_DUP("presence", "true", "false");
1398 TEST_DUP("reference", "1", "2");
1399 TEST_DUP("status", "current", "obsolete");
1400 TEST_DUP("when", "true", "false");
1401#undef TEST_DUP
1402
1403 /* full content */
Radek Krejci313d9902018-11-08 09:42:58 +01001404 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;}"
1405 "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 +01001406 assert_int_equal(LY_SUCCESS, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1407 assert_non_null(c);
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001408 assert_int_equal(LYS_CONTAINER, c->nodetype);
1409 assert_string_equal("cont", c->name);
Radek Krejci8c370832018-11-02 15:10:03 +01001410 assert_non_null(c->actions);
1411 assert_non_null(c->child);
1412 assert_string_equal("test", c->dsc);
1413 assert_non_null(c->exts);
1414 assert_non_null(c->groupings);
1415 assert_non_null(c->iffeatures);
1416 assert_non_null(c->musts);
1417 assert_non_null(c->notifs);
1418 assert_string_equal("true", c->presence);
1419 assert_string_equal("test", c->ref);
1420 assert_non_null(c->typedefs);
1421 assert_non_null(c->when);
1422 assert_null(c->parent);
1423 assert_null(c->next);
1424 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR, c->flags);
Radek Krejci313d9902018-11-08 09:42:58 +01001425 ly_set_erase(&ctx.tpdfs_nodes, NULL);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001426 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001427
1428 /* invalid */
1429 str = " cont {augment /root;} ...";
1430 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1431 logbuf_assert("Invalid keyword \"augment\" as a child of \"container\". Line number 1.");
Radek Krejci4f28eda2018-11-12 11:46:16 +01001432 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001433 str = " cont {nonsence true;} ...";
1434 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1435 logbuf_assert("Invalid character sequence \"nonsence\", expected a keyword. Line number 1.");
Radek Krejci4f28eda2018-11-12 11:46:16 +01001436 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001437
Radek Krejcif538ce52019-03-05 10:46:14 +01001438 ctx.mod_version = 1; /* simulate YANG 1.0 */
1439 str = " cont {action x;} ...";
1440 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1441 logbuf_assert("Invalid keyword \"action\" as a child of \"container\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
1442 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
1443
Radek Krejci8c370832018-11-02 15:10:03 +01001444 ly_ctx_destroy(ctx.ctx, NULL);
1445}
1446
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001447static void
1448test_leaf(void **state)
1449{
1450 *state = test_leaf;
1451
Radek Krejcie7b95092019-05-15 11:03:07 +02001452 struct lys_parser_ctx ctx = {0};
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001453 struct lysp_node_leaf *l = NULL;
1454 const char *str;
1455
1456 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1457 assert_non_null(ctx.ctx);
1458 ctx.line = 1;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001459 //ctx.mod->version = 2; /* simulate YANG 1.1 */
1460
1461 /* invalid cardinality */
1462#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1463 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1464 assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l)); \
1465 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1466 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1467
1468 TEST_DUP("config", "true", "false");
1469 TEST_DUP("default", "x", "y");
1470 TEST_DUP("description", "text1", "text2");
1471 TEST_DUP("mandatory", "true", "false");
1472 TEST_DUP("reference", "1", "2");
1473 TEST_DUP("status", "current", "obsolete");
Radek Krejci0e5d8382018-11-28 16:37:53 +01001474 TEST_DUP("type", "int8", "uint8");
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001475 TEST_DUP("units", "text1", "text2");
1476 TEST_DUP("when", "true", "false");
1477#undef TEST_DUP
1478
1479 /* full content - without mandatory which is mutual exclusive with default */
1480 str = "l {config false;default \"xxx\";description test;if-feature f;"
1481 "must 'expr';reference test;status current;type string; units yyy;when true;m:ext;} ...";
1482 assert_int_equal(LY_SUCCESS, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
1483 assert_non_null(l);
1484 assert_int_equal(LYS_LEAF, l->nodetype);
1485 assert_string_equal("l", l->name);
1486 assert_string_equal("test", l->dsc);
1487 assert_string_equal("xxx", l->dflt);
1488 assert_string_equal("yyy", l->units);
1489 assert_string_equal("string", l->type.name);
1490 assert_non_null(l->exts);
1491 assert_non_null(l->iffeatures);
1492 assert_non_null(l->musts);
1493 assert_string_equal("test", l->ref);
1494 assert_non_null(l->when);
1495 assert_null(l->parent);
1496 assert_null(l->next);
1497 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR, l->flags);
1498 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1499
1500 /* full content - now with mandatory */
1501 str = "l {mandatory true; type string;} ...";
1502 assert_int_equal(LY_SUCCESS, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
1503 assert_non_null(l);
1504 assert_int_equal(LYS_LEAF, l->nodetype);
1505 assert_string_equal("l", l->name);
1506 assert_string_equal("string", l->type.name);
1507 assert_int_equal(LYS_MAND_TRUE, l->flags);
1508 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1509
1510 /* invalid */
1511 str = " l {mandatory true; default xx; type string;} ...";
1512 assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
Radek Krejcia9026eb2018-12-12 16:04:47 +01001513 logbuf_assert("Invalid combination of keywords \"mandatory\" and \"default\" as substatements of \"leaf\". Line number 1.");
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001514 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1515
1516 str = " l {description \"missing type\";} ...";
1517 assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
1518 logbuf_assert("Missing mandatory keyword \"type\" as a child of \"leaf\". Line number 1.");
1519 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1520
1521 *state = NULL;
1522 ly_ctx_destroy(ctx.ctx, NULL);
1523}
1524
Radek Krejci0e5d8382018-11-28 16:37:53 +01001525static void
1526test_leaflist(void **state)
1527{
1528 *state = test_leaf;
1529
Radek Krejcie7b95092019-05-15 11:03:07 +02001530 struct lys_parser_ctx ctx = {0};
Radek Krejci0e5d8382018-11-28 16:37:53 +01001531 struct lysp_node_leaflist *ll = NULL;
1532 const char *str;
1533
1534 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1535 assert_non_null(ctx.ctx);
1536 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001537 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci0e5d8382018-11-28 16:37:53 +01001538
1539 /* invalid cardinality */
1540#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1541 str = "ll {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1542 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll)); \
1543 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1544 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1545
1546 TEST_DUP("config", "true", "false");
1547 TEST_DUP("description", "text1", "text2");
1548 TEST_DUP("max-elements", "10", "20");
1549 TEST_DUP("min-elements", "10", "20");
1550 TEST_DUP("ordered-by", "user", "system");
1551 TEST_DUP("reference", "1", "2");
1552 TEST_DUP("status", "current", "obsolete");
1553 TEST_DUP("type", "int8", "uint8");
1554 TEST_DUP("units", "text1", "text2");
1555 TEST_DUP("when", "true", "false");
1556#undef TEST_DUP
1557
1558 /* full content - without min-elements which is mutual exclusive with default */
1559 str = "ll {config false;default \"xxx\"; default \"yyy\";description test;if-feature f;"
1560 "max-elements 10;must 'expr';ordered-by user;reference test;"
1561 "status current;type string; units zzz;when true;m:ext;} ...";
1562 assert_int_equal(LY_SUCCESS, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1563 assert_non_null(ll);
1564 assert_int_equal(LYS_LEAFLIST, ll->nodetype);
1565 assert_string_equal("ll", ll->name);
1566 assert_string_equal("test", ll->dsc);
1567 assert_non_null(ll->dflts);
1568 assert_int_equal(2, LY_ARRAY_SIZE(ll->dflts));
1569 assert_string_equal("xxx", ll->dflts[0]);
1570 assert_string_equal("yyy", ll->dflts[1]);
1571 assert_string_equal("zzz", ll->units);
1572 assert_int_equal(10, ll->max);
1573 assert_int_equal(0, ll->min);
1574 assert_string_equal("string", ll->type.name);
1575 assert_non_null(ll->exts);
1576 assert_non_null(ll->iffeatures);
1577 assert_non_null(ll->musts);
1578 assert_string_equal("test", ll->ref);
1579 assert_non_null(ll->when);
1580 assert_null(ll->parent);
1581 assert_null(ll->next);
1582 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_USER | LYS_SET_MAX, ll->flags);
1583 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1584
1585 /* full content - now with min-elements */
1586 str = "ll {min-elements 10; type string;} ...";
1587 assert_int_equal(LY_SUCCESS, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1588 assert_non_null(ll);
1589 assert_int_equal(LYS_LEAFLIST, ll->nodetype);
1590 assert_string_equal("ll", ll->name);
1591 assert_string_equal("string", ll->type.name);
1592 assert_int_equal(0, ll->max);
1593 assert_int_equal(10, ll->min);
1594 assert_int_equal(LYS_SET_MIN, ll->flags);
1595 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1596
1597 /* invalid */
1598 str = " ll {min-elements 1; default xx; type string;} ...";
1599 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
Radek Krejcia9026eb2018-12-12 16:04:47 +01001600 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 +01001601 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1602
1603 str = " ll {description \"missing type\";} ...";
1604 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1605 logbuf_assert("Missing mandatory keyword \"type\" as a child of \"leaf-list\". Line number 1.");
1606 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1607
Radek Krejcidf6cad12018-11-28 17:10:55 +01001608 str = " ll {type string; min-elements 10; max-elements 1;} ..."; /* invalid combination of min/max */
1609 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1610 logbuf_assert("Invalid combination of min-elements and max-elements: min value 10 is bigger than the max value 1. Line number 1.");
1611 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1612
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001613 ctx.mod_version = 1; /* simulate YANG 1.0 - default statement is not allowed */
Radek Krejci0e5d8382018-11-28 16:37:53 +01001614 str = " ll {default xx; type string;} ...";
1615 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1616 logbuf_assert("Invalid keyword \"default\" as a child of \"leaf-list\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
1617 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1618
1619 *state = NULL;
1620 ly_ctx_destroy(ctx.ctx, NULL);
1621}
1622
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001623static void
1624test_list(void **state)
1625{
1626 *state = test_list;
1627
Radek Krejcie7b95092019-05-15 11:03:07 +02001628 struct lys_parser_ctx ctx = {0};
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001629 struct lysp_node_list *l = NULL;
1630 const char *str;
1631
1632 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1633 assert_non_null(ctx.ctx);
1634 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001635 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001636
1637 /* invalid cardinality */
1638#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1639 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1640 assert_int_equal(LY_EVALID, parse_list(&ctx, &str, NULL, (struct lysp_node**)&l)); \
1641 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1642 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1643
1644 TEST_DUP("config", "true", "false");
1645 TEST_DUP("description", "text1", "text2");
1646 TEST_DUP("key", "one", "two");
1647 TEST_DUP("max-elements", "10", "20");
1648 TEST_DUP("min-elements", "10", "20");
1649 TEST_DUP("ordered-by", "user", "system");
1650 TEST_DUP("reference", "1", "2");
1651 TEST_DUP("status", "current", "obsolete");
1652 TEST_DUP("when", "true", "false");
1653#undef TEST_DUP
1654
1655 /* full content */
1656 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;}"
1657 "leaf-list ll {type string;} list li;max-elements 10; min-elements 1;must 'expr';notification not; ordered-by system; reference test;"
1658 "status current;typedef t {type int8;}unique xxx;unique yyy;uses g;when true;m:ext;} ...";
1659 assert_int_equal(LY_SUCCESS, parse_list(&ctx, &str, NULL, (struct lysp_node**)&l));
1660 assert_non_null(l);
1661 assert_int_equal(LYS_LIST, l->nodetype);
1662 assert_string_equal("l", l->name);
1663 assert_string_equal("test", l->dsc);
1664 assert_string_equal("l", l->key);
1665 assert_non_null(l->uniques);
1666 assert_int_equal(2, LY_ARRAY_SIZE(l->uniques));
1667 assert_string_equal("xxx", l->uniques[0]);
1668 assert_string_equal("yyy", l->uniques[1]);
1669 assert_int_equal(10, l->max);
1670 assert_int_equal(1, l->min);
1671 assert_non_null(l->exts);
1672 assert_non_null(l->iffeatures);
1673 assert_non_null(l->musts);
1674 assert_string_equal("test", l->ref);
1675 assert_non_null(l->when);
1676 assert_null(l->parent);
1677 assert_null(l->next);
1678 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM | LYS_SET_MAX | LYS_SET_MIN, l->flags);
1679 ly_set_erase(&ctx.tpdfs_nodes, NULL);
1680 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1681
Radek Krejcif538ce52019-03-05 10:46:14 +01001682 /* invalid content */
1683 ctx.mod_version = 1; /* simulate YANG 1.0 */
1684 str = "l {action x;} ...";
1685 assert_int_equal(LY_EVALID, parse_list(&ctx, &str, NULL, (struct lysp_node**)&l));
1686 logbuf_assert("Invalid keyword \"action\" as a child of \"list\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
1687 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1688
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001689 *state = NULL;
1690 ly_ctx_destroy(ctx.ctx, NULL);
1691}
1692
Radek Krejci056d0a82018-12-06 16:57:25 +01001693static void
1694test_choice(void **state)
1695{
1696 *state = test_choice;
1697
Radek Krejcie7b95092019-05-15 11:03:07 +02001698 struct lys_parser_ctx ctx = {0};
Radek Krejci056d0a82018-12-06 16:57:25 +01001699 struct lysp_node_choice *ch = NULL;
1700 const char *str;
1701
1702 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1703 assert_non_null(ctx.ctx);
1704 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001705 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci056d0a82018-12-06 16:57:25 +01001706
1707 /* invalid cardinality */
1708#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1709 str = "ch {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1710 assert_int_equal(LY_EVALID, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch)); \
1711 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1712 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1713
1714 TEST_DUP("config", "true", "false");
1715 TEST_DUP("default", "a", "b");
1716 TEST_DUP("description", "text1", "text2");
1717 TEST_DUP("mandatory", "true", "false");
1718 TEST_DUP("reference", "1", "2");
1719 TEST_DUP("status", "current", "obsolete");
1720 TEST_DUP("when", "true", "false");
1721#undef TEST_DUP
1722
Radek Krejcia9026eb2018-12-12 16:04:47 +01001723 /* full content - without default due to a collision with mandatory */
1724 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 +01001725 "leaf-list ll {type string;} list li;mandatory true;reference test;status current;when true;m:ext;} ...";
1726 assert_int_equal(LY_SUCCESS, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch));
1727 assert_non_null(ch);
1728 assert_int_equal(LYS_CHOICE, ch->nodetype);
1729 assert_string_equal("ch", ch->name);
1730 assert_string_equal("test", ch->dsc);
1731 assert_non_null(ch->exts);
1732 assert_non_null(ch->iffeatures);
1733 assert_string_equal("test", ch->ref);
1734 assert_non_null(ch->when);
1735 assert_null(ch->parent);
1736 assert_null(ch->next);
1737 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_MAND_TRUE, ch->flags);
1738 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1739
Radek Krejcia9026eb2018-12-12 16:04:47 +01001740 /* full content - the default missing from the previous node */
1741 str = "ch {default c;case c;} ...";
1742 assert_int_equal(LY_SUCCESS, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch));
1743 assert_non_null(ch);
1744 assert_int_equal(LYS_CHOICE, ch->nodetype);
1745 assert_string_equal("ch", ch->name);
1746 assert_string_equal("c", ch->dflt);
1747 assert_int_equal(0, ch->flags);
1748 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1749
1750 /* invalid content */
1751 str = "ch {mandatory true; default c1; case c1 {leaf x{type string;}}} ...";
1752 assert_int_equal(LY_EVALID, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch));
1753 logbuf_assert("Invalid combination of keywords \"mandatory\" and \"default\" as substatements of \"choice\". Line number 1.");
1754 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1755
1756 *state = NULL;
1757 ly_ctx_destroy(ctx.ctx, NULL);
1758}
1759
1760static void
1761test_case(void **state)
1762{
1763 *state = test_case;
1764
Radek Krejcie7b95092019-05-15 11:03:07 +02001765 struct lys_parser_ctx ctx = {0};
Radek Krejcia9026eb2018-12-12 16:04:47 +01001766 struct lysp_node_case *cs = NULL;
1767 const char *str;
1768
1769 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1770 assert_non_null(ctx.ctx);
1771 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001772 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejcia9026eb2018-12-12 16:04:47 +01001773
1774 /* invalid cardinality */
1775#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1776 str = "cs {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1777 assert_int_equal(LY_EVALID, parse_case(&ctx, &str, NULL, (struct lysp_node**)&cs)); \
1778 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1779 lysp_node_free(ctx.ctx, (struct lysp_node*)cs); cs = NULL;
1780
1781 TEST_DUP("description", "text1", "text2");
1782 TEST_DUP("reference", "1", "2");
1783 TEST_DUP("status", "current", "obsolete");
1784 TEST_DUP("when", "true", "false");
1785#undef TEST_DUP
1786
1787 /* full content */
1788 str = "cs {anydata any;anyxml anyxml; choice ch;container c;description test;if-feature f;leaf l {type string;}"
1789 "leaf-list ll {type string;} list li;reference test;status current;uses grp;when true;m:ext;} ...";
1790 assert_int_equal(LY_SUCCESS, parse_case(&ctx, &str, NULL, (struct lysp_node**)&cs));
1791 assert_non_null(cs);
1792 assert_int_equal(LYS_CASE, cs->nodetype);
1793 assert_string_equal("cs", cs->name);
1794 assert_string_equal("test", cs->dsc);
1795 assert_non_null(cs->exts);
1796 assert_non_null(cs->iffeatures);
1797 assert_string_equal("test", cs->ref);
1798 assert_non_null(cs->when);
1799 assert_null(cs->parent);
1800 assert_null(cs->next);
1801 assert_int_equal(LYS_STATUS_CURR, cs->flags);
1802 lysp_node_free(ctx.ctx, (struct lysp_node*)cs); cs = NULL;
1803
1804 /* invalid content */
1805 str = "cs {config true} ...";
1806 assert_int_equal(LY_EVALID, parse_case(&ctx, &str, NULL, (struct lysp_node**)&cs));
1807 logbuf_assert("Invalid keyword \"config\" as a child of \"case\". Line number 1.");
1808 lysp_node_free(ctx.ctx, (struct lysp_node*)cs); cs = NULL;
1809
Radek Krejci056d0a82018-12-06 16:57:25 +01001810 *state = NULL;
1811 ly_ctx_destroy(ctx.ctx, NULL);
1812}
1813
Radek Krejci9800fb82018-12-13 14:26:23 +01001814static void
1815test_any(void **state, enum yang_keyword kw)
1816{
1817 *state = test_any;
1818
Radek Krejcie7b95092019-05-15 11:03:07 +02001819 struct lys_parser_ctx ctx = {0};
Radek Krejci9800fb82018-12-13 14:26:23 +01001820 struct lysp_node_anydata *any = NULL;
1821 const char *str;
1822
1823 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1824 assert_non_null(ctx.ctx);
1825 ctx.line = 1;
Radek Krejci9800fb82018-12-13 14:26:23 +01001826 if (kw == YANG_ANYDATA) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001827 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci9800fb82018-12-13 14:26:23 +01001828 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001829 ctx.mod_version = 1; /* simulate YANG 1.0 */
Radek Krejci9800fb82018-12-13 14:26:23 +01001830 }
1831
1832 /* invalid cardinality */
1833#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1834 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1835 assert_int_equal(LY_EVALID, parse_any(&ctx, &str, kw, NULL, (struct lysp_node**)&any)); \
1836 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1837 lysp_node_free(ctx.ctx, (struct lysp_node*)any); any = NULL;
1838
1839 TEST_DUP("config", "true", "false");
1840 TEST_DUP("description", "text1", "text2");
1841 TEST_DUP("mandatory", "true", "false");
1842 TEST_DUP("reference", "1", "2");
1843 TEST_DUP("status", "current", "obsolete");
1844 TEST_DUP("when", "true", "false");
1845#undef TEST_DUP
1846
1847 /* full content */
1848 str = "any {config true;description test;if-feature f;mandatory true;must 'expr';reference test;status current;when true;m:ext;} ...";
1849 assert_int_equal(LY_SUCCESS, parse_any(&ctx, &str, kw, NULL, (struct lysp_node**)&any));
1850 assert_non_null(any);
1851 assert_int_equal(kw == YANG_ANYDATA ? LYS_ANYDATA : LYS_ANYXML, any->nodetype);
1852 assert_string_equal("any", any->name);
1853 assert_string_equal("test", any->dsc);
1854 assert_non_null(any->exts);
1855 assert_non_null(any->iffeatures);
1856 assert_non_null(any->musts);
1857 assert_string_equal("test", any->ref);
1858 assert_non_null(any->when);
1859 assert_null(any->parent);
1860 assert_null(any->next);
1861 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR | LYS_MAND_TRUE, any->flags);
1862 lysp_node_free(ctx.ctx, (struct lysp_node*)any); any = NULL;
1863
1864 *state = NULL;
1865 ly_ctx_destroy(ctx.ctx, NULL);
1866}
1867
1868static void
1869test_anydata(void **state)
1870{
1871 return test_any(state, YANG_ANYDATA);
1872}
1873
1874static void
1875test_anyxml(void **state)
1876{
1877 return test_any(state, YANG_ANYXML);
1878}
1879
Radek Krejcie86bf772018-12-14 11:39:53 +01001880static void
1881test_grouping(void **state)
1882{
1883 *state = test_grouping;
1884
Radek Krejcie7b95092019-05-15 11:03:07 +02001885 struct lys_parser_ctx ctx = {0};
Radek Krejcie86bf772018-12-14 11:39:53 +01001886 struct lysp_grp *grp = NULL;
1887 const char *str;
1888
1889 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1890 assert_non_null(ctx.ctx);
1891 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001892 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejcie86bf772018-12-14 11:39:53 +01001893
1894 /* invalid cardinality */
1895#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1896 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1897 assert_int_equal(LY_EVALID, parse_grouping(&ctx, &str, NULL, &grp)); \
1898 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1899 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1900
1901 TEST_DUP("description", "text1", "text2");
1902 TEST_DUP("reference", "1", "2");
1903 TEST_DUP("status", "current", "obsolete");
1904#undef TEST_DUP
1905
1906 /* full content */
1907 str = "grp {action x;anydata any;anyxml anyxml; choice ch;container c;description test;grouping g;leaf l {type string;}"
1908 "leaf-list ll {type string;} list li;notification not;reference test;status current;typedef t {type int8;}uses g;m:ext;} ...";
1909 assert_int_equal(LY_SUCCESS, parse_grouping(&ctx, &str, NULL, &grp));
1910 assert_non_null(grp);
1911 assert_int_equal(LYS_GROUPING, grp->nodetype);
1912 assert_string_equal("grp", grp->name);
1913 assert_string_equal("test", grp->dsc);
1914 assert_non_null(grp->exts);
1915 assert_string_equal("test", grp->ref);
1916 assert_null(grp->parent);
1917 assert_int_equal( LYS_STATUS_CURR, grp->flags);
1918 ly_set_erase(&ctx.tpdfs_nodes, NULL);
1919 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1920
1921 /* invalid content */
1922 str = "grp {config true} ...";
1923 assert_int_equal(LY_EVALID, parse_grouping(&ctx, &str, NULL, &grp));
1924 logbuf_assert("Invalid keyword \"config\" as a child of \"grouping\". Line number 1.");
1925 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1926
1927 str = "grp {must 'expr'} ...";
1928 assert_int_equal(LY_EVALID, parse_grouping(&ctx, &str, NULL, &grp));
1929 logbuf_assert("Invalid keyword \"must\" as a child of \"grouping\". Line number 1.");
1930 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1931
1932 *state = NULL;
1933 ly_ctx_destroy(ctx.ctx, NULL);
1934}
1935
1936static void
Radek Krejcif538ce52019-03-05 10:46:14 +01001937test_action(void **state)
1938{
1939 *state = test_action;
1940
Radek Krejcie7b95092019-05-15 11:03:07 +02001941 struct lys_parser_ctx ctx = {0};
Radek Krejcif538ce52019-03-05 10:46:14 +01001942 struct lysp_action *rpcs = NULL;
1943 struct lysp_node_container *c = NULL;
1944 const char *str;
1945
1946 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1947 assert_non_null(ctx.ctx);
1948 ctx.line = 1;
1949 ctx.mod_version = 2; /* simulate YANG 1.1 */
1950
1951 /* invalid cardinality */
1952#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1953 str = "func {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1954 assert_int_equal(LY_EVALID, parse_action(&ctx, &str, NULL, &rpcs)); \
1955 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1956 FREE_ARRAY(ctx.ctx, rpcs, lysp_action_free); rpcs = NULL;
1957
1958 TEST_DUP("description", "text1", "text2");
1959 TEST_DUP("input", "", "");
1960 TEST_DUP("output", "", "");
1961 TEST_DUP("reference", "1", "2");
1962 TEST_DUP("status", "current", "obsolete");
1963#undef TEST_DUP
1964
1965 /* full content */
1966 str = "top;";
1967 assert_int_equal(LY_SUCCESS, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1968 str = "func {description test;grouping grp;if-feature f;reference test;status current;typedef mytype {type int8;} m:ext;"
1969 "input {anydata a1; anyxml a2; choice ch; container c; grouping grp; leaf l {type int8;} leaf-list ll {type int8;}"
1970 " list li; must 1; typedef mytypei {type int8;} uses grp; m:ext;}"
1971 "output {anydata a1; anyxml a2; choice ch; container c; grouping grp; leaf l {type int8;} leaf-list ll {type int8;}"
1972 " list li; must 1; typedef mytypeo {type int8;} uses grp; m:ext;}} ...";
1973 assert_int_equal(LY_SUCCESS, parse_action(&ctx, &str, (struct lysp_node*)c, &rpcs));
1974 assert_non_null(rpcs);
1975 assert_int_equal(LYS_ACTION, rpcs->nodetype);
1976 assert_string_equal("func", rpcs->name);
1977 assert_string_equal("test", rpcs->dsc);
1978 assert_non_null(rpcs->exts);
1979 assert_non_null(rpcs->iffeatures);
1980 assert_string_equal("test", rpcs->ref);
1981 assert_non_null(rpcs->groupings);
1982 assert_non_null(rpcs->typedefs);
1983 assert_int_equal(LYS_STATUS_CURR, rpcs->flags);
1984 /* input */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001985 assert_int_equal(rpcs->input.nodetype, LYS_INPUT);
Radek Krejcif538ce52019-03-05 10:46:14 +01001986 assert_non_null(rpcs->input.groupings);
1987 assert_non_null(rpcs->input.exts);
1988 assert_non_null(rpcs->input.musts);
1989 assert_non_null(rpcs->input.typedefs);
1990 assert_non_null(rpcs->input.data);
1991 /* output */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001992 assert_int_equal(rpcs->output.nodetype, LYS_OUTPUT);
Radek Krejcif538ce52019-03-05 10:46:14 +01001993 assert_non_null(rpcs->output.groupings);
1994 assert_non_null(rpcs->output.exts);
1995 assert_non_null(rpcs->output.musts);
1996 assert_non_null(rpcs->output.typedefs);
1997 assert_non_null(rpcs->output.data);
1998
1999 ly_set_erase(&ctx.tpdfs_nodes, NULL);
2000 FREE_ARRAY(ctx.ctx, rpcs, lysp_action_free); rpcs = NULL;
2001
2002 /* invalid content */
2003 str = "func {config true} ...";
2004 assert_int_equal(LY_EVALID, parse_action(&ctx, &str, NULL, &rpcs));
2005 logbuf_assert("Invalid keyword \"config\" as a child of \"rpc\". Line number 1.");
2006 FREE_ARRAY(ctx.ctx, rpcs, lysp_action_free); rpcs = NULL;
2007
2008 *state = NULL;
2009 lysp_node_free(ctx.ctx, (struct lysp_node*)c);
2010 ly_ctx_destroy(ctx.ctx, NULL);
2011}
2012
2013static void
Radek Krejcifc11bd72019-04-11 16:00:05 +02002014test_notification(void **state)
2015{
2016 *state = test_notification;
2017
Radek Krejcie7b95092019-05-15 11:03:07 +02002018 struct lys_parser_ctx ctx = {0};
Radek Krejcifc11bd72019-04-11 16:00:05 +02002019 struct lysp_notif *notifs = NULL;
2020 struct lysp_node_container *c = NULL;
2021 const char *str;
2022
2023 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
2024 assert_non_null(ctx.ctx);
2025 ctx.line = 1;
2026 ctx.mod_version = 2; /* simulate YANG 1.1 */
2027
2028 /* invalid cardinality */
2029#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
2030 str = "func {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
2031 assert_int_equal(LY_EVALID, parse_notif(&ctx, &str, NULL, &notifs)); \
2032 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
2033 FREE_ARRAY(ctx.ctx, notifs, lysp_notif_free); notifs = NULL;
2034
2035 TEST_DUP("description", "text1", "text2");
2036 TEST_DUP("reference", "1", "2");
2037 TEST_DUP("status", "current", "obsolete");
2038#undef TEST_DUP
2039
2040 /* full content */
2041 str = "top;";
2042 assert_int_equal(LY_SUCCESS, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
2043 str = "ntf {anydata a1; anyxml a2; choice ch; container c; description test; grouping grp; if-feature f; leaf l {type int8;}"
2044 "leaf-list ll {type int8;} list li; must 1; reference test; status current; typedef mytype {type int8;} uses grp; m:ext;}";
2045 assert_int_equal(LY_SUCCESS, parse_notif(&ctx, &str, (struct lysp_node*)c, &notifs));
2046 assert_non_null(notifs);
2047 assert_int_equal(LYS_NOTIF, notifs->nodetype);
2048 assert_string_equal("ntf", notifs->name);
2049 assert_string_equal("test", notifs->dsc);
2050 assert_non_null(notifs->exts);
2051 assert_non_null(notifs->iffeatures);
2052 assert_string_equal("test", notifs->ref);
2053 assert_non_null(notifs->groupings);
2054 assert_non_null(notifs->typedefs);
2055 assert_non_null(notifs->musts);
2056 assert_non_null(notifs->data);
2057 assert_int_equal(LYS_STATUS_CURR, notifs->flags);
2058
2059 ly_set_erase(&ctx.tpdfs_nodes, NULL);
2060 FREE_ARRAY(ctx.ctx, notifs, lysp_notif_free); notifs = NULL;
2061
2062 /* invalid content */
2063 str = "ntf {config true} ...";
2064 assert_int_equal(LY_EVALID, parse_notif(&ctx, &str, NULL, &notifs));
2065 logbuf_assert("Invalid keyword \"config\" as a child of \"notification\". Line number 1.");
2066 FREE_ARRAY(ctx.ctx, notifs, lysp_notif_free); notifs = NULL;
2067
2068 *state = NULL;
2069 lysp_node_free(ctx.ctx, (struct lysp_node*)c);
2070 ly_ctx_destroy(ctx.ctx, NULL);
2071}
2072
2073static void
Radek Krejcie86bf772018-12-14 11:39:53 +01002074test_uses(void **state)
2075{
2076 *state = test_uses;
2077
Radek Krejcie7b95092019-05-15 11:03:07 +02002078 struct lys_parser_ctx ctx = {0};
Radek Krejcie86bf772018-12-14 11:39:53 +01002079 struct lysp_node_uses *u = NULL;
2080 const char *str;
2081
2082 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
2083 assert_non_null(ctx.ctx);
2084 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002085 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejcie86bf772018-12-14 11:39:53 +01002086
2087 /* invalid cardinality */
2088#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
2089 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
2090 assert_int_equal(LY_EVALID, parse_uses(&ctx, &str, NULL, (struct lysp_node**)&u)); \
2091 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
2092 lysp_node_free(ctx.ctx, (struct lysp_node*)u); u = NULL;
2093
2094 TEST_DUP("description", "text1", "text2");
2095 TEST_DUP("reference", "1", "2");
2096 TEST_DUP("status", "current", "obsolete");
2097 TEST_DUP("when", "true", "false");
2098#undef TEST_DUP
2099
2100 /* full content */
2101 str = "grpref {augment some/node;description test;if-feature f;reference test;refine some/other/node;status current;when true;m:ext;} ...";
2102 assert_int_equal(LY_SUCCESS, parse_uses(&ctx, &str, NULL, (struct lysp_node**)&u));
2103 assert_non_null(u);
2104 assert_int_equal(LYS_USES, u->nodetype);
2105 assert_string_equal("grpref", u->name);
2106 assert_string_equal("test", u->dsc);
2107 assert_non_null(u->exts);
2108 assert_non_null(u->iffeatures);
2109 assert_string_equal("test", u->ref);
2110 assert_non_null(u->augments);
2111 assert_non_null(u->refines);
2112 assert_non_null(u->when);
2113 assert_null(u->parent);
2114 assert_null(u->next);
2115 assert_int_equal(LYS_STATUS_CURR, u->flags);
2116 lysp_node_free(ctx.ctx, (struct lysp_node*)u); u = NULL;
2117
2118 *state = NULL;
2119 ly_ctx_destroy(ctx.ctx, NULL);
2120}
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002121
2122
2123static void
2124test_augment(void **state)
2125{
2126 *state = test_augment;
2127
Radek Krejcie7b95092019-05-15 11:03:07 +02002128 struct lys_parser_ctx ctx = {0};
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002129 struct lysp_augment *a = NULL;
2130 const char *str;
2131
2132 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
2133 assert_non_null(ctx.ctx);
2134 ctx.line = 1;
Radek Krejcib4adbf12019-02-25 13:35:22 +01002135 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002136
2137 /* invalid cardinality */
2138#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
2139 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
2140 assert_int_equal(LY_EVALID, parse_augment(&ctx, &str, NULL, &a)); \
2141 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
Radek Krejcib4adbf12019-02-25 13:35:22 +01002142 FREE_ARRAY(ctx.ctx, a, lysp_augment_free); a = NULL;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002143
2144 TEST_DUP("description", "text1", "text2");
2145 TEST_DUP("reference", "1", "2");
2146 TEST_DUP("status", "current", "obsolete");
2147 TEST_DUP("when", "true", "false");
2148#undef TEST_DUP
2149
2150 /* full content */
2151 str = "/target/nodeid {action x; anydata any;anyxml anyxml; case cs; choice ch;container c;description test;if-feature f;leaf l {type string;}"
2152 "leaf-list ll {type string;} list li;notification not;reference test;status current;uses g;when true;m:ext;} ...";
2153 assert_int_equal(LY_SUCCESS, parse_augment(&ctx, &str, NULL, &a));
2154 assert_non_null(a);
2155 assert_int_equal(LYS_AUGMENT, a->nodetype);
2156 assert_string_equal("/target/nodeid", a->nodeid);
2157 assert_string_equal("test", a->dsc);
2158 assert_non_null(a->exts);
2159 assert_non_null(a->iffeatures);
2160 assert_string_equal("test", a->ref);
2161 assert_non_null(a->when);
2162 assert_null(a->parent);
2163 assert_int_equal(LYS_STATUS_CURR, a->flags);
Radek Krejcib4adbf12019-02-25 13:35:22 +01002164 FREE_ARRAY(ctx.ctx, a, lysp_augment_free); a = NULL;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002165
2166 *state = NULL;
2167 ly_ctx_destroy(ctx.ctx, NULL);
2168}
2169
Radek Krejcif09e4e82019-06-14 15:08:11 +02002170static void
2171test_when(void **state)
2172{
2173 *state = test_when;
2174
2175 struct lys_parser_ctx ctx = {0};
2176 struct lysp_when *w = NULL;
2177 const char *str;
2178
2179 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
2180 assert_non_null(ctx.ctx);
2181 ctx.line = 1;
2182 ctx.mod_version = 2; /* simulate YANG 1.1 */
2183
2184 /* invalid cardinality */
2185#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
2186 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
2187 assert_int_equal(LY_EVALID, parse_when(&ctx, &str, &w)); \
2188 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
2189 FREE_MEMBER(ctx.ctx, w, lysp_when_free); w = NULL;
2190
2191 TEST_DUP("description", "text1", "text2");
2192 TEST_DUP("reference", "1", "2");
2193#undef TEST_DUP
2194
2195 /* full content */
2196 str = "expression {description test;reference test;m:ext;} ...";
2197 assert_int_equal(LY_SUCCESS, parse_when(&ctx, &str, &w));
2198 assert_non_null(w);
2199 assert_string_equal("expression", w->cond);
2200 assert_string_equal("test", w->dsc);
2201 assert_string_equal("test", w->ref);
2202 assert_non_null(w->exts);
2203 FREE_MEMBER(ctx.ctx, w, lysp_when_free); w = NULL;
2204
2205 /* empty condition */
2206 str = "\"\";";
2207 assert_int_equal(LY_SUCCESS, parse_when(&ctx, &str, &w));
2208 logbuf_assert("Empty argument of when statement does not make sense.");
2209 assert_non_null(w);
2210 assert_string_equal("", w->cond);
2211 FREE_MEMBER(ctx.ctx, w, lysp_when_free); w = NULL;
2212
2213 *state = NULL;
2214 ly_ctx_destroy(ctx.ctx, NULL);
2215}
2216
David Sedlákd6ce6d72019-07-16 17:30:18 +02002217static void
2218test_value(void **state)
2219{
2220 *state = test_value;
2221 struct lys_parser_ctx ctx;
2222
2223 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
2224 assert_non_null(ctx.ctx);
2225 ctx.line = 1;
2226 ctx.indent = 0;
2227 int64_t val = 0;
2228 uint16_t flags = 0;
2229
2230 const char *data = "-0;";
2231 assert_int_equal(parse_type_enum_value_pos(&ctx, &data, YANG_VALUE, &val, &flags, NULL), LY_SUCCESS);
2232 assert_int_equal(val, 0);
2233
2234 data = "-0;";
2235 flags = 0;
2236 assert_int_equal(parse_type_enum_value_pos(&ctx, &data, YANG_POSITION, &val, &flags, NULL), LY_EVALID);
2237 logbuf_assert("Invalid value \"-0\" of \"position\". Line number 1.");
2238
David Sedlák1af868e2019-07-17 17:03:14 +02002239 *state = NULL;
David Sedlákd6ce6d72019-07-16 17:30:18 +02002240 ly_ctx_destroy(ctx.ctx, NULL);
2241}
2242
Radek Krejci80dd33e2018-09-26 15:57:18 +02002243int main(void)
2244{
2245 const struct CMUnitTest tests[] = {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002246 cmocka_unit_test_setup(test_helpers, logger_setup),
Radek Krejci80dd33e2018-09-26 15:57:18 +02002247 cmocka_unit_test_setup(test_comments, logger_setup),
Radek Krejciefd22f62018-09-27 11:47:58 +02002248 cmocka_unit_test_setup(test_arg, logger_setup),
Radek Krejcidcc7b322018-10-11 14:24:02 +02002249 cmocka_unit_test_setup(test_stmts, logger_setup),
Radek Krejci05b13982018-11-28 16:22:07 +01002250 cmocka_unit_test_setup_teardown(test_minmax, logger_setup, logger_teardown),
Radek Krejci40544fa2019-01-11 09:38:37 +01002251 cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown),
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002252 cmocka_unit_test_setup_teardown(test_identity, logger_setup, logger_teardown),
Radek Krejci2c02f3e2018-10-16 10:54:38 +02002253 cmocka_unit_test_setup(test_feature, logger_setup),
2254 cmocka_unit_test_setup(test_deviation, logger_setup),
2255 cmocka_unit_test_setup(test_deviate, logger_setup),
Radek Krejci8c370832018-11-02 15:10:03 +01002256 cmocka_unit_test_setup(test_container, logger_setup),
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002257 cmocka_unit_test_setup_teardown(test_leaf, logger_setup, logger_teardown),
Radek Krejci0e5d8382018-11-28 16:37:53 +01002258 cmocka_unit_test_setup_teardown(test_leaflist, logger_setup, logger_teardown),
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002259 cmocka_unit_test_setup_teardown(test_list, logger_setup, logger_teardown),
Radek Krejci056d0a82018-12-06 16:57:25 +01002260 cmocka_unit_test_setup_teardown(test_choice, logger_setup, logger_teardown),
Radek Krejcia9026eb2018-12-12 16:04:47 +01002261 cmocka_unit_test_setup_teardown(test_case, logger_setup, logger_teardown),
Radek Krejci9800fb82018-12-13 14:26:23 +01002262 cmocka_unit_test_setup_teardown(test_anydata, logger_setup, logger_teardown),
2263 cmocka_unit_test_setup_teardown(test_anyxml, logger_setup, logger_teardown),
Radek Krejcif538ce52019-03-05 10:46:14 +01002264 cmocka_unit_test_setup_teardown(test_action, logger_setup, logger_teardown),
Radek Krejcifc11bd72019-04-11 16:00:05 +02002265 cmocka_unit_test_setup_teardown(test_notification, logger_setup, logger_teardown),
Radek Krejcie86bf772018-12-14 11:39:53 +01002266 cmocka_unit_test_setup_teardown(test_grouping, logger_setup, logger_teardown),
2267 cmocka_unit_test_setup_teardown(test_uses, logger_setup, logger_teardown),
Radek Krejcib4adbf12019-02-25 13:35:22 +01002268 cmocka_unit_test_setup_teardown(test_augment, logger_setup, logger_teardown),
Radek Krejcif09e4e82019-06-14 15:08:11 +02002269 cmocka_unit_test_setup_teardown(test_when, logger_setup, logger_teardown),
David Sedlákd6ce6d72019-07-16 17:30:18 +02002270 cmocka_unit_test_setup_teardown(test_value, logger_setup, logger_teardown),
Radek Krejci80dd33e2018-09-26 15:57:18 +02002271 };
2272
2273 return cmocka_run_group_tests(tests, NULL, NULL);
2274}