blob: f63aceeccb6616123ac045ac8cb71b30e7aa38b9 [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
1050 str = "module " SCHEMA_BEGINNING "} module q {namespace urn:q;prefixq;}";
1051 m = mod->mod;
1052 free(mod);
1053 m->parsed = NULL;
1054 assert_int_equal(LY_EVALID, yang_parse_module(&ctx, str, m));
Radek Krejci0a1d0d42019-05-16 15:14:51 +02001055 logbuf_assert("Trailing garbage \"module q {names...\" after module, expected end-of-input. Line number 3.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001056 mod = mod_renew(&ctx);
1057
1058 str = "prefix " SCHEMA_BEGINNING "}";
1059 m = mod->mod;
1060 free(mod);
1061 m->parsed = NULL;
1062 assert_int_equal(LY_EVALID, yang_parse_module(&ctx, str, m));
1063 logbuf_assert("Invalid keyword \"prefix\", expected \"module\" or \"submodule\". Line number 3.");
1064 mod = mod_renew(&ctx);
Radek Krejci09306362018-10-15 15:26:01 +02001065
David Sedlák9fb515f2019-07-11 10:33:58 +02001066 str = "module " SCHEMA_BEGINNING "}";
1067 str = "module " SCHEMA_BEGINNING "leaf enum {type enumeration {enum seven { position 7;}}}}";
1068 m = mod->mod;
1069 free(mod);
1070 m->parsed = NULL;
1071 assert_int_equal(LY_EVALID, yang_parse_module(&ctx, str, m));
1072 logbuf_assert("Invalid keyword \"position\" as a child of \"enum\". Line number 3.");
1073 mod = mod_renew(&ctx);
1074
Radek Krejci156ccaf2018-10-15 15:49:17 +02001075 /* extensions */
1076 TEST_GENERIC("prefix:test;}", mod->exts,
Radek Krejci2c4e7172018-10-19 15:56:26 +02001077 assert_string_equal("prefix:test", mod->exts[0].name);
1078 assert_int_equal(LYEXT_SUBSTMT_SELF, mod->exts[0].insubstmt));
Radek Krejci40544fa2019-01-11 09:38:37 +01001079 mod = mod_renew(&ctx);
Radek Krejci156ccaf2018-10-15 15:49:17 +02001080
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001081 /* invalid substatement */
1082 str = SCHEMA_BEGINNING "must false;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001083 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001084 logbuf_assert("Invalid keyword \"must\" as a child of \"module\". Line number 3.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001085 mod = mod_renew(&ctx);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001086
Radek Krejci09306362018-10-15 15:26:01 +02001087 /* submodule */
Radek Krejci40544fa2019-01-11 09:38:37 +01001088 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001089
1090 /* missing mandatory substatements */
1091 str = " subname {}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001092 lydict_remove(ctx.ctx, submod->name);
1093 assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod));
1094 assert_string_equal("subname", submod->name);
Radek Krejci09306362018-10-15 15:26:01 +02001095 logbuf_assert("Missing mandatory keyword \"belongs-to\" as a child of \"submodule\". Line number 3.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001096 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001097
Radek Krejci313d9902018-11-08 09:42:58 +01001098 str = " subname {belongs-to name {prefix x;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001099 lydict_remove(ctx.ctx, submod->name);
1100 assert_int_equal(LY_SUCCESS, parse_submodule(&ctx, &str, submod));
1101 assert_string_equal("name", submod->belongsto);
1102 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001103
1104#undef SCHEMA_BEGINNING
Radek Krejci313d9902018-11-08 09:42:58 +01001105#define SCHEMA_BEGINNING " subname {belongs-to name {prefix x;}"
Radek Krejci09306362018-10-15 15:26:01 +02001106
1107 /* duplicated namespace, prefix */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001108 str = " subname {belongs-to name {prefix x;}belongs-to module1;belongs-to module2;} ...";
1109 assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod)); \
1110 logbuf_assert("Duplicate keyword \"belongs-to\". Line number 3."); \
1111 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001112
1113 /* not allowed in submodule (module-specific) */
1114 str = SCHEMA_BEGINNING "namespace \"urn:z\";}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001115 assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod));
Radek Krejci09306362018-10-15 15:26:01 +02001116 logbuf_assert("Invalid keyword \"namespace\" as a child of \"submodule\". Line number 3.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001117 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001118 str = SCHEMA_BEGINNING "prefix m;}}";
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 \"prefix\" as a child of \"submodule\". Line number 3.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001121 submod = submod_renew(&ctx, submod);
Radek Krejcia042ea12018-10-13 07:52:15 +02001122
Radek Krejci40544fa2019-01-11 09:38:37 +01001123 str = "submodule " SCHEMA_BEGINNING "} module q {namespace urn:q;prefixq;}";
1124 lysp_submodule_free(ctx.ctx, submod);
1125 submod = NULL;
1126 assert_int_equal(LY_EVALID, yang_parse_submodule(&ctx, str, &submod));
Radek Krejci0a1d0d42019-05-16 15:14:51 +02001127 logbuf_assert("Trailing garbage \"module q {names...\" after submodule, expected end-of-input. Line number 3.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001128
1129 str = "prefix " SCHEMA_BEGINNING "}";
1130 assert_int_equal(LY_EVALID, yang_parse_submodule(&ctx, str, &submod));
1131 logbuf_assert("Invalid keyword \"prefix\", expected \"module\" or \"submodule\". Line number 3.");
1132 submod = submod_renew(&ctx, submod);
1133
Radek Krejcia042ea12018-10-13 07:52:15 +02001134#undef TEST_GENERIC
1135#undef TEST_NODE
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001136#undef TEST_DUP
Radek Krejcia042ea12018-10-13 07:52:15 +02001137#undef SCHEMA_BEGINNING
1138
Radek Krejci9fcacc12018-10-11 15:59:11 +02001139 lysp_module_free(mod);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001140 lysp_submodule_free(ctx.ctx, submod);
Radek Krejci9fcacc12018-10-11 15:59:11 +02001141 ly_ctx_destroy(ctx.ctx, NULL);
Radek Krejci40544fa2019-01-11 09:38:37 +01001142
1143 *state = NULL;
Radek Krejciefd22f62018-09-27 11:47:58 +02001144}
1145
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001146static void
1147test_identity(void **state)
1148{
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001149 *state = test_identity;
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001150
Radek Krejcie7b95092019-05-15 11:03:07 +02001151 struct lys_parser_ctx ctx;
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001152 struct lysp_ident *ident = NULL;
1153 const char *str;
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001154
1155 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1156 assert_non_null(ctx.ctx);
1157 ctx.line = 1;
1158 ctx.indent = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001159 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001160
1161 /* invalid cardinality */
1162#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001163 TEST_DUP_GENERIC(" test {", MEMBER, VALUE1, VALUE2, parse_identity, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001164 &ident, "1", FREE_ARRAY(ctx.ctx, ident, lysp_ident_free); ident = NULL)
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001165
Radek Krejci38222632019-02-12 16:55:05 +01001166 TEST_DUP("description", "a", "b");
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001167 TEST_DUP("reference", "a", "b");
1168 TEST_DUP("status", "current", "obsolete");
1169
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001170 /* full content */
David Sedlák40bb13b2019-07-10 14:34:18 +02001171 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 +02001172 assert_int_equal(LY_SUCCESS, parse_identity(&ctx, &str, &ident));
1173 assert_non_null(ident);
1174 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001175 FREE_ARRAY(ctx.ctx, ident, lysp_ident_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001176 ident = NULL;
1177
1178 /* invalid substatement */
1179 str = " test {organization XXX;}";
1180 assert_int_equal(LY_EVALID, parse_identity(&ctx, &str, &ident));
1181 logbuf_assert("Invalid keyword \"organization\" as a child of \"identity\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001182 FREE_ARRAY(ctx.ctx, ident, lysp_ident_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001183 ident = NULL;
1184
1185#undef TEST_DUP
1186
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001187 *state = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001188 ly_ctx_destroy(ctx.ctx, NULL);
1189}
1190
1191static void
1192test_feature(void **state)
1193{
1194 (void) state; /* unused */
1195
Radek Krejcie7b95092019-05-15 11:03:07 +02001196 struct lys_parser_ctx ctx;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001197 struct lysp_feature *features = NULL;
1198 const char *str;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001199
1200 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1201 assert_non_null(ctx.ctx);
1202 ctx.line = 1;
1203 ctx.indent = 0;
1204
1205 /* invalid cardinality */
1206#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1207 TEST_DUP_GENERIC(" test {", MEMBER, VALUE1, VALUE2, parse_feature, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001208 &features, "1", FREE_ARRAY(ctx.ctx, features, lysp_feature_free); features = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001209
1210 TEST_DUP("description", "a", "b");
1211 TEST_DUP("reference", "a", "b");
1212 TEST_DUP("status", "current", "obsolete");
1213
1214 /* full content */
1215 str = " test {description text;reference \'another text\';status current; if-feature x;if-feature y;prefix:ext;} ...";
1216 assert_int_equal(LY_SUCCESS, parse_feature(&ctx, &str, &features));
1217 assert_non_null(features);
1218 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001219 FREE_ARRAY(ctx.ctx, features, lysp_feature_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001220 features = NULL;
1221
1222 /* invalid substatement */
1223 str = " test {organization XXX;}";
1224 assert_int_equal(LY_EVALID, parse_feature(&ctx, &str, &features));
1225 logbuf_assert("Invalid keyword \"organization\" as a child of \"feature\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001226 FREE_ARRAY(ctx.ctx, features, lysp_feature_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001227 features = NULL;
1228
1229#undef TEST_DUP
1230
1231 ly_ctx_destroy(ctx.ctx, NULL);
1232}
1233
1234static void
1235test_deviation(void **state)
1236{
1237 (void) state; /* unused */
1238
Radek Krejcie7b95092019-05-15 11:03:07 +02001239 struct lys_parser_ctx ctx;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001240 struct lysp_deviation *d = NULL;
1241 const char *str;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001242
1243 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1244 assert_non_null(ctx.ctx);
1245 ctx.line = 1;
1246 ctx.indent = 0;
1247
1248 /* invalid cardinality */
1249#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1250 TEST_DUP_GENERIC(" test {deviate not-supported;", MEMBER, VALUE1, VALUE2, parse_deviation, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001251 &d, "1", FREE_ARRAY(ctx.ctx, d, lysp_deviation_free); d = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001252
1253 TEST_DUP("description", "a", "b");
1254 TEST_DUP("reference", "a", "b");
1255
1256 /* full content */
1257 str = " test {deviate not-supported;description text;reference \'another text\';prefix:ext;} ...";
1258 assert_int_equal(LY_SUCCESS, parse_deviation(&ctx, &str, &d));
1259 assert_non_null(d);
1260 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001261 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001262 d = NULL;
1263
1264 /* missing mandatory substatement */
1265 str = " test {description text;}";
1266 assert_int_equal(LY_EVALID, parse_deviation(&ctx, &str, &d));
1267 logbuf_assert("Missing mandatory keyword \"deviate\" as a child of \"deviation\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001268 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001269 d = NULL;
1270
1271 /* invalid substatement */
1272 str = " test {deviate not-supported; status obsolete;}";
1273 assert_int_equal(LY_EVALID, parse_deviation(&ctx, &str, &d));
1274 logbuf_assert("Invalid keyword \"status\" as a child of \"deviation\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001275 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001276 d = NULL;
1277
1278#undef TEST_DUP
1279
1280 ly_ctx_destroy(ctx.ctx, NULL);
1281}
1282
1283static void
1284test_deviate(void **state)
1285{
1286 (void) state; /* unused */
1287
Radek Krejcie7b95092019-05-15 11:03:07 +02001288 struct lys_parser_ctx ctx;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001289 struct lysp_deviate *d = NULL;
1290 const char *str;
1291
1292 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1293 assert_non_null(ctx.ctx);
1294 ctx.line = 1;
1295 ctx.indent = 0;
1296
1297 /* invalid cardinality */
1298#define TEST_DUP(TYPE, MEMBER, VALUE1, VALUE2) \
1299 TEST_DUP_GENERIC(TYPE" {", MEMBER, VALUE1, VALUE2, parse_deviate, \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001300 &d, "1", lysp_deviate_free(ctx.ctx, d); free(d); d = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001301
1302 TEST_DUP("add", "config", "true", "false");
1303 TEST_DUP("replace", "default", "int8", "uint8");
1304 TEST_DUP("add", "mandatory", "true", "false");
1305 TEST_DUP("add", "max-elements", "1", "2");
1306 TEST_DUP("add", "min-elements", "1", "2");
1307 TEST_DUP("replace", "type", "int8", "uint8");
1308 TEST_DUP("add", "units", "kilometers", "miles");
1309
1310 /* full contents */
1311 str = " not-supported {prefix:ext;} ...";
1312 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1313 assert_non_null(d);
1314 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001315 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001316 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;} ...";
1317 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1318 assert_non_null(d);
1319 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001320 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001321 str = " delete {units meters; must 1; must 2; unique x; unique y; default a; default b; prefix:ext;} ...";
1322 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1323 assert_non_null(d);
1324 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001325 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001326 str = " replace {type string; units meters; default a; config true; mandatory true; min-elements 1; max-elements 2; prefix:ext;} ...";
1327 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1328 assert_non_null(d);
1329 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001330 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001331
1332 /* invalid substatements */
1333#define TEST_NOT_SUP(DEV, STMT, VALUE) \
1334 str = " "DEV" {"STMT" "VALUE";}..."; \
1335 assert_int_equal(LY_EVALID, parse_deviate(&ctx, &str, &d)); \
1336 logbuf_assert("Deviate \""DEV"\" does not support keyword \""STMT"\". Line number 1."); \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001337 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001338
1339 TEST_NOT_SUP("not-supported", "units", "meters");
1340 TEST_NOT_SUP("not-supported", "must", "1");
1341 TEST_NOT_SUP("not-supported", "unique", "x");
1342 TEST_NOT_SUP("not-supported", "default", "a");
1343 TEST_NOT_SUP("not-supported", "config", "true");
1344 TEST_NOT_SUP("not-supported", "mandatory", "true");
1345 TEST_NOT_SUP("not-supported", "min-elements", "1");
1346 TEST_NOT_SUP("not-supported", "max-elements", "2");
1347 TEST_NOT_SUP("not-supported", "type", "string");
1348 TEST_NOT_SUP("add", "type", "string");
1349 TEST_NOT_SUP("delete", "config", "true");
1350 TEST_NOT_SUP("delete", "mandatory", "true");
1351 TEST_NOT_SUP("delete", "min-elements", "1");
1352 TEST_NOT_SUP("delete", "max-elements", "2");
1353 TEST_NOT_SUP("delete", "type", "string");
1354 TEST_NOT_SUP("replace", "must", "1");
1355 TEST_NOT_SUP("replace", "unique", "a");
1356
1357 str = " nonsence; ...";
1358 assert_int_equal(LY_EVALID, parse_deviate(&ctx, &str, &d));
1359 logbuf_assert("Invalid value \"nonsence\" of \"deviate\". Line number 1.");
1360 assert_null(d);
1361
1362#undef TEST_NOT_SUP
1363#undef TEST_DUP
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001364
1365 ly_ctx_destroy(ctx.ctx, NULL);
1366}
1367
Radek Krejci8c370832018-11-02 15:10:03 +01001368static void
1369test_container(void **state)
1370{
1371 (void) state; /* unused */
1372
Radek Krejcie7b95092019-05-15 11:03:07 +02001373 struct lys_parser_ctx ctx = {0};
Radek Krejci8c370832018-11-02 15:10:03 +01001374 struct lysp_node_container *c = NULL;
1375 const char *str;
1376
1377 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1378 assert_non_null(ctx.ctx);
1379 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001380 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci8c370832018-11-02 15:10:03 +01001381
1382 /* invalid cardinality */
1383#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1384 str = "cont {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1385 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c)); \
1386 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001387 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001388
1389 TEST_DUP("config", "true", "false");
1390 TEST_DUP("description", "text1", "text2");
1391 TEST_DUP("presence", "true", "false");
1392 TEST_DUP("reference", "1", "2");
1393 TEST_DUP("status", "current", "obsolete");
1394 TEST_DUP("when", "true", "false");
1395#undef TEST_DUP
1396
1397 /* full content */
Radek Krejci313d9902018-11-08 09:42:58 +01001398 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;}"
1399 "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 +01001400 assert_int_equal(LY_SUCCESS, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1401 assert_non_null(c);
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001402 assert_int_equal(LYS_CONTAINER, c->nodetype);
1403 assert_string_equal("cont", c->name);
Radek Krejci8c370832018-11-02 15:10:03 +01001404 assert_non_null(c->actions);
1405 assert_non_null(c->child);
1406 assert_string_equal("test", c->dsc);
1407 assert_non_null(c->exts);
1408 assert_non_null(c->groupings);
1409 assert_non_null(c->iffeatures);
1410 assert_non_null(c->musts);
1411 assert_non_null(c->notifs);
1412 assert_string_equal("true", c->presence);
1413 assert_string_equal("test", c->ref);
1414 assert_non_null(c->typedefs);
1415 assert_non_null(c->when);
1416 assert_null(c->parent);
1417 assert_null(c->next);
1418 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR, c->flags);
Radek Krejci313d9902018-11-08 09:42:58 +01001419 ly_set_erase(&ctx.tpdfs_nodes, NULL);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001420 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001421
1422 /* invalid */
1423 str = " cont {augment /root;} ...";
1424 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1425 logbuf_assert("Invalid keyword \"augment\" as a child of \"container\". Line number 1.");
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 str = " cont {nonsence true;} ...";
1428 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1429 logbuf_assert("Invalid character sequence \"nonsence\", expected a keyword. Line number 1.");
Radek Krejci4f28eda2018-11-12 11:46:16 +01001430 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001431
Radek Krejcif538ce52019-03-05 10:46:14 +01001432 ctx.mod_version = 1; /* simulate YANG 1.0 */
1433 str = " cont {action x;} ...";
1434 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1435 logbuf_assert("Invalid keyword \"action\" as a child of \"container\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
1436 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
1437
Radek Krejci8c370832018-11-02 15:10:03 +01001438 ly_ctx_destroy(ctx.ctx, NULL);
1439}
1440
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001441static void
1442test_leaf(void **state)
1443{
1444 *state = test_leaf;
1445
Radek Krejcie7b95092019-05-15 11:03:07 +02001446 struct lys_parser_ctx ctx = {0};
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001447 struct lysp_node_leaf *l = NULL;
1448 const char *str;
1449
1450 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1451 assert_non_null(ctx.ctx);
1452 ctx.line = 1;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001453 //ctx.mod->version = 2; /* simulate YANG 1.1 */
1454
1455 /* invalid cardinality */
1456#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1457 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1458 assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l)); \
1459 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1460 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1461
1462 TEST_DUP("config", "true", "false");
1463 TEST_DUP("default", "x", "y");
1464 TEST_DUP("description", "text1", "text2");
1465 TEST_DUP("mandatory", "true", "false");
1466 TEST_DUP("reference", "1", "2");
1467 TEST_DUP("status", "current", "obsolete");
Radek Krejci0e5d8382018-11-28 16:37:53 +01001468 TEST_DUP("type", "int8", "uint8");
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001469 TEST_DUP("units", "text1", "text2");
1470 TEST_DUP("when", "true", "false");
1471#undef TEST_DUP
1472
1473 /* full content - without mandatory which is mutual exclusive with default */
1474 str = "l {config false;default \"xxx\";description test;if-feature f;"
1475 "must 'expr';reference test;status current;type string; units yyy;when true;m:ext;} ...";
1476 assert_int_equal(LY_SUCCESS, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
1477 assert_non_null(l);
1478 assert_int_equal(LYS_LEAF, l->nodetype);
1479 assert_string_equal("l", l->name);
1480 assert_string_equal("test", l->dsc);
1481 assert_string_equal("xxx", l->dflt);
1482 assert_string_equal("yyy", l->units);
1483 assert_string_equal("string", l->type.name);
1484 assert_non_null(l->exts);
1485 assert_non_null(l->iffeatures);
1486 assert_non_null(l->musts);
1487 assert_string_equal("test", l->ref);
1488 assert_non_null(l->when);
1489 assert_null(l->parent);
1490 assert_null(l->next);
1491 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR, l->flags);
1492 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1493
1494 /* full content - now with mandatory */
1495 str = "l {mandatory true; type string;} ...";
1496 assert_int_equal(LY_SUCCESS, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
1497 assert_non_null(l);
1498 assert_int_equal(LYS_LEAF, l->nodetype);
1499 assert_string_equal("l", l->name);
1500 assert_string_equal("string", l->type.name);
1501 assert_int_equal(LYS_MAND_TRUE, l->flags);
1502 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1503
1504 /* invalid */
1505 str = " l {mandatory true; default xx; type string;} ...";
1506 assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
Radek Krejcia9026eb2018-12-12 16:04:47 +01001507 logbuf_assert("Invalid combination of keywords \"mandatory\" and \"default\" as substatements of \"leaf\". Line number 1.");
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001508 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1509
1510 str = " l {description \"missing type\";} ...";
1511 assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
1512 logbuf_assert("Missing mandatory keyword \"type\" as a child of \"leaf\". Line number 1.");
1513 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1514
1515 *state = NULL;
1516 ly_ctx_destroy(ctx.ctx, NULL);
1517}
1518
Radek Krejci0e5d8382018-11-28 16:37:53 +01001519static void
1520test_leaflist(void **state)
1521{
1522 *state = test_leaf;
1523
Radek Krejcie7b95092019-05-15 11:03:07 +02001524 struct lys_parser_ctx ctx = {0};
Radek Krejci0e5d8382018-11-28 16:37:53 +01001525 struct lysp_node_leaflist *ll = NULL;
1526 const char *str;
1527
1528 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1529 assert_non_null(ctx.ctx);
1530 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001531 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci0e5d8382018-11-28 16:37:53 +01001532
1533 /* invalid cardinality */
1534#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1535 str = "ll {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1536 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll)); \
1537 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1538 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1539
1540 TEST_DUP("config", "true", "false");
1541 TEST_DUP("description", "text1", "text2");
1542 TEST_DUP("max-elements", "10", "20");
1543 TEST_DUP("min-elements", "10", "20");
1544 TEST_DUP("ordered-by", "user", "system");
1545 TEST_DUP("reference", "1", "2");
1546 TEST_DUP("status", "current", "obsolete");
1547 TEST_DUP("type", "int8", "uint8");
1548 TEST_DUP("units", "text1", "text2");
1549 TEST_DUP("when", "true", "false");
1550#undef TEST_DUP
1551
1552 /* full content - without min-elements which is mutual exclusive with default */
1553 str = "ll {config false;default \"xxx\"; default \"yyy\";description test;if-feature f;"
1554 "max-elements 10;must 'expr';ordered-by user;reference test;"
1555 "status current;type string; units zzz;when true;m:ext;} ...";
1556 assert_int_equal(LY_SUCCESS, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1557 assert_non_null(ll);
1558 assert_int_equal(LYS_LEAFLIST, ll->nodetype);
1559 assert_string_equal("ll", ll->name);
1560 assert_string_equal("test", ll->dsc);
1561 assert_non_null(ll->dflts);
1562 assert_int_equal(2, LY_ARRAY_SIZE(ll->dflts));
1563 assert_string_equal("xxx", ll->dflts[0]);
1564 assert_string_equal("yyy", ll->dflts[1]);
1565 assert_string_equal("zzz", ll->units);
1566 assert_int_equal(10, ll->max);
1567 assert_int_equal(0, ll->min);
1568 assert_string_equal("string", ll->type.name);
1569 assert_non_null(ll->exts);
1570 assert_non_null(ll->iffeatures);
1571 assert_non_null(ll->musts);
1572 assert_string_equal("test", ll->ref);
1573 assert_non_null(ll->when);
1574 assert_null(ll->parent);
1575 assert_null(ll->next);
1576 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_USER | LYS_SET_MAX, ll->flags);
1577 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1578
1579 /* full content - now with min-elements */
1580 str = "ll {min-elements 10; type string;} ...";
1581 assert_int_equal(LY_SUCCESS, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1582 assert_non_null(ll);
1583 assert_int_equal(LYS_LEAFLIST, ll->nodetype);
1584 assert_string_equal("ll", ll->name);
1585 assert_string_equal("string", ll->type.name);
1586 assert_int_equal(0, ll->max);
1587 assert_int_equal(10, ll->min);
1588 assert_int_equal(LYS_SET_MIN, ll->flags);
1589 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1590
1591 /* invalid */
1592 str = " ll {min-elements 1; default xx; type string;} ...";
1593 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
Radek Krejcia9026eb2018-12-12 16:04:47 +01001594 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 +01001595 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1596
1597 str = " ll {description \"missing type\";} ...";
1598 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1599 logbuf_assert("Missing mandatory keyword \"type\" as a child of \"leaf-list\". Line number 1.");
1600 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1601
Radek Krejcidf6cad12018-11-28 17:10:55 +01001602 str = " ll {type string; min-elements 10; max-elements 1;} ..."; /* invalid combination of min/max */
1603 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1604 logbuf_assert("Invalid combination of min-elements and max-elements: min value 10 is bigger than the max value 1. Line number 1.");
1605 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1606
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001607 ctx.mod_version = 1; /* simulate YANG 1.0 - default statement is not allowed */
Radek Krejci0e5d8382018-11-28 16:37:53 +01001608 str = " ll {default xx; type string;} ...";
1609 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1610 logbuf_assert("Invalid keyword \"default\" as a child of \"leaf-list\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
1611 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1612
1613 *state = NULL;
1614 ly_ctx_destroy(ctx.ctx, NULL);
1615}
1616
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001617static void
1618test_list(void **state)
1619{
1620 *state = test_list;
1621
Radek Krejcie7b95092019-05-15 11:03:07 +02001622 struct lys_parser_ctx ctx = {0};
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001623 struct lysp_node_list *l = NULL;
1624 const char *str;
1625
1626 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1627 assert_non_null(ctx.ctx);
1628 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001629 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001630
1631 /* invalid cardinality */
1632#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1633 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1634 assert_int_equal(LY_EVALID, parse_list(&ctx, &str, NULL, (struct lysp_node**)&l)); \
1635 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1636 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1637
1638 TEST_DUP("config", "true", "false");
1639 TEST_DUP("description", "text1", "text2");
1640 TEST_DUP("key", "one", "two");
1641 TEST_DUP("max-elements", "10", "20");
1642 TEST_DUP("min-elements", "10", "20");
1643 TEST_DUP("ordered-by", "user", "system");
1644 TEST_DUP("reference", "1", "2");
1645 TEST_DUP("status", "current", "obsolete");
1646 TEST_DUP("when", "true", "false");
1647#undef TEST_DUP
1648
1649 /* full content */
1650 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;}"
1651 "leaf-list ll {type string;} list li;max-elements 10; min-elements 1;must 'expr';notification not; ordered-by system; reference test;"
1652 "status current;typedef t {type int8;}unique xxx;unique yyy;uses g;when true;m:ext;} ...";
1653 assert_int_equal(LY_SUCCESS, parse_list(&ctx, &str, NULL, (struct lysp_node**)&l));
1654 assert_non_null(l);
1655 assert_int_equal(LYS_LIST, l->nodetype);
1656 assert_string_equal("l", l->name);
1657 assert_string_equal("test", l->dsc);
1658 assert_string_equal("l", l->key);
1659 assert_non_null(l->uniques);
1660 assert_int_equal(2, LY_ARRAY_SIZE(l->uniques));
1661 assert_string_equal("xxx", l->uniques[0]);
1662 assert_string_equal("yyy", l->uniques[1]);
1663 assert_int_equal(10, l->max);
1664 assert_int_equal(1, l->min);
1665 assert_non_null(l->exts);
1666 assert_non_null(l->iffeatures);
1667 assert_non_null(l->musts);
1668 assert_string_equal("test", l->ref);
1669 assert_non_null(l->when);
1670 assert_null(l->parent);
1671 assert_null(l->next);
1672 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM | LYS_SET_MAX | LYS_SET_MIN, l->flags);
1673 ly_set_erase(&ctx.tpdfs_nodes, NULL);
1674 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1675
Radek Krejcif538ce52019-03-05 10:46:14 +01001676 /* invalid content */
1677 ctx.mod_version = 1; /* simulate YANG 1.0 */
1678 str = "l {action x;} ...";
1679 assert_int_equal(LY_EVALID, parse_list(&ctx, &str, NULL, (struct lysp_node**)&l));
1680 logbuf_assert("Invalid keyword \"action\" as a child of \"list\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
1681 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1682
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001683 *state = NULL;
1684 ly_ctx_destroy(ctx.ctx, NULL);
1685}
1686
Radek Krejci056d0a82018-12-06 16:57:25 +01001687static void
1688test_choice(void **state)
1689{
1690 *state = test_choice;
1691
Radek Krejcie7b95092019-05-15 11:03:07 +02001692 struct lys_parser_ctx ctx = {0};
Radek Krejci056d0a82018-12-06 16:57:25 +01001693 struct lysp_node_choice *ch = NULL;
1694 const char *str;
1695
1696 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1697 assert_non_null(ctx.ctx);
1698 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001699 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci056d0a82018-12-06 16:57:25 +01001700
1701 /* invalid cardinality */
1702#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1703 str = "ch {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1704 assert_int_equal(LY_EVALID, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch)); \
1705 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1706 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1707
1708 TEST_DUP("config", "true", "false");
1709 TEST_DUP("default", "a", "b");
1710 TEST_DUP("description", "text1", "text2");
1711 TEST_DUP("mandatory", "true", "false");
1712 TEST_DUP("reference", "1", "2");
1713 TEST_DUP("status", "current", "obsolete");
1714 TEST_DUP("when", "true", "false");
1715#undef TEST_DUP
1716
Radek Krejcia9026eb2018-12-12 16:04:47 +01001717 /* full content - without default due to a collision with mandatory */
1718 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 +01001719 "leaf-list ll {type string;} list li;mandatory true;reference test;status current;when true;m:ext;} ...";
1720 assert_int_equal(LY_SUCCESS, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch));
1721 assert_non_null(ch);
1722 assert_int_equal(LYS_CHOICE, ch->nodetype);
1723 assert_string_equal("ch", ch->name);
1724 assert_string_equal("test", ch->dsc);
1725 assert_non_null(ch->exts);
1726 assert_non_null(ch->iffeatures);
1727 assert_string_equal("test", ch->ref);
1728 assert_non_null(ch->when);
1729 assert_null(ch->parent);
1730 assert_null(ch->next);
1731 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_MAND_TRUE, ch->flags);
1732 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1733
Radek Krejcia9026eb2018-12-12 16:04:47 +01001734 /* full content - the default missing from the previous node */
1735 str = "ch {default c;case c;} ...";
1736 assert_int_equal(LY_SUCCESS, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch));
1737 assert_non_null(ch);
1738 assert_int_equal(LYS_CHOICE, ch->nodetype);
1739 assert_string_equal("ch", ch->name);
1740 assert_string_equal("c", ch->dflt);
1741 assert_int_equal(0, ch->flags);
1742 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1743
1744 /* invalid content */
1745 str = "ch {mandatory true; default c1; case c1 {leaf x{type string;}}} ...";
1746 assert_int_equal(LY_EVALID, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch));
1747 logbuf_assert("Invalid combination of keywords \"mandatory\" and \"default\" as substatements of \"choice\". Line number 1.");
1748 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1749
1750 *state = NULL;
1751 ly_ctx_destroy(ctx.ctx, NULL);
1752}
1753
1754static void
1755test_case(void **state)
1756{
1757 *state = test_case;
1758
Radek Krejcie7b95092019-05-15 11:03:07 +02001759 struct lys_parser_ctx ctx = {0};
Radek Krejcia9026eb2018-12-12 16:04:47 +01001760 struct lysp_node_case *cs = NULL;
1761 const char *str;
1762
1763 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1764 assert_non_null(ctx.ctx);
1765 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001766 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejcia9026eb2018-12-12 16:04:47 +01001767
1768 /* invalid cardinality */
1769#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1770 str = "cs {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1771 assert_int_equal(LY_EVALID, parse_case(&ctx, &str, NULL, (struct lysp_node**)&cs)); \
1772 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1773 lysp_node_free(ctx.ctx, (struct lysp_node*)cs); cs = NULL;
1774
1775 TEST_DUP("description", "text1", "text2");
1776 TEST_DUP("reference", "1", "2");
1777 TEST_DUP("status", "current", "obsolete");
1778 TEST_DUP("when", "true", "false");
1779#undef TEST_DUP
1780
1781 /* full content */
1782 str = "cs {anydata any;anyxml anyxml; choice ch;container c;description test;if-feature f;leaf l {type string;}"
1783 "leaf-list ll {type string;} list li;reference test;status current;uses grp;when true;m:ext;} ...";
1784 assert_int_equal(LY_SUCCESS, parse_case(&ctx, &str, NULL, (struct lysp_node**)&cs));
1785 assert_non_null(cs);
1786 assert_int_equal(LYS_CASE, cs->nodetype);
1787 assert_string_equal("cs", cs->name);
1788 assert_string_equal("test", cs->dsc);
1789 assert_non_null(cs->exts);
1790 assert_non_null(cs->iffeatures);
1791 assert_string_equal("test", cs->ref);
1792 assert_non_null(cs->when);
1793 assert_null(cs->parent);
1794 assert_null(cs->next);
1795 assert_int_equal(LYS_STATUS_CURR, cs->flags);
1796 lysp_node_free(ctx.ctx, (struct lysp_node*)cs); cs = NULL;
1797
1798 /* invalid content */
1799 str = "cs {config true} ...";
1800 assert_int_equal(LY_EVALID, parse_case(&ctx, &str, NULL, (struct lysp_node**)&cs));
1801 logbuf_assert("Invalid keyword \"config\" as a child of \"case\". Line number 1.");
1802 lysp_node_free(ctx.ctx, (struct lysp_node*)cs); cs = NULL;
1803
Radek Krejci056d0a82018-12-06 16:57:25 +01001804 *state = NULL;
1805 ly_ctx_destroy(ctx.ctx, NULL);
1806}
1807
Radek Krejci9800fb82018-12-13 14:26:23 +01001808static void
1809test_any(void **state, enum yang_keyword kw)
1810{
1811 *state = test_any;
1812
Radek Krejcie7b95092019-05-15 11:03:07 +02001813 struct lys_parser_ctx ctx = {0};
Radek Krejci9800fb82018-12-13 14:26:23 +01001814 struct lysp_node_anydata *any = NULL;
1815 const char *str;
1816
1817 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1818 assert_non_null(ctx.ctx);
1819 ctx.line = 1;
Radek Krejci9800fb82018-12-13 14:26:23 +01001820 if (kw == YANG_ANYDATA) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001821 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci9800fb82018-12-13 14:26:23 +01001822 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001823 ctx.mod_version = 1; /* simulate YANG 1.0 */
Radek Krejci9800fb82018-12-13 14:26:23 +01001824 }
1825
1826 /* invalid cardinality */
1827#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1828 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1829 assert_int_equal(LY_EVALID, parse_any(&ctx, &str, kw, NULL, (struct lysp_node**)&any)); \
1830 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1831 lysp_node_free(ctx.ctx, (struct lysp_node*)any); any = NULL;
1832
1833 TEST_DUP("config", "true", "false");
1834 TEST_DUP("description", "text1", "text2");
1835 TEST_DUP("mandatory", "true", "false");
1836 TEST_DUP("reference", "1", "2");
1837 TEST_DUP("status", "current", "obsolete");
1838 TEST_DUP("when", "true", "false");
1839#undef TEST_DUP
1840
1841 /* full content */
1842 str = "any {config true;description test;if-feature f;mandatory true;must 'expr';reference test;status current;when true;m:ext;} ...";
1843 assert_int_equal(LY_SUCCESS, parse_any(&ctx, &str, kw, NULL, (struct lysp_node**)&any));
1844 assert_non_null(any);
1845 assert_int_equal(kw == YANG_ANYDATA ? LYS_ANYDATA : LYS_ANYXML, any->nodetype);
1846 assert_string_equal("any", any->name);
1847 assert_string_equal("test", any->dsc);
1848 assert_non_null(any->exts);
1849 assert_non_null(any->iffeatures);
1850 assert_non_null(any->musts);
1851 assert_string_equal("test", any->ref);
1852 assert_non_null(any->when);
1853 assert_null(any->parent);
1854 assert_null(any->next);
1855 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR | LYS_MAND_TRUE, any->flags);
1856 lysp_node_free(ctx.ctx, (struct lysp_node*)any); any = NULL;
1857
1858 *state = NULL;
1859 ly_ctx_destroy(ctx.ctx, NULL);
1860}
1861
1862static void
1863test_anydata(void **state)
1864{
1865 return test_any(state, YANG_ANYDATA);
1866}
1867
1868static void
1869test_anyxml(void **state)
1870{
1871 return test_any(state, YANG_ANYXML);
1872}
1873
Radek Krejcie86bf772018-12-14 11:39:53 +01001874static void
1875test_grouping(void **state)
1876{
1877 *state = test_grouping;
1878
Radek Krejcie7b95092019-05-15 11:03:07 +02001879 struct lys_parser_ctx ctx = {0};
Radek Krejcie86bf772018-12-14 11:39:53 +01001880 struct lysp_grp *grp = NULL;
1881 const char *str;
1882
1883 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1884 assert_non_null(ctx.ctx);
1885 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001886 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejcie86bf772018-12-14 11:39:53 +01001887
1888 /* invalid cardinality */
1889#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1890 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1891 assert_int_equal(LY_EVALID, parse_grouping(&ctx, &str, NULL, &grp)); \
1892 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1893 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1894
1895 TEST_DUP("description", "text1", "text2");
1896 TEST_DUP("reference", "1", "2");
1897 TEST_DUP("status", "current", "obsolete");
1898#undef TEST_DUP
1899
1900 /* full content */
1901 str = "grp {action x;anydata any;anyxml anyxml; choice ch;container c;description test;grouping g;leaf l {type string;}"
1902 "leaf-list ll {type string;} list li;notification not;reference test;status current;typedef t {type int8;}uses g;m:ext;} ...";
1903 assert_int_equal(LY_SUCCESS, parse_grouping(&ctx, &str, NULL, &grp));
1904 assert_non_null(grp);
1905 assert_int_equal(LYS_GROUPING, grp->nodetype);
1906 assert_string_equal("grp", grp->name);
1907 assert_string_equal("test", grp->dsc);
1908 assert_non_null(grp->exts);
1909 assert_string_equal("test", grp->ref);
1910 assert_null(grp->parent);
1911 assert_int_equal( LYS_STATUS_CURR, grp->flags);
1912 ly_set_erase(&ctx.tpdfs_nodes, NULL);
1913 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1914
1915 /* invalid content */
1916 str = "grp {config true} ...";
1917 assert_int_equal(LY_EVALID, parse_grouping(&ctx, &str, NULL, &grp));
1918 logbuf_assert("Invalid keyword \"config\" as a child of \"grouping\". Line number 1.");
1919 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1920
1921 str = "grp {must 'expr'} ...";
1922 assert_int_equal(LY_EVALID, parse_grouping(&ctx, &str, NULL, &grp));
1923 logbuf_assert("Invalid keyword \"must\" as a child of \"grouping\". Line number 1.");
1924 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1925
1926 *state = NULL;
1927 ly_ctx_destroy(ctx.ctx, NULL);
1928}
1929
1930static void
Radek Krejcif538ce52019-03-05 10:46:14 +01001931test_action(void **state)
1932{
1933 *state = test_action;
1934
Radek Krejcie7b95092019-05-15 11:03:07 +02001935 struct lys_parser_ctx ctx = {0};
Radek Krejcif538ce52019-03-05 10:46:14 +01001936 struct lysp_action *rpcs = NULL;
1937 struct lysp_node_container *c = NULL;
1938 const char *str;
1939
1940 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1941 assert_non_null(ctx.ctx);
1942 ctx.line = 1;
1943 ctx.mod_version = 2; /* simulate YANG 1.1 */
1944
1945 /* invalid cardinality */
1946#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1947 str = "func {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1948 assert_int_equal(LY_EVALID, parse_action(&ctx, &str, NULL, &rpcs)); \
1949 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1950 FREE_ARRAY(ctx.ctx, rpcs, lysp_action_free); rpcs = NULL;
1951
1952 TEST_DUP("description", "text1", "text2");
1953 TEST_DUP("input", "", "");
1954 TEST_DUP("output", "", "");
1955 TEST_DUP("reference", "1", "2");
1956 TEST_DUP("status", "current", "obsolete");
1957#undef TEST_DUP
1958
1959 /* full content */
1960 str = "top;";
1961 assert_int_equal(LY_SUCCESS, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1962 str = "func {description test;grouping grp;if-feature f;reference test;status current;typedef mytype {type int8;} m:ext;"
1963 "input {anydata a1; anyxml a2; choice ch; container c; grouping grp; leaf l {type int8;} leaf-list ll {type int8;}"
1964 " list li; must 1; typedef mytypei {type int8;} uses grp; m:ext;}"
1965 "output {anydata a1; anyxml a2; choice ch; container c; grouping grp; leaf l {type int8;} leaf-list ll {type int8;}"
1966 " list li; must 1; typedef mytypeo {type int8;} uses grp; m:ext;}} ...";
1967 assert_int_equal(LY_SUCCESS, parse_action(&ctx, &str, (struct lysp_node*)c, &rpcs));
1968 assert_non_null(rpcs);
1969 assert_int_equal(LYS_ACTION, rpcs->nodetype);
1970 assert_string_equal("func", rpcs->name);
1971 assert_string_equal("test", rpcs->dsc);
1972 assert_non_null(rpcs->exts);
1973 assert_non_null(rpcs->iffeatures);
1974 assert_string_equal("test", rpcs->ref);
1975 assert_non_null(rpcs->groupings);
1976 assert_non_null(rpcs->typedefs);
1977 assert_int_equal(LYS_STATUS_CURR, rpcs->flags);
1978 /* input */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001979 assert_int_equal(rpcs->input.nodetype, LYS_INPUT);
Radek Krejcif538ce52019-03-05 10:46:14 +01001980 assert_non_null(rpcs->input.groupings);
1981 assert_non_null(rpcs->input.exts);
1982 assert_non_null(rpcs->input.musts);
1983 assert_non_null(rpcs->input.typedefs);
1984 assert_non_null(rpcs->input.data);
1985 /* output */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001986 assert_int_equal(rpcs->output.nodetype, LYS_OUTPUT);
Radek Krejcif538ce52019-03-05 10:46:14 +01001987 assert_non_null(rpcs->output.groupings);
1988 assert_non_null(rpcs->output.exts);
1989 assert_non_null(rpcs->output.musts);
1990 assert_non_null(rpcs->output.typedefs);
1991 assert_non_null(rpcs->output.data);
1992
1993 ly_set_erase(&ctx.tpdfs_nodes, NULL);
1994 FREE_ARRAY(ctx.ctx, rpcs, lysp_action_free); rpcs = NULL;
1995
1996 /* invalid content */
1997 str = "func {config true} ...";
1998 assert_int_equal(LY_EVALID, parse_action(&ctx, &str, NULL, &rpcs));
1999 logbuf_assert("Invalid keyword \"config\" as a child of \"rpc\". Line number 1.");
2000 FREE_ARRAY(ctx.ctx, rpcs, lysp_action_free); rpcs = NULL;
2001
2002 *state = NULL;
2003 lysp_node_free(ctx.ctx, (struct lysp_node*)c);
2004 ly_ctx_destroy(ctx.ctx, NULL);
2005}
2006
2007static void
Radek Krejcifc11bd72019-04-11 16:00:05 +02002008test_notification(void **state)
2009{
2010 *state = test_notification;
2011
Radek Krejcie7b95092019-05-15 11:03:07 +02002012 struct lys_parser_ctx ctx = {0};
Radek Krejcifc11bd72019-04-11 16:00:05 +02002013 struct lysp_notif *notifs = NULL;
2014 struct lysp_node_container *c = NULL;
2015 const char *str;
2016
2017 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
2018 assert_non_null(ctx.ctx);
2019 ctx.line = 1;
2020 ctx.mod_version = 2; /* simulate YANG 1.1 */
2021
2022 /* invalid cardinality */
2023#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
2024 str = "func {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
2025 assert_int_equal(LY_EVALID, parse_notif(&ctx, &str, NULL, &notifs)); \
2026 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
2027 FREE_ARRAY(ctx.ctx, notifs, lysp_notif_free); notifs = NULL;
2028
2029 TEST_DUP("description", "text1", "text2");
2030 TEST_DUP("reference", "1", "2");
2031 TEST_DUP("status", "current", "obsolete");
2032#undef TEST_DUP
2033
2034 /* full content */
2035 str = "top;";
2036 assert_int_equal(LY_SUCCESS, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
2037 str = "ntf {anydata a1; anyxml a2; choice ch; container c; description test; grouping grp; if-feature f; leaf l {type int8;}"
2038 "leaf-list ll {type int8;} list li; must 1; reference test; status current; typedef mytype {type int8;} uses grp; m:ext;}";
2039 assert_int_equal(LY_SUCCESS, parse_notif(&ctx, &str, (struct lysp_node*)c, &notifs));
2040 assert_non_null(notifs);
2041 assert_int_equal(LYS_NOTIF, notifs->nodetype);
2042 assert_string_equal("ntf", notifs->name);
2043 assert_string_equal("test", notifs->dsc);
2044 assert_non_null(notifs->exts);
2045 assert_non_null(notifs->iffeatures);
2046 assert_string_equal("test", notifs->ref);
2047 assert_non_null(notifs->groupings);
2048 assert_non_null(notifs->typedefs);
2049 assert_non_null(notifs->musts);
2050 assert_non_null(notifs->data);
2051 assert_int_equal(LYS_STATUS_CURR, notifs->flags);
2052
2053 ly_set_erase(&ctx.tpdfs_nodes, NULL);
2054 FREE_ARRAY(ctx.ctx, notifs, lysp_notif_free); notifs = NULL;
2055
2056 /* invalid content */
2057 str = "ntf {config true} ...";
2058 assert_int_equal(LY_EVALID, parse_notif(&ctx, &str, NULL, &notifs));
2059 logbuf_assert("Invalid keyword \"config\" as a child of \"notification\". Line number 1.");
2060 FREE_ARRAY(ctx.ctx, notifs, lysp_notif_free); notifs = NULL;
2061
2062 *state = NULL;
2063 lysp_node_free(ctx.ctx, (struct lysp_node*)c);
2064 ly_ctx_destroy(ctx.ctx, NULL);
2065}
2066
2067static void
Radek Krejcie86bf772018-12-14 11:39:53 +01002068test_uses(void **state)
2069{
2070 *state = test_uses;
2071
Radek Krejcie7b95092019-05-15 11:03:07 +02002072 struct lys_parser_ctx ctx = {0};
Radek Krejcie86bf772018-12-14 11:39:53 +01002073 struct lysp_node_uses *u = NULL;
2074 const char *str;
2075
2076 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
2077 assert_non_null(ctx.ctx);
2078 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002079 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejcie86bf772018-12-14 11:39:53 +01002080
2081 /* invalid cardinality */
2082#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
2083 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
2084 assert_int_equal(LY_EVALID, parse_uses(&ctx, &str, NULL, (struct lysp_node**)&u)); \
2085 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
2086 lysp_node_free(ctx.ctx, (struct lysp_node*)u); u = NULL;
2087
2088 TEST_DUP("description", "text1", "text2");
2089 TEST_DUP("reference", "1", "2");
2090 TEST_DUP("status", "current", "obsolete");
2091 TEST_DUP("when", "true", "false");
2092#undef TEST_DUP
2093
2094 /* full content */
2095 str = "grpref {augment some/node;description test;if-feature f;reference test;refine some/other/node;status current;when true;m:ext;} ...";
2096 assert_int_equal(LY_SUCCESS, parse_uses(&ctx, &str, NULL, (struct lysp_node**)&u));
2097 assert_non_null(u);
2098 assert_int_equal(LYS_USES, u->nodetype);
2099 assert_string_equal("grpref", u->name);
2100 assert_string_equal("test", u->dsc);
2101 assert_non_null(u->exts);
2102 assert_non_null(u->iffeatures);
2103 assert_string_equal("test", u->ref);
2104 assert_non_null(u->augments);
2105 assert_non_null(u->refines);
2106 assert_non_null(u->when);
2107 assert_null(u->parent);
2108 assert_null(u->next);
2109 assert_int_equal(LYS_STATUS_CURR, u->flags);
2110 lysp_node_free(ctx.ctx, (struct lysp_node*)u); u = NULL;
2111
2112 *state = NULL;
2113 ly_ctx_destroy(ctx.ctx, NULL);
2114}
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002115
2116
2117static void
2118test_augment(void **state)
2119{
2120 *state = test_augment;
2121
Radek Krejcie7b95092019-05-15 11:03:07 +02002122 struct lys_parser_ctx ctx = {0};
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002123 struct lysp_augment *a = NULL;
2124 const char *str;
2125
2126 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
2127 assert_non_null(ctx.ctx);
2128 ctx.line = 1;
Radek Krejcib4adbf12019-02-25 13:35:22 +01002129 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002130
2131 /* invalid cardinality */
2132#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
2133 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
2134 assert_int_equal(LY_EVALID, parse_augment(&ctx, &str, NULL, &a)); \
2135 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
Radek Krejcib4adbf12019-02-25 13:35:22 +01002136 FREE_ARRAY(ctx.ctx, a, lysp_augment_free); a = NULL;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002137
2138 TEST_DUP("description", "text1", "text2");
2139 TEST_DUP("reference", "1", "2");
2140 TEST_DUP("status", "current", "obsolete");
2141 TEST_DUP("when", "true", "false");
2142#undef TEST_DUP
2143
2144 /* full content */
2145 str = "/target/nodeid {action x; anydata any;anyxml anyxml; case cs; choice ch;container c;description test;if-feature f;leaf l {type string;}"
2146 "leaf-list ll {type string;} list li;notification not;reference test;status current;uses g;when true;m:ext;} ...";
2147 assert_int_equal(LY_SUCCESS, parse_augment(&ctx, &str, NULL, &a));
2148 assert_non_null(a);
2149 assert_int_equal(LYS_AUGMENT, a->nodetype);
2150 assert_string_equal("/target/nodeid", a->nodeid);
2151 assert_string_equal("test", a->dsc);
2152 assert_non_null(a->exts);
2153 assert_non_null(a->iffeatures);
2154 assert_string_equal("test", a->ref);
2155 assert_non_null(a->when);
2156 assert_null(a->parent);
2157 assert_int_equal(LYS_STATUS_CURR, a->flags);
Radek Krejcib4adbf12019-02-25 13:35:22 +01002158 FREE_ARRAY(ctx.ctx, a, lysp_augment_free); a = NULL;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002159
2160 *state = NULL;
2161 ly_ctx_destroy(ctx.ctx, NULL);
2162}
2163
Radek Krejcif09e4e82019-06-14 15:08:11 +02002164static void
2165test_when(void **state)
2166{
2167 *state = test_when;
2168
2169 struct lys_parser_ctx ctx = {0};
2170 struct lysp_when *w = NULL;
2171 const char *str;
2172
2173 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
2174 assert_non_null(ctx.ctx);
2175 ctx.line = 1;
2176 ctx.mod_version = 2; /* simulate YANG 1.1 */
2177
2178 /* invalid cardinality */
2179#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
2180 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
2181 assert_int_equal(LY_EVALID, parse_when(&ctx, &str, &w)); \
2182 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
2183 FREE_MEMBER(ctx.ctx, w, lysp_when_free); w = NULL;
2184
2185 TEST_DUP("description", "text1", "text2");
2186 TEST_DUP("reference", "1", "2");
2187#undef TEST_DUP
2188
2189 /* full content */
2190 str = "expression {description test;reference test;m:ext;} ...";
2191 assert_int_equal(LY_SUCCESS, parse_when(&ctx, &str, &w));
2192 assert_non_null(w);
2193 assert_string_equal("expression", w->cond);
2194 assert_string_equal("test", w->dsc);
2195 assert_string_equal("test", w->ref);
2196 assert_non_null(w->exts);
2197 FREE_MEMBER(ctx.ctx, w, lysp_when_free); w = NULL;
2198
2199 /* empty condition */
2200 str = "\"\";";
2201 assert_int_equal(LY_SUCCESS, parse_when(&ctx, &str, &w));
2202 logbuf_assert("Empty argument of when statement does not make sense.");
2203 assert_non_null(w);
2204 assert_string_equal("", w->cond);
2205 FREE_MEMBER(ctx.ctx, w, lysp_when_free); w = NULL;
2206
2207 *state = NULL;
2208 ly_ctx_destroy(ctx.ctx, NULL);
2209}
2210
David Sedlákd6ce6d72019-07-16 17:30:18 +02002211static void
2212test_value(void **state)
2213{
2214 *state = test_value;
2215 struct lys_parser_ctx ctx;
2216
2217 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
2218 assert_non_null(ctx.ctx);
2219 ctx.line = 1;
2220 ctx.indent = 0;
2221 int64_t val = 0;
2222 uint16_t flags = 0;
2223
2224 const char *data = "-0;";
2225 assert_int_equal(parse_type_enum_value_pos(&ctx, &data, YANG_VALUE, &val, &flags, NULL), LY_SUCCESS);
2226 assert_int_equal(val, 0);
2227
2228 data = "-0;";
2229 flags = 0;
2230 assert_int_equal(parse_type_enum_value_pos(&ctx, &data, YANG_POSITION, &val, &flags, NULL), LY_EVALID);
2231 logbuf_assert("Invalid value \"-0\" of \"position\". Line number 1.");
2232
2233 ly_ctx_destroy(ctx.ctx, NULL);
2234}
2235
Radek Krejci80dd33e2018-09-26 15:57:18 +02002236int main(void)
2237{
2238 const struct CMUnitTest tests[] = {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002239 cmocka_unit_test_setup(test_helpers, logger_setup),
Radek Krejci80dd33e2018-09-26 15:57:18 +02002240 cmocka_unit_test_setup(test_comments, logger_setup),
Radek Krejciefd22f62018-09-27 11:47:58 +02002241 cmocka_unit_test_setup(test_arg, logger_setup),
Radek Krejcidcc7b322018-10-11 14:24:02 +02002242 cmocka_unit_test_setup(test_stmts, logger_setup),
Radek Krejci05b13982018-11-28 16:22:07 +01002243 cmocka_unit_test_setup_teardown(test_minmax, logger_setup, logger_teardown),
Radek Krejci40544fa2019-01-11 09:38:37 +01002244 cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown),
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002245 cmocka_unit_test_setup_teardown(test_identity, logger_setup, logger_teardown),
Radek Krejci2c02f3e2018-10-16 10:54:38 +02002246 cmocka_unit_test_setup(test_feature, logger_setup),
2247 cmocka_unit_test_setup(test_deviation, logger_setup),
2248 cmocka_unit_test_setup(test_deviate, logger_setup),
Radek Krejci8c370832018-11-02 15:10:03 +01002249 cmocka_unit_test_setup(test_container, logger_setup),
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002250 cmocka_unit_test_setup_teardown(test_leaf, logger_setup, logger_teardown),
Radek Krejci0e5d8382018-11-28 16:37:53 +01002251 cmocka_unit_test_setup_teardown(test_leaflist, logger_setup, logger_teardown),
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002252 cmocka_unit_test_setup_teardown(test_list, logger_setup, logger_teardown),
Radek Krejci056d0a82018-12-06 16:57:25 +01002253 cmocka_unit_test_setup_teardown(test_choice, logger_setup, logger_teardown),
Radek Krejcia9026eb2018-12-12 16:04:47 +01002254 cmocka_unit_test_setup_teardown(test_case, logger_setup, logger_teardown),
Radek Krejci9800fb82018-12-13 14:26:23 +01002255 cmocka_unit_test_setup_teardown(test_anydata, logger_setup, logger_teardown),
2256 cmocka_unit_test_setup_teardown(test_anyxml, logger_setup, logger_teardown),
Radek Krejcif538ce52019-03-05 10:46:14 +01002257 cmocka_unit_test_setup_teardown(test_action, logger_setup, logger_teardown),
Radek Krejcifc11bd72019-04-11 16:00:05 +02002258 cmocka_unit_test_setup_teardown(test_notification, logger_setup, logger_teardown),
Radek Krejcie86bf772018-12-14 11:39:53 +01002259 cmocka_unit_test_setup_teardown(test_grouping, logger_setup, logger_teardown),
2260 cmocka_unit_test_setup_teardown(test_uses, logger_setup, logger_teardown),
Radek Krejcib4adbf12019-02-25 13:35:22 +01002261 cmocka_unit_test_setup_teardown(test_augment, logger_setup, logger_teardown),
Radek Krejcif09e4e82019-06-14 15:08:11 +02002262 cmocka_unit_test_setup_teardown(test_when, logger_setup, logger_teardown),
David Sedlákd6ce6d72019-07-16 17:30:18 +02002263 cmocka_unit_test_setup_teardown(test_value, logger_setup, logger_teardown),
Radek Krejci80dd33e2018-09-26 15:57:18 +02002264 };
2265
2266 return cmocka_run_group_tests(tests, NULL, NULL);
2267}