blob: c171402e380b9d6ba0d162a4ae5e11638848317c [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 *
6 * Copyright (c) 2018 CESNET, z.s.p.o.
7 *
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 */
14
Radek Krejcif3f47842018-11-15 11:22:15 +010015#include "../../src/common.c"
16#include "../../src/set.c"
17#include "../../src/log.c"
18#include "../../src/hash_table.c"
19#include "../../src/xpath.c"
Radek Krejci86d106e2018-10-18 09:53:19 +020020#include "../../src/parser_yang.c"
Radek Krejcif3f47842018-11-15 11:22:15 +010021#include "../../src/context.c"
22#include "../../src/tree_schema_helpers.c"
Radek Krejci19a96102018-11-15 13:38:09 +010023#include "../../src/tree_schema_free.c"
24#include "../../src/tree_schema_compile.c"
Radek Krejcif3f47842018-11-15 11:22:15 +010025#include "../../src/tree_schema.c"
Radek Krejcie7b95092019-05-15 11:03:07 +020026#include "../../src/plugins_types.c"
Radek Krejci86d106e2018-10-18 09:53:19 +020027
Radek Krejci80dd33e2018-09-26 15:57:18 +020028#include <stdarg.h>
29#include <stddef.h>
30#include <setjmp.h>
31#include <cmocka.h>
32
33#include <stdio.h>
34#include <string.h>
35
36#include "libyang.h"
Radek Krejci80dd33e2018-09-26 15:57:18 +020037
38#define BUFSIZE 1024
39char logbuf[BUFSIZE] = {0};
Radek Krejci9ed7a192018-10-31 16:23:51 +010040int store = -1; /* negative for infinite logging, positive for limited logging */
Radek Krejci80dd33e2018-09-26 15:57:18 +020041
42/* set to 0 to printing error messages to stderr instead of checking them in code */
Radek Krejci70853c52018-10-15 14:46:16 +020043#define ENABLE_LOGGER_CHECKING 1
Radek Krejci80dd33e2018-09-26 15:57:18 +020044
Radek Krejcid5f2b5f2018-10-11 10:54:36 +020045#if ENABLE_LOGGER_CHECKING
Radek Krejci80dd33e2018-09-26 15:57:18 +020046static void
47logger(LY_LOG_LEVEL level, const char *msg, const char *path)
48{
49 (void) level; /* unused */
Radek Krejci9ed7a192018-10-31 16:23:51 +010050 if (store) {
51 if (path && path[0]) {
52 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
53 } else {
54 strncpy(logbuf, msg, BUFSIZE - 1);
55 }
56 if (store > 0) {
57 --store;
58 }
Radek Krejci80dd33e2018-09-26 15:57:18 +020059 }
60}
Radek Krejcid5f2b5f2018-10-11 10:54:36 +020061#endif
Radek Krejci80dd33e2018-09-26 15:57:18 +020062
63static int
64logger_setup(void **state)
65{
66 (void) state; /* unused */
67#if ENABLE_LOGGER_CHECKING
68 ly_set_log_clb(logger, 1);
69#endif
70 return 0;
71}
72
Radek Krejcib1a5dcc2018-11-26 14:50:05 +010073static int
74logger_teardown(void **state)
75{
76 (void) state; /* unused */
77#if ENABLE_LOGGER_CHECKING
78 if (*state) {
79 fprintf(stderr, "%s\n", logbuf);
80 }
81#endif
82 return 0;
83}
84
Radek Krejci80dd33e2018-09-26 15:57:18 +020085void
86logbuf_clean(void)
87{
88 logbuf[0] = '\0';
89}
90
91#if ENABLE_LOGGER_CHECKING
92# define logbuf_assert(str) assert_string_equal(logbuf, str)
93#else
94# define logbuf_assert(str)
95#endif
96
Radek Krejci2c02f3e2018-10-16 10:54:38 +020097#define TEST_DUP_GENERIC(PREFIX, MEMBER, VALUE1, VALUE2, FUNC, RESULT, LINE, CLEANUP) \
98 str = PREFIX MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
99 assert_int_equal(LY_EVALID, FUNC(&ctx, &str, RESULT)); \
100 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number "LINE"."); \
101 CLEANUP
102
Radek Krejci44ceedc2018-10-02 15:54:31 +0200103static void
104test_helpers(void **state)
105{
106 (void) state; /* unused */
107
108 const char *str;
Radek Krejci404251e2018-10-09 12:06:44 +0200109 char *buf, *p;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200110 size_t len, size;
111 int prefix;
Radek Krejcie7b95092019-05-15 11:03:07 +0200112 struct lys_parser_ctx ctx;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200113 ctx.ctx = NULL;
114 ctx.line = 1;
115
116 /* storing into buffer */
117 str = "abcd";
118 buf = NULL;
119 size = len = 0;
120 assert_int_equal(LY_SUCCESS, buf_add_char(NULL, &str, 2, &buf, &size, &len));
121 assert_int_not_equal(0, size);
122 assert_int_equal(2, len);
123 assert_string_equal("cd", str);
124 assert_false(strncmp("ab", buf, 2));
125 free(buf);
Radek Krejci404251e2018-10-09 12:06:44 +0200126 buf = NULL;
127
128 /* invalid first characters */
129 len = 0;
130 str = "2invalid";
131 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1));
132 str = ".invalid";
133 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1));
134 str = "-invalid";
135 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1));
136 /* invalid following characters */
137 len = 3; /* number of characters read before the str content */
138 str = "!";
139 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1));
140 str = ":";
141 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1));
142 /* valid colon for prefixed identifiers */
143 len = size = 0;
144 p = NULL;
145 str = "x:id";
146 assert_int_equal(LY_SUCCESS, buf_store_char(&ctx, &str, Y_PREF_IDENTIF_ARG, &p, &len, &buf, &size, 0));
147 assert_int_equal(1, len);
148 assert_null(buf);
149 assert_string_equal(":id", str);
150 assert_int_equal('x', p[len - 1]);
151 assert_int_equal(LY_SUCCESS, buf_store_char(&ctx, &str, Y_PREF_IDENTIF_ARG, &p, &len, &buf, &size, 1));
152 assert_int_equal(2, len);
153 assert_string_equal("id", str);
154 assert_int_equal(':', p[len - 1]);
155 free(buf);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200156
157 /* checking identifiers */
158 assert_int_equal(LY_EVALID, check_identifierchar(&ctx, ':', 0, NULL));
159 logbuf_assert("Invalid identifier character ':'. Line number 1.");
160 assert_int_equal(LY_EVALID, check_identifierchar(&ctx, '#', 1, NULL));
161 logbuf_assert("Invalid identifier first character '#'. Line number 1.");
162
163 assert_int_equal(LY_SUCCESS, check_identifierchar(&ctx, 'a', 1, &prefix));
164 assert_int_equal(0, prefix);
165 assert_int_equal(LY_SUCCESS, check_identifierchar(&ctx, ':', 0, &prefix));
166 assert_int_equal(1, prefix);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200167 assert_int_equal(LY_EVALID, check_identifierchar(&ctx, ':', 0, &prefix));
168 assert_int_equal(1, prefix);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200169 assert_int_equal(LY_SUCCESS, check_identifierchar(&ctx, 'b', 0, &prefix));
170 assert_int_equal(2, prefix);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200171 /* second colon is invalid */
172 assert_int_equal(LY_EVALID, check_identifierchar(&ctx, ':', 0, &prefix));
173 logbuf_assert("Invalid identifier character ':'. Line number 1.");
Radek Krejci44ceedc2018-10-02 15:54:31 +0200174}
Radek Krejci80dd33e2018-09-26 15:57:18 +0200175
176static void
177test_comments(void **state)
178{
179 (void) state; /* unused */
180
Radek Krejcie7b95092019-05-15 11:03:07 +0200181 struct lys_parser_ctx ctx;
Radek Krejci80dd33e2018-09-26 15:57:18 +0200182 const char *str, *p;
Radek Krejciefd22f62018-09-27 11:47:58 +0200183 char *word, *buf;
184 size_t len;
Radek Krejci80dd33e2018-09-26 15:57:18 +0200185
Radek Krejci44ceedc2018-10-02 15:54:31 +0200186 ctx.ctx = NULL;
187 ctx.line = 1;
188
Radek Krejciefd22f62018-09-27 11:47:58 +0200189 str = " // this is a text of / one * line */ comment\nargument";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200190 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200191 assert_string_equal("argument", word);
192 assert_null(buf);
193 assert_int_equal(8, len);
Radek Krejci80dd33e2018-09-26 15:57:18 +0200194
Radek Krejciefd22f62018-09-27 11:47:58 +0200195 str = "/* this is a \n * text // of / block * comment */\"arg\" + \"ume\" \n + \n \"nt\"";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200196 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200197 assert_string_equal("argument", word);
198 assert_ptr_equal(buf, word);
199 assert_int_equal(8, len);
200 free(word);
Radek Krejci80dd33e2018-09-26 15:57:18 +0200201
202 str = p = " this is one line comment on last line";
Radek Krejci44ceedc2018-10-02 15:54:31 +0200203 assert_int_equal(LY_SUCCESS, skip_comment(&ctx, &str, 1));
Radek Krejci80dd33e2018-09-26 15:57:18 +0200204 assert_true(str[0] == '\0');
205
206 str = p = " this is a not terminated comment x";
Radek Krejci44ceedc2018-10-02 15:54:31 +0200207 assert_int_equal(LY_EVALID, skip_comment(&ctx, &str, 2));
208 logbuf_assert("Unexpected end-of-file, non-terminated comment. Line number 5.");
Radek Krejci80dd33e2018-09-26 15:57:18 +0200209 assert_true(str[0] == '\0');
210}
211
Radek Krejciefd22f62018-09-27 11:47:58 +0200212static void
213test_arg(void **state)
214{
215 (void) state; /* unused */
216
Radek Krejcie7b95092019-05-15 11:03:07 +0200217 struct lys_parser_ctx ctx;
Radek Krejciefd22f62018-09-27 11:47:58 +0200218 const char *str;
219 char *word, *buf;
220 size_t len;
221
Radek Krejci44ceedc2018-10-02 15:54:31 +0200222 ctx.ctx = NULL;
223 ctx.line = 1;
224
Radek Krejciefd22f62018-09-27 11:47:58 +0200225 /* missing argument */
226 str = ";";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200227 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_MAYBE_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200228 assert_null(word);
229
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200230 str = "{";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200231 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200232 logbuf_assert("Invalid character sequence \"{\", expected an argument. Line number 1.");
233
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200234 /* invalid escape sequence */
235 str = "\"\\s\"";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200236 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200237 logbuf_assert("Double-quoted string unknown special character \'\\s\'. Line number 1.");
238 str = "\'\\s\'"; /* valid, since it is not an escape sequence in single quoted string */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200239 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200240 assert_int_equal(2, len);
241 assert_string_equal("\\s\'", word);
242 assert_int_equal('\0', str[0]); /* input has been eaten */
243
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200244 /* invalid character after the argument */
245 str = "hello\"";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200246 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200247 logbuf_assert("Invalid character sequence \"\"\", expected unquoted string character, optsep, semicolon or opening brace. Line number 1.");
248 str = "hello}";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200249 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200250 logbuf_assert("Invalid character sequence \"}\", expected unquoted string character, optsep, semicolon or opening brace. Line number 1.");
251
252 str = "hello/x\t"; /* slash is not an invalid character */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200253 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200254 assert_int_equal(7, len);
255 assert_string_equal("hello/x\t", word);
256
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200257 assert_null(buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200258
259 /* different quoting */
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200260 str = "hello ";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200261 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200262 assert_null(buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200263 assert_int_equal(5, len);
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200264 assert_string_equal("hello ", word);
Radek Krejciefd22f62018-09-27 11:47:58 +0200265
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200266 str = "hello/*comment*/\n";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200267 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200268 assert_null(buf);
269 assert_int_equal(5, len);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200270 assert_false(strncmp("hello", word, len));
271
272
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200273 str = "\"hello\\n\\t\\\"\\\\\";";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200274 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200275 assert_null(buf);
276 assert_int_equal(9, len);
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200277 assert_string_equal("hello\\n\\t\\\"\\\\\";", word);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200278
279 ctx.indent = 14;
280 str = "\"hello \t\n\t\t world!\"";
281 /* - space and tabs before newline are stripped out
282 * - space and tabs after newline (indentation) are stripped out
283 */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200284 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200285 assert_non_null(buf);
286 assert_ptr_equal(word, buf);
287 assert_int_equal(14, len);
288 assert_string_equal("hello\n world!", word);
289 free(buf);
290
291 ctx.indent = 14;
292 str = "\"hello\n \tworld!\"";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200293 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200294 assert_non_null(buf);
295 assert_ptr_equal(word, buf);
296 assert_int_equal(12, len);
297 assert_string_equal("hello\nworld!", word);
298 free(buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200299
300 str = "\'hello\'";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200301 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200302 assert_null(buf);
303 assert_int_equal(5, len);
304 assert_false(strncmp("hello", word, 5));
305
306 str = "\"hel\" +\t\n\"lo\"";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200307 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200308 assert_ptr_equal(word, buf);
309 assert_int_equal(5, len);
310 assert_string_equal("hello", word);
311 free(buf);
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200312 str = "\"hel\" +\t\nlo"; /* unquoted the second part */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200313 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200314 logbuf_assert("Both string parts divided by '+' must be quoted. Line number 5.");
Radek Krejciefd22f62018-09-27 11:47:58 +0200315
316 str = "\'he\'\t\n+ \"llo\"";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200317 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200318 assert_ptr_equal(word, buf);
319 assert_int_equal(5, len);
320 assert_string_equal("hello", word);
321 free(buf);
322
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200323 str = " \t\n\"he\"+\'llo\'";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200324 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200325 assert_ptr_equal(word, buf);
326 assert_int_equal(5, len);
327 assert_string_equal("hello", word);
328 free(buf);
329
Radek Krejci44ceedc2018-10-02 15:54:31 +0200330 /* missing argument */
331 str = ";";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200332 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200333 logbuf_assert("Invalid character sequence \";\", expected an argument. Line number 7.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200334}
335
336static void
337test_stmts(void **state)
338{
339 (void) state; /* unused */
340
Radek Krejcie7b95092019-05-15 11:03:07 +0200341 struct lys_parser_ctx ctx;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200342 const char *str, *p;
343 enum yang_keyword kw;
344 char *word;
345 size_t len;
346
347 ctx.ctx = NULL;
348 ctx.line = 1;
349
350 str = "\n// comment\n\tinput\t{";
351 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
352 assert_int_equal(YANG_INPUT, kw);
353 assert_int_equal(5, len);
354 assert_string_equal("input\t{", word);
355 assert_string_equal("\t{", str);
356
357 str = "\t /* comment */\t output\n\t{";
358 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
359 assert_int_equal(YANG_OUTPUT, kw);
360 assert_int_equal(6, len);
361 assert_string_equal("output\n\t{", word);
362 assert_string_equal("\n\t{", str);
363
364 str = "/input { "; /* invalid slash */
365 assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len));
366 logbuf_assert("Invalid identifier first character '/'. Line number 4.");
367
368 str = "not-a-statement-nor-extension { "; /* invalid identifier */
369 assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len));
370 logbuf_assert("Invalid character sequence \"not-a-statement-nor-extension\", expected a keyword. Line number 4.");
371
372 str = "path;"; /* missing sep after the keyword */
373 assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len));
374 logbuf_assert("Invalid character sequence \"path;\", expected a keyword followed by a separator. Line number 4.");
375
376 str = "action ";
377 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
378 assert_int_equal(YANG_ACTION, kw);
379 assert_int_equal(6, len);
380 str = "anydata ";
381 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
382 assert_int_equal(YANG_ANYDATA, kw);
383 assert_int_equal(7, len);
384 str = "anyxml ";
385 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
386 assert_int_equal(YANG_ANYXML, kw);
387 assert_int_equal(6, len);
388 str = "argument ";
389 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
390 assert_int_equal(YANG_ARGUMENT, kw);
391 assert_int_equal(8, len);
392 str = "augment ";
393 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
394 assert_int_equal(YANG_AUGMENT, kw);
395 assert_int_equal(7, len);
396 str = "base ";
397 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
398 assert_int_equal(YANG_BASE, kw);
399 assert_int_equal(4, len);
400 str = "belongs-to ";
401 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
402 assert_int_equal(YANG_BELONGS_TO, kw);
403 assert_int_equal(10, len);
404 str = "bit ";
405 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
406 assert_int_equal(YANG_BIT, kw);
407 assert_int_equal(3, len);
408 str = "case ";
409 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
410 assert_int_equal(YANG_CASE, kw);
411 assert_int_equal(4, len);
412 str = "choice ";
413 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
414 assert_int_equal(YANG_CHOICE, kw);
415 assert_int_equal(6, len);
416 str = "config ";
417 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
418 assert_int_equal(YANG_CONFIG, kw);
419 assert_int_equal(6, len);
420 str = "contact ";
421 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
422 assert_int_equal(YANG_CONTACT, kw);
423 assert_int_equal(7, len);
424 str = "container ";
425 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
426 assert_int_equal(YANG_CONTAINER, kw);
427 assert_int_equal(9, len);
428 str = "default ";
429 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
430 assert_int_equal(YANG_DEFAULT, kw);
431 assert_int_equal(7, len);
432 str = "description ";
433 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
434 assert_int_equal(YANG_DESCRIPTION, kw);
435 assert_int_equal(11, len);
436 str = "deviate ";
437 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
438 assert_int_equal(YANG_DEVIATE, kw);
439 assert_int_equal(7, len);
440 str = "deviation ";
441 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
442 assert_int_equal(YANG_DEVIATION, kw);
443 assert_int_equal(9, len);
444 str = "enum ";
445 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
446 assert_int_equal(YANG_ENUM, kw);
447 assert_int_equal(4, len);
448 str = "error-app-tag ";
449 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
450 assert_int_equal(YANG_ERROR_APP_TAG, kw);
451 assert_int_equal(13, len);
452 str = "error-message ";
453 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
454 assert_int_equal(YANG_ERROR_MESSAGE, kw);
455 assert_int_equal(13, len);
456 str = "extension ";
457 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
458 assert_int_equal(YANG_EXTENSION, kw);
459 assert_int_equal(9, len);
460 str = "feature ";
461 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
462 assert_int_equal(YANG_FEATURE, kw);
463 assert_int_equal(7, len);
464 str = "fraction-digits ";
465 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
466 assert_int_equal(YANG_FRACTION_DIGITS, kw);
467 assert_int_equal(15, len);
468 str = "grouping ";
469 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
470 assert_int_equal(YANG_GROUPING, kw);
471 assert_int_equal(8, len);
472 str = "identity ";
473 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
474 assert_int_equal(YANG_IDENTITY, kw);
475 assert_int_equal(8, len);
476 str = "if-feature ";
477 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
478 assert_int_equal(YANG_IF_FEATURE, kw);
479 assert_int_equal(10, len);
480 str = "import ";
481 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
482 assert_int_equal(YANG_IMPORT, kw);
483 assert_int_equal(6, len);
484 str = "include ";
485 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
486 assert_int_equal(YANG_INCLUDE, kw);
487 assert_int_equal(7, len);
488 str = "input{";
489 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
490 assert_int_equal(YANG_INPUT, kw);
491 assert_int_equal(5, len);
492 str = "key ";
493 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
494 assert_int_equal(YANG_KEY, kw);
495 assert_int_equal(3, len);
496 str = "leaf ";
497 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
498 assert_int_equal(YANG_LEAF, kw);
499 assert_int_equal(4, len);
500 str = "leaf-list ";
501 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
502 assert_int_equal(YANG_LEAF_LIST, kw);
503 assert_int_equal(9, len);
504 str = "length ";
505 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
506 assert_int_equal(YANG_LENGTH, kw);
507 assert_int_equal(6, len);
508 str = "list ";
509 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
510 assert_int_equal(YANG_LIST, kw);
511 assert_int_equal(4, len);
512 str = "mandatory ";
513 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
514 assert_int_equal(YANG_MANDATORY, kw);
515 assert_int_equal(9, len);
516 str = "max-elements ";
517 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
518 assert_int_equal(YANG_MAX_ELEMENTS, kw);
519 assert_int_equal(12, len);
520 str = "min-elements ";
521 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
522 assert_int_equal(YANG_MIN_ELEMENTS, kw);
523 assert_int_equal(12, len);
524 str = "modifier ";
525 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
526 assert_int_equal(YANG_MODIFIER, kw);
527 assert_int_equal(8, len);
528 str = "module ";
529 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
530 assert_int_equal(YANG_MODULE, kw);
531 assert_int_equal(6, len);
532 str = "must ";
533 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
534 assert_int_equal(YANG_MUST, kw);
535 assert_int_equal(4, len);
536 str = "namespace ";
537 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
538 assert_int_equal(YANG_NAMESPACE, kw);
539 assert_int_equal(9, len);
540 str = "notification ";
541 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
542 assert_int_equal(YANG_NOTIFICATION, kw);
543 assert_int_equal(12, len);
544 str = "ordered-by ";
545 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
546 assert_int_equal(YANG_ORDERED_BY, kw);
547 assert_int_equal(10, len);
548 str = "organization ";
549 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
550 assert_int_equal(YANG_ORGANIZATION, kw);
551 assert_int_equal(12, len);
552 str = "output ";
553 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
554 assert_int_equal(YANG_OUTPUT, kw);
555 assert_int_equal(6, len);
556 str = "path ";
557 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
558 assert_int_equal(YANG_PATH, kw);
559 assert_int_equal(4, len);
560 str = "pattern ";
561 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
562 assert_int_equal(YANG_PATTERN, kw);
563 assert_int_equal(7, len);
564 str = "position ";
565 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
566 assert_int_equal(YANG_POSITION, kw);
567 assert_int_equal(8, len);
568 str = "prefix ";
569 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
570 assert_int_equal(YANG_PREFIX, kw);
571 assert_int_equal(6, len);
572 str = "presence ";
573 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
574 assert_int_equal(YANG_PRESENCE, kw);
575 assert_int_equal(8, len);
576 str = "range ";
577 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
578 assert_int_equal(YANG_RANGE, kw);
579 assert_int_equal(5, len);
580 str = "reference ";
581 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
582 assert_int_equal(YANG_REFERENCE, kw);
583 assert_int_equal(9, len);
584 str = "refine ";
585 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
586 assert_int_equal(YANG_REFINE, kw);
587 assert_int_equal(6, len);
588 str = "require-instance ";
589 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
590 assert_int_equal(YANG_REQUIRE_INSTANCE, kw);
591 assert_int_equal(16, len);
592 str = "revision ";
593 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
594 assert_int_equal(YANG_REVISION, kw);
595 assert_int_equal(8, len);
596 str = "revision-date ";
597 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
598 assert_int_equal(YANG_REVISION_DATE, kw);
599 assert_int_equal(13, len);
600 str = "rpc ";
601 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
602 assert_int_equal(YANG_RPC, kw);
603 assert_int_equal(3, len);
604 str = "status ";
605 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
606 assert_int_equal(YANG_STATUS, kw);
607 assert_int_equal(6, len);
608 str = "submodule ";
609 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
610 assert_int_equal(YANG_SUBMODULE, kw);
611 assert_int_equal(9, len);
612 str = "type ";
613 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
614 assert_int_equal(YANG_TYPE, kw);
615 assert_int_equal(4, len);
616 str = "typedef ";
617 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
618 assert_int_equal(YANG_TYPEDEF, kw);
619 assert_int_equal(7, len);
620 str = "unique ";
621 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
622 assert_int_equal(YANG_UNIQUE, kw);
623 assert_int_equal(6, len);
624 str = "units ";
625 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
626 assert_int_equal(YANG_UNITS, kw);
627 assert_int_equal(5, len);
628 str = "uses ";
629 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
630 assert_int_equal(YANG_USES, kw);
631 assert_int_equal(4, len);
632 str = "value ";
633 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
634 assert_int_equal(YANG_VALUE, kw);
635 assert_int_equal(5, len);
636 str = "when ";
637 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
638 assert_int_equal(YANG_WHEN, kw);
639 assert_int_equal(4, len);
640 str = "yang-version ";
641 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
642 assert_int_equal(YANG_YANG_VERSION, kw);
643 assert_int_equal(12, len);
644 str = "yin-element ";
645 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
646 assert_int_equal(YANG_YIN_ELEMENT, kw);
647 assert_int_equal(11, len);
Radek Krejci626df482018-10-11 15:06:31 +0200648 str = ";config false;";
649 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
650 assert_int_equal(YANG_SEMICOLON, kw);
651 assert_int_equal(1, len);
652 assert_string_equal("config false;", str);
653 str = "{ config false;";
654 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
655 assert_int_equal(YANG_LEFT_BRACE, kw);
656 assert_int_equal(1, len);
657 assert_string_equal(" config false;", str);
658 str = "}";
659 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
660 assert_int_equal(YANG_RIGHT_BRACE, kw);
661 assert_int_equal(1, len);
662 assert_string_equal("", str);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200663
664 /* geenric extension */
665 str = p = "nacm:default-deny-write;";
666 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
667 assert_int_equal(YANG_CUSTOM, kw);
668 assert_int_equal(23, len);
669 assert_ptr_equal(p, word);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200670}
Radek Krejci44ceedc2018-10-02 15:54:31 +0200671
Radek Krejci05b13982018-11-28 16:22:07 +0100672static void
673test_minmax(void **state)
674{
675 *state = test_minmax;
676
Radek Krejcie7b95092019-05-15 11:03:07 +0200677 struct lys_parser_ctx ctx = {0};
Radek Krejci05b13982018-11-28 16:22:07 +0100678 uint16_t flags = 0;
679 uint32_t value = 0;
680 struct lysp_ext_instance *ext = NULL;
681 const char *str;
682
683 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
684 assert_non_null(ctx.ctx);
685 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100686 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci05b13982018-11-28 16:22:07 +0100687
Radek Krejcidf6cad12018-11-28 17:10:55 +0100688 str = " 1invalid; ...";
Radek Krejci05b13982018-11-28 16:22:07 +0100689 assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext));
Radek Krejcidf6cad12018-11-28 17:10:55 +0100690 logbuf_assert("Invalid value \"1invalid\" of \"min-elements\". Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100691
692 flags = value = 0;
693 str = " -1; ...";
694 assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext));
695 logbuf_assert("Invalid value \"-1\" of \"min-elements\". Line number 1.");
696
Radek Krejcidf6cad12018-11-28 17:10:55 +0100697 /* implementation limit */
698 flags = value = 0;
699 str = " 4294967296; ...";
700 assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext));
701 logbuf_assert("Value \"4294967296\" is out of \"min-elements\" bounds. Line number 1.");
702
Radek Krejci05b13982018-11-28 16:22:07 +0100703 flags = value = 0;
704 str = " 1; ...";
705 assert_int_equal(LY_SUCCESS, parse_minelements(&ctx, &str, &value, &flags, &ext));
706 assert_int_equal(LYS_SET_MIN, flags);
707 assert_int_equal(1, value);
708
709 flags = value = 0;
710 str = " 1 {m:ext;} ...";
711 assert_int_equal(LY_SUCCESS, parse_minelements(&ctx, &str, &value, &flags, &ext));
712 assert_int_equal(LYS_SET_MIN, flags);
713 assert_int_equal(1, value);
714 assert_non_null(ext);
715 FREE_ARRAY(ctx.ctx, ext, lysp_ext_instance_free);
716 ext = NULL;
717
718 flags = value = 0;
719 str = " 1 {config true;} ...";
720 assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext));
721 logbuf_assert("Invalid keyword \"config\" as a child of \"min-elements\". Line number 1.");
722
Radek Krejcidf6cad12018-11-28 17:10:55 +0100723 str = " 1invalid; ...";
Radek Krejci05b13982018-11-28 16:22:07 +0100724 assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext));
Radek Krejcidf6cad12018-11-28 17:10:55 +0100725 logbuf_assert("Invalid value \"1invalid\" of \"max-elements\". Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100726
727 flags = value = 0;
728 str = " -1; ...";
729 assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext));
730 logbuf_assert("Invalid value \"-1\" of \"max-elements\". Line number 1.");
731
Radek Krejcidf6cad12018-11-28 17:10:55 +0100732 /* implementation limit */
733 flags = value = 0;
734 str = " 4294967296; ...";
735 assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext));
736 logbuf_assert("Value \"4294967296\" is out of \"max-elements\" bounds. Line number 1.");
737
Radek Krejci05b13982018-11-28 16:22:07 +0100738 flags = value = 0;
739 str = " 1; ...";
740 assert_int_equal(LY_SUCCESS, parse_maxelements(&ctx, &str, &value, &flags, &ext));
741 assert_int_equal(LYS_SET_MAX, flags);
742 assert_int_equal(1, value);
743
744 flags = value = 0;
745 str = " unbounded; ...";
746 assert_int_equal(LY_SUCCESS, parse_maxelements(&ctx, &str, &value, &flags, &ext));
747 assert_int_equal(LYS_SET_MAX, flags);
748 assert_int_equal(0, value);
749
750 flags = value = 0;
751 str = " 1 {m:ext;} ...";
752 assert_int_equal(LY_SUCCESS, parse_maxelements(&ctx, &str, &value, &flags, &ext));
753 assert_int_equal(LYS_SET_MAX, flags);
754 assert_int_equal(1, value);
755 assert_non_null(ext);
756 FREE_ARRAY(ctx.ctx, ext, lysp_ext_instance_free);
757 ext = NULL;
758
759 flags = value = 0;
760 str = " 1 {config true;} ...";
761 assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext));
762 logbuf_assert("Invalid keyword \"config\" as a child of \"max-elements\". Line number 1.");
763
764 *state = NULL;
765 ly_ctx_destroy(ctx.ctx, NULL);
766}
767
Radek Krejci9fcacc12018-10-11 15:59:11 +0200768static struct lysp_module *
Radek Krejcie7b95092019-05-15 11:03:07 +0200769mod_renew(struct lys_parser_ctx *ctx)
Radek Krejci9fcacc12018-10-11 15:59:11 +0200770{
Radek Krejci40544fa2019-01-11 09:38:37 +0100771 struct lysp_module *mod_p;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100772 static struct lys_module mod = {0};
773
774 lysc_module_free(mod.compiled, NULL);
775 lysp_module_free(mod.parsed);
776 FREE_STRING(mod.ctx, mod.name);
777 FREE_STRING(mod.ctx, mod.ns);
778 FREE_STRING(mod.ctx, mod.prefix);
779 FREE_STRING(mod.ctx, mod.filepath);
780 FREE_STRING(mod.ctx, mod.org);
781 FREE_STRING(mod.ctx, mod.contact);
782 FREE_STRING(mod.ctx, mod.dsc);
783 FREE_STRING(mod.ctx, mod.ref);
784 memset(&mod, 0, sizeof mod);
785 mod.ctx = ctx->ctx;
786
787 mod_p = calloc(1, sizeof *mod_p);
788 mod.parsed = mod_p;
789 mod_p->mod = &mod;
790 assert_non_null(mod_p);
791 return mod_p;
792}
793
794static struct lysp_submodule *
Radek Krejcie7b95092019-05-15 11:03:07 +0200795submod_renew(struct lys_parser_ctx *ctx, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100796{
797 lysp_submodule_free(ctx->ctx, submod);
798 submod = calloc(1, sizeof *submod);
799 assert_non_null(submod);
800 return submod;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200801}
802
Radek Krejcid33273d2018-10-25 14:55:52 +0200803static LY_ERR test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
804 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
805 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
806{
807 *module_data = user_data;
808 *format = LYS_IN_YANG;
809 *free_module_data = NULL;
810 return LY_SUCCESS;
811}
812
Radek Krejci9fcacc12018-10-11 15:59:11 +0200813static void
814test_module(void **state)
815{
Radek Krejci40544fa2019-01-11 09:38:37 +0100816 *state = test_module;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200817
Radek Krejcie7b95092019-05-15 11:03:07 +0200818 struct lys_parser_ctx ctx;
Radek Krejci40544fa2019-01-11 09:38:37 +0100819 struct lysp_module *mod = NULL;
820 struct lysp_submodule *submod = NULL;
821 struct lys_module *m;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200822 const char *str;
823
824 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
825 assert_non_null(ctx.ctx);
826 ctx.line = 1;
Radek Krejcia042ea12018-10-13 07:52:15 +0200827 ctx.indent = 0;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200828
Radek Krejci40544fa2019-01-11 09:38:37 +0100829 mod = mod_renew(&ctx);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200830
831 /* missing mandatory substatements */
832 str = " name {}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100833 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
834 assert_string_equal("name", mod->mod->name);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200835 logbuf_assert("Missing mandatory keyword \"namespace\" as a child of \"module\". Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100836 mod = mod_renew(&ctx);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200837
838 str = " name {namespace urn:x;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100839 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
840 assert_string_equal("urn:x", mod->mod->ns);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200841 logbuf_assert("Missing mandatory keyword \"prefix\" as a child of \"module\". Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100842 mod = mod_renew(&ctx);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200843
844 str = " name {namespace urn:x;prefix \"x\";}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100845 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod));
846 assert_string_equal("x", mod->mod->prefix);
Radek Krejci40544fa2019-01-11 09:38:37 +0100847 mod = mod_renew(&ctx);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200848
Radek Krejci027d5802018-11-14 16:57:28 +0100849#define SCHEMA_BEGINNING " name {yang-version 1.1;namespace urn:x;prefix \"x\";"
850#define SCHEMA_BEGINNING2 " name {namespace urn:x;prefix \"x\";"
Radek Krejcia042ea12018-10-13 07:52:15 +0200851#define TEST_NODE(NODETYPE, INPUT, NAME) \
852 str = SCHEMA_BEGINNING INPUT; \
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100853 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod)); \
Radek Krejcia042ea12018-10-13 07:52:15 +0200854 assert_non_null(mod->data); \
855 assert_int_equal(NODETYPE, mod->data->nodetype); \
856 assert_string_equal(NAME, mod->data->name); \
Radek Krejci40544fa2019-01-11 09:38:37 +0100857 mod = mod_renew(&ctx);
Radek Krejcia042ea12018-10-13 07:52:15 +0200858#define TEST_GENERIC(INPUT, TARGET, TEST) \
859 str = SCHEMA_BEGINNING INPUT; \
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100860 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod)); \
Radek Krejcia042ea12018-10-13 07:52:15 +0200861 assert_non_null(TARGET); \
862 TEST; \
Radek Krejci40544fa2019-01-11 09:38:37 +0100863 mod = mod_renew(&ctx);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100864#define TEST_DUP(MEMBER, VALUE1, VALUE2, LINE) \
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200865 TEST_DUP_GENERIC(SCHEMA_BEGINNING, MEMBER, VALUE1, VALUE2, \
Radek Krejci40544fa2019-01-11 09:38:37 +0100866 parse_module, mod, LINE, mod = mod_renew(&ctx))
Radek Krejcia042ea12018-10-13 07:52:15 +0200867
868 /* duplicated namespace, prefix */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100869 TEST_DUP("namespace", "y", "z", "1");
870 TEST_DUP("prefix", "y", "z", "1");
871 TEST_DUP("contact", "a", "b", "1");
872 TEST_DUP("description", "a", "b", "1");
873 TEST_DUP("organization", "a", "b", "1");
874 TEST_DUP("reference", "a", "b", "1");
Radek Krejcia042ea12018-10-13 07:52:15 +0200875
Radek Krejci70853c52018-10-15 14:46:16 +0200876 /* not allowed in module (submodule-specific) */
877 str = SCHEMA_BEGINNING "belongs-to master {prefix m;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100878 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejci70853c52018-10-15 14:46:16 +0200879 logbuf_assert("Invalid keyword \"belongs-to\" as a child of \"module\". Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100880 mod = mod_renew(&ctx);
Radek Krejci70853c52018-10-15 14:46:16 +0200881
Radek Krejcia042ea12018-10-13 07:52:15 +0200882 /* anydata */
883 TEST_NODE(LYS_ANYDATA, "anydata test;}", "test");
884 /* anyxml */
885 TEST_NODE(LYS_ANYXML, "anyxml test;}", "test");
886 /* augment */
887 TEST_GENERIC("augment /somepath;}", mod->augments,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200888 assert_string_equal("/somepath", mod->augments[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200889 /* choice */
890 TEST_NODE(LYS_CHOICE, "choice test;}", "test");
891 /* contact 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100892 TEST_GENERIC("contact \"firstname\" + \n\t\" surname\";}", mod->mod->contact,
893 assert_string_equal("firstname surname", mod->mod->contact));
Radek Krejcia042ea12018-10-13 07:52:15 +0200894 /* container */
895 TEST_NODE(LYS_CONTAINER, "container test;}", "test");
896 /* description 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100897 TEST_GENERIC("description \'some description\';}", mod->mod->dsc,
898 assert_string_equal("some description", mod->mod->dsc));
Radek Krejcia042ea12018-10-13 07:52:15 +0200899 /* deviation */
900 TEST_GENERIC("deviation /somepath {deviate not-supported;}}", mod->deviations,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200901 assert_string_equal("/somepath", mod->deviations[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200902 /* extension */
903 TEST_GENERIC("extension test;}", mod->extensions,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200904 assert_string_equal("test", mod->extensions[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200905 /* feature */
906 TEST_GENERIC("feature test;}", mod->features,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200907 assert_string_equal("test", mod->features[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200908 /* grouping */
909 TEST_GENERIC("grouping grp;}", mod->groupings,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200910 assert_string_equal("grp", mod->groupings[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200911 /* identity */
912 TEST_GENERIC("identity test;}", mod->identities,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200913 assert_string_equal("test", mod->identities[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200914 /* import */
Radek Krejci086c7132018-10-26 15:29:04 +0200915 ly_ctx_set_module_imp_clb(ctx.ctx, test_imp_clb, "module zzz { namespace urn:zzz; prefix z;}");
916 TEST_GENERIC("import zzz {prefix z;}}", mod->imports,
917 assert_string_equal("zzz", mod->imports[0].name));
Radek Krejci70853c52018-10-15 14:46:16 +0200918
Radek Krejcia042ea12018-10-13 07:52:15 +0200919 /* import - prefix collision */
Radek Krejci086c7132018-10-26 15:29:04 +0200920 str = SCHEMA_BEGINNING "import zzz {prefix x;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100921 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejci70853c52018-10-15 14:46:16 +0200922 logbuf_assert("Prefix \"x\" already used as module prefix. Line number 2.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100923 mod = mod_renew(&ctx);
Radek Krejci086c7132018-10-26 15:29:04 +0200924 str = SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix y;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100925 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejci086c7132018-10-26 15:29:04 +0200926 logbuf_assert("Prefix \"y\" already used to import \"zzz\" module. Line number 2.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100927 mod = mod_renew(&ctx);
Radek Krejci086c7132018-10-26 15:29:04 +0200928 str = "module" SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix z;}}";
929 assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
930 assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx));
931 logbuf_assert("Single revision of the module \"zzz\" referred twice.");
Radek Krejci70853c52018-10-15 14:46:16 +0200932
Radek Krejcia042ea12018-10-13 07:52:15 +0200933 /* include */
Radek Krejci9ed7a192018-10-31 16:23:51 +0100934 store = 1;
Radek Krejcid33273d2018-10-25 14:55:52 +0200935 ly_ctx_set_module_imp_clb(ctx.ctx, test_imp_clb, "module xxx { namespace urn:xxx; prefix x;}");
Radek Krejci086c7132018-10-26 15:29:04 +0200936 str = "module" SCHEMA_BEGINNING "include xxx;}";
937 assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
938 assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx));
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100939 logbuf_assert("Input data contains module in situation when a submodule is expected.");
Radek Krejci9ed7a192018-10-31 16:23:51 +0100940 store = -1;
Radek Krejcid33273d2018-10-25 14:55:52 +0200941
Radek Krejci9ed7a192018-10-31 16:23:51 +0100942 store = 1;
Radek Krejci313d9902018-11-08 09:42:58 +0100943 ly_ctx_set_module_imp_clb(ctx.ctx, test_imp_clb, "submodule xxx {belongs-to wrong-name {prefix w;}}");
Radek Krejci086c7132018-10-26 15:29:04 +0200944 str = "module" SCHEMA_BEGINNING "include xxx;}";
945 assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
946 assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx));
947 logbuf_assert("Included \"xxx\" submodule from \"name\" belongs-to a different module \"wrong-name\".");
Radek Krejci9ed7a192018-10-31 16:23:51 +0100948 store = -1;
Radek Krejcid33273d2018-10-25 14:55:52 +0200949
Radek Krejci313d9902018-11-08 09:42:58 +0100950 ly_ctx_set_module_imp_clb(ctx.ctx, test_imp_clb, "submodule xxx {belongs-to name {prefix x;}}");
Radek Krejcid33273d2018-10-25 14:55:52 +0200951 TEST_GENERIC("include xxx;}", mod->includes,
Radek Krejci086c7132018-10-26 15:29:04 +0200952 assert_string_equal("xxx", mod->includes[0].name));
Radek Krejcid33273d2018-10-25 14:55:52 +0200953
Radek Krejcia042ea12018-10-13 07:52:15 +0200954 /* leaf */
955 TEST_NODE(LYS_LEAF, "leaf test {type string;}}", "test");
956 /* leaf-list */
957 TEST_NODE(LYS_LEAFLIST, "leaf-list test {type string;}}", "test");
958 /* list */
959 TEST_NODE(LYS_LIST, "list test {key a;leaf a {type string;}}}", "test");
960 /* notification */
961 TEST_GENERIC("notification test;}", mod->notifs,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200962 assert_string_equal("test", mod->notifs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200963 /* organization 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100964 TEST_GENERIC("organization \"CESNET a.l.e.\";}", mod->mod->org,
965 assert_string_equal("CESNET a.l.e.", mod->mod->org));
Radek Krejcia042ea12018-10-13 07:52:15 +0200966 /* reference 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100967 TEST_GENERIC("reference RFC7950;}", mod->mod->ref,
968 assert_string_equal("RFC7950", mod->mod->ref));
Radek Krejcia042ea12018-10-13 07:52:15 +0200969 /* revision */
970 TEST_GENERIC("revision 2018-10-12;}", mod->revs,
Radek Krejcib7db73a2018-10-24 14:18:40 +0200971 assert_string_equal("2018-10-12", mod->revs[0].date));
Radek Krejcia042ea12018-10-13 07:52:15 +0200972 /* rpc */
973 TEST_GENERIC("rpc test;}", mod->rpcs,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200974 assert_string_equal("test", mod->rpcs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200975 /* typedef */
976 TEST_GENERIC("typedef test{type string;}}", mod->typedefs,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200977 assert_string_equal("test", mod->typedefs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200978 /* uses */
979 TEST_NODE(LYS_USES, "uses test;}", "test");
980 /* yang-version */
Radek Krejci027d5802018-11-14 16:57:28 +0100981 str = SCHEMA_BEGINNING2 "\n\tyang-version 10;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100982 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejcia042ea12018-10-13 07:52:15 +0200983 logbuf_assert("Invalid value \"10\" of \"yang-version\". Line number 3.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100984 mod = mod_renew(&ctx);
Radek Krejci027d5802018-11-14 16:57:28 +0100985 str = SCHEMA_BEGINNING2 "yang-version 1.0;yang-version 1.1;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100986 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejcia042ea12018-10-13 07:52:15 +0200987 logbuf_assert("Duplicate keyword \"yang-version\". Line number 3.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100988 mod = mod_renew(&ctx);
Radek Krejci027d5802018-11-14 16:57:28 +0100989 str = SCHEMA_BEGINNING2 "yang-version 1.0;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100990 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod));
991 assert_int_equal(1, mod->mod->version);
Radek Krejci40544fa2019-01-11 09:38:37 +0100992 mod = mod_renew(&ctx);
Radek Krejci027d5802018-11-14 16:57:28 +0100993 str = SCHEMA_BEGINNING2 "yang-version \"1.1\";}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100994 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod));
995 assert_int_equal(2, mod->mod->version);
Radek Krejci40544fa2019-01-11 09:38:37 +0100996 mod = mod_renew(&ctx);
997
998 str = "module " SCHEMA_BEGINNING "} module q {namespace urn:q;prefixq;}";
999 m = mod->mod;
1000 free(mod);
1001 m->parsed = NULL;
1002 assert_int_equal(LY_EVALID, yang_parse_module(&ctx, str, m));
1003 logbuf_assert("Invalid character sequence \"module\", expected end-of-file. Line number 3.");
1004 mod = mod_renew(&ctx);
1005
1006 str = "prefix " SCHEMA_BEGINNING "}";
1007 m = mod->mod;
1008 free(mod);
1009 m->parsed = NULL;
1010 assert_int_equal(LY_EVALID, yang_parse_module(&ctx, str, m));
1011 logbuf_assert("Invalid keyword \"prefix\", expected \"module\" or \"submodule\". Line number 3.");
1012 mod = mod_renew(&ctx);
Radek Krejci09306362018-10-15 15:26:01 +02001013
Radek Krejci156ccaf2018-10-15 15:49:17 +02001014 /* extensions */
1015 TEST_GENERIC("prefix:test;}", mod->exts,
Radek Krejci2c4e7172018-10-19 15:56:26 +02001016 assert_string_equal("prefix:test", mod->exts[0].name);
1017 assert_int_equal(LYEXT_SUBSTMT_SELF, mod->exts[0].insubstmt));
Radek Krejci40544fa2019-01-11 09:38:37 +01001018 mod = mod_renew(&ctx);
Radek Krejci156ccaf2018-10-15 15:49:17 +02001019
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001020 /* invalid substatement */
1021 str = SCHEMA_BEGINNING "must false;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001022 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001023 logbuf_assert("Invalid keyword \"must\" as a child of \"module\". Line number 3.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001024 mod = mod_renew(&ctx);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001025
Radek Krejci09306362018-10-15 15:26:01 +02001026 /* submodule */
Radek Krejci40544fa2019-01-11 09:38:37 +01001027 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001028
1029 /* missing mandatory substatements */
1030 str = " subname {}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001031 lydict_remove(ctx.ctx, submod->name);
1032 assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod));
1033 assert_string_equal("subname", submod->name);
Radek Krejci09306362018-10-15 15:26:01 +02001034 logbuf_assert("Missing mandatory keyword \"belongs-to\" as a child of \"submodule\". Line number 3.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001035 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001036
Radek Krejci313d9902018-11-08 09:42:58 +01001037 str = " subname {belongs-to name {prefix x;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001038 lydict_remove(ctx.ctx, submod->name);
1039 assert_int_equal(LY_SUCCESS, parse_submodule(&ctx, &str, submod));
1040 assert_string_equal("name", submod->belongsto);
1041 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001042
1043#undef SCHEMA_BEGINNING
Radek Krejci313d9902018-11-08 09:42:58 +01001044#define SCHEMA_BEGINNING " subname {belongs-to name {prefix x;}"
Radek Krejci09306362018-10-15 15:26:01 +02001045
1046 /* duplicated namespace, prefix */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001047 str = " subname {belongs-to name {prefix x;}belongs-to module1;belongs-to module2;} ...";
1048 assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod)); \
1049 logbuf_assert("Duplicate keyword \"belongs-to\". Line number 3."); \
1050 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001051
1052 /* not allowed in submodule (module-specific) */
1053 str = SCHEMA_BEGINNING "namespace \"urn:z\";}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001054 assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod));
Radek Krejci09306362018-10-15 15:26:01 +02001055 logbuf_assert("Invalid keyword \"namespace\" as a child of \"submodule\". Line number 3.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001056 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001057 str = SCHEMA_BEGINNING "prefix m;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001058 assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod));
Radek Krejci09306362018-10-15 15:26:01 +02001059 logbuf_assert("Invalid keyword \"prefix\" as a child of \"submodule\". Line number 3.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001060 submod = submod_renew(&ctx, submod);
Radek Krejcia042ea12018-10-13 07:52:15 +02001061
Radek Krejci40544fa2019-01-11 09:38:37 +01001062 str = "submodule " SCHEMA_BEGINNING "} module q {namespace urn:q;prefixq;}";
1063 lysp_submodule_free(ctx.ctx, submod);
1064 submod = NULL;
1065 assert_int_equal(LY_EVALID, yang_parse_submodule(&ctx, str, &submod));
1066 logbuf_assert("Invalid character sequence \"module\", expected end-of-file. Line number 3.");
1067
1068 str = "prefix " SCHEMA_BEGINNING "}";
1069 assert_int_equal(LY_EVALID, yang_parse_submodule(&ctx, str, &submod));
1070 logbuf_assert("Invalid keyword \"prefix\", expected \"module\" or \"submodule\". Line number 3.");
1071 submod = submod_renew(&ctx, submod);
1072
Radek Krejcia042ea12018-10-13 07:52:15 +02001073#undef TEST_GENERIC
1074#undef TEST_NODE
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001075#undef TEST_DUP
Radek Krejcia042ea12018-10-13 07:52:15 +02001076#undef SCHEMA_BEGINNING
1077
Radek Krejci9fcacc12018-10-11 15:59:11 +02001078 lysp_module_free(mod);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001079 lysp_submodule_free(ctx.ctx, submod);
Radek Krejci9fcacc12018-10-11 15:59:11 +02001080 ly_ctx_destroy(ctx.ctx, NULL);
Radek Krejci40544fa2019-01-11 09:38:37 +01001081
1082 *state = NULL;
Radek Krejciefd22f62018-09-27 11:47:58 +02001083}
1084
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001085static void
1086test_identity(void **state)
1087{
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001088 *state = test_identity;
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001089
Radek Krejcie7b95092019-05-15 11:03:07 +02001090 struct lys_parser_ctx ctx;
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001091 struct lysp_ident *ident = NULL;
1092 const char *str;
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001093
1094 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1095 assert_non_null(ctx.ctx);
1096 ctx.line = 1;
1097 ctx.indent = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001098 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001099
1100 /* invalid cardinality */
1101#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001102 TEST_DUP_GENERIC(" test {", MEMBER, VALUE1, VALUE2, parse_identity, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001103 &ident, "1", FREE_ARRAY(ctx.ctx, ident, lysp_ident_free); ident = NULL)
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001104
Radek Krejci38222632019-02-12 16:55:05 +01001105 TEST_DUP("description", "a", "b");
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001106 TEST_DUP("reference", "a", "b");
1107 TEST_DUP("status", "current", "obsolete");
1108
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001109 /* full content */
1110 str = " test {base \"a\";base b; description text;reference \'another text\';status current; if-feature x;if-feature y;prefix:ext;} ...";
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001111 assert_int_equal(LY_SUCCESS, parse_identity(&ctx, &str, &ident));
1112 assert_non_null(ident);
1113 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001114 FREE_ARRAY(ctx.ctx, ident, lysp_ident_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001115 ident = NULL;
1116
1117 /* invalid substatement */
1118 str = " test {organization XXX;}";
1119 assert_int_equal(LY_EVALID, parse_identity(&ctx, &str, &ident));
1120 logbuf_assert("Invalid keyword \"organization\" as a child of \"identity\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001121 FREE_ARRAY(ctx.ctx, ident, lysp_ident_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001122 ident = NULL;
1123
1124#undef TEST_DUP
1125
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001126 *state = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001127 ly_ctx_destroy(ctx.ctx, NULL);
1128}
1129
1130static void
1131test_feature(void **state)
1132{
1133 (void) state; /* unused */
1134
Radek Krejcie7b95092019-05-15 11:03:07 +02001135 struct lys_parser_ctx ctx;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001136 struct lysp_feature *features = NULL;
1137 const char *str;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001138
1139 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1140 assert_non_null(ctx.ctx);
1141 ctx.line = 1;
1142 ctx.indent = 0;
1143
1144 /* invalid cardinality */
1145#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1146 TEST_DUP_GENERIC(" test {", MEMBER, VALUE1, VALUE2, parse_feature, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001147 &features, "1", FREE_ARRAY(ctx.ctx, features, lysp_feature_free); features = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001148
1149 TEST_DUP("description", "a", "b");
1150 TEST_DUP("reference", "a", "b");
1151 TEST_DUP("status", "current", "obsolete");
1152
1153 /* full content */
1154 str = " test {description text;reference \'another text\';status current; if-feature x;if-feature y;prefix:ext;} ...";
1155 assert_int_equal(LY_SUCCESS, parse_feature(&ctx, &str, &features));
1156 assert_non_null(features);
1157 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001158 FREE_ARRAY(ctx.ctx, features, lysp_feature_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001159 features = NULL;
1160
1161 /* invalid substatement */
1162 str = " test {organization XXX;}";
1163 assert_int_equal(LY_EVALID, parse_feature(&ctx, &str, &features));
1164 logbuf_assert("Invalid keyword \"organization\" as a child of \"feature\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001165 FREE_ARRAY(ctx.ctx, features, lysp_feature_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001166 features = NULL;
1167
1168#undef TEST_DUP
1169
1170 ly_ctx_destroy(ctx.ctx, NULL);
1171}
1172
1173static void
1174test_deviation(void **state)
1175{
1176 (void) state; /* unused */
1177
Radek Krejcie7b95092019-05-15 11:03:07 +02001178 struct lys_parser_ctx ctx;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001179 struct lysp_deviation *d = NULL;
1180 const char *str;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001181
1182 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1183 assert_non_null(ctx.ctx);
1184 ctx.line = 1;
1185 ctx.indent = 0;
1186
1187 /* invalid cardinality */
1188#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1189 TEST_DUP_GENERIC(" test {deviate not-supported;", MEMBER, VALUE1, VALUE2, parse_deviation, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001190 &d, "1", FREE_ARRAY(ctx.ctx, d, lysp_deviation_free); d = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001191
1192 TEST_DUP("description", "a", "b");
1193 TEST_DUP("reference", "a", "b");
1194
1195 /* full content */
1196 str = " test {deviate not-supported;description text;reference \'another text\';prefix:ext;} ...";
1197 assert_int_equal(LY_SUCCESS, parse_deviation(&ctx, &str, &d));
1198 assert_non_null(d);
1199 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001200 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001201 d = NULL;
1202
1203 /* missing mandatory substatement */
1204 str = " test {description text;}";
1205 assert_int_equal(LY_EVALID, parse_deviation(&ctx, &str, &d));
1206 logbuf_assert("Missing mandatory keyword \"deviate\" as a child of \"deviation\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001207 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001208 d = NULL;
1209
1210 /* invalid substatement */
1211 str = " test {deviate not-supported; status obsolete;}";
1212 assert_int_equal(LY_EVALID, parse_deviation(&ctx, &str, &d));
1213 logbuf_assert("Invalid keyword \"status\" as a child of \"deviation\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001214 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001215 d = NULL;
1216
1217#undef TEST_DUP
1218
1219 ly_ctx_destroy(ctx.ctx, NULL);
1220}
1221
1222static void
1223test_deviate(void **state)
1224{
1225 (void) state; /* unused */
1226
Radek Krejcie7b95092019-05-15 11:03:07 +02001227 struct lys_parser_ctx ctx;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001228 struct lysp_deviate *d = NULL;
1229 const char *str;
1230
1231 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1232 assert_non_null(ctx.ctx);
1233 ctx.line = 1;
1234 ctx.indent = 0;
1235
1236 /* invalid cardinality */
1237#define TEST_DUP(TYPE, MEMBER, VALUE1, VALUE2) \
1238 TEST_DUP_GENERIC(TYPE" {", MEMBER, VALUE1, VALUE2, parse_deviate, \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001239 &d, "1", lysp_deviate_free(ctx.ctx, d); free(d); d = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001240
1241 TEST_DUP("add", "config", "true", "false");
1242 TEST_DUP("replace", "default", "int8", "uint8");
1243 TEST_DUP("add", "mandatory", "true", "false");
1244 TEST_DUP("add", "max-elements", "1", "2");
1245 TEST_DUP("add", "min-elements", "1", "2");
1246 TEST_DUP("replace", "type", "int8", "uint8");
1247 TEST_DUP("add", "units", "kilometers", "miles");
1248
1249 /* full contents */
1250 str = " not-supported {prefix:ext;} ...";
1251 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1252 assert_non_null(d);
1253 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001254 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001255 str = " 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;} ...";
1256 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1257 assert_non_null(d);
1258 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001259 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001260 str = " delete {units meters; must 1; must 2; unique x; unique y; default a; default b; prefix:ext;} ...";
1261 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1262 assert_non_null(d);
1263 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001264 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001265 str = " replace {type string; units meters; default a; config true; mandatory true; min-elements 1; max-elements 2; prefix:ext;} ...";
1266 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1267 assert_non_null(d);
1268 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001269 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001270
1271 /* invalid substatements */
1272#define TEST_NOT_SUP(DEV, STMT, VALUE) \
1273 str = " "DEV" {"STMT" "VALUE";}..."; \
1274 assert_int_equal(LY_EVALID, parse_deviate(&ctx, &str, &d)); \
1275 logbuf_assert("Deviate \""DEV"\" does not support keyword \""STMT"\". Line number 1."); \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001276 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001277
1278 TEST_NOT_SUP("not-supported", "units", "meters");
1279 TEST_NOT_SUP("not-supported", "must", "1");
1280 TEST_NOT_SUP("not-supported", "unique", "x");
1281 TEST_NOT_SUP("not-supported", "default", "a");
1282 TEST_NOT_SUP("not-supported", "config", "true");
1283 TEST_NOT_SUP("not-supported", "mandatory", "true");
1284 TEST_NOT_SUP("not-supported", "min-elements", "1");
1285 TEST_NOT_SUP("not-supported", "max-elements", "2");
1286 TEST_NOT_SUP("not-supported", "type", "string");
1287 TEST_NOT_SUP("add", "type", "string");
1288 TEST_NOT_SUP("delete", "config", "true");
1289 TEST_NOT_SUP("delete", "mandatory", "true");
1290 TEST_NOT_SUP("delete", "min-elements", "1");
1291 TEST_NOT_SUP("delete", "max-elements", "2");
1292 TEST_NOT_SUP("delete", "type", "string");
1293 TEST_NOT_SUP("replace", "must", "1");
1294 TEST_NOT_SUP("replace", "unique", "a");
1295
1296 str = " nonsence; ...";
1297 assert_int_equal(LY_EVALID, parse_deviate(&ctx, &str, &d));
1298 logbuf_assert("Invalid value \"nonsence\" of \"deviate\". Line number 1.");
1299 assert_null(d);
1300
1301#undef TEST_NOT_SUP
1302#undef TEST_DUP
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001303
1304 ly_ctx_destroy(ctx.ctx, NULL);
1305}
1306
Radek Krejci8c370832018-11-02 15:10:03 +01001307static void
1308test_container(void **state)
1309{
1310 (void) state; /* unused */
1311
Radek Krejcie7b95092019-05-15 11:03:07 +02001312 struct lys_parser_ctx ctx = {0};
Radek Krejci8c370832018-11-02 15:10:03 +01001313 struct lysp_node_container *c = NULL;
1314 const char *str;
1315
1316 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1317 assert_non_null(ctx.ctx);
1318 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001319 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci8c370832018-11-02 15:10:03 +01001320
1321 /* invalid cardinality */
1322#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1323 str = "cont {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1324 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c)); \
1325 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001326 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001327
1328 TEST_DUP("config", "true", "false");
1329 TEST_DUP("description", "text1", "text2");
1330 TEST_DUP("presence", "true", "false");
1331 TEST_DUP("reference", "1", "2");
1332 TEST_DUP("status", "current", "obsolete");
1333 TEST_DUP("when", "true", "false");
1334#undef TEST_DUP
1335
1336 /* full content */
Radek Krejci313d9902018-11-08 09:42:58 +01001337 str = "cont {action x;anydata any;anyxml anyxml; choice ch;config false;container c;description test;grouping g;if-feature f; leaf l {type string;}"
1338 "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 Krejci8c370832018-11-02 15:10:03 +01001339 assert_int_equal(LY_SUCCESS, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1340 assert_non_null(c);
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001341 assert_int_equal(LYS_CONTAINER, c->nodetype);
1342 assert_string_equal("cont", c->name);
Radek Krejci8c370832018-11-02 15:10:03 +01001343 assert_non_null(c->actions);
1344 assert_non_null(c->child);
1345 assert_string_equal("test", c->dsc);
1346 assert_non_null(c->exts);
1347 assert_non_null(c->groupings);
1348 assert_non_null(c->iffeatures);
1349 assert_non_null(c->musts);
1350 assert_non_null(c->notifs);
1351 assert_string_equal("true", c->presence);
1352 assert_string_equal("test", c->ref);
1353 assert_non_null(c->typedefs);
1354 assert_non_null(c->when);
1355 assert_null(c->parent);
1356 assert_null(c->next);
1357 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR, c->flags);
Radek Krejci313d9902018-11-08 09:42:58 +01001358 ly_set_erase(&ctx.tpdfs_nodes, NULL);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001359 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001360
1361 /* invalid */
1362 str = " cont {augment /root;} ...";
1363 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1364 logbuf_assert("Invalid keyword \"augment\" as a child of \"container\". Line number 1.");
Radek Krejci4f28eda2018-11-12 11:46:16 +01001365 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001366 str = " cont {nonsence true;} ...";
1367 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1368 logbuf_assert("Invalid character sequence \"nonsence\", expected a keyword. Line number 1.");
Radek Krejci4f28eda2018-11-12 11:46:16 +01001369 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001370
Radek Krejcif538ce52019-03-05 10:46:14 +01001371 ctx.mod_version = 1; /* simulate YANG 1.0 */
1372 str = " cont {action x;} ...";
1373 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1374 logbuf_assert("Invalid keyword \"action\" as a child of \"container\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
1375 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
1376
Radek Krejci8c370832018-11-02 15:10:03 +01001377 ly_ctx_destroy(ctx.ctx, NULL);
1378}
1379
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001380static void
1381test_leaf(void **state)
1382{
1383 *state = test_leaf;
1384
Radek Krejcie7b95092019-05-15 11:03:07 +02001385 struct lys_parser_ctx ctx = {0};
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001386 struct lysp_node_leaf *l = NULL;
1387 const char *str;
1388
1389 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1390 assert_non_null(ctx.ctx);
1391 ctx.line = 1;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001392 //ctx.mod->version = 2; /* simulate YANG 1.1 */
1393
1394 /* invalid cardinality */
1395#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1396 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1397 assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l)); \
1398 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1399 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1400
1401 TEST_DUP("config", "true", "false");
1402 TEST_DUP("default", "x", "y");
1403 TEST_DUP("description", "text1", "text2");
1404 TEST_DUP("mandatory", "true", "false");
1405 TEST_DUP("reference", "1", "2");
1406 TEST_DUP("status", "current", "obsolete");
Radek Krejci0e5d8382018-11-28 16:37:53 +01001407 TEST_DUP("type", "int8", "uint8");
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001408 TEST_DUP("units", "text1", "text2");
1409 TEST_DUP("when", "true", "false");
1410#undef TEST_DUP
1411
1412 /* full content - without mandatory which is mutual exclusive with default */
1413 str = "l {config false;default \"xxx\";description test;if-feature f;"
1414 "must 'expr';reference test;status current;type string; units yyy;when true;m:ext;} ...";
1415 assert_int_equal(LY_SUCCESS, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
1416 assert_non_null(l);
1417 assert_int_equal(LYS_LEAF, l->nodetype);
1418 assert_string_equal("l", l->name);
1419 assert_string_equal("test", l->dsc);
1420 assert_string_equal("xxx", l->dflt);
1421 assert_string_equal("yyy", l->units);
1422 assert_string_equal("string", l->type.name);
1423 assert_non_null(l->exts);
1424 assert_non_null(l->iffeatures);
1425 assert_non_null(l->musts);
1426 assert_string_equal("test", l->ref);
1427 assert_non_null(l->when);
1428 assert_null(l->parent);
1429 assert_null(l->next);
1430 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR, l->flags);
1431 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1432
1433 /* full content - now with mandatory */
1434 str = "l {mandatory true; type string;} ...";
1435 assert_int_equal(LY_SUCCESS, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
1436 assert_non_null(l);
1437 assert_int_equal(LYS_LEAF, l->nodetype);
1438 assert_string_equal("l", l->name);
1439 assert_string_equal("string", l->type.name);
1440 assert_int_equal(LYS_MAND_TRUE, l->flags);
1441 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1442
1443 /* invalid */
1444 str = " l {mandatory true; default xx; type string;} ...";
1445 assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
Radek Krejcia9026eb2018-12-12 16:04:47 +01001446 logbuf_assert("Invalid combination of keywords \"mandatory\" and \"default\" as substatements of \"leaf\". Line number 1.");
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001447 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1448
1449 str = " l {description \"missing type\";} ...";
1450 assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
1451 logbuf_assert("Missing mandatory keyword \"type\" as a child of \"leaf\". Line number 1.");
1452 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1453
1454 *state = NULL;
1455 ly_ctx_destroy(ctx.ctx, NULL);
1456}
1457
Radek Krejci0e5d8382018-11-28 16:37:53 +01001458static void
1459test_leaflist(void **state)
1460{
1461 *state = test_leaf;
1462
Radek Krejcie7b95092019-05-15 11:03:07 +02001463 struct lys_parser_ctx ctx = {0};
Radek Krejci0e5d8382018-11-28 16:37:53 +01001464 struct lysp_node_leaflist *ll = NULL;
1465 const char *str;
1466
1467 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1468 assert_non_null(ctx.ctx);
1469 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001470 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci0e5d8382018-11-28 16:37:53 +01001471
1472 /* invalid cardinality */
1473#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1474 str = "ll {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1475 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll)); \
1476 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1477 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1478
1479 TEST_DUP("config", "true", "false");
1480 TEST_DUP("description", "text1", "text2");
1481 TEST_DUP("max-elements", "10", "20");
1482 TEST_DUP("min-elements", "10", "20");
1483 TEST_DUP("ordered-by", "user", "system");
1484 TEST_DUP("reference", "1", "2");
1485 TEST_DUP("status", "current", "obsolete");
1486 TEST_DUP("type", "int8", "uint8");
1487 TEST_DUP("units", "text1", "text2");
1488 TEST_DUP("when", "true", "false");
1489#undef TEST_DUP
1490
1491 /* full content - without min-elements which is mutual exclusive with default */
1492 str = "ll {config false;default \"xxx\"; default \"yyy\";description test;if-feature f;"
1493 "max-elements 10;must 'expr';ordered-by user;reference test;"
1494 "status current;type string; units zzz;when true;m:ext;} ...";
1495 assert_int_equal(LY_SUCCESS, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1496 assert_non_null(ll);
1497 assert_int_equal(LYS_LEAFLIST, ll->nodetype);
1498 assert_string_equal("ll", ll->name);
1499 assert_string_equal("test", ll->dsc);
1500 assert_non_null(ll->dflts);
1501 assert_int_equal(2, LY_ARRAY_SIZE(ll->dflts));
1502 assert_string_equal("xxx", ll->dflts[0]);
1503 assert_string_equal("yyy", ll->dflts[1]);
1504 assert_string_equal("zzz", ll->units);
1505 assert_int_equal(10, ll->max);
1506 assert_int_equal(0, ll->min);
1507 assert_string_equal("string", ll->type.name);
1508 assert_non_null(ll->exts);
1509 assert_non_null(ll->iffeatures);
1510 assert_non_null(ll->musts);
1511 assert_string_equal("test", ll->ref);
1512 assert_non_null(ll->when);
1513 assert_null(ll->parent);
1514 assert_null(ll->next);
1515 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_USER | LYS_SET_MAX, ll->flags);
1516 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1517
1518 /* full content - now with min-elements */
1519 str = "ll {min-elements 10; type string;} ...";
1520 assert_int_equal(LY_SUCCESS, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1521 assert_non_null(ll);
1522 assert_int_equal(LYS_LEAFLIST, ll->nodetype);
1523 assert_string_equal("ll", ll->name);
1524 assert_string_equal("string", ll->type.name);
1525 assert_int_equal(0, ll->max);
1526 assert_int_equal(10, ll->min);
1527 assert_int_equal(LYS_SET_MIN, ll->flags);
1528 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1529
1530 /* invalid */
1531 str = " ll {min-elements 1; default xx; type string;} ...";
1532 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
Radek Krejcia9026eb2018-12-12 16:04:47 +01001533 logbuf_assert("Invalid combination of keywords \"min-elements\" and \"default\" as substatements of \"leaf-list\". Line number 1.");
Radek Krejci0e5d8382018-11-28 16:37:53 +01001534 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1535
1536 str = " ll {description \"missing type\";} ...";
1537 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1538 logbuf_assert("Missing mandatory keyword \"type\" as a child of \"leaf-list\". Line number 1.");
1539 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1540
Radek Krejcidf6cad12018-11-28 17:10:55 +01001541 str = " ll {type string; min-elements 10; max-elements 1;} ..."; /* invalid combination of min/max */
1542 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1543 logbuf_assert("Invalid combination of min-elements and max-elements: min value 10 is bigger than the max value 1. Line number 1.");
1544 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1545
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001546 ctx.mod_version = 1; /* simulate YANG 1.0 - default statement is not allowed */
Radek Krejci0e5d8382018-11-28 16:37:53 +01001547 str = " ll {default xx; type string;} ...";
1548 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1549 logbuf_assert("Invalid keyword \"default\" as a child of \"leaf-list\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
1550 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1551
1552 *state = NULL;
1553 ly_ctx_destroy(ctx.ctx, NULL);
1554}
1555
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001556static void
1557test_list(void **state)
1558{
1559 *state = test_list;
1560
Radek Krejcie7b95092019-05-15 11:03:07 +02001561 struct lys_parser_ctx ctx = {0};
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001562 struct lysp_node_list *l = NULL;
1563 const char *str;
1564
1565 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1566 assert_non_null(ctx.ctx);
1567 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001568 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001569
1570 /* invalid cardinality */
1571#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1572 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1573 assert_int_equal(LY_EVALID, parse_list(&ctx, &str, NULL, (struct lysp_node**)&l)); \
1574 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1575 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1576
1577 TEST_DUP("config", "true", "false");
1578 TEST_DUP("description", "text1", "text2");
1579 TEST_DUP("key", "one", "two");
1580 TEST_DUP("max-elements", "10", "20");
1581 TEST_DUP("min-elements", "10", "20");
1582 TEST_DUP("ordered-by", "user", "system");
1583 TEST_DUP("reference", "1", "2");
1584 TEST_DUP("status", "current", "obsolete");
1585 TEST_DUP("when", "true", "false");
1586#undef TEST_DUP
1587
1588 /* full content */
1589 str = "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;}"
1590 "leaf-list ll {type string;} list li;max-elements 10; min-elements 1;must 'expr';notification not; ordered-by system; reference test;"
1591 "status current;typedef t {type int8;}unique xxx;unique yyy;uses g;when true;m:ext;} ...";
1592 assert_int_equal(LY_SUCCESS, parse_list(&ctx, &str, NULL, (struct lysp_node**)&l));
1593 assert_non_null(l);
1594 assert_int_equal(LYS_LIST, l->nodetype);
1595 assert_string_equal("l", l->name);
1596 assert_string_equal("test", l->dsc);
1597 assert_string_equal("l", l->key);
1598 assert_non_null(l->uniques);
1599 assert_int_equal(2, LY_ARRAY_SIZE(l->uniques));
1600 assert_string_equal("xxx", l->uniques[0]);
1601 assert_string_equal("yyy", l->uniques[1]);
1602 assert_int_equal(10, l->max);
1603 assert_int_equal(1, l->min);
1604 assert_non_null(l->exts);
1605 assert_non_null(l->iffeatures);
1606 assert_non_null(l->musts);
1607 assert_string_equal("test", l->ref);
1608 assert_non_null(l->when);
1609 assert_null(l->parent);
1610 assert_null(l->next);
1611 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM | LYS_SET_MAX | LYS_SET_MIN, l->flags);
1612 ly_set_erase(&ctx.tpdfs_nodes, NULL);
1613 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1614
Radek Krejcif538ce52019-03-05 10:46:14 +01001615 /* invalid content */
1616 ctx.mod_version = 1; /* simulate YANG 1.0 */
1617 str = "l {action x;} ...";
1618 assert_int_equal(LY_EVALID, parse_list(&ctx, &str, NULL, (struct lysp_node**)&l));
1619 logbuf_assert("Invalid keyword \"action\" as a child of \"list\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
1620 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1621
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001622 *state = NULL;
1623 ly_ctx_destroy(ctx.ctx, NULL);
1624}
1625
Radek Krejci056d0a82018-12-06 16:57:25 +01001626static void
1627test_choice(void **state)
1628{
1629 *state = test_choice;
1630
Radek Krejcie7b95092019-05-15 11:03:07 +02001631 struct lys_parser_ctx ctx = {0};
Radek Krejci056d0a82018-12-06 16:57:25 +01001632 struct lysp_node_choice *ch = NULL;
1633 const char *str;
1634
1635 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1636 assert_non_null(ctx.ctx);
1637 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001638 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci056d0a82018-12-06 16:57:25 +01001639
1640 /* invalid cardinality */
1641#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1642 str = "ch {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1643 assert_int_equal(LY_EVALID, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch)); \
1644 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1645 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1646
1647 TEST_DUP("config", "true", "false");
1648 TEST_DUP("default", "a", "b");
1649 TEST_DUP("description", "text1", "text2");
1650 TEST_DUP("mandatory", "true", "false");
1651 TEST_DUP("reference", "1", "2");
1652 TEST_DUP("status", "current", "obsolete");
1653 TEST_DUP("when", "true", "false");
1654#undef TEST_DUP
1655
Radek Krejcia9026eb2018-12-12 16:04:47 +01001656 /* full content - without default due to a collision with mandatory */
1657 str = "ch {anydata any;anyxml anyxml; case c;choice ch;config false;container c;description test;if-feature f;leaf l {type string;}"
Radek Krejci056d0a82018-12-06 16:57:25 +01001658 "leaf-list ll {type string;} list li;mandatory true;reference test;status current;when true;m:ext;} ...";
1659 assert_int_equal(LY_SUCCESS, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch));
1660 assert_non_null(ch);
1661 assert_int_equal(LYS_CHOICE, ch->nodetype);
1662 assert_string_equal("ch", ch->name);
1663 assert_string_equal("test", ch->dsc);
1664 assert_non_null(ch->exts);
1665 assert_non_null(ch->iffeatures);
1666 assert_string_equal("test", ch->ref);
1667 assert_non_null(ch->when);
1668 assert_null(ch->parent);
1669 assert_null(ch->next);
1670 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_MAND_TRUE, ch->flags);
1671 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1672
Radek Krejcia9026eb2018-12-12 16:04:47 +01001673 /* full content - the default missing from the previous node */
1674 str = "ch {default c;case c;} ...";
1675 assert_int_equal(LY_SUCCESS, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch));
1676 assert_non_null(ch);
1677 assert_int_equal(LYS_CHOICE, ch->nodetype);
1678 assert_string_equal("ch", ch->name);
1679 assert_string_equal("c", ch->dflt);
1680 assert_int_equal(0, ch->flags);
1681 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1682
1683 /* invalid content */
1684 str = "ch {mandatory true; default c1; case c1 {leaf x{type string;}}} ...";
1685 assert_int_equal(LY_EVALID, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch));
1686 logbuf_assert("Invalid combination of keywords \"mandatory\" and \"default\" as substatements of \"choice\". Line number 1.");
1687 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1688
1689 *state = NULL;
1690 ly_ctx_destroy(ctx.ctx, NULL);
1691}
1692
1693static void
1694test_case(void **state)
1695{
1696 *state = test_case;
1697
Radek Krejcie7b95092019-05-15 11:03:07 +02001698 struct lys_parser_ctx ctx = {0};
Radek Krejcia9026eb2018-12-12 16:04:47 +01001699 struct lysp_node_case *cs = NULL;
1700 const char *str;
1701
1702 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1703 assert_non_null(ctx.ctx);
1704 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001705 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejcia9026eb2018-12-12 16:04:47 +01001706
1707 /* invalid cardinality */
1708#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1709 str = "cs {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1710 assert_int_equal(LY_EVALID, parse_case(&ctx, &str, NULL, (struct lysp_node**)&cs)); \
1711 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1712 lysp_node_free(ctx.ctx, (struct lysp_node*)cs); cs = NULL;
1713
1714 TEST_DUP("description", "text1", "text2");
1715 TEST_DUP("reference", "1", "2");
1716 TEST_DUP("status", "current", "obsolete");
1717 TEST_DUP("when", "true", "false");
1718#undef TEST_DUP
1719
1720 /* full content */
1721 str = "cs {anydata any;anyxml anyxml; choice ch;container c;description test;if-feature f;leaf l {type string;}"
1722 "leaf-list ll {type string;} list li;reference test;status current;uses grp;when true;m:ext;} ...";
1723 assert_int_equal(LY_SUCCESS, parse_case(&ctx, &str, NULL, (struct lysp_node**)&cs));
1724 assert_non_null(cs);
1725 assert_int_equal(LYS_CASE, cs->nodetype);
1726 assert_string_equal("cs", cs->name);
1727 assert_string_equal("test", cs->dsc);
1728 assert_non_null(cs->exts);
1729 assert_non_null(cs->iffeatures);
1730 assert_string_equal("test", cs->ref);
1731 assert_non_null(cs->when);
1732 assert_null(cs->parent);
1733 assert_null(cs->next);
1734 assert_int_equal(LYS_STATUS_CURR, cs->flags);
1735 lysp_node_free(ctx.ctx, (struct lysp_node*)cs); cs = NULL;
1736
1737 /* invalid content */
1738 str = "cs {config true} ...";
1739 assert_int_equal(LY_EVALID, parse_case(&ctx, &str, NULL, (struct lysp_node**)&cs));
1740 logbuf_assert("Invalid keyword \"config\" as a child of \"case\". Line number 1.");
1741 lysp_node_free(ctx.ctx, (struct lysp_node*)cs); cs = NULL;
1742
Radek Krejci056d0a82018-12-06 16:57:25 +01001743 *state = NULL;
1744 ly_ctx_destroy(ctx.ctx, NULL);
1745}
1746
Radek Krejci9800fb82018-12-13 14:26:23 +01001747static void
1748test_any(void **state, enum yang_keyword kw)
1749{
1750 *state = test_any;
1751
Radek Krejcie7b95092019-05-15 11:03:07 +02001752 struct lys_parser_ctx ctx = {0};
Radek Krejci9800fb82018-12-13 14:26:23 +01001753 struct lysp_node_anydata *any = NULL;
1754 const char *str;
1755
1756 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1757 assert_non_null(ctx.ctx);
1758 ctx.line = 1;
Radek Krejci9800fb82018-12-13 14:26:23 +01001759 if (kw == YANG_ANYDATA) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001760 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci9800fb82018-12-13 14:26:23 +01001761 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001762 ctx.mod_version = 1; /* simulate YANG 1.0 */
Radek Krejci9800fb82018-12-13 14:26:23 +01001763 }
1764
1765 /* invalid cardinality */
1766#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1767 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1768 assert_int_equal(LY_EVALID, parse_any(&ctx, &str, kw, NULL, (struct lysp_node**)&any)); \
1769 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1770 lysp_node_free(ctx.ctx, (struct lysp_node*)any); any = NULL;
1771
1772 TEST_DUP("config", "true", "false");
1773 TEST_DUP("description", "text1", "text2");
1774 TEST_DUP("mandatory", "true", "false");
1775 TEST_DUP("reference", "1", "2");
1776 TEST_DUP("status", "current", "obsolete");
1777 TEST_DUP("when", "true", "false");
1778#undef TEST_DUP
1779
1780 /* full content */
1781 str = "any {config true;description test;if-feature f;mandatory true;must 'expr';reference test;status current;when true;m:ext;} ...";
1782 assert_int_equal(LY_SUCCESS, parse_any(&ctx, &str, kw, NULL, (struct lysp_node**)&any));
1783 assert_non_null(any);
1784 assert_int_equal(kw == YANG_ANYDATA ? LYS_ANYDATA : LYS_ANYXML, any->nodetype);
1785 assert_string_equal("any", any->name);
1786 assert_string_equal("test", any->dsc);
1787 assert_non_null(any->exts);
1788 assert_non_null(any->iffeatures);
1789 assert_non_null(any->musts);
1790 assert_string_equal("test", any->ref);
1791 assert_non_null(any->when);
1792 assert_null(any->parent);
1793 assert_null(any->next);
1794 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR | LYS_MAND_TRUE, any->flags);
1795 lysp_node_free(ctx.ctx, (struct lysp_node*)any); any = NULL;
1796
1797 *state = NULL;
1798 ly_ctx_destroy(ctx.ctx, NULL);
1799}
1800
1801static void
1802test_anydata(void **state)
1803{
1804 return test_any(state, YANG_ANYDATA);
1805}
1806
1807static void
1808test_anyxml(void **state)
1809{
1810 return test_any(state, YANG_ANYXML);
1811}
1812
Radek Krejcie86bf772018-12-14 11:39:53 +01001813static void
1814test_grouping(void **state)
1815{
1816 *state = test_grouping;
1817
Radek Krejcie7b95092019-05-15 11:03:07 +02001818 struct lys_parser_ctx ctx = {0};
Radek Krejcie86bf772018-12-14 11:39:53 +01001819 struct lysp_grp *grp = NULL;
1820 const char *str;
1821
1822 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1823 assert_non_null(ctx.ctx);
1824 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001825 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejcie86bf772018-12-14 11:39:53 +01001826
1827 /* invalid cardinality */
1828#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1829 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1830 assert_int_equal(LY_EVALID, parse_grouping(&ctx, &str, NULL, &grp)); \
1831 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1832 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1833
1834 TEST_DUP("description", "text1", "text2");
1835 TEST_DUP("reference", "1", "2");
1836 TEST_DUP("status", "current", "obsolete");
1837#undef TEST_DUP
1838
1839 /* full content */
1840 str = "grp {action x;anydata any;anyxml anyxml; choice ch;container c;description test;grouping g;leaf l {type string;}"
1841 "leaf-list ll {type string;} list li;notification not;reference test;status current;typedef t {type int8;}uses g;m:ext;} ...";
1842 assert_int_equal(LY_SUCCESS, parse_grouping(&ctx, &str, NULL, &grp));
1843 assert_non_null(grp);
1844 assert_int_equal(LYS_GROUPING, grp->nodetype);
1845 assert_string_equal("grp", grp->name);
1846 assert_string_equal("test", grp->dsc);
1847 assert_non_null(grp->exts);
1848 assert_string_equal("test", grp->ref);
1849 assert_null(grp->parent);
1850 assert_int_equal( LYS_STATUS_CURR, grp->flags);
1851 ly_set_erase(&ctx.tpdfs_nodes, NULL);
1852 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1853
1854 /* invalid content */
1855 str = "grp {config true} ...";
1856 assert_int_equal(LY_EVALID, parse_grouping(&ctx, &str, NULL, &grp));
1857 logbuf_assert("Invalid keyword \"config\" as a child of \"grouping\". Line number 1.");
1858 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1859
1860 str = "grp {must 'expr'} ...";
1861 assert_int_equal(LY_EVALID, parse_grouping(&ctx, &str, NULL, &grp));
1862 logbuf_assert("Invalid keyword \"must\" as a child of \"grouping\". Line number 1.");
1863 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1864
1865 *state = NULL;
1866 ly_ctx_destroy(ctx.ctx, NULL);
1867}
1868
1869static void
Radek Krejcif538ce52019-03-05 10:46:14 +01001870test_action(void **state)
1871{
1872 *state = test_action;
1873
Radek Krejcie7b95092019-05-15 11:03:07 +02001874 struct lys_parser_ctx ctx = {0};
Radek Krejcif538ce52019-03-05 10:46:14 +01001875 struct lysp_action *rpcs = NULL;
1876 struct lysp_node_container *c = NULL;
1877 const char *str;
1878
1879 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1880 assert_non_null(ctx.ctx);
1881 ctx.line = 1;
1882 ctx.mod_version = 2; /* simulate YANG 1.1 */
1883
1884 /* invalid cardinality */
1885#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1886 str = "func {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1887 assert_int_equal(LY_EVALID, parse_action(&ctx, &str, NULL, &rpcs)); \
1888 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1889 FREE_ARRAY(ctx.ctx, rpcs, lysp_action_free); rpcs = NULL;
1890
1891 TEST_DUP("description", "text1", "text2");
1892 TEST_DUP("input", "", "");
1893 TEST_DUP("output", "", "");
1894 TEST_DUP("reference", "1", "2");
1895 TEST_DUP("status", "current", "obsolete");
1896#undef TEST_DUP
1897
1898 /* full content */
1899 str = "top;";
1900 assert_int_equal(LY_SUCCESS, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1901 str = "func {description test;grouping grp;if-feature f;reference test;status current;typedef mytype {type int8;} m:ext;"
1902 "input {anydata a1; anyxml a2; choice ch; container c; grouping grp; leaf l {type int8;} leaf-list ll {type int8;}"
1903 " list li; must 1; typedef mytypei {type int8;} uses grp; m:ext;}"
1904 "output {anydata a1; anyxml a2; choice ch; container c; grouping grp; leaf l {type int8;} leaf-list ll {type int8;}"
1905 " list li; must 1; typedef mytypeo {type int8;} uses grp; m:ext;}} ...";
1906 assert_int_equal(LY_SUCCESS, parse_action(&ctx, &str, (struct lysp_node*)c, &rpcs));
1907 assert_non_null(rpcs);
1908 assert_int_equal(LYS_ACTION, rpcs->nodetype);
1909 assert_string_equal("func", rpcs->name);
1910 assert_string_equal("test", rpcs->dsc);
1911 assert_non_null(rpcs->exts);
1912 assert_non_null(rpcs->iffeatures);
1913 assert_string_equal("test", rpcs->ref);
1914 assert_non_null(rpcs->groupings);
1915 assert_non_null(rpcs->typedefs);
1916 assert_int_equal(LYS_STATUS_CURR, rpcs->flags);
1917 /* input */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001918 assert_int_equal(rpcs->input.nodetype, LYS_INPUT);
Radek Krejcif538ce52019-03-05 10:46:14 +01001919 assert_non_null(rpcs->input.groupings);
1920 assert_non_null(rpcs->input.exts);
1921 assert_non_null(rpcs->input.musts);
1922 assert_non_null(rpcs->input.typedefs);
1923 assert_non_null(rpcs->input.data);
1924 /* output */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001925 assert_int_equal(rpcs->output.nodetype, LYS_OUTPUT);
Radek Krejcif538ce52019-03-05 10:46:14 +01001926 assert_non_null(rpcs->output.groupings);
1927 assert_non_null(rpcs->output.exts);
1928 assert_non_null(rpcs->output.musts);
1929 assert_non_null(rpcs->output.typedefs);
1930 assert_non_null(rpcs->output.data);
1931
1932 ly_set_erase(&ctx.tpdfs_nodes, NULL);
1933 FREE_ARRAY(ctx.ctx, rpcs, lysp_action_free); rpcs = NULL;
1934
1935 /* invalid content */
1936 str = "func {config true} ...";
1937 assert_int_equal(LY_EVALID, parse_action(&ctx, &str, NULL, &rpcs));
1938 logbuf_assert("Invalid keyword \"config\" as a child of \"rpc\". Line number 1.");
1939 FREE_ARRAY(ctx.ctx, rpcs, lysp_action_free); rpcs = NULL;
1940
1941 *state = NULL;
1942 lysp_node_free(ctx.ctx, (struct lysp_node*)c);
1943 ly_ctx_destroy(ctx.ctx, NULL);
1944}
1945
1946static void
Radek Krejcifc11bd72019-04-11 16:00:05 +02001947test_notification(void **state)
1948{
1949 *state = test_notification;
1950
Radek Krejcie7b95092019-05-15 11:03:07 +02001951 struct lys_parser_ctx ctx = {0};
Radek Krejcifc11bd72019-04-11 16:00:05 +02001952 struct lysp_notif *notifs = NULL;
1953 struct lysp_node_container *c = NULL;
1954 const char *str;
1955
1956 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1957 assert_non_null(ctx.ctx);
1958 ctx.line = 1;
1959 ctx.mod_version = 2; /* simulate YANG 1.1 */
1960
1961 /* invalid cardinality */
1962#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1963 str = "func {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1964 assert_int_equal(LY_EVALID, parse_notif(&ctx, &str, NULL, &notifs)); \
1965 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1966 FREE_ARRAY(ctx.ctx, notifs, lysp_notif_free); notifs = NULL;
1967
1968 TEST_DUP("description", "text1", "text2");
1969 TEST_DUP("reference", "1", "2");
1970 TEST_DUP("status", "current", "obsolete");
1971#undef TEST_DUP
1972
1973 /* full content */
1974 str = "top;";
1975 assert_int_equal(LY_SUCCESS, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1976 str = "ntf {anydata a1; anyxml a2; choice ch; container c; description test; grouping grp; if-feature f; leaf l {type int8;}"
1977 "leaf-list ll {type int8;} list li; must 1; reference test; status current; typedef mytype {type int8;} uses grp; m:ext;}";
1978 assert_int_equal(LY_SUCCESS, parse_notif(&ctx, &str, (struct lysp_node*)c, &notifs));
1979 assert_non_null(notifs);
1980 assert_int_equal(LYS_NOTIF, notifs->nodetype);
1981 assert_string_equal("ntf", notifs->name);
1982 assert_string_equal("test", notifs->dsc);
1983 assert_non_null(notifs->exts);
1984 assert_non_null(notifs->iffeatures);
1985 assert_string_equal("test", notifs->ref);
1986 assert_non_null(notifs->groupings);
1987 assert_non_null(notifs->typedefs);
1988 assert_non_null(notifs->musts);
1989 assert_non_null(notifs->data);
1990 assert_int_equal(LYS_STATUS_CURR, notifs->flags);
1991
1992 ly_set_erase(&ctx.tpdfs_nodes, NULL);
1993 FREE_ARRAY(ctx.ctx, notifs, lysp_notif_free); notifs = NULL;
1994
1995 /* invalid content */
1996 str = "ntf {config true} ...";
1997 assert_int_equal(LY_EVALID, parse_notif(&ctx, &str, NULL, &notifs));
1998 logbuf_assert("Invalid keyword \"config\" as a child of \"notification\". Line number 1.");
1999 FREE_ARRAY(ctx.ctx, notifs, lysp_notif_free); notifs = NULL;
2000
2001 *state = NULL;
2002 lysp_node_free(ctx.ctx, (struct lysp_node*)c);
2003 ly_ctx_destroy(ctx.ctx, NULL);
2004}
2005
2006static void
Radek Krejcie86bf772018-12-14 11:39:53 +01002007test_uses(void **state)
2008{
2009 *state = test_uses;
2010
Radek Krejcie7b95092019-05-15 11:03:07 +02002011 struct lys_parser_ctx ctx = {0};
Radek Krejcie86bf772018-12-14 11:39:53 +01002012 struct lysp_node_uses *u = NULL;
2013 const char *str;
2014
2015 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
2016 assert_non_null(ctx.ctx);
2017 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002018 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejcie86bf772018-12-14 11:39:53 +01002019
2020 /* invalid cardinality */
2021#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
2022 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
2023 assert_int_equal(LY_EVALID, parse_uses(&ctx, &str, NULL, (struct lysp_node**)&u)); \
2024 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
2025 lysp_node_free(ctx.ctx, (struct lysp_node*)u); u = NULL;
2026
2027 TEST_DUP("description", "text1", "text2");
2028 TEST_DUP("reference", "1", "2");
2029 TEST_DUP("status", "current", "obsolete");
2030 TEST_DUP("when", "true", "false");
2031#undef TEST_DUP
2032
2033 /* full content */
2034 str = "grpref {augment some/node;description test;if-feature f;reference test;refine some/other/node;status current;when true;m:ext;} ...";
2035 assert_int_equal(LY_SUCCESS, parse_uses(&ctx, &str, NULL, (struct lysp_node**)&u));
2036 assert_non_null(u);
2037 assert_int_equal(LYS_USES, u->nodetype);
2038 assert_string_equal("grpref", u->name);
2039 assert_string_equal("test", u->dsc);
2040 assert_non_null(u->exts);
2041 assert_non_null(u->iffeatures);
2042 assert_string_equal("test", u->ref);
2043 assert_non_null(u->augments);
2044 assert_non_null(u->refines);
2045 assert_non_null(u->when);
2046 assert_null(u->parent);
2047 assert_null(u->next);
2048 assert_int_equal(LYS_STATUS_CURR, u->flags);
2049 lysp_node_free(ctx.ctx, (struct lysp_node*)u); u = NULL;
2050
2051 *state = NULL;
2052 ly_ctx_destroy(ctx.ctx, NULL);
2053}
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002054
2055
2056static void
2057test_augment(void **state)
2058{
2059 *state = test_augment;
2060
Radek Krejcie7b95092019-05-15 11:03:07 +02002061 struct lys_parser_ctx ctx = {0};
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002062 struct lysp_augment *a = NULL;
2063 const char *str;
2064
2065 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
2066 assert_non_null(ctx.ctx);
2067 ctx.line = 1;
Radek Krejcib4adbf12019-02-25 13:35:22 +01002068 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002069
2070 /* invalid cardinality */
2071#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
2072 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
2073 assert_int_equal(LY_EVALID, parse_augment(&ctx, &str, NULL, &a)); \
2074 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
Radek Krejcib4adbf12019-02-25 13:35:22 +01002075 FREE_ARRAY(ctx.ctx, a, lysp_augment_free); a = NULL;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002076
2077 TEST_DUP("description", "text1", "text2");
2078 TEST_DUP("reference", "1", "2");
2079 TEST_DUP("status", "current", "obsolete");
2080 TEST_DUP("when", "true", "false");
2081#undef TEST_DUP
2082
2083 /* full content */
2084 str = "/target/nodeid {action x; anydata any;anyxml anyxml; case cs; choice ch;container c;description test;if-feature f;leaf l {type string;}"
2085 "leaf-list ll {type string;} list li;notification not;reference test;status current;uses g;when true;m:ext;} ...";
2086 assert_int_equal(LY_SUCCESS, parse_augment(&ctx, &str, NULL, &a));
2087 assert_non_null(a);
2088 assert_int_equal(LYS_AUGMENT, a->nodetype);
2089 assert_string_equal("/target/nodeid", a->nodeid);
2090 assert_string_equal("test", a->dsc);
2091 assert_non_null(a->exts);
2092 assert_non_null(a->iffeatures);
2093 assert_string_equal("test", a->ref);
2094 assert_non_null(a->when);
2095 assert_null(a->parent);
2096 assert_int_equal(LYS_STATUS_CURR, a->flags);
Radek Krejcib4adbf12019-02-25 13:35:22 +01002097 FREE_ARRAY(ctx.ctx, a, lysp_augment_free); a = NULL;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002098
2099 *state = NULL;
2100 ly_ctx_destroy(ctx.ctx, NULL);
2101}
2102
Radek Krejci80dd33e2018-09-26 15:57:18 +02002103int main(void)
2104{
2105 const struct CMUnitTest tests[] = {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002106 cmocka_unit_test_setup(test_helpers, logger_setup),
Radek Krejci80dd33e2018-09-26 15:57:18 +02002107 cmocka_unit_test_setup(test_comments, logger_setup),
Radek Krejciefd22f62018-09-27 11:47:58 +02002108 cmocka_unit_test_setup(test_arg, logger_setup),
Radek Krejcidcc7b322018-10-11 14:24:02 +02002109 cmocka_unit_test_setup(test_stmts, logger_setup),
Radek Krejci05b13982018-11-28 16:22:07 +01002110 cmocka_unit_test_setup_teardown(test_minmax, logger_setup, logger_teardown),
Radek Krejci40544fa2019-01-11 09:38:37 +01002111 cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown),
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002112 cmocka_unit_test_setup_teardown(test_identity, logger_setup, logger_teardown),
Radek Krejci2c02f3e2018-10-16 10:54:38 +02002113 cmocka_unit_test_setup(test_feature, logger_setup),
2114 cmocka_unit_test_setup(test_deviation, logger_setup),
2115 cmocka_unit_test_setup(test_deviate, logger_setup),
Radek Krejci8c370832018-11-02 15:10:03 +01002116 cmocka_unit_test_setup(test_container, logger_setup),
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002117 cmocka_unit_test_setup_teardown(test_leaf, logger_setup, logger_teardown),
Radek Krejci0e5d8382018-11-28 16:37:53 +01002118 cmocka_unit_test_setup_teardown(test_leaflist, logger_setup, logger_teardown),
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002119 cmocka_unit_test_setup_teardown(test_list, logger_setup, logger_teardown),
Radek Krejci056d0a82018-12-06 16:57:25 +01002120 cmocka_unit_test_setup_teardown(test_choice, logger_setup, logger_teardown),
Radek Krejcia9026eb2018-12-12 16:04:47 +01002121 cmocka_unit_test_setup_teardown(test_case, logger_setup, logger_teardown),
Radek Krejci9800fb82018-12-13 14:26:23 +01002122 cmocka_unit_test_setup_teardown(test_anydata, logger_setup, logger_teardown),
2123 cmocka_unit_test_setup_teardown(test_anyxml, logger_setup, logger_teardown),
Radek Krejcif538ce52019-03-05 10:46:14 +01002124 cmocka_unit_test_setup_teardown(test_action, logger_setup, logger_teardown),
Radek Krejcifc11bd72019-04-11 16:00:05 +02002125 cmocka_unit_test_setup_teardown(test_notification, logger_setup, logger_teardown),
Radek Krejcie86bf772018-12-14 11:39:53 +01002126 cmocka_unit_test_setup_teardown(test_grouping, logger_setup, logger_teardown),
2127 cmocka_unit_test_setup_teardown(test_uses, logger_setup, logger_teardown),
Radek Krejcib4adbf12019-02-25 13:35:22 +01002128 cmocka_unit_test_setup_teardown(test_augment, logger_setup, logger_teardown),
Radek Krejci80dd33e2018-09-26 15:57:18 +02002129 };
2130
2131 return cmocka_run_group_tests(tests, NULL, NULL);
2132}