blob: 3a7dea01c0f81dde0d0999485c0742a4092d01e7 [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 Krejci0bcdaed2019-01-10 10:21:34 +01001104 //TEST_DUP("description", "a", "b");
1105 str = " test {description a;description b;} ...";
1106 assert_int_equal(LY_EVALID, parse_identity(&ctx, &str, &ident));
1107 logbuf_assert("Duplicate keyword \"description\". Line number 1.");
1108 FREE_ARRAY(ctx.ctx, ident, lysp_ident_free); ident = NULL;
1109
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001110 TEST_DUP("reference", "a", "b");
1111 TEST_DUP("status", "current", "obsolete");
1112
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001113 /* full content */
1114 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 +02001115 assert_int_equal(LY_SUCCESS, parse_identity(&ctx, &str, &ident));
1116 assert_non_null(ident);
1117 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001118 FREE_ARRAY(ctx.ctx, ident, lysp_ident_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001119 ident = NULL;
1120
1121 /* invalid substatement */
1122 str = " test {organization XXX;}";
1123 assert_int_equal(LY_EVALID, parse_identity(&ctx, &str, &ident));
1124 logbuf_assert("Invalid keyword \"organization\" as a child of \"identity\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001125 FREE_ARRAY(ctx.ctx, ident, lysp_ident_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001126 ident = NULL;
1127
1128#undef TEST_DUP
1129
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001130 *state = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001131 ly_ctx_destroy(ctx.ctx, NULL);
1132}
1133
1134static void
1135test_feature(void **state)
1136{
1137 (void) state; /* unused */
1138
1139 struct ly_parser_ctx ctx;
1140 struct lysp_feature *features = NULL;
1141 const char *str;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001142
1143 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1144 assert_non_null(ctx.ctx);
1145 ctx.line = 1;
1146 ctx.indent = 0;
1147
1148 /* invalid cardinality */
1149#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1150 TEST_DUP_GENERIC(" test {", MEMBER, VALUE1, VALUE2, parse_feature, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001151 &features, "1", FREE_ARRAY(ctx.ctx, features, lysp_feature_free); features = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001152
1153 TEST_DUP("description", "a", "b");
1154 TEST_DUP("reference", "a", "b");
1155 TEST_DUP("status", "current", "obsolete");
1156
1157 /* full content */
1158 str = " test {description text;reference \'another text\';status current; if-feature x;if-feature y;prefix:ext;} ...";
1159 assert_int_equal(LY_SUCCESS, parse_feature(&ctx, &str, &features));
1160 assert_non_null(features);
1161 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001162 FREE_ARRAY(ctx.ctx, features, lysp_feature_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001163 features = NULL;
1164
1165 /* invalid substatement */
1166 str = " test {organization XXX;}";
1167 assert_int_equal(LY_EVALID, parse_feature(&ctx, &str, &features));
1168 logbuf_assert("Invalid keyword \"organization\" as a child of \"feature\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001169 FREE_ARRAY(ctx.ctx, features, lysp_feature_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001170 features = NULL;
1171
1172#undef TEST_DUP
1173
1174 ly_ctx_destroy(ctx.ctx, NULL);
1175}
1176
1177static void
1178test_deviation(void **state)
1179{
1180 (void) state; /* unused */
1181
1182 struct ly_parser_ctx ctx;
1183 struct lysp_deviation *d = NULL;
1184 const char *str;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001185
1186 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1187 assert_non_null(ctx.ctx);
1188 ctx.line = 1;
1189 ctx.indent = 0;
1190
1191 /* invalid cardinality */
1192#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1193 TEST_DUP_GENERIC(" test {deviate not-supported;", MEMBER, VALUE1, VALUE2, parse_deviation, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001194 &d, "1", FREE_ARRAY(ctx.ctx, d, lysp_deviation_free); d = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001195
1196 TEST_DUP("description", "a", "b");
1197 TEST_DUP("reference", "a", "b");
1198
1199 /* full content */
1200 str = " test {deviate not-supported;description text;reference \'another text\';prefix:ext;} ...";
1201 assert_int_equal(LY_SUCCESS, parse_deviation(&ctx, &str, &d));
1202 assert_non_null(d);
1203 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001204 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001205 d = NULL;
1206
1207 /* missing mandatory substatement */
1208 str = " test {description text;}";
1209 assert_int_equal(LY_EVALID, parse_deviation(&ctx, &str, &d));
1210 logbuf_assert("Missing mandatory keyword \"deviate\" as a child of \"deviation\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001211 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001212 d = NULL;
1213
1214 /* invalid substatement */
1215 str = " test {deviate not-supported; status obsolete;}";
1216 assert_int_equal(LY_EVALID, parse_deviation(&ctx, &str, &d));
1217 logbuf_assert("Invalid keyword \"status\" as a child of \"deviation\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001218 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001219 d = NULL;
1220
1221#undef TEST_DUP
1222
1223 ly_ctx_destroy(ctx.ctx, NULL);
1224}
1225
1226static void
1227test_deviate(void **state)
1228{
1229 (void) state; /* unused */
1230
1231 struct ly_parser_ctx ctx;
1232 struct lysp_deviate *d = NULL;
1233 const char *str;
1234
1235 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1236 assert_non_null(ctx.ctx);
1237 ctx.line = 1;
1238 ctx.indent = 0;
1239
1240 /* invalid cardinality */
1241#define TEST_DUP(TYPE, MEMBER, VALUE1, VALUE2) \
1242 TEST_DUP_GENERIC(TYPE" {", MEMBER, VALUE1, VALUE2, parse_deviate, \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001243 &d, "1", lysp_deviate_free(ctx.ctx, d); free(d); d = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001244
1245 TEST_DUP("add", "config", "true", "false");
1246 TEST_DUP("replace", "default", "int8", "uint8");
1247 TEST_DUP("add", "mandatory", "true", "false");
1248 TEST_DUP("add", "max-elements", "1", "2");
1249 TEST_DUP("add", "min-elements", "1", "2");
1250 TEST_DUP("replace", "type", "int8", "uint8");
1251 TEST_DUP("add", "units", "kilometers", "miles");
1252
1253 /* full contents */
1254 str = " not-supported {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 = " 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;} ...";
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 = " delete {units meters; must 1; must 2; unique x; unique y; default a; default b; 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 str = " replace {type string; units meters; default a; config true; mandatory true; min-elements 1; max-elements 2; prefix:ext;} ...";
1270 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1271 assert_non_null(d);
1272 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001273 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001274
1275 /* invalid substatements */
1276#define TEST_NOT_SUP(DEV, STMT, VALUE) \
1277 str = " "DEV" {"STMT" "VALUE";}..."; \
1278 assert_int_equal(LY_EVALID, parse_deviate(&ctx, &str, &d)); \
1279 logbuf_assert("Deviate \""DEV"\" does not support keyword \""STMT"\". Line number 1."); \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001280 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001281
1282 TEST_NOT_SUP("not-supported", "units", "meters");
1283 TEST_NOT_SUP("not-supported", "must", "1");
1284 TEST_NOT_SUP("not-supported", "unique", "x");
1285 TEST_NOT_SUP("not-supported", "default", "a");
1286 TEST_NOT_SUP("not-supported", "config", "true");
1287 TEST_NOT_SUP("not-supported", "mandatory", "true");
1288 TEST_NOT_SUP("not-supported", "min-elements", "1");
1289 TEST_NOT_SUP("not-supported", "max-elements", "2");
1290 TEST_NOT_SUP("not-supported", "type", "string");
1291 TEST_NOT_SUP("add", "type", "string");
1292 TEST_NOT_SUP("delete", "config", "true");
1293 TEST_NOT_SUP("delete", "mandatory", "true");
1294 TEST_NOT_SUP("delete", "min-elements", "1");
1295 TEST_NOT_SUP("delete", "max-elements", "2");
1296 TEST_NOT_SUP("delete", "type", "string");
1297 TEST_NOT_SUP("replace", "must", "1");
1298 TEST_NOT_SUP("replace", "unique", "a");
1299
1300 str = " nonsence; ...";
1301 assert_int_equal(LY_EVALID, parse_deviate(&ctx, &str, &d));
1302 logbuf_assert("Invalid value \"nonsence\" of \"deviate\". Line number 1.");
1303 assert_null(d);
1304
1305#undef TEST_NOT_SUP
1306#undef TEST_DUP
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001307
1308 ly_ctx_destroy(ctx.ctx, NULL);
1309}
1310
Radek Krejci8c370832018-11-02 15:10:03 +01001311static void
1312test_container(void **state)
1313{
1314 (void) state; /* unused */
1315
Radek Krejci313d9902018-11-08 09:42:58 +01001316 struct ly_parser_ctx ctx = {0};
Radek Krejci8c370832018-11-02 15:10:03 +01001317 struct lysp_node_container *c = NULL;
1318 const char *str;
1319
1320 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1321 assert_non_null(ctx.ctx);
1322 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001323 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci8c370832018-11-02 15:10:03 +01001324
1325 /* invalid cardinality */
1326#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1327 str = "cont {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1328 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c)); \
1329 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001330 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001331
1332 TEST_DUP("config", "true", "false");
1333 TEST_DUP("description", "text1", "text2");
1334 TEST_DUP("presence", "true", "false");
1335 TEST_DUP("reference", "1", "2");
1336 TEST_DUP("status", "current", "obsolete");
1337 TEST_DUP("when", "true", "false");
1338#undef TEST_DUP
1339
1340 /* full content */
Radek Krejci313d9902018-11-08 09:42:58 +01001341 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;}"
1342 "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 +01001343 assert_int_equal(LY_SUCCESS, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1344 assert_non_null(c);
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001345 assert_int_equal(LYS_CONTAINER, c->nodetype);
1346 assert_string_equal("cont", c->name);
Radek Krejci8c370832018-11-02 15:10:03 +01001347 assert_non_null(c->actions);
1348 assert_non_null(c->child);
1349 assert_string_equal("test", c->dsc);
1350 assert_non_null(c->exts);
1351 assert_non_null(c->groupings);
1352 assert_non_null(c->iffeatures);
1353 assert_non_null(c->musts);
1354 assert_non_null(c->notifs);
1355 assert_string_equal("true", c->presence);
1356 assert_string_equal("test", c->ref);
1357 assert_non_null(c->typedefs);
1358 assert_non_null(c->when);
1359 assert_null(c->parent);
1360 assert_null(c->next);
1361 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR, c->flags);
Radek Krejci313d9902018-11-08 09:42:58 +01001362 ly_set_erase(&ctx.tpdfs_nodes, NULL);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001363 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001364
1365 /* invalid */
1366 str = " cont {augment /root;} ...";
1367 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1368 logbuf_assert("Invalid keyword \"augment\" as a child of \"container\". Line number 1.");
Radek Krejci4f28eda2018-11-12 11:46:16 +01001369 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001370 str = " cont {nonsence true;} ...";
1371 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1372 logbuf_assert("Invalid character sequence \"nonsence\", expected a keyword. Line number 1.");
Radek Krejci4f28eda2018-11-12 11:46:16 +01001373 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001374
1375 ly_ctx_destroy(ctx.ctx, NULL);
1376}
1377
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001378static void
1379test_leaf(void **state)
1380{
1381 *state = test_leaf;
1382
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001383 struct ly_parser_ctx ctx = {0};
1384 struct lysp_node_leaf *l = NULL;
1385 const char *str;
1386
1387 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1388 assert_non_null(ctx.ctx);
1389 ctx.line = 1;
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001390 //ctx.mod->version = 2; /* simulate YANG 1.1 */
1391
1392 /* invalid cardinality */
1393#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1394 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1395 assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l)); \
1396 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1397 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1398
1399 TEST_DUP("config", "true", "false");
1400 TEST_DUP("default", "x", "y");
1401 TEST_DUP("description", "text1", "text2");
1402 TEST_DUP("mandatory", "true", "false");
1403 TEST_DUP("reference", "1", "2");
1404 TEST_DUP("status", "current", "obsolete");
Radek Krejci0e5d8382018-11-28 16:37:53 +01001405 TEST_DUP("type", "int8", "uint8");
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001406 TEST_DUP("units", "text1", "text2");
1407 TEST_DUP("when", "true", "false");
1408#undef TEST_DUP
1409
1410 /* full content - without mandatory which is mutual exclusive with default */
1411 str = "l {config false;default \"xxx\";description test;if-feature f;"
1412 "must 'expr';reference test;status current;type string; units yyy;when true;m:ext;} ...";
1413 assert_int_equal(LY_SUCCESS, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
1414 assert_non_null(l);
1415 assert_int_equal(LYS_LEAF, l->nodetype);
1416 assert_string_equal("l", l->name);
1417 assert_string_equal("test", l->dsc);
1418 assert_string_equal("xxx", l->dflt);
1419 assert_string_equal("yyy", l->units);
1420 assert_string_equal("string", l->type.name);
1421 assert_non_null(l->exts);
1422 assert_non_null(l->iffeatures);
1423 assert_non_null(l->musts);
1424 assert_string_equal("test", l->ref);
1425 assert_non_null(l->when);
1426 assert_null(l->parent);
1427 assert_null(l->next);
1428 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR, l->flags);
1429 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1430
1431 /* full content - now with mandatory */
1432 str = "l {mandatory true; type string;} ...";
1433 assert_int_equal(LY_SUCCESS, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
1434 assert_non_null(l);
1435 assert_int_equal(LYS_LEAF, l->nodetype);
1436 assert_string_equal("l", l->name);
1437 assert_string_equal("string", l->type.name);
1438 assert_int_equal(LYS_MAND_TRUE, l->flags);
1439 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1440
1441 /* invalid */
1442 str = " l {mandatory true; default xx; type string;} ...";
1443 assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
Radek Krejcia9026eb2018-12-12 16:04:47 +01001444 logbuf_assert("Invalid combination of keywords \"mandatory\" and \"default\" as substatements of \"leaf\". Line number 1.");
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001445 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1446
1447 str = " l {description \"missing type\";} ...";
1448 assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
1449 logbuf_assert("Missing mandatory keyword \"type\" as a child of \"leaf\". Line number 1.");
1450 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1451
1452 *state = NULL;
1453 ly_ctx_destroy(ctx.ctx, NULL);
1454}
1455
Radek Krejci0e5d8382018-11-28 16:37:53 +01001456static void
1457test_leaflist(void **state)
1458{
1459 *state = test_leaf;
1460
Radek Krejci0e5d8382018-11-28 16:37:53 +01001461 struct ly_parser_ctx ctx = {0};
1462 struct lysp_node_leaflist *ll = NULL;
1463 const char *str;
1464
1465 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1466 assert_non_null(ctx.ctx);
1467 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001468 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci0e5d8382018-11-28 16:37:53 +01001469
1470 /* invalid cardinality */
1471#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1472 str = "ll {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1473 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll)); \
1474 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1475 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1476
1477 TEST_DUP("config", "true", "false");
1478 TEST_DUP("description", "text1", "text2");
1479 TEST_DUP("max-elements", "10", "20");
1480 TEST_DUP("min-elements", "10", "20");
1481 TEST_DUP("ordered-by", "user", "system");
1482 TEST_DUP("reference", "1", "2");
1483 TEST_DUP("status", "current", "obsolete");
1484 TEST_DUP("type", "int8", "uint8");
1485 TEST_DUP("units", "text1", "text2");
1486 TEST_DUP("when", "true", "false");
1487#undef TEST_DUP
1488
1489 /* full content - without min-elements which is mutual exclusive with default */
1490 str = "ll {config false;default \"xxx\"; default \"yyy\";description test;if-feature f;"
1491 "max-elements 10;must 'expr';ordered-by user;reference test;"
1492 "status current;type string; units zzz;when true;m:ext;} ...";
1493 assert_int_equal(LY_SUCCESS, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1494 assert_non_null(ll);
1495 assert_int_equal(LYS_LEAFLIST, ll->nodetype);
1496 assert_string_equal("ll", ll->name);
1497 assert_string_equal("test", ll->dsc);
1498 assert_non_null(ll->dflts);
1499 assert_int_equal(2, LY_ARRAY_SIZE(ll->dflts));
1500 assert_string_equal("xxx", ll->dflts[0]);
1501 assert_string_equal("yyy", ll->dflts[1]);
1502 assert_string_equal("zzz", ll->units);
1503 assert_int_equal(10, ll->max);
1504 assert_int_equal(0, ll->min);
1505 assert_string_equal("string", ll->type.name);
1506 assert_non_null(ll->exts);
1507 assert_non_null(ll->iffeatures);
1508 assert_non_null(ll->musts);
1509 assert_string_equal("test", ll->ref);
1510 assert_non_null(ll->when);
1511 assert_null(ll->parent);
1512 assert_null(ll->next);
1513 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_USER | LYS_SET_MAX, ll->flags);
1514 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1515
1516 /* full content - now with min-elements */
1517 str = "ll {min-elements 10; type string;} ...";
1518 assert_int_equal(LY_SUCCESS, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1519 assert_non_null(ll);
1520 assert_int_equal(LYS_LEAFLIST, ll->nodetype);
1521 assert_string_equal("ll", ll->name);
1522 assert_string_equal("string", ll->type.name);
1523 assert_int_equal(0, ll->max);
1524 assert_int_equal(10, ll->min);
1525 assert_int_equal(LYS_SET_MIN, ll->flags);
1526 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1527
1528 /* invalid */
1529 str = " ll {min-elements 1; default xx; type string;} ...";
1530 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
Radek Krejcia9026eb2018-12-12 16:04:47 +01001531 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 +01001532 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1533
1534 str = " ll {description \"missing type\";} ...";
1535 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1536 logbuf_assert("Missing mandatory keyword \"type\" as a child of \"leaf-list\". Line number 1.");
1537 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1538
Radek Krejcidf6cad12018-11-28 17:10:55 +01001539 str = " ll {type string; min-elements 10; max-elements 1;} ..."; /* invalid combination of min/max */
1540 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1541 logbuf_assert("Invalid combination of min-elements and max-elements: min value 10 is bigger than the max value 1. Line number 1.");
1542 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1543
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001544 ctx.mod_version = 1; /* simulate YANG 1.0 - default statement is not allowed */
Radek Krejci0e5d8382018-11-28 16:37:53 +01001545 str = " ll {default xx; type string;} ...";
1546 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1547 logbuf_assert("Invalid keyword \"default\" as a child of \"leaf-list\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
1548 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1549
1550 *state = NULL;
1551 ly_ctx_destroy(ctx.ctx, NULL);
1552}
1553
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001554static void
1555test_list(void **state)
1556{
1557 *state = test_list;
1558
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001559 struct ly_parser_ctx ctx = {0};
1560 struct lysp_node_list *l = NULL;
1561 const char *str;
1562
1563 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1564 assert_non_null(ctx.ctx);
1565 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001566 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001567
1568 /* invalid cardinality */
1569#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1570 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1571 assert_int_equal(LY_EVALID, parse_list(&ctx, &str, NULL, (struct lysp_node**)&l)); \
1572 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1573 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1574
1575 TEST_DUP("config", "true", "false");
1576 TEST_DUP("description", "text1", "text2");
1577 TEST_DUP("key", "one", "two");
1578 TEST_DUP("max-elements", "10", "20");
1579 TEST_DUP("min-elements", "10", "20");
1580 TEST_DUP("ordered-by", "user", "system");
1581 TEST_DUP("reference", "1", "2");
1582 TEST_DUP("status", "current", "obsolete");
1583 TEST_DUP("when", "true", "false");
1584#undef TEST_DUP
1585
1586 /* full content */
1587 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;}"
1588 "leaf-list ll {type string;} list li;max-elements 10; min-elements 1;must 'expr';notification not; ordered-by system; reference test;"
1589 "status current;typedef t {type int8;}unique xxx;unique yyy;uses g;when true;m:ext;} ...";
1590 assert_int_equal(LY_SUCCESS, parse_list(&ctx, &str, NULL, (struct lysp_node**)&l));
1591 assert_non_null(l);
1592 assert_int_equal(LYS_LIST, l->nodetype);
1593 assert_string_equal("l", l->name);
1594 assert_string_equal("test", l->dsc);
1595 assert_string_equal("l", l->key);
1596 assert_non_null(l->uniques);
1597 assert_int_equal(2, LY_ARRAY_SIZE(l->uniques));
1598 assert_string_equal("xxx", l->uniques[0]);
1599 assert_string_equal("yyy", l->uniques[1]);
1600 assert_int_equal(10, l->max);
1601 assert_int_equal(1, l->min);
1602 assert_non_null(l->exts);
1603 assert_non_null(l->iffeatures);
1604 assert_non_null(l->musts);
1605 assert_string_equal("test", l->ref);
1606 assert_non_null(l->when);
1607 assert_null(l->parent);
1608 assert_null(l->next);
1609 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM | LYS_SET_MAX | LYS_SET_MIN, l->flags);
1610 ly_set_erase(&ctx.tpdfs_nodes, NULL);
1611 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1612
1613 *state = NULL;
1614 ly_ctx_destroy(ctx.ctx, NULL);
1615}
1616
Radek Krejci056d0a82018-12-06 16:57:25 +01001617static void
1618test_choice(void **state)
1619{
1620 *state = test_choice;
1621
Radek Krejci056d0a82018-12-06 16:57:25 +01001622 struct ly_parser_ctx ctx = {0};
1623 struct lysp_node_choice *ch = NULL;
1624 const char *str;
1625
1626 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1627 assert_non_null(ctx.ctx);
1628 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001629 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci056d0a82018-12-06 16:57:25 +01001630
1631 /* invalid cardinality */
1632#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1633 str = "ch {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1634 assert_int_equal(LY_EVALID, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch)); \
1635 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1636 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1637
1638 TEST_DUP("config", "true", "false");
1639 TEST_DUP("default", "a", "b");
1640 TEST_DUP("description", "text1", "text2");
1641 TEST_DUP("mandatory", "true", "false");
1642 TEST_DUP("reference", "1", "2");
1643 TEST_DUP("status", "current", "obsolete");
1644 TEST_DUP("when", "true", "false");
1645#undef TEST_DUP
1646
Radek Krejcia9026eb2018-12-12 16:04:47 +01001647 /* full content - without default due to a collision with mandatory */
1648 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 +01001649 "leaf-list ll {type string;} list li;mandatory true;reference test;status current;when true;m:ext;} ...";
1650 assert_int_equal(LY_SUCCESS, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch));
1651 assert_non_null(ch);
1652 assert_int_equal(LYS_CHOICE, ch->nodetype);
1653 assert_string_equal("ch", ch->name);
1654 assert_string_equal("test", ch->dsc);
1655 assert_non_null(ch->exts);
1656 assert_non_null(ch->iffeatures);
1657 assert_string_equal("test", ch->ref);
1658 assert_non_null(ch->when);
1659 assert_null(ch->parent);
1660 assert_null(ch->next);
1661 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_MAND_TRUE, ch->flags);
1662 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1663
Radek Krejcia9026eb2018-12-12 16:04:47 +01001664 /* full content - the default missing from the previous node */
1665 str = "ch {default c;case c;} ...";
1666 assert_int_equal(LY_SUCCESS, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch));
1667 assert_non_null(ch);
1668 assert_int_equal(LYS_CHOICE, ch->nodetype);
1669 assert_string_equal("ch", ch->name);
1670 assert_string_equal("c", ch->dflt);
1671 assert_int_equal(0, ch->flags);
1672 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1673
1674 /* invalid content */
1675 str = "ch {mandatory true; default c1; case c1 {leaf x{type string;}}} ...";
1676 assert_int_equal(LY_EVALID, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch));
1677 logbuf_assert("Invalid combination of keywords \"mandatory\" and \"default\" as substatements of \"choice\". Line number 1.");
1678 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1679
1680 *state = NULL;
1681 ly_ctx_destroy(ctx.ctx, NULL);
1682}
1683
1684static void
1685test_case(void **state)
1686{
1687 *state = test_case;
1688
Radek Krejcia9026eb2018-12-12 16:04:47 +01001689 struct ly_parser_ctx ctx = {0};
1690 struct lysp_node_case *cs = NULL;
1691 const char *str;
1692
1693 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1694 assert_non_null(ctx.ctx);
1695 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001696 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejcia9026eb2018-12-12 16:04:47 +01001697
1698 /* invalid cardinality */
1699#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1700 str = "cs {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1701 assert_int_equal(LY_EVALID, parse_case(&ctx, &str, NULL, (struct lysp_node**)&cs)); \
1702 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1703 lysp_node_free(ctx.ctx, (struct lysp_node*)cs); cs = NULL;
1704
1705 TEST_DUP("description", "text1", "text2");
1706 TEST_DUP("reference", "1", "2");
1707 TEST_DUP("status", "current", "obsolete");
1708 TEST_DUP("when", "true", "false");
1709#undef TEST_DUP
1710
1711 /* full content */
1712 str = "cs {anydata any;anyxml anyxml; choice ch;container c;description test;if-feature f;leaf l {type string;}"
1713 "leaf-list ll {type string;} list li;reference test;status current;uses grp;when true;m:ext;} ...";
1714 assert_int_equal(LY_SUCCESS, parse_case(&ctx, &str, NULL, (struct lysp_node**)&cs));
1715 assert_non_null(cs);
1716 assert_int_equal(LYS_CASE, cs->nodetype);
1717 assert_string_equal("cs", cs->name);
1718 assert_string_equal("test", cs->dsc);
1719 assert_non_null(cs->exts);
1720 assert_non_null(cs->iffeatures);
1721 assert_string_equal("test", cs->ref);
1722 assert_non_null(cs->when);
1723 assert_null(cs->parent);
1724 assert_null(cs->next);
1725 assert_int_equal(LYS_STATUS_CURR, cs->flags);
1726 lysp_node_free(ctx.ctx, (struct lysp_node*)cs); cs = NULL;
1727
1728 /* invalid content */
1729 str = "cs {config true} ...";
1730 assert_int_equal(LY_EVALID, parse_case(&ctx, &str, NULL, (struct lysp_node**)&cs));
1731 logbuf_assert("Invalid keyword \"config\" as a child of \"case\". Line number 1.");
1732 lysp_node_free(ctx.ctx, (struct lysp_node*)cs); cs = NULL;
1733
Radek Krejci056d0a82018-12-06 16:57:25 +01001734 *state = NULL;
1735 ly_ctx_destroy(ctx.ctx, NULL);
1736}
1737
Radek Krejci9800fb82018-12-13 14:26:23 +01001738static void
1739test_any(void **state, enum yang_keyword kw)
1740{
1741 *state = test_any;
1742
Radek Krejci9800fb82018-12-13 14:26:23 +01001743 struct ly_parser_ctx ctx = {0};
1744 struct lysp_node_anydata *any = NULL;
1745 const char *str;
1746
1747 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1748 assert_non_null(ctx.ctx);
1749 ctx.line = 1;
Radek Krejci9800fb82018-12-13 14:26:23 +01001750 if (kw == YANG_ANYDATA) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001751 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejci9800fb82018-12-13 14:26:23 +01001752 } else {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001753 ctx.mod_version = 1; /* simulate YANG 1.0 */
Radek Krejci9800fb82018-12-13 14:26:23 +01001754 }
1755
1756 /* invalid cardinality */
1757#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1758 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1759 assert_int_equal(LY_EVALID, parse_any(&ctx, &str, kw, NULL, (struct lysp_node**)&any)); \
1760 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1761 lysp_node_free(ctx.ctx, (struct lysp_node*)any); any = NULL;
1762
1763 TEST_DUP("config", "true", "false");
1764 TEST_DUP("description", "text1", "text2");
1765 TEST_DUP("mandatory", "true", "false");
1766 TEST_DUP("reference", "1", "2");
1767 TEST_DUP("status", "current", "obsolete");
1768 TEST_DUP("when", "true", "false");
1769#undef TEST_DUP
1770
1771 /* full content */
1772 str = "any {config true;description test;if-feature f;mandatory true;must 'expr';reference test;status current;when true;m:ext;} ...";
1773 assert_int_equal(LY_SUCCESS, parse_any(&ctx, &str, kw, NULL, (struct lysp_node**)&any));
1774 assert_non_null(any);
1775 assert_int_equal(kw == YANG_ANYDATA ? LYS_ANYDATA : LYS_ANYXML, any->nodetype);
1776 assert_string_equal("any", any->name);
1777 assert_string_equal("test", any->dsc);
1778 assert_non_null(any->exts);
1779 assert_non_null(any->iffeatures);
1780 assert_non_null(any->musts);
1781 assert_string_equal("test", any->ref);
1782 assert_non_null(any->when);
1783 assert_null(any->parent);
1784 assert_null(any->next);
1785 assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR | LYS_MAND_TRUE, any->flags);
1786 lysp_node_free(ctx.ctx, (struct lysp_node*)any); any = NULL;
1787
1788 *state = NULL;
1789 ly_ctx_destroy(ctx.ctx, NULL);
1790}
1791
1792static void
1793test_anydata(void **state)
1794{
1795 return test_any(state, YANG_ANYDATA);
1796}
1797
1798static void
1799test_anyxml(void **state)
1800{
1801 return test_any(state, YANG_ANYXML);
1802}
1803
Radek Krejcie86bf772018-12-14 11:39:53 +01001804static void
1805test_grouping(void **state)
1806{
1807 *state = test_grouping;
1808
Radek Krejcie86bf772018-12-14 11:39:53 +01001809 struct ly_parser_ctx ctx = {0};
1810 struct lysp_grp *grp = NULL;
1811 const char *str;
1812
1813 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1814 assert_non_null(ctx.ctx);
1815 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001816 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejcie86bf772018-12-14 11:39:53 +01001817
1818 /* invalid cardinality */
1819#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1820 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1821 assert_int_equal(LY_EVALID, parse_grouping(&ctx, &str, NULL, &grp)); \
1822 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1823 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1824
1825 TEST_DUP("description", "text1", "text2");
1826 TEST_DUP("reference", "1", "2");
1827 TEST_DUP("status", "current", "obsolete");
1828#undef TEST_DUP
1829
1830 /* full content */
1831 str = "grp {action x;anydata any;anyxml anyxml; choice ch;container c;description test;grouping g;leaf l {type string;}"
1832 "leaf-list ll {type string;} list li;notification not;reference test;status current;typedef t {type int8;}uses g;m:ext;} ...";
1833 assert_int_equal(LY_SUCCESS, parse_grouping(&ctx, &str, NULL, &grp));
1834 assert_non_null(grp);
1835 assert_int_equal(LYS_GROUPING, grp->nodetype);
1836 assert_string_equal("grp", grp->name);
1837 assert_string_equal("test", grp->dsc);
1838 assert_non_null(grp->exts);
1839 assert_string_equal("test", grp->ref);
1840 assert_null(grp->parent);
1841 assert_int_equal( LYS_STATUS_CURR, grp->flags);
1842 ly_set_erase(&ctx.tpdfs_nodes, NULL);
1843 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1844
1845 /* invalid content */
1846 str = "grp {config true} ...";
1847 assert_int_equal(LY_EVALID, parse_grouping(&ctx, &str, NULL, &grp));
1848 logbuf_assert("Invalid keyword \"config\" as a child of \"grouping\". Line number 1.");
1849 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1850
1851 str = "grp {must 'expr'} ...";
1852 assert_int_equal(LY_EVALID, parse_grouping(&ctx, &str, NULL, &grp));
1853 logbuf_assert("Invalid keyword \"must\" as a child of \"grouping\". Line number 1.");
1854 FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL;
1855
1856 *state = NULL;
1857 ly_ctx_destroy(ctx.ctx, NULL);
1858}
1859
1860static void
1861test_uses(void **state)
1862{
1863 *state = test_uses;
1864
Radek Krejcie86bf772018-12-14 11:39:53 +01001865 struct ly_parser_ctx ctx = {0};
1866 struct lysp_node_uses *u = NULL;
1867 const char *str;
1868
1869 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1870 assert_non_null(ctx.ctx);
1871 ctx.line = 1;
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001872 ctx.mod_version = 2; /* simulate YANG 1.1 */
Radek Krejcie86bf772018-12-14 11:39:53 +01001873
1874 /* invalid cardinality */
1875#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1876 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1877 assert_int_equal(LY_EVALID, parse_uses(&ctx, &str, NULL, (struct lysp_node**)&u)); \
1878 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1879 lysp_node_free(ctx.ctx, (struct lysp_node*)u); u = NULL;
1880
1881 TEST_DUP("description", "text1", "text2");
1882 TEST_DUP("reference", "1", "2");
1883 TEST_DUP("status", "current", "obsolete");
1884 TEST_DUP("when", "true", "false");
1885#undef TEST_DUP
1886
1887 /* full content */
1888 str = "grpref {augment some/node;description test;if-feature f;reference test;refine some/other/node;status current;when true;m:ext;} ...";
1889 assert_int_equal(LY_SUCCESS, parse_uses(&ctx, &str, NULL, (struct lysp_node**)&u));
1890 assert_non_null(u);
1891 assert_int_equal(LYS_USES, u->nodetype);
1892 assert_string_equal("grpref", u->name);
1893 assert_string_equal("test", u->dsc);
1894 assert_non_null(u->exts);
1895 assert_non_null(u->iffeatures);
1896 assert_string_equal("test", u->ref);
1897 assert_non_null(u->augments);
1898 assert_non_null(u->refines);
1899 assert_non_null(u->when);
1900 assert_null(u->parent);
1901 assert_null(u->next);
1902 assert_int_equal(LYS_STATUS_CURR, u->flags);
1903 lysp_node_free(ctx.ctx, (struct lysp_node*)u); u = NULL;
1904
1905 *state = NULL;
1906 ly_ctx_destroy(ctx.ctx, NULL);
1907}
Radek Krejci5a7c4a52019-02-12 15:45:11 +01001908
1909
1910static void
1911test_augment(void **state)
1912{
1913 *state = test_augment;
1914
1915 struct ly_parser_ctx ctx = {0};
1916 struct lysp_augment *a = NULL;
1917 const char *str;
1918
1919 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1920 assert_non_null(ctx.ctx);
1921 ctx.line = 1;
1922 //ctx.mod_version = 2; /* simulate YANG 1.1 */
1923
1924 /* invalid cardinality */
1925#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1926 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1927 assert_int_equal(LY_EVALID, parse_augment(&ctx, &str, NULL, &a)); \
1928 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1929 lysp_augment_free(ctx.ctx, a); a = NULL;
1930
1931 TEST_DUP("description", "text1", "text2");
1932 TEST_DUP("reference", "1", "2");
1933 TEST_DUP("status", "current", "obsolete");
1934 TEST_DUP("when", "true", "false");
1935#undef TEST_DUP
1936
1937 /* full content */
1938 str = "/target/nodeid {action x; anydata any;anyxml anyxml; case cs; choice ch;container c;description test;if-feature f;leaf l {type string;}"
1939 "leaf-list ll {type string;} list li;notification not;reference test;status current;uses g;when true;m:ext;} ...";
1940 assert_int_equal(LY_SUCCESS, parse_augment(&ctx, &str, NULL, &a));
1941 assert_non_null(a);
1942 assert_int_equal(LYS_AUGMENT, a->nodetype);
1943 assert_string_equal("/target/nodeid", a->nodeid);
1944 assert_string_equal("test", a->dsc);
1945 assert_non_null(a->exts);
1946 assert_non_null(a->iffeatures);
1947 assert_string_equal("test", a->ref);
1948 assert_non_null(a->when);
1949 assert_null(a->parent);
1950 assert_int_equal(LYS_STATUS_CURR, a->flags);
1951 lysp_augment_free(ctx.ctx, a); a = NULL;
1952
1953 *state = NULL;
1954 ly_ctx_destroy(ctx.ctx, NULL);
1955}
1956
Radek Krejci80dd33e2018-09-26 15:57:18 +02001957int main(void)
1958{
1959 const struct CMUnitTest tests[] = {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001960 cmocka_unit_test_setup(test_helpers, logger_setup),
Radek Krejci80dd33e2018-09-26 15:57:18 +02001961 cmocka_unit_test_setup(test_comments, logger_setup),
Radek Krejciefd22f62018-09-27 11:47:58 +02001962 cmocka_unit_test_setup(test_arg, logger_setup),
Radek Krejcidcc7b322018-10-11 14:24:02 +02001963 cmocka_unit_test_setup(test_stmts, logger_setup),
Radek Krejci05b13982018-11-28 16:22:07 +01001964 cmocka_unit_test_setup_teardown(test_minmax, logger_setup, logger_teardown),
Radek Krejci40544fa2019-01-11 09:38:37 +01001965 cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown),
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001966 cmocka_unit_test_setup_teardown(test_identity, logger_setup, logger_teardown),
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001967 cmocka_unit_test_setup(test_feature, logger_setup),
1968 cmocka_unit_test_setup(test_deviation, logger_setup),
1969 cmocka_unit_test_setup(test_deviate, logger_setup),
Radek Krejci8c370832018-11-02 15:10:03 +01001970 cmocka_unit_test_setup(test_container, logger_setup),
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001971 cmocka_unit_test_setup_teardown(test_leaf, logger_setup, logger_teardown),
Radek Krejci0e5d8382018-11-28 16:37:53 +01001972 cmocka_unit_test_setup_teardown(test_leaflist, logger_setup, logger_teardown),
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001973 cmocka_unit_test_setup_teardown(test_list, logger_setup, logger_teardown),
Radek Krejci056d0a82018-12-06 16:57:25 +01001974 cmocka_unit_test_setup_teardown(test_choice, logger_setup, logger_teardown),
Radek Krejcia9026eb2018-12-12 16:04:47 +01001975 cmocka_unit_test_setup_teardown(test_case, logger_setup, logger_teardown),
Radek Krejci9800fb82018-12-13 14:26:23 +01001976 cmocka_unit_test_setup_teardown(test_anydata, logger_setup, logger_teardown),
1977 cmocka_unit_test_setup_teardown(test_anyxml, logger_setup, logger_teardown),
Radek Krejcie86bf772018-12-14 11:39:53 +01001978 cmocka_unit_test_setup_teardown(test_grouping, logger_setup, logger_teardown),
1979 cmocka_unit_test_setup_teardown(test_uses, logger_setup, logger_teardown),
Radek Krejci80dd33e2018-09-26 15:57:18 +02001980 };
1981
1982 return cmocka_run_group_tests(tests, NULL, NULL);
1983}