blob: ea24966090b2204fe2119b020830c48ae5bf5c95 [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 Krejci70593c12020-06-13 20:48:09 +020023#include "common.h"
24#include "parser_internal.h"
25#include "tree_schema.h"
26#include "tree_schema_internal.h"
Radek Krejci2d7a47b2019-05-16 13:34:10 +020027
28/* originally static functions from tree_schema_free.c and parser_yang.c */
29void lysp_ext_instance_free(struct ly_ctx *ctx, struct lysp_ext_instance *ext);
Radek Krejci2d7a47b2019-05-16 13:34:10 +020030void lysp_deviation_free(struct ly_ctx *ctx, struct lysp_deviation *dev);
31void lysp_grp_free(struct ly_ctx *ctx, struct lysp_grp *grp);
32void lysp_action_free(struct ly_ctx *ctx, struct lysp_action *action);
33void lysp_notif_free(struct ly_ctx *ctx, struct lysp_notif *notif);
34void lysp_augment_free(struct ly_ctx *ctx, struct lysp_augment *augment);
35void lysp_deviate_free(struct ly_ctx *ctx, struct lysp_deviate *d);
36void lysp_node_free(struct ly_ctx *ctx, struct lysp_node *node);
Radek Krejcif09e4e82019-06-14 15:08:11 +020037void lysp_when_free(struct ly_ctx *ctx, struct lysp_when *when);
Radek Krejci2d7a47b2019-05-16 13:34:10 +020038
Michal Vasko63f3d842020-07-08 10:10:14 +020039LY_ERR buf_add_char(struct ly_ctx *ctx, struct ly_in *in, size_t len, char **buf, size_t *buf_len, size_t *buf_used);
40LY_ERR buf_store_char(struct lys_yang_parser_ctx *ctx, struct ly_in *in, enum yang_arg arg, char **word_p,
David Sedlák40bb13b2019-07-10 14:34:18 +020041 size_t *word_len, char **word_b, size_t *buf_len, int need_buf, int *prefix);
Michal Vasko63f3d842020-07-08 10:10:14 +020042LY_ERR get_keyword(struct lys_yang_parser_ctx *ctx, struct ly_in *in, enum ly_stmt *kw, char **word_p, size_t *word_len);
43LY_ERR get_argument(struct lys_yang_parser_ctx *ctx, struct ly_in *in, enum yang_arg arg,
Radek Krejci2d7a47b2019-05-16 13:34:10 +020044 uint16_t *flags, char **word_p, char **word_b, size_t *word_len);
Michal Vasko63f3d842020-07-08 10:10:14 +020045LY_ERR skip_comment(struct lys_yang_parser_ctx *ctx, struct ly_in *in, int comment);
Radek Krejci2d7a47b2019-05-16 13:34:10 +020046
Michal Vasko63f3d842020-07-08 10:10:14 +020047LY_ERR parse_action(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_action **actions);
48LY_ERR parse_any(struct lys_yang_parser_ctx *ctx, struct ly_in *in, enum ly_stmt kw, struct lysp_node *parent, struct lysp_node **siblings);
49LY_ERR parse_augment(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_augment **augments);
50LY_ERR parse_case(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_node **siblings);
51LY_ERR parse_container(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_node **siblings);
52LY_ERR parse_deviate(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_deviate **deviates);
53LY_ERR parse_deviation(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_deviation **deviations);
54LY_ERR parse_grouping(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_grp **groupings);
55LY_ERR parse_choice(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_node **siblings);
56LY_ERR parse_leaf(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_node **siblings);
57LY_ERR parse_leaflist(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_node **siblings);
58LY_ERR parse_list(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_node **siblings);
59LY_ERR parse_maxelements(struct lys_yang_parser_ctx *ctx, struct ly_in *in, uint32_t *max, uint16_t *flags, struct lysp_ext_instance **exts);
60LY_ERR parse_minelements(struct lys_yang_parser_ctx *ctx, struct ly_in *in, uint32_t *min, uint16_t *flags, struct lysp_ext_instance **exts);
61LY_ERR parse_module(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_module *mod);
62LY_ERR parse_notif(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_notif **notifs);
63LY_ERR parse_submodule(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_submodule *submod);
64LY_ERR parse_uses(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_node *parent, struct lysp_node **siblings);
65LY_ERR parse_when(struct lys_yang_parser_ctx *ctx, struct ly_in *in, struct lysp_when **when_p);
66LY_ERR parse_type_enum_value_pos(struct lys_yang_parser_ctx *ctx, struct ly_in *in, enum ly_stmt val_kw, int64_t *value, uint16_t *flags, struct lysp_ext_instance **exts);
Radek Krejci80dd33e2018-09-26 15:57:18 +020067
68#define BUFSIZE 1024
69char logbuf[BUFSIZE] = {0};
Radek Krejci9ed7a192018-10-31 16:23:51 +010070int store = -1; /* negative for infinite logging, positive for limited logging */
Radek Krejci80dd33e2018-09-26 15:57:18 +020071
72/* set to 0 to printing error messages to stderr instead of checking them in code */
Radek Krejci70853c52018-10-15 14:46:16 +020073#define ENABLE_LOGGER_CHECKING 1
Radek Krejci80dd33e2018-09-26 15:57:18 +020074
Radek Krejcid5f2b5f2018-10-11 10:54:36 +020075#if ENABLE_LOGGER_CHECKING
Radek Krejci80dd33e2018-09-26 15:57:18 +020076static void
77logger(LY_LOG_LEVEL level, const char *msg, const char *path)
78{
79 (void) level; /* unused */
Radek Krejci9ed7a192018-10-31 16:23:51 +010080 if (store) {
81 if (path && path[0]) {
82 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
83 } else {
84 strncpy(logbuf, msg, BUFSIZE - 1);
85 }
86 if (store > 0) {
87 --store;
88 }
Radek Krejci80dd33e2018-09-26 15:57:18 +020089 }
90}
Radek Krejcid5f2b5f2018-10-11 10:54:36 +020091#endif
Radek Krejci80dd33e2018-09-26 15:57:18 +020092
93static int
94logger_setup(void **state)
95{
96 (void) state; /* unused */
97#if ENABLE_LOGGER_CHECKING
98 ly_set_log_clb(logger, 1);
99#endif
100 return 0;
101}
102
Radek Krejcib1a5dcc2018-11-26 14:50:05 +0100103static int
104logger_teardown(void **state)
105{
106 (void) state; /* unused */
107#if ENABLE_LOGGER_CHECKING
108 if (*state) {
109 fprintf(stderr, "%s\n", logbuf);
110 }
111#endif
112 return 0;
113}
114
Radek Krejci80dd33e2018-09-26 15:57:18 +0200115void
116logbuf_clean(void)
117{
118 logbuf[0] = '\0';
119}
120
121#if ENABLE_LOGGER_CHECKING
122# define logbuf_assert(str) assert_string_equal(logbuf, str)
123#else
124# define logbuf_assert(str)
125#endif
126
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200127#define TEST_DUP_GENERIC(PREFIX, MEMBER, VALUE1, VALUE2, FUNC, RESULT, LINE, CLEANUP) \
Michal Vasko63f3d842020-07-08 10:10:14 +0200128 in.current = PREFIX MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
129 assert_int_equal(LY_EVALID, FUNC(&ctx, &in, RESULT)); \
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200130 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number "LINE"."); \
131 CLEANUP
132
Radek Krejci44ceedc2018-10-02 15:54:31 +0200133static void
134test_helpers(void **state)
135{
136 (void) state; /* unused */
137
Michal Vasko63f3d842020-07-08 10:10:14 +0200138 struct ly_in in = {0};
Radek Krejci404251e2018-10-09 12:06:44 +0200139 char *buf, *p;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200140 size_t len, size;
Michal Vaskob36053d2020-03-26 15:49:30 +0100141 struct lys_yang_parser_ctx ctx;
142 ctx.format = LYS_IN_YANG;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200143 ctx.ctx = NULL;
Radek Krejci99435242019-09-05 16:19:15 +0200144 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200145 ctx.line = 1;
David Sedlák40bb13b2019-07-10 14:34:18 +0200146 int prefix = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200147
148 /* storing into buffer */
Michal Vasko63f3d842020-07-08 10:10:14 +0200149 in.current = "abcd";
Radek Krejci44ceedc2018-10-02 15:54:31 +0200150 buf = NULL;
151 size = len = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200152 assert_int_equal(LY_SUCCESS, buf_add_char(NULL, &in, 2, &buf, &size, &len));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200153 assert_int_not_equal(0, size);
154 assert_int_equal(2, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200155 assert_string_equal("cd", in.current);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200156 assert_false(strncmp("ab", buf, 2));
157 free(buf);
Radek Krejci404251e2018-10-09 12:06:44 +0200158 buf = NULL;
159
160 /* invalid first characters */
161 len = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200162 in.current = "2invalid";
163 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &in, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
164 in.current = ".invalid";
165 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &in, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
166 in.current = "-invalid";
167 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &in, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
Radek Krejci404251e2018-10-09 12:06:44 +0200168 /* invalid following characters */
169 len = 3; /* number of characters read before the str content */
Michal Vasko63f3d842020-07-08 10:10:14 +0200170 in.current = "!";
171 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &in, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
172 in.current = ":";
173 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &in, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
Radek Krejci404251e2018-10-09 12:06:44 +0200174 /* valid colon for prefixed identifiers */
175 len = size = 0;
176 p = NULL;
David Sedlák40bb13b2019-07-10 14:34:18 +0200177 prefix = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200178 in.current = "x:id";
179 assert_int_equal(LY_SUCCESS, buf_store_char(&ctx, &in, Y_PREF_IDENTIF_ARG, &p, &len, &buf, &size, 0, &prefix));
Radek Krejci404251e2018-10-09 12:06:44 +0200180 assert_int_equal(1, len);
181 assert_null(buf);
Michal Vasko63f3d842020-07-08 10:10:14 +0200182 assert_string_equal(":id", in.current);
Radek Krejci404251e2018-10-09 12:06:44 +0200183 assert_int_equal('x', p[len - 1]);
Michal Vasko63f3d842020-07-08 10:10:14 +0200184 assert_int_equal(LY_SUCCESS, buf_store_char(&ctx, &in, Y_PREF_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
Radek Krejci404251e2018-10-09 12:06:44 +0200185 assert_int_equal(2, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200186 assert_string_equal("id", in.current);
Radek Krejci404251e2018-10-09 12:06:44 +0200187 assert_int_equal(':', p[len - 1]);
188 free(buf);
David Sedlák40bb13b2019-07-10 14:34:18 +0200189 prefix = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200190
191 /* checking identifiers */
Michal Vaskob36053d2020-03-26 15:49:30 +0100192 assert_int_equal(LY_EVALID, lysp_check_identifierchar((struct lys_parser_ctx *)&ctx, ':', 0, NULL));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200193 logbuf_assert("Invalid identifier character ':'. Line number 1.");
Michal Vaskob36053d2020-03-26 15:49:30 +0100194 assert_int_equal(LY_EVALID, lysp_check_identifierchar((struct lys_parser_ctx *)&ctx, '#', 1, NULL));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200195 logbuf_assert("Invalid identifier first character '#'. Line number 1.");
196
Michal Vaskob36053d2020-03-26 15:49:30 +0100197 assert_int_equal(LY_SUCCESS, lysp_check_identifierchar((struct lys_parser_ctx *)&ctx, 'a', 1, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200198 assert_int_equal(0, prefix);
Michal Vaskob36053d2020-03-26 15:49:30 +0100199 assert_int_equal(LY_SUCCESS, lysp_check_identifierchar((struct lys_parser_ctx *)&ctx, ':', 0, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200200 assert_int_equal(1, prefix);
Michal Vaskob36053d2020-03-26 15:49:30 +0100201 assert_int_equal(LY_EVALID, lysp_check_identifierchar((struct lys_parser_ctx *)&ctx, ':', 0, &prefix));
Radek Krejcidcc7b322018-10-11 14:24:02 +0200202 assert_int_equal(1, prefix);
Michal Vaskob36053d2020-03-26 15:49:30 +0100203 assert_int_equal(LY_SUCCESS, lysp_check_identifierchar((struct lys_parser_ctx *)&ctx, 'b', 0, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200204 assert_int_equal(2, prefix);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200205 /* second colon is invalid */
Michal Vaskob36053d2020-03-26 15:49:30 +0100206 assert_int_equal(LY_EVALID, lysp_check_identifierchar((struct lys_parser_ctx *)&ctx, ':', 0, &prefix));
Radek Krejcidcc7b322018-10-11 14:24:02 +0200207 logbuf_assert("Invalid identifier character ':'. Line number 1.");
Radek Krejci44ceedc2018-10-02 15:54:31 +0200208}
Radek Krejci80dd33e2018-09-26 15:57:18 +0200209
210static void
211test_comments(void **state)
212{
213 (void) state; /* unused */
214
Michal Vasko63f3d842020-07-08 10:10:14 +0200215 struct ly_in in = {0};
Michal Vaskob36053d2020-03-26 15:49:30 +0100216 struct lys_yang_parser_ctx ctx;
Radek Krejciefd22f62018-09-27 11:47:58 +0200217 char *word, *buf;
218 size_t len;
Radek Krejci80dd33e2018-09-26 15:57:18 +0200219
Michal Vaskob36053d2020-03-26 15:49:30 +0100220 ctx.format = LYS_IN_YANG;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200221 ctx.ctx = NULL;
Radek Krejci99435242019-09-05 16:19:15 +0200222 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200223 ctx.line = 1;
224
Michal Vasko63f3d842020-07-08 10:10:14 +0200225 in.current = " // this is a text of / one * line */ comment\nargument;";
226 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &in, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200227 assert_string_equal("argument;", word);
Radek Krejciefd22f62018-09-27 11:47:58 +0200228 assert_null(buf);
229 assert_int_equal(8, len);
Radek Krejci80dd33e2018-09-26 15:57:18 +0200230
Michal Vasko63f3d842020-07-08 10:10:14 +0200231 in.current = "/* this is a \n * text // of / block * comment */\"arg\" + \"ume\" \n + \n \"nt\";";
232 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &in, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200233 assert_string_equal("argument", word);
234 assert_ptr_equal(buf, word);
235 assert_int_equal(8, len);
236 free(word);
Radek Krejci80dd33e2018-09-26 15:57:18 +0200237
Michal Vasko63f3d842020-07-08 10:10:14 +0200238 in.current = " this is one line comment on last line";
239 assert_int_equal(LY_SUCCESS, skip_comment(&ctx, &in, 1));
240 assert_true(in.current[0] == '\0');
Radek Krejci80dd33e2018-09-26 15:57:18 +0200241
Michal Vasko63f3d842020-07-08 10:10:14 +0200242 in.current = " this is a not terminated comment x";
243 assert_int_equal(LY_EVALID, skip_comment(&ctx, &in, 2));
Radek Krejci0a1d0d42019-05-16 15:14:51 +0200244 logbuf_assert("Unexpected end-of-input, non-terminated comment. Line number 5.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200245 assert_true(in.current[0] == '\0');
Radek Krejci80dd33e2018-09-26 15:57:18 +0200246}
247
Radek Krejciefd22f62018-09-27 11:47:58 +0200248static void
249test_arg(void **state)
250{
251 (void) state; /* unused */
252
Michal Vaskob36053d2020-03-26 15:49:30 +0100253 struct lys_yang_parser_ctx ctx;
Michal Vasko63f3d842020-07-08 10:10:14 +0200254 struct ly_in in = {0};
Radek Krejciefd22f62018-09-27 11:47:58 +0200255 char *word, *buf;
256 size_t len;
257
Michal Vaskob36053d2020-03-26 15:49:30 +0100258 ctx.format = LYS_IN_YANG;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200259 ctx.ctx = NULL;
Radek Krejci99435242019-09-05 16:19:15 +0200260 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200261 ctx.line = 1;
262
Radek Krejciefd22f62018-09-27 11:47:58 +0200263 /* missing argument */
Michal Vasko63f3d842020-07-08 10:10:14 +0200264 in.current = ";";
265 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &in, Y_MAYBE_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200266 assert_null(word);
267
Michal Vasko63f3d842020-07-08 10:10:14 +0200268 in.current = "{";
269 assert_int_equal(LY_EVALID, get_argument(&ctx, &in, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200270 logbuf_assert("Invalid character sequence \"{\", expected an argument. Line number 1.");
271
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200272 /* invalid escape sequence */
Michal Vasko63f3d842020-07-08 10:10:14 +0200273 in.current = "\"\\s\"";
274 assert_int_equal(LY_EVALID, get_argument(&ctx, &in, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200275 logbuf_assert("Double-quoted string unknown special character \'\\s\'. Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200276 in.current = "\'\\s\'"; /* valid, since it is not an escape sequence in single quoted string */
277 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &in, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200278 assert_int_equal(2, len);
279 assert_string_equal("\\s\'", word);
Michal Vasko63f3d842020-07-08 10:10:14 +0200280 assert_int_equal('\0', in.current[0]); /* input has been eaten */
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200281
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200282 /* invalid character after the argument */
Michal Vasko63f3d842020-07-08 10:10:14 +0200283 in.current = "hello\"";
284 assert_int_equal(LY_EVALID, get_argument(&ctx, &in, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200285 logbuf_assert("Invalid character sequence \"\"\", expected unquoted string character, optsep, semicolon or opening brace. Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200286 in.current = "hello}";
287 assert_int_equal(LY_EVALID, get_argument(&ctx, &in, 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
David Sedlák40bb13b2019-07-10 14:34:18 +0200290 /* invalid identifier-ref-arg-str */
Michal Vasko63f3d842020-07-08 10:10:14 +0200291 in.current = "pre:pre:value";
292 assert_int_equal(LY_EVALID, get_argument(&ctx, &in, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &len));
David Sedlák40bb13b2019-07-10 14:34:18 +0200293
Michal Vasko63f3d842020-07-08 10:10:14 +0200294 in.current = "\"\";"; /* empty identifier is not allowed */
295 assert_int_equal(LY_EVALID, get_argument(&ctx, &in, Y_IDENTIF_ARG, NULL, &word, &buf, &len));
Radek Krejci4e199f52019-05-28 09:09:28 +0200296 logbuf_assert("Statement argument is required. Line number 1.");
297 logbuf_clean();
Michal Vasko63f3d842020-07-08 10:10:14 +0200298 in.current = "\"\";"; /* empty reference identifier is not allowed */
299 assert_int_equal(LY_EVALID, get_argument(&ctx, &in, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &len));
Radek Krejci4e199f52019-05-28 09:09:28 +0200300 logbuf_assert("Statement argument is required. Line number 1.");
301
Michal Vasko63f3d842020-07-08 10:10:14 +0200302 in.current = "hello/x\t"; /* slash is not an invalid character */
303 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &in, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200304 assert_int_equal(7, len);
305 assert_string_equal("hello/x\t", word);
306
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200307 assert_null(buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200308
309 /* different quoting */
Michal Vasko63f3d842020-07-08 10:10:14 +0200310 in.current = "hello ";
311 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &in, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200312 assert_null(buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200313 assert_int_equal(5, len);
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200314 assert_string_equal("hello ", word);
Radek Krejciefd22f62018-09-27 11:47:58 +0200315
Michal Vasko63f3d842020-07-08 10:10:14 +0200316 in.current = "hello/*comment*/\n";
317 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &in, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200318 assert_null(buf);
319 assert_int_equal(5, len);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200320 assert_false(strncmp("hello", word, len));
321
322
Michal Vasko63f3d842020-07-08 10:10:14 +0200323 in.current = "\"hello\\n\\t\\\"\\\\\";";
324 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &in, Y_STR_ARG, NULL, &word, &buf, &len));
fredgand49fe112019-10-21 20:51:50 +0800325 assert_non_null(buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200326 assert_int_equal(9, len);
fredgand49fe112019-10-21 20:51:50 +0800327 assert_string_equal("hello\n\t\"\\", word);
328 free(buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200329
330 ctx.indent = 14;
Michal Vasko63f3d842020-07-08 10:10:14 +0200331 in.current = "\"hello \t\n\t\t world!\"";
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200332 /* - space and tabs before newline are stripped out
333 * - space and tabs after newline (indentation) are stripped out
334 */
Michal Vasko63f3d842020-07-08 10:10:14 +0200335 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &in, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200336 assert_non_null(buf);
337 assert_ptr_equal(word, buf);
338 assert_int_equal(14, len);
339 assert_string_equal("hello\n world!", word);
340 free(buf);
Radek Krejciff13cd12019-10-25 15:34:24 +0200341 /* In contrast to previous, the backslash-escaped tabs are expanded after trimming, so they are preserved */
342 ctx.indent = 14;
Michal Vasko63f3d842020-07-08 10:10:14 +0200343 in.current = "\"hello \\t\n\t\\t world!\"";
344 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &in, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciff13cd12019-10-25 15:34:24 +0200345 assert_non_null(buf);
346 assert_ptr_equal(word, buf);
347 assert_int_equal(16, len);
348 assert_string_equal("hello \t\n\t world!", word);
349 free(buf);
350 /* Do not handle whitespaces after backslash-escaped newline as indentation */
351 ctx.indent = 14;
Michal Vasko63f3d842020-07-08 10:10:14 +0200352 in.current = "\"hello\\n\t\t world!\"";
353 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &in, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciff13cd12019-10-25 15:34:24 +0200354 assert_non_null(buf);
355 assert_ptr_equal(word, buf);
356 assert_int_equal(15, len);
357 assert_string_equal("hello\n\t\t world!", word);
358 free(buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200359
360 ctx.indent = 14;
Michal Vasko63f3d842020-07-08 10:10:14 +0200361 in.current = "\"hello\n \tworld!\"";
362 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &in, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200363 assert_non_null(buf);
364 assert_ptr_equal(word, buf);
365 assert_int_equal(12, len);
366 assert_string_equal("hello\nworld!", word);
367 free(buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200368
Michal Vasko63f3d842020-07-08 10:10:14 +0200369 in.current = "\'hello\'";
370 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &in, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200371 assert_null(buf);
372 assert_int_equal(5, len);
373 assert_false(strncmp("hello", word, 5));
374
Michal Vasko63f3d842020-07-08 10:10:14 +0200375 in.current = "\"hel\" +\t\n\"lo\"";
376 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &in, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200377 assert_ptr_equal(word, buf);
378 assert_int_equal(5, len);
379 assert_string_equal("hello", word);
380 free(buf);
Michal Vasko63f3d842020-07-08 10:10:14 +0200381 in.current = "\"hel\" +\t\nlo"; /* unquoted the second part */
382 assert_int_equal(LY_EVALID, get_argument(&ctx, &in, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciff13cd12019-10-25 15:34:24 +0200383 logbuf_assert("Both string parts divided by '+' must be quoted. Line number 6.");
Radek Krejciefd22f62018-09-27 11:47:58 +0200384
Michal Vasko63f3d842020-07-08 10:10:14 +0200385 in.current = "\'he\'\t\n+ \"llo\"";
386 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &in, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200387 assert_ptr_equal(word, buf);
388 assert_int_equal(5, len);
389 assert_string_equal("hello", word);
390 free(buf);
391
Michal Vasko63f3d842020-07-08 10:10:14 +0200392 in.current = " \t\n\"he\"+\'llo\'";
393 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &in, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200394 assert_ptr_equal(word, buf);
395 assert_int_equal(5, len);
396 assert_string_equal("hello", word);
397 free(buf);
398
Radek Krejci44ceedc2018-10-02 15:54:31 +0200399 /* missing argument */
Michal Vasko63f3d842020-07-08 10:10:14 +0200400 in.current = ";";
401 assert_int_equal(LY_EVALID, get_argument(&ctx, &in, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciff13cd12019-10-25 15:34:24 +0200402 logbuf_assert("Invalid character sequence \";\", expected an argument. Line number 8.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200403}
404
405static void
406test_stmts(void **state)
407{
408 (void) state; /* unused */
409
Michal Vaskob36053d2020-03-26 15:49:30 +0100410 struct lys_yang_parser_ctx ctx;
Michal Vasko63f3d842020-07-08 10:10:14 +0200411 struct ly_in in = {0};
412 const char *p;
Radek Krejcid6b76452019-09-03 17:03:03 +0200413 enum ly_stmt kw;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200414 char *word;
415 size_t len;
416
Michal Vaskob36053d2020-03-26 15:49:30 +0100417 ctx.format = LYS_IN_YANG;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200418 ctx.ctx = NULL;
Radek Krejci99435242019-09-05 16:19:15 +0200419 ctx.pos_type = LY_VLOG_LINE;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200420 ctx.line = 1;
421
Michal Vasko63f3d842020-07-08 10:10:14 +0200422 in.current = "\n// comment\n\tinput\t{";
423 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200424 assert_int_equal(LY_STMT_INPUT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200425 assert_int_equal(5, len);
426 assert_string_equal("input\t{", word);
Michal Vasko63f3d842020-07-08 10:10:14 +0200427 assert_string_equal("\t{", in.current);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200428
Michal Vasko63f3d842020-07-08 10:10:14 +0200429 in.current = "\t /* comment */\t output\n\t{";
430 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200431 assert_int_equal(LY_STMT_OUTPUT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200432 assert_int_equal(6, len);
433 assert_string_equal("output\n\t{", word);
Michal Vasko63f3d842020-07-08 10:10:14 +0200434 assert_string_equal("\n\t{", in.current);
435 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200436 assert_int_equal(LY_STMT_SYNTAX_LEFT_BRACE, kw);
Radek Krejciabdd8062019-06-11 16:44:19 +0200437 assert_int_equal(1, len);
438 assert_string_equal("{", word);
Michal Vasko63f3d842020-07-08 10:10:14 +0200439 assert_string_equal("", in.current);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200440
Michal Vasko63f3d842020-07-08 10:10:14 +0200441 in.current = "/input { "; /* invalid slash */
442 assert_int_equal(LY_EVALID, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcidcc7b322018-10-11 14:24:02 +0200443 logbuf_assert("Invalid identifier first character '/'. Line number 4.");
444
Michal Vasko63f3d842020-07-08 10:10:14 +0200445 in.current = "not-a-statement-nor-extension { "; /* invalid identifier */
446 assert_int_equal(LY_EVALID, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcidcc7b322018-10-11 14:24:02 +0200447 logbuf_assert("Invalid character sequence \"not-a-statement-nor-extension\", expected a keyword. Line number 4.");
448
Michal Vasko63f3d842020-07-08 10:10:14 +0200449 in.current = "path;"; /* missing sep after the keyword */
450 assert_int_equal(LY_EVALID, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcidcc7b322018-10-11 14:24:02 +0200451 logbuf_assert("Invalid character sequence \"path;\", expected a keyword followed by a separator. Line number 4.");
452
Michal Vasko63f3d842020-07-08 10:10:14 +0200453 in.current = "action ";
454 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200455 assert_int_equal(LY_STMT_ACTION, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200456 assert_int_equal(6, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200457 in.current = "anydata ";
458 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200459 assert_int_equal(LY_STMT_ANYDATA, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200460 assert_int_equal(7, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200461 in.current = "anyxml ";
462 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200463 assert_int_equal(LY_STMT_ANYXML, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200464 assert_int_equal(6, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200465 in.current = "argument ";
466 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200467 assert_int_equal(LY_STMT_ARGUMENT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200468 assert_int_equal(8, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200469 in.current = "augment ";
470 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200471 assert_int_equal(LY_STMT_AUGMENT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200472 assert_int_equal(7, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200473 in.current = "base ";
474 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200475 assert_int_equal(LY_STMT_BASE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200476 assert_int_equal(4, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200477 in.current = "belongs-to ";
478 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200479 assert_int_equal(LY_STMT_BELONGS_TO, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200480 assert_int_equal(10, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200481 in.current = "bit ";
482 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200483 assert_int_equal(LY_STMT_BIT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200484 assert_int_equal(3, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200485 in.current = "case ";
486 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200487 assert_int_equal(LY_STMT_CASE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200488 assert_int_equal(4, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200489 in.current = "choice ";
490 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200491 assert_int_equal(LY_STMT_CHOICE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200492 assert_int_equal(6, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200493 in.current = "config ";
494 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200495 assert_int_equal(LY_STMT_CONFIG, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200496 assert_int_equal(6, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200497 in.current = "contact ";
498 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200499 assert_int_equal(LY_STMT_CONTACT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200500 assert_int_equal(7, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200501 in.current = "container ";
502 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200503 assert_int_equal(LY_STMT_CONTAINER, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200504 assert_int_equal(9, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200505 in.current = "default ";
506 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200507 assert_int_equal(LY_STMT_DEFAULT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200508 assert_int_equal(7, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200509 in.current = "description ";
510 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200511 assert_int_equal(LY_STMT_DESCRIPTION, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200512 assert_int_equal(11, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200513 in.current = "deviate ";
514 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200515 assert_int_equal(LY_STMT_DEVIATE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200516 assert_int_equal(7, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200517 in.current = "deviation ";
518 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200519 assert_int_equal(LY_STMT_DEVIATION, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200520 assert_int_equal(9, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200521 in.current = "enum ";
522 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200523 assert_int_equal(LY_STMT_ENUM, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200524 assert_int_equal(4, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200525 in.current = "error-app-tag ";
526 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200527 assert_int_equal(LY_STMT_ERROR_APP_TAG, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200528 assert_int_equal(13, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200529 in.current = "error-message ";
530 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200531 assert_int_equal(LY_STMT_ERROR_MESSAGE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200532 assert_int_equal(13, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200533 in.current = "extension ";
534 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200535 assert_int_equal(LY_STMT_EXTENSION, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200536 assert_int_equal(9, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200537 in.current = "feature ";
538 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200539 assert_int_equal(LY_STMT_FEATURE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200540 assert_int_equal(7, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200541 in.current = "fraction-digits ";
542 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200543 assert_int_equal(LY_STMT_FRACTION_DIGITS, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200544 assert_int_equal(15, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200545 in.current = "grouping ";
546 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200547 assert_int_equal(LY_STMT_GROUPING, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200548 assert_int_equal(8, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200549 in.current = "identity ";
550 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200551 assert_int_equal(LY_STMT_IDENTITY, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200552 assert_int_equal(8, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200553 in.current = "if-feature ";
554 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200555 assert_int_equal(LY_STMT_IF_FEATURE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200556 assert_int_equal(10, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200557 in.current = "import ";
558 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200559 assert_int_equal(LY_STMT_IMPORT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200560 assert_int_equal(6, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200561 in.current = "include ";
562 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200563 assert_int_equal(LY_STMT_INCLUDE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200564 assert_int_equal(7, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200565 in.current = "input{";
566 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200567 assert_int_equal(LY_STMT_INPUT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200568 assert_int_equal(5, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200569 in.current = "key ";
570 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200571 assert_int_equal(LY_STMT_KEY, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200572 assert_int_equal(3, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200573 in.current = "leaf ";
574 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200575 assert_int_equal(LY_STMT_LEAF, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200576 assert_int_equal(4, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200577 in.current = "leaf-list ";
578 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200579 assert_int_equal(LY_STMT_LEAF_LIST, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200580 assert_int_equal(9, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200581 in.current = "length ";
582 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200583 assert_int_equal(LY_STMT_LENGTH, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200584 assert_int_equal(6, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200585 in.current = "list ";
586 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200587 assert_int_equal(LY_STMT_LIST, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200588 assert_int_equal(4, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200589 in.current = "mandatory ";
590 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200591 assert_int_equal(LY_STMT_MANDATORY, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200592 assert_int_equal(9, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200593 in.current = "max-elements ";
594 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200595 assert_int_equal(LY_STMT_MAX_ELEMENTS, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200596 assert_int_equal(12, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200597 in.current = "min-elements ";
598 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200599 assert_int_equal(LY_STMT_MIN_ELEMENTS, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200600 assert_int_equal(12, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200601 in.current = "modifier ";
602 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200603 assert_int_equal(LY_STMT_MODIFIER, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200604 assert_int_equal(8, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200605 in.current = "module ";
606 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200607 assert_int_equal(LY_STMT_MODULE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200608 assert_int_equal(6, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200609 in.current = "must ";
610 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200611 assert_int_equal(LY_STMT_MUST, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200612 assert_int_equal(4, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200613 in.current = "namespace ";
614 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200615 assert_int_equal(LY_STMT_NAMESPACE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200616 assert_int_equal(9, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200617 in.current = "notification ";
618 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200619 assert_int_equal(LY_STMT_NOTIFICATION, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200620 assert_int_equal(12, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200621 in.current = "ordered-by ";
622 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200623 assert_int_equal(LY_STMT_ORDERED_BY, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200624 assert_int_equal(10, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200625 in.current = "organization ";
626 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200627 assert_int_equal(LY_STMT_ORGANIZATION, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200628 assert_int_equal(12, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200629 in.current = "output ";
630 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200631 assert_int_equal(LY_STMT_OUTPUT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200632 assert_int_equal(6, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200633 in.current = "path ";
634 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200635 assert_int_equal(LY_STMT_PATH, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200636 assert_int_equal(4, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200637 in.current = "pattern ";
638 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200639 assert_int_equal(LY_STMT_PATTERN, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200640 assert_int_equal(7, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200641 in.current = "position ";
642 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200643 assert_int_equal(LY_STMT_POSITION, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200644 assert_int_equal(8, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200645 in.current = "prefix ";
646 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200647 assert_int_equal(LY_STMT_PREFIX, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200648 assert_int_equal(6, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200649 in.current = "presence ";
650 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200651 assert_int_equal(LY_STMT_PRESENCE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200652 assert_int_equal(8, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200653 in.current = "range ";
654 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200655 assert_int_equal(LY_STMT_RANGE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200656 assert_int_equal(5, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200657 in.current = "reference ";
658 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200659 assert_int_equal(LY_STMT_REFERENCE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200660 assert_int_equal(9, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200661 in.current = "refine ";
662 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200663 assert_int_equal(LY_STMT_REFINE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200664 assert_int_equal(6, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200665 in.current = "require-instance ";
666 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200667 assert_int_equal(LY_STMT_REQUIRE_INSTANCE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200668 assert_int_equal(16, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200669 in.current = "revision ";
670 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200671 assert_int_equal(LY_STMT_REVISION, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200672 assert_int_equal(8, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200673 in.current = "revision-date ";
674 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200675 assert_int_equal(LY_STMT_REVISION_DATE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200676 assert_int_equal(13, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200677 in.current = "rpc ";
678 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200679 assert_int_equal(LY_STMT_RPC, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200680 assert_int_equal(3, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200681 in.current = "status ";
682 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200683 assert_int_equal(LY_STMT_STATUS, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200684 assert_int_equal(6, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200685 in.current = "submodule ";
686 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200687 assert_int_equal(LY_STMT_SUBMODULE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200688 assert_int_equal(9, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200689 in.current = "type ";
690 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200691 assert_int_equal(LY_STMT_TYPE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200692 assert_int_equal(4, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200693 in.current = "typedef ";
694 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200695 assert_int_equal(LY_STMT_TYPEDEF, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200696 assert_int_equal(7, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200697 in.current = "unique ";
698 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200699 assert_int_equal(LY_STMT_UNIQUE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200700 assert_int_equal(6, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200701 in.current = "units ";
702 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200703 assert_int_equal(LY_STMT_UNITS, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200704 assert_int_equal(5, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200705 in.current = "uses ";
706 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200707 assert_int_equal(LY_STMT_USES, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200708 assert_int_equal(4, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200709 in.current = "value ";
710 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200711 assert_int_equal(LY_STMT_VALUE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200712 assert_int_equal(5, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200713 in.current = "when ";
714 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200715 assert_int_equal(LY_STMT_WHEN, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200716 assert_int_equal(4, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200717 in.current = "yang-version ";
718 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200719 assert_int_equal(LY_STMT_YANG_VERSION, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200720 assert_int_equal(12, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200721 in.current = "yin-element ";
722 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200723 assert_int_equal(LY_STMT_YIN_ELEMENT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200724 assert_int_equal(11, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200725 in.current = ";config false;";
726 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200727 assert_int_equal(LY_STMT_SYNTAX_SEMICOLON, kw);
Radek Krejci626df482018-10-11 15:06:31 +0200728 assert_int_equal(1, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200729 assert_string_equal("config false;", in.current);
730 in.current = "{ config false;";
731 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200732 assert_int_equal(LY_STMT_SYNTAX_LEFT_BRACE, kw);
Radek Krejci626df482018-10-11 15:06:31 +0200733 assert_int_equal(1, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200734 assert_string_equal(" config false;", in.current);
735 in.current = "}";
736 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200737 assert_int_equal(LY_STMT_SYNTAX_RIGHT_BRACE, kw);
Radek Krejci626df482018-10-11 15:06:31 +0200738 assert_int_equal(1, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200739 assert_string_equal("", in.current);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200740
741 /* geenric extension */
Michal Vasko63f3d842020-07-08 10:10:14 +0200742 in.current = p = "nacm:default-deny-write;";
743 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &in, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200744 assert_int_equal(LY_STMT_EXTENSION_INSTANCE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200745 assert_int_equal(23, len);
746 assert_ptr_equal(p, word);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200747}
Radek Krejci44ceedc2018-10-02 15:54:31 +0200748
Radek Krejci05b13982018-11-28 16:22:07 +0100749static void
750test_minmax(void **state)
751{
752 *state = test_minmax;
753
Michal Vaskob36053d2020-03-26 15:49:30 +0100754 struct lys_yang_parser_ctx ctx = {0};
Radek Krejci05b13982018-11-28 16:22:07 +0100755 uint16_t flags = 0;
756 uint32_t value = 0;
757 struct lysp_ext_instance *ext = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +0200758 struct ly_in in = {0};
Radek Krejci05b13982018-11-28 16:22:07 +0100759
Michal Vaskob36053d2020-03-26 15:49:30 +0100760 ctx.format = LYS_IN_YANG;
Radek Krejci05b13982018-11-28 16:22:07 +0100761 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
762 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +0200763 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci05b13982018-11-28 16:22:07 +0100764 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100765 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci05b13982018-11-28 16:22:07 +0100766
Michal Vasko63f3d842020-07-08 10:10:14 +0200767 in.current = " 1invalid; ...";
768 assert_int_equal(LY_EVALID, parse_minelements(&ctx, &in, &value, &flags, &ext));
Radek Krejcidf6cad12018-11-28 17:10:55 +0100769 logbuf_assert("Invalid value \"1invalid\" of \"min-elements\". Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100770
771 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200772 in.current = " -1; ...";
773 assert_int_equal(LY_EVALID, parse_minelements(&ctx, &in, &value, &flags, &ext));
Radek Krejci05b13982018-11-28 16:22:07 +0100774 logbuf_assert("Invalid value \"-1\" of \"min-elements\". Line number 1.");
775
Radek Krejcidf6cad12018-11-28 17:10:55 +0100776 /* implementation limit */
777 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200778 in.current = " 4294967296; ...";
779 assert_int_equal(LY_EVALID, parse_minelements(&ctx, &in, &value, &flags, &ext));
Radek Krejcidf6cad12018-11-28 17:10:55 +0100780 logbuf_assert("Value \"4294967296\" is out of \"min-elements\" bounds. Line number 1.");
781
Radek Krejci05b13982018-11-28 16:22:07 +0100782 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200783 in.current = " 1; ...";
784 assert_int_equal(LY_SUCCESS, parse_minelements(&ctx, &in, &value, &flags, &ext));
Radek Krejci05b13982018-11-28 16:22:07 +0100785 assert_int_equal(LYS_SET_MIN, flags);
786 assert_int_equal(1, value);
787
788 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200789 in.current = " 1 {m:ext;} ...";
790 assert_int_equal(LY_SUCCESS, parse_minelements(&ctx, &in, &value, &flags, &ext));
Radek Krejci05b13982018-11-28 16:22:07 +0100791 assert_int_equal(LYS_SET_MIN, flags);
792 assert_int_equal(1, value);
793 assert_non_null(ext);
794 FREE_ARRAY(ctx.ctx, ext, lysp_ext_instance_free);
795 ext = NULL;
796
797 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200798 in.current = " 1 {config true;} ...";
799 assert_int_equal(LY_EVALID, parse_minelements(&ctx, &in, &value, &flags, &ext));
Radek Krejci05b13982018-11-28 16:22:07 +0100800 logbuf_assert("Invalid keyword \"config\" as a child of \"min-elements\". Line number 1.");
801
Michal Vasko63f3d842020-07-08 10:10:14 +0200802 in.current = " 1invalid; ...";
803 assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &in, &value, &flags, &ext));
Radek Krejcidf6cad12018-11-28 17:10:55 +0100804 logbuf_assert("Invalid value \"1invalid\" of \"max-elements\". Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100805
806 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200807 in.current = " -1; ...";
808 assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &in, &value, &flags, &ext));
Radek Krejci05b13982018-11-28 16:22:07 +0100809 logbuf_assert("Invalid value \"-1\" of \"max-elements\". Line number 1.");
810
Radek Krejcidf6cad12018-11-28 17:10:55 +0100811 /* implementation limit */
812 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200813 in.current = " 4294967296; ...";
814 assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &in, &value, &flags, &ext));
Radek Krejcidf6cad12018-11-28 17:10:55 +0100815 logbuf_assert("Value \"4294967296\" is out of \"max-elements\" bounds. Line number 1.");
816
Radek Krejci05b13982018-11-28 16:22:07 +0100817 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200818 in.current = " 1; ...";
819 assert_int_equal(LY_SUCCESS, parse_maxelements(&ctx, &in, &value, &flags, &ext));
Radek Krejci05b13982018-11-28 16:22:07 +0100820 assert_int_equal(LYS_SET_MAX, flags);
821 assert_int_equal(1, value);
822
823 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200824 in.current = " unbounded; ...";
825 assert_int_equal(LY_SUCCESS, parse_maxelements(&ctx, &in, &value, &flags, &ext));
Radek Krejci05b13982018-11-28 16:22:07 +0100826 assert_int_equal(LYS_SET_MAX, flags);
827 assert_int_equal(0, value);
828
829 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200830 in.current = " 1 {m:ext;} ...";
831 assert_int_equal(LY_SUCCESS, parse_maxelements(&ctx, &in, &value, &flags, &ext));
Radek Krejci05b13982018-11-28 16:22:07 +0100832 assert_int_equal(LYS_SET_MAX, flags);
833 assert_int_equal(1, value);
834 assert_non_null(ext);
835 FREE_ARRAY(ctx.ctx, ext, lysp_ext_instance_free);
836 ext = NULL;
837
838 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200839 in.current = " 1 {config true;} ...";
840 assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &in, &value, &flags, &ext));
Radek Krejci05b13982018-11-28 16:22:07 +0100841 logbuf_assert("Invalid keyword \"config\" as a child of \"max-elements\". Line number 1.");
842
843 *state = NULL;
844 ly_ctx_destroy(ctx.ctx, NULL);
845}
846
Radek Krejci9fcacc12018-10-11 15:59:11 +0200847static struct lysp_module *
Michal Vaskob36053d2020-03-26 15:49:30 +0100848mod_renew(struct lys_yang_parser_ctx *ctx)
Radek Krejci9fcacc12018-10-11 15:59:11 +0200849{
Radek Krejci40544fa2019-01-11 09:38:37 +0100850 struct lysp_module *mod_p;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100851 static struct lys_module mod = {0};
852
853 lysc_module_free(mod.compiled, NULL);
854 lysp_module_free(mod.parsed);
855 FREE_STRING(mod.ctx, mod.name);
856 FREE_STRING(mod.ctx, mod.ns);
857 FREE_STRING(mod.ctx, mod.prefix);
858 FREE_STRING(mod.ctx, mod.filepath);
859 FREE_STRING(mod.ctx, mod.org);
860 FREE_STRING(mod.ctx, mod.contact);
861 FREE_STRING(mod.ctx, mod.dsc);
862 FREE_STRING(mod.ctx, mod.ref);
863 memset(&mod, 0, sizeof mod);
864 mod.ctx = ctx->ctx;
865
866 mod_p = calloc(1, sizeof *mod_p);
867 mod.parsed = mod_p;
868 mod_p->mod = &mod;
869 assert_non_null(mod_p);
870 return mod_p;
871}
872
873static struct lysp_submodule *
Michal Vaskob36053d2020-03-26 15:49:30 +0100874submod_renew(struct lys_yang_parser_ctx *ctx, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100875{
876 lysp_submodule_free(ctx->ctx, submod);
877 submod = calloc(1, sizeof *submod);
878 assert_non_null(submod);
879 return submod;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200880}
881
Radek Krejcid33273d2018-10-25 14:55:52 +0200882static LY_ERR test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
883 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
884 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
885{
886 *module_data = user_data;
887 *format = LYS_IN_YANG;
888 *free_module_data = NULL;
889 return LY_SUCCESS;
890}
891
Radek Krejci9fcacc12018-10-11 15:59:11 +0200892static void
893test_module(void **state)
894{
Radek Krejci40544fa2019-01-11 09:38:37 +0100895 *state = test_module;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200896
Michal Vaskob36053d2020-03-26 15:49:30 +0100897 struct lys_yang_parser_ctx ctx;
Radek Krejci40544fa2019-01-11 09:38:37 +0100898 struct lysp_module *mod = NULL;
899 struct lysp_submodule *submod = NULL;
900 struct lys_module *m;
Michal Vasko63f3d842020-07-08 10:10:14 +0200901 struct ly_in in = {0};
Radek Krejci9fcacc12018-10-11 15:59:11 +0200902
Michal Vaskob36053d2020-03-26 15:49:30 +0100903 ctx.format = LYS_IN_YANG;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200904 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
905 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +0200906 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200907 ctx.line = 1;
Radek Krejcia042ea12018-10-13 07:52:15 +0200908 ctx.indent = 0;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200909
Radek Krejci40544fa2019-01-11 09:38:37 +0100910 mod = mod_renew(&ctx);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200911
912 /* missing mandatory substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +0200913 in.current = " name {}";
914 assert_int_equal(LY_EVALID, parse_module(&ctx, &in, mod));
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100915 assert_string_equal("name", mod->mod->name);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200916 logbuf_assert("Missing mandatory keyword \"namespace\" as a child of \"module\". Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100917 mod = mod_renew(&ctx);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200918
Michal Vasko63f3d842020-07-08 10:10:14 +0200919 in.current = " name {namespace urn:x;}";
920 assert_int_equal(LY_EVALID, parse_module(&ctx, &in, mod));
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100921 assert_string_equal("urn:x", mod->mod->ns);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200922 logbuf_assert("Missing mandatory keyword \"prefix\" as a child of \"module\". Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100923 mod = mod_renew(&ctx);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200924
Michal Vasko63f3d842020-07-08 10:10:14 +0200925 in.current = " name {namespace urn:x;prefix \"x\";}";
926 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &in, mod));
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100927 assert_string_equal("x", mod->mod->prefix);
Radek Krejci40544fa2019-01-11 09:38:37 +0100928 mod = mod_renew(&ctx);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200929
Radek Krejci027d5802018-11-14 16:57:28 +0100930#define SCHEMA_BEGINNING " name {yang-version 1.1;namespace urn:x;prefix \"x\";"
931#define SCHEMA_BEGINNING2 " name {namespace urn:x;prefix \"x\";"
Radek Krejcia042ea12018-10-13 07:52:15 +0200932#define TEST_NODE(NODETYPE, INPUT, NAME) \
Michal Vasko63f3d842020-07-08 10:10:14 +0200933 in.current = SCHEMA_BEGINNING INPUT; \
934 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &in, mod)); \
Radek Krejcia042ea12018-10-13 07:52:15 +0200935 assert_non_null(mod->data); \
936 assert_int_equal(NODETYPE, mod->data->nodetype); \
937 assert_string_equal(NAME, mod->data->name); \
Radek Krejci40544fa2019-01-11 09:38:37 +0100938 mod = mod_renew(&ctx);
Radek Krejcia042ea12018-10-13 07:52:15 +0200939#define TEST_GENERIC(INPUT, TARGET, TEST) \
Michal Vasko63f3d842020-07-08 10:10:14 +0200940 in.current = SCHEMA_BEGINNING INPUT; \
941 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &in, mod)); \
Radek Krejcia042ea12018-10-13 07:52:15 +0200942 assert_non_null(TARGET); \
943 TEST; \
Radek Krejci40544fa2019-01-11 09:38:37 +0100944 mod = mod_renew(&ctx);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100945#define TEST_DUP(MEMBER, VALUE1, VALUE2, LINE) \
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200946 TEST_DUP_GENERIC(SCHEMA_BEGINNING, MEMBER, VALUE1, VALUE2, \
Radek Krejci40544fa2019-01-11 09:38:37 +0100947 parse_module, mod, LINE, mod = mod_renew(&ctx))
Radek Krejcia042ea12018-10-13 07:52:15 +0200948
949 /* duplicated namespace, prefix */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100950 TEST_DUP("namespace", "y", "z", "1");
951 TEST_DUP("prefix", "y", "z", "1");
952 TEST_DUP("contact", "a", "b", "1");
953 TEST_DUP("description", "a", "b", "1");
954 TEST_DUP("organization", "a", "b", "1");
955 TEST_DUP("reference", "a", "b", "1");
Radek Krejcia042ea12018-10-13 07:52:15 +0200956
Radek Krejci70853c52018-10-15 14:46:16 +0200957 /* not allowed in module (submodule-specific) */
Michal Vasko63f3d842020-07-08 10:10:14 +0200958 in.current = SCHEMA_BEGINNING "belongs-to master {prefix m;}}";
959 assert_int_equal(LY_EVALID, parse_module(&ctx, &in, mod));
Radek Krejci70853c52018-10-15 14:46:16 +0200960 logbuf_assert("Invalid keyword \"belongs-to\" as a child of \"module\". Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100961 mod = mod_renew(&ctx);
Radek Krejci70853c52018-10-15 14:46:16 +0200962
Radek Krejcia042ea12018-10-13 07:52:15 +0200963 /* anydata */
964 TEST_NODE(LYS_ANYDATA, "anydata test;}", "test");
965 /* anyxml */
966 TEST_NODE(LYS_ANYXML, "anyxml test;}", "test");
967 /* augment */
968 TEST_GENERIC("augment /somepath;}", mod->augments,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200969 assert_string_equal("/somepath", mod->augments[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200970 /* choice */
971 TEST_NODE(LYS_CHOICE, "choice test;}", "test");
972 /* contact 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100973 TEST_GENERIC("contact \"firstname\" + \n\t\" surname\";}", mod->mod->contact,
974 assert_string_equal("firstname surname", mod->mod->contact));
Radek Krejcia042ea12018-10-13 07:52:15 +0200975 /* container */
976 TEST_NODE(LYS_CONTAINER, "container test;}", "test");
977 /* description 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100978 TEST_GENERIC("description \'some description\';}", mod->mod->dsc,
979 assert_string_equal("some description", mod->mod->dsc));
Radek Krejcia042ea12018-10-13 07:52:15 +0200980 /* deviation */
981 TEST_GENERIC("deviation /somepath {deviate not-supported;}}", mod->deviations,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200982 assert_string_equal("/somepath", mod->deviations[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200983 /* extension */
984 TEST_GENERIC("extension test;}", mod->extensions,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200985 assert_string_equal("test", mod->extensions[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200986 /* feature */
987 TEST_GENERIC("feature test;}", mod->features,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200988 assert_string_equal("test", mod->features[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200989 /* grouping */
990 TEST_GENERIC("grouping grp;}", mod->groupings,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200991 assert_string_equal("grp", mod->groupings[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200992 /* identity */
993 TEST_GENERIC("identity test;}", mod->identities,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200994 assert_string_equal("test", mod->identities[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200995 /* import */
Radek Krejci086c7132018-10-26 15:29:04 +0200996 ly_ctx_set_module_imp_clb(ctx.ctx, test_imp_clb, "module zzz { namespace urn:zzz; prefix z;}");
997 TEST_GENERIC("import zzz {prefix z;}}", mod->imports,
998 assert_string_equal("zzz", mod->imports[0].name));
Radek Krejci70853c52018-10-15 14:46:16 +0200999
Radek Krejcia042ea12018-10-13 07:52:15 +02001000 /* import - prefix collision */
Michal Vasko63f3d842020-07-08 10:10:14 +02001001 in.current = SCHEMA_BEGINNING "import zzz {prefix x;}}";
1002 assert_int_equal(LY_EVALID, parse_module(&ctx, &in, mod));
Radek Krejci70853c52018-10-15 14:46:16 +02001003 logbuf_assert("Prefix \"x\" already used as module prefix. Line number 2.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001004 mod = mod_renew(&ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02001005 in.current = SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix y;}}";
1006 assert_int_equal(LY_EVALID, parse_module(&ctx, &in, mod));
Radek Krejci086c7132018-10-26 15:29:04 +02001007 logbuf_assert("Prefix \"y\" already used to import \"zzz\" module. Line number 2.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001008 mod = mod_renew(&ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02001009 in.current = "module" SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix z;}}";
1010 assert_null(lys_parse_mem(ctx.ctx, in.current, LYS_IN_YANG));
Radek Krejci086c7132018-10-26 15:29:04 +02001011 assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx));
1012 logbuf_assert("Single revision of the module \"zzz\" referred twice.");
Radek Krejci70853c52018-10-15 14:46:16 +02001013
Radek Krejcia042ea12018-10-13 07:52:15 +02001014 /* include */
Radek Krejci9ed7a192018-10-31 16:23:51 +01001015 store = 1;
Radek Krejcid33273d2018-10-25 14:55:52 +02001016 ly_ctx_set_module_imp_clb(ctx.ctx, test_imp_clb, "module xxx { namespace urn:xxx; prefix x;}");
Michal Vasko63f3d842020-07-08 10:10:14 +02001017 in.current = "module" SCHEMA_BEGINNING "include xxx;}";
1018 assert_null(lys_parse_mem(ctx.ctx, in.current, LYS_IN_YANG));
Radek Krejci086c7132018-10-26 15:29:04 +02001019 assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001020 logbuf_assert("Input data contains module in situation when a submodule is expected.");
Radek Krejci9ed7a192018-10-31 16:23:51 +01001021 store = -1;
Radek Krejcid33273d2018-10-25 14:55:52 +02001022
Radek Krejci9ed7a192018-10-31 16:23:51 +01001023 store = 1;
Radek Krejci313d9902018-11-08 09:42:58 +01001024 ly_ctx_set_module_imp_clb(ctx.ctx, test_imp_clb, "submodule xxx {belongs-to wrong-name {prefix w;}}");
Michal Vasko63f3d842020-07-08 10:10:14 +02001025 in.current = "module" SCHEMA_BEGINNING "include xxx;}";
1026 assert_null(lys_parse_mem(ctx.ctx, in.current, LYS_IN_YANG));
Radek Krejci086c7132018-10-26 15:29:04 +02001027 assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx));
1028 logbuf_assert("Included \"xxx\" submodule from \"name\" belongs-to a different module \"wrong-name\".");
Radek Krejci9ed7a192018-10-31 16:23:51 +01001029 store = -1;
Radek Krejcid33273d2018-10-25 14:55:52 +02001030
Radek Krejci313d9902018-11-08 09:42:58 +01001031 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 +02001032 TEST_GENERIC("include xxx;}", mod->includes,
Radek Krejci086c7132018-10-26 15:29:04 +02001033 assert_string_equal("xxx", mod->includes[0].name));
Radek Krejcid33273d2018-10-25 14:55:52 +02001034
Radek Krejcia042ea12018-10-13 07:52:15 +02001035 /* leaf */
1036 TEST_NODE(LYS_LEAF, "leaf test {type string;}}", "test");
1037 /* leaf-list */
1038 TEST_NODE(LYS_LEAFLIST, "leaf-list test {type string;}}", "test");
1039 /* list */
1040 TEST_NODE(LYS_LIST, "list test {key a;leaf a {type string;}}}", "test");
1041 /* notification */
1042 TEST_GENERIC("notification test;}", mod->notifs,
Radek Krejci2c4e7172018-10-19 15:56:26 +02001043 assert_string_equal("test", mod->notifs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +02001044 /* organization 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001045 TEST_GENERIC("organization \"CESNET a.l.e.\";}", mod->mod->org,
1046 assert_string_equal("CESNET a.l.e.", mod->mod->org));
Radek Krejcia042ea12018-10-13 07:52:15 +02001047 /* reference 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001048 TEST_GENERIC("reference RFC7950;}", mod->mod->ref,
1049 assert_string_equal("RFC7950", mod->mod->ref));
Radek Krejcia042ea12018-10-13 07:52:15 +02001050 /* revision */
1051 TEST_GENERIC("revision 2018-10-12;}", mod->revs,
Radek Krejcib7db73a2018-10-24 14:18:40 +02001052 assert_string_equal("2018-10-12", mod->revs[0].date));
Radek Krejcia042ea12018-10-13 07:52:15 +02001053 /* rpc */
1054 TEST_GENERIC("rpc test;}", mod->rpcs,
Radek Krejci2c4e7172018-10-19 15:56:26 +02001055 assert_string_equal("test", mod->rpcs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +02001056 /* typedef */
1057 TEST_GENERIC("typedef test{type string;}}", mod->typedefs,
Radek Krejci2c4e7172018-10-19 15:56:26 +02001058 assert_string_equal("test", mod->typedefs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +02001059 /* uses */
1060 TEST_NODE(LYS_USES, "uses test;}", "test");
1061 /* yang-version */
Michal Vasko63f3d842020-07-08 10:10:14 +02001062 in.current = SCHEMA_BEGINNING2 "\n\tyang-version 10;}";
1063 assert_int_equal(LY_EVALID, parse_module(&ctx, &in, mod));
Radek Krejcia042ea12018-10-13 07:52:15 +02001064 logbuf_assert("Invalid value \"10\" of \"yang-version\". Line number 3.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001065 mod = mod_renew(&ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02001066 in.current = SCHEMA_BEGINNING2 "yang-version 1.0;yang-version 1.1;}";
1067 assert_int_equal(LY_EVALID, parse_module(&ctx, &in, mod));
Radek Krejcia042ea12018-10-13 07:52:15 +02001068 logbuf_assert("Duplicate keyword \"yang-version\". Line number 3.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001069 mod = mod_renew(&ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02001070 in.current = SCHEMA_BEGINNING2 "yang-version 1.0;}";
1071 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &in, mod));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001072 assert_int_equal(1, mod->mod->version);
Radek Krejci40544fa2019-01-11 09:38:37 +01001073 mod = mod_renew(&ctx);
Michal Vasko63f3d842020-07-08 10:10:14 +02001074 in.current = SCHEMA_BEGINNING2 "yang-version \"1.1\";}";
1075 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &in, mod));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001076 assert_int_equal(2, mod->mod->version);
Radek Krejci40544fa2019-01-11 09:38:37 +01001077 mod = mod_renew(&ctx);
1078
Michal Vaskob36053d2020-03-26 15:49:30 +01001079 struct lys_yang_parser_ctx *ctx_p = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02001080 in.current = "module " SCHEMA_BEGINNING "} module q {namespace urn:q;prefixq;}";
Radek Krejci40544fa2019-01-11 09:38:37 +01001081 m = mod->mod;
1082 free(mod);
1083 m->parsed = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02001084 assert_int_equal(LY_EVALID, yang_parse_module(&ctx_p, &in, m));
David Sedlák1b623122019-08-05 15:27:49 +02001085 logbuf_assert("Trailing garbage \"module q {names...\" after module, expected end-of-input. Line number 1.");
Michal Vaskob36053d2020-03-26 15:49:30 +01001086 yang_parser_ctx_free(ctx_p);
Radek Krejci40544fa2019-01-11 09:38:37 +01001087 mod = mod_renew(&ctx);
1088
Michal Vasko63f3d842020-07-08 10:10:14 +02001089 in.current = "prefix " SCHEMA_BEGINNING "}";
Radek Krejci40544fa2019-01-11 09:38:37 +01001090 m = mod->mod;
1091 free(mod);
1092 m->parsed = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02001093 assert_int_equal(LY_EVALID, yang_parse_module(&ctx_p, &in, m));
Michal Vaskob36053d2020-03-26 15:49:30 +01001094 yang_parser_ctx_free(ctx_p);
David Sedlák1b623122019-08-05 15:27:49 +02001095 logbuf_assert("Invalid keyword \"prefix\", expected \"module\" or \"submodule\". Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001096 mod = mod_renew(&ctx);
Radek Krejci09306362018-10-15 15:26:01 +02001097
Michal Vasko63f3d842020-07-08 10:10:14 +02001098 in.current = "module " SCHEMA_BEGINNING "leaf enum {type enumeration {enum seven { position 7;}}}}";
David Sedlák9fb515f2019-07-11 10:33:58 +02001099 m = mod->mod;
1100 free(mod);
1101 m->parsed = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02001102 assert_int_equal(LY_EVALID, yang_parse_module(&ctx_p, &in, m));
Michal Vaskob36053d2020-03-26 15:49:30 +01001103 yang_parser_ctx_free(ctx_p);
David Sedlák1b623122019-08-05 15:27:49 +02001104 logbuf_assert("Invalid keyword \"position\" as a child of \"enum\". Line number 1.");
David Sedlák9fb515f2019-07-11 10:33:58 +02001105 mod = mod_renew(&ctx);
1106
Radek Krejci156ccaf2018-10-15 15:49:17 +02001107 /* extensions */
1108 TEST_GENERIC("prefix:test;}", mod->exts,
Radek Krejci2c4e7172018-10-19 15:56:26 +02001109 assert_string_equal("prefix:test", mod->exts[0].name);
1110 assert_int_equal(LYEXT_SUBSTMT_SELF, mod->exts[0].insubstmt));
Radek Krejci40544fa2019-01-11 09:38:37 +01001111 mod = mod_renew(&ctx);
Radek Krejci156ccaf2018-10-15 15:49:17 +02001112
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001113 /* invalid substatement */
Michal Vasko63f3d842020-07-08 10:10:14 +02001114 in.current = SCHEMA_BEGINNING "must false;}";
1115 assert_int_equal(LY_EVALID, parse_module(&ctx, &in, mod));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001116 logbuf_assert("Invalid keyword \"must\" as a child of \"module\". Line number 3.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001117 mod = mod_renew(&ctx);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001118
Radek Krejci09306362018-10-15 15:26:01 +02001119 /* submodule */
Radek Krejci40544fa2019-01-11 09:38:37 +01001120 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001121
1122 /* missing mandatory substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +02001123 in.current = " subname {}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001124 lydict_remove(ctx.ctx, submod->name);
Michal Vasko63f3d842020-07-08 10:10:14 +02001125 assert_int_equal(LY_EVALID, parse_submodule(&ctx, &in, submod));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001126 assert_string_equal("subname", submod->name);
Radek Krejci09306362018-10-15 15:26:01 +02001127 logbuf_assert("Missing mandatory keyword \"belongs-to\" as a child of \"submodule\". Line number 3.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001128 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001129
Michal Vasko63f3d842020-07-08 10:10:14 +02001130 in.current = " subname {belongs-to name {prefix x;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001131 lydict_remove(ctx.ctx, submod->name);
Michal Vasko63f3d842020-07-08 10:10:14 +02001132 assert_int_equal(LY_SUCCESS, parse_submodule(&ctx, &in, submod));
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001133 assert_string_equal("name", submod->belongsto);
1134 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001135
1136#undef SCHEMA_BEGINNING
Radek Krejci313d9902018-11-08 09:42:58 +01001137#define SCHEMA_BEGINNING " subname {belongs-to name {prefix x;}"
Radek Krejci09306362018-10-15 15:26:01 +02001138
1139 /* duplicated namespace, prefix */
Michal Vasko63f3d842020-07-08 10:10:14 +02001140 in.current = " subname {belongs-to name {prefix x;}belongs-to module1;belongs-to module2;} ...";
1141 assert_int_equal(LY_EVALID, parse_submodule(&ctx, &in, submod)); \
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001142 logbuf_assert("Duplicate keyword \"belongs-to\". Line number 3."); \
1143 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001144
1145 /* not allowed in submodule (module-specific) */
Michal Vasko63f3d842020-07-08 10:10:14 +02001146 in.current = SCHEMA_BEGINNING "namespace \"urn:z\";}";
1147 assert_int_equal(LY_EVALID, parse_submodule(&ctx, &in, submod));
Radek Krejci09306362018-10-15 15:26:01 +02001148 logbuf_assert("Invalid keyword \"namespace\" as a child of \"submodule\". Line number 3.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001149 submod = submod_renew(&ctx, submod);
Michal Vasko63f3d842020-07-08 10:10:14 +02001150 in.current = SCHEMA_BEGINNING "prefix m;}}";
1151 assert_int_equal(LY_EVALID, parse_submodule(&ctx, &in, submod));
Radek Krejci09306362018-10-15 15:26:01 +02001152 logbuf_assert("Invalid keyword \"prefix\" as a child of \"submodule\". Line number 3.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001153 submod = submod_renew(&ctx, submod);
Radek Krejcia042ea12018-10-13 07:52:15 +02001154
Michal Vasko63f3d842020-07-08 10:10:14 +02001155 in.current = "submodule " SCHEMA_BEGINNING "} module q {namespace urn:q;prefixq;}";
Radek Krejci40544fa2019-01-11 09:38:37 +01001156 lysp_submodule_free(ctx.ctx, submod);
1157 submod = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02001158 assert_int_equal(LY_EVALID, yang_parse_submodule(&ctx_p, ctx.ctx, (struct lys_parser_ctx *)&ctx, &in, &submod));
Michal Vaskob36053d2020-03-26 15:49:30 +01001159 yang_parser_ctx_free(ctx_p);
David Sedlák1b623122019-08-05 15:27:49 +02001160 logbuf_assert("Trailing garbage \"module q {names...\" after submodule, expected end-of-input. Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001161
Michal Vasko63f3d842020-07-08 10:10:14 +02001162 in.current = "prefix " SCHEMA_BEGINNING "}";
1163 assert_int_equal(LY_EVALID, yang_parse_submodule(&ctx_p, ctx.ctx, (struct lys_parser_ctx *)&ctx, &in, &submod));
Michal Vaskob36053d2020-03-26 15:49:30 +01001164 yang_parser_ctx_free(ctx_p);
David Sedlák1b623122019-08-05 15:27:49 +02001165 logbuf_assert("Invalid keyword \"prefix\", expected \"module\" or \"submodule\". Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001166 submod = submod_renew(&ctx, submod);
1167
Radek Krejcia042ea12018-10-13 07:52:15 +02001168#undef TEST_GENERIC
1169#undef TEST_NODE
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001170#undef TEST_DUP
Radek Krejcia042ea12018-10-13 07:52:15 +02001171#undef SCHEMA_BEGINNING
1172
Radek Krejci9fcacc12018-10-11 15:59:11 +02001173 lysp_module_free(mod);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001174 lysp_submodule_free(ctx.ctx, submod);
Radek Krejci9fcacc12018-10-11 15:59:11 +02001175 ly_ctx_destroy(ctx.ctx, NULL);
Radek Krejci40544fa2019-01-11 09:38:37 +01001176
1177 *state = NULL;
Radek Krejciefd22f62018-09-27 11:47:58 +02001178}
1179
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001180static void
1181test_deviation(void **state)
1182{
1183 (void) state; /* unused */
1184
Michal Vaskob36053d2020-03-26 15:49:30 +01001185 struct lys_yang_parser_ctx ctx;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001186 struct lysp_deviation *d = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02001187 struct ly_in in = {0};
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001188
Michal Vaskob36053d2020-03-26 15:49:30 +01001189 ctx.format = LYS_IN_YANG;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001190 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1191 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001192 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001193 ctx.line = 1;
1194 ctx.indent = 0;
1195
1196 /* invalid cardinality */
1197#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1198 TEST_DUP_GENERIC(" test {deviate not-supported;", MEMBER, VALUE1, VALUE2, parse_deviation, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001199 &d, "1", FREE_ARRAY(ctx.ctx, d, lysp_deviation_free); d = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001200
1201 TEST_DUP("description", "a", "b");
1202 TEST_DUP("reference", "a", "b");
1203
1204 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001205 in.current = " test {deviate not-supported;description text;reference \'another text\';prefix:ext;} ...";
1206 assert_int_equal(LY_SUCCESS, parse_deviation(&ctx, &in, &d));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001207 assert_non_null(d);
Michal Vasko63f3d842020-07-08 10:10:14 +02001208 assert_string_equal(" ...", in.current);
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001209 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001210 d = NULL;
1211
1212 /* missing mandatory substatement */
Michal Vasko63f3d842020-07-08 10:10:14 +02001213 in.current = " test {description text;}";
1214 assert_int_equal(LY_EVALID, parse_deviation(&ctx, &in, &d));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001215 logbuf_assert("Missing mandatory keyword \"deviate\" as a child of \"deviation\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001216 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001217 d = NULL;
1218
1219 /* invalid substatement */
Michal Vasko63f3d842020-07-08 10:10:14 +02001220 in.current = " test {deviate not-supported; status obsolete;}";
1221 assert_int_equal(LY_EVALID, parse_deviation(&ctx, &in, &d));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001222 logbuf_assert("Invalid keyword \"status\" as a child of \"deviation\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001223 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001224 d = NULL;
1225
1226#undef TEST_DUP
1227
1228 ly_ctx_destroy(ctx.ctx, NULL);
1229}
1230
1231static void
1232test_deviate(void **state)
1233{
1234 (void) state; /* unused */
1235
Michal Vaskob36053d2020-03-26 15:49:30 +01001236 struct lys_yang_parser_ctx ctx;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001237 struct lysp_deviate *d = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02001238 struct ly_in in = {0};
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001239
Michal Vaskob36053d2020-03-26 15:49:30 +01001240 ctx.format = LYS_IN_YANG;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001241 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1242 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001243 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001244 ctx.line = 1;
1245 ctx.indent = 0;
1246
1247 /* invalid cardinality */
1248#define TEST_DUP(TYPE, MEMBER, VALUE1, VALUE2) \
1249 TEST_DUP_GENERIC(TYPE" {", MEMBER, VALUE1, VALUE2, parse_deviate, \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001250 &d, "1", lysp_deviate_free(ctx.ctx, d); free(d); d = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001251
1252 TEST_DUP("add", "config", "true", "false");
1253 TEST_DUP("replace", "default", "int8", "uint8");
1254 TEST_DUP("add", "mandatory", "true", "false");
1255 TEST_DUP("add", "max-elements", "1", "2");
1256 TEST_DUP("add", "min-elements", "1", "2");
1257 TEST_DUP("replace", "type", "int8", "uint8");
1258 TEST_DUP("add", "units", "kilometers", "miles");
1259
1260 /* full contents */
Michal Vasko63f3d842020-07-08 10:10:14 +02001261 in.current = " not-supported {prefix:ext;} ...";
1262 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &in, &d));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001263 assert_non_null(d);
Michal Vasko63f3d842020-07-08 10:10:14 +02001264 assert_string_equal(" ...", in.current);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001265 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02001266 in.current = " 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;} ...";
1267 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &in, &d));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001268 assert_non_null(d);
Michal Vasko63f3d842020-07-08 10:10:14 +02001269 assert_string_equal(" ...", in.current);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001270 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02001271 in.current = " delete {units meters; must 1; must 2; unique x; unique y; default a; default b; prefix:ext;} ...";
1272 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &in, &d));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001273 assert_non_null(d);
Michal Vasko63f3d842020-07-08 10:10:14 +02001274 assert_string_equal(" ...", in.current);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001275 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02001276 in.current = " replace {type string; units meters; default a; config true; mandatory true; min-elements 1; max-elements 2; prefix:ext;} ...";
1277 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &in, &d));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001278 assert_non_null(d);
Michal Vasko63f3d842020-07-08 10:10:14 +02001279 assert_string_equal(" ...", in.current);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001280 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001281
1282 /* invalid substatements */
1283#define TEST_NOT_SUP(DEV, STMT, VALUE) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001284 in.current = " "DEV" {"STMT" "VALUE";}..."; \
1285 assert_int_equal(LY_EVALID, parse_deviate(&ctx, &in, &d)); \
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001286 logbuf_assert("Deviate \""DEV"\" does not support keyword \""STMT"\". Line number 1."); \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001287 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001288
1289 TEST_NOT_SUP("not-supported", "units", "meters");
1290 TEST_NOT_SUP("not-supported", "must", "1");
1291 TEST_NOT_SUP("not-supported", "unique", "x");
1292 TEST_NOT_SUP("not-supported", "default", "a");
1293 TEST_NOT_SUP("not-supported", "config", "true");
1294 TEST_NOT_SUP("not-supported", "mandatory", "true");
1295 TEST_NOT_SUP("not-supported", "min-elements", "1");
1296 TEST_NOT_SUP("not-supported", "max-elements", "2");
1297 TEST_NOT_SUP("not-supported", "type", "string");
1298 TEST_NOT_SUP("add", "type", "string");
1299 TEST_NOT_SUP("delete", "config", "true");
1300 TEST_NOT_SUP("delete", "mandatory", "true");
1301 TEST_NOT_SUP("delete", "min-elements", "1");
1302 TEST_NOT_SUP("delete", "max-elements", "2");
1303 TEST_NOT_SUP("delete", "type", "string");
1304 TEST_NOT_SUP("replace", "must", "1");
1305 TEST_NOT_SUP("replace", "unique", "a");
1306
Michal Vasko63f3d842020-07-08 10:10:14 +02001307 in.current = " nonsence; ...";
1308 assert_int_equal(LY_EVALID, parse_deviate(&ctx, &in, &d));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001309 logbuf_assert("Invalid value \"nonsence\" of \"deviate\". Line number 1.");
1310 assert_null(d);
1311
1312#undef TEST_NOT_SUP
1313#undef TEST_DUP
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001314
1315 ly_ctx_destroy(ctx.ctx, NULL);
1316}
1317
Radek Krejci8c370832018-11-02 15:10:03 +01001318static void
1319test_container(void **state)
1320{
1321 (void) state; /* unused */
1322
Michal Vaskob36053d2020-03-26 15:49:30 +01001323 struct lys_yang_parser_ctx ctx = {0};
Radek Krejci8c370832018-11-02 15:10:03 +01001324 struct lysp_node_container *c = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02001325 struct ly_in in = {0};
Radek Krejci8c370832018-11-02 15:10:03 +01001326
Michal Vaskob36053d2020-03-26 15:49:30 +01001327 ctx.format = LYS_IN_YANG;
Radek Krejci8c370832018-11-02 15:10:03 +01001328 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1329 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001330 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci8c370832018-11-02 15:10:03 +01001331 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001332 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci8c370832018-11-02 15:10:03 +01001333
1334 /* invalid cardinality */
1335#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001336 in.current = "cont {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1337 assert_int_equal(LY_EVALID, parse_container(&ctx, &in, NULL, (struct lysp_node**)&c)); \
Radek Krejci8c370832018-11-02 15:10:03 +01001338 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001339 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001340
1341 TEST_DUP("config", "true", "false");
1342 TEST_DUP("description", "text1", "text2");
1343 TEST_DUP("presence", "true", "false");
1344 TEST_DUP("reference", "1", "2");
1345 TEST_DUP("status", "current", "obsolete");
1346 TEST_DUP("when", "true", "false");
1347#undef TEST_DUP
1348
1349 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001350 in.current = "cont {action x;anydata any;anyxml anyxml; choice ch;config false;container c;description test;grouping g;if-feature f; leaf l {type string;}"
Radek Krejci313d9902018-11-08 09:42:58 +01001351 "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;} ...";
Michal Vasko63f3d842020-07-08 10:10:14 +02001352 assert_int_equal(LY_SUCCESS, parse_container(&ctx, &in, NULL, (struct lysp_node**)&c));
Radek Krejci8c370832018-11-02 15:10:03 +01001353 assert_non_null(c);
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001354 assert_int_equal(LYS_CONTAINER, c->nodetype);
1355 assert_string_equal("cont", c->name);
Radek Krejci8c370832018-11-02 15:10:03 +01001356 assert_non_null(c->actions);
1357 assert_non_null(c->child);
1358 assert_string_equal("test", c->dsc);
1359 assert_non_null(c->exts);
1360 assert_non_null(c->groupings);
1361 assert_non_null(c->iffeatures);
1362 assert_non_null(c->musts);
1363 assert_non_null(c->notifs);
1364 assert_string_equal("true", c->presence);
1365 assert_string_equal("test", c->ref);
1366 assert_non_null(c->typedefs);
1367 assert_non_null(c->when);
1368 assert_null(c->parent);
1369 assert_null(c->next);
1370 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR, c->flags);
Radek Krejci313d9902018-11-08 09:42:58 +01001371 ly_set_erase(&ctx.tpdfs_nodes, NULL);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001372 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001373
1374 /* invalid */
Michal Vasko63f3d842020-07-08 10:10:14 +02001375 in.current = " cont {augment /root;} ...";
1376 assert_int_equal(LY_EVALID, parse_container(&ctx, &in, NULL, (struct lysp_node**)&c));
Radek Krejci8c370832018-11-02 15:10:03 +01001377 logbuf_assert("Invalid keyword \"augment\" as a child of \"container\". Line number 1.");
Radek Krejci4f28eda2018-11-12 11:46:16 +01001378 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02001379 in.current = " cont {nonsence true;} ...";
1380 assert_int_equal(LY_EVALID, parse_container(&ctx, &in, NULL, (struct lysp_node**)&c));
Radek Krejci8c370832018-11-02 15:10:03 +01001381 logbuf_assert("Invalid character sequence \"nonsence\", expected a keyword. Line number 1.");
Radek Krejci4f28eda2018-11-12 11:46:16 +01001382 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001383
Radek Krejcif538ce52019-03-05 10:46:14 +01001384 ctx.mod_version = 1; /* simulate YANG 1.0 */
Michal Vasko63f3d842020-07-08 10:10:14 +02001385 in.current = " cont {action x;} ...";
1386 assert_int_equal(LY_EVALID, parse_container(&ctx, &in, NULL, (struct lysp_node**)&c));
Radek Krejcif538ce52019-03-05 10:46:14 +01001387 logbuf_assert("Invalid keyword \"action\" as a child of \"container\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
1388 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
1389
Radek Krejci8c370832018-11-02 15:10:03 +01001390 ly_ctx_destroy(ctx.ctx, NULL);
1391}
1392
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001393static void
1394test_leaf(void **state)
1395{
1396 *state = test_leaf;
1397
Michal Vaskob36053d2020-03-26 15:49:30 +01001398 struct lys_yang_parser_ctx ctx = {0};
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001399 struct lysp_node_leaf *l = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02001400 struct ly_in in = {0};
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001401
Michal Vaskob36053d2020-03-26 15:49:30 +01001402 ctx.format = LYS_IN_YANG;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001403 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1404 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001405 ctx.pos_type = LY_VLOG_LINE;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001406 ctx.line = 1;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001407 //ctx.mod->version = 2; /* simulate YANG 1.1 */
1408
1409 /* invalid cardinality */
1410#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001411 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1412 assert_int_equal(LY_EVALID, parse_leaf(&ctx, &in, NULL, (struct lysp_node**)&l)); \
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001413 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1414 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1415
1416 TEST_DUP("config", "true", "false");
1417 TEST_DUP("default", "x", "y");
1418 TEST_DUP("description", "text1", "text2");
1419 TEST_DUP("mandatory", "true", "false");
1420 TEST_DUP("reference", "1", "2");
1421 TEST_DUP("status", "current", "obsolete");
Radek Krejci0e5d8382018-11-28 16:37:53 +01001422 TEST_DUP("type", "int8", "uint8");
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001423 TEST_DUP("units", "text1", "text2");
1424 TEST_DUP("when", "true", "false");
1425#undef TEST_DUP
1426
1427 /* full content - without mandatory which is mutual exclusive with default */
Michal Vasko63f3d842020-07-08 10:10:14 +02001428 in.current = "l {config false;default \"xxx\";description test;if-feature f;"
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001429 "must 'expr';reference test;status current;type string; units yyy;when true;m:ext;} ...";
Michal Vasko63f3d842020-07-08 10:10:14 +02001430 assert_int_equal(LY_SUCCESS, parse_leaf(&ctx, &in, NULL, (struct lysp_node**)&l));
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001431 assert_non_null(l);
1432 assert_int_equal(LYS_LEAF, l->nodetype);
1433 assert_string_equal("l", l->name);
1434 assert_string_equal("test", l->dsc);
1435 assert_string_equal("xxx", l->dflt);
1436 assert_string_equal("yyy", l->units);
1437 assert_string_equal("string", l->type.name);
1438 assert_non_null(l->exts);
1439 assert_non_null(l->iffeatures);
1440 assert_non_null(l->musts);
1441 assert_string_equal("test", l->ref);
1442 assert_non_null(l->when);
1443 assert_null(l->parent);
1444 assert_null(l->next);
1445 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR, l->flags);
1446 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1447
1448 /* full content - now with mandatory */
Michal Vasko63f3d842020-07-08 10:10:14 +02001449 in.current = "l {mandatory true; type string;} ...";
1450 assert_int_equal(LY_SUCCESS, parse_leaf(&ctx, &in, NULL, (struct lysp_node**)&l));
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001451 assert_non_null(l);
1452 assert_int_equal(LYS_LEAF, l->nodetype);
1453 assert_string_equal("l", l->name);
1454 assert_string_equal("string", l->type.name);
1455 assert_int_equal(LYS_MAND_TRUE, l->flags);
1456 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1457
1458 /* invalid */
Michal Vasko63f3d842020-07-08 10:10:14 +02001459 in.current = " l {mandatory true; default xx; type string;} ...";
1460 assert_int_equal(LY_EVALID, parse_leaf(&ctx, &in, NULL, (struct lysp_node**)&l));
Radek Krejcia9026eb2018-12-12 16:04:47 +01001461 logbuf_assert("Invalid combination of keywords \"mandatory\" and \"default\" as substatements of \"leaf\". Line number 1.");
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001462 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1463
Michal Vasko63f3d842020-07-08 10:10:14 +02001464 in.current = " l {description \"missing type\";} ...";
1465 assert_int_equal(LY_EVALID, parse_leaf(&ctx, &in, NULL, (struct lysp_node**)&l));
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001466 logbuf_assert("Missing mandatory keyword \"type\" as a child of \"leaf\". Line number 1.");
1467 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1468
1469 *state = NULL;
1470 ly_ctx_destroy(ctx.ctx, NULL);
1471}
1472
Radek Krejci0e5d8382018-11-28 16:37:53 +01001473static void
1474test_leaflist(void **state)
1475{
1476 *state = test_leaf;
1477
Michal Vaskob36053d2020-03-26 15:49:30 +01001478 struct lys_yang_parser_ctx ctx = {0};
Radek Krejci0e5d8382018-11-28 16:37:53 +01001479 struct lysp_node_leaflist *ll = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02001480 struct ly_in in = {0};
Radek Krejci0e5d8382018-11-28 16:37:53 +01001481
Michal Vaskob36053d2020-03-26 15:49:30 +01001482 ctx.format = LYS_IN_YANG;
Radek Krejci0e5d8382018-11-28 16:37:53 +01001483 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1484 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001485 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci0e5d8382018-11-28 16:37:53 +01001486 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001487 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci0e5d8382018-11-28 16:37:53 +01001488
1489 /* invalid cardinality */
1490#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001491 in.current = "ll {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1492 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &in, NULL, (struct lysp_node**)&ll)); \
Radek Krejci0e5d8382018-11-28 16:37:53 +01001493 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1494 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1495
1496 TEST_DUP("config", "true", "false");
1497 TEST_DUP("description", "text1", "text2");
1498 TEST_DUP("max-elements", "10", "20");
1499 TEST_DUP("min-elements", "10", "20");
1500 TEST_DUP("ordered-by", "user", "system");
1501 TEST_DUP("reference", "1", "2");
1502 TEST_DUP("status", "current", "obsolete");
1503 TEST_DUP("type", "int8", "uint8");
1504 TEST_DUP("units", "text1", "text2");
1505 TEST_DUP("when", "true", "false");
1506#undef TEST_DUP
1507
1508 /* full content - without min-elements which is mutual exclusive with default */
Michal Vasko63f3d842020-07-08 10:10:14 +02001509 in.current = "ll {config false;default \"xxx\"; default \"yyy\";description test;if-feature f;"
Radek Krejci0e5d8382018-11-28 16:37:53 +01001510 "max-elements 10;must 'expr';ordered-by user;reference test;"
1511 "status current;type string; units zzz;when true;m:ext;} ...";
Michal Vasko63f3d842020-07-08 10:10:14 +02001512 assert_int_equal(LY_SUCCESS, parse_leaflist(&ctx, &in, NULL, (struct lysp_node**)&ll));
Radek Krejci0e5d8382018-11-28 16:37:53 +01001513 assert_non_null(ll);
1514 assert_int_equal(LYS_LEAFLIST, ll->nodetype);
1515 assert_string_equal("ll", ll->name);
1516 assert_string_equal("test", ll->dsc);
1517 assert_non_null(ll->dflts);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001518 assert_int_equal(2, LY_ARRAY_COUNT(ll->dflts));
Radek Krejci0e5d8382018-11-28 16:37:53 +01001519 assert_string_equal("xxx", ll->dflts[0]);
1520 assert_string_equal("yyy", ll->dflts[1]);
1521 assert_string_equal("zzz", ll->units);
1522 assert_int_equal(10, ll->max);
1523 assert_int_equal(0, ll->min);
1524 assert_string_equal("string", ll->type.name);
1525 assert_non_null(ll->exts);
1526 assert_non_null(ll->iffeatures);
1527 assert_non_null(ll->musts);
1528 assert_string_equal("test", ll->ref);
1529 assert_non_null(ll->when);
1530 assert_null(ll->parent);
1531 assert_null(ll->next);
1532 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_USER | LYS_SET_MAX, ll->flags);
1533 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1534
1535 /* full content - now with min-elements */
Michal Vasko63f3d842020-07-08 10:10:14 +02001536 in.current = "ll {min-elements 10; type string;} ...";
1537 assert_int_equal(LY_SUCCESS, parse_leaflist(&ctx, &in, NULL, (struct lysp_node**)&ll));
Radek Krejci0e5d8382018-11-28 16:37:53 +01001538 assert_non_null(ll);
1539 assert_int_equal(LYS_LEAFLIST, ll->nodetype);
1540 assert_string_equal("ll", ll->name);
1541 assert_string_equal("string", ll->type.name);
1542 assert_int_equal(0, ll->max);
1543 assert_int_equal(10, ll->min);
1544 assert_int_equal(LYS_SET_MIN, ll->flags);
1545 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1546
1547 /* invalid */
Michal Vasko63f3d842020-07-08 10:10:14 +02001548 in.current = " ll {min-elements 1; default xx; type string;} ...";
1549 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &in, NULL, (struct lysp_node**)&ll));
Radek Krejcia9026eb2018-12-12 16:04:47 +01001550 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 +01001551 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1552
Michal Vasko63f3d842020-07-08 10:10:14 +02001553 in.current = " ll {description \"missing type\";} ...";
1554 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &in, NULL, (struct lysp_node**)&ll));
Radek Krejci0e5d8382018-11-28 16:37:53 +01001555 logbuf_assert("Missing mandatory keyword \"type\" as a child of \"leaf-list\". Line number 1.");
1556 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1557
Michal Vasko63f3d842020-07-08 10:10:14 +02001558 in.current = " ll {type string; min-elements 10; max-elements 1;} ..."; /* invalid combination of min/max */
1559 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &in, NULL, (struct lysp_node**)&ll));
Radek Krejcidf6cad12018-11-28 17:10:55 +01001560 logbuf_assert("Invalid combination of min-elements and max-elements: min value 10 is bigger than the max value 1. Line number 1.");
1561 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1562
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001563 ctx.mod_version = 1; /* simulate YANG 1.0 - default statement is not allowed */
Michal Vasko63f3d842020-07-08 10:10:14 +02001564 in.current = " ll {default xx; type string;} ...";
1565 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &in, NULL, (struct lysp_node**)&ll));
Radek Krejci0e5d8382018-11-28 16:37:53 +01001566 logbuf_assert("Invalid keyword \"default\" as a child of \"leaf-list\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
1567 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1568
1569 *state = NULL;
1570 ly_ctx_destroy(ctx.ctx, NULL);
1571}
1572
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001573static void
1574test_list(void **state)
1575{
1576 *state = test_list;
1577
Michal Vaskob36053d2020-03-26 15:49:30 +01001578 struct lys_yang_parser_ctx ctx = {0};
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001579 struct lysp_node_list *l = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02001580 struct ly_in in = {0};
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001581
Michal Vaskob36053d2020-03-26 15:49:30 +01001582 ctx.format = LYS_IN_YANG;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001583 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1584 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001585 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001586 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001587 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001588
1589 /* invalid cardinality */
1590#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001591 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1592 assert_int_equal(LY_EVALID, parse_list(&ctx, &in, NULL, (struct lysp_node**)&l)); \
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001593 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1594 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1595
1596 TEST_DUP("config", "true", "false");
1597 TEST_DUP("description", "text1", "text2");
1598 TEST_DUP("key", "one", "two");
1599 TEST_DUP("max-elements", "10", "20");
1600 TEST_DUP("min-elements", "10", "20");
1601 TEST_DUP("ordered-by", "user", "system");
1602 TEST_DUP("reference", "1", "2");
1603 TEST_DUP("status", "current", "obsolete");
1604 TEST_DUP("when", "true", "false");
1605#undef TEST_DUP
1606
1607 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001608 in.current = "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;}"
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001609 "leaf-list ll {type string;} list li;max-elements 10; min-elements 1;must 'expr';notification not; ordered-by system; reference test;"
1610 "status current;typedef t {type int8;}unique xxx;unique yyy;uses g;when true;m:ext;} ...";
Michal Vasko63f3d842020-07-08 10:10:14 +02001611 assert_int_equal(LY_SUCCESS, parse_list(&ctx, &in, NULL, (struct lysp_node**)&l));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001612 assert_non_null(l);
1613 assert_int_equal(LYS_LIST, l->nodetype);
1614 assert_string_equal("l", l->name);
1615 assert_string_equal("test", l->dsc);
1616 assert_string_equal("l", l->key);
1617 assert_non_null(l->uniques);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001618 assert_int_equal(2, LY_ARRAY_COUNT(l->uniques));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001619 assert_string_equal("xxx", l->uniques[0]);
1620 assert_string_equal("yyy", l->uniques[1]);
1621 assert_int_equal(10, l->max);
1622 assert_int_equal(1, l->min);
1623 assert_non_null(l->exts);
1624 assert_non_null(l->iffeatures);
1625 assert_non_null(l->musts);
1626 assert_string_equal("test", l->ref);
1627 assert_non_null(l->when);
1628 assert_null(l->parent);
1629 assert_null(l->next);
1630 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM | LYS_SET_MAX | LYS_SET_MIN, l->flags);
1631 ly_set_erase(&ctx.tpdfs_nodes, NULL);
1632 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1633
Radek Krejcif538ce52019-03-05 10:46:14 +01001634 /* invalid content */
1635 ctx.mod_version = 1; /* simulate YANG 1.0 */
Michal Vasko63f3d842020-07-08 10:10:14 +02001636 in.current = "l {action x;} ...";
1637 assert_int_equal(LY_EVALID, parse_list(&ctx, &in, NULL, (struct lysp_node**)&l));
Radek Krejcif538ce52019-03-05 10:46:14 +01001638 logbuf_assert("Invalid keyword \"action\" as a child of \"list\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
1639 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1640
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001641 *state = NULL;
1642 ly_ctx_destroy(ctx.ctx, NULL);
1643}
1644
Radek Krejci056d0a82018-12-06 16:57:25 +01001645static void
1646test_choice(void **state)
1647{
1648 *state = test_choice;
1649
Michal Vaskob36053d2020-03-26 15:49:30 +01001650 struct lys_yang_parser_ctx ctx = {0};
Radek Krejci056d0a82018-12-06 16:57:25 +01001651 struct lysp_node_choice *ch = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02001652 struct ly_in in = {0};
Radek Krejci056d0a82018-12-06 16:57:25 +01001653
Michal Vaskob36053d2020-03-26 15:49:30 +01001654 ctx.format = LYS_IN_YANG;
Radek Krejci056d0a82018-12-06 16:57:25 +01001655 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1656 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001657 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci056d0a82018-12-06 16:57:25 +01001658 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001659 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci056d0a82018-12-06 16:57:25 +01001660
1661 /* invalid cardinality */
1662#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001663 in.current = "ch {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1664 assert_int_equal(LY_EVALID, parse_choice(&ctx, &in, NULL, (struct lysp_node**)&ch)); \
Radek Krejci056d0a82018-12-06 16:57:25 +01001665 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1666 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1667
1668 TEST_DUP("config", "true", "false");
1669 TEST_DUP("default", "a", "b");
1670 TEST_DUP("description", "text1", "text2");
1671 TEST_DUP("mandatory", "true", "false");
1672 TEST_DUP("reference", "1", "2");
1673 TEST_DUP("status", "current", "obsolete");
1674 TEST_DUP("when", "true", "false");
1675#undef TEST_DUP
1676
Radek Krejcia9026eb2018-12-12 16:04:47 +01001677 /* full content - without default due to a collision with mandatory */
Michal Vasko63f3d842020-07-08 10:10:14 +02001678 in.current = "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 +01001679 "leaf-list ll {type string;} list li;mandatory true;reference test;status current;when true;m:ext;} ...";
Michal Vasko63f3d842020-07-08 10:10:14 +02001680 assert_int_equal(LY_SUCCESS, parse_choice(&ctx, &in, NULL, (struct lysp_node**)&ch));
Radek Krejci056d0a82018-12-06 16:57:25 +01001681 assert_non_null(ch);
1682 assert_int_equal(LYS_CHOICE, ch->nodetype);
1683 assert_string_equal("ch", ch->name);
1684 assert_string_equal("test", ch->dsc);
1685 assert_non_null(ch->exts);
1686 assert_non_null(ch->iffeatures);
1687 assert_string_equal("test", ch->ref);
1688 assert_non_null(ch->when);
1689 assert_null(ch->parent);
1690 assert_null(ch->next);
1691 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_MAND_TRUE, ch->flags);
1692 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1693
Radek Krejcia9026eb2018-12-12 16:04:47 +01001694 /* full content - the default missing from the previous node */
Michal Vasko63f3d842020-07-08 10:10:14 +02001695 in.current = "ch {default c;case c;} ...";
1696 assert_int_equal(LY_SUCCESS, parse_choice(&ctx, &in, NULL, (struct lysp_node**)&ch));
Radek Krejcia9026eb2018-12-12 16:04:47 +01001697 assert_non_null(ch);
1698 assert_int_equal(LYS_CHOICE, ch->nodetype);
1699 assert_string_equal("ch", ch->name);
1700 assert_string_equal("c", ch->dflt);
1701 assert_int_equal(0, ch->flags);
1702 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1703
1704 /* invalid content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001705 in.current = "ch {mandatory true; default c1; case c1 {leaf x{type string;}}} ...";
1706 assert_int_equal(LY_EVALID, parse_choice(&ctx, &in, NULL, (struct lysp_node**)&ch));
Radek Krejcia9026eb2018-12-12 16:04:47 +01001707 logbuf_assert("Invalid combination of keywords \"mandatory\" and \"default\" as substatements of \"choice\". Line number 1.");
1708 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1709
1710 *state = NULL;
1711 ly_ctx_destroy(ctx.ctx, NULL);
1712}
1713
1714static void
1715test_case(void **state)
1716{
1717 *state = test_case;
1718
Michal Vaskob36053d2020-03-26 15:49:30 +01001719 struct lys_yang_parser_ctx ctx = {0};
Radek Krejcia9026eb2018-12-12 16:04:47 +01001720 struct lysp_node_case *cs = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02001721 struct ly_in in = {0};
Radek Krejcia9026eb2018-12-12 16:04:47 +01001722
Michal Vaskob36053d2020-03-26 15:49:30 +01001723 ctx.format = LYS_IN_YANG;
Radek Krejcia9026eb2018-12-12 16:04:47 +01001724 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1725 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001726 ctx.pos_type = LY_VLOG_LINE;
Radek Krejcia9026eb2018-12-12 16:04:47 +01001727 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001728 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejcia9026eb2018-12-12 16:04:47 +01001729
1730 /* invalid cardinality */
1731#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001732 in.current = "cs {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1733 assert_int_equal(LY_EVALID, parse_case(&ctx, &in, NULL, (struct lysp_node**)&cs)); \
Radek Krejcia9026eb2018-12-12 16:04:47 +01001734 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1735 lysp_node_free(ctx.ctx, (struct lysp_node*)cs); cs = NULL;
1736
1737 TEST_DUP("description", "text1", "text2");
1738 TEST_DUP("reference", "1", "2");
1739 TEST_DUP("status", "current", "obsolete");
1740 TEST_DUP("when", "true", "false");
1741#undef TEST_DUP
1742
1743 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001744 in.current = "cs {anydata any;anyxml anyxml; choice ch;container c;description test;if-feature f;leaf l {type string;}"
Radek Krejcia9026eb2018-12-12 16:04:47 +01001745 "leaf-list ll {type string;} list li;reference test;status current;uses grp;when true;m:ext;} ...";
Michal Vasko63f3d842020-07-08 10:10:14 +02001746 assert_int_equal(LY_SUCCESS, parse_case(&ctx, &in, NULL, (struct lysp_node**)&cs));
Radek Krejcia9026eb2018-12-12 16:04:47 +01001747 assert_non_null(cs);
1748 assert_int_equal(LYS_CASE, cs->nodetype);
1749 assert_string_equal("cs", cs->name);
1750 assert_string_equal("test", cs->dsc);
1751 assert_non_null(cs->exts);
1752 assert_non_null(cs->iffeatures);
1753 assert_string_equal("test", cs->ref);
1754 assert_non_null(cs->when);
1755 assert_null(cs->parent);
1756 assert_null(cs->next);
1757 assert_int_equal(LYS_STATUS_CURR, cs->flags);
1758 lysp_node_free(ctx.ctx, (struct lysp_node*)cs); cs = NULL;
1759
1760 /* invalid content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001761 in.current = "cs {config true} ...";
1762 assert_int_equal(LY_EVALID, parse_case(&ctx, &in, NULL, (struct lysp_node**)&cs));
Radek Krejcia9026eb2018-12-12 16:04:47 +01001763 logbuf_assert("Invalid keyword \"config\" as a child of \"case\". Line number 1.");
1764 lysp_node_free(ctx.ctx, (struct lysp_node*)cs); cs = NULL;
1765
Radek Krejci056d0a82018-12-06 16:57:25 +01001766 *state = NULL;
1767 ly_ctx_destroy(ctx.ctx, NULL);
1768}
1769
Radek Krejci9800fb82018-12-13 14:26:23 +01001770static void
Radek Krejcid6b76452019-09-03 17:03:03 +02001771test_any(void **state, enum ly_stmt kw)
Radek Krejci9800fb82018-12-13 14:26:23 +01001772{
1773 *state = test_any;
1774
Michal Vaskob36053d2020-03-26 15:49:30 +01001775 struct lys_yang_parser_ctx ctx = {0};
Radek Krejci9800fb82018-12-13 14:26:23 +01001776 struct lysp_node_anydata *any = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02001777 struct ly_in in = {0};
Radek Krejci9800fb82018-12-13 14:26:23 +01001778
Michal Vaskob36053d2020-03-26 15:49:30 +01001779 ctx.format = LYS_IN_YANG;
Radek Krejci9800fb82018-12-13 14:26:23 +01001780 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1781 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001782 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci9800fb82018-12-13 14:26:23 +01001783 ctx.line = 1;
Radek Krejcid6b76452019-09-03 17:03:03 +02001784 if (kw == LY_STMT_ANYDATA) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001785 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci9800fb82018-12-13 14:26:23 +01001786 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001787 ctx.mod_version = 1; /* simulate YANG 1.0 */
Radek Krejci9800fb82018-12-13 14:26:23 +01001788 }
1789
1790 /* invalid cardinality */
1791#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001792 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1793 assert_int_equal(LY_EVALID, parse_any(&ctx, &in, kw, NULL, (struct lysp_node**)&any)); \
Radek Krejci9800fb82018-12-13 14:26:23 +01001794 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1795 lysp_node_free(ctx.ctx, (struct lysp_node*)any); any = NULL;
1796
1797 TEST_DUP("config", "true", "false");
1798 TEST_DUP("description", "text1", "text2");
1799 TEST_DUP("mandatory", "true", "false");
1800 TEST_DUP("reference", "1", "2");
1801 TEST_DUP("status", "current", "obsolete");
1802 TEST_DUP("when", "true", "false");
1803#undef TEST_DUP
1804
1805 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001806 in.current = "any {config true;description test;if-feature f;mandatory true;must 'expr';reference test;status current;when true;m:ext;} ...";
1807 assert_int_equal(LY_SUCCESS, parse_any(&ctx, &in, kw, NULL, (struct lysp_node**)&any));
Radek Krejci9800fb82018-12-13 14:26:23 +01001808 assert_non_null(any);
Radek Krejcid6b76452019-09-03 17:03:03 +02001809 assert_int_equal(kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML, any->nodetype);
Radek Krejci9800fb82018-12-13 14:26:23 +01001810 assert_string_equal("any", any->name);
1811 assert_string_equal("test", any->dsc);
1812 assert_non_null(any->exts);
1813 assert_non_null(any->iffeatures);
1814 assert_non_null(any->musts);
1815 assert_string_equal("test", any->ref);
1816 assert_non_null(any->when);
1817 assert_null(any->parent);
1818 assert_null(any->next);
1819 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR | LYS_MAND_TRUE, any->flags);
1820 lysp_node_free(ctx.ctx, (struct lysp_node*)any); any = NULL;
1821
1822 *state = NULL;
1823 ly_ctx_destroy(ctx.ctx, NULL);
1824}
1825
1826static void
1827test_anydata(void **state)
1828{
Radek Krejcid6b76452019-09-03 17:03:03 +02001829 return test_any(state, LY_STMT_ANYDATA);
Radek Krejci9800fb82018-12-13 14:26:23 +01001830}
1831
1832static void
1833test_anyxml(void **state)
1834{
Radek Krejcid6b76452019-09-03 17:03:03 +02001835 return test_any(state, LY_STMT_ANYXML);
Radek Krejci9800fb82018-12-13 14:26:23 +01001836}
1837
Radek Krejcie86bf772018-12-14 11:39:53 +01001838static void
1839test_grouping(void **state)
1840{
1841 *state = test_grouping;
1842
Michal Vaskob36053d2020-03-26 15:49:30 +01001843 struct lys_yang_parser_ctx ctx = {0};
Radek Krejcie86bf772018-12-14 11:39:53 +01001844 struct lysp_grp *grp = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02001845 struct ly_in in = {0};
Radek Krejcie86bf772018-12-14 11:39:53 +01001846
Michal Vaskob36053d2020-03-26 15:49:30 +01001847 ctx.format = LYS_IN_YANG;
Radek Krejcie86bf772018-12-14 11:39:53 +01001848 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1849 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001850 ctx.pos_type = LY_VLOG_LINE;
Radek Krejcie86bf772018-12-14 11:39:53 +01001851 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001852 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejcie86bf772018-12-14 11:39:53 +01001853
1854 /* invalid cardinality */
1855#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001856 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1857 assert_int_equal(LY_EVALID, parse_grouping(&ctx, &in, NULL, &grp)); \
Radek Krejcie86bf772018-12-14 11:39:53 +01001858 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1859 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1860
1861 TEST_DUP("description", "text1", "text2");
1862 TEST_DUP("reference", "1", "2");
1863 TEST_DUP("status", "current", "obsolete");
1864#undef TEST_DUP
1865
1866 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001867 in.current = "grp {action x;anydata any;anyxml anyxml; choice ch;container c;description test;grouping g;leaf l {type string;}"
Radek Krejcie86bf772018-12-14 11:39:53 +01001868 "leaf-list ll {type string;} list li;notification not;reference test;status current;typedef t {type int8;}uses g;m:ext;} ...";
Michal Vasko63f3d842020-07-08 10:10:14 +02001869 assert_int_equal(LY_SUCCESS, parse_grouping(&ctx, &in, NULL, &grp));
Radek Krejcie86bf772018-12-14 11:39:53 +01001870 assert_non_null(grp);
1871 assert_int_equal(LYS_GROUPING, grp->nodetype);
1872 assert_string_equal("grp", grp->name);
1873 assert_string_equal("test", grp->dsc);
1874 assert_non_null(grp->exts);
1875 assert_string_equal("test", grp->ref);
1876 assert_null(grp->parent);
1877 assert_int_equal( LYS_STATUS_CURR, grp->flags);
1878 ly_set_erase(&ctx.tpdfs_nodes, NULL);
1879 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1880
1881 /* invalid content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001882 in.current = "grp {config true} ...";
1883 assert_int_equal(LY_EVALID, parse_grouping(&ctx, &in, NULL, &grp));
Radek Krejcie86bf772018-12-14 11:39:53 +01001884 logbuf_assert("Invalid keyword \"config\" as a child of \"grouping\". Line number 1.");
1885 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1886
Michal Vasko63f3d842020-07-08 10:10:14 +02001887 in.current = "grp {must 'expr'} ...";
1888 assert_int_equal(LY_EVALID, parse_grouping(&ctx, &in, NULL, &grp));
Radek Krejcie86bf772018-12-14 11:39:53 +01001889 logbuf_assert("Invalid keyword \"must\" as a child of \"grouping\". Line number 1.");
1890 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1891
1892 *state = NULL;
1893 ly_ctx_destroy(ctx.ctx, NULL);
1894}
1895
1896static void
Radek Krejcif538ce52019-03-05 10:46:14 +01001897test_action(void **state)
1898{
1899 *state = test_action;
1900
Michal Vaskob36053d2020-03-26 15:49:30 +01001901 struct lys_yang_parser_ctx ctx = {0};
Radek Krejcif538ce52019-03-05 10:46:14 +01001902 struct lysp_action *rpcs = NULL;
1903 struct lysp_node_container *c = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02001904 struct ly_in in = {0};
Radek Krejcif538ce52019-03-05 10:46:14 +01001905
Michal Vaskob36053d2020-03-26 15:49:30 +01001906 ctx.format = LYS_IN_YANG;
Radek Krejcif538ce52019-03-05 10:46:14 +01001907 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1908 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001909 ctx.pos_type = LY_VLOG_LINE;
Radek Krejcif538ce52019-03-05 10:46:14 +01001910 ctx.line = 1;
1911 ctx.mod_version = 2; /* simulate YANG 1.1 */
1912
1913 /* invalid cardinality */
1914#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001915 in.current = "func {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1916 assert_int_equal(LY_EVALID, parse_action(&ctx, &in, NULL, &rpcs)); \
Radek Krejcif538ce52019-03-05 10:46:14 +01001917 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1918 FREE_ARRAY(ctx.ctx, rpcs, lysp_action_free); rpcs = NULL;
1919
1920 TEST_DUP("description", "text1", "text2");
Michal Vaskob83af8a2020-01-06 09:49:22 +01001921 TEST_DUP("input", "{leaf l1 {type empty;}} description a", "{leaf l2 {type empty;}} description a");
1922 TEST_DUP("output", "{leaf l1 {type empty;}} description a", "{leaf l2 {type empty;}} description a");
Radek Krejcif538ce52019-03-05 10:46:14 +01001923 TEST_DUP("reference", "1", "2");
1924 TEST_DUP("status", "current", "obsolete");
1925#undef TEST_DUP
1926
1927 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001928 in.current = "top;";
1929 assert_int_equal(LY_SUCCESS, parse_container(&ctx, &in, NULL, (struct lysp_node**)&c));
1930 in.current = "func {description test;grouping grp;if-feature f;reference test;status current;typedef mytype {type int8;} m:ext;"
Radek Krejcif538ce52019-03-05 10:46:14 +01001931 "input {anydata a1; anyxml a2; choice ch; container c; grouping grp; leaf l {type int8;} leaf-list ll {type int8;}"
1932 " list li; must 1; typedef mytypei {type int8;} uses grp; m:ext;}"
1933 "output {anydata a1; anyxml a2; choice ch; container c; grouping grp; leaf l {type int8;} leaf-list ll {type int8;}"
1934 " list li; must 1; typedef mytypeo {type int8;} uses grp; m:ext;}} ...";
Michal Vasko63f3d842020-07-08 10:10:14 +02001935 assert_int_equal(LY_SUCCESS, parse_action(&ctx, &in, (struct lysp_node*)c, &rpcs));
Radek Krejcif538ce52019-03-05 10:46:14 +01001936 assert_non_null(rpcs);
1937 assert_int_equal(LYS_ACTION, rpcs->nodetype);
1938 assert_string_equal("func", rpcs->name);
1939 assert_string_equal("test", rpcs->dsc);
1940 assert_non_null(rpcs->exts);
1941 assert_non_null(rpcs->iffeatures);
1942 assert_string_equal("test", rpcs->ref);
1943 assert_non_null(rpcs->groupings);
1944 assert_non_null(rpcs->typedefs);
1945 assert_int_equal(LYS_STATUS_CURR, rpcs->flags);
1946 /* input */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001947 assert_int_equal(rpcs->input.nodetype, LYS_INPUT);
Radek Krejcif538ce52019-03-05 10:46:14 +01001948 assert_non_null(rpcs->input.groupings);
1949 assert_non_null(rpcs->input.exts);
1950 assert_non_null(rpcs->input.musts);
1951 assert_non_null(rpcs->input.typedefs);
1952 assert_non_null(rpcs->input.data);
1953 /* output */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001954 assert_int_equal(rpcs->output.nodetype, LYS_OUTPUT);
Radek Krejcif538ce52019-03-05 10:46:14 +01001955 assert_non_null(rpcs->output.groupings);
1956 assert_non_null(rpcs->output.exts);
1957 assert_non_null(rpcs->output.musts);
1958 assert_non_null(rpcs->output.typedefs);
1959 assert_non_null(rpcs->output.data);
1960
1961 ly_set_erase(&ctx.tpdfs_nodes, NULL);
1962 FREE_ARRAY(ctx.ctx, rpcs, lysp_action_free); rpcs = NULL;
1963
1964 /* invalid content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001965 in.current = "func {config true} ...";
1966 assert_int_equal(LY_EVALID, parse_action(&ctx, &in, NULL, &rpcs));
Radek Krejcif538ce52019-03-05 10:46:14 +01001967 logbuf_assert("Invalid keyword \"config\" as a child of \"rpc\". Line number 1.");
1968 FREE_ARRAY(ctx.ctx, rpcs, lysp_action_free); rpcs = NULL;
1969
1970 *state = NULL;
1971 lysp_node_free(ctx.ctx, (struct lysp_node*)c);
1972 ly_ctx_destroy(ctx.ctx, NULL);
1973}
1974
1975static void
Radek Krejcifc11bd72019-04-11 16:00:05 +02001976test_notification(void **state)
1977{
1978 *state = test_notification;
1979
Michal Vaskob36053d2020-03-26 15:49:30 +01001980 struct lys_yang_parser_ctx ctx = {0};
Radek Krejcifc11bd72019-04-11 16:00:05 +02001981 struct lysp_notif *notifs = NULL;
1982 struct lysp_node_container *c = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02001983 struct ly_in in = {0};
Radek Krejcifc11bd72019-04-11 16:00:05 +02001984
Michal Vaskob36053d2020-03-26 15:49:30 +01001985 ctx.format = LYS_IN_YANG;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001986 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1987 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02001988 ctx.pos_type = LY_VLOG_LINE;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001989 ctx.line = 1;
1990 ctx.mod_version = 2; /* simulate YANG 1.1 */
1991
1992 /* invalid cardinality */
1993#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001994 in.current = "func {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1995 assert_int_equal(LY_EVALID, parse_notif(&ctx, &in, NULL, &notifs)); \
Radek Krejcifc11bd72019-04-11 16:00:05 +02001996 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1997 FREE_ARRAY(ctx.ctx, notifs, lysp_notif_free); notifs = NULL;
1998
1999 TEST_DUP("description", "text1", "text2");
2000 TEST_DUP("reference", "1", "2");
2001 TEST_DUP("status", "current", "obsolete");
2002#undef TEST_DUP
2003
2004 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02002005 in.current = "top;";
2006 assert_int_equal(LY_SUCCESS, parse_container(&ctx, &in, NULL, (struct lysp_node**)&c));
2007 in.current = "ntf {anydata a1; anyxml a2; choice ch; container c; description test; grouping grp; if-feature f; leaf l {type int8;}"
Radek Krejcifc11bd72019-04-11 16:00:05 +02002008 "leaf-list ll {type int8;} list li; must 1; reference test; status current; typedef mytype {type int8;} uses grp; m:ext;}";
Michal Vasko63f3d842020-07-08 10:10:14 +02002009 assert_int_equal(LY_SUCCESS, parse_notif(&ctx, &in, (struct lysp_node*)c, &notifs));
Radek Krejcifc11bd72019-04-11 16:00:05 +02002010 assert_non_null(notifs);
2011 assert_int_equal(LYS_NOTIF, notifs->nodetype);
2012 assert_string_equal("ntf", notifs->name);
2013 assert_string_equal("test", notifs->dsc);
2014 assert_non_null(notifs->exts);
2015 assert_non_null(notifs->iffeatures);
2016 assert_string_equal("test", notifs->ref);
2017 assert_non_null(notifs->groupings);
2018 assert_non_null(notifs->typedefs);
2019 assert_non_null(notifs->musts);
2020 assert_non_null(notifs->data);
2021 assert_int_equal(LYS_STATUS_CURR, notifs->flags);
2022
2023 ly_set_erase(&ctx.tpdfs_nodes, NULL);
2024 FREE_ARRAY(ctx.ctx, notifs, lysp_notif_free); notifs = NULL;
2025
2026 /* invalid content */
Michal Vasko63f3d842020-07-08 10:10:14 +02002027 in.current = "ntf {config true} ...";
2028 assert_int_equal(LY_EVALID, parse_notif(&ctx, &in, NULL, &notifs));
Radek Krejcifc11bd72019-04-11 16:00:05 +02002029 logbuf_assert("Invalid keyword \"config\" as a child of \"notification\". Line number 1.");
2030 FREE_ARRAY(ctx.ctx, notifs, lysp_notif_free); notifs = NULL;
2031
2032 *state = NULL;
2033 lysp_node_free(ctx.ctx, (struct lysp_node*)c);
2034 ly_ctx_destroy(ctx.ctx, NULL);
2035}
2036
2037static void
Radek Krejcie86bf772018-12-14 11:39:53 +01002038test_uses(void **state)
2039{
2040 *state = test_uses;
2041
Michal Vaskob36053d2020-03-26 15:49:30 +01002042 struct lys_yang_parser_ctx ctx = {0};
Radek Krejcie86bf772018-12-14 11:39:53 +01002043 struct lysp_node_uses *u = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02002044 struct ly_in in = {0};
Radek Krejcie86bf772018-12-14 11:39:53 +01002045
Michal Vaskob36053d2020-03-26 15:49:30 +01002046 ctx.format = LYS_IN_YANG;
Radek Krejcie86bf772018-12-14 11:39:53 +01002047 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
2048 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02002049 ctx.pos_type = LY_VLOG_LINE;
Radek Krejcie86bf772018-12-14 11:39:53 +01002050 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002051 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejcie86bf772018-12-14 11:39:53 +01002052
2053 /* invalid cardinality */
2054#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02002055 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
2056 assert_int_equal(LY_EVALID, parse_uses(&ctx, &in, NULL, (struct lysp_node**)&u)); \
Radek Krejcie86bf772018-12-14 11:39:53 +01002057 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
2058 lysp_node_free(ctx.ctx, (struct lysp_node*)u); u = NULL;
2059
2060 TEST_DUP("description", "text1", "text2");
2061 TEST_DUP("reference", "1", "2");
2062 TEST_DUP("status", "current", "obsolete");
2063 TEST_DUP("when", "true", "false");
2064#undef TEST_DUP
2065
2066 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02002067 in.current = "grpref {augment some/node;description test;if-feature f;reference test;refine some/other/node;status current;when true;m:ext;} ...";
2068 assert_int_equal(LY_SUCCESS, parse_uses(&ctx, &in, NULL, (struct lysp_node**)&u));
Radek Krejcie86bf772018-12-14 11:39:53 +01002069 assert_non_null(u);
2070 assert_int_equal(LYS_USES, u->nodetype);
2071 assert_string_equal("grpref", u->name);
2072 assert_string_equal("test", u->dsc);
2073 assert_non_null(u->exts);
2074 assert_non_null(u->iffeatures);
2075 assert_string_equal("test", u->ref);
2076 assert_non_null(u->augments);
2077 assert_non_null(u->refines);
2078 assert_non_null(u->when);
2079 assert_null(u->parent);
2080 assert_null(u->next);
2081 assert_int_equal(LYS_STATUS_CURR, u->flags);
2082 lysp_node_free(ctx.ctx, (struct lysp_node*)u); u = NULL;
2083
2084 *state = NULL;
2085 ly_ctx_destroy(ctx.ctx, NULL);
2086}
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002087
2088
2089static void
2090test_augment(void **state)
2091{
2092 *state = test_augment;
2093
Michal Vaskob36053d2020-03-26 15:49:30 +01002094 struct lys_yang_parser_ctx ctx = {0};
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002095 struct lysp_augment *a = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02002096 struct ly_in in = {0};
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002097
Michal Vaskob36053d2020-03-26 15:49:30 +01002098 ctx.format = LYS_IN_YANG;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002099 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
2100 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02002101 ctx.pos_type = LY_VLOG_LINE;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002102 ctx.line = 1;
Radek Krejcib4adbf12019-02-25 13:35:22 +01002103 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002104
2105 /* invalid cardinality */
2106#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02002107 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
2108 assert_int_equal(LY_EVALID, parse_augment(&ctx, &in, NULL, &a)); \
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002109 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
Radek Krejcib4adbf12019-02-25 13:35:22 +01002110 FREE_ARRAY(ctx.ctx, a, lysp_augment_free); a = NULL;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002111
2112 TEST_DUP("description", "text1", "text2");
2113 TEST_DUP("reference", "1", "2");
2114 TEST_DUP("status", "current", "obsolete");
2115 TEST_DUP("when", "true", "false");
2116#undef TEST_DUP
2117
2118 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02002119 in.current = "/target/nodeid {action x; anydata any;anyxml anyxml; case cs; choice ch;container c;description test;if-feature f;leaf l {type string;}"
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002120 "leaf-list ll {type string;} list li;notification not;reference test;status current;uses g;when true;m:ext;} ...";
Michal Vasko63f3d842020-07-08 10:10:14 +02002121 assert_int_equal(LY_SUCCESS, parse_augment(&ctx, &in, NULL, &a));
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002122 assert_non_null(a);
2123 assert_int_equal(LYS_AUGMENT, a->nodetype);
2124 assert_string_equal("/target/nodeid", a->nodeid);
2125 assert_string_equal("test", a->dsc);
2126 assert_non_null(a->exts);
2127 assert_non_null(a->iffeatures);
2128 assert_string_equal("test", a->ref);
2129 assert_non_null(a->when);
2130 assert_null(a->parent);
2131 assert_int_equal(LYS_STATUS_CURR, a->flags);
Radek Krejcib4adbf12019-02-25 13:35:22 +01002132 FREE_ARRAY(ctx.ctx, a, lysp_augment_free); a = NULL;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002133
2134 *state = NULL;
2135 ly_ctx_destroy(ctx.ctx, NULL);
2136}
2137
Radek Krejcif09e4e82019-06-14 15:08:11 +02002138static void
2139test_when(void **state)
2140{
2141 *state = test_when;
2142
Michal Vaskob36053d2020-03-26 15:49:30 +01002143 struct lys_yang_parser_ctx ctx = {0};
Radek Krejcif09e4e82019-06-14 15:08:11 +02002144 struct lysp_when *w = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02002145 struct ly_in in = {0};
Radek Krejcif09e4e82019-06-14 15:08:11 +02002146
Michal Vaskob36053d2020-03-26 15:49:30 +01002147 ctx.format = LYS_IN_YANG;
Radek Krejcif09e4e82019-06-14 15:08:11 +02002148 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
2149 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02002150 ctx.pos_type = LY_VLOG_LINE;
Radek Krejcif09e4e82019-06-14 15:08:11 +02002151 ctx.line = 1;
2152 ctx.mod_version = 2; /* simulate YANG 1.1 */
2153
2154 /* invalid cardinality */
2155#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02002156 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
2157 assert_int_equal(LY_EVALID, parse_when(&ctx, &in, &w)); \
Radek Krejcif09e4e82019-06-14 15:08:11 +02002158 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
2159 FREE_MEMBER(ctx.ctx, w, lysp_when_free); w = NULL;
2160
2161 TEST_DUP("description", "text1", "text2");
2162 TEST_DUP("reference", "1", "2");
2163#undef TEST_DUP
2164
2165 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02002166 in.current = "expression {description test;reference test;m:ext;} ...";
2167 assert_int_equal(LY_SUCCESS, parse_when(&ctx, &in, &w));
Radek Krejcif09e4e82019-06-14 15:08:11 +02002168 assert_non_null(w);
2169 assert_string_equal("expression", w->cond);
2170 assert_string_equal("test", w->dsc);
2171 assert_string_equal("test", w->ref);
2172 assert_non_null(w->exts);
2173 FREE_MEMBER(ctx.ctx, w, lysp_when_free); w = NULL;
2174
2175 /* empty condition */
Michal Vasko63f3d842020-07-08 10:10:14 +02002176 in.current = "\"\";";
2177 assert_int_equal(LY_SUCCESS, parse_when(&ctx, &in, &w));
Radek Krejcif09e4e82019-06-14 15:08:11 +02002178 logbuf_assert("Empty argument of when statement does not make sense.");
2179 assert_non_null(w);
2180 assert_string_equal("", w->cond);
2181 FREE_MEMBER(ctx.ctx, w, lysp_when_free); w = NULL;
2182
2183 *state = NULL;
2184 ly_ctx_destroy(ctx.ctx, NULL);
2185}
2186
David Sedlákd6ce6d72019-07-16 17:30:18 +02002187static void
2188test_value(void **state)
2189{
2190 *state = test_value;
Michal Vaskob36053d2020-03-26 15:49:30 +01002191 struct lys_yang_parser_ctx ctx;
Michal Vasko63f3d842020-07-08 10:10:14 +02002192 struct ly_in in = {0};
David Sedlákd6ce6d72019-07-16 17:30:18 +02002193
Michal Vaskob36053d2020-03-26 15:49:30 +01002194 ctx.format = LYS_IN_YANG;
David Sedlákd6ce6d72019-07-16 17:30:18 +02002195 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
2196 assert_non_null(ctx.ctx);
Radek Krejci99435242019-09-05 16:19:15 +02002197 ctx.pos_type = LY_VLOG_LINE;
David Sedlákd6ce6d72019-07-16 17:30:18 +02002198 ctx.line = 1;
2199 ctx.indent = 0;
2200 int64_t val = 0;
2201 uint16_t flags = 0;
2202
Michal Vasko63f3d842020-07-08 10:10:14 +02002203 in.current = "-0;";
2204 assert_int_equal(parse_type_enum_value_pos(&ctx, &in, LY_STMT_VALUE, &val, &flags, NULL), LY_SUCCESS);
David Sedlákd6ce6d72019-07-16 17:30:18 +02002205 assert_int_equal(val, 0);
2206
Michal Vasko63f3d842020-07-08 10:10:14 +02002207 in.current = "-0;";
David Sedlákd6ce6d72019-07-16 17:30:18 +02002208 flags = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +02002209 assert_int_equal(parse_type_enum_value_pos(&ctx, &in, LY_STMT_POSITION, &val, &flags, NULL), LY_EVALID);
David Sedlákd6ce6d72019-07-16 17:30:18 +02002210 logbuf_assert("Invalid value \"-0\" of \"position\". Line number 1.");
2211
David Sedlák1af868e2019-07-17 17:03:14 +02002212 *state = NULL;
David Sedlákd6ce6d72019-07-16 17:30:18 +02002213 ly_ctx_destroy(ctx.ctx, NULL);
2214}
2215
Radek Krejci80dd33e2018-09-26 15:57:18 +02002216int main(void)
2217{
2218 const struct CMUnitTest tests[] = {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002219 cmocka_unit_test_setup(test_helpers, logger_setup),
Radek Krejci80dd33e2018-09-26 15:57:18 +02002220 cmocka_unit_test_setup(test_comments, logger_setup),
Radek Krejciefd22f62018-09-27 11:47:58 +02002221 cmocka_unit_test_setup(test_arg, logger_setup),
Radek Krejcidcc7b322018-10-11 14:24:02 +02002222 cmocka_unit_test_setup(test_stmts, logger_setup),
Radek Krejci05b13982018-11-28 16:22:07 +01002223 cmocka_unit_test_setup_teardown(test_minmax, logger_setup, logger_teardown),
Radek Krejci40544fa2019-01-11 09:38:37 +01002224 cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown),
Radek Krejci2c02f3e2018-10-16 10:54:38 +02002225 cmocka_unit_test_setup(test_deviation, logger_setup),
2226 cmocka_unit_test_setup(test_deviate, logger_setup),
Radek Krejci8c370832018-11-02 15:10:03 +01002227 cmocka_unit_test_setup(test_container, logger_setup),
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002228 cmocka_unit_test_setup_teardown(test_leaf, logger_setup, logger_teardown),
Radek Krejci0e5d8382018-11-28 16:37:53 +01002229 cmocka_unit_test_setup_teardown(test_leaflist, logger_setup, logger_teardown),
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002230 cmocka_unit_test_setup_teardown(test_list, logger_setup, logger_teardown),
Radek Krejci056d0a82018-12-06 16:57:25 +01002231 cmocka_unit_test_setup_teardown(test_choice, logger_setup, logger_teardown),
Radek Krejcia9026eb2018-12-12 16:04:47 +01002232 cmocka_unit_test_setup_teardown(test_case, logger_setup, logger_teardown),
Radek Krejci9800fb82018-12-13 14:26:23 +01002233 cmocka_unit_test_setup_teardown(test_anydata, logger_setup, logger_teardown),
2234 cmocka_unit_test_setup_teardown(test_anyxml, logger_setup, logger_teardown),
Radek Krejcif538ce52019-03-05 10:46:14 +01002235 cmocka_unit_test_setup_teardown(test_action, logger_setup, logger_teardown),
Radek Krejcifc11bd72019-04-11 16:00:05 +02002236 cmocka_unit_test_setup_teardown(test_notification, logger_setup, logger_teardown),
Radek Krejcie86bf772018-12-14 11:39:53 +01002237 cmocka_unit_test_setup_teardown(test_grouping, logger_setup, logger_teardown),
2238 cmocka_unit_test_setup_teardown(test_uses, logger_setup, logger_teardown),
Radek Krejcib4adbf12019-02-25 13:35:22 +01002239 cmocka_unit_test_setup_teardown(test_augment, logger_setup, logger_teardown),
Radek Krejcif09e4e82019-06-14 15:08:11 +02002240 cmocka_unit_test_setup_teardown(test_when, logger_setup, logger_teardown),
David Sedlákd6ce6d72019-07-16 17:30:18 +02002241 cmocka_unit_test_setup_teardown(test_value, logger_setup, logger_teardown),
Radek Krejci80dd33e2018-09-26 15:57:18 +02002242 };
2243
2244 return cmocka_run_group_tests(tests, NULL, NULL);
2245}