blob: 92e4ec8191219fe6ac4d773901cb5489db5a478a [file] [log] [blame]
Radek Krejci80dd33e2018-09-26 15:57:18 +02001/*
2 * @file test_parser_yang.c
3 * @author: Radek Krejci <rkrejci@cesnet.cz>
4 * @brief unit tests for functions from parser_yang.c
5 *
6 * Copyright (c) 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Radek Krejcif3f47842018-11-15 11:22:15 +010015#include "../../src/common.c"
16#include "../../src/set.c"
17#include "../../src/log.c"
18#include "../../src/hash_table.c"
19#include "../../src/xpath.c"
Radek Krejci86d106e2018-10-18 09:53:19 +020020#include "../../src/parser_yang.c"
Radek Krejcif3f47842018-11-15 11:22:15 +010021#include "../../src/context.c"
22#include "../../src/tree_schema_helpers.c"
Radek Krejci19a96102018-11-15 13:38:09 +010023#include "../../src/tree_schema_free.c"
24#include "../../src/tree_schema_compile.c"
Radek Krejcif3f47842018-11-15 11:22:15 +010025#include "../../src/tree_schema.c"
Radek Krejci86d106e2018-10-18 09:53:19 +020026
Radek Krejci80dd33e2018-09-26 15:57:18 +020027#include <stdarg.h>
28#include <stddef.h>
29#include <setjmp.h>
30#include <cmocka.h>
31
32#include <stdio.h>
33#include <string.h>
34
35#include "libyang.h"
Radek Krejci80dd33e2018-09-26 15:57:18 +020036
37#define BUFSIZE 1024
38char logbuf[BUFSIZE] = {0};
Radek Krejci9ed7a192018-10-31 16:23:51 +010039int store = -1; /* negative for infinite logging, positive for limited logging */
Radek Krejci80dd33e2018-09-26 15:57:18 +020040
41/* set to 0 to printing error messages to stderr instead of checking them in code */
Radek Krejci70853c52018-10-15 14:46:16 +020042#define ENABLE_LOGGER_CHECKING 1
Radek Krejci80dd33e2018-09-26 15:57:18 +020043
Radek Krejcid5f2b5f2018-10-11 10:54:36 +020044#if ENABLE_LOGGER_CHECKING
Radek Krejci80dd33e2018-09-26 15:57:18 +020045static void
46logger(LY_LOG_LEVEL level, const char *msg, const char *path)
47{
48 (void) level; /* unused */
Radek Krejci9ed7a192018-10-31 16:23:51 +010049 if (store) {
50 if (path && path[0]) {
51 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
52 } else {
53 strncpy(logbuf, msg, BUFSIZE - 1);
54 }
55 if (store > 0) {
56 --store;
57 }
Radek Krejci80dd33e2018-09-26 15:57:18 +020058 }
59}
Radek Krejcid5f2b5f2018-10-11 10:54:36 +020060#endif
Radek Krejci80dd33e2018-09-26 15:57:18 +020061
62static int
63logger_setup(void **state)
64{
65 (void) state; /* unused */
66#if ENABLE_LOGGER_CHECKING
67 ly_set_log_clb(logger, 1);
68#endif
69 return 0;
70}
71
Radek Krejcib1a5dcc2018-11-26 14:50:05 +010072static int
73logger_teardown(void **state)
74{
75 (void) state; /* unused */
76#if ENABLE_LOGGER_CHECKING
77 if (*state) {
78 fprintf(stderr, "%s\n", logbuf);
79 }
80#endif
81 return 0;
82}
83
Radek Krejci80dd33e2018-09-26 15:57:18 +020084void
85logbuf_clean(void)
86{
87 logbuf[0] = '\0';
88}
89
90#if ENABLE_LOGGER_CHECKING
91# define logbuf_assert(str) assert_string_equal(logbuf, str)
92#else
93# define logbuf_assert(str)
94#endif
95
Radek Krejci2c02f3e2018-10-16 10:54:38 +020096#define TEST_DUP_GENERIC(PREFIX, MEMBER, VALUE1, VALUE2, FUNC, RESULT, LINE, CLEANUP) \
97 str = PREFIX MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
98 assert_int_equal(LY_EVALID, FUNC(&ctx, &str, RESULT)); \
99 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number "LINE"."); \
100 CLEANUP
101
Radek Krejci44ceedc2018-10-02 15:54:31 +0200102static void
103test_helpers(void **state)
104{
105 (void) state; /* unused */
106
107 const char *str;
Radek Krejci404251e2018-10-09 12:06:44 +0200108 char *buf, *p;
Radek Krejci44ceedc2018-10-02 15:54:31 +0200109 size_t len, size;
110 int prefix;
111 struct ly_parser_ctx ctx;
112 ctx.ctx = NULL;
113 ctx.line = 1;
114
115 /* storing into buffer */
116 str = "abcd";
117 buf = NULL;
118 size = len = 0;
119 assert_int_equal(LY_SUCCESS, buf_add_char(NULL, &str, 2, &buf, &size, &len));
120 assert_int_not_equal(0, size);
121 assert_int_equal(2, len);
122 assert_string_equal("cd", str);
123 assert_false(strncmp("ab", buf, 2));
124 free(buf);
Radek Krejci404251e2018-10-09 12:06:44 +0200125 buf = NULL;
126
127 /* invalid first characters */
128 len = 0;
129 str = "2invalid";
130 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1));
131 str = ".invalid";
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 /* invalid following characters */
136 len = 3; /* number of characters read before the str content */
137 str = "!";
138 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1));
139 str = ":";
140 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1));
141 /* valid colon for prefixed identifiers */
142 len = size = 0;
143 p = NULL;
144 str = "x:id";
145 assert_int_equal(LY_SUCCESS, buf_store_char(&ctx, &str, Y_PREF_IDENTIF_ARG, &p, &len, &buf, &size, 0));
146 assert_int_equal(1, len);
147 assert_null(buf);
148 assert_string_equal(":id", str);
149 assert_int_equal('x', p[len - 1]);
150 assert_int_equal(LY_SUCCESS, buf_store_char(&ctx, &str, Y_PREF_IDENTIF_ARG, &p, &len, &buf, &size, 1));
151 assert_int_equal(2, len);
152 assert_string_equal("id", str);
153 assert_int_equal(':', p[len - 1]);
154 free(buf);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200155
156 /* checking identifiers */
157 assert_int_equal(LY_EVALID, check_identifierchar(&ctx, ':', 0, NULL));
158 logbuf_assert("Invalid identifier character ':'. Line number 1.");
159 assert_int_equal(LY_EVALID, check_identifierchar(&ctx, '#', 1, NULL));
160 logbuf_assert("Invalid identifier first character '#'. Line number 1.");
161
162 assert_int_equal(LY_SUCCESS, check_identifierchar(&ctx, 'a', 1, &prefix));
163 assert_int_equal(0, prefix);
164 assert_int_equal(LY_SUCCESS, check_identifierchar(&ctx, ':', 0, &prefix));
165 assert_int_equal(1, prefix);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200166 assert_int_equal(LY_EVALID, check_identifierchar(&ctx, ':', 0, &prefix));
167 assert_int_equal(1, prefix);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200168 assert_int_equal(LY_SUCCESS, check_identifierchar(&ctx, 'b', 0, &prefix));
169 assert_int_equal(2, prefix);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200170 /* second colon is invalid */
171 assert_int_equal(LY_EVALID, check_identifierchar(&ctx, ':', 0, &prefix));
172 logbuf_assert("Invalid identifier character ':'. Line number 1.");
Radek Krejci44ceedc2018-10-02 15:54:31 +0200173}
Radek Krejci80dd33e2018-09-26 15:57:18 +0200174
175static void
176test_comments(void **state)
177{
178 (void) state; /* unused */
179
Radek Krejci44ceedc2018-10-02 15:54:31 +0200180 struct ly_parser_ctx ctx;
Radek Krejci80dd33e2018-09-26 15:57:18 +0200181 const char *str, *p;
Radek Krejciefd22f62018-09-27 11:47:58 +0200182 char *word, *buf;
183 size_t len;
Radek Krejci80dd33e2018-09-26 15:57:18 +0200184
Radek Krejci44ceedc2018-10-02 15:54:31 +0200185 ctx.ctx = NULL;
186 ctx.line = 1;
187
Radek Krejciefd22f62018-09-27 11:47:58 +0200188 str = " // this is a text of / one * line */ comment\nargument";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200189 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200190 assert_string_equal("argument", word);
191 assert_null(buf);
192 assert_int_equal(8, len);
Radek Krejci80dd33e2018-09-26 15:57:18 +0200193
Radek Krejciefd22f62018-09-27 11:47:58 +0200194 str = "/* this is a \n * text // of / block * comment */\"arg\" + \"ume\" \n + \n \"nt\"";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200195 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200196 assert_string_equal("argument", word);
197 assert_ptr_equal(buf, word);
198 assert_int_equal(8, len);
199 free(word);
Radek Krejci80dd33e2018-09-26 15:57:18 +0200200
201 str = p = " this is one line comment on last line";
Radek Krejci44ceedc2018-10-02 15:54:31 +0200202 assert_int_equal(LY_SUCCESS, skip_comment(&ctx, &str, 1));
Radek Krejci80dd33e2018-09-26 15:57:18 +0200203 assert_true(str[0] == '\0');
204
205 str = p = " this is a not terminated comment x";
Radek Krejci44ceedc2018-10-02 15:54:31 +0200206 assert_int_equal(LY_EVALID, skip_comment(&ctx, &str, 2));
207 logbuf_assert("Unexpected end-of-file, non-terminated comment. Line number 5.");
Radek Krejci80dd33e2018-09-26 15:57:18 +0200208 assert_true(str[0] == '\0');
209}
210
Radek Krejciefd22f62018-09-27 11:47:58 +0200211static void
212test_arg(void **state)
213{
214 (void) state; /* unused */
215
Radek Krejci44ceedc2018-10-02 15:54:31 +0200216 struct ly_parser_ctx ctx;
Radek Krejciefd22f62018-09-27 11:47:58 +0200217 const char *str;
218 char *word, *buf;
219 size_t len;
220
Radek Krejci44ceedc2018-10-02 15:54:31 +0200221 ctx.ctx = NULL;
222 ctx.line = 1;
223
Radek Krejciefd22f62018-09-27 11:47:58 +0200224 /* missing argument */
225 str = ";";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200226 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_MAYBE_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200227 assert_null(word);
228
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200229 str = "{";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200230 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200231 logbuf_assert("Invalid character sequence \"{\", expected an argument. Line number 1.");
232
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200233 /* invalid escape sequence */
234 str = "\"\\s\"";
235 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200236 logbuf_assert("Double-quoted string unknown special character \'\\s\'. Line number 1.");
237 str = "\'\\s\'"; /* valid, since it is not an escape sequence in single quoted string */
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200238 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200239 assert_int_equal(2, len);
240 assert_string_equal("\\s\'", word);
241 assert_int_equal('\0', str[0]); /* input has been eaten */
242
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200243 /* invalid character after the argument */
244 str = "hello\"";
245 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
246 logbuf_assert("Invalid character sequence \"\"\", expected unquoted string character, optsep, semicolon or opening brace. Line number 1.");
247 str = "hello}";
248 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
249 logbuf_assert("Invalid character sequence \"}\", expected unquoted string character, optsep, semicolon or opening brace. Line number 1.");
250
251 str = "hello/x\t"; /* slash is not an invalid character */
252 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
253 assert_int_equal(7, len);
254 assert_string_equal("hello/x\t", word);
255
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200256 assert_null(buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200257
258 /* different quoting */
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200259 str = "hello ";
260 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200261 assert_null(buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200262 assert_int_equal(5, len);
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200263 assert_string_equal("hello ", word);
Radek Krejciefd22f62018-09-27 11:47:58 +0200264
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200265 str = "hello/*comment*/\n";
266 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200267 assert_null(buf);
268 assert_int_equal(5, len);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200269 assert_false(strncmp("hello", word, len));
270
271
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200272 str = "\"hello\\n\\t\\\"\\\\\";";
273 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200274 assert_null(buf);
275 assert_int_equal(9, len);
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200276 assert_string_equal("hello\\n\\t\\\"\\\\\";", word);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200277
278 ctx.indent = 14;
279 str = "\"hello \t\n\t\t world!\"";
280 /* - space and tabs before newline are stripped out
281 * - space and tabs after newline (indentation) are stripped out
282 */
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200283 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200284 assert_non_null(buf);
285 assert_ptr_equal(word, buf);
286 assert_int_equal(14, len);
287 assert_string_equal("hello\n world!", word);
288 free(buf);
289
290 ctx.indent = 14;
291 str = "\"hello\n \tworld!\"";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200292 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200293 assert_non_null(buf);
294 assert_ptr_equal(word, buf);
295 assert_int_equal(12, len);
296 assert_string_equal("hello\nworld!", word);
297 free(buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200298
299 str = "\'hello\'";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200300 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200301 assert_null(buf);
302 assert_int_equal(5, len);
303 assert_false(strncmp("hello", word, 5));
304
305 str = "\"hel\" +\t\n\"lo\"";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200306 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200307 assert_ptr_equal(word, buf);
308 assert_int_equal(5, len);
309 assert_string_equal("hello", word);
310 free(buf);
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200311 str = "\"hel\" +\t\nlo"; /* unquoted the second part */
312 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
313 logbuf_assert("Both string parts divided by '+' must be quoted. Line number 5.");
Radek Krejciefd22f62018-09-27 11:47:58 +0200314
315 str = "\'he\'\t\n+ \"llo\"";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200316 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200317 assert_ptr_equal(word, buf);
318 assert_int_equal(5, len);
319 assert_string_equal("hello", word);
320 free(buf);
321
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200322 str = " \t\n\"he\"+\'llo\'";
323 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200324 assert_ptr_equal(word, buf);
325 assert_int_equal(5, len);
326 assert_string_equal("hello", word);
327 free(buf);
328
Radek Krejci44ceedc2018-10-02 15:54:31 +0200329 /* missing argument */
330 str = ";";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200331 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
332 logbuf_assert("Invalid character sequence \";\", expected an argument. Line number 7.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200333}
334
335static void
336test_stmts(void **state)
337{
338 (void) state; /* unused */
339
340 struct ly_parser_ctx ctx;
341 const char *str, *p;
342 enum yang_keyword kw;
343 char *word;
344 size_t len;
345
346 ctx.ctx = NULL;
347 ctx.line = 1;
348
349 str = "\n// comment\n\tinput\t{";
350 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
351 assert_int_equal(YANG_INPUT, kw);
352 assert_int_equal(5, len);
353 assert_string_equal("input\t{", word);
354 assert_string_equal("\t{", str);
355
356 str = "\t /* comment */\t output\n\t{";
357 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
358 assert_int_equal(YANG_OUTPUT, kw);
359 assert_int_equal(6, len);
360 assert_string_equal("output\n\t{", word);
361 assert_string_equal("\n\t{", str);
362
363 str = "/input { "; /* invalid slash */
364 assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len));
365 logbuf_assert("Invalid identifier first character '/'. Line number 4.");
366
367 str = "not-a-statement-nor-extension { "; /* invalid identifier */
368 assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len));
369 logbuf_assert("Invalid character sequence \"not-a-statement-nor-extension\", expected a keyword. Line number 4.");
370
371 str = "path;"; /* missing sep after the keyword */
372 assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len));
373 logbuf_assert("Invalid character sequence \"path;\", expected a keyword followed by a separator. Line number 4.");
374
375 str = "action ";
376 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
377 assert_int_equal(YANG_ACTION, kw);
378 assert_int_equal(6, len);
379 str = "anydata ";
380 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
381 assert_int_equal(YANG_ANYDATA, kw);
382 assert_int_equal(7, len);
383 str = "anyxml ";
384 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
385 assert_int_equal(YANG_ANYXML, kw);
386 assert_int_equal(6, len);
387 str = "argument ";
388 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
389 assert_int_equal(YANG_ARGUMENT, kw);
390 assert_int_equal(8, len);
391 str = "augment ";
392 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
393 assert_int_equal(YANG_AUGMENT, kw);
394 assert_int_equal(7, len);
395 str = "base ";
396 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
397 assert_int_equal(YANG_BASE, kw);
398 assert_int_equal(4, len);
399 str = "belongs-to ";
400 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
401 assert_int_equal(YANG_BELONGS_TO, kw);
402 assert_int_equal(10, len);
403 str = "bit ";
404 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
405 assert_int_equal(YANG_BIT, kw);
406 assert_int_equal(3, len);
407 str = "case ";
408 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
409 assert_int_equal(YANG_CASE, kw);
410 assert_int_equal(4, len);
411 str = "choice ";
412 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
413 assert_int_equal(YANG_CHOICE, kw);
414 assert_int_equal(6, len);
415 str = "config ";
416 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
417 assert_int_equal(YANG_CONFIG, kw);
418 assert_int_equal(6, len);
419 str = "contact ";
420 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
421 assert_int_equal(YANG_CONTACT, kw);
422 assert_int_equal(7, len);
423 str = "container ";
424 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
425 assert_int_equal(YANG_CONTAINER, kw);
426 assert_int_equal(9, len);
427 str = "default ";
428 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
429 assert_int_equal(YANG_DEFAULT, kw);
430 assert_int_equal(7, len);
431 str = "description ";
432 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
433 assert_int_equal(YANG_DESCRIPTION, kw);
434 assert_int_equal(11, len);
435 str = "deviate ";
436 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
437 assert_int_equal(YANG_DEVIATE, kw);
438 assert_int_equal(7, len);
439 str = "deviation ";
440 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
441 assert_int_equal(YANG_DEVIATION, kw);
442 assert_int_equal(9, len);
443 str = "enum ";
444 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
445 assert_int_equal(YANG_ENUM, kw);
446 assert_int_equal(4, len);
447 str = "error-app-tag ";
448 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
449 assert_int_equal(YANG_ERROR_APP_TAG, kw);
450 assert_int_equal(13, len);
451 str = "error-message ";
452 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
453 assert_int_equal(YANG_ERROR_MESSAGE, kw);
454 assert_int_equal(13, len);
455 str = "extension ";
456 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
457 assert_int_equal(YANG_EXTENSION, kw);
458 assert_int_equal(9, len);
459 str = "feature ";
460 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
461 assert_int_equal(YANG_FEATURE, kw);
462 assert_int_equal(7, len);
463 str = "fraction-digits ";
464 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
465 assert_int_equal(YANG_FRACTION_DIGITS, kw);
466 assert_int_equal(15, len);
467 str = "grouping ";
468 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
469 assert_int_equal(YANG_GROUPING, kw);
470 assert_int_equal(8, len);
471 str = "identity ";
472 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
473 assert_int_equal(YANG_IDENTITY, kw);
474 assert_int_equal(8, len);
475 str = "if-feature ";
476 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
477 assert_int_equal(YANG_IF_FEATURE, kw);
478 assert_int_equal(10, len);
479 str = "import ";
480 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
481 assert_int_equal(YANG_IMPORT, kw);
482 assert_int_equal(6, len);
483 str = "include ";
484 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
485 assert_int_equal(YANG_INCLUDE, kw);
486 assert_int_equal(7, len);
487 str = "input{";
488 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
489 assert_int_equal(YANG_INPUT, kw);
490 assert_int_equal(5, len);
491 str = "key ";
492 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
493 assert_int_equal(YANG_KEY, kw);
494 assert_int_equal(3, len);
495 str = "leaf ";
496 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
497 assert_int_equal(YANG_LEAF, kw);
498 assert_int_equal(4, len);
499 str = "leaf-list ";
500 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
501 assert_int_equal(YANG_LEAF_LIST, kw);
502 assert_int_equal(9, len);
503 str = "length ";
504 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
505 assert_int_equal(YANG_LENGTH, kw);
506 assert_int_equal(6, len);
507 str = "list ";
508 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
509 assert_int_equal(YANG_LIST, kw);
510 assert_int_equal(4, len);
511 str = "mandatory ";
512 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
513 assert_int_equal(YANG_MANDATORY, kw);
514 assert_int_equal(9, len);
515 str = "max-elements ";
516 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
517 assert_int_equal(YANG_MAX_ELEMENTS, kw);
518 assert_int_equal(12, len);
519 str = "min-elements ";
520 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
521 assert_int_equal(YANG_MIN_ELEMENTS, kw);
522 assert_int_equal(12, len);
523 str = "modifier ";
524 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
525 assert_int_equal(YANG_MODIFIER, kw);
526 assert_int_equal(8, len);
527 str = "module ";
528 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
529 assert_int_equal(YANG_MODULE, kw);
530 assert_int_equal(6, len);
531 str = "must ";
532 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
533 assert_int_equal(YANG_MUST, kw);
534 assert_int_equal(4, len);
535 str = "namespace ";
536 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
537 assert_int_equal(YANG_NAMESPACE, kw);
538 assert_int_equal(9, len);
539 str = "notification ";
540 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
541 assert_int_equal(YANG_NOTIFICATION, kw);
542 assert_int_equal(12, len);
543 str = "ordered-by ";
544 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
545 assert_int_equal(YANG_ORDERED_BY, kw);
546 assert_int_equal(10, len);
547 str = "organization ";
548 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
549 assert_int_equal(YANG_ORGANIZATION, kw);
550 assert_int_equal(12, len);
551 str = "output ";
552 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
553 assert_int_equal(YANG_OUTPUT, kw);
554 assert_int_equal(6, len);
555 str = "path ";
556 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
557 assert_int_equal(YANG_PATH, kw);
558 assert_int_equal(4, len);
559 str = "pattern ";
560 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
561 assert_int_equal(YANG_PATTERN, kw);
562 assert_int_equal(7, len);
563 str = "position ";
564 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
565 assert_int_equal(YANG_POSITION, kw);
566 assert_int_equal(8, len);
567 str = "prefix ";
568 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
569 assert_int_equal(YANG_PREFIX, kw);
570 assert_int_equal(6, len);
571 str = "presence ";
572 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
573 assert_int_equal(YANG_PRESENCE, kw);
574 assert_int_equal(8, len);
575 str = "range ";
576 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
577 assert_int_equal(YANG_RANGE, kw);
578 assert_int_equal(5, len);
579 str = "reference ";
580 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
581 assert_int_equal(YANG_REFERENCE, kw);
582 assert_int_equal(9, len);
583 str = "refine ";
584 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
585 assert_int_equal(YANG_REFINE, kw);
586 assert_int_equal(6, len);
587 str = "require-instance ";
588 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
589 assert_int_equal(YANG_REQUIRE_INSTANCE, kw);
590 assert_int_equal(16, len);
591 str = "revision ";
592 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
593 assert_int_equal(YANG_REVISION, kw);
594 assert_int_equal(8, len);
595 str = "revision-date ";
596 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
597 assert_int_equal(YANG_REVISION_DATE, kw);
598 assert_int_equal(13, len);
599 str = "rpc ";
600 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
601 assert_int_equal(YANG_RPC, kw);
602 assert_int_equal(3, len);
603 str = "status ";
604 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
605 assert_int_equal(YANG_STATUS, kw);
606 assert_int_equal(6, len);
607 str = "submodule ";
608 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
609 assert_int_equal(YANG_SUBMODULE, kw);
610 assert_int_equal(9, len);
611 str = "type ";
612 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
613 assert_int_equal(YANG_TYPE, kw);
614 assert_int_equal(4, len);
615 str = "typedef ";
616 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
617 assert_int_equal(YANG_TYPEDEF, kw);
618 assert_int_equal(7, len);
619 str = "unique ";
620 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
621 assert_int_equal(YANG_UNIQUE, kw);
622 assert_int_equal(6, len);
623 str = "units ";
624 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
625 assert_int_equal(YANG_UNITS, kw);
626 assert_int_equal(5, len);
627 str = "uses ";
628 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
629 assert_int_equal(YANG_USES, kw);
630 assert_int_equal(4, len);
631 str = "value ";
632 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
633 assert_int_equal(YANG_VALUE, kw);
634 assert_int_equal(5, len);
635 str = "when ";
636 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
637 assert_int_equal(YANG_WHEN, kw);
638 assert_int_equal(4, len);
639 str = "yang-version ";
640 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
641 assert_int_equal(YANG_YANG_VERSION, kw);
642 assert_int_equal(12, len);
643 str = "yin-element ";
644 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
645 assert_int_equal(YANG_YIN_ELEMENT, kw);
646 assert_int_equal(11, len);
Radek Krejci626df482018-10-11 15:06:31 +0200647 str = ";config false;";
648 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
649 assert_int_equal(YANG_SEMICOLON, kw);
650 assert_int_equal(1, len);
651 assert_string_equal("config false;", str);
652 str = "{ config false;";
653 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
654 assert_int_equal(YANG_LEFT_BRACE, kw);
655 assert_int_equal(1, len);
656 assert_string_equal(" config false;", str);
657 str = "}";
658 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
659 assert_int_equal(YANG_RIGHT_BRACE, kw);
660 assert_int_equal(1, len);
661 assert_string_equal("", str);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200662
663 /* geenric extension */
664 str = p = "nacm:default-deny-write;";
665 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
666 assert_int_equal(YANG_CUSTOM, kw);
667 assert_int_equal(23, len);
668 assert_ptr_equal(p, word);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200669}
Radek Krejci44ceedc2018-10-02 15:54:31 +0200670
Radek Krejci05b13982018-11-28 16:22:07 +0100671static void
672test_minmax(void **state)
673{
674 *state = test_minmax;
675
Radek Krejci05b13982018-11-28 16:22:07 +0100676 struct ly_parser_ctx ctx = {0};
677 uint16_t flags = 0;
678 uint32_t value = 0;
679 struct lysp_ext_instance *ext = NULL;
680 const char *str;
681
682 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
683 assert_non_null(ctx.ctx);
684 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100685 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci05b13982018-11-28 16:22:07 +0100686
Radek Krejcidf6cad12018-11-28 17:10:55 +0100687 str = " 1invalid; ...";
Radek Krejci05b13982018-11-28 16:22:07 +0100688 assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext));
Radek Krejcidf6cad12018-11-28 17:10:55 +0100689 logbuf_assert("Invalid value \"1invalid\" of \"min-elements\". Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100690
691 flags = value = 0;
692 str = " -1; ...";
693 assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext));
694 logbuf_assert("Invalid value \"-1\" of \"min-elements\". Line number 1.");
695
Radek Krejcidf6cad12018-11-28 17:10:55 +0100696 /* implementation limit */
697 flags = value = 0;
698 str = " 4294967296; ...";
699 assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext));
700 logbuf_assert("Value \"4294967296\" is out of \"min-elements\" bounds. Line number 1.");
701
Radek Krejci05b13982018-11-28 16:22:07 +0100702 flags = value = 0;
703 str = " 1; ...";
704 assert_int_equal(LY_SUCCESS, parse_minelements(&ctx, &str, &value, &flags, &ext));
705 assert_int_equal(LYS_SET_MIN, flags);
706 assert_int_equal(1, value);
707
708 flags = value = 0;
709 str = " 1 {m:ext;} ...";
710 assert_int_equal(LY_SUCCESS, parse_minelements(&ctx, &str, &value, &flags, &ext));
711 assert_int_equal(LYS_SET_MIN, flags);
712 assert_int_equal(1, value);
713 assert_non_null(ext);
714 FREE_ARRAY(ctx.ctx, ext, lysp_ext_instance_free);
715 ext = NULL;
716
717 flags = value = 0;
718 str = " 1 {config true;} ...";
719 assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext));
720 logbuf_assert("Invalid keyword \"config\" as a child of \"min-elements\". Line number 1.");
721
Radek Krejcidf6cad12018-11-28 17:10:55 +0100722 str = " 1invalid; ...";
Radek Krejci05b13982018-11-28 16:22:07 +0100723 assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext));
Radek Krejcidf6cad12018-11-28 17:10:55 +0100724 logbuf_assert("Invalid value \"1invalid\" of \"max-elements\". Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100725
726 flags = value = 0;
727 str = " -1; ...";
728 assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext));
729 logbuf_assert("Invalid value \"-1\" of \"max-elements\". Line number 1.");
730
Radek Krejcidf6cad12018-11-28 17:10:55 +0100731 /* implementation limit */
732 flags = value = 0;
733 str = " 4294967296; ...";
734 assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext));
735 logbuf_assert("Value \"4294967296\" is out of \"max-elements\" bounds. Line number 1.");
736
Radek Krejci05b13982018-11-28 16:22:07 +0100737 flags = value = 0;
738 str = " 1; ...";
739 assert_int_equal(LY_SUCCESS, parse_maxelements(&ctx, &str, &value, &flags, &ext));
740 assert_int_equal(LYS_SET_MAX, flags);
741 assert_int_equal(1, value);
742
743 flags = value = 0;
744 str = " unbounded; ...";
745 assert_int_equal(LY_SUCCESS, parse_maxelements(&ctx, &str, &value, &flags, &ext));
746 assert_int_equal(LYS_SET_MAX, flags);
747 assert_int_equal(0, value);
748
749 flags = value = 0;
750 str = " 1 {m:ext;} ...";
751 assert_int_equal(LY_SUCCESS, parse_maxelements(&ctx, &str, &value, &flags, &ext));
752 assert_int_equal(LYS_SET_MAX, flags);
753 assert_int_equal(1, value);
754 assert_non_null(ext);
755 FREE_ARRAY(ctx.ctx, ext, lysp_ext_instance_free);
756 ext = NULL;
757
758 flags = value = 0;
759 str = " 1 {config true;} ...";
760 assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext));
761 logbuf_assert("Invalid keyword \"config\" as a child of \"max-elements\". Line number 1.");
762
763 *state = NULL;
764 ly_ctx_destroy(ctx.ctx, NULL);
765}
766
Radek Krejci9fcacc12018-10-11 15:59:11 +0200767static struct lysp_module *
Radek Krejci40544fa2019-01-11 09:38:37 +0100768mod_renew(struct ly_parser_ctx *ctx)
Radek Krejci9fcacc12018-10-11 15:59:11 +0200769{
Radek Krejci40544fa2019-01-11 09:38:37 +0100770 struct lysp_module *mod_p;
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100771 static struct lys_module mod = {0};
772
773 lysc_module_free(mod.compiled, NULL);
774 lysp_module_free(mod.parsed);
775 FREE_STRING(mod.ctx, mod.name);
776 FREE_STRING(mod.ctx, mod.ns);
777 FREE_STRING(mod.ctx, mod.prefix);
778 FREE_STRING(mod.ctx, mod.filepath);
779 FREE_STRING(mod.ctx, mod.org);
780 FREE_STRING(mod.ctx, mod.contact);
781 FREE_STRING(mod.ctx, mod.dsc);
782 FREE_STRING(mod.ctx, mod.ref);
783 memset(&mod, 0, sizeof mod);
784 mod.ctx = ctx->ctx;
785
786 mod_p = calloc(1, sizeof *mod_p);
787 mod.parsed = mod_p;
788 mod_p->mod = &mod;
789 assert_non_null(mod_p);
790 return mod_p;
791}
792
793static struct lysp_submodule *
794submod_renew(struct ly_parser_ctx *ctx, struct lysp_submodule *submod)
795{
796 lysp_submodule_free(ctx->ctx, submod);
797 submod = calloc(1, sizeof *submod);
798 assert_non_null(submod);
799 return submod;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200800}
801
Radek Krejcid33273d2018-10-25 14:55:52 +0200802static LY_ERR test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
803 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
804 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
805{
806 *module_data = user_data;
807 *format = LYS_IN_YANG;
808 *free_module_data = NULL;
809 return LY_SUCCESS;
810}
811
Radek Krejci9fcacc12018-10-11 15:59:11 +0200812static void
813test_module(void **state)
814{
Radek Krejci40544fa2019-01-11 09:38:37 +0100815 *state = test_module;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200816
817 struct ly_parser_ctx ctx;
Radek Krejci40544fa2019-01-11 09:38:37 +0100818 struct lysp_module *mod = NULL;
819 struct lysp_submodule *submod = NULL;
820 struct lys_module *m;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200821 const char *str;
822
823 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
824 assert_non_null(ctx.ctx);
825 ctx.line = 1;
Radek Krejcia042ea12018-10-13 07:52:15 +0200826 ctx.indent = 0;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200827
Radek Krejci40544fa2019-01-11 09:38:37 +0100828 mod = mod_renew(&ctx);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200829
830 /* missing mandatory substatements */
831 str = " name {}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100832 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
833 assert_string_equal("name", mod->mod->name);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200834 logbuf_assert("Missing mandatory keyword \"namespace\" as a child of \"module\". Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100835 mod = mod_renew(&ctx);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200836
837 str = " name {namespace urn:x;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100838 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
839 assert_string_equal("urn:x", mod->mod->ns);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200840 logbuf_assert("Missing mandatory keyword \"prefix\" as a child of \"module\". Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100841 mod = mod_renew(&ctx);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200842
843 str = " name {namespace urn:x;prefix \"x\";}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100844 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod));
845 assert_string_equal("x", mod->mod->prefix);
Radek Krejci40544fa2019-01-11 09:38:37 +0100846 mod = mod_renew(&ctx);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200847
Radek Krejci027d5802018-11-14 16:57:28 +0100848#define SCHEMA_BEGINNING " name {yang-version 1.1;namespace urn:x;prefix \"x\";"
849#define SCHEMA_BEGINNING2 " name {namespace urn:x;prefix \"x\";"
Radek Krejcia042ea12018-10-13 07:52:15 +0200850#define TEST_NODE(NODETYPE, INPUT, NAME) \
851 str = SCHEMA_BEGINNING INPUT; \
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100852 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod)); \
Radek Krejcia042ea12018-10-13 07:52:15 +0200853 assert_non_null(mod->data); \
854 assert_int_equal(NODETYPE, mod->data->nodetype); \
855 assert_string_equal(NAME, mod->data->name); \
Radek Krejci40544fa2019-01-11 09:38:37 +0100856 mod = mod_renew(&ctx);
Radek Krejcia042ea12018-10-13 07:52:15 +0200857#define TEST_GENERIC(INPUT, TARGET, TEST) \
858 str = SCHEMA_BEGINNING INPUT; \
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100859 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod)); \
Radek Krejcia042ea12018-10-13 07:52:15 +0200860 assert_non_null(TARGET); \
861 TEST; \
Radek Krejci40544fa2019-01-11 09:38:37 +0100862 mod = mod_renew(&ctx);
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100863#define TEST_DUP(MEMBER, VALUE1, VALUE2, LINE) \
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200864 TEST_DUP_GENERIC(SCHEMA_BEGINNING, MEMBER, VALUE1, VALUE2, \
Radek Krejci40544fa2019-01-11 09:38:37 +0100865 parse_module, mod, LINE, mod = mod_renew(&ctx))
Radek Krejcia042ea12018-10-13 07:52:15 +0200866
867 /* duplicated namespace, prefix */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100868 TEST_DUP("namespace", "y", "z", "1");
869 TEST_DUP("prefix", "y", "z", "1");
870 TEST_DUP("contact", "a", "b", "1");
871 TEST_DUP("description", "a", "b", "1");
872 TEST_DUP("organization", "a", "b", "1");
873 TEST_DUP("reference", "a", "b", "1");
Radek Krejcia042ea12018-10-13 07:52:15 +0200874
Radek Krejci70853c52018-10-15 14:46:16 +0200875 /* not allowed in module (submodule-specific) */
876 str = SCHEMA_BEGINNING "belongs-to master {prefix m;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100877 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejci70853c52018-10-15 14:46:16 +0200878 logbuf_assert("Invalid keyword \"belongs-to\" as a child of \"module\". Line number 1.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100879 mod = mod_renew(&ctx);
Radek Krejci70853c52018-10-15 14:46:16 +0200880
Radek Krejcia042ea12018-10-13 07:52:15 +0200881 /* anydata */
882 TEST_NODE(LYS_ANYDATA, "anydata test;}", "test");
883 /* anyxml */
884 TEST_NODE(LYS_ANYXML, "anyxml test;}", "test");
885 /* augment */
886 TEST_GENERIC("augment /somepath;}", mod->augments,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200887 assert_string_equal("/somepath", mod->augments[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200888 /* choice */
889 TEST_NODE(LYS_CHOICE, "choice test;}", "test");
890 /* contact 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100891 TEST_GENERIC("contact \"firstname\" + \n\t\" surname\";}", mod->mod->contact,
892 assert_string_equal("firstname surname", mod->mod->contact));
Radek Krejcia042ea12018-10-13 07:52:15 +0200893 /* container */
894 TEST_NODE(LYS_CONTAINER, "container test;}", "test");
895 /* description 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100896 TEST_GENERIC("description \'some description\';}", mod->mod->dsc,
897 assert_string_equal("some description", mod->mod->dsc));
Radek Krejcia042ea12018-10-13 07:52:15 +0200898 /* deviation */
899 TEST_GENERIC("deviation /somepath {deviate not-supported;}}", mod->deviations,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200900 assert_string_equal("/somepath", mod->deviations[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200901 /* extension */
902 TEST_GENERIC("extension test;}", mod->extensions,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200903 assert_string_equal("test", mod->extensions[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200904 /* feature */
905 TEST_GENERIC("feature test;}", mod->features,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200906 assert_string_equal("test", mod->features[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200907 /* grouping */
908 TEST_GENERIC("grouping grp;}", mod->groupings,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200909 assert_string_equal("grp", mod->groupings[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200910 /* identity */
911 TEST_GENERIC("identity test;}", mod->identities,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200912 assert_string_equal("test", mod->identities[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200913 /* import */
Radek Krejci086c7132018-10-26 15:29:04 +0200914 ly_ctx_set_module_imp_clb(ctx.ctx, test_imp_clb, "module zzz { namespace urn:zzz; prefix z;}");
915 TEST_GENERIC("import zzz {prefix z;}}", mod->imports,
916 assert_string_equal("zzz", mod->imports[0].name));
Radek Krejci70853c52018-10-15 14:46:16 +0200917
Radek Krejcia042ea12018-10-13 07:52:15 +0200918 /* import - prefix collision */
Radek Krejci086c7132018-10-26 15:29:04 +0200919 str = SCHEMA_BEGINNING "import zzz {prefix x;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100920 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejci70853c52018-10-15 14:46:16 +0200921 logbuf_assert("Prefix \"x\" already used as module prefix. Line number 2.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100922 mod = mod_renew(&ctx);
Radek Krejci086c7132018-10-26 15:29:04 +0200923 str = SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix y;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100924 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejci086c7132018-10-26 15:29:04 +0200925 logbuf_assert("Prefix \"y\" already used to import \"zzz\" module. Line number 2.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100926 mod = mod_renew(&ctx);
Radek Krejci086c7132018-10-26 15:29:04 +0200927 str = "module" SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix z;}}";
928 assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
929 assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx));
930 logbuf_assert("Single revision of the module \"zzz\" referred twice.");
Radek Krejci70853c52018-10-15 14:46:16 +0200931
Radek Krejcia042ea12018-10-13 07:52:15 +0200932 /* include */
Radek Krejci9ed7a192018-10-31 16:23:51 +0100933 store = 1;
Radek Krejcid33273d2018-10-25 14:55:52 +0200934 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 +0200935 str = "module" SCHEMA_BEGINNING "include xxx;}";
936 assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
937 assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx));
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100938 logbuf_assert("Input data contains module in situation when a submodule is expected.");
Radek Krejci9ed7a192018-10-31 16:23:51 +0100939 store = -1;
Radek Krejcid33273d2018-10-25 14:55:52 +0200940
Radek Krejci9ed7a192018-10-31 16:23:51 +0100941 store = 1;
Radek Krejci313d9902018-11-08 09:42:58 +0100942 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 +0200943 str = "module" SCHEMA_BEGINNING "include xxx;}";
944 assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
945 assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx));
946 logbuf_assert("Included \"xxx\" submodule from \"name\" belongs-to a different module \"wrong-name\".");
Radek Krejci9ed7a192018-10-31 16:23:51 +0100947 store = -1;
Radek Krejcid33273d2018-10-25 14:55:52 +0200948
Radek Krejci313d9902018-11-08 09:42:58 +0100949 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 +0200950 TEST_GENERIC("include xxx;}", mod->includes,
Radek Krejci086c7132018-10-26 15:29:04 +0200951 assert_string_equal("xxx", mod->includes[0].name));
Radek Krejcid33273d2018-10-25 14:55:52 +0200952
Radek Krejcia042ea12018-10-13 07:52:15 +0200953 /* leaf */
954 TEST_NODE(LYS_LEAF, "leaf test {type string;}}", "test");
955 /* leaf-list */
956 TEST_NODE(LYS_LEAFLIST, "leaf-list test {type string;}}", "test");
957 /* list */
958 TEST_NODE(LYS_LIST, "list test {key a;leaf a {type string;}}}", "test");
959 /* notification */
960 TEST_GENERIC("notification test;}", mod->notifs,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200961 assert_string_equal("test", mod->notifs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200962 /* organization 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100963 TEST_GENERIC("organization \"CESNET a.l.e.\";}", mod->mod->org,
964 assert_string_equal("CESNET a.l.e.", mod->mod->org));
Radek Krejcia042ea12018-10-13 07:52:15 +0200965 /* reference 0..1 */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100966 TEST_GENERIC("reference RFC7950;}", mod->mod->ref,
967 assert_string_equal("RFC7950", mod->mod->ref));
Radek Krejcia042ea12018-10-13 07:52:15 +0200968 /* revision */
969 TEST_GENERIC("revision 2018-10-12;}", mod->revs,
Radek Krejcib7db73a2018-10-24 14:18:40 +0200970 assert_string_equal("2018-10-12", mod->revs[0].date));
Radek Krejcia042ea12018-10-13 07:52:15 +0200971 /* rpc */
972 TEST_GENERIC("rpc test;}", mod->rpcs,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200973 assert_string_equal("test", mod->rpcs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200974 /* typedef */
975 TEST_GENERIC("typedef test{type string;}}", mod->typedefs,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200976 assert_string_equal("test", mod->typedefs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200977 /* uses */
978 TEST_NODE(LYS_USES, "uses test;}", "test");
979 /* yang-version */
Radek Krejci027d5802018-11-14 16:57:28 +0100980 str = SCHEMA_BEGINNING2 "\n\tyang-version 10;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100981 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejcia042ea12018-10-13 07:52:15 +0200982 logbuf_assert("Invalid value \"10\" of \"yang-version\". Line number 3.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100983 mod = mod_renew(&ctx);
Radek Krejci027d5802018-11-14 16:57:28 +0100984 str = SCHEMA_BEGINNING2 "yang-version 1.0;yang-version 1.1;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100985 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejcia042ea12018-10-13 07:52:15 +0200986 logbuf_assert("Duplicate keyword \"yang-version\". Line number 3.");
Radek Krejci40544fa2019-01-11 09:38:37 +0100987 mod = mod_renew(&ctx);
Radek Krejci027d5802018-11-14 16:57:28 +0100988 str = SCHEMA_BEGINNING2 "yang-version 1.0;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100989 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod));
990 assert_int_equal(1, mod->mod->version);
Radek Krejci40544fa2019-01-11 09:38:37 +0100991 mod = mod_renew(&ctx);
Radek Krejci027d5802018-11-14 16:57:28 +0100992 str = SCHEMA_BEGINNING2 "yang-version \"1.1\";}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100993 assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod));
994 assert_int_equal(2, mod->mod->version);
Radek Krejci40544fa2019-01-11 09:38:37 +0100995 mod = mod_renew(&ctx);
996
997 str = "module " SCHEMA_BEGINNING "} module q {namespace urn:q;prefixq;}";
998 m = mod->mod;
999 free(mod);
1000 m->parsed = NULL;
1001 assert_int_equal(LY_EVALID, yang_parse_module(&ctx, str, m));
1002 logbuf_assert("Invalid character sequence \"module\", expected end-of-file. Line number 3.");
1003 mod = mod_renew(&ctx);
1004
1005 str = "prefix " SCHEMA_BEGINNING "}";
1006 m = mod->mod;
1007 free(mod);
1008 m->parsed = NULL;
1009 assert_int_equal(LY_EVALID, yang_parse_module(&ctx, str, m));
1010 logbuf_assert("Invalid keyword \"prefix\", expected \"module\" or \"submodule\". Line number 3.");
1011 mod = mod_renew(&ctx);
Radek Krejci09306362018-10-15 15:26:01 +02001012
Radek Krejci156ccaf2018-10-15 15:49:17 +02001013 /* extensions */
1014 TEST_GENERIC("prefix:test;}", mod->exts,
Radek Krejci2c4e7172018-10-19 15:56:26 +02001015 assert_string_equal("prefix:test", mod->exts[0].name);
1016 assert_int_equal(LYEXT_SUBSTMT_SELF, mod->exts[0].insubstmt));
Radek Krejci40544fa2019-01-11 09:38:37 +01001017 mod = mod_renew(&ctx);
Radek Krejci156ccaf2018-10-15 15:49:17 +02001018
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001019 /* invalid substatement */
1020 str = SCHEMA_BEGINNING "must false;}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001021 assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod));
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001022 logbuf_assert("Invalid keyword \"must\" as a child of \"module\". Line number 3.");
Radek Krejci40544fa2019-01-11 09:38:37 +01001023 mod = mod_renew(&ctx);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001024
Radek Krejci09306362018-10-15 15:26:01 +02001025 /* submodule */
Radek Krejci40544fa2019-01-11 09:38:37 +01001026 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001027
1028 /* missing mandatory substatements */
1029 str = " subname {}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001030 lydict_remove(ctx.ctx, submod->name);
1031 assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod));
1032 assert_string_equal("subname", submod->name);
Radek Krejci09306362018-10-15 15:26:01 +02001033 logbuf_assert("Missing mandatory keyword \"belongs-to\" as a child of \"submodule\". Line number 3.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001034 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001035
Radek Krejci313d9902018-11-08 09:42:58 +01001036 str = " subname {belongs-to name {prefix x;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001037 lydict_remove(ctx.ctx, submod->name);
1038 assert_int_equal(LY_SUCCESS, parse_submodule(&ctx, &str, submod));
1039 assert_string_equal("name", submod->belongsto);
1040 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001041
1042#undef SCHEMA_BEGINNING
Radek Krejci313d9902018-11-08 09:42:58 +01001043#define SCHEMA_BEGINNING " subname {belongs-to name {prefix x;}"
Radek Krejci09306362018-10-15 15:26:01 +02001044
1045 /* duplicated namespace, prefix */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001046 str = " subname {belongs-to name {prefix x;}belongs-to module1;belongs-to module2;} ...";
1047 assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod)); \
1048 logbuf_assert("Duplicate keyword \"belongs-to\". Line number 3."); \
1049 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001050
1051 /* not allowed in submodule (module-specific) */
1052 str = SCHEMA_BEGINNING "namespace \"urn:z\";}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001053 assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod));
Radek Krejci09306362018-10-15 15:26:01 +02001054 logbuf_assert("Invalid keyword \"namespace\" as a child of \"submodule\". Line number 3.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001055 submod = submod_renew(&ctx, submod);
Radek Krejci09306362018-10-15 15:26:01 +02001056 str = SCHEMA_BEGINNING "prefix m;}}";
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001057 assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod));
Radek Krejci09306362018-10-15 15:26:01 +02001058 logbuf_assert("Invalid keyword \"prefix\" as a child of \"submodule\". Line number 3.");
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001059 submod = submod_renew(&ctx, submod);
Radek Krejcia042ea12018-10-13 07:52:15 +02001060
Radek Krejci40544fa2019-01-11 09:38:37 +01001061 str = "submodule " SCHEMA_BEGINNING "} module q {namespace urn:q;prefixq;}";
1062 lysp_submodule_free(ctx.ctx, submod);
1063 submod = NULL;
1064 assert_int_equal(LY_EVALID, yang_parse_submodule(&ctx, str, &submod));
1065 logbuf_assert("Invalid character sequence \"module\", expected end-of-file. Line number 3.");
1066
1067 str = "prefix " SCHEMA_BEGINNING "}";
1068 assert_int_equal(LY_EVALID, yang_parse_submodule(&ctx, str, &submod));
1069 logbuf_assert("Invalid keyword \"prefix\", expected \"module\" or \"submodule\". Line number 3.");
1070 submod = submod_renew(&ctx, submod);
1071
Radek Krejcia042ea12018-10-13 07:52:15 +02001072#undef TEST_GENERIC
1073#undef TEST_NODE
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001074#undef TEST_DUP
Radek Krejcia042ea12018-10-13 07:52:15 +02001075#undef SCHEMA_BEGINNING
1076
Radek Krejci9fcacc12018-10-11 15:59:11 +02001077 lysp_module_free(mod);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001078 lysp_submodule_free(ctx.ctx, submod);
Radek Krejci9fcacc12018-10-11 15:59:11 +02001079 ly_ctx_destroy(ctx.ctx, NULL);
Radek Krejci40544fa2019-01-11 09:38:37 +01001080
1081 *state = NULL;
Radek Krejciefd22f62018-09-27 11:47:58 +02001082}
1083
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001084static void
1085test_identity(void **state)
1086{
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001087 *state = test_identity;
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001088
1089 struct ly_parser_ctx ctx;
1090 struct lysp_ident *ident = NULL;
1091 const char *str;
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001092
1093 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1094 assert_non_null(ctx.ctx);
1095 ctx.line = 1;
1096 ctx.indent = 0;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001097 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001098
1099 /* invalid cardinality */
1100#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001101 TEST_DUP_GENERIC(" test {", MEMBER, VALUE1, VALUE2, parse_identity, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001102 &ident, "1", FREE_ARRAY(ctx.ctx, ident, lysp_ident_free); ident = NULL)
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001103
Radek Krejci38222632019-02-12 16:55:05 +01001104 TEST_DUP("description", "a", "b");
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001105 TEST_DUP("reference", "a", "b");
1106 TEST_DUP("status", "current", "obsolete");
1107
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001108 /* full content */
1109 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 +02001110 assert_int_equal(LY_SUCCESS, parse_identity(&ctx, &str, &ident));
1111 assert_non_null(ident);
1112 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001113 FREE_ARRAY(ctx.ctx, ident, lysp_ident_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001114 ident = NULL;
1115
1116 /* invalid substatement */
1117 str = " test {organization XXX;}";
1118 assert_int_equal(LY_EVALID, parse_identity(&ctx, &str, &ident));
1119 logbuf_assert("Invalid keyword \"organization\" as a child of \"identity\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001120 FREE_ARRAY(ctx.ctx, ident, lysp_ident_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001121 ident = NULL;
1122
1123#undef TEST_DUP
1124
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001125 *state = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001126 ly_ctx_destroy(ctx.ctx, NULL);
1127}
1128
1129static void
1130test_feature(void **state)
1131{
1132 (void) state; /* unused */
1133
1134 struct ly_parser_ctx ctx;
1135 struct lysp_feature *features = NULL;
1136 const char *str;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001137
1138 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1139 assert_non_null(ctx.ctx);
1140 ctx.line = 1;
1141 ctx.indent = 0;
1142
1143 /* invalid cardinality */
1144#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1145 TEST_DUP_GENERIC(" test {", MEMBER, VALUE1, VALUE2, parse_feature, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001146 &features, "1", FREE_ARRAY(ctx.ctx, features, lysp_feature_free); features = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001147
1148 TEST_DUP("description", "a", "b");
1149 TEST_DUP("reference", "a", "b");
1150 TEST_DUP("status", "current", "obsolete");
1151
1152 /* full content */
1153 str = " test {description text;reference \'another text\';status current; if-feature x;if-feature y;prefix:ext;} ...";
1154 assert_int_equal(LY_SUCCESS, parse_feature(&ctx, &str, &features));
1155 assert_non_null(features);
1156 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001157 FREE_ARRAY(ctx.ctx, features, lysp_feature_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001158 features = NULL;
1159
1160 /* invalid substatement */
1161 str = " test {organization XXX;}";
1162 assert_int_equal(LY_EVALID, parse_feature(&ctx, &str, &features));
1163 logbuf_assert("Invalid keyword \"organization\" as a child of \"feature\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001164 FREE_ARRAY(ctx.ctx, features, lysp_feature_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001165 features = NULL;
1166
1167#undef TEST_DUP
1168
1169 ly_ctx_destroy(ctx.ctx, NULL);
1170}
1171
1172static void
1173test_deviation(void **state)
1174{
1175 (void) state; /* unused */
1176
1177 struct ly_parser_ctx ctx;
1178 struct lysp_deviation *d = NULL;
1179 const char *str;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001180
1181 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1182 assert_non_null(ctx.ctx);
1183 ctx.line = 1;
1184 ctx.indent = 0;
1185
1186 /* invalid cardinality */
1187#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1188 TEST_DUP_GENERIC(" test {deviate not-supported;", MEMBER, VALUE1, VALUE2, parse_deviation, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001189 &d, "1", FREE_ARRAY(ctx.ctx, d, lysp_deviation_free); d = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001190
1191 TEST_DUP("description", "a", "b");
1192 TEST_DUP("reference", "a", "b");
1193
1194 /* full content */
1195 str = " test {deviate not-supported;description text;reference \'another text\';prefix:ext;} ...";
1196 assert_int_equal(LY_SUCCESS, parse_deviation(&ctx, &str, &d));
1197 assert_non_null(d);
1198 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001199 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001200 d = NULL;
1201
1202 /* missing mandatory substatement */
1203 str = " test {description text;}";
1204 assert_int_equal(LY_EVALID, parse_deviation(&ctx, &str, &d));
1205 logbuf_assert("Missing mandatory keyword \"deviate\" as a child of \"deviation\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001206 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001207 d = NULL;
1208
1209 /* invalid substatement */
1210 str = " test {deviate not-supported; status obsolete;}";
1211 assert_int_equal(LY_EVALID, parse_deviation(&ctx, &str, &d));
1212 logbuf_assert("Invalid keyword \"status\" as a child of \"deviation\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001213 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001214 d = NULL;
1215
1216#undef TEST_DUP
1217
1218 ly_ctx_destroy(ctx.ctx, NULL);
1219}
1220
1221static void
1222test_deviate(void **state)
1223{
1224 (void) state; /* unused */
1225
1226 struct ly_parser_ctx ctx;
1227 struct lysp_deviate *d = NULL;
1228 const char *str;
1229
1230 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1231 assert_non_null(ctx.ctx);
1232 ctx.line = 1;
1233 ctx.indent = 0;
1234
1235 /* invalid cardinality */
1236#define TEST_DUP(TYPE, MEMBER, VALUE1, VALUE2) \
1237 TEST_DUP_GENERIC(TYPE" {", MEMBER, VALUE1, VALUE2, parse_deviate, \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001238 &d, "1", lysp_deviate_free(ctx.ctx, d); free(d); d = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001239
1240 TEST_DUP("add", "config", "true", "false");
1241 TEST_DUP("replace", "default", "int8", "uint8");
1242 TEST_DUP("add", "mandatory", "true", "false");
1243 TEST_DUP("add", "max-elements", "1", "2");
1244 TEST_DUP("add", "min-elements", "1", "2");
1245 TEST_DUP("replace", "type", "int8", "uint8");
1246 TEST_DUP("add", "units", "kilometers", "miles");
1247
1248 /* full contents */
1249 str = " not-supported {prefix:ext;} ...";
1250 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1251 assert_non_null(d);
1252 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001253 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001254 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;} ...";
1255 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1256 assert_non_null(d);
1257 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001258 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001259 str = " delete {units meters; must 1; must 2; unique x; unique y; default a; default b; prefix:ext;} ...";
1260 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1261 assert_non_null(d);
1262 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001263 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001264 str = " replace {type string; units meters; default a; config true; mandatory true; min-elements 1; max-elements 2; prefix:ext;} ...";
1265 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1266 assert_non_null(d);
1267 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001268 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001269
1270 /* invalid substatements */
1271#define TEST_NOT_SUP(DEV, STMT, VALUE) \
1272 str = " "DEV" {"STMT" "VALUE";}..."; \
1273 assert_int_equal(LY_EVALID, parse_deviate(&ctx, &str, &d)); \
1274 logbuf_assert("Deviate \""DEV"\" does not support keyword \""STMT"\". Line number 1."); \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001275 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001276
1277 TEST_NOT_SUP("not-supported", "units", "meters");
1278 TEST_NOT_SUP("not-supported", "must", "1");
1279 TEST_NOT_SUP("not-supported", "unique", "x");
1280 TEST_NOT_SUP("not-supported", "default", "a");
1281 TEST_NOT_SUP("not-supported", "config", "true");
1282 TEST_NOT_SUP("not-supported", "mandatory", "true");
1283 TEST_NOT_SUP("not-supported", "min-elements", "1");
1284 TEST_NOT_SUP("not-supported", "max-elements", "2");
1285 TEST_NOT_SUP("not-supported", "type", "string");
1286 TEST_NOT_SUP("add", "type", "string");
1287 TEST_NOT_SUP("delete", "config", "true");
1288 TEST_NOT_SUP("delete", "mandatory", "true");
1289 TEST_NOT_SUP("delete", "min-elements", "1");
1290 TEST_NOT_SUP("delete", "max-elements", "2");
1291 TEST_NOT_SUP("delete", "type", "string");
1292 TEST_NOT_SUP("replace", "must", "1");
1293 TEST_NOT_SUP("replace", "unique", "a");
1294
1295 str = " nonsence; ...";
1296 assert_int_equal(LY_EVALID, parse_deviate(&ctx, &str, &d));
1297 logbuf_assert("Invalid value \"nonsence\" of \"deviate\". Line number 1.");
1298 assert_null(d);
1299
1300#undef TEST_NOT_SUP
1301#undef TEST_DUP
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001302
1303 ly_ctx_destroy(ctx.ctx, NULL);
1304}
1305
Radek Krejci8c370832018-11-02 15:10:03 +01001306static void
1307test_container(void **state)
1308{
1309 (void) state; /* unused */
1310
Radek Krejci313d9902018-11-08 09:42:58 +01001311 struct ly_parser_ctx ctx = {0};
Radek Krejci8c370832018-11-02 15:10:03 +01001312 struct lysp_node_container *c = NULL;
1313 const char *str;
1314
1315 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1316 assert_non_null(ctx.ctx);
1317 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001318 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci8c370832018-11-02 15:10:03 +01001319
1320 /* invalid cardinality */
1321#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1322 str = "cont {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1323 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c)); \
1324 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001325 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001326
1327 TEST_DUP("config", "true", "false");
1328 TEST_DUP("description", "text1", "text2");
1329 TEST_DUP("presence", "true", "false");
1330 TEST_DUP("reference", "1", "2");
1331 TEST_DUP("status", "current", "obsolete");
1332 TEST_DUP("when", "true", "false");
1333#undef TEST_DUP
1334
1335 /* full content */
Radek Krejci313d9902018-11-08 09:42:58 +01001336 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;}"
1337 "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 +01001338 assert_int_equal(LY_SUCCESS, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1339 assert_non_null(c);
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001340 assert_int_equal(LYS_CONTAINER, c->nodetype);
1341 assert_string_equal("cont", c->name);
Radek Krejci8c370832018-11-02 15:10:03 +01001342 assert_non_null(c->actions);
1343 assert_non_null(c->child);
1344 assert_string_equal("test", c->dsc);
1345 assert_non_null(c->exts);
1346 assert_non_null(c->groupings);
1347 assert_non_null(c->iffeatures);
1348 assert_non_null(c->musts);
1349 assert_non_null(c->notifs);
1350 assert_string_equal("true", c->presence);
1351 assert_string_equal("test", c->ref);
1352 assert_non_null(c->typedefs);
1353 assert_non_null(c->when);
1354 assert_null(c->parent);
1355 assert_null(c->next);
1356 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR, c->flags);
Radek Krejci313d9902018-11-08 09:42:58 +01001357 ly_set_erase(&ctx.tpdfs_nodes, NULL);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001358 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001359
1360 /* invalid */
1361 str = " cont {augment /root;} ...";
1362 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1363 logbuf_assert("Invalid keyword \"augment\" as a child of \"container\". Line number 1.");
Radek Krejci4f28eda2018-11-12 11:46:16 +01001364 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001365 str = " cont {nonsence true;} ...";
1366 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1367 logbuf_assert("Invalid character sequence \"nonsence\", expected a keyword. Line number 1.");
Radek Krejci4f28eda2018-11-12 11:46:16 +01001368 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001369
Radek Krejcif538ce52019-03-05 10:46:14 +01001370 ctx.mod_version = 1; /* simulate YANG 1.0 */
1371 str = " cont {action x;} ...";
1372 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1373 logbuf_assert("Invalid keyword \"action\" as a child of \"container\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
1374 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
1375
Radek Krejci8c370832018-11-02 15:10:03 +01001376 ly_ctx_destroy(ctx.ctx, NULL);
1377}
1378
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001379static void
1380test_leaf(void **state)
1381{
1382 *state = test_leaf;
1383
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001384 struct ly_parser_ctx ctx = {0};
1385 struct lysp_node_leaf *l = NULL;
1386 const char *str;
1387
1388 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1389 assert_non_null(ctx.ctx);
1390 ctx.line = 1;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001391 //ctx.mod->version = 2; /* simulate YANG 1.1 */
1392
1393 /* invalid cardinality */
1394#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1395 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1396 assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l)); \
1397 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1398 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1399
1400 TEST_DUP("config", "true", "false");
1401 TEST_DUP("default", "x", "y");
1402 TEST_DUP("description", "text1", "text2");
1403 TEST_DUP("mandatory", "true", "false");
1404 TEST_DUP("reference", "1", "2");
1405 TEST_DUP("status", "current", "obsolete");
Radek Krejci0e5d8382018-11-28 16:37:53 +01001406 TEST_DUP("type", "int8", "uint8");
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001407 TEST_DUP("units", "text1", "text2");
1408 TEST_DUP("when", "true", "false");
1409#undef TEST_DUP
1410
1411 /* full content - without mandatory which is mutual exclusive with default */
1412 str = "l {config false;default \"xxx\";description test;if-feature f;"
1413 "must 'expr';reference test;status current;type string; units yyy;when true;m:ext;} ...";
1414 assert_int_equal(LY_SUCCESS, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
1415 assert_non_null(l);
1416 assert_int_equal(LYS_LEAF, l->nodetype);
1417 assert_string_equal("l", l->name);
1418 assert_string_equal("test", l->dsc);
1419 assert_string_equal("xxx", l->dflt);
1420 assert_string_equal("yyy", l->units);
1421 assert_string_equal("string", l->type.name);
1422 assert_non_null(l->exts);
1423 assert_non_null(l->iffeatures);
1424 assert_non_null(l->musts);
1425 assert_string_equal("test", l->ref);
1426 assert_non_null(l->when);
1427 assert_null(l->parent);
1428 assert_null(l->next);
1429 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR, l->flags);
1430 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1431
1432 /* full content - now with mandatory */
1433 str = "l {mandatory true; type string;} ...";
1434 assert_int_equal(LY_SUCCESS, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
1435 assert_non_null(l);
1436 assert_int_equal(LYS_LEAF, l->nodetype);
1437 assert_string_equal("l", l->name);
1438 assert_string_equal("string", l->type.name);
1439 assert_int_equal(LYS_MAND_TRUE, l->flags);
1440 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1441
1442 /* invalid */
1443 str = " l {mandatory true; default xx; type string;} ...";
1444 assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
Radek Krejcia9026eb2018-12-12 16:04:47 +01001445 logbuf_assert("Invalid combination of keywords \"mandatory\" and \"default\" as substatements of \"leaf\". Line number 1.");
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001446 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1447
1448 str = " l {description \"missing type\";} ...";
1449 assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
1450 logbuf_assert("Missing mandatory keyword \"type\" as a child of \"leaf\". Line number 1.");
1451 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1452
1453 *state = NULL;
1454 ly_ctx_destroy(ctx.ctx, NULL);
1455}
1456
Radek Krejci0e5d8382018-11-28 16:37:53 +01001457static void
1458test_leaflist(void **state)
1459{
1460 *state = test_leaf;
1461
Radek Krejci0e5d8382018-11-28 16:37:53 +01001462 struct ly_parser_ctx ctx = {0};
1463 struct lysp_node_leaflist *ll = NULL;
1464 const char *str;
1465
1466 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1467 assert_non_null(ctx.ctx);
1468 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001469 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci0e5d8382018-11-28 16:37:53 +01001470
1471 /* invalid cardinality */
1472#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1473 str = "ll {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1474 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll)); \
1475 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1476 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1477
1478 TEST_DUP("config", "true", "false");
1479 TEST_DUP("description", "text1", "text2");
1480 TEST_DUP("max-elements", "10", "20");
1481 TEST_DUP("min-elements", "10", "20");
1482 TEST_DUP("ordered-by", "user", "system");
1483 TEST_DUP("reference", "1", "2");
1484 TEST_DUP("status", "current", "obsolete");
1485 TEST_DUP("type", "int8", "uint8");
1486 TEST_DUP("units", "text1", "text2");
1487 TEST_DUP("when", "true", "false");
1488#undef TEST_DUP
1489
1490 /* full content - without min-elements which is mutual exclusive with default */
1491 str = "ll {config false;default \"xxx\"; default \"yyy\";description test;if-feature f;"
1492 "max-elements 10;must 'expr';ordered-by user;reference test;"
1493 "status current;type string; units zzz;when true;m:ext;} ...";
1494 assert_int_equal(LY_SUCCESS, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1495 assert_non_null(ll);
1496 assert_int_equal(LYS_LEAFLIST, ll->nodetype);
1497 assert_string_equal("ll", ll->name);
1498 assert_string_equal("test", ll->dsc);
1499 assert_non_null(ll->dflts);
1500 assert_int_equal(2, LY_ARRAY_SIZE(ll->dflts));
1501 assert_string_equal("xxx", ll->dflts[0]);
1502 assert_string_equal("yyy", ll->dflts[1]);
1503 assert_string_equal("zzz", ll->units);
1504 assert_int_equal(10, ll->max);
1505 assert_int_equal(0, ll->min);
1506 assert_string_equal("string", ll->type.name);
1507 assert_non_null(ll->exts);
1508 assert_non_null(ll->iffeatures);
1509 assert_non_null(ll->musts);
1510 assert_string_equal("test", ll->ref);
1511 assert_non_null(ll->when);
1512 assert_null(ll->parent);
1513 assert_null(ll->next);
1514 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_USER | LYS_SET_MAX, ll->flags);
1515 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1516
1517 /* full content - now with min-elements */
1518 str = "ll {min-elements 10; type string;} ...";
1519 assert_int_equal(LY_SUCCESS, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1520 assert_non_null(ll);
1521 assert_int_equal(LYS_LEAFLIST, ll->nodetype);
1522 assert_string_equal("ll", ll->name);
1523 assert_string_equal("string", ll->type.name);
1524 assert_int_equal(0, ll->max);
1525 assert_int_equal(10, ll->min);
1526 assert_int_equal(LYS_SET_MIN, ll->flags);
1527 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1528
1529 /* invalid */
1530 str = " ll {min-elements 1; default xx; type string;} ...";
1531 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
Radek Krejcia9026eb2018-12-12 16:04:47 +01001532 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 +01001533 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1534
1535 str = " ll {description \"missing type\";} ...";
1536 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1537 logbuf_assert("Missing mandatory keyword \"type\" as a child of \"leaf-list\". Line number 1.");
1538 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1539
Radek Krejcidf6cad12018-11-28 17:10:55 +01001540 str = " ll {type string; min-elements 10; max-elements 1;} ..."; /* invalid combination of min/max */
1541 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1542 logbuf_assert("Invalid combination of min-elements and max-elements: min value 10 is bigger than the max value 1. Line number 1.");
1543 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1544
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001545 ctx.mod_version = 1; /* simulate YANG 1.0 - default statement is not allowed */
Radek Krejci0e5d8382018-11-28 16:37:53 +01001546 str = " ll {default xx; type string;} ...";
1547 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1548 logbuf_assert("Invalid keyword \"default\" as a child of \"leaf-list\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
1549 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1550
1551 *state = NULL;
1552 ly_ctx_destroy(ctx.ctx, NULL);
1553}
1554
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001555static void
1556test_list(void **state)
1557{
1558 *state = test_list;
1559
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001560 struct ly_parser_ctx ctx = {0};
1561 struct lysp_node_list *l = NULL;
1562 const char *str;
1563
1564 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1565 assert_non_null(ctx.ctx);
1566 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001567 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001568
1569 /* invalid cardinality */
1570#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1571 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1572 assert_int_equal(LY_EVALID, parse_list(&ctx, &str, NULL, (struct lysp_node**)&l)); \
1573 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1574 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1575
1576 TEST_DUP("config", "true", "false");
1577 TEST_DUP("description", "text1", "text2");
1578 TEST_DUP("key", "one", "two");
1579 TEST_DUP("max-elements", "10", "20");
1580 TEST_DUP("min-elements", "10", "20");
1581 TEST_DUP("ordered-by", "user", "system");
1582 TEST_DUP("reference", "1", "2");
1583 TEST_DUP("status", "current", "obsolete");
1584 TEST_DUP("when", "true", "false");
1585#undef TEST_DUP
1586
1587 /* full content */
1588 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;}"
1589 "leaf-list ll {type string;} list li;max-elements 10; min-elements 1;must 'expr';notification not; ordered-by system; reference test;"
1590 "status current;typedef t {type int8;}unique xxx;unique yyy;uses g;when true;m:ext;} ...";
1591 assert_int_equal(LY_SUCCESS, parse_list(&ctx, &str, NULL, (struct lysp_node**)&l));
1592 assert_non_null(l);
1593 assert_int_equal(LYS_LIST, l->nodetype);
1594 assert_string_equal("l", l->name);
1595 assert_string_equal("test", l->dsc);
1596 assert_string_equal("l", l->key);
1597 assert_non_null(l->uniques);
1598 assert_int_equal(2, LY_ARRAY_SIZE(l->uniques));
1599 assert_string_equal("xxx", l->uniques[0]);
1600 assert_string_equal("yyy", l->uniques[1]);
1601 assert_int_equal(10, l->max);
1602 assert_int_equal(1, l->min);
1603 assert_non_null(l->exts);
1604 assert_non_null(l->iffeatures);
1605 assert_non_null(l->musts);
1606 assert_string_equal("test", l->ref);
1607 assert_non_null(l->when);
1608 assert_null(l->parent);
1609 assert_null(l->next);
1610 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM | LYS_SET_MAX | LYS_SET_MIN, l->flags);
1611 ly_set_erase(&ctx.tpdfs_nodes, NULL);
1612 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1613
Radek Krejcif538ce52019-03-05 10:46:14 +01001614 /* invalid content */
1615 ctx.mod_version = 1; /* simulate YANG 1.0 */
1616 str = "l {action x;} ...";
1617 assert_int_equal(LY_EVALID, parse_list(&ctx, &str, NULL, (struct lysp_node**)&l));
1618 logbuf_assert("Invalid keyword \"action\" as a child of \"list\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
1619 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1620
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001621 *state = NULL;
1622 ly_ctx_destroy(ctx.ctx, NULL);
1623}
1624
Radek Krejci056d0a82018-12-06 16:57:25 +01001625static void
1626test_choice(void **state)
1627{
1628 *state = test_choice;
1629
Radek Krejci056d0a82018-12-06 16:57:25 +01001630 struct ly_parser_ctx ctx = {0};
1631 struct lysp_node_choice *ch = NULL;
1632 const char *str;
1633
1634 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1635 assert_non_null(ctx.ctx);
1636 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001637 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci056d0a82018-12-06 16:57:25 +01001638
1639 /* invalid cardinality */
1640#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1641 str = "ch {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1642 assert_int_equal(LY_EVALID, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch)); \
1643 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1644 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1645
1646 TEST_DUP("config", "true", "false");
1647 TEST_DUP("default", "a", "b");
1648 TEST_DUP("description", "text1", "text2");
1649 TEST_DUP("mandatory", "true", "false");
1650 TEST_DUP("reference", "1", "2");
1651 TEST_DUP("status", "current", "obsolete");
1652 TEST_DUP("when", "true", "false");
1653#undef TEST_DUP
1654
Radek Krejcia9026eb2018-12-12 16:04:47 +01001655 /* full content - without default due to a collision with mandatory */
1656 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 +01001657 "leaf-list ll {type string;} list li;mandatory true;reference test;status current;when true;m:ext;} ...";
1658 assert_int_equal(LY_SUCCESS, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch));
1659 assert_non_null(ch);
1660 assert_int_equal(LYS_CHOICE, ch->nodetype);
1661 assert_string_equal("ch", ch->name);
1662 assert_string_equal("test", ch->dsc);
1663 assert_non_null(ch->exts);
1664 assert_non_null(ch->iffeatures);
1665 assert_string_equal("test", ch->ref);
1666 assert_non_null(ch->when);
1667 assert_null(ch->parent);
1668 assert_null(ch->next);
1669 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_MAND_TRUE, ch->flags);
1670 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1671
Radek Krejcia9026eb2018-12-12 16:04:47 +01001672 /* full content - the default missing from the previous node */
1673 str = "ch {default c;case c;} ...";
1674 assert_int_equal(LY_SUCCESS, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch));
1675 assert_non_null(ch);
1676 assert_int_equal(LYS_CHOICE, ch->nodetype);
1677 assert_string_equal("ch", ch->name);
1678 assert_string_equal("c", ch->dflt);
1679 assert_int_equal(0, ch->flags);
1680 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1681
1682 /* invalid content */
1683 str = "ch {mandatory true; default c1; case c1 {leaf x{type string;}}} ...";
1684 assert_int_equal(LY_EVALID, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch));
1685 logbuf_assert("Invalid combination of keywords \"mandatory\" and \"default\" as substatements of \"choice\". Line number 1.");
1686 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1687
1688 *state = NULL;
1689 ly_ctx_destroy(ctx.ctx, NULL);
1690}
1691
1692static void
1693test_case(void **state)
1694{
1695 *state = test_case;
1696
Radek Krejcia9026eb2018-12-12 16:04:47 +01001697 struct ly_parser_ctx ctx = {0};
1698 struct lysp_node_case *cs = NULL;
1699 const char *str;
1700
1701 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1702 assert_non_null(ctx.ctx);
1703 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001704 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejcia9026eb2018-12-12 16:04:47 +01001705
1706 /* invalid cardinality */
1707#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1708 str = "cs {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1709 assert_int_equal(LY_EVALID, parse_case(&ctx, &str, NULL, (struct lysp_node**)&cs)); \
1710 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1711 lysp_node_free(ctx.ctx, (struct lysp_node*)cs); cs = NULL;
1712
1713 TEST_DUP("description", "text1", "text2");
1714 TEST_DUP("reference", "1", "2");
1715 TEST_DUP("status", "current", "obsolete");
1716 TEST_DUP("when", "true", "false");
1717#undef TEST_DUP
1718
1719 /* full content */
1720 str = "cs {anydata any;anyxml anyxml; choice ch;container c;description test;if-feature f;leaf l {type string;}"
1721 "leaf-list ll {type string;} list li;reference test;status current;uses grp;when true;m:ext;} ...";
1722 assert_int_equal(LY_SUCCESS, parse_case(&ctx, &str, NULL, (struct lysp_node**)&cs));
1723 assert_non_null(cs);
1724 assert_int_equal(LYS_CASE, cs->nodetype);
1725 assert_string_equal("cs", cs->name);
1726 assert_string_equal("test", cs->dsc);
1727 assert_non_null(cs->exts);
1728 assert_non_null(cs->iffeatures);
1729 assert_string_equal("test", cs->ref);
1730 assert_non_null(cs->when);
1731 assert_null(cs->parent);
1732 assert_null(cs->next);
1733 assert_int_equal(LYS_STATUS_CURR, cs->flags);
1734 lysp_node_free(ctx.ctx, (struct lysp_node*)cs); cs = NULL;
1735
1736 /* invalid content */
1737 str = "cs {config true} ...";
1738 assert_int_equal(LY_EVALID, parse_case(&ctx, &str, NULL, (struct lysp_node**)&cs));
1739 logbuf_assert("Invalid keyword \"config\" as a child of \"case\". Line number 1.");
1740 lysp_node_free(ctx.ctx, (struct lysp_node*)cs); cs = NULL;
1741
Radek Krejci056d0a82018-12-06 16:57:25 +01001742 *state = NULL;
1743 ly_ctx_destroy(ctx.ctx, NULL);
1744}
1745
Radek Krejci9800fb82018-12-13 14:26:23 +01001746static void
1747test_any(void **state, enum yang_keyword kw)
1748{
1749 *state = test_any;
1750
Radek Krejci9800fb82018-12-13 14:26:23 +01001751 struct ly_parser_ctx ctx = {0};
1752 struct lysp_node_anydata *any = NULL;
1753 const char *str;
1754
1755 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1756 assert_non_null(ctx.ctx);
1757 ctx.line = 1;
Radek Krejci9800fb82018-12-13 14:26:23 +01001758 if (kw == YANG_ANYDATA) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001759 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci9800fb82018-12-13 14:26:23 +01001760 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001761 ctx.mod_version = 1; /* simulate YANG 1.0 */
Radek Krejci9800fb82018-12-13 14:26:23 +01001762 }
1763
1764 /* invalid cardinality */
1765#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1766 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1767 assert_int_equal(LY_EVALID, parse_any(&ctx, &str, kw, NULL, (struct lysp_node**)&any)); \
1768 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1769 lysp_node_free(ctx.ctx, (struct lysp_node*)any); any = NULL;
1770
1771 TEST_DUP("config", "true", "false");
1772 TEST_DUP("description", "text1", "text2");
1773 TEST_DUP("mandatory", "true", "false");
1774 TEST_DUP("reference", "1", "2");
1775 TEST_DUP("status", "current", "obsolete");
1776 TEST_DUP("when", "true", "false");
1777#undef TEST_DUP
1778
1779 /* full content */
1780 str = "any {config true;description test;if-feature f;mandatory true;must 'expr';reference test;status current;when true;m:ext;} ...";
1781 assert_int_equal(LY_SUCCESS, parse_any(&ctx, &str, kw, NULL, (struct lysp_node**)&any));
1782 assert_non_null(any);
1783 assert_int_equal(kw == YANG_ANYDATA ? LYS_ANYDATA : LYS_ANYXML, any->nodetype);
1784 assert_string_equal("any", any->name);
1785 assert_string_equal("test", any->dsc);
1786 assert_non_null(any->exts);
1787 assert_non_null(any->iffeatures);
1788 assert_non_null(any->musts);
1789 assert_string_equal("test", any->ref);
1790 assert_non_null(any->when);
1791 assert_null(any->parent);
1792 assert_null(any->next);
1793 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR | LYS_MAND_TRUE, any->flags);
1794 lysp_node_free(ctx.ctx, (struct lysp_node*)any); any = NULL;
1795
1796 *state = NULL;
1797 ly_ctx_destroy(ctx.ctx, NULL);
1798}
1799
1800static void
1801test_anydata(void **state)
1802{
1803 return test_any(state, YANG_ANYDATA);
1804}
1805
1806static void
1807test_anyxml(void **state)
1808{
1809 return test_any(state, YANG_ANYXML);
1810}
1811
Radek Krejcie86bf772018-12-14 11:39:53 +01001812static void
1813test_grouping(void **state)
1814{
1815 *state = test_grouping;
1816
Radek Krejcie86bf772018-12-14 11:39:53 +01001817 struct ly_parser_ctx ctx = {0};
1818 struct lysp_grp *grp = NULL;
1819 const char *str;
1820
1821 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1822 assert_non_null(ctx.ctx);
1823 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001824 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejcie86bf772018-12-14 11:39:53 +01001825
1826 /* invalid cardinality */
1827#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1828 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1829 assert_int_equal(LY_EVALID, parse_grouping(&ctx, &str, NULL, &grp)); \
1830 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1831 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1832
1833 TEST_DUP("description", "text1", "text2");
1834 TEST_DUP("reference", "1", "2");
1835 TEST_DUP("status", "current", "obsolete");
1836#undef TEST_DUP
1837
1838 /* full content */
1839 str = "grp {action x;anydata any;anyxml anyxml; choice ch;container c;description test;grouping g;leaf l {type string;}"
1840 "leaf-list ll {type string;} list li;notification not;reference test;status current;typedef t {type int8;}uses g;m:ext;} ...";
1841 assert_int_equal(LY_SUCCESS, parse_grouping(&ctx, &str, NULL, &grp));
1842 assert_non_null(grp);
1843 assert_int_equal(LYS_GROUPING, grp->nodetype);
1844 assert_string_equal("grp", grp->name);
1845 assert_string_equal("test", grp->dsc);
1846 assert_non_null(grp->exts);
1847 assert_string_equal("test", grp->ref);
1848 assert_null(grp->parent);
1849 assert_int_equal( LYS_STATUS_CURR, grp->flags);
1850 ly_set_erase(&ctx.tpdfs_nodes, NULL);
1851 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1852
1853 /* invalid content */
1854 str = "grp {config true} ...";
1855 assert_int_equal(LY_EVALID, parse_grouping(&ctx, &str, NULL, &grp));
1856 logbuf_assert("Invalid keyword \"config\" as a child of \"grouping\". Line number 1.");
1857 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1858
1859 str = "grp {must 'expr'} ...";
1860 assert_int_equal(LY_EVALID, parse_grouping(&ctx, &str, NULL, &grp));
1861 logbuf_assert("Invalid keyword \"must\" as a child of \"grouping\". Line number 1.");
1862 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1863
1864 *state = NULL;
1865 ly_ctx_destroy(ctx.ctx, NULL);
1866}
1867
1868static void
Radek Krejcif538ce52019-03-05 10:46:14 +01001869test_action(void **state)
1870{
1871 *state = test_action;
1872
1873 struct ly_parser_ctx ctx = {0};
1874 struct lysp_action *rpcs = NULL;
1875 struct lysp_node_container *c = NULL;
1876 const char *str;
1877
1878 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1879 assert_non_null(ctx.ctx);
1880 ctx.line = 1;
1881 ctx.mod_version = 2; /* simulate YANG 1.1 */
1882
1883 /* invalid cardinality */
1884#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1885 str = "func {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1886 assert_int_equal(LY_EVALID, parse_action(&ctx, &str, NULL, &rpcs)); \
1887 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1888 FREE_ARRAY(ctx.ctx, rpcs, lysp_action_free); rpcs = NULL;
1889
1890 TEST_DUP("description", "text1", "text2");
1891 TEST_DUP("input", "", "");
1892 TEST_DUP("output", "", "");
1893 TEST_DUP("reference", "1", "2");
1894 TEST_DUP("status", "current", "obsolete");
1895#undef TEST_DUP
1896
1897 /* full content */
1898 str = "top;";
1899 assert_int_equal(LY_SUCCESS, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1900 str = "func {description test;grouping grp;if-feature f;reference test;status current;typedef mytype {type int8;} m:ext;"
1901 "input {anydata a1; anyxml a2; choice ch; container c; grouping grp; leaf l {type int8;} leaf-list ll {type int8;}"
1902 " list li; must 1; typedef mytypei {type int8;} uses grp; m:ext;}"
1903 "output {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 mytypeo {type int8;} uses grp; m:ext;}} ...";
1905 assert_int_equal(LY_SUCCESS, parse_action(&ctx, &str, (struct lysp_node*)c, &rpcs));
1906 assert_non_null(rpcs);
1907 assert_int_equal(LYS_ACTION, rpcs->nodetype);
1908 assert_string_equal("func", rpcs->name);
1909 assert_string_equal("test", rpcs->dsc);
1910 assert_non_null(rpcs->exts);
1911 assert_non_null(rpcs->iffeatures);
1912 assert_string_equal("test", rpcs->ref);
1913 assert_non_null(rpcs->groupings);
1914 assert_non_null(rpcs->typedefs);
1915 assert_int_equal(LYS_STATUS_CURR, rpcs->flags);
1916 /* input */
1917 assert_int_equal(rpcs->input.nodetype, LYS_INOUT);
1918 assert_non_null(rpcs->input.groupings);
1919 assert_non_null(rpcs->input.exts);
1920 assert_non_null(rpcs->input.musts);
1921 assert_non_null(rpcs->input.typedefs);
1922 assert_non_null(rpcs->input.data);
1923 /* output */
1924 assert_int_equal(rpcs->output.nodetype, LYS_INOUT);
1925 assert_non_null(rpcs->output.groupings);
1926 assert_non_null(rpcs->output.exts);
1927 assert_non_null(rpcs->output.musts);
1928 assert_non_null(rpcs->output.typedefs);
1929 assert_non_null(rpcs->output.data);
1930
1931 ly_set_erase(&ctx.tpdfs_nodes, NULL);
1932 FREE_ARRAY(ctx.ctx, rpcs, lysp_action_free); rpcs = NULL;
1933
1934 /* invalid content */
1935 str = "func {config true} ...";
1936 assert_int_equal(LY_EVALID, parse_action(&ctx, &str, NULL, &rpcs));
1937 logbuf_assert("Invalid keyword \"config\" as a child of \"rpc\". Line number 1.");
1938 FREE_ARRAY(ctx.ctx, rpcs, lysp_action_free); rpcs = NULL;
1939
1940 *state = NULL;
1941 lysp_node_free(ctx.ctx, (struct lysp_node*)c);
1942 ly_ctx_destroy(ctx.ctx, NULL);
1943}
1944
1945static void
Radek Krejcie86bf772018-12-14 11:39:53 +01001946test_uses(void **state)
1947{
1948 *state = test_uses;
1949
Radek Krejcie86bf772018-12-14 11:39:53 +01001950 struct ly_parser_ctx ctx = {0};
1951 struct lysp_node_uses *u = NULL;
1952 const char *str;
1953
1954 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1955 assert_non_null(ctx.ctx);
1956 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001957 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejcie86bf772018-12-14 11:39:53 +01001958
1959 /* invalid cardinality */
1960#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1961 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1962 assert_int_equal(LY_EVALID, parse_uses(&ctx, &str, NULL, (struct lysp_node**)&u)); \
1963 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1964 lysp_node_free(ctx.ctx, (struct lysp_node*)u); u = NULL;
1965
1966 TEST_DUP("description", "text1", "text2");
1967 TEST_DUP("reference", "1", "2");
1968 TEST_DUP("status", "current", "obsolete");
1969 TEST_DUP("when", "true", "false");
1970#undef TEST_DUP
1971
1972 /* full content */
1973 str = "grpref {augment some/node;description test;if-feature f;reference test;refine some/other/node;status current;when true;m:ext;} ...";
1974 assert_int_equal(LY_SUCCESS, parse_uses(&ctx, &str, NULL, (struct lysp_node**)&u));
1975 assert_non_null(u);
1976 assert_int_equal(LYS_USES, u->nodetype);
1977 assert_string_equal("grpref", u->name);
1978 assert_string_equal("test", u->dsc);
1979 assert_non_null(u->exts);
1980 assert_non_null(u->iffeatures);
1981 assert_string_equal("test", u->ref);
1982 assert_non_null(u->augments);
1983 assert_non_null(u->refines);
1984 assert_non_null(u->when);
1985 assert_null(u->parent);
1986 assert_null(u->next);
1987 assert_int_equal(LYS_STATUS_CURR, u->flags);
1988 lysp_node_free(ctx.ctx, (struct lysp_node*)u); u = NULL;
1989
1990 *state = NULL;
1991 ly_ctx_destroy(ctx.ctx, NULL);
1992}
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001993
1994
1995static void
1996test_augment(void **state)
1997{
1998 *state = test_augment;
1999
2000 struct ly_parser_ctx ctx = {0};
2001 struct lysp_augment *a = NULL;
2002 const char *str;
2003
2004 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
2005 assert_non_null(ctx.ctx);
2006 ctx.line = 1;
Radek Krejcib4adbf12019-02-25 13:35:22 +01002007 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002008
2009 /* invalid cardinality */
2010#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
2011 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
2012 assert_int_equal(LY_EVALID, parse_augment(&ctx, &str, NULL, &a)); \
2013 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
Radek Krejcib4adbf12019-02-25 13:35:22 +01002014 FREE_ARRAY(ctx.ctx, a, lysp_augment_free); a = NULL;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002015
2016 TEST_DUP("description", "text1", "text2");
2017 TEST_DUP("reference", "1", "2");
2018 TEST_DUP("status", "current", "obsolete");
2019 TEST_DUP("when", "true", "false");
2020#undef TEST_DUP
2021
2022 /* full content */
2023 str = "/target/nodeid {action x; anydata any;anyxml anyxml; case cs; choice ch;container c;description test;if-feature f;leaf l {type string;}"
2024 "leaf-list ll {type string;} list li;notification not;reference test;status current;uses g;when true;m:ext;} ...";
2025 assert_int_equal(LY_SUCCESS, parse_augment(&ctx, &str, NULL, &a));
2026 assert_non_null(a);
2027 assert_int_equal(LYS_AUGMENT, a->nodetype);
2028 assert_string_equal("/target/nodeid", a->nodeid);
2029 assert_string_equal("test", a->dsc);
2030 assert_non_null(a->exts);
2031 assert_non_null(a->iffeatures);
2032 assert_string_equal("test", a->ref);
2033 assert_non_null(a->when);
2034 assert_null(a->parent);
2035 assert_int_equal(LYS_STATUS_CURR, a->flags);
Radek Krejcib4adbf12019-02-25 13:35:22 +01002036 FREE_ARRAY(ctx.ctx, a, lysp_augment_free); a = NULL;
Radek Krejci5a7c4a52019-02-12 15:45:11 +01002037
2038 *state = NULL;
2039 ly_ctx_destroy(ctx.ctx, NULL);
2040}
2041
Radek Krejci80dd33e2018-09-26 15:57:18 +02002042int main(void)
2043{
2044 const struct CMUnitTest tests[] = {
Radek Krejci44ceedc2018-10-02 15:54:31 +02002045 cmocka_unit_test_setup(test_helpers, logger_setup),
Radek Krejci80dd33e2018-09-26 15:57:18 +02002046 cmocka_unit_test_setup(test_comments, logger_setup),
Radek Krejciefd22f62018-09-27 11:47:58 +02002047 cmocka_unit_test_setup(test_arg, logger_setup),
Radek Krejcidcc7b322018-10-11 14:24:02 +02002048 cmocka_unit_test_setup(test_stmts, logger_setup),
Radek Krejci05b13982018-11-28 16:22:07 +01002049 cmocka_unit_test_setup_teardown(test_minmax, logger_setup, logger_teardown),
Radek Krejci40544fa2019-01-11 09:38:37 +01002050 cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown),
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002051 cmocka_unit_test_setup_teardown(test_identity, logger_setup, logger_teardown),
Radek Krejci2c02f3e2018-10-16 10:54:38 +02002052 cmocka_unit_test_setup(test_feature, logger_setup),
2053 cmocka_unit_test_setup(test_deviation, logger_setup),
2054 cmocka_unit_test_setup(test_deviate, logger_setup),
Radek Krejci8c370832018-11-02 15:10:03 +01002055 cmocka_unit_test_setup(test_container, logger_setup),
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01002056 cmocka_unit_test_setup_teardown(test_leaf, logger_setup, logger_teardown),
Radek Krejci0e5d8382018-11-28 16:37:53 +01002057 cmocka_unit_test_setup_teardown(test_leaflist, logger_setup, logger_teardown),
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002058 cmocka_unit_test_setup_teardown(test_list, logger_setup, logger_teardown),
Radek Krejci056d0a82018-12-06 16:57:25 +01002059 cmocka_unit_test_setup_teardown(test_choice, logger_setup, logger_teardown),
Radek Krejcia9026eb2018-12-12 16:04:47 +01002060 cmocka_unit_test_setup_teardown(test_case, logger_setup, logger_teardown),
Radek Krejci9800fb82018-12-13 14:26:23 +01002061 cmocka_unit_test_setup_teardown(test_anydata, logger_setup, logger_teardown),
2062 cmocka_unit_test_setup_teardown(test_anyxml, logger_setup, logger_teardown),
Radek Krejcif538ce52019-03-05 10:46:14 +01002063 cmocka_unit_test_setup_teardown(test_action, logger_setup, logger_teardown),
Radek Krejcie86bf772018-12-14 11:39:53 +01002064 cmocka_unit_test_setup_teardown(test_grouping, logger_setup, logger_teardown),
2065 cmocka_unit_test_setup_teardown(test_uses, logger_setup, logger_teardown),
Radek Krejcib4adbf12019-02-25 13:35:22 +01002066 cmocka_unit_test_setup_teardown(test_augment, logger_setup, logger_teardown),
Radek Krejci80dd33e2018-09-26 15:57:18 +02002067 };
2068
2069 return cmocka_run_group_tests(tests, NULL, NULL);
2070}