blob: 6d1c2ae48aedc09ad0e3e259c42c07bf1c4433ba [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));
Michal Vaskoc8806c82022-12-06 10:31:24 +010078 YCTX->main_ctx = (struct lysp_ctx *)YCTX;
Radek Iša56ca9e42020-09-08 18:42:00 +020079 YCTX->format = LYS_IN_YANG;
Michal Vasko8a67eff2021-12-07 14:04:47 +010080 ly_set_new(&YCTX->parsed_mods);
Michal Vasko5d24f6c2020-10-13 13:49:06 +020081
Radek Iša56ca9e42020-09-08 18:42:00 +020082 /* allocate new parsed module */
Michal Vasko8a67eff2021-12-07 14:04:47 +010083 pmod = calloc(1, sizeof *pmod);
84 ly_set_add(YCTX->parsed_mods, pmod, 1, NULL);
Michal Vasko5d24f6c2020-10-13 13:49:06 +020085
Radek Iša56ca9e42020-09-08 18:42:00 +020086 /* allocate new module */
Michal Vasko8a67eff2021-12-07 14:04:47 +010087 pmod->mod = calloc(1, sizeof *pmod->mod);
88 pmod->mod->ctx = UTEST_LYCTX;
89 pmod->mod->parsed = pmod;
Michal Vasko5d24f6c2020-10-13 13:49:06 +020090
Radek Krejci1640e802021-01-08 11:01:30 +010091 /* initilize and use the global easily available and customizable input handler */
92 in.line = 1;
93 YCTX->in = &in;
Michal Vaskof8ebf132022-11-21 14:06:48 +010094 LOG_LOCSET(NULL, NULL, NULL, &in);
Radek Krejci1640e802021-01-08 11:01:30 +010095
Michal Vaskoc636ea42022-09-16 10:20:31 +020096 fctx.ctx = PARSER_CTX(YCTX);
97 fctx.mod = pmod->mod;
98
Michal Vasko5d24f6c2020-10-13 13:49:06 +020099 return 0;
100}
101
102static int
Radek Iša56ca9e42020-09-08 18:42:00 +0200103teardown(void **state)
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200104{
Michal Vaskoc636ea42022-09-16 10:20:31 +0200105 lys_module_free(&fctx, PARSER_CUR_PMOD(YCTX)->mod, 0);
Radek Krejciddace2c2021-01-08 11:30:56 +0100106 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejci1640e802021-01-08 11:01:30 +0100107
Michal Vasko8a67eff2021-12-07 14:04:47 +0100108 ly_set_free(YCTX->parsed_mods, NULL);
Michal Vaskoc0c64ae2022-10-06 10:15:23 +0200109 ly_set_erase(&YCTX->ext_inst, NULL);
Radek Iša56ca9e42020-09-08 18:42:00 +0200110 free(YCTX);
111 YCTX = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200112
Michal Vaskoc636ea42022-09-16 10:20:31 +0200113 lysf_ctx_erase(&fctx);
114
Radek Iša56ca9e42020-09-08 18:42:00 +0200115 UTEST_TEARDOWN;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200116
117 return 0;
118}
119
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200120#define TEST_DUP_GENERIC(PREFIX, MEMBER, VALUE1, VALUE2, FUNC, RESULT, LINE, CLEANUP) \
Michal Vasko63f3d842020-07-08 10:10:14 +0200121 in.current = PREFIX MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +0100122 assert_int_equal(LY_EVALID, FUNC(YCTX, RESULT)); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200123 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number "LINE".");\
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200124 CLEANUP
Radek Krejci44ceedc2018-10-02 15:54:31 +0200125static void
126test_helpers(void **state)
127{
Radek Krejci404251e2018-10-09 12:06:44 +0200128 char *buf, *p;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200129 size_t len, size;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200130 uint8_t prefix = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200131
132 /* storing into buffer */
Michal Vasko63f3d842020-07-08 10:10:14 +0200133 in.current = "abcd";
Radek Krejci44ceedc2018-10-02 15:54:31 +0200134 buf = NULL;
135 size = len = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200136 assert_int_equal(LY_SUCCESS, buf_add_char(NULL, &in, 2, &buf, &size, &len));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200137 assert_int_not_equal(0, size);
138 assert_int_equal(2, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200139 assert_string_equal("cd", in.current);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200140 assert_false(strncmp("ab", buf, 2));
141 free(buf);
Radek Krejci404251e2018-10-09 12:06:44 +0200142 buf = NULL;
143
144 /* invalid first characters */
145 len = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200146 in.current = "2invalid";
Radek Krejci33090f92020-12-17 20:12:46 +0100147 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 +0200148 in.current = ".invalid";
Radek Krejci33090f92020-12-17 20:12:46 +0100149 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 +0200150 in.current = "-invalid";
Radek Krejci33090f92020-12-17 20:12:46 +0100151 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 +0200152 /* invalid following characters */
153 len = 3; /* number of characters read before the str content */
Michal Vasko63f3d842020-07-08 10:10:14 +0200154 in.current = "!";
Radek Krejci33090f92020-12-17 20:12:46 +0100155 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 +0200156 in.current = ":";
Radek Krejci33090f92020-12-17 20:12:46 +0100157 assert_int_equal(LY_EVALID, buf_store_char(YCTX, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
Michal Vasko62af3692023-02-09 14:00:09 +0100158 UTEST_LOG_CTX_CLEAN;
Radek Krejci404251e2018-10-09 12:06:44 +0200159 /* valid colon for prefixed identifiers */
160 len = size = 0;
161 p = NULL;
David Sedlák40bb13b2019-07-10 14:34:18 +0200162 prefix = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200163 in.current = "x:id";
Radek Krejci33090f92020-12-17 20:12:46 +0100164 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 +0200165 assert_int_equal(1, len);
166 assert_null(buf);
Michal Vasko63f3d842020-07-08 10:10:14 +0200167 assert_string_equal(":id", in.current);
Radek Krejci404251e2018-10-09 12:06:44 +0200168 assert_int_equal('x', p[len - 1]);
Radek Krejci33090f92020-12-17 20:12:46 +0100169 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 +0200170 assert_int_equal(2, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200171 assert_string_equal("id", in.current);
Radek Krejci404251e2018-10-09 12:06:44 +0200172 assert_int_equal(':', p[len - 1]);
173 free(buf);
David Sedlák40bb13b2019-07-10 14:34:18 +0200174 prefix = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200175
176 /* checking identifiers */
Michal Vaskod0625d72022-10-06 15:02:50 +0200177 assert_int_equal(LY_EVALID, lysp_check_identifierchar((struct lysp_ctx *)YCTX, ':', 0, NULL));
Radek Iša56ca9e42020-09-08 18:42:00 +0200178 CHECK_LOG_CTX("Invalid identifier character ':' (0x003a).", "Line number 1.");
Michal Vaskod0625d72022-10-06 15:02:50 +0200179 assert_int_equal(LY_EVALID, lysp_check_identifierchar((struct lysp_ctx *)YCTX, '#', 1, NULL));
Radek Iša56ca9e42020-09-08 18:42:00 +0200180 CHECK_LOG_CTX("Invalid identifier first character '#' (0x0023).", "Line number 1.");
Radek Krejci44ceedc2018-10-02 15:54:31 +0200181
Michal Vaskod0625d72022-10-06 15:02:50 +0200182 assert_int_equal(LY_SUCCESS, lysp_check_identifierchar((struct lysp_ctx *)YCTX, 'a', 1, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200183 assert_int_equal(0, prefix);
Michal Vaskod0625d72022-10-06 15:02:50 +0200184 assert_int_equal(LY_SUCCESS, lysp_check_identifierchar((struct lysp_ctx *)YCTX, ':', 0, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200185 assert_int_equal(1, prefix);
Michal Vaskod0625d72022-10-06 15:02:50 +0200186 assert_int_equal(LY_EVALID, lysp_check_identifierchar((struct lysp_ctx *)YCTX, ':', 0, &prefix));
Michal Vasko62af3692023-02-09 14:00:09 +0100187 CHECK_LOG_CTX("Invalid identifier first character ':' (0x003a).", "Line number 1.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200188 assert_int_equal(1, prefix);
Michal Vaskod0625d72022-10-06 15:02:50 +0200189 assert_int_equal(LY_SUCCESS, lysp_check_identifierchar((struct lysp_ctx *)YCTX, 'b', 0, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200190 assert_int_equal(2, prefix);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200191 /* second colon is invalid */
Michal Vaskod0625d72022-10-06 15:02:50 +0200192 assert_int_equal(LY_EVALID, lysp_check_identifierchar((struct lysp_ctx *)YCTX, ':', 0, &prefix));
Radek Iša56ca9e42020-09-08 18:42:00 +0200193 CHECK_LOG_CTX("Invalid identifier character ':' (0x003a).", "Line number 1.");
Radek Krejci44ceedc2018-10-02 15:54:31 +0200194}
Radek Krejci80dd33e2018-09-26 15:57:18 +0200195
Radek Krejcidd713ce2021-01-04 23:12:12 +0100196#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 +0200197 {\
Radek Krejcidd713ce2021-01-04 23:12:12 +0100198 const char * text = INPUT_TEXT;\
199 in.line = 1;\
Radek Iša56ca9e42020-09-08 18:42:00 +0200200 in.current = text;\
Radek Krejci33090f92020-12-17 20:12:46 +0100201 assert_int_equal(LY_SUCCESS, get_argument(CTX, Y_MAYBE_STR_ARG, NULL, &word, &buf, &len));\
Radek Iša56ca9e42020-09-08 18:42:00 +0200202 assert_string_equal(word, EXPECT_WORD);\
203 assert_int_equal(len, EXPECT_LEN);\
204 assert_string_equal(EXPECT_CURRENT, in.current);\
Radek Krejcidd713ce2021-01-04 23:12:12 +0100205 assert_int_equal(EXPECT_LINE, in.line);\
Radek Iša56ca9e42020-09-08 18:42:00 +0200206 }
207
Radek Krejci80dd33e2018-09-26 15:57:18 +0200208static void
209test_comments(void **state)
210{
Radek Krejciefd22f62018-09-27 11:47:58 +0200211 char *word, *buf;
212 size_t len;
Radek Krejci80dd33e2018-09-26 15:57:18 +0200213
Radek Krejcidd713ce2021-01-04 23:12:12 +0100214 TEST_GET_ARGUMENT_SUCCESS(" // this is a text of / one * line */ comment\nargument;",
215 YCTX, Y_STR_ARG, "argument;", 8, ";", 2);
Radek Krejciefd22f62018-09-27 11:47:58 +0200216 assert_null(buf);
Radek Krejci80dd33e2018-09-26 15:57:18 +0200217
Radek Krejcidd713ce2021-01-04 23:12:12 +0100218 TEST_GET_ARGUMENT_SUCCESS("/* this is a \n * text // of / block * comment */\"arg\" + \"ume\" \n + \n \"nt\";",
219 YCTX, Y_STR_ARG, "argument", 8, ";", 4);
Radek Krejciefd22f62018-09-27 11:47:58 +0200220 assert_ptr_equal(buf, word);
Radek Krejciefd22f62018-09-27 11:47:58 +0200221 free(word);
Radek Krejci80dd33e2018-09-26 15:57:18 +0200222
Radek Krejcidd713ce2021-01-04 23:12:12 +0100223 in.line = 1;
Michal Vasko63f3d842020-07-08 10:10:14 +0200224 in.current = " this is one line comment on last line";
Radek Krejci33090f92020-12-17 20:12:46 +0100225 assert_int_equal(LY_SUCCESS, skip_comment(YCTX, 1));
Michal Vasko63f3d842020-07-08 10:10:14 +0200226 assert_true(in.current[0] == '\0');
Radek Krejci80dd33e2018-09-26 15:57:18 +0200227
Radek Krejcidd713ce2021-01-04 23:12:12 +0100228 in.line = 1;
Michal Vasko63f3d842020-07-08 10:10:14 +0200229 in.current = " this is a not terminated comment x";
Radek Krejci33090f92020-12-17 20:12:46 +0100230 assert_int_equal(LY_EVALID, skip_comment(YCTX, 2));
Radek Krejcidd713ce2021-01-04 23:12:12 +0100231 CHECK_LOG_CTX("Unexpected end-of-input, non-terminated comment.", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200232 assert_true(in.current[0] == '\0');
Radek Krejci80dd33e2018-09-26 15:57:18 +0200233}
234
Radek Krejciefd22f62018-09-27 11:47:58 +0200235static void
236test_arg(void **state)
237{
Radek Krejciefd22f62018-09-27 11:47:58 +0200238 char *word, *buf;
239 size_t len;
240
241 /* missing argument */
Michal Vasko63f3d842020-07-08 10:10:14 +0200242 in.current = ";";
Radek Krejci33090f92020-12-17 20:12:46 +0100243 assert_int_equal(LY_SUCCESS, get_argument(YCTX, Y_MAYBE_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200244 assert_null(word);
245
Michal Vasko63f3d842020-07-08 10:10:14 +0200246 in.current = "{";
Radek Krejci33090f92020-12-17 20:12:46 +0100247 assert_int_equal(LY_EVALID, get_argument(YCTX, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200248 CHECK_LOG_CTX("Invalid character sequence \"{\", expected an argument.", "Line number 1.");
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200249
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200250 /* invalid escape sequence */
Michal Vasko63f3d842020-07-08 10:10:14 +0200251 in.current = "\"\\s\"";
Radek Krejci33090f92020-12-17 20:12:46 +0100252 assert_int_equal(LY_EVALID, get_argument(YCTX, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200253 CHECK_LOG_CTX("Double-quoted string unknown special character \'\\s\'.", "Line number 1.");
254
Radek Krejcidd713ce2021-01-04 23:12:12 +0100255 TEST_GET_ARGUMENT_SUCCESS("\'\\s\'", YCTX, Y_STR_ARG, "\\s\'", 2, "", 1);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200256
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200257 /* invalid character after the argument */
Michal Vasko63f3d842020-07-08 10:10:14 +0200258 in.current = "hello\"";
Radek Krejci33090f92020-12-17 20:12:46 +0100259 assert_int_equal(LY_EVALID, get_argument(YCTX, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200260 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 +0200261
Radek Iša56ca9e42020-09-08 18:42:00 +0200262 in.current = "hello}";
Radek Krejci33090f92020-12-17 20:12:46 +0100263 assert_int_equal(LY_EVALID, get_argument(YCTX, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200264 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 +0200265 /* invalid identifier-ref-arg-str */
Michal Vasko63f3d842020-07-08 10:10:14 +0200266 in.current = "pre:pre:value";
Radek Krejci33090f92020-12-17 20:12:46 +0100267 assert_int_equal(LY_EVALID, get_argument(YCTX, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200268 CHECK_LOG_CTX("Invalid identifier character ':' (0x003a).", "Line number 1.");
David Sedlák40bb13b2019-07-10 14:34:18 +0200269
Michal Vasko63f3d842020-07-08 10:10:14 +0200270 in.current = "\"\";"; /* empty identifier is not allowed */
Radek Krejci33090f92020-12-17 20:12:46 +0100271 assert_int_equal(LY_EVALID, get_argument(YCTX, Y_IDENTIF_ARG, NULL, &word, &buf, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200272 CHECK_LOG_CTX("Statement argument is required.", "Line number 1.");
273
Michal Vasko63f3d842020-07-08 10:10:14 +0200274 in.current = "\"\";"; /* empty reference identifier is not allowed */
Radek Krejci33090f92020-12-17 20:12:46 +0100275 assert_int_equal(LY_EVALID, get_argument(YCTX, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200276 CHECK_LOG_CTX("Statement argument is required.", "Line number 1.");
Radek Krejci4e199f52019-05-28 09:09:28 +0200277
Radek Iša56ca9e42020-09-08 18:42:00 +0200278 /* slash is not an invalid character */
Radek Krejcidd713ce2021-01-04 23:12:12 +0100279 TEST_GET_ARGUMENT_SUCCESS("hello/x\t", YCTX, Y_STR_ARG, "hello/x\t", 7, "\t", 1);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200280 assert_null(buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200281
282 /* different quoting */
Radek Krejcidd713ce2021-01-04 23:12:12 +0100283 TEST_GET_ARGUMENT_SUCCESS("hello/x\t", YCTX, Y_STR_ARG, "hello/x\t", 7, "\t", 1);
Radek Krejciefd22f62018-09-27 11:47:58 +0200284
Radek Krejcidd713ce2021-01-04 23:12:12 +0100285 TEST_GET_ARGUMENT_SUCCESS("hello ", YCTX, Y_STR_ARG, "hello ", 5, " ", 1);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200286
Radek Krejcidd713ce2021-01-04 23:12:12 +0100287 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 +0200288
Radek Krejcidd713ce2021-01-04 23:12:12 +0100289 TEST_GET_ARGUMENT_SUCCESS("\"hello\\n\\t\\\"\\\\\";", YCTX, Y_STR_ARG, "hello\n\t\"\\", 9, ";", 1);
fredgand49fe112019-10-21 20:51:50 +0800290 free(buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200291
Radek Iša56ca9e42020-09-08 18:42:00 +0200292 YCTX->indent = 14;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200293 /* - space and tabs before newline are stripped out
294 * - space and tabs after newline (indentation) are stripped out
295 */
Radek Krejcidd713ce2021-01-04 23:12:12 +0100296 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 +0200297 free(buf);
Radek Iša56ca9e42020-09-08 18:42:00 +0200298
299/* In contrast to previous, the backslash-escaped tabs are expanded after trimming, so they are preserved */
300 YCTX->indent = 14;
Radek Krejcidd713ce2021-01-04 23:12:12 +0100301 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 +0200302 assert_ptr_equal(word, buf);
Radek Krejciff13cd12019-10-25 15:34:24 +0200303 free(buf);
Radek Iša56ca9e42020-09-08 18:42:00 +0200304
Radek Krejciff13cd12019-10-25 15:34:24 +0200305 /* Do not handle whitespaces after backslash-escaped newline as indentation */
Radek Iša56ca9e42020-09-08 18:42:00 +0200306 YCTX->indent = 14;
Radek Krejcidd713ce2021-01-04 23:12:12 +0100307 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 +0200308 assert_ptr_equal(word, buf);
Radek Krejciff13cd12019-10-25 15:34:24 +0200309 free(buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200310
Radek Iša56ca9e42020-09-08 18:42:00 +0200311 YCTX->indent = 14;
Radek Krejcidd713ce2021-01-04 23:12:12 +0100312 TEST_GET_ARGUMENT_SUCCESS("\"hello\n \tworld!\"", YCTX, Y_STR_ARG, "hello\nworld!", 12, "", 2);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200313 assert_ptr_equal(word, buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200314 free(buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200315
Radek Krejcidd713ce2021-01-04 23:12:12 +0100316 TEST_GET_ARGUMENT_SUCCESS("\'hello\'", YCTX, Y_STR_ARG, "hello'", 5, "", 1);
Radek Krejciefd22f62018-09-27 11:47:58 +0200317
Radek Krejcidd713ce2021-01-04 23:12:12 +0100318 TEST_GET_ARGUMENT_SUCCESS("\"hel\" +\t\n\"lo\"", YCTX, Y_STR_ARG, "hello", 5, "", 2);
Radek Krejciefd22f62018-09-27 11:47:58 +0200319 assert_ptr_equal(word, buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200320 free(buf);
Radek Iša56ca9e42020-09-08 18:42:00 +0200321
Radek Krejcidd713ce2021-01-04 23:12:12 +0100322 in.line = 1;
Michal Vasko63f3d842020-07-08 10:10:14 +0200323 in.current = "\"hel\" +\t\nlo"; /* unquoted the second part */
Radek Krejci33090f92020-12-17 20:12:46 +0100324 assert_int_equal(LY_EVALID, get_argument(YCTX, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcidd713ce2021-01-04 23:12:12 +0100325 CHECK_LOG_CTX("Both string parts divided by '+' must be quoted.", "Line number 2.");
Radek Krejciefd22f62018-09-27 11:47:58 +0200326
Radek Krejcidd713ce2021-01-04 23:12:12 +0100327 TEST_GET_ARGUMENT_SUCCESS("\'he\'\t\n+ \"llo\"", YCTX, Y_STR_ARG, "hello", 5, "", 2);
Radek Krejciefd22f62018-09-27 11:47:58 +0200328 free(buf);
329
Radek Krejcidd713ce2021-01-04 23:12:12 +0100330 TEST_GET_ARGUMENT_SUCCESS(" \t\n\"he\"+\'llo\'", YCTX, Y_STR_ARG, "hello", 5, "", 2);
Radek Krejciefd22f62018-09-27 11:47:58 +0200331 free(buf);
332
Radek Krejci44ceedc2018-10-02 15:54:31 +0200333 /* missing argument */
Radek Krejcidd713ce2021-01-04 23:12:12 +0100334 in.line = 1;
Michal Vasko63f3d842020-07-08 10:10:14 +0200335 in.current = ";";
Radek Krejci33090f92020-12-17 20:12:46 +0100336 assert_int_equal(LY_EVALID, get_argument(YCTX, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcidd713ce2021-01-04 23:12:12 +0100337 CHECK_LOG_CTX("Invalid character sequence \";\", expected an argument.", "Line number 1.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200338}
339
Radek Iša56ca9e42020-09-08 18:42:00 +0200340#define TEST_STMS_SUCCESS(INPUT_TEXT, CTX, ACTION, EXPECT_WORD)\
341 in.current = INPUT_TEXT;\
Radek Krejci33090f92020-12-17 20:12:46 +0100342 assert_int_equal(LY_SUCCESS, get_keyword(CTX, &kw, &word, &len));\
Radek Iša56ca9e42020-09-08 18:42:00 +0200343 assert_int_equal(ACTION, kw);\
344 assert_int_equal(strlen(EXPECT_WORD), len);\
345 assert_true(0 == strncmp(EXPECT_WORD, word, len))
346
Radek Krejcidcc7b322018-10-11 14:24:02 +0200347static void
348test_stmts(void **state)
349{
Michal Vasko63f3d842020-07-08 10:10:14 +0200350 const char *p;
Radek Krejcid6b76452019-09-03 17:03:03 +0200351 enum ly_stmt kw;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200352 char *word;
353 size_t len;
354
Michal Vasko63f3d842020-07-08 10:10:14 +0200355 in.current = "\n// comment\n\tinput\t{";
Radek Krejci33090f92020-12-17 20:12:46 +0100356 assert_int_equal(LY_SUCCESS, get_keyword(YCTX, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200357 assert_int_equal(LY_STMT_INPUT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200358 assert_int_equal(5, len);
359 assert_string_equal("input\t{", word);
Michal Vasko63f3d842020-07-08 10:10:14 +0200360 assert_string_equal("\t{", in.current);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200361
Michal Vasko63f3d842020-07-08 10:10:14 +0200362 in.current = "\t /* comment */\t output\n\t{";
Radek Krejci33090f92020-12-17 20:12:46 +0100363 assert_int_equal(LY_SUCCESS, get_keyword(YCTX, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200364 assert_int_equal(LY_STMT_OUTPUT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200365 assert_int_equal(6, len);
366 assert_string_equal("output\n\t{", word);
Michal Vasko63f3d842020-07-08 10:10:14 +0200367 assert_string_equal("\n\t{", in.current);
Radek Krejci33090f92020-12-17 20:12:46 +0100368 assert_int_equal(LY_SUCCESS, get_keyword(YCTX, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200369 assert_int_equal(LY_STMT_SYNTAX_LEFT_BRACE, kw);
Radek Krejciabdd8062019-06-11 16:44:19 +0200370 assert_int_equal(1, len);
371 assert_string_equal("{", word);
Michal Vasko63f3d842020-07-08 10:10:14 +0200372 assert_string_equal("", in.current);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200373
Michal Vasko63f3d842020-07-08 10:10:14 +0200374 in.current = "/input { "; /* invalid slash */
Radek Krejci33090f92020-12-17 20:12:46 +0100375 assert_int_equal(LY_EVALID, get_keyword(YCTX, &kw, &word, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200376 CHECK_LOG_CTX("Invalid identifier first character '/'.", "Line number 4.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200377
Michal Vasko63f3d842020-07-08 10:10:14 +0200378 in.current = "not-a-statement-nor-extension { "; /* invalid identifier */
Radek Krejci33090f92020-12-17 20:12:46 +0100379 assert_int_equal(LY_EVALID, get_keyword(YCTX, &kw, &word, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200380 CHECK_LOG_CTX("Invalid character sequence \"not-a-statement-nor-extension\", expected a keyword.", "Line number 4.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200381
Michal Vasko63f3d842020-07-08 10:10:14 +0200382 in.current = "path;"; /* missing sep after the keyword */
Radek Krejci33090f92020-12-17 20:12:46 +0100383 assert_int_equal(LY_EVALID, get_keyword(YCTX, &kw, &word, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200384 CHECK_LOG_CTX("Invalid character sequence \"path;\", expected a keyword followed by a separator.", "Line number 4.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200385
Radek Iša56ca9e42020-09-08 18:42:00 +0200386 TEST_STMS_SUCCESS("action ", YCTX, LY_STMT_ACTION, "action");
387
388 TEST_STMS_SUCCESS("anydata ", YCTX, LY_STMT_ANYDATA, "anydata");
389 TEST_STMS_SUCCESS("anyxml ", YCTX, LY_STMT_ANYXML, "anyxml");
390 TEST_STMS_SUCCESS("argument ", YCTX, LY_STMT_ARGUMENT, "argument");
391 TEST_STMS_SUCCESS("augment ", YCTX, LY_STMT_AUGMENT, "augment");
392 TEST_STMS_SUCCESS("base ", YCTX, LY_STMT_BASE, "base");
393 TEST_STMS_SUCCESS("belongs-to ", YCTX, LY_STMT_BELONGS_TO, "belongs-to");
394 TEST_STMS_SUCCESS("bit ", YCTX, LY_STMT_BIT, "bit");
395 TEST_STMS_SUCCESS("case ", YCTX, LY_STMT_CASE, "case");
396 TEST_STMS_SUCCESS("choice ", YCTX, LY_STMT_CHOICE, "choice");
397 TEST_STMS_SUCCESS("config ", YCTX, LY_STMT_CONFIG, "config");
398 TEST_STMS_SUCCESS("contact ", YCTX, LY_STMT_CONTACT, "contact");
399 TEST_STMS_SUCCESS("container ", YCTX, LY_STMT_CONTAINER, "container");
400 TEST_STMS_SUCCESS("default ", YCTX, LY_STMT_DEFAULT, "default");
401 TEST_STMS_SUCCESS("description ", YCTX, LY_STMT_DESCRIPTION, "description");
402 TEST_STMS_SUCCESS("deviate ", YCTX, LY_STMT_DEVIATE, "deviate");
403 TEST_STMS_SUCCESS("deviation ", YCTX, LY_STMT_DEVIATION, "deviation");
404 TEST_STMS_SUCCESS("enum ", YCTX, LY_STMT_ENUM, "enum");
405 TEST_STMS_SUCCESS("error-app-tag ", YCTX, LY_STMT_ERROR_APP_TAG, "error-app-tag");
406 TEST_STMS_SUCCESS("error-message ", YCTX, LY_STMT_ERROR_MESSAGE, "error-message");
407 TEST_STMS_SUCCESS("extension ", YCTX, LY_STMT_EXTENSION, "extension");
408 TEST_STMS_SUCCESS("feature ", YCTX, LY_STMT_FEATURE, "feature");
409 TEST_STMS_SUCCESS("fraction-digits ", YCTX, LY_STMT_FRACTION_DIGITS, "fraction-digits");
410 TEST_STMS_SUCCESS("grouping ", YCTX, LY_STMT_GROUPING, "grouping");
411 TEST_STMS_SUCCESS("identity ", YCTX, LY_STMT_IDENTITY, "identity");
412 TEST_STMS_SUCCESS("if-feature ", YCTX, LY_STMT_IF_FEATURE, "if-feature");
413 TEST_STMS_SUCCESS("import ", YCTX, LY_STMT_IMPORT, "import");
414 TEST_STMS_SUCCESS("include ", YCTX, LY_STMT_INCLUDE, "include");
415 TEST_STMS_SUCCESS("input{", YCTX, LY_STMT_INPUT, "input");
416 TEST_STMS_SUCCESS("key ", YCTX, LY_STMT_KEY, "key");
417 TEST_STMS_SUCCESS("leaf ", YCTX, LY_STMT_LEAF, "leaf");
418 TEST_STMS_SUCCESS("leaf-list ", YCTX, LY_STMT_LEAF_LIST, "leaf-list");
419 TEST_STMS_SUCCESS("length ", YCTX, LY_STMT_LENGTH, "length");
420 TEST_STMS_SUCCESS("list ", YCTX, LY_STMT_LIST, "list");
421 TEST_STMS_SUCCESS("mandatory ", YCTX, LY_STMT_MANDATORY, "mandatory");
422 TEST_STMS_SUCCESS("max-elements ", YCTX, LY_STMT_MAX_ELEMENTS, "max-elements");
423 TEST_STMS_SUCCESS("min-elements ", YCTX, LY_STMT_MIN_ELEMENTS, "min-elements");
424 TEST_STMS_SUCCESS("modifier ", YCTX, LY_STMT_MODIFIER, "modifier");
425 TEST_STMS_SUCCESS("module ", YCTX, LY_STMT_MODULE, "module");
426 TEST_STMS_SUCCESS("must ", YCTX, LY_STMT_MUST, "must");
427 TEST_STMS_SUCCESS("namespace ", YCTX, LY_STMT_NAMESPACE, "namespace");
428 TEST_STMS_SUCCESS("notification ", YCTX, LY_STMT_NOTIFICATION, "notification");
429 TEST_STMS_SUCCESS("ordered-by ", YCTX, LY_STMT_ORDERED_BY, "ordered-by");
430 TEST_STMS_SUCCESS("organization ", YCTX, LY_STMT_ORGANIZATION, "organization");
431 TEST_STMS_SUCCESS("output ", YCTX, LY_STMT_OUTPUT, "output");
432 TEST_STMS_SUCCESS("path ", YCTX, LY_STMT_PATH, "path");
433 TEST_STMS_SUCCESS("pattern ", YCTX, LY_STMT_PATTERN, "pattern");
434 TEST_STMS_SUCCESS("position ", YCTX, LY_STMT_POSITION, "position");
435 TEST_STMS_SUCCESS("prefix ", YCTX, LY_STMT_PREFIX, "prefix");
436 TEST_STMS_SUCCESS("presence ", YCTX, LY_STMT_PRESENCE, "presence");
437 TEST_STMS_SUCCESS("range ", YCTX, LY_STMT_RANGE, "range");
438 TEST_STMS_SUCCESS("reference ", YCTX, LY_STMT_REFERENCE, "reference");
439 TEST_STMS_SUCCESS("refine ", YCTX, LY_STMT_REFINE, "refine");
440 TEST_STMS_SUCCESS("require-instance ", YCTX, LY_STMT_REQUIRE_INSTANCE, "require-instance");
441 TEST_STMS_SUCCESS("revision ", YCTX, LY_STMT_REVISION, "revision");
442 TEST_STMS_SUCCESS("revision-date ", YCTX, LY_STMT_REVISION_DATE, "revision-date");
443 TEST_STMS_SUCCESS("rpc ", YCTX, LY_STMT_RPC, "rpc");
444 TEST_STMS_SUCCESS("status ", YCTX, LY_STMT_STATUS, "status");
445 TEST_STMS_SUCCESS("submodule ", YCTX, LY_STMT_SUBMODULE, "submodule");
446 TEST_STMS_SUCCESS("type ", YCTX, LY_STMT_TYPE, "type");
447 TEST_STMS_SUCCESS("typedef ", YCTX, LY_STMT_TYPEDEF, "typedef");
448 TEST_STMS_SUCCESS("unique ", YCTX, LY_STMT_UNIQUE, "unique");
449 TEST_STMS_SUCCESS("units ", YCTX, LY_STMT_UNITS, "units");
450 TEST_STMS_SUCCESS("uses ", YCTX, LY_STMT_USES, "uses");
451 TEST_STMS_SUCCESS("value ", YCTX, LY_STMT_VALUE, "value");
452 TEST_STMS_SUCCESS("when ", YCTX, LY_STMT_WHEN, "when");
453 TEST_STMS_SUCCESS("yang-version ", YCTX, LY_STMT_YANG_VERSION, "yang-version");
454 TEST_STMS_SUCCESS("yin-element ", YCTX, LY_STMT_YIN_ELEMENT, "yin-element");
455 TEST_STMS_SUCCESS(";config false;", YCTX, LY_STMT_SYNTAX_SEMICOLON, ";");
Michal Vasko63f3d842020-07-08 10:10:14 +0200456 assert_string_equal("config false;", in.current);
Radek Iša56ca9e42020-09-08 18:42:00 +0200457 TEST_STMS_SUCCESS("{ config false;", YCTX, LY_STMT_SYNTAX_LEFT_BRACE, "{");
Michal Vasko63f3d842020-07-08 10:10:14 +0200458 assert_string_equal(" config false;", in.current);
Radek Iša56ca9e42020-09-08 18:42:00 +0200459 TEST_STMS_SUCCESS("}", YCTX, LY_STMT_SYNTAX_RIGHT_BRACE, "}");
Michal Vasko63f3d842020-07-08 10:10:14 +0200460 assert_string_equal("", in.current);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200461
462 /* geenric extension */
Michal Vasko63f3d842020-07-08 10:10:14 +0200463 in.current = p = "nacm:default-deny-write;";
Radek Krejci33090f92020-12-17 20:12:46 +0100464 assert_int_equal(LY_SUCCESS, get_keyword(YCTX, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200465 assert_int_equal(LY_STMT_EXTENSION_INSTANCE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200466 assert_int_equal(23, len);
467 assert_ptr_equal(p, word);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200468}
Radek Krejci44ceedc2018-10-02 15:54:31 +0200469
Radek Krejci05b13982018-11-28 16:22:07 +0100470static void
471test_minmax(void **state)
472{
Radek Krejci05b13982018-11-28 16:22:07 +0100473 uint16_t flags = 0;
474 uint32_t value = 0;
475 struct lysp_ext_instance *ext = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +0100476
Michal Vasko8a67eff2021-12-07 14:04:47 +0100477 PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
Radek Krejci05b13982018-11-28 16:22:07 +0100478
Michal Vasko63f3d842020-07-08 10:10:14 +0200479 in.current = " 1invalid; ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100480 assert_int_equal(LY_EVALID, parse_minelements(YCTX, &value, &flags, &ext));
Radek Iša56ca9e42020-09-08 18:42:00 +0200481 CHECK_LOG_CTX("Invalid value \"1invalid\" of \"min-elements\".", "Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100482
483 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200484 in.current = " -1; ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100485 assert_int_equal(LY_EVALID, parse_minelements(YCTX, &value, &flags, &ext));
Radek Iša56ca9e42020-09-08 18:42:00 +0200486 CHECK_LOG_CTX("Invalid value \"-1\" of \"min-elements\".", "Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100487
Radek Krejcidf6cad12018-11-28 17:10:55 +0100488 /* implementation limit */
489 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200490 in.current = " 4294967296; ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100491 assert_int_equal(LY_EVALID, parse_minelements(YCTX, &value, &flags, &ext));
Radek Iša56ca9e42020-09-08 18:42:00 +0200492 CHECK_LOG_CTX("Value \"4294967296\" is out of \"min-elements\" bounds.", "Line number 1.");
Radek Krejcidf6cad12018-11-28 17:10:55 +0100493
Radek Krejci05b13982018-11-28 16:22:07 +0100494 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200495 in.current = " 1 {config true;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100496 assert_int_equal(LY_EVALID, parse_minelements(YCTX, &value, &flags, &ext));
Radek Iša56ca9e42020-09-08 18:42:00 +0200497 CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"min-elements\".", "Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100498
Michal Vasko63f3d842020-07-08 10:10:14 +0200499 in.current = " 1invalid; ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100500 assert_int_equal(LY_EVALID, parse_maxelements(YCTX, &value, &flags, &ext));
Radek Iša56ca9e42020-09-08 18:42:00 +0200501 CHECK_LOG_CTX("Invalid value \"1invalid\" of \"max-elements\".", "Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100502
503 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200504 in.current = " -1; ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100505 assert_int_equal(LY_EVALID, parse_maxelements(YCTX, &value, &flags, &ext));
Radek Iša56ca9e42020-09-08 18:42:00 +0200506 CHECK_LOG_CTX("Invalid value \"-1\" of \"max-elements\".", "Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100507
Radek Krejcidf6cad12018-11-28 17:10:55 +0100508 /* implementation limit */
509 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200510 in.current = " 4294967296; ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100511 assert_int_equal(LY_EVALID, parse_maxelements(YCTX, &value, &flags, &ext));
Radek Iša56ca9e42020-09-08 18:42:00 +0200512 CHECK_LOG_CTX("Value \"4294967296\" is out of \"max-elements\" bounds.", "Line number 1.");
Radek Krejcidf6cad12018-11-28 17:10:55 +0100513
Radek Krejci05b13982018-11-28 16:22:07 +0100514 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200515 in.current = " 1 {config true;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100516 assert_int_equal(LY_EVALID, parse_maxelements(YCTX, &value, &flags, &ext));
Radek Iša56ca9e42020-09-08 18:42:00 +0200517 CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"max-elements\".", "Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100518}
519
Michal Vasko12ef5362022-09-16 15:13:58 +0200520static void
521test_valid_module(void **state)
522{
523 struct lys_module *mod;
524 char *printed;
525 const char *links_yang =
526 "module links {\n"
527 " yang-version 1.1;\n"
528 " namespace \"urn:module2\";\n"
529 " prefix mod2;\n"
530 "\n"
531 " identity just-another-identity;\n"
532 "\n"
533 " leaf one-leaf {\n"
534 " type string;\n"
535 " }\n"
536 "\n"
537 " list list-for-augment {\n"
538 " key keyleaf;\n"
539 "\n"
540 " leaf keyleaf {\n"
541 " type string;\n"
542 " }\n"
543 "\n"
544 " leaf just-leaf {\n"
545 " type int32;\n"
546 " }\n"
547 " }\n"
548 "\n"
549 " leaf rleaf {\n"
550 " type string;\n"
551 " }\n"
552 "\n"
553 " leaf-list llist {\n"
554 " type string;\n"
555 " min-elements 0;\n"
556 " max-elements 100;\n"
557 " ordered-by user;\n"
558 " }\n"
559 "\n"
560 " grouping rgroup {\n"
561 " leaf rg1 {\n"
562 " type string;\n"
563 " }\n"
564 "\n"
565 " leaf rg2 {\n"
566 " type string;\n"
567 " }\n"
568 " }\n"
569 "}\n";
570 const char *statements_yang =
571 "module statements {\n"
572 " yang-version 1.1;\n"
573 " namespace \"urn:module\";\n"
574 " prefix mod;\n"
575 "\n"
576 " import links {\n"
577 " prefix mod2;\n"
578 " }\n"
579 "\n"
580 " extension ext;\n"
581 "\n"
582 " identity random-identity {\n"
583 " base mod2:just-another-identity;\n"
584 " base another-identity;\n"
585 " }\n"
586 "\n"
587 " identity another-identity {\n"
588 " base mod2:just-another-identity;\n"
589 " }\n"
590 "\n"
591 " typedef percent {\n"
592 " type uint8 {\n"
593 " range \"0 .. 100\";\n"
594 " }\n"
595 " units \"percent\";\n"
596 " }\n"
597 "\n"
598 " list list1 {\n"
599 " key \"a\";\n"
600 " leaf a {\n"
601 " type string;\n"
602 " }\n"
603 " leaf x {\n"
604 " type string;\n"
605 " }\n"
606 " leaf y {\n"
607 " type string;\n"
608 " }\n"
609 " }\n"
610 " container ice-cream-shop {\n"
611 " container employees {\n"
612 " when \"/list1/x\";\n"
613 " list employee {\n"
614 " key \"id\";\n"
615 " unique \"name\";\n"
616 " config true;\n"
617 " min-elements 0 {\n"
618 " mod:ext;\n"
619 " }\n"
620 " max-elements unbounded;\n"
621 " leaf id {\n"
622 " type uint64;\n"
623 " mandatory true;\n"
624 " }\n"
625 " leaf name {\n"
626 " type string;\n"
627 " }\n"
628 " leaf age {\n"
629 " type uint32;\n"
630 " }\n"
631 " }\n"
632 " }\n"
633 " }\n"
634 " container random {\n"
635 " grouping group {\n"
636 " leaf g1 {\n"
637 " type percent;\n"
638 " mandatory false;\n"
639 " }\n"
640 " leaf g2 {\n"
641 " type string;\n"
642 " }\n"
643 " }\n"
644 " choice switch {\n"
645 " case a {\n"
646 " leaf aleaf {\n"
647 " type string;\n"
648 " default \"aaa\";\n"
649 " }\n"
650 " }\n"
651 " case c {\n"
652 " leaf cleaf {\n"
653 " type string;\n"
654 " }\n"
655 " }\n"
656 " }\n"
657 " anyxml xml-data;\n"
658 " anydata any-data;\n"
659 " leaf-list leaflist {\n"
660 " type string;\n"
661 " min-elements 0;\n"
662 " max-elements 20;\n"
663 " }\n"
664 " uses group;\n"
665 " uses mod2:rgroup;\n"
666 " leaf lref {\n"
667 " type leafref {\n"
668 " path \"/mod2:one-leaf\";\n"
669 " }\n"
670 " }\n"
671 " leaf iref {\n"
672 " type identityref {\n"
673 " base mod2:just-another-identity;\n"
674 " }\n"
675 " }\n"
676 " }\n"
677 "\n"
678 " augment \"/random\" {\n"
679 " leaf aug-leaf {\n"
680 " type string;\n"
681 " }\n"
682 " }\n"
683 "\n"
684 " notification notif;\n"
685 "\n"
686 " deviation \"/mod:ice-cream-shop/mod:employees/mod:employee/mod:age\" {\n"
687 " deviate not-supported {\n"
688 " mod:ext;\n"
689 " }\n"
690 " }\n"
691 " deviation \"/mod:list1\" {\n"
692 " deviate add {\n"
693 " mod:ext;\n"
694 " must \"1\";\n"
695 " must \"2\";\n"
696 " unique \"x\";\n"
697 " unique \"y\";\n"
698 " config true;\n"
699 " min-elements 1;\n"
700 " max-elements 2;\n"
701 " }\n"
702 " }\n"
703 " deviation \"/mod:ice-cream-shop/mod:employees/mod:employee\" {\n"
704 " deviate delete {\n"
705 " unique \"name\";\n"
706 " }\n"
707 " }\n"
708 " deviation \"/mod:random/mod:leaflist\" {\n"
709 " deviate replace {\n"
710 " type uint32;\n"
711 " min-elements 10;\n"
712 " max-elements 15;\n"
713 " }\n"
714 " }\n"
715 "}\n";
716
717 UTEST_ADD_MODULE(links_yang, LYS_IN_YANG, NULL, NULL);
718 UTEST_ADD_MODULE(statements_yang, LYS_IN_YANG, NULL, &mod);
719 lys_print_mem(&printed, mod, LYS_OUT_YANG, 0);
720 assert_string_equal(printed, statements_yang);
721 free(printed);
722}
723
Radek Krejci9fcacc12018-10-11 15:59:11 +0200724static struct lysp_module *
Michal Vaskod0625d72022-10-06 15:02:50 +0200725mod_renew(struct lysp_yang_ctx *ctx)
Radek Krejci9fcacc12018-10-11 15:59:11 +0200726{
Michal Vasko8a67eff2021-12-07 14:04:47 +0100727 struct ly_ctx *ly_ctx = PARSER_CUR_PMOD(ctx)->mod->ctx;
728 struct lysp_module *pmod;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100729
Michal Vaskoc636ea42022-09-16 10:20:31 +0200730 lys_module_free(&fctx, PARSER_CUR_PMOD(ctx)->mod, 0);
Michal Vasko8a67eff2021-12-07 14:04:47 +0100731 pmod = calloc(1, sizeof *pmod);
732 ctx->parsed_mods->objs[0] = pmod;
733 pmod->mod = calloc(1, sizeof *pmod->mod);
734 pmod->mod->parsed = pmod;
735 pmod->mod->ctx = ly_ctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100736
Radek Krejci2efc45b2020-12-22 16:25:44 +0100737 ctx->in->line = 1;
Michal Vaskoc636ea42022-09-16 10:20:31 +0200738 fctx.mod = pmod->mod;
Radek Krejci2efc45b2020-12-22 16:25:44 +0100739
Michal Vasko8a67eff2021-12-07 14:04:47 +0100740 return pmod;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100741}
742
743static struct lysp_submodule *
Michal Vaskod0625d72022-10-06 15:02:50 +0200744submod_renew(struct lysp_yang_ctx *ctx)
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100745{
Michal Vasko8a67eff2021-12-07 14:04:47 +0100746 struct ly_ctx *ly_ctx = PARSER_CUR_PMOD(ctx)->mod->ctx;
747 struct lysp_submodule *submod;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200748
Michal Vaskoc636ea42022-09-16 10:20:31 +0200749 lys_module_free(&fctx, PARSER_CUR_PMOD(ctx)->mod, 0);
Michal Vasko8a67eff2021-12-07 14:04:47 +0100750 submod = calloc(1, sizeof *submod);
751 ctx->parsed_mods->objs[0] = submod;
752 submod->mod = calloc(1, sizeof *submod->mod);
753 lydict_insert(ly_ctx, "name", 0, &submod->mod->name);
754 submod->mod->parsed = (struct lysp_module *)submod;
755 submod->mod->ctx = ly_ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200756
Michal Vaskoc636ea42022-09-16 10:20:31 +0200757 fctx.mod = submod->mod;
758
Michal Vasko8a67eff2021-12-07 14:04:47 +0100759 return submod;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200760}
761
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100762static LY_ERR
763test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
764 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
765 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
Radek Krejcid33273d2018-10-25 14:55:52 +0200766{
767 *module_data = user_data;
768 *format = LYS_IN_YANG;
769 *free_module_data = NULL;
770 return LY_SUCCESS;
771}
772
Radek Krejci9fcacc12018-10-11 15:59:11 +0200773static void
774test_module(void **state)
775{
Radek Krejci40544fa2019-01-11 09:38:37 +0100776 struct lysp_module *mod = NULL;
777 struct lysp_submodule *submod = NULL;
778 struct lys_module *m;
Michal Vaskod0625d72022-10-06 15:02:50 +0200779 struct lysp_yang_ctx *ctx_p;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200780
Radek Iša56ca9e42020-09-08 18:42:00 +0200781 mod = mod_renew(YCTX);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200782
783 /* missing mandatory substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +0200784 in.current = " name {}";
Radek Krejci33090f92020-12-17 20:12:46 +0100785 assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100786 assert_string_equal("name", mod->mod->name);
Radek Iša56ca9e42020-09-08 18:42:00 +0200787 CHECK_LOG_CTX("Missing mandatory keyword \"namespace\" as a child of \"module\".", "Line number 1.");
Radek Krejci9fcacc12018-10-11 15:59:11 +0200788
Radek Iša56ca9e42020-09-08 18:42:00 +0200789 mod = mod_renew(YCTX);
Michal Vaskob24145d2022-07-13 18:34:39 +0200790 in.current = " name {namespace urn:name;}";
Radek Krejci33090f92020-12-17 20:12:46 +0100791 assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
Michal Vaskob24145d2022-07-13 18:34:39 +0200792 assert_string_equal("urn:name", mod->mod->ns);
Radek Iša56ca9e42020-09-08 18:42:00 +0200793 CHECK_LOG_CTX("Missing mandatory keyword \"prefix\" as a child of \"module\".", "Line number 1.");
794 mod = mod_renew(YCTX);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200795
Michal Vaskob24145d2022-07-13 18:34:39 +0200796 in.current = " name {namespace urn:name;prefix \"n\";}";
Radek Krejci33090f92020-12-17 20:12:46 +0100797 assert_int_equal(LY_SUCCESS, parse_module(YCTX, mod));
Michal Vaskob24145d2022-07-13 18:34:39 +0200798 assert_string_equal("n", mod->mod->prefix);
Radek Iša56ca9e42020-09-08 18:42:00 +0200799 mod = mod_renew(YCTX);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200800
Radek Krejci027d5802018-11-14 16:57:28 +0100801#define SCHEMA_BEGINNING " name {yang-version 1.1;namespace urn:x;prefix \"x\";"
802#define SCHEMA_BEGINNING2 " name {namespace urn:x;prefix \"x\";"
Radek Krejcia042ea12018-10-13 07:52:15 +0200803#define TEST_NODE(NODETYPE, INPUT, NAME) \
Michal Vasko63f3d842020-07-08 10:10:14 +0200804 in.current = SCHEMA_BEGINNING INPUT; \
Radek Krejci33090f92020-12-17 20:12:46 +0100805 assert_int_equal(LY_SUCCESS, parse_module(YCTX, mod)); \
Radek Krejcia042ea12018-10-13 07:52:15 +0200806 assert_non_null(mod->data); \
807 assert_int_equal(NODETYPE, mod->data->nodetype); \
808 assert_string_equal(NAME, mod->data->name); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200809 mod = mod_renew(YCTX);
Radek Krejcia042ea12018-10-13 07:52:15 +0200810#define TEST_GENERIC(INPUT, TARGET, TEST) \
Michal Vasko63f3d842020-07-08 10:10:14 +0200811 in.current = SCHEMA_BEGINNING INPUT; \
Radek Krejci33090f92020-12-17 20:12:46 +0100812 assert_int_equal(LY_SUCCESS, parse_module(YCTX, mod)); \
Radek Krejcia042ea12018-10-13 07:52:15 +0200813 assert_non_null(TARGET); \
814 TEST; \
Radek Iša56ca9e42020-09-08 18:42:00 +0200815 mod = mod_renew(YCTX);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100816#define TEST_DUP(MEMBER, VALUE1, VALUE2, LINE) \
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200817 TEST_DUP_GENERIC(SCHEMA_BEGINNING, MEMBER, VALUE1, VALUE2, \
Radek Iša56ca9e42020-09-08 18:42:00 +0200818 parse_module, mod, LINE, mod = mod_renew(YCTX))
Radek Krejcia042ea12018-10-13 07:52:15 +0200819
820 /* duplicated namespace, prefix */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100821 TEST_DUP("namespace", "y", "z", "1");
822 TEST_DUP("prefix", "y", "z", "1");
823 TEST_DUP("contact", "a", "b", "1");
824 TEST_DUP("description", "a", "b", "1");
825 TEST_DUP("organization", "a", "b", "1");
826 TEST_DUP("reference", "a", "b", "1");
Radek Krejcia042ea12018-10-13 07:52:15 +0200827
Radek Krejci70853c52018-10-15 14:46:16 +0200828 /* not allowed in module (submodule-specific) */
Michal Vasko63f3d842020-07-08 10:10:14 +0200829 in.current = SCHEMA_BEGINNING "belongs-to master {prefix m;}}";
Radek Krejci33090f92020-12-17 20:12:46 +0100830 assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
Radek Iša56ca9e42020-09-08 18:42:00 +0200831 CHECK_LOG_CTX("Invalid keyword \"belongs-to\" as a child of \"module\".", "Line number 1.");
832 mod = mod_renew(YCTX);
Radek Krejci70853c52018-10-15 14:46:16 +0200833
Radek Krejcia042ea12018-10-13 07:52:15 +0200834 /* anydata */
835 TEST_NODE(LYS_ANYDATA, "anydata test;}", "test");
836 /* anyxml */
837 TEST_NODE(LYS_ANYXML, "anyxml test;}", "test");
838 /* augment */
839 TEST_GENERIC("augment /somepath;}", mod->augments,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100840 assert_string_equal("/somepath", mod->augments[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200841 /* choice */
842 TEST_NODE(LYS_CHOICE, "choice test;}", "test");
843 /* contact 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100844 TEST_GENERIC("contact \"firstname\" + \n\t\" surname\";}", mod->mod->contact,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100845 assert_string_equal("firstname surname", mod->mod->contact));
Radek Krejcia042ea12018-10-13 07:52:15 +0200846 /* container */
847 TEST_NODE(LYS_CONTAINER, "container test;}", "test");
848 /* description 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100849 TEST_GENERIC("description \'some description\';}", mod->mod->dsc,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100850 assert_string_equal("some description", mod->mod->dsc));
Radek Krejcia042ea12018-10-13 07:52:15 +0200851 /* deviation */
852 TEST_GENERIC("deviation /somepath {deviate not-supported;}}", mod->deviations,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100853 assert_string_equal("/somepath", mod->deviations[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200854 /* extension */
855 TEST_GENERIC("extension test;}", mod->extensions,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100856 assert_string_equal("test", mod->extensions[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200857 /* feature */
858 TEST_GENERIC("feature test;}", mod->features,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100859 assert_string_equal("test", mod->features[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200860 /* grouping */
861 TEST_GENERIC("grouping grp;}", mod->groupings,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100862 assert_string_equal("grp", mod->groupings[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200863 /* identity */
864 TEST_GENERIC("identity test;}", mod->identities,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100865 assert_string_equal("test", mod->identities[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200866 /* import */
Michal Vasko8a67eff2021-12-07 14:04:47 +0100867 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 +0200868 TEST_GENERIC("import zzz {prefix z;}}", mod->imports,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100869 assert_string_equal("zzz", mod->imports[0].name));
Radek Krejci70853c52018-10-15 14:46:16 +0200870
Radek Krejcia042ea12018-10-13 07:52:15 +0200871 /* import - prefix collision */
Michal Vasko63f3d842020-07-08 10:10:14 +0200872 in.current = SCHEMA_BEGINNING "import zzz {prefix x;}}";
Radek Krejci33090f92020-12-17 20:12:46 +0100873 assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
Radek Krejci2efc45b2020-12-22 16:25:44 +0100874 CHECK_LOG_CTX("Prefix \"x\" already used as module prefix.", "Line number 1.");
Radek Iša56ca9e42020-09-08 18:42:00 +0200875 mod = mod_renew(YCTX);
876
Michal Vasko63f3d842020-07-08 10:10:14 +0200877 in.current = SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix y;}}";
Radek Krejci33090f92020-12-17 20:12:46 +0100878 assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
Radek Krejci2efc45b2020-12-22 16:25:44 +0100879 CHECK_LOG_CTX("Prefix \"y\" already used to import \"zzz\" module.", "Line number 1.");
Radek Iša56ca9e42020-09-08 18:42:00 +0200880
881 mod = mod_renew(YCTX);
Radek Krejciddace2c2021-01-08 11:30:56 +0100882 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100883
Michal Vaskob24145d2022-07-13 18:34:39 +0200884 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 +0100885 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 +0200886 CHECK_LOG_CTX("Single revision of the module \"zzz\" imported twice.", NULL);
Radek Krejci70853c52018-10-15 14:46:16 +0200887
Radek Krejcia042ea12018-10-13 07:52:15 +0200888 /* include */
Michal Vasko8a67eff2021-12-07 14:04:47 +0100889 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 +0200890 in.current = "module" SCHEMA_BEGINNING "include xxx;}";
Michal Vasko8a67eff2021-12-07 14:04:47 +0100891 assert_int_equal(lys_parse_mem(PARSER_CUR_PMOD(YCTX)->mod->ctx, in.current, LYS_IN_YANG, NULL), LY_EVALID);
Michal Vasko62af3692023-02-09 14:00:09 +0100892 CHECK_LOG_CTX("Parsing module \"name\" failed.", NULL);
893 CHECK_LOG_CTX("Including \"xxx\" submodule into \"name\" failed.", NULL);
894 CHECK_LOG_CTX("Data model \"xxx\" not found in local searchdirs.", NULL);
895 CHECK_LOG_CTX("Parsing submodule failed.", NULL);
896 CHECK_LOG_CTX("Input data contains module in situation when a submodule is expected.", NULL);
Radek Krejcid33273d2018-10-25 14:55:52 +0200897
Michal Vasko8a67eff2021-12-07 14:04:47 +0100898 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 +0200899 in.current = "module" SCHEMA_BEGINNING "include xxx;}";
Michal Vasko8a67eff2021-12-07 14:04:47 +0100900 assert_int_equal(lys_parse_mem(PARSER_CUR_PMOD(YCTX)->mod->ctx, in.current, LYS_IN_YANG, NULL), LY_EVALID);
Michal Vasko62af3692023-02-09 14:00:09 +0100901 CHECK_LOG_CTX("Parsing module \"name\" failed.", NULL);
902 CHECK_LOG_CTX("Including \"xxx\" submodule into \"name\" failed.", NULL);
903 UTEST_LOG_CTX_CLEAN;
Radek Krejcid33273d2018-10-25 14:55:52 +0200904
Michal Vasko8a67eff2021-12-07 14:04:47 +0100905 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 +0200906 TEST_GENERIC("include xxx;}", mod->includes,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100907 assert_string_equal("xxx", mod->includes[0].name));
Radek Krejcid33273d2018-10-25 14:55:52 +0200908
Radek Krejcia042ea12018-10-13 07:52:15 +0200909 /* leaf */
910 TEST_NODE(LYS_LEAF, "leaf test {type string;}}", "test");
911 /* leaf-list */
912 TEST_NODE(LYS_LEAFLIST, "leaf-list test {type string;}}", "test");
913 /* list */
914 TEST_NODE(LYS_LIST, "list test {key a;leaf a {type string;}}}", "test");
915 /* notification */
916 TEST_GENERIC("notification test;}", mod->notifs,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100917 assert_string_equal("test", mod->notifs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200918 /* organization 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100919 TEST_GENERIC("organization \"CESNET a.l.e.\";}", mod->mod->org,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100920 assert_string_equal("CESNET a.l.e.", mod->mod->org));
Radek Krejcia042ea12018-10-13 07:52:15 +0200921 /* reference 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100922 TEST_GENERIC("reference RFC7950;}", mod->mod->ref,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100923 assert_string_equal("RFC7950", mod->mod->ref));
Radek Krejcia042ea12018-10-13 07:52:15 +0200924 /* revision */
925 TEST_GENERIC("revision 2018-10-12;}", mod->revs,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100926 assert_string_equal("2018-10-12", mod->revs[0].date));
Radek Krejcia042ea12018-10-13 07:52:15 +0200927 /* rpc */
928 TEST_GENERIC("rpc test;}", mod->rpcs,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100929 assert_string_equal("test", mod->rpcs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200930 /* typedef */
931 TEST_GENERIC("typedef test{type string;}}", mod->typedefs,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100932 assert_string_equal("test", mod->typedefs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200933 /* uses */
934 TEST_NODE(LYS_USES, "uses test;}", "test");
935 /* yang-version */
Michal Vasko63f3d842020-07-08 10:10:14 +0200936 in.current = SCHEMA_BEGINNING2 "\n\tyang-version 10;}";
Radek Krejci33090f92020-12-17 20:12:46 +0100937 assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
Radek Krejci2efc45b2020-12-22 16:25:44 +0100938 CHECK_LOG_CTX("Invalid value \"10\" of \"yang-version\".", NULL);
Radek Iša56ca9e42020-09-08 18:42:00 +0200939 mod = mod_renew(YCTX);
Radek Krejci96e48da2020-09-04 13:18:06 +0200940 in.current = SCHEMA_BEGINNING2 "yang-version 1;yang-version 1.1;}";
Radek Krejci33090f92020-12-17 20:12:46 +0100941 assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
Radek Krejci2efc45b2020-12-22 16:25:44 +0100942 CHECK_LOG_CTX("Duplicate keyword \"yang-version\".", NULL);
Radek Iša56ca9e42020-09-08 18:42:00 +0200943 mod = mod_renew(YCTX);
Radek Krejci96e48da2020-09-04 13:18:06 +0200944 in.current = SCHEMA_BEGINNING2 "yang-version 1;}";
Radek Krejci33090f92020-12-17 20:12:46 +0100945 assert_int_equal(LY_SUCCESS, parse_module(YCTX, mod));
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200946 assert_int_equal(1, mod->version);
Radek Iša56ca9e42020-09-08 18:42:00 +0200947 mod = mod_renew(YCTX);
Michal Vasko63f3d842020-07-08 10:10:14 +0200948 in.current = SCHEMA_BEGINNING2 "yang-version \"1.1\";}";
Radek Krejci33090f92020-12-17 20:12:46 +0100949 assert_int_equal(LY_SUCCESS, parse_module(YCTX, mod));
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200950 assert_int_equal(2, mod->version);
Radek Iša56ca9e42020-09-08 18:42:00 +0200951 mod = mod_renew(YCTX);
Radek Krejci40544fa2019-01-11 09:38:37 +0100952
Michal Vasko63f3d842020-07-08 10:10:14 +0200953 in.current = "module " SCHEMA_BEGINNING "} module q {namespace urn:q;prefixq;}";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200954 m = calloc(1, sizeof *m);
Michal Vasko8a67eff2021-12-07 14:04:47 +0100955 m->ctx = PARSER_CUR_PMOD(YCTX)->mod->ctx;
aPiecekc3e26142021-06-22 14:25:49 +0200956 assert_int_equal(LY_EVALID, yang_parse_module(&ctx_p, &in, m));
Radek Iša56ca9e42020-09-08 18:42:00 +0200957 CHECK_LOG_CTX("Trailing garbage \"module q {names...\" after module, expected end-of-input.", "Line number 1.");
Michal Vaskod0625d72022-10-06 15:02:50 +0200958 lysp_yang_ctx_free(ctx_p);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200959 lys_module_free(&fctx, m, 0);
Radek Krejci40544fa2019-01-11 09:38:37 +0100960
Michal Vasko63f3d842020-07-08 10:10:14 +0200961 in.current = "prefix " SCHEMA_BEGINNING "}";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200962 m = calloc(1, sizeof *m);
Michal Vasko8a67eff2021-12-07 14:04:47 +0100963 m->ctx = PARSER_CUR_PMOD(YCTX)->mod->ctx;
aPiecekc3e26142021-06-22 14:25:49 +0200964 assert_int_equal(LY_EVALID, yang_parse_module(&ctx_p, &in, m));
Radek Iša56ca9e42020-09-08 18:42:00 +0200965 CHECK_LOG_CTX("Invalid keyword \"prefix\", expected \"module\" or \"submodule\".", "Line number 1.");
Michal Vaskod0625d72022-10-06 15:02:50 +0200966 lysp_yang_ctx_free(ctx_p);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200967 lys_module_free(&fctx, m, 0);
Radek Krejci09306362018-10-15 15:26:01 +0200968
Michal Vasko63f3d842020-07-08 10:10:14 +0200969 in.current = "module " SCHEMA_BEGINNING "leaf enum {type enumeration {enum seven { position 7;}}}}";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200970 m = calloc(1, sizeof *m);
Michal Vasko8a67eff2021-12-07 14:04:47 +0100971 m->ctx = PARSER_CUR_PMOD(YCTX)->mod->ctx;
aPiecekc3e26142021-06-22 14:25:49 +0200972 assert_int_equal(LY_EVALID, yang_parse_module(&ctx_p, &in, m));
Radek Iša56ca9e42020-09-08 18:42:00 +0200973 CHECK_LOG_CTX("Invalid keyword \"position\" as a child of \"enum\".", "Line number 1.");
Michal Vaskod0625d72022-10-06 15:02:50 +0200974 lysp_yang_ctx_free(ctx_p);
Michal Vaskoc636ea42022-09-16 10:20:31 +0200975 lys_module_free(&fctx, m, 0);
David Sedlák9fb515f2019-07-11 10:33:58 +0200976
Radek Krejci156ccaf2018-10-15 15:49:17 +0200977 /* extensions */
978 TEST_GENERIC("prefix:test;}", mod->exts,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100979 assert_string_equal("prefix:test", mod->exts[0].name);
Radek Krejciab430862021-03-02 20:13:40 +0100980 assert_int_equal(LY_STMT_MODULE, mod->exts[0].parent_stmt));
Radek Iša56ca9e42020-09-08 18:42:00 +0200981 mod = mod_renew(YCTX);
Radek Krejci156ccaf2018-10-15 15:49:17 +0200982
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200983 /* invalid substatement */
Michal Vasko63f3d842020-07-08 10:10:14 +0200984 in.current = SCHEMA_BEGINNING "must false;}";
Radek Krejci33090f92020-12-17 20:12:46 +0100985 assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
Radek Krejci2efc45b2020-12-22 16:25:44 +0100986 CHECK_LOG_CTX("Invalid keyword \"must\" as a child of \"module\".", NULL);
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200987
Radek Krejci09306362018-10-15 15:26:01 +0200988 /* submodule */
Radek Iša56ca9e42020-09-08 18:42:00 +0200989 submod = submod_renew(YCTX);
Radek Krejci09306362018-10-15 15:26:01 +0200990
991 /* missing mandatory substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +0200992 in.current = " subname {}";
Radek Krejci33090f92020-12-17 20:12:46 +0100993 assert_int_equal(LY_EVALID, parse_submodule(YCTX, submod));
Radek Krejci2efc45b2020-12-22 16:25:44 +0100994 CHECK_LOG_CTX("Missing mandatory keyword \"belongs-to\" as a child of \"submodule\".", NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100995 assert_string_equal("subname", submod->name);
Radek Iša56ca9e42020-09-08 18:42:00 +0200996
997 submod = submod_renew(YCTX);
Radek Krejci09306362018-10-15 15:26:01 +0200998
Michal Vasko63f3d842020-07-08 10:10:14 +0200999 in.current = " subname {belongs-to name {prefix x;}}";
Radek Krejci33090f92020-12-17 20:12:46 +01001000 assert_int_equal(LY_SUCCESS, parse_submodule(YCTX, submod));
Michal Vaskoc3781c32020-10-06 14:04:08 +02001001 assert_string_equal("name", submod->mod->name);
Radek Iša56ca9e42020-09-08 18:42:00 +02001002 submod = submod_renew(YCTX);
Radek Krejci09306362018-10-15 15:26:01 +02001003
1004#undef SCHEMA_BEGINNING
Radek Krejci313d9902018-11-08 09:42:58 +01001005#define SCHEMA_BEGINNING " subname {belongs-to name {prefix x;}"
Radek Krejci09306362018-10-15 15:26:01 +02001006
1007 /* duplicated namespace, prefix */
Michal Vasko63f3d842020-07-08 10:10:14 +02001008 in.current = " subname {belongs-to name {prefix x;}belongs-to module1;belongs-to module2;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001009 assert_int_equal(LY_EVALID, parse_submodule(YCTX, submod));
Radek Krejci2efc45b2020-12-22 16:25:44 +01001010 CHECK_LOG_CTX("Duplicate keyword \"belongs-to\".", NULL);
Radek Iša56ca9e42020-09-08 18:42:00 +02001011 submod = submod_renew(YCTX);
Radek Krejci09306362018-10-15 15:26:01 +02001012
1013 /* not allowed in submodule (module-specific) */
Michal Vasko63f3d842020-07-08 10:10:14 +02001014 in.current = SCHEMA_BEGINNING "namespace \"urn:z\";}";
Radek Krejci33090f92020-12-17 20:12:46 +01001015 assert_int_equal(LY_EVALID, parse_submodule(YCTX, submod));
Radek Krejci2efc45b2020-12-22 16:25:44 +01001016 CHECK_LOG_CTX("Invalid keyword \"namespace\" as a child of \"submodule\".", NULL);
Radek Iša56ca9e42020-09-08 18:42:00 +02001017 submod = submod_renew(YCTX);
Michal Vasko63f3d842020-07-08 10:10:14 +02001018 in.current = SCHEMA_BEGINNING "prefix m;}}";
Radek Krejci33090f92020-12-17 20:12:46 +01001019 assert_int_equal(LY_EVALID, parse_submodule(YCTX, submod));
Radek Krejci2efc45b2020-12-22 16:25:44 +01001020 CHECK_LOG_CTX("Invalid keyword \"prefix\" as a child of \"submodule\".", NULL);
Radek Iša56ca9e42020-09-08 18:42:00 +02001021 submod = submod_renew(YCTX);
Radek Krejcia042ea12018-10-13 07:52:15 +02001022
Michal Vasko63f3d842020-07-08 10:10:14 +02001023 in.current = "submodule " SCHEMA_BEGINNING "} module q {namespace urn:q;prefixq;}";
Michal Vaskod0625d72022-10-06 15:02:50 +02001024 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 +02001025 CHECK_LOG_CTX("Trailing garbage \"module q {names...\" after submodule, expected end-of-input.", "Line number 1.");
Michal Vaskod0625d72022-10-06 15:02:50 +02001026 lysp_yang_ctx_free(ctx_p);
Radek Krejci40544fa2019-01-11 09:38:37 +01001027
Michal Vasko63f3d842020-07-08 10:10:14 +02001028 in.current = "prefix " SCHEMA_BEGINNING "}";
Michal Vaskod0625d72022-10-06 15:02:50 +02001029 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 +02001030 CHECK_LOG_CTX("Invalid keyword \"prefix\", expected \"module\" or \"submodule\".", "Line number 1.");
Michal Vaskod0625d72022-10-06 15:02:50 +02001031 lysp_yang_ctx_free(ctx_p);
Radek Iša56ca9e42020-09-08 18:42:00 +02001032 submod = submod_renew(YCTX);
Radek Krejci40544fa2019-01-11 09:38:37 +01001033
Radek Krejcia042ea12018-10-13 07:52:15 +02001034#undef TEST_GENERIC
1035#undef TEST_NODE
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001036#undef TEST_DUP
Radek Krejcia042ea12018-10-13 07:52:15 +02001037#undef SCHEMA_BEGINNING
Radek Krejciefd22f62018-09-27 11:47:58 +02001038}
1039
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001040static void
1041test_deviation(void **state)
1042{
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001043 struct lysp_deviation *d = NULL;
Radek Krejci33090f92020-12-17 20:12:46 +01001044
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001045 /* invalid cardinality */
Michal Vasko12ef5362022-09-16 15:13:58 +02001046 TEST_DUP_GENERIC(" test {deviate not-supported;", "description", "a", "b", parse_deviation, &d, "1", );
1047 TEST_DUP_GENERIC(" test {deviate not-supported;", "reference", "a", "b", parse_deviation, &d, "1", );
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001048
1049 /* missing mandatory substatement */
Michal Vasko63f3d842020-07-08 10:10:14 +02001050 in.current = " test {description text;}";
Radek Krejci33090f92020-12-17 20:12:46 +01001051 assert_int_equal(LY_EVALID, parse_deviation(YCTX, &d));
Radek Iša56ca9e42020-09-08 18:42:00 +02001052 CHECK_LOG_CTX("Missing mandatory keyword \"deviate\" as a child of \"deviation\".", "Line number 1.");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001053
1054 /* invalid substatement */
Michal Vasko63f3d842020-07-08 10:10:14 +02001055 in.current = " test {deviate not-supported; status obsolete;}";
Radek Krejci33090f92020-12-17 20:12:46 +01001056 assert_int_equal(LY_EVALID, parse_deviation(YCTX, &d));
Radek Iša56ca9e42020-09-08 18:42:00 +02001057 CHECK_LOG_CTX("Invalid keyword \"status\" as a child of \"deviation\".", "Line number 1.");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001058}
1059
1060static void
1061test_deviate(void **state)
1062{
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001063 struct lysp_deviate *d = NULL;
Radek Krejci33090f92020-12-17 20:12:46 +01001064
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001065 /* invalid cardinality */
Michal Vasko12ef5362022-09-16 15:13:58 +02001066 TEST_DUP_GENERIC("add {", "config", "true", "false", parse_deviate, &d, "1", );
1067 TEST_DUP_GENERIC("add {", "mandatory", "true", "false", parse_deviate, &d, "1", );
1068 TEST_DUP_GENERIC("add {", "max-elements", "1", "2", parse_deviate, &d, "1", );
1069 TEST_DUP_GENERIC("add {", "min-elements", "1", "2", parse_deviate, &d, "1", );
1070 TEST_DUP_GENERIC("add {", "units", "kilometers", "miles", parse_deviate, &d, "1", );
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001071
1072 /* invalid substatements */
1073#define TEST_NOT_SUP(DEV, STMT, VALUE) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001074 in.current = " "DEV" {"STMT" "VALUE";}..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001075 assert_int_equal(LY_EVALID, parse_deviate(YCTX, &d)); \
Michal Vasko12ef5362022-09-16 15:13:58 +02001076 CHECK_LOG_CTX("Deviate \""DEV"\" does not support keyword \""STMT"\".", "Line number 1.");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001077
1078 TEST_NOT_SUP("not-supported", "units", "meters");
1079 TEST_NOT_SUP("not-supported", "must", "1");
1080 TEST_NOT_SUP("not-supported", "unique", "x");
1081 TEST_NOT_SUP("not-supported", "default", "a");
1082 TEST_NOT_SUP("not-supported", "config", "true");
1083 TEST_NOT_SUP("not-supported", "mandatory", "true");
1084 TEST_NOT_SUP("not-supported", "min-elements", "1");
1085 TEST_NOT_SUP("not-supported", "max-elements", "2");
1086 TEST_NOT_SUP("not-supported", "type", "string");
1087 TEST_NOT_SUP("add", "type", "string");
1088 TEST_NOT_SUP("delete", "config", "true");
1089 TEST_NOT_SUP("delete", "mandatory", "true");
1090 TEST_NOT_SUP("delete", "min-elements", "1");
1091 TEST_NOT_SUP("delete", "max-elements", "2");
1092 TEST_NOT_SUP("delete", "type", "string");
1093 TEST_NOT_SUP("replace", "must", "1");
1094 TEST_NOT_SUP("replace", "unique", "a");
1095
Michal Vasko63f3d842020-07-08 10:10:14 +02001096 in.current = " nonsence; ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001097 assert_int_equal(LY_EVALID, parse_deviate(YCTX, &d));
Radek Iša56ca9e42020-09-08 18:42:00 +02001098 CHECK_LOG_CTX("Invalid value \"nonsence\" of \"deviate\".", "Line number 1.");\
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001099 assert_null(d);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001100#undef TEST_NOT_SUP
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001101}
1102
Radek Krejci8c370832018-11-02 15:10:03 +01001103static void
1104test_container(void **state)
1105{
Radek Krejci8c370832018-11-02 15:10:03 +01001106 struct lysp_node_container *c = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001107
Michal Vasko8a67eff2021-12-07 14:04:47 +01001108 PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
Michal Vaskod0625d72022-10-06 15:02:50 +02001109 YCTX->main_ctx = (struct lysp_ctx *)YCTX;
Radek Krejci8c370832018-11-02 15:10:03 +01001110
1111 /* invalid cardinality */
1112#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001113 in.current = "cont {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001114 assert_int_equal(LY_EVALID, parse_container(YCTX, NULL, (struct lysp_node**)&c)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001115 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Michal Vaskoc636ea42022-09-16 10:20:31 +02001116 lysp_node_free(&fctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001117
1118 TEST_DUP("config", "true", "false");
1119 TEST_DUP("description", "text1", "text2");
1120 TEST_DUP("presence", "true", "false");
1121 TEST_DUP("reference", "1", "2");
1122 TEST_DUP("status", "current", "obsolete");
1123 TEST_DUP("when", "true", "false");
1124#undef TEST_DUP
1125
1126 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001127 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 +01001128 "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 +01001129 assert_int_equal(LY_SUCCESS, parse_container(YCTX, NULL, (struct lysp_node **)&c));
Radek Iša56ca9e42020-09-08 18:42:00 +02001130 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 +01001131 assert_non_null(c->actions);
1132 assert_non_null(c->child);
Radek Krejci8c370832018-11-02 15:10:03 +01001133 assert_non_null(c->groupings);
Radek Krejci8c370832018-11-02 15:10:03 +01001134 assert_non_null(c->musts);
1135 assert_non_null(c->notifs);
1136 assert_string_equal("true", c->presence);
Radek Krejci8c370832018-11-02 15:10:03 +01001137 assert_non_null(c->typedefs);
Radek Iša56ca9e42020-09-08 18:42:00 +02001138 ly_set_erase(&YCTX->tpdfs_nodes, NULL);
aPiecek63e080d2021-06-29 13:53:28 +02001139 ly_set_erase(&YCTX->grps_nodes, NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001140 lysp_node_free(&fctx, (struct lysp_node *)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001141
1142 /* invalid */
Michal Vasko63f3d842020-07-08 10:10:14 +02001143 in.current = " cont {augment /root;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001144 assert_int_equal(LY_EVALID, parse_container(YCTX, NULL, (struct lysp_node **)&c));
Radek Iša56ca9e42020-09-08 18:42:00 +02001145 CHECK_LOG_CTX("Invalid keyword \"augment\" as a child of \"container\".", "Line number 1.");
Michal Vaskoc636ea42022-09-16 10:20:31 +02001146 lysp_node_free(&fctx, (struct lysp_node *)c); c = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02001147 in.current = " cont {nonsence true;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001148 assert_int_equal(LY_EVALID, parse_container(YCTX, NULL, (struct lysp_node **)&c));
Radek Iša56ca9e42020-09-08 18:42:00 +02001149 CHECK_LOG_CTX("Invalid character sequence \"nonsence\", expected a keyword.", "Line number 1.");
Michal Vaskoc636ea42022-09-16 10:20:31 +02001150 lysp_node_free(&fctx, (struct lysp_node *)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001151
Michal Vasko8a67eff2021-12-07 14:04:47 +01001152 PARSER_CUR_PMOD(YCTX)->version = 1; /* simulate YANG 1.0 */
Michal Vasko63f3d842020-07-08 10:10:14 +02001153 in.current = " cont {action x;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001154 assert_int_equal(LY_EVALID, parse_container(YCTX, NULL, (struct lysp_node **)&c));
Radek Iša56ca9e42020-09-08 18:42:00 +02001155 CHECK_LOG_CTX("Invalid keyword \"action\" as a child of \"container\" - "
1156 "the statement is allowed only in YANG 1.1 modules.", "Line number 1.");
Michal Vaskoc636ea42022-09-16 10:20:31 +02001157 lysp_node_free(&fctx, (struct lysp_node *)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001158}
1159
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001160static void
1161test_leaf(void **state)
1162{
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001163 struct lysp_node_leaf *l = NULL;
Radek Krejci33090f92020-12-17 20:12:46 +01001164
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001165 /* invalid cardinality */
1166#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001167 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001168 assert_int_equal(LY_EVALID, parse_leaf(YCTX, NULL, (struct lysp_node**)&l)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001169 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Michal Vaskoc636ea42022-09-16 10:20:31 +02001170 lysp_node_free(&fctx, (struct lysp_node*)l); l = NULL;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001171
1172 TEST_DUP("config", "true", "false");
1173 TEST_DUP("default", "x", "y");
1174 TEST_DUP("description", "text1", "text2");
1175 TEST_DUP("mandatory", "true", "false");
1176 TEST_DUP("reference", "1", "2");
1177 TEST_DUP("status", "current", "obsolete");
Radek Krejci0e5d8382018-11-28 16:37:53 +01001178 TEST_DUP("type", "int8", "uint8");
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001179 TEST_DUP("units", "text1", "text2");
1180 TEST_DUP("when", "true", "false");
1181#undef TEST_DUP
1182
1183 /* full content - without mandatory which is mutual exclusive with default */
Michal Vasko63f3d842020-07-08 10:10:14 +02001184 in.current = "l {config false;default \"xxx\";description test;if-feature f;"
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001185 "must 'expr';reference test;status current;type string; units yyy;when true;m:ext;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001186 assert_int_equal(LY_SUCCESS, parse_leaf(YCTX, NULL, (struct lysp_node **)&l));
Radek Iša56ca9e42020-09-08 18:42:00 +02001187 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 +02001188 assert_string_equal("xxx", l->dflt.str);
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001189 assert_string_equal("yyy", l->units);
1190 assert_string_equal("string", l->type.name);
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001191 assert_non_null(l->musts);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001192 lysp_node_free(&fctx, (struct lysp_node *)l); l = NULL;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001193
1194 /* full content - now with mandatory */
Michal Vasko63f3d842020-07-08 10:10:14 +02001195 in.current = "l {mandatory true; type string;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001196 assert_int_equal(LY_SUCCESS, parse_leaf(YCTX, NULL, (struct lysp_node **)&l));
Radek Iša56ca9e42020-09-08 18:42:00 +02001197 CHECK_LYSP_NODE(l, NULL, 0, LYS_MAND_TRUE, 0, "l", 0, LYS_LEAF, 0, NULL, 0);
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001198 assert_string_equal("string", l->type.name);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001199 lysp_node_free(&fctx, (struct lysp_node *)l); l = NULL;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001200
1201 /* invalid */
Michal Vasko63f3d842020-07-08 10:10:14 +02001202 in.current = " l {description \"missing type\";} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001203 assert_int_equal(LY_EVALID, parse_leaf(YCTX, NULL, (struct lysp_node **)&l));
Radek Iša56ca9e42020-09-08 18:42:00 +02001204 CHECK_LOG_CTX("Missing mandatory keyword \"type\" as a child of \"leaf\".", "Line number 1.");
Michal Vaskoc636ea42022-09-16 10:20:31 +02001205 lysp_node_free(&fctx, (struct lysp_node *)l); l = NULL;
aPiecek4bb1e372021-05-07 11:01:00 +02001206
1207 in.current = "l { type iid { path qpud wrong {";
1208 assert_int_equal(LY_EVALID, parse_leaf(YCTX, NULL, (struct lysp_node **)&l));
1209 CHECK_LOG_CTX("Invalid character sequence \"wrong\", expected a keyword.", "Line number 1.");
Michal Vaskoc636ea42022-09-16 10:20:31 +02001210 lysp_node_free(&fctx, (struct lysp_node *)l); l = NULL;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001211}
1212
Radek Krejci0e5d8382018-11-28 16:37:53 +01001213static void
1214test_leaflist(void **state)
1215{
Radek Krejci0e5d8382018-11-28 16:37:53 +01001216 struct lysp_node_leaflist *ll = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001217
Michal Vasko8a67eff2021-12-07 14:04:47 +01001218 PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
Radek Krejci0e5d8382018-11-28 16:37:53 +01001219
1220 /* invalid cardinality */
1221#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001222 in.current = "ll {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001223 assert_int_equal(LY_EVALID, parse_leaflist(YCTX, NULL, (struct lysp_node**)&ll)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001224 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Michal Vaskoc636ea42022-09-16 10:20:31 +02001225 lysp_node_free(&fctx, (struct lysp_node*)ll); ll = NULL;
Radek Krejci0e5d8382018-11-28 16:37:53 +01001226
1227 TEST_DUP("config", "true", "false");
1228 TEST_DUP("description", "text1", "text2");
1229 TEST_DUP("max-elements", "10", "20");
1230 TEST_DUP("min-elements", "10", "20");
1231 TEST_DUP("ordered-by", "user", "system");
1232 TEST_DUP("reference", "1", "2");
1233 TEST_DUP("status", "current", "obsolete");
1234 TEST_DUP("type", "int8", "uint8");
1235 TEST_DUP("units", "text1", "text2");
1236 TEST_DUP("when", "true", "false");
1237#undef TEST_DUP
1238
1239 /* full content - without min-elements which is mutual exclusive with default */
Michal Vasko63f3d842020-07-08 10:10:14 +02001240 in.current = "ll {config false;default \"xxx\"; default \"yyy\";description test;if-feature f;"
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001241 "max-elements 10;must 'expr';ordered-by user;reference test;"
1242 "status current;type string; units zzz;when true;m:ext;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001243 assert_int_equal(LY_SUCCESS, parse_leaflist(YCTX, NULL, (struct lysp_node **)&ll));
Radek Iša56ca9e42020-09-08 18:42:00 +02001244 CHECK_LYSP_NODE(ll, "test", 1, 0x446, 1, "ll", 0, LYS_LEAFLIST, 0, "test", 1);
Radek Krejci0e5d8382018-11-28 16:37:53 +01001245 assert_non_null(ll->dflts);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001246 assert_int_equal(2, LY_ARRAY_COUNT(ll->dflts));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001247 assert_string_equal("xxx", ll->dflts[0].str);
1248 assert_string_equal("yyy", ll->dflts[1].str);
Radek Krejci0e5d8382018-11-28 16:37:53 +01001249 assert_string_equal("zzz", ll->units);
1250 assert_int_equal(10, ll->max);
1251 assert_int_equal(0, ll->min);
1252 assert_string_equal("string", ll->type.name);
Radek Krejci0e5d8382018-11-28 16:37:53 +01001253 assert_non_null(ll->musts);
Radek Krejci0e5d8382018-11-28 16:37:53 +01001254 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_USER | LYS_SET_MAX, ll->flags);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001255 lysp_node_free(&fctx, (struct lysp_node *)ll); ll = NULL;
Radek Krejci0e5d8382018-11-28 16:37:53 +01001256
1257 /* full content - now with min-elements */
Michal Vasko63f3d842020-07-08 10:10:14 +02001258 in.current = "ll {min-elements 10; type string;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001259 assert_int_equal(LY_SUCCESS, parse_leaflist(YCTX, NULL, (struct lysp_node **)&ll));
Radek Iša56ca9e42020-09-08 18:42:00 +02001260 CHECK_LYSP_NODE(ll, NULL, 0, 0x200, 0, "ll", 0, LYS_LEAFLIST, 0, NULL, 0);
Radek Krejci0e5d8382018-11-28 16:37:53 +01001261 assert_string_equal("string", ll->type.name);
1262 assert_int_equal(0, ll->max);
1263 assert_int_equal(10, ll->min);
1264 assert_int_equal(LYS_SET_MIN, ll->flags);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001265 lysp_node_free(&fctx, (struct lysp_node *)ll); ll = NULL;
Radek Krejci0e5d8382018-11-28 16:37:53 +01001266
1267 /* invalid */
Michal Vasko63f3d842020-07-08 10:10:14 +02001268 in.current = " ll {description \"missing type\";} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001269 assert_int_equal(LY_EVALID, parse_leaflist(YCTX, NULL, (struct lysp_node **)&ll));
Radek Iša56ca9e42020-09-08 18:42:00 +02001270 CHECK_LOG_CTX("Missing mandatory keyword \"type\" as a child of \"leaf-list\".", "Line number 1.");
Michal Vaskoc636ea42022-09-16 10:20:31 +02001271 lysp_node_free(&fctx, (struct lysp_node *)ll); ll = NULL;
Radek Krejci0e5d8382018-11-28 16:37:53 +01001272
Michal Vasko8a67eff2021-12-07 14:04:47 +01001273 PARSER_CUR_PMOD(YCTX)->version = 1; /* simulate YANG 1.0 - default statement is not allowed */
Michal Vasko63f3d842020-07-08 10:10:14 +02001274 in.current = " ll {default xx; type string;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001275 assert_int_equal(LY_EVALID, parse_leaflist(YCTX, NULL, (struct lysp_node **)&ll));
Radek Iša56ca9e42020-09-08 18:42:00 +02001276 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 +02001277 lysp_node_free(&fctx, (struct lysp_node *)ll); ll = NULL;
Radek Krejci0e5d8382018-11-28 16:37:53 +01001278}
1279
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001280static void
1281test_list(void **state)
1282{
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001283 struct lysp_node_list *l = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001284
Michal Vasko8a67eff2021-12-07 14:04:47 +01001285 PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
Michal Vaskod0625d72022-10-06 15:02:50 +02001286 YCTX->main_ctx = (struct lysp_ctx *)YCTX;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001287
1288 /* invalid cardinality */
1289#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001290 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001291 assert_int_equal(LY_EVALID, parse_list(YCTX, NULL, (struct lysp_node**)&l)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001292 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Michal Vaskoc636ea42022-09-16 10:20:31 +02001293 lysp_node_free(&fctx, (struct lysp_node*)l); l = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001294
1295 TEST_DUP("config", "true", "false");
1296 TEST_DUP("description", "text1", "text2");
1297 TEST_DUP("key", "one", "two");
1298 TEST_DUP("max-elements", "10", "20");
1299 TEST_DUP("min-elements", "10", "20");
1300 TEST_DUP("ordered-by", "user", "system");
1301 TEST_DUP("reference", "1", "2");
1302 TEST_DUP("status", "current", "obsolete");
1303 TEST_DUP("when", "true", "false");
1304#undef TEST_DUP
1305
1306 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001307 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 +01001308 "leaf-list ll {type string;} list li;max-elements 10; min-elements 1;must 'expr';notification not; ordered-by system; reference test;"
1309 "status current;typedef t {type int8;}unique xxx;unique yyy;uses g;when true;m:ext;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001310 assert_int_equal(LY_SUCCESS, parse_list(YCTX, NULL, (struct lysp_node **)&l));
Radek Iša56ca9e42020-09-08 18:42:00 +02001311 CHECK_LYSP_NODE(l, "test", 1, LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM | LYS_SET_MAX | LYS_SET_MIN, 1, "l",
1312 0, LYS_LIST, 0, "test", 1);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001313 assert_string_equal("l", l->key);
1314 assert_non_null(l->uniques);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001315 assert_int_equal(2, LY_ARRAY_COUNT(l->uniques));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001316 assert_string_equal("xxx", l->uniques[0].str);
1317 assert_string_equal("yyy", l->uniques[1].str);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001318 assert_int_equal(10, l->max);
1319 assert_int_equal(1, l->min);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001320 assert_non_null(l->musts);
Radek Iša56ca9e42020-09-08 18:42:00 +02001321 ly_set_erase(&YCTX->tpdfs_nodes, NULL);
aPiecek63e080d2021-06-29 13:53:28 +02001322 ly_set_erase(&YCTX->grps_nodes, NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001323 lysp_node_free(&fctx, (struct lysp_node *)l); l = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001324
Radek Krejcif538ce52019-03-05 10:46:14 +01001325 /* invalid content */
Michal Vasko8a67eff2021-12-07 14:04:47 +01001326 PARSER_CUR_PMOD(YCTX)->version = 1; /* simulate YANG 1.0 */
Michal Vasko63f3d842020-07-08 10:10:14 +02001327 in.current = "l {action x;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001328 assert_int_equal(LY_EVALID, parse_list(YCTX, NULL, (struct lysp_node **)&l));
Radek Iša56ca9e42020-09-08 18:42:00 +02001329 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 +02001330 lysp_node_free(&fctx, (struct lysp_node *)l); l = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001331}
1332
Radek Krejci056d0a82018-12-06 16:57:25 +01001333static void
1334test_choice(void **state)
1335{
Radek Krejci056d0a82018-12-06 16:57:25 +01001336 struct lysp_node_choice *ch = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001337
Michal Vasko8a67eff2021-12-07 14:04:47 +01001338 PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
Radek Krejci056d0a82018-12-06 16:57:25 +01001339
1340 /* invalid cardinality */
1341#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001342 in.current = "ch {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001343 assert_int_equal(LY_EVALID, parse_choice(YCTX, NULL, (struct lysp_node**)&ch)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001344 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Michal Vaskoc636ea42022-09-16 10:20:31 +02001345 lysp_node_free(&fctx, (struct lysp_node*)ch); ch = NULL;
Radek Krejci056d0a82018-12-06 16:57:25 +01001346
1347 TEST_DUP("config", "true", "false");
1348 TEST_DUP("default", "a", "b");
1349 TEST_DUP("description", "text1", "text2");
1350 TEST_DUP("mandatory", "true", "false");
1351 TEST_DUP("reference", "1", "2");
1352 TEST_DUP("status", "current", "obsolete");
1353 TEST_DUP("when", "true", "false");
1354#undef TEST_DUP
1355
Radek Krejcia9026eb2018-12-12 16:04:47 +01001356 /* full content - without default due to a collision with mandatory */
Michal Vasko63f3d842020-07-08 10:10:14 +02001357 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 +01001358 "leaf-list ll {type string;} list li;mandatory true;reference test;status current;when true;m:ext;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001359 assert_int_equal(LY_SUCCESS, parse_choice(YCTX, NULL, (struct lysp_node **)&ch));
Radek Iša56ca9e42020-09-08 18:42:00 +02001360 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 +02001361 lysp_node_free(&fctx, (struct lysp_node *)ch); ch = NULL;
Radek Krejci056d0a82018-12-06 16:57:25 +01001362
Radek Krejcia9026eb2018-12-12 16:04:47 +01001363 /* full content - the default missing from the previous node */
Michal Vasko63f3d842020-07-08 10:10:14 +02001364 in.current = "ch {default c;case c;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001365 assert_int_equal(LY_SUCCESS, parse_choice(YCTX, NULL, (struct lysp_node **)&ch));
Radek Iša56ca9e42020-09-08 18:42:00 +02001366 CHECK_LYSP_NODE(ch, NULL, 0, 0, 0, "ch", 0, LYS_CHOICE, 0, NULL, 0);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001367 assert_string_equal("c", ch->dflt.str);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001368 lysp_node_free(&fctx, (struct lysp_node *)ch); ch = NULL;
Radek Krejcia9026eb2018-12-12 16:04:47 +01001369}
1370
1371static void
1372test_case(void **state)
1373{
Radek Krejcia9026eb2018-12-12 16:04:47 +01001374 struct lysp_node_case *cs = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001375
Michal Vasko8a67eff2021-12-07 14:04:47 +01001376 PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
Radek Krejcia9026eb2018-12-12 16:04:47 +01001377
1378 /* invalid cardinality */
1379#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001380 in.current = "cs {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001381 assert_int_equal(LY_EVALID, parse_case(YCTX, NULL, (struct lysp_node**)&cs)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001382 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Michal Vaskoc636ea42022-09-16 10:20:31 +02001383 lysp_node_free(&fctx, (struct lysp_node*)cs); cs = NULL;
Radek Krejcia9026eb2018-12-12 16:04:47 +01001384
1385 TEST_DUP("description", "text1", "text2");
1386 TEST_DUP("reference", "1", "2");
1387 TEST_DUP("status", "current", "obsolete");
1388 TEST_DUP("when", "true", "false");
1389#undef TEST_DUP
1390
1391 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001392 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 +01001393 "leaf-list ll {type string;} list li;reference test;status current;uses grp;when true;m:ext;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001394 assert_int_equal(LY_SUCCESS, parse_case(YCTX, NULL, (struct lysp_node **)&cs));
Radek Iša56ca9e42020-09-08 18:42:00 +02001395 CHECK_LYSP_NODE(cs, "test", 1, LYS_STATUS_CURR, 1, "cs", 0, LYS_CASE, 0, "test", 1);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001396 lysp_node_free(&fctx, (struct lysp_node *)cs); cs = NULL;
Radek Krejcia9026eb2018-12-12 16:04:47 +01001397
1398 /* invalid content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001399 in.current = "cs {config true} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001400 assert_int_equal(LY_EVALID, parse_case(YCTX, NULL, (struct lysp_node **)&cs));
Radek Iša56ca9e42020-09-08 18:42:00 +02001401 CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"case\".", "Line number 1.");
Michal Vaskoc636ea42022-09-16 10:20:31 +02001402 lysp_node_free(&fctx, (struct lysp_node *)cs); cs = NULL;
Radek Krejci056d0a82018-12-06 16:57:25 +01001403}
1404
Radek Krejci9800fb82018-12-13 14:26:23 +01001405static void
Radek Krejcid6b76452019-09-03 17:03:03 +02001406test_any(void **state, enum ly_stmt kw)
Radek Krejci9800fb82018-12-13 14:26:23 +01001407{
Radek Krejci9800fb82018-12-13 14:26:23 +01001408 struct lysp_node_anydata *any = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001409
Radek Krejcid6b76452019-09-03 17:03:03 +02001410 if (kw == LY_STMT_ANYDATA) {
Michal Vasko8a67eff2021-12-07 14:04:47 +01001411 PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
Radek Krejci9800fb82018-12-13 14:26:23 +01001412 } else {
Michal Vasko8a67eff2021-12-07 14:04:47 +01001413 PARSER_CUR_PMOD(YCTX)->version = 1; /* simulate YANG 1.0 */
Radek Krejci9800fb82018-12-13 14:26:23 +01001414 }
1415
1416 /* invalid cardinality */
1417#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001418 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001419 assert_int_equal(LY_EVALID, parse_any(YCTX, kw, NULL, (struct lysp_node**)&any)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001420 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Michal Vaskoc636ea42022-09-16 10:20:31 +02001421 lysp_node_free(&fctx, (struct lysp_node*)any); any = NULL;
Radek Krejci9800fb82018-12-13 14:26:23 +01001422
1423 TEST_DUP("config", "true", "false");
1424 TEST_DUP("description", "text1", "text2");
1425 TEST_DUP("mandatory", "true", "false");
1426 TEST_DUP("reference", "1", "2");
1427 TEST_DUP("status", "current", "obsolete");
1428 TEST_DUP("when", "true", "false");
1429#undef TEST_DUP
1430
1431 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001432 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 +01001433 assert_int_equal(LY_SUCCESS, parse_any(YCTX, kw, NULL, (struct lysp_node **)&any));
Radek Iša56ca9e42020-09-08 18:42:00 +02001434 // CHECK_LYSP_NODE(NODE, DSC, EXTS, FLAGS, IFFEATURES, NAME, NEXT, TYPE, PARENT, REF, WHEN)
1435 uint16_t node_type = kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
Michal Vasko2bf4af42023-01-04 12:08:38 +01001436
Radek Iša56ca9e42020-09-08 18:42:00 +02001437 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 +01001438 assert_non_null(any->musts);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001439 lysp_node_free(&fctx, (struct lysp_node *)any); any = NULL;
Radek Krejci9800fb82018-12-13 14:26:23 +01001440}
1441
1442static void
1443test_anydata(void **state)
1444{
Michal Vaskoa0a498b2021-09-22 12:17:48 +02001445 test_any(state, LY_STMT_ANYDATA);
Radek Krejci9800fb82018-12-13 14:26:23 +01001446}
1447
1448static void
1449test_anyxml(void **state)
1450{
Michal Vaskoa0a498b2021-09-22 12:17:48 +02001451 test_any(state, LY_STMT_ANYXML);
Radek Krejci9800fb82018-12-13 14:26:23 +01001452}
1453
Radek Krejcie86bf772018-12-14 11:39:53 +01001454static void
1455test_grouping(void **state)
1456{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001457 struct lysp_node_grp *grp = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001458
Michal Vasko8a67eff2021-12-07 14:04:47 +01001459 PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
Michal Vaskod0625d72022-10-06 15:02:50 +02001460 YCTX->main_ctx = (struct lysp_ctx *)YCTX;
Radek Krejcie86bf772018-12-14 11:39:53 +01001461
1462 /* invalid cardinality */
1463#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001464 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001465 assert_int_equal(LY_EVALID, parse_grouping(YCTX, NULL, &grp)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001466 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Michal Vaskoc636ea42022-09-16 10:20:31 +02001467 lysp_node_free(&fctx, &grp->node); grp = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001468
1469 TEST_DUP("description", "text1", "text2");
1470 TEST_DUP("reference", "1", "2");
1471 TEST_DUP("status", "current", "obsolete");
1472#undef TEST_DUP
1473
1474 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001475 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 +01001476 "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 +01001477 assert_int_equal(LY_SUCCESS, parse_grouping(YCTX, NULL, &grp));
Radek Krejcie86bf772018-12-14 11:39:53 +01001478 assert_non_null(grp);
1479 assert_int_equal(LYS_GROUPING, grp->nodetype);
1480 assert_string_equal("grp", grp->name);
1481 assert_string_equal("test", grp->dsc);
1482 assert_non_null(grp->exts);
1483 assert_string_equal("test", grp->ref);
1484 assert_null(grp->parent);
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001485 assert_int_equal(LYS_STATUS_CURR, grp->flags);
Radek Iša56ca9e42020-09-08 18:42:00 +02001486 ly_set_erase(&YCTX->tpdfs_nodes, NULL);
aPiecek63e080d2021-06-29 13:53:28 +02001487 ly_set_erase(&YCTX->grps_nodes, NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001488 lysp_node_free(&fctx, &grp->node);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001489 grp = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001490
1491 /* invalid content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001492 in.current = "grp {config true} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001493 assert_int_equal(LY_EVALID, parse_grouping(YCTX, NULL, &grp));
Radek Iša56ca9e42020-09-08 18:42:00 +02001494 CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"grouping\".", "Line number 1.");
Michal Vaskoc636ea42022-09-16 10:20:31 +02001495 lysp_node_free(&fctx, &grp->node);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001496 grp = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001497
Michal Vasko63f3d842020-07-08 10:10:14 +02001498 in.current = "grp {must 'expr'} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001499 assert_int_equal(LY_EVALID, parse_grouping(YCTX, NULL, &grp));
Radek Iša56ca9e42020-09-08 18:42:00 +02001500 CHECK_LOG_CTX("Invalid keyword \"must\" as a child of \"grouping\".", "Line number 1.");
Michal Vaskoc636ea42022-09-16 10:20:31 +02001501 lysp_node_free(&fctx, &grp->node);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001502 grp = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001503}
1504
1505static void
Radek Krejcif538ce52019-03-05 10:46:14 +01001506test_action(void **state)
1507{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001508 struct lysp_node_action *rpcs = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01001509 struct lysp_node_container *c = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001510
Michal Vasko8a67eff2021-12-07 14:04:47 +01001511 PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
Michal Vaskod0625d72022-10-06 15:02:50 +02001512 YCTX->main_ctx = (struct lysp_ctx *)YCTX;
Radek Krejcif538ce52019-03-05 10:46:14 +01001513
1514 /* invalid cardinality */
1515#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001516 in.current = "func {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001517 assert_int_equal(LY_EVALID, parse_action(YCTX, NULL, &rpcs)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001518 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Michal Vaskoc636ea42022-09-16 10:20:31 +02001519 lysp_node_free(&fctx, (struct lysp_node*)rpcs); rpcs = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01001520
1521 TEST_DUP("description", "text1", "text2");
Michal Vaskob83af8a2020-01-06 09:49:22 +01001522 TEST_DUP("input", "{leaf l1 {type empty;}} description a", "{leaf l2 {type empty;}} description a");
1523 TEST_DUP("output", "{leaf l1 {type empty;}} description a", "{leaf l2 {type empty;}} description a");
Radek Krejcif538ce52019-03-05 10:46:14 +01001524 TEST_DUP("reference", "1", "2");
1525 TEST_DUP("status", "current", "obsolete");
1526#undef TEST_DUP
1527
1528 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001529 in.current = "top;";
Radek Krejci33090f92020-12-17 20:12:46 +01001530 assert_int_equal(LY_SUCCESS, parse_container(YCTX, NULL, (struct lysp_node **)&c));
Michal Vasko63f3d842020-07-08 10:10:14 +02001531 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 +01001532 "input {anydata a1; anyxml a2; choice ch; container c; grouping grp; leaf l {type int8;} leaf-list ll {type int8;}"
1533 " list li; must 1; typedef mytypei {type int8;} uses grp; m:ext;}"
1534 "output {anydata a1; anyxml a2; choice ch; container c; grouping grp; leaf l {type int8;} leaf-list ll {type int8;}"
1535 " list li; must 1; typedef mytypeo {type int8;} uses grp; m:ext;}} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001536 assert_int_equal(LY_SUCCESS, parse_action(YCTX, (struct lysp_node *)c, &rpcs));
Radek Krejcif538ce52019-03-05 10:46:14 +01001537 assert_non_null(rpcs);
1538 assert_int_equal(LYS_ACTION, rpcs->nodetype);
1539 assert_string_equal("func", rpcs->name);
1540 assert_string_equal("test", rpcs->dsc);
1541 assert_non_null(rpcs->exts);
1542 assert_non_null(rpcs->iffeatures);
1543 assert_string_equal("test", rpcs->ref);
1544 assert_non_null(rpcs->groupings);
1545 assert_non_null(rpcs->typedefs);
1546 assert_int_equal(LYS_STATUS_CURR, rpcs->flags);
1547 /* input */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001548 assert_int_equal(rpcs->input.nodetype, LYS_INPUT);
Radek Krejcif538ce52019-03-05 10:46:14 +01001549 assert_non_null(rpcs->input.groupings);
1550 assert_non_null(rpcs->input.exts);
1551 assert_non_null(rpcs->input.musts);
1552 assert_non_null(rpcs->input.typedefs);
Radek Krejci01180ac2021-01-27 08:48:22 +01001553 assert_non_null(rpcs->input.child);
Radek Krejcif538ce52019-03-05 10:46:14 +01001554 /* output */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001555 assert_int_equal(rpcs->output.nodetype, LYS_OUTPUT);
Radek Krejcif538ce52019-03-05 10:46:14 +01001556 assert_non_null(rpcs->output.groupings);
1557 assert_non_null(rpcs->output.exts);
1558 assert_non_null(rpcs->output.musts);
1559 assert_non_null(rpcs->output.typedefs);
Radek Krejci01180ac2021-01-27 08:48:22 +01001560 assert_non_null(rpcs->output.child);
Radek Krejcif538ce52019-03-05 10:46:14 +01001561
Radek Iša56ca9e42020-09-08 18:42:00 +02001562 ly_set_erase(&YCTX->tpdfs_nodes, NULL);
aPiecek63e080d2021-06-29 13:53:28 +02001563 ly_set_erase(&YCTX->grps_nodes, NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001564 lysp_node_free(&fctx, (struct lysp_node *)rpcs); rpcs = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01001565
1566 /* invalid content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001567 in.current = "func {config true} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001568 assert_int_equal(LY_EVALID, parse_action(YCTX, NULL, &rpcs));
Radek Iša56ca9e42020-09-08 18:42:00 +02001569 CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"rpc\".", "Line number 1.");
Michal Vaskoc636ea42022-09-16 10:20:31 +02001570 lysp_node_free(&fctx, (struct lysp_node *)rpcs); rpcs = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01001571
Michal Vaskoc636ea42022-09-16 10:20:31 +02001572 lysp_node_free(&fctx, (struct lysp_node *)c);
Radek Krejcif538ce52019-03-05 10:46:14 +01001573}
1574
1575static void
Radek Krejcifc11bd72019-04-11 16:00:05 +02001576test_notification(void **state)
1577{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001578 struct lysp_node_notif *notifs = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001579 struct lysp_node_container *c = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001580
Michal Vasko8a67eff2021-12-07 14:04:47 +01001581 PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
Michal Vaskod0625d72022-10-06 15:02:50 +02001582 YCTX->main_ctx = (struct lysp_ctx *)YCTX;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001583
1584 /* invalid cardinality */
1585#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001586 in.current = "func {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001587 assert_int_equal(LY_EVALID, parse_notif(YCTX, NULL, &notifs)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001588 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Michal Vaskoc636ea42022-09-16 10:20:31 +02001589 lysp_node_free(&fctx, (struct lysp_node*)notifs); notifs = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001590
1591 TEST_DUP("description", "text1", "text2");
1592 TEST_DUP("reference", "1", "2");
1593 TEST_DUP("status", "current", "obsolete");
1594#undef TEST_DUP
1595
1596 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001597 in.current = "top;";
Radek Krejci33090f92020-12-17 20:12:46 +01001598 assert_int_equal(LY_SUCCESS, parse_container(YCTX, NULL, (struct lysp_node **)&c));
Michal Vasko63f3d842020-07-08 10:10:14 +02001599 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 +01001600 "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 +01001601 assert_int_equal(LY_SUCCESS, parse_notif(YCTX, (struct lysp_node *)c, &notifs));
Radek Krejcifc11bd72019-04-11 16:00:05 +02001602 assert_non_null(notifs);
1603 assert_int_equal(LYS_NOTIF, notifs->nodetype);
1604 assert_string_equal("ntf", notifs->name);
1605 assert_string_equal("test", notifs->dsc);
1606 assert_non_null(notifs->exts);
1607 assert_non_null(notifs->iffeatures);
1608 assert_string_equal("test", notifs->ref);
1609 assert_non_null(notifs->groupings);
1610 assert_non_null(notifs->typedefs);
1611 assert_non_null(notifs->musts);
Radek Krejci01180ac2021-01-27 08:48:22 +01001612 assert_non_null(notifs->child);
Radek Krejcifc11bd72019-04-11 16:00:05 +02001613 assert_int_equal(LYS_STATUS_CURR, notifs->flags);
1614
Radek Iša56ca9e42020-09-08 18:42:00 +02001615 ly_set_erase(&YCTX->tpdfs_nodes, NULL);
aPiecek63e080d2021-06-29 13:53:28 +02001616 ly_set_erase(&YCTX->grps_nodes, NULL);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001617 lysp_node_free(&fctx, (struct lysp_node *)notifs); notifs = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001618
1619 /* invalid content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001620 in.current = "ntf {config true} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001621 assert_int_equal(LY_EVALID, parse_notif(YCTX, NULL, &notifs));
Radek Iša56ca9e42020-09-08 18:42:00 +02001622 CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"notification\".", "Line number 1.");
Michal Vaskoc636ea42022-09-16 10:20:31 +02001623 lysp_node_free(&fctx, (struct lysp_node *)notifs); notifs = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001624
Michal Vaskoc636ea42022-09-16 10:20:31 +02001625 lysp_node_free(&fctx, (struct lysp_node *)c);
Radek Krejcifc11bd72019-04-11 16:00:05 +02001626}
1627
1628static void
Radek Krejcie86bf772018-12-14 11:39:53 +01001629test_uses(void **state)
1630{
Radek Krejcie86bf772018-12-14 11:39:53 +01001631 struct lysp_node_uses *u = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001632
Michal Vasko8a67eff2021-12-07 14:04:47 +01001633 PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
Radek Krejcie86bf772018-12-14 11:39:53 +01001634
1635 /* invalid cardinality */
1636#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001637 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001638 assert_int_equal(LY_EVALID, parse_uses(YCTX, NULL, (struct lysp_node**)&u)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001639 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Michal Vaskoc636ea42022-09-16 10:20:31 +02001640 lysp_node_free(&fctx, (struct lysp_node*)u); u = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001641
1642 TEST_DUP("description", "text1", "text2");
1643 TEST_DUP("reference", "1", "2");
1644 TEST_DUP("status", "current", "obsolete");
1645 TEST_DUP("when", "true", "false");
1646#undef TEST_DUP
1647
1648 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001649 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 +01001650 assert_int_equal(LY_SUCCESS, parse_uses(YCTX, NULL, (struct lysp_node **)&u));
Radek Iša56ca9e42020-09-08 18:42:00 +02001651 CHECK_LYSP_NODE(u, "test", 1, LYS_STATUS_CURR, 1, "grpref", 0, LYS_USES, 0, "test", 1);
Radek Krejcie86bf772018-12-14 11:39:53 +01001652 assert_non_null(u->augments);
1653 assert_non_null(u->refines);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001654 lysp_node_free(&fctx, (struct lysp_node *)u); u = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001655}
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001656
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001657static void
1658test_augment(void **state)
1659{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001660 struct lysp_node_augment *a = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001661
Michal Vasko8a67eff2021-12-07 14:04:47 +01001662 PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001663
1664 /* invalid cardinality */
1665#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001666 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001667 assert_int_equal(LY_EVALID, parse_augment(YCTX, NULL, &a)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001668 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Michal Vaskoc636ea42022-09-16 10:20:31 +02001669 lysp_node_free(&fctx, (struct lysp_node *)a); a = NULL;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001670
1671 TEST_DUP("description", "text1", "text2");
1672 TEST_DUP("reference", "1", "2");
1673 TEST_DUP("status", "current", "obsolete");
1674 TEST_DUP("when", "true", "false");
1675#undef TEST_DUP
1676
1677 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001678 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 +01001679 "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 +01001680 assert_int_equal(LY_SUCCESS, parse_augment(YCTX, NULL, &a));
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001681 assert_non_null(a);
1682 assert_int_equal(LYS_AUGMENT, a->nodetype);
1683 assert_string_equal("/target/nodeid", a->nodeid);
1684 assert_string_equal("test", a->dsc);
1685 assert_non_null(a->exts);
1686 assert_non_null(a->iffeatures);
1687 assert_string_equal("test", a->ref);
1688 assert_non_null(a->when);
1689 assert_null(a->parent);
1690 assert_int_equal(LYS_STATUS_CURR, a->flags);
Michal Vaskoc636ea42022-09-16 10:20:31 +02001691 lysp_node_free(&fctx, (struct lysp_node *)a); a = NULL;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001692}
1693
Radek Krejcif09e4e82019-06-14 15:08:11 +02001694static void
1695test_when(void **state)
1696{
Radek Krejcif09e4e82019-06-14 15:08:11 +02001697 struct lysp_when *w = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001698
Michal Vasko8a67eff2021-12-07 14:04:47 +01001699 PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
Radek Krejcif09e4e82019-06-14 15:08:11 +02001700
Michal Vasko12ef5362022-09-16 15:13:58 +02001701 in.current = "l { description text1;description text2;} ...";
1702 assert_int_equal(LY_EVALID, parse_when(YCTX, &w));
1703 assert_null(w);
1704 CHECK_LOG_CTX("Duplicate keyword \"description\".", "Line number 1.");
Radek Krejcif09e4e82019-06-14 15:08:11 +02001705
Michal Vasko12ef5362022-09-16 15:13:58 +02001706 in.current = "l { reference 1;reference 2;} ...";
1707 assert_int_equal(LY_EVALID, parse_when(YCTX, &w));
1708 assert_null(w);
1709 CHECK_LOG_CTX("Duplicate keyword \"reference\".", "Line number 1.");
Radek Krejcif09e4e82019-06-14 15:08:11 +02001710}
1711
David Sedlákd6ce6d72019-07-16 17:30:18 +02001712static void
1713test_value(void **state)
1714{
Michal Vasko193dacd2022-10-13 08:43:05 +02001715 struct lysp_type_enum enm;
David Sedlákd6ce6d72019-07-16 17:30:18 +02001716
Michal Vasko63f3d842020-07-08 10:10:14 +02001717 in.current = "-0;";
Michal Vasko193dacd2022-10-13 08:43:05 +02001718 memset(&enm, 0, sizeof enm);
1719 assert_int_equal(parse_type_enum_value_pos(YCTX, LY_STMT_VALUE, &enm), LY_SUCCESS);
David Sedlákd6ce6d72019-07-16 17:30:18 +02001720
Michal Vasko63f3d842020-07-08 10:10:14 +02001721 in.current = "-0;";
Michal Vasko193dacd2022-10-13 08:43:05 +02001722 memset(&enm, 0, sizeof enm);
1723 assert_int_equal(parse_type_enum_value_pos(YCTX, LY_STMT_POSITION, &enm), LY_EVALID);
Radek Iša56ca9e42020-09-08 18:42:00 +02001724 CHECK_LOG_CTX("Invalid value \"-0\" of \"position\".", "Line number 1.");
David Sedlákd6ce6d72019-07-16 17:30:18 +02001725}
1726
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001727int
1728main(void)
Radek Krejci80dd33e2018-09-26 15:57:18 +02001729{
1730 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +02001731 UTEST(test_helpers, setup, teardown),
1732 UTEST(test_comments, setup, teardown),
1733 UTEST(test_arg, setup, teardown),
1734 UTEST(test_stmts, setup, teardown),
1735 UTEST(test_minmax, setup, teardown),
Michal Vasko12ef5362022-09-16 15:13:58 +02001736 UTEST(test_valid_module, setup, teardown),
Radek Iša56ca9e42020-09-08 18:42:00 +02001737 UTEST(test_module, setup, teardown),
1738 UTEST(test_deviation, setup, teardown),
1739 UTEST(test_deviate, setup, teardown),
1740 UTEST(test_container, setup, teardown),
1741 UTEST(test_leaf, setup, teardown),
1742 UTEST(test_leaflist, setup, teardown),
1743 UTEST(test_list, setup, teardown),
1744 UTEST(test_choice, setup, teardown),
1745 UTEST(test_case, setup, teardown),
1746 UTEST(test_anydata, setup, teardown),
1747 UTEST(test_anyxml, setup, teardown),
1748 UTEST(test_action, setup, teardown),
1749 UTEST(test_notification, setup, teardown),
1750 UTEST(test_grouping, setup, teardown),
1751 UTEST(test_uses, setup, teardown),
1752 UTEST(test_augment, setup, teardown),
1753 UTEST(test_when, setup, teardown),
1754 UTEST(test_value, setup, teardown),
Radek Krejci80dd33e2018-09-26 15:57:18 +02001755 };
1756
1757 return cmocka_run_group_tests(tests, NULL, NULL);
1758}