blob: 06cfecf62e2e2cbf602a159aadfa971ac2f70b31 [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"
Radek Krejcibbbbda92019-05-16 12:16:28 +020016#include "../../src/compat.c"
Radek Krejcif3f47842018-11-15 11:22:15 +010017#include "../../src/set.c"
18#include "../../src/log.c"
19#include "../../src/hash_table.c"
20#include "../../src/xpath.c"
Radek Krejci86d106e2018-10-18 09:53:19 +020021#include "../../src/parser_yang.c"
Radek Krejcif3f47842018-11-15 11:22:15 +010022#include "../../src/context.c"
23#include "../../src/tree_schema_helpers.c"
Radek Krejci19a96102018-11-15 13:38:09 +010024#include "../../src/tree_schema_free.c"
25#include "../../src/tree_schema_compile.c"
Radek Krejcif3f47842018-11-15 11:22:15 +010026#include "../../src/tree_schema.c"
Radek Krejcie7b95092019-05-15 11:03:07 +020027#include "../../src/plugins_types.c"
Radek Krejci86d106e2018-10-18 09:53:19 +020028
Radek Krejci80dd33e2018-09-26 15:57:18 +020029#include <stdarg.h>
30#include <stddef.h>
31#include <setjmp.h>
32#include <cmocka.h>
33
34#include <stdio.h>
35#include <string.h>
36
37#include "libyang.h"
Radek Krejci80dd33e2018-09-26 15:57:18 +020038
39#define BUFSIZE 1024
40char logbuf[BUFSIZE] = {0};
Radek Krejci9ed7a192018-10-31 16:23:51 +010041int store = -1; /* negative for infinite logging, positive for limited logging */
Radek Krejci80dd33e2018-09-26 15:57:18 +020042
43/* set to 0 to printing error messages to stderr instead of checking them in code */
Radek Krejci70853c52018-10-15 14:46:16 +020044#define ENABLE_LOGGER_CHECKING 1
Radek Krejci80dd33e2018-09-26 15:57:18 +020045
Radek Krejcid5f2b5f2018-10-11 10:54:36 +020046#if ENABLE_LOGGER_CHECKING
Radek Krejci80dd33e2018-09-26 15:57:18 +020047static void
48logger(LY_LOG_LEVEL level, const char *msg, const char *path)
49{
50 (void) level; /* unused */
Radek Krejci9ed7a192018-10-31 16:23:51 +010051 if (store) {
52 if (path && path[0]) {
53 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
54 } else {
55 strncpy(logbuf, msg, BUFSIZE - 1);
56 }
57 if (store > 0) {
58 --store;
59 }
Radek Krejci80dd33e2018-09-26 15:57:18 +020060 }
61}
Radek Krejcid5f2b5f2018-10-11 10:54:36 +020062#endif
Radek Krejci80dd33e2018-09-26 15:57:18 +020063
64static int
65logger_setup(void **state)
66{
67 (void) state; /* unused */
68#if ENABLE_LOGGER_CHECKING
69 ly_set_log_clb(logger, 1);
70#endif
71 return 0;
72}
73
Radek Krejcib1a5dcc2018-11-26 14:50:05 +010074static int
75logger_teardown(void **state)
76{
77 (void) state; /* unused */
78#if ENABLE_LOGGER_CHECKING
79 if (*state) {
80 fprintf(stderr, "%s\n", logbuf);
81 }
82#endif
83 return 0;
84}
85
Radek Krejci80dd33e2018-09-26 15:57:18 +020086void
87logbuf_clean(void)
88{
89 logbuf[0] = '\0';
90}
91
92#if ENABLE_LOGGER_CHECKING
93# define logbuf_assert(str) assert_string_equal(logbuf, str)
94#else
95# define logbuf_assert(str)
96#endif
97
Radek Krejci2c02f3e2018-10-16 10:54:38 +020098#define TEST_DUP_GENERIC(PREFIX, MEMBER, VALUE1, VALUE2, FUNC, RESULT, LINE, CLEANUP) \
99 str = PREFIX MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
100 assert_int_equal(LY_EVALID, FUNC(&ctx, &str, RESULT)); \
101 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number "LINE"."); \
102 CLEANUP
103
Radek Krejci44ceedc2018-10-02 15:54:31 +0200104static void
105test_helpers(void **state)
106{
107 (void) state; /* unused */
108
109 const char *str;
Radek Krejci404251e2018-10-09 12:06:44 +0200110 char *buf, *p;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200111 size_t len, size;
112 int prefix;
Radek Krejcie7b95092019-05-15 11:03:07 +0200113 struct lys_parser_ctx ctx;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200114 ctx.ctx = NULL;
115 ctx.line = 1;
116
117 /* storing into buffer */
118 str = "abcd";
119 buf = NULL;
120 size = len = 0;
121 assert_int_equal(LY_SUCCESS, buf_add_char(NULL, &str, 2, &buf, &size, &len));
122 assert_int_not_equal(0, size);
123 assert_int_equal(2, len);
124 assert_string_equal("cd", str);
125 assert_false(strncmp("ab", buf, 2));
126 free(buf);
Radek Krejci404251e2018-10-09 12:06:44 +0200127 buf = NULL;
128
129 /* invalid first characters */
130 len = 0;
131 str = "2invalid";
132 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1));
133 str = ".invalid";
134 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1));
135 str = "-invalid";
136 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1));
137 /* invalid following characters */
138 len = 3; /* number of characters read before the str content */
139 str = "!";
140 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1));
141 str = ":";
142 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1));
143 /* valid colon for prefixed identifiers */
144 len = size = 0;
145 p = NULL;
146 str = "x:id";
147 assert_int_equal(LY_SUCCESS, buf_store_char(&ctx, &str, Y_PREF_IDENTIF_ARG, &p, &len, &buf, &size, 0));
148 assert_int_equal(1, len);
149 assert_null(buf);
150 assert_string_equal(":id", str);
151 assert_int_equal('x', p[len - 1]);
152 assert_int_equal(LY_SUCCESS, buf_store_char(&ctx, &str, Y_PREF_IDENTIF_ARG, &p, &len, &buf, &size, 1));
153 assert_int_equal(2, len);
154 assert_string_equal("id", str);
155 assert_int_equal(':', p[len - 1]);
156 free(buf);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200157
158 /* checking identifiers */
159 assert_int_equal(LY_EVALID, check_identifierchar(&ctx, ':', 0, NULL));
160 logbuf_assert("Invalid identifier character ':'. Line number 1.");
161 assert_int_equal(LY_EVALID, check_identifierchar(&ctx, '#', 1, NULL));
162 logbuf_assert("Invalid identifier first character '#'. Line number 1.");
163
164 assert_int_equal(LY_SUCCESS, check_identifierchar(&ctx, 'a', 1, &prefix));
165 assert_int_equal(0, prefix);
166 assert_int_equal(LY_SUCCESS, check_identifierchar(&ctx, ':', 0, &prefix));
167 assert_int_equal(1, prefix);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200168 assert_int_equal(LY_EVALID, check_identifierchar(&ctx, ':', 0, &prefix));
169 assert_int_equal(1, prefix);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200170 assert_int_equal(LY_SUCCESS, check_identifierchar(&ctx, 'b', 0, &prefix));
171 assert_int_equal(2, prefix);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200172 /* second colon is invalid */
173 assert_int_equal(LY_EVALID, check_identifierchar(&ctx, ':', 0, &prefix));
174 logbuf_assert("Invalid identifier character ':'. Line number 1.");
Radek Krejci44ceedc2018-10-02 15:54:31 +0200175}
Radek Krejci80dd33e2018-09-26 15:57:18 +0200176
177static void
178test_comments(void **state)
179{
180 (void) state; /* unused */
181
Radek Krejcie7b95092019-05-15 11:03:07 +0200182 struct lys_parser_ctx ctx;
Radek Krejci80dd33e2018-09-26 15:57:18 +0200183 const char *str, *p;
Radek Krejciefd22f62018-09-27 11:47:58 +0200184 char *word, *buf;
185 size_t len;
Radek Krejci80dd33e2018-09-26 15:57:18 +0200186
Radek Krejci44ceedc2018-10-02 15:54:31 +0200187 ctx.ctx = NULL;
188 ctx.line = 1;
189
Radek Krejciefd22f62018-09-27 11:47:58 +0200190 str = " // this is a text of / one * line */ comment\nargument";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200191 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200192 assert_string_equal("argument", word);
193 assert_null(buf);
194 assert_int_equal(8, len);
Radek Krejci80dd33e2018-09-26 15:57:18 +0200195
Radek Krejciefd22f62018-09-27 11:47:58 +0200196 str = "/* this is a \n * text // of / block * comment */\"arg\" + \"ume\" \n + \n \"nt\"";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200197 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200198 assert_string_equal("argument", word);
199 assert_ptr_equal(buf, word);
200 assert_int_equal(8, len);
201 free(word);
Radek Krejci80dd33e2018-09-26 15:57:18 +0200202
203 str = p = " this is one line comment on last line";
Radek Krejci44ceedc2018-10-02 15:54:31 +0200204 assert_int_equal(LY_SUCCESS, skip_comment(&ctx, &str, 1));
Radek Krejci80dd33e2018-09-26 15:57:18 +0200205 assert_true(str[0] == '\0');
206
207 str = p = " this is a not terminated comment x";
Radek Krejci44ceedc2018-10-02 15:54:31 +0200208 assert_int_equal(LY_EVALID, skip_comment(&ctx, &str, 2));
209 logbuf_assert("Unexpected end-of-file, non-terminated comment. Line number 5.");
Radek Krejci80dd33e2018-09-26 15:57:18 +0200210 assert_true(str[0] == '\0');
211}
212
Radek Krejciefd22f62018-09-27 11:47:58 +0200213static void
214test_arg(void **state)
215{
216 (void) state; /* unused */
217
Radek Krejcie7b95092019-05-15 11:03:07 +0200218 struct lys_parser_ctx ctx;
Radek Krejciefd22f62018-09-27 11:47:58 +0200219 const char *str;
220 char *word, *buf;
221 size_t len;
222
Radek Krejci44ceedc2018-10-02 15:54:31 +0200223 ctx.ctx = NULL;
224 ctx.line = 1;
225
Radek Krejciefd22f62018-09-27 11:47:58 +0200226 /* missing argument */
227 str = ";";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200228 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_MAYBE_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200229 assert_null(word);
230
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200231 str = "{";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200232 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200233 logbuf_assert("Invalid character sequence \"{\", expected an argument. Line number 1.");
234
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200235 /* invalid escape sequence */
236 str = "\"\\s\"";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200237 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200238 logbuf_assert("Double-quoted string unknown special character \'\\s\'. Line number 1.");
239 str = "\'\\s\'"; /* valid, since it is not an escape sequence in single quoted string */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200240 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200241 assert_int_equal(2, len);
242 assert_string_equal("\\s\'", word);
243 assert_int_equal('\0', str[0]); /* input has been eaten */
244
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200245 /* invalid character after the argument */
246 str = "hello\"";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200247 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200248 logbuf_assert("Invalid character sequence \"\"\", expected unquoted string character, optsep, semicolon or opening brace. Line number 1.");
249 str = "hello}";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200250 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200251 logbuf_assert("Invalid character sequence \"}\", expected unquoted string character, optsep, semicolon or opening brace. Line number 1.");
252
253 str = "hello/x\t"; /* slash is not an invalid character */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200254 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200255 assert_int_equal(7, len);
256 assert_string_equal("hello/x\t", word);
257
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200258 assert_null(buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200259
260 /* different quoting */
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200261 str = "hello ";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200262 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200263 assert_null(buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200264 assert_int_equal(5, len);
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200265 assert_string_equal("hello ", word);
Radek Krejciefd22f62018-09-27 11:47:58 +0200266
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200267 str = "hello/*comment*/\n";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200268 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200269 assert_null(buf);
270 assert_int_equal(5, len);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200271 assert_false(strncmp("hello", word, len));
272
273
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200274 str = "\"hello\\n\\t\\\"\\\\\";";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200275 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200276 assert_null(buf);
277 assert_int_equal(9, len);
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200278 assert_string_equal("hello\\n\\t\\\"\\\\\";", word);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200279
280 ctx.indent = 14;
281 str = "\"hello \t\n\t\t world!\"";
282 /* - space and tabs before newline are stripped out
283 * - space and tabs after newline (indentation) are stripped out
284 */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200285 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200286 assert_non_null(buf);
287 assert_ptr_equal(word, buf);
288 assert_int_equal(14, len);
289 assert_string_equal("hello\n world!", word);
290 free(buf);
291
292 ctx.indent = 14;
293 str = "\"hello\n \tworld!\"";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200294 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200295 assert_non_null(buf);
296 assert_ptr_equal(word, buf);
297 assert_int_equal(12, len);
298 assert_string_equal("hello\nworld!", word);
299 free(buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200300
301 str = "\'hello\'";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200302 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200303 assert_null(buf);
304 assert_int_equal(5, len);
305 assert_false(strncmp("hello", word, 5));
306
307 str = "\"hel\" +\t\n\"lo\"";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200308 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200309 assert_ptr_equal(word, buf);
310 assert_int_equal(5, len);
311 assert_string_equal("hello", word);
312 free(buf);
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200313 str = "\"hel\" +\t\nlo"; /* unquoted the second part */
Radek Krejcid3ca0632019-04-16 16:54:54 +0200314 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200315 logbuf_assert("Both string parts divided by '+' must be quoted. Line number 5.");
Radek Krejciefd22f62018-09-27 11:47:58 +0200316
317 str = "\'he\'\t\n+ \"llo\"";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200318 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200319 assert_ptr_equal(word, buf);
320 assert_int_equal(5, len);
321 assert_string_equal("hello", word);
322 free(buf);
323
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200324 str = " \t\n\"he\"+\'llo\'";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200325 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200326 assert_ptr_equal(word, buf);
327 assert_int_equal(5, len);
328 assert_string_equal("hello", word);
329 free(buf);
330
Radek Krejci44ceedc2018-10-02 15:54:31 +0200331 /* missing argument */
332 str = ";";
Radek Krejcid3ca0632019-04-16 16:54:54 +0200333 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, NULL, &word, &buf, &len));
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200334 logbuf_assert("Invalid character sequence \";\", expected an argument. Line number 7.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200335}
336
337static void
338test_stmts(void **state)
339{
340 (void) state; /* unused */
341
Radek Krejcie7b95092019-05-15 11:03:07 +0200342 struct lys_parser_ctx ctx;
Radek Krejcidcc7b322018-10-11 14:24:02 +0200343 const char *str, *p;
344 enum yang_keyword kw;
345 char *word;
346 size_t len;
347
348 ctx.ctx = NULL;
349 ctx.line = 1;
350
351 str = "\n// comment\n\tinput\t{";
352 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
353 assert_int_equal(YANG_INPUT, kw);
354 assert_int_equal(5, len);
355 assert_string_equal("input\t{", word);
356 assert_string_equal("\t{", str);
357
358 str = "\t /* comment */\t output\n\t{";
359 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
360 assert_int_equal(YANG_OUTPUT, kw);
361 assert_int_equal(6, len);
362 assert_string_equal("output\n\t{", word);
363 assert_string_equal("\n\t{", str);
364
365 str = "/input { "; /* invalid slash */
366 assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len));
367 logbuf_assert("Invalid identifier first character '/'. Line number 4.");
368
369 str = "not-a-statement-nor-extension { "; /* invalid identifier */
370 assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len));
371 logbuf_assert("Invalid character sequence \"not-a-statement-nor-extension\", expected a keyword. Line number 4.");
372
373 str = "path;"; /* missing sep after the keyword */
374 assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len));
375 logbuf_assert("Invalid character sequence \"path;\", expected a keyword followed by a separator. Line number 4.");
376
377 str = "action ";
378 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
379 assert_int_equal(YANG_ACTION, kw);
380 assert_int_equal(6, len);
381 str = "anydata ";
382 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
383 assert_int_equal(YANG_ANYDATA, kw);
384 assert_int_equal(7, len);
385 str = "anyxml ";
386 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
387 assert_int_equal(YANG_ANYXML, kw);
388 assert_int_equal(6, len);
389 str = "argument ";
390 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
391 assert_int_equal(YANG_ARGUMENT, kw);
392 assert_int_equal(8, len);
393 str = "augment ";
394 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
395 assert_int_equal(YANG_AUGMENT, kw);
396 assert_int_equal(7, len);
397 str = "base ";
398 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
399 assert_int_equal(YANG_BASE, kw);
400 assert_int_equal(4, len);
401 str = "belongs-to ";
402 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
403 assert_int_equal(YANG_BELONGS_TO, kw);
404 assert_int_equal(10, len);
405 str = "bit ";
406 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
407 assert_int_equal(YANG_BIT, kw);
408 assert_int_equal(3, len);
409 str = "case ";
410 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
411 assert_int_equal(YANG_CASE, kw);
412 assert_int_equal(4, len);
413 str = "choice ";
414 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
415 assert_int_equal(YANG_CHOICE, kw);
416 assert_int_equal(6, len);
417 str = "config ";
418 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
419 assert_int_equal(YANG_CONFIG, kw);
420 assert_int_equal(6, len);
421 str = "contact ";
422 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
423 assert_int_equal(YANG_CONTACT, kw);
424 assert_int_equal(7, len);
425 str = "container ";
426 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
427 assert_int_equal(YANG_CONTAINER, kw);
428 assert_int_equal(9, len);
429 str = "default ";
430 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
431 assert_int_equal(YANG_DEFAULT, kw);
432 assert_int_equal(7, len);
433 str = "description ";
434 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
435 assert_int_equal(YANG_DESCRIPTION, kw);
436 assert_int_equal(11, len);
437 str = "deviate ";
438 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
439 assert_int_equal(YANG_DEVIATE, kw);
440 assert_int_equal(7, len);
441 str = "deviation ";
442 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
443 assert_int_equal(YANG_DEVIATION, kw);
444 assert_int_equal(9, len);
445 str = "enum ";
446 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
447 assert_int_equal(YANG_ENUM, kw);
448 assert_int_equal(4, len);
449 str = "error-app-tag ";
450 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
451 assert_int_equal(YANG_ERROR_APP_TAG, kw);
452 assert_int_equal(13, len);
453 str = "error-message ";
454 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
455 assert_int_equal(YANG_ERROR_MESSAGE, kw);
456 assert_int_equal(13, len);
457 str = "extension ";
458 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
459 assert_int_equal(YANG_EXTENSION, kw);
460 assert_int_equal(9, len);
461 str = "feature ";
462 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
463 assert_int_equal(YANG_FEATURE, kw);
464 assert_int_equal(7, len);
465 str = "fraction-digits ";
466 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
467 assert_int_equal(YANG_FRACTION_DIGITS, kw);
468 assert_int_equal(15, len);
469 str = "grouping ";
470 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
471 assert_int_equal(YANG_GROUPING, kw);
472 assert_int_equal(8, len);
473 str = "identity ";
474 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
475 assert_int_equal(YANG_IDENTITY, kw);
476 assert_int_equal(8, len);
477 str = "if-feature ";
478 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
479 assert_int_equal(YANG_IF_FEATURE, kw);
480 assert_int_equal(10, len);
481 str = "import ";
482 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
483 assert_int_equal(YANG_IMPORT, kw);
484 assert_int_equal(6, len);
485 str = "include ";
486 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
487 assert_int_equal(YANG_INCLUDE, kw);
488 assert_int_equal(7, len);
489 str = "input{";
490 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
491 assert_int_equal(YANG_INPUT, kw);
492 assert_int_equal(5, len);
493 str = "key ";
494 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
495 assert_int_equal(YANG_KEY, kw);
496 assert_int_equal(3, len);
497 str = "leaf ";
498 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
499 assert_int_equal(YANG_LEAF, kw);
500 assert_int_equal(4, len);
501 str = "leaf-list ";
502 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
503 assert_int_equal(YANG_LEAF_LIST, kw);
504 assert_int_equal(9, len);
505 str = "length ";
506 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
507 assert_int_equal(YANG_LENGTH, kw);
508 assert_int_equal(6, len);
509 str = "list ";
510 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
511 assert_int_equal(YANG_LIST, kw);
512 assert_int_equal(4, len);
513 str = "mandatory ";
514 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
515 assert_int_equal(YANG_MANDATORY, kw);
516 assert_int_equal(9, len);
517 str = "max-elements ";
518 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
519 assert_int_equal(YANG_MAX_ELEMENTS, kw);
520 assert_int_equal(12, len);
521 str = "min-elements ";
522 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
523 assert_int_equal(YANG_MIN_ELEMENTS, kw);
524 assert_int_equal(12, len);
525 str = "modifier ";
526 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
527 assert_int_equal(YANG_MODIFIER, kw);
528 assert_int_equal(8, len);
529 str = "module ";
530 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
531 assert_int_equal(YANG_MODULE, kw);
532 assert_int_equal(6, len);
533 str = "must ";
534 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
535 assert_int_equal(YANG_MUST, kw);
536 assert_int_equal(4, len);
537 str = "namespace ";
538 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
539 assert_int_equal(YANG_NAMESPACE, kw);
540 assert_int_equal(9, len);
541 str = "notification ";
542 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
543 assert_int_equal(YANG_NOTIFICATION, kw);
544 assert_int_equal(12, len);
545 str = "ordered-by ";
546 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
547 assert_int_equal(YANG_ORDERED_BY, kw);
548 assert_int_equal(10, len);
549 str = "organization ";
550 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
551 assert_int_equal(YANG_ORGANIZATION, kw);
552 assert_int_equal(12, len);
553 str = "output ";
554 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
555 assert_int_equal(YANG_OUTPUT, kw);
556 assert_int_equal(6, len);
557 str = "path ";
558 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
559 assert_int_equal(YANG_PATH, kw);
560 assert_int_equal(4, len);
561 str = "pattern ";
562 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
563 assert_int_equal(YANG_PATTERN, kw);
564 assert_int_equal(7, len);
565 str = "position ";
566 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
567 assert_int_equal(YANG_POSITION, kw);
568 assert_int_equal(8, len);
569 str = "prefix ";
570 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
571 assert_int_equal(YANG_PREFIX, kw);
572 assert_int_equal(6, len);
573 str = "presence ";
574 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
575 assert_int_equal(YANG_PRESENCE, kw);
576 assert_int_equal(8, len);
577 str = "range ";
578 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
579 assert_int_equal(YANG_RANGE, kw);
580 assert_int_equal(5, len);
581 str = "reference ";
582 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
583 assert_int_equal(YANG_REFERENCE, kw);
584 assert_int_equal(9, len);
585 str = "refine ";
586 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
587 assert_int_equal(YANG_REFINE, kw);
588 assert_int_equal(6, len);
589 str = "require-instance ";
590 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
591 assert_int_equal(YANG_REQUIRE_INSTANCE, kw);
592 assert_int_equal(16, len);
593 str = "revision ";
594 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
595 assert_int_equal(YANG_REVISION, kw);
596 assert_int_equal(8, len);
597 str = "revision-date ";
598 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
599 assert_int_equal(YANG_REVISION_DATE, kw);
600 assert_int_equal(13, len);
601 str = "rpc ";
602 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
603 assert_int_equal(YANG_RPC, kw);
604 assert_int_equal(3, len);
605 str = "status ";
606 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
607 assert_int_equal(YANG_STATUS, kw);
608 assert_int_equal(6, len);
609 str = "submodule ";
610 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
611 assert_int_equal(YANG_SUBMODULE, kw);
612 assert_int_equal(9, len);
613 str = "type ";
614 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
615 assert_int_equal(YANG_TYPE, kw);
616 assert_int_equal(4, len);
617 str = "typedef ";
618 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
619 assert_int_equal(YANG_TYPEDEF, kw);
620 assert_int_equal(7, len);
621 str = "unique ";
622 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
623 assert_int_equal(YANG_UNIQUE, kw);
624 assert_int_equal(6, len);
625 str = "units ";
626 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
627 assert_int_equal(YANG_UNITS, kw);
628 assert_int_equal(5, len);
629 str = "uses ";
630 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
631 assert_int_equal(YANG_USES, kw);
632 assert_int_equal(4, len);
633 str = "value ";
634 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
635 assert_int_equal(YANG_VALUE, kw);
636 assert_int_equal(5, len);
637 str = "when ";
638 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
639 assert_int_equal(YANG_WHEN, kw);
640 assert_int_equal(4, len);
641 str = "yang-version ";
642 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
643 assert_int_equal(YANG_YANG_VERSION, kw);
644 assert_int_equal(12, len);
645 str = "yin-element ";
646 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
647 assert_int_equal(YANG_YIN_ELEMENT, kw);
648 assert_int_equal(11, len);
Radek Krejci626df482018-10-11 15:06:31 +0200649 str = ";config false;";
650 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
651 assert_int_equal(YANG_SEMICOLON, kw);
652 assert_int_equal(1, len);
653 assert_string_equal("config false;", str);
654 str = "{ config false;";
655 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
656 assert_int_equal(YANG_LEFT_BRACE, kw);
657 assert_int_equal(1, len);
658 assert_string_equal(" config false;", str);
659 str = "}";
660 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
661 assert_int_equal(YANG_RIGHT_BRACE, kw);
662 assert_int_equal(1, len);
663 assert_string_equal("", str);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200664
665 /* geenric extension */
666 str = p = "nacm:default-deny-write;";
667 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
668 assert_int_equal(YANG_CUSTOM, kw);
669 assert_int_equal(23, len);
670 assert_ptr_equal(p, word);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200671}
Radek Krejci44ceedc2018-10-02 15:54:31 +0200672
Radek Krejci05b13982018-11-28 16:22:07 +0100673static void
674test_minmax(void **state)
675{
676 *state = test_minmax;
677
Radek Krejcie7b95092019-05-15 11:03:07 +0200678 struct lys_parser_ctx ctx = {0};
Radek Krejci05b13982018-11-28 16:22:07 +0100679 uint16_t flags = 0;
680 uint32_t value = 0;
681 struct lysp_ext_instance *ext = NULL;
682 const char *str;
683
684 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
685 assert_non_null(ctx.ctx);
686 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100687 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci05b13982018-11-28 16:22:07 +0100688
Radek Krejcidf6cad12018-11-28 17:10:55 +0100689 str = " 1invalid; ...";
Radek Krejci05b13982018-11-28 16:22:07 +0100690 assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext));
Radek Krejcidf6cad12018-11-28 17:10:55 +0100691 logbuf_assert("Invalid value \"1invalid\" of \"min-elements\". Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100692
693 flags = value = 0;
694 str = " -1; ...";
695 assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext));
696 logbuf_assert("Invalid value \"-1\" of \"min-elements\". Line number 1.");
697
Radek Krejcidf6cad12018-11-28 17:10:55 +0100698 /* implementation limit */
699 flags = value = 0;
700 str = " 4294967296; ...";
701 assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext));
702 logbuf_assert("Value \"4294967296\" is out of \"min-elements\" bounds. Line number 1.");
703
Radek Krejci05b13982018-11-28 16:22:07 +0100704 flags = value = 0;
705 str = " 1; ...";
706 assert_int_equal(LY_SUCCESS, parse_minelements(&ctx, &str, &value, &flags, &ext));
707 assert_int_equal(LYS_SET_MIN, flags);
708 assert_int_equal(1, value);
709
710 flags = value = 0;
711 str = " 1 {m:ext;} ...";
712 assert_int_equal(LY_SUCCESS, parse_minelements(&ctx, &str, &value, &flags, &ext));
713 assert_int_equal(LYS_SET_MIN, flags);
714 assert_int_equal(1, value);
715 assert_non_null(ext);
716 FREE_ARRAY(ctx.ctx, ext, lysp_ext_instance_free);
717 ext = NULL;
718
719 flags = value = 0;
720 str = " 1 {config true;} ...";
721 assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext));
722 logbuf_assert("Invalid keyword \"config\" as a child of \"min-elements\". Line number 1.");
723
Radek Krejcidf6cad12018-11-28 17:10:55 +0100724 str = " 1invalid; ...";
Radek Krejci05b13982018-11-28 16:22:07 +0100725 assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext));
Radek Krejcidf6cad12018-11-28 17:10:55 +0100726 logbuf_assert("Invalid value \"1invalid\" of \"max-elements\". Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100727
728 flags = value = 0;
729 str = " -1; ...";
730 assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext));
731 logbuf_assert("Invalid value \"-1\" of \"max-elements\". Line number 1.");
732
Radek Krejcidf6cad12018-11-28 17:10:55 +0100733 /* implementation limit */
734 flags = value = 0;
735 str = " 4294967296; ...";
736 assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext));
737 logbuf_assert("Value \"4294967296\" is out of \"max-elements\" bounds. Line number 1.");
738
Radek Krejci05b13982018-11-28 16:22:07 +0100739 flags = value = 0;
740 str = " 1; ...";
741 assert_int_equal(LY_SUCCESS, parse_maxelements(&ctx, &str, &value, &flags, &ext));
742 assert_int_equal(LYS_SET_MAX, flags);
743 assert_int_equal(1, value);
744
745 flags = value = 0;
746 str = " unbounded; ...";
747 assert_int_equal(LY_SUCCESS, parse_maxelements(&ctx, &str, &value, &flags, &ext));
748 assert_int_equal(LYS_SET_MAX, flags);
749 assert_int_equal(0, value);
750
751 flags = value = 0;
752 str = " 1 {m:ext;} ...";
753 assert_int_equal(LY_SUCCESS, parse_maxelements(&ctx, &str, &value, &flags, &ext));
754 assert_int_equal(LYS_SET_MAX, flags);
755 assert_int_equal(1, value);
756 assert_non_null(ext);
757 FREE_ARRAY(ctx.ctx, ext, lysp_ext_instance_free);
758 ext = NULL;
759
760 flags = value = 0;
761 str = " 1 {config true;} ...";
762 assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext));
763 logbuf_assert("Invalid keyword \"config\" as a child of \"max-elements\". Line number 1.");
764
765 *state = NULL;
766 ly_ctx_destroy(ctx.ctx, NULL);
767}
768
Radek Krejci9fcacc12018-10-11 15:59:11 +0200769static struct lysp_module *
Radek Krejcie7b95092019-05-15 11:03:07 +0200770mod_renew(struct lys_parser_ctx *ctx)
Radek Krejci9fcacc12018-10-11 15:59:11 +0200771{
Radek Krejci40544fa2019-01-11 09:38:37 +0100772 struct lysp_module *mod_p;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100773 static struct lys_module mod = {0};
774
775 lysc_module_free(mod.compiled, NULL);
776 lysp_module_free(mod.parsed);
777 FREE_STRING(mod.ctx, mod.name);
778 FREE_STRING(mod.ctx, mod.ns);
779 FREE_STRING(mod.ctx, mod.prefix);
780 FREE_STRING(mod.ctx, mod.filepath);
781 FREE_STRING(mod.ctx, mod.org);
782 FREE_STRING(mod.ctx, mod.contact);
783 FREE_STRING(mod.ctx, mod.dsc);
784 FREE_STRING(mod.ctx, mod.ref);
785 memset(&mod, 0, sizeof mod);
786 mod.ctx = ctx->ctx;
787
788 mod_p = calloc(1, sizeof *mod_p);
789 mod.parsed = mod_p;
790 mod_p->mod = &mod;
791 assert_non_null(mod_p);
792 return mod_p;
793}
794
795static struct lysp_submodule *
Radek Krejcie7b95092019-05-15 11:03:07 +0200796submod_renew(struct lys_parser_ctx *ctx, struct lysp_submodule *submod)
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100797{
798 lysp_submodule_free(ctx->ctx, submod);
799 submod = calloc(1, sizeof *submod);
800 assert_non_null(submod);
801 return submod;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200802}
803
Radek Krejcid33273d2018-10-25 14:55:52 +0200804static LY_ERR test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
805 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
806 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
807{
808 *module_data = user_data;
809 *format = LYS_IN_YANG;
810 *free_module_data = NULL;
811 return LY_SUCCESS;
812}
813
Radek Krejci9fcacc12018-10-11 15:59:11 +0200814static void
815test_module(void **state)
816{
Radek Krejci40544fa2019-01-11 09:38:37 +0100817 *state = test_module;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200818
Radek Krejcie7b95092019-05-15 11:03:07 +0200819 struct lys_parser_ctx ctx;
Radek Krejci40544fa2019-01-11 09:38:37 +0100820 struct lysp_module *mod = NULL;
821 struct lysp_submodule *submod = NULL;
822 struct lys_module *m;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200823 const char *str;
824
825 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
826 assert_non_null(ctx.ctx);
827 ctx.line = 1;
Radek Krejcia042ea12018-10-13 07:52:15 +0200828 ctx.indent = 0;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200829
Radek Krejci40544fa2019-01-11 09:38:37 +0100830 mod = mod_renew(&ctx);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200831
832 /* missing mandatory substatements */
833 str = " name {}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100834 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
835 assert_string_equal("name", mod->mod->name);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200836 logbuf_assert("Missing mandatory keyword \"namespace\" as a child of \"module\". Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100837 mod = mod_renew(&ctx);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200838
839 str = " name {namespace urn:x;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100840 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
841 assert_string_equal("urn:x", mod->mod->ns);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200842 logbuf_assert("Missing mandatory keyword \"prefix\" as a child of \"module\". Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100843 mod = mod_renew(&ctx);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200844
845 str = " name {namespace urn:x;prefix \"x\";}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100846 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod));
847 assert_string_equal("x", mod->mod->prefix);
Radek Krejci40544fa2019-01-11 09:38:37 +0100848 mod = mod_renew(&ctx);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200849
Radek Krejci027d5802018-11-14 16:57:28 +0100850#define SCHEMA_BEGINNING " name {yang-version 1.1;namespace urn:x;prefix \"x\";"
851#define SCHEMA_BEGINNING2 " name {namespace urn:x;prefix \"x\";"
Radek Krejcia042ea12018-10-13 07:52:15 +0200852#define TEST_NODE(NODETYPE, INPUT, NAME) \
853 str = SCHEMA_BEGINNING INPUT; \
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100854 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod)); \
Radek Krejcia042ea12018-10-13 07:52:15 +0200855 assert_non_null(mod->data); \
856 assert_int_equal(NODETYPE, mod->data->nodetype); \
857 assert_string_equal(NAME, mod->data->name); \
Radek Krejci40544fa2019-01-11 09:38:37 +0100858 mod = mod_renew(&ctx);
Radek Krejcia042ea12018-10-13 07:52:15 +0200859#define TEST_GENERIC(INPUT, TARGET, TEST) \
860 str = SCHEMA_BEGINNING INPUT; \
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100861 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod)); \
Radek Krejcia042ea12018-10-13 07:52:15 +0200862 assert_non_null(TARGET); \
863 TEST; \
Radek Krejci40544fa2019-01-11 09:38:37 +0100864 mod = mod_renew(&ctx);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100865#define TEST_DUP(MEMBER, VALUE1, VALUE2, LINE) \
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200866 TEST_DUP_GENERIC(SCHEMA_BEGINNING, MEMBER, VALUE1, VALUE2, \
Radek Krejci40544fa2019-01-11 09:38:37 +0100867 parse_module, mod, LINE, mod = mod_renew(&ctx))
Radek Krejcia042ea12018-10-13 07:52:15 +0200868
869 /* duplicated namespace, prefix */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100870 TEST_DUP("namespace", "y", "z", "1");
871 TEST_DUP("prefix", "y", "z", "1");
872 TEST_DUP("contact", "a", "b", "1");
873 TEST_DUP("description", "a", "b", "1");
874 TEST_DUP("organization", "a", "b", "1");
875 TEST_DUP("reference", "a", "b", "1");
Radek Krejcia042ea12018-10-13 07:52:15 +0200876
Radek Krejci70853c52018-10-15 14:46:16 +0200877 /* not allowed in module (submodule-specific) */
878 str = SCHEMA_BEGINNING "belongs-to master {prefix m;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100879 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejci70853c52018-10-15 14:46:16 +0200880 logbuf_assert("Invalid keyword \"belongs-to\" as a child of \"module\". Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100881 mod = mod_renew(&ctx);
Radek Krejci70853c52018-10-15 14:46:16 +0200882
Radek Krejcia042ea12018-10-13 07:52:15 +0200883 /* anydata */
884 TEST_NODE(LYS_ANYDATA, "anydata test;}", "test");
885 /* anyxml */
886 TEST_NODE(LYS_ANYXML, "anyxml test;}", "test");
887 /* augment */
888 TEST_GENERIC("augment /somepath;}", mod->augments,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200889 assert_string_equal("/somepath", mod->augments[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200890 /* choice */
891 TEST_NODE(LYS_CHOICE, "choice test;}", "test");
892 /* contact 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100893 TEST_GENERIC("contact \"firstname\" + \n\t\" surname\";}", mod->mod->contact,
894 assert_string_equal("firstname surname", mod->mod->contact));
Radek Krejcia042ea12018-10-13 07:52:15 +0200895 /* container */
896 TEST_NODE(LYS_CONTAINER, "container test;}", "test");
897 /* description 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100898 TEST_GENERIC("description \'some description\';}", mod->mod->dsc,
899 assert_string_equal("some description", mod->mod->dsc));
Radek Krejcia042ea12018-10-13 07:52:15 +0200900 /* deviation */
901 TEST_GENERIC("deviation /somepath {deviate not-supported;}}", mod->deviations,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200902 assert_string_equal("/somepath", mod->deviations[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200903 /* extension */
904 TEST_GENERIC("extension test;}", mod->extensions,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200905 assert_string_equal("test", mod->extensions[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200906 /* feature */
907 TEST_GENERIC("feature test;}", mod->features,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200908 assert_string_equal("test", mod->features[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200909 /* grouping */
910 TEST_GENERIC("grouping grp;}", mod->groupings,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200911 assert_string_equal("grp", mod->groupings[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200912 /* identity */
913 TEST_GENERIC("identity test;}", mod->identities,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200914 assert_string_equal("test", mod->identities[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200915 /* import */
Radek Krejci086c7132018-10-26 15:29:04 +0200916 ly_ctx_set_module_imp_clb(ctx.ctx, test_imp_clb, "module zzz { namespace urn:zzz; prefix z;}");
917 TEST_GENERIC("import zzz {prefix z;}}", mod->imports,
918 assert_string_equal("zzz", mod->imports[0].name));
Radek Krejci70853c52018-10-15 14:46:16 +0200919
Radek Krejcia042ea12018-10-13 07:52:15 +0200920 /* import - prefix collision */
Radek Krejci086c7132018-10-26 15:29:04 +0200921 str = SCHEMA_BEGINNING "import zzz {prefix x;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100922 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejci70853c52018-10-15 14:46:16 +0200923 logbuf_assert("Prefix \"x\" already used as module prefix. Line number 2.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100924 mod = mod_renew(&ctx);
Radek Krejci086c7132018-10-26 15:29:04 +0200925 str = SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix y;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100926 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejci086c7132018-10-26 15:29:04 +0200927 logbuf_assert("Prefix \"y\" already used to import \"zzz\" module. Line number 2.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100928 mod = mod_renew(&ctx);
Radek Krejci086c7132018-10-26 15:29:04 +0200929 str = "module" SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix z;}}";
930 assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
931 assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx));
932 logbuf_assert("Single revision of the module \"zzz\" referred twice.");
Radek Krejci70853c52018-10-15 14:46:16 +0200933
Radek Krejcia042ea12018-10-13 07:52:15 +0200934 /* include */
Radek Krejci9ed7a192018-10-31 16:23:51 +0100935 store = 1;
Radek Krejcid33273d2018-10-25 14:55:52 +0200936 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 +0200937 str = "module" SCHEMA_BEGINNING "include xxx;}";
938 assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
939 assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx));
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100940 logbuf_assert("Input data contains module in situation when a submodule is expected.");
Radek Krejci9ed7a192018-10-31 16:23:51 +0100941 store = -1;
Radek Krejcid33273d2018-10-25 14:55:52 +0200942
Radek Krejci9ed7a192018-10-31 16:23:51 +0100943 store = 1;
Radek Krejci313d9902018-11-08 09:42:58 +0100944 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 +0200945 str = "module" SCHEMA_BEGINNING "include xxx;}";
946 assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
947 assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx));
948 logbuf_assert("Included \"xxx\" submodule from \"name\" belongs-to a different module \"wrong-name\".");
Radek Krejci9ed7a192018-10-31 16:23:51 +0100949 store = -1;
Radek Krejcid33273d2018-10-25 14:55:52 +0200950
Radek Krejci313d9902018-11-08 09:42:58 +0100951 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 +0200952 TEST_GENERIC("include xxx;}", mod->includes,
Radek Krejci086c7132018-10-26 15:29:04 +0200953 assert_string_equal("xxx", mod->includes[0].name));
Radek Krejcid33273d2018-10-25 14:55:52 +0200954
Radek Krejcia042ea12018-10-13 07:52:15 +0200955 /* leaf */
956 TEST_NODE(LYS_LEAF, "leaf test {type string;}}", "test");
957 /* leaf-list */
958 TEST_NODE(LYS_LEAFLIST, "leaf-list test {type string;}}", "test");
959 /* list */
960 TEST_NODE(LYS_LIST, "list test {key a;leaf a {type string;}}}", "test");
961 /* notification */
962 TEST_GENERIC("notification test;}", mod->notifs,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200963 assert_string_equal("test", mod->notifs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200964 /* organization 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100965 TEST_GENERIC("organization \"CESNET a.l.e.\";}", mod->mod->org,
966 assert_string_equal("CESNET a.l.e.", mod->mod->org));
Radek Krejcia042ea12018-10-13 07:52:15 +0200967 /* reference 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100968 TEST_GENERIC("reference RFC7950;}", mod->mod->ref,
969 assert_string_equal("RFC7950", mod->mod->ref));
Radek Krejcia042ea12018-10-13 07:52:15 +0200970 /* revision */
971 TEST_GENERIC("revision 2018-10-12;}", mod->revs,
Radek Krejcib7db73a2018-10-24 14:18:40 +0200972 assert_string_equal("2018-10-12", mod->revs[0].date));
Radek Krejcia042ea12018-10-13 07:52:15 +0200973 /* rpc */
974 TEST_GENERIC("rpc test;}", mod->rpcs,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200975 assert_string_equal("test", mod->rpcs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200976 /* typedef */
977 TEST_GENERIC("typedef test{type string;}}", mod->typedefs,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200978 assert_string_equal("test", mod->typedefs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200979 /* uses */
980 TEST_NODE(LYS_USES, "uses test;}", "test");
981 /* yang-version */
Radek Krejci027d5802018-11-14 16:57:28 +0100982 str = SCHEMA_BEGINNING2 "\n\tyang-version 10;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100983 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejcia042ea12018-10-13 07:52:15 +0200984 logbuf_assert("Invalid value \"10\" of \"yang-version\". Line number 3.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100985 mod = mod_renew(&ctx);
Radek Krejci027d5802018-11-14 16:57:28 +0100986 str = SCHEMA_BEGINNING2 "yang-version 1.0;yang-version 1.1;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100987 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejcia042ea12018-10-13 07:52:15 +0200988 logbuf_assert("Duplicate keyword \"yang-version\". Line number 3.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100989 mod = mod_renew(&ctx);
Radek Krejci027d5802018-11-14 16:57:28 +0100990 str = SCHEMA_BEGINNING2 "yang-version 1.0;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100991 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod));
992 assert_int_equal(1, mod->mod->version);
Radek Krejci40544fa2019-01-11 09:38:37 +0100993 mod = mod_renew(&ctx);
Radek Krejci027d5802018-11-14 16:57:28 +0100994 str = SCHEMA_BEGINNING2 "yang-version \"1.1\";}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100995 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod));
996 assert_int_equal(2, mod->mod->version);
Radek Krejci40544fa2019-01-11 09:38:37 +0100997 mod = mod_renew(&ctx);
998
999 str = "module " SCHEMA_BEGINNING "} module q {namespace urn:q;prefixq;}";
1000 m = mod->mod;
1001 free(mod);
1002 m->parsed = NULL;
1003 assert_int_equal(LY_EVALID, yang_parse_module(&ctx, str, m));
1004 logbuf_assert("Invalid character sequence \"module\", expected end-of-file. Line number 3.");
1005 mod = mod_renew(&ctx);
1006
1007 str = "prefix " SCHEMA_BEGINNING "}";
1008 m = mod->mod;
1009 free(mod);
1010 m->parsed = NULL;
1011 assert_int_equal(LY_EVALID, yang_parse_module(&ctx, str, m));
1012 logbuf_assert("Invalid keyword \"prefix\", expected \"module\" or \"submodule\". Line number 3.");
1013 mod = mod_renew(&ctx);
Radek Krejci09306362018-10-15 15:26:01 +02001014
Radek Krejci156ccaf2018-10-15 15:49:17 +02001015 /* extensions */
1016 TEST_GENERIC("prefix:test;}", mod->exts,
Radek Krejci2c4e7172018-10-19 15:56:26 +02001017 assert_string_equal("prefix:test", mod->exts[0].name);
1018 assert_int_equal(LYEXT_SUBSTMT_SELF, mod->exts[0].insubstmt));
Radek Krejci40544fa2019-01-11 09:38:37 +01001019 mod = mod_renew(&ctx);
Radek Krejci156ccaf2018-10-15 15:49:17 +02001020
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001021 /* invalid substatement */
1022 str = SCHEMA_BEGINNING "must false;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001023 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001024 logbuf_assert("Invalid keyword \"must\" as a child of \"module\". Line number 3.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001025 mod = mod_renew(&ctx);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001026
Radek Krejci09306362018-10-15 15:26:01 +02001027 /* submodule */
Radek Krejci40544fa2019-01-11 09:38:37 +01001028 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001029
1030 /* missing mandatory substatements */
1031 str = " subname {}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001032 lydict_remove(ctx.ctx, submod->name);
1033 assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod));
1034 assert_string_equal("subname", submod->name);
Radek Krejci09306362018-10-15 15:26:01 +02001035 logbuf_assert("Missing mandatory keyword \"belongs-to\" as a child of \"submodule\". Line number 3.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001036 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001037
Radek Krejci313d9902018-11-08 09:42:58 +01001038 str = " subname {belongs-to name {prefix x;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001039 lydict_remove(ctx.ctx, submod->name);
1040 assert_int_equal(LY_SUCCESS, parse_submodule(&ctx, &str, submod));
1041 assert_string_equal("name", submod->belongsto);
1042 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001043
1044#undef SCHEMA_BEGINNING
Radek Krejci313d9902018-11-08 09:42:58 +01001045#define SCHEMA_BEGINNING " subname {belongs-to name {prefix x;}"
Radek Krejci09306362018-10-15 15:26:01 +02001046
1047 /* duplicated namespace, prefix */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001048 str = " subname {belongs-to name {prefix x;}belongs-to module1;belongs-to module2;} ...";
1049 assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod)); \
1050 logbuf_assert("Duplicate keyword \"belongs-to\". Line number 3."); \
1051 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001052
1053 /* not allowed in submodule (module-specific) */
1054 str = SCHEMA_BEGINNING "namespace \"urn:z\";}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001055 assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod));
Radek Krejci09306362018-10-15 15:26:01 +02001056 logbuf_assert("Invalid keyword \"namespace\" as a child of \"submodule\". Line number 3.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001057 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001058 str = SCHEMA_BEGINNING "prefix m;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001059 assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod));
Radek Krejci09306362018-10-15 15:26:01 +02001060 logbuf_assert("Invalid keyword \"prefix\" as a child of \"submodule\". Line number 3.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001061 submod = submod_renew(&ctx, submod);
Radek Krejcia042ea12018-10-13 07:52:15 +02001062
Radek Krejci40544fa2019-01-11 09:38:37 +01001063 str = "submodule " SCHEMA_BEGINNING "} module q {namespace urn:q;prefixq;}";
1064 lysp_submodule_free(ctx.ctx, submod);
1065 submod = NULL;
1066 assert_int_equal(LY_EVALID, yang_parse_submodule(&ctx, str, &submod));
1067 logbuf_assert("Invalid character sequence \"module\", expected end-of-file. Line number 3.");
1068
1069 str = "prefix " SCHEMA_BEGINNING "}";
1070 assert_int_equal(LY_EVALID, yang_parse_submodule(&ctx, str, &submod));
1071 logbuf_assert("Invalid keyword \"prefix\", expected \"module\" or \"submodule\". Line number 3.");
1072 submod = submod_renew(&ctx, submod);
1073
Radek Krejcia042ea12018-10-13 07:52:15 +02001074#undef TEST_GENERIC
1075#undef TEST_NODE
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001076#undef TEST_DUP
Radek Krejcia042ea12018-10-13 07:52:15 +02001077#undef SCHEMA_BEGINNING
1078
Radek Krejci9fcacc12018-10-11 15:59:11 +02001079 lysp_module_free(mod);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001080 lysp_submodule_free(ctx.ctx, submod);
Radek Krejci9fcacc12018-10-11 15:59:11 +02001081 ly_ctx_destroy(ctx.ctx, NULL);
Radek Krejci40544fa2019-01-11 09:38:37 +01001082
1083 *state = NULL;
Radek Krejciefd22f62018-09-27 11:47:58 +02001084}
1085
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001086static void
1087test_identity(void **state)
1088{
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001089 *state = test_identity;
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001090
Radek Krejcie7b95092019-05-15 11:03:07 +02001091 struct lys_parser_ctx ctx;
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001092 struct lysp_ident *ident = NULL;
1093 const char *str;
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001094
1095 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1096 assert_non_null(ctx.ctx);
1097 ctx.line = 1;
1098 ctx.indent = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001099 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001100
1101 /* invalid cardinality */
1102#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001103 TEST_DUP_GENERIC(" test {", MEMBER, VALUE1, VALUE2, parse_identity, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001104 &ident, "1", FREE_ARRAY(ctx.ctx, ident, lysp_ident_free); ident = NULL)
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001105
Radek Krejci38222632019-02-12 16:55:05 +01001106 TEST_DUP("description", "a", "b");
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001107 TEST_DUP("reference", "a", "b");
1108 TEST_DUP("status", "current", "obsolete");
1109
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001110 /* full content */
1111 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 +02001112 assert_int_equal(LY_SUCCESS, parse_identity(&ctx, &str, &ident));
1113 assert_non_null(ident);
1114 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001115 FREE_ARRAY(ctx.ctx, ident, lysp_ident_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001116 ident = NULL;
1117
1118 /* invalid substatement */
1119 str = " test {organization XXX;}";
1120 assert_int_equal(LY_EVALID, parse_identity(&ctx, &str, &ident));
1121 logbuf_assert("Invalid keyword \"organization\" as a child of \"identity\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001122 FREE_ARRAY(ctx.ctx, ident, lysp_ident_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001123 ident = NULL;
1124
1125#undef TEST_DUP
1126
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001127 *state = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001128 ly_ctx_destroy(ctx.ctx, NULL);
1129}
1130
1131static void
1132test_feature(void **state)
1133{
1134 (void) state; /* unused */
1135
Radek Krejcie7b95092019-05-15 11:03:07 +02001136 struct lys_parser_ctx ctx;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001137 struct lysp_feature *features = NULL;
1138 const char *str;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001139
1140 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1141 assert_non_null(ctx.ctx);
1142 ctx.line = 1;
1143 ctx.indent = 0;
1144
1145 /* invalid cardinality */
1146#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1147 TEST_DUP_GENERIC(" test {", MEMBER, VALUE1, VALUE2, parse_feature, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001148 &features, "1", FREE_ARRAY(ctx.ctx, features, lysp_feature_free); features = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001149
1150 TEST_DUP("description", "a", "b");
1151 TEST_DUP("reference", "a", "b");
1152 TEST_DUP("status", "current", "obsolete");
1153
1154 /* full content */
1155 str = " test {description text;reference \'another text\';status current; if-feature x;if-feature y;prefix:ext;} ...";
1156 assert_int_equal(LY_SUCCESS, parse_feature(&ctx, &str, &features));
1157 assert_non_null(features);
1158 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001159 FREE_ARRAY(ctx.ctx, features, lysp_feature_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001160 features = NULL;
1161
1162 /* invalid substatement */
1163 str = " test {organization XXX;}";
1164 assert_int_equal(LY_EVALID, parse_feature(&ctx, &str, &features));
1165 logbuf_assert("Invalid keyword \"organization\" as a child of \"feature\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001166 FREE_ARRAY(ctx.ctx, features, lysp_feature_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001167 features = NULL;
1168
1169#undef TEST_DUP
1170
1171 ly_ctx_destroy(ctx.ctx, NULL);
1172}
1173
1174static void
1175test_deviation(void **state)
1176{
1177 (void) state; /* unused */
1178
Radek Krejcie7b95092019-05-15 11:03:07 +02001179 struct lys_parser_ctx ctx;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001180 struct lysp_deviation *d = NULL;
1181 const char *str;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001182
1183 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1184 assert_non_null(ctx.ctx);
1185 ctx.line = 1;
1186 ctx.indent = 0;
1187
1188 /* invalid cardinality */
1189#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1190 TEST_DUP_GENERIC(" test {deviate not-supported;", MEMBER, VALUE1, VALUE2, parse_deviation, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001191 &d, "1", FREE_ARRAY(ctx.ctx, d, lysp_deviation_free); d = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001192
1193 TEST_DUP("description", "a", "b");
1194 TEST_DUP("reference", "a", "b");
1195
1196 /* full content */
1197 str = " test {deviate not-supported;description text;reference \'another text\';prefix:ext;} ...";
1198 assert_int_equal(LY_SUCCESS, parse_deviation(&ctx, &str, &d));
1199 assert_non_null(d);
1200 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001201 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001202 d = NULL;
1203
1204 /* missing mandatory substatement */
1205 str = " test {description text;}";
1206 assert_int_equal(LY_EVALID, parse_deviation(&ctx, &str, &d));
1207 logbuf_assert("Missing mandatory keyword \"deviate\" as a child of \"deviation\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001208 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001209 d = NULL;
1210
1211 /* invalid substatement */
1212 str = " test {deviate not-supported; status obsolete;}";
1213 assert_int_equal(LY_EVALID, parse_deviation(&ctx, &str, &d));
1214 logbuf_assert("Invalid keyword \"status\" as a child of \"deviation\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001215 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001216 d = NULL;
1217
1218#undef TEST_DUP
1219
1220 ly_ctx_destroy(ctx.ctx, NULL);
1221}
1222
1223static void
1224test_deviate(void **state)
1225{
1226 (void) state; /* unused */
1227
Radek Krejcie7b95092019-05-15 11:03:07 +02001228 struct lys_parser_ctx ctx;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001229 struct lysp_deviate *d = NULL;
1230 const char *str;
1231
1232 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1233 assert_non_null(ctx.ctx);
1234 ctx.line = 1;
1235 ctx.indent = 0;
1236
1237 /* invalid cardinality */
1238#define TEST_DUP(TYPE, MEMBER, VALUE1, VALUE2) \
1239 TEST_DUP_GENERIC(TYPE" {", MEMBER, VALUE1, VALUE2, parse_deviate, \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001240 &d, "1", lysp_deviate_free(ctx.ctx, d); free(d); d = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001241
1242 TEST_DUP("add", "config", "true", "false");
1243 TEST_DUP("replace", "default", "int8", "uint8");
1244 TEST_DUP("add", "mandatory", "true", "false");
1245 TEST_DUP("add", "max-elements", "1", "2");
1246 TEST_DUP("add", "min-elements", "1", "2");
1247 TEST_DUP("replace", "type", "int8", "uint8");
1248 TEST_DUP("add", "units", "kilometers", "miles");
1249
1250 /* full contents */
1251 str = " not-supported {prefix:ext;} ...";
1252 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1253 assert_non_null(d);
1254 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001255 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001256 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;} ...";
1257 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1258 assert_non_null(d);
1259 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001260 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001261 str = " delete {units meters; must 1; must 2; unique x; unique y; default a; default b; prefix:ext;} ...";
1262 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1263 assert_non_null(d);
1264 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001265 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001266 str = " replace {type string; units meters; default a; config true; mandatory true; min-elements 1; max-elements 2; prefix:ext;} ...";
1267 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1268 assert_non_null(d);
1269 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001270 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001271
1272 /* invalid substatements */
1273#define TEST_NOT_SUP(DEV, STMT, VALUE) \
1274 str = " "DEV" {"STMT" "VALUE";}..."; \
1275 assert_int_equal(LY_EVALID, parse_deviate(&ctx, &str, &d)); \
1276 logbuf_assert("Deviate \""DEV"\" does not support keyword \""STMT"\". Line number 1."); \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001277 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001278
1279 TEST_NOT_SUP("not-supported", "units", "meters");
1280 TEST_NOT_SUP("not-supported", "must", "1");
1281 TEST_NOT_SUP("not-supported", "unique", "x");
1282 TEST_NOT_SUP("not-supported", "default", "a");
1283 TEST_NOT_SUP("not-supported", "config", "true");
1284 TEST_NOT_SUP("not-supported", "mandatory", "true");
1285 TEST_NOT_SUP("not-supported", "min-elements", "1");
1286 TEST_NOT_SUP("not-supported", "max-elements", "2");
1287 TEST_NOT_SUP("not-supported", "type", "string");
1288 TEST_NOT_SUP("add", "type", "string");
1289 TEST_NOT_SUP("delete", "config", "true");
1290 TEST_NOT_SUP("delete", "mandatory", "true");
1291 TEST_NOT_SUP("delete", "min-elements", "1");
1292 TEST_NOT_SUP("delete", "max-elements", "2");
1293 TEST_NOT_SUP("delete", "type", "string");
1294 TEST_NOT_SUP("replace", "must", "1");
1295 TEST_NOT_SUP("replace", "unique", "a");
1296
1297 str = " nonsence; ...";
1298 assert_int_equal(LY_EVALID, parse_deviate(&ctx, &str, &d));
1299 logbuf_assert("Invalid value \"nonsence\" of \"deviate\". Line number 1.");
1300 assert_null(d);
1301
1302#undef TEST_NOT_SUP
1303#undef TEST_DUP
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001304
1305 ly_ctx_destroy(ctx.ctx, NULL);
1306}
1307
Radek Krejci8c370832018-11-02 15:10:03 +01001308static void
1309test_container(void **state)
1310{
1311 (void) state; /* unused */
1312
Radek Krejcie7b95092019-05-15 11:03:07 +02001313 struct lys_parser_ctx ctx = {0};
Radek Krejci8c370832018-11-02 15:10:03 +01001314 struct lysp_node_container *c = NULL;
1315 const char *str;
1316
1317 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1318 assert_non_null(ctx.ctx);
1319 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001320 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci8c370832018-11-02 15:10:03 +01001321
1322 /* invalid cardinality */
1323#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1324 str = "cont {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1325 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c)); \
1326 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001327 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001328
1329 TEST_DUP("config", "true", "false");
1330 TEST_DUP("description", "text1", "text2");
1331 TEST_DUP("presence", "true", "false");
1332 TEST_DUP("reference", "1", "2");
1333 TEST_DUP("status", "current", "obsolete");
1334 TEST_DUP("when", "true", "false");
1335#undef TEST_DUP
1336
1337 /* full content */
Radek Krejci313d9902018-11-08 09:42:58 +01001338 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;}"
1339 "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 +01001340 assert_int_equal(LY_SUCCESS, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1341 assert_non_null(c);
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001342 assert_int_equal(LYS_CONTAINER, c->nodetype);
1343 assert_string_equal("cont", c->name);
Radek Krejci8c370832018-11-02 15:10:03 +01001344 assert_non_null(c->actions);
1345 assert_non_null(c->child);
1346 assert_string_equal("test", c->dsc);
1347 assert_non_null(c->exts);
1348 assert_non_null(c->groupings);
1349 assert_non_null(c->iffeatures);
1350 assert_non_null(c->musts);
1351 assert_non_null(c->notifs);
1352 assert_string_equal("true", c->presence);
1353 assert_string_equal("test", c->ref);
1354 assert_non_null(c->typedefs);
1355 assert_non_null(c->when);
1356 assert_null(c->parent);
1357 assert_null(c->next);
1358 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR, c->flags);
Radek Krejci313d9902018-11-08 09:42:58 +01001359 ly_set_erase(&ctx.tpdfs_nodes, NULL);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001360 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001361
1362 /* invalid */
1363 str = " cont {augment /root;} ...";
1364 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1365 logbuf_assert("Invalid keyword \"augment\" as a child of \"container\". Line number 1.");
Radek Krejci4f28eda2018-11-12 11:46:16 +01001366 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001367 str = " cont {nonsence true;} ...";
1368 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1369 logbuf_assert("Invalid character sequence \"nonsence\", expected a keyword. Line number 1.");
Radek Krejci4f28eda2018-11-12 11:46:16 +01001370 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001371
Radek Krejcif538ce52019-03-05 10:46:14 +01001372 ctx.mod_version = 1; /* simulate YANG 1.0 */
1373 str = " cont {action x;} ...";
1374 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1375 logbuf_assert("Invalid keyword \"action\" as a child of \"container\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
1376 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
1377
Radek Krejci8c370832018-11-02 15:10:03 +01001378 ly_ctx_destroy(ctx.ctx, NULL);
1379}
1380
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001381static void
1382test_leaf(void **state)
1383{
1384 *state = test_leaf;
1385
Radek Krejcie7b95092019-05-15 11:03:07 +02001386 struct lys_parser_ctx ctx = {0};
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001387 struct lysp_node_leaf *l = NULL;
1388 const char *str;
1389
1390 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1391 assert_non_null(ctx.ctx);
1392 ctx.line = 1;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001393 //ctx.mod->version = 2; /* simulate YANG 1.1 */
1394
1395 /* invalid cardinality */
1396#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1397 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1398 assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l)); \
1399 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1400 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1401
1402 TEST_DUP("config", "true", "false");
1403 TEST_DUP("default", "x", "y");
1404 TEST_DUP("description", "text1", "text2");
1405 TEST_DUP("mandatory", "true", "false");
1406 TEST_DUP("reference", "1", "2");
1407 TEST_DUP("status", "current", "obsolete");
Radek Krejci0e5d8382018-11-28 16:37:53 +01001408 TEST_DUP("type", "int8", "uint8");
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001409 TEST_DUP("units", "text1", "text2");
1410 TEST_DUP("when", "true", "false");
1411#undef TEST_DUP
1412
1413 /* full content - without mandatory which is mutual exclusive with default */
1414 str = "l {config false;default \"xxx\";description test;if-feature f;"
1415 "must 'expr';reference test;status current;type string; units yyy;when true;m:ext;} ...";
1416 assert_int_equal(LY_SUCCESS, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
1417 assert_non_null(l);
1418 assert_int_equal(LYS_LEAF, l->nodetype);
1419 assert_string_equal("l", l->name);
1420 assert_string_equal("test", l->dsc);
1421 assert_string_equal("xxx", l->dflt);
1422 assert_string_equal("yyy", l->units);
1423 assert_string_equal("string", l->type.name);
1424 assert_non_null(l->exts);
1425 assert_non_null(l->iffeatures);
1426 assert_non_null(l->musts);
1427 assert_string_equal("test", l->ref);
1428 assert_non_null(l->when);
1429 assert_null(l->parent);
1430 assert_null(l->next);
1431 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR, l->flags);
1432 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1433
1434 /* full content - now with mandatory */
1435 str = "l {mandatory true; type string;} ...";
1436 assert_int_equal(LY_SUCCESS, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
1437 assert_non_null(l);
1438 assert_int_equal(LYS_LEAF, l->nodetype);
1439 assert_string_equal("l", l->name);
1440 assert_string_equal("string", l->type.name);
1441 assert_int_equal(LYS_MAND_TRUE, l->flags);
1442 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1443
1444 /* invalid */
1445 str = " l {mandatory true; default xx; type string;} ...";
1446 assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
Radek Krejcia9026eb2018-12-12 16:04:47 +01001447 logbuf_assert("Invalid combination of keywords \"mandatory\" and \"default\" as substatements of \"leaf\". Line number 1.");
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001448 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1449
1450 str = " l {description \"missing type\";} ...";
1451 assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
1452 logbuf_assert("Missing mandatory keyword \"type\" as a child of \"leaf\". Line number 1.");
1453 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1454
1455 *state = NULL;
1456 ly_ctx_destroy(ctx.ctx, NULL);
1457}
1458
Radek Krejci0e5d8382018-11-28 16:37:53 +01001459static void
1460test_leaflist(void **state)
1461{
1462 *state = test_leaf;
1463
Radek Krejcie7b95092019-05-15 11:03:07 +02001464 struct lys_parser_ctx ctx = {0};
Radek Krejci0e5d8382018-11-28 16:37:53 +01001465 struct lysp_node_leaflist *ll = NULL;
1466 const char *str;
1467
1468 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1469 assert_non_null(ctx.ctx);
1470 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001471 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci0e5d8382018-11-28 16:37:53 +01001472
1473 /* invalid cardinality */
1474#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1475 str = "ll {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1476 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll)); \
1477 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1478 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1479
1480 TEST_DUP("config", "true", "false");
1481 TEST_DUP("description", "text1", "text2");
1482 TEST_DUP("max-elements", "10", "20");
1483 TEST_DUP("min-elements", "10", "20");
1484 TEST_DUP("ordered-by", "user", "system");
1485 TEST_DUP("reference", "1", "2");
1486 TEST_DUP("status", "current", "obsolete");
1487 TEST_DUP("type", "int8", "uint8");
1488 TEST_DUP("units", "text1", "text2");
1489 TEST_DUP("when", "true", "false");
1490#undef TEST_DUP
1491
1492 /* full content - without min-elements which is mutual exclusive with default */
1493 str = "ll {config false;default \"xxx\"; default \"yyy\";description test;if-feature f;"
1494 "max-elements 10;must 'expr';ordered-by user;reference test;"
1495 "status current;type string; units zzz;when true;m:ext;} ...";
1496 assert_int_equal(LY_SUCCESS, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1497 assert_non_null(ll);
1498 assert_int_equal(LYS_LEAFLIST, ll->nodetype);
1499 assert_string_equal("ll", ll->name);
1500 assert_string_equal("test", ll->dsc);
1501 assert_non_null(ll->dflts);
1502 assert_int_equal(2, LY_ARRAY_SIZE(ll->dflts));
1503 assert_string_equal("xxx", ll->dflts[0]);
1504 assert_string_equal("yyy", ll->dflts[1]);
1505 assert_string_equal("zzz", ll->units);
1506 assert_int_equal(10, ll->max);
1507 assert_int_equal(0, ll->min);
1508 assert_string_equal("string", ll->type.name);
1509 assert_non_null(ll->exts);
1510 assert_non_null(ll->iffeatures);
1511 assert_non_null(ll->musts);
1512 assert_string_equal("test", ll->ref);
1513 assert_non_null(ll->when);
1514 assert_null(ll->parent);
1515 assert_null(ll->next);
1516 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_USER | LYS_SET_MAX, ll->flags);
1517 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1518
1519 /* full content - now with min-elements */
1520 str = "ll {min-elements 10; type string;} ...";
1521 assert_int_equal(LY_SUCCESS, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1522 assert_non_null(ll);
1523 assert_int_equal(LYS_LEAFLIST, ll->nodetype);
1524 assert_string_equal("ll", ll->name);
1525 assert_string_equal("string", ll->type.name);
1526 assert_int_equal(0, ll->max);
1527 assert_int_equal(10, ll->min);
1528 assert_int_equal(LYS_SET_MIN, ll->flags);
1529 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1530
1531 /* invalid */
1532 str = " ll {min-elements 1; default xx; type string;} ...";
1533 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
Radek Krejcia9026eb2018-12-12 16:04:47 +01001534 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 +01001535 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1536
1537 str = " ll {description \"missing type\";} ...";
1538 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1539 logbuf_assert("Missing mandatory keyword \"type\" as a child of \"leaf-list\". Line number 1.");
1540 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1541
Radek Krejcidf6cad12018-11-28 17:10:55 +01001542 str = " ll {type string; min-elements 10; max-elements 1;} ..."; /* invalid combination of min/max */
1543 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1544 logbuf_assert("Invalid combination of min-elements and max-elements: min value 10 is bigger than the max value 1. Line number 1.");
1545 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1546
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001547 ctx.mod_version = 1; /* simulate YANG 1.0 - default statement is not allowed */
Radek Krejci0e5d8382018-11-28 16:37:53 +01001548 str = " ll {default xx; type string;} ...";
1549 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1550 logbuf_assert("Invalid keyword \"default\" as a child of \"leaf-list\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
1551 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1552
1553 *state = NULL;
1554 ly_ctx_destroy(ctx.ctx, NULL);
1555}
1556
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001557static void
1558test_list(void **state)
1559{
1560 *state = test_list;
1561
Radek Krejcie7b95092019-05-15 11:03:07 +02001562 struct lys_parser_ctx ctx = {0};
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001563 struct lysp_node_list *l = NULL;
1564 const char *str;
1565
1566 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1567 assert_non_null(ctx.ctx);
1568 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001569 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001570
1571 /* invalid cardinality */
1572#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1573 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1574 assert_int_equal(LY_EVALID, parse_list(&ctx, &str, NULL, (struct lysp_node**)&l)); \
1575 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1576 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1577
1578 TEST_DUP("config", "true", "false");
1579 TEST_DUP("description", "text1", "text2");
1580 TEST_DUP("key", "one", "two");
1581 TEST_DUP("max-elements", "10", "20");
1582 TEST_DUP("min-elements", "10", "20");
1583 TEST_DUP("ordered-by", "user", "system");
1584 TEST_DUP("reference", "1", "2");
1585 TEST_DUP("status", "current", "obsolete");
1586 TEST_DUP("when", "true", "false");
1587#undef TEST_DUP
1588
1589 /* full content */
1590 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;}"
1591 "leaf-list ll {type string;} list li;max-elements 10; min-elements 1;must 'expr';notification not; ordered-by system; reference test;"
1592 "status current;typedef t {type int8;}unique xxx;unique yyy;uses g;when true;m:ext;} ...";
1593 assert_int_equal(LY_SUCCESS, parse_list(&ctx, &str, NULL, (struct lysp_node**)&l));
1594 assert_non_null(l);
1595 assert_int_equal(LYS_LIST, l->nodetype);
1596 assert_string_equal("l", l->name);
1597 assert_string_equal("test", l->dsc);
1598 assert_string_equal("l", l->key);
1599 assert_non_null(l->uniques);
1600 assert_int_equal(2, LY_ARRAY_SIZE(l->uniques));
1601 assert_string_equal("xxx", l->uniques[0]);
1602 assert_string_equal("yyy", l->uniques[1]);
1603 assert_int_equal(10, l->max);
1604 assert_int_equal(1, l->min);
1605 assert_non_null(l->exts);
1606 assert_non_null(l->iffeatures);
1607 assert_non_null(l->musts);
1608 assert_string_equal("test", l->ref);
1609 assert_non_null(l->when);
1610 assert_null(l->parent);
1611 assert_null(l->next);
1612 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM | LYS_SET_MAX | LYS_SET_MIN, l->flags);
1613 ly_set_erase(&ctx.tpdfs_nodes, NULL);
1614 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1615
Radek Krejcif538ce52019-03-05 10:46:14 +01001616 /* invalid content */
1617 ctx.mod_version = 1; /* simulate YANG 1.0 */
1618 str = "l {action x;} ...";
1619 assert_int_equal(LY_EVALID, parse_list(&ctx, &str, NULL, (struct lysp_node**)&l));
1620 logbuf_assert("Invalid keyword \"action\" as a child of \"list\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
1621 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1622
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001623 *state = NULL;
1624 ly_ctx_destroy(ctx.ctx, NULL);
1625}
1626
Radek Krejci056d0a82018-12-06 16:57:25 +01001627static void
1628test_choice(void **state)
1629{
1630 *state = test_choice;
1631
Radek Krejcie7b95092019-05-15 11:03:07 +02001632 struct lys_parser_ctx ctx = {0};
Radek Krejci056d0a82018-12-06 16:57:25 +01001633 struct lysp_node_choice *ch = NULL;
1634 const char *str;
1635
1636 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1637 assert_non_null(ctx.ctx);
1638 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001639 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci056d0a82018-12-06 16:57:25 +01001640
1641 /* invalid cardinality */
1642#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1643 str = "ch {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1644 assert_int_equal(LY_EVALID, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch)); \
1645 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1646 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1647
1648 TEST_DUP("config", "true", "false");
1649 TEST_DUP("default", "a", "b");
1650 TEST_DUP("description", "text1", "text2");
1651 TEST_DUP("mandatory", "true", "false");
1652 TEST_DUP("reference", "1", "2");
1653 TEST_DUP("status", "current", "obsolete");
1654 TEST_DUP("when", "true", "false");
1655#undef TEST_DUP
1656
Radek Krejcia9026eb2018-12-12 16:04:47 +01001657 /* full content - without default due to a collision with mandatory */
1658 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 +01001659 "leaf-list ll {type string;} list li;mandatory true;reference test;status current;when true;m:ext;} ...";
1660 assert_int_equal(LY_SUCCESS, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch));
1661 assert_non_null(ch);
1662 assert_int_equal(LYS_CHOICE, ch->nodetype);
1663 assert_string_equal("ch", ch->name);
1664 assert_string_equal("test", ch->dsc);
1665 assert_non_null(ch->exts);
1666 assert_non_null(ch->iffeatures);
1667 assert_string_equal("test", ch->ref);
1668 assert_non_null(ch->when);
1669 assert_null(ch->parent);
1670 assert_null(ch->next);
1671 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_MAND_TRUE, ch->flags);
1672 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1673
Radek Krejcia9026eb2018-12-12 16:04:47 +01001674 /* full content - the default missing from the previous node */
1675 str = "ch {default c;case c;} ...";
1676 assert_int_equal(LY_SUCCESS, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch));
1677 assert_non_null(ch);
1678 assert_int_equal(LYS_CHOICE, ch->nodetype);
1679 assert_string_equal("ch", ch->name);
1680 assert_string_equal("c", ch->dflt);
1681 assert_int_equal(0, ch->flags);
1682 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1683
1684 /* invalid content */
1685 str = "ch {mandatory true; default c1; case c1 {leaf x{type string;}}} ...";
1686 assert_int_equal(LY_EVALID, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch));
1687 logbuf_assert("Invalid combination of keywords \"mandatory\" and \"default\" as substatements of \"choice\". Line number 1.");
1688 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1689
1690 *state = NULL;
1691 ly_ctx_destroy(ctx.ctx, NULL);
1692}
1693
1694static void
1695test_case(void **state)
1696{
1697 *state = test_case;
1698
Radek Krejcie7b95092019-05-15 11:03:07 +02001699 struct lys_parser_ctx ctx = {0};
Radek Krejcia9026eb2018-12-12 16:04:47 +01001700 struct lysp_node_case *cs = NULL;
1701 const char *str;
1702
1703 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1704 assert_non_null(ctx.ctx);
1705 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001706 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejcia9026eb2018-12-12 16:04:47 +01001707
1708 /* invalid cardinality */
1709#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1710 str = "cs {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1711 assert_int_equal(LY_EVALID, parse_case(&ctx, &str, NULL, (struct lysp_node**)&cs)); \
1712 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1713 lysp_node_free(ctx.ctx, (struct lysp_node*)cs); cs = NULL;
1714
1715 TEST_DUP("description", "text1", "text2");
1716 TEST_DUP("reference", "1", "2");
1717 TEST_DUP("status", "current", "obsolete");
1718 TEST_DUP("when", "true", "false");
1719#undef TEST_DUP
1720
1721 /* full content */
1722 str = "cs {anydata any;anyxml anyxml; choice ch;container c;description test;if-feature f;leaf l {type string;}"
1723 "leaf-list ll {type string;} list li;reference test;status current;uses grp;when true;m:ext;} ...";
1724 assert_int_equal(LY_SUCCESS, parse_case(&ctx, &str, NULL, (struct lysp_node**)&cs));
1725 assert_non_null(cs);
1726 assert_int_equal(LYS_CASE, cs->nodetype);
1727 assert_string_equal("cs", cs->name);
1728 assert_string_equal("test", cs->dsc);
1729 assert_non_null(cs->exts);
1730 assert_non_null(cs->iffeatures);
1731 assert_string_equal("test", cs->ref);
1732 assert_non_null(cs->when);
1733 assert_null(cs->parent);
1734 assert_null(cs->next);
1735 assert_int_equal(LYS_STATUS_CURR, cs->flags);
1736 lysp_node_free(ctx.ctx, (struct lysp_node*)cs); cs = NULL;
1737
1738 /* invalid content */
1739 str = "cs {config true} ...";
1740 assert_int_equal(LY_EVALID, parse_case(&ctx, &str, NULL, (struct lysp_node**)&cs));
1741 logbuf_assert("Invalid keyword \"config\" as a child of \"case\". Line number 1.");
1742 lysp_node_free(ctx.ctx, (struct lysp_node*)cs); cs = NULL;
1743
Radek Krejci056d0a82018-12-06 16:57:25 +01001744 *state = NULL;
1745 ly_ctx_destroy(ctx.ctx, NULL);
1746}
1747
Radek Krejci9800fb82018-12-13 14:26:23 +01001748static void
1749test_any(void **state, enum yang_keyword kw)
1750{
1751 *state = test_any;
1752
Radek Krejcie7b95092019-05-15 11:03:07 +02001753 struct lys_parser_ctx ctx = {0};
Radek Krejci9800fb82018-12-13 14:26:23 +01001754 struct lysp_node_anydata *any = NULL;
1755 const char *str;
1756
1757 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1758 assert_non_null(ctx.ctx);
1759 ctx.line = 1;
Radek Krejci9800fb82018-12-13 14:26:23 +01001760 if (kw == YANG_ANYDATA) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001761 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci9800fb82018-12-13 14:26:23 +01001762 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001763 ctx.mod_version = 1; /* simulate YANG 1.0 */
Radek Krejci9800fb82018-12-13 14:26:23 +01001764 }
1765
1766 /* invalid cardinality */
1767#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1768 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1769 assert_int_equal(LY_EVALID, parse_any(&ctx, &str, kw, NULL, (struct lysp_node**)&any)); \
1770 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1771 lysp_node_free(ctx.ctx, (struct lysp_node*)any); any = NULL;
1772
1773 TEST_DUP("config", "true", "false");
1774 TEST_DUP("description", "text1", "text2");
1775 TEST_DUP("mandatory", "true", "false");
1776 TEST_DUP("reference", "1", "2");
1777 TEST_DUP("status", "current", "obsolete");
1778 TEST_DUP("when", "true", "false");
1779#undef TEST_DUP
1780
1781 /* full content */
1782 str = "any {config true;description test;if-feature f;mandatory true;must 'expr';reference test;status current;when true;m:ext;} ...";
1783 assert_int_equal(LY_SUCCESS, parse_any(&ctx, &str, kw, NULL, (struct lysp_node**)&any));
1784 assert_non_null(any);
1785 assert_int_equal(kw == YANG_ANYDATA ? LYS_ANYDATA : LYS_ANYXML, any->nodetype);
1786 assert_string_equal("any", any->name);
1787 assert_string_equal("test", any->dsc);
1788 assert_non_null(any->exts);
1789 assert_non_null(any->iffeatures);
1790 assert_non_null(any->musts);
1791 assert_string_equal("test", any->ref);
1792 assert_non_null(any->when);
1793 assert_null(any->parent);
1794 assert_null(any->next);
1795 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR | LYS_MAND_TRUE, any->flags);
1796 lysp_node_free(ctx.ctx, (struct lysp_node*)any); any = NULL;
1797
1798 *state = NULL;
1799 ly_ctx_destroy(ctx.ctx, NULL);
1800}
1801
1802static void
1803test_anydata(void **state)
1804{
1805 return test_any(state, YANG_ANYDATA);
1806}
1807
1808static void
1809test_anyxml(void **state)
1810{
1811 return test_any(state, YANG_ANYXML);
1812}
1813
Radek Krejcie86bf772018-12-14 11:39:53 +01001814static void
1815test_grouping(void **state)
1816{
1817 *state = test_grouping;
1818
Radek Krejcie7b95092019-05-15 11:03:07 +02001819 struct lys_parser_ctx ctx = {0};
Radek Krejcie86bf772018-12-14 11:39:53 +01001820 struct lysp_grp *grp = NULL;
1821 const char *str;
1822
1823 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1824 assert_non_null(ctx.ctx);
1825 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001826 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejcie86bf772018-12-14 11:39:53 +01001827
1828 /* invalid cardinality */
1829#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1830 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1831 assert_int_equal(LY_EVALID, parse_grouping(&ctx, &str, NULL, &grp)); \
1832 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1833 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1834
1835 TEST_DUP("description", "text1", "text2");
1836 TEST_DUP("reference", "1", "2");
1837 TEST_DUP("status", "current", "obsolete");
1838#undef TEST_DUP
1839
1840 /* full content */
1841 str = "grp {action x;anydata any;anyxml anyxml; choice ch;container c;description test;grouping g;leaf l {type string;}"
1842 "leaf-list ll {type string;} list li;notification not;reference test;status current;typedef t {type int8;}uses g;m:ext;} ...";
1843 assert_int_equal(LY_SUCCESS, parse_grouping(&ctx, &str, NULL, &grp));
1844 assert_non_null(grp);
1845 assert_int_equal(LYS_GROUPING, grp->nodetype);
1846 assert_string_equal("grp", grp->name);
1847 assert_string_equal("test", grp->dsc);
1848 assert_non_null(grp->exts);
1849 assert_string_equal("test", grp->ref);
1850 assert_null(grp->parent);
1851 assert_int_equal( LYS_STATUS_CURR, grp->flags);
1852 ly_set_erase(&ctx.tpdfs_nodes, NULL);
1853 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1854
1855 /* invalid content */
1856 str = "grp {config true} ...";
1857 assert_int_equal(LY_EVALID, parse_grouping(&ctx, &str, NULL, &grp));
1858 logbuf_assert("Invalid keyword \"config\" as a child of \"grouping\". Line number 1.");
1859 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1860
1861 str = "grp {must 'expr'} ...";
1862 assert_int_equal(LY_EVALID, parse_grouping(&ctx, &str, NULL, &grp));
1863 logbuf_assert("Invalid keyword \"must\" as a child of \"grouping\". Line number 1.");
1864 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1865
1866 *state = NULL;
1867 ly_ctx_destroy(ctx.ctx, NULL);
1868}
1869
1870static void
Radek Krejcif538ce52019-03-05 10:46:14 +01001871test_action(void **state)
1872{
1873 *state = test_action;
1874
Radek Krejcie7b95092019-05-15 11:03:07 +02001875 struct lys_parser_ctx ctx = {0};
Radek Krejcif538ce52019-03-05 10:46:14 +01001876 struct lysp_action *rpcs = NULL;
1877 struct lysp_node_container *c = NULL;
1878 const char *str;
1879
1880 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1881 assert_non_null(ctx.ctx);
1882 ctx.line = 1;
1883 ctx.mod_version = 2; /* simulate YANG 1.1 */
1884
1885 /* invalid cardinality */
1886#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1887 str = "func {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1888 assert_int_equal(LY_EVALID, parse_action(&ctx, &str, NULL, &rpcs)); \
1889 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1890 FREE_ARRAY(ctx.ctx, rpcs, lysp_action_free); rpcs = NULL;
1891
1892 TEST_DUP("description", "text1", "text2");
1893 TEST_DUP("input", "", "");
1894 TEST_DUP("output", "", "");
1895 TEST_DUP("reference", "1", "2");
1896 TEST_DUP("status", "current", "obsolete");
1897#undef TEST_DUP
1898
1899 /* full content */
1900 str = "top;";
1901 assert_int_equal(LY_SUCCESS, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1902 str = "func {description test;grouping grp;if-feature f;reference test;status current;typedef mytype {type int8;} m:ext;"
1903 "input {anydata a1; anyxml a2; choice ch; container c; grouping grp; leaf l {type int8;} leaf-list ll {type int8;}"
1904 " list li; must 1; typedef mytypei {type int8;} uses grp; m:ext;}"
1905 "output {anydata a1; anyxml a2; choice ch; container c; grouping grp; leaf l {type int8;} leaf-list ll {type int8;}"
1906 " list li; must 1; typedef mytypeo {type int8;} uses grp; m:ext;}} ...";
1907 assert_int_equal(LY_SUCCESS, parse_action(&ctx, &str, (struct lysp_node*)c, &rpcs));
1908 assert_non_null(rpcs);
1909 assert_int_equal(LYS_ACTION, rpcs->nodetype);
1910 assert_string_equal("func", rpcs->name);
1911 assert_string_equal("test", rpcs->dsc);
1912 assert_non_null(rpcs->exts);
1913 assert_non_null(rpcs->iffeatures);
1914 assert_string_equal("test", rpcs->ref);
1915 assert_non_null(rpcs->groupings);
1916 assert_non_null(rpcs->typedefs);
1917 assert_int_equal(LYS_STATUS_CURR, rpcs->flags);
1918 /* input */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001919 assert_int_equal(rpcs->input.nodetype, LYS_INPUT);
Radek Krejcif538ce52019-03-05 10:46:14 +01001920 assert_non_null(rpcs->input.groupings);
1921 assert_non_null(rpcs->input.exts);
1922 assert_non_null(rpcs->input.musts);
1923 assert_non_null(rpcs->input.typedefs);
1924 assert_non_null(rpcs->input.data);
1925 /* output */
Radek Krejcid3ca0632019-04-16 16:54:54 +02001926 assert_int_equal(rpcs->output.nodetype, LYS_OUTPUT);
Radek Krejcif538ce52019-03-05 10:46:14 +01001927 assert_non_null(rpcs->output.groupings);
1928 assert_non_null(rpcs->output.exts);
1929 assert_non_null(rpcs->output.musts);
1930 assert_non_null(rpcs->output.typedefs);
1931 assert_non_null(rpcs->output.data);
1932
1933 ly_set_erase(&ctx.tpdfs_nodes, NULL);
1934 FREE_ARRAY(ctx.ctx, rpcs, lysp_action_free); rpcs = NULL;
1935
1936 /* invalid content */
1937 str = "func {config true} ...";
1938 assert_int_equal(LY_EVALID, parse_action(&ctx, &str, NULL, &rpcs));
1939 logbuf_assert("Invalid keyword \"config\" as a child of \"rpc\". Line number 1.");
1940 FREE_ARRAY(ctx.ctx, rpcs, lysp_action_free); rpcs = NULL;
1941
1942 *state = NULL;
1943 lysp_node_free(ctx.ctx, (struct lysp_node*)c);
1944 ly_ctx_destroy(ctx.ctx, NULL);
1945}
1946
1947static void
Radek Krejcifc11bd72019-04-11 16:00:05 +02001948test_notification(void **state)
1949{
1950 *state = test_notification;
1951
Radek Krejcie7b95092019-05-15 11:03:07 +02001952 struct lys_parser_ctx ctx = {0};
Radek Krejcifc11bd72019-04-11 16:00:05 +02001953 struct lysp_notif *notifs = NULL;
1954 struct lysp_node_container *c = NULL;
1955 const char *str;
1956
1957 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1958 assert_non_null(ctx.ctx);
1959 ctx.line = 1;
1960 ctx.mod_version = 2; /* simulate YANG 1.1 */
1961
1962 /* invalid cardinality */
1963#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1964 str = "func {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1965 assert_int_equal(LY_EVALID, parse_notif(&ctx, &str, NULL, &notifs)); \
1966 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1967 FREE_ARRAY(ctx.ctx, notifs, lysp_notif_free); notifs = NULL;
1968
1969 TEST_DUP("description", "text1", "text2");
1970 TEST_DUP("reference", "1", "2");
1971 TEST_DUP("status", "current", "obsolete");
1972#undef TEST_DUP
1973
1974 /* full content */
1975 str = "top;";
1976 assert_int_equal(LY_SUCCESS, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1977 str = "ntf {anydata a1; anyxml a2; choice ch; container c; description test; grouping grp; if-feature f; leaf l {type int8;}"
1978 "leaf-list ll {type int8;} list li; must 1; reference test; status current; typedef mytype {type int8;} uses grp; m:ext;}";
1979 assert_int_equal(LY_SUCCESS, parse_notif(&ctx, &str, (struct lysp_node*)c, &notifs));
1980 assert_non_null(notifs);
1981 assert_int_equal(LYS_NOTIF, notifs->nodetype);
1982 assert_string_equal("ntf", notifs->name);
1983 assert_string_equal("test", notifs->dsc);
1984 assert_non_null(notifs->exts);
1985 assert_non_null(notifs->iffeatures);
1986 assert_string_equal("test", notifs->ref);
1987 assert_non_null(notifs->groupings);
1988 assert_non_null(notifs->typedefs);
1989 assert_non_null(notifs->musts);
1990 assert_non_null(notifs->data);
1991 assert_int_equal(LYS_STATUS_CURR, notifs->flags);
1992
1993 ly_set_erase(&ctx.tpdfs_nodes, NULL);
1994 FREE_ARRAY(ctx.ctx, notifs, lysp_notif_free); notifs = NULL;
1995
1996 /* invalid content */
1997 str = "ntf {config true} ...";
1998 assert_int_equal(LY_EVALID, parse_notif(&ctx, &str, NULL, &notifs));
1999 logbuf_assert("Invalid keyword \"config\" as a child of \"notification\". Line number 1.");
2000 FREE_ARRAY(ctx.ctx, notifs, lysp_notif_free); notifs = NULL;
2001
2002 *state = NULL;
2003 lysp_node_free(ctx.ctx, (struct lysp_node*)c);
2004 ly_ctx_destroy(ctx.ctx, NULL);
2005}
2006
2007static void
Radek Krejcie86bf772018-12-14 11:39:53 +01002008test_uses(void **state)
2009{
2010 *state = test_uses;
2011
Radek Krejcie7b95092019-05-15 11:03:07 +02002012 struct lys_parser_ctx ctx = {0};
Radek Krejcie86bf772018-12-14 11:39:53 +01002013 struct lysp_node_uses *u = NULL;
2014 const char *str;
2015
2016 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
2017 assert_non_null(ctx.ctx);
2018 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002019 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejcie86bf772018-12-14 11:39:53 +01002020
2021 /* invalid cardinality */
2022#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
2023 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
2024 assert_int_equal(LY_EVALID, parse_uses(&ctx, &str, NULL, (struct lysp_node**)&u)); \
2025 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
2026 lysp_node_free(ctx.ctx, (struct lysp_node*)u); u = NULL;
2027
2028 TEST_DUP("description", "text1", "text2");
2029 TEST_DUP("reference", "1", "2");
2030 TEST_DUP("status", "current", "obsolete");
2031 TEST_DUP("when", "true", "false");
2032#undef TEST_DUP
2033
2034 /* full content */
2035 str = "grpref {augment some/node;description test;if-feature f;reference test;refine some/other/node;status current;when true;m:ext;} ...";
2036 assert_int_equal(LY_SUCCESS, parse_uses(&ctx, &str, NULL, (struct lysp_node**)&u));
2037 assert_non_null(u);
2038 assert_int_equal(LYS_USES, u->nodetype);
2039 assert_string_equal("grpref", u->name);
2040 assert_string_equal("test", u->dsc);
2041 assert_non_null(u->exts);
2042 assert_non_null(u->iffeatures);
2043 assert_string_equal("test", u->ref);
2044 assert_non_null(u->augments);
2045 assert_non_null(u->refines);
2046 assert_non_null(u->when);
2047 assert_null(u->parent);
2048 assert_null(u->next);
2049 assert_int_equal(LYS_STATUS_CURR, u->flags);
2050 lysp_node_free(ctx.ctx, (struct lysp_node*)u); u = NULL;
2051
2052 *state = NULL;
2053 ly_ctx_destroy(ctx.ctx, NULL);
2054}
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002055
2056
2057static void
2058test_augment(void **state)
2059{
2060 *state = test_augment;
2061
Radek Krejcie7b95092019-05-15 11:03:07 +02002062 struct lys_parser_ctx ctx = {0};
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002063 struct lysp_augment *a = NULL;
2064 const char *str;
2065
2066 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
2067 assert_non_null(ctx.ctx);
2068 ctx.line = 1;
Radek Krejcib4adbf12019-02-25 13:35:22 +01002069 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002070
2071 /* invalid cardinality */
2072#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
2073 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
2074 assert_int_equal(LY_EVALID, parse_augment(&ctx, &str, NULL, &a)); \
2075 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
Radek Krejcib4adbf12019-02-25 13:35:22 +01002076 FREE_ARRAY(ctx.ctx, a, lysp_augment_free); a = NULL;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002077
2078 TEST_DUP("description", "text1", "text2");
2079 TEST_DUP("reference", "1", "2");
2080 TEST_DUP("status", "current", "obsolete");
2081 TEST_DUP("when", "true", "false");
2082#undef TEST_DUP
2083
2084 /* full content */
2085 str = "/target/nodeid {action x; anydata any;anyxml anyxml; case cs; choice ch;container c;description test;if-feature f;leaf l {type string;}"
2086 "leaf-list ll {type string;} list li;notification not;reference test;status current;uses g;when true;m:ext;} ...";
2087 assert_int_equal(LY_SUCCESS, parse_augment(&ctx, &str, NULL, &a));
2088 assert_non_null(a);
2089 assert_int_equal(LYS_AUGMENT, a->nodetype);
2090 assert_string_equal("/target/nodeid", a->nodeid);
2091 assert_string_equal("test", a->dsc);
2092 assert_non_null(a->exts);
2093 assert_non_null(a->iffeatures);
2094 assert_string_equal("test", a->ref);
2095 assert_non_null(a->when);
2096 assert_null(a->parent);
2097 assert_int_equal(LYS_STATUS_CURR, a->flags);
Radek Krejcib4adbf12019-02-25 13:35:22 +01002098 FREE_ARRAY(ctx.ctx, a, lysp_augment_free); a = NULL;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002099
2100 *state = NULL;
2101 ly_ctx_destroy(ctx.ctx, NULL);
2102}
2103
Radek Krejci80dd33e2018-09-26 15:57:18 +02002104int main(void)
2105{
2106 const struct CMUnitTest tests[] = {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002107 cmocka_unit_test_setup(test_helpers, logger_setup),
Radek Krejci80dd33e2018-09-26 15:57:18 +02002108 cmocka_unit_test_setup(test_comments, logger_setup),
Radek Krejciefd22f62018-09-27 11:47:58 +02002109 cmocka_unit_test_setup(test_arg, logger_setup),
Radek Krejcidcc7b322018-10-11 14:24:02 +02002110 cmocka_unit_test_setup(test_stmts, logger_setup),
Radek Krejci05b13982018-11-28 16:22:07 +01002111 cmocka_unit_test_setup_teardown(test_minmax, logger_setup, logger_teardown),
Radek Krejci40544fa2019-01-11 09:38:37 +01002112 cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown),
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002113 cmocka_unit_test_setup_teardown(test_identity, logger_setup, logger_teardown),
Radek Krejci2c02f3e2018-10-16 10:54:38 +02002114 cmocka_unit_test_setup(test_feature, logger_setup),
2115 cmocka_unit_test_setup(test_deviation, logger_setup),
2116 cmocka_unit_test_setup(test_deviate, logger_setup),
Radek Krejci8c370832018-11-02 15:10:03 +01002117 cmocka_unit_test_setup(test_container, logger_setup),
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002118 cmocka_unit_test_setup_teardown(test_leaf, logger_setup, logger_teardown),
Radek Krejci0e5d8382018-11-28 16:37:53 +01002119 cmocka_unit_test_setup_teardown(test_leaflist, logger_setup, logger_teardown),
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002120 cmocka_unit_test_setup_teardown(test_list, logger_setup, logger_teardown),
Radek Krejci056d0a82018-12-06 16:57:25 +01002121 cmocka_unit_test_setup_teardown(test_choice, logger_setup, logger_teardown),
Radek Krejcia9026eb2018-12-12 16:04:47 +01002122 cmocka_unit_test_setup_teardown(test_case, logger_setup, logger_teardown),
Radek Krejci9800fb82018-12-13 14:26:23 +01002123 cmocka_unit_test_setup_teardown(test_anydata, logger_setup, logger_teardown),
2124 cmocka_unit_test_setup_teardown(test_anyxml, logger_setup, logger_teardown),
Radek Krejcif538ce52019-03-05 10:46:14 +01002125 cmocka_unit_test_setup_teardown(test_action, logger_setup, logger_teardown),
Radek Krejcifc11bd72019-04-11 16:00:05 +02002126 cmocka_unit_test_setup_teardown(test_notification, logger_setup, logger_teardown),
Radek Krejcie86bf772018-12-14 11:39:53 +01002127 cmocka_unit_test_setup_teardown(test_grouping, logger_setup, logger_teardown),
2128 cmocka_unit_test_setup_teardown(test_uses, logger_setup, logger_teardown),
Radek Krejcib4adbf12019-02-25 13:35:22 +01002129 cmocka_unit_test_setup_teardown(test_augment, logger_setup, logger_teardown),
Radek Krejci80dd33e2018-09-26 15:57:18 +02002130 };
2131
2132 return cmocka_run_group_tests(tests, NULL, NULL);
2133}