blob: 5f29c27c4217c1eeb556d5c5ddecd9ae57a235da [file] [log] [blame]
Radek Krejci80dd33e2018-09-26 15:57:18 +02001/*
2 * @file test_parser_yang.c
3 * @author: Radek Krejci <rkrejci@cesnet.cz>
4 * @brief unit tests for functions from parser_yang.c
5 *
Radek Iša56ca9e42020-09-08 18:42:00 +02006 * Copyright (c) 2018-2020 CESNET, z.s.p.o.
Radek Krejci80dd33e2018-09-26 15:57:18 +02007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Radek Iša56ca9e42020-09-08 18:42:00 +020014#define _UTEST_MAIN_
15#include "utests.h"
Radek Krejci80dd33e2018-09-26 15:57:18 +020016
Radek Krejci80dd33e2018-09-26 15:57:18 +020017#include <stdio.h>
18#include <string.h>
19
Radek Krejci70593c12020-06-13 20:48:09 +020020#include "common.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020021#include "in_internal.h"
Radek Krejci70593c12020-06-13 20:48:09 +020022#include "parser_internal.h"
Michal Vasko405cc9e2020-12-01 12:01:27 +010023#include "schema_compile.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010024#include "tree_edit.h"
Radek Krejci70593c12020-06-13 20:48:09 +020025#include "tree_schema.h"
26#include "tree_schema_internal.h"
Radek Krejci2d7a47b2019-05-16 13:34:10 +020027
28/* originally static functions from tree_schema_free.c and parser_yang.c */
29void lysp_ext_instance_free(struct ly_ctx *ctx, struct lysp_ext_instance *ext);
Radek Krejci2d7a47b2019-05-16 13:34:10 +020030void lysp_deviation_free(struct ly_ctx *ctx, struct lysp_deviation *dev);
Radek Krejci2a9fc652021-01-22 17:44:34 +010031void lysp_grp_free(struct ly_ctx *ctx, struct lysp_node_grp *grp);
32void lysp_augment_free(struct ly_ctx *ctx, struct lysp_node_augment *augment);
Radek Krejci2d7a47b2019-05-16 13:34:10 +020033void lysp_deviate_free(struct ly_ctx *ctx, struct lysp_deviate *d);
34void lysp_node_free(struct ly_ctx *ctx, struct lysp_node *node);
Radek Krejcif09e4e82019-06-14 15:08:11 +020035void lysp_when_free(struct ly_ctx *ctx, struct lysp_when *when);
Radek Krejci2d7a47b2019-05-16 13:34:10 +020036
Michal Vasko63f3d842020-07-08 10:10:14 +020037LY_ERR buf_add_char(struct ly_ctx *ctx, struct ly_in *in, size_t len, char **buf, size_t *buf_len, size_t *buf_used);
Radek Krejci33090f92020-12-17 20:12:46 +010038LY_ERR buf_store_char(struct lys_yang_parser_ctx *ctx, enum yang_arg arg, char **word_p,
Radek Krejcib4ac5a92020-11-23 17:54:33 +010039 size_t *word_len, char **word_b, size_t *buf_len, uint8_t need_buf, uint8_t *prefix);
Radek Krejci33090f92020-12-17 20:12:46 +010040LY_ERR get_keyword(struct lys_yang_parser_ctx *ctx, enum ly_stmt *kw, char **word_p, size_t *word_len);
41LY_ERR get_argument(struct lys_yang_parser_ctx *ctx, enum yang_arg arg,
Radek Krejcib4ac5a92020-11-23 17:54:33 +010042 uint16_t *flags, char **word_p, char **word_b, size_t *word_len);
Radek Krejci33090f92020-12-17 20:12:46 +010043LY_ERR skip_comment(struct lys_yang_parser_ctx *ctx, uint8_t comment);
Radek Krejci2d7a47b2019-05-16 13:34:10 +020044
Radek Krejci2a9fc652021-01-22 17:44:34 +010045LY_ERR parse_action(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_action **actions);
Radek Krejci33090f92020-12-17 20:12:46 +010046LY_ERR parse_any(struct lys_yang_parser_ctx *ctx, enum ly_stmt kw, struct lysp_node *parent, struct lysp_node **siblings);
Radek Krejci2a9fc652021-01-22 17:44:34 +010047LY_ERR parse_augment(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_augment **augments);
Radek Krejci33090f92020-12-17 20:12:46 +010048LY_ERR parse_case(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
49LY_ERR parse_container(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
50LY_ERR parse_deviate(struct lys_yang_parser_ctx *ctx, struct lysp_deviate **deviates);
51LY_ERR parse_deviation(struct lys_yang_parser_ctx *ctx, struct lysp_deviation **deviations);
Radek Krejci2a9fc652021-01-22 17:44:34 +010052LY_ERR parse_grouping(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_grp **groupings);
Radek Krejci33090f92020-12-17 20:12:46 +010053LY_ERR parse_choice(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
54LY_ERR parse_leaf(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
55LY_ERR parse_leaflist(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
56LY_ERR parse_list(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
57LY_ERR parse_maxelements(struct lys_yang_parser_ctx *ctx, uint32_t *max, uint16_t *flags, struct lysp_ext_instance **exts);
58LY_ERR parse_minelements(struct lys_yang_parser_ctx *ctx, uint32_t *min, uint16_t *flags, struct lysp_ext_instance **exts);
59LY_ERR parse_module(struct lys_yang_parser_ctx *ctx, struct lysp_module *mod);
Radek Krejci2a9fc652021-01-22 17:44:34 +010060LY_ERR parse_notif(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node_notif **notifs);
Radek Krejci33090f92020-12-17 20:12:46 +010061LY_ERR parse_submodule(struct lys_yang_parser_ctx *ctx, struct lysp_submodule *submod);
62LY_ERR parse_uses(struct lys_yang_parser_ctx *ctx, struct lysp_node *parent, struct lysp_node **siblings);
63LY_ERR parse_when(struct lys_yang_parser_ctx *ctx, struct lysp_when **when_p);
64LY_ERR parse_type_enum_value_pos(struct lys_yang_parser_ctx *ctx, enum ly_stmt val_kw, int64_t *value, uint16_t *flags, struct lysp_ext_instance **exts);
Radek Krejci80dd33e2018-09-26 15:57:18 +020065
Radek Iša56ca9e42020-09-08 18:42:00 +020066struct lys_yang_parser_ctx *YCTX;
Radek Krejci80dd33e2018-09-26 15:57:18 +020067
Radek Krejci1640e802021-01-08 11:01:30 +010068struct ly_in in = {0};
69
Radek Krejci33090f92020-12-17 20:12:46 +010070#define YCTX_INIT \
Radek Krejcid54412f2020-12-17 20:25:35 +010071 in.line = 1; \
Radek Krejci2efc45b2020-12-22 16:25:44 +010072 YCTX->in = &in; \
73 LOG_LOCINIT(UTEST_LYCTX, NULL, NULL, NULL, &in)
Radek Krejci33090f92020-12-17 20:12:46 +010074
Michal Vasko5d24f6c2020-10-13 13:49:06 +020075static int
Radek Iša56ca9e42020-09-08 18:42:00 +020076setup(void **state)
Michal Vasko5d24f6c2020-10-13 13:49:06 +020077{
Radek Iša56ca9e42020-09-08 18:42:00 +020078 UTEST_SETUP;
Michal Vasko5d24f6c2020-10-13 13:49:06 +020079
Radek Iša56ca9e42020-09-08 18:42:00 +020080 /* allocate parser context */
81 YCTX = calloc(1, sizeof(*YCTX));
82 YCTX->format = LYS_IN_YANG;
Michal Vasko5d24f6c2020-10-13 13:49:06 +020083
Radek Iša56ca9e42020-09-08 18:42:00 +020084 /* allocate new parsed module */
85 YCTX->parsed_mod = calloc(1, sizeof *YCTX->parsed_mod);
Michal Vasko5d24f6c2020-10-13 13:49:06 +020086
Radek Iša56ca9e42020-09-08 18:42:00 +020087 /* allocate new module */
88 YCTX->parsed_mod->mod = calloc(1, sizeof *YCTX->parsed_mod->mod);
89 YCTX->parsed_mod->mod->ctx = UTEST_LYCTX;
90 YCTX->parsed_mod->mod->parsed = YCTX->parsed_mod;
Michal Vasko5d24f6c2020-10-13 13:49:06 +020091
Radek Krejci1640e802021-01-08 11:01:30 +010092 /* initilize and use the global easily available and customizable input handler */
93 in.line = 1;
94 YCTX->in = &in;
Radek Krejciddace2c2021-01-08 11:30:56 +010095 LOG_LOCINIT(NULL, NULL, NULL, &in);
Radek Krejci1640e802021-01-08 11:01:30 +010096
Michal Vasko5d24f6c2020-10-13 13:49:06 +020097 return 0;
98}
99
100static int
Radek Iša56ca9e42020-09-08 18:42:00 +0200101teardown(void **state)
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200102{
Radek Krejci90ed21e2021-04-12 14:47:46 +0200103 lys_module_free(YCTX->parsed_mod->mod);
Radek Krejciddace2c2021-01-08 11:30:56 +0100104 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejci1640e802021-01-08 11:01:30 +0100105
Radek Iša56ca9e42020-09-08 18:42:00 +0200106 free(YCTX);
107 YCTX = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200108
Radek Iša56ca9e42020-09-08 18:42:00 +0200109 UTEST_TEARDOWN;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200110
111 return 0;
112}
113
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200114#define TEST_DUP_GENERIC(PREFIX, MEMBER, VALUE1, VALUE2, FUNC, RESULT, LINE, CLEANUP) \
Michal Vasko63f3d842020-07-08 10:10:14 +0200115 in.current = PREFIX MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +0100116 assert_int_equal(LY_EVALID, FUNC(YCTX, RESULT)); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200117 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number "LINE".");\
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200118 CLEANUP
Radek Krejci44ceedc2018-10-02 15:54:31 +0200119static void
120test_helpers(void **state)
121{
Radek Krejci404251e2018-10-09 12:06:44 +0200122 char *buf, *p;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200123 size_t len, size;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200124 uint8_t prefix = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200125
126 /* storing into buffer */
Michal Vasko63f3d842020-07-08 10:10:14 +0200127 in.current = "abcd";
Radek Krejci44ceedc2018-10-02 15:54:31 +0200128 buf = NULL;
129 size = len = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200130 assert_int_equal(LY_SUCCESS, buf_add_char(NULL, &in, 2, &buf, &size, &len));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200131 assert_int_not_equal(0, size);
132 assert_int_equal(2, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200133 assert_string_equal("cd", in.current);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200134 assert_false(strncmp("ab", buf, 2));
135 free(buf);
Radek Krejci404251e2018-10-09 12:06:44 +0200136 buf = NULL;
137
138 /* invalid first characters */
139 len = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200140 in.current = "2invalid";
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));
Michal Vasko63f3d842020-07-08 10:10:14 +0200144 in.current = "-invalid";
Radek Krejci33090f92020-12-17 20:12:46 +0100145 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 +0200146 /* invalid following characters */
147 len = 3; /* number of characters read before the str content */
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));
Michal Vasko63f3d842020-07-08 10:10:14 +0200150 in.current = ":";
Radek Krejci33090f92020-12-17 20:12:46 +0100151 assert_int_equal(LY_EVALID, buf_store_char(YCTX, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
Radek Krejci404251e2018-10-09 12:06:44 +0200152 /* valid colon for prefixed identifiers */
153 len = size = 0;
154 p = NULL;
David Sedlák40bb13b2019-07-10 14:34:18 +0200155 prefix = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200156 in.current = "x:id";
Radek Krejci33090f92020-12-17 20:12:46 +0100157 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 +0200158 assert_int_equal(1, len);
159 assert_null(buf);
Michal Vasko63f3d842020-07-08 10:10:14 +0200160 assert_string_equal(":id", in.current);
Radek Krejci404251e2018-10-09 12:06:44 +0200161 assert_int_equal('x', p[len - 1]);
Radek Krejci33090f92020-12-17 20:12:46 +0100162 assert_int_equal(LY_SUCCESS, buf_store_char(YCTX, Y_PREF_IDENTIF_ARG, &p, &len, &buf, &size, 1, &prefix));
Radek Krejci404251e2018-10-09 12:06:44 +0200163 assert_int_equal(2, len);
Michal Vasko63f3d842020-07-08 10:10:14 +0200164 assert_string_equal("id", in.current);
Radek Krejci404251e2018-10-09 12:06:44 +0200165 assert_int_equal(':', p[len - 1]);
166 free(buf);
David Sedlák40bb13b2019-07-10 14:34:18 +0200167 prefix = 0;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200168
169 /* checking identifiers */
Radek Iša56ca9e42020-09-08 18:42:00 +0200170 assert_int_equal(LY_EVALID, lysp_check_identifierchar((struct lys_parser_ctx *)YCTX, ':', 0, NULL));
171 CHECK_LOG_CTX("Invalid identifier character ':' (0x003a).", "Line number 1.");
172 assert_int_equal(LY_EVALID, lysp_check_identifierchar((struct lys_parser_ctx *)YCTX, '#', 1, NULL));
173 CHECK_LOG_CTX("Invalid identifier first character '#' (0x0023).", "Line number 1.");
Radek Krejci44ceedc2018-10-02 15:54:31 +0200174
Radek Iša56ca9e42020-09-08 18:42:00 +0200175 assert_int_equal(LY_SUCCESS, lysp_check_identifierchar((struct lys_parser_ctx *)YCTX, 'a', 1, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200176 assert_int_equal(0, prefix);
Radek Iša56ca9e42020-09-08 18:42:00 +0200177 assert_int_equal(LY_SUCCESS, lysp_check_identifierchar((struct lys_parser_ctx *)YCTX, ':', 0, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200178 assert_int_equal(1, prefix);
Radek Iša56ca9e42020-09-08 18:42:00 +0200179 assert_int_equal(LY_EVALID, lysp_check_identifierchar((struct lys_parser_ctx *)YCTX, ':', 0, &prefix));
Radek Krejcidcc7b322018-10-11 14:24:02 +0200180 assert_int_equal(1, prefix);
Radek Iša56ca9e42020-09-08 18:42:00 +0200181 assert_int_equal(LY_SUCCESS, lysp_check_identifierchar((struct lys_parser_ctx *)YCTX, 'b', 0, &prefix));
Radek Krejci44ceedc2018-10-02 15:54:31 +0200182 assert_int_equal(2, prefix);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200183 /* second colon is invalid */
Radek Iša56ca9e42020-09-08 18:42:00 +0200184 assert_int_equal(LY_EVALID, lysp_check_identifierchar((struct lys_parser_ctx *)YCTX, ':', 0, &prefix));
185 CHECK_LOG_CTX("Invalid identifier character ':' (0x003a).", "Line number 1.");
Radek Krejci44ceedc2018-10-02 15:54:31 +0200186}
Radek Krejci80dd33e2018-09-26 15:57:18 +0200187
Radek Krejcidd713ce2021-01-04 23:12:12 +0100188#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 +0200189 {\
Radek Krejcidd713ce2021-01-04 23:12:12 +0100190 const char * text = INPUT_TEXT;\
191 in.line = 1;\
Radek Iša56ca9e42020-09-08 18:42:00 +0200192 in.current = text;\
Radek Krejci33090f92020-12-17 20:12:46 +0100193 assert_int_equal(LY_SUCCESS, get_argument(CTX, Y_MAYBE_STR_ARG, NULL, &word, &buf, &len));\
Radek Iša56ca9e42020-09-08 18:42:00 +0200194 assert_string_equal(word, EXPECT_WORD);\
195 assert_int_equal(len, EXPECT_LEN);\
196 assert_string_equal(EXPECT_CURRENT, in.current);\
Radek Krejcidd713ce2021-01-04 23:12:12 +0100197 assert_int_equal(EXPECT_LINE, in.line);\
Radek Iša56ca9e42020-09-08 18:42:00 +0200198 }
199
Radek Krejci80dd33e2018-09-26 15:57:18 +0200200static void
201test_comments(void **state)
202{
Radek Krejciefd22f62018-09-27 11:47:58 +0200203 char *word, *buf;
204 size_t len;
Radek Krejci80dd33e2018-09-26 15:57:18 +0200205
Radek Krejcidd713ce2021-01-04 23:12:12 +0100206 TEST_GET_ARGUMENT_SUCCESS(" // this is a text of / one * line */ comment\nargument;",
207 YCTX, Y_STR_ARG, "argument;", 8, ";", 2);
Radek Krejciefd22f62018-09-27 11:47:58 +0200208 assert_null(buf);
Radek Krejci80dd33e2018-09-26 15:57:18 +0200209
Radek Krejcidd713ce2021-01-04 23:12:12 +0100210 TEST_GET_ARGUMENT_SUCCESS("/* this is a \n * text // of / block * comment */\"arg\" + \"ume\" \n + \n \"nt\";",
211 YCTX, Y_STR_ARG, "argument", 8, ";", 4);
Radek Krejciefd22f62018-09-27 11:47:58 +0200212 assert_ptr_equal(buf, word);
Radek Krejciefd22f62018-09-27 11:47:58 +0200213 free(word);
Radek Krejci80dd33e2018-09-26 15:57:18 +0200214
Radek Krejcidd713ce2021-01-04 23:12:12 +0100215 in.line = 1;
Michal Vasko63f3d842020-07-08 10:10:14 +0200216 in.current = " this is one line comment on last line";
Radek Krejci33090f92020-12-17 20:12:46 +0100217 assert_int_equal(LY_SUCCESS, skip_comment(YCTX, 1));
Michal Vasko63f3d842020-07-08 10:10:14 +0200218 assert_true(in.current[0] == '\0');
Radek Krejci80dd33e2018-09-26 15:57:18 +0200219
Radek Krejcidd713ce2021-01-04 23:12:12 +0100220 in.line = 1;
Michal Vasko63f3d842020-07-08 10:10:14 +0200221 in.current = " this is a not terminated comment x";
Radek Krejci33090f92020-12-17 20:12:46 +0100222 assert_int_equal(LY_EVALID, skip_comment(YCTX, 2));
Radek Krejcidd713ce2021-01-04 23:12:12 +0100223 CHECK_LOG_CTX("Unexpected end-of-input, non-terminated comment.", "Line number 1.");
Michal Vasko63f3d842020-07-08 10:10:14 +0200224 assert_true(in.current[0] == '\0');
Radek Krejci80dd33e2018-09-26 15:57:18 +0200225}
226
Radek Krejciefd22f62018-09-27 11:47:58 +0200227static void
228test_arg(void **state)
229{
Radek Krejciefd22f62018-09-27 11:47:58 +0200230 char *word, *buf;
231 size_t len;
232
233 /* missing argument */
Michal Vasko63f3d842020-07-08 10:10:14 +0200234 in.current = ";";
Radek Krejci33090f92020-12-17 20:12:46 +0100235 assert_int_equal(LY_SUCCESS, get_argument(YCTX, Y_MAYBE_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200236 assert_null(word);
237
Michal Vasko63f3d842020-07-08 10:10:14 +0200238 in.current = "{";
Radek Krejci33090f92020-12-17 20:12:46 +0100239 assert_int_equal(LY_EVALID, get_argument(YCTX, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200240 CHECK_LOG_CTX("Invalid character sequence \"{\", expected an argument.", "Line number 1.");
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200241
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200242 /* invalid escape sequence */
Michal Vasko63f3d842020-07-08 10:10:14 +0200243 in.current = "\"\\s\"";
Radek Krejci33090f92020-12-17 20:12:46 +0100244 assert_int_equal(LY_EVALID, get_argument(YCTX, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200245 CHECK_LOG_CTX("Double-quoted string unknown special character \'\\s\'.", "Line number 1.");
246
Radek Krejcidd713ce2021-01-04 23:12:12 +0100247 TEST_GET_ARGUMENT_SUCCESS("\'\\s\'", YCTX, Y_STR_ARG, "\\s\'", 2, "", 1);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200248
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200249 /* invalid character after the argument */
Michal Vasko63f3d842020-07-08 10:10:14 +0200250 in.current = "hello\"";
Radek Krejci33090f92020-12-17 20:12:46 +0100251 assert_int_equal(LY_EVALID, get_argument(YCTX, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200252 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 +0200253
Radek Iša56ca9e42020-09-08 18:42:00 +0200254 in.current = "hello}";
Radek Krejci33090f92020-12-17 20:12:46 +0100255 assert_int_equal(LY_EVALID, get_argument(YCTX, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200256 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 +0200257 /* invalid identifier-ref-arg-str */
Michal Vasko63f3d842020-07-08 10:10:14 +0200258 in.current = "pre:pre:value";
Radek Krejci33090f92020-12-17 20:12:46 +0100259 assert_int_equal(LY_EVALID, get_argument(YCTX, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200260 CHECK_LOG_CTX("Invalid identifier character ':' (0x003a).", "Line number 1.");
David Sedlák40bb13b2019-07-10 14:34:18 +0200261
Michal Vasko63f3d842020-07-08 10:10:14 +0200262 in.current = "\"\";"; /* empty identifier is not allowed */
Radek Krejci33090f92020-12-17 20:12:46 +0100263 assert_int_equal(LY_EVALID, get_argument(YCTX, Y_IDENTIF_ARG, NULL, &word, &buf, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200264 CHECK_LOG_CTX("Statement argument is required.", "Line number 1.");
265
Michal Vasko63f3d842020-07-08 10:10:14 +0200266 in.current = "\"\";"; /* empty reference identifier is not allowed */
Radek Krejci33090f92020-12-17 20:12:46 +0100267 assert_int_equal(LY_EVALID, get_argument(YCTX, Y_PREF_IDENTIF_ARG, NULL, &word, &buf, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200268 CHECK_LOG_CTX("Statement argument is required.", "Line number 1.");
Radek Krejci4e199f52019-05-28 09:09:28 +0200269
Radek Iša56ca9e42020-09-08 18:42:00 +0200270 /* slash is not an invalid character */
Radek Krejcidd713ce2021-01-04 23:12:12 +0100271 TEST_GET_ARGUMENT_SUCCESS("hello/x\t", YCTX, Y_STR_ARG, "hello/x\t", 7, "\t", 1);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200272 assert_null(buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200273
274 /* different quoting */
Radek Krejcidd713ce2021-01-04 23:12:12 +0100275 TEST_GET_ARGUMENT_SUCCESS("hello/x\t", YCTX, Y_STR_ARG, "hello/x\t", 7, "\t", 1);
Radek Krejciefd22f62018-09-27 11:47:58 +0200276
Radek Krejcidd713ce2021-01-04 23:12:12 +0100277 TEST_GET_ARGUMENT_SUCCESS("hello ", YCTX, Y_STR_ARG, "hello ", 5, " ", 1);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200278
Radek Krejcidd713ce2021-01-04 23:12:12 +0100279 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 +0200280
Radek Krejcidd713ce2021-01-04 23:12:12 +0100281 TEST_GET_ARGUMENT_SUCCESS("\"hello\\n\\t\\\"\\\\\";", YCTX, Y_STR_ARG, "hello\n\t\"\\", 9, ";", 1);
fredgand49fe112019-10-21 20:51:50 +0800282 free(buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200283
Radek Iša56ca9e42020-09-08 18:42:00 +0200284 YCTX->indent = 14;
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200285 /* - space and tabs before newline are stripped out
286 * - space and tabs after newline (indentation) are stripped out
287 */
Radek Krejcidd713ce2021-01-04 23:12:12 +0100288 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 +0200289 free(buf);
Radek Iša56ca9e42020-09-08 18:42:00 +0200290
291/* In contrast to previous, the backslash-escaped tabs are expanded after trimming, so they are preserved */
292 YCTX->indent = 14;
Radek Krejcidd713ce2021-01-04 23:12:12 +0100293 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 +0200294 assert_ptr_equal(word, buf);
Radek Krejciff13cd12019-10-25 15:34:24 +0200295 free(buf);
Radek Iša56ca9e42020-09-08 18:42:00 +0200296
Radek Krejciff13cd12019-10-25 15:34:24 +0200297 /* Do not handle whitespaces after backslash-escaped newline as indentation */
Radek Iša56ca9e42020-09-08 18:42:00 +0200298 YCTX->indent = 14;
Radek Krejcidd713ce2021-01-04 23:12:12 +0100299 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 +0200300 assert_ptr_equal(word, buf);
Radek Krejciff13cd12019-10-25 15:34:24 +0200301 free(buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200302
Radek Iša56ca9e42020-09-08 18:42:00 +0200303 YCTX->indent = 14;
Radek Krejcidd713ce2021-01-04 23:12:12 +0100304 TEST_GET_ARGUMENT_SUCCESS("\"hello\n \tworld!\"", YCTX, Y_STR_ARG, "hello\nworld!", 12, "", 2);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200305 assert_ptr_equal(word, buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200306 free(buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200307
Radek Krejcidd713ce2021-01-04 23:12:12 +0100308 TEST_GET_ARGUMENT_SUCCESS("\'hello\'", YCTX, Y_STR_ARG, "hello'", 5, "", 1);
Radek Krejciefd22f62018-09-27 11:47:58 +0200309
Radek Krejcidd713ce2021-01-04 23:12:12 +0100310 TEST_GET_ARGUMENT_SUCCESS("\"hel\" +\t\n\"lo\"", YCTX, Y_STR_ARG, "hello", 5, "", 2);
Radek Krejciefd22f62018-09-27 11:47:58 +0200311 assert_ptr_equal(word, buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200312 free(buf);
Radek Iša56ca9e42020-09-08 18:42:00 +0200313
Radek Krejcidd713ce2021-01-04 23:12:12 +0100314 in.line = 1;
Michal Vasko63f3d842020-07-08 10:10:14 +0200315 in.current = "\"hel\" +\t\nlo"; /* unquoted the second part */
Radek Krejci33090f92020-12-17 20:12:46 +0100316 assert_int_equal(LY_EVALID, get_argument(YCTX, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcidd713ce2021-01-04 23:12:12 +0100317 CHECK_LOG_CTX("Both string parts divided by '+' must be quoted.", "Line number 2.");
Radek Krejciefd22f62018-09-27 11:47:58 +0200318
Radek Krejcidd713ce2021-01-04 23:12:12 +0100319 TEST_GET_ARGUMENT_SUCCESS("\'he\'\t\n+ \"llo\"", YCTX, Y_STR_ARG, "hello", 5, "", 2);
Radek Krejciefd22f62018-09-27 11:47:58 +0200320 free(buf);
321
Radek Krejcidd713ce2021-01-04 23:12:12 +0100322 TEST_GET_ARGUMENT_SUCCESS(" \t\n\"he\"+\'llo\'", YCTX, Y_STR_ARG, "hello", 5, "", 2);
Radek Krejciefd22f62018-09-27 11:47:58 +0200323 free(buf);
324
Radek Krejci44ceedc2018-10-02 15:54:31 +0200325 /* missing argument */
Radek Krejcidd713ce2021-01-04 23:12:12 +0100326 in.line = 1;
Michal Vasko63f3d842020-07-08 10:10:14 +0200327 in.current = ";";
Radek Krejci33090f92020-12-17 20:12:46 +0100328 assert_int_equal(LY_EVALID, get_argument(YCTX, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcidd713ce2021-01-04 23:12:12 +0100329 CHECK_LOG_CTX("Invalid character sequence \";\", expected an argument.", "Line number 1.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200330}
331
Radek Iša56ca9e42020-09-08 18:42:00 +0200332#define TEST_STMS_SUCCESS(INPUT_TEXT, CTX, ACTION, EXPECT_WORD)\
333 in.current = INPUT_TEXT;\
Radek Krejci33090f92020-12-17 20:12:46 +0100334 assert_int_equal(LY_SUCCESS, get_keyword(CTX, &kw, &word, &len));\
Radek Iša56ca9e42020-09-08 18:42:00 +0200335 assert_int_equal(ACTION, kw);\
336 assert_int_equal(strlen(EXPECT_WORD), len);\
337 assert_true(0 == strncmp(EXPECT_WORD, word, len))
338
Radek Krejcidcc7b322018-10-11 14:24:02 +0200339static void
340test_stmts(void **state)
341{
Michal Vasko63f3d842020-07-08 10:10:14 +0200342 const char *p;
Radek Krejcid6b76452019-09-03 17:03:03 +0200343 enum ly_stmt kw;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200344 char *word;
345 size_t len;
346
Michal Vasko63f3d842020-07-08 10:10:14 +0200347 in.current = "\n// comment\n\tinput\t{";
Radek Krejci33090f92020-12-17 20:12:46 +0100348 assert_int_equal(LY_SUCCESS, get_keyword(YCTX, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200349 assert_int_equal(LY_STMT_INPUT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200350 assert_int_equal(5, len);
351 assert_string_equal("input\t{", word);
Michal Vasko63f3d842020-07-08 10:10:14 +0200352 assert_string_equal("\t{", in.current);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200353
Michal Vasko63f3d842020-07-08 10:10:14 +0200354 in.current = "\t /* comment */\t output\n\t{";
Radek Krejci33090f92020-12-17 20:12:46 +0100355 assert_int_equal(LY_SUCCESS, get_keyword(YCTX, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200356 assert_int_equal(LY_STMT_OUTPUT, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200357 assert_int_equal(6, len);
358 assert_string_equal("output\n\t{", word);
Michal Vasko63f3d842020-07-08 10:10:14 +0200359 assert_string_equal("\n\t{", in.current);
Radek Krejci33090f92020-12-17 20:12:46 +0100360 assert_int_equal(LY_SUCCESS, get_keyword(YCTX, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200361 assert_int_equal(LY_STMT_SYNTAX_LEFT_BRACE, kw);
Radek Krejciabdd8062019-06-11 16:44:19 +0200362 assert_int_equal(1, len);
363 assert_string_equal("{", word);
Michal Vasko63f3d842020-07-08 10:10:14 +0200364 assert_string_equal("", in.current);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200365
Michal Vasko63f3d842020-07-08 10:10:14 +0200366 in.current = "/input { "; /* invalid slash */
Radek Krejci33090f92020-12-17 20:12:46 +0100367 assert_int_equal(LY_EVALID, get_keyword(YCTX, &kw, &word, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200368 CHECK_LOG_CTX("Invalid identifier first character '/'.", "Line number 4.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200369
Michal Vasko63f3d842020-07-08 10:10:14 +0200370 in.current = "not-a-statement-nor-extension { "; /* invalid identifier */
Radek Krejci33090f92020-12-17 20:12:46 +0100371 assert_int_equal(LY_EVALID, get_keyword(YCTX, &kw, &word, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200372 CHECK_LOG_CTX("Invalid character sequence \"not-a-statement-nor-extension\", expected a keyword.", "Line number 4.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200373
Michal Vasko63f3d842020-07-08 10:10:14 +0200374 in.current = "path;"; /* missing sep after the keyword */
Radek Krejci33090f92020-12-17 20:12:46 +0100375 assert_int_equal(LY_EVALID, get_keyword(YCTX, &kw, &word, &len));
Radek Iša56ca9e42020-09-08 18:42:00 +0200376 CHECK_LOG_CTX("Invalid character sequence \"path;\", expected a keyword followed by a separator.", "Line number 4.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200377
Radek Iša56ca9e42020-09-08 18:42:00 +0200378 TEST_STMS_SUCCESS("action ", YCTX, LY_STMT_ACTION, "action");
379
380 TEST_STMS_SUCCESS("anydata ", YCTX, LY_STMT_ANYDATA, "anydata");
381 TEST_STMS_SUCCESS("anyxml ", YCTX, LY_STMT_ANYXML, "anyxml");
382 TEST_STMS_SUCCESS("argument ", YCTX, LY_STMT_ARGUMENT, "argument");
383 TEST_STMS_SUCCESS("augment ", YCTX, LY_STMT_AUGMENT, "augment");
384 TEST_STMS_SUCCESS("base ", YCTX, LY_STMT_BASE, "base");
385 TEST_STMS_SUCCESS("belongs-to ", YCTX, LY_STMT_BELONGS_TO, "belongs-to");
386 TEST_STMS_SUCCESS("bit ", YCTX, LY_STMT_BIT, "bit");
387 TEST_STMS_SUCCESS("case ", YCTX, LY_STMT_CASE, "case");
388 TEST_STMS_SUCCESS("choice ", YCTX, LY_STMT_CHOICE, "choice");
389 TEST_STMS_SUCCESS("config ", YCTX, LY_STMT_CONFIG, "config");
390 TEST_STMS_SUCCESS("contact ", YCTX, LY_STMT_CONTACT, "contact");
391 TEST_STMS_SUCCESS("container ", YCTX, LY_STMT_CONTAINER, "container");
392 TEST_STMS_SUCCESS("default ", YCTX, LY_STMT_DEFAULT, "default");
393 TEST_STMS_SUCCESS("description ", YCTX, LY_STMT_DESCRIPTION, "description");
394 TEST_STMS_SUCCESS("deviate ", YCTX, LY_STMT_DEVIATE, "deviate");
395 TEST_STMS_SUCCESS("deviation ", YCTX, LY_STMT_DEVIATION, "deviation");
396 TEST_STMS_SUCCESS("enum ", YCTX, LY_STMT_ENUM, "enum");
397 TEST_STMS_SUCCESS("error-app-tag ", YCTX, LY_STMT_ERROR_APP_TAG, "error-app-tag");
398 TEST_STMS_SUCCESS("error-message ", YCTX, LY_STMT_ERROR_MESSAGE, "error-message");
399 TEST_STMS_SUCCESS("extension ", YCTX, LY_STMT_EXTENSION, "extension");
400 TEST_STMS_SUCCESS("feature ", YCTX, LY_STMT_FEATURE, "feature");
401 TEST_STMS_SUCCESS("fraction-digits ", YCTX, LY_STMT_FRACTION_DIGITS, "fraction-digits");
402 TEST_STMS_SUCCESS("grouping ", YCTX, LY_STMT_GROUPING, "grouping");
403 TEST_STMS_SUCCESS("identity ", YCTX, LY_STMT_IDENTITY, "identity");
404 TEST_STMS_SUCCESS("if-feature ", YCTX, LY_STMT_IF_FEATURE, "if-feature");
405 TEST_STMS_SUCCESS("import ", YCTX, LY_STMT_IMPORT, "import");
406 TEST_STMS_SUCCESS("include ", YCTX, LY_STMT_INCLUDE, "include");
407 TEST_STMS_SUCCESS("input{", YCTX, LY_STMT_INPUT, "input");
408 TEST_STMS_SUCCESS("key ", YCTX, LY_STMT_KEY, "key");
409 TEST_STMS_SUCCESS("leaf ", YCTX, LY_STMT_LEAF, "leaf");
410 TEST_STMS_SUCCESS("leaf-list ", YCTX, LY_STMT_LEAF_LIST, "leaf-list");
411 TEST_STMS_SUCCESS("length ", YCTX, LY_STMT_LENGTH, "length");
412 TEST_STMS_SUCCESS("list ", YCTX, LY_STMT_LIST, "list");
413 TEST_STMS_SUCCESS("mandatory ", YCTX, LY_STMT_MANDATORY, "mandatory");
414 TEST_STMS_SUCCESS("max-elements ", YCTX, LY_STMT_MAX_ELEMENTS, "max-elements");
415 TEST_STMS_SUCCESS("min-elements ", YCTX, LY_STMT_MIN_ELEMENTS, "min-elements");
416 TEST_STMS_SUCCESS("modifier ", YCTX, LY_STMT_MODIFIER, "modifier");
417 TEST_STMS_SUCCESS("module ", YCTX, LY_STMT_MODULE, "module");
418 TEST_STMS_SUCCESS("must ", YCTX, LY_STMT_MUST, "must");
419 TEST_STMS_SUCCESS("namespace ", YCTX, LY_STMT_NAMESPACE, "namespace");
420 TEST_STMS_SUCCESS("notification ", YCTX, LY_STMT_NOTIFICATION, "notification");
421 TEST_STMS_SUCCESS("ordered-by ", YCTX, LY_STMT_ORDERED_BY, "ordered-by");
422 TEST_STMS_SUCCESS("organization ", YCTX, LY_STMT_ORGANIZATION, "organization");
423 TEST_STMS_SUCCESS("output ", YCTX, LY_STMT_OUTPUT, "output");
424 TEST_STMS_SUCCESS("path ", YCTX, LY_STMT_PATH, "path");
425 TEST_STMS_SUCCESS("pattern ", YCTX, LY_STMT_PATTERN, "pattern");
426 TEST_STMS_SUCCESS("position ", YCTX, LY_STMT_POSITION, "position");
427 TEST_STMS_SUCCESS("prefix ", YCTX, LY_STMT_PREFIX, "prefix");
428 TEST_STMS_SUCCESS("presence ", YCTX, LY_STMT_PRESENCE, "presence");
429 TEST_STMS_SUCCESS("range ", YCTX, LY_STMT_RANGE, "range");
430 TEST_STMS_SUCCESS("reference ", YCTX, LY_STMT_REFERENCE, "reference");
431 TEST_STMS_SUCCESS("refine ", YCTX, LY_STMT_REFINE, "refine");
432 TEST_STMS_SUCCESS("require-instance ", YCTX, LY_STMT_REQUIRE_INSTANCE, "require-instance");
433 TEST_STMS_SUCCESS("revision ", YCTX, LY_STMT_REVISION, "revision");
434 TEST_STMS_SUCCESS("revision-date ", YCTX, LY_STMT_REVISION_DATE, "revision-date");
435 TEST_STMS_SUCCESS("rpc ", YCTX, LY_STMT_RPC, "rpc");
436 TEST_STMS_SUCCESS("status ", YCTX, LY_STMT_STATUS, "status");
437 TEST_STMS_SUCCESS("submodule ", YCTX, LY_STMT_SUBMODULE, "submodule");
438 TEST_STMS_SUCCESS("type ", YCTX, LY_STMT_TYPE, "type");
439 TEST_STMS_SUCCESS("typedef ", YCTX, LY_STMT_TYPEDEF, "typedef");
440 TEST_STMS_SUCCESS("unique ", YCTX, LY_STMT_UNIQUE, "unique");
441 TEST_STMS_SUCCESS("units ", YCTX, LY_STMT_UNITS, "units");
442 TEST_STMS_SUCCESS("uses ", YCTX, LY_STMT_USES, "uses");
443 TEST_STMS_SUCCESS("value ", YCTX, LY_STMT_VALUE, "value");
444 TEST_STMS_SUCCESS("when ", YCTX, LY_STMT_WHEN, "when");
445 TEST_STMS_SUCCESS("yang-version ", YCTX, LY_STMT_YANG_VERSION, "yang-version");
446 TEST_STMS_SUCCESS("yin-element ", YCTX, LY_STMT_YIN_ELEMENT, "yin-element");
447 TEST_STMS_SUCCESS(";config false;", YCTX, LY_STMT_SYNTAX_SEMICOLON, ";");
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("{ config false;", YCTX, LY_STMT_SYNTAX_LEFT_BRACE, "{");
Michal Vasko63f3d842020-07-08 10:10:14 +0200450 assert_string_equal(" config false;", in.current);
Radek Iša56ca9e42020-09-08 18:42:00 +0200451 TEST_STMS_SUCCESS("}", YCTX, LY_STMT_SYNTAX_RIGHT_BRACE, "}");
Michal Vasko63f3d842020-07-08 10:10:14 +0200452 assert_string_equal("", in.current);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200453
454 /* geenric extension */
Michal Vasko63f3d842020-07-08 10:10:14 +0200455 in.current = p = "nacm:default-deny-write;";
Radek Krejci33090f92020-12-17 20:12:46 +0100456 assert_int_equal(LY_SUCCESS, get_keyword(YCTX, &kw, &word, &len));
Radek Krejcid6b76452019-09-03 17:03:03 +0200457 assert_int_equal(LY_STMT_EXTENSION_INSTANCE, kw);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200458 assert_int_equal(23, len);
459 assert_ptr_equal(p, word);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200460}
Radek Krejci44ceedc2018-10-02 15:54:31 +0200461
Radek Iša56ca9e42020-09-08 18:42:00 +0200462#define TEST_MINMAX_SUCCESS(INPUT_TEXT, CTX, TYPE, VALUE)\
463 in.current = INPUT_TEXT;\
464 if(TYPE == LYS_SET_MIN){\
Radek Krejci33090f92020-12-17 20:12:46 +0100465 assert_int_equal(LY_SUCCESS, parse_minelements(CTX, &value, &flags, &ext));\
Radek Iša56ca9e42020-09-08 18:42:00 +0200466 }\
467 if(TYPE == LYS_SET_MAX){\
Radek Krejci33090f92020-12-17 20:12:46 +0100468 assert_int_equal(LY_SUCCESS, parse_maxelements(CTX, &value, &flags, &ext));\
Radek Iša56ca9e42020-09-08 18:42:00 +0200469 }\
470 assert_int_equal(TYPE, flags);\
471 assert_int_equal(VALUE, value)
472
Radek Krejci05b13982018-11-28 16:22:07 +0100473static void
474test_minmax(void **state)
475{
Radek Krejci05b13982018-11-28 16:22:07 +0100476 uint16_t flags = 0;
477 uint32_t value = 0;
478 struct lysp_ext_instance *ext = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +0100479
Radek Iša56ca9e42020-09-08 18:42:00 +0200480 YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejci05b13982018-11-28 16:22:07 +0100481
Michal Vasko63f3d842020-07-08 10:10:14 +0200482 in.current = " 1invalid; ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100483 assert_int_equal(LY_EVALID, parse_minelements(YCTX, &value, &flags, &ext));
Radek Iša56ca9e42020-09-08 18:42:00 +0200484 CHECK_LOG_CTX("Invalid value \"1invalid\" of \"min-elements\".", "Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100485
486 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200487 in.current = " -1; ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100488 assert_int_equal(LY_EVALID, parse_minelements(YCTX, &value, &flags, &ext));
Radek Iša56ca9e42020-09-08 18:42:00 +0200489 CHECK_LOG_CTX("Invalid value \"-1\" of \"min-elements\".", "Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100490
Radek Krejcidf6cad12018-11-28 17:10:55 +0100491 /* implementation limit */
492 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200493 in.current = " 4294967296; ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100494 assert_int_equal(LY_EVALID, parse_minelements(YCTX, &value, &flags, &ext));
Radek Iša56ca9e42020-09-08 18:42:00 +0200495 CHECK_LOG_CTX("Value \"4294967296\" is out of \"min-elements\" bounds.", "Line number 1.");
Radek Krejcidf6cad12018-11-28 17:10:55 +0100496
Radek Krejci05b13982018-11-28 16:22:07 +0100497 flags = value = 0;
Radek Iša56ca9e42020-09-08 18:42:00 +0200498 TEST_MINMAX_SUCCESS(" 1; ...", YCTX, LYS_SET_MIN, 1);
Radek Krejci05b13982018-11-28 16:22:07 +0100499
500 flags = value = 0;
Radek Iša56ca9e42020-09-08 18:42:00 +0200501 TEST_MINMAX_SUCCESS(" 1 {m:ext;} ...", YCTX, LYS_SET_MIN, 1);
Radek Krejci05b13982018-11-28 16:22:07 +0100502 assert_non_null(ext);
Radek Iša56ca9e42020-09-08 18:42:00 +0200503 FREE_ARRAY(YCTX->parsed_mod->mod->ctx, ext, lysp_ext_instance_free);
Radek Krejci05b13982018-11-28 16:22:07 +0100504 ext = NULL;
505
506 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200507 in.current = " 1 {config true;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100508 assert_int_equal(LY_EVALID, parse_minelements(YCTX, &value, &flags, &ext));
Radek Iša56ca9e42020-09-08 18:42:00 +0200509 CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"min-elements\".", "Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100510
Michal Vasko63f3d842020-07-08 10:10:14 +0200511 in.current = " 1invalid; ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100512 assert_int_equal(LY_EVALID, parse_maxelements(YCTX, &value, &flags, &ext));
Radek Iša56ca9e42020-09-08 18:42:00 +0200513 CHECK_LOG_CTX("Invalid value \"1invalid\" of \"max-elements\".", "Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100514
515 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200516 in.current = " -1; ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100517 assert_int_equal(LY_EVALID, parse_maxelements(YCTX, &value, &flags, &ext));
Radek Iša56ca9e42020-09-08 18:42:00 +0200518 CHECK_LOG_CTX("Invalid value \"-1\" of \"max-elements\".", "Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100519
Radek Krejcidf6cad12018-11-28 17:10:55 +0100520 /* implementation limit */
521 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200522 in.current = " 4294967296; ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100523 assert_int_equal(LY_EVALID, parse_maxelements(YCTX, &value, &flags, &ext));
Radek Iša56ca9e42020-09-08 18:42:00 +0200524 CHECK_LOG_CTX("Value \"4294967296\" is out of \"max-elements\" bounds.", "Line number 1.");
Radek Krejcidf6cad12018-11-28 17:10:55 +0100525
Radek Krejci05b13982018-11-28 16:22:07 +0100526 flags = value = 0;
Radek Iša56ca9e42020-09-08 18:42:00 +0200527 TEST_MINMAX_SUCCESS(" 1; ...", YCTX, LYS_SET_MAX, 1);
Radek Krejci05b13982018-11-28 16:22:07 +0100528
529 flags = value = 0;
Radek Iša56ca9e42020-09-08 18:42:00 +0200530 TEST_MINMAX_SUCCESS(" unbounded; ...", YCTX, LYS_SET_MAX, 0);
Radek Krejci05b13982018-11-28 16:22:07 +0100531
532 flags = value = 0;
Radek Iša56ca9e42020-09-08 18:42:00 +0200533 TEST_MINMAX_SUCCESS(" 1 {m:ext;} ...", YCTX, LYS_SET_MAX, 1);
Radek Krejci05b13982018-11-28 16:22:07 +0100534 assert_non_null(ext);
Radek Iša56ca9e42020-09-08 18:42:00 +0200535 FREE_ARRAY(YCTX->parsed_mod->mod->ctx, ext, lysp_ext_instance_free);
Radek Krejci05b13982018-11-28 16:22:07 +0100536 ext = NULL;
537
538 flags = value = 0;
Michal Vasko63f3d842020-07-08 10:10:14 +0200539 in.current = " 1 {config true;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100540 assert_int_equal(LY_EVALID, parse_maxelements(YCTX, &value, &flags, &ext));
Radek Iša56ca9e42020-09-08 18:42:00 +0200541 CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"max-elements\".", "Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100542}
543
Radek Krejci9fcacc12018-10-11 15:59:11 +0200544static struct lysp_module *
Michal Vaskob36053d2020-03-26 15:49:30 +0100545mod_renew(struct lys_yang_parser_ctx *ctx)
Radek Krejci9fcacc12018-10-11 15:59:11 +0200546{
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200547 struct ly_ctx *ly_ctx = ctx->parsed_mod->mod->ctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100548
Radek Krejci90ed21e2021-04-12 14:47:46 +0200549 lys_module_free(ctx->parsed_mod->mod);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200550 ctx->parsed_mod = calloc(1, sizeof *ctx->parsed_mod);
551 ctx->parsed_mod->mod = calloc(1, sizeof *ctx->parsed_mod->mod);
552 ctx->parsed_mod->mod->parsed = ctx->parsed_mod;
553 ctx->parsed_mod->mod->ctx = ly_ctx;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100554
Radek Krejci2efc45b2020-12-22 16:25:44 +0100555 ctx->in->line = 1;
556
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200557 return ctx->parsed_mod;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100558}
559
560static struct lysp_submodule *
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200561submod_renew(struct lys_yang_parser_ctx *ctx)
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100562{
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200563 struct ly_ctx *ly_ctx = ctx->parsed_mod->mod->ctx;
564
Radek Krejci90ed21e2021-04-12 14:47:46 +0200565 lys_module_free(ctx->parsed_mod->mod);
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200566 ctx->parsed_mod = calloc(1, sizeof(struct lysp_submodule));
567 ctx->parsed_mod->mod = calloc(1, sizeof *ctx->parsed_mod->mod);
568 lydict_insert(ly_ctx, "name", 0, &ctx->parsed_mod->mod->name);
569 ctx->parsed_mod->mod->parsed = ctx->parsed_mod;
570 ctx->parsed_mod->mod->ctx = ly_ctx;
571
572 return (struct lysp_submodule *)ctx->parsed_mod;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200573}
574
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100575static LY_ERR
576test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
577 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
578 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
Radek Krejcid33273d2018-10-25 14:55:52 +0200579{
580 *module_data = user_data;
581 *format = LYS_IN_YANG;
582 *free_module_data = NULL;
583 return LY_SUCCESS;
584}
585
Radek Krejci9fcacc12018-10-11 15:59:11 +0200586static void
587test_module(void **state)
588{
Radek Krejci40544fa2019-01-11 09:38:37 +0100589 struct lysp_module *mod = NULL;
590 struct lysp_submodule *submod = NULL;
591 struct lys_module *m;
Michal Vasko405cc9e2020-12-01 12:01:27 +0100592 struct lys_glob_unres unres = {0};
Radek Iša56ca9e42020-09-08 18:42:00 +0200593 struct lys_yang_parser_ctx *ctx_p;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200594
Radek Iša56ca9e42020-09-08 18:42:00 +0200595 mod = mod_renew(YCTX);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200596
597 /* missing mandatory substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +0200598 in.current = " name {}";
Radek Krejci33090f92020-12-17 20:12:46 +0100599 assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100600 assert_string_equal("name", mod->mod->name);
Radek Iša56ca9e42020-09-08 18:42:00 +0200601 CHECK_LOG_CTX("Missing mandatory keyword \"namespace\" as a child of \"module\".", "Line number 1.");
Radek Krejci9fcacc12018-10-11 15:59:11 +0200602
Radek Iša56ca9e42020-09-08 18:42:00 +0200603 mod = mod_renew(YCTX);
Michal Vasko63f3d842020-07-08 10:10:14 +0200604 in.current = " name {namespace urn:x;}";
Radek Krejci33090f92020-12-17 20:12:46 +0100605 assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100606 assert_string_equal("urn:x", mod->mod->ns);
Radek Iša56ca9e42020-09-08 18:42:00 +0200607 CHECK_LOG_CTX("Missing mandatory keyword \"prefix\" as a child of \"module\".", "Line number 1.");
608 mod = mod_renew(YCTX);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200609
Michal Vasko63f3d842020-07-08 10:10:14 +0200610 in.current = " name {namespace urn:x;prefix \"x\";}";
Radek Krejci33090f92020-12-17 20:12:46 +0100611 assert_int_equal(LY_SUCCESS, parse_module(YCTX, mod));
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100612 assert_string_equal("x", mod->mod->prefix);
Radek Iša56ca9e42020-09-08 18:42:00 +0200613 mod = mod_renew(YCTX);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200614
Radek Krejci027d5802018-11-14 16:57:28 +0100615#define SCHEMA_BEGINNING " name {yang-version 1.1;namespace urn:x;prefix \"x\";"
616#define SCHEMA_BEGINNING2 " name {namespace urn:x;prefix \"x\";"
Radek Krejcia042ea12018-10-13 07:52:15 +0200617#define TEST_NODE(NODETYPE, INPUT, NAME) \
Michal Vasko63f3d842020-07-08 10:10:14 +0200618 in.current = SCHEMA_BEGINNING INPUT; \
Radek Krejci33090f92020-12-17 20:12:46 +0100619 assert_int_equal(LY_SUCCESS, parse_module(YCTX, mod)); \
Radek Krejcia042ea12018-10-13 07:52:15 +0200620 assert_non_null(mod->data); \
621 assert_int_equal(NODETYPE, mod->data->nodetype); \
622 assert_string_equal(NAME, mod->data->name); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200623 mod = mod_renew(YCTX);
Radek Krejcia042ea12018-10-13 07:52:15 +0200624#define TEST_GENERIC(INPUT, TARGET, TEST) \
Michal Vasko63f3d842020-07-08 10:10:14 +0200625 in.current = SCHEMA_BEGINNING INPUT; \
Radek Krejci33090f92020-12-17 20:12:46 +0100626 assert_int_equal(LY_SUCCESS, parse_module(YCTX, mod)); \
Radek Krejcia042ea12018-10-13 07:52:15 +0200627 assert_non_null(TARGET); \
628 TEST; \
Radek Iša56ca9e42020-09-08 18:42:00 +0200629 mod = mod_renew(YCTX);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100630#define TEST_DUP(MEMBER, VALUE1, VALUE2, LINE) \
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200631 TEST_DUP_GENERIC(SCHEMA_BEGINNING, MEMBER, VALUE1, VALUE2, \
Radek Iša56ca9e42020-09-08 18:42:00 +0200632 parse_module, mod, LINE, mod = mod_renew(YCTX))
Radek Krejcia042ea12018-10-13 07:52:15 +0200633
634 /* duplicated namespace, prefix */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100635 TEST_DUP("namespace", "y", "z", "1");
636 TEST_DUP("prefix", "y", "z", "1");
637 TEST_DUP("contact", "a", "b", "1");
638 TEST_DUP("description", "a", "b", "1");
639 TEST_DUP("organization", "a", "b", "1");
640 TEST_DUP("reference", "a", "b", "1");
Radek Krejcia042ea12018-10-13 07:52:15 +0200641
Radek Krejci70853c52018-10-15 14:46:16 +0200642 /* not allowed in module (submodule-specific) */
Michal Vasko63f3d842020-07-08 10:10:14 +0200643 in.current = SCHEMA_BEGINNING "belongs-to master {prefix m;}}";
Radek Krejci33090f92020-12-17 20:12:46 +0100644 assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
Radek Iša56ca9e42020-09-08 18:42:00 +0200645 CHECK_LOG_CTX("Invalid keyword \"belongs-to\" as a child of \"module\".", "Line number 1.");
646 mod = mod_renew(YCTX);
Radek Krejci70853c52018-10-15 14:46:16 +0200647
Radek Krejcia042ea12018-10-13 07:52:15 +0200648 /* anydata */
649 TEST_NODE(LYS_ANYDATA, "anydata test;}", "test");
650 /* anyxml */
651 TEST_NODE(LYS_ANYXML, "anyxml test;}", "test");
652 /* augment */
653 TEST_GENERIC("augment /somepath;}", mod->augments,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100654 assert_string_equal("/somepath", mod->augments[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200655 /* choice */
656 TEST_NODE(LYS_CHOICE, "choice test;}", "test");
657 /* contact 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100658 TEST_GENERIC("contact \"firstname\" + \n\t\" surname\";}", mod->mod->contact,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100659 assert_string_equal("firstname surname", mod->mod->contact));
Radek Krejcia042ea12018-10-13 07:52:15 +0200660 /* container */
661 TEST_NODE(LYS_CONTAINER, "container test;}", "test");
662 /* description 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100663 TEST_GENERIC("description \'some description\';}", mod->mod->dsc,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100664 assert_string_equal("some description", mod->mod->dsc));
Radek Krejcia042ea12018-10-13 07:52:15 +0200665 /* deviation */
666 TEST_GENERIC("deviation /somepath {deviate not-supported;}}", mod->deviations,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100667 assert_string_equal("/somepath", mod->deviations[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200668 /* extension */
669 TEST_GENERIC("extension test;}", mod->extensions,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100670 assert_string_equal("test", mod->extensions[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200671 /* feature */
672 TEST_GENERIC("feature test;}", mod->features,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100673 assert_string_equal("test", mod->features[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200674 /* grouping */
675 TEST_GENERIC("grouping grp;}", mod->groupings,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100676 assert_string_equal("grp", mod->groupings[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200677 /* identity */
678 TEST_GENERIC("identity test;}", mod->identities,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100679 assert_string_equal("test", mod->identities[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200680 /* import */
Radek Iša56ca9e42020-09-08 18:42:00 +0200681 ly_ctx_set_module_imp_clb(YCTX->parsed_mod->mod->ctx, test_imp_clb, "module zzz { namespace urn:zzz; prefix z;}");
Radek Krejci086c7132018-10-26 15:29:04 +0200682 TEST_GENERIC("import zzz {prefix z;}}", mod->imports,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100683 assert_string_equal("zzz", mod->imports[0].name));
Radek Krejci70853c52018-10-15 14:46:16 +0200684
Radek Krejcia042ea12018-10-13 07:52:15 +0200685 /* import - prefix collision */
Michal Vasko63f3d842020-07-08 10:10:14 +0200686 in.current = SCHEMA_BEGINNING "import zzz {prefix x;}}";
Radek Krejci33090f92020-12-17 20:12:46 +0100687 assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
Radek Krejci2efc45b2020-12-22 16:25:44 +0100688 CHECK_LOG_CTX("Prefix \"x\" already used as module prefix.", "Line number 1.");
Radek Iša56ca9e42020-09-08 18:42:00 +0200689 mod = mod_renew(YCTX);
690
Michal Vasko63f3d842020-07-08 10:10:14 +0200691 in.current = SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix y;}}";
Radek Krejci33090f92020-12-17 20:12:46 +0100692 assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
Radek Krejci2efc45b2020-12-22 16:25:44 +0100693 CHECK_LOG_CTX("Prefix \"y\" already used to import \"zzz\" module.", "Line number 1.");
Radek Iša56ca9e42020-09-08 18:42:00 +0200694
695 mod = mod_renew(YCTX);
Radek Krejciddace2c2021-01-08 11:30:56 +0100696 LOG_LOCBACK(0, 0, 0, 1);
Radek Krejci2efc45b2020-12-22 16:25:44 +0100697
Michal Vasko7c8439f2020-08-05 13:25:19 +0200698 in.current = "module name10 {yang-version 1.1;namespace urn:x;prefix \"x\";import zzz {prefix y;}import zzz {prefix z;}}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200699 assert_int_equal(lys_parse_mem(YCTX->parsed_mod->mod->ctx, in.current, LYS_IN_YANG, NULL), LY_SUCCESS);
700 CHECK_LOG_CTX("Single revision of the module \"zzz\" imported twice.", NULL);
Radek Krejci70853c52018-10-15 14:46:16 +0200701
Radek Krejcia042ea12018-10-13 07:52:15 +0200702 /* include */
Radek Iša56ca9e42020-09-08 18:42:00 +0200703 ly_ctx_set_module_imp_clb(YCTX->parsed_mod->mod->ctx, test_imp_clb, "module xxx { namespace urn:xxx; prefix x;}");
Michal Vasko63f3d842020-07-08 10:10:14 +0200704 in.current = "module" SCHEMA_BEGINNING "include xxx;}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200705 assert_int_equal(lys_parse_mem(YCTX->parsed_mod->mod->ctx, in.current, LYS_IN_YANG, NULL), LY_EVALID);
706 CHECK_LOG_CTX("Including \"xxx\" submodule into \"name\" failed.", NULL);
Radek Krejcid33273d2018-10-25 14:55:52 +0200707
Radek Iša56ca9e42020-09-08 18:42:00 +0200708 ly_ctx_set_module_imp_clb(YCTX->parsed_mod->mod->ctx, test_imp_clb, "submodule xxx {belongs-to wrong-name {prefix w;}}");
Michal Vasko63f3d842020-07-08 10:10:14 +0200709 in.current = "module" SCHEMA_BEGINNING "include xxx;}";
Radek Iša56ca9e42020-09-08 18:42:00 +0200710 assert_int_equal(lys_parse_mem(YCTX->parsed_mod->mod->ctx, in.current, LYS_IN_YANG, NULL), LY_EVALID);
711 CHECK_LOG_CTX("Including \"xxx\" submodule into \"name\" failed.", NULL);
Radek Krejcid33273d2018-10-25 14:55:52 +0200712
Radek Iša56ca9e42020-09-08 18:42:00 +0200713 ly_ctx_set_module_imp_clb(YCTX->parsed_mod->mod->ctx, test_imp_clb, "submodule xxx {belongs-to name {prefix x;}}");
Radek Krejcid33273d2018-10-25 14:55:52 +0200714 TEST_GENERIC("include xxx;}", mod->includes,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100715 assert_string_equal("xxx", mod->includes[0].name));
Radek Krejcid33273d2018-10-25 14:55:52 +0200716
Radek Krejcia042ea12018-10-13 07:52:15 +0200717 /* leaf */
718 TEST_NODE(LYS_LEAF, "leaf test {type string;}}", "test");
719 /* leaf-list */
720 TEST_NODE(LYS_LEAFLIST, "leaf-list test {type string;}}", "test");
721 /* list */
722 TEST_NODE(LYS_LIST, "list test {key a;leaf a {type string;}}}", "test");
723 /* notification */
724 TEST_GENERIC("notification test;}", mod->notifs,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100725 assert_string_equal("test", mod->notifs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200726 /* organization 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100727 TEST_GENERIC("organization \"CESNET a.l.e.\";}", mod->mod->org,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100728 assert_string_equal("CESNET a.l.e.", mod->mod->org));
Radek Krejcia042ea12018-10-13 07:52:15 +0200729 /* reference 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100730 TEST_GENERIC("reference RFC7950;}", mod->mod->ref,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100731 assert_string_equal("RFC7950", mod->mod->ref));
Radek Krejcia042ea12018-10-13 07:52:15 +0200732 /* revision */
733 TEST_GENERIC("revision 2018-10-12;}", mod->revs,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100734 assert_string_equal("2018-10-12", mod->revs[0].date));
Radek Krejcia042ea12018-10-13 07:52:15 +0200735 /* rpc */
736 TEST_GENERIC("rpc test;}", mod->rpcs,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100737 assert_string_equal("test", mod->rpcs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200738 /* typedef */
739 TEST_GENERIC("typedef test{type string;}}", mod->typedefs,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100740 assert_string_equal("test", mod->typedefs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200741 /* uses */
742 TEST_NODE(LYS_USES, "uses test;}", "test");
743 /* yang-version */
Michal Vasko63f3d842020-07-08 10:10:14 +0200744 in.current = SCHEMA_BEGINNING2 "\n\tyang-version 10;}";
Radek Krejci33090f92020-12-17 20:12:46 +0100745 assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
Radek Krejci2efc45b2020-12-22 16:25:44 +0100746 CHECK_LOG_CTX("Invalid value \"10\" of \"yang-version\".", NULL);
Radek Iša56ca9e42020-09-08 18:42:00 +0200747 mod = mod_renew(YCTX);
Radek Krejci96e48da2020-09-04 13:18:06 +0200748 in.current = SCHEMA_BEGINNING2 "yang-version 1;yang-version 1.1;}";
Radek Krejci33090f92020-12-17 20:12:46 +0100749 assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
Radek Krejci2efc45b2020-12-22 16:25:44 +0100750 CHECK_LOG_CTX("Duplicate keyword \"yang-version\".", NULL);
Radek Iša56ca9e42020-09-08 18:42:00 +0200751 mod = mod_renew(YCTX);
Radek Krejci96e48da2020-09-04 13:18:06 +0200752 in.current = SCHEMA_BEGINNING2 "yang-version 1;}";
Radek Krejci33090f92020-12-17 20:12:46 +0100753 assert_int_equal(LY_SUCCESS, parse_module(YCTX, mod));
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200754 assert_int_equal(1, mod->version);
Radek Iša56ca9e42020-09-08 18:42:00 +0200755 mod = mod_renew(YCTX);
Michal Vasko63f3d842020-07-08 10:10:14 +0200756 in.current = SCHEMA_BEGINNING2 "yang-version \"1.1\";}";
Radek Krejci33090f92020-12-17 20:12:46 +0100757 assert_int_equal(LY_SUCCESS, parse_module(YCTX, mod));
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200758 assert_int_equal(2, mod->version);
Radek Iša56ca9e42020-09-08 18:42:00 +0200759 mod = mod_renew(YCTX);
Radek Krejci40544fa2019-01-11 09:38:37 +0100760
Michal Vasko63f3d842020-07-08 10:10:14 +0200761 in.current = "module " SCHEMA_BEGINNING "} module q {namespace urn:q;prefixq;}";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200762 m = calloc(1, sizeof *m);
Radek Iša56ca9e42020-09-08 18:42:00 +0200763 m->ctx = YCTX->parsed_mod->mod->ctx;
Michal Vasko405cc9e2020-12-01 12:01:27 +0100764 assert_int_equal(LY_EVALID, yang_parse_module(&ctx_p, &in, m, &unres));
Radek Iša56ca9e42020-09-08 18:42:00 +0200765 CHECK_LOG_CTX("Trailing garbage \"module q {names...\" after module, expected end-of-input.", "Line number 1.");
Michal Vaskob36053d2020-03-26 15:49:30 +0100766 yang_parser_ctx_free(ctx_p);
Radek Krejci90ed21e2021-04-12 14:47:46 +0200767 lys_module_free(m);
Radek Krejci40544fa2019-01-11 09:38:37 +0100768
Michal Vasko63f3d842020-07-08 10:10:14 +0200769 in.current = "prefix " SCHEMA_BEGINNING "}";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200770 m = calloc(1, sizeof *m);
Radek Iša56ca9e42020-09-08 18:42:00 +0200771 m->ctx = YCTX->parsed_mod->mod->ctx;
Michal Vasko405cc9e2020-12-01 12:01:27 +0100772 assert_int_equal(LY_EVALID, yang_parse_module(&ctx_p, &in, m, &unres));
Radek Iša56ca9e42020-09-08 18:42:00 +0200773 CHECK_LOG_CTX("Invalid keyword \"prefix\", expected \"module\" or \"submodule\".", "Line number 1.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200774 yang_parser_ctx_free(ctx_p);
Radek Krejci90ed21e2021-04-12 14:47:46 +0200775 lys_module_free(m);
Radek Krejci09306362018-10-15 15:26:01 +0200776
Michal Vasko63f3d842020-07-08 10:10:14 +0200777 in.current = "module " SCHEMA_BEGINNING "leaf enum {type enumeration {enum seven { position 7;}}}}";
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200778 m = calloc(1, sizeof *m);
Radek Iša56ca9e42020-09-08 18:42:00 +0200779 m->ctx = YCTX->parsed_mod->mod->ctx;
Michal Vasko405cc9e2020-12-01 12:01:27 +0100780 assert_int_equal(LY_EVALID, yang_parse_module(&ctx_p, &in, m, &unres));
Radek Iša56ca9e42020-09-08 18:42:00 +0200781 CHECK_LOG_CTX("Invalid keyword \"position\" as a child of \"enum\".", "Line number 1.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200782 yang_parser_ctx_free(ctx_p);
Radek Krejci90ed21e2021-04-12 14:47:46 +0200783 lys_module_free(m);
David Sedlák9fb515f2019-07-11 10:33:58 +0200784
Radek Krejci156ccaf2018-10-15 15:49:17 +0200785 /* extensions */
786 TEST_GENERIC("prefix:test;}", mod->exts,
Radek Krejcib4ac5a92020-11-23 17:54:33 +0100787 assert_string_equal("prefix:test", mod->exts[0].name);
Radek Krejciab430862021-03-02 20:13:40 +0100788 assert_int_equal(LY_STMT_MODULE, mod->exts[0].parent_stmt));
Radek Iša56ca9e42020-09-08 18:42:00 +0200789 mod = mod_renew(YCTX);
Radek Krejci156ccaf2018-10-15 15:49:17 +0200790
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200791 /* invalid substatement */
Michal Vasko63f3d842020-07-08 10:10:14 +0200792 in.current = SCHEMA_BEGINNING "must false;}";
Radek Krejci33090f92020-12-17 20:12:46 +0100793 assert_int_equal(LY_EVALID, parse_module(YCTX, mod));
Radek Krejci2efc45b2020-12-22 16:25:44 +0100794 CHECK_LOG_CTX("Invalid keyword \"must\" as a child of \"module\".", NULL);
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200795
Radek Krejci09306362018-10-15 15:26:01 +0200796 /* submodule */
Radek Iša56ca9e42020-09-08 18:42:00 +0200797 submod = submod_renew(YCTX);
Radek Krejci09306362018-10-15 15:26:01 +0200798
799 /* missing mandatory substatements */
Michal Vasko63f3d842020-07-08 10:10:14 +0200800 in.current = " subname {}";
Radek Krejci33090f92020-12-17 20:12:46 +0100801 assert_int_equal(LY_EVALID, parse_submodule(YCTX, submod));
Radek Krejci2efc45b2020-12-22 16:25:44 +0100802 CHECK_LOG_CTX("Missing mandatory keyword \"belongs-to\" as a child of \"submodule\".", NULL);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100803 assert_string_equal("subname", submod->name);
Radek Iša56ca9e42020-09-08 18:42:00 +0200804
805 submod = submod_renew(YCTX);
Radek Krejci09306362018-10-15 15:26:01 +0200806
Michal Vasko63f3d842020-07-08 10:10:14 +0200807 in.current = " subname {belongs-to name {prefix x;}}";
Radek Krejci33090f92020-12-17 20:12:46 +0100808 assert_int_equal(LY_SUCCESS, parse_submodule(YCTX, submod));
Michal Vaskoc3781c32020-10-06 14:04:08 +0200809 assert_string_equal("name", submod->mod->name);
Radek Iša56ca9e42020-09-08 18:42:00 +0200810 submod = submod_renew(YCTX);
Radek Krejci09306362018-10-15 15:26:01 +0200811
812#undef SCHEMA_BEGINNING
Radek Krejci313d9902018-11-08 09:42:58 +0100813#define SCHEMA_BEGINNING " subname {belongs-to name {prefix x;}"
Radek Krejci09306362018-10-15 15:26:01 +0200814
815 /* duplicated namespace, prefix */
Michal Vasko63f3d842020-07-08 10:10:14 +0200816 in.current = " subname {belongs-to name {prefix x;}belongs-to module1;belongs-to module2;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100817 assert_int_equal(LY_EVALID, parse_submodule(YCTX, submod));
Radek Krejci2efc45b2020-12-22 16:25:44 +0100818 CHECK_LOG_CTX("Duplicate keyword \"belongs-to\".", NULL);
Radek Iša56ca9e42020-09-08 18:42:00 +0200819 submod = submod_renew(YCTX);
Radek Krejci09306362018-10-15 15:26:01 +0200820
821 /* not allowed in submodule (module-specific) */
Michal Vasko63f3d842020-07-08 10:10:14 +0200822 in.current = SCHEMA_BEGINNING "namespace \"urn:z\";}";
Radek Krejci33090f92020-12-17 20:12:46 +0100823 assert_int_equal(LY_EVALID, parse_submodule(YCTX, submod));
Radek Krejci2efc45b2020-12-22 16:25:44 +0100824 CHECK_LOG_CTX("Invalid keyword \"namespace\" as a child of \"submodule\".", NULL);
Radek Iša56ca9e42020-09-08 18:42:00 +0200825 submod = submod_renew(YCTX);
Michal Vasko63f3d842020-07-08 10:10:14 +0200826 in.current = SCHEMA_BEGINNING "prefix m;}}";
Radek Krejci33090f92020-12-17 20:12:46 +0100827 assert_int_equal(LY_EVALID, parse_submodule(YCTX, submod));
Radek Krejci2efc45b2020-12-22 16:25:44 +0100828 CHECK_LOG_CTX("Invalid keyword \"prefix\" as a child of \"submodule\".", NULL);
Radek Iša56ca9e42020-09-08 18:42:00 +0200829 submod = submod_renew(YCTX);
Radek Krejcia042ea12018-10-13 07:52:15 +0200830
Michal Vasko63f3d842020-07-08 10:10:14 +0200831 in.current = "submodule " SCHEMA_BEGINNING "} module q {namespace urn:q;prefixq;}";
Radek Krejci33090f92020-12-17 20:12:46 +0100832 assert_int_equal(LY_EVALID, yang_parse_submodule(&ctx_p, YCTX->parsed_mod->mod->ctx, (struct lys_parser_ctx *)YCTX, YCTX->in, &submod));
Radek Iša56ca9e42020-09-08 18:42:00 +0200833 CHECK_LOG_CTX("Trailing garbage \"module q {names...\" after submodule, expected end-of-input.", "Line number 1.");
Michal Vaskob36053d2020-03-26 15:49:30 +0100834 yang_parser_ctx_free(ctx_p);
Radek Krejci40544fa2019-01-11 09:38:37 +0100835
Michal Vasko63f3d842020-07-08 10:10:14 +0200836 in.current = "prefix " SCHEMA_BEGINNING "}";
Radek Krejci33090f92020-12-17 20:12:46 +0100837 assert_int_equal(LY_EVALID, yang_parse_submodule(&ctx_p, YCTX->parsed_mod->mod->ctx, (struct lys_parser_ctx *)YCTX, YCTX->in, &submod));
Radek Iša56ca9e42020-09-08 18:42:00 +0200838 CHECK_LOG_CTX("Invalid keyword \"prefix\", expected \"module\" or \"submodule\".", "Line number 1.");
Michal Vaskob36053d2020-03-26 15:49:30 +0100839 yang_parser_ctx_free(ctx_p);
Radek Iša56ca9e42020-09-08 18:42:00 +0200840 submod = submod_renew(YCTX);
Radek Krejci40544fa2019-01-11 09:38:37 +0100841
Radek Krejcia042ea12018-10-13 07:52:15 +0200842#undef TEST_GENERIC
843#undef TEST_NODE
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200844#undef TEST_DUP
Radek Krejcia042ea12018-10-13 07:52:15 +0200845#undef SCHEMA_BEGINNING
Radek Krejciefd22f62018-09-27 11:47:58 +0200846}
847
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200848static void
849test_deviation(void **state)
850{
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200851 struct lysp_deviation *d = NULL;
Radek Krejci33090f92020-12-17 20:12:46 +0100852
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200853 /* invalid cardinality */
854#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
855 TEST_DUP_GENERIC(" test {deviate not-supported;", MEMBER, VALUE1, VALUE2, parse_deviation, \
Radek Iša56ca9e42020-09-08 18:42:00 +0200856 &d, "1", FREE_ARRAY(YCTX->parsed_mod->mod->ctx, d, lysp_deviation_free); d = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200857
858 TEST_DUP("description", "a", "b");
859 TEST_DUP("reference", "a", "b");
860
861 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +0200862 in.current = " test {deviate not-supported;description text;reference \'another text\';prefix:ext;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100863 assert_int_equal(LY_SUCCESS, parse_deviation(YCTX, &d));
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200864 assert_non_null(d);
Michal Vasko63f3d842020-07-08 10:10:14 +0200865 assert_string_equal(" ...", in.current);
Radek Iša56ca9e42020-09-08 18:42:00 +0200866 FREE_ARRAY(YCTX->parsed_mod->mod->ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200867 d = NULL;
868
869 /* missing mandatory substatement */
Michal Vasko63f3d842020-07-08 10:10:14 +0200870 in.current = " test {description text;}";
Radek Krejci33090f92020-12-17 20:12:46 +0100871 assert_int_equal(LY_EVALID, parse_deviation(YCTX, &d));
Radek Iša56ca9e42020-09-08 18:42:00 +0200872 CHECK_LOG_CTX("Missing mandatory keyword \"deviate\" as a child of \"deviation\".", "Line number 1.");
873 FREE_ARRAY(YCTX->parsed_mod->mod->ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200874 d = NULL;
875
876 /* invalid substatement */
Michal Vasko63f3d842020-07-08 10:10:14 +0200877 in.current = " test {deviate not-supported; status obsolete;}";
Radek Krejci33090f92020-12-17 20:12:46 +0100878 assert_int_equal(LY_EVALID, parse_deviation(YCTX, &d));
Radek Iša56ca9e42020-09-08 18:42:00 +0200879 CHECK_LOG_CTX("Invalid keyword \"status\" as a child of \"deviation\".", "Line number 1.");
880 FREE_ARRAY(YCTX->parsed_mod->mod->ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200881 d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200882#undef TEST_DUP
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200883}
884
Radek Iša56ca9e42020-09-08 18:42:00 +0200885#define TEST_DEVIATE_SUCCESS(INPUT_TEXT, REMAIN_TEXT)\
886 in.current = INPUT_TEXT;\
Radek Krejci33090f92020-12-17 20:12:46 +0100887 assert_int_equal(LY_SUCCESS, parse_deviate(YCTX, &d));\
Radek Iša56ca9e42020-09-08 18:42:00 +0200888 assert_non_null(d);\
889 assert_string_equal(REMAIN_TEXT, in.current);\
890 lysp_deviate_free(YCTX->parsed_mod->mod->ctx, d); free(d); d = NULL
891
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200892static void
893test_deviate(void **state)
894{
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200895 struct lysp_deviate *d = NULL;
Radek Krejci33090f92020-12-17 20:12:46 +0100896
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200897 /* invalid cardinality */
898#define TEST_DUP(TYPE, MEMBER, VALUE1, VALUE2) \
899 TEST_DUP_GENERIC(TYPE" {", MEMBER, VALUE1, VALUE2, parse_deviate, \
Radek Iša56ca9e42020-09-08 18:42:00 +0200900 &d, "1", lysp_deviate_free(YCTX->parsed_mod->mod->ctx, d); free(d); d = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200901
902 TEST_DUP("add", "config", "true", "false");
Radek Iša07767952021-01-31 20:54:24 +0100903#if 0
904 /*test on int8 now is in file tests/utest/types/int8.c */
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200905 TEST_DUP("replace", "default", "int8", "uint8");
Radek Iša07767952021-01-31 20:54:24 +0100906#endif
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200907 TEST_DUP("add", "mandatory", "true", "false");
908 TEST_DUP("add", "max-elements", "1", "2");
909 TEST_DUP("add", "min-elements", "1", "2");
Radek Iša07767952021-01-31 20:54:24 +0100910#if 0
911 /*test on int8 now is in file tests/utest/types/int8.c*/
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200912 TEST_DUP("replace", "type", "int8", "uint8");
Radek Iša07767952021-01-31 20:54:24 +0100913#endif
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200914 TEST_DUP("add", "units", "kilometers", "miles");
915
916 /* full contents */
Radek Iša56ca9e42020-09-08 18:42:00 +0200917 TEST_DEVIATE_SUCCESS(" not-supported {prefix:ext;} ...", " ...");
918 TEST_DEVIATE_SUCCESS(" add {units meters; must 1; must 2; unique x; unique y; default a; default b; config true; mandatory true; min-elements 1; max-elements 2; prefix:ext;} ...", " ...");
919 TEST_DEVIATE_SUCCESS(" delete {units meters; must 1; must 2; unique x; unique y; default a; default b; prefix:ext;} ...", " ...");
920 TEST_DEVIATE_SUCCESS(" replace {type string; units meters; default a; config true; mandatory true; min-elements 1; max-elements 2; prefix:ext;} ...", " ...");
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200921
922 /* invalid substatements */
923#define TEST_NOT_SUP(DEV, STMT, VALUE) \
Michal Vasko63f3d842020-07-08 10:10:14 +0200924 in.current = " "DEV" {"STMT" "VALUE";}..."; \
Radek Krejci33090f92020-12-17 20:12:46 +0100925 assert_int_equal(LY_EVALID, parse_deviate(YCTX, &d)); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200926 CHECK_LOG_CTX("Deviate \""DEV"\" does not support keyword \""STMT"\".", "Line number 1.");\
927 lysp_deviate_free(YCTX->parsed_mod->mod->ctx, d); free(d); d = NULL
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200928
929 TEST_NOT_SUP("not-supported", "units", "meters");
930 TEST_NOT_SUP("not-supported", "must", "1");
931 TEST_NOT_SUP("not-supported", "unique", "x");
932 TEST_NOT_SUP("not-supported", "default", "a");
933 TEST_NOT_SUP("not-supported", "config", "true");
934 TEST_NOT_SUP("not-supported", "mandatory", "true");
935 TEST_NOT_SUP("not-supported", "min-elements", "1");
936 TEST_NOT_SUP("not-supported", "max-elements", "2");
937 TEST_NOT_SUP("not-supported", "type", "string");
938 TEST_NOT_SUP("add", "type", "string");
939 TEST_NOT_SUP("delete", "config", "true");
940 TEST_NOT_SUP("delete", "mandatory", "true");
941 TEST_NOT_SUP("delete", "min-elements", "1");
942 TEST_NOT_SUP("delete", "max-elements", "2");
943 TEST_NOT_SUP("delete", "type", "string");
944 TEST_NOT_SUP("replace", "must", "1");
945 TEST_NOT_SUP("replace", "unique", "a");
946
Michal Vasko63f3d842020-07-08 10:10:14 +0200947 in.current = " nonsence; ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100948 assert_int_equal(LY_EVALID, parse_deviate(YCTX, &d));
Radek Iša56ca9e42020-09-08 18:42:00 +0200949 CHECK_LOG_CTX("Invalid value \"nonsence\" of \"deviate\".", "Line number 1.");\
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200950 assert_null(d);
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200951#undef TEST_NOT_SUP
952#undef TEST_DUP
Radek Krejci4c6d9bd2018-10-15 16:43:06 +0200953}
954
Radek Krejci8c370832018-11-02 15:10:03 +0100955static void
956test_container(void **state)
957{
Radek Krejci8c370832018-11-02 15:10:03 +0100958 struct lysp_node_container *c = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +0100959
Radek Iša56ca9e42020-09-08 18:42:00 +0200960 YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejci8c370832018-11-02 15:10:03 +0100961
962 /* invalid cardinality */
963#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +0200964 in.current = "cont {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +0100965 assert_int_equal(LY_EVALID, parse_container(YCTX, NULL, (struct lysp_node**)&c)); \
Radek Iša56ca9e42020-09-08 18:42:00 +0200966 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
967 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +0100968
969 TEST_DUP("config", "true", "false");
970 TEST_DUP("description", "text1", "text2");
971 TEST_DUP("presence", "true", "false");
972 TEST_DUP("reference", "1", "2");
973 TEST_DUP("status", "current", "obsolete");
974 TEST_DUP("when", "true", "false");
975#undef TEST_DUP
976
977 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +0200978 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 +0100979 "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 +0100980 assert_int_equal(LY_SUCCESS, parse_container(YCTX, NULL, (struct lysp_node **)&c));
Radek Iša56ca9e42020-09-08 18:42:00 +0200981 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 +0100982 assert_non_null(c->actions);
983 assert_non_null(c->child);
Radek Krejci8c370832018-11-02 15:10:03 +0100984 assert_non_null(c->groupings);
Radek Krejci8c370832018-11-02 15:10:03 +0100985 assert_non_null(c->musts);
986 assert_non_null(c->notifs);
987 assert_string_equal("true", c->presence);
Radek Krejci8c370832018-11-02 15:10:03 +0100988 assert_non_null(c->typedefs);
Radek Iša56ca9e42020-09-08 18:42:00 +0200989 ly_set_erase(&YCTX->tpdfs_nodes, NULL);
990 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +0100991
992 /* invalid */
Michal Vasko63f3d842020-07-08 10:10:14 +0200993 in.current = " cont {augment /root;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100994 assert_int_equal(LY_EVALID, parse_container(YCTX, NULL, (struct lysp_node **)&c));
Radek Iša56ca9e42020-09-08 18:42:00 +0200995 CHECK_LOG_CTX("Invalid keyword \"augment\" as a child of \"container\".", "Line number 1.");
996 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)c); c = NULL;
Michal Vasko63f3d842020-07-08 10:10:14 +0200997 in.current = " cont {nonsence true;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +0100998 assert_int_equal(LY_EVALID, parse_container(YCTX, NULL, (struct lysp_node **)&c));
Radek Iša56ca9e42020-09-08 18:42:00 +0200999 CHECK_LOG_CTX("Invalid character sequence \"nonsence\", expected a keyword.", "Line number 1.");
1000 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001001
Radek Iša56ca9e42020-09-08 18:42:00 +02001002 YCTX->parsed_mod->version = 1; /* simulate YANG 1.0 */
Michal Vasko63f3d842020-07-08 10:10:14 +02001003 in.current = " cont {action x;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001004 assert_int_equal(LY_EVALID, parse_container(YCTX, NULL, (struct lysp_node **)&c));
Radek Iša56ca9e42020-09-08 18:42:00 +02001005 CHECK_LOG_CTX("Invalid keyword \"action\" as a child of \"container\" - "
1006 "the statement is allowed only in YANG 1.1 modules.", "Line number 1.");
1007 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001008}
1009
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001010static void
1011test_leaf(void **state)
1012{
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001013 struct lysp_node_leaf *l = NULL;
Radek Krejci33090f92020-12-17 20:12:46 +01001014
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001015 /* invalid cardinality */
1016#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001017 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001018 assert_int_equal(LY_EVALID, parse_leaf(YCTX, NULL, (struct lysp_node**)&l)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001019 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
1020 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node*)l); l = NULL;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001021
1022 TEST_DUP("config", "true", "false");
1023 TEST_DUP("default", "x", "y");
1024 TEST_DUP("description", "text1", "text2");
1025 TEST_DUP("mandatory", "true", "false");
1026 TEST_DUP("reference", "1", "2");
1027 TEST_DUP("status", "current", "obsolete");
Radek Krejci0e5d8382018-11-28 16:37:53 +01001028 TEST_DUP("type", "int8", "uint8");
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001029 TEST_DUP("units", "text1", "text2");
1030 TEST_DUP("when", "true", "false");
1031#undef TEST_DUP
1032
1033 /* full content - without mandatory which is mutual exclusive with default */
Michal Vasko63f3d842020-07-08 10:10:14 +02001034 in.current = "l {config false;default \"xxx\";description test;if-feature f;"
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001035 "must 'expr';reference test;status current;type string; units yyy;when true;m:ext;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001036 assert_int_equal(LY_SUCCESS, parse_leaf(YCTX, NULL, (struct lysp_node **)&l));
Radek Iša56ca9e42020-09-08 18:42:00 +02001037 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 +02001038 assert_string_equal("xxx", l->dflt.str);
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001039 assert_string_equal("yyy", l->units);
1040 assert_string_equal("string", l->type.name);
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001041 assert_non_null(l->musts);
Radek Iša56ca9e42020-09-08 18:42:00 +02001042 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)l); l = NULL;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001043
1044 /* full content - now with mandatory */
Michal Vasko63f3d842020-07-08 10:10:14 +02001045 in.current = "l {mandatory true; type string;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001046 assert_int_equal(LY_SUCCESS, parse_leaf(YCTX, NULL, (struct lysp_node **)&l));
Radek Iša56ca9e42020-09-08 18:42:00 +02001047 CHECK_LYSP_NODE(l, NULL, 0, LYS_MAND_TRUE, 0, "l", 0, LYS_LEAF, 0, NULL, 0);
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001048 assert_string_equal("string", l->type.name);
Radek Iša56ca9e42020-09-08 18:42:00 +02001049 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)l); l = NULL;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001050
1051 /* invalid */
Michal Vasko63f3d842020-07-08 10:10:14 +02001052 in.current = " l {description \"missing type\";} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001053 assert_int_equal(LY_EVALID, parse_leaf(YCTX, NULL, (struct lysp_node **)&l));
Radek Iša56ca9e42020-09-08 18:42:00 +02001054 CHECK_LOG_CTX("Missing mandatory keyword \"type\" as a child of \"leaf\".", "Line number 1.");
1055 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)l); l = NULL;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001056}
1057
Radek Krejci0e5d8382018-11-28 16:37:53 +01001058static void
1059test_leaflist(void **state)
1060{
Radek Krejci0e5d8382018-11-28 16:37:53 +01001061 struct lysp_node_leaflist *ll = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001062
Radek Iša56ca9e42020-09-08 18:42:00 +02001063 YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejci0e5d8382018-11-28 16:37:53 +01001064
1065 /* invalid cardinality */
1066#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001067 in.current = "ll {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001068 assert_int_equal(LY_EVALID, parse_leaflist(YCTX, NULL, (struct lysp_node**)&ll)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001069 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
1070 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node*)ll); ll = NULL;
Radek Krejci0e5d8382018-11-28 16:37:53 +01001071
1072 TEST_DUP("config", "true", "false");
1073 TEST_DUP("description", "text1", "text2");
1074 TEST_DUP("max-elements", "10", "20");
1075 TEST_DUP("min-elements", "10", "20");
1076 TEST_DUP("ordered-by", "user", "system");
1077 TEST_DUP("reference", "1", "2");
1078 TEST_DUP("status", "current", "obsolete");
1079 TEST_DUP("type", "int8", "uint8");
1080 TEST_DUP("units", "text1", "text2");
1081 TEST_DUP("when", "true", "false");
1082#undef TEST_DUP
1083
1084 /* full content - without min-elements which is mutual exclusive with default */
Michal Vasko63f3d842020-07-08 10:10:14 +02001085 in.current = "ll {config false;default \"xxx\"; default \"yyy\";description test;if-feature f;"
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001086 "max-elements 10;must 'expr';ordered-by user;reference test;"
1087 "status current;type string; units zzz;when true;m:ext;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001088 assert_int_equal(LY_SUCCESS, parse_leaflist(YCTX, NULL, (struct lysp_node **)&ll));
Radek Iša56ca9e42020-09-08 18:42:00 +02001089 CHECK_LYSP_NODE(ll, "test", 1, 0x446, 1, "ll", 0, LYS_LEAFLIST, 0, "test", 1);
Radek Krejci0e5d8382018-11-28 16:37:53 +01001090 assert_non_null(ll->dflts);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001091 assert_int_equal(2, LY_ARRAY_COUNT(ll->dflts));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001092 assert_string_equal("xxx", ll->dflts[0].str);
1093 assert_string_equal("yyy", ll->dflts[1].str);
Radek Krejci0e5d8382018-11-28 16:37:53 +01001094 assert_string_equal("zzz", ll->units);
1095 assert_int_equal(10, ll->max);
1096 assert_int_equal(0, ll->min);
1097 assert_string_equal("string", ll->type.name);
Radek Krejci0e5d8382018-11-28 16:37:53 +01001098 assert_non_null(ll->musts);
Radek Krejci0e5d8382018-11-28 16:37:53 +01001099 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_USER | LYS_SET_MAX, ll->flags);
Radek Iša56ca9e42020-09-08 18:42:00 +02001100 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)ll); ll = NULL;
Radek Krejci0e5d8382018-11-28 16:37:53 +01001101
1102 /* full content - now with min-elements */
Michal Vasko63f3d842020-07-08 10:10:14 +02001103 in.current = "ll {min-elements 10; type string;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001104 assert_int_equal(LY_SUCCESS, parse_leaflist(YCTX, NULL, (struct lysp_node **)&ll));
Radek Iša56ca9e42020-09-08 18:42:00 +02001105 CHECK_LYSP_NODE(ll, NULL, 0, 0x200, 0, "ll", 0, LYS_LEAFLIST, 0, NULL, 0);
Radek Krejci0e5d8382018-11-28 16:37:53 +01001106 assert_string_equal("string", ll->type.name);
1107 assert_int_equal(0, ll->max);
1108 assert_int_equal(10, ll->min);
1109 assert_int_equal(LYS_SET_MIN, ll->flags);
Radek Iša56ca9e42020-09-08 18:42:00 +02001110 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)ll); ll = NULL;
Radek Krejci0e5d8382018-11-28 16:37:53 +01001111
1112 /* invalid */
Michal Vasko63f3d842020-07-08 10:10:14 +02001113 in.current = " ll {description \"missing type\";} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001114 assert_int_equal(LY_EVALID, parse_leaflist(YCTX, NULL, (struct lysp_node **)&ll));
Radek Iša56ca9e42020-09-08 18:42:00 +02001115 CHECK_LOG_CTX("Missing mandatory keyword \"type\" as a child of \"leaf-list\".", "Line number 1.");
1116 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)ll); ll = NULL;
Radek Krejci0e5d8382018-11-28 16:37:53 +01001117
Radek Iša56ca9e42020-09-08 18:42:00 +02001118 YCTX->parsed_mod->version = 1; /* simulate YANG 1.0 - default statement is not allowed */
Michal Vasko63f3d842020-07-08 10:10:14 +02001119 in.current = " ll {default xx; type string;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001120 assert_int_equal(LY_EVALID, parse_leaflist(YCTX, NULL, (struct lysp_node **)&ll));
Radek Iša56ca9e42020-09-08 18:42:00 +02001121 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.");
1122 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)ll); ll = NULL;
Radek Krejci0e5d8382018-11-28 16:37:53 +01001123}
1124
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001125static void
1126test_list(void **state)
1127{
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001128 struct lysp_node_list *l = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001129
Radek Iša56ca9e42020-09-08 18:42:00 +02001130 YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001131
1132 /* invalid cardinality */
1133#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001134 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001135 assert_int_equal(LY_EVALID, parse_list(YCTX, NULL, (struct lysp_node**)&l)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001136 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
1137 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node*)l); l = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001138
1139 TEST_DUP("config", "true", "false");
1140 TEST_DUP("description", "text1", "text2");
1141 TEST_DUP("key", "one", "two");
1142 TEST_DUP("max-elements", "10", "20");
1143 TEST_DUP("min-elements", "10", "20");
1144 TEST_DUP("ordered-by", "user", "system");
1145 TEST_DUP("reference", "1", "2");
1146 TEST_DUP("status", "current", "obsolete");
1147 TEST_DUP("when", "true", "false");
1148#undef TEST_DUP
1149
1150 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001151 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 +01001152 "leaf-list ll {type string;} list li;max-elements 10; min-elements 1;must 'expr';notification not; ordered-by system; reference test;"
1153 "status current;typedef t {type int8;}unique xxx;unique yyy;uses g;when true;m:ext;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001154 assert_int_equal(LY_SUCCESS, parse_list(YCTX, NULL, (struct lysp_node **)&l));
Radek Iša56ca9e42020-09-08 18:42:00 +02001155 CHECK_LYSP_NODE(l, "test", 1, LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM | LYS_SET_MAX | LYS_SET_MIN, 1, "l",
1156 0, LYS_LIST, 0, "test", 1);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001157 assert_string_equal("l", l->key);
1158 assert_non_null(l->uniques);
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001159 assert_int_equal(2, LY_ARRAY_COUNT(l->uniques));
Michal Vasko7f45cf22020-10-01 12:49:44 +02001160 assert_string_equal("xxx", l->uniques[0].str);
1161 assert_string_equal("yyy", l->uniques[1].str);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001162 assert_int_equal(10, l->max);
1163 assert_int_equal(1, l->min);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001164 assert_non_null(l->musts);
Radek Iša56ca9e42020-09-08 18:42:00 +02001165 ly_set_erase(&YCTX->tpdfs_nodes, NULL);
1166 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)l); l = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001167
Radek Krejcif538ce52019-03-05 10:46:14 +01001168 /* invalid content */
Radek Iša56ca9e42020-09-08 18:42:00 +02001169 YCTX->parsed_mod->version = 1; /* simulate YANG 1.0 */
Michal Vasko63f3d842020-07-08 10:10:14 +02001170 in.current = "l {action x;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001171 assert_int_equal(LY_EVALID, parse_list(YCTX, NULL, (struct lysp_node **)&l));
Radek Iša56ca9e42020-09-08 18:42:00 +02001172 CHECK_LOG_CTX("Invalid keyword \"action\" as a child of \"list\" - the statement is allowed only in YANG 1.1 modules.", "Line number 1.");
1173 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)l); l = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001174}
1175
Radek Krejci056d0a82018-12-06 16:57:25 +01001176static void
1177test_choice(void **state)
1178{
Radek Krejci056d0a82018-12-06 16:57:25 +01001179 struct lysp_node_choice *ch = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001180
Radek Iša56ca9e42020-09-08 18:42:00 +02001181 YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejci056d0a82018-12-06 16:57:25 +01001182
1183 /* invalid cardinality */
1184#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001185 in.current = "ch {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001186 assert_int_equal(LY_EVALID, parse_choice(YCTX, NULL, (struct lysp_node**)&ch)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001187 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
1188 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node*)ch); ch = NULL;
Radek Krejci056d0a82018-12-06 16:57:25 +01001189
1190 TEST_DUP("config", "true", "false");
1191 TEST_DUP("default", "a", "b");
1192 TEST_DUP("description", "text1", "text2");
1193 TEST_DUP("mandatory", "true", "false");
1194 TEST_DUP("reference", "1", "2");
1195 TEST_DUP("status", "current", "obsolete");
1196 TEST_DUP("when", "true", "false");
1197#undef TEST_DUP
1198
Radek Krejcia9026eb2018-12-12 16:04:47 +01001199 /* full content - without default due to a collision with mandatory */
Michal Vasko63f3d842020-07-08 10:10:14 +02001200 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 +01001201 "leaf-list ll {type string;} list li;mandatory true;reference test;status current;when true;m:ext;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001202 assert_int_equal(LY_SUCCESS, parse_choice(YCTX, NULL, (struct lysp_node **)&ch));
Radek Iša56ca9e42020-09-08 18:42:00 +02001203 CHECK_LYSP_NODE(ch, "test", 1, LYS_CONFIG_R | LYS_STATUS_CURR | LYS_MAND_TRUE, 1, "ch", 0, LYS_CHOICE, 0, "test", 1);
1204 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)ch); ch = NULL;
Radek Krejci056d0a82018-12-06 16:57:25 +01001205
Radek Krejcia9026eb2018-12-12 16:04:47 +01001206 /* full content - the default missing from the previous node */
Michal Vasko63f3d842020-07-08 10:10:14 +02001207 in.current = "ch {default c;case c;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001208 assert_int_equal(LY_SUCCESS, parse_choice(YCTX, NULL, (struct lysp_node **)&ch));
Radek Iša56ca9e42020-09-08 18:42:00 +02001209 CHECK_LYSP_NODE(ch, NULL, 0, 0, 0, "ch", 0, LYS_CHOICE, 0, NULL, 0);
Michal Vasko7f45cf22020-10-01 12:49:44 +02001210 assert_string_equal("c", ch->dflt.str);
Radek Iša56ca9e42020-09-08 18:42:00 +02001211 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)ch); ch = NULL;
Radek Krejcia9026eb2018-12-12 16:04:47 +01001212}
1213
1214static void
1215test_case(void **state)
1216{
Radek Krejcia9026eb2018-12-12 16:04:47 +01001217 struct lysp_node_case *cs = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001218
Radek Iša56ca9e42020-09-08 18:42:00 +02001219 YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejcia9026eb2018-12-12 16:04:47 +01001220
1221 /* invalid cardinality */
1222#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001223 in.current = "cs {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001224 assert_int_equal(LY_EVALID, parse_case(YCTX, NULL, (struct lysp_node**)&cs)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001225 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
1226 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node*)cs); cs = NULL;
Radek Krejcia9026eb2018-12-12 16:04:47 +01001227
1228 TEST_DUP("description", "text1", "text2");
1229 TEST_DUP("reference", "1", "2");
1230 TEST_DUP("status", "current", "obsolete");
1231 TEST_DUP("when", "true", "false");
1232#undef TEST_DUP
1233
1234 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001235 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 +01001236 "leaf-list ll {type string;} list li;reference test;status current;uses grp;when true;m:ext;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001237 assert_int_equal(LY_SUCCESS, parse_case(YCTX, NULL, (struct lysp_node **)&cs));
Radek Iša56ca9e42020-09-08 18:42:00 +02001238 CHECK_LYSP_NODE(cs, "test", 1, LYS_STATUS_CURR, 1, "cs", 0, LYS_CASE, 0, "test", 1);
1239 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)cs); cs = NULL;
Radek Krejcia9026eb2018-12-12 16:04:47 +01001240
1241 /* invalid content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001242 in.current = "cs {config true} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001243 assert_int_equal(LY_EVALID, parse_case(YCTX, NULL, (struct lysp_node **)&cs));
Radek Iša56ca9e42020-09-08 18:42:00 +02001244 CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"case\".", "Line number 1.");
1245 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)cs); cs = NULL;
Radek Krejci056d0a82018-12-06 16:57:25 +01001246}
1247
Radek Krejci9800fb82018-12-13 14:26:23 +01001248static void
Radek Krejcid6b76452019-09-03 17:03:03 +02001249test_any(void **state, enum ly_stmt kw)
Radek Krejci9800fb82018-12-13 14:26:23 +01001250{
Radek Krejci9800fb82018-12-13 14:26:23 +01001251 struct lysp_node_anydata *any = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001252
Radek Krejcid6b76452019-09-03 17:03:03 +02001253 if (kw == LY_STMT_ANYDATA) {
Radek Iša56ca9e42020-09-08 18:42:00 +02001254 YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejci9800fb82018-12-13 14:26:23 +01001255 } else {
Radek Iša56ca9e42020-09-08 18:42:00 +02001256 YCTX->parsed_mod->version = 1; /* simulate YANG 1.0 */
Radek Krejci9800fb82018-12-13 14:26:23 +01001257 }
1258
1259 /* invalid cardinality */
1260#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001261 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001262 assert_int_equal(LY_EVALID, parse_any(YCTX, kw, NULL, (struct lysp_node**)&any)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001263 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
1264 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node*)any); any = NULL;
Radek Krejci9800fb82018-12-13 14:26:23 +01001265
1266 TEST_DUP("config", "true", "false");
1267 TEST_DUP("description", "text1", "text2");
1268 TEST_DUP("mandatory", "true", "false");
1269 TEST_DUP("reference", "1", "2");
1270 TEST_DUP("status", "current", "obsolete");
1271 TEST_DUP("when", "true", "false");
1272#undef TEST_DUP
1273
1274 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001275 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 +01001276 assert_int_equal(LY_SUCCESS, parse_any(YCTX, kw, NULL, (struct lysp_node **)&any));
Radek Iša56ca9e42020-09-08 18:42:00 +02001277 // CHECK_LYSP_NODE(NODE, DSC, EXTS, FLAGS, IFFEATURES, NAME, NEXT, TYPE, PARENT, REF, WHEN)
1278 uint16_t node_type = kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
1279 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 +01001280 assert_non_null(any->musts);
Radek Iša56ca9e42020-09-08 18:42:00 +02001281 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)any); any = NULL;
Radek Krejci9800fb82018-12-13 14:26:23 +01001282}
1283
1284static void
1285test_anydata(void **state)
1286{
Radek Krejcid6b76452019-09-03 17:03:03 +02001287 return test_any(state, LY_STMT_ANYDATA);
Radek Krejci9800fb82018-12-13 14:26:23 +01001288}
1289
1290static void
1291test_anyxml(void **state)
1292{
Radek Krejcid6b76452019-09-03 17:03:03 +02001293 return test_any(state, LY_STMT_ANYXML);
Radek Krejci9800fb82018-12-13 14:26:23 +01001294}
1295
Radek Krejcie86bf772018-12-14 11:39:53 +01001296static void
1297test_grouping(void **state)
1298{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001299 struct lysp_node_grp *grp = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001300
Radek Iša56ca9e42020-09-08 18:42:00 +02001301 YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejcie86bf772018-12-14 11:39:53 +01001302
1303 /* invalid cardinality */
1304#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001305 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001306 assert_int_equal(LY_EVALID, parse_grouping(YCTX, NULL, &grp)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001307 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Radek Krejci2a9fc652021-01-22 17:44:34 +01001308 lysp_node_free(YCTX->parsed_mod->mod->ctx, &grp->node); grp = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001309
1310 TEST_DUP("description", "text1", "text2");
1311 TEST_DUP("reference", "1", "2");
1312 TEST_DUP("status", "current", "obsolete");
1313#undef TEST_DUP
1314
1315 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001316 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 +01001317 "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 +01001318 assert_int_equal(LY_SUCCESS, parse_grouping(YCTX, NULL, &grp));
Radek Krejcie86bf772018-12-14 11:39:53 +01001319 assert_non_null(grp);
1320 assert_int_equal(LYS_GROUPING, grp->nodetype);
1321 assert_string_equal("grp", grp->name);
1322 assert_string_equal("test", grp->dsc);
1323 assert_non_null(grp->exts);
1324 assert_string_equal("test", grp->ref);
1325 assert_null(grp->parent);
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001326 assert_int_equal(LYS_STATUS_CURR, grp->flags);
Radek Iša56ca9e42020-09-08 18:42:00 +02001327 ly_set_erase(&YCTX->tpdfs_nodes, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001328 lysp_node_free(YCTX->parsed_mod->mod->ctx, &grp->node);
1329 grp = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001330
1331 /* invalid content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001332 in.current = "grp {config true} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001333 assert_int_equal(LY_EVALID, parse_grouping(YCTX, NULL, &grp));
Radek Iša56ca9e42020-09-08 18:42:00 +02001334 CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"grouping\".", "Line number 1.");
Radek Krejci2a9fc652021-01-22 17:44:34 +01001335 lysp_node_free(YCTX->parsed_mod->mod->ctx, &grp->node);
1336 grp = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001337
Michal Vasko63f3d842020-07-08 10:10:14 +02001338 in.current = "grp {must 'expr'} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001339 assert_int_equal(LY_EVALID, parse_grouping(YCTX, NULL, &grp));
Radek Iša56ca9e42020-09-08 18:42:00 +02001340 CHECK_LOG_CTX("Invalid keyword \"must\" as a child of \"grouping\".", "Line number 1.");
Radek Krejci2a9fc652021-01-22 17:44:34 +01001341 lysp_node_free(YCTX->parsed_mod->mod->ctx, &grp->node);
1342 grp = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001343}
1344
1345static void
Radek Krejcif538ce52019-03-05 10:46:14 +01001346test_action(void **state)
1347{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001348 struct lysp_node_action *rpcs = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01001349 struct lysp_node_container *c = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001350
Radek Iša56ca9e42020-09-08 18:42:00 +02001351 YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejcif538ce52019-03-05 10:46:14 +01001352
1353 /* invalid cardinality */
1354#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001355 in.current = "func {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001356 assert_int_equal(LY_EVALID, parse_action(YCTX, NULL, &rpcs)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001357 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Radek Krejci2a9fc652021-01-22 17:44:34 +01001358 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node*)rpcs); rpcs = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01001359
1360 TEST_DUP("description", "text1", "text2");
Michal Vaskob83af8a2020-01-06 09:49:22 +01001361 TEST_DUP("input", "{leaf l1 {type empty;}} description a", "{leaf l2 {type empty;}} description a");
1362 TEST_DUP("output", "{leaf l1 {type empty;}} description a", "{leaf l2 {type empty;}} description a");
Radek Krejcif538ce52019-03-05 10:46:14 +01001363 TEST_DUP("reference", "1", "2");
1364 TEST_DUP("status", "current", "obsolete");
1365#undef TEST_DUP
1366
1367 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001368 in.current = "top;";
Radek Krejci33090f92020-12-17 20:12:46 +01001369 assert_int_equal(LY_SUCCESS, parse_container(YCTX, NULL, (struct lysp_node **)&c));
Michal Vasko63f3d842020-07-08 10:10:14 +02001370 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 +01001371 "input {anydata a1; anyxml a2; choice ch; container c; grouping grp; leaf l {type int8;} leaf-list ll {type int8;}"
1372 " list li; must 1; typedef mytypei {type int8;} uses grp; m:ext;}"
1373 "output {anydata a1; anyxml a2; choice ch; container c; grouping grp; leaf l {type int8;} leaf-list ll {type int8;}"
1374 " list li; must 1; typedef mytypeo {type int8;} uses grp; m:ext;}} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001375 assert_int_equal(LY_SUCCESS, parse_action(YCTX, (struct lysp_node *)c, &rpcs));
Radek Krejcif538ce52019-03-05 10:46:14 +01001376 assert_non_null(rpcs);
1377 assert_int_equal(LYS_ACTION, rpcs->nodetype);
1378 assert_string_equal("func", rpcs->name);
1379 assert_string_equal("test", rpcs->dsc);
1380 assert_non_null(rpcs->exts);
1381 assert_non_null(rpcs->iffeatures);
1382 assert_string_equal("test", rpcs->ref);
1383 assert_non_null(rpcs->groupings);
1384 assert_non_null(rpcs->typedefs);
1385 assert_int_equal(LYS_STATUS_CURR, rpcs->flags);
1386 /* input */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001387 assert_int_equal(rpcs->input.nodetype, LYS_INPUT);
Radek Krejcif538ce52019-03-05 10:46:14 +01001388 assert_non_null(rpcs->input.groupings);
1389 assert_non_null(rpcs->input.exts);
1390 assert_non_null(rpcs->input.musts);
1391 assert_non_null(rpcs->input.typedefs);
Radek Krejci01180ac2021-01-27 08:48:22 +01001392 assert_non_null(rpcs->input.child);
Radek Krejcif538ce52019-03-05 10:46:14 +01001393 /* output */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001394 assert_int_equal(rpcs->output.nodetype, LYS_OUTPUT);
Radek Krejcif538ce52019-03-05 10:46:14 +01001395 assert_non_null(rpcs->output.groupings);
1396 assert_non_null(rpcs->output.exts);
1397 assert_non_null(rpcs->output.musts);
1398 assert_non_null(rpcs->output.typedefs);
Radek Krejci01180ac2021-01-27 08:48:22 +01001399 assert_non_null(rpcs->output.child);
Radek Krejcif538ce52019-03-05 10:46:14 +01001400
Radek Iša56ca9e42020-09-08 18:42:00 +02001401 ly_set_erase(&YCTX->tpdfs_nodes, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001402 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)rpcs); rpcs = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01001403
1404 /* invalid content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001405 in.current = "func {config true} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001406 assert_int_equal(LY_EVALID, parse_action(YCTX, NULL, &rpcs));
Radek Iša56ca9e42020-09-08 18:42:00 +02001407 CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"rpc\".", "Line number 1.");
Radek Krejci2a9fc652021-01-22 17:44:34 +01001408 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)rpcs); rpcs = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01001409
Radek Iša56ca9e42020-09-08 18:42:00 +02001410 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)c);
Radek Krejcif538ce52019-03-05 10:46:14 +01001411}
1412
1413static void
Radek Krejcifc11bd72019-04-11 16:00:05 +02001414test_notification(void **state)
1415{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001416 struct lysp_node_notif *notifs = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001417 struct lysp_node_container *c = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001418
Radek Iša56ca9e42020-09-08 18:42:00 +02001419 YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejcifc11bd72019-04-11 16:00:05 +02001420
1421 /* invalid cardinality */
1422#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001423 in.current = "func {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001424 assert_int_equal(LY_EVALID, parse_notif(YCTX, NULL, &notifs)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001425 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Radek Krejci2a9fc652021-01-22 17:44:34 +01001426 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node*)notifs); notifs = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001427
1428 TEST_DUP("description", "text1", "text2");
1429 TEST_DUP("reference", "1", "2");
1430 TEST_DUP("status", "current", "obsolete");
1431#undef TEST_DUP
1432
1433 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001434 in.current = "top;";
Radek Krejci33090f92020-12-17 20:12:46 +01001435 assert_int_equal(LY_SUCCESS, parse_container(YCTX, NULL, (struct lysp_node **)&c));
Michal Vasko63f3d842020-07-08 10:10:14 +02001436 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 +01001437 "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 +01001438 assert_int_equal(LY_SUCCESS, parse_notif(YCTX, (struct lysp_node *)c, &notifs));
Radek Krejcifc11bd72019-04-11 16:00:05 +02001439 assert_non_null(notifs);
1440 assert_int_equal(LYS_NOTIF, notifs->nodetype);
1441 assert_string_equal("ntf", notifs->name);
1442 assert_string_equal("test", notifs->dsc);
1443 assert_non_null(notifs->exts);
1444 assert_non_null(notifs->iffeatures);
1445 assert_string_equal("test", notifs->ref);
1446 assert_non_null(notifs->groupings);
1447 assert_non_null(notifs->typedefs);
1448 assert_non_null(notifs->musts);
Radek Krejci01180ac2021-01-27 08:48:22 +01001449 assert_non_null(notifs->child);
Radek Krejcifc11bd72019-04-11 16:00:05 +02001450 assert_int_equal(LYS_STATUS_CURR, notifs->flags);
1451
Radek Iša56ca9e42020-09-08 18:42:00 +02001452 ly_set_erase(&YCTX->tpdfs_nodes, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001453 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)notifs); notifs = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001454
1455 /* invalid content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001456 in.current = "ntf {config true} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001457 assert_int_equal(LY_EVALID, parse_notif(YCTX, NULL, &notifs));
Radek Iša56ca9e42020-09-08 18:42:00 +02001458 CHECK_LOG_CTX("Invalid keyword \"config\" as a child of \"notification\".", "Line number 1.");
Radek Krejci2a9fc652021-01-22 17:44:34 +01001459 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)notifs); notifs = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02001460
Radek Iša56ca9e42020-09-08 18:42:00 +02001461 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)c);
Radek Krejcifc11bd72019-04-11 16:00:05 +02001462}
1463
1464static void
Radek Krejcie86bf772018-12-14 11:39:53 +01001465test_uses(void **state)
1466{
Radek Krejcie86bf772018-12-14 11:39:53 +01001467 struct lysp_node_uses *u = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001468
Radek Iša56ca9e42020-09-08 18:42:00 +02001469 YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejcie86bf772018-12-14 11:39:53 +01001470
1471 /* invalid cardinality */
1472#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001473 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001474 assert_int_equal(LY_EVALID, parse_uses(YCTX, NULL, (struct lysp_node**)&u)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001475 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
1476 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node*)u); u = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001477
1478 TEST_DUP("description", "text1", "text2");
1479 TEST_DUP("reference", "1", "2");
1480 TEST_DUP("status", "current", "obsolete");
1481 TEST_DUP("when", "true", "false");
1482#undef TEST_DUP
1483
1484 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001485 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 +01001486 assert_int_equal(LY_SUCCESS, parse_uses(YCTX, NULL, (struct lysp_node **)&u));
Radek Iša56ca9e42020-09-08 18:42:00 +02001487 CHECK_LYSP_NODE(u, "test", 1, LYS_STATUS_CURR, 1, "grpref", 0, LYS_USES, 0, "test", 1);
Radek Krejcie86bf772018-12-14 11:39:53 +01001488 assert_non_null(u->augments);
1489 assert_non_null(u->refines);
Radek Iša56ca9e42020-09-08 18:42:00 +02001490 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)u); u = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001491}
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001492
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001493static void
1494test_augment(void **state)
1495{
Radek Krejci2a9fc652021-01-22 17:44:34 +01001496 struct lysp_node_augment *a = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001497
Radek Iša56ca9e42020-09-08 18:42:00 +02001498 YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001499
1500 /* invalid cardinality */
1501#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001502 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001503 assert_int_equal(LY_EVALID, parse_augment(YCTX, NULL, &a)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001504 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
Radek Krejci2a9fc652021-01-22 17:44:34 +01001505 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)a); a = NULL;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001506
1507 TEST_DUP("description", "text1", "text2");
1508 TEST_DUP("reference", "1", "2");
1509 TEST_DUP("status", "current", "obsolete");
1510 TEST_DUP("when", "true", "false");
1511#undef TEST_DUP
1512
1513 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001514 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 +01001515 "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 +01001516 assert_int_equal(LY_SUCCESS, parse_augment(YCTX, NULL, &a));
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001517 assert_non_null(a);
1518 assert_int_equal(LYS_AUGMENT, a->nodetype);
1519 assert_string_equal("/target/nodeid", a->nodeid);
1520 assert_string_equal("test", a->dsc);
1521 assert_non_null(a->exts);
1522 assert_non_null(a->iffeatures);
1523 assert_string_equal("test", a->ref);
1524 assert_non_null(a->when);
1525 assert_null(a->parent);
1526 assert_int_equal(LYS_STATUS_CURR, a->flags);
Radek Krejci2a9fc652021-01-22 17:44:34 +01001527 lysp_node_free(YCTX->parsed_mod->mod->ctx, (struct lysp_node *)a); a = NULL;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001528}
1529
Radek Krejcif09e4e82019-06-14 15:08:11 +02001530static void
1531test_when(void **state)
1532{
Radek Krejcif09e4e82019-06-14 15:08:11 +02001533 struct lysp_when *w = NULL;
Radek Krejcidf549132021-01-21 10:32:32 +01001534
Radek Iša56ca9e42020-09-08 18:42:00 +02001535 YCTX->parsed_mod->version = 2; /* simulate YANG 1.1 */
Radek Krejcif09e4e82019-06-14 15:08:11 +02001536
1537 /* invalid cardinality */
1538#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Michal Vasko63f3d842020-07-08 10:10:14 +02001539 in.current = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
Radek Krejci33090f92020-12-17 20:12:46 +01001540 assert_int_equal(LY_EVALID, parse_when(YCTX, &w)); \
Radek Iša56ca9e42020-09-08 18:42:00 +02001541 CHECK_LOG_CTX("Duplicate keyword \""MEMBER"\".", "Line number 1."); \
1542 FREE_MEMBER(YCTX->parsed_mod->mod->ctx, w, lysp_when_free); w = NULL;
Radek Krejcif09e4e82019-06-14 15:08:11 +02001543
1544 TEST_DUP("description", "text1", "text2");
1545 TEST_DUP("reference", "1", "2");
1546#undef TEST_DUP
1547
1548 /* full content */
Michal Vasko63f3d842020-07-08 10:10:14 +02001549 in.current = "expression {description test;reference test;m:ext;} ...";
Radek Krejci33090f92020-12-17 20:12:46 +01001550 assert_int_equal(LY_SUCCESS, parse_when(YCTX, &w));
Radek Krejcif09e4e82019-06-14 15:08:11 +02001551 assert_non_null(w);
1552 assert_string_equal("expression", w->cond);
1553 assert_string_equal("test", w->dsc);
1554 assert_string_equal("test", w->ref);
1555 assert_non_null(w->exts);
Radek Iša56ca9e42020-09-08 18:42:00 +02001556 FREE_MEMBER(YCTX->parsed_mod->mod->ctx, w, lysp_when_free); w = NULL;
Radek Krejcif09e4e82019-06-14 15:08:11 +02001557
1558 /* empty condition */
Michal Vasko63f3d842020-07-08 10:10:14 +02001559 in.current = "\"\";";
Radek Krejci33090f92020-12-17 20:12:46 +01001560 assert_int_equal(LY_SUCCESS, parse_when(YCTX, &w));
Radek Iša56ca9e42020-09-08 18:42:00 +02001561 CHECK_LOG_CTX("Empty argument of when statement does not make sense.", NULL);
Radek Krejcif09e4e82019-06-14 15:08:11 +02001562 assert_non_null(w);
1563 assert_string_equal("", w->cond);
Radek Iša56ca9e42020-09-08 18:42:00 +02001564 FREE_MEMBER(YCTX->parsed_mod->mod->ctx, w, lysp_when_free); w = NULL;
Radek Krejcif09e4e82019-06-14 15:08:11 +02001565}
1566
David Sedlákd6ce6d72019-07-16 17:30:18 +02001567static void
1568test_value(void **state)
1569{
David Sedlákd6ce6d72019-07-16 17:30:18 +02001570 int64_t val = 0;
1571 uint16_t flags = 0;
1572
Michal Vasko63f3d842020-07-08 10:10:14 +02001573 in.current = "-0;";
Radek Krejci33090f92020-12-17 20:12:46 +01001574 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 +02001575 assert_int_equal(val, 0);
1576
Michal Vasko63f3d842020-07-08 10:10:14 +02001577 in.current = "-0;";
David Sedlákd6ce6d72019-07-16 17:30:18 +02001578 flags = 0;
Radek Krejci33090f92020-12-17 20:12:46 +01001579 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 +02001580 CHECK_LOG_CTX("Invalid value \"-0\" of \"position\".", "Line number 1.");
David Sedlákd6ce6d72019-07-16 17:30:18 +02001581}
1582
Radek Krejcib4ac5a92020-11-23 17:54:33 +01001583int
1584main(void)
Radek Krejci80dd33e2018-09-26 15:57:18 +02001585{
1586 const struct CMUnitTest tests[] = {
Radek Iša56ca9e42020-09-08 18:42:00 +02001587 UTEST(test_helpers, setup, teardown),
1588 UTEST(test_comments, setup, teardown),
1589 UTEST(test_arg, setup, teardown),
1590 UTEST(test_stmts, setup, teardown),
1591 UTEST(test_minmax, setup, teardown),
1592 UTEST(test_module, setup, teardown),
1593 UTEST(test_deviation, setup, teardown),
1594 UTEST(test_deviate, setup, teardown),
1595 UTEST(test_container, setup, teardown),
1596 UTEST(test_leaf, setup, teardown),
1597 UTEST(test_leaflist, setup, teardown),
1598 UTEST(test_list, setup, teardown),
1599 UTEST(test_choice, setup, teardown),
1600 UTEST(test_case, setup, teardown),
1601 UTEST(test_anydata, setup, teardown),
1602 UTEST(test_anyxml, setup, teardown),
1603 UTEST(test_action, setup, teardown),
1604 UTEST(test_notification, setup, teardown),
1605 UTEST(test_grouping, setup, teardown),
1606 UTEST(test_uses, setup, teardown),
1607 UTEST(test_augment, setup, teardown),
1608 UTEST(test_when, setup, teardown),
1609 UTEST(test_value, setup, teardown),
Radek Krejci80dd33e2018-09-26 15:57:18 +02001610 };
1611
1612 return cmocka_run_group_tests(tests, NULL, NULL);
1613}