blob: 4ce4dc42d32d3968e3a9120e647d6dba91aa8ee3 [file] [log] [blame]
Michal Vasko12ef5362022-09-16 15:13:58 +02001/**
2 * @file test_yang.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @author Michal Vasko <mvasko@cesnet.cz>
5 * @brief unit tests for YANG module parser and printer
Radek Krejci80dd33e2018-09-26 15:57:18 +02006 *
Michal Vasko12ef5362022-09-16 15:13:58 +02007 * Copyright (c) 2018 - 2022 CESNET, z.s.p.o.
Radek Krejci80dd33e2018-09-26 15:57:18 +02008 *
9 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * https://opensource.org/licenses/BSD-3-Clause
14 */
Radek Iša56ca9e42020-09-08 18:42:00 +020015#define _UTEST_MAIN_
16#include "utests.h"
Radek Krejci80dd33e2018-09-26 15:57:18 +020017
Radek Krejci80dd33e2018-09-26 15:57:18 +020018#include <stdio.h>
19#include <string.h>
20
Radek Krejci70593c12020-06-13 20:48:09 +020021#include "common.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020022#include "in_internal.h"
Radek Krejci70593c12020-06-13 20:48:09 +020023#include "parser_internal.h"
Michal Vasko405cc9e2020-12-01 12:01:27 +010024#include "schema_compile.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010025#include "tree_edit.h"
Radek Krejci70593c12020-06-13 20:48:09 +020026#include "tree_schema.h"
Michal Vaskoc636ea42022-09-16 10:20:31 +020027#include "tree_schema_free.h"
Radek Krejci2d7a47b2019-05-16 13:34:10 +020028
Michal Vasko12ef5362022-09-16 15:13:58 +020029/* originally static functions from parser_yang.c and parser_yin.c */
Michal Vasko63f3d842020-07-08 10:10:14 +020030LY_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);
Michal Vaskod0625d72022-10-06 15:02:50 +020031LY_ERR buf_store_char(struct lysp_yang_ctx *ctx, enum yang_arg arg, char **word_p,
Radek Krejcib4ac5a92020-11-23 17:54:33 +010032 size_t *word_len, char **word_b, size_t *buf_len, uint8_t need_buf, uint8_t *prefix);
Michal Vaskod0625d72022-10-06 15:02:50 +020033LY_ERR get_keyword(struct lysp_yang_ctx *ctx, enum ly_stmt *kw, char **word_p, size_t *word_len);
34LY_ERR get_argument(struct lysp_yang_ctx *ctx, enum yang_arg arg,
Radek Krejcib4ac5a92020-11-23 17:54:33 +010035 uint16_t *flags, char **word_p, char **word_b, size_t *word_len);
Michal Vaskod0625d72022-10-06 15:02:50 +020036LY_ERR skip_comment(struct lysp_yang_ctx *ctx, uint8_t comment);
Radek Krejci2d7a47b2019-05-16 13:34:10 +020037
Michal Vaskod0625d72022-10-06 15:02:50 +020038LY_ERR parse_action(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_action **actions);
39LY_ERR parse_any(struct lysp_yang_ctx *ctx, enum ly_stmt kw, struct lysp_node *parent, struct lysp_node **siblings);
40LY_ERR parse_augment(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_augment **augments);
41LY_ERR parse_case(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
42LY_ERR parse_container(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
43LY_ERR parse_deviate(struct lysp_yang_ctx *ctx, struct lysp_deviate **deviates);
44LY_ERR parse_deviation(struct lysp_yang_ctx *ctx, struct lysp_deviation **deviations);
45LY_ERR parse_grouping(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_grp **groupings);
46LY_ERR parse_choice(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
47LY_ERR parse_leaf(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
48LY_ERR parse_leaflist(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
49LY_ERR parse_list(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
50LY_ERR parse_maxelements(struct lysp_yang_ctx *ctx, uint32_t *max, uint16_t *flags, struct lysp_ext_instance **exts);
51LY_ERR parse_minelements(struct lysp_yang_ctx *ctx, uint32_t *min, uint16_t *flags, struct lysp_ext_instance **exts);
52LY_ERR parse_module(struct lysp_yang_ctx *ctx, struct lysp_module *mod);
53LY_ERR parse_notif(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node_notif **notifs);
54LY_ERR parse_submodule(struct lysp_yang_ctx *ctx, struct lysp_submodule *submod);
55LY_ERR parse_uses(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
56LY_ERR parse_when(struct lysp_yang_ctx *ctx, struct lysp_when **when_p);
Michal Vasko193dacd2022-10-13 08:43:05 +020057LY_ERR parse_type_enum_value_pos(struct lysp_yang_ctx *ctx, enum ly_stmt val_kw, struct lysp_type_enum *enm);
Radek Krejci80dd33e2018-09-26 15:57:18 +020058
Michal Vaskod0625d72022-10-06 15:02:50 +020059struct lysp_yang_ctx *YCTX;
Michal Vaskoc636ea42022-09-16 10:20:31 +020060struct lysf_ctx fctx;
Radek Krejci80dd33e2018-09-26 15:57:18 +020061
Radek Krejci1640e802021-01-08 11:01:30 +010062struct ly_in in = {0};
63
Radek Krejci33090f92020-12-17 20:12:46 +010064#define YCTX_INIT \
Radek Krejcid54412f2020-12-17 20:25:35 +010065 in.line = 1; \
Radek Krejci2efc45b2020-12-22 16:25:44 +010066 YCTX->in = &in; \
67 LOG_LOCINIT(UTEST_LYCTX, NULL, NULL, NULL, &in)
Radek Krejci33090f92020-12-17 20:12:46 +010068
Michal Vasko5d24f6c2020-10-13 13:49:06 +020069static int
Radek Iša56ca9e42020-09-08 18:42:00 +020070setup(void **state)
Michal Vasko5d24f6c2020-10-13 13:49:06 +020071{
Michal Vasko8a67eff2021-12-07 14:04:47 +010072 struct lysp_module *pmod;
73
Radek Iša56ca9e42020-09-08 18:42:00 +020074 UTEST_SETUP;
Michal Vasko5d24f6c2020-10-13 13:49:06 +020075
Radek Iša56ca9e42020-09-08 18:42:00 +020076 /* allocate parser context */
77 YCTX = calloc(1, sizeof(*YCTX));
78 YCTX->format = LYS_IN_YANG;
Michal Vasko8a67eff2021-12-07 14:04:47 +010079 ly_set_new(&YCTX->parsed_mods);
Michal Vasko5d24f6c2020-10-13 13:49:06 +020080
Radek Iša56ca9e42020-09-08 18:42:00 +020081 /* allocate new parsed module */
Michal Vasko8a67eff2021-12-07 14:04:47 +010082 pmod = calloc(1, sizeof *pmod);
83 ly_set_add(YCTX->parsed_mods, pmod, 1, NULL);
Michal Vasko5d24f6c2020-10-13 13:49:06 +020084
Radek Iša56ca9e42020-09-08 18:42:00 +020085 /* allocate new module */
Michal Vasko8a67eff2021-12-07 14:04:47 +010086 pmod->mod = calloc(1, sizeof *pmod->mod);
87 pmod->mod->ctx = UTEST_LYCTX;
88 pmod->mod->parsed = pmod;
Michal Vasko5d24f6c2020-10-13 13:49:06 +020089
Radek Krejci1640e802021-01-08 11:01:30 +010090 /* initilize and use the global easily available and customizable input handler */
91 in.line = 1;
92 YCTX->in = &in;
Michal Vaskof8ebf132022-11-21 14:06:48 +010093 LOG_LOCSET(NULL, NULL, NULL, &in);
Radek Krejci1640e802021-01-08 11:01:30 +010094
Michal Vaskoc636ea42022-09-16 10:20:31 +020095 fctx.ctx = PARSER_CTX(YCTX);
96 fctx.mod = pmod->mod;
97
Michal Vasko5d24f6c2020-10-13 13:49:06 +020098 return 0;
99}
100
101static int
Radek Iša56ca9e42020-09-08 18:42:00 +0200102teardown(void **state)
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200103{
Michal Vaskoc636ea42022-09-16 10:20:31 +0200104 lys_module_free(&fctx, PARSER_CUR_PMOD(YCTX)->mod, 0);
Radek Krejciddace2c2021-01-08 11:30:56 +0100105 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejci1640e802021-01-08 11:01:30 +0100106
Michal Vasko8a67eff2021-12-07 14:04:47 +0100107 ly_set_free(YCTX->parsed_mods, NULL);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +0200108 ly_set_erase(&YCTX->ext_inst, NULL);
Radek Iša56ca9e42020-09-08 18:42:00 +0200109 free(YCTX);
110 YCTX = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200111
Michal Vaskoc636ea42022-09-16 10:20:31 +0200112 lysf_ctx_erase(&fctx);
113
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 */
Michal Vaskod0625d72022-10-06 15:02:50 +0200175 assert_int_equal(LY_EVALID, lysp_check_identifierchar((struct lysp_ctx *)YCTX, ':', 0, NULL));
Radek Iša56ca9e42020-09-08 18:42:00 +0200176 CHECK_LOG_CTX("Invalid identifier character ':' (0x003a).", "Line number 1.");
Michal Vaskod0625d72022-10-06 15:02:50 +0200177 assert_int_equal(LY_EVALID, lysp_check_identifierchar((struct lysp_ctx *)YCTX, '#', 1, NULL));
Radek Iša56ca9e42020-09-08 18:42:00 +0200178 CHECK_LOG_CTX("Invalid identifier first character '#' (0x0023).", "Line number 1.");
Radek Krejci44ceedc2018-10-02 15:54:31 +0200179
Michal Vaskod0625d72022-10-06 15:02:50 +0200180 assert_int_equal(LY_SUCCESS, lysp_check_identifierchar((struct lysp_ctx *)YCTX, 'a', 1, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200181 assert_int_equal(0, prefix);
Michal Vaskod0625d72022-10-06 15:02:50 +0200182 assert_int_equal(LY_SUCCESS, lysp_check_identifierchar((struct lysp_ctx *)YCTX, ':', 0, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200183 assert_int_equal(1, prefix);
Michal Vaskod0625d72022-10-06 15:02:50 +0200184 assert_int_equal(LY_EVALID, lysp_check_identifierchar((struct lysp_ctx *)YCTX, ':', 0, &prefix));
Radek Krejcidcc7b322018-10-11 14:24:02 +0200185 assert_int_equal(1, prefix);
Michal Vaskod0625d72022-10-06 15:02:50 +0200186 assert_int_equal(LY_SUCCESS, lysp_check_identifierchar((struct lysp_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 */
Michal Vaskod0625d72022-10-06 15:02:50 +0200189 assert_int_equal(LY_EVALID, lysp_check_identifierchar((struct lysp_ctx *)YCTX, ':', 0, &prefix));
Radek Iša56ca9e42020-09-08 18:42:00 +0200190 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 Krejci05b13982018-11-28 16:22:07 +0100467static void
468test_minmax(void **state)
469{
Radek Krejci05b13982018-11-28 16:22:07 +0100470 uint16_t flags = 0;
471 uint32_t value = 0;
472 struct lysp_ext_instance *ext = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +0100473
Michal Vasko8a67eff2021-12-07 14:04:47 +0100474 PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
Radek Krejci05b13982018-11-28 16:22:07 +0100475
Michal Vasko63f3d842020-07-08 10:10:14 +0200476 in.current = " 1invalid; ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100477 assert_int_equal(LY_EVALID, parse_minelements(YCTX, &value, &flags, &ext));
Radek Iša56ca9e42020-09-08 18:42:00 +0200478 CHECK_LOG_CTX("Invalid value \"1invalid\" of \"min-elements\".", "Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100479
480 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200481 in.current = " -1; ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100482 assert_int_equal(LY_EVALID, parse_minelements(YCTX, &value, &flags, &ext));
Radek Iša56ca9e42020-09-08 18:42:00 +0200483 CHECK_LOG_CTX("Invalid value \"-1\" of \"min-elements\".", "Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100484
Radek Krejcidf6cad12018-11-28 17:10:55 +0100485 /* implementation limit */
486 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200487 in.current = " 4294967296; ...";
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("Value \"4294967296\" is out of \"min-elements\" bounds.", "Line number 1.");
Radek Krejcidf6cad12018-11-28 17:10:55 +0100490
Radek Krejci05b13982018-11-28 16:22:07 +0100491 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200492 in.current = " 1 {config true;} ...";
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 keyword \"config\" as a child of \"min-elements\".", "Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100495
Michal Vasko63f3d842020-07-08 10:10:14 +0200496 in.current = " 1invalid; ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100497 assert_int_equal(LY_EVALID, parse_maxelements(YCTX, &value, &flags, &ext));
Radek Iša56ca9e42020-09-08 18:42:00 +0200498 CHECK_LOG_CTX("Invalid value \"1invalid\" of \"max-elements\".", "Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100499
500 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200501 in.current = " -1; ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100502 assert_int_equal(LY_EVALID, parse_maxelements(YCTX, &value, &flags, &ext));
Radek Iša56ca9e42020-09-08 18:42:00 +0200503 CHECK_LOG_CTX("Invalid value \"-1\" of \"max-elements\".", "Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100504
Radek Krejcidf6cad12018-11-28 17:10:55 +0100505 /* implementation limit */
506 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200507 in.current = " 4294967296; ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100508 assert_int_equal(LY_EVALID, parse_maxelements(YCTX, &value, &flags, &ext));
Radek Iša56ca9e42020-09-08 18:42:00 +0200509 CHECK_LOG_CTX("Value \"4294967296\" is out of \"max-elements\" bounds.", "Line number 1.");
Radek Krejcidf6cad12018-11-28 17:10:55 +0100510
Radek Krejci05b13982018-11-28 16:22:07 +0100511 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_maxelements(YCTX, &value, &flags, &ext));
Radek Iša56ca9e42020-09-08 18:42:00 +0200514 CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"max-elements\".", "Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100515}
516
Michal Vasko12ef5362022-09-16 15:13:58 +0200517static void
518test_valid_module(void **state)
519{
520 struct lys_module *mod;
521 char *printed;
522 const char *links_yang =
523 "module links {\n"
524 " yang-version 1.1;\n"
525 " namespace \"urn:module2\";\n"
526 " prefix mod2;\n"
527 "\n"
528 " identity just-another-identity;\n"
529 "\n"
530 " leaf one-leaf {\n"
531 " type string;\n"
532 " }\n"
533 "\n"
534 " list list-for-augment {\n"
535 " key keyleaf;\n"
536 "\n"
537 " leaf keyleaf {\n"
538 " type string;\n"
539 " }\n"
540 "\n"
541 " leaf just-leaf {\n"
542 " type int32;\n"
543 " }\n"
544 " }\n"
545 "\n"
546 " leaf rleaf {\n"
547 " type string;\n"
548 " }\n"
549 "\n"
550 " leaf-list llist {\n"
551 " type string;\n"
552 " min-elements 0;\n"
553 " max-elements 100;\n"
554 " ordered-by user;\n"
555 " }\n"
556 "\n"
557 " grouping rgroup {\n"
558 " leaf rg1 {\n"
559 " type string;\n"
560 " }\n"
561 "\n"
562 " leaf rg2 {\n"
563 " type string;\n"
564 " }\n"
565 " }\n"
566 "}\n";
567 const char *statements_yang =
568 "module statements {\n"
569 " yang-version 1.1;\n"
570 " namespace \"urn:module\";\n"
571 " prefix mod;\n"
572 "\n"
573 " import links {\n"
574 " prefix mod2;\n"
575 " }\n"
576 "\n"
577 " extension ext;\n"
578 "\n"
579 " identity random-identity {\n"
580 " base mod2:just-another-identity;\n"
581 " base another-identity;\n"
582 " }\n"
583 "\n"
584 " identity another-identity {\n"
585 " base mod2:just-another-identity;\n"
586 " }\n"
587 "\n"
588 " typedef percent {\n"
589 " type uint8 {\n"
590 " range \"0 .. 100\";\n"
591 " }\n"
592 " units \"percent\";\n"
593 " }\n"
594 "\n"
595 " list list1 {\n"
596 " key \"a\";\n"
597 " leaf a {\n"
598 " type string;\n"
599 " }\n"
600 " leaf x {\n"
601 " type string;\n"
602 " }\n"
603 " leaf y {\n"
604 " type string;\n"
605 " }\n"
606 " }\n"
607 " container ice-cream-shop {\n"
608 " container employees {\n"
609 " when \"/list1/x\";\n"
610 " list employee {\n"
611 " key \"id\";\n"
612 " unique \"name\";\n"
613 " config true;\n"
614 " min-elements 0 {\n"
615 " mod:ext;\n"
616 " }\n"
617 " max-elements unbounded;\n"
618 " leaf id {\n"
619 " type uint64;\n"
620 " mandatory true;\n"
621 " }\n"
622 " leaf name {\n"
623 " type string;\n"
624 " }\n"
625 " leaf age {\n"
626 " type uint32;\n"
627 " }\n"
628 " }\n"
629 " }\n"
630 " }\n"
631 " container random {\n"
632 " grouping group {\n"
633 " leaf g1 {\n"
634 " type percent;\n"
635 " mandatory false;\n"
636 " }\n"
637 " leaf g2 {\n"
638 " type string;\n"
639 " }\n"
640 " }\n"
641 " choice switch {\n"
642 " case a {\n"
643 " leaf aleaf {\n"
644 " type string;\n"
645 " default \"aaa\";\n"
646 " }\n"
647 " }\n"
648 " case c {\n"
649 " leaf cleaf {\n"
650 " type string;\n"
651 " }\n"
652 " }\n"
653 " }\n"
654 " anyxml xml-data;\n"
655 " anydata any-data;\n"
656 " leaf-list leaflist {\n"
657 " type string;\n"
658 " min-elements 0;\n"
659 " max-elements 20;\n"
660 " }\n"
661 " uses group;\n"
662 " uses mod2:rgroup;\n"
663 " leaf lref {\n"
664 " type leafref {\n"
665 " path \"/mod2:one-leaf\";\n"
666 " }\n"
667 " }\n"
668 " leaf iref {\n"
669 " type identityref {\n"
670 " base mod2:just-another-identity;\n"
671 " }\n"
672 " }\n"
673 " }\n"
674 "\n"
675 " augment \"/random\" {\n"
676 " leaf aug-leaf {\n"
677 " type string;\n"
678 " }\n"
679 " }\n"
680 "\n"
681 " notification notif;\n"
682 "\n"
683 " deviation \"/mod:ice-cream-shop/mod:employees/mod:employee/mod:age\" {\n"
684 " deviate not-supported {\n"
685 " mod:ext;\n"
686 " }\n"
687 " }\n"
688 " deviation \"/mod:list1\" {\n"
689 " deviate add {\n"
690 " mod:ext;\n"
691 " must \"1\";\n"
692 " must \"2\";\n"
693 " unique \"x\";\n"
694 " unique \"y\";\n"
695 " config true;\n"
696 " min-elements 1;\n"
697 " max-elements 2;\n"
698 " }\n"
699 " }\n"
700 " deviation \"/mod:ice-cream-shop/mod:employees/mod:employee\" {\n"
701 " deviate delete {\n"
702 " unique \"name\";\n"
703 " }\n"
704 " }\n"
705 " deviation \"/mod:random/mod:leaflist\" {\n"
706 " deviate replace {\n"
707 " type uint32;\n"
708 " min-elements 10;\n"
709 " max-elements 15;\n"
710 " }\n"
711 " }\n"
712 "}\n";
713
714 UTEST_ADD_MODULE(links_yang, LYS_IN_YANG, NULL, NULL);
715 UTEST_ADD_MODULE(statements_yang, LYS_IN_YANG, NULL, &mod);
716 lys_print_mem(&printed, mod, LYS_OUT_YANG, 0);
717 assert_string_equal(printed, statements_yang);
718 free(printed);
719}
720
Radek Krejci9fcacc12018-10-11 15:59:11 +0200721static struct lysp_module *
Michal Vaskod0625d72022-10-06 15:02:50 +0200722mod_renew(struct lysp_yang_ctx *ctx)
Radek Krejci9fcacc12018-10-11 15:59:11 +0200723{
Michal Vasko8a67eff2021-12-07 14:04:47 +0100724 struct ly_ctx *ly_ctx = PARSER_CUR_PMOD(ctx)->mod->ctx;
725 struct lysp_module *pmod;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100726
Michal Vaskoc636ea42022-09-16 10:20:31 +0200727 lys_module_free(&fctx, PARSER_CUR_PMOD(ctx)->mod, 0);
Michal Vasko8a67eff2021-12-07 14:04:47 +0100728 pmod = calloc(1, sizeof *pmod);
729 ctx->parsed_mods->objs[0] = pmod;
730 pmod->mod = calloc(1, sizeof *pmod->mod);
731 pmod->mod->parsed = pmod;
732 pmod->mod->ctx = ly_ctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100733
Radek Krejci2efc45b2020-12-22 16:25:44 +0100734 ctx->in->line = 1;
Michal Vaskoc636ea42022-09-16 10:20:31 +0200735 fctx.mod = pmod->mod;
Radek Krejci2efc45b2020-12-22 16:25:44 +0100736
Michal Vasko8a67eff2021-12-07 14:04:47 +0100737 return pmod;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100738}
739
740static struct lysp_submodule *
Michal Vaskod0625d72022-10-06 15:02:50 +0200741submod_renew(struct lysp_yang_ctx *ctx)
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100742{
Michal Vasko8a67eff2021-12-07 14:04:47 +0100743 struct ly_ctx *ly_ctx = PARSER_CUR_PMOD(ctx)->mod->ctx;
744 struct lysp_submodule *submod;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200745
Michal Vaskoc636ea42022-09-16 10:20:31 +0200746 lys_module_free(&fctx, PARSER_CUR_PMOD(ctx)->mod, 0);
Michal Vasko8a67eff2021-12-07 14:04:47 +0100747 submod = calloc(1, sizeof *submod);
748 ctx->parsed_mods->objs[0] = submod;
749 submod->mod = calloc(1, sizeof *submod->mod);
750 lydict_insert(ly_ctx, "name", 0, &submod->mod->name);
751 submod->mod->parsed = (struct lysp_module *)submod;
752 submod->mod->ctx = ly_ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200753
Michal Vaskoc636ea42022-09-16 10:20:31 +0200754 fctx.mod = submod->mod;
755
Michal Vasko8a67eff2021-12-07 14:04:47 +0100756 return submod;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200757}
758
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100759static LY_ERR
760test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
761 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
762 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
Radek Krejcid33273d2018-10-25 14:55:52 +0200763{
764 *module_data = user_data;
765 *format = LYS_IN_YANG;
766 *free_module_data = NULL;
767 return LY_SUCCESS;
768}
769
Radek Krejci9fcacc12018-10-11 15:59:11 +0200770static void
771test_module(void **state)
772{
Radek Krejci40544fa2019-01-11 09:38:37 +0100773 struct lysp_module *mod = NULL;
774 struct lysp_submodule *submod = NULL;
775 struct lys_module *m;
Michal Vaskod0625d72022-10-06 15:02:50 +0200776 struct lysp_yang_ctx *ctx_p;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200777
Radek Iša56ca9e42020-09-08 18:42:00 +0200778 mod = mod_renew(YCTX);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200779
780 /* missing mandatory substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +0200781 in.current = " name {}";
Radek Krejci33090f92020-12-17 20:12:46 +0100782 assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100783 assert_string_equal("name", mod->mod->name);
Radek Iša56ca9e42020-09-08 18:42:00 +0200784 CHECK_LOG_CTX("Missing mandatory keyword \"namespace\" as a child of \"module\".", "Line number 1.");
Radek Krejci9fcacc12018-10-11 15:59:11 +0200785
Radek Iša56ca9e42020-09-08 18:42:00 +0200786 mod = mod_renew(YCTX);
Michal Vaskob24145d2022-07-13 18:34:39 +0200787 in.current = " name {namespace urn:name;}";
Radek Krejci33090f92020-12-17 20:12:46 +0100788 assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
Michal Vaskob24145d2022-07-13 18:34:39 +0200789 assert_string_equal("urn:name", mod->mod->ns);
Radek Iša56ca9e42020-09-08 18:42:00 +0200790 CHECK_LOG_CTX("Missing mandatory keyword \"prefix\" as a child of \"module\".", "Line number 1.");
791 mod = mod_renew(YCTX);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200792
Michal Vaskob24145d2022-07-13 18:34:39 +0200793 in.current = " name {namespace urn:name;prefix \"n\";}";
Radek Krejci33090f92020-12-17 20:12:46 +0100794 assert_int_equal(LY_SUCCESS, parse_module(YCTX, mod));
Michal Vaskob24145d2022-07-13 18:34:39 +0200795 assert_string_equal("n", mod->mod->prefix);
Radek Iša56ca9e42020-09-08 18:42:00 +0200796 mod = mod_renew(YCTX);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200797
Radek Krejci027d5802018-11-14 16:57:28 +0100798#define SCHEMA_BEGINNING " name {yang-version 1.1;namespace urn:x;prefix \"x\";"
799#define SCHEMA_BEGINNING2 " name {namespace urn:x;prefix \"x\";"
Radek Krejcia042ea12018-10-13 07:52:15 +0200800#define TEST_NODE(NODETYPE, INPUT, NAME) \
Michal Vasko63f3d842020-07-08 10:10:14 +0200801 in.current = SCHEMA_BEGINNING INPUT; \
Radek Krejci33090f92020-12-17 20:12:46 +0100802 assert_int_equal(LY_SUCCESS, parse_module(YCTX, mod)); \
Radek Krejcia042ea12018-10-13 07:52:15 +0200803 assert_non_null(mod->data); \
804 assert_int_equal(NODETYPE, mod->data->nodetype); \
805 assert_string_equal(NAME, mod->data->name); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200806 mod = mod_renew(YCTX);
Radek Krejcia042ea12018-10-13 07:52:15 +0200807#define TEST_GENERIC(INPUT, TARGET, TEST) \
Michal Vasko63f3d842020-07-08 10:10:14 +0200808 in.current = SCHEMA_BEGINNING INPUT; \
Radek Krejci33090f92020-12-17 20:12:46 +0100809 assert_int_equal(LY_SUCCESS, parse_module(YCTX, mod)); \
Radek Krejcia042ea12018-10-13 07:52:15 +0200810 assert_non_null(TARGET); \
811 TEST; \
Radek Iša56ca9e42020-09-08 18:42:00 +0200812 mod = mod_renew(YCTX);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100813#define TEST_DUP(MEMBER, VALUE1, VALUE2, LINE) \
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200814 TEST_DUP_GENERIC(SCHEMA_BEGINNING, MEMBER, VALUE1, VALUE2, \
Radek Iša56ca9e42020-09-08 18:42:00 +0200815 parse_module, mod, LINE, mod = mod_renew(YCTX))
Radek Krejcia042ea12018-10-13 07:52:15 +0200816
817 /* duplicated namespace, prefix */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100818 TEST_DUP("namespace", "y", "z", "1");
819 TEST_DUP("prefix", "y", "z", "1");
820 TEST_DUP("contact", "a", "b", "1");
821 TEST_DUP("description", "a", "b", "1");
822 TEST_DUP("organization", "a", "b", "1");
823 TEST_DUP("reference", "a", "b", "1");
Radek Krejcia042ea12018-10-13 07:52:15 +0200824
Radek Krejci70853c52018-10-15 14:46:16 +0200825 /* not allowed in module (submodule-specific) */
Michal Vasko63f3d842020-07-08 10:10:14 +0200826 in.current = SCHEMA_BEGINNING "belongs-to master {prefix m;}}";
Radek Krejci33090f92020-12-17 20:12:46 +0100827 assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
Radek Iša56ca9e42020-09-08 18:42:00 +0200828 CHECK_LOG_CTX("Invalid keyword \"belongs-to\" as a child of \"module\".", "Line number 1.");
829 mod = mod_renew(YCTX);
Radek Krejci70853c52018-10-15 14:46:16 +0200830
Radek Krejcia042ea12018-10-13 07:52:15 +0200831 /* anydata */
832 TEST_NODE(LYS_ANYDATA, "anydata test;}", "test");
833 /* anyxml */
834 TEST_NODE(LYS_ANYXML, "anyxml test;}", "test");
835 /* augment */
836 TEST_GENERIC("augment /somepath;}", mod->augments,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100837 assert_string_equal("/somepath", mod->augments[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200838 /* choice */
839 TEST_NODE(LYS_CHOICE, "choice test;}", "test");
840 /* contact 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100841 TEST_GENERIC("contact \"firstname\" + \n\t\" surname\";}", mod->mod->contact,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100842 assert_string_equal("firstname surname", mod->mod->contact));
Radek Krejcia042ea12018-10-13 07:52:15 +0200843 /* container */
844 TEST_NODE(LYS_CONTAINER, "container test;}", "test");
845 /* description 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100846 TEST_GENERIC("description \'some description\';}", mod->mod->dsc,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100847 assert_string_equal("some description", mod->mod->dsc));
Radek Krejcia042ea12018-10-13 07:52:15 +0200848 /* deviation */
849 TEST_GENERIC("deviation /somepath {deviate not-supported;}}", mod->deviations,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100850 assert_string_equal("/somepath", mod->deviations[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200851 /* extension */
852 TEST_GENERIC("extension test;}", mod->extensions,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100853 assert_string_equal("test", mod->extensions[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200854 /* feature */
855 TEST_GENERIC("feature test;}", mod->features,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100856 assert_string_equal("test", mod->features[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200857 /* grouping */
858 TEST_GENERIC("grouping grp;}", mod->groupings,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100859 assert_string_equal("grp", mod->groupings[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200860 /* identity */
861 TEST_GENERIC("identity test;}", mod->identities,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100862 assert_string_equal("test", mod->identities[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200863 /* import */
Michal Vasko8a67eff2021-12-07 14:04:47 +0100864 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 +0200865 TEST_GENERIC("import zzz {prefix z;}}", mod->imports,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100866 assert_string_equal("zzz", mod->imports[0].name));
Radek Krejci70853c52018-10-15 14:46:16 +0200867
Radek Krejcia042ea12018-10-13 07:52:15 +0200868 /* import - prefix collision */
Michal Vasko63f3d842020-07-08 10:10:14 +0200869 in.current = SCHEMA_BEGINNING "import zzz {prefix x;}}";
Radek Krejci33090f92020-12-17 20:12:46 +0100870 assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
Radek Krejci2efc45b2020-12-22 16:25:44 +0100871 CHECK_LOG_CTX("Prefix \"x\" already used as module prefix.", "Line number 1.");
Radek Iša56ca9e42020-09-08 18:42:00 +0200872 mod = mod_renew(YCTX);
873
Michal Vasko63f3d842020-07-08 10:10:14 +0200874 in.current = SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix y;}}";
Radek Krejci33090f92020-12-17 20:12:46 +0100875 assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
Radek Krejci2efc45b2020-12-22 16:25:44 +0100876 CHECK_LOG_CTX("Prefix \"y\" already used to import \"zzz\" module.", "Line number 1.");
Radek Iša56ca9e42020-09-08 18:42:00 +0200877
878 mod = mod_renew(YCTX);
Radek Krejciddace2c2021-01-08 11:30:56 +0100879 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100880
Michal Vaskob24145d2022-07-13 18:34:39 +0200881 in.current = "module name10 {yang-version 1.1;namespace urn:name10;prefix \"n10\";import zzz {prefix y;}import zzz {prefix z;}}";
Michal Vasko8a67eff2021-12-07 14:04:47 +0100882 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 +0200883 CHECK_LOG_CTX("Single revision of the module \"zzz\" imported twice.", NULL);
Radek Krejci70853c52018-10-15 14:46:16 +0200884
Radek Krejcia042ea12018-10-13 07:52:15 +0200885 /* include */
Michal Vasko8a67eff2021-12-07 14:04:47 +0100886 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 +0200887 in.current = "module" SCHEMA_BEGINNING "include xxx;}";
Michal Vasko8a67eff2021-12-07 14:04:47 +0100888 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 +0200889 CHECK_LOG_CTX("Parsing module \"name\" failed.", NULL, "Including \"xxx\" submodule into \"name\" failed.", NULL);
Radek Krejcid33273d2018-10-25 14:55:52 +0200890
Michal Vasko8a67eff2021-12-07 14:04:47 +0100891 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 +0200892 in.current = "module" SCHEMA_BEGINNING "include xxx;}";
Michal Vasko8a67eff2021-12-07 14:04:47 +0100893 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 +0200894 CHECK_LOG_CTX("Parsing module \"name\" failed.", NULL, "Including \"xxx\" submodule into \"name\" failed.", NULL);
Radek Krejcid33273d2018-10-25 14:55:52 +0200895
Michal Vasko8a67eff2021-12-07 14:04:47 +0100896 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 +0200897 TEST_GENERIC("include xxx;}", mod->includes,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100898 assert_string_equal("xxx", mod->includes[0].name));
Radek Krejcid33273d2018-10-25 14:55:52 +0200899
Radek Krejcia042ea12018-10-13 07:52:15 +0200900 /* leaf */
901 TEST_NODE(LYS_LEAF, "leaf test {type string;}}", "test");
902 /* leaf-list */
903 TEST_NODE(LYS_LEAFLIST, "leaf-list test {type string;}}", "test");
904 /* list */
905 TEST_NODE(LYS_LIST, "list test {key a;leaf a {type string;}}}", "test");
906 /* notification */
907 TEST_GENERIC("notification test;}", mod->notifs,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100908 assert_string_equal("test", mod->notifs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200909 /* organization 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100910 TEST_GENERIC("organization \"CESNET a.l.e.\";}", mod->mod->org,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100911 assert_string_equal("CESNET a.l.e.", mod->mod->org));
Radek Krejcia042ea12018-10-13 07:52:15 +0200912 /* reference 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100913 TEST_GENERIC("reference RFC7950;}", mod->mod->ref,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100914 assert_string_equal("RFC7950", mod->mod->ref));
Radek Krejcia042ea12018-10-13 07:52:15 +0200915 /* revision */
916 TEST_GENERIC("revision 2018-10-12;}", mod->revs,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100917 assert_string_equal("2018-10-12", mod->revs[0].date));
Radek Krejcia042ea12018-10-13 07:52:15 +0200918 /* rpc */
919 TEST_GENERIC("rpc test;}", mod->rpcs,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100920 assert_string_equal("test", mod->rpcs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200921 /* typedef */
922 TEST_GENERIC("typedef test{type string;}}", mod->typedefs,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100923 assert_string_equal("test", mod->typedefs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200924 /* uses */
925 TEST_NODE(LYS_USES, "uses test;}", "test");
926 /* yang-version */
Michal Vasko63f3d842020-07-08 10:10:14 +0200927 in.current = SCHEMA_BEGINNING2 "\n\tyang-version 10;}";
Radek Krejci33090f92020-12-17 20:12:46 +0100928 assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
Radek Krejci2efc45b2020-12-22 16:25:44 +0100929 CHECK_LOG_CTX("Invalid value \"10\" of \"yang-version\".", NULL);
Radek Iša56ca9e42020-09-08 18:42:00 +0200930 mod = mod_renew(YCTX);
Radek Krejci96e48da2020-09-04 13:18:06 +0200931 in.current = SCHEMA_BEGINNING2 "yang-version 1;yang-version 1.1;}";
Radek Krejci33090f92020-12-17 20:12:46 +0100932 assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
Radek Krejci2efc45b2020-12-22 16:25:44 +0100933 CHECK_LOG_CTX("Duplicate keyword \"yang-version\".", NULL);
Radek Iša56ca9e42020-09-08 18:42:00 +0200934 mod = mod_renew(YCTX);
Radek Krejci96e48da2020-09-04 13:18:06 +0200935 in.current = SCHEMA_BEGINNING2 "yang-version 1;}";
Radek Krejci33090f92020-12-17 20:12:46 +0100936 assert_int_equal(LY_SUCCESS, parse_module(YCTX, mod));
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200937 assert_int_equal(1, mod->version);
Radek Iša56ca9e42020-09-08 18:42:00 +0200938 mod = mod_renew(YCTX);
Michal Vasko63f3d842020-07-08 10:10:14 +0200939 in.current = SCHEMA_BEGINNING2 "yang-version \"1.1\";}";
Radek Krejci33090f92020-12-17 20:12:46 +0100940 assert_int_equal(LY_SUCCESS, parse_module(YCTX, mod));
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200941 assert_int_equal(2, mod->version);
Radek Iša56ca9e42020-09-08 18:42:00 +0200942 mod = mod_renew(YCTX);
Radek Krejci40544fa2019-01-11 09:38:37 +0100943
Michal Vasko63f3d842020-07-08 10:10:14 +0200944 in.current = "module " SCHEMA_BEGINNING "} module q {namespace urn:q;prefixq;}";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200945 m = calloc(1, sizeof *m);
Michal Vasko8a67eff2021-12-07 14:04:47 +0100946 m->ctx = PARSER_CUR_PMOD(YCTX)->mod->ctx;
aPiecekc3e26142021-06-22 14:25:49 +0200947 assert_int_equal(LY_EVALID, yang_parse_module(&ctx_p, &in, m));
Radek Iša56ca9e42020-09-08 18:42:00 +0200948 CHECK_LOG_CTX("Trailing garbage \"module q {names...\" after module, expected end-of-input.", "Line number 1.");
Michal Vaskod0625d72022-10-06 15:02:50 +0200949 lysp_yang_ctx_free(ctx_p);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200950 lys_module_free(&fctx, m, 0);
Radek Krejci40544fa2019-01-11 09:38:37 +0100951
Michal Vasko63f3d842020-07-08 10:10:14 +0200952 in.current = "prefix " SCHEMA_BEGINNING "}";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200953 m = calloc(1, sizeof *m);
Michal Vasko8a67eff2021-12-07 14:04:47 +0100954 m->ctx = PARSER_CUR_PMOD(YCTX)->mod->ctx;
aPiecekc3e26142021-06-22 14:25:49 +0200955 assert_int_equal(LY_EVALID, yang_parse_module(&ctx_p, &in, m));
Radek Iša56ca9e42020-09-08 18:42:00 +0200956 CHECK_LOG_CTX("Invalid keyword \"prefix\", expected \"module\" or \"submodule\".", "Line number 1.");
Michal Vaskod0625d72022-10-06 15:02:50 +0200957 lysp_yang_ctx_free(ctx_p);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200958 lys_module_free(&fctx, m, 0);
Radek Krejci09306362018-10-15 15:26:01 +0200959
Michal Vasko63f3d842020-07-08 10:10:14 +0200960 in.current = "module " SCHEMA_BEGINNING "leaf enum {type enumeration {enum seven { position 7;}}}}";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200961 m = calloc(1, sizeof *m);
Michal Vasko8a67eff2021-12-07 14:04:47 +0100962 m->ctx = PARSER_CUR_PMOD(YCTX)->mod->ctx;
aPiecekc3e26142021-06-22 14:25:49 +0200963 assert_int_equal(LY_EVALID, yang_parse_module(&ctx_p, &in, m));
Radek Iša56ca9e42020-09-08 18:42:00 +0200964 CHECK_LOG_CTX("Invalid keyword \"position\" as a child of \"enum\".", "Line number 1.");
Michal Vaskod0625d72022-10-06 15:02:50 +0200965 lysp_yang_ctx_free(ctx_p);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200966 lys_module_free(&fctx, m, 0);
David Sedlák9fb515f2019-07-11 10:33:58 +0200967
Radek Krejci156ccaf2018-10-15 15:49:17 +0200968 /* extensions */
969 TEST_GENERIC("prefix:test;}", mod->exts,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100970 assert_string_equal("prefix:test", mod->exts[0].name);
Radek Krejciab430862021-03-02 20:13:40 +0100971 assert_int_equal(LY_STMT_MODULE, mod->exts[0].parent_stmt));
Radek Iša56ca9e42020-09-08 18:42:00 +0200972 mod = mod_renew(YCTX);
Radek Krejci156ccaf2018-10-15 15:49:17 +0200973
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200974 /* invalid substatement */
Michal Vasko63f3d842020-07-08 10:10:14 +0200975 in.current = SCHEMA_BEGINNING "must false;}";
Radek Krejci33090f92020-12-17 20:12:46 +0100976 assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
Radek Krejci2efc45b2020-12-22 16:25:44 +0100977 CHECK_LOG_CTX("Invalid keyword \"must\" as a child of \"module\".", NULL);
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200978
Radek Krejci09306362018-10-15 15:26:01 +0200979 /* submodule */
Radek Iša56ca9e42020-09-08 18:42:00 +0200980 submod = submod_renew(YCTX);
Radek Krejci09306362018-10-15 15:26:01 +0200981
982 /* missing mandatory substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +0200983 in.current = " subname {}";
Radek Krejci33090f92020-12-17 20:12:46 +0100984 assert_int_equal(LY_EVALID, parse_submodule(YCTX, submod));
Radek Krejci2efc45b2020-12-22 16:25:44 +0100985 CHECK_LOG_CTX("Missing mandatory keyword \"belongs-to\" as a child of \"submodule\".", NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100986 assert_string_equal("subname", submod->name);
Radek Iša56ca9e42020-09-08 18:42:00 +0200987
988 submod = submod_renew(YCTX);
Radek Krejci09306362018-10-15 15:26:01 +0200989
Michal Vasko63f3d842020-07-08 10:10:14 +0200990 in.current = " subname {belongs-to name {prefix x;}}";
Radek Krejci33090f92020-12-17 20:12:46 +0100991 assert_int_equal(LY_SUCCESS, parse_submodule(YCTX, submod));
Michal Vaskoc3781c32020-10-06 14:04:08 +0200992 assert_string_equal("name", submod->mod->name);
Radek Iša56ca9e42020-09-08 18:42:00 +0200993 submod = submod_renew(YCTX);
Radek Krejci09306362018-10-15 15:26:01 +0200994
995#undef SCHEMA_BEGINNING
Radek Krejci313d9902018-11-08 09:42:58 +0100996#define SCHEMA_BEGINNING " subname {belongs-to name {prefix x;}"
Radek Krejci09306362018-10-15 15:26:01 +0200997
998 /* duplicated namespace, prefix */
Michal Vasko63f3d842020-07-08 10:10:14 +0200999 in.current = " subname {belongs-to name {prefix x;}belongs-to module1;belongs-to module2;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001000 assert_int_equal(LY_EVALID, parse_submodule(YCTX, submod));
Radek Krejci2efc45b2020-12-22 16:25:44 +01001001 CHECK_LOG_CTX("Duplicate keyword \"belongs-to\".", NULL);
Radek Iša56ca9e42020-09-08 18:42:00 +02001002 submod = submod_renew(YCTX);
Radek Krejci09306362018-10-15 15:26:01 +02001003
1004 /* not allowed in submodule (module-specific) */
Michal Vasko63f3d842020-07-08 10:10:14 +02001005 in.current = SCHEMA_BEGINNING "namespace \"urn:z\";}";
Radek Krejci33090f92020-12-17 20:12:46 +01001006 assert_int_equal(LY_EVALID, parse_submodule(YCTX, submod));
Radek Krejci2efc45b2020-12-22 16:25:44 +01001007 CHECK_LOG_CTX("Invalid keyword \"namespace\" as a child of \"submodule\".", NULL);
Radek Iša56ca9e42020-09-08 18:42:00 +02001008 submod = submod_renew(YCTX);
Michal Vasko63f3d842020-07-08 10:10:14 +02001009 in.current = SCHEMA_BEGINNING "prefix m;}}";
Radek Krejci33090f92020-12-17 20:12:46 +01001010 assert_int_equal(LY_EVALID, parse_submodule(YCTX, submod));
Radek Krejci2efc45b2020-12-22 16:25:44 +01001011 CHECK_LOG_CTX("Invalid keyword \"prefix\" as a child of \"submodule\".", NULL);
Radek Iša56ca9e42020-09-08 18:42:00 +02001012 submod = submod_renew(YCTX);
Radek Krejcia042ea12018-10-13 07:52:15 +02001013
Michal Vasko63f3d842020-07-08 10:10:14 +02001014 in.current = "submodule " SCHEMA_BEGINNING "} module q {namespace urn:q;prefixq;}";
Michal Vaskod0625d72022-10-06 15:02:50 +02001015 assert_int_equal(LY_EVALID, yang_parse_submodule(&ctx_p, PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_ctx *)YCTX, YCTX->in, &submod));
Radek Iša56ca9e42020-09-08 18:42:00 +02001016 CHECK_LOG_CTX("Trailing garbage \"module q {names...\" after submodule, expected end-of-input.", "Line number 1.");
Michal Vaskod0625d72022-10-06 15:02:50 +02001017 lysp_yang_ctx_free(ctx_p);
Radek Krejci40544fa2019-01-11 09:38:37 +01001018
Michal Vasko63f3d842020-07-08 10:10:14 +02001019 in.current = "prefix " SCHEMA_BEGINNING "}";
Michal Vaskod0625d72022-10-06 15:02:50 +02001020 assert_int_equal(LY_EVALID, yang_parse_submodule(&ctx_p, PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_ctx *)YCTX, YCTX->in, &submod));
Radek Iša56ca9e42020-09-08 18:42:00 +02001021 CHECK_LOG_CTX("Invalid keyword \"prefix\", expected \"module\" or \"submodule\".", "Line number 1.");
Michal Vaskod0625d72022-10-06 15:02:50 +02001022 lysp_yang_ctx_free(ctx_p);
Radek Iša56ca9e42020-09-08 18:42:00 +02001023 submod = submod_renew(YCTX);
Radek Krejci40544fa2019-01-11 09:38:37 +01001024
Radek Krejcia042ea12018-10-13 07:52:15 +02001025#undef TEST_GENERIC
1026#undef TEST_NODE
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001027#undef TEST_DUP
Radek Krejcia042ea12018-10-13 07:52:15 +02001028#undef SCHEMA_BEGINNING
Radek Krejciefd22f62018-09-27 11:47:58 +02001029}
1030
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001031static void
1032test_deviation(void **state)
1033{
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001034 struct lysp_deviation *d = NULL;
Radek Krejci33090f92020-12-17 20:12:46 +01001035
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001036 /* invalid cardinality */
Michal Vasko12ef5362022-09-16 15:13:58 +02001037 TEST_DUP_GENERIC(" test {deviate not-supported;", "description", "a", "b", parse_deviation, &d, "1", );
1038 TEST_DUP_GENERIC(" test {deviate not-supported;", "reference", "a", "b", parse_deviation, &d, "1", );
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001039
1040 /* missing mandatory substatement */
Michal Vasko63f3d842020-07-08 10:10:14 +02001041 in.current = " test {description text;}";
Radek Krejci33090f92020-12-17 20:12:46 +01001042 assert_int_equal(LY_EVALID, parse_deviation(YCTX, &d));
Radek Iša56ca9e42020-09-08 18:42:00 +02001043 CHECK_LOG_CTX("Missing mandatory keyword \"deviate\" as a child of \"deviation\".", "Line number 1.");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001044
1045 /* invalid substatement */
Michal Vasko63f3d842020-07-08 10:10:14 +02001046 in.current = " test {deviate not-supported; status obsolete;}";
Radek Krejci33090f92020-12-17 20:12:46 +01001047 assert_int_equal(LY_EVALID, parse_deviation(YCTX, &d));
Radek Iša56ca9e42020-09-08 18:42:00 +02001048 CHECK_LOG_CTX("Invalid keyword \"status\" as a child of \"deviation\".", "Line number 1.");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001049}
1050
1051static void
1052test_deviate(void **state)
1053{
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001054 struct lysp_deviate *d = NULL;
Radek Krejci33090f92020-12-17 20:12:46 +01001055
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001056 /* invalid cardinality */
Michal Vasko12ef5362022-09-16 15:13:58 +02001057 TEST_DUP_GENERIC("add {", "config", "true", "false", parse_deviate, &d, "1", );
1058 TEST_DUP_GENERIC("add {", "mandatory", "true", "false", parse_deviate, &d, "1", );
1059 TEST_DUP_GENERIC("add {", "max-elements", "1", "2", parse_deviate, &d, "1", );
1060 TEST_DUP_GENERIC("add {", "min-elements", "1", "2", parse_deviate, &d, "1", );
1061 TEST_DUP_GENERIC("add {", "units", "kilometers", "miles", parse_deviate, &d, "1", );
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001062
1063 /* invalid substatements */
1064#define TEST_NOT_SUP(DEV, STMT, VALUE) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001065 in.current = " "DEV" {"STMT" "VALUE";}..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001066 assert_int_equal(LY_EVALID, parse_deviate(YCTX, &d)); \
Michal Vasko12ef5362022-09-16 15:13:58 +02001067 CHECK_LOG_CTX("Deviate \""DEV"\" does not support keyword \""STMT"\".", "Line number 1.");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001068
1069 TEST_NOT_SUP("not-supported", "units", "meters");
1070 TEST_NOT_SUP("not-supported", "must", "1");
1071 TEST_NOT_SUP("not-supported", "unique", "x");
1072 TEST_NOT_SUP("not-supported", "default", "a");
1073 TEST_NOT_SUP("not-supported", "config", "true");
1074 TEST_NOT_SUP("not-supported", "mandatory", "true");
1075 TEST_NOT_SUP("not-supported", "min-elements", "1");
1076 TEST_NOT_SUP("not-supported", "max-elements", "2");
1077 TEST_NOT_SUP("not-supported", "type", "string");
1078 TEST_NOT_SUP("add", "type", "string");
1079 TEST_NOT_SUP("delete", "config", "true");
1080 TEST_NOT_SUP("delete", "mandatory", "true");
1081 TEST_NOT_SUP("delete", "min-elements", "1");
1082 TEST_NOT_SUP("delete", "max-elements", "2");
1083 TEST_NOT_SUP("delete", "type", "string");
1084 TEST_NOT_SUP("replace", "must", "1");
1085 TEST_NOT_SUP("replace", "unique", "a");
1086
Michal Vasko63f3d842020-07-08 10:10:14 +02001087 in.current = " nonsence; ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001088 assert_int_equal(LY_EVALID, parse_deviate(YCTX, &d));
Radek Iša56ca9e42020-09-08 18:42:00 +02001089 CHECK_LOG_CTX("Invalid value \"nonsence\" of \"deviate\".", "Line number 1.");\
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001090 assert_null(d);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001091#undef TEST_NOT_SUP
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001092}
1093
Radek Krejci8c370832018-11-02 15:10:03 +01001094static void
1095test_container(void **state)
1096{
Radek Krejci8c370832018-11-02 15:10:03 +01001097 struct lysp_node_container *c = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001098
Michal Vasko8a67eff2021-12-07 14:04:47 +01001099 PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
Michal Vaskod0625d72022-10-06 15:02:50 +02001100 YCTX->main_ctx = (struct lysp_ctx *)YCTX;
Radek Krejci8c370832018-11-02 15:10:03 +01001101
1102 /* invalid cardinality */
1103#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001104 in.current = "cont {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001105 assert_int_equal(LY_EVALID, parse_container(YCTX, NULL, (struct lysp_node**)&c)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001106 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Michal Vaskoc636ea42022-09-16 10:20:31 +02001107 lysp_node_free(&fctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001108
1109 TEST_DUP("config", "true", "false");
1110 TEST_DUP("description", "text1", "text2");
1111 TEST_DUP("presence", "true", "false");
1112 TEST_DUP("reference", "1", "2");
1113 TEST_DUP("status", "current", "obsolete");
1114 TEST_DUP("when", "true", "false");
1115#undef TEST_DUP
1116
1117 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001118 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 +01001119 "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 +01001120 assert_int_equal(LY_SUCCESS, parse_container(YCTX, NULL, (struct lysp_node **)&c));
Radek Iša56ca9e42020-09-08 18:42:00 +02001121 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 +01001122 assert_non_null(c->actions);
1123 assert_non_null(c->child);
Radek Krejci8c370832018-11-02 15:10:03 +01001124 assert_non_null(c->groupings);
Radek Krejci8c370832018-11-02 15:10:03 +01001125 assert_non_null(c->musts);
1126 assert_non_null(c->notifs);
1127 assert_string_equal("true", c->presence);
Radek Krejci8c370832018-11-02 15:10:03 +01001128 assert_non_null(c->typedefs);
Radek Iša56ca9e42020-09-08 18:42:00 +02001129 ly_set_erase(&YCTX->tpdfs_nodes, NULL);
aPiecek63e080d2021-06-29 13:53:28 +02001130 ly_set_erase(&YCTX->grps_nodes, NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001131 lysp_node_free(&fctx, (struct lysp_node *)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001132
1133 /* invalid */
Michal Vasko63f3d842020-07-08 10:10:14 +02001134 in.current = " cont {augment /root;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001135 assert_int_equal(LY_EVALID, parse_container(YCTX, NULL, (struct lysp_node **)&c));
Radek Iša56ca9e42020-09-08 18:42:00 +02001136 CHECK_LOG_CTX("Invalid keyword \"augment\" as a child of \"container\".", "Line number 1.");
Michal Vaskoc636ea42022-09-16 10:20:31 +02001137 lysp_node_free(&fctx, (struct lysp_node *)c); c = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02001138 in.current = " cont {nonsence true;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001139 assert_int_equal(LY_EVALID, parse_container(YCTX, NULL, (struct lysp_node **)&c));
Radek Iša56ca9e42020-09-08 18:42:00 +02001140 CHECK_LOG_CTX("Invalid character sequence \"nonsence\", expected a keyword.", "Line number 1.");
Michal Vaskoc636ea42022-09-16 10:20:31 +02001141 lysp_node_free(&fctx, (struct lysp_node *)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001142
Michal Vasko8a67eff2021-12-07 14:04:47 +01001143 PARSER_CUR_PMOD(YCTX)->version = 1; /* simulate YANG 1.0 */
Michal Vasko63f3d842020-07-08 10:10:14 +02001144 in.current = " cont {action x;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001145 assert_int_equal(LY_EVALID, parse_container(YCTX, NULL, (struct lysp_node **)&c));
Radek Iša56ca9e42020-09-08 18:42:00 +02001146 CHECK_LOG_CTX("Invalid keyword \"action\" as a child of \"container\" - "
1147 "the statement is allowed only in YANG 1.1 modules.", "Line number 1.");
Michal Vaskoc636ea42022-09-16 10:20:31 +02001148 lysp_node_free(&fctx, (struct lysp_node *)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001149}
1150
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001151static void
1152test_leaf(void **state)
1153{
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001154 struct lysp_node_leaf *l = NULL;
Radek Krejci33090f92020-12-17 20:12:46 +01001155
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001156 /* invalid cardinality */
1157#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001158 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001159 assert_int_equal(LY_EVALID, parse_leaf(YCTX, NULL, (struct lysp_node**)&l)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001160 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Michal Vaskoc636ea42022-09-16 10:20:31 +02001161 lysp_node_free(&fctx, (struct lysp_node*)l); l = NULL;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001162
1163 TEST_DUP("config", "true", "false");
1164 TEST_DUP("default", "x", "y");
1165 TEST_DUP("description", "text1", "text2");
1166 TEST_DUP("mandatory", "true", "false");
1167 TEST_DUP("reference", "1", "2");
1168 TEST_DUP("status", "current", "obsolete");
Radek Krejci0e5d8382018-11-28 16:37:53 +01001169 TEST_DUP("type", "int8", "uint8");
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001170 TEST_DUP("units", "text1", "text2");
1171 TEST_DUP("when", "true", "false");
1172#undef TEST_DUP
1173
1174 /* full content - without mandatory which is mutual exclusive with default */
Michal Vasko63f3d842020-07-08 10:10:14 +02001175 in.current = "l {config false;default \"xxx\";description test;if-feature f;"
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001176 "must 'expr';reference test;status current;type string; units yyy;when true;m:ext;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001177 assert_int_equal(LY_SUCCESS, parse_leaf(YCTX, NULL, (struct lysp_node **)&l));
Radek Iša56ca9e42020-09-08 18:42:00 +02001178 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 +02001179 assert_string_equal("xxx", l->dflt.str);
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001180 assert_string_equal("yyy", l->units);
1181 assert_string_equal("string", l->type.name);
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001182 assert_non_null(l->musts);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001183 lysp_node_free(&fctx, (struct lysp_node *)l); l = NULL;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001184
1185 /* full content - now with mandatory */
Michal Vasko63f3d842020-07-08 10:10:14 +02001186 in.current = "l {mandatory true; type string;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001187 assert_int_equal(LY_SUCCESS, parse_leaf(YCTX, NULL, (struct lysp_node **)&l));
Radek Iša56ca9e42020-09-08 18:42:00 +02001188 CHECK_LYSP_NODE(l, NULL, 0, LYS_MAND_TRUE, 0, "l", 0, LYS_LEAF, 0, NULL, 0);
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001189 assert_string_equal("string", l->type.name);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001190 lysp_node_free(&fctx, (struct lysp_node *)l); l = NULL;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001191
1192 /* invalid */
Michal Vasko63f3d842020-07-08 10:10:14 +02001193 in.current = " l {description \"missing type\";} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001194 assert_int_equal(LY_EVALID, parse_leaf(YCTX, NULL, (struct lysp_node **)&l));
Radek Iša56ca9e42020-09-08 18:42:00 +02001195 CHECK_LOG_CTX("Missing mandatory keyword \"type\" as a child of \"leaf\".", "Line number 1.");
Michal Vaskoc636ea42022-09-16 10:20:31 +02001196 lysp_node_free(&fctx, (struct lysp_node *)l); l = NULL;
aPiecek4bb1e372021-05-07 11:01:00 +02001197
1198 in.current = "l { type iid { path qpud wrong {";
1199 assert_int_equal(LY_EVALID, parse_leaf(YCTX, NULL, (struct lysp_node **)&l));
1200 CHECK_LOG_CTX("Invalid character sequence \"wrong\", expected a keyword.", "Line number 1.");
Michal Vaskoc636ea42022-09-16 10:20:31 +02001201 lysp_node_free(&fctx, (struct lysp_node *)l); l = NULL;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001202}
1203
Radek Krejci0e5d8382018-11-28 16:37:53 +01001204static void
1205test_leaflist(void **state)
1206{
Radek Krejci0e5d8382018-11-28 16:37:53 +01001207 struct lysp_node_leaflist *ll = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001208
Michal Vasko8a67eff2021-12-07 14:04:47 +01001209 PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
Radek Krejci0e5d8382018-11-28 16:37:53 +01001210
1211 /* invalid cardinality */
1212#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001213 in.current = "ll {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001214 assert_int_equal(LY_EVALID, parse_leaflist(YCTX, NULL, (struct lysp_node**)&ll)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001215 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Michal Vaskoc636ea42022-09-16 10:20:31 +02001216 lysp_node_free(&fctx, (struct lysp_node*)ll); ll = NULL;
Radek Krejci0e5d8382018-11-28 16:37:53 +01001217
1218 TEST_DUP("config", "true", "false");
1219 TEST_DUP("description", "text1", "text2");
1220 TEST_DUP("max-elements", "10", "20");
1221 TEST_DUP("min-elements", "10", "20");
1222 TEST_DUP("ordered-by", "user", "system");
1223 TEST_DUP("reference", "1", "2");
1224 TEST_DUP("status", "current", "obsolete");
1225 TEST_DUP("type", "int8", "uint8");
1226 TEST_DUP("units", "text1", "text2");
1227 TEST_DUP("when", "true", "false");
1228#undef TEST_DUP
1229
1230 /* full content - without min-elements which is mutual exclusive with default */
Michal Vasko63f3d842020-07-08 10:10:14 +02001231 in.current = "ll {config false;default \"xxx\"; default \"yyy\";description test;if-feature f;"
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001232 "max-elements 10;must 'expr';ordered-by user;reference test;"
1233 "status current;type string; units zzz;when true;m:ext;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001234 assert_int_equal(LY_SUCCESS, parse_leaflist(YCTX, NULL, (struct lysp_node **)&ll));
Radek Iša56ca9e42020-09-08 18:42:00 +02001235 CHECK_LYSP_NODE(ll, "test", 1, 0x446, 1, "ll", 0, LYS_LEAFLIST, 0, "test", 1);
Radek Krejci0e5d8382018-11-28 16:37:53 +01001236 assert_non_null(ll->dflts);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001237 assert_int_equal(2, LY_ARRAY_COUNT(ll->dflts));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001238 assert_string_equal("xxx", ll->dflts[0].str);
1239 assert_string_equal("yyy", ll->dflts[1].str);
Radek Krejci0e5d8382018-11-28 16:37:53 +01001240 assert_string_equal("zzz", ll->units);
1241 assert_int_equal(10, ll->max);
1242 assert_int_equal(0, ll->min);
1243 assert_string_equal("string", ll->type.name);
Radek Krejci0e5d8382018-11-28 16:37:53 +01001244 assert_non_null(ll->musts);
Radek Krejci0e5d8382018-11-28 16:37:53 +01001245 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_USER | LYS_SET_MAX, ll->flags);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001246 lysp_node_free(&fctx, (struct lysp_node *)ll); ll = NULL;
Radek Krejci0e5d8382018-11-28 16:37:53 +01001247
1248 /* full content - now with min-elements */
Michal Vasko63f3d842020-07-08 10:10:14 +02001249 in.current = "ll {min-elements 10; type string;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001250 assert_int_equal(LY_SUCCESS, parse_leaflist(YCTX, NULL, (struct lysp_node **)&ll));
Radek Iša56ca9e42020-09-08 18:42:00 +02001251 CHECK_LYSP_NODE(ll, NULL, 0, 0x200, 0, "ll", 0, LYS_LEAFLIST, 0, NULL, 0);
Radek Krejci0e5d8382018-11-28 16:37:53 +01001252 assert_string_equal("string", ll->type.name);
1253 assert_int_equal(0, ll->max);
1254 assert_int_equal(10, ll->min);
1255 assert_int_equal(LYS_SET_MIN, ll->flags);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001256 lysp_node_free(&fctx, (struct lysp_node *)ll); ll = NULL;
Radek Krejci0e5d8382018-11-28 16:37:53 +01001257
1258 /* invalid */
Michal Vasko63f3d842020-07-08 10:10:14 +02001259 in.current = " ll {description \"missing type\";} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001260 assert_int_equal(LY_EVALID, parse_leaflist(YCTX, NULL, (struct lysp_node **)&ll));
Radek Iša56ca9e42020-09-08 18:42:00 +02001261 CHECK_LOG_CTX("Missing mandatory keyword \"type\" as a child of \"leaf-list\".", "Line number 1.");
Michal Vaskoc636ea42022-09-16 10:20:31 +02001262 lysp_node_free(&fctx, (struct lysp_node *)ll); ll = NULL;
Radek Krejci0e5d8382018-11-28 16:37:53 +01001263
Michal Vasko8a67eff2021-12-07 14:04:47 +01001264 PARSER_CUR_PMOD(YCTX)->version = 1; /* simulate YANG 1.0 - default statement is not allowed */
Michal Vasko63f3d842020-07-08 10:10:14 +02001265 in.current = " ll {default xx; type string;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001266 assert_int_equal(LY_EVALID, parse_leaflist(YCTX, NULL, (struct lysp_node **)&ll));
Radek Iša56ca9e42020-09-08 18:42:00 +02001267 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 Vaskoc636ea42022-09-16 10:20:31 +02001268 lysp_node_free(&fctx, (struct lysp_node *)ll); ll = NULL;
Radek Krejci0e5d8382018-11-28 16:37:53 +01001269}
1270
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001271static void
1272test_list(void **state)
1273{
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001274 struct lysp_node_list *l = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001275
Michal Vasko8a67eff2021-12-07 14:04:47 +01001276 PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
Michal Vaskod0625d72022-10-06 15:02:50 +02001277 YCTX->main_ctx = (struct lysp_ctx *)YCTX;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001278
1279 /* invalid cardinality */
1280#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001281 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001282 assert_int_equal(LY_EVALID, parse_list(YCTX, NULL, (struct lysp_node**)&l)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001283 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Michal Vaskoc636ea42022-09-16 10:20:31 +02001284 lysp_node_free(&fctx, (struct lysp_node*)l); l = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001285
1286 TEST_DUP("config", "true", "false");
1287 TEST_DUP("description", "text1", "text2");
1288 TEST_DUP("key", "one", "two");
1289 TEST_DUP("max-elements", "10", "20");
1290 TEST_DUP("min-elements", "10", "20");
1291 TEST_DUP("ordered-by", "user", "system");
1292 TEST_DUP("reference", "1", "2");
1293 TEST_DUP("status", "current", "obsolete");
1294 TEST_DUP("when", "true", "false");
1295#undef TEST_DUP
1296
1297 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001298 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 +01001299 "leaf-list ll {type string;} list li;max-elements 10; min-elements 1;must 'expr';notification not; ordered-by system; reference test;"
1300 "status current;typedef t {type int8;}unique xxx;unique yyy;uses g;when true;m:ext;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001301 assert_int_equal(LY_SUCCESS, parse_list(YCTX, NULL, (struct lysp_node **)&l));
Radek Iša56ca9e42020-09-08 18:42:00 +02001302 CHECK_LYSP_NODE(l, "test", 1, LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM | LYS_SET_MAX | LYS_SET_MIN, 1, "l",
1303 0, LYS_LIST, 0, "test", 1);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001304 assert_string_equal("l", l->key);
1305 assert_non_null(l->uniques);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001306 assert_int_equal(2, LY_ARRAY_COUNT(l->uniques));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001307 assert_string_equal("xxx", l->uniques[0].str);
1308 assert_string_equal("yyy", l->uniques[1].str);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001309 assert_int_equal(10, l->max);
1310 assert_int_equal(1, l->min);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001311 assert_non_null(l->musts);
Radek Iša56ca9e42020-09-08 18:42:00 +02001312 ly_set_erase(&YCTX->tpdfs_nodes, NULL);
aPiecek63e080d2021-06-29 13:53:28 +02001313 ly_set_erase(&YCTX->grps_nodes, NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001314 lysp_node_free(&fctx, (struct lysp_node *)l); l = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001315
Radek Krejcif538ce52019-03-05 10:46:14 +01001316 /* invalid content */
Michal Vasko8a67eff2021-12-07 14:04:47 +01001317 PARSER_CUR_PMOD(YCTX)->version = 1; /* simulate YANG 1.0 */
Michal Vasko63f3d842020-07-08 10:10:14 +02001318 in.current = "l {action x;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001319 assert_int_equal(LY_EVALID, parse_list(YCTX, NULL, (struct lysp_node **)&l));
Radek Iša56ca9e42020-09-08 18:42:00 +02001320 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 Vaskoc636ea42022-09-16 10:20:31 +02001321 lysp_node_free(&fctx, (struct lysp_node *)l); l = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001322}
1323
Radek Krejci056d0a82018-12-06 16:57:25 +01001324static void
1325test_choice(void **state)
1326{
Radek Krejci056d0a82018-12-06 16:57:25 +01001327 struct lysp_node_choice *ch = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001328
Michal Vasko8a67eff2021-12-07 14:04:47 +01001329 PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
Radek Krejci056d0a82018-12-06 16:57:25 +01001330
1331 /* invalid cardinality */
1332#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001333 in.current = "ch {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001334 assert_int_equal(LY_EVALID, parse_choice(YCTX, NULL, (struct lysp_node**)&ch)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001335 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Michal Vaskoc636ea42022-09-16 10:20:31 +02001336 lysp_node_free(&fctx, (struct lysp_node*)ch); ch = NULL;
Radek Krejci056d0a82018-12-06 16:57:25 +01001337
1338 TEST_DUP("config", "true", "false");
1339 TEST_DUP("default", "a", "b");
1340 TEST_DUP("description", "text1", "text2");
1341 TEST_DUP("mandatory", "true", "false");
1342 TEST_DUP("reference", "1", "2");
1343 TEST_DUP("status", "current", "obsolete");
1344 TEST_DUP("when", "true", "false");
1345#undef TEST_DUP
1346
Radek Krejcia9026eb2018-12-12 16:04:47 +01001347 /* full content - without default due to a collision with mandatory */
Michal Vasko63f3d842020-07-08 10:10:14 +02001348 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 +01001349 "leaf-list ll {type string;} list li;mandatory true;reference test;status current;when true;m:ext;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001350 assert_int_equal(LY_SUCCESS, parse_choice(YCTX, NULL, (struct lysp_node **)&ch));
Radek Iša56ca9e42020-09-08 18:42:00 +02001351 CHECK_LYSP_NODE(ch, "test", 1, LYS_CONFIG_R | LYS_STATUS_CURR | LYS_MAND_TRUE, 1, "ch", 0, LYS_CHOICE, 0, "test", 1);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001352 lysp_node_free(&fctx, (struct lysp_node *)ch); ch = NULL;
Radek Krejci056d0a82018-12-06 16:57:25 +01001353
Radek Krejcia9026eb2018-12-12 16:04:47 +01001354 /* full content - the default missing from the previous node */
Michal Vasko63f3d842020-07-08 10:10:14 +02001355 in.current = "ch {default c;case c;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001356 assert_int_equal(LY_SUCCESS, parse_choice(YCTX, NULL, (struct lysp_node **)&ch));
Radek Iša56ca9e42020-09-08 18:42:00 +02001357 CHECK_LYSP_NODE(ch, NULL, 0, 0, 0, "ch", 0, LYS_CHOICE, 0, NULL, 0);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001358 assert_string_equal("c", ch->dflt.str);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001359 lysp_node_free(&fctx, (struct lysp_node *)ch); ch = NULL;
Radek Krejcia9026eb2018-12-12 16:04:47 +01001360}
1361
1362static void
1363test_case(void **state)
1364{
Radek Krejcia9026eb2018-12-12 16:04:47 +01001365 struct lysp_node_case *cs = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001366
Michal Vasko8a67eff2021-12-07 14:04:47 +01001367 PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
Radek Krejcia9026eb2018-12-12 16:04:47 +01001368
1369 /* invalid cardinality */
1370#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001371 in.current = "cs {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001372 assert_int_equal(LY_EVALID, parse_case(YCTX, NULL, (struct lysp_node**)&cs)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001373 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Michal Vaskoc636ea42022-09-16 10:20:31 +02001374 lysp_node_free(&fctx, (struct lysp_node*)cs); cs = NULL;
Radek Krejcia9026eb2018-12-12 16:04:47 +01001375
1376 TEST_DUP("description", "text1", "text2");
1377 TEST_DUP("reference", "1", "2");
1378 TEST_DUP("status", "current", "obsolete");
1379 TEST_DUP("when", "true", "false");
1380#undef TEST_DUP
1381
1382 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001383 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 +01001384 "leaf-list ll {type string;} list li;reference test;status current;uses grp;when true;m:ext;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001385 assert_int_equal(LY_SUCCESS, parse_case(YCTX, NULL, (struct lysp_node **)&cs));
Radek Iša56ca9e42020-09-08 18:42:00 +02001386 CHECK_LYSP_NODE(cs, "test", 1, LYS_STATUS_CURR, 1, "cs", 0, LYS_CASE, 0, "test", 1);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001387 lysp_node_free(&fctx, (struct lysp_node *)cs); cs = NULL;
Radek Krejcia9026eb2018-12-12 16:04:47 +01001388
1389 /* invalid content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001390 in.current = "cs {config true} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001391 assert_int_equal(LY_EVALID, parse_case(YCTX, NULL, (struct lysp_node **)&cs));
Radek Iša56ca9e42020-09-08 18:42:00 +02001392 CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"case\".", "Line number 1.");
Michal Vaskoc636ea42022-09-16 10:20:31 +02001393 lysp_node_free(&fctx, (struct lysp_node *)cs); cs = NULL;
Radek Krejci056d0a82018-12-06 16:57:25 +01001394}
1395
Radek Krejci9800fb82018-12-13 14:26:23 +01001396static void
Radek Krejcid6b76452019-09-03 17:03:03 +02001397test_any(void **state, enum ly_stmt kw)
Radek Krejci9800fb82018-12-13 14:26:23 +01001398{
Radek Krejci9800fb82018-12-13 14:26:23 +01001399 struct lysp_node_anydata *any = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001400
Radek Krejcid6b76452019-09-03 17:03:03 +02001401 if (kw == LY_STMT_ANYDATA) {
Michal Vasko8a67eff2021-12-07 14:04:47 +01001402 PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
Radek Krejci9800fb82018-12-13 14:26:23 +01001403 } else {
Michal Vasko8a67eff2021-12-07 14:04:47 +01001404 PARSER_CUR_PMOD(YCTX)->version = 1; /* simulate YANG 1.0 */
Radek Krejci9800fb82018-12-13 14:26:23 +01001405 }
1406
1407 /* invalid cardinality */
1408#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001409 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001410 assert_int_equal(LY_EVALID, parse_any(YCTX, kw, NULL, (struct lysp_node**)&any)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001411 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Michal Vaskoc636ea42022-09-16 10:20:31 +02001412 lysp_node_free(&fctx, (struct lysp_node*)any); any = NULL;
Radek Krejci9800fb82018-12-13 14:26:23 +01001413
1414 TEST_DUP("config", "true", "false");
1415 TEST_DUP("description", "text1", "text2");
1416 TEST_DUP("mandatory", "true", "false");
1417 TEST_DUP("reference", "1", "2");
1418 TEST_DUP("status", "current", "obsolete");
1419 TEST_DUP("when", "true", "false");
1420#undef TEST_DUP
1421
1422 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001423 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 +01001424 assert_int_equal(LY_SUCCESS, parse_any(YCTX, kw, NULL, (struct lysp_node **)&any));
Radek Iša56ca9e42020-09-08 18:42:00 +02001425 // CHECK_LYSP_NODE(NODE, DSC, EXTS, FLAGS, IFFEATURES, NAME, NEXT, TYPE, PARENT, REF, WHEN)
1426 uint16_t node_type = kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
1427 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 +01001428 assert_non_null(any->musts);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001429 lysp_node_free(&fctx, (struct lysp_node *)any); any = NULL;
Radek Krejci9800fb82018-12-13 14:26:23 +01001430}
1431
1432static void
1433test_anydata(void **state)
1434{
Michal Vaskoa0a498b2021-09-22 12:17:48 +02001435 test_any(state, LY_STMT_ANYDATA);
Radek Krejci9800fb82018-12-13 14:26:23 +01001436}
1437
1438static void
1439test_anyxml(void **state)
1440{
Michal Vaskoa0a498b2021-09-22 12:17:48 +02001441 test_any(state, LY_STMT_ANYXML);
Radek Krejci9800fb82018-12-13 14:26:23 +01001442}
1443
Radek Krejcie86bf772018-12-14 11:39:53 +01001444static void
1445test_grouping(void **state)
1446{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001447 struct lysp_node_grp *grp = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001448
Michal Vasko8a67eff2021-12-07 14:04:47 +01001449 PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
Michal Vaskod0625d72022-10-06 15:02:50 +02001450 YCTX->main_ctx = (struct lysp_ctx *)YCTX;
Radek Krejcie86bf772018-12-14 11:39:53 +01001451
1452 /* invalid cardinality */
1453#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001454 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001455 assert_int_equal(LY_EVALID, parse_grouping(YCTX, NULL, &grp)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001456 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Michal Vaskoc636ea42022-09-16 10:20:31 +02001457 lysp_node_free(&fctx, &grp->node); grp = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001458
1459 TEST_DUP("description", "text1", "text2");
1460 TEST_DUP("reference", "1", "2");
1461 TEST_DUP("status", "current", "obsolete");
1462#undef TEST_DUP
1463
1464 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001465 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 +01001466 "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 +01001467 assert_int_equal(LY_SUCCESS, parse_grouping(YCTX, NULL, &grp));
Radek Krejcie86bf772018-12-14 11:39:53 +01001468 assert_non_null(grp);
1469 assert_int_equal(LYS_GROUPING, grp->nodetype);
1470 assert_string_equal("grp", grp->name);
1471 assert_string_equal("test", grp->dsc);
1472 assert_non_null(grp->exts);
1473 assert_string_equal("test", grp->ref);
1474 assert_null(grp->parent);
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001475 assert_int_equal(LYS_STATUS_CURR, grp->flags);
Radek Iša56ca9e42020-09-08 18:42:00 +02001476 ly_set_erase(&YCTX->tpdfs_nodes, NULL);
aPiecek63e080d2021-06-29 13:53:28 +02001477 ly_set_erase(&YCTX->grps_nodes, NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001478 lysp_node_free(&fctx, &grp->node);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001479 grp = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001480
1481 /* invalid content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001482 in.current = "grp {config true} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001483 assert_int_equal(LY_EVALID, parse_grouping(YCTX, NULL, &grp));
Radek Iša56ca9e42020-09-08 18:42:00 +02001484 CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"grouping\".", "Line number 1.");
Michal Vaskoc636ea42022-09-16 10:20:31 +02001485 lysp_node_free(&fctx, &grp->node);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001486 grp = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001487
Michal Vasko63f3d842020-07-08 10:10:14 +02001488 in.current = "grp {must 'expr'} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001489 assert_int_equal(LY_EVALID, parse_grouping(YCTX, NULL, &grp));
Radek Iša56ca9e42020-09-08 18:42:00 +02001490 CHECK_LOG_CTX("Invalid keyword \"must\" as a child of \"grouping\".", "Line number 1.");
Michal Vaskoc636ea42022-09-16 10:20:31 +02001491 lysp_node_free(&fctx, &grp->node);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001492 grp = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001493}
1494
1495static void
Radek Krejcif538ce52019-03-05 10:46:14 +01001496test_action(void **state)
1497{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001498 struct lysp_node_action *rpcs = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01001499 struct lysp_node_container *c = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001500
Michal Vasko8a67eff2021-12-07 14:04:47 +01001501 PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
Michal Vaskod0625d72022-10-06 15:02:50 +02001502 YCTX->main_ctx = (struct lysp_ctx *)YCTX;
Radek Krejcif538ce52019-03-05 10:46:14 +01001503
1504 /* invalid cardinality */
1505#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001506 in.current = "func {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001507 assert_int_equal(LY_EVALID, parse_action(YCTX, NULL, &rpcs)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001508 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Michal Vaskoc636ea42022-09-16 10:20:31 +02001509 lysp_node_free(&fctx, (struct lysp_node*)rpcs); rpcs = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01001510
1511 TEST_DUP("description", "text1", "text2");
Michal Vaskob83af8a2020-01-06 09:49:22 +01001512 TEST_DUP("input", "{leaf l1 {type empty;}} description a", "{leaf l2 {type empty;}} description a");
1513 TEST_DUP("output", "{leaf l1 {type empty;}} description a", "{leaf l2 {type empty;}} description a");
Radek Krejcif538ce52019-03-05 10:46:14 +01001514 TEST_DUP("reference", "1", "2");
1515 TEST_DUP("status", "current", "obsolete");
1516#undef TEST_DUP
1517
1518 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001519 in.current = "top;";
Radek Krejci33090f92020-12-17 20:12:46 +01001520 assert_int_equal(LY_SUCCESS, parse_container(YCTX, NULL, (struct lysp_node **)&c));
Michal Vasko63f3d842020-07-08 10:10:14 +02001521 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 +01001522 "input {anydata a1; anyxml a2; choice ch; container c; grouping grp; leaf l {type int8;} leaf-list ll {type int8;}"
1523 " list li; must 1; typedef mytypei {type int8;} uses grp; m:ext;}"
1524 "output {anydata a1; anyxml a2; choice ch; container c; grouping grp; leaf l {type int8;} leaf-list ll {type int8;}"
1525 " list li; must 1; typedef mytypeo {type int8;} uses grp; m:ext;}} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001526 assert_int_equal(LY_SUCCESS, parse_action(YCTX, (struct lysp_node *)c, &rpcs));
Radek Krejcif538ce52019-03-05 10:46:14 +01001527 assert_non_null(rpcs);
1528 assert_int_equal(LYS_ACTION, rpcs->nodetype);
1529 assert_string_equal("func", rpcs->name);
1530 assert_string_equal("test", rpcs->dsc);
1531 assert_non_null(rpcs->exts);
1532 assert_non_null(rpcs->iffeatures);
1533 assert_string_equal("test", rpcs->ref);
1534 assert_non_null(rpcs->groupings);
1535 assert_non_null(rpcs->typedefs);
1536 assert_int_equal(LYS_STATUS_CURR, rpcs->flags);
1537 /* input */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001538 assert_int_equal(rpcs->input.nodetype, LYS_INPUT);
Radek Krejcif538ce52019-03-05 10:46:14 +01001539 assert_non_null(rpcs->input.groupings);
1540 assert_non_null(rpcs->input.exts);
1541 assert_non_null(rpcs->input.musts);
1542 assert_non_null(rpcs->input.typedefs);
Radek Krejci01180ac2021-01-27 08:48:22 +01001543 assert_non_null(rpcs->input.child);
Radek Krejcif538ce52019-03-05 10:46:14 +01001544 /* output */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001545 assert_int_equal(rpcs->output.nodetype, LYS_OUTPUT);
Radek Krejcif538ce52019-03-05 10:46:14 +01001546 assert_non_null(rpcs->output.groupings);
1547 assert_non_null(rpcs->output.exts);
1548 assert_non_null(rpcs->output.musts);
1549 assert_non_null(rpcs->output.typedefs);
Radek Krejci01180ac2021-01-27 08:48:22 +01001550 assert_non_null(rpcs->output.child);
Radek Krejcif538ce52019-03-05 10:46:14 +01001551
Radek Iša56ca9e42020-09-08 18:42:00 +02001552 ly_set_erase(&YCTX->tpdfs_nodes, NULL);
aPiecek63e080d2021-06-29 13:53:28 +02001553 ly_set_erase(&YCTX->grps_nodes, NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001554 lysp_node_free(&fctx, (struct lysp_node *)rpcs); rpcs = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01001555
1556 /* invalid content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001557 in.current = "func {config true} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001558 assert_int_equal(LY_EVALID, parse_action(YCTX, NULL, &rpcs));
Radek Iša56ca9e42020-09-08 18:42:00 +02001559 CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"rpc\".", "Line number 1.");
Michal Vaskoc636ea42022-09-16 10:20:31 +02001560 lysp_node_free(&fctx, (struct lysp_node *)rpcs); rpcs = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01001561
Michal Vaskoc636ea42022-09-16 10:20:31 +02001562 lysp_node_free(&fctx, (struct lysp_node *)c);
Radek Krejcif538ce52019-03-05 10:46:14 +01001563}
1564
1565static void
Radek Krejcifc11bd72019-04-11 16:00:05 +02001566test_notification(void **state)
1567{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001568 struct lysp_node_notif *notifs = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001569 struct lysp_node_container *c = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001570
Michal Vasko8a67eff2021-12-07 14:04:47 +01001571 PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
Michal Vaskod0625d72022-10-06 15:02:50 +02001572 YCTX->main_ctx = (struct lysp_ctx *)YCTX;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001573
1574 /* invalid cardinality */
1575#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001576 in.current = "func {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001577 assert_int_equal(LY_EVALID, parse_notif(YCTX, NULL, &notifs)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001578 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Michal Vaskoc636ea42022-09-16 10:20:31 +02001579 lysp_node_free(&fctx, (struct lysp_node*)notifs); notifs = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001580
1581 TEST_DUP("description", "text1", "text2");
1582 TEST_DUP("reference", "1", "2");
1583 TEST_DUP("status", "current", "obsolete");
1584#undef TEST_DUP
1585
1586 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001587 in.current = "top;";
Radek Krejci33090f92020-12-17 20:12:46 +01001588 assert_int_equal(LY_SUCCESS, parse_container(YCTX, NULL, (struct lysp_node **)&c));
Michal Vasko63f3d842020-07-08 10:10:14 +02001589 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 +01001590 "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 +01001591 assert_int_equal(LY_SUCCESS, parse_notif(YCTX, (struct lysp_node *)c, &notifs));
Radek Krejcifc11bd72019-04-11 16:00:05 +02001592 assert_non_null(notifs);
1593 assert_int_equal(LYS_NOTIF, notifs->nodetype);
1594 assert_string_equal("ntf", notifs->name);
1595 assert_string_equal("test", notifs->dsc);
1596 assert_non_null(notifs->exts);
1597 assert_non_null(notifs->iffeatures);
1598 assert_string_equal("test", notifs->ref);
1599 assert_non_null(notifs->groupings);
1600 assert_non_null(notifs->typedefs);
1601 assert_non_null(notifs->musts);
Radek Krejci01180ac2021-01-27 08:48:22 +01001602 assert_non_null(notifs->child);
Radek Krejcifc11bd72019-04-11 16:00:05 +02001603 assert_int_equal(LYS_STATUS_CURR, notifs->flags);
1604
Radek Iša56ca9e42020-09-08 18:42:00 +02001605 ly_set_erase(&YCTX->tpdfs_nodes, NULL);
aPiecek63e080d2021-06-29 13:53:28 +02001606 ly_set_erase(&YCTX->grps_nodes, NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001607 lysp_node_free(&fctx, (struct lysp_node *)notifs); notifs = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001608
1609 /* invalid content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001610 in.current = "ntf {config true} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001611 assert_int_equal(LY_EVALID, parse_notif(YCTX, NULL, &notifs));
Radek Iša56ca9e42020-09-08 18:42:00 +02001612 CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"notification\".", "Line number 1.");
Michal Vaskoc636ea42022-09-16 10:20:31 +02001613 lysp_node_free(&fctx, (struct lysp_node *)notifs); notifs = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001614
Michal Vaskoc636ea42022-09-16 10:20:31 +02001615 lysp_node_free(&fctx, (struct lysp_node *)c);
Radek Krejcifc11bd72019-04-11 16:00:05 +02001616}
1617
1618static void
Radek Krejcie86bf772018-12-14 11:39:53 +01001619test_uses(void **state)
1620{
Radek Krejcie86bf772018-12-14 11:39:53 +01001621 struct lysp_node_uses *u = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001622
Michal Vasko8a67eff2021-12-07 14:04:47 +01001623 PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
Radek Krejcie86bf772018-12-14 11:39:53 +01001624
1625 /* invalid cardinality */
1626#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001627 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001628 assert_int_equal(LY_EVALID, parse_uses(YCTX, NULL, (struct lysp_node**)&u)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001629 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Michal Vaskoc636ea42022-09-16 10:20:31 +02001630 lysp_node_free(&fctx, (struct lysp_node*)u); u = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001631
1632 TEST_DUP("description", "text1", "text2");
1633 TEST_DUP("reference", "1", "2");
1634 TEST_DUP("status", "current", "obsolete");
1635 TEST_DUP("when", "true", "false");
1636#undef TEST_DUP
1637
1638 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001639 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 +01001640 assert_int_equal(LY_SUCCESS, parse_uses(YCTX, NULL, (struct lysp_node **)&u));
Radek Iša56ca9e42020-09-08 18:42:00 +02001641 CHECK_LYSP_NODE(u, "test", 1, LYS_STATUS_CURR, 1, "grpref", 0, LYS_USES, 0, "test", 1);
Radek Krejcie86bf772018-12-14 11:39:53 +01001642 assert_non_null(u->augments);
1643 assert_non_null(u->refines);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001644 lysp_node_free(&fctx, (struct lysp_node *)u); u = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001645}
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001646
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001647static void
1648test_augment(void **state)
1649{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001650 struct lysp_node_augment *a = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001651
Michal Vasko8a67eff2021-12-07 14:04:47 +01001652 PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001653
1654 /* invalid cardinality */
1655#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001656 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001657 assert_int_equal(LY_EVALID, parse_augment(YCTX, NULL, &a)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001658 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Michal Vaskoc636ea42022-09-16 10:20:31 +02001659 lysp_node_free(&fctx, (struct lysp_node *)a); a = NULL;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001660
1661 TEST_DUP("description", "text1", "text2");
1662 TEST_DUP("reference", "1", "2");
1663 TEST_DUP("status", "current", "obsolete");
1664 TEST_DUP("when", "true", "false");
1665#undef TEST_DUP
1666
1667 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001668 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 +01001669 "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 +01001670 assert_int_equal(LY_SUCCESS, parse_augment(YCTX, NULL, &a));
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001671 assert_non_null(a);
1672 assert_int_equal(LYS_AUGMENT, a->nodetype);
1673 assert_string_equal("/target/nodeid", a->nodeid);
1674 assert_string_equal("test", a->dsc);
1675 assert_non_null(a->exts);
1676 assert_non_null(a->iffeatures);
1677 assert_string_equal("test", a->ref);
1678 assert_non_null(a->when);
1679 assert_null(a->parent);
1680 assert_int_equal(LYS_STATUS_CURR, a->flags);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001681 lysp_node_free(&fctx, (struct lysp_node *)a); a = NULL;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001682}
1683
Radek Krejcif09e4e82019-06-14 15:08:11 +02001684static void
1685test_when(void **state)
1686{
Radek Krejcif09e4e82019-06-14 15:08:11 +02001687 struct lysp_when *w = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001688
Michal Vasko8a67eff2021-12-07 14:04:47 +01001689 PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
Radek Krejcif09e4e82019-06-14 15:08:11 +02001690
Michal Vasko12ef5362022-09-16 15:13:58 +02001691 in.current = "l { description text1;description text2;} ...";
1692 assert_int_equal(LY_EVALID, parse_when(YCTX, &w));
1693 assert_null(w);
1694 CHECK_LOG_CTX("Duplicate keyword \"description\".", "Line number 1.");
Radek Krejcif09e4e82019-06-14 15:08:11 +02001695
Michal Vasko12ef5362022-09-16 15:13:58 +02001696 in.current = "l { reference 1;reference 2;} ...";
1697 assert_int_equal(LY_EVALID, parse_when(YCTX, &w));
1698 assert_null(w);
1699 CHECK_LOG_CTX("Duplicate keyword \"reference\".", "Line number 1.");
Radek Krejcif09e4e82019-06-14 15:08:11 +02001700}
1701
David Sedlákd6ce6d72019-07-16 17:30:18 +02001702static void
1703test_value(void **state)
1704{
Michal Vasko193dacd2022-10-13 08:43:05 +02001705 struct lysp_type_enum enm;
David Sedlákd6ce6d72019-07-16 17:30:18 +02001706
Michal Vasko63f3d842020-07-08 10:10:14 +02001707 in.current = "-0;";
Michal Vasko193dacd2022-10-13 08:43:05 +02001708 memset(&enm, 0, sizeof enm);
1709 assert_int_equal(parse_type_enum_value_pos(YCTX, LY_STMT_VALUE, &enm), LY_SUCCESS);
David Sedlákd6ce6d72019-07-16 17:30:18 +02001710
Michal Vasko63f3d842020-07-08 10:10:14 +02001711 in.current = "-0;";
Michal Vasko193dacd2022-10-13 08:43:05 +02001712 memset(&enm, 0, sizeof enm);
1713 assert_int_equal(parse_type_enum_value_pos(YCTX, LY_STMT_POSITION, &enm), LY_EVALID);
Radek Iša56ca9e42020-09-08 18:42:00 +02001714 CHECK_LOG_CTX("Invalid value \"-0\" of \"position\".", "Line number 1.");
David Sedlákd6ce6d72019-07-16 17:30:18 +02001715}
1716
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001717int
1718main(void)
Radek Krejci80dd33e2018-09-26 15:57:18 +02001719{
1720 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +02001721 UTEST(test_helpers, setup, teardown),
1722 UTEST(test_comments, setup, teardown),
1723 UTEST(test_arg, setup, teardown),
1724 UTEST(test_stmts, setup, teardown),
1725 UTEST(test_minmax, setup, teardown),
Michal Vasko12ef5362022-09-16 15:13:58 +02001726 UTEST(test_valid_module, setup, teardown),
Radek Iša56ca9e42020-09-08 18:42:00 +02001727 UTEST(test_module, setup, teardown),
1728 UTEST(test_deviation, setup, teardown),
1729 UTEST(test_deviate, setup, teardown),
1730 UTEST(test_container, setup, teardown),
1731 UTEST(test_leaf, setup, teardown),
1732 UTEST(test_leaflist, setup, teardown),
1733 UTEST(test_list, setup, teardown),
1734 UTEST(test_choice, setup, teardown),
1735 UTEST(test_case, setup, teardown),
1736 UTEST(test_anydata, setup, teardown),
1737 UTEST(test_anyxml, setup, teardown),
1738 UTEST(test_action, setup, teardown),
1739 UTEST(test_notification, setup, teardown),
1740 UTEST(test_grouping, setup, teardown),
1741 UTEST(test_uses, setup, teardown),
1742 UTEST(test_augment, setup, teardown),
1743 UTEST(test_when, setup, teardown),
1744 UTEST(test_value, setup, teardown),
Radek Krejci80dd33e2018-09-26 15:57:18 +02001745 };
1746
1747 return cmocka_run_group_tests(tests, NULL, NULL);
1748}