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