blob: 9ebfbecb8227c5c002b27edda11af95d39403628 [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
72void
73logbuf_clean(void)
74{
75 logbuf[0] = '\0';
76}
77
78#if ENABLE_LOGGER_CHECKING
79# define logbuf_assert(str) assert_string_equal(logbuf, str)
80#else
81# define logbuf_assert(str)
82#endif
83
Radek Krejci2c02f3e2018-10-16 10:54:38 +020084#define TEST_DUP_GENERIC(PREFIX, MEMBER, VALUE1, VALUE2, FUNC, RESULT, LINE, CLEANUP) \
85 str = PREFIX MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
86 assert_int_equal(LY_EVALID, FUNC(&ctx, &str, RESULT)); \
87 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number "LINE"."); \
88 CLEANUP
89
Radek Krejci44ceedc2018-10-02 15:54:31 +020090static void
91test_helpers(void **state)
92{
93 (void) state; /* unused */
94
95 const char *str;
Radek Krejci404251e2018-10-09 12:06:44 +020096 char *buf, *p;
Radek Krejci44ceedc2018-10-02 15:54:31 +020097 size_t len, size;
98 int prefix;
99 struct ly_parser_ctx ctx;
100 ctx.ctx = NULL;
101 ctx.line = 1;
102
103 /* storing into buffer */
104 str = "abcd";
105 buf = NULL;
106 size = len = 0;
107 assert_int_equal(LY_SUCCESS, buf_add_char(NULL, &str, 2, &buf, &size, &len));
108 assert_int_not_equal(0, size);
109 assert_int_equal(2, len);
110 assert_string_equal("cd", str);
111 assert_false(strncmp("ab", buf, 2));
112 free(buf);
Radek Krejci404251e2018-10-09 12:06:44 +0200113 buf = NULL;
114
115 /* invalid first characters */
116 len = 0;
117 str = "2invalid";
118 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1));
119 str = ".invalid";
120 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1));
121 str = "-invalid";
122 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1));
123 /* invalid following characters */
124 len = 3; /* number of characters read before the str content */
125 str = "!";
126 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1));
127 str = ":";
128 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1));
129 /* valid colon for prefixed identifiers */
130 len = size = 0;
131 p = NULL;
132 str = "x:id";
133 assert_int_equal(LY_SUCCESS, buf_store_char(&ctx, &str, Y_PREF_IDENTIF_ARG, &p, &len, &buf, &size, 0));
134 assert_int_equal(1, len);
135 assert_null(buf);
136 assert_string_equal(":id", str);
137 assert_int_equal('x', p[len - 1]);
138 assert_int_equal(LY_SUCCESS, buf_store_char(&ctx, &str, Y_PREF_IDENTIF_ARG, &p, &len, &buf, &size, 1));
139 assert_int_equal(2, len);
140 assert_string_equal("id", str);
141 assert_int_equal(':', p[len - 1]);
142 free(buf);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200143
144 /* checking identifiers */
145 assert_int_equal(LY_EVALID, check_identifierchar(&ctx, ':', 0, NULL));
146 logbuf_assert("Invalid identifier character ':'. Line number 1.");
147 assert_int_equal(LY_EVALID, check_identifierchar(&ctx, '#', 1, NULL));
148 logbuf_assert("Invalid identifier first character '#'. Line number 1.");
149
150 assert_int_equal(LY_SUCCESS, check_identifierchar(&ctx, 'a', 1, &prefix));
151 assert_int_equal(0, prefix);
152 assert_int_equal(LY_SUCCESS, check_identifierchar(&ctx, ':', 0, &prefix));
153 assert_int_equal(1, prefix);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200154 assert_int_equal(LY_EVALID, check_identifierchar(&ctx, ':', 0, &prefix));
155 assert_int_equal(1, prefix);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200156 assert_int_equal(LY_SUCCESS, check_identifierchar(&ctx, 'b', 0, &prefix));
157 assert_int_equal(2, prefix);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200158 /* second colon is invalid */
159 assert_int_equal(LY_EVALID, check_identifierchar(&ctx, ':', 0, &prefix));
160 logbuf_assert("Invalid identifier character ':'. Line number 1.");
Radek Krejci44ceedc2018-10-02 15:54:31 +0200161}
Radek Krejci80dd33e2018-09-26 15:57:18 +0200162
163static void
164test_comments(void **state)
165{
166 (void) state; /* unused */
167
Radek Krejci44ceedc2018-10-02 15:54:31 +0200168 struct ly_parser_ctx ctx;
Radek Krejci80dd33e2018-09-26 15:57:18 +0200169 const char *str, *p;
Radek Krejciefd22f62018-09-27 11:47:58 +0200170 char *word, *buf;
171 size_t len;
Radek Krejci80dd33e2018-09-26 15:57:18 +0200172
Radek Krejci44ceedc2018-10-02 15:54:31 +0200173 ctx.ctx = NULL;
174 ctx.line = 1;
175
Radek Krejciefd22f62018-09-27 11:47:58 +0200176 str = " // this is a text of / one * line */ comment\nargument";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200177 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200178 assert_string_equal("argument", word);
179 assert_null(buf);
180 assert_int_equal(8, len);
Radek Krejci80dd33e2018-09-26 15:57:18 +0200181
Radek Krejciefd22f62018-09-27 11:47:58 +0200182 str = "/* this is a \n * text // of / block * comment */\"arg\" + \"ume\" \n + \n \"nt\"";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200183 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200184 assert_string_equal("argument", word);
185 assert_ptr_equal(buf, word);
186 assert_int_equal(8, len);
187 free(word);
Radek Krejci80dd33e2018-09-26 15:57:18 +0200188
189 str = p = " this is one line comment on last line";
Radek Krejci44ceedc2018-10-02 15:54:31 +0200190 assert_int_equal(LY_SUCCESS, skip_comment(&ctx, &str, 1));
Radek Krejci80dd33e2018-09-26 15:57:18 +0200191 assert_true(str[0] == '\0');
192
193 str = p = " this is a not terminated comment x";
Radek Krejci44ceedc2018-10-02 15:54:31 +0200194 assert_int_equal(LY_EVALID, skip_comment(&ctx, &str, 2));
195 logbuf_assert("Unexpected end-of-file, non-terminated comment. Line number 5.");
Radek Krejci80dd33e2018-09-26 15:57:18 +0200196 assert_true(str[0] == '\0');
197}
198
Radek Krejciefd22f62018-09-27 11:47:58 +0200199static void
200test_arg(void **state)
201{
202 (void) state; /* unused */
203
Radek Krejci44ceedc2018-10-02 15:54:31 +0200204 struct ly_parser_ctx ctx;
Radek Krejciefd22f62018-09-27 11:47:58 +0200205 const char *str;
206 char *word, *buf;
207 size_t len;
208
Radek Krejci44ceedc2018-10-02 15:54:31 +0200209 ctx.ctx = NULL;
210 ctx.line = 1;
211
Radek Krejciefd22f62018-09-27 11:47:58 +0200212 /* missing argument */
213 str = ";";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200214 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_MAYBE_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200215 assert_null(word);
216
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200217 str = "{";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200218 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200219 logbuf_assert("Invalid character sequence \"{\", expected an argument. Line number 1.");
220
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200221 /* invalid escape sequence */
222 str = "\"\\s\"";
223 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200224 logbuf_assert("Double-quoted string unknown special character \'\\s\'. Line number 1.");
225 str = "\'\\s\'"; /* valid, since it is not an escape sequence in single quoted string */
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200226 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200227 assert_int_equal(2, len);
228 assert_string_equal("\\s\'", word);
229 assert_int_equal('\0', str[0]); /* input has been eaten */
230
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200231 /* invalid character after the argument */
232 str = "hello\"";
233 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
234 logbuf_assert("Invalid character sequence \"\"\", expected unquoted string character, optsep, semicolon or opening brace. Line number 1.");
235 str = "hello}";
236 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
237 logbuf_assert("Invalid character sequence \"}\", expected unquoted string character, optsep, semicolon or opening brace. Line number 1.");
238
239 str = "hello/x\t"; /* slash is not an invalid character */
240 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
241 assert_int_equal(7, len);
242 assert_string_equal("hello/x\t", word);
243
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200244 assert_null(buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200245
246 /* different quoting */
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200247 str = "hello ";
248 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200249 assert_null(buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200250 assert_int_equal(5, len);
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200251 assert_string_equal("hello ", word);
Radek Krejciefd22f62018-09-27 11:47:58 +0200252
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200253 str = "hello/*comment*/\n";
254 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200255 assert_null(buf);
256 assert_int_equal(5, len);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200257 assert_false(strncmp("hello", word, len));
258
259
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200260 str = "\"hello\\n\\t\\\"\\\\\";";
261 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200262 assert_null(buf);
263 assert_int_equal(9, len);
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200264 assert_string_equal("hello\\n\\t\\\"\\\\\";", word);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200265
266 ctx.indent = 14;
267 str = "\"hello \t\n\t\t world!\"";
268 /* - space and tabs before newline are stripped out
269 * - space and tabs after newline (indentation) are stripped out
270 */
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200271 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200272 assert_non_null(buf);
273 assert_ptr_equal(word, buf);
274 assert_int_equal(14, len);
275 assert_string_equal("hello\n world!", word);
276 free(buf);
277
278 ctx.indent = 14;
279 str = "\"hello\n \tworld!\"";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200280 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200281 assert_non_null(buf);
282 assert_ptr_equal(word, buf);
283 assert_int_equal(12, len);
284 assert_string_equal("hello\nworld!", word);
285 free(buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200286
287 str = "\'hello\'";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200288 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200289 assert_null(buf);
290 assert_int_equal(5, len);
291 assert_false(strncmp("hello", word, 5));
292
293 str = "\"hel\" +\t\n\"lo\"";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200294 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200295 assert_ptr_equal(word, buf);
296 assert_int_equal(5, len);
297 assert_string_equal("hello", word);
298 free(buf);
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200299 str = "\"hel\" +\t\nlo"; /* unquoted the second part */
300 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
301 logbuf_assert("Both string parts divided by '+' must be quoted. Line number 5.");
Radek Krejciefd22f62018-09-27 11:47:58 +0200302
303 str = "\'he\'\t\n+ \"llo\"";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200304 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200305 assert_ptr_equal(word, buf);
306 assert_int_equal(5, len);
307 assert_string_equal("hello", word);
308 free(buf);
309
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200310 str = " \t\n\"he\"+\'llo\'";
311 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200312 assert_ptr_equal(word, buf);
313 assert_int_equal(5, len);
314 assert_string_equal("hello", word);
315 free(buf);
316
Radek Krejci44ceedc2018-10-02 15:54:31 +0200317 /* missing argument */
318 str = ";";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200319 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
320 logbuf_assert("Invalid character sequence \";\", expected an argument. Line number 7.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200321}
322
323static void
324test_stmts(void **state)
325{
326 (void) state; /* unused */
327
328 struct ly_parser_ctx ctx;
329 const char *str, *p;
330 enum yang_keyword kw;
331 char *word;
332 size_t len;
333
334 ctx.ctx = NULL;
335 ctx.line = 1;
336
337 str = "\n// comment\n\tinput\t{";
338 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
339 assert_int_equal(YANG_INPUT, kw);
340 assert_int_equal(5, len);
341 assert_string_equal("input\t{", word);
342 assert_string_equal("\t{", str);
343
344 str = "\t /* comment */\t output\n\t{";
345 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
346 assert_int_equal(YANG_OUTPUT, kw);
347 assert_int_equal(6, len);
348 assert_string_equal("output\n\t{", word);
349 assert_string_equal("\n\t{", str);
350
351 str = "/input { "; /* invalid slash */
352 assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len));
353 logbuf_assert("Invalid identifier first character '/'. Line number 4.");
354
355 str = "not-a-statement-nor-extension { "; /* invalid identifier */
356 assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len));
357 logbuf_assert("Invalid character sequence \"not-a-statement-nor-extension\", expected a keyword. Line number 4.");
358
359 str = "path;"; /* missing sep after the keyword */
360 assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len));
361 logbuf_assert("Invalid character sequence \"path;\", expected a keyword followed by a separator. Line number 4.");
362
363 str = "action ";
364 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
365 assert_int_equal(YANG_ACTION, kw);
366 assert_int_equal(6, len);
367 str = "anydata ";
368 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
369 assert_int_equal(YANG_ANYDATA, kw);
370 assert_int_equal(7, len);
371 str = "anyxml ";
372 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
373 assert_int_equal(YANG_ANYXML, kw);
374 assert_int_equal(6, len);
375 str = "argument ";
376 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
377 assert_int_equal(YANG_ARGUMENT, kw);
378 assert_int_equal(8, len);
379 str = "augment ";
380 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
381 assert_int_equal(YANG_AUGMENT, kw);
382 assert_int_equal(7, len);
383 str = "base ";
384 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
385 assert_int_equal(YANG_BASE, kw);
386 assert_int_equal(4, len);
387 str = "belongs-to ";
388 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
389 assert_int_equal(YANG_BELONGS_TO, kw);
390 assert_int_equal(10, len);
391 str = "bit ";
392 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
393 assert_int_equal(YANG_BIT, kw);
394 assert_int_equal(3, len);
395 str = "case ";
396 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
397 assert_int_equal(YANG_CASE, kw);
398 assert_int_equal(4, len);
399 str = "choice ";
400 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
401 assert_int_equal(YANG_CHOICE, kw);
402 assert_int_equal(6, len);
403 str = "config ";
404 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
405 assert_int_equal(YANG_CONFIG, kw);
406 assert_int_equal(6, len);
407 str = "contact ";
408 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
409 assert_int_equal(YANG_CONTACT, kw);
410 assert_int_equal(7, len);
411 str = "container ";
412 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
413 assert_int_equal(YANG_CONTAINER, kw);
414 assert_int_equal(9, len);
415 str = "default ";
416 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
417 assert_int_equal(YANG_DEFAULT, kw);
418 assert_int_equal(7, len);
419 str = "description ";
420 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
421 assert_int_equal(YANG_DESCRIPTION, kw);
422 assert_int_equal(11, len);
423 str = "deviate ";
424 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
425 assert_int_equal(YANG_DEVIATE, kw);
426 assert_int_equal(7, len);
427 str = "deviation ";
428 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
429 assert_int_equal(YANG_DEVIATION, kw);
430 assert_int_equal(9, len);
431 str = "enum ";
432 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
433 assert_int_equal(YANG_ENUM, kw);
434 assert_int_equal(4, len);
435 str = "error-app-tag ";
436 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
437 assert_int_equal(YANG_ERROR_APP_TAG, kw);
438 assert_int_equal(13, len);
439 str = "error-message ";
440 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
441 assert_int_equal(YANG_ERROR_MESSAGE, kw);
442 assert_int_equal(13, len);
443 str = "extension ";
444 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
445 assert_int_equal(YANG_EXTENSION, kw);
446 assert_int_equal(9, len);
447 str = "feature ";
448 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
449 assert_int_equal(YANG_FEATURE, kw);
450 assert_int_equal(7, len);
451 str = "fraction-digits ";
452 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
453 assert_int_equal(YANG_FRACTION_DIGITS, kw);
454 assert_int_equal(15, len);
455 str = "grouping ";
456 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
457 assert_int_equal(YANG_GROUPING, kw);
458 assert_int_equal(8, len);
459 str = "identity ";
460 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
461 assert_int_equal(YANG_IDENTITY, kw);
462 assert_int_equal(8, len);
463 str = "if-feature ";
464 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
465 assert_int_equal(YANG_IF_FEATURE, kw);
466 assert_int_equal(10, len);
467 str = "import ";
468 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
469 assert_int_equal(YANG_IMPORT, kw);
470 assert_int_equal(6, len);
471 str = "include ";
472 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
473 assert_int_equal(YANG_INCLUDE, kw);
474 assert_int_equal(7, len);
475 str = "input{";
476 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
477 assert_int_equal(YANG_INPUT, kw);
478 assert_int_equal(5, len);
479 str = "key ";
480 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
481 assert_int_equal(YANG_KEY, kw);
482 assert_int_equal(3, len);
483 str = "leaf ";
484 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
485 assert_int_equal(YANG_LEAF, kw);
486 assert_int_equal(4, len);
487 str = "leaf-list ";
488 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
489 assert_int_equal(YANG_LEAF_LIST, kw);
490 assert_int_equal(9, len);
491 str = "length ";
492 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
493 assert_int_equal(YANG_LENGTH, kw);
494 assert_int_equal(6, len);
495 str = "list ";
496 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
497 assert_int_equal(YANG_LIST, kw);
498 assert_int_equal(4, len);
499 str = "mandatory ";
500 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
501 assert_int_equal(YANG_MANDATORY, kw);
502 assert_int_equal(9, len);
503 str = "max-elements ";
504 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
505 assert_int_equal(YANG_MAX_ELEMENTS, kw);
506 assert_int_equal(12, len);
507 str = "min-elements ";
508 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
509 assert_int_equal(YANG_MIN_ELEMENTS, kw);
510 assert_int_equal(12, len);
511 str = "modifier ";
512 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
513 assert_int_equal(YANG_MODIFIER, kw);
514 assert_int_equal(8, len);
515 str = "module ";
516 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
517 assert_int_equal(YANG_MODULE, kw);
518 assert_int_equal(6, len);
519 str = "must ";
520 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
521 assert_int_equal(YANG_MUST, kw);
522 assert_int_equal(4, len);
523 str = "namespace ";
524 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
525 assert_int_equal(YANG_NAMESPACE, kw);
526 assert_int_equal(9, len);
527 str = "notification ";
528 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
529 assert_int_equal(YANG_NOTIFICATION, kw);
530 assert_int_equal(12, len);
531 str = "ordered-by ";
532 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
533 assert_int_equal(YANG_ORDERED_BY, kw);
534 assert_int_equal(10, len);
535 str = "organization ";
536 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
537 assert_int_equal(YANG_ORGANIZATION, kw);
538 assert_int_equal(12, len);
539 str = "output ";
540 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
541 assert_int_equal(YANG_OUTPUT, kw);
542 assert_int_equal(6, len);
543 str = "path ";
544 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
545 assert_int_equal(YANG_PATH, kw);
546 assert_int_equal(4, len);
547 str = "pattern ";
548 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
549 assert_int_equal(YANG_PATTERN, kw);
550 assert_int_equal(7, len);
551 str = "position ";
552 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
553 assert_int_equal(YANG_POSITION, kw);
554 assert_int_equal(8, len);
555 str = "prefix ";
556 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
557 assert_int_equal(YANG_PREFIX, kw);
558 assert_int_equal(6, len);
559 str = "presence ";
560 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
561 assert_int_equal(YANG_PRESENCE, kw);
562 assert_int_equal(8, len);
563 str = "range ";
564 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
565 assert_int_equal(YANG_RANGE, kw);
566 assert_int_equal(5, len);
567 str = "reference ";
568 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
569 assert_int_equal(YANG_REFERENCE, kw);
570 assert_int_equal(9, len);
571 str = "refine ";
572 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
573 assert_int_equal(YANG_REFINE, kw);
574 assert_int_equal(6, len);
575 str = "require-instance ";
576 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
577 assert_int_equal(YANG_REQUIRE_INSTANCE, kw);
578 assert_int_equal(16, len);
579 str = "revision ";
580 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
581 assert_int_equal(YANG_REVISION, kw);
582 assert_int_equal(8, len);
583 str = "revision-date ";
584 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
585 assert_int_equal(YANG_REVISION_DATE, kw);
586 assert_int_equal(13, len);
587 str = "rpc ";
588 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
589 assert_int_equal(YANG_RPC, kw);
590 assert_int_equal(3, len);
591 str = "status ";
592 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
593 assert_int_equal(YANG_STATUS, kw);
594 assert_int_equal(6, len);
595 str = "submodule ";
596 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
597 assert_int_equal(YANG_SUBMODULE, kw);
598 assert_int_equal(9, len);
599 str = "type ";
600 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
601 assert_int_equal(YANG_TYPE, kw);
602 assert_int_equal(4, len);
603 str = "typedef ";
604 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
605 assert_int_equal(YANG_TYPEDEF, kw);
606 assert_int_equal(7, len);
607 str = "unique ";
608 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
609 assert_int_equal(YANG_UNIQUE, kw);
610 assert_int_equal(6, len);
611 str = "units ";
612 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
613 assert_int_equal(YANG_UNITS, kw);
614 assert_int_equal(5, len);
615 str = "uses ";
616 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
617 assert_int_equal(YANG_USES, kw);
618 assert_int_equal(4, len);
619 str = "value ";
620 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
621 assert_int_equal(YANG_VALUE, kw);
622 assert_int_equal(5, len);
623 str = "when ";
624 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
625 assert_int_equal(YANG_WHEN, kw);
626 assert_int_equal(4, len);
627 str = "yang-version ";
628 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
629 assert_int_equal(YANG_YANG_VERSION, kw);
630 assert_int_equal(12, len);
631 str = "yin-element ";
632 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
633 assert_int_equal(YANG_YIN_ELEMENT, kw);
634 assert_int_equal(11, len);
Radek Krejci626df482018-10-11 15:06:31 +0200635 str = ";config false;";
636 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
637 assert_int_equal(YANG_SEMICOLON, kw);
638 assert_int_equal(1, len);
639 assert_string_equal("config false;", str);
640 str = "{ config false;";
641 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
642 assert_int_equal(YANG_LEFT_BRACE, kw);
643 assert_int_equal(1, len);
644 assert_string_equal(" config false;", str);
645 str = "}";
646 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
647 assert_int_equal(YANG_RIGHT_BRACE, kw);
648 assert_int_equal(1, len);
649 assert_string_equal("", str);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200650
651 /* geenric extension */
652 str = p = "nacm:default-deny-write;";
653 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
654 assert_int_equal(YANG_CUSTOM, kw);
655 assert_int_equal(23, len);
656 assert_ptr_equal(p, word);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200657}
Radek Krejci44ceedc2018-10-02 15:54:31 +0200658
Radek Krejci9fcacc12018-10-11 15:59:11 +0200659static struct lysp_module *
Radek Krejci09306362018-10-15 15:26:01 +0200660mod_renew(struct ly_parser_ctx *ctx, struct lysp_module *mod, uint8_t submodule)
Radek Krejci9fcacc12018-10-11 15:59:11 +0200661{
662 lysp_module_free(mod);
663 mod = calloc(1, sizeof *mod);
664 mod->ctx = ctx->ctx;
Radek Krejci09306362018-10-15 15:26:01 +0200665 mod->submodule = submodule;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200666 assert_non_null(mod);
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100667 ctx->mod = mod;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200668 return mod;
669}
670
Radek Krejcid33273d2018-10-25 14:55:52 +0200671static LY_ERR test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
672 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
673 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
674{
675 *module_data = user_data;
676 *format = LYS_IN_YANG;
677 *free_module_data = NULL;
678 return LY_SUCCESS;
679}
680
Radek Krejci9fcacc12018-10-11 15:59:11 +0200681static void
682test_module(void **state)
683{
684 (void) state; /* unused */
685
686 struct ly_parser_ctx ctx;
687 struct lysp_module *mod;
688 const char *str;
689
690 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
691 assert_non_null(ctx.ctx);
692 ctx.line = 1;
Radek Krejcia042ea12018-10-13 07:52:15 +0200693 ctx.indent = 0;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200694
Radek Krejci09306362018-10-15 15:26:01 +0200695 mod = mod_renew(&ctx, NULL, 0);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200696
697 /* missing mandatory substatements */
698 str = " name {}";
699 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
700 assert_string_equal("name", mod->name);
701 logbuf_assert("Missing mandatory keyword \"namespace\" as a child of \"module\". Line number 1.");
Radek Krejci09306362018-10-15 15:26:01 +0200702 mod = mod_renew(&ctx, mod, 0);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200703
704 str = " name {namespace urn:x;}";
705 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
706 assert_string_equal("urn:x", mod->ns);
707 logbuf_assert("Missing mandatory keyword \"prefix\" as a child of \"module\". Line number 1.");
Radek Krejci09306362018-10-15 15:26:01 +0200708 mod = mod_renew(&ctx, mod, 0);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200709
710 str = " name {namespace urn:x;prefix \"x\";}";
711 assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod));
712 assert_string_equal("x", mod->prefix);
Radek Krejci09306362018-10-15 15:26:01 +0200713 mod = mod_renew(&ctx, mod, 0);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200714
Radek Krejci027d5802018-11-14 16:57:28 +0100715#define SCHEMA_BEGINNING " name {yang-version 1.1;namespace urn:x;prefix \"x\";"
716#define SCHEMA_BEGINNING2 " name {namespace urn:x;prefix \"x\";"
Radek Krejcia042ea12018-10-13 07:52:15 +0200717#define TEST_NODE(NODETYPE, INPUT, NAME) \
718 str = SCHEMA_BEGINNING INPUT; \
719 assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod)); \
720 assert_non_null(mod->data); \
721 assert_int_equal(NODETYPE, mod->data->nodetype); \
722 assert_string_equal(NAME, mod->data->name); \
Radek Krejci09306362018-10-15 15:26:01 +0200723 mod = mod_renew(&ctx, mod, 0);
Radek Krejcia042ea12018-10-13 07:52:15 +0200724#define TEST_GENERIC(INPUT, TARGET, TEST) \
725 str = SCHEMA_BEGINNING INPUT; \
726 assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod)); \
727 assert_non_null(TARGET); \
728 TEST; \
Radek Krejci09306362018-10-15 15:26:01 +0200729 mod = mod_renew(&ctx, mod, 0);
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200730#define TEST_DUP(MEMBER, VALUE1, VALUE2, LINE, SUBMODULE) \
731 TEST_DUP_GENERIC(SCHEMA_BEGINNING, MEMBER, VALUE1, VALUE2, \
732 parse_sub_module, mod, LINE, mod = mod_renew(&ctx, mod, SUBMODULE))
Radek Krejcia042ea12018-10-13 07:52:15 +0200733
734 /* duplicated namespace, prefix */
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200735 TEST_DUP("namespace", "y", "z", "1", 0);
736 TEST_DUP("prefix", "y", "z", "1", 0);
737 TEST_DUP("contact", "a", "b", "1", 0);
738 TEST_DUP("description", "a", "b", "1", 0);
739 TEST_DUP("organization", "a", "b", "1", 0);
740 TEST_DUP("reference", "a", "b", "1", 0);
Radek Krejcia042ea12018-10-13 07:52:15 +0200741
Radek Krejci70853c52018-10-15 14:46:16 +0200742 /* not allowed in module (submodule-specific) */
743 str = SCHEMA_BEGINNING "belongs-to master {prefix m;}}";
744 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
745 logbuf_assert("Invalid keyword \"belongs-to\" as a child of \"module\". Line number 1.");
Radek Krejci09306362018-10-15 15:26:01 +0200746 mod = mod_renew(&ctx, mod, 0);
Radek Krejci70853c52018-10-15 14:46:16 +0200747
Radek Krejcia042ea12018-10-13 07:52:15 +0200748 /* anydata */
749 TEST_NODE(LYS_ANYDATA, "anydata test;}", "test");
750 /* anyxml */
751 TEST_NODE(LYS_ANYXML, "anyxml test;}", "test");
752 /* augment */
753 TEST_GENERIC("augment /somepath;}", mod->augments,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200754 assert_string_equal("/somepath", mod->augments[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200755 /* choice */
756 TEST_NODE(LYS_CHOICE, "choice test;}", "test");
757 /* contact 0..1 */
758 TEST_GENERIC("contact \"firstname\" + \n\t\" surname\";}", mod->contact,
759 assert_string_equal("firstname surname", mod->contact));
760 /* container */
761 TEST_NODE(LYS_CONTAINER, "container test;}", "test");
762 /* description 0..1 */
763 TEST_GENERIC("description \'some description\';}", mod->dsc,
764 assert_string_equal("some description", mod->dsc));
765 /* deviation */
766 TEST_GENERIC("deviation /somepath {deviate not-supported;}}", mod->deviations,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200767 assert_string_equal("/somepath", mod->deviations[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200768 /* extension */
769 TEST_GENERIC("extension test;}", mod->extensions,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200770 assert_string_equal("test", mod->extensions[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200771 /* feature */
772 TEST_GENERIC("feature test;}", mod->features,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200773 assert_string_equal("test", mod->features[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200774 /* grouping */
775 TEST_GENERIC("grouping grp;}", mod->groupings,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200776 assert_string_equal("grp", mod->groupings[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200777 /* identity */
778 TEST_GENERIC("identity test;}", mod->identities,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200779 assert_string_equal("test", mod->identities[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200780 /* import */
Radek Krejci086c7132018-10-26 15:29:04 +0200781 ly_ctx_set_module_imp_clb(ctx.ctx, test_imp_clb, "module zzz { namespace urn:zzz; prefix z;}");
782 TEST_GENERIC("import zzz {prefix z;}}", mod->imports,
783 assert_string_equal("zzz", mod->imports[0].name));
Radek Krejci70853c52018-10-15 14:46:16 +0200784
Radek Krejcia042ea12018-10-13 07:52:15 +0200785 /* import - prefix collision */
Radek Krejci086c7132018-10-26 15:29:04 +0200786 str = SCHEMA_BEGINNING "import zzz {prefix x;}}";
Radek Krejcia042ea12018-10-13 07:52:15 +0200787 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
Radek Krejci70853c52018-10-15 14:46:16 +0200788 logbuf_assert("Prefix \"x\" already used as module prefix. Line number 2.");
Radek Krejci09306362018-10-15 15:26:01 +0200789 mod = mod_renew(&ctx, mod, 0);
Radek Krejci086c7132018-10-26 15:29:04 +0200790 str = SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix y;}}";
Radek Krejci86d106e2018-10-18 09:53:19 +0200791 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
Radek Krejci086c7132018-10-26 15:29:04 +0200792 logbuf_assert("Prefix \"y\" already used to import \"zzz\" module. Line number 2.");
Radek Krejci86d106e2018-10-18 09:53:19 +0200793 mod = mod_renew(&ctx, mod, 0);
Radek Krejci086c7132018-10-26 15:29:04 +0200794 str = "module" SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix z;}}";
795 assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
796 assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx));
797 logbuf_assert("Single revision of the module \"zzz\" referred twice.");
Radek Krejci70853c52018-10-15 14:46:16 +0200798
Radek Krejcia042ea12018-10-13 07:52:15 +0200799 /* include */
Radek Krejci9ed7a192018-10-31 16:23:51 +0100800 store = 1;
Radek Krejcid33273d2018-10-25 14:55:52 +0200801 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 +0200802 str = "module" SCHEMA_BEGINNING "include xxx;}";
803 assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
804 assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx));
805 logbuf_assert("Included \"xxx\" schema from \"name\" is actually not a submodule.");
Radek Krejci9ed7a192018-10-31 16:23:51 +0100806 store = -1;
Radek Krejcid33273d2018-10-25 14:55:52 +0200807
Radek Krejci9ed7a192018-10-31 16:23:51 +0100808 store = 1;
Radek Krejci313d9902018-11-08 09:42:58 +0100809 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 +0200810 str = "module" SCHEMA_BEGINNING "include xxx;}";
811 assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
812 assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx));
813 logbuf_assert("Included \"xxx\" submodule from \"name\" belongs-to a different module \"wrong-name\".");
Radek Krejci9ed7a192018-10-31 16:23:51 +0100814 store = -1;
Radek Krejcid33273d2018-10-25 14:55:52 +0200815
Radek Krejci313d9902018-11-08 09:42:58 +0100816 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 +0200817 TEST_GENERIC("include xxx;}", mod->includes,
Radek Krejci086c7132018-10-26 15:29:04 +0200818 assert_string_equal("xxx", mod->includes[0].name));
Radek Krejcid33273d2018-10-25 14:55:52 +0200819
Radek Krejcia042ea12018-10-13 07:52:15 +0200820 /* leaf */
821 TEST_NODE(LYS_LEAF, "leaf test {type string;}}", "test");
822 /* leaf-list */
823 TEST_NODE(LYS_LEAFLIST, "leaf-list test {type string;}}", "test");
824 /* list */
825 TEST_NODE(LYS_LIST, "list test {key a;leaf a {type string;}}}", "test");
826 /* notification */
827 TEST_GENERIC("notification test;}", mod->notifs,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200828 assert_string_equal("test", mod->notifs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200829 /* organization 0..1 */
830 TEST_GENERIC("organization \"CESNET a.l.e.\";}", mod->org,
831 assert_string_equal("CESNET a.l.e.", mod->org));
832 /* reference 0..1 */
833 TEST_GENERIC("reference RFC7950;}", mod->ref,
834 assert_string_equal("RFC7950", mod->ref));
835 /* revision */
836 TEST_GENERIC("revision 2018-10-12;}", mod->revs,
Radek Krejcib7db73a2018-10-24 14:18:40 +0200837 assert_string_equal("2018-10-12", mod->revs[0].date));
Radek Krejcia042ea12018-10-13 07:52:15 +0200838 /* rpc */
839 TEST_GENERIC("rpc test;}", mod->rpcs,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200840 assert_string_equal("test", mod->rpcs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200841 /* typedef */
842 TEST_GENERIC("typedef test{type string;}}", mod->typedefs,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200843 assert_string_equal("test", mod->typedefs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200844 /* uses */
845 TEST_NODE(LYS_USES, "uses test;}", "test");
846 /* yang-version */
Radek Krejci027d5802018-11-14 16:57:28 +0100847 str = SCHEMA_BEGINNING2 "\n\tyang-version 10;}";
Radek Krejcia042ea12018-10-13 07:52:15 +0200848 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
849 logbuf_assert("Invalid value \"10\" of \"yang-version\". Line number 3.");
Radek Krejci09306362018-10-15 15:26:01 +0200850 mod = mod_renew(&ctx, mod, 0);
Radek Krejci027d5802018-11-14 16:57:28 +0100851 str = SCHEMA_BEGINNING2 "yang-version 1.0;yang-version 1.1;}";
Radek Krejcia042ea12018-10-13 07:52:15 +0200852 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
853 logbuf_assert("Duplicate keyword \"yang-version\". Line number 3.");
Radek Krejci09306362018-10-15 15:26:01 +0200854 mod = mod_renew(&ctx, mod, 0);
Radek Krejci027d5802018-11-14 16:57:28 +0100855 str = SCHEMA_BEGINNING2 "yang-version 1.0;}";
Radek Krejcia042ea12018-10-13 07:52:15 +0200856 assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod));
857 assert_int_equal(1, mod->version);
Radek Krejci09306362018-10-15 15:26:01 +0200858 mod = mod_renew(&ctx, mod, 0);
Radek Krejci027d5802018-11-14 16:57:28 +0100859 str = SCHEMA_BEGINNING2 "yang-version \"1.1\";}";
Radek Krejcia042ea12018-10-13 07:52:15 +0200860 assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod));
861 assert_int_equal(2, mod->version);
Radek Krejci09306362018-10-15 15:26:01 +0200862 mod = mod_renew(&ctx, mod, 0);
863
Radek Krejci156ccaf2018-10-15 15:49:17 +0200864 /* extensions */
865 TEST_GENERIC("prefix:test;}", mod->exts,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200866 assert_string_equal("prefix:test", mod->exts[0].name);
867 assert_int_equal(LYEXT_SUBSTMT_SELF, mod->exts[0].insubstmt));
Radek Krejci156ccaf2018-10-15 15:49:17 +0200868 mod = mod_renew(&ctx, mod, 0);
869
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200870 /* invalid substatement */
871 str = SCHEMA_BEGINNING "must false;}";
872 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
873 logbuf_assert("Invalid keyword \"must\" as a child of \"module\". Line number 3.");
874 mod = mod_renew(&ctx, mod, 0);
875
Radek Krejci09306362018-10-15 15:26:01 +0200876 /* submodule */
877 mod->submodule = 1;
878
879 /* missing mandatory substatements */
880 str = " subname {}";
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100881 lydict_remove(ctx.ctx, mod->name);
Radek Krejci09306362018-10-15 15:26:01 +0200882 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
883 assert_string_equal("subname", mod->name);
884 logbuf_assert("Missing mandatory keyword \"belongs-to\" as a child of \"submodule\". Line number 3.");
885 mod = mod_renew(&ctx, mod, 1);
886
Radek Krejci313d9902018-11-08 09:42:58 +0100887 str = " subname {belongs-to name {prefix x;}}";
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100888 lydict_remove(ctx.ctx, mod->name);
Radek Krejci09306362018-10-15 15:26:01 +0200889 assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod));
890 assert_string_equal("name", mod->belongsto);
891 mod = mod_renew(&ctx, mod, 1);
892
893#undef SCHEMA_BEGINNING
Radek Krejci313d9902018-11-08 09:42:58 +0100894#define SCHEMA_BEGINNING " subname {belongs-to name {prefix x;}"
Radek Krejci09306362018-10-15 15:26:01 +0200895
896 /* duplicated namespace, prefix */
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200897 TEST_DUP("belongs-to", "module1", "module2", "3", 1);
Radek Krejci09306362018-10-15 15:26:01 +0200898
899 /* not allowed in submodule (module-specific) */
900 str = SCHEMA_BEGINNING "namespace \"urn:z\";}";
901 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
902 logbuf_assert("Invalid keyword \"namespace\" as a child of \"submodule\". Line number 3.");
903 mod = mod_renew(&ctx, mod, 1);
904 str = SCHEMA_BEGINNING "prefix m;}}";
905 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
906 logbuf_assert("Invalid keyword \"prefix\" as a child of \"submodule\". Line number 3.");
907 mod = mod_renew(&ctx, mod, 1);
Radek Krejcia042ea12018-10-13 07:52:15 +0200908
909#undef TEST_GENERIC
910#undef TEST_NODE
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200911#undef TEST_DUP
Radek Krejcia042ea12018-10-13 07:52:15 +0200912#undef SCHEMA_BEGINNING
913
Radek Krejci9fcacc12018-10-11 15:59:11 +0200914 lysp_module_free(mod);
915 ly_ctx_destroy(ctx.ctx, NULL);
Radek Krejciefd22f62018-09-27 11:47:58 +0200916}
917
Radek Krejci4c6d9bd2018-10-15 16:43:06 +0200918static void
919test_identity(void **state)
920{
921 (void) state; /* unused */
922
923 struct ly_parser_ctx ctx;
Radek Krejci027d5802018-11-14 16:57:28 +0100924 struct lysp_module *mod = NULL, m = {0};
Radek Krejci4c6d9bd2018-10-15 16:43:06 +0200925 struct lysp_ident *ident = NULL;
926 const char *str;
Radek Krejci4c6d9bd2018-10-15 16:43:06 +0200927
928 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
929 assert_non_null(ctx.ctx);
930 ctx.line = 1;
931 ctx.indent = 0;
Radek Krejci027d5802018-11-14 16:57:28 +0100932 ctx.mod = &m;
933 ctx.mod->version = 2; /* simulate YANG 1.1 */
Radek Krejci4c6d9bd2018-10-15 16:43:06 +0200934
935 /* invalid cardinality */
936#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200937 TEST_DUP_GENERIC(" test {", MEMBER, VALUE1, VALUE2, parse_identity, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +0200938 &ident, "1", FREE_ARRAY(ctx.ctx, ident, lysp_ident_free); ident = NULL)
Radek Krejci4c6d9bd2018-10-15 16:43:06 +0200939
940 TEST_DUP("description", "a", "b");
941 TEST_DUP("reference", "a", "b");
942 TEST_DUP("status", "current", "obsolete");
943
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200944 /* full content */
945 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 +0200946 assert_int_equal(LY_SUCCESS, parse_identity(&ctx, &str, &ident));
947 assert_non_null(ident);
948 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +0200949 FREE_ARRAY(ctx.ctx, ident, lysp_ident_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200950 ident = NULL;
951
952 /* invalid substatement */
953 str = " test {organization XXX;}";
954 assert_int_equal(LY_EVALID, parse_identity(&ctx, &str, &ident));
955 logbuf_assert("Invalid keyword \"organization\" as a child of \"identity\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +0200956 FREE_ARRAY(ctx.ctx, ident, lysp_ident_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200957 ident = NULL;
958
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100959 /* identity duplication */
960 str = "module a {namespace urn:a; prefix a; identity a; identity a;}";
Radek Krejci313d9902018-11-08 09:42:58 +0100961 assert_int_equal(LY_EVALID, yang_parse(&ctx, str, &mod));
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100962 logbuf_assert("Duplicate identifier \"a\" of identity statement. Line number 1.");
963 assert_null(mod);
964
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200965#undef TEST_DUP
966
967 ly_ctx_destroy(ctx.ctx, NULL);
968}
969
970static void
971test_feature(void **state)
972{
973 (void) state; /* unused */
974
975 struct ly_parser_ctx ctx;
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100976 struct lysp_module *mod = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200977 struct lysp_feature *features = NULL;
978 const char *str;
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200979
980 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
981 assert_non_null(ctx.ctx);
982 ctx.line = 1;
983 ctx.indent = 0;
984
985 /* invalid cardinality */
986#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
987 TEST_DUP_GENERIC(" test {", MEMBER, VALUE1, VALUE2, parse_feature, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +0200988 &features, "1", FREE_ARRAY(ctx.ctx, features, lysp_feature_free); features = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200989
990 TEST_DUP("description", "a", "b");
991 TEST_DUP("reference", "a", "b");
992 TEST_DUP("status", "current", "obsolete");
993
994 /* full content */
995 str = " test {description text;reference \'another text\';status current; if-feature x;if-feature y;prefix:ext;} ...";
996 assert_int_equal(LY_SUCCESS, parse_feature(&ctx, &str, &features));
997 assert_non_null(features);
998 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +0200999 FREE_ARRAY(ctx.ctx, features, lysp_feature_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001000 features = NULL;
1001
1002 /* invalid substatement */
1003 str = " test {organization XXX;}";
1004 assert_int_equal(LY_EVALID, parse_feature(&ctx, &str, &features));
1005 logbuf_assert("Invalid keyword \"organization\" as a child of \"feature\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001006 FREE_ARRAY(ctx.ctx, features, lysp_feature_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001007 features = NULL;
1008
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001009 /* feature duplication */
1010 str = "module a {namespace urn:a; prefix a; feature a; feature a;}";
Radek Krejci313d9902018-11-08 09:42:58 +01001011 assert_int_equal(LY_EVALID, yang_parse(&ctx, str, &mod));
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001012 logbuf_assert("Duplicate identifier \"a\" of feature statement. Line number 1.");
1013 assert_null(mod);
1014
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001015#undef TEST_DUP
1016
1017 ly_ctx_destroy(ctx.ctx, NULL);
1018}
1019
1020static void
1021test_deviation(void **state)
1022{
1023 (void) state; /* unused */
1024
1025 struct ly_parser_ctx ctx;
1026 struct lysp_deviation *d = NULL;
1027 const char *str;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001028
1029 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1030 assert_non_null(ctx.ctx);
1031 ctx.line = 1;
1032 ctx.indent = 0;
1033
1034 /* invalid cardinality */
1035#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1036 TEST_DUP_GENERIC(" test {deviate not-supported;", MEMBER, VALUE1, VALUE2, parse_deviation, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001037 &d, "1", FREE_ARRAY(ctx.ctx, d, lysp_deviation_free); d = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001038
1039 TEST_DUP("description", "a", "b");
1040 TEST_DUP("reference", "a", "b");
1041
1042 /* full content */
1043 str = " test {deviate not-supported;description text;reference \'another text\';prefix:ext;} ...";
1044 assert_int_equal(LY_SUCCESS, parse_deviation(&ctx, &str, &d));
1045 assert_non_null(d);
1046 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001047 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001048 d = NULL;
1049
1050 /* missing mandatory substatement */
1051 str = " test {description text;}";
1052 assert_int_equal(LY_EVALID, parse_deviation(&ctx, &str, &d));
1053 logbuf_assert("Missing mandatory keyword \"deviate\" as a child of \"deviation\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001054 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001055 d = NULL;
1056
1057 /* invalid substatement */
1058 str = " test {deviate not-supported; status obsolete;}";
1059 assert_int_equal(LY_EVALID, parse_deviation(&ctx, &str, &d));
1060 logbuf_assert("Invalid keyword \"status\" as a child of \"deviation\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001061 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001062 d = NULL;
1063
1064#undef TEST_DUP
1065
1066 ly_ctx_destroy(ctx.ctx, NULL);
1067}
1068
1069static void
1070test_deviate(void **state)
1071{
1072 (void) state; /* unused */
1073
1074 struct ly_parser_ctx ctx;
1075 struct lysp_deviate *d = NULL;
1076 const char *str;
1077
1078 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1079 assert_non_null(ctx.ctx);
1080 ctx.line = 1;
1081 ctx.indent = 0;
1082
1083 /* invalid cardinality */
1084#define TEST_DUP(TYPE, MEMBER, VALUE1, VALUE2) \
1085 TEST_DUP_GENERIC(TYPE" {", MEMBER, VALUE1, VALUE2, parse_deviate, \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001086 &d, "1", lysp_deviate_free(ctx.ctx, d); free(d); d = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001087
1088 TEST_DUP("add", "config", "true", "false");
1089 TEST_DUP("replace", "default", "int8", "uint8");
1090 TEST_DUP("add", "mandatory", "true", "false");
1091 TEST_DUP("add", "max-elements", "1", "2");
1092 TEST_DUP("add", "min-elements", "1", "2");
1093 TEST_DUP("replace", "type", "int8", "uint8");
1094 TEST_DUP("add", "units", "kilometers", "miles");
1095
1096 /* full contents */
1097 str = " not-supported {prefix:ext;} ...";
1098 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1099 assert_non_null(d);
1100 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001101 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001102 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;} ...";
1103 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1104 assert_non_null(d);
1105 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001106 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001107 str = " delete {units meters; must 1; must 2; unique x; unique y; default a; default b; prefix:ext;} ...";
1108 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1109 assert_non_null(d);
1110 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001111 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001112 str = " replace {type string; units meters; default a; config true; mandatory true; min-elements 1; max-elements 2; prefix:ext;} ...";
1113 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1114 assert_non_null(d);
1115 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001116 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001117
1118 /* invalid substatements */
1119#define TEST_NOT_SUP(DEV, STMT, VALUE) \
1120 str = " "DEV" {"STMT" "VALUE";}..."; \
1121 assert_int_equal(LY_EVALID, parse_deviate(&ctx, &str, &d)); \
1122 logbuf_assert("Deviate \""DEV"\" does not support keyword \""STMT"\". Line number 1."); \
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
1125 TEST_NOT_SUP("not-supported", "units", "meters");
1126 TEST_NOT_SUP("not-supported", "must", "1");
1127 TEST_NOT_SUP("not-supported", "unique", "x");
1128 TEST_NOT_SUP("not-supported", "default", "a");
1129 TEST_NOT_SUP("not-supported", "config", "true");
1130 TEST_NOT_SUP("not-supported", "mandatory", "true");
1131 TEST_NOT_SUP("not-supported", "min-elements", "1");
1132 TEST_NOT_SUP("not-supported", "max-elements", "2");
1133 TEST_NOT_SUP("not-supported", "type", "string");
1134 TEST_NOT_SUP("add", "type", "string");
1135 TEST_NOT_SUP("delete", "config", "true");
1136 TEST_NOT_SUP("delete", "mandatory", "true");
1137 TEST_NOT_SUP("delete", "min-elements", "1");
1138 TEST_NOT_SUP("delete", "max-elements", "2");
1139 TEST_NOT_SUP("delete", "type", "string");
1140 TEST_NOT_SUP("replace", "must", "1");
1141 TEST_NOT_SUP("replace", "unique", "a");
1142
1143 str = " nonsence; ...";
1144 assert_int_equal(LY_EVALID, parse_deviate(&ctx, &str, &d));
1145 logbuf_assert("Invalid value \"nonsence\" of \"deviate\". Line number 1.");
1146 assert_null(d);
1147
1148#undef TEST_NOT_SUP
1149#undef TEST_DUP
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001150
1151 ly_ctx_destroy(ctx.ctx, NULL);
1152}
1153
Radek Krejci8c370832018-11-02 15:10:03 +01001154static void
1155test_container(void **state)
1156{
1157 (void) state; /* unused */
1158
Radek Krejci313d9902018-11-08 09:42:58 +01001159 struct lysp_module mod = {0};
1160 struct ly_parser_ctx ctx = {0};
Radek Krejci8c370832018-11-02 15:10:03 +01001161 struct lysp_node_container *c = NULL;
1162 const char *str;
1163
1164 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1165 assert_non_null(ctx.ctx);
1166 ctx.line = 1;
Radek Krejci313d9902018-11-08 09:42:58 +01001167 ctx.mod = &mod;
Radek Krejci027d5802018-11-14 16:57:28 +01001168 ctx.mod->version = 2; /* simulate YANG 1.1 */
Radek Krejci8c370832018-11-02 15:10:03 +01001169
1170 /* invalid cardinality */
1171#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1172 str = "cont {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1173 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c)); \
1174 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001175 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001176
1177 TEST_DUP("config", "true", "false");
1178 TEST_DUP("description", "text1", "text2");
1179 TEST_DUP("presence", "true", "false");
1180 TEST_DUP("reference", "1", "2");
1181 TEST_DUP("status", "current", "obsolete");
1182 TEST_DUP("when", "true", "false");
1183#undef TEST_DUP
1184
1185 /* full content */
Radek Krejci313d9902018-11-08 09:42:58 +01001186 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;}"
1187 "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 +01001188 assert_int_equal(LY_SUCCESS, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1189 assert_non_null(c);
1190 assert_non_null(c->actions);
1191 assert_non_null(c->child);
1192 assert_string_equal("test", c->dsc);
1193 assert_non_null(c->exts);
1194 assert_non_null(c->groupings);
1195 assert_non_null(c->iffeatures);
1196 assert_non_null(c->musts);
1197 assert_non_null(c->notifs);
1198 assert_string_equal("true", c->presence);
1199 assert_string_equal("test", c->ref);
1200 assert_non_null(c->typedefs);
1201 assert_non_null(c->when);
1202 assert_null(c->parent);
1203 assert_null(c->next);
1204 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR, c->flags);
Radek Krejci313d9902018-11-08 09:42:58 +01001205 ly_set_erase(&ctx.tpdfs_nodes, NULL);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001206 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001207
1208 /* invalid */
1209 str = " cont {augment /root;} ...";
1210 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1211 logbuf_assert("Invalid keyword \"augment\" as a child of \"container\". Line number 1.");
Radek Krejci4f28eda2018-11-12 11:46:16 +01001212 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001213 str = " cont {nonsence true;} ...";
1214 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1215 logbuf_assert("Invalid character sequence \"nonsence\", expected a keyword. Line number 1.");
Radek Krejci4f28eda2018-11-12 11:46:16 +01001216 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001217
1218 ly_ctx_destroy(ctx.ctx, NULL);
1219}
1220
Radek Krejci80dd33e2018-09-26 15:57:18 +02001221int main(void)
1222{
1223 const struct CMUnitTest tests[] = {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001224 cmocka_unit_test_setup(test_helpers, logger_setup),
Radek Krejci80dd33e2018-09-26 15:57:18 +02001225 cmocka_unit_test_setup(test_comments, logger_setup),
Radek Krejciefd22f62018-09-27 11:47:58 +02001226 cmocka_unit_test_setup(test_arg, logger_setup),
Radek Krejcidcc7b322018-10-11 14:24:02 +02001227 cmocka_unit_test_setup(test_stmts, logger_setup),
Radek Krejci9fcacc12018-10-11 15:59:11 +02001228 cmocka_unit_test_setup(test_module, logger_setup),
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001229 cmocka_unit_test_setup(test_identity, logger_setup),
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001230 cmocka_unit_test_setup(test_feature, logger_setup),
1231 cmocka_unit_test_setup(test_deviation, logger_setup),
1232 cmocka_unit_test_setup(test_deviate, logger_setup),
Radek Krejci8c370832018-11-02 15:10:03 +01001233 cmocka_unit_test_setup(test_container, logger_setup),
Radek Krejci80dd33e2018-09-26 15:57:18 +02001234 };
1235
1236 return cmocka_run_group_tests(tests, NULL, NULL);
1237}