blob: 34c6d36c898519ca62ea49a1b08d83ce8f7dabac [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"
27#include "tree_schema_internal.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);
Radek Krejci33090f92020-12-17 20:12:46 +010031LY_ERR buf_store_char(struct lys_yang_parser_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);
Radek Krejci33090f92020-12-17 20:12:46 +010033LY_ERR get_keyword(struct lys_yang_parser_ctx *ctx, enum ly_stmt *kw, char **word_p, size_t *word_len);
34LY_ERR get_argument(struct lys_yang_parser_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);
Radek Krejci33090f92020-12-17 20:12:46 +010036LY_ERR skip_comment(struct lys_yang_parser_ctx *ctx, uint8_t comment);
Radek Krejci2d7a47b2019-05-16 13:34:10 +020037
Radek Krejci2a9fc652021-01-22 17:44:34 +010038LY_ERR parse_action(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_action **actions);
Radek Krejci33090f92020-12-17 20:12:46 +010039LY_ERR parse_any(struct lys_yang_parser_ctx *ctx, enum ly_stmt kw, struct lysp_node *parent, struct lysp_node **siblings);
Radek Krejci2a9fc652021-01-22 17:44:34 +010040LY_ERR parse_augment(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_augment **augments);
Radek Krejci33090f92020-12-17 20:12:46 +010041LY_ERR parse_case(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
42LY_ERR parse_container(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
43LY_ERR parse_deviate(struct lys_yang_parser_ctx *ctx, struct lysp_deviate **deviates);
44LY_ERR parse_deviation(struct lys_yang_parser_ctx *ctx, struct lysp_deviation **deviations);
Radek Krejci2a9fc652021-01-22 17:44:34 +010045LY_ERR parse_grouping(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_grp **groupings);
Radek Krejci33090f92020-12-17 20:12:46 +010046LY_ERR parse_choice(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
47LY_ERR parse_leaf(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
48LY_ERR parse_leaflist(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
49LY_ERR parse_list(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
50LY_ERR parse_maxelements(struct lys_yang_parser_ctx *ctx, uint32_t *max, uint16_t *flags, struct lysp_ext_instance **exts);
51LY_ERR parse_minelements(struct lys_yang_parser_ctx *ctx, uint32_t *min, uint16_t *flags, struct lysp_ext_instance **exts);
52LY_ERR parse_module(struct lys_yang_parser_ctx *ctx, struct lysp_module *mod);
Radek Krejci2a9fc652021-01-22 17:44:34 +010053LY_ERR parse_notif(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_notif **notifs);
Radek Krejci33090f92020-12-17 20:12:46 +010054LY_ERR parse_submodule(struct lys_yang_parser_ctx *ctx, struct lysp_submodule *submod);
55LY_ERR parse_uses(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
56LY_ERR parse_when(struct lys_yang_parser_ctx *ctx, struct lysp_when **when_p);
57LY_ERR parse_type_enum_value_pos(struct lys_yang_parser_ctx *ctx, enum ly_stmt val_kw, int64_t *value, uint16_t *flags, struct lysp_ext_instance **exts);
Radek Krejci80dd33e2018-09-26 15:57:18 +020058
Radek Iša56ca9e42020-09-08 18:42:00 +020059struct lys_yang_parser_ctx *YCTX;
Radek Krejci80dd33e2018-09-26 15:57:18 +020060
Radek Krejci1640e802021-01-08 11:01:30 +010061struct ly_in in = {0};
62
Radek Krejci33090f92020-12-17 20:12:46 +010063#define YCTX_INIT \
Radek Krejcid54412f2020-12-17 20:25:35 +010064 in.line = 1; \
Radek Krejci2efc45b2020-12-22 16:25:44 +010065 YCTX->in = &in; \
66 LOG_LOCINIT(UTEST_LYCTX, NULL, NULL, NULL, &in)
Radek Krejci33090f92020-12-17 20:12:46 +010067
Michal Vasko5d24f6c2020-10-13 13:49:06 +020068static int
Radek Iša56ca9e42020-09-08 18:42:00 +020069setup(void **state)
Michal Vasko5d24f6c2020-10-13 13:49:06 +020070{
Michal Vasko8a67eff2021-12-07 14:04:47 +010071 struct lysp_module *pmod;
72
Radek Iša56ca9e42020-09-08 18:42:00 +020073 UTEST_SETUP;
Michal Vasko5d24f6c2020-10-13 13:49:06 +020074
Radek Iša56ca9e42020-09-08 18:42:00 +020075 /* allocate parser context */
76 YCTX = calloc(1, sizeof(*YCTX));
77 YCTX->format = LYS_IN_YANG;
Michal Vasko8a67eff2021-12-07 14:04:47 +010078 ly_set_new(&YCTX->parsed_mods);
Michal Vasko5d24f6c2020-10-13 13:49:06 +020079
Radek Iša56ca9e42020-09-08 18:42:00 +020080 /* allocate new parsed module */
Michal Vasko8a67eff2021-12-07 14:04:47 +010081 pmod = calloc(1, sizeof *pmod);
82 ly_set_add(YCTX->parsed_mods, pmod, 1, NULL);
Michal Vasko5d24f6c2020-10-13 13:49:06 +020083
Radek Iša56ca9e42020-09-08 18:42:00 +020084 /* allocate new module */
Michal Vasko8a67eff2021-12-07 14:04:47 +010085 pmod->mod = calloc(1, sizeof *pmod->mod);
86 pmod->mod->ctx = UTEST_LYCTX;
87 pmod->mod->parsed = pmod;
Michal Vasko5d24f6c2020-10-13 13:49:06 +020088
Radek Krejci1640e802021-01-08 11:01:30 +010089 /* initilize and use the global easily available and customizable input handler */
90 in.line = 1;
91 YCTX->in = &in;
Radek Krejciddace2c2021-01-08 11:30:56 +010092 LOG_LOCINIT(NULL, NULL, NULL, &in);
Radek Krejci1640e802021-01-08 11:01:30 +010093
Michal Vasko5d24f6c2020-10-13 13:49:06 +020094 return 0;
95}
96
97static int
Radek Iša56ca9e42020-09-08 18:42:00 +020098teardown(void **state)
Michal Vasko5d24f6c2020-10-13 13:49:06 +020099{
Michal Vasko4f9da5e2022-03-14 13:11:26 +0100100 lys_module_free(PARSER_CUR_PMOD(YCTX)->mod, 0);
Radek Krejciddace2c2021-01-08 11:30:56 +0100101 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejci1640e802021-01-08 11:01:30 +0100102
Michal Vasko8a67eff2021-12-07 14:04:47 +0100103 ly_set_free(YCTX->parsed_mods, NULL);
Radek Iša56ca9e42020-09-08 18:42:00 +0200104 free(YCTX);
105 YCTX = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200106
Radek Iša56ca9e42020-09-08 18:42:00 +0200107 UTEST_TEARDOWN;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200108
109 return 0;
110}
111
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200112#define TEST_DUP_GENERIC(PREFIX, MEMBER, VALUE1, VALUE2, FUNC, RESULT, LINE, CLEANUP) \
Michal Vasko63f3d842020-07-08 10:10:14 +0200113 in.current = PREFIX MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +0100114 assert_int_equal(LY_EVALID, FUNC(YCTX, RESULT)); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200115 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number "LINE".");\
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200116 CLEANUP
Radek Krejci44ceedc2018-10-02 15:54:31 +0200117static void
118test_helpers(void **state)
119{
Radek Krejci404251e2018-10-09 12:06:44 +0200120 char *buf, *p;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200121 size_t len, size;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200122 uint8_t prefix = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200123
124 /* storing into buffer */
Michal Vasko63f3d842020-07-08 10:10:14 +0200125 in.current = "abcd";
Radek Krejci44ceedc2018-10-02 15:54:31 +0200126 buf = NULL;
127 size = len = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200128 assert_int_equal(LY_SUCCESS, buf_add_char(NULL, &in, 2, &buf, &size, &len));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200129 assert_int_not_equal(0, size);
130 assert_int_equal(2, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200131 assert_string_equal("cd", in.current);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200132 assert_false(strncmp("ab", buf, 2));
133 free(buf);
Radek Krejci404251e2018-10-09 12:06:44 +0200134 buf = NULL;
135
136 /* invalid first characters */
137 len = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200138 in.current = "2invalid";
Radek Krejci33090f92020-12-17 20:12:46 +0100139 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 +0200140 in.current = ".invalid";
Radek Krejci33090f92020-12-17 20:12:46 +0100141 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 +0200142 in.current = "-invalid";
Radek Krejci33090f92020-12-17 20:12:46 +0100143 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 +0200144 /* invalid following characters */
145 len = 3; /* number of characters read before the str content */
Michal Vasko63f3d842020-07-08 10:10:14 +0200146 in.current = "!";
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 = ":";
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));
Radek Krejci404251e2018-10-09 12:06:44 +0200150 /* valid colon for prefixed identifiers */
151 len = size = 0;
152 p = NULL;
David Sedlák40bb13b2019-07-10 14:34:18 +0200153 prefix = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200154 in.current = "x:id";
Radek Krejci33090f92020-12-17 20:12:46 +0100155 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 +0200156 assert_int_equal(1, len);
157 assert_null(buf);
Michal Vasko63f3d842020-07-08 10:10:14 +0200158 assert_string_equal(":id", in.current);
Radek Krejci404251e2018-10-09 12:06:44 +0200159 assert_int_equal('x', p[len - 1]);
Radek Krejci33090f92020-12-17 20:12:46 +0100160 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 +0200161 assert_int_equal(2, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200162 assert_string_equal("id", in.current);
Radek Krejci404251e2018-10-09 12:06:44 +0200163 assert_int_equal(':', p[len - 1]);
164 free(buf);
David Sedlák40bb13b2019-07-10 14:34:18 +0200165 prefix = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200166
167 /* checking identifiers */
Radek Iša56ca9e42020-09-08 18:42:00 +0200168 assert_int_equal(LY_EVALID, lysp_check_identifierchar((struct lys_parser_ctx *)YCTX, ':', 0, NULL));
169 CHECK_LOG_CTX("Invalid identifier character ':' (0x003a).", "Line number 1.");
170 assert_int_equal(LY_EVALID, lysp_check_identifierchar((struct lys_parser_ctx *)YCTX, '#', 1, NULL));
171 CHECK_LOG_CTX("Invalid identifier first character '#' (0x0023).", "Line number 1.");
Radek Krejci44ceedc2018-10-02 15:54:31 +0200172
Radek Iša56ca9e42020-09-08 18:42:00 +0200173 assert_int_equal(LY_SUCCESS, lysp_check_identifierchar((struct lys_parser_ctx *)YCTX, 'a', 1, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200174 assert_int_equal(0, prefix);
Radek Iša56ca9e42020-09-08 18:42:00 +0200175 assert_int_equal(LY_SUCCESS, lysp_check_identifierchar((struct lys_parser_ctx *)YCTX, ':', 0, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200176 assert_int_equal(1, prefix);
Radek Iša56ca9e42020-09-08 18:42:00 +0200177 assert_int_equal(LY_EVALID, lysp_check_identifierchar((struct lys_parser_ctx *)YCTX, ':', 0, &prefix));
Radek Krejcidcc7b322018-10-11 14:24:02 +0200178 assert_int_equal(1, prefix);
Radek Iša56ca9e42020-09-08 18:42:00 +0200179 assert_int_equal(LY_SUCCESS, lysp_check_identifierchar((struct lys_parser_ctx *)YCTX, 'b', 0, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200180 assert_int_equal(2, prefix);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200181 /* second colon is invalid */
Radek Iša56ca9e42020-09-08 18:42:00 +0200182 assert_int_equal(LY_EVALID, lysp_check_identifierchar((struct lys_parser_ctx *)YCTX, ':', 0, &prefix));
183 CHECK_LOG_CTX("Invalid identifier character ':' (0x003a).", "Line number 1.");
Radek Krejci44ceedc2018-10-02 15:54:31 +0200184}
Radek Krejci80dd33e2018-09-26 15:57:18 +0200185
Radek Krejcidd713ce2021-01-04 23:12:12 +0100186#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 +0200187 {\
Radek Krejcidd713ce2021-01-04 23:12:12 +0100188 const char * text = INPUT_TEXT;\
189 in.line = 1;\
Radek Iša56ca9e42020-09-08 18:42:00 +0200190 in.current = text;\
Radek Krejci33090f92020-12-17 20:12:46 +0100191 assert_int_equal(LY_SUCCESS, get_argument(CTX, Y_MAYBE_STR_ARG, NULL, &word, &buf, &len));\
Radek Iša56ca9e42020-09-08 18:42:00 +0200192 assert_string_equal(word, EXPECT_WORD);\
193 assert_int_equal(len, EXPECT_LEN);\
194 assert_string_equal(EXPECT_CURRENT, in.current);\
Radek Krejcidd713ce2021-01-04 23:12:12 +0100195 assert_int_equal(EXPECT_LINE, in.line);\
Radek Iša56ca9e42020-09-08 18:42:00 +0200196 }
197
Radek Krejci80dd33e2018-09-26 15:57:18 +0200198static void
199test_comments(void **state)
200{
Radek Krejciefd22f62018-09-27 11:47:58 +0200201 char *word, *buf;
202 size_t len;
Radek Krejci80dd33e2018-09-26 15:57:18 +0200203
Radek Krejcidd713ce2021-01-04 23:12:12 +0100204 TEST_GET_ARGUMENT_SUCCESS(" // this is a text of / one * line */ comment\nargument;",
205 YCTX, Y_STR_ARG, "argument;", 8, ";", 2);
Radek Krejciefd22f62018-09-27 11:47:58 +0200206 assert_null(buf);
Radek Krejci80dd33e2018-09-26 15:57:18 +0200207
Radek Krejcidd713ce2021-01-04 23:12:12 +0100208 TEST_GET_ARGUMENT_SUCCESS("/* this is a \n * text // of / block * comment */\"arg\" + \"ume\" \n + \n \"nt\";",
209 YCTX, Y_STR_ARG, "argument", 8, ";", 4);
Radek Krejciefd22f62018-09-27 11:47:58 +0200210 assert_ptr_equal(buf, word);
Radek Krejciefd22f62018-09-27 11:47:58 +0200211 free(word);
Radek Krejci80dd33e2018-09-26 15:57:18 +0200212
Radek Krejcidd713ce2021-01-04 23:12:12 +0100213 in.line = 1;
Michal Vasko63f3d842020-07-08 10:10:14 +0200214 in.current = " this is one line comment on last line";
Radek Krejci33090f92020-12-17 20:12:46 +0100215 assert_int_equal(LY_SUCCESS, skip_comment(YCTX, 1));
Michal Vasko63f3d842020-07-08 10:10:14 +0200216 assert_true(in.current[0] == '\0');
Radek Krejci80dd33e2018-09-26 15:57:18 +0200217
Radek Krejcidd713ce2021-01-04 23:12:12 +0100218 in.line = 1;
Michal Vasko63f3d842020-07-08 10:10:14 +0200219 in.current = " this is a not terminated comment x";
Radek Krejci33090f92020-12-17 20:12:46 +0100220 assert_int_equal(LY_EVALID, skip_comment(YCTX, 2));
Radek Krejcidd713ce2021-01-04 23:12:12 +0100221 CHECK_LOG_CTX("Unexpected end-of-input, non-terminated comment.", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200222 assert_true(in.current[0] == '\0');
Radek Krejci80dd33e2018-09-26 15:57:18 +0200223}
224
Radek Krejciefd22f62018-09-27 11:47:58 +0200225static void
226test_arg(void **state)
227{
Radek Krejciefd22f62018-09-27 11:47:58 +0200228 char *word, *buf;
229 size_t len;
230
231 /* missing argument */
Michal Vasko63f3d842020-07-08 10:10:14 +0200232 in.current = ";";
Radek Krejci33090f92020-12-17 20:12:46 +0100233 assert_int_equal(LY_SUCCESS, get_argument(YCTX, Y_MAYBE_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200234 assert_null(word);
235
Michal Vasko63f3d842020-07-08 10:10:14 +0200236 in.current = "{";
Radek Krejci33090f92020-12-17 20:12:46 +0100237 assert_int_equal(LY_EVALID, get_argument(YCTX, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200238 CHECK_LOG_CTX("Invalid character sequence \"{\", expected an argument.", "Line number 1.");
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200239
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200240 /* invalid escape sequence */
Michal Vasko63f3d842020-07-08 10:10:14 +0200241 in.current = "\"\\s\"";
Radek Krejci33090f92020-12-17 20:12:46 +0100242 assert_int_equal(LY_EVALID, get_argument(YCTX, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200243 CHECK_LOG_CTX("Double-quoted string unknown special character \'\\s\'.", "Line number 1.");
244
Radek Krejcidd713ce2021-01-04 23:12:12 +0100245 TEST_GET_ARGUMENT_SUCCESS("\'\\s\'", YCTX, Y_STR_ARG, "\\s\'", 2, "", 1);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200246
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200247 /* invalid character after the argument */
Michal Vasko63f3d842020-07-08 10:10:14 +0200248 in.current = "hello\"";
Radek Krejci33090f92020-12-17 20:12:46 +0100249 assert_int_equal(LY_EVALID, get_argument(YCTX, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200250 CHECK_LOG_CTX("Invalid character sequence \"\"\", expected unquoted string character, optsep, semicolon or opening brace.", "Line number 1.");
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200251
Radek Iša56ca9e42020-09-08 18:42:00 +0200252 in.current = "hello}";
Radek Krejci33090f92020-12-17 20:12:46 +0100253 assert_int_equal(LY_EVALID, get_argument(YCTX, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200254 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 +0200255 /* invalid identifier-ref-arg-str */
Michal Vasko63f3d842020-07-08 10:10:14 +0200256 in.current = "pre:pre:value";
Radek Krejci33090f92020-12-17 20:12:46 +0100257 assert_int_equal(LY_EVALID, get_argument(YCTX, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200258 CHECK_LOG_CTX("Invalid identifier character ':' (0x003a).", "Line number 1.");
David Sedlák40bb13b2019-07-10 14:34:18 +0200259
Michal Vasko63f3d842020-07-08 10:10:14 +0200260 in.current = "\"\";"; /* empty identifier is not allowed */
Radek Krejci33090f92020-12-17 20:12:46 +0100261 assert_int_equal(LY_EVALID, get_argument(YCTX, Y_IDENTIF_ARG, NULL, &word, &buf, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200262 CHECK_LOG_CTX("Statement argument is required.", "Line number 1.");
263
Michal Vasko63f3d842020-07-08 10:10:14 +0200264 in.current = "\"\";"; /* empty reference identifier is not allowed */
Radek Krejci33090f92020-12-17 20:12:46 +0100265 assert_int_equal(LY_EVALID, get_argument(YCTX, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200266 CHECK_LOG_CTX("Statement argument is required.", "Line number 1.");
Radek Krejci4e199f52019-05-28 09:09:28 +0200267
Radek Iša56ca9e42020-09-08 18:42:00 +0200268 /* slash is not an invalid character */
Radek Krejcidd713ce2021-01-04 23:12:12 +0100269 TEST_GET_ARGUMENT_SUCCESS("hello/x\t", YCTX, Y_STR_ARG, "hello/x\t", 7, "\t", 1);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200270 assert_null(buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200271
272 /* different quoting */
Radek Krejcidd713ce2021-01-04 23:12:12 +0100273 TEST_GET_ARGUMENT_SUCCESS("hello/x\t", YCTX, Y_STR_ARG, "hello/x\t", 7, "\t", 1);
Radek Krejciefd22f62018-09-27 11:47:58 +0200274
Radek Krejcidd713ce2021-01-04 23:12:12 +0100275 TEST_GET_ARGUMENT_SUCCESS("hello ", YCTX, Y_STR_ARG, "hello ", 5, " ", 1);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200276
Radek Krejcidd713ce2021-01-04 23:12:12 +0100277 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 +0200278
Radek Krejcidd713ce2021-01-04 23:12:12 +0100279 TEST_GET_ARGUMENT_SUCCESS("\"hello\\n\\t\\\"\\\\\";", YCTX, Y_STR_ARG, "hello\n\t\"\\", 9, ";", 1);
fredgand49fe112019-10-21 20:51:50 +0800280 free(buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200281
Radek Iša56ca9e42020-09-08 18:42:00 +0200282 YCTX->indent = 14;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200283 /* - space and tabs before newline are stripped out
284 * - space and tabs after newline (indentation) are stripped out
285 */
Radek Krejcidd713ce2021-01-04 23:12:12 +0100286 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 +0200287 free(buf);
Radek Iša56ca9e42020-09-08 18:42:00 +0200288
289/* In contrast to previous, the backslash-escaped tabs are expanded after trimming, so they are preserved */
290 YCTX->indent = 14;
Radek Krejcidd713ce2021-01-04 23:12:12 +0100291 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 +0200292 assert_ptr_equal(word, buf);
Radek Krejciff13cd12019-10-25 15:34:24 +0200293 free(buf);
Radek Iša56ca9e42020-09-08 18:42:00 +0200294
Radek Krejciff13cd12019-10-25 15:34:24 +0200295 /* Do not handle whitespaces after backslash-escaped newline as indentation */
Radek Iša56ca9e42020-09-08 18:42:00 +0200296 YCTX->indent = 14;
Radek Krejcidd713ce2021-01-04 23:12:12 +0100297 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 +0200298 assert_ptr_equal(word, buf);
Radek Krejciff13cd12019-10-25 15:34:24 +0200299 free(buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200300
Radek Iša56ca9e42020-09-08 18:42:00 +0200301 YCTX->indent = 14;
Radek Krejcidd713ce2021-01-04 23:12:12 +0100302 TEST_GET_ARGUMENT_SUCCESS("\"hello\n \tworld!\"", YCTX, Y_STR_ARG, "hello\nworld!", 12, "", 2);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200303 assert_ptr_equal(word, buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200304 free(buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200305
Radek Krejcidd713ce2021-01-04 23:12:12 +0100306 TEST_GET_ARGUMENT_SUCCESS("\'hello\'", YCTX, Y_STR_ARG, "hello'", 5, "", 1);
Radek Krejciefd22f62018-09-27 11:47:58 +0200307
Radek Krejcidd713ce2021-01-04 23:12:12 +0100308 TEST_GET_ARGUMENT_SUCCESS("\"hel\" +\t\n\"lo\"", YCTX, Y_STR_ARG, "hello", 5, "", 2);
Radek Krejciefd22f62018-09-27 11:47:58 +0200309 assert_ptr_equal(word, buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200310 free(buf);
Radek Iša56ca9e42020-09-08 18:42:00 +0200311
Radek Krejcidd713ce2021-01-04 23:12:12 +0100312 in.line = 1;
Michal Vasko63f3d842020-07-08 10:10:14 +0200313 in.current = "\"hel\" +\t\nlo"; /* unquoted the second part */
Radek Krejci33090f92020-12-17 20:12:46 +0100314 assert_int_equal(LY_EVALID, get_argument(YCTX, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcidd713ce2021-01-04 23:12:12 +0100315 CHECK_LOG_CTX("Both string parts divided by '+' must be quoted.", "Line number 2.");
Radek Krejciefd22f62018-09-27 11:47:58 +0200316
Radek Krejcidd713ce2021-01-04 23:12:12 +0100317 TEST_GET_ARGUMENT_SUCCESS("\'he\'\t\n+ \"llo\"", YCTX, Y_STR_ARG, "hello", 5, "", 2);
Radek Krejciefd22f62018-09-27 11:47:58 +0200318 free(buf);
319
Radek Krejcidd713ce2021-01-04 23:12:12 +0100320 TEST_GET_ARGUMENT_SUCCESS(" \t\n\"he\"+\'llo\'", YCTX, Y_STR_ARG, "hello", 5, "", 2);
Radek Krejciefd22f62018-09-27 11:47:58 +0200321 free(buf);
322
Radek Krejci44ceedc2018-10-02 15:54:31 +0200323 /* missing argument */
Radek Krejcidd713ce2021-01-04 23:12:12 +0100324 in.line = 1;
Michal Vasko63f3d842020-07-08 10:10:14 +0200325 in.current = ";";
Radek Krejci33090f92020-12-17 20:12:46 +0100326 assert_int_equal(LY_EVALID, get_argument(YCTX, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcidd713ce2021-01-04 23:12:12 +0100327 CHECK_LOG_CTX("Invalid character sequence \";\", expected an argument.", "Line number 1.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200328}
329
Radek Iša56ca9e42020-09-08 18:42:00 +0200330#define TEST_STMS_SUCCESS(INPUT_TEXT, CTX, ACTION, EXPECT_WORD)\
331 in.current = INPUT_TEXT;\
Radek Krejci33090f92020-12-17 20:12:46 +0100332 assert_int_equal(LY_SUCCESS, get_keyword(CTX, &kw, &word, &len));\
Radek Iša56ca9e42020-09-08 18:42:00 +0200333 assert_int_equal(ACTION, kw);\
334 assert_int_equal(strlen(EXPECT_WORD), len);\
335 assert_true(0 == strncmp(EXPECT_WORD, word, len))
336
Radek Krejcidcc7b322018-10-11 14:24:02 +0200337static void
338test_stmts(void **state)
339{
Michal Vasko63f3d842020-07-08 10:10:14 +0200340 const char *p;
Radek Krejcid6b76452019-09-03 17:03:03 +0200341 enum ly_stmt kw;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200342 char *word;
343 size_t len;
344
Michal Vasko63f3d842020-07-08 10:10:14 +0200345 in.current = "\n// comment\n\tinput\t{";
Radek Krejci33090f92020-12-17 20:12:46 +0100346 assert_int_equal(LY_SUCCESS, get_keyword(YCTX, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200347 assert_int_equal(LY_STMT_INPUT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200348 assert_int_equal(5, len);
349 assert_string_equal("input\t{", word);
Michal Vasko63f3d842020-07-08 10:10:14 +0200350 assert_string_equal("\t{", in.current);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200351
Michal Vasko63f3d842020-07-08 10:10:14 +0200352 in.current = "\t /* comment */\t output\n\t{";
Radek Krejci33090f92020-12-17 20:12:46 +0100353 assert_int_equal(LY_SUCCESS, get_keyword(YCTX, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200354 assert_int_equal(LY_STMT_OUTPUT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200355 assert_int_equal(6, len);
356 assert_string_equal("output\n\t{", word);
Michal Vasko63f3d842020-07-08 10:10:14 +0200357 assert_string_equal("\n\t{", in.current);
Radek Krejci33090f92020-12-17 20:12:46 +0100358 assert_int_equal(LY_SUCCESS, get_keyword(YCTX, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200359 assert_int_equal(LY_STMT_SYNTAX_LEFT_BRACE, kw);
Radek Krejciabdd8062019-06-11 16:44:19 +0200360 assert_int_equal(1, len);
361 assert_string_equal("{", word);
Michal Vasko63f3d842020-07-08 10:10:14 +0200362 assert_string_equal("", in.current);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200363
Michal Vasko63f3d842020-07-08 10:10:14 +0200364 in.current = "/input { "; /* invalid slash */
Radek Krejci33090f92020-12-17 20:12:46 +0100365 assert_int_equal(LY_EVALID, get_keyword(YCTX, &kw, &word, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200366 CHECK_LOG_CTX("Invalid identifier first character '/'.", "Line number 4.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200367
Michal Vasko63f3d842020-07-08 10:10:14 +0200368 in.current = "not-a-statement-nor-extension { "; /* invalid identifier */
Radek Krejci33090f92020-12-17 20:12:46 +0100369 assert_int_equal(LY_EVALID, get_keyword(YCTX, &kw, &word, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200370 CHECK_LOG_CTX("Invalid character sequence \"not-a-statement-nor-extension\", expected a keyword.", "Line number 4.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200371
Michal Vasko63f3d842020-07-08 10:10:14 +0200372 in.current = "path;"; /* missing sep after the keyword */
Radek Krejci33090f92020-12-17 20:12:46 +0100373 assert_int_equal(LY_EVALID, get_keyword(YCTX, &kw, &word, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200374 CHECK_LOG_CTX("Invalid character sequence \"path;\", expected a keyword followed by a separator.", "Line number 4.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200375
Radek Iša56ca9e42020-09-08 18:42:00 +0200376 TEST_STMS_SUCCESS("action ", YCTX, LY_STMT_ACTION, "action");
377
378 TEST_STMS_SUCCESS("anydata ", YCTX, LY_STMT_ANYDATA, "anydata");
379 TEST_STMS_SUCCESS("anyxml ", YCTX, LY_STMT_ANYXML, "anyxml");
380 TEST_STMS_SUCCESS("argument ", YCTX, LY_STMT_ARGUMENT, "argument");
381 TEST_STMS_SUCCESS("augment ", YCTX, LY_STMT_AUGMENT, "augment");
382 TEST_STMS_SUCCESS("base ", YCTX, LY_STMT_BASE, "base");
383 TEST_STMS_SUCCESS("belongs-to ", YCTX, LY_STMT_BELONGS_TO, "belongs-to");
384 TEST_STMS_SUCCESS("bit ", YCTX, LY_STMT_BIT, "bit");
385 TEST_STMS_SUCCESS("case ", YCTX, LY_STMT_CASE, "case");
386 TEST_STMS_SUCCESS("choice ", YCTX, LY_STMT_CHOICE, "choice");
387 TEST_STMS_SUCCESS("config ", YCTX, LY_STMT_CONFIG, "config");
388 TEST_STMS_SUCCESS("contact ", YCTX, LY_STMT_CONTACT, "contact");
389 TEST_STMS_SUCCESS("container ", YCTX, LY_STMT_CONTAINER, "container");
390 TEST_STMS_SUCCESS("default ", YCTX, LY_STMT_DEFAULT, "default");
391 TEST_STMS_SUCCESS("description ", YCTX, LY_STMT_DESCRIPTION, "description");
392 TEST_STMS_SUCCESS("deviate ", YCTX, LY_STMT_DEVIATE, "deviate");
393 TEST_STMS_SUCCESS("deviation ", YCTX, LY_STMT_DEVIATION, "deviation");
394 TEST_STMS_SUCCESS("enum ", YCTX, LY_STMT_ENUM, "enum");
395 TEST_STMS_SUCCESS("error-app-tag ", YCTX, LY_STMT_ERROR_APP_TAG, "error-app-tag");
396 TEST_STMS_SUCCESS("error-message ", YCTX, LY_STMT_ERROR_MESSAGE, "error-message");
397 TEST_STMS_SUCCESS("extension ", YCTX, LY_STMT_EXTENSION, "extension");
398 TEST_STMS_SUCCESS("feature ", YCTX, LY_STMT_FEATURE, "feature");
399 TEST_STMS_SUCCESS("fraction-digits ", YCTX, LY_STMT_FRACTION_DIGITS, "fraction-digits");
400 TEST_STMS_SUCCESS("grouping ", YCTX, LY_STMT_GROUPING, "grouping");
401 TEST_STMS_SUCCESS("identity ", YCTX, LY_STMT_IDENTITY, "identity");
402 TEST_STMS_SUCCESS("if-feature ", YCTX, LY_STMT_IF_FEATURE, "if-feature");
403 TEST_STMS_SUCCESS("import ", YCTX, LY_STMT_IMPORT, "import");
404 TEST_STMS_SUCCESS("include ", YCTX, LY_STMT_INCLUDE, "include");
405 TEST_STMS_SUCCESS("input{", YCTX, LY_STMT_INPUT, "input");
406 TEST_STMS_SUCCESS("key ", YCTX, LY_STMT_KEY, "key");
407 TEST_STMS_SUCCESS("leaf ", YCTX, LY_STMT_LEAF, "leaf");
408 TEST_STMS_SUCCESS("leaf-list ", YCTX, LY_STMT_LEAF_LIST, "leaf-list");
409 TEST_STMS_SUCCESS("length ", YCTX, LY_STMT_LENGTH, "length");
410 TEST_STMS_SUCCESS("list ", YCTX, LY_STMT_LIST, "list");
411 TEST_STMS_SUCCESS("mandatory ", YCTX, LY_STMT_MANDATORY, "mandatory");
412 TEST_STMS_SUCCESS("max-elements ", YCTX, LY_STMT_MAX_ELEMENTS, "max-elements");
413 TEST_STMS_SUCCESS("min-elements ", YCTX, LY_STMT_MIN_ELEMENTS, "min-elements");
414 TEST_STMS_SUCCESS("modifier ", YCTX, LY_STMT_MODIFIER, "modifier");
415 TEST_STMS_SUCCESS("module ", YCTX, LY_STMT_MODULE, "module");
416 TEST_STMS_SUCCESS("must ", YCTX, LY_STMT_MUST, "must");
417 TEST_STMS_SUCCESS("namespace ", YCTX, LY_STMT_NAMESPACE, "namespace");
418 TEST_STMS_SUCCESS("notification ", YCTX, LY_STMT_NOTIFICATION, "notification");
419 TEST_STMS_SUCCESS("ordered-by ", YCTX, LY_STMT_ORDERED_BY, "ordered-by");
420 TEST_STMS_SUCCESS("organization ", YCTX, LY_STMT_ORGANIZATION, "organization");
421 TEST_STMS_SUCCESS("output ", YCTX, LY_STMT_OUTPUT, "output");
422 TEST_STMS_SUCCESS("path ", YCTX, LY_STMT_PATH, "path");
423 TEST_STMS_SUCCESS("pattern ", YCTX, LY_STMT_PATTERN, "pattern");
424 TEST_STMS_SUCCESS("position ", YCTX, LY_STMT_POSITION, "position");
425 TEST_STMS_SUCCESS("prefix ", YCTX, LY_STMT_PREFIX, "prefix");
426 TEST_STMS_SUCCESS("presence ", YCTX, LY_STMT_PRESENCE, "presence");
427 TEST_STMS_SUCCESS("range ", YCTX, LY_STMT_RANGE, "range");
428 TEST_STMS_SUCCESS("reference ", YCTX, LY_STMT_REFERENCE, "reference");
429 TEST_STMS_SUCCESS("refine ", YCTX, LY_STMT_REFINE, "refine");
430 TEST_STMS_SUCCESS("require-instance ", YCTX, LY_STMT_REQUIRE_INSTANCE, "require-instance");
431 TEST_STMS_SUCCESS("revision ", YCTX, LY_STMT_REVISION, "revision");
432 TEST_STMS_SUCCESS("revision-date ", YCTX, LY_STMT_REVISION_DATE, "revision-date");
433 TEST_STMS_SUCCESS("rpc ", YCTX, LY_STMT_RPC, "rpc");
434 TEST_STMS_SUCCESS("status ", YCTX, LY_STMT_STATUS, "status");
435 TEST_STMS_SUCCESS("submodule ", YCTX, LY_STMT_SUBMODULE, "submodule");
436 TEST_STMS_SUCCESS("type ", YCTX, LY_STMT_TYPE, "type");
437 TEST_STMS_SUCCESS("typedef ", YCTX, LY_STMT_TYPEDEF, "typedef");
438 TEST_STMS_SUCCESS("unique ", YCTX, LY_STMT_UNIQUE, "unique");
439 TEST_STMS_SUCCESS("units ", YCTX, LY_STMT_UNITS, "units");
440 TEST_STMS_SUCCESS("uses ", YCTX, LY_STMT_USES, "uses");
441 TEST_STMS_SUCCESS("value ", YCTX, LY_STMT_VALUE, "value");
442 TEST_STMS_SUCCESS("when ", YCTX, LY_STMT_WHEN, "when");
443 TEST_STMS_SUCCESS("yang-version ", YCTX, LY_STMT_YANG_VERSION, "yang-version");
444 TEST_STMS_SUCCESS("yin-element ", YCTX, LY_STMT_YIN_ELEMENT, "yin-element");
445 TEST_STMS_SUCCESS(";config false;", YCTX, LY_STMT_SYNTAX_SEMICOLON, ";");
Michal Vasko63f3d842020-07-08 10:10:14 +0200446 assert_string_equal("config false;", in.current);
Radek Iša56ca9e42020-09-08 18:42:00 +0200447 TEST_STMS_SUCCESS("{ config false;", YCTX, LY_STMT_SYNTAX_LEFT_BRACE, "{");
Michal Vasko63f3d842020-07-08 10:10:14 +0200448 assert_string_equal(" config false;", in.current);
Radek Iša56ca9e42020-09-08 18:42:00 +0200449 TEST_STMS_SUCCESS("}", YCTX, LY_STMT_SYNTAX_RIGHT_BRACE, "}");
Michal Vasko63f3d842020-07-08 10:10:14 +0200450 assert_string_equal("", in.current);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200451
452 /* geenric extension */
Michal Vasko63f3d842020-07-08 10:10:14 +0200453 in.current = p = "nacm:default-deny-write;";
Radek Krejci33090f92020-12-17 20:12:46 +0100454 assert_int_equal(LY_SUCCESS, get_keyword(YCTX, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200455 assert_int_equal(LY_STMT_EXTENSION_INSTANCE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200456 assert_int_equal(23, len);
457 assert_ptr_equal(p, word);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200458}
Radek Krejci44ceedc2018-10-02 15:54:31 +0200459
Radek Krejci05b13982018-11-28 16:22:07 +0100460static void
461test_minmax(void **state)
462{
Radek Krejci05b13982018-11-28 16:22:07 +0100463 uint16_t flags = 0;
464 uint32_t value = 0;
465 struct lysp_ext_instance *ext = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +0100466
Michal Vasko8a67eff2021-12-07 14:04:47 +0100467 PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
Radek Krejci05b13982018-11-28 16:22:07 +0100468
Michal Vasko63f3d842020-07-08 10:10:14 +0200469 in.current = " 1invalid; ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100470 assert_int_equal(LY_EVALID, parse_minelements(YCTX, &value, &flags, &ext));
Radek Iša56ca9e42020-09-08 18:42:00 +0200471 CHECK_LOG_CTX("Invalid value \"1invalid\" of \"min-elements\".", "Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100472
473 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200474 in.current = " -1; ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100475 assert_int_equal(LY_EVALID, parse_minelements(YCTX, &value, &flags, &ext));
Radek Iša56ca9e42020-09-08 18:42:00 +0200476 CHECK_LOG_CTX("Invalid value \"-1\" of \"min-elements\".", "Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100477
Radek Krejcidf6cad12018-11-28 17:10:55 +0100478 /* implementation limit */
479 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200480 in.current = " 4294967296; ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100481 assert_int_equal(LY_EVALID, parse_minelements(YCTX, &value, &flags, &ext));
Radek Iša56ca9e42020-09-08 18:42:00 +0200482 CHECK_LOG_CTX("Value \"4294967296\" is out of \"min-elements\" bounds.", "Line number 1.");
Radek Krejcidf6cad12018-11-28 17:10:55 +0100483
Radek Krejci05b13982018-11-28 16:22:07 +0100484 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200485 in.current = " 1 {config true;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100486 assert_int_equal(LY_EVALID, parse_minelements(YCTX, &value, &flags, &ext));
Radek Iša56ca9e42020-09-08 18:42:00 +0200487 CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"min-elements\".", "Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100488
Michal Vasko63f3d842020-07-08 10:10:14 +0200489 in.current = " 1invalid; ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100490 assert_int_equal(LY_EVALID, parse_maxelements(YCTX, &value, &flags, &ext));
Radek Iša56ca9e42020-09-08 18:42:00 +0200491 CHECK_LOG_CTX("Invalid value \"1invalid\" of \"max-elements\".", "Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100492
493 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200494 in.current = " -1; ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100495 assert_int_equal(LY_EVALID, parse_maxelements(YCTX, &value, &flags, &ext));
Radek Iša56ca9e42020-09-08 18:42:00 +0200496 CHECK_LOG_CTX("Invalid value \"-1\" of \"max-elements\".", "Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100497
Radek Krejcidf6cad12018-11-28 17:10:55 +0100498 /* implementation limit */
499 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200500 in.current = " 4294967296; ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100501 assert_int_equal(LY_EVALID, parse_maxelements(YCTX, &value, &flags, &ext));
Radek Iša56ca9e42020-09-08 18:42:00 +0200502 CHECK_LOG_CTX("Value \"4294967296\" is out of \"max-elements\" bounds.", "Line number 1.");
Radek Krejcidf6cad12018-11-28 17:10:55 +0100503
Radek Krejci05b13982018-11-28 16:22:07 +0100504 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200505 in.current = " 1 {config true;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100506 assert_int_equal(LY_EVALID, parse_maxelements(YCTX, &value, &flags, &ext));
Radek Iša56ca9e42020-09-08 18:42:00 +0200507 CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"max-elements\".", "Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100508}
509
Michal Vasko12ef5362022-09-16 15:13:58 +0200510static void
511test_valid_module(void **state)
512{
513 struct lys_module *mod;
514 char *printed;
515 const char *links_yang =
516 "module links {\n"
517 " yang-version 1.1;\n"
518 " namespace \"urn:module2\";\n"
519 " prefix mod2;\n"
520 "\n"
521 " identity just-another-identity;\n"
522 "\n"
523 " leaf one-leaf {\n"
524 " type string;\n"
525 " }\n"
526 "\n"
527 " list list-for-augment {\n"
528 " key keyleaf;\n"
529 "\n"
530 " leaf keyleaf {\n"
531 " type string;\n"
532 " }\n"
533 "\n"
534 " leaf just-leaf {\n"
535 " type int32;\n"
536 " }\n"
537 " }\n"
538 "\n"
539 " leaf rleaf {\n"
540 " type string;\n"
541 " }\n"
542 "\n"
543 " leaf-list llist {\n"
544 " type string;\n"
545 " min-elements 0;\n"
546 " max-elements 100;\n"
547 " ordered-by user;\n"
548 " }\n"
549 "\n"
550 " grouping rgroup {\n"
551 " leaf rg1 {\n"
552 " type string;\n"
553 " }\n"
554 "\n"
555 " leaf rg2 {\n"
556 " type string;\n"
557 " }\n"
558 " }\n"
559 "}\n";
560 const char *statements_yang =
561 "module statements {\n"
562 " yang-version 1.1;\n"
563 " namespace \"urn:module\";\n"
564 " prefix mod;\n"
565 "\n"
566 " import links {\n"
567 " prefix mod2;\n"
568 " }\n"
569 "\n"
570 " extension ext;\n"
571 "\n"
572 " identity random-identity {\n"
573 " base mod2:just-another-identity;\n"
574 " base another-identity;\n"
575 " }\n"
576 "\n"
577 " identity another-identity {\n"
578 " base mod2:just-another-identity;\n"
579 " }\n"
580 "\n"
581 " typedef percent {\n"
582 " type uint8 {\n"
583 " range \"0 .. 100\";\n"
584 " }\n"
585 " units \"percent\";\n"
586 " }\n"
587 "\n"
588 " list list1 {\n"
589 " key \"a\";\n"
590 " leaf a {\n"
591 " type string;\n"
592 " }\n"
593 " leaf x {\n"
594 " type string;\n"
595 " }\n"
596 " leaf y {\n"
597 " type string;\n"
598 " }\n"
599 " }\n"
600 " container ice-cream-shop {\n"
601 " container employees {\n"
602 " when \"/list1/x\";\n"
603 " list employee {\n"
604 " key \"id\";\n"
605 " unique \"name\";\n"
606 " config true;\n"
607 " min-elements 0 {\n"
608 " mod:ext;\n"
609 " }\n"
610 " max-elements unbounded;\n"
611 " leaf id {\n"
612 " type uint64;\n"
613 " mandatory true;\n"
614 " }\n"
615 " leaf name {\n"
616 " type string;\n"
617 " }\n"
618 " leaf age {\n"
619 " type uint32;\n"
620 " }\n"
621 " }\n"
622 " }\n"
623 " }\n"
624 " container random {\n"
625 " grouping group {\n"
626 " leaf g1 {\n"
627 " type percent;\n"
628 " mandatory false;\n"
629 " }\n"
630 " leaf g2 {\n"
631 " type string;\n"
632 " }\n"
633 " }\n"
634 " choice switch {\n"
635 " case a {\n"
636 " leaf aleaf {\n"
637 " type string;\n"
638 " default \"aaa\";\n"
639 " }\n"
640 " }\n"
641 " case c {\n"
642 " leaf cleaf {\n"
643 " type string;\n"
644 " }\n"
645 " }\n"
646 " }\n"
647 " anyxml xml-data;\n"
648 " anydata any-data;\n"
649 " leaf-list leaflist {\n"
650 " type string;\n"
651 " min-elements 0;\n"
652 " max-elements 20;\n"
653 " }\n"
654 " uses group;\n"
655 " uses mod2:rgroup;\n"
656 " leaf lref {\n"
657 " type leafref {\n"
658 " path \"/mod2:one-leaf\";\n"
659 " }\n"
660 " }\n"
661 " leaf iref {\n"
662 " type identityref {\n"
663 " base mod2:just-another-identity;\n"
664 " }\n"
665 " }\n"
666 " }\n"
667 "\n"
668 " augment \"/random\" {\n"
669 " leaf aug-leaf {\n"
670 " type string;\n"
671 " }\n"
672 " }\n"
673 "\n"
674 " notification notif;\n"
675 "\n"
676 " deviation \"/mod:ice-cream-shop/mod:employees/mod:employee/mod:age\" {\n"
677 " deviate not-supported {\n"
678 " mod:ext;\n"
679 " }\n"
680 " }\n"
681 " deviation \"/mod:list1\" {\n"
682 " deviate add {\n"
683 " mod:ext;\n"
684 " must \"1\";\n"
685 " must \"2\";\n"
686 " unique \"x\";\n"
687 " unique \"y\";\n"
688 " config true;\n"
689 " min-elements 1;\n"
690 " max-elements 2;\n"
691 " }\n"
692 " }\n"
693 " deviation \"/mod:ice-cream-shop/mod:employees/mod:employee\" {\n"
694 " deviate delete {\n"
695 " unique \"name\";\n"
696 " }\n"
697 " }\n"
698 " deviation \"/mod:random/mod:leaflist\" {\n"
699 " deviate replace {\n"
700 " type uint32;\n"
701 " min-elements 10;\n"
702 " max-elements 15;\n"
703 " }\n"
704 " }\n"
705 "}\n";
706
707 UTEST_ADD_MODULE(links_yang, LYS_IN_YANG, NULL, NULL);
708 UTEST_ADD_MODULE(statements_yang, LYS_IN_YANG, NULL, &mod);
709 lys_print_mem(&printed, mod, LYS_OUT_YANG, 0);
710 assert_string_equal(printed, statements_yang);
711 free(printed);
712}
713
Radek Krejci9fcacc12018-10-11 15:59:11 +0200714static struct lysp_module *
Michal Vaskob36053d2020-03-26 15:49:30 +0100715mod_renew(struct lys_yang_parser_ctx *ctx)
Radek Krejci9fcacc12018-10-11 15:59:11 +0200716{
Michal Vasko8a67eff2021-12-07 14:04:47 +0100717 struct ly_ctx *ly_ctx = PARSER_CUR_PMOD(ctx)->mod->ctx;
718 struct lysp_module *pmod;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100719
Michal Vasko4f9da5e2022-03-14 13:11:26 +0100720 lys_module_free(PARSER_CUR_PMOD(ctx)->mod, 0);
Michal Vasko8a67eff2021-12-07 14:04:47 +0100721 pmod = calloc(1, sizeof *pmod);
722 ctx->parsed_mods->objs[0] = pmod;
723 pmod->mod = calloc(1, sizeof *pmod->mod);
724 pmod->mod->parsed = pmod;
725 pmod->mod->ctx = ly_ctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100726
Radek Krejci2efc45b2020-12-22 16:25:44 +0100727 ctx->in->line = 1;
728
Michal Vasko8a67eff2021-12-07 14:04:47 +0100729 return pmod;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100730}
731
732static struct lysp_submodule *
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200733submod_renew(struct lys_yang_parser_ctx *ctx)
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100734{
Michal Vasko8a67eff2021-12-07 14:04:47 +0100735 struct ly_ctx *ly_ctx = PARSER_CUR_PMOD(ctx)->mod->ctx;
736 struct lysp_submodule *submod;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200737
Michal Vasko4f9da5e2022-03-14 13:11:26 +0100738 lys_module_free(PARSER_CUR_PMOD(ctx)->mod, 0);
Michal Vasko8a67eff2021-12-07 14:04:47 +0100739 submod = calloc(1, sizeof *submod);
740 ctx->parsed_mods->objs[0] = submod;
741 submod->mod = calloc(1, sizeof *submod->mod);
742 lydict_insert(ly_ctx, "name", 0, &submod->mod->name);
743 submod->mod->parsed = (struct lysp_module *)submod;
744 submod->mod->ctx = ly_ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200745
Michal Vasko8a67eff2021-12-07 14:04:47 +0100746 return submod;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200747}
748
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100749static LY_ERR
750test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
751 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
752 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
Radek Krejcid33273d2018-10-25 14:55:52 +0200753{
754 *module_data = user_data;
755 *format = LYS_IN_YANG;
756 *free_module_data = NULL;
757 return LY_SUCCESS;
758}
759
Radek Krejci9fcacc12018-10-11 15:59:11 +0200760static void
761test_module(void **state)
762{
Radek Krejci40544fa2019-01-11 09:38:37 +0100763 struct lysp_module *mod = NULL;
764 struct lysp_submodule *submod = NULL;
765 struct lys_module *m;
Radek Iša56ca9e42020-09-08 18:42:00 +0200766 struct lys_yang_parser_ctx *ctx_p;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200767
Radek Iša56ca9e42020-09-08 18:42:00 +0200768 mod = mod_renew(YCTX);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200769
770 /* missing mandatory substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +0200771 in.current = " name {}";
Radek Krejci33090f92020-12-17 20:12:46 +0100772 assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100773 assert_string_equal("name", mod->mod->name);
Radek Iša56ca9e42020-09-08 18:42:00 +0200774 CHECK_LOG_CTX("Missing mandatory keyword \"namespace\" as a child of \"module\".", "Line number 1.");
Radek Krejci9fcacc12018-10-11 15:59:11 +0200775
Radek Iša56ca9e42020-09-08 18:42:00 +0200776 mod = mod_renew(YCTX);
Michal Vaskob24145d2022-07-13 18:34:39 +0200777 in.current = " name {namespace urn:name;}";
Radek Krejci33090f92020-12-17 20:12:46 +0100778 assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
Michal Vaskob24145d2022-07-13 18:34:39 +0200779 assert_string_equal("urn:name", mod->mod->ns);
Radek Iša56ca9e42020-09-08 18:42:00 +0200780 CHECK_LOG_CTX("Missing mandatory keyword \"prefix\" as a child of \"module\".", "Line number 1.");
781 mod = mod_renew(YCTX);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200782
Michal Vaskob24145d2022-07-13 18:34:39 +0200783 in.current = " name {namespace urn:name;prefix \"n\";}";
Radek Krejci33090f92020-12-17 20:12:46 +0100784 assert_int_equal(LY_SUCCESS, parse_module(YCTX, mod));
Michal Vaskob24145d2022-07-13 18:34:39 +0200785 assert_string_equal("n", mod->mod->prefix);
Radek Iša56ca9e42020-09-08 18:42:00 +0200786 mod = mod_renew(YCTX);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200787
Radek Krejci027d5802018-11-14 16:57:28 +0100788#define SCHEMA_BEGINNING " name {yang-version 1.1;namespace urn:x;prefix \"x\";"
789#define SCHEMA_BEGINNING2 " name {namespace urn:x;prefix \"x\";"
Radek Krejcia042ea12018-10-13 07:52:15 +0200790#define TEST_NODE(NODETYPE, INPUT, NAME) \
Michal Vasko63f3d842020-07-08 10:10:14 +0200791 in.current = SCHEMA_BEGINNING INPUT; \
Radek Krejci33090f92020-12-17 20:12:46 +0100792 assert_int_equal(LY_SUCCESS, parse_module(YCTX, mod)); \
Radek Krejcia042ea12018-10-13 07:52:15 +0200793 assert_non_null(mod->data); \
794 assert_int_equal(NODETYPE, mod->data->nodetype); \
795 assert_string_equal(NAME, mod->data->name); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200796 mod = mod_renew(YCTX);
Radek Krejcia042ea12018-10-13 07:52:15 +0200797#define TEST_GENERIC(INPUT, TARGET, TEST) \
Michal Vasko63f3d842020-07-08 10:10:14 +0200798 in.current = SCHEMA_BEGINNING INPUT; \
Radek Krejci33090f92020-12-17 20:12:46 +0100799 assert_int_equal(LY_SUCCESS, parse_module(YCTX, mod)); \
Radek Krejcia042ea12018-10-13 07:52:15 +0200800 assert_non_null(TARGET); \
801 TEST; \
Radek Iša56ca9e42020-09-08 18:42:00 +0200802 mod = mod_renew(YCTX);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100803#define TEST_DUP(MEMBER, VALUE1, VALUE2, LINE) \
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200804 TEST_DUP_GENERIC(SCHEMA_BEGINNING, MEMBER, VALUE1, VALUE2, \
Radek Iša56ca9e42020-09-08 18:42:00 +0200805 parse_module, mod, LINE, mod = mod_renew(YCTX))
Radek Krejcia042ea12018-10-13 07:52:15 +0200806
807 /* duplicated namespace, prefix */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100808 TEST_DUP("namespace", "y", "z", "1");
809 TEST_DUP("prefix", "y", "z", "1");
810 TEST_DUP("contact", "a", "b", "1");
811 TEST_DUP("description", "a", "b", "1");
812 TEST_DUP("organization", "a", "b", "1");
813 TEST_DUP("reference", "a", "b", "1");
Radek Krejcia042ea12018-10-13 07:52:15 +0200814
Radek Krejci70853c52018-10-15 14:46:16 +0200815 /* not allowed in module (submodule-specific) */
Michal Vasko63f3d842020-07-08 10:10:14 +0200816 in.current = SCHEMA_BEGINNING "belongs-to master {prefix m;}}";
Radek Krejci33090f92020-12-17 20:12:46 +0100817 assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
Radek Iša56ca9e42020-09-08 18:42:00 +0200818 CHECK_LOG_CTX("Invalid keyword \"belongs-to\" as a child of \"module\".", "Line number 1.");
819 mod = mod_renew(YCTX);
Radek Krejci70853c52018-10-15 14:46:16 +0200820
Radek Krejcia042ea12018-10-13 07:52:15 +0200821 /* anydata */
822 TEST_NODE(LYS_ANYDATA, "anydata test;}", "test");
823 /* anyxml */
824 TEST_NODE(LYS_ANYXML, "anyxml test;}", "test");
825 /* augment */
826 TEST_GENERIC("augment /somepath;}", mod->augments,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100827 assert_string_equal("/somepath", mod->augments[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200828 /* choice */
829 TEST_NODE(LYS_CHOICE, "choice test;}", "test");
830 /* contact 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100831 TEST_GENERIC("contact \"firstname\" + \n\t\" surname\";}", mod->mod->contact,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100832 assert_string_equal("firstname surname", mod->mod->contact));
Radek Krejcia042ea12018-10-13 07:52:15 +0200833 /* container */
834 TEST_NODE(LYS_CONTAINER, "container test;}", "test");
835 /* description 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100836 TEST_GENERIC("description \'some description\';}", mod->mod->dsc,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100837 assert_string_equal("some description", mod->mod->dsc));
Radek Krejcia042ea12018-10-13 07:52:15 +0200838 /* deviation */
839 TEST_GENERIC("deviation /somepath {deviate not-supported;}}", mod->deviations,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100840 assert_string_equal("/somepath", mod->deviations[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200841 /* extension */
842 TEST_GENERIC("extension test;}", mod->extensions,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100843 assert_string_equal("test", mod->extensions[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200844 /* feature */
845 TEST_GENERIC("feature test;}", mod->features,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100846 assert_string_equal("test", mod->features[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200847 /* grouping */
848 TEST_GENERIC("grouping grp;}", mod->groupings,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100849 assert_string_equal("grp", mod->groupings[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200850 /* identity */
851 TEST_GENERIC("identity test;}", mod->identities,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100852 assert_string_equal("test", mod->identities[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200853 /* import */
Michal Vasko8a67eff2021-12-07 14:04:47 +0100854 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 +0200855 TEST_GENERIC("import zzz {prefix z;}}", mod->imports,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100856 assert_string_equal("zzz", mod->imports[0].name));
Radek Krejci70853c52018-10-15 14:46:16 +0200857
Radek Krejcia042ea12018-10-13 07:52:15 +0200858 /* import - prefix collision */
Michal Vasko63f3d842020-07-08 10:10:14 +0200859 in.current = SCHEMA_BEGINNING "import zzz {prefix x;}}";
Radek Krejci33090f92020-12-17 20:12:46 +0100860 assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
Radek Krejci2efc45b2020-12-22 16:25:44 +0100861 CHECK_LOG_CTX("Prefix \"x\" already used as module prefix.", "Line number 1.");
Radek Iša56ca9e42020-09-08 18:42:00 +0200862 mod = mod_renew(YCTX);
863
Michal Vasko63f3d842020-07-08 10:10:14 +0200864 in.current = SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix y;}}";
Radek Krejci33090f92020-12-17 20:12:46 +0100865 assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
Radek Krejci2efc45b2020-12-22 16:25:44 +0100866 CHECK_LOG_CTX("Prefix \"y\" already used to import \"zzz\" module.", "Line number 1.");
Radek Iša56ca9e42020-09-08 18:42:00 +0200867
868 mod = mod_renew(YCTX);
Radek Krejciddace2c2021-01-08 11:30:56 +0100869 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100870
Michal Vaskob24145d2022-07-13 18:34:39 +0200871 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 +0100872 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 +0200873 CHECK_LOG_CTX("Single revision of the module \"zzz\" imported twice.", NULL);
Radek Krejci70853c52018-10-15 14:46:16 +0200874
Radek Krejcia042ea12018-10-13 07:52:15 +0200875 /* include */
Michal Vasko8a67eff2021-12-07 14:04:47 +0100876 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 +0200877 in.current = "module" SCHEMA_BEGINNING "include xxx;}";
Michal Vasko8a67eff2021-12-07 14:04:47 +0100878 assert_int_equal(lys_parse_mem(PARSER_CUR_PMOD(YCTX)->mod->ctx, in.current, LYS_IN_YANG, NULL), LY_EVALID);
Radek Krejci8297b792020-08-16 14:49:05 +0200879 CHECK_LOG_CTX("Parsing module \"name\" failed.", NULL, "Including \"xxx\" submodule into \"name\" failed.", NULL);
Radek Krejcid33273d2018-10-25 14:55:52 +0200880
Michal Vasko8a67eff2021-12-07 14:04:47 +0100881 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 +0200882 in.current = "module" SCHEMA_BEGINNING "include xxx;}";
Michal Vasko8a67eff2021-12-07 14:04:47 +0100883 assert_int_equal(lys_parse_mem(PARSER_CUR_PMOD(YCTX)->mod->ctx, in.current, LYS_IN_YANG, NULL), LY_EVALID);
Radek Krejci8297b792020-08-16 14:49:05 +0200884 CHECK_LOG_CTX("Parsing module \"name\" failed.", NULL, "Including \"xxx\" submodule into \"name\" failed.", NULL);
Radek Krejcid33273d2018-10-25 14:55:52 +0200885
Michal Vasko8a67eff2021-12-07 14:04:47 +0100886 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 +0200887 TEST_GENERIC("include xxx;}", mod->includes,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100888 assert_string_equal("xxx", mod->includes[0].name));
Radek Krejcid33273d2018-10-25 14:55:52 +0200889
Radek Krejcia042ea12018-10-13 07:52:15 +0200890 /* leaf */
891 TEST_NODE(LYS_LEAF, "leaf test {type string;}}", "test");
892 /* leaf-list */
893 TEST_NODE(LYS_LEAFLIST, "leaf-list test {type string;}}", "test");
894 /* list */
895 TEST_NODE(LYS_LIST, "list test {key a;leaf a {type string;}}}", "test");
896 /* notification */
897 TEST_GENERIC("notification test;}", mod->notifs,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100898 assert_string_equal("test", mod->notifs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200899 /* organization 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100900 TEST_GENERIC("organization \"CESNET a.l.e.\";}", mod->mod->org,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100901 assert_string_equal("CESNET a.l.e.", mod->mod->org));
Radek Krejcia042ea12018-10-13 07:52:15 +0200902 /* reference 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100903 TEST_GENERIC("reference RFC7950;}", mod->mod->ref,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100904 assert_string_equal("RFC7950", mod->mod->ref));
Radek Krejcia042ea12018-10-13 07:52:15 +0200905 /* revision */
906 TEST_GENERIC("revision 2018-10-12;}", mod->revs,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100907 assert_string_equal("2018-10-12", mod->revs[0].date));
Radek Krejcia042ea12018-10-13 07:52:15 +0200908 /* rpc */
909 TEST_GENERIC("rpc test;}", mod->rpcs,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100910 assert_string_equal("test", mod->rpcs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200911 /* typedef */
912 TEST_GENERIC("typedef test{type string;}}", mod->typedefs,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100913 assert_string_equal("test", mod->typedefs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200914 /* uses */
915 TEST_NODE(LYS_USES, "uses test;}", "test");
916 /* yang-version */
Michal Vasko63f3d842020-07-08 10:10:14 +0200917 in.current = SCHEMA_BEGINNING2 "\n\tyang-version 10;}";
Radek Krejci33090f92020-12-17 20:12:46 +0100918 assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
Radek Krejci2efc45b2020-12-22 16:25:44 +0100919 CHECK_LOG_CTX("Invalid value \"10\" of \"yang-version\".", NULL);
Radek Iša56ca9e42020-09-08 18:42:00 +0200920 mod = mod_renew(YCTX);
Radek Krejci96e48da2020-09-04 13:18:06 +0200921 in.current = SCHEMA_BEGINNING2 "yang-version 1;yang-version 1.1;}";
Radek Krejci33090f92020-12-17 20:12:46 +0100922 assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
Radek Krejci2efc45b2020-12-22 16:25:44 +0100923 CHECK_LOG_CTX("Duplicate keyword \"yang-version\".", NULL);
Radek Iša56ca9e42020-09-08 18:42:00 +0200924 mod = mod_renew(YCTX);
Radek Krejci96e48da2020-09-04 13:18:06 +0200925 in.current = SCHEMA_BEGINNING2 "yang-version 1;}";
Radek Krejci33090f92020-12-17 20:12:46 +0100926 assert_int_equal(LY_SUCCESS, parse_module(YCTX, mod));
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200927 assert_int_equal(1, mod->version);
Radek Iša56ca9e42020-09-08 18:42:00 +0200928 mod = mod_renew(YCTX);
Michal Vasko63f3d842020-07-08 10:10:14 +0200929 in.current = SCHEMA_BEGINNING2 "yang-version \"1.1\";}";
Radek Krejci33090f92020-12-17 20:12:46 +0100930 assert_int_equal(LY_SUCCESS, parse_module(YCTX, mod));
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200931 assert_int_equal(2, mod->version);
Radek Iša56ca9e42020-09-08 18:42:00 +0200932 mod = mod_renew(YCTX);
Radek Krejci40544fa2019-01-11 09:38:37 +0100933
Michal Vasko63f3d842020-07-08 10:10:14 +0200934 in.current = "module " SCHEMA_BEGINNING "} module q {namespace urn:q;prefixq;}";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200935 m = calloc(1, sizeof *m);
Michal Vasko8a67eff2021-12-07 14:04:47 +0100936 m->ctx = PARSER_CUR_PMOD(YCTX)->mod->ctx;
aPiecekc3e26142021-06-22 14:25:49 +0200937 assert_int_equal(LY_EVALID, yang_parse_module(&ctx_p, &in, m));
Radek Iša56ca9e42020-09-08 18:42:00 +0200938 CHECK_LOG_CTX("Trailing garbage \"module q {names...\" after module, expected end-of-input.", "Line number 1.");
Michal Vaskob36053d2020-03-26 15:49:30 +0100939 yang_parser_ctx_free(ctx_p);
Michal Vasko4f9da5e2022-03-14 13:11:26 +0100940 lys_module_free(m, 0);
Radek Krejci40544fa2019-01-11 09:38:37 +0100941
Michal Vasko63f3d842020-07-08 10:10:14 +0200942 in.current = "prefix " SCHEMA_BEGINNING "}";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200943 m = calloc(1, sizeof *m);
Michal Vasko8a67eff2021-12-07 14:04:47 +0100944 m->ctx = PARSER_CUR_PMOD(YCTX)->mod->ctx;
aPiecekc3e26142021-06-22 14:25:49 +0200945 assert_int_equal(LY_EVALID, yang_parse_module(&ctx_p, &in, m));
Radek Iša56ca9e42020-09-08 18:42:00 +0200946 CHECK_LOG_CTX("Invalid keyword \"prefix\", expected \"module\" or \"submodule\".", "Line number 1.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200947 yang_parser_ctx_free(ctx_p);
Michal Vasko4f9da5e2022-03-14 13:11:26 +0100948 lys_module_free(m, 0);
Radek Krejci09306362018-10-15 15:26:01 +0200949
Michal Vasko63f3d842020-07-08 10:10:14 +0200950 in.current = "module " SCHEMA_BEGINNING "leaf enum {type enumeration {enum seven { position 7;}}}}";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200951 m = calloc(1, sizeof *m);
Michal Vasko8a67eff2021-12-07 14:04:47 +0100952 m->ctx = PARSER_CUR_PMOD(YCTX)->mod->ctx;
aPiecekc3e26142021-06-22 14:25:49 +0200953 assert_int_equal(LY_EVALID, yang_parse_module(&ctx_p, &in, m));
Radek Iša56ca9e42020-09-08 18:42:00 +0200954 CHECK_LOG_CTX("Invalid keyword \"position\" as a child of \"enum\".", "Line number 1.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200955 yang_parser_ctx_free(ctx_p);
Michal Vasko4f9da5e2022-03-14 13:11:26 +0100956 lys_module_free(m, 0);
David Sedlák9fb515f2019-07-11 10:33:58 +0200957
Radek Krejci156ccaf2018-10-15 15:49:17 +0200958 /* extensions */
959 TEST_GENERIC("prefix:test;}", mod->exts,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100960 assert_string_equal("prefix:test", mod->exts[0].name);
Radek Krejciab430862021-03-02 20:13:40 +0100961 assert_int_equal(LY_STMT_MODULE, mod->exts[0].parent_stmt));
Radek Iša56ca9e42020-09-08 18:42:00 +0200962 mod = mod_renew(YCTX);
Radek Krejci156ccaf2018-10-15 15:49:17 +0200963
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200964 /* invalid substatement */
Michal Vasko63f3d842020-07-08 10:10:14 +0200965 in.current = SCHEMA_BEGINNING "must false;}";
Radek Krejci33090f92020-12-17 20:12:46 +0100966 assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
Radek Krejci2efc45b2020-12-22 16:25:44 +0100967 CHECK_LOG_CTX("Invalid keyword \"must\" as a child of \"module\".", NULL);
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200968
Radek Krejci09306362018-10-15 15:26:01 +0200969 /* submodule */
Radek Iša56ca9e42020-09-08 18:42:00 +0200970 submod = submod_renew(YCTX);
Radek Krejci09306362018-10-15 15:26:01 +0200971
972 /* missing mandatory substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +0200973 in.current = " subname {}";
Radek Krejci33090f92020-12-17 20:12:46 +0100974 assert_int_equal(LY_EVALID, parse_submodule(YCTX, submod));
Radek Krejci2efc45b2020-12-22 16:25:44 +0100975 CHECK_LOG_CTX("Missing mandatory keyword \"belongs-to\" as a child of \"submodule\".", NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100976 assert_string_equal("subname", submod->name);
Radek Iša56ca9e42020-09-08 18:42:00 +0200977
978 submod = submod_renew(YCTX);
Radek Krejci09306362018-10-15 15:26:01 +0200979
Michal Vasko63f3d842020-07-08 10:10:14 +0200980 in.current = " subname {belongs-to name {prefix x;}}";
Radek Krejci33090f92020-12-17 20:12:46 +0100981 assert_int_equal(LY_SUCCESS, parse_submodule(YCTX, submod));
Michal Vaskoc3781c32020-10-06 14:04:08 +0200982 assert_string_equal("name", submod->mod->name);
Radek Iša56ca9e42020-09-08 18:42:00 +0200983 submod = submod_renew(YCTX);
Radek Krejci09306362018-10-15 15:26:01 +0200984
985#undef SCHEMA_BEGINNING
Radek Krejci313d9902018-11-08 09:42:58 +0100986#define SCHEMA_BEGINNING " subname {belongs-to name {prefix x;}"
Radek Krejci09306362018-10-15 15:26:01 +0200987
988 /* duplicated namespace, prefix */
Michal Vasko63f3d842020-07-08 10:10:14 +0200989 in.current = " subname {belongs-to name {prefix x;}belongs-to module1;belongs-to module2;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100990 assert_int_equal(LY_EVALID, parse_submodule(YCTX, submod));
Radek Krejci2efc45b2020-12-22 16:25:44 +0100991 CHECK_LOG_CTX("Duplicate keyword \"belongs-to\".", NULL);
Radek Iša56ca9e42020-09-08 18:42:00 +0200992 submod = submod_renew(YCTX);
Radek Krejci09306362018-10-15 15:26:01 +0200993
994 /* not allowed in submodule (module-specific) */
Michal Vasko63f3d842020-07-08 10:10:14 +0200995 in.current = SCHEMA_BEGINNING "namespace \"urn:z\";}";
Radek Krejci33090f92020-12-17 20:12:46 +0100996 assert_int_equal(LY_EVALID, parse_submodule(YCTX, submod));
Radek Krejci2efc45b2020-12-22 16:25:44 +0100997 CHECK_LOG_CTX("Invalid keyword \"namespace\" as a child of \"submodule\".", NULL);
Radek Iša56ca9e42020-09-08 18:42:00 +0200998 submod = submod_renew(YCTX);
Michal Vasko63f3d842020-07-08 10:10:14 +0200999 in.current = SCHEMA_BEGINNING "prefix m;}}";
Radek Krejci33090f92020-12-17 20:12:46 +01001000 assert_int_equal(LY_EVALID, parse_submodule(YCTX, submod));
Radek Krejci2efc45b2020-12-22 16:25:44 +01001001 CHECK_LOG_CTX("Invalid keyword \"prefix\" as a child of \"submodule\".", NULL);
Radek Iša56ca9e42020-09-08 18:42:00 +02001002 submod = submod_renew(YCTX);
Radek Krejcia042ea12018-10-13 07:52:15 +02001003
Michal Vasko63f3d842020-07-08 10:10:14 +02001004 in.current = "submodule " SCHEMA_BEGINNING "} module q {namespace urn:q;prefixq;}";
Michal Vasko8a67eff2021-12-07 14:04:47 +01001005 assert_int_equal(LY_EVALID, yang_parse_submodule(&ctx_p, PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lys_parser_ctx *)YCTX, YCTX->in, &submod));
Radek Iša56ca9e42020-09-08 18:42:00 +02001006 CHECK_LOG_CTX("Trailing garbage \"module q {names...\" after submodule, expected end-of-input.", "Line number 1.");
Michal Vaskob36053d2020-03-26 15:49:30 +01001007 yang_parser_ctx_free(ctx_p);
Radek Krejci40544fa2019-01-11 09:38:37 +01001008
Michal Vasko63f3d842020-07-08 10:10:14 +02001009 in.current = "prefix " SCHEMA_BEGINNING "}";
Michal Vasko8a67eff2021-12-07 14:04:47 +01001010 assert_int_equal(LY_EVALID, yang_parse_submodule(&ctx_p, PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lys_parser_ctx *)YCTX, YCTX->in, &submod));
Radek Iša56ca9e42020-09-08 18:42:00 +02001011 CHECK_LOG_CTX("Invalid keyword \"prefix\", expected \"module\" or \"submodule\".", "Line number 1.");
Michal Vaskob36053d2020-03-26 15:49:30 +01001012 yang_parser_ctx_free(ctx_p);
Radek Iša56ca9e42020-09-08 18:42:00 +02001013 submod = submod_renew(YCTX);
Radek Krejci40544fa2019-01-11 09:38:37 +01001014
Radek Krejcia042ea12018-10-13 07:52:15 +02001015#undef TEST_GENERIC
1016#undef TEST_NODE
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001017#undef TEST_DUP
Radek Krejcia042ea12018-10-13 07:52:15 +02001018#undef SCHEMA_BEGINNING
Radek Krejciefd22f62018-09-27 11:47:58 +02001019}
1020
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001021static void
1022test_deviation(void **state)
1023{
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001024 struct lysp_deviation *d = NULL;
Radek Krejci33090f92020-12-17 20:12:46 +01001025
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001026 /* invalid cardinality */
Michal Vasko12ef5362022-09-16 15:13:58 +02001027 TEST_DUP_GENERIC(" test {deviate not-supported;", "description", "a", "b", parse_deviation, &d, "1", );
1028 TEST_DUP_GENERIC(" test {deviate not-supported;", "reference", "a", "b", parse_deviation, &d, "1", );
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001029
1030 /* missing mandatory substatement */
Michal Vasko63f3d842020-07-08 10:10:14 +02001031 in.current = " test {description text;}";
Radek Krejci33090f92020-12-17 20:12:46 +01001032 assert_int_equal(LY_EVALID, parse_deviation(YCTX, &d));
Radek Iša56ca9e42020-09-08 18:42:00 +02001033 CHECK_LOG_CTX("Missing mandatory keyword \"deviate\" as a child of \"deviation\".", "Line number 1.");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001034
1035 /* invalid substatement */
Michal Vasko63f3d842020-07-08 10:10:14 +02001036 in.current = " test {deviate not-supported; status obsolete;}";
Radek Krejci33090f92020-12-17 20:12:46 +01001037 assert_int_equal(LY_EVALID, parse_deviation(YCTX, &d));
Radek Iša56ca9e42020-09-08 18:42:00 +02001038 CHECK_LOG_CTX("Invalid keyword \"status\" as a child of \"deviation\".", "Line number 1.");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001039}
1040
1041static void
1042test_deviate(void **state)
1043{
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001044 struct lysp_deviate *d = NULL;
Radek Krejci33090f92020-12-17 20:12:46 +01001045
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001046 /* invalid cardinality */
Michal Vasko12ef5362022-09-16 15:13:58 +02001047 TEST_DUP_GENERIC("add {", "config", "true", "false", parse_deviate, &d, "1", );
1048 TEST_DUP_GENERIC("add {", "mandatory", "true", "false", parse_deviate, &d, "1", );
1049 TEST_DUP_GENERIC("add {", "max-elements", "1", "2", parse_deviate, &d, "1", );
1050 TEST_DUP_GENERIC("add {", "min-elements", "1", "2", parse_deviate, &d, "1", );
1051 TEST_DUP_GENERIC("add {", "units", "kilometers", "miles", parse_deviate, &d, "1", );
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001052
1053 /* invalid substatements */
1054#define TEST_NOT_SUP(DEV, STMT, VALUE) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001055 in.current = " "DEV" {"STMT" "VALUE";}..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001056 assert_int_equal(LY_EVALID, parse_deviate(YCTX, &d)); \
Michal Vasko12ef5362022-09-16 15:13:58 +02001057 CHECK_LOG_CTX("Deviate \""DEV"\" does not support keyword \""STMT"\".", "Line number 1.");
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001058
1059 TEST_NOT_SUP("not-supported", "units", "meters");
1060 TEST_NOT_SUP("not-supported", "must", "1");
1061 TEST_NOT_SUP("not-supported", "unique", "x");
1062 TEST_NOT_SUP("not-supported", "default", "a");
1063 TEST_NOT_SUP("not-supported", "config", "true");
1064 TEST_NOT_SUP("not-supported", "mandatory", "true");
1065 TEST_NOT_SUP("not-supported", "min-elements", "1");
1066 TEST_NOT_SUP("not-supported", "max-elements", "2");
1067 TEST_NOT_SUP("not-supported", "type", "string");
1068 TEST_NOT_SUP("add", "type", "string");
1069 TEST_NOT_SUP("delete", "config", "true");
1070 TEST_NOT_SUP("delete", "mandatory", "true");
1071 TEST_NOT_SUP("delete", "min-elements", "1");
1072 TEST_NOT_SUP("delete", "max-elements", "2");
1073 TEST_NOT_SUP("delete", "type", "string");
1074 TEST_NOT_SUP("replace", "must", "1");
1075 TEST_NOT_SUP("replace", "unique", "a");
1076
Michal Vasko63f3d842020-07-08 10:10:14 +02001077 in.current = " nonsence; ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001078 assert_int_equal(LY_EVALID, parse_deviate(YCTX, &d));
Radek Iša56ca9e42020-09-08 18:42:00 +02001079 CHECK_LOG_CTX("Invalid value \"nonsence\" of \"deviate\".", "Line number 1.");\
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001080 assert_null(d);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001081#undef TEST_NOT_SUP
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001082}
1083
Radek Krejci8c370832018-11-02 15:10:03 +01001084static void
1085test_container(void **state)
1086{
Radek Krejci8c370832018-11-02 15:10:03 +01001087 struct lysp_node_container *c = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001088
Michal Vasko8a67eff2021-12-07 14:04:47 +01001089 PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
aPiecek8d4e75d2021-06-24 14:47:06 +02001090 YCTX->main_ctx = (struct lys_parser_ctx *)YCTX;
Radek Krejci8c370832018-11-02 15:10:03 +01001091
1092 /* invalid cardinality */
1093#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001094 in.current = "cont {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001095 assert_int_equal(LY_EVALID, parse_container(YCTX, NULL, (struct lysp_node**)&c)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001096 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Michal Vasko8a67eff2021-12-07 14:04:47 +01001097 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001098
1099 TEST_DUP("config", "true", "false");
1100 TEST_DUP("description", "text1", "text2");
1101 TEST_DUP("presence", "true", "false");
1102 TEST_DUP("reference", "1", "2");
1103 TEST_DUP("status", "current", "obsolete");
1104 TEST_DUP("when", "true", "false");
1105#undef TEST_DUP
1106
1107 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001108 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 +01001109 "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 +01001110 assert_int_equal(LY_SUCCESS, parse_container(YCTX, NULL, (struct lysp_node **)&c));
Radek Iša56ca9e42020-09-08 18:42:00 +02001111 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 +01001112 assert_non_null(c->actions);
1113 assert_non_null(c->child);
Radek Krejci8c370832018-11-02 15:10:03 +01001114 assert_non_null(c->groupings);
Radek Krejci8c370832018-11-02 15:10:03 +01001115 assert_non_null(c->musts);
1116 assert_non_null(c->notifs);
1117 assert_string_equal("true", c->presence);
Radek Krejci8c370832018-11-02 15:10:03 +01001118 assert_non_null(c->typedefs);
Radek Iša56ca9e42020-09-08 18:42:00 +02001119 ly_set_erase(&YCTX->tpdfs_nodes, NULL);
aPiecek63e080d2021-06-29 13:53:28 +02001120 ly_set_erase(&YCTX->grps_nodes, NULL);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001121 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001122
1123 /* invalid */
Michal Vasko63f3d842020-07-08 10:10:14 +02001124 in.current = " cont {augment /root;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001125 assert_int_equal(LY_EVALID, parse_container(YCTX, NULL, (struct lysp_node **)&c));
Radek Iša56ca9e42020-09-08 18:42:00 +02001126 CHECK_LOG_CTX("Invalid keyword \"augment\" as a child of \"container\".", "Line number 1.");
Michal Vasko8a67eff2021-12-07 14:04:47 +01001127 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)c); c = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +02001128 in.current = " cont {nonsence true;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001129 assert_int_equal(LY_EVALID, parse_container(YCTX, NULL, (struct lysp_node **)&c));
Radek Iša56ca9e42020-09-08 18:42:00 +02001130 CHECK_LOG_CTX("Invalid character sequence \"nonsence\", expected a keyword.", "Line number 1.");
Michal Vasko8a67eff2021-12-07 14:04:47 +01001131 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001132
Michal Vasko8a67eff2021-12-07 14:04:47 +01001133 PARSER_CUR_PMOD(YCTX)->version = 1; /* simulate YANG 1.0 */
Michal Vasko63f3d842020-07-08 10:10:14 +02001134 in.current = " cont {action x;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001135 assert_int_equal(LY_EVALID, parse_container(YCTX, NULL, (struct lysp_node **)&c));
Radek Iša56ca9e42020-09-08 18:42:00 +02001136 CHECK_LOG_CTX("Invalid keyword \"action\" as a child of \"container\" - "
1137 "the statement is allowed only in YANG 1.1 modules.", "Line number 1.");
Michal Vasko8a67eff2021-12-07 14:04:47 +01001138 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001139}
1140
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001141static void
1142test_leaf(void **state)
1143{
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001144 struct lysp_node_leaf *l = NULL;
Radek Krejci33090f92020-12-17 20:12:46 +01001145
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001146 /* invalid cardinality */
1147#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001148 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001149 assert_int_equal(LY_EVALID, parse_leaf(YCTX, NULL, (struct lysp_node**)&l)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001150 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Michal Vasko8a67eff2021-12-07 14:04:47 +01001151 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node*)l); l = NULL;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001152
1153 TEST_DUP("config", "true", "false");
1154 TEST_DUP("default", "x", "y");
1155 TEST_DUP("description", "text1", "text2");
1156 TEST_DUP("mandatory", "true", "false");
1157 TEST_DUP("reference", "1", "2");
1158 TEST_DUP("status", "current", "obsolete");
Radek Krejci0e5d8382018-11-28 16:37:53 +01001159 TEST_DUP("type", "int8", "uint8");
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001160 TEST_DUP("units", "text1", "text2");
1161 TEST_DUP("when", "true", "false");
1162#undef TEST_DUP
1163
1164 /* full content - without mandatory which is mutual exclusive with default */
Michal Vasko63f3d842020-07-08 10:10:14 +02001165 in.current = "l {config false;default \"xxx\";description test;if-feature f;"
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001166 "must 'expr';reference test;status current;type string; units yyy;when true;m:ext;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001167 assert_int_equal(LY_SUCCESS, parse_leaf(YCTX, NULL, (struct lysp_node **)&l));
Radek Iša56ca9e42020-09-08 18:42:00 +02001168 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 +02001169 assert_string_equal("xxx", l->dflt.str);
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001170 assert_string_equal("yyy", l->units);
1171 assert_string_equal("string", l->type.name);
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001172 assert_non_null(l->musts);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001173 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)l); l = NULL;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001174
1175 /* full content - now with mandatory */
Michal Vasko63f3d842020-07-08 10:10:14 +02001176 in.current = "l {mandatory true; type string;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001177 assert_int_equal(LY_SUCCESS, parse_leaf(YCTX, NULL, (struct lysp_node **)&l));
Radek Iša56ca9e42020-09-08 18:42:00 +02001178 CHECK_LYSP_NODE(l, NULL, 0, LYS_MAND_TRUE, 0, "l", 0, LYS_LEAF, 0, NULL, 0);
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001179 assert_string_equal("string", l->type.name);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001180 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)l); l = NULL;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001181
1182 /* invalid */
Michal Vasko63f3d842020-07-08 10:10:14 +02001183 in.current = " l {description \"missing type\";} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001184 assert_int_equal(LY_EVALID, parse_leaf(YCTX, NULL, (struct lysp_node **)&l));
Radek Iša56ca9e42020-09-08 18:42:00 +02001185 CHECK_LOG_CTX("Missing mandatory keyword \"type\" as a child of \"leaf\".", "Line number 1.");
Michal Vasko8a67eff2021-12-07 14:04:47 +01001186 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)l); l = NULL;
aPiecek4bb1e372021-05-07 11:01:00 +02001187
1188 in.current = "l { type iid { path qpud wrong {";
1189 assert_int_equal(LY_EVALID, parse_leaf(YCTX, NULL, (struct lysp_node **)&l));
1190 CHECK_LOG_CTX("Invalid character sequence \"wrong\", expected a keyword.", "Line number 1.");
Michal Vasko8a67eff2021-12-07 14:04:47 +01001191 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)l); l = NULL;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001192}
1193
Radek Krejci0e5d8382018-11-28 16:37:53 +01001194static void
1195test_leaflist(void **state)
1196{
Radek Krejci0e5d8382018-11-28 16:37:53 +01001197 struct lysp_node_leaflist *ll = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001198
Michal Vasko8a67eff2021-12-07 14:04:47 +01001199 PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
Radek Krejci0e5d8382018-11-28 16:37:53 +01001200
1201 /* invalid cardinality */
1202#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001203 in.current = "ll {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001204 assert_int_equal(LY_EVALID, parse_leaflist(YCTX, NULL, (struct lysp_node**)&ll)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001205 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Michal Vasko8a67eff2021-12-07 14:04:47 +01001206 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node*)ll); ll = NULL;
Radek Krejci0e5d8382018-11-28 16:37:53 +01001207
1208 TEST_DUP("config", "true", "false");
1209 TEST_DUP("description", "text1", "text2");
1210 TEST_DUP("max-elements", "10", "20");
1211 TEST_DUP("min-elements", "10", "20");
1212 TEST_DUP("ordered-by", "user", "system");
1213 TEST_DUP("reference", "1", "2");
1214 TEST_DUP("status", "current", "obsolete");
1215 TEST_DUP("type", "int8", "uint8");
1216 TEST_DUP("units", "text1", "text2");
1217 TEST_DUP("when", "true", "false");
1218#undef TEST_DUP
1219
1220 /* full content - without min-elements which is mutual exclusive with default */
Michal Vasko63f3d842020-07-08 10:10:14 +02001221 in.current = "ll {config false;default \"xxx\"; default \"yyy\";description test;if-feature f;"
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001222 "max-elements 10;must 'expr';ordered-by user;reference test;"
1223 "status current;type string; units zzz;when true;m:ext;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001224 assert_int_equal(LY_SUCCESS, parse_leaflist(YCTX, NULL, (struct lysp_node **)&ll));
Radek Iša56ca9e42020-09-08 18:42:00 +02001225 CHECK_LYSP_NODE(ll, "test", 1, 0x446, 1, "ll", 0, LYS_LEAFLIST, 0, "test", 1);
Radek Krejci0e5d8382018-11-28 16:37:53 +01001226 assert_non_null(ll->dflts);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001227 assert_int_equal(2, LY_ARRAY_COUNT(ll->dflts));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001228 assert_string_equal("xxx", ll->dflts[0].str);
1229 assert_string_equal("yyy", ll->dflts[1].str);
Radek Krejci0e5d8382018-11-28 16:37:53 +01001230 assert_string_equal("zzz", ll->units);
1231 assert_int_equal(10, ll->max);
1232 assert_int_equal(0, ll->min);
1233 assert_string_equal("string", ll->type.name);
Radek Krejci0e5d8382018-11-28 16:37:53 +01001234 assert_non_null(ll->musts);
Radek Krejci0e5d8382018-11-28 16:37:53 +01001235 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_USER | LYS_SET_MAX, ll->flags);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001236 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)ll); ll = NULL;
Radek Krejci0e5d8382018-11-28 16:37:53 +01001237
1238 /* full content - now with min-elements */
Michal Vasko63f3d842020-07-08 10:10:14 +02001239 in.current = "ll {min-elements 10; type string;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001240 assert_int_equal(LY_SUCCESS, parse_leaflist(YCTX, NULL, (struct lysp_node **)&ll));
Radek Iša56ca9e42020-09-08 18:42:00 +02001241 CHECK_LYSP_NODE(ll, NULL, 0, 0x200, 0, "ll", 0, LYS_LEAFLIST, 0, NULL, 0);
Radek Krejci0e5d8382018-11-28 16:37:53 +01001242 assert_string_equal("string", ll->type.name);
1243 assert_int_equal(0, ll->max);
1244 assert_int_equal(10, ll->min);
1245 assert_int_equal(LYS_SET_MIN, ll->flags);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001246 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)ll); ll = NULL;
Radek Krejci0e5d8382018-11-28 16:37:53 +01001247
1248 /* invalid */
Michal Vasko63f3d842020-07-08 10:10:14 +02001249 in.current = " ll {description \"missing type\";} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001250 assert_int_equal(LY_EVALID, parse_leaflist(YCTX, NULL, (struct lysp_node **)&ll));
Radek Iša56ca9e42020-09-08 18:42:00 +02001251 CHECK_LOG_CTX("Missing mandatory keyword \"type\" as a child of \"leaf-list\".", "Line number 1.");
Michal Vasko8a67eff2021-12-07 14:04:47 +01001252 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)ll); ll = NULL;
Radek Krejci0e5d8382018-11-28 16:37:53 +01001253
Michal Vasko8a67eff2021-12-07 14:04:47 +01001254 PARSER_CUR_PMOD(YCTX)->version = 1; /* simulate YANG 1.0 - default statement is not allowed */
Michal Vasko63f3d842020-07-08 10:10:14 +02001255 in.current = " ll {default xx; type string;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001256 assert_int_equal(LY_EVALID, parse_leaflist(YCTX, NULL, (struct lysp_node **)&ll));
Radek Iša56ca9e42020-09-08 18:42:00 +02001257 CHECK_LOG_CTX("Invalid keyword \"default\" as a child of \"leaf-list\" - the statement is allowed only in YANG 1.1 modules.", "Line number 1.");
Michal Vasko8a67eff2021-12-07 14:04:47 +01001258 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)ll); ll = NULL;
Radek Krejci0e5d8382018-11-28 16:37:53 +01001259}
1260
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001261static void
1262test_list(void **state)
1263{
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001264 struct lysp_node_list *l = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001265
Michal Vasko8a67eff2021-12-07 14:04:47 +01001266 PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
aPiecek8d4e75d2021-06-24 14:47:06 +02001267 YCTX->main_ctx = (struct lys_parser_ctx *)YCTX;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001268
1269 /* invalid cardinality */
1270#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001271 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001272 assert_int_equal(LY_EVALID, parse_list(YCTX, NULL, (struct lysp_node**)&l)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001273 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Michal Vasko8a67eff2021-12-07 14:04:47 +01001274 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node*)l); l = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001275
1276 TEST_DUP("config", "true", "false");
1277 TEST_DUP("description", "text1", "text2");
1278 TEST_DUP("key", "one", "two");
1279 TEST_DUP("max-elements", "10", "20");
1280 TEST_DUP("min-elements", "10", "20");
1281 TEST_DUP("ordered-by", "user", "system");
1282 TEST_DUP("reference", "1", "2");
1283 TEST_DUP("status", "current", "obsolete");
1284 TEST_DUP("when", "true", "false");
1285#undef TEST_DUP
1286
1287 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001288 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 +01001289 "leaf-list ll {type string;} list li;max-elements 10; min-elements 1;must 'expr';notification not; ordered-by system; reference test;"
1290 "status current;typedef t {type int8;}unique xxx;unique yyy;uses g;when true;m:ext;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001291 assert_int_equal(LY_SUCCESS, parse_list(YCTX, NULL, (struct lysp_node **)&l));
Radek Iša56ca9e42020-09-08 18:42:00 +02001292 CHECK_LYSP_NODE(l, "test", 1, LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM | LYS_SET_MAX | LYS_SET_MIN, 1, "l",
1293 0, LYS_LIST, 0, "test", 1);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001294 assert_string_equal("l", l->key);
1295 assert_non_null(l->uniques);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001296 assert_int_equal(2, LY_ARRAY_COUNT(l->uniques));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001297 assert_string_equal("xxx", l->uniques[0].str);
1298 assert_string_equal("yyy", l->uniques[1].str);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001299 assert_int_equal(10, l->max);
1300 assert_int_equal(1, l->min);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001301 assert_non_null(l->musts);
Radek Iša56ca9e42020-09-08 18:42:00 +02001302 ly_set_erase(&YCTX->tpdfs_nodes, NULL);
aPiecek63e080d2021-06-29 13:53:28 +02001303 ly_set_erase(&YCTX->grps_nodes, NULL);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001304 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)l); l = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001305
Radek Krejcif538ce52019-03-05 10:46:14 +01001306 /* invalid content */
Michal Vasko8a67eff2021-12-07 14:04:47 +01001307 PARSER_CUR_PMOD(YCTX)->version = 1; /* simulate YANG 1.0 */
Michal Vasko63f3d842020-07-08 10:10:14 +02001308 in.current = "l {action x;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001309 assert_int_equal(LY_EVALID, parse_list(YCTX, NULL, (struct lysp_node **)&l));
Radek Iša56ca9e42020-09-08 18:42:00 +02001310 CHECK_LOG_CTX("Invalid keyword \"action\" as a child of \"list\" - the statement is allowed only in YANG 1.1 modules.", "Line number 1.");
Michal Vasko8a67eff2021-12-07 14:04:47 +01001311 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)l); l = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001312}
1313
Radek Krejci056d0a82018-12-06 16:57:25 +01001314static void
1315test_choice(void **state)
1316{
Radek Krejci056d0a82018-12-06 16:57:25 +01001317 struct lysp_node_choice *ch = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001318
Michal Vasko8a67eff2021-12-07 14:04:47 +01001319 PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
Radek Krejci056d0a82018-12-06 16:57:25 +01001320
1321 /* invalid cardinality */
1322#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001323 in.current = "ch {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001324 assert_int_equal(LY_EVALID, parse_choice(YCTX, NULL, (struct lysp_node**)&ch)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001325 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Michal Vasko8a67eff2021-12-07 14:04:47 +01001326 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node*)ch); ch = NULL;
Radek Krejci056d0a82018-12-06 16:57:25 +01001327
1328 TEST_DUP("config", "true", "false");
1329 TEST_DUP("default", "a", "b");
1330 TEST_DUP("description", "text1", "text2");
1331 TEST_DUP("mandatory", "true", "false");
1332 TEST_DUP("reference", "1", "2");
1333 TEST_DUP("status", "current", "obsolete");
1334 TEST_DUP("when", "true", "false");
1335#undef TEST_DUP
1336
Radek Krejcia9026eb2018-12-12 16:04:47 +01001337 /* full content - without default due to a collision with mandatory */
Michal Vasko63f3d842020-07-08 10:10:14 +02001338 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 +01001339 "leaf-list ll {type string;} list li;mandatory true;reference test;status current;when true;m:ext;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001340 assert_int_equal(LY_SUCCESS, parse_choice(YCTX, NULL, (struct lysp_node **)&ch));
Radek Iša56ca9e42020-09-08 18:42:00 +02001341 CHECK_LYSP_NODE(ch, "test", 1, LYS_CONFIG_R | LYS_STATUS_CURR | LYS_MAND_TRUE, 1, "ch", 0, LYS_CHOICE, 0, "test", 1);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001342 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)ch); ch = NULL;
Radek Krejci056d0a82018-12-06 16:57:25 +01001343
Radek Krejcia9026eb2018-12-12 16:04:47 +01001344 /* full content - the default missing from the previous node */
Michal Vasko63f3d842020-07-08 10:10:14 +02001345 in.current = "ch {default c;case c;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001346 assert_int_equal(LY_SUCCESS, parse_choice(YCTX, NULL, (struct lysp_node **)&ch));
Radek Iša56ca9e42020-09-08 18:42:00 +02001347 CHECK_LYSP_NODE(ch, NULL, 0, 0, 0, "ch", 0, LYS_CHOICE, 0, NULL, 0);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001348 assert_string_equal("c", ch->dflt.str);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001349 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)ch); ch = NULL;
Radek Krejcia9026eb2018-12-12 16:04:47 +01001350}
1351
1352static void
1353test_case(void **state)
1354{
Radek Krejcia9026eb2018-12-12 16:04:47 +01001355 struct lysp_node_case *cs = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001356
Michal Vasko8a67eff2021-12-07 14:04:47 +01001357 PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
Radek Krejcia9026eb2018-12-12 16:04:47 +01001358
1359 /* invalid cardinality */
1360#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001361 in.current = "cs {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001362 assert_int_equal(LY_EVALID, parse_case(YCTX, NULL, (struct lysp_node**)&cs)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001363 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Michal Vasko8a67eff2021-12-07 14:04:47 +01001364 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node*)cs); cs = NULL;
Radek Krejcia9026eb2018-12-12 16:04:47 +01001365
1366 TEST_DUP("description", "text1", "text2");
1367 TEST_DUP("reference", "1", "2");
1368 TEST_DUP("status", "current", "obsolete");
1369 TEST_DUP("when", "true", "false");
1370#undef TEST_DUP
1371
1372 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001373 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 +01001374 "leaf-list ll {type string;} list li;reference test;status current;uses grp;when true;m:ext;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001375 assert_int_equal(LY_SUCCESS, parse_case(YCTX, NULL, (struct lysp_node **)&cs));
Radek Iša56ca9e42020-09-08 18:42:00 +02001376 CHECK_LYSP_NODE(cs, "test", 1, LYS_STATUS_CURR, 1, "cs", 0, LYS_CASE, 0, "test", 1);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001377 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)cs); cs = NULL;
Radek Krejcia9026eb2018-12-12 16:04:47 +01001378
1379 /* invalid content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001380 in.current = "cs {config true} ...";
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("Invalid keyword \"config\" as a child of \"case\".", "Line number 1.");
Michal Vasko8a67eff2021-12-07 14:04:47 +01001383 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)cs); cs = NULL;
Radek Krejci056d0a82018-12-06 16:57:25 +01001384}
1385
Radek Krejci9800fb82018-12-13 14:26:23 +01001386static void
Radek Krejcid6b76452019-09-03 17:03:03 +02001387test_any(void **state, enum ly_stmt kw)
Radek Krejci9800fb82018-12-13 14:26:23 +01001388{
Radek Krejci9800fb82018-12-13 14:26:23 +01001389 struct lysp_node_anydata *any = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001390
Radek Krejcid6b76452019-09-03 17:03:03 +02001391 if (kw == LY_STMT_ANYDATA) {
Michal Vasko8a67eff2021-12-07 14:04:47 +01001392 PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
Radek Krejci9800fb82018-12-13 14:26:23 +01001393 } else {
Michal Vasko8a67eff2021-12-07 14:04:47 +01001394 PARSER_CUR_PMOD(YCTX)->version = 1; /* simulate YANG 1.0 */
Radek Krejci9800fb82018-12-13 14:26:23 +01001395 }
1396
1397 /* invalid cardinality */
1398#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001399 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001400 assert_int_equal(LY_EVALID, parse_any(YCTX, kw, NULL, (struct lysp_node**)&any)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001401 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Michal Vasko8a67eff2021-12-07 14:04:47 +01001402 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node*)any); any = NULL;
Radek Krejci9800fb82018-12-13 14:26:23 +01001403
1404 TEST_DUP("config", "true", "false");
1405 TEST_DUP("description", "text1", "text2");
1406 TEST_DUP("mandatory", "true", "false");
1407 TEST_DUP("reference", "1", "2");
1408 TEST_DUP("status", "current", "obsolete");
1409 TEST_DUP("when", "true", "false");
1410#undef TEST_DUP
1411
1412 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001413 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 +01001414 assert_int_equal(LY_SUCCESS, parse_any(YCTX, kw, NULL, (struct lysp_node **)&any));
Radek Iša56ca9e42020-09-08 18:42:00 +02001415 // CHECK_LYSP_NODE(NODE, DSC, EXTS, FLAGS, IFFEATURES, NAME, NEXT, TYPE, PARENT, REF, WHEN)
1416 uint16_t node_type = kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
1417 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 +01001418 assert_non_null(any->musts);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001419 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)any); any = NULL;
Radek Krejci9800fb82018-12-13 14:26:23 +01001420}
1421
1422static void
1423test_anydata(void **state)
1424{
Michal Vaskoa0a498b2021-09-22 12:17:48 +02001425 test_any(state, LY_STMT_ANYDATA);
Radek Krejci9800fb82018-12-13 14:26:23 +01001426}
1427
1428static void
1429test_anyxml(void **state)
1430{
Michal Vaskoa0a498b2021-09-22 12:17:48 +02001431 test_any(state, LY_STMT_ANYXML);
Radek Krejci9800fb82018-12-13 14:26:23 +01001432}
1433
Radek Krejcie86bf772018-12-14 11:39:53 +01001434static void
1435test_grouping(void **state)
1436{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001437 struct lysp_node_grp *grp = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001438
Michal Vasko8a67eff2021-12-07 14:04:47 +01001439 PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
aPiecek8d4e75d2021-06-24 14:47:06 +02001440 YCTX->main_ctx = (struct lys_parser_ctx *)YCTX;
Radek Krejcie86bf772018-12-14 11:39:53 +01001441
1442 /* invalid cardinality */
1443#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001444 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001445 assert_int_equal(LY_EVALID, parse_grouping(YCTX, NULL, &grp)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001446 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Michal Vasko8a67eff2021-12-07 14:04:47 +01001447 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, &grp->node); grp = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001448
1449 TEST_DUP("description", "text1", "text2");
1450 TEST_DUP("reference", "1", "2");
1451 TEST_DUP("status", "current", "obsolete");
1452#undef TEST_DUP
1453
1454 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001455 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 +01001456 "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 +01001457 assert_int_equal(LY_SUCCESS, parse_grouping(YCTX, NULL, &grp));
Radek Krejcie86bf772018-12-14 11:39:53 +01001458 assert_non_null(grp);
1459 assert_int_equal(LYS_GROUPING, grp->nodetype);
1460 assert_string_equal("grp", grp->name);
1461 assert_string_equal("test", grp->dsc);
1462 assert_non_null(grp->exts);
1463 assert_string_equal("test", grp->ref);
1464 assert_null(grp->parent);
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001465 assert_int_equal(LYS_STATUS_CURR, grp->flags);
Radek Iša56ca9e42020-09-08 18:42:00 +02001466 ly_set_erase(&YCTX->tpdfs_nodes, NULL);
aPiecek63e080d2021-06-29 13:53:28 +02001467 ly_set_erase(&YCTX->grps_nodes, NULL);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001468 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, &grp->node);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001469 grp = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001470
1471 /* invalid content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001472 in.current = "grp {config true} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001473 assert_int_equal(LY_EVALID, parse_grouping(YCTX, NULL, &grp));
Radek Iša56ca9e42020-09-08 18:42:00 +02001474 CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"grouping\".", "Line number 1.");
Michal Vasko8a67eff2021-12-07 14:04:47 +01001475 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, &grp->node);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001476 grp = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001477
Michal Vasko63f3d842020-07-08 10:10:14 +02001478 in.current = "grp {must 'expr'} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001479 assert_int_equal(LY_EVALID, parse_grouping(YCTX, NULL, &grp));
Radek Iša56ca9e42020-09-08 18:42:00 +02001480 CHECK_LOG_CTX("Invalid keyword \"must\" as a child of \"grouping\".", "Line number 1.");
Michal Vasko8a67eff2021-12-07 14:04:47 +01001481 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, &grp->node);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001482 grp = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001483}
1484
1485static void
Radek Krejcif538ce52019-03-05 10:46:14 +01001486test_action(void **state)
1487{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001488 struct lysp_node_action *rpcs = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01001489 struct lysp_node_container *c = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001490
Michal Vasko8a67eff2021-12-07 14:04:47 +01001491 PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
aPiecek8d4e75d2021-06-24 14:47:06 +02001492 YCTX->main_ctx = (struct lys_parser_ctx *)YCTX;
Radek Krejcif538ce52019-03-05 10:46:14 +01001493
1494 /* invalid cardinality */
1495#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001496 in.current = "func {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001497 assert_int_equal(LY_EVALID, parse_action(YCTX, NULL, &rpcs)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001498 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Michal Vasko8a67eff2021-12-07 14:04:47 +01001499 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node*)rpcs); rpcs = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01001500
1501 TEST_DUP("description", "text1", "text2");
Michal Vaskob83af8a2020-01-06 09:49:22 +01001502 TEST_DUP("input", "{leaf l1 {type empty;}} description a", "{leaf l2 {type empty;}} description a");
1503 TEST_DUP("output", "{leaf l1 {type empty;}} description a", "{leaf l2 {type empty;}} description a");
Radek Krejcif538ce52019-03-05 10:46:14 +01001504 TEST_DUP("reference", "1", "2");
1505 TEST_DUP("status", "current", "obsolete");
1506#undef TEST_DUP
1507
1508 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001509 in.current = "top;";
Radek Krejci33090f92020-12-17 20:12:46 +01001510 assert_int_equal(LY_SUCCESS, parse_container(YCTX, NULL, (struct lysp_node **)&c));
Michal Vasko63f3d842020-07-08 10:10:14 +02001511 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 +01001512 "input {anydata a1; anyxml a2; choice ch; container c; grouping grp; leaf l {type int8;} leaf-list ll {type int8;}"
1513 " list li; must 1; typedef mytypei {type int8;} uses grp; m:ext;}"
1514 "output {anydata a1; anyxml a2; choice ch; container c; grouping grp; leaf l {type int8;} leaf-list ll {type int8;}"
1515 " list li; must 1; typedef mytypeo {type int8;} uses grp; m:ext;}} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001516 assert_int_equal(LY_SUCCESS, parse_action(YCTX, (struct lysp_node *)c, &rpcs));
Radek Krejcif538ce52019-03-05 10:46:14 +01001517 assert_non_null(rpcs);
1518 assert_int_equal(LYS_ACTION, rpcs->nodetype);
1519 assert_string_equal("func", rpcs->name);
1520 assert_string_equal("test", rpcs->dsc);
1521 assert_non_null(rpcs->exts);
1522 assert_non_null(rpcs->iffeatures);
1523 assert_string_equal("test", rpcs->ref);
1524 assert_non_null(rpcs->groupings);
1525 assert_non_null(rpcs->typedefs);
1526 assert_int_equal(LYS_STATUS_CURR, rpcs->flags);
1527 /* input */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001528 assert_int_equal(rpcs->input.nodetype, LYS_INPUT);
Radek Krejcif538ce52019-03-05 10:46:14 +01001529 assert_non_null(rpcs->input.groupings);
1530 assert_non_null(rpcs->input.exts);
1531 assert_non_null(rpcs->input.musts);
1532 assert_non_null(rpcs->input.typedefs);
Radek Krejci01180ac2021-01-27 08:48:22 +01001533 assert_non_null(rpcs->input.child);
Radek Krejcif538ce52019-03-05 10:46:14 +01001534 /* output */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001535 assert_int_equal(rpcs->output.nodetype, LYS_OUTPUT);
Radek Krejcif538ce52019-03-05 10:46:14 +01001536 assert_non_null(rpcs->output.groupings);
1537 assert_non_null(rpcs->output.exts);
1538 assert_non_null(rpcs->output.musts);
1539 assert_non_null(rpcs->output.typedefs);
Radek Krejci01180ac2021-01-27 08:48:22 +01001540 assert_non_null(rpcs->output.child);
Radek Krejcif538ce52019-03-05 10:46:14 +01001541
Radek Iša56ca9e42020-09-08 18:42:00 +02001542 ly_set_erase(&YCTX->tpdfs_nodes, NULL);
aPiecek63e080d2021-06-29 13:53:28 +02001543 ly_set_erase(&YCTX->grps_nodes, NULL);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001544 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)rpcs); rpcs = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01001545
1546 /* invalid content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001547 in.current = "func {config true} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001548 assert_int_equal(LY_EVALID, parse_action(YCTX, NULL, &rpcs));
Radek Iša56ca9e42020-09-08 18:42:00 +02001549 CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"rpc\".", "Line number 1.");
Michal Vasko8a67eff2021-12-07 14:04:47 +01001550 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)rpcs); rpcs = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01001551
Michal Vasko8a67eff2021-12-07 14:04:47 +01001552 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)c);
Radek Krejcif538ce52019-03-05 10:46:14 +01001553}
1554
1555static void
Radek Krejcifc11bd72019-04-11 16:00:05 +02001556test_notification(void **state)
1557{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001558 struct lysp_node_notif *notifs = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001559 struct lysp_node_container *c = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001560
Michal Vasko8a67eff2021-12-07 14:04:47 +01001561 PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
aPiecek8d4e75d2021-06-24 14:47:06 +02001562 YCTX->main_ctx = (struct lys_parser_ctx *)YCTX;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001563
1564 /* invalid cardinality */
1565#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001566 in.current = "func {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001567 assert_int_equal(LY_EVALID, parse_notif(YCTX, NULL, &notifs)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001568 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Michal Vasko8a67eff2021-12-07 14:04:47 +01001569 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node*)notifs); notifs = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001570
1571 TEST_DUP("description", "text1", "text2");
1572 TEST_DUP("reference", "1", "2");
1573 TEST_DUP("status", "current", "obsolete");
1574#undef TEST_DUP
1575
1576 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001577 in.current = "top;";
Radek Krejci33090f92020-12-17 20:12:46 +01001578 assert_int_equal(LY_SUCCESS, parse_container(YCTX, NULL, (struct lysp_node **)&c));
Michal Vasko63f3d842020-07-08 10:10:14 +02001579 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 +01001580 "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 +01001581 assert_int_equal(LY_SUCCESS, parse_notif(YCTX, (struct lysp_node *)c, &notifs));
Radek Krejcifc11bd72019-04-11 16:00:05 +02001582 assert_non_null(notifs);
1583 assert_int_equal(LYS_NOTIF, notifs->nodetype);
1584 assert_string_equal("ntf", notifs->name);
1585 assert_string_equal("test", notifs->dsc);
1586 assert_non_null(notifs->exts);
1587 assert_non_null(notifs->iffeatures);
1588 assert_string_equal("test", notifs->ref);
1589 assert_non_null(notifs->groupings);
1590 assert_non_null(notifs->typedefs);
1591 assert_non_null(notifs->musts);
Radek Krejci01180ac2021-01-27 08:48:22 +01001592 assert_non_null(notifs->child);
Radek Krejcifc11bd72019-04-11 16:00:05 +02001593 assert_int_equal(LYS_STATUS_CURR, notifs->flags);
1594
Radek Iša56ca9e42020-09-08 18:42:00 +02001595 ly_set_erase(&YCTX->tpdfs_nodes, NULL);
aPiecek63e080d2021-06-29 13:53:28 +02001596 ly_set_erase(&YCTX->grps_nodes, NULL);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001597 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)notifs); notifs = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001598
1599 /* invalid content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001600 in.current = "ntf {config true} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001601 assert_int_equal(LY_EVALID, parse_notif(YCTX, NULL, &notifs));
Radek Iša56ca9e42020-09-08 18:42:00 +02001602 CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"notification\".", "Line number 1.");
Michal Vasko8a67eff2021-12-07 14:04:47 +01001603 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)notifs); notifs = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001604
Michal Vasko8a67eff2021-12-07 14:04:47 +01001605 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)c);
Radek Krejcifc11bd72019-04-11 16:00:05 +02001606}
1607
1608static void
Radek Krejcie86bf772018-12-14 11:39:53 +01001609test_uses(void **state)
1610{
Radek Krejcie86bf772018-12-14 11:39:53 +01001611 struct lysp_node_uses *u = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001612
Michal Vasko8a67eff2021-12-07 14:04:47 +01001613 PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
Radek Krejcie86bf772018-12-14 11:39:53 +01001614
1615 /* invalid cardinality */
1616#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001617 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001618 assert_int_equal(LY_EVALID, parse_uses(YCTX, NULL, (struct lysp_node**)&u)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001619 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Michal Vasko8a67eff2021-12-07 14:04:47 +01001620 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node*)u); u = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001621
1622 TEST_DUP("description", "text1", "text2");
1623 TEST_DUP("reference", "1", "2");
1624 TEST_DUP("status", "current", "obsolete");
1625 TEST_DUP("when", "true", "false");
1626#undef TEST_DUP
1627
1628 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001629 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 +01001630 assert_int_equal(LY_SUCCESS, parse_uses(YCTX, NULL, (struct lysp_node **)&u));
Radek Iša56ca9e42020-09-08 18:42:00 +02001631 CHECK_LYSP_NODE(u, "test", 1, LYS_STATUS_CURR, 1, "grpref", 0, LYS_USES, 0, "test", 1);
Radek Krejcie86bf772018-12-14 11:39:53 +01001632 assert_non_null(u->augments);
1633 assert_non_null(u->refines);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001634 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)u); u = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001635}
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001636
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001637static void
1638test_augment(void **state)
1639{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001640 struct lysp_node_augment *a = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001641
Michal Vasko8a67eff2021-12-07 14:04:47 +01001642 PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001643
1644 /* invalid cardinality */
1645#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001646 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001647 assert_int_equal(LY_EVALID, parse_augment(YCTX, NULL, &a)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001648 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Michal Vasko8a67eff2021-12-07 14:04:47 +01001649 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)a); a = NULL;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001650
1651 TEST_DUP("description", "text1", "text2");
1652 TEST_DUP("reference", "1", "2");
1653 TEST_DUP("status", "current", "obsolete");
1654 TEST_DUP("when", "true", "false");
1655#undef TEST_DUP
1656
1657 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001658 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 +01001659 "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 +01001660 assert_int_equal(LY_SUCCESS, parse_augment(YCTX, NULL, &a));
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001661 assert_non_null(a);
1662 assert_int_equal(LYS_AUGMENT, a->nodetype);
1663 assert_string_equal("/target/nodeid", a->nodeid);
1664 assert_string_equal("test", a->dsc);
1665 assert_non_null(a->exts);
1666 assert_non_null(a->iffeatures);
1667 assert_string_equal("test", a->ref);
1668 assert_non_null(a->when);
1669 assert_null(a->parent);
1670 assert_int_equal(LYS_STATUS_CURR, a->flags);
Michal Vasko8a67eff2021-12-07 14:04:47 +01001671 lysp_node_free(PARSER_CUR_PMOD(YCTX)->mod->ctx, (struct lysp_node *)a); a = NULL;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001672}
1673
Radek Krejcif09e4e82019-06-14 15:08:11 +02001674static void
1675test_when(void **state)
1676{
Radek Krejcif09e4e82019-06-14 15:08:11 +02001677 struct lysp_when *w = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001678
Michal Vasko8a67eff2021-12-07 14:04:47 +01001679 PARSER_CUR_PMOD(YCTX)->version = 2; /* simulate YANG 1.1 */
Radek Krejcif09e4e82019-06-14 15:08:11 +02001680
Michal Vasko12ef5362022-09-16 15:13:58 +02001681 in.current = "l { description text1;description text2;} ...";
1682 assert_int_equal(LY_EVALID, parse_when(YCTX, &w));
1683 assert_null(w);
1684 CHECK_LOG_CTX("Duplicate keyword \"description\".", "Line number 1.");
Radek Krejcif09e4e82019-06-14 15:08:11 +02001685
Michal Vasko12ef5362022-09-16 15:13:58 +02001686 in.current = "l { reference 1;reference 2;} ...";
1687 assert_int_equal(LY_EVALID, parse_when(YCTX, &w));
1688 assert_null(w);
1689 CHECK_LOG_CTX("Duplicate keyword \"reference\".", "Line number 1.");
Radek Krejcif09e4e82019-06-14 15:08:11 +02001690}
1691
David Sedlákd6ce6d72019-07-16 17:30:18 +02001692static void
1693test_value(void **state)
1694{
David Sedlákd6ce6d72019-07-16 17:30:18 +02001695 int64_t val = 0;
1696 uint16_t flags = 0;
1697
Michal Vasko63f3d842020-07-08 10:10:14 +02001698 in.current = "-0;";
Radek Krejci33090f92020-12-17 20:12:46 +01001699 assert_int_equal(parse_type_enum_value_pos(YCTX, LY_STMT_VALUE, &val, &flags, NULL), LY_SUCCESS);
David Sedlákd6ce6d72019-07-16 17:30:18 +02001700 assert_int_equal(val, 0);
1701
Michal Vasko63f3d842020-07-08 10:10:14 +02001702 in.current = "-0;";
David Sedlákd6ce6d72019-07-16 17:30:18 +02001703 flags = 0;
Radek Krejci33090f92020-12-17 20:12:46 +01001704 assert_int_equal(parse_type_enum_value_pos(YCTX, LY_STMT_POSITION, &val, &flags, NULL), LY_EVALID);
Radek Iša56ca9e42020-09-08 18:42:00 +02001705 CHECK_LOG_CTX("Invalid value \"-0\" of \"position\".", "Line number 1.");
David Sedlákd6ce6d72019-07-16 17:30:18 +02001706}
1707
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001708int
1709main(void)
Radek Krejci80dd33e2018-09-26 15:57:18 +02001710{
1711 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +02001712 UTEST(test_helpers, setup, teardown),
1713 UTEST(test_comments, setup, teardown),
1714 UTEST(test_arg, setup, teardown),
1715 UTEST(test_stmts, setup, teardown),
1716 UTEST(test_minmax, setup, teardown),
Michal Vasko12ef5362022-09-16 15:13:58 +02001717 UTEST(test_valid_module, setup, teardown),
Radek Iša56ca9e42020-09-08 18:42:00 +02001718 UTEST(test_module, setup, teardown),
1719 UTEST(test_deviation, setup, teardown),
1720 UTEST(test_deviate, setup, teardown),
1721 UTEST(test_container, setup, teardown),
1722 UTEST(test_leaf, setup, teardown),
1723 UTEST(test_leaflist, setup, teardown),
1724 UTEST(test_list, setup, teardown),
1725 UTEST(test_choice, setup, teardown),
1726 UTEST(test_case, setup, teardown),
1727 UTEST(test_anydata, setup, teardown),
1728 UTEST(test_anyxml, setup, teardown),
1729 UTEST(test_action, setup, teardown),
1730 UTEST(test_notification, setup, teardown),
1731 UTEST(test_grouping, setup, teardown),
1732 UTEST(test_uses, setup, teardown),
1733 UTEST(test_augment, setup, teardown),
1734 UTEST(test_when, setup, teardown),
1735 UTEST(test_value, setup, teardown),
Radek Krejci80dd33e2018-09-26 15:57:18 +02001736 };
1737
1738 return cmocka_run_group_tests(tests, NULL, NULL);
1739}