blob: 6342358ac5133ea011cbdf9ef1926e3e9c4d0833 [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);
Michal Vaskob36053d2020-03-26 15:49:30 +010041LY_ERR buf_store_char(struct lys_yang_parser_ctx *ctx, const char **input, enum yang_arg arg, char **word_p,
David Sedlák40bb13b2019-07-10 14:34:18 +020042 size_t *word_len, char **word_b, size_t *buf_len, int need_buf, int *prefix);
Michal Vaskob36053d2020-03-26 15:49:30 +010043LY_ERR get_keyword(struct lys_yang_parser_ctx *ctx, const char **data, enum ly_stmt *kw, char **word_p, size_t *word_len);
44LY_ERR get_argument(struct lys_yang_parser_ctx *ctx, const char **data, enum yang_arg arg,
Radek Krejci2d7a47b2019-05-16 13:34:10 +020045 uint16_t *flags, char **word_p, char **word_b, size_t *word_len);
Michal Vaskob36053d2020-03-26 15:49:30 +010046LY_ERR skip_comment(struct lys_yang_parser_ctx *ctx, const char **data, int comment);
Radek Krejci2d7a47b2019-05-16 13:34:10 +020047
Michal Vaskob36053d2020-03-26 15:49:30 +010048LY_ERR parse_action(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_action **actions);
49LY_ERR parse_any(struct lys_yang_parser_ctx *ctx, const char **data, enum ly_stmt kw, struct lysp_node *parent, struct lysp_node **siblings);
50LY_ERR parse_augment(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_augment **augments);
51LY_ERR parse_case(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
52LY_ERR parse_container(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
53LY_ERR parse_deviate(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_deviate **deviates);
54LY_ERR parse_deviation(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_deviation **deviations);
55LY_ERR parse_feature(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_feature **features);
56LY_ERR parse_grouping(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_grp **groupings);
57LY_ERR parse_choice(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
58LY_ERR parse_identity(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_ident **identities);
59LY_ERR parse_leaf(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
60LY_ERR parse_leaflist(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
61LY_ERR parse_list(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
62LY_ERR parse_maxelements(struct lys_yang_parser_ctx *ctx, const char **data, uint32_t *max, uint16_t *flags, struct lysp_ext_instance **exts);
63LY_ERR parse_minelements(struct lys_yang_parser_ctx *ctx, const char **data, uint32_t *min, uint16_t *flags, struct lysp_ext_instance **exts);
64LY_ERR parse_module(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_module *mod);
65LY_ERR parse_notif(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_notif **notifs);
66LY_ERR parse_submodule(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_submodule *submod);
67LY_ERR parse_uses(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_node *parent, struct lysp_node **siblings);
68LY_ERR parse_when(struct lys_yang_parser_ctx *ctx, const char **data, struct lysp_when **when_p);
69LY_ERR parse_type_enum_value_pos(struct lys_yang_parser_ctx *ctx, const char **data, enum ly_stmt 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;
Michal Vaskob36053d2020-03-26 15:49:30 +0100144 struct lys_yang_parser_ctx ctx;
145 ctx.format = LYS_IN_YANG;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200146 ctx.ctx = NULL;
Radek Krejci99435242019-09-05 16:19:15 +0200147 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200148 ctx.line = 1;
David Sedlák40bb13b2019-07-10 14:34:18 +0200149 int prefix = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200150
151 /* storing into buffer */
152 str = "abcd";
153 buf = NULL;
154 size = len = 0;
155 assert_int_equal(LY_SUCCESS, buf_add_char(NULL, &str, 2, &buf, &size, &len));
156 assert_int_not_equal(0, size);
157 assert_int_equal(2, len);
158 assert_string_equal("cd", str);
159 assert_false(strncmp("ab", buf, 2));
160 free(buf);
Radek Krejci404251e2018-10-09 12:06:44 +0200161 buf = NULL;
162
163 /* invalid first characters */
164 len = 0;
165 str = "2invalid";
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 str = "-invalid";
David Sedlák40bb13b2019-07-10 14:34:18 +0200170 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 +0200171 /* invalid following characters */
172 len = 3; /* number of characters read before the str content */
173 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 str = ":";
David Sedlák40bb13b2019-07-10 14:34:18 +0200176 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 +0200177 /* valid colon for prefixed identifiers */
178 len = size = 0;
179 p = NULL;
David Sedlák40bb13b2019-07-10 14:34:18 +0200180 prefix = 0;
Radek Krejci404251e2018-10-09 12:06:44 +0200181 str = "x:id";
David Sedlák40bb13b2019-07-10 14:34:18 +0200182 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 +0200183 assert_int_equal(1, len);
184 assert_null(buf);
185 assert_string_equal(":id", str);
186 assert_int_equal('x', p[len - 1]);
David Sedlák40bb13b2019-07-10 14:34:18 +0200187 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 +0200188 assert_int_equal(2, len);
189 assert_string_equal("id", str);
190 assert_int_equal(':', p[len - 1]);
191 free(buf);
David Sedlák40bb13b2019-07-10 14:34:18 +0200192 prefix = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200193
194 /* checking identifiers */
Michal Vaskob36053d2020-03-26 15:49:30 +0100195 assert_int_equal(LY_EVALID, lysp_check_identifierchar((struct lys_parser_ctx *)&ctx, ':', 0, NULL));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200196 logbuf_assert("Invalid identifier character ':'. Line number 1.");
Michal Vaskob36053d2020-03-26 15:49:30 +0100197 assert_int_equal(LY_EVALID, lysp_check_identifierchar((struct lys_parser_ctx *)&ctx, '#', 1, NULL));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200198 logbuf_assert("Invalid identifier first character '#'. Line number 1.");
199
Michal Vaskob36053d2020-03-26 15:49:30 +0100200 assert_int_equal(LY_SUCCESS, lysp_check_identifierchar((struct lys_parser_ctx *)&ctx, 'a', 1, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200201 assert_int_equal(0, prefix);
Michal Vaskob36053d2020-03-26 15:49:30 +0100202 assert_int_equal(LY_SUCCESS, lysp_check_identifierchar((struct lys_parser_ctx *)&ctx, ':', 0, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200203 assert_int_equal(1, prefix);
Michal Vaskob36053d2020-03-26 15:49:30 +0100204 assert_int_equal(LY_EVALID, lysp_check_identifierchar((struct lys_parser_ctx *)&ctx, ':', 0, &prefix));
Radek Krejcidcc7b322018-10-11 14:24:02 +0200205 assert_int_equal(1, prefix);
Michal Vaskob36053d2020-03-26 15:49:30 +0100206 assert_int_equal(LY_SUCCESS, lysp_check_identifierchar((struct lys_parser_ctx *)&ctx, 'b', 0, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200207 assert_int_equal(2, prefix);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200208 /* second colon is invalid */
Michal Vaskob36053d2020-03-26 15:49:30 +0100209 assert_int_equal(LY_EVALID, lysp_check_identifierchar((struct lys_parser_ctx *)&ctx, ':', 0, &prefix));
Radek Krejcidcc7b322018-10-11 14:24:02 +0200210 logbuf_assert("Invalid identifier character ':'. Line number 1.");
Radek Krejci44ceedc2018-10-02 15:54:31 +0200211}
Radek Krejci80dd33e2018-09-26 15:57:18 +0200212
213static void
214test_comments(void **state)
215{
216 (void) state; /* unused */
217
Michal Vaskob36053d2020-03-26 15:49:30 +0100218 struct lys_yang_parser_ctx ctx;
Radek Krejci80dd33e2018-09-26 15:57:18 +0200219 const char *str, *p;
Radek Krejciefd22f62018-09-27 11:47:58 +0200220 char *word, *buf;
221 size_t len;
Radek Krejci80dd33e2018-09-26 15:57:18 +0200222
Michal Vaskob36053d2020-03-26 15:49:30 +0100223 ctx.format = LYS_IN_YANG;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200224 ctx.ctx = NULL;
Radek Krejci99435242019-09-05 16:19:15 +0200225 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200226 ctx.line = 1;
227
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200228 str = " // this is a text of / one * line */ comment\nargument;";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200229 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200230 assert_string_equal("argument;", word);
Radek Krejciefd22f62018-09-27 11:47:58 +0200231 assert_null(buf);
232 assert_int_equal(8, len);
Radek Krejci80dd33e2018-09-26 15:57:18 +0200233
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200234 str = "/* this is a \n * text // of / block * comment */\"arg\" + \"ume\" \n + \n \"nt\";";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200235 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200236 assert_string_equal("argument", word);
237 assert_ptr_equal(buf, word);
238 assert_int_equal(8, len);
239 free(word);
Radek Krejci80dd33e2018-09-26 15:57:18 +0200240
241 str = p = " this is one line comment on last line";
Radek Krejci44ceedc2018-10-02 15:54:31 +0200242 assert_int_equal(LY_SUCCESS, skip_comment(&ctx, &str, 1));
Radek Krejci80dd33e2018-09-26 15:57:18 +0200243 assert_true(str[0] == '\0');
244
245 str = p = " this is a not terminated comment x";
Radek Krejci44ceedc2018-10-02 15:54:31 +0200246 assert_int_equal(LY_EVALID, skip_comment(&ctx, &str, 2));
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200247 logbuf_assert("Unexpected end-of-input, non-terminated comment. Line number 5.");
Radek Krejci80dd33e2018-09-26 15:57:18 +0200248 assert_true(str[0] == '\0');
249}
250
Radek Krejciefd22f62018-09-27 11:47:58 +0200251static void
252test_arg(void **state)
253{
254 (void) state; /* unused */
255
Michal Vaskob36053d2020-03-26 15:49:30 +0100256 struct lys_yang_parser_ctx ctx;
Radek Krejciefd22f62018-09-27 11:47:58 +0200257 const char *str;
258 char *word, *buf;
259 size_t len;
260
Michal Vaskob36053d2020-03-26 15:49:30 +0100261 ctx.format = LYS_IN_YANG;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200262 ctx.ctx = NULL;
Radek Krejci99435242019-09-05 16:19:15 +0200263 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200264 ctx.line = 1;
265
Radek Krejciefd22f62018-09-27 11:47:58 +0200266 /* missing argument */
267 str = ";";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200268 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_MAYBE_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200269 assert_null(word);
270
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200271 str = "{";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200272 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200273 logbuf_assert("Invalid character sequence \"{\", expected an argument. Line number 1.");
274
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200275 /* invalid escape sequence */
276 str = "\"\\s\"";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200277 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200278 logbuf_assert("Double-quoted string unknown special character \'\\s\'. Line number 1.");
279 str = "\'\\s\'"; /* valid, since it is not an escape sequence in single quoted string */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200280 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200281 assert_int_equal(2, len);
282 assert_string_equal("\\s\'", word);
283 assert_int_equal('\0', str[0]); /* input has been eaten */
284
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200285 /* invalid character after the argument */
286 str = "hello\"";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200287 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200288 logbuf_assert("Invalid character sequence \"\"\", expected unquoted string character, optsep, semicolon or opening brace. Line number 1.");
289 str = "hello}";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200290 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200291 logbuf_assert("Invalid character sequence \"}\", expected unquoted string character, optsep, semicolon or opening brace. Line number 1.");
292
David Sedlák40bb13b2019-07-10 14:34:18 +0200293 /* invalid identifier-ref-arg-str */
294 str = "pre:pre:value";
295 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &len));
296
Radek Krejci4e199f52019-05-28 09:09:28 +0200297 str = "\"\";"; /* empty identifier is not allowed */
298 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_IDENTIF_ARG, NULL, &word, &buf, &len));
299 logbuf_assert("Statement argument is required. Line number 1.");
300 logbuf_clean();
301 str = "\"\";"; /* empty reference identifier is not allowed */
302 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &len));
303 logbuf_assert("Statement argument is required. Line number 1.");
304
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200305 str = "hello/x\t"; /* slash is not an invalid character */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200306 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200307 assert_int_equal(7, len);
308 assert_string_equal("hello/x\t", word);
309
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200310 assert_null(buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200311
312 /* different quoting */
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200313 str = "hello ";
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);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200316 assert_int_equal(5, len);
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200317 assert_string_equal("hello ", word);
Radek Krejciefd22f62018-09-27 11:47:58 +0200318
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200319 str = "hello/*comment*/\n";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200320 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200321 assert_null(buf);
322 assert_int_equal(5, len);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200323 assert_false(strncmp("hello", word, len));
324
325
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200326 str = "\"hello\\n\\t\\\"\\\\\";";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200327 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
fredgand49fe112019-10-21 20:51:50 +0800328 assert_non_null(buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200329 assert_int_equal(9, len);
fredgand49fe112019-10-21 20:51:50 +0800330 assert_string_equal("hello\n\t\"\\", word);
331 free(buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200332
333 ctx.indent = 14;
334 str = "\"hello \t\n\t\t world!\"";
335 /* - space and tabs before newline are stripped out
336 * - space and tabs after newline (indentation) are stripped out
337 */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200338 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200339 assert_non_null(buf);
340 assert_ptr_equal(word, buf);
341 assert_int_equal(14, len);
342 assert_string_equal("hello\n world!", word);
343 free(buf);
Radek Krejciff13cd12019-10-25 15:34:24 +0200344 /* In contrast to previous, the backslash-escaped tabs are expanded after trimming, so they are preserved */
345 ctx.indent = 14;
346 str = "\"hello \\t\n\t\\t world!\"";
347 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
348 assert_non_null(buf);
349 assert_ptr_equal(word, buf);
350 assert_int_equal(16, len);
351 assert_string_equal("hello \t\n\t world!", word);
352 free(buf);
353 /* Do not handle whitespaces after backslash-escaped newline as indentation */
354 ctx.indent = 14;
355 str = "\"hello\\n\t\t world!\"";
356 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
357 assert_non_null(buf);
358 assert_ptr_equal(word, buf);
359 assert_int_equal(15, len);
360 assert_string_equal("hello\n\t\t world!", word);
361 free(buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200362
363 ctx.indent = 14;
364 str = "\"hello\n \tworld!\"";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200365 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200366 assert_non_null(buf);
367 assert_ptr_equal(word, buf);
368 assert_int_equal(12, len);
369 assert_string_equal("hello\nworld!", word);
370 free(buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200371
372 str = "\'hello\'";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200373 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200374 assert_null(buf);
375 assert_int_equal(5, len);
376 assert_false(strncmp("hello", word, 5));
377
378 str = "\"hel\" +\t\n\"lo\"";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200379 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200380 assert_ptr_equal(word, buf);
381 assert_int_equal(5, len);
382 assert_string_equal("hello", word);
383 free(buf);
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200384 str = "\"hel\" +\t\nlo"; /* unquoted the second part */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200385 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciff13cd12019-10-25 15:34:24 +0200386 logbuf_assert("Both string parts divided by '+' must be quoted. Line number 6.");
Radek Krejciefd22f62018-09-27 11:47:58 +0200387
388 str = "\'he\'\t\n+ \"llo\"";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200389 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200390 assert_ptr_equal(word, buf);
391 assert_int_equal(5, len);
392 assert_string_equal("hello", word);
393 free(buf);
394
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200395 str = " \t\n\"he\"+\'llo\'";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200396 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200397 assert_ptr_equal(word, buf);
398 assert_int_equal(5, len);
399 assert_string_equal("hello", word);
400 free(buf);
401
Radek Krejci44ceedc2018-10-02 15:54:31 +0200402 /* missing argument */
403 str = ";";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200404 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciff13cd12019-10-25 15:34:24 +0200405 logbuf_assert("Invalid character sequence \";\", expected an argument. Line number 8.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200406}
407
408static void
409test_stmts(void **state)
410{
411 (void) state; /* unused */
412
Michal Vaskob36053d2020-03-26 15:49:30 +0100413 struct lys_yang_parser_ctx ctx;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200414 const char *str, *p;
Radek Krejcid6b76452019-09-03 17:03:03 +0200415 enum ly_stmt kw;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200416 char *word;
417 size_t len;
418
Michal Vaskob36053d2020-03-26 15:49:30 +0100419 ctx.format = LYS_IN_YANG;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200420 ctx.ctx = NULL;
Radek Krejci99435242019-09-05 16:19:15 +0200421 ctx.pos_type = LY_VLOG_LINE;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200422 ctx.line = 1;
423
424 str = "\n// comment\n\tinput\t{";
425 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200426 assert_int_equal(LY_STMT_INPUT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200427 assert_int_equal(5, len);
428 assert_string_equal("input\t{", word);
429 assert_string_equal("\t{", str);
430
431 str = "\t /* comment */\t output\n\t{";
432 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200433 assert_int_equal(LY_STMT_OUTPUT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200434 assert_int_equal(6, len);
435 assert_string_equal("output\n\t{", word);
436 assert_string_equal("\n\t{", str);
Radek Krejciabdd8062019-06-11 16:44:19 +0200437 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200438 assert_int_equal(LY_STMT_SYNTAX_LEFT_BRACE, kw);
Radek Krejciabdd8062019-06-11 16:44:19 +0200439 assert_int_equal(1, len);
440 assert_string_equal("{", word);
441 assert_string_equal("", str);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200442
443 str = "/input { "; /* invalid slash */
444 assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len));
445 logbuf_assert("Invalid identifier first character '/'. Line number 4.");
446
447 str = "not-a-statement-nor-extension { "; /* invalid identifier */
448 assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len));
449 logbuf_assert("Invalid character sequence \"not-a-statement-nor-extension\", expected a keyword. Line number 4.");
450
451 str = "path;"; /* missing sep after the keyword */
452 assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len));
453 logbuf_assert("Invalid character sequence \"path;\", expected a keyword followed by a separator. Line number 4.");
454
455 str = "action ";
456 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200457 assert_int_equal(LY_STMT_ACTION, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200458 assert_int_equal(6, len);
459 str = "anydata ";
460 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200461 assert_int_equal(LY_STMT_ANYDATA, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200462 assert_int_equal(7, len);
463 str = "anyxml ";
464 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200465 assert_int_equal(LY_STMT_ANYXML, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200466 assert_int_equal(6, len);
467 str = "argument ";
468 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200469 assert_int_equal(LY_STMT_ARGUMENT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200470 assert_int_equal(8, len);
471 str = "augment ";
472 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200473 assert_int_equal(LY_STMT_AUGMENT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200474 assert_int_equal(7, len);
475 str = "base ";
476 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200477 assert_int_equal(LY_STMT_BASE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200478 assert_int_equal(4, len);
479 str = "belongs-to ";
480 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200481 assert_int_equal(LY_STMT_BELONGS_TO, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200482 assert_int_equal(10, len);
483 str = "bit ";
484 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200485 assert_int_equal(LY_STMT_BIT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200486 assert_int_equal(3, len);
487 str = "case ";
488 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200489 assert_int_equal(LY_STMT_CASE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200490 assert_int_equal(4, len);
491 str = "choice ";
492 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200493 assert_int_equal(LY_STMT_CHOICE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200494 assert_int_equal(6, len);
495 str = "config ";
496 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200497 assert_int_equal(LY_STMT_CONFIG, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200498 assert_int_equal(6, len);
499 str = "contact ";
500 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200501 assert_int_equal(LY_STMT_CONTACT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200502 assert_int_equal(7, len);
503 str = "container ";
504 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200505 assert_int_equal(LY_STMT_CONTAINER, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200506 assert_int_equal(9, len);
507 str = "default ";
508 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200509 assert_int_equal(LY_STMT_DEFAULT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200510 assert_int_equal(7, len);
511 str = "description ";
512 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200513 assert_int_equal(LY_STMT_DESCRIPTION, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200514 assert_int_equal(11, len);
515 str = "deviate ";
516 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200517 assert_int_equal(LY_STMT_DEVIATE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200518 assert_int_equal(7, len);
519 str = "deviation ";
520 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200521 assert_int_equal(LY_STMT_DEVIATION, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200522 assert_int_equal(9, len);
523 str = "enum ";
524 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200525 assert_int_equal(LY_STMT_ENUM, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200526 assert_int_equal(4, len);
527 str = "error-app-tag ";
528 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200529 assert_int_equal(LY_STMT_ERROR_APP_TAG, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200530 assert_int_equal(13, len);
531 str = "error-message ";
532 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200533 assert_int_equal(LY_STMT_ERROR_MESSAGE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200534 assert_int_equal(13, len);
535 str = "extension ";
536 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200537 assert_int_equal(LY_STMT_EXTENSION, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200538 assert_int_equal(9, len);
539 str = "feature ";
540 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200541 assert_int_equal(LY_STMT_FEATURE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200542 assert_int_equal(7, len);
543 str = "fraction-digits ";
544 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200545 assert_int_equal(LY_STMT_FRACTION_DIGITS, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200546 assert_int_equal(15, len);
547 str = "grouping ";
548 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200549 assert_int_equal(LY_STMT_GROUPING, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200550 assert_int_equal(8, len);
551 str = "identity ";
552 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200553 assert_int_equal(LY_STMT_IDENTITY, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200554 assert_int_equal(8, len);
555 str = "if-feature ";
556 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200557 assert_int_equal(LY_STMT_IF_FEATURE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200558 assert_int_equal(10, len);
559 str = "import ";
560 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200561 assert_int_equal(LY_STMT_IMPORT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200562 assert_int_equal(6, len);
563 str = "include ";
564 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200565 assert_int_equal(LY_STMT_INCLUDE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200566 assert_int_equal(7, len);
567 str = "input{";
568 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200569 assert_int_equal(LY_STMT_INPUT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200570 assert_int_equal(5, len);
571 str = "key ";
572 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200573 assert_int_equal(LY_STMT_KEY, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200574 assert_int_equal(3, len);
575 str = "leaf ";
576 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200577 assert_int_equal(LY_STMT_LEAF, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200578 assert_int_equal(4, len);
579 str = "leaf-list ";
580 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200581 assert_int_equal(LY_STMT_LEAF_LIST, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200582 assert_int_equal(9, len);
583 str = "length ";
584 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200585 assert_int_equal(LY_STMT_LENGTH, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200586 assert_int_equal(6, len);
587 str = "list ";
588 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200589 assert_int_equal(LY_STMT_LIST, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200590 assert_int_equal(4, len);
591 str = "mandatory ";
592 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200593 assert_int_equal(LY_STMT_MANDATORY, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200594 assert_int_equal(9, len);
595 str = "max-elements ";
596 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200597 assert_int_equal(LY_STMT_MAX_ELEMENTS, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200598 assert_int_equal(12, len);
599 str = "min-elements ";
600 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200601 assert_int_equal(LY_STMT_MIN_ELEMENTS, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200602 assert_int_equal(12, len);
603 str = "modifier ";
604 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200605 assert_int_equal(LY_STMT_MODIFIER, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200606 assert_int_equal(8, len);
607 str = "module ";
608 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200609 assert_int_equal(LY_STMT_MODULE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200610 assert_int_equal(6, len);
611 str = "must ";
612 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200613 assert_int_equal(LY_STMT_MUST, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200614 assert_int_equal(4, len);
615 str = "namespace ";
616 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200617 assert_int_equal(LY_STMT_NAMESPACE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200618 assert_int_equal(9, len);
619 str = "notification ";
620 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200621 assert_int_equal(LY_STMT_NOTIFICATION, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200622 assert_int_equal(12, len);
623 str = "ordered-by ";
624 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200625 assert_int_equal(LY_STMT_ORDERED_BY, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200626 assert_int_equal(10, len);
627 str = "organization ";
628 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200629 assert_int_equal(LY_STMT_ORGANIZATION, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200630 assert_int_equal(12, len);
631 str = "output ";
632 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200633 assert_int_equal(LY_STMT_OUTPUT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200634 assert_int_equal(6, len);
635 str = "path ";
636 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200637 assert_int_equal(LY_STMT_PATH, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200638 assert_int_equal(4, len);
639 str = "pattern ";
640 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200641 assert_int_equal(LY_STMT_PATTERN, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200642 assert_int_equal(7, len);
643 str = "position ";
644 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200645 assert_int_equal(LY_STMT_POSITION, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200646 assert_int_equal(8, len);
647 str = "prefix ";
648 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200649 assert_int_equal(LY_STMT_PREFIX, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200650 assert_int_equal(6, len);
651 str = "presence ";
652 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200653 assert_int_equal(LY_STMT_PRESENCE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200654 assert_int_equal(8, len);
655 str = "range ";
656 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200657 assert_int_equal(LY_STMT_RANGE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200658 assert_int_equal(5, len);
659 str = "reference ";
660 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200661 assert_int_equal(LY_STMT_REFERENCE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200662 assert_int_equal(9, len);
663 str = "refine ";
664 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200665 assert_int_equal(LY_STMT_REFINE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200666 assert_int_equal(6, len);
667 str = "require-instance ";
668 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200669 assert_int_equal(LY_STMT_REQUIRE_INSTANCE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200670 assert_int_equal(16, len);
671 str = "revision ";
672 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200673 assert_int_equal(LY_STMT_REVISION, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200674 assert_int_equal(8, len);
675 str = "revision-date ";
676 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200677 assert_int_equal(LY_STMT_REVISION_DATE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200678 assert_int_equal(13, len);
679 str = "rpc ";
680 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200681 assert_int_equal(LY_STMT_RPC, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200682 assert_int_equal(3, len);
683 str = "status ";
684 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200685 assert_int_equal(LY_STMT_STATUS, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200686 assert_int_equal(6, len);
687 str = "submodule ";
688 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200689 assert_int_equal(LY_STMT_SUBMODULE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200690 assert_int_equal(9, len);
691 str = "type ";
692 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200693 assert_int_equal(LY_STMT_TYPE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200694 assert_int_equal(4, len);
695 str = "typedef ";
696 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200697 assert_int_equal(LY_STMT_TYPEDEF, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200698 assert_int_equal(7, len);
699 str = "unique ";
700 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200701 assert_int_equal(LY_STMT_UNIQUE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200702 assert_int_equal(6, len);
703 str = "units ";
704 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200705 assert_int_equal(LY_STMT_UNITS, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200706 assert_int_equal(5, len);
707 str = "uses ";
708 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200709 assert_int_equal(LY_STMT_USES, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200710 assert_int_equal(4, len);
711 str = "value ";
712 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200713 assert_int_equal(LY_STMT_VALUE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200714 assert_int_equal(5, len);
715 str = "when ";
716 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200717 assert_int_equal(LY_STMT_WHEN, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200718 assert_int_equal(4, len);
719 str = "yang-version ";
720 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200721 assert_int_equal(LY_STMT_YANG_VERSION, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200722 assert_int_equal(12, len);
723 str = "yin-element ";
724 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200725 assert_int_equal(LY_STMT_YIN_ELEMENT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200726 assert_int_equal(11, len);
Radek Krejci626df482018-10-11 15:06:31 +0200727 str = ";config false;";
728 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200729 assert_int_equal(LY_STMT_SYNTAX_SEMICOLON, kw);
Radek Krejci626df482018-10-11 15:06:31 +0200730 assert_int_equal(1, len);
731 assert_string_equal("config false;", str);
732 str = "{ config false;";
733 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200734 assert_int_equal(LY_STMT_SYNTAX_LEFT_BRACE, kw);
Radek Krejci626df482018-10-11 15:06:31 +0200735 assert_int_equal(1, len);
736 assert_string_equal(" config false;", str);
737 str = "}";
738 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200739 assert_int_equal(LY_STMT_SYNTAX_RIGHT_BRACE, kw);
Radek Krejci626df482018-10-11 15:06:31 +0200740 assert_int_equal(1, len);
741 assert_string_equal("", str);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200742
743 /* geenric extension */
744 str = p = "nacm:default-deny-write;";
745 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200746 assert_int_equal(LY_STMT_EXTENSION_INSTANCE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200747 assert_int_equal(23, len);
748 assert_ptr_equal(p, word);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200749}
Radek Krejci44ceedc2018-10-02 15:54:31 +0200750
Radek Krejci05b13982018-11-28 16:22:07 +0100751static void
752test_minmax(void **state)
753{
754 *state = test_minmax;
755
Michal Vaskob36053d2020-03-26 15:49:30 +0100756 struct lys_yang_parser_ctx ctx = {0};
Radek Krejci05b13982018-11-28 16:22:07 +0100757 uint16_t flags = 0;
758 uint32_t value = 0;
759 struct lysp_ext_instance *ext = NULL;
760 const char *str;
761
Michal Vaskob36053d2020-03-26 15:49:30 +0100762 ctx.format = LYS_IN_YANG;
Radek Krejci05b13982018-11-28 16:22:07 +0100763 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
764 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +0200765 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci05b13982018-11-28 16:22:07 +0100766 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100767 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci05b13982018-11-28 16:22:07 +0100768
Radek Krejcidf6cad12018-11-28 17:10:55 +0100769 str = " 1invalid; ...";
Radek Krejci05b13982018-11-28 16:22:07 +0100770 assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext));
Radek Krejcidf6cad12018-11-28 17:10:55 +0100771 logbuf_assert("Invalid value \"1invalid\" of \"min-elements\". Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100772
773 flags = value = 0;
774 str = " -1; ...";
775 assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext));
776 logbuf_assert("Invalid value \"-1\" of \"min-elements\". Line number 1.");
777
Radek Krejcidf6cad12018-11-28 17:10:55 +0100778 /* implementation limit */
779 flags = value = 0;
780 str = " 4294967296; ...";
781 assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext));
782 logbuf_assert("Value \"4294967296\" is out of \"min-elements\" bounds. Line number 1.");
783
Radek Krejci05b13982018-11-28 16:22:07 +0100784 flags = value = 0;
785 str = " 1; ...";
786 assert_int_equal(LY_SUCCESS, parse_minelements(&ctx, &str, &value, &flags, &ext));
787 assert_int_equal(LYS_SET_MIN, flags);
788 assert_int_equal(1, value);
789
790 flags = value = 0;
791 str = " 1 {m:ext;} ...";
792 assert_int_equal(LY_SUCCESS, parse_minelements(&ctx, &str, &value, &flags, &ext));
793 assert_int_equal(LYS_SET_MIN, flags);
794 assert_int_equal(1, value);
795 assert_non_null(ext);
796 FREE_ARRAY(ctx.ctx, ext, lysp_ext_instance_free);
797 ext = NULL;
798
799 flags = value = 0;
800 str = " 1 {config true;} ...";
801 assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext));
802 logbuf_assert("Invalid keyword \"config\" as a child of \"min-elements\". Line number 1.");
803
Radek Krejcidf6cad12018-11-28 17:10:55 +0100804 str = " 1invalid; ...";
Radek Krejci05b13982018-11-28 16:22:07 +0100805 assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext));
Radek Krejcidf6cad12018-11-28 17:10:55 +0100806 logbuf_assert("Invalid value \"1invalid\" of \"max-elements\". Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100807
808 flags = value = 0;
809 str = " -1; ...";
810 assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext));
811 logbuf_assert("Invalid value \"-1\" of \"max-elements\". Line number 1.");
812
Radek Krejcidf6cad12018-11-28 17:10:55 +0100813 /* implementation limit */
814 flags = value = 0;
815 str = " 4294967296; ...";
816 assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext));
817 logbuf_assert("Value \"4294967296\" is out of \"max-elements\" bounds. Line number 1.");
818
Radek Krejci05b13982018-11-28 16:22:07 +0100819 flags = value = 0;
820 str = " 1; ...";
821 assert_int_equal(LY_SUCCESS, parse_maxelements(&ctx, &str, &value, &flags, &ext));
822 assert_int_equal(LYS_SET_MAX, flags);
823 assert_int_equal(1, value);
824
825 flags = value = 0;
826 str = " unbounded; ...";
827 assert_int_equal(LY_SUCCESS, parse_maxelements(&ctx, &str, &value, &flags, &ext));
828 assert_int_equal(LYS_SET_MAX, flags);
829 assert_int_equal(0, value);
830
831 flags = value = 0;
832 str = " 1 {m:ext;} ...";
833 assert_int_equal(LY_SUCCESS, parse_maxelements(&ctx, &str, &value, &flags, &ext));
834 assert_int_equal(LYS_SET_MAX, flags);
835 assert_int_equal(1, value);
836 assert_non_null(ext);
837 FREE_ARRAY(ctx.ctx, ext, lysp_ext_instance_free);
838 ext = NULL;
839
840 flags = value = 0;
841 str = " 1 {config true;} ...";
842 assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext));
843 logbuf_assert("Invalid keyword \"config\" as a child of \"max-elements\". Line number 1.");
844
845 *state = NULL;
846 ly_ctx_destroy(ctx.ctx, NULL);
847}
848
Radek Krejci9fcacc12018-10-11 15:59:11 +0200849static struct lysp_module *
Michal Vaskob36053d2020-03-26 15:49:30 +0100850mod_renew(struct lys_yang_parser_ctx *ctx)
Radek Krejci9fcacc12018-10-11 15:59:11 +0200851{
Radek Krejci40544fa2019-01-11 09:38:37 +0100852 struct lysp_module *mod_p;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100853 static struct lys_module mod = {0};
854
855 lysc_module_free(mod.compiled, NULL);
856 lysp_module_free(mod.parsed);
857 FREE_STRING(mod.ctx, mod.name);
858 FREE_STRING(mod.ctx, mod.ns);
859 FREE_STRING(mod.ctx, mod.prefix);
860 FREE_STRING(mod.ctx, mod.filepath);
861 FREE_STRING(mod.ctx, mod.org);
862 FREE_STRING(mod.ctx, mod.contact);
863 FREE_STRING(mod.ctx, mod.dsc);
864 FREE_STRING(mod.ctx, mod.ref);
865 memset(&mod, 0, sizeof mod);
866 mod.ctx = ctx->ctx;
867
868 mod_p = calloc(1, sizeof *mod_p);
869 mod.parsed = mod_p;
870 mod_p->mod = &mod;
871 assert_non_null(mod_p);
872 return mod_p;
873}
874
875static struct lysp_submodule *
Michal Vaskob36053d2020-03-26 15:49:30 +0100876submod_renew(struct lys_yang_parser_ctx *ctx, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100877{
878 lysp_submodule_free(ctx->ctx, submod);
879 submod = calloc(1, sizeof *submod);
880 assert_non_null(submod);
881 return submod;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200882}
883
Radek Krejcid33273d2018-10-25 14:55:52 +0200884static LY_ERR test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
885 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
886 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
887{
888 *module_data = user_data;
889 *format = LYS_IN_YANG;
890 *free_module_data = NULL;
891 return LY_SUCCESS;
892}
893
Radek Krejci9fcacc12018-10-11 15:59:11 +0200894static void
895test_module(void **state)
896{
Radek Krejci40544fa2019-01-11 09:38:37 +0100897 *state = test_module;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200898
Michal Vaskob36053d2020-03-26 15:49:30 +0100899 struct lys_yang_parser_ctx ctx;
Radek Krejci40544fa2019-01-11 09:38:37 +0100900 struct lysp_module *mod = NULL;
901 struct lysp_submodule *submod = NULL;
902 struct lys_module *m;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200903 const char *str;
904
Michal Vaskob36053d2020-03-26 15:49:30 +0100905 ctx.format = LYS_IN_YANG;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200906 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
907 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +0200908 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200909 ctx.line = 1;
Radek Krejcia042ea12018-10-13 07:52:15 +0200910 ctx.indent = 0;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200911
Radek Krejci40544fa2019-01-11 09:38:37 +0100912 mod = mod_renew(&ctx);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200913
914 /* missing mandatory substatements */
915 str = " name {}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100916 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
917 assert_string_equal("name", mod->mod->name);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200918 logbuf_assert("Missing mandatory keyword \"namespace\" as a child of \"module\". Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100919 mod = mod_renew(&ctx);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200920
921 str = " name {namespace urn:x;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100922 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
923 assert_string_equal("urn:x", mod->mod->ns);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200924 logbuf_assert("Missing mandatory keyword \"prefix\" as a child of \"module\". Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100925 mod = mod_renew(&ctx);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200926
927 str = " name {namespace urn:x;prefix \"x\";}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100928 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod));
929 assert_string_equal("x", mod->mod->prefix);
Radek Krejci40544fa2019-01-11 09:38:37 +0100930 mod = mod_renew(&ctx);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200931
Radek Krejci027d5802018-11-14 16:57:28 +0100932#define SCHEMA_BEGINNING " name {yang-version 1.1;namespace urn:x;prefix \"x\";"
933#define SCHEMA_BEGINNING2 " name {namespace urn:x;prefix \"x\";"
Radek Krejcia042ea12018-10-13 07:52:15 +0200934#define TEST_NODE(NODETYPE, INPUT, NAME) \
935 str = SCHEMA_BEGINNING INPUT; \
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100936 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod)); \
Radek Krejcia042ea12018-10-13 07:52:15 +0200937 assert_non_null(mod->data); \
938 assert_int_equal(NODETYPE, mod->data->nodetype); \
939 assert_string_equal(NAME, mod->data->name); \
Radek Krejci40544fa2019-01-11 09:38:37 +0100940 mod = mod_renew(&ctx);
Radek Krejcia042ea12018-10-13 07:52:15 +0200941#define TEST_GENERIC(INPUT, TARGET, TEST) \
942 str = SCHEMA_BEGINNING INPUT; \
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100943 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod)); \
Radek Krejcia042ea12018-10-13 07:52:15 +0200944 assert_non_null(TARGET); \
945 TEST; \
Radek Krejci40544fa2019-01-11 09:38:37 +0100946 mod = mod_renew(&ctx);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100947#define TEST_DUP(MEMBER, VALUE1, VALUE2, LINE) \
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200948 TEST_DUP_GENERIC(SCHEMA_BEGINNING, MEMBER, VALUE1, VALUE2, \
Radek Krejci40544fa2019-01-11 09:38:37 +0100949 parse_module, mod, LINE, mod = mod_renew(&ctx))
Radek Krejcia042ea12018-10-13 07:52:15 +0200950
951 /* duplicated namespace, prefix */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100952 TEST_DUP("namespace", "y", "z", "1");
953 TEST_DUP("prefix", "y", "z", "1");
954 TEST_DUP("contact", "a", "b", "1");
955 TEST_DUP("description", "a", "b", "1");
956 TEST_DUP("organization", "a", "b", "1");
957 TEST_DUP("reference", "a", "b", "1");
Radek Krejcia042ea12018-10-13 07:52:15 +0200958
Radek Krejci70853c52018-10-15 14:46:16 +0200959 /* not allowed in module (submodule-specific) */
960 str = SCHEMA_BEGINNING "belongs-to master {prefix m;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100961 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejci70853c52018-10-15 14:46:16 +0200962 logbuf_assert("Invalid keyword \"belongs-to\" as a child of \"module\". Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100963 mod = mod_renew(&ctx);
Radek Krejci70853c52018-10-15 14:46:16 +0200964
Radek Krejcia042ea12018-10-13 07:52:15 +0200965 /* anydata */
966 TEST_NODE(LYS_ANYDATA, "anydata test;}", "test");
967 /* anyxml */
968 TEST_NODE(LYS_ANYXML, "anyxml test;}", "test");
969 /* augment */
970 TEST_GENERIC("augment /somepath;}", mod->augments,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200971 assert_string_equal("/somepath", mod->augments[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200972 /* choice */
973 TEST_NODE(LYS_CHOICE, "choice test;}", "test");
974 /* contact 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100975 TEST_GENERIC("contact \"firstname\" + \n\t\" surname\";}", mod->mod->contact,
976 assert_string_equal("firstname surname", mod->mod->contact));
Radek Krejcia042ea12018-10-13 07:52:15 +0200977 /* container */
978 TEST_NODE(LYS_CONTAINER, "container test;}", "test");
979 /* description 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100980 TEST_GENERIC("description \'some description\';}", mod->mod->dsc,
981 assert_string_equal("some description", mod->mod->dsc));
Radek Krejcia042ea12018-10-13 07:52:15 +0200982 /* deviation */
983 TEST_GENERIC("deviation /somepath {deviate not-supported;}}", mod->deviations,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200984 assert_string_equal("/somepath", mod->deviations[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200985 /* extension */
986 TEST_GENERIC("extension test;}", mod->extensions,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200987 assert_string_equal("test", mod->extensions[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200988 /* feature */
989 TEST_GENERIC("feature test;}", mod->features,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200990 assert_string_equal("test", mod->features[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200991 /* grouping */
992 TEST_GENERIC("grouping grp;}", mod->groupings,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200993 assert_string_equal("grp", mod->groupings[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200994 /* identity */
995 TEST_GENERIC("identity test;}", mod->identities,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200996 assert_string_equal("test", mod->identities[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200997 /* import */
Radek Krejci086c7132018-10-26 15:29:04 +0200998 ly_ctx_set_module_imp_clb(ctx.ctx, test_imp_clb, "module zzz { namespace urn:zzz; prefix z;}");
999 TEST_GENERIC("import zzz {prefix z;}}", mod->imports,
1000 assert_string_equal("zzz", mod->imports[0].name));
Radek Krejci70853c52018-10-15 14:46:16 +02001001
Radek Krejcia042ea12018-10-13 07:52:15 +02001002 /* import - prefix collision */
Radek Krejci086c7132018-10-26 15:29:04 +02001003 str = SCHEMA_BEGINNING "import zzz {prefix x;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001004 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejci70853c52018-10-15 14:46:16 +02001005 logbuf_assert("Prefix \"x\" already used as module prefix. Line number 2.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001006 mod = mod_renew(&ctx);
Radek Krejci086c7132018-10-26 15:29:04 +02001007 str = SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix y;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001008 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejci086c7132018-10-26 15:29:04 +02001009 logbuf_assert("Prefix \"y\" already used to import \"zzz\" module. Line number 2.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001010 mod = mod_renew(&ctx);
Radek Krejci086c7132018-10-26 15:29:04 +02001011 str = "module" SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix z;}}";
1012 assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
1013 assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx));
1014 logbuf_assert("Single revision of the module \"zzz\" referred twice.");
Radek Krejci70853c52018-10-15 14:46:16 +02001015
Radek Krejcia042ea12018-10-13 07:52:15 +02001016 /* include */
Radek Krejci9ed7a192018-10-31 16:23:51 +01001017 store = 1;
Radek Krejcid33273d2018-10-25 14:55:52 +02001018 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 +02001019 str = "module" SCHEMA_BEGINNING "include xxx;}";
1020 assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
1021 assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001022 logbuf_assert("Input data contains module in situation when a submodule is expected.");
Radek Krejci9ed7a192018-10-31 16:23:51 +01001023 store = -1;
Radek Krejcid33273d2018-10-25 14:55:52 +02001024
Radek Krejci9ed7a192018-10-31 16:23:51 +01001025 store = 1;
Radek Krejci313d9902018-11-08 09:42:58 +01001026 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 +02001027 str = "module" SCHEMA_BEGINNING "include xxx;}";
1028 assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
1029 assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx));
1030 logbuf_assert("Included \"xxx\" submodule from \"name\" belongs-to a different module \"wrong-name\".");
Radek Krejci9ed7a192018-10-31 16:23:51 +01001031 store = -1;
Radek Krejcid33273d2018-10-25 14:55:52 +02001032
Radek Krejci313d9902018-11-08 09:42:58 +01001033 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 +02001034 TEST_GENERIC("include xxx;}", mod->includes,
Radek Krejci086c7132018-10-26 15:29:04 +02001035 assert_string_equal("xxx", mod->includes[0].name));
Radek Krejcid33273d2018-10-25 14:55:52 +02001036
Radek Krejcia042ea12018-10-13 07:52:15 +02001037 /* leaf */
1038 TEST_NODE(LYS_LEAF, "leaf test {type string;}}", "test");
1039 /* leaf-list */
1040 TEST_NODE(LYS_LEAFLIST, "leaf-list test {type string;}}", "test");
1041 /* list */
1042 TEST_NODE(LYS_LIST, "list test {key a;leaf a {type string;}}}", "test");
1043 /* notification */
1044 TEST_GENERIC("notification test;}", mod->notifs,
Radek Krejci2c4e7172018-10-19 15:56:26 +02001045 assert_string_equal("test", mod->notifs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +02001046 /* organization 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001047 TEST_GENERIC("organization \"CESNET a.l.e.\";}", mod->mod->org,
1048 assert_string_equal("CESNET a.l.e.", mod->mod->org));
Radek Krejcia042ea12018-10-13 07:52:15 +02001049 /* reference 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001050 TEST_GENERIC("reference RFC7950;}", mod->mod->ref,
1051 assert_string_equal("RFC7950", mod->mod->ref));
Radek Krejcia042ea12018-10-13 07:52:15 +02001052 /* revision */
1053 TEST_GENERIC("revision 2018-10-12;}", mod->revs,
Radek Krejcib7db73a2018-10-24 14:18:40 +02001054 assert_string_equal("2018-10-12", mod->revs[0].date));
Radek Krejcia042ea12018-10-13 07:52:15 +02001055 /* rpc */
1056 TEST_GENERIC("rpc test;}", mod->rpcs,
Radek Krejci2c4e7172018-10-19 15:56:26 +02001057 assert_string_equal("test", mod->rpcs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +02001058 /* typedef */
1059 TEST_GENERIC("typedef test{type string;}}", mod->typedefs,
Radek Krejci2c4e7172018-10-19 15:56:26 +02001060 assert_string_equal("test", mod->typedefs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +02001061 /* uses */
1062 TEST_NODE(LYS_USES, "uses test;}", "test");
1063 /* yang-version */
Radek Krejci027d5802018-11-14 16:57:28 +01001064 str = SCHEMA_BEGINNING2 "\n\tyang-version 10;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001065 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejcia042ea12018-10-13 07:52:15 +02001066 logbuf_assert("Invalid value \"10\" of \"yang-version\". Line number 3.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001067 mod = mod_renew(&ctx);
Radek Krejci027d5802018-11-14 16:57:28 +01001068 str = SCHEMA_BEGINNING2 "yang-version 1.0;yang-version 1.1;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001069 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejcia042ea12018-10-13 07:52:15 +02001070 logbuf_assert("Duplicate keyword \"yang-version\". Line number 3.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001071 mod = mod_renew(&ctx);
Radek Krejci027d5802018-11-14 16:57:28 +01001072 str = SCHEMA_BEGINNING2 "yang-version 1.0;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001073 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod));
1074 assert_int_equal(1, mod->mod->version);
Radek Krejci40544fa2019-01-11 09:38:37 +01001075 mod = mod_renew(&ctx);
Radek Krejci027d5802018-11-14 16:57:28 +01001076 str = SCHEMA_BEGINNING2 "yang-version \"1.1\";}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001077 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod));
1078 assert_int_equal(2, mod->mod->version);
Radek Krejci40544fa2019-01-11 09:38:37 +01001079 mod = mod_renew(&ctx);
1080
Michal Vaskob36053d2020-03-26 15:49:30 +01001081 struct lys_yang_parser_ctx *ctx_p = NULL;
Radek Krejci40544fa2019-01-11 09:38:37 +01001082 str = "module " SCHEMA_BEGINNING "} module q {namespace urn:q;prefixq;}";
1083 m = mod->mod;
1084 free(mod);
1085 m->parsed = NULL;
David Sedlák1b623122019-08-05 15:27:49 +02001086 assert_int_equal(LY_EVALID, yang_parse_module(&ctx_p, str, m));
1087 logbuf_assert("Trailing garbage \"module q {names...\" after module, expected end-of-input. Line number 1.");
Michal Vaskob36053d2020-03-26 15:49:30 +01001088 yang_parser_ctx_free(ctx_p);
Radek Krejci40544fa2019-01-11 09:38:37 +01001089 mod = mod_renew(&ctx);
1090
1091 str = "prefix " SCHEMA_BEGINNING "}";
1092 m = mod->mod;
1093 free(mod);
1094 m->parsed = NULL;
David Sedlák1b623122019-08-05 15:27:49 +02001095 assert_int_equal(LY_EVALID, yang_parse_module(&ctx_p, str, m));
Michal Vaskob36053d2020-03-26 15:49:30 +01001096 yang_parser_ctx_free(ctx_p);
David Sedlák1b623122019-08-05 15:27:49 +02001097 logbuf_assert("Invalid keyword \"prefix\", expected \"module\" or \"submodule\". Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001098 mod = mod_renew(&ctx);
Radek Krejci09306362018-10-15 15:26:01 +02001099
David Sedlák9fb515f2019-07-11 10:33:58 +02001100 str = "module " SCHEMA_BEGINNING "}";
1101 str = "module " SCHEMA_BEGINNING "leaf enum {type enumeration {enum seven { position 7;}}}}";
1102 m = mod->mod;
1103 free(mod);
1104 m->parsed = NULL;
David Sedlák1b623122019-08-05 15:27:49 +02001105 assert_int_equal(LY_EVALID, yang_parse_module(&ctx_p, str, m));
Michal Vaskob36053d2020-03-26 15:49:30 +01001106 yang_parser_ctx_free(ctx_p);
David Sedlák1b623122019-08-05 15:27:49 +02001107 logbuf_assert("Invalid keyword \"position\" as a child of \"enum\". Line number 1.");
David Sedlák9fb515f2019-07-11 10:33:58 +02001108 mod = mod_renew(&ctx);
1109
Radek Krejci156ccaf2018-10-15 15:49:17 +02001110 /* extensions */
1111 TEST_GENERIC("prefix:test;}", mod->exts,
Radek Krejci2c4e7172018-10-19 15:56:26 +02001112 assert_string_equal("prefix:test", mod->exts[0].name);
1113 assert_int_equal(LYEXT_SUBSTMT_SELF, mod->exts[0].insubstmt));
Radek Krejci40544fa2019-01-11 09:38:37 +01001114 mod = mod_renew(&ctx);
Radek Krejci156ccaf2018-10-15 15:49:17 +02001115
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001116 /* invalid substatement */
1117 str = SCHEMA_BEGINNING "must false;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001118 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001119 logbuf_assert("Invalid keyword \"must\" as a child of \"module\". Line number 3.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001120 mod = mod_renew(&ctx);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001121
Radek Krejci09306362018-10-15 15:26:01 +02001122 /* submodule */
Radek Krejci40544fa2019-01-11 09:38:37 +01001123 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001124
1125 /* missing mandatory substatements */
1126 str = " subname {}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001127 lydict_remove(ctx.ctx, submod->name);
1128 assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod));
1129 assert_string_equal("subname", submod->name);
Radek Krejci09306362018-10-15 15:26:01 +02001130 logbuf_assert("Missing mandatory keyword \"belongs-to\" as a child of \"submodule\". Line number 3.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001131 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001132
Radek Krejci313d9902018-11-08 09:42:58 +01001133 str = " subname {belongs-to name {prefix x;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001134 lydict_remove(ctx.ctx, submod->name);
1135 assert_int_equal(LY_SUCCESS, parse_submodule(&ctx, &str, submod));
1136 assert_string_equal("name", submod->belongsto);
1137 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001138
1139#undef SCHEMA_BEGINNING
Radek Krejci313d9902018-11-08 09:42:58 +01001140#define SCHEMA_BEGINNING " subname {belongs-to name {prefix x;}"
Radek Krejci09306362018-10-15 15:26:01 +02001141
1142 /* duplicated namespace, prefix */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001143 str = " subname {belongs-to name {prefix x;}belongs-to module1;belongs-to module2;} ...";
1144 assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod)); \
1145 logbuf_assert("Duplicate keyword \"belongs-to\". Line number 3."); \
1146 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001147
1148 /* not allowed in submodule (module-specific) */
1149 str = SCHEMA_BEGINNING "namespace \"urn:z\";}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001150 assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod));
Radek Krejci09306362018-10-15 15:26:01 +02001151 logbuf_assert("Invalid keyword \"namespace\" as a child of \"submodule\". Line number 3.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001152 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001153 str = SCHEMA_BEGINNING "prefix m;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001154 assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod));
Radek Krejci09306362018-10-15 15:26:01 +02001155 logbuf_assert("Invalid keyword \"prefix\" as a child of \"submodule\". Line number 3.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001156 submod = submod_renew(&ctx, submod);
Radek Krejcia042ea12018-10-13 07:52:15 +02001157
Radek Krejci40544fa2019-01-11 09:38:37 +01001158 str = "submodule " SCHEMA_BEGINNING "} module q {namespace urn:q;prefixq;}";
1159 lysp_submodule_free(ctx.ctx, submod);
1160 submod = NULL;
Michal Vaskob36053d2020-03-26 15:49:30 +01001161 assert_int_equal(LY_EVALID, yang_parse_submodule(&ctx_p, ctx.ctx, (struct lys_parser_ctx *)&ctx, str, &submod));
1162 yang_parser_ctx_free(ctx_p);
David Sedlák1b623122019-08-05 15:27:49 +02001163 logbuf_assert("Trailing garbage \"module q {names...\" after submodule, expected end-of-input. Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001164
1165 str = "prefix " SCHEMA_BEGINNING "}";
Michal Vaskob36053d2020-03-26 15:49:30 +01001166 assert_int_equal(LY_EVALID, yang_parse_submodule(&ctx_p, ctx.ctx, (struct lys_parser_ctx *)&ctx, str, &submod));
1167 yang_parser_ctx_free(ctx_p);
David Sedlák1b623122019-08-05 15:27:49 +02001168 logbuf_assert("Invalid keyword \"prefix\", expected \"module\" or \"submodule\". Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001169 submod = submod_renew(&ctx, submod);
1170
Radek Krejcia042ea12018-10-13 07:52:15 +02001171#undef TEST_GENERIC
1172#undef TEST_NODE
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001173#undef TEST_DUP
Radek Krejcia042ea12018-10-13 07:52:15 +02001174#undef SCHEMA_BEGINNING
1175
Radek Krejci9fcacc12018-10-11 15:59:11 +02001176 lysp_module_free(mod);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001177 lysp_submodule_free(ctx.ctx, submod);
Radek Krejci9fcacc12018-10-11 15:59:11 +02001178 ly_ctx_destroy(ctx.ctx, NULL);
Radek Krejci40544fa2019-01-11 09:38:37 +01001179
1180 *state = NULL;
Radek Krejciefd22f62018-09-27 11:47:58 +02001181}
1182
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001183static void
1184test_identity(void **state)
1185{
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001186 *state = test_identity;
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001187
Michal Vaskob36053d2020-03-26 15:49:30 +01001188 struct lys_yang_parser_ctx ctx;
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001189 struct lysp_ident *ident = NULL;
1190 const char *str;
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001191
Michal Vaskob36053d2020-03-26 15:49:30 +01001192 ctx.format = LYS_IN_YANG;
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001193 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1194 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001195 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001196 ctx.line = 1;
1197 ctx.indent = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001198 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001199
1200 /* invalid cardinality */
1201#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001202 TEST_DUP_GENERIC(" test {", MEMBER, VALUE1, VALUE2, parse_identity, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001203 &ident, "1", FREE_ARRAY(ctx.ctx, ident, lysp_ident_free); ident = NULL)
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001204
Radek Krejci38222632019-02-12 16:55:05 +01001205 TEST_DUP("description", "a", "b");
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001206 TEST_DUP("reference", "a", "b");
1207 TEST_DUP("status", "current", "obsolete");
1208
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001209 /* full content */
David Sedlák40bb13b2019-07-10 14:34:18 +02001210 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 +02001211 assert_int_equal(LY_SUCCESS, parse_identity(&ctx, &str, &ident));
1212 assert_non_null(ident);
1213 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001214 FREE_ARRAY(ctx.ctx, ident, lysp_ident_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001215 ident = NULL;
1216
1217 /* invalid substatement */
1218 str = " test {organization XXX;}";
1219 assert_int_equal(LY_EVALID, parse_identity(&ctx, &str, &ident));
1220 logbuf_assert("Invalid keyword \"organization\" as a child of \"identity\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001221 FREE_ARRAY(ctx.ctx, ident, lysp_ident_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001222 ident = NULL;
1223
1224#undef TEST_DUP
1225
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001226 *state = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001227 ly_ctx_destroy(ctx.ctx, NULL);
1228}
1229
1230static void
1231test_feature(void **state)
1232{
1233 (void) state; /* unused */
1234
Michal Vaskob36053d2020-03-26 15:49:30 +01001235 struct lys_yang_parser_ctx ctx;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001236 struct lysp_feature *features = NULL;
1237 const char *str;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001238
Michal Vaskob36053d2020-03-26 15:49:30 +01001239 ctx.format = LYS_IN_YANG;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001240 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1241 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001242 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001243 ctx.line = 1;
1244 ctx.indent = 0;
1245
1246 /* invalid cardinality */
1247#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1248 TEST_DUP_GENERIC(" test {", MEMBER, VALUE1, VALUE2, parse_feature, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001249 &features, "1", FREE_ARRAY(ctx.ctx, features, lysp_feature_free); features = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001250
1251 TEST_DUP("description", "a", "b");
1252 TEST_DUP("reference", "a", "b");
1253 TEST_DUP("status", "current", "obsolete");
1254
1255 /* full content */
1256 str = " test {description text;reference \'another text\';status current; if-feature x;if-feature y;prefix:ext;} ...";
1257 assert_int_equal(LY_SUCCESS, parse_feature(&ctx, &str, &features));
1258 assert_non_null(features);
1259 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001260 FREE_ARRAY(ctx.ctx, features, lysp_feature_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001261 features = NULL;
1262
1263 /* invalid substatement */
1264 str = " test {organization XXX;}";
1265 assert_int_equal(LY_EVALID, parse_feature(&ctx, &str, &features));
1266 logbuf_assert("Invalid keyword \"organization\" as a child of \"feature\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001267 FREE_ARRAY(ctx.ctx, features, lysp_feature_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001268 features = NULL;
1269
1270#undef TEST_DUP
1271
1272 ly_ctx_destroy(ctx.ctx, NULL);
1273}
1274
1275static void
1276test_deviation(void **state)
1277{
1278 (void) state; /* unused */
1279
Michal Vaskob36053d2020-03-26 15:49:30 +01001280 struct lys_yang_parser_ctx ctx;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001281 struct lysp_deviation *d = NULL;
1282 const char *str;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001283
Michal Vaskob36053d2020-03-26 15:49:30 +01001284 ctx.format = LYS_IN_YANG;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001285 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1286 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001287 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001288 ctx.line = 1;
1289 ctx.indent = 0;
1290
1291 /* invalid cardinality */
1292#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1293 TEST_DUP_GENERIC(" test {deviate not-supported;", MEMBER, VALUE1, VALUE2, parse_deviation, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001294 &d, "1", FREE_ARRAY(ctx.ctx, d, lysp_deviation_free); d = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001295
1296 TEST_DUP("description", "a", "b");
1297 TEST_DUP("reference", "a", "b");
1298
1299 /* full content */
1300 str = " test {deviate not-supported;description text;reference \'another text\';prefix:ext;} ...";
1301 assert_int_equal(LY_SUCCESS, parse_deviation(&ctx, &str, &d));
1302 assert_non_null(d);
1303 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001304 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001305 d = NULL;
1306
1307 /* missing mandatory substatement */
1308 str = " test {description text;}";
1309 assert_int_equal(LY_EVALID, parse_deviation(&ctx, &str, &d));
1310 logbuf_assert("Missing mandatory keyword \"deviate\" as a child of \"deviation\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001311 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001312 d = NULL;
1313
1314 /* invalid substatement */
1315 str = " test {deviate not-supported; status obsolete;}";
1316 assert_int_equal(LY_EVALID, parse_deviation(&ctx, &str, &d));
1317 logbuf_assert("Invalid keyword \"status\" as a child of \"deviation\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001318 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001319 d = NULL;
1320
1321#undef TEST_DUP
1322
1323 ly_ctx_destroy(ctx.ctx, NULL);
1324}
1325
1326static void
1327test_deviate(void **state)
1328{
1329 (void) state; /* unused */
1330
Michal Vaskob36053d2020-03-26 15:49:30 +01001331 struct lys_yang_parser_ctx ctx;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001332 struct lysp_deviate *d = NULL;
1333 const char *str;
1334
Michal Vaskob36053d2020-03-26 15:49:30 +01001335 ctx.format = LYS_IN_YANG;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001336 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1337 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001338 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001339 ctx.line = 1;
1340 ctx.indent = 0;
1341
1342 /* invalid cardinality */
1343#define TEST_DUP(TYPE, MEMBER, VALUE1, VALUE2) \
1344 TEST_DUP_GENERIC(TYPE" {", MEMBER, VALUE1, VALUE2, parse_deviate, \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001345 &d, "1", lysp_deviate_free(ctx.ctx, d); free(d); d = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001346
1347 TEST_DUP("add", "config", "true", "false");
1348 TEST_DUP("replace", "default", "int8", "uint8");
1349 TEST_DUP("add", "mandatory", "true", "false");
1350 TEST_DUP("add", "max-elements", "1", "2");
1351 TEST_DUP("add", "min-elements", "1", "2");
1352 TEST_DUP("replace", "type", "int8", "uint8");
1353 TEST_DUP("add", "units", "kilometers", "miles");
1354
1355 /* full contents */
1356 str = " not-supported {prefix:ext;} ...";
1357 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1358 assert_non_null(d);
1359 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001360 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001361 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;} ...";
1362 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1363 assert_non_null(d);
1364 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001365 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001366 str = " delete {units meters; must 1; must 2; unique x; unique y; default a; default b; prefix:ext;} ...";
1367 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1368 assert_non_null(d);
1369 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001370 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001371 str = " replace {type string; units meters; default a; config true; mandatory true; min-elements 1; max-elements 2; prefix:ext;} ...";
1372 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1373 assert_non_null(d);
1374 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001375 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001376
1377 /* invalid substatements */
1378#define TEST_NOT_SUP(DEV, STMT, VALUE) \
1379 str = " "DEV" {"STMT" "VALUE";}..."; \
1380 assert_int_equal(LY_EVALID, parse_deviate(&ctx, &str, &d)); \
1381 logbuf_assert("Deviate \""DEV"\" does not support keyword \""STMT"\". Line number 1."); \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001382 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001383
1384 TEST_NOT_SUP("not-supported", "units", "meters");
1385 TEST_NOT_SUP("not-supported", "must", "1");
1386 TEST_NOT_SUP("not-supported", "unique", "x");
1387 TEST_NOT_SUP("not-supported", "default", "a");
1388 TEST_NOT_SUP("not-supported", "config", "true");
1389 TEST_NOT_SUP("not-supported", "mandatory", "true");
1390 TEST_NOT_SUP("not-supported", "min-elements", "1");
1391 TEST_NOT_SUP("not-supported", "max-elements", "2");
1392 TEST_NOT_SUP("not-supported", "type", "string");
1393 TEST_NOT_SUP("add", "type", "string");
1394 TEST_NOT_SUP("delete", "config", "true");
1395 TEST_NOT_SUP("delete", "mandatory", "true");
1396 TEST_NOT_SUP("delete", "min-elements", "1");
1397 TEST_NOT_SUP("delete", "max-elements", "2");
1398 TEST_NOT_SUP("delete", "type", "string");
1399 TEST_NOT_SUP("replace", "must", "1");
1400 TEST_NOT_SUP("replace", "unique", "a");
1401
1402 str = " nonsence; ...";
1403 assert_int_equal(LY_EVALID, parse_deviate(&ctx, &str, &d));
1404 logbuf_assert("Invalid value \"nonsence\" of \"deviate\". Line number 1.");
1405 assert_null(d);
1406
1407#undef TEST_NOT_SUP
1408#undef TEST_DUP
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001409
1410 ly_ctx_destroy(ctx.ctx, NULL);
1411}
1412
Radek Krejci8c370832018-11-02 15:10:03 +01001413static void
1414test_container(void **state)
1415{
1416 (void) state; /* unused */
1417
Michal Vaskob36053d2020-03-26 15:49:30 +01001418 struct lys_yang_parser_ctx ctx = {0};
Radek Krejci8c370832018-11-02 15:10:03 +01001419 struct lysp_node_container *c = NULL;
1420 const char *str;
1421
Michal Vaskob36053d2020-03-26 15:49:30 +01001422 ctx.format = LYS_IN_YANG;
Radek Krejci8c370832018-11-02 15:10:03 +01001423 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1424 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001425 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci8c370832018-11-02 15:10:03 +01001426 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001427 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci8c370832018-11-02 15:10:03 +01001428
1429 /* invalid cardinality */
1430#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1431 str = "cont {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1432 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c)); \
1433 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001434 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001435
1436 TEST_DUP("config", "true", "false");
1437 TEST_DUP("description", "text1", "text2");
1438 TEST_DUP("presence", "true", "false");
1439 TEST_DUP("reference", "1", "2");
1440 TEST_DUP("status", "current", "obsolete");
1441 TEST_DUP("when", "true", "false");
1442#undef TEST_DUP
1443
1444 /* full content */
Radek Krejci313d9902018-11-08 09:42:58 +01001445 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;}"
1446 "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 +01001447 assert_int_equal(LY_SUCCESS, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1448 assert_non_null(c);
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001449 assert_int_equal(LYS_CONTAINER, c->nodetype);
1450 assert_string_equal("cont", c->name);
Radek Krejci8c370832018-11-02 15:10:03 +01001451 assert_non_null(c->actions);
1452 assert_non_null(c->child);
1453 assert_string_equal("test", c->dsc);
1454 assert_non_null(c->exts);
1455 assert_non_null(c->groupings);
1456 assert_non_null(c->iffeatures);
1457 assert_non_null(c->musts);
1458 assert_non_null(c->notifs);
1459 assert_string_equal("true", c->presence);
1460 assert_string_equal("test", c->ref);
1461 assert_non_null(c->typedefs);
1462 assert_non_null(c->when);
1463 assert_null(c->parent);
1464 assert_null(c->next);
1465 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR, c->flags);
Radek Krejci313d9902018-11-08 09:42:58 +01001466 ly_set_erase(&ctx.tpdfs_nodes, NULL);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001467 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001468
1469 /* invalid */
1470 str = " cont {augment /root;} ...";
1471 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1472 logbuf_assert("Invalid keyword \"augment\" as a child of \"container\". Line number 1.");
Radek Krejci4f28eda2018-11-12 11:46:16 +01001473 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001474 str = " cont {nonsence true;} ...";
1475 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1476 logbuf_assert("Invalid character sequence \"nonsence\", expected a keyword. Line number 1.");
Radek Krejci4f28eda2018-11-12 11:46:16 +01001477 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001478
Radek Krejcif538ce52019-03-05 10:46:14 +01001479 ctx.mod_version = 1; /* simulate YANG 1.0 */
1480 str = " cont {action x;} ...";
1481 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1482 logbuf_assert("Invalid keyword \"action\" as a child of \"container\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
1483 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
1484
Radek Krejci8c370832018-11-02 15:10:03 +01001485 ly_ctx_destroy(ctx.ctx, NULL);
1486}
1487
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001488static void
1489test_leaf(void **state)
1490{
1491 *state = test_leaf;
1492
Michal Vaskob36053d2020-03-26 15:49:30 +01001493 struct lys_yang_parser_ctx ctx = {0};
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001494 struct lysp_node_leaf *l = NULL;
1495 const char *str;
1496
Michal Vaskob36053d2020-03-26 15:49:30 +01001497 ctx.format = LYS_IN_YANG;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001498 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1499 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001500 ctx.pos_type = LY_VLOG_LINE;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001501 ctx.line = 1;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001502 //ctx.mod->version = 2; /* simulate YANG 1.1 */
1503
1504 /* invalid cardinality */
1505#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1506 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1507 assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l)); \
1508 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1509 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1510
1511 TEST_DUP("config", "true", "false");
1512 TEST_DUP("default", "x", "y");
1513 TEST_DUP("description", "text1", "text2");
1514 TEST_DUP("mandatory", "true", "false");
1515 TEST_DUP("reference", "1", "2");
1516 TEST_DUP("status", "current", "obsolete");
Radek Krejci0e5d8382018-11-28 16:37:53 +01001517 TEST_DUP("type", "int8", "uint8");
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001518 TEST_DUP("units", "text1", "text2");
1519 TEST_DUP("when", "true", "false");
1520#undef TEST_DUP
1521
1522 /* full content - without mandatory which is mutual exclusive with default */
1523 str = "l {config false;default \"xxx\";description test;if-feature f;"
1524 "must 'expr';reference test;status current;type string; units yyy;when true;m:ext;} ...";
1525 assert_int_equal(LY_SUCCESS, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
1526 assert_non_null(l);
1527 assert_int_equal(LYS_LEAF, l->nodetype);
1528 assert_string_equal("l", l->name);
1529 assert_string_equal("test", l->dsc);
1530 assert_string_equal("xxx", l->dflt);
1531 assert_string_equal("yyy", l->units);
1532 assert_string_equal("string", l->type.name);
1533 assert_non_null(l->exts);
1534 assert_non_null(l->iffeatures);
1535 assert_non_null(l->musts);
1536 assert_string_equal("test", l->ref);
1537 assert_non_null(l->when);
1538 assert_null(l->parent);
1539 assert_null(l->next);
1540 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR, l->flags);
1541 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1542
1543 /* full content - now with mandatory */
1544 str = "l {mandatory true; type string;} ...";
1545 assert_int_equal(LY_SUCCESS, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
1546 assert_non_null(l);
1547 assert_int_equal(LYS_LEAF, l->nodetype);
1548 assert_string_equal("l", l->name);
1549 assert_string_equal("string", l->type.name);
1550 assert_int_equal(LYS_MAND_TRUE, l->flags);
1551 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1552
1553 /* invalid */
1554 str = " l {mandatory true; default xx; type string;} ...";
1555 assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
Radek Krejcia9026eb2018-12-12 16:04:47 +01001556 logbuf_assert("Invalid combination of keywords \"mandatory\" and \"default\" as substatements of \"leaf\". Line number 1.");
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001557 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1558
1559 str = " l {description \"missing type\";} ...";
1560 assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
1561 logbuf_assert("Missing mandatory keyword \"type\" as a child of \"leaf\". Line number 1.");
1562 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1563
1564 *state = NULL;
1565 ly_ctx_destroy(ctx.ctx, NULL);
1566}
1567
Radek Krejci0e5d8382018-11-28 16:37:53 +01001568static void
1569test_leaflist(void **state)
1570{
1571 *state = test_leaf;
1572
Michal Vaskob36053d2020-03-26 15:49:30 +01001573 struct lys_yang_parser_ctx ctx = {0};
Radek Krejci0e5d8382018-11-28 16:37:53 +01001574 struct lysp_node_leaflist *ll = NULL;
1575 const char *str;
1576
Michal Vaskob36053d2020-03-26 15:49:30 +01001577 ctx.format = LYS_IN_YANG;
Radek Krejci0e5d8382018-11-28 16:37:53 +01001578 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1579 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001580 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci0e5d8382018-11-28 16:37:53 +01001581 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001582 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci0e5d8382018-11-28 16:37:53 +01001583
1584 /* invalid cardinality */
1585#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1586 str = "ll {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1587 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll)); \
1588 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1589 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1590
1591 TEST_DUP("config", "true", "false");
1592 TEST_DUP("description", "text1", "text2");
1593 TEST_DUP("max-elements", "10", "20");
1594 TEST_DUP("min-elements", "10", "20");
1595 TEST_DUP("ordered-by", "user", "system");
1596 TEST_DUP("reference", "1", "2");
1597 TEST_DUP("status", "current", "obsolete");
1598 TEST_DUP("type", "int8", "uint8");
1599 TEST_DUP("units", "text1", "text2");
1600 TEST_DUP("when", "true", "false");
1601#undef TEST_DUP
1602
1603 /* full content - without min-elements which is mutual exclusive with default */
1604 str = "ll {config false;default \"xxx\"; default \"yyy\";description test;if-feature f;"
1605 "max-elements 10;must 'expr';ordered-by user;reference test;"
1606 "status current;type string; units zzz;when true;m:ext;} ...";
1607 assert_int_equal(LY_SUCCESS, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1608 assert_non_null(ll);
1609 assert_int_equal(LYS_LEAFLIST, ll->nodetype);
1610 assert_string_equal("ll", ll->name);
1611 assert_string_equal("test", ll->dsc);
1612 assert_non_null(ll->dflts);
1613 assert_int_equal(2, LY_ARRAY_SIZE(ll->dflts));
1614 assert_string_equal("xxx", ll->dflts[0]);
1615 assert_string_equal("yyy", ll->dflts[1]);
1616 assert_string_equal("zzz", ll->units);
1617 assert_int_equal(10, ll->max);
1618 assert_int_equal(0, ll->min);
1619 assert_string_equal("string", ll->type.name);
1620 assert_non_null(ll->exts);
1621 assert_non_null(ll->iffeatures);
1622 assert_non_null(ll->musts);
1623 assert_string_equal("test", ll->ref);
1624 assert_non_null(ll->when);
1625 assert_null(ll->parent);
1626 assert_null(ll->next);
1627 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_USER | LYS_SET_MAX, ll->flags);
1628 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1629
1630 /* full content - now with min-elements */
1631 str = "ll {min-elements 10; type string;} ...";
1632 assert_int_equal(LY_SUCCESS, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1633 assert_non_null(ll);
1634 assert_int_equal(LYS_LEAFLIST, ll->nodetype);
1635 assert_string_equal("ll", ll->name);
1636 assert_string_equal("string", ll->type.name);
1637 assert_int_equal(0, ll->max);
1638 assert_int_equal(10, ll->min);
1639 assert_int_equal(LYS_SET_MIN, ll->flags);
1640 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1641
1642 /* invalid */
1643 str = " ll {min-elements 1; default xx; type string;} ...";
1644 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
Radek Krejcia9026eb2018-12-12 16:04:47 +01001645 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 +01001646 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1647
1648 str = " ll {description \"missing type\";} ...";
1649 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1650 logbuf_assert("Missing mandatory keyword \"type\" as a child of \"leaf-list\". Line number 1.");
1651 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1652
Radek Krejcidf6cad12018-11-28 17:10:55 +01001653 str = " ll {type string; min-elements 10; max-elements 1;} ..."; /* invalid combination of min/max */
1654 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1655 logbuf_assert("Invalid combination of min-elements and max-elements: min value 10 is bigger than the max value 1. Line number 1.");
1656 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1657
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001658 ctx.mod_version = 1; /* simulate YANG 1.0 - default statement is not allowed */
Radek Krejci0e5d8382018-11-28 16:37:53 +01001659 str = " ll {default xx; type string;} ...";
1660 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1661 logbuf_assert("Invalid keyword \"default\" as a child of \"leaf-list\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
1662 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1663
1664 *state = NULL;
1665 ly_ctx_destroy(ctx.ctx, NULL);
1666}
1667
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001668static void
1669test_list(void **state)
1670{
1671 *state = test_list;
1672
Michal Vaskob36053d2020-03-26 15:49:30 +01001673 struct lys_yang_parser_ctx ctx = {0};
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001674 struct lysp_node_list *l = NULL;
1675 const char *str;
1676
Michal Vaskob36053d2020-03-26 15:49:30 +01001677 ctx.format = LYS_IN_YANG;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001678 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1679 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001680 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001681 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001682 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001683
1684 /* invalid cardinality */
1685#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1686 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1687 assert_int_equal(LY_EVALID, parse_list(&ctx, &str, NULL, (struct lysp_node**)&l)); \
1688 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1689 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1690
1691 TEST_DUP("config", "true", "false");
1692 TEST_DUP("description", "text1", "text2");
1693 TEST_DUP("key", "one", "two");
1694 TEST_DUP("max-elements", "10", "20");
1695 TEST_DUP("min-elements", "10", "20");
1696 TEST_DUP("ordered-by", "user", "system");
1697 TEST_DUP("reference", "1", "2");
1698 TEST_DUP("status", "current", "obsolete");
1699 TEST_DUP("when", "true", "false");
1700#undef TEST_DUP
1701
1702 /* full content */
1703 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;}"
1704 "leaf-list ll {type string;} list li;max-elements 10; min-elements 1;must 'expr';notification not; ordered-by system; reference test;"
1705 "status current;typedef t {type int8;}unique xxx;unique yyy;uses g;when true;m:ext;} ...";
1706 assert_int_equal(LY_SUCCESS, parse_list(&ctx, &str, NULL, (struct lysp_node**)&l));
1707 assert_non_null(l);
1708 assert_int_equal(LYS_LIST, l->nodetype);
1709 assert_string_equal("l", l->name);
1710 assert_string_equal("test", l->dsc);
1711 assert_string_equal("l", l->key);
1712 assert_non_null(l->uniques);
1713 assert_int_equal(2, LY_ARRAY_SIZE(l->uniques));
1714 assert_string_equal("xxx", l->uniques[0]);
1715 assert_string_equal("yyy", l->uniques[1]);
1716 assert_int_equal(10, l->max);
1717 assert_int_equal(1, l->min);
1718 assert_non_null(l->exts);
1719 assert_non_null(l->iffeatures);
1720 assert_non_null(l->musts);
1721 assert_string_equal("test", l->ref);
1722 assert_non_null(l->when);
1723 assert_null(l->parent);
1724 assert_null(l->next);
1725 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM | LYS_SET_MAX | LYS_SET_MIN, l->flags);
1726 ly_set_erase(&ctx.tpdfs_nodes, NULL);
1727 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1728
Radek Krejcif538ce52019-03-05 10:46:14 +01001729 /* invalid content */
1730 ctx.mod_version = 1; /* simulate YANG 1.0 */
1731 str = "l {action x;} ...";
1732 assert_int_equal(LY_EVALID, parse_list(&ctx, &str, NULL, (struct lysp_node**)&l));
1733 logbuf_assert("Invalid keyword \"action\" as a child of \"list\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
1734 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1735
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001736 *state = NULL;
1737 ly_ctx_destroy(ctx.ctx, NULL);
1738}
1739
Radek Krejci056d0a82018-12-06 16:57:25 +01001740static void
1741test_choice(void **state)
1742{
1743 *state = test_choice;
1744
Michal Vaskob36053d2020-03-26 15:49:30 +01001745 struct lys_yang_parser_ctx ctx = {0};
Radek Krejci056d0a82018-12-06 16:57:25 +01001746 struct lysp_node_choice *ch = NULL;
1747 const char *str;
1748
Michal Vaskob36053d2020-03-26 15:49:30 +01001749 ctx.format = LYS_IN_YANG;
Radek Krejci056d0a82018-12-06 16:57:25 +01001750 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1751 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001752 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci056d0a82018-12-06 16:57:25 +01001753 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001754 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci056d0a82018-12-06 16:57:25 +01001755
1756 /* invalid cardinality */
1757#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1758 str = "ch {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1759 assert_int_equal(LY_EVALID, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch)); \
1760 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1761 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1762
1763 TEST_DUP("config", "true", "false");
1764 TEST_DUP("default", "a", "b");
1765 TEST_DUP("description", "text1", "text2");
1766 TEST_DUP("mandatory", "true", "false");
1767 TEST_DUP("reference", "1", "2");
1768 TEST_DUP("status", "current", "obsolete");
1769 TEST_DUP("when", "true", "false");
1770#undef TEST_DUP
1771
Radek Krejcia9026eb2018-12-12 16:04:47 +01001772 /* full content - without default due to a collision with mandatory */
1773 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 +01001774 "leaf-list ll {type string;} list li;mandatory true;reference test;status current;when true;m:ext;} ...";
1775 assert_int_equal(LY_SUCCESS, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch));
1776 assert_non_null(ch);
1777 assert_int_equal(LYS_CHOICE, ch->nodetype);
1778 assert_string_equal("ch", ch->name);
1779 assert_string_equal("test", ch->dsc);
1780 assert_non_null(ch->exts);
1781 assert_non_null(ch->iffeatures);
1782 assert_string_equal("test", ch->ref);
1783 assert_non_null(ch->when);
1784 assert_null(ch->parent);
1785 assert_null(ch->next);
1786 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_MAND_TRUE, ch->flags);
1787 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1788
Radek Krejcia9026eb2018-12-12 16:04:47 +01001789 /* full content - the default missing from the previous node */
1790 str = "ch {default c;case c;} ...";
1791 assert_int_equal(LY_SUCCESS, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch));
1792 assert_non_null(ch);
1793 assert_int_equal(LYS_CHOICE, ch->nodetype);
1794 assert_string_equal("ch", ch->name);
1795 assert_string_equal("c", ch->dflt);
1796 assert_int_equal(0, ch->flags);
1797 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1798
1799 /* invalid content */
1800 str = "ch {mandatory true; default c1; case c1 {leaf x{type string;}}} ...";
1801 assert_int_equal(LY_EVALID, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch));
1802 logbuf_assert("Invalid combination of keywords \"mandatory\" and \"default\" as substatements of \"choice\". Line number 1.");
1803 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1804
1805 *state = NULL;
1806 ly_ctx_destroy(ctx.ctx, NULL);
1807}
1808
1809static void
1810test_case(void **state)
1811{
1812 *state = test_case;
1813
Michal Vaskob36053d2020-03-26 15:49:30 +01001814 struct lys_yang_parser_ctx ctx = {0};
Radek Krejcia9026eb2018-12-12 16:04:47 +01001815 struct lysp_node_case *cs = NULL;
1816 const char *str;
1817
Michal Vaskob36053d2020-03-26 15:49:30 +01001818 ctx.format = LYS_IN_YANG;
Radek Krejcia9026eb2018-12-12 16:04:47 +01001819 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1820 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001821 ctx.pos_type = LY_VLOG_LINE;
Radek Krejcia9026eb2018-12-12 16:04:47 +01001822 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001823 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejcia9026eb2018-12-12 16:04:47 +01001824
1825 /* invalid cardinality */
1826#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1827 str = "cs {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1828 assert_int_equal(LY_EVALID, parse_case(&ctx, &str, NULL, (struct lysp_node**)&cs)); \
1829 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1830 lysp_node_free(ctx.ctx, (struct lysp_node*)cs); cs = NULL;
1831
1832 TEST_DUP("description", "text1", "text2");
1833 TEST_DUP("reference", "1", "2");
1834 TEST_DUP("status", "current", "obsolete");
1835 TEST_DUP("when", "true", "false");
1836#undef TEST_DUP
1837
1838 /* full content */
1839 str = "cs {anydata any;anyxml anyxml; choice ch;container c;description test;if-feature f;leaf l {type string;}"
1840 "leaf-list ll {type string;} list li;reference test;status current;uses grp;when true;m:ext;} ...";
1841 assert_int_equal(LY_SUCCESS, parse_case(&ctx, &str, NULL, (struct lysp_node**)&cs));
1842 assert_non_null(cs);
1843 assert_int_equal(LYS_CASE, cs->nodetype);
1844 assert_string_equal("cs", cs->name);
1845 assert_string_equal("test", cs->dsc);
1846 assert_non_null(cs->exts);
1847 assert_non_null(cs->iffeatures);
1848 assert_string_equal("test", cs->ref);
1849 assert_non_null(cs->when);
1850 assert_null(cs->parent);
1851 assert_null(cs->next);
1852 assert_int_equal(LYS_STATUS_CURR, cs->flags);
1853 lysp_node_free(ctx.ctx, (struct lysp_node*)cs); cs = NULL;
1854
1855 /* invalid content */
1856 str = "cs {config true} ...";
1857 assert_int_equal(LY_EVALID, parse_case(&ctx, &str, NULL, (struct lysp_node**)&cs));
1858 logbuf_assert("Invalid keyword \"config\" as a child of \"case\". Line number 1.");
1859 lysp_node_free(ctx.ctx, (struct lysp_node*)cs); cs = NULL;
1860
Radek Krejci056d0a82018-12-06 16:57:25 +01001861 *state = NULL;
1862 ly_ctx_destroy(ctx.ctx, NULL);
1863}
1864
Radek Krejci9800fb82018-12-13 14:26:23 +01001865static void
Radek Krejcid6b76452019-09-03 17:03:03 +02001866test_any(void **state, enum ly_stmt kw)
Radek Krejci9800fb82018-12-13 14:26:23 +01001867{
1868 *state = test_any;
1869
Michal Vaskob36053d2020-03-26 15:49:30 +01001870 struct lys_yang_parser_ctx ctx = {0};
Radek Krejci9800fb82018-12-13 14:26:23 +01001871 struct lysp_node_anydata *any = NULL;
1872 const char *str;
1873
Michal Vaskob36053d2020-03-26 15:49:30 +01001874 ctx.format = LYS_IN_YANG;
Radek Krejci9800fb82018-12-13 14:26:23 +01001875 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1876 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001877 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci9800fb82018-12-13 14:26:23 +01001878 ctx.line = 1;
Radek Krejcid6b76452019-09-03 17:03:03 +02001879 if (kw == LY_STMT_ANYDATA) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001880 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci9800fb82018-12-13 14:26:23 +01001881 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001882 ctx.mod_version = 1; /* simulate YANG 1.0 */
Radek Krejci9800fb82018-12-13 14:26:23 +01001883 }
1884
1885 /* invalid cardinality */
1886#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1887 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1888 assert_int_equal(LY_EVALID, parse_any(&ctx, &str, kw, NULL, (struct lysp_node**)&any)); \
1889 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1890 lysp_node_free(ctx.ctx, (struct lysp_node*)any); any = NULL;
1891
1892 TEST_DUP("config", "true", "false");
1893 TEST_DUP("description", "text1", "text2");
1894 TEST_DUP("mandatory", "true", "false");
1895 TEST_DUP("reference", "1", "2");
1896 TEST_DUP("status", "current", "obsolete");
1897 TEST_DUP("when", "true", "false");
1898#undef TEST_DUP
1899
1900 /* full content */
1901 str = "any {config true;description test;if-feature f;mandatory true;must 'expr';reference test;status current;when true;m:ext;} ...";
1902 assert_int_equal(LY_SUCCESS, parse_any(&ctx, &str, kw, NULL, (struct lysp_node**)&any));
1903 assert_non_null(any);
Radek Krejcid6b76452019-09-03 17:03:03 +02001904 assert_int_equal(kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML, any->nodetype);
Radek Krejci9800fb82018-12-13 14:26:23 +01001905 assert_string_equal("any", any->name);
1906 assert_string_equal("test", any->dsc);
1907 assert_non_null(any->exts);
1908 assert_non_null(any->iffeatures);
1909 assert_non_null(any->musts);
1910 assert_string_equal("test", any->ref);
1911 assert_non_null(any->when);
1912 assert_null(any->parent);
1913 assert_null(any->next);
1914 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR | LYS_MAND_TRUE, any->flags);
1915 lysp_node_free(ctx.ctx, (struct lysp_node*)any); any = NULL;
1916
1917 *state = NULL;
1918 ly_ctx_destroy(ctx.ctx, NULL);
1919}
1920
1921static void
1922test_anydata(void **state)
1923{
Radek Krejcid6b76452019-09-03 17:03:03 +02001924 return test_any(state, LY_STMT_ANYDATA);
Radek Krejci9800fb82018-12-13 14:26:23 +01001925}
1926
1927static void
1928test_anyxml(void **state)
1929{
Radek Krejcid6b76452019-09-03 17:03:03 +02001930 return test_any(state, LY_STMT_ANYXML);
Radek Krejci9800fb82018-12-13 14:26:23 +01001931}
1932
Radek Krejcie86bf772018-12-14 11:39:53 +01001933static void
1934test_grouping(void **state)
1935{
1936 *state = test_grouping;
1937
Michal Vaskob36053d2020-03-26 15:49:30 +01001938 struct lys_yang_parser_ctx ctx = {0};
Radek Krejcie86bf772018-12-14 11:39:53 +01001939 struct lysp_grp *grp = NULL;
1940 const char *str;
1941
Michal Vaskob36053d2020-03-26 15:49:30 +01001942 ctx.format = LYS_IN_YANG;
Radek Krejcie86bf772018-12-14 11:39:53 +01001943 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1944 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001945 ctx.pos_type = LY_VLOG_LINE;
Radek Krejcie86bf772018-12-14 11:39:53 +01001946 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001947 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejcie86bf772018-12-14 11:39:53 +01001948
1949 /* invalid cardinality */
1950#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1951 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1952 assert_int_equal(LY_EVALID, parse_grouping(&ctx, &str, NULL, &grp)); \
1953 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1954 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1955
1956 TEST_DUP("description", "text1", "text2");
1957 TEST_DUP("reference", "1", "2");
1958 TEST_DUP("status", "current", "obsolete");
1959#undef TEST_DUP
1960
1961 /* full content */
1962 str = "grp {action x;anydata any;anyxml anyxml; choice ch;container c;description test;grouping g;leaf l {type string;}"
1963 "leaf-list ll {type string;} list li;notification not;reference test;status current;typedef t {type int8;}uses g;m:ext;} ...";
1964 assert_int_equal(LY_SUCCESS, parse_grouping(&ctx, &str, NULL, &grp));
1965 assert_non_null(grp);
1966 assert_int_equal(LYS_GROUPING, grp->nodetype);
1967 assert_string_equal("grp", grp->name);
1968 assert_string_equal("test", grp->dsc);
1969 assert_non_null(grp->exts);
1970 assert_string_equal("test", grp->ref);
1971 assert_null(grp->parent);
1972 assert_int_equal( LYS_STATUS_CURR, grp->flags);
1973 ly_set_erase(&ctx.tpdfs_nodes, NULL);
1974 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1975
1976 /* invalid content */
1977 str = "grp {config true} ...";
1978 assert_int_equal(LY_EVALID, parse_grouping(&ctx, &str, NULL, &grp));
1979 logbuf_assert("Invalid keyword \"config\" as a child of \"grouping\". Line number 1.");
1980 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1981
1982 str = "grp {must 'expr'} ...";
1983 assert_int_equal(LY_EVALID, parse_grouping(&ctx, &str, NULL, &grp));
1984 logbuf_assert("Invalid keyword \"must\" as a child of \"grouping\". Line number 1.");
1985 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1986
1987 *state = NULL;
1988 ly_ctx_destroy(ctx.ctx, NULL);
1989}
1990
1991static void
Radek Krejcif538ce52019-03-05 10:46:14 +01001992test_action(void **state)
1993{
1994 *state = test_action;
1995
Michal Vaskob36053d2020-03-26 15:49:30 +01001996 struct lys_yang_parser_ctx ctx = {0};
Radek Krejcif538ce52019-03-05 10:46:14 +01001997 struct lysp_action *rpcs = NULL;
1998 struct lysp_node_container *c = NULL;
1999 const char *str;
2000
Michal Vaskob36053d2020-03-26 15:49:30 +01002001 ctx.format = LYS_IN_YANG;
Radek Krejcif538ce52019-03-05 10:46:14 +01002002 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
2003 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02002004 ctx.pos_type = LY_VLOG_LINE;
Radek Krejcif538ce52019-03-05 10:46:14 +01002005 ctx.line = 1;
2006 ctx.mod_version = 2; /* simulate YANG 1.1 */
2007
2008 /* invalid cardinality */
2009#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
2010 str = "func {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
2011 assert_int_equal(LY_EVALID, parse_action(&ctx, &str, NULL, &rpcs)); \
2012 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
2013 FREE_ARRAY(ctx.ctx, rpcs, lysp_action_free); rpcs = NULL;
2014
2015 TEST_DUP("description", "text1", "text2");
Michal Vaskob83af8a2020-01-06 09:49:22 +01002016 TEST_DUP("input", "{leaf l1 {type empty;}} description a", "{leaf l2 {type empty;}} description a");
2017 TEST_DUP("output", "{leaf l1 {type empty;}} description a", "{leaf l2 {type empty;}} description a");
Radek Krejcif538ce52019-03-05 10:46:14 +01002018 TEST_DUP("reference", "1", "2");
2019 TEST_DUP("status", "current", "obsolete");
2020#undef TEST_DUP
2021
2022 /* full content */
2023 str = "top;";
2024 assert_int_equal(LY_SUCCESS, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
2025 str = "func {description test;grouping grp;if-feature f;reference test;status current;typedef mytype {type int8;} m:ext;"
2026 "input {anydata a1; anyxml a2; choice ch; container c; grouping grp; leaf l {type int8;} leaf-list ll {type int8;}"
2027 " list li; must 1; typedef mytypei {type int8;} uses grp; m:ext;}"
2028 "output {anydata a1; anyxml a2; choice ch; container c; grouping grp; leaf l {type int8;} leaf-list ll {type int8;}"
2029 " list li; must 1; typedef mytypeo {type int8;} uses grp; m:ext;}} ...";
2030 assert_int_equal(LY_SUCCESS, parse_action(&ctx, &str, (struct lysp_node*)c, &rpcs));
2031 assert_non_null(rpcs);
2032 assert_int_equal(LYS_ACTION, rpcs->nodetype);
2033 assert_string_equal("func", rpcs->name);
2034 assert_string_equal("test", rpcs->dsc);
2035 assert_non_null(rpcs->exts);
2036 assert_non_null(rpcs->iffeatures);
2037 assert_string_equal("test", rpcs->ref);
2038 assert_non_null(rpcs->groupings);
2039 assert_non_null(rpcs->typedefs);
2040 assert_int_equal(LYS_STATUS_CURR, rpcs->flags);
2041 /* input */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002042 assert_int_equal(rpcs->input.nodetype, LYS_INPUT);
Radek Krejcif538ce52019-03-05 10:46:14 +01002043 assert_non_null(rpcs->input.groupings);
2044 assert_non_null(rpcs->input.exts);
2045 assert_non_null(rpcs->input.musts);
2046 assert_non_null(rpcs->input.typedefs);
2047 assert_non_null(rpcs->input.data);
2048 /* output */
Radek Krejcid3ca0632019-04-16 16:54:54 +02002049 assert_int_equal(rpcs->output.nodetype, LYS_OUTPUT);
Radek Krejcif538ce52019-03-05 10:46:14 +01002050 assert_non_null(rpcs->output.groupings);
2051 assert_non_null(rpcs->output.exts);
2052 assert_non_null(rpcs->output.musts);
2053 assert_non_null(rpcs->output.typedefs);
2054 assert_non_null(rpcs->output.data);
2055
2056 ly_set_erase(&ctx.tpdfs_nodes, NULL);
2057 FREE_ARRAY(ctx.ctx, rpcs, lysp_action_free); rpcs = NULL;
2058
2059 /* invalid content */
2060 str = "func {config true} ...";
2061 assert_int_equal(LY_EVALID, parse_action(&ctx, &str, NULL, &rpcs));
2062 logbuf_assert("Invalid keyword \"config\" as a child of \"rpc\". Line number 1.");
2063 FREE_ARRAY(ctx.ctx, rpcs, lysp_action_free); rpcs = NULL;
2064
2065 *state = NULL;
2066 lysp_node_free(ctx.ctx, (struct lysp_node*)c);
2067 ly_ctx_destroy(ctx.ctx, NULL);
2068}
2069
2070static void
Radek Krejcifc11bd72019-04-11 16:00:05 +02002071test_notification(void **state)
2072{
2073 *state = test_notification;
2074
Michal Vaskob36053d2020-03-26 15:49:30 +01002075 struct lys_yang_parser_ctx ctx = {0};
Radek Krejcifc11bd72019-04-11 16:00:05 +02002076 struct lysp_notif *notifs = NULL;
2077 struct lysp_node_container *c = NULL;
2078 const char *str;
2079
Michal Vaskob36053d2020-03-26 15:49:30 +01002080 ctx.format = LYS_IN_YANG;
Radek Krejcifc11bd72019-04-11 16:00:05 +02002081 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
2082 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02002083 ctx.pos_type = LY_VLOG_LINE;
Radek Krejcifc11bd72019-04-11 16:00:05 +02002084 ctx.line = 1;
2085 ctx.mod_version = 2; /* simulate YANG 1.1 */
2086
2087 /* invalid cardinality */
2088#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
2089 str = "func {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
2090 assert_int_equal(LY_EVALID, parse_notif(&ctx, &str, NULL, &notifs)); \
2091 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
2092 FREE_ARRAY(ctx.ctx, notifs, lysp_notif_free); notifs = NULL;
2093
2094 TEST_DUP("description", "text1", "text2");
2095 TEST_DUP("reference", "1", "2");
2096 TEST_DUP("status", "current", "obsolete");
2097#undef TEST_DUP
2098
2099 /* full content */
2100 str = "top;";
2101 assert_int_equal(LY_SUCCESS, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
2102 str = "ntf {anydata a1; anyxml a2; choice ch; container c; description test; grouping grp; if-feature f; leaf l {type int8;}"
2103 "leaf-list ll {type int8;} list li; must 1; reference test; status current; typedef mytype {type int8;} uses grp; m:ext;}";
2104 assert_int_equal(LY_SUCCESS, parse_notif(&ctx, &str, (struct lysp_node*)c, &notifs));
2105 assert_non_null(notifs);
2106 assert_int_equal(LYS_NOTIF, notifs->nodetype);
2107 assert_string_equal("ntf", notifs->name);
2108 assert_string_equal("test", notifs->dsc);
2109 assert_non_null(notifs->exts);
2110 assert_non_null(notifs->iffeatures);
2111 assert_string_equal("test", notifs->ref);
2112 assert_non_null(notifs->groupings);
2113 assert_non_null(notifs->typedefs);
2114 assert_non_null(notifs->musts);
2115 assert_non_null(notifs->data);
2116 assert_int_equal(LYS_STATUS_CURR, notifs->flags);
2117
2118 ly_set_erase(&ctx.tpdfs_nodes, NULL);
2119 FREE_ARRAY(ctx.ctx, notifs, lysp_notif_free); notifs = NULL;
2120
2121 /* invalid content */
2122 str = "ntf {config true} ...";
2123 assert_int_equal(LY_EVALID, parse_notif(&ctx, &str, NULL, &notifs));
2124 logbuf_assert("Invalid keyword \"config\" as a child of \"notification\". Line number 1.");
2125 FREE_ARRAY(ctx.ctx, notifs, lysp_notif_free); notifs = NULL;
2126
2127 *state = NULL;
2128 lysp_node_free(ctx.ctx, (struct lysp_node*)c);
2129 ly_ctx_destroy(ctx.ctx, NULL);
2130}
2131
2132static void
Radek Krejcie86bf772018-12-14 11:39:53 +01002133test_uses(void **state)
2134{
2135 *state = test_uses;
2136
Michal Vaskob36053d2020-03-26 15:49:30 +01002137 struct lys_yang_parser_ctx ctx = {0};
Radek Krejcie86bf772018-12-14 11:39:53 +01002138 struct lysp_node_uses *u = NULL;
2139 const char *str;
2140
Michal Vaskob36053d2020-03-26 15:49:30 +01002141 ctx.format = LYS_IN_YANG;
Radek Krejcie86bf772018-12-14 11:39:53 +01002142 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
2143 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02002144 ctx.pos_type = LY_VLOG_LINE;
Radek Krejcie86bf772018-12-14 11:39:53 +01002145 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002146 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejcie86bf772018-12-14 11:39:53 +01002147
2148 /* invalid cardinality */
2149#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
2150 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
2151 assert_int_equal(LY_EVALID, parse_uses(&ctx, &str, NULL, (struct lysp_node**)&u)); \
2152 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
2153 lysp_node_free(ctx.ctx, (struct lysp_node*)u); u = NULL;
2154
2155 TEST_DUP("description", "text1", "text2");
2156 TEST_DUP("reference", "1", "2");
2157 TEST_DUP("status", "current", "obsolete");
2158 TEST_DUP("when", "true", "false");
2159#undef TEST_DUP
2160
2161 /* full content */
2162 str = "grpref {augment some/node;description test;if-feature f;reference test;refine some/other/node;status current;when true;m:ext;} ...";
2163 assert_int_equal(LY_SUCCESS, parse_uses(&ctx, &str, NULL, (struct lysp_node**)&u));
2164 assert_non_null(u);
2165 assert_int_equal(LYS_USES, u->nodetype);
2166 assert_string_equal("grpref", u->name);
2167 assert_string_equal("test", u->dsc);
2168 assert_non_null(u->exts);
2169 assert_non_null(u->iffeatures);
2170 assert_string_equal("test", u->ref);
2171 assert_non_null(u->augments);
2172 assert_non_null(u->refines);
2173 assert_non_null(u->when);
2174 assert_null(u->parent);
2175 assert_null(u->next);
2176 assert_int_equal(LYS_STATUS_CURR, u->flags);
2177 lysp_node_free(ctx.ctx, (struct lysp_node*)u); u = NULL;
2178
2179 *state = NULL;
2180 ly_ctx_destroy(ctx.ctx, NULL);
2181}
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002182
2183
2184static void
2185test_augment(void **state)
2186{
2187 *state = test_augment;
2188
Michal Vaskob36053d2020-03-26 15:49:30 +01002189 struct lys_yang_parser_ctx ctx = {0};
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002190 struct lysp_augment *a = NULL;
2191 const char *str;
2192
Michal Vaskob36053d2020-03-26 15:49:30 +01002193 ctx.format = LYS_IN_YANG;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002194 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
2195 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02002196 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002197 ctx.line = 1;
Radek Krejcib4adbf12019-02-25 13:35:22 +01002198 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002199
2200 /* invalid cardinality */
2201#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
2202 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
2203 assert_int_equal(LY_EVALID, parse_augment(&ctx, &str, NULL, &a)); \
2204 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
Radek Krejcib4adbf12019-02-25 13:35:22 +01002205 FREE_ARRAY(ctx.ctx, a, lysp_augment_free); a = NULL;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002206
2207 TEST_DUP("description", "text1", "text2");
2208 TEST_DUP("reference", "1", "2");
2209 TEST_DUP("status", "current", "obsolete");
2210 TEST_DUP("when", "true", "false");
2211#undef TEST_DUP
2212
2213 /* full content */
2214 str = "/target/nodeid {action x; anydata any;anyxml anyxml; case cs; choice ch;container c;description test;if-feature f;leaf l {type string;}"
2215 "leaf-list ll {type string;} list li;notification not;reference test;status current;uses g;when true;m:ext;} ...";
2216 assert_int_equal(LY_SUCCESS, parse_augment(&ctx, &str, NULL, &a));
2217 assert_non_null(a);
2218 assert_int_equal(LYS_AUGMENT, a->nodetype);
2219 assert_string_equal("/target/nodeid", a->nodeid);
2220 assert_string_equal("test", a->dsc);
2221 assert_non_null(a->exts);
2222 assert_non_null(a->iffeatures);
2223 assert_string_equal("test", a->ref);
2224 assert_non_null(a->when);
2225 assert_null(a->parent);
2226 assert_int_equal(LYS_STATUS_CURR, a->flags);
Radek Krejcib4adbf12019-02-25 13:35:22 +01002227 FREE_ARRAY(ctx.ctx, a, lysp_augment_free); a = NULL;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002228
2229 *state = NULL;
2230 ly_ctx_destroy(ctx.ctx, NULL);
2231}
2232
Radek Krejcif09e4e82019-06-14 15:08:11 +02002233static void
2234test_when(void **state)
2235{
2236 *state = test_when;
2237
Michal Vaskob36053d2020-03-26 15:49:30 +01002238 struct lys_yang_parser_ctx ctx = {0};
Radek Krejcif09e4e82019-06-14 15:08:11 +02002239 struct lysp_when *w = NULL;
2240 const char *str;
2241
Michal Vaskob36053d2020-03-26 15:49:30 +01002242 ctx.format = LYS_IN_YANG;
Radek Krejcif09e4e82019-06-14 15:08:11 +02002243 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
2244 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02002245 ctx.pos_type = LY_VLOG_LINE;
Radek Krejcif09e4e82019-06-14 15:08:11 +02002246 ctx.line = 1;
2247 ctx.mod_version = 2; /* simulate YANG 1.1 */
2248
2249 /* invalid cardinality */
2250#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
2251 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
2252 assert_int_equal(LY_EVALID, parse_when(&ctx, &str, &w)); \
2253 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
2254 FREE_MEMBER(ctx.ctx, w, lysp_when_free); w = NULL;
2255
2256 TEST_DUP("description", "text1", "text2");
2257 TEST_DUP("reference", "1", "2");
2258#undef TEST_DUP
2259
2260 /* full content */
2261 str = "expression {description test;reference test;m:ext;} ...";
2262 assert_int_equal(LY_SUCCESS, parse_when(&ctx, &str, &w));
2263 assert_non_null(w);
2264 assert_string_equal("expression", w->cond);
2265 assert_string_equal("test", w->dsc);
2266 assert_string_equal("test", w->ref);
2267 assert_non_null(w->exts);
2268 FREE_MEMBER(ctx.ctx, w, lysp_when_free); w = NULL;
2269
2270 /* empty condition */
2271 str = "\"\";";
2272 assert_int_equal(LY_SUCCESS, parse_when(&ctx, &str, &w));
2273 logbuf_assert("Empty argument of when statement does not make sense.");
2274 assert_non_null(w);
2275 assert_string_equal("", w->cond);
2276 FREE_MEMBER(ctx.ctx, w, lysp_when_free); w = NULL;
2277
2278 *state = NULL;
2279 ly_ctx_destroy(ctx.ctx, NULL);
2280}
2281
David Sedlákd6ce6d72019-07-16 17:30:18 +02002282static void
2283test_value(void **state)
2284{
2285 *state = test_value;
Michal Vaskob36053d2020-03-26 15:49:30 +01002286 struct lys_yang_parser_ctx ctx;
David Sedlákd6ce6d72019-07-16 17:30:18 +02002287
Michal Vaskob36053d2020-03-26 15:49:30 +01002288 ctx.format = LYS_IN_YANG;
David Sedlákd6ce6d72019-07-16 17:30:18 +02002289 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
2290 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02002291 ctx.pos_type = LY_VLOG_LINE;
David Sedlákd6ce6d72019-07-16 17:30:18 +02002292 ctx.line = 1;
2293 ctx.indent = 0;
2294 int64_t val = 0;
2295 uint16_t flags = 0;
2296
2297 const char *data = "-0;";
Radek Krejcid6b76452019-09-03 17:03:03 +02002298 assert_int_equal(parse_type_enum_value_pos(&ctx, &data, LY_STMT_VALUE, &val, &flags, NULL), LY_SUCCESS);
David Sedlákd6ce6d72019-07-16 17:30:18 +02002299 assert_int_equal(val, 0);
2300
2301 data = "-0;";
2302 flags = 0;
Radek Krejcid6b76452019-09-03 17:03:03 +02002303 assert_int_equal(parse_type_enum_value_pos(&ctx, &data, LY_STMT_POSITION, &val, &flags, NULL), LY_EVALID);
David Sedlákd6ce6d72019-07-16 17:30:18 +02002304 logbuf_assert("Invalid value \"-0\" of \"position\". Line number 1.");
2305
David Sedlák1af868e2019-07-17 17:03:14 +02002306 *state = NULL;
David Sedlákd6ce6d72019-07-16 17:30:18 +02002307 ly_ctx_destroy(ctx.ctx, NULL);
2308}
2309
Radek Krejci80dd33e2018-09-26 15:57:18 +02002310int main(void)
2311{
2312 const struct CMUnitTest tests[] = {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002313 cmocka_unit_test_setup(test_helpers, logger_setup),
Radek Krejci80dd33e2018-09-26 15:57:18 +02002314 cmocka_unit_test_setup(test_comments, logger_setup),
Radek Krejciefd22f62018-09-27 11:47:58 +02002315 cmocka_unit_test_setup(test_arg, logger_setup),
Radek Krejcidcc7b322018-10-11 14:24:02 +02002316 cmocka_unit_test_setup(test_stmts, logger_setup),
Radek Krejci05b13982018-11-28 16:22:07 +01002317 cmocka_unit_test_setup_teardown(test_minmax, logger_setup, logger_teardown),
Radek Krejci40544fa2019-01-11 09:38:37 +01002318 cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown),
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002319 cmocka_unit_test_setup_teardown(test_identity, logger_setup, logger_teardown),
Radek Krejci2c02f3e2018-10-16 10:54:38 +02002320 cmocka_unit_test_setup(test_feature, logger_setup),
2321 cmocka_unit_test_setup(test_deviation, logger_setup),
2322 cmocka_unit_test_setup(test_deviate, logger_setup),
Radek Krejci8c370832018-11-02 15:10:03 +01002323 cmocka_unit_test_setup(test_container, logger_setup),
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002324 cmocka_unit_test_setup_teardown(test_leaf, logger_setup, logger_teardown),
Radek Krejci0e5d8382018-11-28 16:37:53 +01002325 cmocka_unit_test_setup_teardown(test_leaflist, logger_setup, logger_teardown),
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002326 cmocka_unit_test_setup_teardown(test_list, logger_setup, logger_teardown),
Radek Krejci056d0a82018-12-06 16:57:25 +01002327 cmocka_unit_test_setup_teardown(test_choice, logger_setup, logger_teardown),
Radek Krejcia9026eb2018-12-12 16:04:47 +01002328 cmocka_unit_test_setup_teardown(test_case, logger_setup, logger_teardown),
Radek Krejci9800fb82018-12-13 14:26:23 +01002329 cmocka_unit_test_setup_teardown(test_anydata, logger_setup, logger_teardown),
2330 cmocka_unit_test_setup_teardown(test_anyxml, logger_setup, logger_teardown),
Radek Krejcif538ce52019-03-05 10:46:14 +01002331 cmocka_unit_test_setup_teardown(test_action, logger_setup, logger_teardown),
Radek Krejcifc11bd72019-04-11 16:00:05 +02002332 cmocka_unit_test_setup_teardown(test_notification, logger_setup, logger_teardown),
Radek Krejcie86bf772018-12-14 11:39:53 +01002333 cmocka_unit_test_setup_teardown(test_grouping, logger_setup, logger_teardown),
2334 cmocka_unit_test_setup_teardown(test_uses, logger_setup, logger_teardown),
Radek Krejcib4adbf12019-02-25 13:35:22 +01002335 cmocka_unit_test_setup_teardown(test_augment, logger_setup, logger_teardown),
Radek Krejcif09e4e82019-06-14 15:08:11 +02002336 cmocka_unit_test_setup_teardown(test_when, logger_setup, logger_teardown),
David Sedlákd6ce6d72019-07-16 17:30:18 +02002337 cmocka_unit_test_setup_teardown(test_value, logger_setup, logger_teardown),
Radek Krejci80dd33e2018-09-26 15:57:18 +02002338 };
2339
2340 return cmocka_run_group_tests(tests, NULL, NULL);
2341}