blob: f31d66c285f8dd89cf245c79ca0a5f52e35358d2 [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 Krejci9fcacc12018-10-11 15:59:11 +0200671static struct lysp_module *
Radek Krejci09306362018-10-15 15:26:01 +0200672mod_renew(struct ly_parser_ctx *ctx, struct lysp_module *mod, uint8_t submodule)
Radek Krejci9fcacc12018-10-11 15:59:11 +0200673{
674 lysp_module_free(mod);
675 mod = calloc(1, sizeof *mod);
676 mod->ctx = ctx->ctx;
Radek Krejci09306362018-10-15 15:26:01 +0200677 mod->submodule = submodule;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200678 assert_non_null(mod);
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100679 ctx->mod = mod;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200680 return mod;
681}
682
Radek Krejcid33273d2018-10-25 14:55:52 +0200683static LY_ERR test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
684 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
685 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
686{
687 *module_data = user_data;
688 *format = LYS_IN_YANG;
689 *free_module_data = NULL;
690 return LY_SUCCESS;
691}
692
Radek Krejci9fcacc12018-10-11 15:59:11 +0200693static void
694test_module(void **state)
695{
696 (void) state; /* unused */
697
698 struct ly_parser_ctx ctx;
699 struct lysp_module *mod;
700 const char *str;
701
702 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
703 assert_non_null(ctx.ctx);
704 ctx.line = 1;
Radek Krejcia042ea12018-10-13 07:52:15 +0200705 ctx.indent = 0;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200706
Radek Krejci09306362018-10-15 15:26:01 +0200707 mod = mod_renew(&ctx, NULL, 0);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200708
709 /* missing mandatory substatements */
710 str = " name {}";
711 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
712 assert_string_equal("name", mod->name);
713 logbuf_assert("Missing mandatory keyword \"namespace\" as a child of \"module\". Line number 1.");
Radek Krejci09306362018-10-15 15:26:01 +0200714 mod = mod_renew(&ctx, mod, 0);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200715
716 str = " name {namespace urn:x;}";
717 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
718 assert_string_equal("urn:x", mod->ns);
719 logbuf_assert("Missing mandatory keyword \"prefix\" as a child of \"module\". Line number 1.");
Radek Krejci09306362018-10-15 15:26:01 +0200720 mod = mod_renew(&ctx, mod, 0);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200721
722 str = " name {namespace urn:x;prefix \"x\";}";
723 assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod));
724 assert_string_equal("x", mod->prefix);
Radek Krejci09306362018-10-15 15:26:01 +0200725 mod = mod_renew(&ctx, mod, 0);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200726
Radek Krejci027d5802018-11-14 16:57:28 +0100727#define SCHEMA_BEGINNING " name {yang-version 1.1;namespace urn:x;prefix \"x\";"
728#define SCHEMA_BEGINNING2 " name {namespace urn:x;prefix \"x\";"
Radek Krejcia042ea12018-10-13 07:52:15 +0200729#define TEST_NODE(NODETYPE, INPUT, NAME) \
730 str = SCHEMA_BEGINNING INPUT; \
731 assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod)); \
732 assert_non_null(mod->data); \
733 assert_int_equal(NODETYPE, mod->data->nodetype); \
734 assert_string_equal(NAME, mod->data->name); \
Radek Krejci09306362018-10-15 15:26:01 +0200735 mod = mod_renew(&ctx, mod, 0);
Radek Krejcia042ea12018-10-13 07:52:15 +0200736#define TEST_GENERIC(INPUT, TARGET, TEST) \
737 str = SCHEMA_BEGINNING INPUT; \
738 assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod)); \
739 assert_non_null(TARGET); \
740 TEST; \
Radek Krejci09306362018-10-15 15:26:01 +0200741 mod = mod_renew(&ctx, mod, 0);
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200742#define TEST_DUP(MEMBER, VALUE1, VALUE2, LINE, SUBMODULE) \
743 TEST_DUP_GENERIC(SCHEMA_BEGINNING, MEMBER, VALUE1, VALUE2, \
744 parse_sub_module, mod, LINE, mod = mod_renew(&ctx, mod, SUBMODULE))
Radek Krejcia042ea12018-10-13 07:52:15 +0200745
746 /* duplicated namespace, prefix */
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200747 TEST_DUP("namespace", "y", "z", "1", 0);
748 TEST_DUP("prefix", "y", "z", "1", 0);
749 TEST_DUP("contact", "a", "b", "1", 0);
750 TEST_DUP("description", "a", "b", "1", 0);
751 TEST_DUP("organization", "a", "b", "1", 0);
752 TEST_DUP("reference", "a", "b", "1", 0);
Radek Krejcia042ea12018-10-13 07:52:15 +0200753
Radek Krejci70853c52018-10-15 14:46:16 +0200754 /* not allowed in module (submodule-specific) */
755 str = SCHEMA_BEGINNING "belongs-to master {prefix m;}}";
756 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
757 logbuf_assert("Invalid keyword \"belongs-to\" as a child of \"module\". Line number 1.");
Radek Krejci09306362018-10-15 15:26:01 +0200758 mod = mod_renew(&ctx, mod, 0);
Radek Krejci70853c52018-10-15 14:46:16 +0200759
Radek Krejcia042ea12018-10-13 07:52:15 +0200760 /* anydata */
761 TEST_NODE(LYS_ANYDATA, "anydata test;}", "test");
762 /* anyxml */
763 TEST_NODE(LYS_ANYXML, "anyxml test;}", "test");
764 /* augment */
765 TEST_GENERIC("augment /somepath;}", mod->augments,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200766 assert_string_equal("/somepath", mod->augments[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200767 /* choice */
768 TEST_NODE(LYS_CHOICE, "choice test;}", "test");
769 /* contact 0..1 */
770 TEST_GENERIC("contact \"firstname\" + \n\t\" surname\";}", mod->contact,
771 assert_string_equal("firstname surname", mod->contact));
772 /* container */
773 TEST_NODE(LYS_CONTAINER, "container test;}", "test");
774 /* description 0..1 */
775 TEST_GENERIC("description \'some description\';}", mod->dsc,
776 assert_string_equal("some description", mod->dsc));
777 /* deviation */
778 TEST_GENERIC("deviation /somepath {deviate not-supported;}}", mod->deviations,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200779 assert_string_equal("/somepath", mod->deviations[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200780 /* extension */
781 TEST_GENERIC("extension test;}", mod->extensions,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200782 assert_string_equal("test", mod->extensions[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200783 /* feature */
784 TEST_GENERIC("feature test;}", mod->features,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200785 assert_string_equal("test", mod->features[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200786 /* grouping */
787 TEST_GENERIC("grouping grp;}", mod->groupings,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200788 assert_string_equal("grp", mod->groupings[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200789 /* identity */
790 TEST_GENERIC("identity test;}", mod->identities,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200791 assert_string_equal("test", mod->identities[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200792 /* import */
Radek Krejci086c7132018-10-26 15:29:04 +0200793 ly_ctx_set_module_imp_clb(ctx.ctx, test_imp_clb, "module zzz { namespace urn:zzz; prefix z;}");
794 TEST_GENERIC("import zzz {prefix z;}}", mod->imports,
795 assert_string_equal("zzz", mod->imports[0].name));
Radek Krejci70853c52018-10-15 14:46:16 +0200796
Radek Krejcia042ea12018-10-13 07:52:15 +0200797 /* import - prefix collision */
Radek Krejci086c7132018-10-26 15:29:04 +0200798 str = SCHEMA_BEGINNING "import zzz {prefix x;}}";
Radek Krejcia042ea12018-10-13 07:52:15 +0200799 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
Radek Krejci70853c52018-10-15 14:46:16 +0200800 logbuf_assert("Prefix \"x\" already used as module prefix. Line number 2.");
Radek Krejci09306362018-10-15 15:26:01 +0200801 mod = mod_renew(&ctx, mod, 0);
Radek Krejci086c7132018-10-26 15:29:04 +0200802 str = SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix y;}}";
Radek Krejci86d106e2018-10-18 09:53:19 +0200803 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
Radek Krejci086c7132018-10-26 15:29:04 +0200804 logbuf_assert("Prefix \"y\" already used to import \"zzz\" module. Line number 2.");
Radek Krejci86d106e2018-10-18 09:53:19 +0200805 mod = mod_renew(&ctx, mod, 0);
Radek Krejci086c7132018-10-26 15:29:04 +0200806 str = "module" SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix z;}}";
807 assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
808 assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx));
809 logbuf_assert("Single revision of the module \"zzz\" referred twice.");
Radek Krejci70853c52018-10-15 14:46:16 +0200810
Radek Krejcia042ea12018-10-13 07:52:15 +0200811 /* include */
Radek Krejci9ed7a192018-10-31 16:23:51 +0100812 store = 1;
Radek Krejcid33273d2018-10-25 14:55:52 +0200813 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 +0200814 str = "module" SCHEMA_BEGINNING "include xxx;}";
815 assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
816 assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx));
817 logbuf_assert("Included \"xxx\" schema from \"name\" is actually not a submodule.");
Radek Krejci9ed7a192018-10-31 16:23:51 +0100818 store = -1;
Radek Krejcid33273d2018-10-25 14:55:52 +0200819
Radek Krejci9ed7a192018-10-31 16:23:51 +0100820 store = 1;
Radek Krejci313d9902018-11-08 09:42:58 +0100821 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 +0200822 str = "module" SCHEMA_BEGINNING "include xxx;}";
823 assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
824 assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx));
825 logbuf_assert("Included \"xxx\" submodule from \"name\" belongs-to a different module \"wrong-name\".");
Radek Krejci9ed7a192018-10-31 16:23:51 +0100826 store = -1;
Radek Krejcid33273d2018-10-25 14:55:52 +0200827
Radek Krejci313d9902018-11-08 09:42:58 +0100828 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 +0200829 TEST_GENERIC("include xxx;}", mod->includes,
Radek Krejci086c7132018-10-26 15:29:04 +0200830 assert_string_equal("xxx", mod->includes[0].name));
Radek Krejcid33273d2018-10-25 14:55:52 +0200831
Radek Krejcia042ea12018-10-13 07:52:15 +0200832 /* leaf */
833 TEST_NODE(LYS_LEAF, "leaf test {type string;}}", "test");
834 /* leaf-list */
835 TEST_NODE(LYS_LEAFLIST, "leaf-list test {type string;}}", "test");
836 /* list */
837 TEST_NODE(LYS_LIST, "list test {key a;leaf a {type string;}}}", "test");
838 /* notification */
839 TEST_GENERIC("notification test;}", mod->notifs,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200840 assert_string_equal("test", mod->notifs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200841 /* organization 0..1 */
842 TEST_GENERIC("organization \"CESNET a.l.e.\";}", mod->org,
843 assert_string_equal("CESNET a.l.e.", mod->org));
844 /* reference 0..1 */
845 TEST_GENERIC("reference RFC7950;}", mod->ref,
846 assert_string_equal("RFC7950", mod->ref));
847 /* revision */
848 TEST_GENERIC("revision 2018-10-12;}", mod->revs,
Radek Krejcib7db73a2018-10-24 14:18:40 +0200849 assert_string_equal("2018-10-12", mod->revs[0].date));
Radek Krejcia042ea12018-10-13 07:52:15 +0200850 /* rpc */
851 TEST_GENERIC("rpc test;}", mod->rpcs,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200852 assert_string_equal("test", mod->rpcs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200853 /* typedef */
854 TEST_GENERIC("typedef test{type string;}}", mod->typedefs,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200855 assert_string_equal("test", mod->typedefs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200856 /* uses */
857 TEST_NODE(LYS_USES, "uses test;}", "test");
858 /* yang-version */
Radek Krejci027d5802018-11-14 16:57:28 +0100859 str = SCHEMA_BEGINNING2 "\n\tyang-version 10;}";
Radek Krejcia042ea12018-10-13 07:52:15 +0200860 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
861 logbuf_assert("Invalid value \"10\" of \"yang-version\". Line number 3.");
Radek Krejci09306362018-10-15 15:26:01 +0200862 mod = mod_renew(&ctx, mod, 0);
Radek Krejci027d5802018-11-14 16:57:28 +0100863 str = SCHEMA_BEGINNING2 "yang-version 1.0;yang-version 1.1;}";
Radek Krejcia042ea12018-10-13 07:52:15 +0200864 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
865 logbuf_assert("Duplicate keyword \"yang-version\". Line number 3.");
Radek Krejci09306362018-10-15 15:26:01 +0200866 mod = mod_renew(&ctx, mod, 0);
Radek Krejci027d5802018-11-14 16:57:28 +0100867 str = SCHEMA_BEGINNING2 "yang-version 1.0;}";
Radek Krejcia042ea12018-10-13 07:52:15 +0200868 assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod));
869 assert_int_equal(1, mod->version);
Radek Krejci09306362018-10-15 15:26:01 +0200870 mod = mod_renew(&ctx, mod, 0);
Radek Krejci027d5802018-11-14 16:57:28 +0100871 str = SCHEMA_BEGINNING2 "yang-version \"1.1\";}";
Radek Krejcia042ea12018-10-13 07:52:15 +0200872 assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod));
873 assert_int_equal(2, mod->version);
Radek Krejci09306362018-10-15 15:26:01 +0200874 mod = mod_renew(&ctx, mod, 0);
875
Radek Krejci156ccaf2018-10-15 15:49:17 +0200876 /* extensions */
877 TEST_GENERIC("prefix:test;}", mod->exts,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200878 assert_string_equal("prefix:test", mod->exts[0].name);
879 assert_int_equal(LYEXT_SUBSTMT_SELF, mod->exts[0].insubstmt));
Radek Krejci156ccaf2018-10-15 15:49:17 +0200880 mod = mod_renew(&ctx, mod, 0);
881
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200882 /* invalid substatement */
883 str = SCHEMA_BEGINNING "must false;}";
884 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
885 logbuf_assert("Invalid keyword \"must\" as a child of \"module\". Line number 3.");
886 mod = mod_renew(&ctx, mod, 0);
887
Radek Krejci09306362018-10-15 15:26:01 +0200888 /* submodule */
889 mod->submodule = 1;
890
891 /* missing mandatory substatements */
892 str = " subname {}";
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100893 lydict_remove(ctx.ctx, mod->name);
Radek Krejci09306362018-10-15 15:26:01 +0200894 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
895 assert_string_equal("subname", mod->name);
896 logbuf_assert("Missing mandatory keyword \"belongs-to\" as a child of \"submodule\". Line number 3.");
897 mod = mod_renew(&ctx, mod, 1);
898
Radek Krejci313d9902018-11-08 09:42:58 +0100899 str = " subname {belongs-to name {prefix x;}}";
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100900 lydict_remove(ctx.ctx, mod->name);
Radek Krejci09306362018-10-15 15:26:01 +0200901 assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod));
902 assert_string_equal("name", mod->belongsto);
903 mod = mod_renew(&ctx, mod, 1);
904
905#undef SCHEMA_BEGINNING
Radek Krejci313d9902018-11-08 09:42:58 +0100906#define SCHEMA_BEGINNING " subname {belongs-to name {prefix x;}"
Radek Krejci09306362018-10-15 15:26:01 +0200907
908 /* duplicated namespace, prefix */
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200909 TEST_DUP("belongs-to", "module1", "module2", "3", 1);
Radek Krejci09306362018-10-15 15:26:01 +0200910
911 /* not allowed in submodule (module-specific) */
912 str = SCHEMA_BEGINNING "namespace \"urn:z\";}";
913 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
914 logbuf_assert("Invalid keyword \"namespace\" as a child of \"submodule\". Line number 3.");
915 mod = mod_renew(&ctx, mod, 1);
916 str = SCHEMA_BEGINNING "prefix m;}}";
917 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
918 logbuf_assert("Invalid keyword \"prefix\" as a child of \"submodule\". Line number 3.");
919 mod = mod_renew(&ctx, mod, 1);
Radek Krejcia042ea12018-10-13 07:52:15 +0200920
921#undef TEST_GENERIC
922#undef TEST_NODE
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200923#undef TEST_DUP
Radek Krejcia042ea12018-10-13 07:52:15 +0200924#undef SCHEMA_BEGINNING
925
Radek Krejci9fcacc12018-10-11 15:59:11 +0200926 lysp_module_free(mod);
927 ly_ctx_destroy(ctx.ctx, NULL);
Radek Krejciefd22f62018-09-27 11:47:58 +0200928}
929
Radek Krejci4c6d9bd2018-10-15 16:43:06 +0200930static void
931test_identity(void **state)
932{
933 (void) state; /* unused */
934
935 struct ly_parser_ctx ctx;
Radek Krejci027d5802018-11-14 16:57:28 +0100936 struct lysp_module *mod = NULL, m = {0};
Radek Krejci4c6d9bd2018-10-15 16:43:06 +0200937 struct lysp_ident *ident = NULL;
938 const char *str;
Radek Krejci4c6d9bd2018-10-15 16:43:06 +0200939
940 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
941 assert_non_null(ctx.ctx);
942 ctx.line = 1;
943 ctx.indent = 0;
Radek Krejci027d5802018-11-14 16:57:28 +0100944 ctx.mod = &m;
945 ctx.mod->version = 2; /* simulate YANG 1.1 */
Radek Krejci4c6d9bd2018-10-15 16:43:06 +0200946
947 /* invalid cardinality */
948#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200949 TEST_DUP_GENERIC(" test {", MEMBER, VALUE1, VALUE2, parse_identity, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +0200950 &ident, "1", FREE_ARRAY(ctx.ctx, ident, lysp_ident_free); ident = NULL)
Radek Krejci4c6d9bd2018-10-15 16:43:06 +0200951
952 TEST_DUP("description", "a", "b");
953 TEST_DUP("reference", "a", "b");
954 TEST_DUP("status", "current", "obsolete");
955
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200956 /* full content */
957 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 +0200958 assert_int_equal(LY_SUCCESS, parse_identity(&ctx, &str, &ident));
959 assert_non_null(ident);
960 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +0200961 FREE_ARRAY(ctx.ctx, ident, lysp_ident_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200962 ident = NULL;
963
964 /* invalid substatement */
965 str = " test {organization XXX;}";
966 assert_int_equal(LY_EVALID, parse_identity(&ctx, &str, &ident));
967 logbuf_assert("Invalid keyword \"organization\" as a child of \"identity\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +0200968 FREE_ARRAY(ctx.ctx, ident, lysp_ident_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200969 ident = NULL;
970
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100971 /* identity duplication */
972 str = "module a {namespace urn:a; prefix a; identity a; identity a;}";
Radek Krejci313d9902018-11-08 09:42:58 +0100973 assert_int_equal(LY_EVALID, yang_parse(&ctx, str, &mod));
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100974 logbuf_assert("Duplicate identifier \"a\" of identity statement. Line number 1.");
975 assert_null(mod);
976
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200977#undef TEST_DUP
978
979 ly_ctx_destroy(ctx.ctx, NULL);
980}
981
982static void
983test_feature(void **state)
984{
985 (void) state; /* unused */
986
987 struct ly_parser_ctx ctx;
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100988 struct lysp_module *mod = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200989 struct lysp_feature *features = NULL;
990 const char *str;
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200991
992 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
993 assert_non_null(ctx.ctx);
994 ctx.line = 1;
995 ctx.indent = 0;
996
997 /* invalid cardinality */
998#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
999 TEST_DUP_GENERIC(" test {", MEMBER, VALUE1, VALUE2, parse_feature, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001000 &features, "1", FREE_ARRAY(ctx.ctx, features, lysp_feature_free); features = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001001
1002 TEST_DUP("description", "a", "b");
1003 TEST_DUP("reference", "a", "b");
1004 TEST_DUP("status", "current", "obsolete");
1005
1006 /* full content */
1007 str = " test {description text;reference \'another text\';status current; if-feature x;if-feature y;prefix:ext;} ...";
1008 assert_int_equal(LY_SUCCESS, parse_feature(&ctx, &str, &features));
1009 assert_non_null(features);
1010 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001011 FREE_ARRAY(ctx.ctx, features, lysp_feature_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001012 features = NULL;
1013
1014 /* invalid substatement */
1015 str = " test {organization XXX;}";
1016 assert_int_equal(LY_EVALID, parse_feature(&ctx, &str, &features));
1017 logbuf_assert("Invalid keyword \"organization\" as a child of \"feature\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001018 FREE_ARRAY(ctx.ctx, features, lysp_feature_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001019 features = NULL;
1020
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001021 /* feature duplication */
1022 str = "module a {namespace urn:a; prefix a; feature a; feature a;}";
Radek Krejci313d9902018-11-08 09:42:58 +01001023 assert_int_equal(LY_EVALID, yang_parse(&ctx, str, &mod));
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001024 logbuf_assert("Duplicate identifier \"a\" of feature statement. Line number 1.");
1025 assert_null(mod);
1026
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001027#undef TEST_DUP
1028
1029 ly_ctx_destroy(ctx.ctx, NULL);
1030}
1031
1032static void
1033test_deviation(void **state)
1034{
1035 (void) state; /* unused */
1036
1037 struct ly_parser_ctx ctx;
1038 struct lysp_deviation *d = NULL;
1039 const char *str;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001040
1041 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1042 assert_non_null(ctx.ctx);
1043 ctx.line = 1;
1044 ctx.indent = 0;
1045
1046 /* invalid cardinality */
1047#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1048 TEST_DUP_GENERIC(" test {deviate not-supported;", MEMBER, VALUE1, VALUE2, parse_deviation, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001049 &d, "1", FREE_ARRAY(ctx.ctx, d, lysp_deviation_free); d = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001050
1051 TEST_DUP("description", "a", "b");
1052 TEST_DUP("reference", "a", "b");
1053
1054 /* full content */
1055 str = " test {deviate not-supported;description text;reference \'another text\';prefix:ext;} ...";
1056 assert_int_equal(LY_SUCCESS, parse_deviation(&ctx, &str, &d));
1057 assert_non_null(d);
1058 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001059 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001060 d = NULL;
1061
1062 /* missing mandatory substatement */
1063 str = " test {description text;}";
1064 assert_int_equal(LY_EVALID, parse_deviation(&ctx, &str, &d));
1065 logbuf_assert("Missing mandatory keyword \"deviate\" as a child of \"deviation\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001066 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001067 d = NULL;
1068
1069 /* invalid substatement */
1070 str = " test {deviate not-supported; status obsolete;}";
1071 assert_int_equal(LY_EVALID, parse_deviation(&ctx, &str, &d));
1072 logbuf_assert("Invalid keyword \"status\" as a child of \"deviation\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001073 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001074 d = NULL;
1075
1076#undef TEST_DUP
1077
1078 ly_ctx_destroy(ctx.ctx, NULL);
1079}
1080
1081static void
1082test_deviate(void **state)
1083{
1084 (void) state; /* unused */
1085
1086 struct ly_parser_ctx ctx;
1087 struct lysp_deviate *d = NULL;
1088 const char *str;
1089
1090 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1091 assert_non_null(ctx.ctx);
1092 ctx.line = 1;
1093 ctx.indent = 0;
1094
1095 /* invalid cardinality */
1096#define TEST_DUP(TYPE, MEMBER, VALUE1, VALUE2) \
1097 TEST_DUP_GENERIC(TYPE" {", MEMBER, VALUE1, VALUE2, parse_deviate, \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001098 &d, "1", lysp_deviate_free(ctx.ctx, d); free(d); d = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001099
1100 TEST_DUP("add", "config", "true", "false");
1101 TEST_DUP("replace", "default", "int8", "uint8");
1102 TEST_DUP("add", "mandatory", "true", "false");
1103 TEST_DUP("add", "max-elements", "1", "2");
1104 TEST_DUP("add", "min-elements", "1", "2");
1105 TEST_DUP("replace", "type", "int8", "uint8");
1106 TEST_DUP("add", "units", "kilometers", "miles");
1107
1108 /* full contents */
1109 str = " not-supported {prefix:ext;} ...";
1110 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1111 assert_non_null(d);
1112 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001113 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001114 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;} ...";
1115 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1116 assert_non_null(d);
1117 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001118 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001119 str = " delete {units meters; must 1; must 2; unique x; unique y; default a; default b; prefix:ext;} ...";
1120 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1121 assert_non_null(d);
1122 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001123 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001124 str = " replace {type string; units meters; default a; config true; mandatory true; min-elements 1; max-elements 2; prefix:ext;} ...";
1125 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1126 assert_non_null(d);
1127 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001128 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001129
1130 /* invalid substatements */
1131#define TEST_NOT_SUP(DEV, STMT, VALUE) \
1132 str = " "DEV" {"STMT" "VALUE";}..."; \
1133 assert_int_equal(LY_EVALID, parse_deviate(&ctx, &str, &d)); \
1134 logbuf_assert("Deviate \""DEV"\" does not support keyword \""STMT"\". Line number 1."); \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001135 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001136
1137 TEST_NOT_SUP("not-supported", "units", "meters");
1138 TEST_NOT_SUP("not-supported", "must", "1");
1139 TEST_NOT_SUP("not-supported", "unique", "x");
1140 TEST_NOT_SUP("not-supported", "default", "a");
1141 TEST_NOT_SUP("not-supported", "config", "true");
1142 TEST_NOT_SUP("not-supported", "mandatory", "true");
1143 TEST_NOT_SUP("not-supported", "min-elements", "1");
1144 TEST_NOT_SUP("not-supported", "max-elements", "2");
1145 TEST_NOT_SUP("not-supported", "type", "string");
1146 TEST_NOT_SUP("add", "type", "string");
1147 TEST_NOT_SUP("delete", "config", "true");
1148 TEST_NOT_SUP("delete", "mandatory", "true");
1149 TEST_NOT_SUP("delete", "min-elements", "1");
1150 TEST_NOT_SUP("delete", "max-elements", "2");
1151 TEST_NOT_SUP("delete", "type", "string");
1152 TEST_NOT_SUP("replace", "must", "1");
1153 TEST_NOT_SUP("replace", "unique", "a");
1154
1155 str = " nonsence; ...";
1156 assert_int_equal(LY_EVALID, parse_deviate(&ctx, &str, &d));
1157 logbuf_assert("Invalid value \"nonsence\" of \"deviate\". Line number 1.");
1158 assert_null(d);
1159
1160#undef TEST_NOT_SUP
1161#undef TEST_DUP
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001162
1163 ly_ctx_destroy(ctx.ctx, NULL);
1164}
1165
Radek Krejci8c370832018-11-02 15:10:03 +01001166static void
1167test_container(void **state)
1168{
1169 (void) state; /* unused */
1170
Radek Krejci313d9902018-11-08 09:42:58 +01001171 struct lysp_module mod = {0};
1172 struct ly_parser_ctx ctx = {0};
Radek Krejci8c370832018-11-02 15:10:03 +01001173 struct lysp_node_container *c = NULL;
1174 const char *str;
1175
1176 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1177 assert_non_null(ctx.ctx);
1178 ctx.line = 1;
Radek Krejci313d9902018-11-08 09:42:58 +01001179 ctx.mod = &mod;
Radek Krejci027d5802018-11-14 16:57:28 +01001180 ctx.mod->version = 2; /* simulate YANG 1.1 */
Radek Krejci8c370832018-11-02 15:10:03 +01001181
1182 /* invalid cardinality */
1183#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1184 str = "cont {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1185 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c)); \
1186 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001187 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001188
1189 TEST_DUP("config", "true", "false");
1190 TEST_DUP("description", "text1", "text2");
1191 TEST_DUP("presence", "true", "false");
1192 TEST_DUP("reference", "1", "2");
1193 TEST_DUP("status", "current", "obsolete");
1194 TEST_DUP("when", "true", "false");
1195#undef TEST_DUP
1196
1197 /* full content */
Radek Krejci313d9902018-11-08 09:42:58 +01001198 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;}"
1199 "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 +01001200 assert_int_equal(LY_SUCCESS, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1201 assert_non_null(c);
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001202 assert_int_equal(LYS_CONTAINER, c->nodetype);
1203 assert_string_equal("cont", c->name);
Radek Krejci8c370832018-11-02 15:10:03 +01001204 assert_non_null(c->actions);
1205 assert_non_null(c->child);
1206 assert_string_equal("test", c->dsc);
1207 assert_non_null(c->exts);
1208 assert_non_null(c->groupings);
1209 assert_non_null(c->iffeatures);
1210 assert_non_null(c->musts);
1211 assert_non_null(c->notifs);
1212 assert_string_equal("true", c->presence);
1213 assert_string_equal("test", c->ref);
1214 assert_non_null(c->typedefs);
1215 assert_non_null(c->when);
1216 assert_null(c->parent);
1217 assert_null(c->next);
1218 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR, c->flags);
Radek Krejci313d9902018-11-08 09:42:58 +01001219 ly_set_erase(&ctx.tpdfs_nodes, NULL);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001220 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001221
1222 /* invalid */
1223 str = " cont {augment /root;} ...";
1224 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1225 logbuf_assert("Invalid keyword \"augment\" as a child of \"container\". Line number 1.");
Radek Krejci4f28eda2018-11-12 11:46:16 +01001226 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001227 str = " cont {nonsence true;} ...";
1228 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1229 logbuf_assert("Invalid character sequence \"nonsence\", expected a keyword. Line number 1.");
Radek Krejci4f28eda2018-11-12 11:46:16 +01001230 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001231
1232 ly_ctx_destroy(ctx.ctx, NULL);
1233}
1234
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001235static void
1236test_leaf(void **state)
1237{
1238 *state = test_leaf;
1239
1240 struct lysp_module mod = {0};
1241 struct ly_parser_ctx ctx = {0};
1242 struct lysp_node_leaf *l = NULL;
1243 const char *str;
1244
1245 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1246 assert_non_null(ctx.ctx);
1247 ctx.line = 1;
1248 ctx.mod = &mod;
1249 //ctx.mod->version = 2; /* simulate YANG 1.1 */
1250
1251 /* invalid cardinality */
1252#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1253 str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1254 assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l)); \
1255 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1256 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1257
1258 TEST_DUP("config", "true", "false");
1259 TEST_DUP("default", "x", "y");
1260 TEST_DUP("description", "text1", "text2");
1261 TEST_DUP("mandatory", "true", "false");
1262 TEST_DUP("reference", "1", "2");
1263 TEST_DUP("status", "current", "obsolete");
1264 TEST_DUP("units", "text1", "text2");
1265 TEST_DUP("when", "true", "false");
1266#undef TEST_DUP
1267
1268 /* full content - without mandatory which is mutual exclusive with default */
1269 str = "l {config false;default \"xxx\";description test;if-feature f;"
1270 "must 'expr';reference test;status current;type string; units yyy;when true;m:ext;} ...";
1271 assert_int_equal(LY_SUCCESS, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
1272 assert_non_null(l);
1273 assert_int_equal(LYS_LEAF, l->nodetype);
1274 assert_string_equal("l", l->name);
1275 assert_string_equal("test", l->dsc);
1276 assert_string_equal("xxx", l->dflt);
1277 assert_string_equal("yyy", l->units);
1278 assert_string_equal("string", l->type.name);
1279 assert_non_null(l->exts);
1280 assert_non_null(l->iffeatures);
1281 assert_non_null(l->musts);
1282 assert_string_equal("test", l->ref);
1283 assert_non_null(l->when);
1284 assert_null(l->parent);
1285 assert_null(l->next);
1286 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR, l->flags);
1287 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1288
1289 /* full content - now with mandatory */
1290 str = "l {mandatory true; type string;} ...";
1291 assert_int_equal(LY_SUCCESS, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
1292 assert_non_null(l);
1293 assert_int_equal(LYS_LEAF, l->nodetype);
1294 assert_string_equal("l", l->name);
1295 assert_string_equal("string", l->type.name);
1296 assert_int_equal(LYS_MAND_TRUE, l->flags);
1297 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1298
1299 /* invalid */
1300 str = " l {mandatory true; default xx; type string;} ...";
1301 assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
1302 logbuf_assert("Invalid combination of keywords \"mandatory\" and \"default\" as children of \"leaf\". Line number 1.");
1303 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1304
1305 str = " l {description \"missing type\";} ...";
1306 assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l));
1307 logbuf_assert("Missing mandatory keyword \"type\" as a child of \"leaf\". Line number 1.");
1308 lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL;
1309
1310 *state = NULL;
1311 ly_ctx_destroy(ctx.ctx, NULL);
1312}
1313
Radek Krejci80dd33e2018-09-26 15:57:18 +02001314int main(void)
1315{
1316 const struct CMUnitTest tests[] = {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001317 cmocka_unit_test_setup(test_helpers, logger_setup),
Radek Krejci80dd33e2018-09-26 15:57:18 +02001318 cmocka_unit_test_setup(test_comments, logger_setup),
Radek Krejciefd22f62018-09-27 11:47:58 +02001319 cmocka_unit_test_setup(test_arg, logger_setup),
Radek Krejcidcc7b322018-10-11 14:24:02 +02001320 cmocka_unit_test_setup(test_stmts, logger_setup),
Radek Krejci9fcacc12018-10-11 15:59:11 +02001321 cmocka_unit_test_setup(test_module, logger_setup),
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001322 cmocka_unit_test_setup(test_identity, logger_setup),
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001323 cmocka_unit_test_setup(test_feature, logger_setup),
1324 cmocka_unit_test_setup(test_deviation, logger_setup),
1325 cmocka_unit_test_setup(test_deviate, logger_setup),
Radek Krejci8c370832018-11-02 15:10:03 +01001326 cmocka_unit_test_setup(test_container, logger_setup),
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01001327 cmocka_unit_test_setup_teardown(test_leaf, logger_setup, logger_teardown),
Radek Krejci80dd33e2018-09-26 15:57:18 +02001328 };
1329
1330 return cmocka_run_group_tests(tests, NULL, NULL);
1331}