blob: bc31e1212b5e8686d9ab89d4e277f9a338e36ed4 [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
676 struct lysp_module mod = {0};
677 struct ly_parser_ctx ctx = {0};
678 uint16_t flags = 0;
679 uint32_t value = 0;
680 struct lysp_ext_instance *ext = NULL;
681 const char *str;
682
683 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
684 assert_non_null(ctx.ctx);
685 ctx.line = 1;
686 ctx.mod = &mod;
687 ctx.mod->version = 2; /* simulate YANG 1.1 */
688
Radek Krejcidf6cad12018-11-28 17:10:55 +0100689 str = " 1invalid; ...";
Radek Krejci05b13982018-11-28 16:22:07 +0100690 assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext));
Radek Krejcidf6cad12018-11-28 17:10:55 +0100691 logbuf_assert("Invalid value \"1invalid\" of \"min-elements\". Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100692
693 flags = value = 0;
694 str = " -1; ...";
695 assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext));
696 logbuf_assert("Invalid value \"-1\" of \"min-elements\". Line number 1.");
697
Radek Krejcidf6cad12018-11-28 17:10:55 +0100698 /* implementation limit */
699 flags = value = 0;
700 str = " 4294967296; ...";
701 assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext));
702 logbuf_assert("Value \"4294967296\" is out of \"min-elements\" bounds. Line number 1.");
703
Radek Krejci05b13982018-11-28 16:22:07 +0100704 flags = value = 0;
705 str = " 1; ...";
706 assert_int_equal(LY_SUCCESS, parse_minelements(&ctx, &str, &value, &flags, &ext));
707 assert_int_equal(LYS_SET_MIN, flags);
708 assert_int_equal(1, value);
709
710 flags = value = 0;
711 str = " 1 {m:ext;} ...";
712 assert_int_equal(LY_SUCCESS, parse_minelements(&ctx, &str, &value, &flags, &ext));
713 assert_int_equal(LYS_SET_MIN, flags);
714 assert_int_equal(1, value);
715 assert_non_null(ext);
716 FREE_ARRAY(ctx.ctx, ext, lysp_ext_instance_free);
717 ext = NULL;
718
719 flags = value = 0;
720 str = " 1 {config true;} ...";
721 assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext));
722 logbuf_assert("Invalid keyword \"config\" as a child of \"min-elements\". Line number 1.");
723
Radek Krejcidf6cad12018-11-28 17:10:55 +0100724 str = " 1invalid; ...";
Radek Krejci05b13982018-11-28 16:22:07 +0100725 assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext));
Radek Krejcidf6cad12018-11-28 17:10:55 +0100726 logbuf_assert("Invalid value \"1invalid\" of \"max-elements\". Line number 1.");
Radek Krejci05b13982018-11-28 16:22:07 +0100727
728 flags = value = 0;
729 str = " -1; ...";
730 assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext));
731 logbuf_assert("Invalid value \"-1\" of \"max-elements\". Line number 1.");
732
Radek Krejcidf6cad12018-11-28 17:10:55 +0100733 /* implementation limit */
734 flags = value = 0;
735 str = " 4294967296; ...";
736 assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext));
737 logbuf_assert("Value \"4294967296\" is out of \"max-elements\" bounds. Line number 1.");
738
Radek Krejci05b13982018-11-28 16:22:07 +0100739 flags = value = 0;
740 str = " 1; ...";
741 assert_int_equal(LY_SUCCESS, parse_maxelements(&ctx, &str, &value, &flags, &ext));
742 assert_int_equal(LYS_SET_MAX, flags);
743 assert_int_equal(1, value);
744
745 flags = value = 0;
746 str = " unbounded; ...";
747 assert_int_equal(LY_SUCCESS, parse_maxelements(&ctx, &str, &value, &flags, &ext));
748 assert_int_equal(LYS_SET_MAX, flags);
749 assert_int_equal(0, value);
750
751 flags = value = 0;
752 str = " 1 {m:ext;} ...";
753 assert_int_equal(LY_SUCCESS, parse_maxelements(&ctx, &str, &value, &flags, &ext));
754 assert_int_equal(LYS_SET_MAX, flags);
755 assert_int_equal(1, value);
756 assert_non_null(ext);
757 FREE_ARRAY(ctx.ctx, ext, lysp_ext_instance_free);
758 ext = NULL;
759
760 flags = value = 0;
761 str = " 1 {config true;} ...";
762 assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext));
763 logbuf_assert("Invalid keyword \"config\" as a child of \"max-elements\". Line number 1.");
764
765 *state = NULL;
766 ly_ctx_destroy(ctx.ctx, NULL);
767}
768
Radek Krejci9fcacc12018-10-11 15:59:11 +0200769static struct lysp_module *
Radek Krejci09306362018-10-15 15:26:01 +0200770mod_renew(struct ly_parser_ctx *ctx, struct lysp_module *mod, uint8_t submodule)
Radek Krejci9fcacc12018-10-11 15:59:11 +0200771{
772 lysp_module_free(mod);
773 mod = calloc(1, sizeof *mod);
774 mod->ctx = ctx->ctx;
Radek Krejci09306362018-10-15 15:26:01 +0200775 mod->submodule = submodule;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200776 assert_non_null(mod);
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100777 ctx->mod = mod;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200778 return mod;
779}
780
Radek Krejcid33273d2018-10-25 14:55:52 +0200781static LY_ERR test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
782 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
783 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
784{
785 *module_data = user_data;
786 *format = LYS_IN_YANG;
787 *free_module_data = NULL;
788 return LY_SUCCESS;
789}
790
Radek Krejci9fcacc12018-10-11 15:59:11 +0200791static void
792test_module(void **state)
793{
794 (void) state; /* unused */
795
796 struct ly_parser_ctx ctx;
797 struct lysp_module *mod;
798 const char *str;
799
800 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
801 assert_non_null(ctx.ctx);
802 ctx.line = 1;
Radek Krejcia042ea12018-10-13 07:52:15 +0200803 ctx.indent = 0;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200804
Radek Krejci09306362018-10-15 15:26:01 +0200805 mod = mod_renew(&ctx, NULL, 0);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200806
807 /* missing mandatory substatements */
808 str = " name {}";
809 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
810 assert_string_equal("name", mod->name);
811 logbuf_assert("Missing mandatory keyword \"namespace\" as a child of \"module\". Line number 1.");
Radek Krejci09306362018-10-15 15:26:01 +0200812 mod = mod_renew(&ctx, mod, 0);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200813
814 str = " name {namespace urn:x;}";
815 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
816 assert_string_equal("urn:x", mod->ns);
817 logbuf_assert("Missing mandatory keyword \"prefix\" as a child of \"module\". Line number 1.");
Radek Krejci09306362018-10-15 15:26:01 +0200818 mod = mod_renew(&ctx, mod, 0);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200819
820 str = " name {namespace urn:x;prefix \"x\";}";
821 assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod));
822 assert_string_equal("x", mod->prefix);
Radek Krejci09306362018-10-15 15:26:01 +0200823 mod = mod_renew(&ctx, mod, 0);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200824
Radek Krejci027d5802018-11-14 16:57:28 +0100825#define SCHEMA_BEGINNING " name {yang-version 1.1;namespace urn:x;prefix \"x\";"
826#define SCHEMA_BEGINNING2 " name {namespace urn:x;prefix \"x\";"
Radek Krejcia042ea12018-10-13 07:52:15 +0200827#define TEST_NODE(NODETYPE, INPUT, NAME) \
828 str = SCHEMA_BEGINNING INPUT; \
829 assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod)); \
830 assert_non_null(mod->data); \
831 assert_int_equal(NODETYPE, mod->data->nodetype); \
832 assert_string_equal(NAME, mod->data->name); \
Radek Krejci09306362018-10-15 15:26:01 +0200833 mod = mod_renew(&ctx, mod, 0);
Radek Krejcia042ea12018-10-13 07:52:15 +0200834#define TEST_GENERIC(INPUT, TARGET, TEST) \
835 str = SCHEMA_BEGINNING INPUT; \
836 assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod)); \
837 assert_non_null(TARGET); \
838 TEST; \
Radek Krejci09306362018-10-15 15:26:01 +0200839 mod = mod_renew(&ctx, mod, 0);
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200840#define TEST_DUP(MEMBER, VALUE1, VALUE2, LINE, SUBMODULE) \
841 TEST_DUP_GENERIC(SCHEMA_BEGINNING, MEMBER, VALUE1, VALUE2, \
842 parse_sub_module, mod, LINE, mod = mod_renew(&ctx, mod, SUBMODULE))
Radek Krejcia042ea12018-10-13 07:52:15 +0200843
844 /* duplicated namespace, prefix */
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200845 TEST_DUP("namespace", "y", "z", "1", 0);
846 TEST_DUP("prefix", "y", "z", "1", 0);
847 TEST_DUP("contact", "a", "b", "1", 0);
848 TEST_DUP("description", "a", "b", "1", 0);
849 TEST_DUP("organization", "a", "b", "1", 0);
850 TEST_DUP("reference", "a", "b", "1", 0);
Radek Krejcia042ea12018-10-13 07:52:15 +0200851
Radek Krejci70853c52018-10-15 14:46:16 +0200852 /* not allowed in module (submodule-specific) */
853 str = SCHEMA_BEGINNING "belongs-to master {prefix m;}}";
854 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
855 logbuf_assert("Invalid keyword \"belongs-to\" as a child of \"module\". Line number 1.");
Radek Krejci09306362018-10-15 15:26:01 +0200856 mod = mod_renew(&ctx, mod, 0);
Radek Krejci70853c52018-10-15 14:46:16 +0200857
Radek Krejcia042ea12018-10-13 07:52:15 +0200858 /* anydata */
859 TEST_NODE(LYS_ANYDATA, "anydata test;}", "test");
860 /* anyxml */
861 TEST_NODE(LYS_ANYXML, "anyxml test;}", "test");
862 /* augment */
863 TEST_GENERIC("augment /somepath;}", mod->augments,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200864 assert_string_equal("/somepath", mod->augments[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200865 /* choice */
866 TEST_NODE(LYS_CHOICE, "choice test;}", "test");
867 /* contact 0..1 */
868 TEST_GENERIC("contact \"firstname\" + \n\t\" surname\";}", mod->contact,
869 assert_string_equal("firstname surname", mod->contact));
870 /* container */
871 TEST_NODE(LYS_CONTAINER, "container test;}", "test");
872 /* description 0..1 */
873 TEST_GENERIC("description \'some description\';}", mod->dsc,
874 assert_string_equal("some description", mod->dsc));
875 /* deviation */
876 TEST_GENERIC("deviation /somepath {deviate not-supported;}}", mod->deviations,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200877 assert_string_equal("/somepath", mod->deviations[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200878 /* extension */
879 TEST_GENERIC("extension test;}", mod->extensions,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200880 assert_string_equal("test", mod->extensions[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200881 /* feature */
882 TEST_GENERIC("feature test;}", mod->features,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200883 assert_string_equal("test", mod->features[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200884 /* grouping */
885 TEST_GENERIC("grouping grp;}", mod->groupings,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200886 assert_string_equal("grp", mod->groupings[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200887 /* identity */
888 TEST_GENERIC("identity test;}", mod->identities,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200889 assert_string_equal("test", mod->identities[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200890 /* import */
Radek Krejci086c7132018-10-26 15:29:04 +0200891 ly_ctx_set_module_imp_clb(ctx.ctx, test_imp_clb, "module zzz { namespace urn:zzz; prefix z;}");
892 TEST_GENERIC("import zzz {prefix z;}}", mod->imports,
893 assert_string_equal("zzz", mod->imports[0].name));
Radek Krejci70853c52018-10-15 14:46:16 +0200894
Radek Krejcia042ea12018-10-13 07:52:15 +0200895 /* import - prefix collision */
Radek Krejci086c7132018-10-26 15:29:04 +0200896 str = SCHEMA_BEGINNING "import zzz {prefix x;}}";
Radek Krejcia042ea12018-10-13 07:52:15 +0200897 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
Radek Krejci70853c52018-10-15 14:46:16 +0200898 logbuf_assert("Prefix \"x\" already used as module prefix. Line number 2.");
Radek Krejci09306362018-10-15 15:26:01 +0200899 mod = mod_renew(&ctx, mod, 0);
Radek Krejci086c7132018-10-26 15:29:04 +0200900 str = SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix y;}}";
Radek Krejci86d106e2018-10-18 09:53:19 +0200901 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
Radek Krejci086c7132018-10-26 15:29:04 +0200902 logbuf_assert("Prefix \"y\" already used to import \"zzz\" module. Line number 2.");
Radek Krejci86d106e2018-10-18 09:53:19 +0200903 mod = mod_renew(&ctx, mod, 0);
Radek Krejci086c7132018-10-26 15:29:04 +0200904 str = "module" SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix z;}}";
905 assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
906 assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx));
907 logbuf_assert("Single revision of the module \"zzz\" referred twice.");
Radek Krejci70853c52018-10-15 14:46:16 +0200908
Radek Krejcia042ea12018-10-13 07:52:15 +0200909 /* include */
Radek Krejci9ed7a192018-10-31 16:23:51 +0100910 store = 1;
Radek Krejcid33273d2018-10-25 14:55:52 +0200911 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 +0200912 str = "module" SCHEMA_BEGINNING "include xxx;}";
913 assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
914 assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx));
915 logbuf_assert("Included \"xxx\" schema from \"name\" is actually not a submodule.");
Radek Krejci9ed7a192018-10-31 16:23:51 +0100916 store = -1;
Radek Krejcid33273d2018-10-25 14:55:52 +0200917
Radek Krejci9ed7a192018-10-31 16:23:51 +0100918 store = 1;
Radek Krejci313d9902018-11-08 09:42:58 +0100919 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 +0200920 str = "module" SCHEMA_BEGINNING "include xxx;}";
921 assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
922 assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx));
923 logbuf_assert("Included \"xxx\" submodule from \"name\" belongs-to a different module \"wrong-name\".");
Radek Krejci9ed7a192018-10-31 16:23:51 +0100924 store = -1;
Radek Krejcid33273d2018-10-25 14:55:52 +0200925
Radek Krejci313d9902018-11-08 09:42:58 +0100926 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 +0200927 TEST_GENERIC("include xxx;}", mod->includes,
Radek Krejci086c7132018-10-26 15:29:04 +0200928 assert_string_equal("xxx", mod->includes[0].name));
Radek Krejcid33273d2018-10-25 14:55:52 +0200929
Radek Krejcia042ea12018-10-13 07:52:15 +0200930 /* leaf */
931 TEST_NODE(LYS_LEAF, "leaf test {type string;}}", "test");
932 /* leaf-list */
933 TEST_NODE(LYS_LEAFLIST, "leaf-list test {type string;}}", "test");
934 /* list */
935 TEST_NODE(LYS_LIST, "list test {key a;leaf a {type string;}}}", "test");
936 /* notification */
937 TEST_GENERIC("notification test;}", mod->notifs,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200938 assert_string_equal("test", mod->notifs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200939 /* organization 0..1 */
940 TEST_GENERIC("organization \"CESNET a.l.e.\";}", mod->org,
941 assert_string_equal("CESNET a.l.e.", mod->org));
942 /* reference 0..1 */
943 TEST_GENERIC("reference RFC7950;}", mod->ref,
944 assert_string_equal("RFC7950", mod->ref));
945 /* revision */
946 TEST_GENERIC("revision 2018-10-12;}", mod->revs,
Radek Krejcib7db73a2018-10-24 14:18:40 +0200947 assert_string_equal("2018-10-12", mod->revs[0].date));
Radek Krejcia042ea12018-10-13 07:52:15 +0200948 /* rpc */
949 TEST_GENERIC("rpc test;}", mod->rpcs,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200950 assert_string_equal("test", mod->rpcs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200951 /* typedef */
952 TEST_GENERIC("typedef test{type string;}}", mod->typedefs,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200953 assert_string_equal("test", mod->typedefs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200954 /* uses */
955 TEST_NODE(LYS_USES, "uses test;}", "test");
956 /* yang-version */
Radek Krejci027d5802018-11-14 16:57:28 +0100957 str = SCHEMA_BEGINNING2 "\n\tyang-version 10;}";
Radek Krejcia042ea12018-10-13 07:52:15 +0200958 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
959 logbuf_assert("Invalid value \"10\" of \"yang-version\". Line number 3.");
Radek Krejci09306362018-10-15 15:26:01 +0200960 mod = mod_renew(&ctx, mod, 0);
Radek Krejci027d5802018-11-14 16:57:28 +0100961 str = SCHEMA_BEGINNING2 "yang-version 1.0;yang-version 1.1;}";
Radek Krejcia042ea12018-10-13 07:52:15 +0200962 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
963 logbuf_assert("Duplicate keyword \"yang-version\". Line number 3.");
Radek Krejci09306362018-10-15 15:26:01 +0200964 mod = mod_renew(&ctx, mod, 0);
Radek Krejci027d5802018-11-14 16:57:28 +0100965 str = SCHEMA_BEGINNING2 "yang-version 1.0;}";
Radek Krejcia042ea12018-10-13 07:52:15 +0200966 assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod));
967 assert_int_equal(1, mod->version);
Radek Krejci09306362018-10-15 15:26:01 +0200968 mod = mod_renew(&ctx, mod, 0);
Radek Krejci027d5802018-11-14 16:57:28 +0100969 str = SCHEMA_BEGINNING2 "yang-version \"1.1\";}";
Radek Krejcia042ea12018-10-13 07:52:15 +0200970 assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod));
971 assert_int_equal(2, mod->version);
Radek Krejci09306362018-10-15 15:26:01 +0200972 mod = mod_renew(&ctx, mod, 0);
973
Radek Krejci156ccaf2018-10-15 15:49:17 +0200974 /* extensions */
975 TEST_GENERIC("prefix:test;}", mod->exts,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200976 assert_string_equal("prefix:test", mod->exts[0].name);
977 assert_int_equal(LYEXT_SUBSTMT_SELF, mod->exts[0].insubstmt));
Radek Krejci156ccaf2018-10-15 15:49:17 +0200978 mod = mod_renew(&ctx, mod, 0);
979
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200980 /* invalid substatement */
981 str = SCHEMA_BEGINNING "must false;}";
982 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
983 logbuf_assert("Invalid keyword \"must\" as a child of \"module\". Line number 3.");
984 mod = mod_renew(&ctx, mod, 0);
985
Radek Krejci09306362018-10-15 15:26:01 +0200986 /* submodule */
987 mod->submodule = 1;
988
989 /* missing mandatory substatements */
990 str = " subname {}";
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100991 lydict_remove(ctx.ctx, mod->name);
Radek Krejci09306362018-10-15 15:26:01 +0200992 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
993 assert_string_equal("subname", mod->name);
994 logbuf_assert("Missing mandatory keyword \"belongs-to\" as a child of \"submodule\". Line number 3.");
995 mod = mod_renew(&ctx, mod, 1);
996
Radek Krejci313d9902018-11-08 09:42:58 +0100997 str = " subname {belongs-to name {prefix x;}}";
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100998 lydict_remove(ctx.ctx, mod->name);
Radek Krejci09306362018-10-15 15:26:01 +0200999 assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod));
1000 assert_string_equal("name", mod->belongsto);
1001 mod = mod_renew(&ctx, mod, 1);
1002
1003#undef SCHEMA_BEGINNING
Radek Krejci313d9902018-11-08 09:42:58 +01001004#define SCHEMA_BEGINNING " subname {belongs-to name {prefix x;}"
Radek Krejci09306362018-10-15 15:26:01 +02001005
1006 /* duplicated namespace, prefix */
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001007 TEST_DUP("belongs-to", "module1", "module2", "3", 1);
Radek Krejci09306362018-10-15 15:26:01 +02001008
1009 /* not allowed in submodule (module-specific) */
1010 str = SCHEMA_BEGINNING "namespace \"urn:z\";}";
1011 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
1012 logbuf_assert("Invalid keyword \"namespace\" as a child of \"submodule\". Line number 3.");
1013 mod = mod_renew(&ctx, mod, 1);
1014 str = SCHEMA_BEGINNING "prefix m;}}";
1015 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
1016 logbuf_assert("Invalid keyword \"prefix\" as a child of \"submodule\". Line number 3.");
1017 mod = mod_renew(&ctx, mod, 1);
Radek Krejcia042ea12018-10-13 07:52:15 +02001018
1019#undef TEST_GENERIC
1020#undef TEST_NODE
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001021#undef TEST_DUP
Radek Krejcia042ea12018-10-13 07:52:15 +02001022#undef SCHEMA_BEGINNING
1023
Radek Krejci9fcacc12018-10-11 15:59:11 +02001024 lysp_module_free(mod);
1025 ly_ctx_destroy(ctx.ctx, NULL);
Radek Krejciefd22f62018-09-27 11:47:58 +02001026}
1027
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001028static void
1029test_identity(void **state)
1030{
1031 (void) state; /* unused */
1032
1033 struct ly_parser_ctx ctx;
Radek Krejcid05cbd92018-12-05 14:26:40 +01001034 struct lysp_module m = {0};
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001035 struct lysp_ident *ident = NULL;
1036 const char *str;
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001037
1038 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1039 assert_non_null(ctx.ctx);
1040 ctx.line = 1;
1041 ctx.indent = 0;
Radek Krejci027d5802018-11-14 16:57:28 +01001042 ctx.mod = &m;
1043 ctx.mod->version = 2; /* simulate YANG 1.1 */
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001044
1045 /* invalid cardinality */
1046#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001047 TEST_DUP_GENERIC(" test {", MEMBER, VALUE1, VALUE2, parse_identity, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001048 &ident, "1", FREE_ARRAY(ctx.ctx, ident, lysp_ident_free); ident = NULL)
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001049
1050 TEST_DUP("description", "a", "b");
1051 TEST_DUP("reference", "a", "b");
1052 TEST_DUP("status", "current", "obsolete");
1053
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001054 /* full content */
1055 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 +02001056 assert_int_equal(LY_SUCCESS, parse_identity(&ctx, &str, &ident));
1057 assert_non_null(ident);
1058 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001059 FREE_ARRAY(ctx.ctx, ident, lysp_ident_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001060 ident = NULL;
1061
1062 /* invalid substatement */
1063 str = " test {organization XXX;}";
1064 assert_int_equal(LY_EVALID, parse_identity(&ctx, &str, &ident));
1065 logbuf_assert("Invalid keyword \"organization\" as a child of \"identity\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001066 FREE_ARRAY(ctx.ctx, ident, lysp_ident_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001067 ident = NULL;
1068
1069#undef TEST_DUP
1070
1071 ly_ctx_destroy(ctx.ctx, NULL);
1072}
1073
1074static void
1075test_feature(void **state)
1076{
1077 (void) state; /* unused */
1078
1079 struct ly_parser_ctx ctx;
1080 struct lysp_feature *features = NULL;
1081 const char *str;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001082
1083 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1084 assert_non_null(ctx.ctx);
1085 ctx.line = 1;
1086 ctx.indent = 0;
1087
1088 /* invalid cardinality */
1089#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1090 TEST_DUP_GENERIC(" test {", MEMBER, VALUE1, VALUE2, parse_feature, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001091 &features, "1", FREE_ARRAY(ctx.ctx, features, lysp_feature_free); features = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001092
1093 TEST_DUP("description", "a", "b");
1094 TEST_DUP("reference", "a", "b");
1095 TEST_DUP("status", "current", "obsolete");
1096
1097 /* full content */
1098 str = " test {description text;reference \'another text\';status current; if-feature x;if-feature y;prefix:ext;} ...";
1099 assert_int_equal(LY_SUCCESS, parse_feature(&ctx, &str, &features));
1100 assert_non_null(features);
1101 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001102 FREE_ARRAY(ctx.ctx, features, lysp_feature_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001103 features = NULL;
1104
1105 /* invalid substatement */
1106 str = " test {organization XXX;}";
1107 assert_int_equal(LY_EVALID, parse_feature(&ctx, &str, &features));
1108 logbuf_assert("Invalid keyword \"organization\" as a child of \"feature\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001109 FREE_ARRAY(ctx.ctx, features, lysp_feature_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001110 features = NULL;
1111
1112#undef TEST_DUP
1113
1114 ly_ctx_destroy(ctx.ctx, NULL);
1115}
1116
1117static void
1118test_deviation(void **state)
1119{
1120 (void) state; /* unused */
1121
1122 struct ly_parser_ctx ctx;
1123 struct lysp_deviation *d = NULL;
1124 const char *str;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001125
1126 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1127 assert_non_null(ctx.ctx);
1128 ctx.line = 1;
1129 ctx.indent = 0;
1130
1131 /* invalid cardinality */
1132#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1133 TEST_DUP_GENERIC(" test {deviate not-supported;", MEMBER, VALUE1, VALUE2, parse_deviation, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001134 &d, "1", FREE_ARRAY(ctx.ctx, d, lysp_deviation_free); d = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001135
1136 TEST_DUP("description", "a", "b");
1137 TEST_DUP("reference", "a", "b");
1138
1139 /* full content */
1140 str = " test {deviate not-supported;description text;reference \'another text\';prefix:ext;} ...";
1141 assert_int_equal(LY_SUCCESS, parse_deviation(&ctx, &str, &d));
1142 assert_non_null(d);
1143 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001144 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001145 d = NULL;
1146
1147 /* missing mandatory substatement */
1148 str = " test {description text;}";
1149 assert_int_equal(LY_EVALID, parse_deviation(&ctx, &str, &d));
1150 logbuf_assert("Missing mandatory keyword \"deviate\" as a child of \"deviation\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001151 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001152 d = NULL;
1153
1154 /* invalid substatement */
1155 str = " test {deviate not-supported; status obsolete;}";
1156 assert_int_equal(LY_EVALID, parse_deviation(&ctx, &str, &d));
1157 logbuf_assert("Invalid keyword \"status\" as a child of \"deviation\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001158 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001159 d = NULL;
1160
1161#undef TEST_DUP
1162
1163 ly_ctx_destroy(ctx.ctx, NULL);
1164}
1165
1166static void
1167test_deviate(void **state)
1168{
1169 (void) state; /* unused */
1170
1171 struct ly_parser_ctx ctx;
1172 struct lysp_deviate *d = NULL;
1173 const char *str;
1174
1175 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1176 assert_non_null(ctx.ctx);
1177 ctx.line = 1;
1178 ctx.indent = 0;
1179
1180 /* invalid cardinality */
1181#define TEST_DUP(TYPE, MEMBER, VALUE1, VALUE2) \
1182 TEST_DUP_GENERIC(TYPE" {", MEMBER, VALUE1, VALUE2, parse_deviate, \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001183 &d, "1", lysp_deviate_free(ctx.ctx, d); free(d); d = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001184
1185 TEST_DUP("add", "config", "true", "false");
1186 TEST_DUP("replace", "default", "int8", "uint8");
1187 TEST_DUP("add", "mandatory", "true", "false");
1188 TEST_DUP("add", "max-elements", "1", "2");
1189 TEST_DUP("add", "min-elements", "1", "2");
1190 TEST_DUP("replace", "type", "int8", "uint8");
1191 TEST_DUP("add", "units", "kilometers", "miles");
1192
1193 /* full contents */
1194 str = " not-supported {prefix:ext;} ...";
1195 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1196 assert_non_null(d);
1197 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001198 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001199 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;} ...";
1200 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1201 assert_non_null(d);
1202 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001203 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001204 str = " delete {units meters; must 1; must 2; unique x; unique y; default a; default b; prefix:ext;} ...";
1205 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1206 assert_non_null(d);
1207 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001208 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001209 str = " replace {type string; units meters; default a; config true; mandatory true; min-elements 1; max-elements 2; prefix:ext;} ...";
1210 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1211 assert_non_null(d);
1212 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001213 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001214
1215 /* invalid substatements */
1216#define TEST_NOT_SUP(DEV, STMT, VALUE) \
1217 str = " "DEV" {"STMT" "VALUE";}..."; \
1218 assert_int_equal(LY_EVALID, parse_deviate(&ctx, &str, &d)); \
1219 logbuf_assert("Deviate \""DEV"\" does not support keyword \""STMT"\". Line number 1."); \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001220 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001221
1222 TEST_NOT_SUP("not-supported", "units", "meters");
1223 TEST_NOT_SUP("not-supported", "must", "1");
1224 TEST_NOT_SUP("not-supported", "unique", "x");
1225 TEST_NOT_SUP("not-supported", "default", "a");
1226 TEST_NOT_SUP("not-supported", "config", "true");
1227 TEST_NOT_SUP("not-supported", "mandatory", "true");
1228 TEST_NOT_SUP("not-supported", "min-elements", "1");
1229 TEST_NOT_SUP("not-supported", "max-elements", "2");
1230 TEST_NOT_SUP("not-supported", "type", "string");
1231 TEST_NOT_SUP("add", "type", "string");
1232 TEST_NOT_SUP("delete", "config", "true");
1233 TEST_NOT_SUP("delete", "mandatory", "true");
1234 TEST_NOT_SUP("delete", "min-elements", "1");
1235 TEST_NOT_SUP("delete", "max-elements", "2");
1236 TEST_NOT_SUP("delete", "type", "string");
1237 TEST_NOT_SUP("replace", "must", "1");
1238 TEST_NOT_SUP("replace", "unique", "a");
1239
1240 str = " nonsence; ...";
1241 assert_int_equal(LY_EVALID, parse_deviate(&ctx, &str, &d));
1242 logbuf_assert("Invalid value \"nonsence\" of \"deviate\". Line number 1.");
1243 assert_null(d);
1244
1245#undef TEST_NOT_SUP
1246#undef TEST_DUP
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001247
1248 ly_ctx_destroy(ctx.ctx, NULL);
1249}
1250
Radek Krejci8c370832018-11-02 15:10:03 +01001251static void
1252test_container(void **state)
1253{
1254 (void) state; /* unused */
1255
Radek Krejci313d9902018-11-08 09:42:58 +01001256 struct lysp_module mod = {0};
1257 struct ly_parser_ctx ctx = {0};
Radek Krejci8c370832018-11-02 15:10:03 +01001258 struct lysp_node_container *c = NULL;
1259 const char *str;
1260
1261 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1262 assert_non_null(ctx.ctx);
1263 ctx.line = 1;
Radek Krejci313d9902018-11-08 09:42:58 +01001264 ctx.mod = &mod;
Radek Krejci027d5802018-11-14 16:57:28 +01001265 ctx.mod->version = 2; /* simulate YANG 1.1 */
Radek Krejci8c370832018-11-02 15:10:03 +01001266
1267 /* invalid cardinality */
1268#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1269 str = "cont {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1270 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c)); \
1271 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001272 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001273
1274 TEST_DUP("config", "true", "false");
1275 TEST_DUP("description", "text1", "text2");
1276 TEST_DUP("presence", "true", "false");
1277 TEST_DUP("reference", "1", "2");
1278 TEST_DUP("status", "current", "obsolete");
1279 TEST_DUP("when", "true", "false");
1280#undef TEST_DUP
1281
1282 /* full content */
Radek Krejci313d9902018-11-08 09:42:58 +01001283 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;}"
1284 "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 +01001285 assert_int_equal(LY_SUCCESS, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1286 assert_non_null(c);
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001287 assert_int_equal(LYS_CONTAINER, c->nodetype);
1288 assert_string_equal("cont", c->name);
Radek Krejci8c370832018-11-02 15:10:03 +01001289 assert_non_null(c->actions);
1290 assert_non_null(c->child);
1291 assert_string_equal("test", c->dsc);
1292 assert_non_null(c->exts);
1293 assert_non_null(c->groupings);
1294 assert_non_null(c->iffeatures);
1295 assert_non_null(c->musts);
1296 assert_non_null(c->notifs);
1297 assert_string_equal("true", c->presence);
1298 assert_string_equal("test", c->ref);
1299 assert_non_null(c->typedefs);
1300 assert_non_null(c->when);
1301 assert_null(c->parent);
1302 assert_null(c->next);
1303 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR, c->flags);
Radek Krejci313d9902018-11-08 09:42:58 +01001304 ly_set_erase(&ctx.tpdfs_nodes, NULL);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001305 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001306
1307 /* invalid */
1308 str = " cont {augment /root;} ...";
1309 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1310 logbuf_assert("Invalid keyword \"augment\" as a child of \"container\". Line number 1.");
Radek Krejci4f28eda2018-11-12 11:46:16 +01001311 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001312 str = " cont {nonsence true;} ...";
1313 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1314 logbuf_assert("Invalid character sequence \"nonsence\", expected a keyword. Line number 1.");
Radek Krejci4f28eda2018-11-12 11:46:16 +01001315 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001316
1317 ly_ctx_destroy(ctx.ctx, NULL);
1318}
1319
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001320static void
1321test_leaf(void **state)
1322{
1323 *state = test_leaf;
1324
1325 struct lysp_module mod = {0};
1326 struct ly_parser_ctx ctx = {0};
1327 struct lysp_node_leaf *l = NULL;
1328 const char *str;
1329
1330 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1331 assert_non_null(ctx.ctx);
1332 ctx.line = 1;
1333 ctx.mod = &mod;
1334 //ctx.mod->version = 2; /* simulate YANG 1.1 */
1335
1336 /* invalid cardinality */
1337#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1338 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1339 assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l)); \
1340 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1341 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1342
1343 TEST_DUP("config", "true", "false");
1344 TEST_DUP("default", "x", "y");
1345 TEST_DUP("description", "text1", "text2");
1346 TEST_DUP("mandatory", "true", "false");
1347 TEST_DUP("reference", "1", "2");
1348 TEST_DUP("status", "current", "obsolete");
Radek Krejci0e5d8382018-11-28 16:37:53 +01001349 TEST_DUP("type", "int8", "uint8");
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001350 TEST_DUP("units", "text1", "text2");
1351 TEST_DUP("when", "true", "false");
1352#undef TEST_DUP
1353
1354 /* full content - without mandatory which is mutual exclusive with default */
1355 str = "l {config false;default \"xxx\";description test;if-feature f;"
1356 "must 'expr';reference test;status current;type string; units yyy;when true;m:ext;} ...";
1357 assert_int_equal(LY_SUCCESS, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
1358 assert_non_null(l);
1359 assert_int_equal(LYS_LEAF, l->nodetype);
1360 assert_string_equal("l", l->name);
1361 assert_string_equal("test", l->dsc);
1362 assert_string_equal("xxx", l->dflt);
1363 assert_string_equal("yyy", l->units);
1364 assert_string_equal("string", l->type.name);
1365 assert_non_null(l->exts);
1366 assert_non_null(l->iffeatures);
1367 assert_non_null(l->musts);
1368 assert_string_equal("test", l->ref);
1369 assert_non_null(l->when);
1370 assert_null(l->parent);
1371 assert_null(l->next);
1372 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR, l->flags);
1373 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1374
1375 /* full content - now with mandatory */
1376 str = "l {mandatory true; type string;} ...";
1377 assert_int_equal(LY_SUCCESS, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
1378 assert_non_null(l);
1379 assert_int_equal(LYS_LEAF, l->nodetype);
1380 assert_string_equal("l", l->name);
1381 assert_string_equal("string", l->type.name);
1382 assert_int_equal(LYS_MAND_TRUE, l->flags);
1383 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1384
1385 /* invalid */
1386 str = " l {mandatory true; default xx; type string;} ...";
1387 assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
1388 logbuf_assert("Invalid combination of keywords \"mandatory\" and \"default\" as children of \"leaf\". Line number 1.");
1389 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1390
1391 str = " l {description \"missing type\";} ...";
1392 assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
1393 logbuf_assert("Missing mandatory keyword \"type\" as a child of \"leaf\". Line number 1.");
1394 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1395
1396 *state = NULL;
1397 ly_ctx_destroy(ctx.ctx, NULL);
1398}
1399
Radek Krejci0e5d8382018-11-28 16:37:53 +01001400static void
1401test_leaflist(void **state)
1402{
1403 *state = test_leaf;
1404
1405 struct lysp_module mod = {0};
1406 struct ly_parser_ctx ctx = {0};
1407 struct lysp_node_leaflist *ll = NULL;
1408 const char *str;
1409
1410 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1411 assert_non_null(ctx.ctx);
1412 ctx.line = 1;
1413 ctx.mod = &mod;
1414 ctx.mod->version = 2; /* simulate YANG 1.1 */
1415
1416 /* invalid cardinality */
1417#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1418 str = "ll {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1419 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll)); \
1420 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1421 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1422
1423 TEST_DUP("config", "true", "false");
1424 TEST_DUP("description", "text1", "text2");
1425 TEST_DUP("max-elements", "10", "20");
1426 TEST_DUP("min-elements", "10", "20");
1427 TEST_DUP("ordered-by", "user", "system");
1428 TEST_DUP("reference", "1", "2");
1429 TEST_DUP("status", "current", "obsolete");
1430 TEST_DUP("type", "int8", "uint8");
1431 TEST_DUP("units", "text1", "text2");
1432 TEST_DUP("when", "true", "false");
1433#undef TEST_DUP
1434
1435 /* full content - without min-elements which is mutual exclusive with default */
1436 str = "ll {config false;default \"xxx\"; default \"yyy\";description test;if-feature f;"
1437 "max-elements 10;must 'expr';ordered-by user;reference test;"
1438 "status current;type string; units zzz;when true;m:ext;} ...";
1439 assert_int_equal(LY_SUCCESS, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1440 assert_non_null(ll);
1441 assert_int_equal(LYS_LEAFLIST, ll->nodetype);
1442 assert_string_equal("ll", ll->name);
1443 assert_string_equal("test", ll->dsc);
1444 assert_non_null(ll->dflts);
1445 assert_int_equal(2, LY_ARRAY_SIZE(ll->dflts));
1446 assert_string_equal("xxx", ll->dflts[0]);
1447 assert_string_equal("yyy", ll->dflts[1]);
1448 assert_string_equal("zzz", ll->units);
1449 assert_int_equal(10, ll->max);
1450 assert_int_equal(0, ll->min);
1451 assert_string_equal("string", ll->type.name);
1452 assert_non_null(ll->exts);
1453 assert_non_null(ll->iffeatures);
1454 assert_non_null(ll->musts);
1455 assert_string_equal("test", ll->ref);
1456 assert_non_null(ll->when);
1457 assert_null(ll->parent);
1458 assert_null(ll->next);
1459 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_USER | LYS_SET_MAX, ll->flags);
1460 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1461
1462 /* full content - now with min-elements */
1463 str = "ll {min-elements 10; type string;} ...";
1464 assert_int_equal(LY_SUCCESS, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1465 assert_non_null(ll);
1466 assert_int_equal(LYS_LEAFLIST, ll->nodetype);
1467 assert_string_equal("ll", ll->name);
1468 assert_string_equal("string", ll->type.name);
1469 assert_int_equal(0, ll->max);
1470 assert_int_equal(10, ll->min);
1471 assert_int_equal(LYS_SET_MIN, ll->flags);
1472 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1473
1474 /* invalid */
1475 str = " ll {min-elements 1; default xx; type string;} ...";
1476 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1477 logbuf_assert("Invalid combination of keywords \"min-elements\" and \"default\" as children of \"leaf-list\". Line number 1.");
1478 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1479
1480 str = " ll {description \"missing type\";} ...";
1481 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1482 logbuf_assert("Missing mandatory keyword \"type\" as a child of \"leaf-list\". Line number 1.");
1483 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1484
Radek Krejcidf6cad12018-11-28 17:10:55 +01001485 str = " ll {type string; min-elements 10; max-elements 1;} ..."; /* invalid combination of min/max */
1486 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1487 logbuf_assert("Invalid combination of min-elements and max-elements: min value 10 is bigger than the max value 1. Line number 1.");
1488 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1489
Radek Krejci0e5d8382018-11-28 16:37:53 +01001490 ctx.mod->version = 1; /* simulate YANG 1.0 - default statement is not allowed */
1491 str = " ll {default xx; type string;} ...";
1492 assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll));
1493 logbuf_assert("Invalid keyword \"default\" as a child of \"leaf-list\" - the statement is allowed only in YANG 1.1 modules. Line number 1.");
1494 lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL;
1495
1496 *state = NULL;
1497 ly_ctx_destroy(ctx.ctx, NULL);
1498}
1499
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001500static void
1501test_list(void **state)
1502{
1503 *state = test_list;
1504
1505 struct lysp_module mod = {0};
1506 struct ly_parser_ctx ctx = {0};
1507 struct lysp_node_list *l = NULL;
1508 const char *str;
1509
1510 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1511 assert_non_null(ctx.ctx);
1512 ctx.line = 1;
1513 ctx.mod = &mod;
1514 ctx.mod->version = 2; /* simulate YANG 1.1 */
1515
1516 /* invalid cardinality */
1517#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1518 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1519 assert_int_equal(LY_EVALID, parse_list(&ctx, &str, NULL, (struct lysp_node**)&l)); \
1520 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1521 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1522
1523 TEST_DUP("config", "true", "false");
1524 TEST_DUP("description", "text1", "text2");
1525 TEST_DUP("key", "one", "two");
1526 TEST_DUP("max-elements", "10", "20");
1527 TEST_DUP("min-elements", "10", "20");
1528 TEST_DUP("ordered-by", "user", "system");
1529 TEST_DUP("reference", "1", "2");
1530 TEST_DUP("status", "current", "obsolete");
1531 TEST_DUP("when", "true", "false");
1532#undef TEST_DUP
1533
1534 /* full content */
1535 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;}"
1536 "leaf-list ll {type string;} list li;max-elements 10; min-elements 1;must 'expr';notification not; ordered-by system; reference test;"
1537 "status current;typedef t {type int8;}unique xxx;unique yyy;uses g;when true;m:ext;} ...";
1538 assert_int_equal(LY_SUCCESS, parse_list(&ctx, &str, NULL, (struct lysp_node**)&l));
1539 assert_non_null(l);
1540 assert_int_equal(LYS_LIST, l->nodetype);
1541 assert_string_equal("l", l->name);
1542 assert_string_equal("test", l->dsc);
1543 assert_string_equal("l", l->key);
1544 assert_non_null(l->uniques);
1545 assert_int_equal(2, LY_ARRAY_SIZE(l->uniques));
1546 assert_string_equal("xxx", l->uniques[0]);
1547 assert_string_equal("yyy", l->uniques[1]);
1548 assert_int_equal(10, l->max);
1549 assert_int_equal(1, l->min);
1550 assert_non_null(l->exts);
1551 assert_non_null(l->iffeatures);
1552 assert_non_null(l->musts);
1553 assert_string_equal("test", l->ref);
1554 assert_non_null(l->when);
1555 assert_null(l->parent);
1556 assert_null(l->next);
1557 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM | LYS_SET_MAX | LYS_SET_MIN, l->flags);
1558 ly_set_erase(&ctx.tpdfs_nodes, NULL);
1559 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1560
1561 *state = NULL;
1562 ly_ctx_destroy(ctx.ctx, NULL);
1563}
1564
Radek Krejci056d0a82018-12-06 16:57:25 +01001565static void
1566test_choice(void **state)
1567{
1568 *state = test_choice;
1569
1570 struct lysp_module mod = {0};
1571 struct ly_parser_ctx ctx = {0};
1572 struct lysp_node_choice *ch = NULL;
1573 const char *str;
1574
1575 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1576 assert_non_null(ctx.ctx);
1577 ctx.line = 1;
1578 ctx.mod = &mod;
1579 ctx.mod->version = 2; /* simulate YANG 1.1 */
1580
1581 /* invalid cardinality */
1582#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1583 str = "ch {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1584 assert_int_equal(LY_EVALID, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch)); \
1585 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1586 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1587
1588 TEST_DUP("config", "true", "false");
1589 TEST_DUP("default", "a", "b");
1590 TEST_DUP("description", "text1", "text2");
1591 TEST_DUP("mandatory", "true", "false");
1592 TEST_DUP("reference", "1", "2");
1593 TEST_DUP("status", "current", "obsolete");
1594 TEST_DUP("when", "true", "false");
1595#undef TEST_DUP
1596
1597 /* full content */
1598 str = "ch {anydata any;anyxml anyxml; case c;choice ch;config false;container c;default c;description test;if-feature f;leaf l {type string;}"
1599 "leaf-list ll {type string;} list li;mandatory true;reference test;status current;when true;m:ext;} ...";
1600 assert_int_equal(LY_SUCCESS, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch));
1601 assert_non_null(ch);
1602 assert_int_equal(LYS_CHOICE, ch->nodetype);
1603 assert_string_equal("ch", ch->name);
1604 assert_string_equal("test", ch->dsc);
1605 assert_non_null(ch->exts);
1606 assert_non_null(ch->iffeatures);
1607 assert_string_equal("test", ch->ref);
1608 assert_non_null(ch->when);
1609 assert_null(ch->parent);
1610 assert_null(ch->next);
1611 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_MAND_TRUE, ch->flags);
1612 lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL;
1613
1614 *state = NULL;
1615 ly_ctx_destroy(ctx.ctx, NULL);
1616}
1617
Radek Krejci80dd33e2018-09-26 15:57:18 +02001618int main(void)
1619{
1620 const struct CMUnitTest tests[] = {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001621 cmocka_unit_test_setup(test_helpers, logger_setup),
Radek Krejci80dd33e2018-09-26 15:57:18 +02001622 cmocka_unit_test_setup(test_comments, logger_setup),
Radek Krejciefd22f62018-09-27 11:47:58 +02001623 cmocka_unit_test_setup(test_arg, logger_setup),
Radek Krejcidcc7b322018-10-11 14:24:02 +02001624 cmocka_unit_test_setup(test_stmts, logger_setup),
Radek Krejci05b13982018-11-28 16:22:07 +01001625 cmocka_unit_test_setup_teardown(test_minmax, logger_setup, logger_teardown),
Radek Krejci9fcacc12018-10-11 15:59:11 +02001626 cmocka_unit_test_setup(test_module, logger_setup),
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001627 cmocka_unit_test_setup(test_identity, logger_setup),
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001628 cmocka_unit_test_setup(test_feature, logger_setup),
1629 cmocka_unit_test_setup(test_deviation, logger_setup),
1630 cmocka_unit_test_setup(test_deviate, logger_setup),
Radek Krejci8c370832018-11-02 15:10:03 +01001631 cmocka_unit_test_setup(test_container, logger_setup),
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001632 cmocka_unit_test_setup_teardown(test_leaf, logger_setup, logger_teardown),
Radek Krejci0e5d8382018-11-28 16:37:53 +01001633 cmocka_unit_test_setup_teardown(test_leaflist, logger_setup, logger_teardown),
Radek Krejci9bb94eb2018-12-04 16:48:35 +01001634 cmocka_unit_test_setup_teardown(test_list, logger_setup, logger_teardown),
Radek Krejci056d0a82018-12-06 16:57:25 +01001635 cmocka_unit_test_setup_teardown(test_choice, logger_setup, logger_teardown),
Radek Krejci80dd33e2018-09-26 15:57:18 +02001636 };
1637
1638 return cmocka_run_group_tests(tests, NULL, NULL);
1639}