blob: 0abb8211320a20d3e9bb9e9077b437e94be47e26 [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 *
Radek Iša56ca9e42020-09-08 18:42:00 +02006 * Copyright (c) 2018-2020 CESNET, z.s.p.o.
Radek Krejci80dd33e2018-09-26 15:57:18 +02007 *
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 */
Radek Iša56ca9e42020-09-08 18:42:00 +020014#define _UTEST_MAIN_
15#include "utests.h"
Radek Krejci80dd33e2018-09-26 15:57:18 +020016
Radek Krejci80dd33e2018-09-26 15:57:18 +020017#include <stdio.h>
18#include <string.h>
19
Radek Krejci70593c12020-06-13 20:48:09 +020020#include "common.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020021#include "in_internal.h"
Radek Krejci70593c12020-06-13 20:48:09 +020022#include "parser_internal.h"
Michal Vasko405cc9e2020-12-01 12:01:27 +010023#include "schema_compile.h"
Radek Krejci70593c12020-06-13 20:48:09 +020024#include "tree_schema.h"
25#include "tree_schema_internal.h"
Radek Krejci2d7a47b2019-05-16 13:34:10 +020026
27/* originally static functions from tree_schema_free.c and parser_yang.c */
28void lysp_ext_instance_free(struct ly_ctx *ctx, struct lysp_ext_instance *ext);
Radek Krejci2d7a47b2019-05-16 13:34:10 +020029void lysp_deviation_free(struct ly_ctx *ctx, struct lysp_deviation *dev);
Radek Krejci2a9fc652021-01-22 17:44:34 +010030void lysp_grp_free(struct ly_ctx *ctx, struct lysp_node_grp *grp);
31void lysp_augment_free(struct ly_ctx *ctx, struct lysp_node_augment *augment);
Radek Krejci2d7a47b2019-05-16 13:34:10 +020032void lysp_deviate_free(struct ly_ctx *ctx, struct lysp_deviate *d);
33void lysp_node_free(struct ly_ctx *ctx, struct lysp_node *node);
Radek Krejcif09e4e82019-06-14 15:08:11 +020034void lysp_when_free(struct ly_ctx *ctx, struct lysp_when *when);
Radek Krejci2d7a47b2019-05-16 13:34:10 +020035
Michal Vasko63f3d842020-07-08 10:10:14 +020036LY_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);
Radek Krejci33090f92020-12-17 20:12:46 +010037LY_ERR buf_store_char(struct lys_yang_parser_ctx *ctx, enum yang_arg arg, char **word_p,
Radek Krejcib4ac5a92020-11-23 17:54:33 +010038 size_t *word_len, char **word_b, size_t *buf_len, uint8_t need_buf, uint8_t *prefix);
Radek Krejci33090f92020-12-17 20:12:46 +010039LY_ERR get_keyword(struct lys_yang_parser_ctx *ctx, enum ly_stmt *kw, char **word_p, size_t *word_len);
40LY_ERR get_argument(struct lys_yang_parser_ctx *ctx, enum yang_arg arg,
Radek Krejcib4ac5a92020-11-23 17:54:33 +010041 uint16_t *flags, char **word_p, char **word_b, size_t *word_len);
Radek Krejci33090f92020-12-17 20:12:46 +010042LY_ERR skip_comment(struct lys_yang_parser_ctx *ctx, uint8_t comment);
Radek Krejci2d7a47b2019-05-16 13:34:10 +020043
Radek Krejci2a9fc652021-01-22 17:44:34 +010044LY_ERR parse_action(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_action **actions);
Radek Krejci33090f92020-12-17 20:12:46 +010045LY_ERR parse_any(struct lys_yang_parser_ctx *ctx, enum ly_stmt kw, struct lysp_node *parent, struct lysp_node **siblings);
Radek Krejci2a9fc652021-01-22 17:44:34 +010046LY_ERR parse_augment(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_augment **augments);
Radek Krejci33090f92020-12-17 20:12:46 +010047LY_ERR parse_case(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
48LY_ERR parse_container(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
49LY_ERR parse_deviate(struct lys_yang_parser_ctx *ctx, struct lysp_deviate **deviates);
50LY_ERR parse_deviation(struct lys_yang_parser_ctx *ctx, struct lysp_deviation **deviations);
Radek Krejci2a9fc652021-01-22 17:44:34 +010051LY_ERR parse_grouping(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_grp **groupings);
Radek Krejci33090f92020-12-17 20:12:46 +010052LY_ERR parse_choice(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
53LY_ERR parse_leaf(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
54LY_ERR parse_leaflist(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
55LY_ERR parse_list(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
56LY_ERR parse_maxelements(struct lys_yang_parser_ctx *ctx, uint32_t *max, uint16_t *flags, struct lysp_ext_instance **exts);
57LY_ERR parse_minelements(struct lys_yang_parser_ctx *ctx, uint32_t *min, uint16_t *flags, struct lysp_ext_instance **exts);
58LY_ERR parse_module(struct lys_yang_parser_ctx *ctx, struct lysp_module *mod);
Radek Krejci2a9fc652021-01-22 17:44:34 +010059LY_ERR parse_notif(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_notif **notifs);
Radek Krejci33090f92020-12-17 20:12:46 +010060LY_ERR parse_submodule(struct lys_yang_parser_ctx *ctx, struct lysp_submodule *submod);
61LY_ERR parse_uses(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
62LY_ERR parse_when(struct lys_yang_parser_ctx *ctx, struct lysp_when **when_p);
63LY_ERR parse_type_enum_value_pos(struct lys_yang_parser_ctx *ctx, enum ly_stmt val_kw, int64_t *value, uint16_t *flags, struct lysp_ext_instance **exts);
Radek Krejci80dd33e2018-09-26 15:57:18 +020064
Radek Iša56ca9e42020-09-08 18:42:00 +020065struct lys_yang_parser_ctx *YCTX;
Radek Krejci80dd33e2018-09-26 15:57:18 +020066
Radek Krejci1640e802021-01-08 11:01:30 +010067struct ly_in in = {0};
68
Radek Krejci33090f92020-12-17 20:12:46 +010069#define YCTX_INIT \
Radek Krejcid54412f2020-12-17 20:25:35 +010070 in.line = 1; \
Radek Krejci2efc45b2020-12-22 16:25:44 +010071 YCTX->in = &in; \
72 LOG_LOCINIT(UTEST_LYCTX, NULL, NULL, NULL, &in)
Radek Krejci33090f92020-12-17 20:12:46 +010073
Michal Vasko5d24f6c2020-10-13 13:49:06 +020074static int
Radek Iša56ca9e42020-09-08 18:42:00 +020075setup(void **state)
Michal Vasko5d24f6c2020-10-13 13:49:06 +020076{
Radek Iša56ca9e42020-09-08 18:42:00 +020077 UTEST_SETUP;
Michal Vasko5d24f6c2020-10-13 13:49:06 +020078
Radek Iša56ca9e42020-09-08 18:42:00 +020079 /* allocate parser context */
80 YCTX = calloc(1, sizeof(*YCTX));
81 YCTX->format = LYS_IN_YANG;
Michal Vasko5d24f6c2020-10-13 13:49:06 +020082
Radek Iša56ca9e42020-09-08 18:42:00 +020083 /* allocate new parsed module */
84 YCTX->parsed_mod = calloc(1, sizeof *YCTX->parsed_mod);
Michal Vasko5d24f6c2020-10-13 13:49:06 +020085
Radek Iša56ca9e42020-09-08 18:42:00 +020086 /* allocate new module */
87 YCTX->parsed_mod->mod = calloc(1, sizeof *YCTX->parsed_mod->mod);
88 YCTX->parsed_mod->mod->ctx = UTEST_LYCTX;
89 YCTX->parsed_mod->mod->parsed = YCTX->parsed_mod;
Michal Vasko5d24f6c2020-10-13 13:49:06 +020090
Radek Krejci1640e802021-01-08 11:01:30 +010091 /* initilize and use the global easily available and customizable input handler */
92 in.line = 1;
93 YCTX->in = &in;
Radek Krejciddace2c2021-01-08 11:30:56 +010094 LOG_LOCINIT(NULL, NULL, NULL, &in);
Radek Krejci1640e802021-01-08 11:01:30 +010095
Michal Vasko5d24f6c2020-10-13 13:49:06 +020096 return 0;
97}
98
99static int
Radek Iša56ca9e42020-09-08 18:42:00 +0200100teardown(void **state)
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200101{
Radek Iša56ca9e42020-09-08 18:42:00 +0200102 lys_module_free(YCTX->parsed_mod->mod, NULL);
Radek Krejciddace2c2021-01-08 11:30:56 +0100103 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejci1640e802021-01-08 11:01:30 +0100104
Radek Iša56ca9e42020-09-08 18:42:00 +0200105 free(YCTX);
106 YCTX = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200107
Radek Iša56ca9e42020-09-08 18:42:00 +0200108 UTEST_TEARDOWN;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200109
110 return 0;
111}
112
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200113#define TEST_DUP_GENERIC(PREFIX, MEMBER, VALUE1, VALUE2, FUNC, RESULT, LINE, CLEANUP) \
Michal Vasko63f3d842020-07-08 10:10:14 +0200114 in.current = PREFIX MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +0100115 assert_int_equal(LY_EVALID, FUNC(YCTX, RESULT)); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200116 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number "LINE".");\
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200117 CLEANUP
Radek Krejci44ceedc2018-10-02 15:54:31 +0200118static void
119test_helpers(void **state)
120{
Radek Krejci404251e2018-10-09 12:06:44 +0200121 char *buf, *p;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200122 size_t len, size;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200123 uint8_t prefix = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200124
125 /* storing into buffer */
Michal Vasko63f3d842020-07-08 10:10:14 +0200126 in.current = "abcd";
Radek Krejci44ceedc2018-10-02 15:54:31 +0200127 buf = NULL;
128 size = len = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200129 assert_int_equal(LY_SUCCESS, buf_add_char(NULL, &in, 2, &buf, &size, &len));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200130 assert_int_not_equal(0, size);
131 assert_int_equal(2, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200132 assert_string_equal("cd", in.current);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200133 assert_false(strncmp("ab", buf, 2));
134 free(buf);
Radek Krejci404251e2018-10-09 12:06:44 +0200135 buf = NULL;
136
137 /* invalid first characters */
138 len = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200139 in.current = "2invalid";
Radek Krejci33090f92020-12-17 20:12:46 +0100140 assert_int_equal(LY_EVALID, buf_store_char(YCTX, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
Michal Vasko63f3d842020-07-08 10:10:14 +0200141 in.current = ".invalid";
Radek Krejci33090f92020-12-17 20:12:46 +0100142 assert_int_equal(LY_EVALID, buf_store_char(YCTX, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
Michal Vasko63f3d842020-07-08 10:10:14 +0200143 in.current = "-invalid";
Radek Krejci33090f92020-12-17 20:12:46 +0100144 assert_int_equal(LY_EVALID, buf_store_char(YCTX, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
Radek Krejci404251e2018-10-09 12:06:44 +0200145 /* invalid following characters */
146 len = 3; /* number of characters read before the str content */
Michal Vasko63f3d842020-07-08 10:10:14 +0200147 in.current = "!";
Radek Krejci33090f92020-12-17 20:12:46 +0100148 assert_int_equal(LY_EVALID, buf_store_char(YCTX, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
Michal Vasko63f3d842020-07-08 10:10:14 +0200149 in.current = ":";
Radek Krejci33090f92020-12-17 20:12:46 +0100150 assert_int_equal(LY_EVALID, buf_store_char(YCTX, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
Radek Krejci404251e2018-10-09 12:06:44 +0200151 /* valid colon for prefixed identifiers */
152 len = size = 0;
153 p = NULL;
David Sedlák40bb13b2019-07-10 14:34:18 +0200154 prefix = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200155 in.current = "x:id";
Radek Krejci33090f92020-12-17 20:12:46 +0100156 assert_int_equal(LY_SUCCESS, buf_store_char(YCTX, Y_PREF_IDENTIF_ARG, &p, &len, &buf, &size, 0, &prefix));
Radek Krejci404251e2018-10-09 12:06:44 +0200157 assert_int_equal(1, len);
158 assert_null(buf);
Michal Vasko63f3d842020-07-08 10:10:14 +0200159 assert_string_equal(":id", in.current);
Radek Krejci404251e2018-10-09 12:06:44 +0200160 assert_int_equal('x', p[len - 1]);
Radek Krejci33090f92020-12-17 20:12:46 +0100161 assert_int_equal(LY_SUCCESS, buf_store_char(YCTX, Y_PREF_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
Radek Krejci404251e2018-10-09 12:06:44 +0200162 assert_int_equal(2, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200163 assert_string_equal("id", in.current);
Radek Krejci404251e2018-10-09 12:06:44 +0200164 assert_int_equal(':', p[len - 1]);
165 free(buf);
David Sedlák40bb13b2019-07-10 14:34:18 +0200166 prefix = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200167
168 /* checking identifiers */
Radek Iša56ca9e42020-09-08 18:42:00 +0200169 assert_int_equal(LY_EVALID, lysp_check_identifierchar((struct lys_parser_ctx *)YCTX, ':', 0, NULL));
170 CHECK_LOG_CTX("Invalid identifier character ':' (0x003a).", "Line number 1.");
171 assert_int_equal(LY_EVALID, lysp_check_identifierchar((struct lys_parser_ctx *)YCTX, '#', 1, NULL));
172 CHECK_LOG_CTX("Invalid identifier first character '#' (0x0023).", "Line number 1.");
Radek Krejci44ceedc2018-10-02 15:54:31 +0200173
Radek Iša56ca9e42020-09-08 18:42:00 +0200174 assert_int_equal(LY_SUCCESS, lysp_check_identifierchar((struct lys_parser_ctx *)YCTX, 'a', 1, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200175 assert_int_equal(0, prefix);
Radek Iša56ca9e42020-09-08 18:42:00 +0200176 assert_int_equal(LY_SUCCESS, lysp_check_identifierchar((struct lys_parser_ctx *)YCTX, ':', 0, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200177 assert_int_equal(1, prefix);
Radek Iša56ca9e42020-09-08 18:42:00 +0200178 assert_int_equal(LY_EVALID, lysp_check_identifierchar((struct lys_parser_ctx *)YCTX, ':', 0, &prefix));
Radek Krejcidcc7b322018-10-11 14:24:02 +0200179 assert_int_equal(1, prefix);
Radek Iša56ca9e42020-09-08 18:42:00 +0200180 assert_int_equal(LY_SUCCESS, lysp_check_identifierchar((struct lys_parser_ctx *)YCTX, 'b', 0, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200181 assert_int_equal(2, prefix);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200182 /* second colon is invalid */
Radek Iša56ca9e42020-09-08 18:42:00 +0200183 assert_int_equal(LY_EVALID, lysp_check_identifierchar((struct lys_parser_ctx *)YCTX, ':', 0, &prefix));
184 CHECK_LOG_CTX("Invalid identifier character ':' (0x003a).", "Line number 1.");
Radek Krejci44ceedc2018-10-02 15:54:31 +0200185}
Radek Krejci80dd33e2018-09-26 15:57:18 +0200186
Radek Krejcidd713ce2021-01-04 23:12:12 +0100187#define TEST_GET_ARGUMENT_SUCCESS(INPUT_TEXT, CTX, ARG_TYPE, EXPECT_WORD, EXPECT_LEN, EXPECT_CURRENT, EXPECT_LINE)\
Radek Iša56ca9e42020-09-08 18:42:00 +0200188 {\
Radek Krejcidd713ce2021-01-04 23:12:12 +0100189 const char * text = INPUT_TEXT;\
190 in.line = 1;\
Radek Iša56ca9e42020-09-08 18:42:00 +0200191 in.current = text;\
Radek Krejci33090f92020-12-17 20:12:46 +0100192 assert_int_equal(LY_SUCCESS, get_argument(CTX, Y_MAYBE_STR_ARG, NULL, &word, &buf, &len));\
Radek Iša56ca9e42020-09-08 18:42:00 +0200193 assert_string_equal(word, EXPECT_WORD);\
194 assert_int_equal(len, EXPECT_LEN);\
195 assert_string_equal(EXPECT_CURRENT, in.current);\
Radek Krejcidd713ce2021-01-04 23:12:12 +0100196 assert_int_equal(EXPECT_LINE, in.line);\
Radek Iša56ca9e42020-09-08 18:42:00 +0200197 }
198
Radek Krejci80dd33e2018-09-26 15:57:18 +0200199static void
200test_comments(void **state)
201{
Radek Krejciefd22f62018-09-27 11:47:58 +0200202 char *word, *buf;
203 size_t len;
Radek Krejci80dd33e2018-09-26 15:57:18 +0200204
Radek Krejcidd713ce2021-01-04 23:12:12 +0100205 TEST_GET_ARGUMENT_SUCCESS(" // this is a text of / one * line */ comment\nargument;",
206 YCTX, Y_STR_ARG, "argument;", 8, ";", 2);
Radek Krejciefd22f62018-09-27 11:47:58 +0200207 assert_null(buf);
Radek Krejci80dd33e2018-09-26 15:57:18 +0200208
Radek Krejcidd713ce2021-01-04 23:12:12 +0100209 TEST_GET_ARGUMENT_SUCCESS("/* this is a \n * text // of / block * comment */\"arg\" + \"ume\" \n + \n \"nt\";",
210 YCTX, Y_STR_ARG, "argument", 8, ";", 4);
Radek Krejciefd22f62018-09-27 11:47:58 +0200211 assert_ptr_equal(buf, word);
Radek Krejciefd22f62018-09-27 11:47:58 +0200212 free(word);
Radek Krejci80dd33e2018-09-26 15:57:18 +0200213
Radek Krejcidd713ce2021-01-04 23:12:12 +0100214 in.line = 1;
Michal Vasko63f3d842020-07-08 10:10:14 +0200215 in.current = " this is one line comment on last line";
Radek Krejci33090f92020-12-17 20:12:46 +0100216 assert_int_equal(LY_SUCCESS, skip_comment(YCTX, 1));
Michal Vasko63f3d842020-07-08 10:10:14 +0200217 assert_true(in.current[0] == '\0');
Radek Krejci80dd33e2018-09-26 15:57:18 +0200218
Radek Krejcidd713ce2021-01-04 23:12:12 +0100219 in.line = 1;
Michal Vasko63f3d842020-07-08 10:10:14 +0200220 in.current = " this is a not terminated comment x";
Radek Krejci33090f92020-12-17 20:12:46 +0100221 assert_int_equal(LY_EVALID, skip_comment(YCTX, 2));
Radek Krejcidd713ce2021-01-04 23:12:12 +0100222 CHECK_LOG_CTX("Unexpected end-of-input, non-terminated comment.", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200223 assert_true(in.current[0] == '\0');
Radek Krejci80dd33e2018-09-26 15:57:18 +0200224}
225
Radek Krejciefd22f62018-09-27 11:47:58 +0200226static void
227test_arg(void **state)
228{
Radek Krejciefd22f62018-09-27 11:47:58 +0200229 char *word, *buf;
230 size_t len;
231
232 /* missing argument */
Michal Vasko63f3d842020-07-08 10:10:14 +0200233 in.current = ";";
Radek Krejci33090f92020-12-17 20:12:46 +0100234 assert_int_equal(LY_SUCCESS, get_argument(YCTX, Y_MAYBE_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200235 assert_null(word);
236
Michal Vasko63f3d842020-07-08 10:10:14 +0200237 in.current = "{";
Radek Krejci33090f92020-12-17 20:12:46 +0100238 assert_int_equal(LY_EVALID, get_argument(YCTX, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200239 CHECK_LOG_CTX("Invalid character sequence \"{\", expected an argument.", "Line number 1.");
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200240
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200241 /* invalid escape sequence */
Michal Vasko63f3d842020-07-08 10:10:14 +0200242 in.current = "\"\\s\"";
Radek Krejci33090f92020-12-17 20:12:46 +0100243 assert_int_equal(LY_EVALID, get_argument(YCTX, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200244 CHECK_LOG_CTX("Double-quoted string unknown special character \'\\s\'.", "Line number 1.");
245
Radek Krejcidd713ce2021-01-04 23:12:12 +0100246 TEST_GET_ARGUMENT_SUCCESS("\'\\s\'", YCTX, Y_STR_ARG, "\\s\'", 2, "", 1);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200247
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200248 /* invalid character after the argument */
Michal Vasko63f3d842020-07-08 10:10:14 +0200249 in.current = "hello\"";
Radek Krejci33090f92020-12-17 20:12:46 +0100250 assert_int_equal(LY_EVALID, get_argument(YCTX, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200251 CHECK_LOG_CTX("Invalid character sequence \"\"\", expected unquoted string character, optsep, semicolon or opening brace.", "Line number 1.");
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200252
Radek Iša56ca9e42020-09-08 18:42:00 +0200253 in.current = "hello}";
Radek Krejci33090f92020-12-17 20:12:46 +0100254 assert_int_equal(LY_EVALID, get_argument(YCTX, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200255 CHECK_LOG_CTX("Invalid character sequence \"}\", expected unquoted string character, optsep, semicolon or opening brace.", "Line number 1.");
David Sedlák40bb13b2019-07-10 14:34:18 +0200256 /* invalid identifier-ref-arg-str */
Michal Vasko63f3d842020-07-08 10:10:14 +0200257 in.current = "pre:pre:value";
Radek Krejci33090f92020-12-17 20:12:46 +0100258 assert_int_equal(LY_EVALID, get_argument(YCTX, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200259 CHECK_LOG_CTX("Invalid identifier character ':' (0x003a).", "Line number 1.");
David Sedlák40bb13b2019-07-10 14:34:18 +0200260
Michal Vasko63f3d842020-07-08 10:10:14 +0200261 in.current = "\"\";"; /* empty identifier is not allowed */
Radek Krejci33090f92020-12-17 20:12:46 +0100262 assert_int_equal(LY_EVALID, get_argument(YCTX, Y_IDENTIF_ARG, NULL, &word, &buf, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200263 CHECK_LOG_CTX("Statement argument is required.", "Line number 1.");
264
Michal Vasko63f3d842020-07-08 10:10:14 +0200265 in.current = "\"\";"; /* empty reference identifier is not allowed */
Radek Krejci33090f92020-12-17 20:12:46 +0100266 assert_int_equal(LY_EVALID, get_argument(YCTX, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200267 CHECK_LOG_CTX("Statement argument is required.", "Line number 1.");
Radek Krejci4e199f52019-05-28 09:09:28 +0200268
Radek Iša56ca9e42020-09-08 18:42:00 +0200269 /* slash is not an invalid character */
Radek Krejcidd713ce2021-01-04 23:12:12 +0100270 TEST_GET_ARGUMENT_SUCCESS("hello/x\t", YCTX, Y_STR_ARG, "hello/x\t", 7, "\t", 1);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200271 assert_null(buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200272
273 /* different quoting */
Radek Krejcidd713ce2021-01-04 23:12:12 +0100274 TEST_GET_ARGUMENT_SUCCESS("hello/x\t", YCTX, Y_STR_ARG, "hello/x\t", 7, "\t", 1);
Radek Krejciefd22f62018-09-27 11:47:58 +0200275
Radek Krejcidd713ce2021-01-04 23:12:12 +0100276 TEST_GET_ARGUMENT_SUCCESS("hello ", YCTX, Y_STR_ARG, "hello ", 5, " ", 1);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200277
Radek Krejcidd713ce2021-01-04 23:12:12 +0100278 TEST_GET_ARGUMENT_SUCCESS("hello/*comment*/\n", YCTX, Y_STR_ARG, "hello/*comment*/\n", 5, "\n", 1);
Radek Iša56ca9e42020-09-08 18:42:00 +0200279
Radek Krejcidd713ce2021-01-04 23:12:12 +0100280 TEST_GET_ARGUMENT_SUCCESS("\"hello\\n\\t\\\"\\\\\";", YCTX, Y_STR_ARG, "hello\n\t\"\\", 9, ";", 1);
fredgand49fe112019-10-21 20:51:50 +0800281 free(buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200282
Radek Iša56ca9e42020-09-08 18:42:00 +0200283 YCTX->indent = 14;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200284 /* - space and tabs before newline are stripped out
285 * - space and tabs after newline (indentation) are stripped out
286 */
Radek Krejcidd713ce2021-01-04 23:12:12 +0100287 TEST_GET_ARGUMENT_SUCCESS("\"hello \t\n\t\t world!\"", YCTX, Y_STR_ARG, "hello\n world!", 14, "", 2);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200288 free(buf);
Radek Iša56ca9e42020-09-08 18:42:00 +0200289
290/* In contrast to previous, the backslash-escaped tabs are expanded after trimming, so they are preserved */
291 YCTX->indent = 14;
Radek Krejcidd713ce2021-01-04 23:12:12 +0100292 TEST_GET_ARGUMENT_SUCCESS("\"hello \\t\n\t\\t world!\"", YCTX, Y_STR_ARG, "hello \t\n\t world!", 16, "", 2);
Radek Krejciff13cd12019-10-25 15:34:24 +0200293 assert_ptr_equal(word, buf);
Radek Krejciff13cd12019-10-25 15:34:24 +0200294 free(buf);
Radek Iša56ca9e42020-09-08 18:42:00 +0200295
Radek Krejciff13cd12019-10-25 15:34:24 +0200296 /* Do not handle whitespaces after backslash-escaped newline as indentation */
Radek Iša56ca9e42020-09-08 18:42:00 +0200297 YCTX->indent = 14;
Radek Krejcidd713ce2021-01-04 23:12:12 +0100298 TEST_GET_ARGUMENT_SUCCESS("\"hello\\n\t\t world!\"", YCTX, Y_STR_ARG, "hello\n\t\t world!", 15, "", 1);
Radek Krejciff13cd12019-10-25 15:34:24 +0200299 assert_ptr_equal(word, buf);
Radek Krejciff13cd12019-10-25 15:34:24 +0200300 free(buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200301
Radek Iša56ca9e42020-09-08 18:42:00 +0200302 YCTX->indent = 14;
Radek Krejcidd713ce2021-01-04 23:12:12 +0100303 TEST_GET_ARGUMENT_SUCCESS("\"hello\n \tworld!\"", YCTX, Y_STR_ARG, "hello\nworld!", 12, "", 2);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200304 assert_ptr_equal(word, buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200305 free(buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200306
Radek Krejcidd713ce2021-01-04 23:12:12 +0100307 TEST_GET_ARGUMENT_SUCCESS("\'hello\'", YCTX, Y_STR_ARG, "hello'", 5, "", 1);
Radek Krejciefd22f62018-09-27 11:47:58 +0200308
Radek Krejcidd713ce2021-01-04 23:12:12 +0100309 TEST_GET_ARGUMENT_SUCCESS("\"hel\" +\t\n\"lo\"", YCTX, Y_STR_ARG, "hello", 5, "", 2);
Radek Krejciefd22f62018-09-27 11:47:58 +0200310 assert_ptr_equal(word, buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200311 free(buf);
Radek Iša56ca9e42020-09-08 18:42:00 +0200312
Radek Krejcidd713ce2021-01-04 23:12:12 +0100313 in.line = 1;
Michal Vasko63f3d842020-07-08 10:10:14 +0200314 in.current = "\"hel\" +\t\nlo"; /* unquoted the second part */
Radek Krejci33090f92020-12-17 20:12:46 +0100315 assert_int_equal(LY_EVALID, get_argument(YCTX, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcidd713ce2021-01-04 23:12:12 +0100316 CHECK_LOG_CTX("Both string parts divided by '+' must be quoted.", "Line number 2.");
Radek Krejciefd22f62018-09-27 11:47:58 +0200317
Radek Krejcidd713ce2021-01-04 23:12:12 +0100318 TEST_GET_ARGUMENT_SUCCESS("\'he\'\t\n+ \"llo\"", YCTX, Y_STR_ARG, "hello", 5, "", 2);
Radek Krejciefd22f62018-09-27 11:47:58 +0200319 free(buf);
320
Radek Krejcidd713ce2021-01-04 23:12:12 +0100321 TEST_GET_ARGUMENT_SUCCESS(" \t\n\"he\"+\'llo\'", YCTX, Y_STR_ARG, "hello", 5, "", 2);
Radek Krejciefd22f62018-09-27 11:47:58 +0200322 free(buf);
323
Radek Krejci44ceedc2018-10-02 15:54:31 +0200324 /* missing argument */
Radek Krejcidd713ce2021-01-04 23:12:12 +0100325 in.line = 1;
Michal Vasko63f3d842020-07-08 10:10:14 +0200326 in.current = ";";
Radek Krejci33090f92020-12-17 20:12:46 +0100327 assert_int_equal(LY_EVALID, get_argument(YCTX, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcidd713ce2021-01-04 23:12:12 +0100328 CHECK_LOG_CTX("Invalid character sequence \";\", expected an argument.", "Line number 1.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200329}
330
Radek Iša56ca9e42020-09-08 18:42:00 +0200331#define TEST_STMS_SUCCESS(INPUT_TEXT, CTX, ACTION, EXPECT_WORD)\
332 in.current = INPUT_TEXT;\
Radek Krejci33090f92020-12-17 20:12:46 +0100333 assert_int_equal(LY_SUCCESS, get_keyword(CTX, &kw, &word, &len));\
Radek Iša56ca9e42020-09-08 18:42:00 +0200334 assert_int_equal(ACTION, kw);\
335 assert_int_equal(strlen(EXPECT_WORD), len);\
336 assert_true(0 == strncmp(EXPECT_WORD, word, len))
337
Radek Krejcidcc7b322018-10-11 14:24:02 +0200338static void
339test_stmts(void **state)
340{
Michal Vasko63f3d842020-07-08 10:10:14 +0200341 const char *p;
Radek Krejcid6b76452019-09-03 17:03:03 +0200342 enum ly_stmt kw;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200343 char *word;
344 size_t len;
345
Michal Vasko63f3d842020-07-08 10:10:14 +0200346 in.current = "\n// comment\n\tinput\t{";
Radek Krejci33090f92020-12-17 20:12:46 +0100347 assert_int_equal(LY_SUCCESS, get_keyword(YCTX, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200348 assert_int_equal(LY_STMT_INPUT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200349 assert_int_equal(5, len);
350 assert_string_equal("input\t{", word);
Michal Vasko63f3d842020-07-08 10:10:14 +0200351 assert_string_equal("\t{", in.current);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200352
Michal Vasko63f3d842020-07-08 10:10:14 +0200353 in.current = "\t /* comment */\t output\n\t{";
Radek Krejci33090f92020-12-17 20:12:46 +0100354 assert_int_equal(LY_SUCCESS, get_keyword(YCTX, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200355 assert_int_equal(LY_STMT_OUTPUT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200356 assert_int_equal(6, len);
357 assert_string_equal("output\n\t{", word);
Michal Vasko63f3d842020-07-08 10:10:14 +0200358 assert_string_equal("\n\t{", in.current);
Radek Krejci33090f92020-12-17 20:12:46 +0100359 assert_int_equal(LY_SUCCESS, get_keyword(YCTX, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200360 assert_int_equal(LY_STMT_SYNTAX_LEFT_BRACE, kw);
Radek Krejciabdd8062019-06-11 16:44:19 +0200361 assert_int_equal(1, len);
362 assert_string_equal("{", word);
Michal Vasko63f3d842020-07-08 10:10:14 +0200363 assert_string_equal("", in.current);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200364
Michal Vasko63f3d842020-07-08 10:10:14 +0200365 in.current = "/input { "; /* invalid slash */
Radek Krejci33090f92020-12-17 20:12:46 +0100366 assert_int_equal(LY_EVALID, get_keyword(YCTX, &kw, &word, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200367 CHECK_LOG_CTX("Invalid identifier first character '/'.", "Line number 4.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200368
Michal Vasko63f3d842020-07-08 10:10:14 +0200369 in.current = "not-a-statement-nor-extension { "; /* invalid identifier */
Radek Krejci33090f92020-12-17 20:12:46 +0100370 assert_int_equal(LY_EVALID, get_keyword(YCTX, &kw, &word, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200371 CHECK_LOG_CTX("Invalid character sequence \"not-a-statement-nor-extension\", expected a keyword.", "Line number 4.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200372
Michal Vasko63f3d842020-07-08 10:10:14 +0200373 in.current = "path;"; /* missing sep after the keyword */
Radek Krejci33090f92020-12-17 20:12:46 +0100374 assert_int_equal(LY_EVALID, get_keyword(YCTX, &kw, &word, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200375 CHECK_LOG_CTX("Invalid character sequence \"path;\", expected a keyword followed by a separator.", "Line number 4.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200376
Radek Iša56ca9e42020-09-08 18:42:00 +0200377 TEST_STMS_SUCCESS("action ", YCTX, LY_STMT_ACTION, "action");
378
379 TEST_STMS_SUCCESS("anydata ", YCTX, LY_STMT_ANYDATA, "anydata");
380 TEST_STMS_SUCCESS("anyxml ", YCTX, LY_STMT_ANYXML, "anyxml");
381 TEST_STMS_SUCCESS("argument ", YCTX, LY_STMT_ARGUMENT, "argument");
382 TEST_STMS_SUCCESS("augment ", YCTX, LY_STMT_AUGMENT, "augment");
383 TEST_STMS_SUCCESS("base ", YCTX, LY_STMT_BASE, "base");
384 TEST_STMS_SUCCESS("belongs-to ", YCTX, LY_STMT_BELONGS_TO, "belongs-to");
385 TEST_STMS_SUCCESS("bit ", YCTX, LY_STMT_BIT, "bit");
386 TEST_STMS_SUCCESS("case ", YCTX, LY_STMT_CASE, "case");
387 TEST_STMS_SUCCESS("choice ", YCTX, LY_STMT_CHOICE, "choice");
388 TEST_STMS_SUCCESS("config ", YCTX, LY_STMT_CONFIG, "config");
389 TEST_STMS_SUCCESS("contact ", YCTX, LY_STMT_CONTACT, "contact");
390 TEST_STMS_SUCCESS("container ", YCTX, LY_STMT_CONTAINER, "container");
391 TEST_STMS_SUCCESS("default ", YCTX, LY_STMT_DEFAULT, "default");
392 TEST_STMS_SUCCESS("description ", YCTX, LY_STMT_DESCRIPTION, "description");
393 TEST_STMS_SUCCESS("deviate ", YCTX, LY_STMT_DEVIATE, "deviate");
394 TEST_STMS_SUCCESS("deviation ", YCTX, LY_STMT_DEVIATION, "deviation");
395 TEST_STMS_SUCCESS("enum ", YCTX, LY_STMT_ENUM, "enum");
396 TEST_STMS_SUCCESS("error-app-tag ", YCTX, LY_STMT_ERROR_APP_TAG, "error-app-tag");
397 TEST_STMS_SUCCESS("error-message ", YCTX, LY_STMT_ERROR_MESSAGE, "error-message");
398 TEST_STMS_SUCCESS("extension ", YCTX, LY_STMT_EXTENSION, "extension");
399 TEST_STMS_SUCCESS("feature ", YCTX, LY_STMT_FEATURE, "feature");
400 TEST_STMS_SUCCESS("fraction-digits ", YCTX, LY_STMT_FRACTION_DIGITS, "fraction-digits");
401 TEST_STMS_SUCCESS("grouping ", YCTX, LY_STMT_GROUPING, "grouping");
402 TEST_STMS_SUCCESS("identity ", YCTX, LY_STMT_IDENTITY, "identity");
403 TEST_STMS_SUCCESS("if-feature ", YCTX, LY_STMT_IF_FEATURE, "if-feature");
404 TEST_STMS_SUCCESS("import ", YCTX, LY_STMT_IMPORT, "import");
405 TEST_STMS_SUCCESS("include ", YCTX, LY_STMT_INCLUDE, "include");
406 TEST_STMS_SUCCESS("input{", YCTX, LY_STMT_INPUT, "input");
407 TEST_STMS_SUCCESS("key ", YCTX, LY_STMT_KEY, "key");
408 TEST_STMS_SUCCESS("leaf ", YCTX, LY_STMT_LEAF, "leaf");
409 TEST_STMS_SUCCESS("leaf-list ", YCTX, LY_STMT_LEAF_LIST, "leaf-list");
410 TEST_STMS_SUCCESS("length ", YCTX, LY_STMT_LENGTH, "length");
411 TEST_STMS_SUCCESS("list ", YCTX, LY_STMT_LIST, "list");
412 TEST_STMS_SUCCESS("mandatory ", YCTX, LY_STMT_MANDATORY, "mandatory");
413 TEST_STMS_SUCCESS("max-elements ", YCTX, LY_STMT_MAX_ELEMENTS, "max-elements");
414 TEST_STMS_SUCCESS("min-elements ", YCTX, LY_STMT_MIN_ELEMENTS, "min-elements");
415 TEST_STMS_SUCCESS("modifier ", YCTX, LY_STMT_MODIFIER, "modifier");
416 TEST_STMS_SUCCESS("module ", YCTX, LY_STMT_MODULE, "module");
417 TEST_STMS_SUCCESS("must ", YCTX, LY_STMT_MUST, "must");
418 TEST_STMS_SUCCESS("namespace ", YCTX, LY_STMT_NAMESPACE, "namespace");
419 TEST_STMS_SUCCESS("notification ", YCTX, LY_STMT_NOTIFICATION, "notification");
420 TEST_STMS_SUCCESS("ordered-by ", YCTX, LY_STMT_ORDERED_BY, "ordered-by");
421 TEST_STMS_SUCCESS("organization ", YCTX, LY_STMT_ORGANIZATION, "organization");
422 TEST_STMS_SUCCESS("output ", YCTX, LY_STMT_OUTPUT, "output");
423 TEST_STMS_SUCCESS("path ", YCTX, LY_STMT_PATH, "path");
424 TEST_STMS_SUCCESS("pattern ", YCTX, LY_STMT_PATTERN, "pattern");
425 TEST_STMS_SUCCESS("position ", YCTX, LY_STMT_POSITION, "position");
426 TEST_STMS_SUCCESS("prefix ", YCTX, LY_STMT_PREFIX, "prefix");
427 TEST_STMS_SUCCESS("presence ", YCTX, LY_STMT_PRESENCE, "presence");
428 TEST_STMS_SUCCESS("range ", YCTX, LY_STMT_RANGE, "range");
429 TEST_STMS_SUCCESS("reference ", YCTX, LY_STMT_REFERENCE, "reference");
430 TEST_STMS_SUCCESS("refine ", YCTX, LY_STMT_REFINE, "refine");
431 TEST_STMS_SUCCESS("require-instance ", YCTX, LY_STMT_REQUIRE_INSTANCE, "require-instance");
432 TEST_STMS_SUCCESS("revision ", YCTX, LY_STMT_REVISION, "revision");
433 TEST_STMS_SUCCESS("revision-date ", YCTX, LY_STMT_REVISION_DATE, "revision-date");
434 TEST_STMS_SUCCESS("rpc ", YCTX, LY_STMT_RPC, "rpc");
435 TEST_STMS_SUCCESS("status ", YCTX, LY_STMT_STATUS, "status");
436 TEST_STMS_SUCCESS("submodule ", YCTX, LY_STMT_SUBMODULE, "submodule");
437 TEST_STMS_SUCCESS("type ", YCTX, LY_STMT_TYPE, "type");
438 TEST_STMS_SUCCESS("typedef ", YCTX, LY_STMT_TYPEDEF, "typedef");
439 TEST_STMS_SUCCESS("unique ", YCTX, LY_STMT_UNIQUE, "unique");
440 TEST_STMS_SUCCESS("units ", YCTX, LY_STMT_UNITS, "units");
441 TEST_STMS_SUCCESS("uses ", YCTX, LY_STMT_USES, "uses");
442 TEST_STMS_SUCCESS("value ", YCTX, LY_STMT_VALUE, "value");
443 TEST_STMS_SUCCESS("when ", YCTX, LY_STMT_WHEN, "when");
444 TEST_STMS_SUCCESS("yang-version ", YCTX, LY_STMT_YANG_VERSION, "yang-version");
445 TEST_STMS_SUCCESS("yin-element ", YCTX, LY_STMT_YIN_ELEMENT, "yin-element");
446 TEST_STMS_SUCCESS(";config false;", YCTX, LY_STMT_SYNTAX_SEMICOLON, ";");
Michal Vasko63f3d842020-07-08 10:10:14 +0200447 assert_string_equal("config false;", in.current);
Radek Iša56ca9e42020-09-08 18:42:00 +0200448 TEST_STMS_SUCCESS("{ config false;", YCTX, LY_STMT_SYNTAX_LEFT_BRACE, "{");
Michal Vasko63f3d842020-07-08 10:10:14 +0200449 assert_string_equal(" config false;", in.current);
Radek Iša56ca9e42020-09-08 18:42:00 +0200450 TEST_STMS_SUCCESS("}", YCTX, LY_STMT_SYNTAX_RIGHT_BRACE, "}");
Michal Vasko63f3d842020-07-08 10:10:14 +0200451 assert_string_equal("", in.current);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200452
453 /* geenric extension */
Michal Vasko63f3d842020-07-08 10:10:14 +0200454 in.current = p = "nacm:default-deny-write;";
Radek Krejci33090f92020-12-17 20:12:46 +0100455 assert_int_equal(LY_SUCCESS, get_keyword(YCTX, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200456 assert_int_equal(LY_STMT_EXTENSION_INSTANCE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200457 assert_int_equal(23, len);
458 assert_ptr_equal(p, word);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200459}
Radek Krejci44ceedc2018-10-02 15:54:31 +0200460
Radek Iša56ca9e42020-09-08 18:42:00 +0200461#define TEST_MINMAX_SUCCESS(INPUT_TEXT, CTX, TYPE, VALUE)\
462 in.current = INPUT_TEXT;\
463 if(TYPE == LYS_SET_MIN){\
Radek Krejci33090f92020-12-17 20:12:46 +0100464 assert_int_equal(LY_SUCCESS, parse_minelements(CTX, &value, &flags, &ext));\
Radek Iša56ca9e42020-09-08 18:42:00 +0200465 }\
466 if(TYPE == LYS_SET_MAX){\
Radek Krejci33090f92020-12-17 20:12:46 +0100467 assert_int_equal(LY_SUCCESS, parse_maxelements(CTX, &value, &flags, &ext));\
Radek Iša56ca9e42020-09-08 18:42:00 +0200468 }\
469 assert_int_equal(TYPE, flags);\
470 assert_int_equal(VALUE, value)
471
Radek Krejci05b13982018-11-28 16:22:07 +0100472static void
473test_minmax(void **state)
474{
Radek Krejci05b13982018-11-28 16:22:07 +0100475 uint16_t flags = 0;
476 uint32_t value = 0;
477 struct lysp_ext_instance *ext = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +0100478
Radek Iša56ca9e42020-09-08 18:42:00 +0200479 YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejci05b13982018-11-28 16:22:07 +0100480
Michal Vasko63f3d842020-07-08 10:10:14 +0200481 in.current = " 1invalid; ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100482 assert_int_equal(LY_EVALID, parse_minelements(YCTX, &value, &flags, &ext));
Radek Iša56ca9e42020-09-08 18:42:00 +0200483 CHECK_LOG_CTX("Invalid value \"1invalid\" of \"min-elements\".", "Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100484
485 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200486 in.current = " -1; ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100487 assert_int_equal(LY_EVALID, parse_minelements(YCTX, &value, &flags, &ext));
Radek Iša56ca9e42020-09-08 18:42:00 +0200488 CHECK_LOG_CTX("Invalid value \"-1\" of \"min-elements\".", "Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100489
Radek Krejcidf6cad12018-11-28 17:10:55 +0100490 /* implementation limit */
491 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200492 in.current = " 4294967296; ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100493 assert_int_equal(LY_EVALID, parse_minelements(YCTX, &value, &flags, &ext));
Radek Iša56ca9e42020-09-08 18:42:00 +0200494 CHECK_LOG_CTX("Value \"4294967296\" is out of \"min-elements\" bounds.", "Line number 1.");
Radek Krejcidf6cad12018-11-28 17:10:55 +0100495
Radek Krejci05b13982018-11-28 16:22:07 +0100496 flags = value = 0;
Radek Iša56ca9e42020-09-08 18:42:00 +0200497 TEST_MINMAX_SUCCESS(" 1; ...", YCTX, LYS_SET_MIN, 1);
Radek Krejci05b13982018-11-28 16:22:07 +0100498
499 flags = value = 0;
Radek Iša56ca9e42020-09-08 18:42:00 +0200500 TEST_MINMAX_SUCCESS(" 1 {m:ext;} ...", YCTX, LYS_SET_MIN, 1);
Radek Krejci05b13982018-11-28 16:22:07 +0100501 assert_non_null(ext);
Radek Iša56ca9e42020-09-08 18:42:00 +0200502 FREE_ARRAY(YCTX->parsed_mod->mod->ctx, ext, lysp_ext_instance_free);
Radek Krejci05b13982018-11-28 16:22:07 +0100503 ext = NULL;
504
505 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200506 in.current = " 1 {config true;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100507 assert_int_equal(LY_EVALID, parse_minelements(YCTX, &value, &flags, &ext));
Radek Iša56ca9e42020-09-08 18:42:00 +0200508 CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"min-elements\".", "Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100509
Michal Vasko63f3d842020-07-08 10:10:14 +0200510 in.current = " 1invalid; ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100511 assert_int_equal(LY_EVALID, parse_maxelements(YCTX, &value, &flags, &ext));
Radek Iša56ca9e42020-09-08 18:42:00 +0200512 CHECK_LOG_CTX("Invalid value \"1invalid\" of \"max-elements\".", "Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100513
514 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200515 in.current = " -1; ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100516 assert_int_equal(LY_EVALID, parse_maxelements(YCTX, &value, &flags, &ext));
Radek Iša56ca9e42020-09-08 18:42:00 +0200517 CHECK_LOG_CTX("Invalid value \"-1\" of \"max-elements\".", "Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100518
Radek Krejcidf6cad12018-11-28 17:10:55 +0100519 /* implementation limit */
520 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200521 in.current = " 4294967296; ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100522 assert_int_equal(LY_EVALID, parse_maxelements(YCTX, &value, &flags, &ext));
Radek Iša56ca9e42020-09-08 18:42:00 +0200523 CHECK_LOG_CTX("Value \"4294967296\" is out of \"max-elements\" bounds.", "Line number 1.");
Radek Krejcidf6cad12018-11-28 17:10:55 +0100524
Radek Krejci05b13982018-11-28 16:22:07 +0100525 flags = value = 0;
Radek Iša56ca9e42020-09-08 18:42:00 +0200526 TEST_MINMAX_SUCCESS(" 1; ...", YCTX, LYS_SET_MAX, 1);
Radek Krejci05b13982018-11-28 16:22:07 +0100527
528 flags = value = 0;
Radek Iša56ca9e42020-09-08 18:42:00 +0200529 TEST_MINMAX_SUCCESS(" unbounded; ...", YCTX, LYS_SET_MAX, 0);
Radek Krejci05b13982018-11-28 16:22:07 +0100530
531 flags = value = 0;
Radek Iša56ca9e42020-09-08 18:42:00 +0200532 TEST_MINMAX_SUCCESS(" 1 {m:ext;} ...", YCTX, LYS_SET_MAX, 1);
Radek Krejci05b13982018-11-28 16:22:07 +0100533 assert_non_null(ext);
Radek Iša56ca9e42020-09-08 18:42:00 +0200534 FREE_ARRAY(YCTX->parsed_mod->mod->ctx, ext, lysp_ext_instance_free);
Radek Krejci05b13982018-11-28 16:22:07 +0100535 ext = NULL;
536
537 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200538 in.current = " 1 {config true;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100539 assert_int_equal(LY_EVALID, parse_maxelements(YCTX, &value, &flags, &ext));
Radek Iša56ca9e42020-09-08 18:42:00 +0200540 CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"max-elements\".", "Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100541}
542
Radek Krejci9fcacc12018-10-11 15:59:11 +0200543static struct lysp_module *
Michal Vaskob36053d2020-03-26 15:49:30 +0100544mod_renew(struct lys_yang_parser_ctx *ctx)
Radek Krejci9fcacc12018-10-11 15:59:11 +0200545{
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200546 struct ly_ctx *ly_ctx = ctx->parsed_mod->mod->ctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100547
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200548 lys_module_free(ctx->parsed_mod->mod, NULL);
549 ctx->parsed_mod = calloc(1, sizeof *ctx->parsed_mod);
550 ctx->parsed_mod->mod = calloc(1, sizeof *ctx->parsed_mod->mod);
551 ctx->parsed_mod->mod->parsed = ctx->parsed_mod;
552 ctx->parsed_mod->mod->ctx = ly_ctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100553
Radek Krejci2efc45b2020-12-22 16:25:44 +0100554 ctx->in->line = 1;
555
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200556 return ctx->parsed_mod;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100557}
558
559static struct lysp_submodule *
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200560submod_renew(struct lys_yang_parser_ctx *ctx)
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100561{
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200562 struct ly_ctx *ly_ctx = ctx->parsed_mod->mod->ctx;
563
564 lys_module_free(ctx->parsed_mod->mod, NULL);
565 ctx->parsed_mod = calloc(1, sizeof(struct lysp_submodule));
566 ctx->parsed_mod->mod = calloc(1, sizeof *ctx->parsed_mod->mod);
567 lydict_insert(ly_ctx, "name", 0, &ctx->parsed_mod->mod->name);
568 ctx->parsed_mod->mod->parsed = ctx->parsed_mod;
569 ctx->parsed_mod->mod->ctx = ly_ctx;
570
571 return (struct lysp_submodule *)ctx->parsed_mod;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200572}
573
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100574static LY_ERR
575test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
576 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
577 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
Radek Krejcid33273d2018-10-25 14:55:52 +0200578{
579 *module_data = user_data;
580 *format = LYS_IN_YANG;
581 *free_module_data = NULL;
582 return LY_SUCCESS;
583}
584
Radek Krejci9fcacc12018-10-11 15:59:11 +0200585static void
586test_module(void **state)
587{
Radek Krejci40544fa2019-01-11 09:38:37 +0100588 struct lysp_module *mod = NULL;
589 struct lysp_submodule *submod = NULL;
590 struct lys_module *m;
Michal Vasko405cc9e2020-12-01 12:01:27 +0100591 struct lys_glob_unres unres = {0};
Radek Iša56ca9e42020-09-08 18:42:00 +0200592 struct lys_yang_parser_ctx *ctx_p;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200593
Radek Iša56ca9e42020-09-08 18:42:00 +0200594 mod = mod_renew(YCTX);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200595
596 /* missing mandatory substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +0200597 in.current = " name {}";
Radek Krejci33090f92020-12-17 20:12:46 +0100598 assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100599 assert_string_equal("name", mod->mod->name);
Radek Iša56ca9e42020-09-08 18:42:00 +0200600 CHECK_LOG_CTX("Missing mandatory keyword \"namespace\" as a child of \"module\".", "Line number 1.");
Radek Krejci9fcacc12018-10-11 15:59:11 +0200601
Radek Iša56ca9e42020-09-08 18:42:00 +0200602 mod = mod_renew(YCTX);
Michal Vasko63f3d842020-07-08 10:10:14 +0200603 in.current = " name {namespace urn:x;}";
Radek Krejci33090f92020-12-17 20:12:46 +0100604 assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100605 assert_string_equal("urn:x", mod->mod->ns);
Radek Iša56ca9e42020-09-08 18:42:00 +0200606 CHECK_LOG_CTX("Missing mandatory keyword \"prefix\" as a child of \"module\".", "Line number 1.");
607 mod = mod_renew(YCTX);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200608
Michal Vasko63f3d842020-07-08 10:10:14 +0200609 in.current = " name {namespace urn:x;prefix \"x\";}";
Radek Krejci33090f92020-12-17 20:12:46 +0100610 assert_int_equal(LY_SUCCESS, parse_module(YCTX, mod));
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100611 assert_string_equal("x", mod->mod->prefix);
Radek Iša56ca9e42020-09-08 18:42:00 +0200612 mod = mod_renew(YCTX);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200613
Radek Krejci027d5802018-11-14 16:57:28 +0100614#define SCHEMA_BEGINNING " name {yang-version 1.1;namespace urn:x;prefix \"x\";"
615#define SCHEMA_BEGINNING2 " name {namespace urn:x;prefix \"x\";"
Radek Krejcia042ea12018-10-13 07:52:15 +0200616#define TEST_NODE(NODETYPE, INPUT, NAME) \
Michal Vasko63f3d842020-07-08 10:10:14 +0200617 in.current = SCHEMA_BEGINNING INPUT; \
Radek Krejci33090f92020-12-17 20:12:46 +0100618 assert_int_equal(LY_SUCCESS, parse_module(YCTX, mod)); \
Radek Krejcia042ea12018-10-13 07:52:15 +0200619 assert_non_null(mod->data); \
620 assert_int_equal(NODETYPE, mod->data->nodetype); \
621 assert_string_equal(NAME, mod->data->name); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200622 mod = mod_renew(YCTX);
Radek Krejcia042ea12018-10-13 07:52:15 +0200623#define TEST_GENERIC(INPUT, TARGET, TEST) \
Michal Vasko63f3d842020-07-08 10:10:14 +0200624 in.current = SCHEMA_BEGINNING INPUT; \
Radek Krejci33090f92020-12-17 20:12:46 +0100625 assert_int_equal(LY_SUCCESS, parse_module(YCTX, mod)); \
Radek Krejcia042ea12018-10-13 07:52:15 +0200626 assert_non_null(TARGET); \
627 TEST; \
Radek Iša56ca9e42020-09-08 18:42:00 +0200628 mod = mod_renew(YCTX);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100629#define TEST_DUP(MEMBER, VALUE1, VALUE2, LINE) \
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200630 TEST_DUP_GENERIC(SCHEMA_BEGINNING, MEMBER, VALUE1, VALUE2, \
Radek Iša56ca9e42020-09-08 18:42:00 +0200631 parse_module, mod, LINE, mod = mod_renew(YCTX))
Radek Krejcia042ea12018-10-13 07:52:15 +0200632
633 /* duplicated namespace, prefix */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100634 TEST_DUP("namespace", "y", "z", "1");
635 TEST_DUP("prefix", "y", "z", "1");
636 TEST_DUP("contact", "a", "b", "1");
637 TEST_DUP("description", "a", "b", "1");
638 TEST_DUP("organization", "a", "b", "1");
639 TEST_DUP("reference", "a", "b", "1");
Radek Krejcia042ea12018-10-13 07:52:15 +0200640
Radek Krejci70853c52018-10-15 14:46:16 +0200641 /* not allowed in module (submodule-specific) */
Michal Vasko63f3d842020-07-08 10:10:14 +0200642 in.current = SCHEMA_BEGINNING "belongs-to master {prefix m;}}";
Radek Krejci33090f92020-12-17 20:12:46 +0100643 assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
Radek Iša56ca9e42020-09-08 18:42:00 +0200644 CHECK_LOG_CTX("Invalid keyword \"belongs-to\" as a child of \"module\".", "Line number 1.");
645 mod = mod_renew(YCTX);
Radek Krejci70853c52018-10-15 14:46:16 +0200646
Radek Krejcia042ea12018-10-13 07:52:15 +0200647 /* anydata */
648 TEST_NODE(LYS_ANYDATA, "anydata test;}", "test");
649 /* anyxml */
650 TEST_NODE(LYS_ANYXML, "anyxml test;}", "test");
651 /* augment */
652 TEST_GENERIC("augment /somepath;}", mod->augments,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100653 assert_string_equal("/somepath", mod->augments[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200654 /* choice */
655 TEST_NODE(LYS_CHOICE, "choice test;}", "test");
656 /* contact 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100657 TEST_GENERIC("contact \"firstname\" + \n\t\" surname\";}", mod->mod->contact,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100658 assert_string_equal("firstname surname", mod->mod->contact));
Radek Krejcia042ea12018-10-13 07:52:15 +0200659 /* container */
660 TEST_NODE(LYS_CONTAINER, "container test;}", "test");
661 /* description 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100662 TEST_GENERIC("description \'some description\';}", mod->mod->dsc,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100663 assert_string_equal("some description", mod->mod->dsc));
Radek Krejcia042ea12018-10-13 07:52:15 +0200664 /* deviation */
665 TEST_GENERIC("deviation /somepath {deviate not-supported;}}", mod->deviations,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100666 assert_string_equal("/somepath", mod->deviations[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200667 /* extension */
668 TEST_GENERIC("extension test;}", mod->extensions,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100669 assert_string_equal("test", mod->extensions[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200670 /* feature */
671 TEST_GENERIC("feature test;}", mod->features,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100672 assert_string_equal("test", mod->features[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200673 /* grouping */
674 TEST_GENERIC("grouping grp;}", mod->groupings,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100675 assert_string_equal("grp", mod->groupings[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200676 /* identity */
677 TEST_GENERIC("identity test;}", mod->identities,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100678 assert_string_equal("test", mod->identities[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200679 /* import */
Radek Iša56ca9e42020-09-08 18:42:00 +0200680 ly_ctx_set_module_imp_clb(YCTX->parsed_mod->mod->ctx, test_imp_clb, "module zzz { namespace urn:zzz; prefix z;}");
Radek Krejci086c7132018-10-26 15:29:04 +0200681 TEST_GENERIC("import zzz {prefix z;}}", mod->imports,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100682 assert_string_equal("zzz", mod->imports[0].name));
Radek Krejci70853c52018-10-15 14:46:16 +0200683
Radek Krejcia042ea12018-10-13 07:52:15 +0200684 /* import - prefix collision */
Michal Vasko63f3d842020-07-08 10:10:14 +0200685 in.current = SCHEMA_BEGINNING "import zzz {prefix x;}}";
Radek Krejci33090f92020-12-17 20:12:46 +0100686 assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
Radek Krejci2efc45b2020-12-22 16:25:44 +0100687 CHECK_LOG_CTX("Prefix \"x\" already used as module prefix.", "Line number 1.");
Radek Iša56ca9e42020-09-08 18:42:00 +0200688 mod = mod_renew(YCTX);
689
Michal Vasko63f3d842020-07-08 10:10:14 +0200690 in.current = SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix y;}}";
Radek Krejci33090f92020-12-17 20:12:46 +0100691 assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
Radek Krejci2efc45b2020-12-22 16:25:44 +0100692 CHECK_LOG_CTX("Prefix \"y\" already used to import \"zzz\" module.", "Line number 1.");
Radek Iša56ca9e42020-09-08 18:42:00 +0200693
694 mod = mod_renew(YCTX);
Radek Krejciddace2c2021-01-08 11:30:56 +0100695 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100696
Michal Vasko7c8439f2020-08-05 13:25:19 +0200697 in.current = "module name10 {yang-version 1.1;namespace urn:x;prefix \"x\";import zzz {prefix y;}import zzz {prefix z;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200698 assert_int_equal(lys_parse_mem(YCTX->parsed_mod->mod->ctx, in.current, LYS_IN_YANG, NULL), LY_SUCCESS);
699 CHECK_LOG_CTX("Single revision of the module \"zzz\" imported twice.", NULL);
Radek Krejci70853c52018-10-15 14:46:16 +0200700
Radek Krejcia042ea12018-10-13 07:52:15 +0200701 /* include */
Radek Iša56ca9e42020-09-08 18:42:00 +0200702 ly_ctx_set_module_imp_clb(YCTX->parsed_mod->mod->ctx, test_imp_clb, "module xxx { namespace urn:xxx; prefix x;}");
Michal Vasko63f3d842020-07-08 10:10:14 +0200703 in.current = "module" SCHEMA_BEGINNING "include xxx;}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200704 assert_int_equal(lys_parse_mem(YCTX->parsed_mod->mod->ctx, in.current, LYS_IN_YANG, NULL), LY_EVALID);
705 CHECK_LOG_CTX("Including \"xxx\" submodule into \"name\" failed.", NULL);
Radek Krejcid33273d2018-10-25 14:55:52 +0200706
Radek Iša56ca9e42020-09-08 18:42:00 +0200707 ly_ctx_set_module_imp_clb(YCTX->parsed_mod->mod->ctx, test_imp_clb, "submodule xxx {belongs-to wrong-name {prefix w;}}");
Michal Vasko63f3d842020-07-08 10:10:14 +0200708 in.current = "module" SCHEMA_BEGINNING "include xxx;}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200709 assert_int_equal(lys_parse_mem(YCTX->parsed_mod->mod->ctx, in.current, LYS_IN_YANG, NULL), LY_EVALID);
710 CHECK_LOG_CTX("Including \"xxx\" submodule into \"name\" failed.", NULL);
Radek Krejcid33273d2018-10-25 14:55:52 +0200711
Radek Iša56ca9e42020-09-08 18:42:00 +0200712 ly_ctx_set_module_imp_clb(YCTX->parsed_mod->mod->ctx, test_imp_clb, "submodule xxx {belongs-to name {prefix x;}}");
Radek Krejcid33273d2018-10-25 14:55:52 +0200713 TEST_GENERIC("include xxx;}", mod->includes,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100714 assert_string_equal("xxx", mod->includes[0].name));
Radek Krejcid33273d2018-10-25 14:55:52 +0200715
Radek Krejcia042ea12018-10-13 07:52:15 +0200716 /* leaf */
717 TEST_NODE(LYS_LEAF, "leaf test {type string;}}", "test");
718 /* leaf-list */
719 TEST_NODE(LYS_LEAFLIST, "leaf-list test {type string;}}", "test");
720 /* list */
721 TEST_NODE(LYS_LIST, "list test {key a;leaf a {type string;}}}", "test");
722 /* notification */
723 TEST_GENERIC("notification test;}", mod->notifs,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100724 assert_string_equal("test", mod->notifs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200725 /* organization 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100726 TEST_GENERIC("organization \"CESNET a.l.e.\";}", mod->mod->org,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100727 assert_string_equal("CESNET a.l.e.", mod->mod->org));
Radek Krejcia042ea12018-10-13 07:52:15 +0200728 /* reference 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100729 TEST_GENERIC("reference RFC7950;}", mod->mod->ref,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100730 assert_string_equal("RFC7950", mod->mod->ref));
Radek Krejcia042ea12018-10-13 07:52:15 +0200731 /* revision */
732 TEST_GENERIC("revision 2018-10-12;}", mod->revs,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100733 assert_string_equal("2018-10-12", mod->revs[0].date));
Radek Krejcia042ea12018-10-13 07:52:15 +0200734 /* rpc */
735 TEST_GENERIC("rpc test;}", mod->rpcs,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100736 assert_string_equal("test", mod->rpcs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200737 /* typedef */
738 TEST_GENERIC("typedef test{type string;}}", mod->typedefs,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100739 assert_string_equal("test", mod->typedefs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200740 /* uses */
741 TEST_NODE(LYS_USES, "uses test;}", "test");
742 /* yang-version */
Michal Vasko63f3d842020-07-08 10:10:14 +0200743 in.current = SCHEMA_BEGINNING2 "\n\tyang-version 10;}";
Radek Krejci33090f92020-12-17 20:12:46 +0100744 assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
Radek Krejci2efc45b2020-12-22 16:25:44 +0100745 CHECK_LOG_CTX("Invalid value \"10\" of \"yang-version\".", NULL);
Radek Iša56ca9e42020-09-08 18:42:00 +0200746 mod = mod_renew(YCTX);
Radek Krejci96e48da2020-09-04 13:18:06 +0200747 in.current = SCHEMA_BEGINNING2 "yang-version 1;yang-version 1.1;}";
Radek Krejci33090f92020-12-17 20:12:46 +0100748 assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
Radek Krejci2efc45b2020-12-22 16:25:44 +0100749 CHECK_LOG_CTX("Duplicate keyword \"yang-version\".", NULL);
Radek Iša56ca9e42020-09-08 18:42:00 +0200750 mod = mod_renew(YCTX);
Radek Krejci96e48da2020-09-04 13:18:06 +0200751 in.current = SCHEMA_BEGINNING2 "yang-version 1;}";
Radek Krejci33090f92020-12-17 20:12:46 +0100752 assert_int_equal(LY_SUCCESS, parse_module(YCTX, mod));
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200753 assert_int_equal(1, mod->version);
Radek Iša56ca9e42020-09-08 18:42:00 +0200754 mod = mod_renew(YCTX);
Michal Vasko63f3d842020-07-08 10:10:14 +0200755 in.current = SCHEMA_BEGINNING2 "yang-version \"1.1\";}";
Radek Krejci33090f92020-12-17 20:12:46 +0100756 assert_int_equal(LY_SUCCESS, parse_module(YCTX, mod));
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200757 assert_int_equal(2, mod->version);
Radek Iša56ca9e42020-09-08 18:42:00 +0200758 mod = mod_renew(YCTX);
Radek Krejci40544fa2019-01-11 09:38:37 +0100759
Michal Vasko63f3d842020-07-08 10:10:14 +0200760 in.current = "module " SCHEMA_BEGINNING "} module q {namespace urn:q;prefixq;}";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200761 m = calloc(1, sizeof *m);
Radek Iša56ca9e42020-09-08 18:42:00 +0200762 m->ctx = YCTX->parsed_mod->mod->ctx;
Michal Vasko405cc9e2020-12-01 12:01:27 +0100763 assert_int_equal(LY_EVALID, yang_parse_module(&ctx_p, &in, m, &unres));
Radek Iša56ca9e42020-09-08 18:42:00 +0200764 CHECK_LOG_CTX("Trailing garbage \"module q {names...\" after module, expected end-of-input.", "Line number 1.");
Michal Vaskob36053d2020-03-26 15:49:30 +0100765 yang_parser_ctx_free(ctx_p);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200766 lys_module_free(m, NULL);
Radek Krejci40544fa2019-01-11 09:38:37 +0100767
Michal Vasko63f3d842020-07-08 10:10:14 +0200768 in.current = "prefix " SCHEMA_BEGINNING "}";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200769 m = calloc(1, sizeof *m);
Radek Iša56ca9e42020-09-08 18:42:00 +0200770 m->ctx = YCTX->parsed_mod->mod->ctx;
Michal Vasko405cc9e2020-12-01 12:01:27 +0100771 assert_int_equal(LY_EVALID, yang_parse_module(&ctx_p, &in, m, &unres));
Radek Iša56ca9e42020-09-08 18:42:00 +0200772 CHECK_LOG_CTX("Invalid keyword \"prefix\", expected \"module\" or \"submodule\".", "Line number 1.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200773 yang_parser_ctx_free(ctx_p);
774 lys_module_free(m, NULL);
Radek Krejci09306362018-10-15 15:26:01 +0200775
Michal Vasko63f3d842020-07-08 10:10:14 +0200776 in.current = "module " SCHEMA_BEGINNING "leaf enum {type enumeration {enum seven { position 7;}}}}";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200777 m = calloc(1, sizeof *m);
Radek Iša56ca9e42020-09-08 18:42:00 +0200778 m->ctx = YCTX->parsed_mod->mod->ctx;
Michal Vasko405cc9e2020-12-01 12:01:27 +0100779 assert_int_equal(LY_EVALID, yang_parse_module(&ctx_p, &in, m, &unres));
Radek Iša56ca9e42020-09-08 18:42:00 +0200780 CHECK_LOG_CTX("Invalid keyword \"position\" as a child of \"enum\".", "Line number 1.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200781 yang_parser_ctx_free(ctx_p);
782 lys_module_free(m, NULL);
David Sedlák9fb515f2019-07-11 10:33:58 +0200783
Radek Krejci156ccaf2018-10-15 15:49:17 +0200784 /* extensions */
785 TEST_GENERIC("prefix:test;}", mod->exts,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100786 assert_string_equal("prefix:test", mod->exts[0].name);
Radek Krejciab430862021-03-02 20:13:40 +0100787 assert_int_equal(LY_STMT_MODULE, mod->exts[0].parent_stmt));
Radek Iša56ca9e42020-09-08 18:42:00 +0200788 mod = mod_renew(YCTX);
Radek Krejci156ccaf2018-10-15 15:49:17 +0200789
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200790 /* invalid substatement */
Michal Vasko63f3d842020-07-08 10:10:14 +0200791 in.current = SCHEMA_BEGINNING "must false;}";
Radek Krejci33090f92020-12-17 20:12:46 +0100792 assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
Radek Krejci2efc45b2020-12-22 16:25:44 +0100793 CHECK_LOG_CTX("Invalid keyword \"must\" as a child of \"module\".", NULL);
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200794
Radek Krejci09306362018-10-15 15:26:01 +0200795 /* submodule */
Radek Iša56ca9e42020-09-08 18:42:00 +0200796 submod = submod_renew(YCTX);
Radek Krejci09306362018-10-15 15:26:01 +0200797
798 /* missing mandatory substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +0200799 in.current = " subname {}";
Radek Krejci33090f92020-12-17 20:12:46 +0100800 assert_int_equal(LY_EVALID, parse_submodule(YCTX, submod));
Radek Krejci2efc45b2020-12-22 16:25:44 +0100801 CHECK_LOG_CTX("Missing mandatory keyword \"belongs-to\" as a child of \"submodule\".", NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100802 assert_string_equal("subname", submod->name);
Radek Iša56ca9e42020-09-08 18:42:00 +0200803
804 submod = submod_renew(YCTX);
Radek Krejci09306362018-10-15 15:26:01 +0200805
Michal Vasko63f3d842020-07-08 10:10:14 +0200806 in.current = " subname {belongs-to name {prefix x;}}";
Radek Krejci33090f92020-12-17 20:12:46 +0100807 assert_int_equal(LY_SUCCESS, parse_submodule(YCTX, submod));
Michal Vaskoc3781c32020-10-06 14:04:08 +0200808 assert_string_equal("name", submod->mod->name);
Radek Iša56ca9e42020-09-08 18:42:00 +0200809 submod = submod_renew(YCTX);
Radek Krejci09306362018-10-15 15:26:01 +0200810
811#undef SCHEMA_BEGINNING
Radek Krejci313d9902018-11-08 09:42:58 +0100812#define SCHEMA_BEGINNING " subname {belongs-to name {prefix x;}"
Radek Krejci09306362018-10-15 15:26:01 +0200813
814 /* duplicated namespace, prefix */
Michal Vasko63f3d842020-07-08 10:10:14 +0200815 in.current = " subname {belongs-to name {prefix x;}belongs-to module1;belongs-to module2;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100816 assert_int_equal(LY_EVALID, parse_submodule(YCTX, submod));
Radek Krejci2efc45b2020-12-22 16:25:44 +0100817 CHECK_LOG_CTX("Duplicate keyword \"belongs-to\".", NULL);
Radek Iša56ca9e42020-09-08 18:42:00 +0200818 submod = submod_renew(YCTX);
Radek Krejci09306362018-10-15 15:26:01 +0200819
820 /* not allowed in submodule (module-specific) */
Michal Vasko63f3d842020-07-08 10:10:14 +0200821 in.current = SCHEMA_BEGINNING "namespace \"urn:z\";}";
Radek Krejci33090f92020-12-17 20:12:46 +0100822 assert_int_equal(LY_EVALID, parse_submodule(YCTX, submod));
Radek Krejci2efc45b2020-12-22 16:25:44 +0100823 CHECK_LOG_CTX("Invalid keyword \"namespace\" as a child of \"submodule\".", NULL);
Radek Iša56ca9e42020-09-08 18:42:00 +0200824 submod = submod_renew(YCTX);
Michal Vasko63f3d842020-07-08 10:10:14 +0200825 in.current = SCHEMA_BEGINNING "prefix m;}}";
Radek Krejci33090f92020-12-17 20:12:46 +0100826 assert_int_equal(LY_EVALID, parse_submodule(YCTX, submod));
Radek Krejci2efc45b2020-12-22 16:25:44 +0100827 CHECK_LOG_CTX("Invalid keyword \"prefix\" as a child of \"submodule\".", NULL);
Radek Iša56ca9e42020-09-08 18:42:00 +0200828 submod = submod_renew(YCTX);
Radek Krejcia042ea12018-10-13 07:52:15 +0200829
Michal Vasko63f3d842020-07-08 10:10:14 +0200830 in.current = "submodule " SCHEMA_BEGINNING "} module q {namespace urn:q;prefixq;}";
Radek Krejci33090f92020-12-17 20:12:46 +0100831 assert_int_equal(LY_EVALID, yang_parse_submodule(&ctx_p, YCTX->parsed_mod->mod->ctx, (struct lys_parser_ctx *)YCTX, YCTX->in, &submod));
Radek Iša56ca9e42020-09-08 18:42:00 +0200832 CHECK_LOG_CTX("Trailing garbage \"module q {names...\" after submodule, expected end-of-input.", "Line number 1.");
Michal Vaskob36053d2020-03-26 15:49:30 +0100833 yang_parser_ctx_free(ctx_p);
Radek Krejci40544fa2019-01-11 09:38:37 +0100834
Michal Vasko63f3d842020-07-08 10:10:14 +0200835 in.current = "prefix " SCHEMA_BEGINNING "}";
Radek Krejci33090f92020-12-17 20:12:46 +0100836 assert_int_equal(LY_EVALID, yang_parse_submodule(&ctx_p, YCTX->parsed_mod->mod->ctx, (struct lys_parser_ctx *)YCTX, YCTX->in, &submod));
Radek Iša56ca9e42020-09-08 18:42:00 +0200837 CHECK_LOG_CTX("Invalid keyword \"prefix\", expected \"module\" or \"submodule\".", "Line number 1.");
Michal Vaskob36053d2020-03-26 15:49:30 +0100838 yang_parser_ctx_free(ctx_p);
Radek Iša56ca9e42020-09-08 18:42:00 +0200839 submod = submod_renew(YCTX);
Radek Krejci40544fa2019-01-11 09:38:37 +0100840
Radek Krejcia042ea12018-10-13 07:52:15 +0200841#undef TEST_GENERIC
842#undef TEST_NODE
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200843#undef TEST_DUP
Radek Krejcia042ea12018-10-13 07:52:15 +0200844#undef SCHEMA_BEGINNING
Radek Krejciefd22f62018-09-27 11:47:58 +0200845}
846
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200847static void
848test_deviation(void **state)
849{
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200850 struct lysp_deviation *d = NULL;
Radek Krejci33090f92020-12-17 20:12:46 +0100851
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200852 /* invalid cardinality */
853#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
854 TEST_DUP_GENERIC(" test {deviate not-supported;", MEMBER, VALUE1, VALUE2, parse_deviation, \
Radek Iša56ca9e42020-09-08 18:42:00 +0200855 &d, "1", FREE_ARRAY(YCTX->parsed_mod->mod->ctx, d, lysp_deviation_free); d = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200856
857 TEST_DUP("description", "a", "b");
858 TEST_DUP("reference", "a", "b");
859
860 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +0200861 in.current = " test {deviate not-supported;description text;reference \'another text\';prefix:ext;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100862 assert_int_equal(LY_SUCCESS, parse_deviation(YCTX, &d));
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200863 assert_non_null(d);
Michal Vasko63f3d842020-07-08 10:10:14 +0200864 assert_string_equal(" ...", in.current);
Radek Iša56ca9e42020-09-08 18:42:00 +0200865 FREE_ARRAY(YCTX->parsed_mod->mod->ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200866 d = NULL;
867
868 /* missing mandatory substatement */
Michal Vasko63f3d842020-07-08 10:10:14 +0200869 in.current = " test {description text;}";
Radek Krejci33090f92020-12-17 20:12:46 +0100870 assert_int_equal(LY_EVALID, parse_deviation(YCTX, &d));
Radek Iša56ca9e42020-09-08 18:42:00 +0200871 CHECK_LOG_CTX("Missing mandatory keyword \"deviate\" as a child of \"deviation\".", "Line number 1.");
872 FREE_ARRAY(YCTX->parsed_mod->mod->ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200873 d = NULL;
874
875 /* invalid substatement */
Michal Vasko63f3d842020-07-08 10:10:14 +0200876 in.current = " test {deviate not-supported; status obsolete;}";
Radek Krejci33090f92020-12-17 20:12:46 +0100877 assert_int_equal(LY_EVALID, parse_deviation(YCTX, &d));
Radek Iša56ca9e42020-09-08 18:42:00 +0200878 CHECK_LOG_CTX("Invalid keyword \"status\" as a child of \"deviation\".", "Line number 1.");
879 FREE_ARRAY(YCTX->parsed_mod->mod->ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200880 d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200881#undef TEST_DUP
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200882}
883
Radek Iša56ca9e42020-09-08 18:42:00 +0200884#define TEST_DEVIATE_SUCCESS(INPUT_TEXT, REMAIN_TEXT)\
885 in.current = INPUT_TEXT;\
Radek Krejci33090f92020-12-17 20:12:46 +0100886 assert_int_equal(LY_SUCCESS, parse_deviate(YCTX, &d));\
Radek Iša56ca9e42020-09-08 18:42:00 +0200887 assert_non_null(d);\
888 assert_string_equal(REMAIN_TEXT, in.current);\
889 lysp_deviate_free(YCTX->parsed_mod->mod->ctx, d); free(d); d = NULL
890
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200891static void
892test_deviate(void **state)
893{
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200894 struct lysp_deviate *d = NULL;
Radek Krejci33090f92020-12-17 20:12:46 +0100895
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200896 /* invalid cardinality */
897#define TEST_DUP(TYPE, MEMBER, VALUE1, VALUE2) \
898 TEST_DUP_GENERIC(TYPE" {", MEMBER, VALUE1, VALUE2, parse_deviate, \
Radek Iša56ca9e42020-09-08 18:42:00 +0200899 &d, "1", lysp_deviate_free(YCTX->parsed_mod->mod->ctx, d); free(d); d = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200900
901 TEST_DUP("add", "config", "true", "false");
Radek Iša07767952021-01-31 20:54:24 +0100902#if 0
903 /*test on int8 now is in file tests/utest/types/int8.c */
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200904 TEST_DUP("replace", "default", "int8", "uint8");
Radek Iša07767952021-01-31 20:54:24 +0100905#endif
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200906 TEST_DUP("add", "mandatory", "true", "false");
907 TEST_DUP("add", "max-elements", "1", "2");
908 TEST_DUP("add", "min-elements", "1", "2");
Radek Iša07767952021-01-31 20:54:24 +0100909#if 0
910 /*test on int8 now is in file tests/utest/types/int8.c*/
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200911 TEST_DUP("replace", "type", "int8", "uint8");
Radek Iša07767952021-01-31 20:54:24 +0100912#endif
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200913 TEST_DUP("add", "units", "kilometers", "miles");
914
915 /* full contents */
Radek Iša56ca9e42020-09-08 18:42:00 +0200916 TEST_DEVIATE_SUCCESS(" not-supported {prefix:ext;} ...", " ...");
917 TEST_DEVIATE_SUCCESS(" 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;} ...", " ...");
918 TEST_DEVIATE_SUCCESS(" delete {units meters; must 1; must 2; unique x; unique y; default a; default b; prefix:ext;} ...", " ...");
919 TEST_DEVIATE_SUCCESS(" replace {type string; units meters; default a; config true; mandatory true; min-elements 1; max-elements 2; prefix:ext;} ...", " ...");
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200920
921 /* invalid substatements */
922#define TEST_NOT_SUP(DEV, STMT, VALUE) \
Michal Vasko63f3d842020-07-08 10:10:14 +0200923 in.current = " "DEV" {"STMT" "VALUE";}..."; \
Radek Krejci33090f92020-12-17 20:12:46 +0100924 assert_int_equal(LY_EVALID, parse_deviate(YCTX, &d)); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200925 CHECK_LOG_CTX("Deviate \""DEV"\" does not support keyword \""STMT"\".", "Line number 1.");\
926 lysp_deviate_free(YCTX->parsed_mod->mod->ctx, d); free(d); d = NULL
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200927
928 TEST_NOT_SUP("not-supported", "units", "meters");
929 TEST_NOT_SUP("not-supported", "must", "1");
930 TEST_NOT_SUP("not-supported", "unique", "x");
931 TEST_NOT_SUP("not-supported", "default", "a");
932 TEST_NOT_SUP("not-supported", "config", "true");
933 TEST_NOT_SUP("not-supported", "mandatory", "true");
934 TEST_NOT_SUP("not-supported", "min-elements", "1");
935 TEST_NOT_SUP("not-supported", "max-elements", "2");
936 TEST_NOT_SUP("not-supported", "type", "string");
937 TEST_NOT_SUP("add", "type", "string");
938 TEST_NOT_SUP("delete", "config", "true");
939 TEST_NOT_SUP("delete", "mandatory", "true");
940 TEST_NOT_SUP("delete", "min-elements", "1");
941 TEST_NOT_SUP("delete", "max-elements", "2");
942 TEST_NOT_SUP("delete", "type", "string");
943 TEST_NOT_SUP("replace", "must", "1");
944 TEST_NOT_SUP("replace", "unique", "a");
945
Michal Vasko63f3d842020-07-08 10:10:14 +0200946 in.current = " nonsence; ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100947 assert_int_equal(LY_EVALID, parse_deviate(YCTX, &d));
Radek Iša56ca9e42020-09-08 18:42:00 +0200948 CHECK_LOG_CTX("Invalid value \"nonsence\" of \"deviate\".", "Line number 1.");\
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200949 assert_null(d);
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200950#undef TEST_NOT_SUP
951#undef TEST_DUP
Radek Krejci4c6d9bd2018-10-15 16:43:06 +0200952}
953
Radek Krejci8c370832018-11-02 15:10:03 +0100954static void
955test_container(void **state)
956{
Radek Krejci8c370832018-11-02 15:10:03 +0100957 struct lysp_node_container *c = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +0100958
Radek Iša56ca9e42020-09-08 18:42:00 +0200959 YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejci8c370832018-11-02 15:10:03 +0100960
961 /* invalid cardinality */
962#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +0200963 in.current = "cont {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +0100964 assert_int_equal(LY_EVALID, parse_container(YCTX, NULL, (struct lysp_node**)&c)); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200965 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
966 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +0100967
968 TEST_DUP("config", "true", "false");
969 TEST_DUP("description", "text1", "text2");
970 TEST_DUP("presence", "true", "false");
971 TEST_DUP("reference", "1", "2");
972 TEST_DUP("status", "current", "obsolete");
973 TEST_DUP("when", "true", "false");
974#undef TEST_DUP
975
976 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +0200977 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 Krejcib4ac5a92020-11-23 17:54:33 +0100978 "leaf-list ll {type string;} list li;must 'expr';notification not; presence true; reference test;status current;typedef t {type int8;}uses g;when true;m:ext;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100979 assert_int_equal(LY_SUCCESS, parse_container(YCTX, NULL, (struct lysp_node **)&c));
Radek Iša56ca9e42020-09-08 18:42:00 +0200980 CHECK_LYSP_NODE(c, "test", 1, LYS_CONFIG_R | LYS_STATUS_CURR, 1, "cont", 0, LYS_CONTAINER, 0, "test", 1);
Radek Krejci8c370832018-11-02 15:10:03 +0100981 assert_non_null(c->actions);
982 assert_non_null(c->child);
Radek Krejci8c370832018-11-02 15:10:03 +0100983 assert_non_null(c->groupings);
Radek Krejci8c370832018-11-02 15:10:03 +0100984 assert_non_null(c->musts);
985 assert_non_null(c->notifs);
986 assert_string_equal("true", c->presence);
Radek Krejci8c370832018-11-02 15:10:03 +0100987 assert_non_null(c->typedefs);
Radek Iša56ca9e42020-09-08 18:42:00 +0200988 ly_set_erase(&YCTX->tpdfs_nodes, NULL);
989 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +0100990
991 /* invalid */
Michal Vasko63f3d842020-07-08 10:10:14 +0200992 in.current = " cont {augment /root;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100993 assert_int_equal(LY_EVALID, parse_container(YCTX, NULL, (struct lysp_node **)&c));
Radek Iša56ca9e42020-09-08 18:42:00 +0200994 CHECK_LOG_CTX("Invalid keyword \"augment\" as a child of \"container\".", "Line number 1.");
995 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)c); c = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +0200996 in.current = " cont {nonsence true;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100997 assert_int_equal(LY_EVALID, parse_container(YCTX, NULL, (struct lysp_node **)&c));
Radek Iša56ca9e42020-09-08 18:42:00 +0200998 CHECK_LOG_CTX("Invalid character sequence \"nonsence\", expected a keyword.", "Line number 1.");
999 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001000
Radek Iša56ca9e42020-09-08 18:42:00 +02001001 YCTX->parsed_mod->version = 1; /* simulate YANG 1.0 */
Michal Vasko63f3d842020-07-08 10:10:14 +02001002 in.current = " cont {action x;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001003 assert_int_equal(LY_EVALID, parse_container(YCTX, NULL, (struct lysp_node **)&c));
Radek Iša56ca9e42020-09-08 18:42:00 +02001004 CHECK_LOG_CTX("Invalid keyword \"action\" as a child of \"container\" - "
1005 "the statement is allowed only in YANG 1.1 modules.", "Line number 1.");
1006 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001007}
1008
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001009static void
1010test_leaf(void **state)
1011{
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001012 struct lysp_node_leaf *l = NULL;
Radek Krejci33090f92020-12-17 20:12:46 +01001013
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001014 /* invalid cardinality */
1015#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001016 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001017 assert_int_equal(LY_EVALID, parse_leaf(YCTX, NULL, (struct lysp_node**)&l)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001018 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
1019 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node*)l); l = NULL;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001020
1021 TEST_DUP("config", "true", "false");
1022 TEST_DUP("default", "x", "y");
1023 TEST_DUP("description", "text1", "text2");
1024 TEST_DUP("mandatory", "true", "false");
1025 TEST_DUP("reference", "1", "2");
1026 TEST_DUP("status", "current", "obsolete");
Radek Krejci0e5d8382018-11-28 16:37:53 +01001027 TEST_DUP("type", "int8", "uint8");
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001028 TEST_DUP("units", "text1", "text2");
1029 TEST_DUP("when", "true", "false");
1030#undef TEST_DUP
1031
1032 /* full content - without mandatory which is mutual exclusive with default */
Michal Vasko63f3d842020-07-08 10:10:14 +02001033 in.current = "l {config false;default \"xxx\";description test;if-feature f;"
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001034 "must 'expr';reference test;status current;type string; units yyy;when true;m:ext;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001035 assert_int_equal(LY_SUCCESS, parse_leaf(YCTX, NULL, (struct lysp_node **)&l));
Radek Iša56ca9e42020-09-08 18:42:00 +02001036 CHECK_LYSP_NODE(l, "test", 1, LYS_CONFIG_R | LYS_STATUS_CURR, 1, "l", 0, LYS_LEAF, 0, "test", 1);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001037 assert_string_equal("xxx", l->dflt.str);
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001038 assert_string_equal("yyy", l->units);
1039 assert_string_equal("string", l->type.name);
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001040 assert_non_null(l->musts);
Radek Iša56ca9e42020-09-08 18:42:00 +02001041 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)l); l = NULL;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001042
1043 /* full content - now with mandatory */
Michal Vasko63f3d842020-07-08 10:10:14 +02001044 in.current = "l {mandatory true; type string;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001045 assert_int_equal(LY_SUCCESS, parse_leaf(YCTX, NULL, (struct lysp_node **)&l));
Radek Iša56ca9e42020-09-08 18:42:00 +02001046 CHECK_LYSP_NODE(l, NULL, 0, LYS_MAND_TRUE, 0, "l", 0, LYS_LEAF, 0, NULL, 0);
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001047 assert_string_equal("string", l->type.name);
Radek Iša56ca9e42020-09-08 18:42:00 +02001048 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)l); l = NULL;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001049
1050 /* invalid */
Michal Vasko63f3d842020-07-08 10:10:14 +02001051 in.current = " l {description \"missing type\";} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001052 assert_int_equal(LY_EVALID, parse_leaf(YCTX, NULL, (struct lysp_node **)&l));
Radek Iša56ca9e42020-09-08 18:42:00 +02001053 CHECK_LOG_CTX("Missing mandatory keyword \"type\" as a child of \"leaf\".", "Line number 1.");
1054 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)l); l = NULL;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001055}
1056
Radek Krejci0e5d8382018-11-28 16:37:53 +01001057static void
1058test_leaflist(void **state)
1059{
Radek Krejci0e5d8382018-11-28 16:37:53 +01001060 struct lysp_node_leaflist *ll = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001061
Radek Iša56ca9e42020-09-08 18:42:00 +02001062 YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejci0e5d8382018-11-28 16:37:53 +01001063
1064 /* invalid cardinality */
1065#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001066 in.current = "ll {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001067 assert_int_equal(LY_EVALID, parse_leaflist(YCTX, NULL, (struct lysp_node**)&ll)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001068 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
1069 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node*)ll); ll = NULL;
Radek Krejci0e5d8382018-11-28 16:37:53 +01001070
1071 TEST_DUP("config", "true", "false");
1072 TEST_DUP("description", "text1", "text2");
1073 TEST_DUP("max-elements", "10", "20");
1074 TEST_DUP("min-elements", "10", "20");
1075 TEST_DUP("ordered-by", "user", "system");
1076 TEST_DUP("reference", "1", "2");
1077 TEST_DUP("status", "current", "obsolete");
1078 TEST_DUP("type", "int8", "uint8");
1079 TEST_DUP("units", "text1", "text2");
1080 TEST_DUP("when", "true", "false");
1081#undef TEST_DUP
1082
1083 /* full content - without min-elements which is mutual exclusive with default */
Michal Vasko63f3d842020-07-08 10:10:14 +02001084 in.current = "ll {config false;default \"xxx\"; default \"yyy\";description test;if-feature f;"
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001085 "max-elements 10;must 'expr';ordered-by user;reference test;"
1086 "status current;type string; units zzz;when true;m:ext;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001087 assert_int_equal(LY_SUCCESS, parse_leaflist(YCTX, NULL, (struct lysp_node **)&ll));
Radek Iša56ca9e42020-09-08 18:42:00 +02001088 CHECK_LYSP_NODE(ll, "test", 1, 0x446, 1, "ll", 0, LYS_LEAFLIST, 0, "test", 1);
Radek Krejci0e5d8382018-11-28 16:37:53 +01001089 assert_non_null(ll->dflts);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001090 assert_int_equal(2, LY_ARRAY_COUNT(ll->dflts));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001091 assert_string_equal("xxx", ll->dflts[0].str);
1092 assert_string_equal("yyy", ll->dflts[1].str);
Radek Krejci0e5d8382018-11-28 16:37:53 +01001093 assert_string_equal("zzz", ll->units);
1094 assert_int_equal(10, ll->max);
1095 assert_int_equal(0, ll->min);
1096 assert_string_equal("string", ll->type.name);
Radek Krejci0e5d8382018-11-28 16:37:53 +01001097 assert_non_null(ll->musts);
Radek Krejci0e5d8382018-11-28 16:37:53 +01001098 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_USER | LYS_SET_MAX, ll->flags);
Radek Iša56ca9e42020-09-08 18:42:00 +02001099 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)ll); ll = NULL;
Radek Krejci0e5d8382018-11-28 16:37:53 +01001100
1101 /* full content - now with min-elements */
Michal Vasko63f3d842020-07-08 10:10:14 +02001102 in.current = "ll {min-elements 10; type string;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001103 assert_int_equal(LY_SUCCESS, parse_leaflist(YCTX, NULL, (struct lysp_node **)&ll));
Radek Iša56ca9e42020-09-08 18:42:00 +02001104 CHECK_LYSP_NODE(ll, NULL, 0, 0x200, 0, "ll", 0, LYS_LEAFLIST, 0, NULL, 0);
Radek Krejci0e5d8382018-11-28 16:37:53 +01001105 assert_string_equal("string", ll->type.name);
1106 assert_int_equal(0, ll->max);
1107 assert_int_equal(10, ll->min);
1108 assert_int_equal(LYS_SET_MIN, ll->flags);
Radek Iša56ca9e42020-09-08 18:42:00 +02001109 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)ll); ll = NULL;
Radek Krejci0e5d8382018-11-28 16:37:53 +01001110
1111 /* invalid */
Michal Vasko63f3d842020-07-08 10:10:14 +02001112 in.current = " ll {description \"missing type\";} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001113 assert_int_equal(LY_EVALID, parse_leaflist(YCTX, NULL, (struct lysp_node **)&ll));
Radek Iša56ca9e42020-09-08 18:42:00 +02001114 CHECK_LOG_CTX("Missing mandatory keyword \"type\" as a child of \"leaf-list\".", "Line number 1.");
1115 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)ll); ll = NULL;
Radek Krejci0e5d8382018-11-28 16:37:53 +01001116
Radek Iša56ca9e42020-09-08 18:42:00 +02001117 YCTX->parsed_mod->version = 1; /* simulate YANG 1.0 - default statement is not allowed */
Michal Vasko63f3d842020-07-08 10:10:14 +02001118 in.current = " ll {default xx; type string;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001119 assert_int_equal(LY_EVALID, parse_leaflist(YCTX, NULL, (struct lysp_node **)&ll));
Radek Iša56ca9e42020-09-08 18:42:00 +02001120 CHECK_LOG_CTX("Invalid keyword \"default\" as a child of \"leaf-list\" - the statement is allowed only in YANG 1.1 modules.", "Line number 1.");
1121 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)ll); ll = NULL;
Radek Krejci0e5d8382018-11-28 16:37:53 +01001122}
1123
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001124static void
1125test_list(void **state)
1126{
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001127 struct lysp_node_list *l = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001128
Radek Iša56ca9e42020-09-08 18:42:00 +02001129 YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001130
1131 /* invalid cardinality */
1132#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001133 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001134 assert_int_equal(LY_EVALID, parse_list(YCTX, NULL, (struct lysp_node**)&l)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001135 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
1136 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node*)l); l = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001137
1138 TEST_DUP("config", "true", "false");
1139 TEST_DUP("description", "text1", "text2");
1140 TEST_DUP("key", "one", "two");
1141 TEST_DUP("max-elements", "10", "20");
1142 TEST_DUP("min-elements", "10", "20");
1143 TEST_DUP("ordered-by", "user", "system");
1144 TEST_DUP("reference", "1", "2");
1145 TEST_DUP("status", "current", "obsolete");
1146 TEST_DUP("when", "true", "false");
1147#undef TEST_DUP
1148
1149 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001150 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 Krejcib4ac5a92020-11-23 17:54:33 +01001151 "leaf-list ll {type string;} list li;max-elements 10; min-elements 1;must 'expr';notification not; ordered-by system; reference test;"
1152 "status current;typedef t {type int8;}unique xxx;unique yyy;uses g;when true;m:ext;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001153 assert_int_equal(LY_SUCCESS, parse_list(YCTX, NULL, (struct lysp_node **)&l));
Radek Iša56ca9e42020-09-08 18:42:00 +02001154 CHECK_LYSP_NODE(l, "test", 1, LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM | LYS_SET_MAX | LYS_SET_MIN, 1, "l",
1155 0, LYS_LIST, 0, "test", 1);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001156 assert_string_equal("l", l->key);
1157 assert_non_null(l->uniques);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001158 assert_int_equal(2, LY_ARRAY_COUNT(l->uniques));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001159 assert_string_equal("xxx", l->uniques[0].str);
1160 assert_string_equal("yyy", l->uniques[1].str);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001161 assert_int_equal(10, l->max);
1162 assert_int_equal(1, l->min);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001163 assert_non_null(l->musts);
Radek Iša56ca9e42020-09-08 18:42:00 +02001164 ly_set_erase(&YCTX->tpdfs_nodes, NULL);
1165 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)l); l = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001166
Radek Krejcif538ce52019-03-05 10:46:14 +01001167 /* invalid content */
Radek Iša56ca9e42020-09-08 18:42:00 +02001168 YCTX->parsed_mod->version = 1; /* simulate YANG 1.0 */
Michal Vasko63f3d842020-07-08 10:10:14 +02001169 in.current = "l {action x;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001170 assert_int_equal(LY_EVALID, parse_list(YCTX, NULL, (struct lysp_node **)&l));
Radek Iša56ca9e42020-09-08 18:42:00 +02001171 CHECK_LOG_CTX("Invalid keyword \"action\" as a child of \"list\" - the statement is allowed only in YANG 1.1 modules.", "Line number 1.");
1172 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)l); l = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001173}
1174
Radek Krejci056d0a82018-12-06 16:57:25 +01001175static void
1176test_choice(void **state)
1177{
Radek Krejci056d0a82018-12-06 16:57:25 +01001178 struct lysp_node_choice *ch = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001179
Radek Iša56ca9e42020-09-08 18:42:00 +02001180 YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejci056d0a82018-12-06 16:57:25 +01001181
1182 /* invalid cardinality */
1183#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001184 in.current = "ch {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001185 assert_int_equal(LY_EVALID, parse_choice(YCTX, NULL, (struct lysp_node**)&ch)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001186 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
1187 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node*)ch); ch = NULL;
Radek Krejci056d0a82018-12-06 16:57:25 +01001188
1189 TEST_DUP("config", "true", "false");
1190 TEST_DUP("default", "a", "b");
1191 TEST_DUP("description", "text1", "text2");
1192 TEST_DUP("mandatory", "true", "false");
1193 TEST_DUP("reference", "1", "2");
1194 TEST_DUP("status", "current", "obsolete");
1195 TEST_DUP("when", "true", "false");
1196#undef TEST_DUP
1197
Radek Krejcia9026eb2018-12-12 16:04:47 +01001198 /* full content - without default due to a collision with mandatory */
Michal Vasko63f3d842020-07-08 10:10:14 +02001199 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 Krejcib4ac5a92020-11-23 17:54:33 +01001200 "leaf-list ll {type string;} list li;mandatory true;reference test;status current;when true;m:ext;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001201 assert_int_equal(LY_SUCCESS, parse_choice(YCTX, NULL, (struct lysp_node **)&ch));
Radek Iša56ca9e42020-09-08 18:42:00 +02001202 CHECK_LYSP_NODE(ch, "test", 1, LYS_CONFIG_R | LYS_STATUS_CURR | LYS_MAND_TRUE, 1, "ch", 0, LYS_CHOICE, 0, "test", 1);
1203 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)ch); ch = NULL;
Radek Krejci056d0a82018-12-06 16:57:25 +01001204
Radek Krejcia9026eb2018-12-12 16:04:47 +01001205 /* full content - the default missing from the previous node */
Michal Vasko63f3d842020-07-08 10:10:14 +02001206 in.current = "ch {default c;case c;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001207 assert_int_equal(LY_SUCCESS, parse_choice(YCTX, NULL, (struct lysp_node **)&ch));
Radek Iša56ca9e42020-09-08 18:42:00 +02001208 CHECK_LYSP_NODE(ch, NULL, 0, 0, 0, "ch", 0, LYS_CHOICE, 0, NULL, 0);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001209 assert_string_equal("c", ch->dflt.str);
Radek Iša56ca9e42020-09-08 18:42:00 +02001210 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)ch); ch = NULL;
Radek Krejcia9026eb2018-12-12 16:04:47 +01001211}
1212
1213static void
1214test_case(void **state)
1215{
Radek Krejcia9026eb2018-12-12 16:04:47 +01001216 struct lysp_node_case *cs = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001217
Radek Iša56ca9e42020-09-08 18:42:00 +02001218 YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejcia9026eb2018-12-12 16:04:47 +01001219
1220 /* invalid cardinality */
1221#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001222 in.current = "cs {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001223 assert_int_equal(LY_EVALID, parse_case(YCTX, NULL, (struct lysp_node**)&cs)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001224 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
1225 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node*)cs); cs = NULL;
Radek Krejcia9026eb2018-12-12 16:04:47 +01001226
1227 TEST_DUP("description", "text1", "text2");
1228 TEST_DUP("reference", "1", "2");
1229 TEST_DUP("status", "current", "obsolete");
1230 TEST_DUP("when", "true", "false");
1231#undef TEST_DUP
1232
1233 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001234 in.current = "cs {anydata any;anyxml anyxml; choice ch;container c;description test;if-feature f;leaf l {type string;}"
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001235 "leaf-list ll {type string;} list li;reference test;status current;uses grp;when true;m:ext;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001236 assert_int_equal(LY_SUCCESS, parse_case(YCTX, NULL, (struct lysp_node **)&cs));
Radek Iša56ca9e42020-09-08 18:42:00 +02001237 CHECK_LYSP_NODE(cs, "test", 1, LYS_STATUS_CURR, 1, "cs", 0, LYS_CASE, 0, "test", 1);
1238 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)cs); cs = NULL;
Radek Krejcia9026eb2018-12-12 16:04:47 +01001239
1240 /* invalid content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001241 in.current = "cs {config true} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001242 assert_int_equal(LY_EVALID, parse_case(YCTX, NULL, (struct lysp_node **)&cs));
Radek Iša56ca9e42020-09-08 18:42:00 +02001243 CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"case\".", "Line number 1.");
1244 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)cs); cs = NULL;
Radek Krejci056d0a82018-12-06 16:57:25 +01001245}
1246
Radek Krejci9800fb82018-12-13 14:26:23 +01001247static void
Radek Krejcid6b76452019-09-03 17:03:03 +02001248test_any(void **state, enum ly_stmt kw)
Radek Krejci9800fb82018-12-13 14:26:23 +01001249{
Radek Krejci9800fb82018-12-13 14:26:23 +01001250 struct lysp_node_anydata *any = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001251
Radek Krejcid6b76452019-09-03 17:03:03 +02001252 if (kw == LY_STMT_ANYDATA) {
Radek Iša56ca9e42020-09-08 18:42:00 +02001253 YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejci9800fb82018-12-13 14:26:23 +01001254 } else {
Radek Iša56ca9e42020-09-08 18:42:00 +02001255 YCTX->parsed_mod->version = 1; /* simulate YANG 1.0 */
Radek Krejci9800fb82018-12-13 14:26:23 +01001256 }
1257
1258 /* invalid cardinality */
1259#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001260 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001261 assert_int_equal(LY_EVALID, parse_any(YCTX, kw, NULL, (struct lysp_node**)&any)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001262 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
1263 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node*)any); any = NULL;
Radek Krejci9800fb82018-12-13 14:26:23 +01001264
1265 TEST_DUP("config", "true", "false");
1266 TEST_DUP("description", "text1", "text2");
1267 TEST_DUP("mandatory", "true", "false");
1268 TEST_DUP("reference", "1", "2");
1269 TEST_DUP("status", "current", "obsolete");
1270 TEST_DUP("when", "true", "false");
1271#undef TEST_DUP
1272
1273 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001274 in.current = "any {config true;description test;if-feature f;mandatory true;must 'expr';reference test;status current;when true;m:ext;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001275 assert_int_equal(LY_SUCCESS, parse_any(YCTX, kw, NULL, (struct lysp_node **)&any));
Radek Iša56ca9e42020-09-08 18:42:00 +02001276 // CHECK_LYSP_NODE(NODE, DSC, EXTS, FLAGS, IFFEATURES, NAME, NEXT, TYPE, PARENT, REF, WHEN)
1277 uint16_t node_type = kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
1278 CHECK_LYSP_NODE(any, "test", 1, LYS_CONFIG_W | LYS_STATUS_CURR | LYS_MAND_TRUE, 1, "any", 0, node_type, 0, "test", 1);
Radek Krejci9800fb82018-12-13 14:26:23 +01001279 assert_non_null(any->musts);
Radek Iša56ca9e42020-09-08 18:42:00 +02001280 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)any); any = NULL;
Radek Krejci9800fb82018-12-13 14:26:23 +01001281}
1282
1283static void
1284test_anydata(void **state)
1285{
Radek Krejcid6b76452019-09-03 17:03:03 +02001286 return test_any(state, LY_STMT_ANYDATA);
Radek Krejci9800fb82018-12-13 14:26:23 +01001287}
1288
1289static void
1290test_anyxml(void **state)
1291{
Radek Krejcid6b76452019-09-03 17:03:03 +02001292 return test_any(state, LY_STMT_ANYXML);
Radek Krejci9800fb82018-12-13 14:26:23 +01001293}
1294
Radek Krejcie86bf772018-12-14 11:39:53 +01001295static void
1296test_grouping(void **state)
1297{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001298 struct lysp_node_grp *grp = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001299
Radek Iša56ca9e42020-09-08 18:42:00 +02001300 YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejcie86bf772018-12-14 11:39:53 +01001301
1302 /* invalid cardinality */
1303#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001304 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001305 assert_int_equal(LY_EVALID, parse_grouping(YCTX, NULL, &grp)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001306 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Radek Krejci2a9fc652021-01-22 17:44:34 +01001307 lysp_node_free(YCTX->parsed_mod->mod->ctx, &grp->node); grp = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001308
1309 TEST_DUP("description", "text1", "text2");
1310 TEST_DUP("reference", "1", "2");
1311 TEST_DUP("status", "current", "obsolete");
1312#undef TEST_DUP
1313
1314 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001315 in.current = "grp {action x;anydata any;anyxml anyxml; choice ch;container c;description test;grouping g;leaf l {type string;}"
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001316 "leaf-list ll {type string;} list li;notification not;reference test;status current;typedef t {type int8;}uses g;m:ext;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001317 assert_int_equal(LY_SUCCESS, parse_grouping(YCTX, NULL, &grp));
Radek Krejcie86bf772018-12-14 11:39:53 +01001318 assert_non_null(grp);
1319 assert_int_equal(LYS_GROUPING, grp->nodetype);
1320 assert_string_equal("grp", grp->name);
1321 assert_string_equal("test", grp->dsc);
1322 assert_non_null(grp->exts);
1323 assert_string_equal("test", grp->ref);
1324 assert_null(grp->parent);
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001325 assert_int_equal(LYS_STATUS_CURR, grp->flags);
Radek Iša56ca9e42020-09-08 18:42:00 +02001326 ly_set_erase(&YCTX->tpdfs_nodes, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001327 lysp_node_free(YCTX->parsed_mod->mod->ctx, &grp->node);
1328 grp = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001329
1330 /* invalid content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001331 in.current = "grp {config true} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001332 assert_int_equal(LY_EVALID, parse_grouping(YCTX, NULL, &grp));
Radek Iša56ca9e42020-09-08 18:42:00 +02001333 CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"grouping\".", "Line number 1.");
Radek Krejci2a9fc652021-01-22 17:44:34 +01001334 lysp_node_free(YCTX->parsed_mod->mod->ctx, &grp->node);
1335 grp = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001336
Michal Vasko63f3d842020-07-08 10:10:14 +02001337 in.current = "grp {must 'expr'} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001338 assert_int_equal(LY_EVALID, parse_grouping(YCTX, NULL, &grp));
Radek Iša56ca9e42020-09-08 18:42:00 +02001339 CHECK_LOG_CTX("Invalid keyword \"must\" as a child of \"grouping\".", "Line number 1.");
Radek Krejci2a9fc652021-01-22 17:44:34 +01001340 lysp_node_free(YCTX->parsed_mod->mod->ctx, &grp->node);
1341 grp = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001342}
1343
1344static void
Radek Krejcif538ce52019-03-05 10:46:14 +01001345test_action(void **state)
1346{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001347 struct lysp_node_action *rpcs = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01001348 struct lysp_node_container *c = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001349
Radek Iša56ca9e42020-09-08 18:42:00 +02001350 YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejcif538ce52019-03-05 10:46:14 +01001351
1352 /* invalid cardinality */
1353#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001354 in.current = "func {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001355 assert_int_equal(LY_EVALID, parse_action(YCTX, NULL, &rpcs)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001356 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Radek Krejci2a9fc652021-01-22 17:44:34 +01001357 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node*)rpcs); rpcs = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01001358
1359 TEST_DUP("description", "text1", "text2");
Michal Vaskob83af8a2020-01-06 09:49:22 +01001360 TEST_DUP("input", "{leaf l1 {type empty;}} description a", "{leaf l2 {type empty;}} description a");
1361 TEST_DUP("output", "{leaf l1 {type empty;}} description a", "{leaf l2 {type empty;}} description a");
Radek Krejcif538ce52019-03-05 10:46:14 +01001362 TEST_DUP("reference", "1", "2");
1363 TEST_DUP("status", "current", "obsolete");
1364#undef TEST_DUP
1365
1366 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001367 in.current = "top;";
Radek Krejci33090f92020-12-17 20:12:46 +01001368 assert_int_equal(LY_SUCCESS, parse_container(YCTX, NULL, (struct lysp_node **)&c));
Michal Vasko63f3d842020-07-08 10:10:14 +02001369 in.current = "func {description test;grouping grp;if-feature f;reference test;status current;typedef mytype {type int8;} m:ext;"
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001370 "input {anydata a1; anyxml a2; choice ch; container c; grouping grp; leaf l {type int8;} leaf-list ll {type int8;}"
1371 " list li; must 1; typedef mytypei {type int8;} uses grp; m:ext;}"
1372 "output {anydata a1; anyxml a2; choice ch; container c; grouping grp; leaf l {type int8;} leaf-list ll {type int8;}"
1373 " list li; must 1; typedef mytypeo {type int8;} uses grp; m:ext;}} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001374 assert_int_equal(LY_SUCCESS, parse_action(YCTX, (struct lysp_node *)c, &rpcs));
Radek Krejcif538ce52019-03-05 10:46:14 +01001375 assert_non_null(rpcs);
1376 assert_int_equal(LYS_ACTION, rpcs->nodetype);
1377 assert_string_equal("func", rpcs->name);
1378 assert_string_equal("test", rpcs->dsc);
1379 assert_non_null(rpcs->exts);
1380 assert_non_null(rpcs->iffeatures);
1381 assert_string_equal("test", rpcs->ref);
1382 assert_non_null(rpcs->groupings);
1383 assert_non_null(rpcs->typedefs);
1384 assert_int_equal(LYS_STATUS_CURR, rpcs->flags);
1385 /* input */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001386 assert_int_equal(rpcs->input.nodetype, LYS_INPUT);
Radek Krejcif538ce52019-03-05 10:46:14 +01001387 assert_non_null(rpcs->input.groupings);
1388 assert_non_null(rpcs->input.exts);
1389 assert_non_null(rpcs->input.musts);
1390 assert_non_null(rpcs->input.typedefs);
Radek Krejci01180ac2021-01-27 08:48:22 +01001391 assert_non_null(rpcs->input.child);
Radek Krejcif538ce52019-03-05 10:46:14 +01001392 /* output */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001393 assert_int_equal(rpcs->output.nodetype, LYS_OUTPUT);
Radek Krejcif538ce52019-03-05 10:46:14 +01001394 assert_non_null(rpcs->output.groupings);
1395 assert_non_null(rpcs->output.exts);
1396 assert_non_null(rpcs->output.musts);
1397 assert_non_null(rpcs->output.typedefs);
Radek Krejci01180ac2021-01-27 08:48:22 +01001398 assert_non_null(rpcs->output.child);
Radek Krejcif538ce52019-03-05 10:46:14 +01001399
Radek Iša56ca9e42020-09-08 18:42:00 +02001400 ly_set_erase(&YCTX->tpdfs_nodes, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001401 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)rpcs); rpcs = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01001402
1403 /* invalid content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001404 in.current = "func {config true} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001405 assert_int_equal(LY_EVALID, parse_action(YCTX, NULL, &rpcs));
Radek Iša56ca9e42020-09-08 18:42:00 +02001406 CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"rpc\".", "Line number 1.");
Radek Krejci2a9fc652021-01-22 17:44:34 +01001407 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)rpcs); rpcs = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01001408
Radek Iša56ca9e42020-09-08 18:42:00 +02001409 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)c);
Radek Krejcif538ce52019-03-05 10:46:14 +01001410}
1411
1412static void
Radek Krejcifc11bd72019-04-11 16:00:05 +02001413test_notification(void **state)
1414{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001415 struct lysp_node_notif *notifs = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001416 struct lysp_node_container *c = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001417
Radek Iša56ca9e42020-09-08 18:42:00 +02001418 YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejcifc11bd72019-04-11 16:00:05 +02001419
1420 /* invalid cardinality */
1421#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001422 in.current = "func {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001423 assert_int_equal(LY_EVALID, parse_notif(YCTX, NULL, &notifs)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001424 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Radek Krejci2a9fc652021-01-22 17:44:34 +01001425 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node*)notifs); notifs = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001426
1427 TEST_DUP("description", "text1", "text2");
1428 TEST_DUP("reference", "1", "2");
1429 TEST_DUP("status", "current", "obsolete");
1430#undef TEST_DUP
1431
1432 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001433 in.current = "top;";
Radek Krejci33090f92020-12-17 20:12:46 +01001434 assert_int_equal(LY_SUCCESS, parse_container(YCTX, NULL, (struct lysp_node **)&c));
Michal Vasko63f3d842020-07-08 10:10:14 +02001435 in.current = "ntf {anydata a1; anyxml a2; choice ch; container c; description test; grouping grp; if-feature f; leaf l {type int8;}"
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001436 "leaf-list ll {type int8;} list li; must 1; reference test; status current; typedef mytype {type int8;} uses grp; m:ext;}";
Radek Krejci33090f92020-12-17 20:12:46 +01001437 assert_int_equal(LY_SUCCESS, parse_notif(YCTX, (struct lysp_node *)c, &notifs));
Radek Krejcifc11bd72019-04-11 16:00:05 +02001438 assert_non_null(notifs);
1439 assert_int_equal(LYS_NOTIF, notifs->nodetype);
1440 assert_string_equal("ntf", notifs->name);
1441 assert_string_equal("test", notifs->dsc);
1442 assert_non_null(notifs->exts);
1443 assert_non_null(notifs->iffeatures);
1444 assert_string_equal("test", notifs->ref);
1445 assert_non_null(notifs->groupings);
1446 assert_non_null(notifs->typedefs);
1447 assert_non_null(notifs->musts);
Radek Krejci01180ac2021-01-27 08:48:22 +01001448 assert_non_null(notifs->child);
Radek Krejcifc11bd72019-04-11 16:00:05 +02001449 assert_int_equal(LYS_STATUS_CURR, notifs->flags);
1450
Radek Iša56ca9e42020-09-08 18:42:00 +02001451 ly_set_erase(&YCTX->tpdfs_nodes, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001452 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)notifs); notifs = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001453
1454 /* invalid content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001455 in.current = "ntf {config true} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001456 assert_int_equal(LY_EVALID, parse_notif(YCTX, NULL, &notifs));
Radek Iša56ca9e42020-09-08 18:42:00 +02001457 CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"notification\".", "Line number 1.");
Radek Krejci2a9fc652021-01-22 17:44:34 +01001458 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)notifs); notifs = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001459
Radek Iša56ca9e42020-09-08 18:42:00 +02001460 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)c);
Radek Krejcifc11bd72019-04-11 16:00:05 +02001461}
1462
1463static void
Radek Krejcie86bf772018-12-14 11:39:53 +01001464test_uses(void **state)
1465{
Radek Krejcie86bf772018-12-14 11:39:53 +01001466 struct lysp_node_uses *u = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001467
Radek Iša56ca9e42020-09-08 18:42:00 +02001468 YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejcie86bf772018-12-14 11:39:53 +01001469
1470 /* invalid cardinality */
1471#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001472 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001473 assert_int_equal(LY_EVALID, parse_uses(YCTX, NULL, (struct lysp_node**)&u)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001474 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
1475 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node*)u); u = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001476
1477 TEST_DUP("description", "text1", "text2");
1478 TEST_DUP("reference", "1", "2");
1479 TEST_DUP("status", "current", "obsolete");
1480 TEST_DUP("when", "true", "false");
1481#undef TEST_DUP
1482
1483 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001484 in.current = "grpref {augment some/node;description test;if-feature f;reference test;refine some/other/node;status current;when true;m:ext;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001485 assert_int_equal(LY_SUCCESS, parse_uses(YCTX, NULL, (struct lysp_node **)&u));
Radek Iša56ca9e42020-09-08 18:42:00 +02001486 CHECK_LYSP_NODE(u, "test", 1, LYS_STATUS_CURR, 1, "grpref", 0, LYS_USES, 0, "test", 1);
Radek Krejcie86bf772018-12-14 11:39:53 +01001487 assert_non_null(u->augments);
1488 assert_non_null(u->refines);
Radek Iša56ca9e42020-09-08 18:42:00 +02001489 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)u); u = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001490}
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001491
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001492static void
1493test_augment(void **state)
1494{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001495 struct lysp_node_augment *a = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001496
Radek Iša56ca9e42020-09-08 18:42:00 +02001497 YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001498
1499 /* invalid cardinality */
1500#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001501 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001502 assert_int_equal(LY_EVALID, parse_augment(YCTX, NULL, &a)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001503 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Radek Krejci2a9fc652021-01-22 17:44:34 +01001504 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)a); a = NULL;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001505
1506 TEST_DUP("description", "text1", "text2");
1507 TEST_DUP("reference", "1", "2");
1508 TEST_DUP("status", "current", "obsolete");
1509 TEST_DUP("when", "true", "false");
1510#undef TEST_DUP
1511
1512 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001513 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 Krejcib4ac5a92020-11-23 17:54:33 +01001514 "leaf-list ll {type string;} list li;notification not;reference test;status current;uses g;when true;m:ext;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001515 assert_int_equal(LY_SUCCESS, parse_augment(YCTX, NULL, &a));
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001516 assert_non_null(a);
1517 assert_int_equal(LYS_AUGMENT, a->nodetype);
1518 assert_string_equal("/target/nodeid", a->nodeid);
1519 assert_string_equal("test", a->dsc);
1520 assert_non_null(a->exts);
1521 assert_non_null(a->iffeatures);
1522 assert_string_equal("test", a->ref);
1523 assert_non_null(a->when);
1524 assert_null(a->parent);
1525 assert_int_equal(LYS_STATUS_CURR, a->flags);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001526 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)a); a = NULL;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001527}
1528
Radek Krejcif09e4e82019-06-14 15:08:11 +02001529static void
1530test_when(void **state)
1531{
Radek Krejcif09e4e82019-06-14 15:08:11 +02001532 struct lysp_when *w = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001533
Radek Iša56ca9e42020-09-08 18:42:00 +02001534 YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejcif09e4e82019-06-14 15:08:11 +02001535
1536 /* invalid cardinality */
1537#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001538 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001539 assert_int_equal(LY_EVALID, parse_when(YCTX, &w)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001540 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
1541 FREE_MEMBER(YCTX->parsed_mod->mod->ctx, w, lysp_when_free); w = NULL;
Radek Krejcif09e4e82019-06-14 15:08:11 +02001542
1543 TEST_DUP("description", "text1", "text2");
1544 TEST_DUP("reference", "1", "2");
1545#undef TEST_DUP
1546
1547 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001548 in.current = "expression {description test;reference test;m:ext;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001549 assert_int_equal(LY_SUCCESS, parse_when(YCTX, &w));
Radek Krejcif09e4e82019-06-14 15:08:11 +02001550 assert_non_null(w);
1551 assert_string_equal("expression", w->cond);
1552 assert_string_equal("test", w->dsc);
1553 assert_string_equal("test", w->ref);
1554 assert_non_null(w->exts);
Radek Iša56ca9e42020-09-08 18:42:00 +02001555 FREE_MEMBER(YCTX->parsed_mod->mod->ctx, w, lysp_when_free); w = NULL;
Radek Krejcif09e4e82019-06-14 15:08:11 +02001556
1557 /* empty condition */
Michal Vasko63f3d842020-07-08 10:10:14 +02001558 in.current = "\"\";";
Radek Krejci33090f92020-12-17 20:12:46 +01001559 assert_int_equal(LY_SUCCESS, parse_when(YCTX, &w));
Radek Iša56ca9e42020-09-08 18:42:00 +02001560 CHECK_LOG_CTX("Empty argument of when statement does not make sense.", NULL);
Radek Krejcif09e4e82019-06-14 15:08:11 +02001561 assert_non_null(w);
1562 assert_string_equal("", w->cond);
Radek Iša56ca9e42020-09-08 18:42:00 +02001563 FREE_MEMBER(YCTX->parsed_mod->mod->ctx, w, lysp_when_free); w = NULL;
Radek Krejcif09e4e82019-06-14 15:08:11 +02001564}
1565
David Sedlákd6ce6d72019-07-16 17:30:18 +02001566static void
1567test_value(void **state)
1568{
David Sedlákd6ce6d72019-07-16 17:30:18 +02001569 int64_t val = 0;
1570 uint16_t flags = 0;
1571
Michal Vasko63f3d842020-07-08 10:10:14 +02001572 in.current = "-0;";
Radek Krejci33090f92020-12-17 20:12:46 +01001573 assert_int_equal(parse_type_enum_value_pos(YCTX, LY_STMT_VALUE, &val, &flags, NULL), LY_SUCCESS);
David Sedlákd6ce6d72019-07-16 17:30:18 +02001574 assert_int_equal(val, 0);
1575
Michal Vasko63f3d842020-07-08 10:10:14 +02001576 in.current = "-0;";
David Sedlákd6ce6d72019-07-16 17:30:18 +02001577 flags = 0;
Radek Krejci33090f92020-12-17 20:12:46 +01001578 assert_int_equal(parse_type_enum_value_pos(YCTX, LY_STMT_POSITION, &val, &flags, NULL), LY_EVALID);
Radek Iša56ca9e42020-09-08 18:42:00 +02001579 CHECK_LOG_CTX("Invalid value \"-0\" of \"position\".", "Line number 1.");
David Sedlákd6ce6d72019-07-16 17:30:18 +02001580}
1581
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001582int
1583main(void)
Radek Krejci80dd33e2018-09-26 15:57:18 +02001584{
1585 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +02001586 UTEST(test_helpers, setup, teardown),
1587 UTEST(test_comments, setup, teardown),
1588 UTEST(test_arg, setup, teardown),
1589 UTEST(test_stmts, setup, teardown),
1590 UTEST(test_minmax, setup, teardown),
1591 UTEST(test_module, setup, teardown),
1592 UTEST(test_deviation, setup, teardown),
1593 UTEST(test_deviate, setup, teardown),
1594 UTEST(test_container, setup, teardown),
1595 UTEST(test_leaf, setup, teardown),
1596 UTEST(test_leaflist, setup, teardown),
1597 UTEST(test_list, setup, teardown),
1598 UTEST(test_choice, setup, teardown),
1599 UTEST(test_case, setup, teardown),
1600 UTEST(test_anydata, setup, teardown),
1601 UTEST(test_anyxml, setup, teardown),
1602 UTEST(test_action, setup, teardown),
1603 UTEST(test_notification, setup, teardown),
1604 UTEST(test_grouping, setup, teardown),
1605 UTEST(test_uses, setup, teardown),
1606 UTEST(test_augment, setup, teardown),
1607 UTEST(test_when, setup, teardown),
1608 UTEST(test_value, setup, teardown),
Radek Krejci80dd33e2018-09-26 15:57:18 +02001609 };
1610
1611 return cmocka_run_group_tests(tests, NULL, NULL);
1612}