blob: 9625ea942b722667e68ddf07ecdf71be29454bb6 [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 Krejci859a15a2021-03-05 20:56:59 +010024#include "tree_edit.h"
Radek Krejci70593c12020-06-13 20:48:09 +020025#include "tree_schema.h"
26#include "tree_schema_internal.h"
Radek Krejci2d7a47b2019-05-16 13:34:10 +020027
28/* originally static functions from tree_schema_free.c and parser_yang.c */
29void lysp_ext_instance_free(struct ly_ctx *ctx, struct lysp_ext_instance *ext);
Radek Krejci2d7a47b2019-05-16 13:34:10 +020030void lysp_deviation_free(struct ly_ctx *ctx, struct lysp_deviation *dev);
Radek Krejci2a9fc652021-01-22 17:44:34 +010031void lysp_grp_free(struct ly_ctx *ctx, struct lysp_node_grp *grp);
32void lysp_augment_free(struct ly_ctx *ctx, struct lysp_node_augment *augment);
Radek Krejci2d7a47b2019-05-16 13:34:10 +020033void lysp_deviate_free(struct ly_ctx *ctx, struct lysp_deviate *d);
34void lysp_node_free(struct ly_ctx *ctx, struct lysp_node *node);
Radek Krejcif09e4e82019-06-14 15:08:11 +020035void lysp_when_free(struct ly_ctx *ctx, struct lysp_when *when);
Radek Krejci2d7a47b2019-05-16 13:34:10 +020036
Michal Vasko63f3d842020-07-08 10:10:14 +020037LY_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 +010038LY_ERR buf_store_char(struct lys_yang_parser_ctx *ctx, enum yang_arg arg, char **word_p,
Radek Krejcib4ac5a92020-11-23 17:54:33 +010039 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 +010040LY_ERR get_keyword(struct lys_yang_parser_ctx *ctx, enum ly_stmt *kw, char **word_p, size_t *word_len);
41LY_ERR get_argument(struct lys_yang_parser_ctx *ctx, enum yang_arg arg,
Radek Krejcib4ac5a92020-11-23 17:54:33 +010042 uint16_t *flags, char **word_p, char **word_b, size_t *word_len);
Radek Krejci33090f92020-12-17 20:12:46 +010043LY_ERR skip_comment(struct lys_yang_parser_ctx *ctx, uint8_t comment);
Radek Krejci2d7a47b2019-05-16 13:34:10 +020044
Radek Krejci2a9fc652021-01-22 17:44:34 +010045LY_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 +010046LY_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 +010047LY_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 +010048LY_ERR parse_case(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
49LY_ERR parse_container(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
50LY_ERR parse_deviate(struct lys_yang_parser_ctx *ctx, struct lysp_deviate **deviates);
51LY_ERR parse_deviation(struct lys_yang_parser_ctx *ctx, struct lysp_deviation **deviations);
Radek Krejci2a9fc652021-01-22 17:44:34 +010052LY_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 +010053LY_ERR parse_choice(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
54LY_ERR parse_leaf(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
55LY_ERR parse_leaflist(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
56LY_ERR parse_list(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
57LY_ERR parse_maxelements(struct lys_yang_parser_ctx *ctx, uint32_t *max, uint16_t *flags, struct lysp_ext_instance **exts);
58LY_ERR parse_minelements(struct lys_yang_parser_ctx *ctx, uint32_t *min, uint16_t *flags, struct lysp_ext_instance **exts);
59LY_ERR parse_module(struct lys_yang_parser_ctx *ctx, struct lysp_module *mod);
Radek Krejci2a9fc652021-01-22 17:44:34 +010060LY_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 +010061LY_ERR parse_submodule(struct lys_yang_parser_ctx *ctx, struct lysp_submodule *submod);
62LY_ERR parse_uses(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
63LY_ERR parse_when(struct lys_yang_parser_ctx *ctx, struct lysp_when **when_p);
64LY_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 +020065
Radek Iša56ca9e42020-09-08 18:42:00 +020066struct lys_yang_parser_ctx *YCTX;
Radek Krejci80dd33e2018-09-26 15:57:18 +020067
Radek Krejci1640e802021-01-08 11:01:30 +010068struct ly_in in = {0};
69
Radek Krejci33090f92020-12-17 20:12:46 +010070#define YCTX_INIT \
Radek Krejcid54412f2020-12-17 20:25:35 +010071 in.line = 1; \
Radek Krejci2efc45b2020-12-22 16:25:44 +010072 YCTX->in = &in; \
73 LOG_LOCINIT(UTEST_LYCTX, NULL, NULL, NULL, &in)
Radek Krejci33090f92020-12-17 20:12:46 +010074
Michal Vasko5d24f6c2020-10-13 13:49:06 +020075static int
Radek Iša56ca9e42020-09-08 18:42:00 +020076setup(void **state)
Michal Vasko5d24f6c2020-10-13 13:49:06 +020077{
Michal Vasko8a67eff2021-12-07 14:04:47 +010078 struct lysp_module *pmod;
79
Radek Iša56ca9e42020-09-08 18:42:00 +020080 UTEST_SETUP;
Michal Vasko5d24f6c2020-10-13 13:49:06 +020081
Radek Iša56ca9e42020-09-08 18:42:00 +020082 /* allocate parser context */
83 YCTX = calloc(1, sizeof(*YCTX));
84 YCTX->format = LYS_IN_YANG;
Michal Vasko8a67eff2021-12-07 14:04:47 +010085 ly_set_new(&YCTX->parsed_mods);
Michal Vasko5d24f6c2020-10-13 13:49:06 +020086
Radek Iša56ca9e42020-09-08 18:42:00 +020087 /* allocate new parsed module */
Michal Vasko8a67eff2021-12-07 14:04:47 +010088 pmod = calloc(1, sizeof *pmod);
89 ly_set_add(YCTX->parsed_mods, pmod, 1, NULL);
Michal Vasko5d24f6c2020-10-13 13:49:06 +020090
Radek Iša56ca9e42020-09-08 18:42:00 +020091 /* allocate new module */
Michal Vasko8a67eff2021-12-07 14:04:47 +010092 pmod->mod = calloc(1, sizeof *pmod->mod);
93 pmod->mod->ctx = UTEST_LYCTX;
94 pmod->mod->parsed = pmod;
Michal Vasko5d24f6c2020-10-13 13:49:06 +020095
Radek Krejci1640e802021-01-08 11:01:30 +010096 /* initilize and use the global easily available and customizable input handler */
97 in.line = 1;
98 YCTX->in = &in;
Radek Krejciddace2c2021-01-08 11:30:56 +010099 LOG_LOCINIT(NULL, NULL, NULL, &in);
Radek Krejci1640e802021-01-08 11:01:30 +0100100
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200101 return 0;
102}
103
104static int
Radek Iša56ca9e42020-09-08 18:42:00 +0200105teardown(void **state)
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200106{
Michal Vasko8a67eff2021-12-07 14:04:47 +0100107 lys_module_free(PARSER_CUR_PMOD(YCTX)->mod);
Radek Krejciddace2c2021-01-08 11:30:56 +0100108 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejci1640e802021-01-08 11:01:30 +0100109
Michal Vasko8a67eff2021-12-07 14:04:47 +0100110 ly_set_free(YCTX->parsed_mods, NULL);
Radek Iša56ca9e42020-09-08 18:42:00 +0200111 free(YCTX);
112 YCTX = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200113
Radek Iša56ca9e42020-09-08 18:42:00 +0200114 UTEST_TEARDOWN;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200115
116 return 0;
117}
118
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200119#define TEST_DUP_GENERIC(PREFIX, MEMBER, VALUE1, VALUE2, FUNC, RESULT, LINE, CLEANUP) \
Michal Vasko63f3d842020-07-08 10:10:14 +0200120 in.current = PREFIX MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +0100121 assert_int_equal(LY_EVALID, FUNC(YCTX, RESULT)); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200122 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number "LINE".");\
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200123 CLEANUP
Radek Krejci44ceedc2018-10-02 15:54:31 +0200124static void
125test_helpers(void **state)
126{
Radek Krejci404251e2018-10-09 12:06:44 +0200127 char *buf, *p;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200128 size_t len, size;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200129 uint8_t prefix = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200130
131 /* storing into buffer */
Michal Vasko63f3d842020-07-08 10:10:14 +0200132 in.current = "abcd";
Radek Krejci44ceedc2018-10-02 15:54:31 +0200133 buf = NULL;
134 size = len = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200135 assert_int_equal(LY_SUCCESS, buf_add_char(NULL, &in, 2, &buf, &size, &len));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200136 assert_int_not_equal(0, size);
137 assert_int_equal(2, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200138 assert_string_equal("cd", in.current);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200139 assert_false(strncmp("ab", buf, 2));
140 free(buf);
Radek Krejci404251e2018-10-09 12:06:44 +0200141 buf = NULL;
142
143 /* invalid first characters */
144 len = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200145 in.current = "2invalid";
Radek Krejci33090f92020-12-17 20:12:46 +0100146 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 +0200147 in.current = ".invalid";
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 = "-invalid";
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 /* invalid following characters */
152 len = 3; /* number of characters read before the str content */
Michal Vasko63f3d842020-07-08 10:10:14 +0200153 in.current = "!";
Radek Krejci33090f92020-12-17 20:12:46 +0100154 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 +0200155 in.current = ":";
Radek Krejci33090f92020-12-17 20:12:46 +0100156 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 +0200157 /* valid colon for prefixed identifiers */
158 len = size = 0;
159 p = NULL;
David Sedlák40bb13b2019-07-10 14:34:18 +0200160 prefix = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200161 in.current = "x:id";
Radek Krejci33090f92020-12-17 20:12:46 +0100162 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 +0200163 assert_int_equal(1, len);
164 assert_null(buf);
Michal Vasko63f3d842020-07-08 10:10:14 +0200165 assert_string_equal(":id", in.current);
Radek Krejci404251e2018-10-09 12:06:44 +0200166 assert_int_equal('x', p[len - 1]);
Radek Krejci33090f92020-12-17 20:12:46 +0100167 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 +0200168 assert_int_equal(2, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200169 assert_string_equal("id", in.current);
Radek Krejci404251e2018-10-09 12:06:44 +0200170 assert_int_equal(':', p[len - 1]);
171 free(buf);
David Sedlák40bb13b2019-07-10 14:34:18 +0200172 prefix = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200173
174 /* checking identifiers */
Radek Iša56ca9e42020-09-08 18:42:00 +0200175 assert_int_equal(LY_EVALID, lysp_check_identifierchar((struct lys_parser_ctx *)YCTX, ':', 0, NULL));
176 CHECK_LOG_CTX("Invalid identifier character ':' (0x003a).", "Line number 1.");
177 assert_int_equal(LY_EVALID, lysp_check_identifierchar((struct lys_parser_ctx *)YCTX, '#', 1, NULL));
178 CHECK_LOG_CTX("Invalid identifier first character '#' (0x0023).", "Line number 1.");
Radek Krejci44ceedc2018-10-02 15:54:31 +0200179
Radek Iša56ca9e42020-09-08 18:42:00 +0200180 assert_int_equal(LY_SUCCESS, lysp_check_identifierchar((struct lys_parser_ctx *)YCTX, 'a', 1, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200181 assert_int_equal(0, prefix);
Radek Iša56ca9e42020-09-08 18:42:00 +0200182 assert_int_equal(LY_SUCCESS, lysp_check_identifierchar((struct lys_parser_ctx *)YCTX, ':', 0, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200183 assert_int_equal(1, prefix);
Radek Iša56ca9e42020-09-08 18:42:00 +0200184 assert_int_equal(LY_EVALID, lysp_check_identifierchar((struct lys_parser_ctx *)YCTX, ':', 0, &prefix));
Radek Krejcidcc7b322018-10-11 14:24:02 +0200185 assert_int_equal(1, prefix);
Radek Iša56ca9e42020-09-08 18:42:00 +0200186 assert_int_equal(LY_SUCCESS, lysp_check_identifierchar((struct lys_parser_ctx *)YCTX, 'b', 0, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200187 assert_int_equal(2, prefix);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200188 /* second colon is invalid */
Radek Iša56ca9e42020-09-08 18:42:00 +0200189 assert_int_equal(LY_EVALID, lysp_check_identifierchar((struct lys_parser_ctx *)YCTX, ':', 0, &prefix));
190 CHECK_LOG_CTX("Invalid identifier character ':' (0x003a).", "Line number 1.");
Radek Krejci44ceedc2018-10-02 15:54:31 +0200191}
Radek Krejci80dd33e2018-09-26 15:57:18 +0200192
Radek Krejcidd713ce2021-01-04 23:12:12 +0100193#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 +0200194 {\
Radek Krejcidd713ce2021-01-04 23:12:12 +0100195 const char * text = INPUT_TEXT;\
196 in.line = 1;\
Radek Iša56ca9e42020-09-08 18:42:00 +0200197 in.current = text;\
Radek Krejci33090f92020-12-17 20:12:46 +0100198 assert_int_equal(LY_SUCCESS, get_argument(CTX, Y_MAYBE_STR_ARG, NULL, &word, &buf, &len));\
Radek Iša56ca9e42020-09-08 18:42:00 +0200199 assert_string_equal(word, EXPECT_WORD);\
200 assert_int_equal(len, EXPECT_LEN);\
201 assert_string_equal(EXPECT_CURRENT, in.current);\
Radek Krejcidd713ce2021-01-04 23:12:12 +0100202 assert_int_equal(EXPECT_LINE, in.line);\
Radek Iša56ca9e42020-09-08 18:42:00 +0200203 }
204
Radek Krejci80dd33e2018-09-26 15:57:18 +0200205static void
206test_comments(void **state)
207{
Radek Krejciefd22f62018-09-27 11:47:58 +0200208 char *word, *buf;
209 size_t len;
Radek Krejci80dd33e2018-09-26 15:57:18 +0200210
Radek Krejcidd713ce2021-01-04 23:12:12 +0100211 TEST_GET_ARGUMENT_SUCCESS(" // this is a text of / one * line */ comment\nargument;",
212 YCTX, Y_STR_ARG, "argument;", 8, ";", 2);
Radek Krejciefd22f62018-09-27 11:47:58 +0200213 assert_null(buf);
Radek Krejci80dd33e2018-09-26 15:57:18 +0200214
Radek Krejcidd713ce2021-01-04 23:12:12 +0100215 TEST_GET_ARGUMENT_SUCCESS("/* this is a \n * text // of / block * comment */\"arg\" + \"ume\" \n + \n \"nt\";",
216 YCTX, Y_STR_ARG, "argument", 8, ";", 4);
Radek Krejciefd22f62018-09-27 11:47:58 +0200217 assert_ptr_equal(buf, word);
Radek Krejciefd22f62018-09-27 11:47:58 +0200218 free(word);
Radek Krejci80dd33e2018-09-26 15:57:18 +0200219
Radek Krejcidd713ce2021-01-04 23:12:12 +0100220 in.line = 1;
Michal Vasko63f3d842020-07-08 10:10:14 +0200221 in.current = " this is one line comment on last line";
Radek Krejci33090f92020-12-17 20:12:46 +0100222 assert_int_equal(LY_SUCCESS, skip_comment(YCTX, 1));
Michal Vasko63f3d842020-07-08 10:10:14 +0200223 assert_true(in.current[0] == '\0');
Radek Krejci80dd33e2018-09-26 15:57:18 +0200224
Radek Krejcidd713ce2021-01-04 23:12:12 +0100225 in.line = 1;
Michal Vasko63f3d842020-07-08 10:10:14 +0200226 in.current = " this is a not terminated comment x";
Radek Krejci33090f92020-12-17 20:12:46 +0100227 assert_int_equal(LY_EVALID, skip_comment(YCTX, 2));
Radek Krejcidd713ce2021-01-04 23:12:12 +0100228 CHECK_LOG_CTX("Unexpected end-of-input, non-terminated comment.", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200229 assert_true(in.current[0] == '\0');
Radek Krejci80dd33e2018-09-26 15:57:18 +0200230}
231
Radek Krejciefd22f62018-09-27 11:47:58 +0200232static void
233test_arg(void **state)
234{
Radek Krejciefd22f62018-09-27 11:47:58 +0200235 char *word, *buf;
236 size_t len;
237
238 /* missing argument */
Michal Vasko63f3d842020-07-08 10:10:14 +0200239 in.current = ";";
Radek Krejci33090f92020-12-17 20:12:46 +0100240 assert_int_equal(LY_SUCCESS, get_argument(YCTX, Y_MAYBE_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200241 assert_null(word);
242
Michal Vasko63f3d842020-07-08 10:10:14 +0200243 in.current = "{";
Radek Krejci33090f92020-12-17 20:12:46 +0100244 assert_int_equal(LY_EVALID, get_argument(YCTX, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200245 CHECK_LOG_CTX("Invalid character sequence \"{\", expected an argument.", "Line number 1.");
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200246
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200247 /* invalid escape sequence */
Michal Vasko63f3d842020-07-08 10:10:14 +0200248 in.current = "\"\\s\"";
Radek Krejci33090f92020-12-17 20:12:46 +0100249 assert_int_equal(LY_EVALID, get_argument(YCTX, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200250 CHECK_LOG_CTX("Double-quoted string unknown special character \'\\s\'.", "Line number 1.");
251
Radek Krejcidd713ce2021-01-04 23:12:12 +0100252 TEST_GET_ARGUMENT_SUCCESS("\'\\s\'", YCTX, Y_STR_ARG, "\\s\'", 2, "", 1);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200253
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200254 /* invalid character after the argument */
Michal Vasko63f3d842020-07-08 10:10:14 +0200255 in.current = "hello\"";
Radek Krejci33090f92020-12-17 20:12:46 +0100256 assert_int_equal(LY_EVALID, get_argument(YCTX, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200257 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 +0200258
Radek Iša56ca9e42020-09-08 18:42:00 +0200259 in.current = "hello}";
Radek Krejci33090f92020-12-17 20:12:46 +0100260 assert_int_equal(LY_EVALID, get_argument(YCTX, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200261 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 +0200262 /* invalid identifier-ref-arg-str */
Michal Vasko63f3d842020-07-08 10:10:14 +0200263 in.current = "pre:pre:value";
Radek Krejci33090f92020-12-17 20:12:46 +0100264 assert_int_equal(LY_EVALID, get_argument(YCTX, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200265 CHECK_LOG_CTX("Invalid identifier character ':' (0x003a).", "Line number 1.");
David Sedlák40bb13b2019-07-10 14:34:18 +0200266
Michal Vasko63f3d842020-07-08 10:10:14 +0200267 in.current = "\"\";"; /* empty identifier is not allowed */
Radek Krejci33090f92020-12-17 20:12:46 +0100268 assert_int_equal(LY_EVALID, get_argument(YCTX, Y_IDENTIF_ARG, NULL, &word, &buf, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200269 CHECK_LOG_CTX("Statement argument is required.", "Line number 1.");
270
Michal Vasko63f3d842020-07-08 10:10:14 +0200271 in.current = "\"\";"; /* empty reference identifier is not allowed */
Radek Krejci33090f92020-12-17 20:12:46 +0100272 assert_int_equal(LY_EVALID, get_argument(YCTX, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200273 CHECK_LOG_CTX("Statement argument is required.", "Line number 1.");
Radek Krejci4e199f52019-05-28 09:09:28 +0200274
Radek Iša56ca9e42020-09-08 18:42:00 +0200275 /* slash is not an invalid character */
Radek Krejcidd713ce2021-01-04 23:12:12 +0100276 TEST_GET_ARGUMENT_SUCCESS("hello/x\t", YCTX, Y_STR_ARG, "hello/x\t", 7, "\t", 1);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200277 assert_null(buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200278
279 /* different quoting */
Radek Krejcidd713ce2021-01-04 23:12:12 +0100280 TEST_GET_ARGUMENT_SUCCESS("hello/x\t", YCTX, Y_STR_ARG, "hello/x\t", 7, "\t", 1);
Radek Krejciefd22f62018-09-27 11:47:58 +0200281
Radek Krejcidd713ce2021-01-04 23:12:12 +0100282 TEST_GET_ARGUMENT_SUCCESS("hello ", YCTX, Y_STR_ARG, "hello ", 5, " ", 1);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200283
Radek Krejcidd713ce2021-01-04 23:12:12 +0100284 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 +0200285
Radek Krejcidd713ce2021-01-04 23:12:12 +0100286 TEST_GET_ARGUMENT_SUCCESS("\"hello\\n\\t\\\"\\\\\";", YCTX, Y_STR_ARG, "hello\n\t\"\\", 9, ";", 1);
fredgand49fe112019-10-21 20:51:50 +0800287 free(buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200288
Radek Iša56ca9e42020-09-08 18:42:00 +0200289 YCTX->indent = 14;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200290 /* - space and tabs before newline are stripped out
291 * - space and tabs after newline (indentation) are stripped out
292 */
Radek Krejcidd713ce2021-01-04 23:12:12 +0100293 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 +0200294 free(buf);
Radek Iša56ca9e42020-09-08 18:42:00 +0200295
296/* In contrast to previous, the backslash-escaped tabs are expanded after trimming, so they are preserved */
297 YCTX->indent = 14;
Radek Krejcidd713ce2021-01-04 23:12:12 +0100298 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 +0200299 assert_ptr_equal(word, buf);
Radek Krejciff13cd12019-10-25 15:34:24 +0200300 free(buf);
Radek Iša56ca9e42020-09-08 18:42:00 +0200301
Radek Krejciff13cd12019-10-25 15:34:24 +0200302 /* Do not handle whitespaces after backslash-escaped newline as indentation */
Radek Iša56ca9e42020-09-08 18:42:00 +0200303 YCTX->indent = 14;
Radek Krejcidd713ce2021-01-04 23:12:12 +0100304 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 +0200305 assert_ptr_equal(word, buf);
Radek Krejciff13cd12019-10-25 15:34:24 +0200306 free(buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200307
Radek Iša56ca9e42020-09-08 18:42:00 +0200308 YCTX->indent = 14;
Radek Krejcidd713ce2021-01-04 23:12:12 +0100309 TEST_GET_ARGUMENT_SUCCESS("\"hello\n \tworld!\"", YCTX, Y_STR_ARG, "hello\nworld!", 12, "", 2);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200310 assert_ptr_equal(word, buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200311 free(buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200312
Radek Krejcidd713ce2021-01-04 23:12:12 +0100313 TEST_GET_ARGUMENT_SUCCESS("\'hello\'", YCTX, Y_STR_ARG, "hello'", 5, "", 1);
Radek Krejciefd22f62018-09-27 11:47:58 +0200314
Radek Krejcidd713ce2021-01-04 23:12:12 +0100315 TEST_GET_ARGUMENT_SUCCESS("\"hel\" +\t\n\"lo\"", YCTX, Y_STR_ARG, "hello", 5, "", 2);
Radek Krejciefd22f62018-09-27 11:47:58 +0200316 assert_ptr_equal(word, buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200317 free(buf);
Radek Iša56ca9e42020-09-08 18:42:00 +0200318
Radek Krejcidd713ce2021-01-04 23:12:12 +0100319 in.line = 1;
Michal Vasko63f3d842020-07-08 10:10:14 +0200320 in.current = "\"hel\" +\t\nlo"; /* unquoted the second part */
Radek Krejci33090f92020-12-17 20:12:46 +0100321 assert_int_equal(LY_EVALID, get_argument(YCTX, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcidd713ce2021-01-04 23:12:12 +0100322 CHECK_LOG_CTX("Both string parts divided by '+' must be quoted.", "Line number 2.");
Radek Krejciefd22f62018-09-27 11:47:58 +0200323
Radek Krejcidd713ce2021-01-04 23:12:12 +0100324 TEST_GET_ARGUMENT_SUCCESS("\'he\'\t\n+ \"llo\"", YCTX, Y_STR_ARG, "hello", 5, "", 2);
Radek Krejciefd22f62018-09-27 11:47:58 +0200325 free(buf);
326
Radek Krejcidd713ce2021-01-04 23:12:12 +0100327 TEST_GET_ARGUMENT_SUCCESS(" \t\n\"he\"+\'llo\'", YCTX, Y_STR_ARG, "hello", 5, "", 2);
Radek Krejciefd22f62018-09-27 11:47:58 +0200328 free(buf);
329
Radek Krejci44ceedc2018-10-02 15:54:31 +0200330 /* missing argument */
Radek Krejcidd713ce2021-01-04 23:12:12 +0100331 in.line = 1;
Michal Vasko63f3d842020-07-08 10:10:14 +0200332 in.current = ";";
Radek Krejci33090f92020-12-17 20:12:46 +0100333 assert_int_equal(LY_EVALID, get_argument(YCTX, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcidd713ce2021-01-04 23:12:12 +0100334 CHECK_LOG_CTX("Invalid character sequence \";\", expected an argument.", "Line number 1.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200335}
336
Radek Iša56ca9e42020-09-08 18:42:00 +0200337#define TEST_STMS_SUCCESS(INPUT_TEXT, CTX, ACTION, EXPECT_WORD)\
338 in.current = INPUT_TEXT;\
Radek Krejci33090f92020-12-17 20:12:46 +0100339 assert_int_equal(LY_SUCCESS, get_keyword(CTX, &kw, &word, &len));\
Radek Iša56ca9e42020-09-08 18:42:00 +0200340 assert_int_equal(ACTION, kw);\
341 assert_int_equal(strlen(EXPECT_WORD), len);\
342 assert_true(0 == strncmp(EXPECT_WORD, word, len))
343
Radek Krejcidcc7b322018-10-11 14:24:02 +0200344static void
345test_stmts(void **state)
346{
Michal Vasko63f3d842020-07-08 10:10:14 +0200347 const char *p;
Radek Krejcid6b76452019-09-03 17:03:03 +0200348 enum ly_stmt kw;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200349 char *word;
350 size_t len;
351
Michal Vasko63f3d842020-07-08 10:10:14 +0200352 in.current = "\n// comment\n\tinput\t{";
Radek Krejci33090f92020-12-17 20:12:46 +0100353 assert_int_equal(LY_SUCCESS, get_keyword(YCTX, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200354 assert_int_equal(LY_STMT_INPUT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200355 assert_int_equal(5, len);
356 assert_string_equal("input\t{", word);
Michal Vasko63f3d842020-07-08 10:10:14 +0200357 assert_string_equal("\t{", in.current);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200358
Michal Vasko63f3d842020-07-08 10:10:14 +0200359 in.current = "\t /* comment */\t output\n\t{";
Radek Krejci33090f92020-12-17 20:12:46 +0100360 assert_int_equal(LY_SUCCESS, get_keyword(YCTX, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200361 assert_int_equal(LY_STMT_OUTPUT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200362 assert_int_equal(6, len);
363 assert_string_equal("output\n\t{", word);
Michal Vasko63f3d842020-07-08 10:10:14 +0200364 assert_string_equal("\n\t{", in.current);
Radek Krejci33090f92020-12-17 20:12:46 +0100365 assert_int_equal(LY_SUCCESS, get_keyword(YCTX, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200366 assert_int_equal(LY_STMT_SYNTAX_LEFT_BRACE, kw);
Radek Krejciabdd8062019-06-11 16:44:19 +0200367 assert_int_equal(1, len);
368 assert_string_equal("{", word);
Michal Vasko63f3d842020-07-08 10:10:14 +0200369 assert_string_equal("", in.current);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200370
Michal Vasko63f3d842020-07-08 10:10:14 +0200371 in.current = "/input { "; /* invalid slash */
Radek Krejci33090f92020-12-17 20:12:46 +0100372 assert_int_equal(LY_EVALID, get_keyword(YCTX, &kw, &word, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200373 CHECK_LOG_CTX("Invalid identifier first character '/'.", "Line number 4.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200374
Michal Vasko63f3d842020-07-08 10:10:14 +0200375 in.current = "not-a-statement-nor-extension { "; /* invalid identifier */
Radek Krejci33090f92020-12-17 20:12:46 +0100376 assert_int_equal(LY_EVALID, get_keyword(YCTX, &kw, &word, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200377 CHECK_LOG_CTX("Invalid character sequence \"not-a-statement-nor-extension\", expected a keyword.", "Line number 4.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200378
Michal Vasko63f3d842020-07-08 10:10:14 +0200379 in.current = "path;"; /* missing sep after the keyword */
Radek Krejci33090f92020-12-17 20:12:46 +0100380 assert_int_equal(LY_EVALID, get_keyword(YCTX, &kw, &word, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200381 CHECK_LOG_CTX("Invalid character sequence \"path;\", expected a keyword followed by a separator.", "Line number 4.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200382
Radek Iša56ca9e42020-09-08 18:42:00 +0200383 TEST_STMS_SUCCESS("action ", YCTX, LY_STMT_ACTION, "action");
384
385 TEST_STMS_SUCCESS("anydata ", YCTX, LY_STMT_ANYDATA, "anydata");
386 TEST_STMS_SUCCESS("anyxml ", YCTX, LY_STMT_ANYXML, "anyxml");
387 TEST_STMS_SUCCESS("argument ", YCTX, LY_STMT_ARGUMENT, "argument");
388 TEST_STMS_SUCCESS("augment ", YCTX, LY_STMT_AUGMENT, "augment");
389 TEST_STMS_SUCCESS("base ", YCTX, LY_STMT_BASE, "base");
390 TEST_STMS_SUCCESS("belongs-to ", YCTX, LY_STMT_BELONGS_TO, "belongs-to");
391 TEST_STMS_SUCCESS("bit ", YCTX, LY_STMT_BIT, "bit");
392 TEST_STMS_SUCCESS("case ", YCTX, LY_STMT_CASE, "case");
393 TEST_STMS_SUCCESS("choice ", YCTX, LY_STMT_CHOICE, "choice");
394 TEST_STMS_SUCCESS("config ", YCTX, LY_STMT_CONFIG, "config");
395 TEST_STMS_SUCCESS("contact ", YCTX, LY_STMT_CONTACT, "contact");
396 TEST_STMS_SUCCESS("container ", YCTX, LY_STMT_CONTAINER, "container");
397 TEST_STMS_SUCCESS("default ", YCTX, LY_STMT_DEFAULT, "default");
398 TEST_STMS_SUCCESS("description ", YCTX, LY_STMT_DESCRIPTION, "description");
399 TEST_STMS_SUCCESS("deviate ", YCTX, LY_STMT_DEVIATE, "deviate");
400 TEST_STMS_SUCCESS("deviation ", YCTX, LY_STMT_DEVIATION, "deviation");
401 TEST_STMS_SUCCESS("enum ", YCTX, LY_STMT_ENUM, "enum");
402 TEST_STMS_SUCCESS("error-app-tag ", YCTX, LY_STMT_ERROR_APP_TAG, "error-app-tag");
403 TEST_STMS_SUCCESS("error-message ", YCTX, LY_STMT_ERROR_MESSAGE, "error-message");
404 TEST_STMS_SUCCESS("extension ", YCTX, LY_STMT_EXTENSION, "extension");
405 TEST_STMS_SUCCESS("feature ", YCTX, LY_STMT_FEATURE, "feature");
406 TEST_STMS_SUCCESS("fraction-digits ", YCTX, LY_STMT_FRACTION_DIGITS, "fraction-digits");
407 TEST_STMS_SUCCESS("grouping ", YCTX, LY_STMT_GROUPING, "grouping");
408 TEST_STMS_SUCCESS("identity ", YCTX, LY_STMT_IDENTITY, "identity");
409 TEST_STMS_SUCCESS("if-feature ", YCTX, LY_STMT_IF_FEATURE, "if-feature");
410 TEST_STMS_SUCCESS("import ", YCTX, LY_STMT_IMPORT, "import");
411 TEST_STMS_SUCCESS("include ", YCTX, LY_STMT_INCLUDE, "include");
412 TEST_STMS_SUCCESS("input{", YCTX, LY_STMT_INPUT, "input");
413 TEST_STMS_SUCCESS("key ", YCTX, LY_STMT_KEY, "key");
414 TEST_STMS_SUCCESS("leaf ", YCTX, LY_STMT_LEAF, "leaf");
415 TEST_STMS_SUCCESS("leaf-list ", YCTX, LY_STMT_LEAF_LIST, "leaf-list");
416 TEST_STMS_SUCCESS("length ", YCTX, LY_STMT_LENGTH, "length");
417 TEST_STMS_SUCCESS("list ", YCTX, LY_STMT_LIST, "list");
418 TEST_STMS_SUCCESS("mandatory ", YCTX, LY_STMT_MANDATORY, "mandatory");
419 TEST_STMS_SUCCESS("max-elements ", YCTX, LY_STMT_MAX_ELEMENTS, "max-elements");
420 TEST_STMS_SUCCESS("min-elements ", YCTX, LY_STMT_MIN_ELEMENTS, "min-elements");
421 TEST_STMS_SUCCESS("modifier ", YCTX, LY_STMT_MODIFIER, "modifier");
422 TEST_STMS_SUCCESS("module ", YCTX, LY_STMT_MODULE, "module");
423 TEST_STMS_SUCCESS("must ", YCTX, LY_STMT_MUST, "must");
424 TEST_STMS_SUCCESS("namespace ", YCTX, LY_STMT_NAMESPACE, "namespace");
425 TEST_STMS_SUCCESS("notification ", YCTX, LY_STMT_NOTIFICATION, "notification");
426 TEST_STMS_SUCCESS("ordered-by ", YCTX, LY_STMT_ORDERED_BY, "ordered-by");
427 TEST_STMS_SUCCESS("organization ", YCTX, LY_STMT_ORGANIZATION, "organization");
428 TEST_STMS_SUCCESS("output ", YCTX, LY_STMT_OUTPUT, "output");
429 TEST_STMS_SUCCESS("path ", YCTX, LY_STMT_PATH, "path");
430 TEST_STMS_SUCCESS("pattern ", YCTX, LY_STMT_PATTERN, "pattern");
431 TEST_STMS_SUCCESS("position ", YCTX, LY_STMT_POSITION, "position");
432 TEST_STMS_SUCCESS("prefix ", YCTX, LY_STMT_PREFIX, "prefix");
433 TEST_STMS_SUCCESS("presence ", YCTX, LY_STMT_PRESENCE, "presence");
434 TEST_STMS_SUCCESS("range ", YCTX, LY_STMT_RANGE, "range");
435 TEST_STMS_SUCCESS("reference ", YCTX, LY_STMT_REFERENCE, "reference");
436 TEST_STMS_SUCCESS("refine ", YCTX, LY_STMT_REFINE, "refine");
437 TEST_STMS_SUCCESS("require-instance ", YCTX, LY_STMT_REQUIRE_INSTANCE, "require-instance");
438 TEST_STMS_SUCCESS("revision ", YCTX, LY_STMT_REVISION, "revision");
439 TEST_STMS_SUCCESS("revision-date ", YCTX, LY_STMT_REVISION_DATE, "revision-date");
440 TEST_STMS_SUCCESS("rpc ", YCTX, LY_STMT_RPC, "rpc");
441 TEST_STMS_SUCCESS("status ", YCTX, LY_STMT_STATUS, "status");
442 TEST_STMS_SUCCESS("submodule ", YCTX, LY_STMT_SUBMODULE, "submodule");
443 TEST_STMS_SUCCESS("type ", YCTX, LY_STMT_TYPE, "type");
444 TEST_STMS_SUCCESS("typedef ", YCTX, LY_STMT_TYPEDEF, "typedef");
445 TEST_STMS_SUCCESS("unique ", YCTX, LY_STMT_UNIQUE, "unique");
446 TEST_STMS_SUCCESS("units ", YCTX, LY_STMT_UNITS, "units");
447 TEST_STMS_SUCCESS("uses ", YCTX, LY_STMT_USES, "uses");
448 TEST_STMS_SUCCESS("value ", YCTX, LY_STMT_VALUE, "value");
449 TEST_STMS_SUCCESS("when ", YCTX, LY_STMT_WHEN, "when");
450 TEST_STMS_SUCCESS("yang-version ", YCTX, LY_STMT_YANG_VERSION, "yang-version");
451 TEST_STMS_SUCCESS("yin-element ", YCTX, LY_STMT_YIN_ELEMENT, "yin-element");
452 TEST_STMS_SUCCESS(";config false;", YCTX, LY_STMT_SYNTAX_SEMICOLON, ";");
Michal Vasko63f3d842020-07-08 10:10:14 +0200453 assert_string_equal("config false;", in.current);
Radek Iša56ca9e42020-09-08 18:42:00 +0200454 TEST_STMS_SUCCESS("{ config false;", YCTX, LY_STMT_SYNTAX_LEFT_BRACE, "{");
Michal Vasko63f3d842020-07-08 10:10:14 +0200455 assert_string_equal(" config false;", in.current);
Radek Iša56ca9e42020-09-08 18:42:00 +0200456 TEST_STMS_SUCCESS("}", YCTX, LY_STMT_SYNTAX_RIGHT_BRACE, "}");
Michal Vasko63f3d842020-07-08 10:10:14 +0200457 assert_string_equal("", in.current);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200458
459 /* geenric extension */
Michal Vasko63f3d842020-07-08 10:10:14 +0200460 in.current = p = "nacm:default-deny-write;";
Radek Krejci33090f92020-12-17 20:12:46 +0100461 assert_int_equal(LY_SUCCESS, get_keyword(YCTX, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200462 assert_int_equal(LY_STMT_EXTENSION_INSTANCE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200463 assert_int_equal(23, len);
464 assert_ptr_equal(p, word);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200465}
Radek Krejci44ceedc2018-10-02 15:54:31 +0200466
Radek Iša56ca9e42020-09-08 18:42:00 +0200467#define TEST_MINMAX_SUCCESS(INPUT_TEXT, CTX, TYPE, VALUE)\
468 in.current = INPUT_TEXT;\
469 if(TYPE == LYS_SET_MIN){\
Radek Krejci33090f92020-12-17 20:12:46 +0100470 assert_int_equal(LY_SUCCESS, parse_minelements(CTX, &value, &flags, &ext));\
Radek Iša56ca9e42020-09-08 18:42:00 +0200471 }\
472 if(TYPE == LYS_SET_MAX){\
Radek Krejci33090f92020-12-17 20:12:46 +0100473 assert_int_equal(LY_SUCCESS, parse_maxelements(CTX, &value, &flags, &ext));\
Radek Iša56ca9e42020-09-08 18:42:00 +0200474 }\
475 assert_int_equal(TYPE, flags);\
476 assert_int_equal(VALUE, value)
477
Radek Krejci05b13982018-11-28 16:22:07 +0100478static void
479test_minmax(void **state)
480{
Radek Krejci05b13982018-11-28 16:22:07 +0100481 uint16_t flags = 0;
482 uint32_t value = 0;
483 struct lysp_ext_instance *ext = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +0100484
Michal Vasko8a67eff2021-12-07 14:04:47 +0100485 PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
Radek Krejci05b13982018-11-28 16:22:07 +0100486
Michal Vasko63f3d842020-07-08 10:10:14 +0200487 in.current = " 1invalid; ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100488 assert_int_equal(LY_EVALID, parse_minelements(YCTX, &value, &flags, &ext));
Radek Iša56ca9e42020-09-08 18:42:00 +0200489 CHECK_LOG_CTX("Invalid value \"1invalid\" of \"min-elements\".", "Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100490
491 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200492 in.current = " -1; ...";
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("Invalid value \"-1\" of \"min-elements\".", "Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100495
Radek Krejcidf6cad12018-11-28 17:10:55 +0100496 /* implementation limit */
497 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200498 in.current = " 4294967296; ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100499 assert_int_equal(LY_EVALID, parse_minelements(YCTX, &value, &flags, &ext));
Radek Iša56ca9e42020-09-08 18:42:00 +0200500 CHECK_LOG_CTX("Value \"4294967296\" is out of \"min-elements\" bounds.", "Line number 1.");
Radek Krejcidf6cad12018-11-28 17:10:55 +0100501
Radek Krejci05b13982018-11-28 16:22:07 +0100502 flags = value = 0;
Radek Iša56ca9e42020-09-08 18:42:00 +0200503 TEST_MINMAX_SUCCESS(" 1; ...", YCTX, LYS_SET_MIN, 1);
Radek Krejci05b13982018-11-28 16:22:07 +0100504
505 flags = value = 0;
Radek Iša56ca9e42020-09-08 18:42:00 +0200506 TEST_MINMAX_SUCCESS(" 1 {m:ext;} ...", YCTX, LYS_SET_MIN, 1);
Radek Krejci05b13982018-11-28 16:22:07 +0100507 assert_non_null(ext);
Michal Vasko8a67eff2021-12-07 14:04:47 +0100508 FREE_ARRAY(PARSER_CUR_PMOD(YCTX)->mod->ctx, ext, lysp_ext_instance_free);
Radek Krejci05b13982018-11-28 16:22:07 +0100509 ext = NULL;
510
511 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200512 in.current = " 1 {config true;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100513 assert_int_equal(LY_EVALID, parse_minelements(YCTX, &value, &flags, &ext));
Radek Iša56ca9e42020-09-08 18:42:00 +0200514 CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"min-elements\".", "Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100515
Michal Vasko63f3d842020-07-08 10:10:14 +0200516 in.current = " 1invalid; ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100517 assert_int_equal(LY_EVALID, parse_maxelements(YCTX, &value, &flags, &ext));
Radek Iša56ca9e42020-09-08 18:42:00 +0200518 CHECK_LOG_CTX("Invalid value \"1invalid\" of \"max-elements\".", "Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100519
520 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200521 in.current = " -1; ...";
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("Invalid value \"-1\" of \"max-elements\".", "Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100524
Radek Krejcidf6cad12018-11-28 17:10:55 +0100525 /* implementation limit */
526 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200527 in.current = " 4294967296; ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100528 assert_int_equal(LY_EVALID, parse_maxelements(YCTX, &value, &flags, &ext));
Radek Iša56ca9e42020-09-08 18:42:00 +0200529 CHECK_LOG_CTX("Value \"4294967296\" is out of \"max-elements\" bounds.", "Line number 1.");
Radek Krejcidf6cad12018-11-28 17:10:55 +0100530
Radek Krejci05b13982018-11-28 16:22:07 +0100531 flags = value = 0;
Radek Iša56ca9e42020-09-08 18:42:00 +0200532 TEST_MINMAX_SUCCESS(" 1; ...", YCTX, LYS_SET_MAX, 1);
Radek Krejci05b13982018-11-28 16:22:07 +0100533
534 flags = value = 0;
Radek Iša56ca9e42020-09-08 18:42:00 +0200535 TEST_MINMAX_SUCCESS(" unbounded; ...", YCTX, LYS_SET_MAX, 0);
Radek Krejci05b13982018-11-28 16:22:07 +0100536
537 flags = value = 0;
Radek Iša56ca9e42020-09-08 18:42:00 +0200538 TEST_MINMAX_SUCCESS(" 1 {m:ext;} ...", YCTX, LYS_SET_MAX, 1);
Radek Krejci05b13982018-11-28 16:22:07 +0100539 assert_non_null(ext);
Michal Vasko8a67eff2021-12-07 14:04:47 +0100540 FREE_ARRAY(PARSER_CUR_PMOD(YCTX)->mod->ctx, ext, lysp_ext_instance_free);
Radek Krejci05b13982018-11-28 16:22:07 +0100541 ext = NULL;
542
543 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200544 in.current = " 1 {config true;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100545 assert_int_equal(LY_EVALID, parse_maxelements(YCTX, &value, &flags, &ext));
Radek Iša56ca9e42020-09-08 18:42:00 +0200546 CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"max-elements\".", "Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100547}
548
Radek Krejci9fcacc12018-10-11 15:59:11 +0200549static struct lysp_module *
Michal Vaskob36053d2020-03-26 15:49:30 +0100550mod_renew(struct lys_yang_parser_ctx *ctx)
Radek Krejci9fcacc12018-10-11 15:59:11 +0200551{
Michal Vasko8a67eff2021-12-07 14:04:47 +0100552 struct ly_ctx *ly_ctx = PARSER_CUR_PMOD(ctx)->mod->ctx;
553 struct lysp_module *pmod;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100554
Michal Vasko8a67eff2021-12-07 14:04:47 +0100555 lys_module_free(PARSER_CUR_PMOD(ctx)->mod);
556 pmod = calloc(1, sizeof *pmod);
557 ctx->parsed_mods->objs[0] = pmod;
558 pmod->mod = calloc(1, sizeof *pmod->mod);
559 pmod->mod->parsed = pmod;
560 pmod->mod->ctx = ly_ctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100561
Radek Krejci2efc45b2020-12-22 16:25:44 +0100562 ctx->in->line = 1;
563
Michal Vasko8a67eff2021-12-07 14:04:47 +0100564 return pmod;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100565}
566
567static struct lysp_submodule *
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200568submod_renew(struct lys_yang_parser_ctx *ctx)
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100569{
Michal Vasko8a67eff2021-12-07 14:04:47 +0100570 struct ly_ctx *ly_ctx = PARSER_CUR_PMOD(ctx)->mod->ctx;
571 struct lysp_submodule *submod;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200572
Michal Vasko8a67eff2021-12-07 14:04:47 +0100573 lys_module_free(PARSER_CUR_PMOD(ctx)->mod);
574 submod = calloc(1, sizeof *submod);
575 ctx->parsed_mods->objs[0] = submod;
576 submod->mod = calloc(1, sizeof *submod->mod);
577 lydict_insert(ly_ctx, "name", 0, &submod->mod->name);
578 submod->mod->parsed = (struct lysp_module *)submod;
579 submod->mod->ctx = ly_ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200580
Michal Vasko8a67eff2021-12-07 14:04:47 +0100581 return submod;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200582}
583
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100584static LY_ERR
585test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
586 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
587 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
Radek Krejcid33273d2018-10-25 14:55:52 +0200588{
589 *module_data = user_data;
590 *format = LYS_IN_YANG;
591 *free_module_data = NULL;
592 return LY_SUCCESS;
593}
594
Radek Krejci9fcacc12018-10-11 15:59:11 +0200595static void
596test_module(void **state)
597{
Radek Krejci40544fa2019-01-11 09:38:37 +0100598 struct lysp_module *mod = NULL;
599 struct lysp_submodule *submod = NULL;
600 struct lys_module *m;
Radek Iša56ca9e42020-09-08 18:42:00 +0200601 struct lys_yang_parser_ctx *ctx_p;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200602
Radek Iša56ca9e42020-09-08 18:42:00 +0200603 mod = mod_renew(YCTX);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200604
605 /* missing mandatory substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +0200606 in.current = " name {}";
Radek Krejci33090f92020-12-17 20:12:46 +0100607 assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100608 assert_string_equal("name", mod->mod->name);
Radek Iša56ca9e42020-09-08 18:42:00 +0200609 CHECK_LOG_CTX("Missing mandatory keyword \"namespace\" as a child of \"module\".", "Line number 1.");
Radek Krejci9fcacc12018-10-11 15:59:11 +0200610
Radek Iša56ca9e42020-09-08 18:42:00 +0200611 mod = mod_renew(YCTX);
Michal Vasko63f3d842020-07-08 10:10:14 +0200612 in.current = " name {namespace urn:x;}";
Radek Krejci33090f92020-12-17 20:12:46 +0100613 assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100614 assert_string_equal("urn:x", mod->mod->ns);
Radek Iša56ca9e42020-09-08 18:42:00 +0200615 CHECK_LOG_CTX("Missing mandatory keyword \"prefix\" as a child of \"module\".", "Line number 1.");
616 mod = mod_renew(YCTX);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200617
Michal Vasko63f3d842020-07-08 10:10:14 +0200618 in.current = " name {namespace urn:x;prefix \"x\";}";
Radek Krejci33090f92020-12-17 20:12:46 +0100619 assert_int_equal(LY_SUCCESS, parse_module(YCTX, mod));
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100620 assert_string_equal("x", mod->mod->prefix);
Radek Iša56ca9e42020-09-08 18:42:00 +0200621 mod = mod_renew(YCTX);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200622
Radek Krejci027d5802018-11-14 16:57:28 +0100623#define SCHEMA_BEGINNING " name {yang-version 1.1;namespace urn:x;prefix \"x\";"
624#define SCHEMA_BEGINNING2 " name {namespace urn:x;prefix \"x\";"
Radek Krejcia042ea12018-10-13 07:52:15 +0200625#define TEST_NODE(NODETYPE, INPUT, NAME) \
Michal Vasko63f3d842020-07-08 10:10:14 +0200626 in.current = SCHEMA_BEGINNING INPUT; \
Radek Krejci33090f92020-12-17 20:12:46 +0100627 assert_int_equal(LY_SUCCESS, parse_module(YCTX, mod)); \
Radek Krejcia042ea12018-10-13 07:52:15 +0200628 assert_non_null(mod->data); \
629 assert_int_equal(NODETYPE, mod->data->nodetype); \
630 assert_string_equal(NAME, mod->data->name); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200631 mod = mod_renew(YCTX);
Radek Krejcia042ea12018-10-13 07:52:15 +0200632#define TEST_GENERIC(INPUT, TARGET, TEST) \
Michal Vasko63f3d842020-07-08 10:10:14 +0200633 in.current = SCHEMA_BEGINNING INPUT; \
Radek Krejci33090f92020-12-17 20:12:46 +0100634 assert_int_equal(LY_SUCCESS, parse_module(YCTX, mod)); \
Radek Krejcia042ea12018-10-13 07:52:15 +0200635 assert_non_null(TARGET); \
636 TEST; \
Radek Iša56ca9e42020-09-08 18:42:00 +0200637 mod = mod_renew(YCTX);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100638#define TEST_DUP(MEMBER, VALUE1, VALUE2, LINE) \
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200639 TEST_DUP_GENERIC(SCHEMA_BEGINNING, MEMBER, VALUE1, VALUE2, \
Radek Iša56ca9e42020-09-08 18:42:00 +0200640 parse_module, mod, LINE, mod = mod_renew(YCTX))
Radek Krejcia042ea12018-10-13 07:52:15 +0200641
642 /* duplicated namespace, prefix */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100643 TEST_DUP("namespace", "y", "z", "1");
644 TEST_DUP("prefix", "y", "z", "1");
645 TEST_DUP("contact", "a", "b", "1");
646 TEST_DUP("description", "a", "b", "1");
647 TEST_DUP("organization", "a", "b", "1");
648 TEST_DUP("reference", "a", "b", "1");
Radek Krejcia042ea12018-10-13 07:52:15 +0200649
Radek Krejci70853c52018-10-15 14:46:16 +0200650 /* not allowed in module (submodule-specific) */
Michal Vasko63f3d842020-07-08 10:10:14 +0200651 in.current = SCHEMA_BEGINNING "belongs-to master {prefix m;}}";
Radek Krejci33090f92020-12-17 20:12:46 +0100652 assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
Radek Iša56ca9e42020-09-08 18:42:00 +0200653 CHECK_LOG_CTX("Invalid keyword \"belongs-to\" as a child of \"module\".", "Line number 1.");
654 mod = mod_renew(YCTX);
Radek Krejci70853c52018-10-15 14:46:16 +0200655
Radek Krejcia042ea12018-10-13 07:52:15 +0200656 /* anydata */
657 TEST_NODE(LYS_ANYDATA, "anydata test;}", "test");
658 /* anyxml */
659 TEST_NODE(LYS_ANYXML, "anyxml test;}", "test");
660 /* augment */
661 TEST_GENERIC("augment /somepath;}", mod->augments,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100662 assert_string_equal("/somepath", mod->augments[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200663 /* choice */
664 TEST_NODE(LYS_CHOICE, "choice test;}", "test");
665 /* contact 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100666 TEST_GENERIC("contact \"firstname\" + \n\t\" surname\";}", mod->mod->contact,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100667 assert_string_equal("firstname surname", mod->mod->contact));
Radek Krejcia042ea12018-10-13 07:52:15 +0200668 /* container */
669 TEST_NODE(LYS_CONTAINER, "container test;}", "test");
670 /* description 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100671 TEST_GENERIC("description \'some description\';}", mod->mod->dsc,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100672 assert_string_equal("some description", mod->mod->dsc));
Radek Krejcia042ea12018-10-13 07:52:15 +0200673 /* deviation */
674 TEST_GENERIC("deviation /somepath {deviate not-supported;}}", mod->deviations,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100675 assert_string_equal("/somepath", mod->deviations[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200676 /* extension */
677 TEST_GENERIC("extension test;}", mod->extensions,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100678 assert_string_equal("test", mod->extensions[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200679 /* feature */
680 TEST_GENERIC("feature test;}", mod->features,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100681 assert_string_equal("test", mod->features[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200682 /* grouping */
683 TEST_GENERIC("grouping grp;}", mod->groupings,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100684 assert_string_equal("grp", mod->groupings[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200685 /* identity */
686 TEST_GENERIC("identity test;}", mod->identities,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100687 assert_string_equal("test", mod->identities[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200688 /* import */
Michal Vasko8a67eff2021-12-07 14:04:47 +0100689 ly_ctx_set_module_imp_clb(PARSER_CUR_PMOD(YCTX)->mod->ctx, test_imp_clb, "module zzz { namespace urn:zzz; prefix z;}");
Radek Krejci086c7132018-10-26 15:29:04 +0200690 TEST_GENERIC("import zzz {prefix z;}}", mod->imports,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100691 assert_string_equal("zzz", mod->imports[0].name));
Radek Krejci70853c52018-10-15 14:46:16 +0200692
Radek Krejcia042ea12018-10-13 07:52:15 +0200693 /* import - prefix collision */
Michal Vasko63f3d842020-07-08 10:10:14 +0200694 in.current = SCHEMA_BEGINNING "import zzz {prefix x;}}";
Radek Krejci33090f92020-12-17 20:12:46 +0100695 assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
Radek Krejci2efc45b2020-12-22 16:25:44 +0100696 CHECK_LOG_CTX("Prefix \"x\" already used as module prefix.", "Line number 1.");
Radek Iša56ca9e42020-09-08 18:42:00 +0200697 mod = mod_renew(YCTX);
698
Michal Vasko63f3d842020-07-08 10:10:14 +0200699 in.current = SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix y;}}";
Radek Krejci33090f92020-12-17 20:12:46 +0100700 assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
Radek Krejci2efc45b2020-12-22 16:25:44 +0100701 CHECK_LOG_CTX("Prefix \"y\" already used to import \"zzz\" module.", "Line number 1.");
Radek Iša56ca9e42020-09-08 18:42:00 +0200702
703 mod = mod_renew(YCTX);
Radek Krejciddace2c2021-01-08 11:30:56 +0100704 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100705
Michal Vasko7c8439f2020-08-05 13:25:19 +0200706 in.current = "module name10 {yang-version 1.1;namespace urn:x;prefix \"x\";import zzz {prefix y;}import zzz {prefix z;}}";
Michal Vasko8a67eff2021-12-07 14:04:47 +0100707 assert_int_equal(lys_parse_mem(PARSER_CUR_PMOD(YCTX)->mod->ctx, in.current, LYS_IN_YANG, NULL), LY_SUCCESS);
Radek Iša56ca9e42020-09-08 18:42:00 +0200708 CHECK_LOG_CTX("Single revision of the module \"zzz\" imported twice.", NULL);
Radek Krejci70853c52018-10-15 14:46:16 +0200709
Radek Krejcia042ea12018-10-13 07:52:15 +0200710 /* include */
Michal Vasko8a67eff2021-12-07 14:04:47 +0100711 ly_ctx_set_module_imp_clb(PARSER_CUR_PMOD(YCTX)->mod->ctx, test_imp_clb, "module xxx { namespace urn:xxx; prefix x;}");
Michal Vasko63f3d842020-07-08 10:10:14 +0200712 in.current = "module" SCHEMA_BEGINNING "include xxx;}";
Michal Vasko8a67eff2021-12-07 14:04:47 +0100713 assert_int_equal(lys_parse_mem(PARSER_CUR_PMOD(YCTX)->mod->ctx, in.current, LYS_IN_YANG, NULL), LY_EVALID);
Radek Krejci8297b792020-08-16 14:49:05 +0200714 CHECK_LOG_CTX("Parsing module \"name\" failed.", NULL, "Including \"xxx\" submodule into \"name\" failed.", NULL);
Radek Krejcid33273d2018-10-25 14:55:52 +0200715
Michal Vasko8a67eff2021-12-07 14:04:47 +0100716 ly_ctx_set_module_imp_clb(PARSER_CUR_PMOD(YCTX)->mod->ctx, test_imp_clb, "submodule xxx {belongs-to wrong-name {prefix w;}}");
Michal Vasko63f3d842020-07-08 10:10:14 +0200717 in.current = "module" SCHEMA_BEGINNING "include xxx;}";
Michal Vasko8a67eff2021-12-07 14:04:47 +0100718 assert_int_equal(lys_parse_mem(PARSER_CUR_PMOD(YCTX)->mod->ctx, in.current, LYS_IN_YANG, NULL), LY_EVALID);
Radek Krejci8297b792020-08-16 14:49:05 +0200719 CHECK_LOG_CTX("Parsing module \"name\" failed.", NULL, "Including \"xxx\" submodule into \"name\" failed.", NULL);
Radek Krejcid33273d2018-10-25 14:55:52 +0200720
Michal Vasko8a67eff2021-12-07 14:04:47 +0100721 ly_ctx_set_module_imp_clb(PARSER_CUR_PMOD(YCTX)->mod->ctx, test_imp_clb, "submodule xxx {belongs-to name {prefix x;}}");
Radek Krejcid33273d2018-10-25 14:55:52 +0200722 TEST_GENERIC("include xxx;}", mod->includes,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100723 assert_string_equal("xxx", mod->includes[0].name));
Radek Krejcid33273d2018-10-25 14:55:52 +0200724
Radek Krejcia042ea12018-10-13 07:52:15 +0200725 /* leaf */
726 TEST_NODE(LYS_LEAF, "leaf test {type string;}}", "test");
727 /* leaf-list */
728 TEST_NODE(LYS_LEAFLIST, "leaf-list test {type string;}}", "test");
729 /* list */
730 TEST_NODE(LYS_LIST, "list test {key a;leaf a {type string;}}}", "test");
731 /* notification */
732 TEST_GENERIC("notification test;}", mod->notifs,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100733 assert_string_equal("test", mod->notifs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200734 /* organization 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100735 TEST_GENERIC("organization \"CESNET a.l.e.\";}", mod->mod->org,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100736 assert_string_equal("CESNET a.l.e.", mod->mod->org));
Radek Krejcia042ea12018-10-13 07:52:15 +0200737 /* reference 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100738 TEST_GENERIC("reference RFC7950;}", mod->mod->ref,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100739 assert_string_equal("RFC7950", mod->mod->ref));
Radek Krejcia042ea12018-10-13 07:52:15 +0200740 /* revision */
741 TEST_GENERIC("revision 2018-10-12;}", mod->revs,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100742 assert_string_equal("2018-10-12", mod->revs[0].date));
Radek Krejcia042ea12018-10-13 07:52:15 +0200743 /* rpc */
744 TEST_GENERIC("rpc test;}", mod->rpcs,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100745 assert_string_equal("test", mod->rpcs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200746 /* typedef */
747 TEST_GENERIC("typedef test{type string;}}", mod->typedefs,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100748 assert_string_equal("test", mod->typedefs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200749 /* uses */
750 TEST_NODE(LYS_USES, "uses test;}", "test");
751 /* yang-version */
Michal Vasko63f3d842020-07-08 10:10:14 +0200752 in.current = SCHEMA_BEGINNING2 "\n\tyang-version 10;}";
Radek Krejci33090f92020-12-17 20:12:46 +0100753 assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
Radek Krejci2efc45b2020-12-22 16:25:44 +0100754 CHECK_LOG_CTX("Invalid value \"10\" of \"yang-version\".", NULL);
Radek Iša56ca9e42020-09-08 18:42:00 +0200755 mod = mod_renew(YCTX);
Radek Krejci96e48da2020-09-04 13:18:06 +0200756 in.current = SCHEMA_BEGINNING2 "yang-version 1;yang-version 1.1;}";
Radek Krejci33090f92020-12-17 20:12:46 +0100757 assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
Radek Krejci2efc45b2020-12-22 16:25:44 +0100758 CHECK_LOG_CTX("Duplicate keyword \"yang-version\".", NULL);
Radek Iša56ca9e42020-09-08 18:42:00 +0200759 mod = mod_renew(YCTX);
Radek Krejci96e48da2020-09-04 13:18:06 +0200760 in.current = SCHEMA_BEGINNING2 "yang-version 1;}";
Radek Krejci33090f92020-12-17 20:12:46 +0100761 assert_int_equal(LY_SUCCESS, parse_module(YCTX, mod));
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200762 assert_int_equal(1, mod->version);
Radek Iša56ca9e42020-09-08 18:42:00 +0200763 mod = mod_renew(YCTX);
Michal Vasko63f3d842020-07-08 10:10:14 +0200764 in.current = SCHEMA_BEGINNING2 "yang-version \"1.1\";}";
Radek Krejci33090f92020-12-17 20:12:46 +0100765 assert_int_equal(LY_SUCCESS, parse_module(YCTX, mod));
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200766 assert_int_equal(2, mod->version);
Radek Iša56ca9e42020-09-08 18:42:00 +0200767 mod = mod_renew(YCTX);
Radek Krejci40544fa2019-01-11 09:38:37 +0100768
Michal Vasko63f3d842020-07-08 10:10:14 +0200769 in.current = "module " SCHEMA_BEGINNING "} module q {namespace urn:q;prefixq;}";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200770 m = calloc(1, sizeof *m);
Michal Vasko8a67eff2021-12-07 14:04:47 +0100771 m->ctx = PARSER_CUR_PMOD(YCTX)->mod->ctx;
aPiecekc3e26142021-06-22 14:25:49 +0200772 assert_int_equal(LY_EVALID, yang_parse_module(&ctx_p, &in, m));
Radek Iša56ca9e42020-09-08 18:42:00 +0200773 CHECK_LOG_CTX("Trailing garbage \"module q {names...\" after module, expected end-of-input.", "Line number 1.");
Michal Vaskob36053d2020-03-26 15:49:30 +0100774 yang_parser_ctx_free(ctx_p);
Radek Krejci90ed21e2021-04-12 14:47:46 +0200775 lys_module_free(m);
Radek Krejci40544fa2019-01-11 09:38:37 +0100776
Michal Vasko63f3d842020-07-08 10:10:14 +0200777 in.current = "prefix " SCHEMA_BEGINNING "}";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200778 m = calloc(1, sizeof *m);
Michal Vasko8a67eff2021-12-07 14:04:47 +0100779 m->ctx = PARSER_CUR_PMOD(YCTX)->mod->ctx;
aPiecekc3e26142021-06-22 14:25:49 +0200780 assert_int_equal(LY_EVALID, yang_parse_module(&ctx_p, &in, m));
Radek Iša56ca9e42020-09-08 18:42:00 +0200781 CHECK_LOG_CTX("Invalid keyword \"prefix\", expected \"module\" or \"submodule\".", "Line number 1.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200782 yang_parser_ctx_free(ctx_p);
Radek Krejci90ed21e2021-04-12 14:47:46 +0200783 lys_module_free(m);
Radek Krejci09306362018-10-15 15:26:01 +0200784
Michal Vasko63f3d842020-07-08 10:10:14 +0200785 in.current = "module " SCHEMA_BEGINNING "leaf enum {type enumeration {enum seven { position 7;}}}}";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200786 m = calloc(1, sizeof *m);
Michal Vasko8a67eff2021-12-07 14:04:47 +0100787 m->ctx = PARSER_CUR_PMOD(YCTX)->mod->ctx;
aPiecekc3e26142021-06-22 14:25:49 +0200788 assert_int_equal(LY_EVALID, yang_parse_module(&ctx_p, &in, m));
Radek Iša56ca9e42020-09-08 18:42:00 +0200789 CHECK_LOG_CTX("Invalid keyword \"position\" as a child of \"enum\".", "Line number 1.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200790 yang_parser_ctx_free(ctx_p);
Radek Krejci90ed21e2021-04-12 14:47:46 +0200791 lys_module_free(m);
David Sedlák9fb515f2019-07-11 10:33:58 +0200792
Radek Krejci156ccaf2018-10-15 15:49:17 +0200793 /* extensions */
794 TEST_GENERIC("prefix:test;}", mod->exts,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100795 assert_string_equal("prefix:test", mod->exts[0].name);
Radek Krejciab430862021-03-02 20:13:40 +0100796 assert_int_equal(LY_STMT_MODULE, mod->exts[0].parent_stmt));
Radek Iša56ca9e42020-09-08 18:42:00 +0200797 mod = mod_renew(YCTX);
Radek Krejci156ccaf2018-10-15 15:49:17 +0200798
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200799 /* invalid substatement */
Michal Vasko63f3d842020-07-08 10:10:14 +0200800 in.current = SCHEMA_BEGINNING "must false;}";
Radek Krejci33090f92020-12-17 20:12:46 +0100801 assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
Radek Krejci2efc45b2020-12-22 16:25:44 +0100802 CHECK_LOG_CTX("Invalid keyword \"must\" as a child of \"module\".", NULL);
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200803
Radek Krejci09306362018-10-15 15:26:01 +0200804 /* submodule */
Radek Iša56ca9e42020-09-08 18:42:00 +0200805 submod = submod_renew(YCTX);
Radek Krejci09306362018-10-15 15:26:01 +0200806
807 /* missing mandatory substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +0200808 in.current = " subname {}";
Radek Krejci33090f92020-12-17 20:12:46 +0100809 assert_int_equal(LY_EVALID, parse_submodule(YCTX, submod));
Radek Krejci2efc45b2020-12-22 16:25:44 +0100810 CHECK_LOG_CTX("Missing mandatory keyword \"belongs-to\" as a child of \"submodule\".", NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100811 assert_string_equal("subname", submod->name);
Radek Iša56ca9e42020-09-08 18:42:00 +0200812
813 submod = submod_renew(YCTX);
Radek Krejci09306362018-10-15 15:26:01 +0200814
Michal Vasko63f3d842020-07-08 10:10:14 +0200815 in.current = " subname {belongs-to name {prefix x;}}";
Radek Krejci33090f92020-12-17 20:12:46 +0100816 assert_int_equal(LY_SUCCESS, parse_submodule(YCTX, submod));
Michal Vaskoc3781c32020-10-06 14:04:08 +0200817 assert_string_equal("name", submod->mod->name);
Radek Iša56ca9e42020-09-08 18:42:00 +0200818 submod = submod_renew(YCTX);
Radek Krejci09306362018-10-15 15:26:01 +0200819
820#undef SCHEMA_BEGINNING
Radek Krejci313d9902018-11-08 09:42:58 +0100821#define SCHEMA_BEGINNING " subname {belongs-to name {prefix x;}"
Radek Krejci09306362018-10-15 15:26:01 +0200822
823 /* duplicated namespace, prefix */
Michal Vasko63f3d842020-07-08 10:10:14 +0200824 in.current = " subname {belongs-to name {prefix x;}belongs-to module1;belongs-to module2;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100825 assert_int_equal(LY_EVALID, parse_submodule(YCTX, submod));
Radek Krejci2efc45b2020-12-22 16:25:44 +0100826 CHECK_LOG_CTX("Duplicate keyword \"belongs-to\".", NULL);
Radek Iša56ca9e42020-09-08 18:42:00 +0200827 submod = submod_renew(YCTX);
Radek Krejci09306362018-10-15 15:26:01 +0200828
829 /* not allowed in submodule (module-specific) */
Michal Vasko63f3d842020-07-08 10:10:14 +0200830 in.current = SCHEMA_BEGINNING "namespace \"urn:z\";}";
Radek Krejci33090f92020-12-17 20:12:46 +0100831 assert_int_equal(LY_EVALID, parse_submodule(YCTX, submod));
Radek Krejci2efc45b2020-12-22 16:25:44 +0100832 CHECK_LOG_CTX("Invalid keyword \"namespace\" as a child of \"submodule\".", NULL);
Radek Iša56ca9e42020-09-08 18:42:00 +0200833 submod = submod_renew(YCTX);
Michal Vasko63f3d842020-07-08 10:10:14 +0200834 in.current = SCHEMA_BEGINNING "prefix m;}}";
Radek Krejci33090f92020-12-17 20:12:46 +0100835 assert_int_equal(LY_EVALID, parse_submodule(YCTX, submod));
Radek Krejci2efc45b2020-12-22 16:25:44 +0100836 CHECK_LOG_CTX("Invalid keyword \"prefix\" as a child of \"submodule\".", NULL);
Radek Iša56ca9e42020-09-08 18:42:00 +0200837 submod = submod_renew(YCTX);
Radek Krejcia042ea12018-10-13 07:52:15 +0200838
Michal Vasko63f3d842020-07-08 10:10:14 +0200839 in.current = "submodule " SCHEMA_BEGINNING "} module q {namespace urn:q;prefixq;}";
Michal Vasko8a67eff2021-12-07 14:04:47 +0100840 assert_int_equal(LY_EVALID, yang_parse_submodule(&ctx_p, PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lys_parser_ctx *)YCTX, YCTX->in, &submod));
Radek Iša56ca9e42020-09-08 18:42:00 +0200841 CHECK_LOG_CTX("Trailing garbage \"module q {names...\" after submodule, expected end-of-input.", "Line number 1.");
Michal Vaskob36053d2020-03-26 15:49:30 +0100842 yang_parser_ctx_free(ctx_p);
Radek Krejci40544fa2019-01-11 09:38:37 +0100843
Michal Vasko63f3d842020-07-08 10:10:14 +0200844 in.current = "prefix " SCHEMA_BEGINNING "}";
Michal Vasko8a67eff2021-12-07 14:04:47 +0100845 assert_int_equal(LY_EVALID, yang_parse_submodule(&ctx_p, PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lys_parser_ctx *)YCTX, YCTX->in, &submod));
Radek Iša56ca9e42020-09-08 18:42:00 +0200846 CHECK_LOG_CTX("Invalid keyword \"prefix\", expected \"module\" or \"submodule\".", "Line number 1.");
Michal Vaskob36053d2020-03-26 15:49:30 +0100847 yang_parser_ctx_free(ctx_p);
Radek Iša56ca9e42020-09-08 18:42:00 +0200848 submod = submod_renew(YCTX);
Radek Krejci40544fa2019-01-11 09:38:37 +0100849
Radek Krejcia042ea12018-10-13 07:52:15 +0200850#undef TEST_GENERIC
851#undef TEST_NODE
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200852#undef TEST_DUP
Radek Krejcia042ea12018-10-13 07:52:15 +0200853#undef SCHEMA_BEGINNING
Radek Krejciefd22f62018-09-27 11:47:58 +0200854}
855
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200856static void
857test_deviation(void **state)
858{
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200859 struct lysp_deviation *d = NULL;
Radek Krejci33090f92020-12-17 20:12:46 +0100860
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200861 /* invalid cardinality */
862#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
863 TEST_DUP_GENERIC(" test {deviate not-supported;", MEMBER, VALUE1, VALUE2, parse_deviation, \
Michal Vasko8a67eff2021-12-07 14:04:47 +0100864 &d, "1", FREE_ARRAY(PARSER_CUR_PMOD(YCTX)->mod->ctx, d, lysp_deviation_free); d = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200865
866 TEST_DUP("description", "a", "b");
867 TEST_DUP("reference", "a", "b");
868
869 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +0200870 in.current = " test {deviate not-supported;description text;reference \'another text\';prefix:ext;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100871 assert_int_equal(LY_SUCCESS, parse_deviation(YCTX, &d));
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200872 assert_non_null(d);
Michal Vasko63f3d842020-07-08 10:10:14 +0200873 assert_string_equal(" ...", in.current);
Michal Vasko8a67eff2021-12-07 14:04:47 +0100874 FREE_ARRAY(PARSER_CUR_PMOD(YCTX)->mod->ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200875 d = NULL;
876
877 /* missing mandatory substatement */
Michal Vasko63f3d842020-07-08 10:10:14 +0200878 in.current = " test {description text;}";
Radek Krejci33090f92020-12-17 20:12:46 +0100879 assert_int_equal(LY_EVALID, parse_deviation(YCTX, &d));
Radek Iša56ca9e42020-09-08 18:42:00 +0200880 CHECK_LOG_CTX("Missing mandatory keyword \"deviate\" as a child of \"deviation\".", "Line number 1.");
Michal Vasko8a67eff2021-12-07 14:04:47 +0100881 FREE_ARRAY(PARSER_CUR_PMOD(YCTX)->mod->ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200882 d = NULL;
883
884 /* invalid substatement */
Michal Vasko63f3d842020-07-08 10:10:14 +0200885 in.current = " test {deviate not-supported; status obsolete;}";
Radek Krejci33090f92020-12-17 20:12:46 +0100886 assert_int_equal(LY_EVALID, parse_deviation(YCTX, &d));
Radek Iša56ca9e42020-09-08 18:42:00 +0200887 CHECK_LOG_CTX("Invalid keyword \"status\" as a child of \"deviation\".", "Line number 1.");
Michal Vasko8a67eff2021-12-07 14:04:47 +0100888 FREE_ARRAY(PARSER_CUR_PMOD(YCTX)->mod->ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200889 d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200890#undef TEST_DUP
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200891}
892
Radek Iša56ca9e42020-09-08 18:42:00 +0200893#define TEST_DEVIATE_SUCCESS(INPUT_TEXT, REMAIN_TEXT)\
894 in.current = INPUT_TEXT;\
Radek Krejci33090f92020-12-17 20:12:46 +0100895 assert_int_equal(LY_SUCCESS, parse_deviate(YCTX, &d));\
Radek Iša56ca9e42020-09-08 18:42:00 +0200896 assert_non_null(d);\
897 assert_string_equal(REMAIN_TEXT, in.current);\
Michal Vasko8a67eff2021-12-07 14:04:47 +0100898 lysp_deviate_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, d); free(d); d = NULL
Radek Iša56ca9e42020-09-08 18:42:00 +0200899
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200900static void
901test_deviate(void **state)
902{
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200903 struct lysp_deviate *d = NULL;
Radek Krejci33090f92020-12-17 20:12:46 +0100904
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200905 /* invalid cardinality */
906#define TEST_DUP(TYPE, MEMBER, VALUE1, VALUE2) \
907 TEST_DUP_GENERIC(TYPE" {", MEMBER, VALUE1, VALUE2, parse_deviate, \
Michal Vasko8a67eff2021-12-07 14:04:47 +0100908 &d, "1", lysp_deviate_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, d); free(d); d = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200909
910 TEST_DUP("add", "config", "true", "false");
Radek Iša07767952021-01-31 20:54:24 +0100911#if 0
912 /*test on int8 now is in file tests/utest/types/int8.c */
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200913 TEST_DUP("replace", "default", "int8", "uint8");
Radek Iša07767952021-01-31 20:54:24 +0100914#endif
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200915 TEST_DUP("add", "mandatory", "true", "false");
916 TEST_DUP("add", "max-elements", "1", "2");
917 TEST_DUP("add", "min-elements", "1", "2");
Radek Iša07767952021-01-31 20:54:24 +0100918#if 0
919 /*test on int8 now is in file tests/utest/types/int8.c*/
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200920 TEST_DUP("replace", "type", "int8", "uint8");
Radek Iša07767952021-01-31 20:54:24 +0100921#endif
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200922 TEST_DUP("add", "units", "kilometers", "miles");
923
924 /* full contents */
Radek Iša56ca9e42020-09-08 18:42:00 +0200925 TEST_DEVIATE_SUCCESS(" not-supported {prefix:ext;} ...", " ...");
926 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;} ...", " ...");
927 TEST_DEVIATE_SUCCESS(" delete {units meters; must 1; must 2; unique x; unique y; default a; default b; prefix:ext;} ...", " ...");
928 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 +0200929
930 /* invalid substatements */
931#define TEST_NOT_SUP(DEV, STMT, VALUE) \
Michal Vasko63f3d842020-07-08 10:10:14 +0200932 in.current = " "DEV" {"STMT" "VALUE";}..."; \
Radek Krejci33090f92020-12-17 20:12:46 +0100933 assert_int_equal(LY_EVALID, parse_deviate(YCTX, &d)); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200934 CHECK_LOG_CTX("Deviate \""DEV"\" does not support keyword \""STMT"\".", "Line number 1.");\
Michal Vasko8a67eff2021-12-07 14:04:47 +0100935 lysp_deviate_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, d); free(d); d = NULL
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200936
937 TEST_NOT_SUP("not-supported", "units", "meters");
938 TEST_NOT_SUP("not-supported", "must", "1");
939 TEST_NOT_SUP("not-supported", "unique", "x");
940 TEST_NOT_SUP("not-supported", "default", "a");
941 TEST_NOT_SUP("not-supported", "config", "true");
942 TEST_NOT_SUP("not-supported", "mandatory", "true");
943 TEST_NOT_SUP("not-supported", "min-elements", "1");
944 TEST_NOT_SUP("not-supported", "max-elements", "2");
945 TEST_NOT_SUP("not-supported", "type", "string");
946 TEST_NOT_SUP("add", "type", "string");
947 TEST_NOT_SUP("delete", "config", "true");
948 TEST_NOT_SUP("delete", "mandatory", "true");
949 TEST_NOT_SUP("delete", "min-elements", "1");
950 TEST_NOT_SUP("delete", "max-elements", "2");
951 TEST_NOT_SUP("delete", "type", "string");
952 TEST_NOT_SUP("replace", "must", "1");
953 TEST_NOT_SUP("replace", "unique", "a");
954
Michal Vasko63f3d842020-07-08 10:10:14 +0200955 in.current = " nonsence; ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100956 assert_int_equal(LY_EVALID, parse_deviate(YCTX, &d));
Radek Iša56ca9e42020-09-08 18:42:00 +0200957 CHECK_LOG_CTX("Invalid value \"nonsence\" of \"deviate\".", "Line number 1.");\
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200958 assert_null(d);
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200959#undef TEST_NOT_SUP
960#undef TEST_DUP
Radek Krejci4c6d9bd2018-10-15 16:43:06 +0200961}
962
Radek Krejci8c370832018-11-02 15:10:03 +0100963static void
964test_container(void **state)
965{
Radek Krejci8c370832018-11-02 15:10:03 +0100966 struct lysp_node_container *c = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +0100967
Michal Vasko8a67eff2021-12-07 14:04:47 +0100968 PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
aPiecek8d4e75d2021-06-24 14:47:06 +0200969 YCTX->main_ctx = (struct lys_parser_ctx *)YCTX;
Radek Krejci8c370832018-11-02 15:10:03 +0100970
971 /* invalid cardinality */
972#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +0200973 in.current = "cont {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +0100974 assert_int_equal(LY_EVALID, parse_container(YCTX, NULL, (struct lysp_node**)&c)); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200975 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Michal Vasko8a67eff2021-12-07 14:04:47 +0100976 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +0100977
978 TEST_DUP("config", "true", "false");
979 TEST_DUP("description", "text1", "text2");
980 TEST_DUP("presence", "true", "false");
981 TEST_DUP("reference", "1", "2");
982 TEST_DUP("status", "current", "obsolete");
983 TEST_DUP("when", "true", "false");
984#undef TEST_DUP
985
986 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +0200987 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 +0100988 "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 +0100989 assert_int_equal(LY_SUCCESS, parse_container(YCTX, NULL, (struct lysp_node **)&c));
Radek Iša56ca9e42020-09-08 18:42:00 +0200990 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 +0100991 assert_non_null(c->actions);
992 assert_non_null(c->child);
Radek Krejci8c370832018-11-02 15:10:03 +0100993 assert_non_null(c->groupings);
Radek Krejci8c370832018-11-02 15:10:03 +0100994 assert_non_null(c->musts);
995 assert_non_null(c->notifs);
996 assert_string_equal("true", c->presence);
Radek Krejci8c370832018-11-02 15:10:03 +0100997 assert_non_null(c->typedefs);
Radek Iša56ca9e42020-09-08 18:42:00 +0200998 ly_set_erase(&YCTX->tpdfs_nodes, NULL);
aPiecek63e080d2021-06-29 13:53:28 +0200999 ly_set_erase(&YCTX->grps_nodes, NULL);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001000 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001001
1002 /* invalid */
Michal Vasko63f3d842020-07-08 10:10:14 +02001003 in.current = " cont {augment /root;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001004 assert_int_equal(LY_EVALID, parse_container(YCTX, NULL, (struct lysp_node **)&c));
Radek Iša56ca9e42020-09-08 18:42:00 +02001005 CHECK_LOG_CTX("Invalid keyword \"augment\" as a child of \"container\".", "Line number 1.");
Michal Vasko8a67eff2021-12-07 14:04:47 +01001006 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)c); c = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02001007 in.current = " cont {nonsence true;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001008 assert_int_equal(LY_EVALID, parse_container(YCTX, NULL, (struct lysp_node **)&c));
Radek Iša56ca9e42020-09-08 18:42:00 +02001009 CHECK_LOG_CTX("Invalid character sequence \"nonsence\", expected a keyword.", "Line number 1.");
Michal Vasko8a67eff2021-12-07 14:04:47 +01001010 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001011
Michal Vasko8a67eff2021-12-07 14:04:47 +01001012 PARSER_CUR_PMOD(YCTX)->version = 1; /* simulate YANG 1.0 */
Michal Vasko63f3d842020-07-08 10:10:14 +02001013 in.current = " cont {action x;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001014 assert_int_equal(LY_EVALID, parse_container(YCTX, NULL, (struct lysp_node **)&c));
Radek Iša56ca9e42020-09-08 18:42:00 +02001015 CHECK_LOG_CTX("Invalid keyword \"action\" as a child of \"container\" - "
1016 "the statement is allowed only in YANG 1.1 modules.", "Line number 1.");
Michal Vasko8a67eff2021-12-07 14:04:47 +01001017 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001018}
1019
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001020static void
1021test_leaf(void **state)
1022{
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001023 struct lysp_node_leaf *l = NULL;
Radek Krejci33090f92020-12-17 20:12:46 +01001024
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001025 /* invalid cardinality */
1026#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001027 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001028 assert_int_equal(LY_EVALID, parse_leaf(YCTX, NULL, (struct lysp_node**)&l)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001029 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Michal Vasko8a67eff2021-12-07 14:04:47 +01001030 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node*)l); l = NULL;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001031
1032 TEST_DUP("config", "true", "false");
1033 TEST_DUP("default", "x", "y");
1034 TEST_DUP("description", "text1", "text2");
1035 TEST_DUP("mandatory", "true", "false");
1036 TEST_DUP("reference", "1", "2");
1037 TEST_DUP("status", "current", "obsolete");
Radek Krejci0e5d8382018-11-28 16:37:53 +01001038 TEST_DUP("type", "int8", "uint8");
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001039 TEST_DUP("units", "text1", "text2");
1040 TEST_DUP("when", "true", "false");
1041#undef TEST_DUP
1042
1043 /* full content - without mandatory which is mutual exclusive with default */
Michal Vasko63f3d842020-07-08 10:10:14 +02001044 in.current = "l {config false;default \"xxx\";description test;if-feature f;"
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001045 "must 'expr';reference test;status current;type string; units yyy;when true;m:ext;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001046 assert_int_equal(LY_SUCCESS, parse_leaf(YCTX, NULL, (struct lysp_node **)&l));
Radek Iša56ca9e42020-09-08 18:42:00 +02001047 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 +02001048 assert_string_equal("xxx", l->dflt.str);
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001049 assert_string_equal("yyy", l->units);
1050 assert_string_equal("string", l->type.name);
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001051 assert_non_null(l->musts);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001052 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)l); l = NULL;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001053
1054 /* full content - now with mandatory */
Michal Vasko63f3d842020-07-08 10:10:14 +02001055 in.current = "l {mandatory true; type string;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001056 assert_int_equal(LY_SUCCESS, parse_leaf(YCTX, NULL, (struct lysp_node **)&l));
Radek Iša56ca9e42020-09-08 18:42:00 +02001057 CHECK_LYSP_NODE(l, NULL, 0, LYS_MAND_TRUE, 0, "l", 0, LYS_LEAF, 0, NULL, 0);
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001058 assert_string_equal("string", l->type.name);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001059 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)l); l = NULL;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001060
1061 /* invalid */
Michal Vasko63f3d842020-07-08 10:10:14 +02001062 in.current = " l {description \"missing type\";} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001063 assert_int_equal(LY_EVALID, parse_leaf(YCTX, NULL, (struct lysp_node **)&l));
Radek Iša56ca9e42020-09-08 18:42:00 +02001064 CHECK_LOG_CTX("Missing mandatory keyword \"type\" as a child of \"leaf\".", "Line number 1.");
Michal Vasko8a67eff2021-12-07 14:04:47 +01001065 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)l); l = NULL;
aPiecek4bb1e372021-05-07 11:01:00 +02001066
1067 in.current = "l { type iid { path qpud wrong {";
1068 assert_int_equal(LY_EVALID, parse_leaf(YCTX, NULL, (struct lysp_node **)&l));
1069 CHECK_LOG_CTX("Invalid character sequence \"wrong\", expected a keyword.", "Line number 1.");
Michal Vasko8a67eff2021-12-07 14:04:47 +01001070 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)l); l = NULL;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001071}
1072
Radek Krejci0e5d8382018-11-28 16:37:53 +01001073static void
1074test_leaflist(void **state)
1075{
Radek Krejci0e5d8382018-11-28 16:37:53 +01001076 struct lysp_node_leaflist *ll = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001077
Michal Vasko8a67eff2021-12-07 14:04:47 +01001078 PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
Radek Krejci0e5d8382018-11-28 16:37:53 +01001079
1080 /* invalid cardinality */
1081#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001082 in.current = "ll {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001083 assert_int_equal(LY_EVALID, parse_leaflist(YCTX, NULL, (struct lysp_node**)&ll)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001084 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Michal Vasko8a67eff2021-12-07 14:04:47 +01001085 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node*)ll); ll = NULL;
Radek Krejci0e5d8382018-11-28 16:37:53 +01001086
1087 TEST_DUP("config", "true", "false");
1088 TEST_DUP("description", "text1", "text2");
1089 TEST_DUP("max-elements", "10", "20");
1090 TEST_DUP("min-elements", "10", "20");
1091 TEST_DUP("ordered-by", "user", "system");
1092 TEST_DUP("reference", "1", "2");
1093 TEST_DUP("status", "current", "obsolete");
1094 TEST_DUP("type", "int8", "uint8");
1095 TEST_DUP("units", "text1", "text2");
1096 TEST_DUP("when", "true", "false");
1097#undef TEST_DUP
1098
1099 /* full content - without min-elements which is mutual exclusive with default */
Michal Vasko63f3d842020-07-08 10:10:14 +02001100 in.current = "ll {config false;default \"xxx\"; default \"yyy\";description test;if-feature f;"
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001101 "max-elements 10;must 'expr';ordered-by user;reference test;"
1102 "status current;type string; units zzz;when true;m:ext;} ...";
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, "test", 1, 0x446, 1, "ll", 0, LYS_LEAFLIST, 0, "test", 1);
Radek Krejci0e5d8382018-11-28 16:37:53 +01001105 assert_non_null(ll->dflts);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001106 assert_int_equal(2, LY_ARRAY_COUNT(ll->dflts));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001107 assert_string_equal("xxx", ll->dflts[0].str);
1108 assert_string_equal("yyy", ll->dflts[1].str);
Radek Krejci0e5d8382018-11-28 16:37:53 +01001109 assert_string_equal("zzz", ll->units);
1110 assert_int_equal(10, ll->max);
1111 assert_int_equal(0, ll->min);
1112 assert_string_equal("string", ll->type.name);
Radek Krejci0e5d8382018-11-28 16:37:53 +01001113 assert_non_null(ll->musts);
Radek Krejci0e5d8382018-11-28 16:37:53 +01001114 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_USER | LYS_SET_MAX, ll->flags);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001115 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)ll); ll = NULL;
Radek Krejci0e5d8382018-11-28 16:37:53 +01001116
1117 /* full content - now with min-elements */
Michal Vasko63f3d842020-07-08 10:10:14 +02001118 in.current = "ll {min-elements 10; type string;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001119 assert_int_equal(LY_SUCCESS, parse_leaflist(YCTX, NULL, (struct lysp_node **)&ll));
Radek Iša56ca9e42020-09-08 18:42:00 +02001120 CHECK_LYSP_NODE(ll, NULL, 0, 0x200, 0, "ll", 0, LYS_LEAFLIST, 0, NULL, 0);
Radek Krejci0e5d8382018-11-28 16:37:53 +01001121 assert_string_equal("string", ll->type.name);
1122 assert_int_equal(0, ll->max);
1123 assert_int_equal(10, ll->min);
1124 assert_int_equal(LYS_SET_MIN, ll->flags);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001125 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)ll); ll = NULL;
Radek Krejci0e5d8382018-11-28 16:37:53 +01001126
1127 /* invalid */
Michal Vasko63f3d842020-07-08 10:10:14 +02001128 in.current = " ll {description \"missing type\";} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001129 assert_int_equal(LY_EVALID, parse_leaflist(YCTX, NULL, (struct lysp_node **)&ll));
Radek Iša56ca9e42020-09-08 18:42:00 +02001130 CHECK_LOG_CTX("Missing mandatory keyword \"type\" as a child of \"leaf-list\".", "Line number 1.");
Michal Vasko8a67eff2021-12-07 14:04:47 +01001131 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)ll); ll = NULL;
Radek Krejci0e5d8382018-11-28 16:37:53 +01001132
Michal Vasko8a67eff2021-12-07 14:04:47 +01001133 PARSER_CUR_PMOD(YCTX)->version = 1; /* simulate YANG 1.0 - default statement is not allowed */
Michal Vasko63f3d842020-07-08 10:10:14 +02001134 in.current = " ll {default xx; type string;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001135 assert_int_equal(LY_EVALID, parse_leaflist(YCTX, NULL, (struct lysp_node **)&ll));
Radek Iša56ca9e42020-09-08 18:42:00 +02001136 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.");
Michal Vasko8a67eff2021-12-07 14:04:47 +01001137 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)ll); ll = NULL;
Radek Krejci0e5d8382018-11-28 16:37:53 +01001138}
1139
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001140static void
1141test_list(void **state)
1142{
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001143 struct lysp_node_list *l = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001144
Michal Vasko8a67eff2021-12-07 14:04:47 +01001145 PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
aPiecek8d4e75d2021-06-24 14:47:06 +02001146 YCTX->main_ctx = (struct lys_parser_ctx *)YCTX;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001147
1148 /* invalid cardinality */
1149#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001150 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001151 assert_int_equal(LY_EVALID, parse_list(YCTX, NULL, (struct lysp_node**)&l)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001152 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Michal Vasko8a67eff2021-12-07 14:04:47 +01001153 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node*)l); l = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001154
1155 TEST_DUP("config", "true", "false");
1156 TEST_DUP("description", "text1", "text2");
1157 TEST_DUP("key", "one", "two");
1158 TEST_DUP("max-elements", "10", "20");
1159 TEST_DUP("min-elements", "10", "20");
1160 TEST_DUP("ordered-by", "user", "system");
1161 TEST_DUP("reference", "1", "2");
1162 TEST_DUP("status", "current", "obsolete");
1163 TEST_DUP("when", "true", "false");
1164#undef TEST_DUP
1165
1166 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001167 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 +01001168 "leaf-list ll {type string;} list li;max-elements 10; min-elements 1;must 'expr';notification not; ordered-by system; reference test;"
1169 "status current;typedef t {type int8;}unique xxx;unique yyy;uses g;when true;m:ext;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001170 assert_int_equal(LY_SUCCESS, parse_list(YCTX, NULL, (struct lysp_node **)&l));
Radek Iša56ca9e42020-09-08 18:42:00 +02001171 CHECK_LYSP_NODE(l, "test", 1, LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM | LYS_SET_MAX | LYS_SET_MIN, 1, "l",
1172 0, LYS_LIST, 0, "test", 1);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001173 assert_string_equal("l", l->key);
1174 assert_non_null(l->uniques);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001175 assert_int_equal(2, LY_ARRAY_COUNT(l->uniques));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001176 assert_string_equal("xxx", l->uniques[0].str);
1177 assert_string_equal("yyy", l->uniques[1].str);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001178 assert_int_equal(10, l->max);
1179 assert_int_equal(1, l->min);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001180 assert_non_null(l->musts);
Radek Iša56ca9e42020-09-08 18:42:00 +02001181 ly_set_erase(&YCTX->tpdfs_nodes, NULL);
aPiecek63e080d2021-06-29 13:53:28 +02001182 ly_set_erase(&YCTX->grps_nodes, NULL);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001183 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)l); l = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001184
Radek Krejcif538ce52019-03-05 10:46:14 +01001185 /* invalid content */
Michal Vasko8a67eff2021-12-07 14:04:47 +01001186 PARSER_CUR_PMOD(YCTX)->version = 1; /* simulate YANG 1.0 */
Michal Vasko63f3d842020-07-08 10:10:14 +02001187 in.current = "l {action x;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001188 assert_int_equal(LY_EVALID, parse_list(YCTX, NULL, (struct lysp_node **)&l));
Radek Iša56ca9e42020-09-08 18:42:00 +02001189 CHECK_LOG_CTX("Invalid keyword \"action\" as a child of \"list\" - the statement is allowed only in YANG 1.1 modules.", "Line number 1.");
Michal Vasko8a67eff2021-12-07 14:04:47 +01001190 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)l); l = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001191}
1192
Radek Krejci056d0a82018-12-06 16:57:25 +01001193static void
1194test_choice(void **state)
1195{
Radek Krejci056d0a82018-12-06 16:57:25 +01001196 struct lysp_node_choice *ch = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001197
Michal Vasko8a67eff2021-12-07 14:04:47 +01001198 PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
Radek Krejci056d0a82018-12-06 16:57:25 +01001199
1200 /* invalid cardinality */
1201#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001202 in.current = "ch {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001203 assert_int_equal(LY_EVALID, parse_choice(YCTX, NULL, (struct lysp_node**)&ch)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001204 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Michal Vasko8a67eff2021-12-07 14:04:47 +01001205 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node*)ch); ch = NULL;
Radek Krejci056d0a82018-12-06 16:57:25 +01001206
1207 TEST_DUP("config", "true", "false");
1208 TEST_DUP("default", "a", "b");
1209 TEST_DUP("description", "text1", "text2");
1210 TEST_DUP("mandatory", "true", "false");
1211 TEST_DUP("reference", "1", "2");
1212 TEST_DUP("status", "current", "obsolete");
1213 TEST_DUP("when", "true", "false");
1214#undef TEST_DUP
1215
Radek Krejcia9026eb2018-12-12 16:04:47 +01001216 /* full content - without default due to a collision with mandatory */
Michal Vasko63f3d842020-07-08 10:10:14 +02001217 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 +01001218 "leaf-list ll {type string;} list li;mandatory true;reference test;status current;when true;m:ext;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001219 assert_int_equal(LY_SUCCESS, parse_choice(YCTX, NULL, (struct lysp_node **)&ch));
Radek Iša56ca9e42020-09-08 18:42:00 +02001220 CHECK_LYSP_NODE(ch, "test", 1, LYS_CONFIG_R | LYS_STATUS_CURR | LYS_MAND_TRUE, 1, "ch", 0, LYS_CHOICE, 0, "test", 1);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001221 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)ch); ch = NULL;
Radek Krejci056d0a82018-12-06 16:57:25 +01001222
Radek Krejcia9026eb2018-12-12 16:04:47 +01001223 /* full content - the default missing from the previous node */
Michal Vasko63f3d842020-07-08 10:10:14 +02001224 in.current = "ch {default c;case c;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001225 assert_int_equal(LY_SUCCESS, parse_choice(YCTX, NULL, (struct lysp_node **)&ch));
Radek Iša56ca9e42020-09-08 18:42:00 +02001226 CHECK_LYSP_NODE(ch, NULL, 0, 0, 0, "ch", 0, LYS_CHOICE, 0, NULL, 0);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001227 assert_string_equal("c", ch->dflt.str);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001228 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)ch); ch = NULL;
Radek Krejcia9026eb2018-12-12 16:04:47 +01001229}
1230
1231static void
1232test_case(void **state)
1233{
Radek Krejcia9026eb2018-12-12 16:04:47 +01001234 struct lysp_node_case *cs = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001235
Michal Vasko8a67eff2021-12-07 14:04:47 +01001236 PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
Radek Krejcia9026eb2018-12-12 16:04:47 +01001237
1238 /* invalid cardinality */
1239#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001240 in.current = "cs {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001241 assert_int_equal(LY_EVALID, parse_case(YCTX, NULL, (struct lysp_node**)&cs)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001242 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Michal Vasko8a67eff2021-12-07 14:04:47 +01001243 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node*)cs); cs = NULL;
Radek Krejcia9026eb2018-12-12 16:04:47 +01001244
1245 TEST_DUP("description", "text1", "text2");
1246 TEST_DUP("reference", "1", "2");
1247 TEST_DUP("status", "current", "obsolete");
1248 TEST_DUP("when", "true", "false");
1249#undef TEST_DUP
1250
1251 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001252 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 +01001253 "leaf-list ll {type string;} list li;reference test;status current;uses grp;when true;m:ext;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001254 assert_int_equal(LY_SUCCESS, parse_case(YCTX, NULL, (struct lysp_node **)&cs));
Radek Iša56ca9e42020-09-08 18:42:00 +02001255 CHECK_LYSP_NODE(cs, "test", 1, LYS_STATUS_CURR, 1, "cs", 0, LYS_CASE, 0, "test", 1);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001256 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)cs); cs = NULL;
Radek Krejcia9026eb2018-12-12 16:04:47 +01001257
1258 /* invalid content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001259 in.current = "cs {config true} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001260 assert_int_equal(LY_EVALID, parse_case(YCTX, NULL, (struct lysp_node **)&cs));
Radek Iša56ca9e42020-09-08 18:42:00 +02001261 CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"case\".", "Line number 1.");
Michal Vasko8a67eff2021-12-07 14:04:47 +01001262 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)cs); cs = NULL;
Radek Krejci056d0a82018-12-06 16:57:25 +01001263}
1264
Radek Krejci9800fb82018-12-13 14:26:23 +01001265static void
Radek Krejcid6b76452019-09-03 17:03:03 +02001266test_any(void **state, enum ly_stmt kw)
Radek Krejci9800fb82018-12-13 14:26:23 +01001267{
Radek Krejci9800fb82018-12-13 14:26:23 +01001268 struct lysp_node_anydata *any = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001269
Radek Krejcid6b76452019-09-03 17:03:03 +02001270 if (kw == LY_STMT_ANYDATA) {
Michal Vasko8a67eff2021-12-07 14:04:47 +01001271 PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
Radek Krejci9800fb82018-12-13 14:26:23 +01001272 } else {
Michal Vasko8a67eff2021-12-07 14:04:47 +01001273 PARSER_CUR_PMOD(YCTX)->version = 1; /* simulate YANG 1.0 */
Radek Krejci9800fb82018-12-13 14:26:23 +01001274 }
1275
1276 /* invalid cardinality */
1277#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001278 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001279 assert_int_equal(LY_EVALID, parse_any(YCTX, kw, NULL, (struct lysp_node**)&any)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001280 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Michal Vasko8a67eff2021-12-07 14:04:47 +01001281 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node*)any); any = NULL;
Radek Krejci9800fb82018-12-13 14:26:23 +01001282
1283 TEST_DUP("config", "true", "false");
1284 TEST_DUP("description", "text1", "text2");
1285 TEST_DUP("mandatory", "true", "false");
1286 TEST_DUP("reference", "1", "2");
1287 TEST_DUP("status", "current", "obsolete");
1288 TEST_DUP("when", "true", "false");
1289#undef TEST_DUP
1290
1291 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001292 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 +01001293 assert_int_equal(LY_SUCCESS, parse_any(YCTX, kw, NULL, (struct lysp_node **)&any));
Radek Iša56ca9e42020-09-08 18:42:00 +02001294 // CHECK_LYSP_NODE(NODE, DSC, EXTS, FLAGS, IFFEATURES, NAME, NEXT, TYPE, PARENT, REF, WHEN)
1295 uint16_t node_type = kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
1296 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 +01001297 assert_non_null(any->musts);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001298 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)any); any = NULL;
Radek Krejci9800fb82018-12-13 14:26:23 +01001299}
1300
1301static void
1302test_anydata(void **state)
1303{
Michal Vaskoa0a498b2021-09-22 12:17:48 +02001304 test_any(state, LY_STMT_ANYDATA);
Radek Krejci9800fb82018-12-13 14:26:23 +01001305}
1306
1307static void
1308test_anyxml(void **state)
1309{
Michal Vaskoa0a498b2021-09-22 12:17:48 +02001310 test_any(state, LY_STMT_ANYXML);
Radek Krejci9800fb82018-12-13 14:26:23 +01001311}
1312
Radek Krejcie86bf772018-12-14 11:39:53 +01001313static void
1314test_grouping(void **state)
1315{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001316 struct lysp_node_grp *grp = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001317
Michal Vasko8a67eff2021-12-07 14:04:47 +01001318 PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
aPiecek8d4e75d2021-06-24 14:47:06 +02001319 YCTX->main_ctx = (struct lys_parser_ctx *)YCTX;
Radek Krejcie86bf772018-12-14 11:39:53 +01001320
1321 /* invalid cardinality */
1322#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001323 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001324 assert_int_equal(LY_EVALID, parse_grouping(YCTX, NULL, &grp)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001325 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Michal Vasko8a67eff2021-12-07 14:04:47 +01001326 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, &grp->node); grp = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001327
1328 TEST_DUP("description", "text1", "text2");
1329 TEST_DUP("reference", "1", "2");
1330 TEST_DUP("status", "current", "obsolete");
1331#undef TEST_DUP
1332
1333 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001334 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 +01001335 "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 +01001336 assert_int_equal(LY_SUCCESS, parse_grouping(YCTX, NULL, &grp));
Radek Krejcie86bf772018-12-14 11:39:53 +01001337 assert_non_null(grp);
1338 assert_int_equal(LYS_GROUPING, grp->nodetype);
1339 assert_string_equal("grp", grp->name);
1340 assert_string_equal("test", grp->dsc);
1341 assert_non_null(grp->exts);
1342 assert_string_equal("test", grp->ref);
1343 assert_null(grp->parent);
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001344 assert_int_equal(LYS_STATUS_CURR, grp->flags);
Radek Iša56ca9e42020-09-08 18:42:00 +02001345 ly_set_erase(&YCTX->tpdfs_nodes, NULL);
aPiecek63e080d2021-06-29 13:53:28 +02001346 ly_set_erase(&YCTX->grps_nodes, NULL);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001347 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, &grp->node);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001348 grp = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001349
1350 /* invalid content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001351 in.current = "grp {config true} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001352 assert_int_equal(LY_EVALID, parse_grouping(YCTX, NULL, &grp));
Radek Iša56ca9e42020-09-08 18:42:00 +02001353 CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"grouping\".", "Line number 1.");
Michal Vasko8a67eff2021-12-07 14:04:47 +01001354 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, &grp->node);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001355 grp = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001356
Michal Vasko63f3d842020-07-08 10:10:14 +02001357 in.current = "grp {must 'expr'} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001358 assert_int_equal(LY_EVALID, parse_grouping(YCTX, NULL, &grp));
Radek Iša56ca9e42020-09-08 18:42:00 +02001359 CHECK_LOG_CTX("Invalid keyword \"must\" as a child of \"grouping\".", "Line number 1.");
Michal Vasko8a67eff2021-12-07 14:04:47 +01001360 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, &grp->node);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001361 grp = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001362}
1363
1364static void
Radek Krejcif538ce52019-03-05 10:46:14 +01001365test_action(void **state)
1366{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001367 struct lysp_node_action *rpcs = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01001368 struct lysp_node_container *c = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001369
Michal Vasko8a67eff2021-12-07 14:04:47 +01001370 PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
aPiecek8d4e75d2021-06-24 14:47:06 +02001371 YCTX->main_ctx = (struct lys_parser_ctx *)YCTX;
Radek Krejcif538ce52019-03-05 10:46:14 +01001372
1373 /* invalid cardinality */
1374#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001375 in.current = "func {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001376 assert_int_equal(LY_EVALID, parse_action(YCTX, NULL, &rpcs)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001377 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Michal Vasko8a67eff2021-12-07 14:04:47 +01001378 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node*)rpcs); rpcs = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01001379
1380 TEST_DUP("description", "text1", "text2");
Michal Vaskob83af8a2020-01-06 09:49:22 +01001381 TEST_DUP("input", "{leaf l1 {type empty;}} description a", "{leaf l2 {type empty;}} description a");
1382 TEST_DUP("output", "{leaf l1 {type empty;}} description a", "{leaf l2 {type empty;}} description a");
Radek Krejcif538ce52019-03-05 10:46:14 +01001383 TEST_DUP("reference", "1", "2");
1384 TEST_DUP("status", "current", "obsolete");
1385#undef TEST_DUP
1386
1387 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001388 in.current = "top;";
Radek Krejci33090f92020-12-17 20:12:46 +01001389 assert_int_equal(LY_SUCCESS, parse_container(YCTX, NULL, (struct lysp_node **)&c));
Michal Vasko63f3d842020-07-08 10:10:14 +02001390 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 +01001391 "input {anydata a1; anyxml a2; choice ch; container c; grouping grp; leaf l {type int8;} leaf-list ll {type int8;}"
1392 " list li; must 1; typedef mytypei {type int8;} uses grp; m:ext;}"
1393 "output {anydata a1; anyxml a2; choice ch; container c; grouping grp; leaf l {type int8;} leaf-list ll {type int8;}"
1394 " list li; must 1; typedef mytypeo {type int8;} uses grp; m:ext;}} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001395 assert_int_equal(LY_SUCCESS, parse_action(YCTX, (struct lysp_node *)c, &rpcs));
Radek Krejcif538ce52019-03-05 10:46:14 +01001396 assert_non_null(rpcs);
1397 assert_int_equal(LYS_ACTION, rpcs->nodetype);
1398 assert_string_equal("func", rpcs->name);
1399 assert_string_equal("test", rpcs->dsc);
1400 assert_non_null(rpcs->exts);
1401 assert_non_null(rpcs->iffeatures);
1402 assert_string_equal("test", rpcs->ref);
1403 assert_non_null(rpcs->groupings);
1404 assert_non_null(rpcs->typedefs);
1405 assert_int_equal(LYS_STATUS_CURR, rpcs->flags);
1406 /* input */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001407 assert_int_equal(rpcs->input.nodetype, LYS_INPUT);
Radek Krejcif538ce52019-03-05 10:46:14 +01001408 assert_non_null(rpcs->input.groupings);
1409 assert_non_null(rpcs->input.exts);
1410 assert_non_null(rpcs->input.musts);
1411 assert_non_null(rpcs->input.typedefs);
Radek Krejci01180ac2021-01-27 08:48:22 +01001412 assert_non_null(rpcs->input.child);
Radek Krejcif538ce52019-03-05 10:46:14 +01001413 /* output */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001414 assert_int_equal(rpcs->output.nodetype, LYS_OUTPUT);
Radek Krejcif538ce52019-03-05 10:46:14 +01001415 assert_non_null(rpcs->output.groupings);
1416 assert_non_null(rpcs->output.exts);
1417 assert_non_null(rpcs->output.musts);
1418 assert_non_null(rpcs->output.typedefs);
Radek Krejci01180ac2021-01-27 08:48:22 +01001419 assert_non_null(rpcs->output.child);
Radek Krejcif538ce52019-03-05 10:46:14 +01001420
Radek Iša56ca9e42020-09-08 18:42:00 +02001421 ly_set_erase(&YCTX->tpdfs_nodes, NULL);
aPiecek63e080d2021-06-29 13:53:28 +02001422 ly_set_erase(&YCTX->grps_nodes, NULL);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001423 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)rpcs); rpcs = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01001424
1425 /* invalid content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001426 in.current = "func {config true} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001427 assert_int_equal(LY_EVALID, parse_action(YCTX, NULL, &rpcs));
Radek Iša56ca9e42020-09-08 18:42:00 +02001428 CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"rpc\".", "Line number 1.");
Michal Vasko8a67eff2021-12-07 14:04:47 +01001429 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)rpcs); rpcs = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01001430
Michal Vasko8a67eff2021-12-07 14:04:47 +01001431 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)c);
Radek Krejcif538ce52019-03-05 10:46:14 +01001432}
1433
1434static void
Radek Krejcifc11bd72019-04-11 16:00:05 +02001435test_notification(void **state)
1436{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001437 struct lysp_node_notif *notifs = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001438 struct lysp_node_container *c = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001439
Michal Vasko8a67eff2021-12-07 14:04:47 +01001440 PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
aPiecek8d4e75d2021-06-24 14:47:06 +02001441 YCTX->main_ctx = (struct lys_parser_ctx *)YCTX;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001442
1443 /* invalid cardinality */
1444#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001445 in.current = "func {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001446 assert_int_equal(LY_EVALID, parse_notif(YCTX, NULL, &notifs)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001447 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Michal Vasko8a67eff2021-12-07 14:04:47 +01001448 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node*)notifs); notifs = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001449
1450 TEST_DUP("description", "text1", "text2");
1451 TEST_DUP("reference", "1", "2");
1452 TEST_DUP("status", "current", "obsolete");
1453#undef TEST_DUP
1454
1455 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001456 in.current = "top;";
Radek Krejci33090f92020-12-17 20:12:46 +01001457 assert_int_equal(LY_SUCCESS, parse_container(YCTX, NULL, (struct lysp_node **)&c));
Michal Vasko63f3d842020-07-08 10:10:14 +02001458 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 +01001459 "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 +01001460 assert_int_equal(LY_SUCCESS, parse_notif(YCTX, (struct lysp_node *)c, &notifs));
Radek Krejcifc11bd72019-04-11 16:00:05 +02001461 assert_non_null(notifs);
1462 assert_int_equal(LYS_NOTIF, notifs->nodetype);
1463 assert_string_equal("ntf", notifs->name);
1464 assert_string_equal("test", notifs->dsc);
1465 assert_non_null(notifs->exts);
1466 assert_non_null(notifs->iffeatures);
1467 assert_string_equal("test", notifs->ref);
1468 assert_non_null(notifs->groupings);
1469 assert_non_null(notifs->typedefs);
1470 assert_non_null(notifs->musts);
Radek Krejci01180ac2021-01-27 08:48:22 +01001471 assert_non_null(notifs->child);
Radek Krejcifc11bd72019-04-11 16:00:05 +02001472 assert_int_equal(LYS_STATUS_CURR, notifs->flags);
1473
Radek Iša56ca9e42020-09-08 18:42:00 +02001474 ly_set_erase(&YCTX->tpdfs_nodes, NULL);
aPiecek63e080d2021-06-29 13:53:28 +02001475 ly_set_erase(&YCTX->grps_nodes, NULL);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001476 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)notifs); notifs = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001477
1478 /* invalid content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001479 in.current = "ntf {config true} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001480 assert_int_equal(LY_EVALID, parse_notif(YCTX, NULL, &notifs));
Radek Iša56ca9e42020-09-08 18:42:00 +02001481 CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"notification\".", "Line number 1.");
Michal Vasko8a67eff2021-12-07 14:04:47 +01001482 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)notifs); notifs = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001483
Michal Vasko8a67eff2021-12-07 14:04:47 +01001484 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)c);
Radek Krejcifc11bd72019-04-11 16:00:05 +02001485}
1486
1487static void
Radek Krejcie86bf772018-12-14 11:39:53 +01001488test_uses(void **state)
1489{
Radek Krejcie86bf772018-12-14 11:39:53 +01001490 struct lysp_node_uses *u = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001491
Michal Vasko8a67eff2021-12-07 14:04:47 +01001492 PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
Radek Krejcie86bf772018-12-14 11:39:53 +01001493
1494 /* invalid cardinality */
1495#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001496 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001497 assert_int_equal(LY_EVALID, parse_uses(YCTX, NULL, (struct lysp_node**)&u)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001498 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Michal Vasko8a67eff2021-12-07 14:04:47 +01001499 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node*)u); u = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001500
1501 TEST_DUP("description", "text1", "text2");
1502 TEST_DUP("reference", "1", "2");
1503 TEST_DUP("status", "current", "obsolete");
1504 TEST_DUP("when", "true", "false");
1505#undef TEST_DUP
1506
1507 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001508 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 +01001509 assert_int_equal(LY_SUCCESS, parse_uses(YCTX, NULL, (struct lysp_node **)&u));
Radek Iša56ca9e42020-09-08 18:42:00 +02001510 CHECK_LYSP_NODE(u, "test", 1, LYS_STATUS_CURR, 1, "grpref", 0, LYS_USES, 0, "test", 1);
Radek Krejcie86bf772018-12-14 11:39:53 +01001511 assert_non_null(u->augments);
1512 assert_non_null(u->refines);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001513 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)u); u = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001514}
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001515
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001516static void
1517test_augment(void **state)
1518{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001519 struct lysp_node_augment *a = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001520
Michal Vasko8a67eff2021-12-07 14:04:47 +01001521 PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001522
1523 /* invalid cardinality */
1524#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001525 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001526 assert_int_equal(LY_EVALID, parse_augment(YCTX, NULL, &a)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001527 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Michal Vasko8a67eff2021-12-07 14:04:47 +01001528 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)a); a = NULL;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001529
1530 TEST_DUP("description", "text1", "text2");
1531 TEST_DUP("reference", "1", "2");
1532 TEST_DUP("status", "current", "obsolete");
1533 TEST_DUP("when", "true", "false");
1534#undef TEST_DUP
1535
1536 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001537 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 +01001538 "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 +01001539 assert_int_equal(LY_SUCCESS, parse_augment(YCTX, NULL, &a));
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001540 assert_non_null(a);
1541 assert_int_equal(LYS_AUGMENT, a->nodetype);
1542 assert_string_equal("/target/nodeid", a->nodeid);
1543 assert_string_equal("test", a->dsc);
1544 assert_non_null(a->exts);
1545 assert_non_null(a->iffeatures);
1546 assert_string_equal("test", a->ref);
1547 assert_non_null(a->when);
1548 assert_null(a->parent);
1549 assert_int_equal(LYS_STATUS_CURR, a->flags);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001550 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)a); a = NULL;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001551}
1552
Radek Krejcif09e4e82019-06-14 15:08:11 +02001553static void
1554test_when(void **state)
1555{
Radek Krejcif09e4e82019-06-14 15:08:11 +02001556 struct lysp_when *w = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001557
Michal Vasko8a67eff2021-12-07 14:04:47 +01001558 PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
Radek Krejcif09e4e82019-06-14 15:08:11 +02001559
1560 /* invalid cardinality */
1561#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001562 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001563 assert_int_equal(LY_EVALID, parse_when(YCTX, &w)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001564 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Michal Vasko8a67eff2021-12-07 14:04:47 +01001565 FREE_MEMBER(PARSER_CUR_PMOD(YCTX)->mod->ctx, w, lysp_when_free); w = NULL;
Radek Krejcif09e4e82019-06-14 15:08:11 +02001566
1567 TEST_DUP("description", "text1", "text2");
1568 TEST_DUP("reference", "1", "2");
1569#undef TEST_DUP
1570
1571 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001572 in.current = "expression {description test;reference test;m:ext;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001573 assert_int_equal(LY_SUCCESS, parse_when(YCTX, &w));
Radek Krejcif09e4e82019-06-14 15:08:11 +02001574 assert_non_null(w);
1575 assert_string_equal("expression", w->cond);
1576 assert_string_equal("test", w->dsc);
1577 assert_string_equal("test", w->ref);
1578 assert_non_null(w->exts);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001579 FREE_MEMBER(PARSER_CUR_PMOD(YCTX)->mod->ctx, w, lysp_when_free); w = NULL;
Radek Krejcif09e4e82019-06-14 15:08:11 +02001580
1581 /* empty condition */
Michal Vasko63f3d842020-07-08 10:10:14 +02001582 in.current = "\"\";";
Radek Krejci33090f92020-12-17 20:12:46 +01001583 assert_int_equal(LY_SUCCESS, parse_when(YCTX, &w));
Radek Iša56ca9e42020-09-08 18:42:00 +02001584 CHECK_LOG_CTX("Empty argument of when statement does not make sense.", NULL);
Radek Krejcif09e4e82019-06-14 15:08:11 +02001585 assert_non_null(w);
1586 assert_string_equal("", w->cond);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001587 FREE_MEMBER(PARSER_CUR_PMOD(YCTX)->mod->ctx, w, lysp_when_free); w = NULL;
Radek Krejcif09e4e82019-06-14 15:08:11 +02001588}
1589
David Sedlákd6ce6d72019-07-16 17:30:18 +02001590static void
1591test_value(void **state)
1592{
David Sedlákd6ce6d72019-07-16 17:30:18 +02001593 int64_t val = 0;
1594 uint16_t flags = 0;
1595
Michal Vasko63f3d842020-07-08 10:10:14 +02001596 in.current = "-0;";
Radek Krejci33090f92020-12-17 20:12:46 +01001597 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 +02001598 assert_int_equal(val, 0);
1599
Michal Vasko63f3d842020-07-08 10:10:14 +02001600 in.current = "-0;";
David Sedlákd6ce6d72019-07-16 17:30:18 +02001601 flags = 0;
Radek Krejci33090f92020-12-17 20:12:46 +01001602 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 +02001603 CHECK_LOG_CTX("Invalid value \"-0\" of \"position\".", "Line number 1.");
David Sedlákd6ce6d72019-07-16 17:30:18 +02001604}
1605
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001606int
1607main(void)
Radek Krejci80dd33e2018-09-26 15:57:18 +02001608{
1609 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +02001610 UTEST(test_helpers, setup, teardown),
1611 UTEST(test_comments, setup, teardown),
1612 UTEST(test_arg, setup, teardown),
1613 UTEST(test_stmts, setup, teardown),
1614 UTEST(test_minmax, setup, teardown),
1615 UTEST(test_module, setup, teardown),
1616 UTEST(test_deviation, setup, teardown),
1617 UTEST(test_deviate, setup, teardown),
1618 UTEST(test_container, setup, teardown),
1619 UTEST(test_leaf, setup, teardown),
1620 UTEST(test_leaflist, setup, teardown),
1621 UTEST(test_list, setup, teardown),
1622 UTEST(test_choice, setup, teardown),
1623 UTEST(test_case, setup, teardown),
1624 UTEST(test_anydata, setup, teardown),
1625 UTEST(test_anyxml, setup, teardown),
1626 UTEST(test_action, setup, teardown),
1627 UTEST(test_notification, setup, teardown),
1628 UTEST(test_grouping, setup, teardown),
1629 UTEST(test_uses, setup, teardown),
1630 UTEST(test_augment, setup, teardown),
1631 UTEST(test_when, setup, teardown),
1632 UTEST(test_value, setup, teardown),
Radek Krejci80dd33e2018-09-26 15:57:18 +02001633 };
1634
1635 return cmocka_run_group_tests(tests, NULL, NULL);
1636}