blob: 23144dc8eff99da2f31a6c5ff2750e319ba3c7c6 [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"
23#include "../../src/tree_schema.c"
Radek Krejci86d106e2018-10-18 09:53:19 +020024
Radek Krejci80dd33e2018-09-26 15:57:18 +020025#include <stdarg.h>
26#include <stddef.h>
27#include <setjmp.h>
28#include <cmocka.h>
29
30#include <stdio.h>
31#include <string.h>
32
33#include "libyang.h"
Radek Krejci80dd33e2018-09-26 15:57:18 +020034
35#define BUFSIZE 1024
36char logbuf[BUFSIZE] = {0};
Radek Krejci9ed7a192018-10-31 16:23:51 +010037int store = -1; /* negative for infinite logging, positive for limited logging */
Radek Krejci80dd33e2018-09-26 15:57:18 +020038
39/* set to 0 to printing error messages to stderr instead of checking them in code */
Radek Krejci70853c52018-10-15 14:46:16 +020040#define ENABLE_LOGGER_CHECKING 1
Radek Krejci80dd33e2018-09-26 15:57:18 +020041
Radek Krejcid5f2b5f2018-10-11 10:54:36 +020042#if ENABLE_LOGGER_CHECKING
Radek Krejci80dd33e2018-09-26 15:57:18 +020043static void
44logger(LY_LOG_LEVEL level, const char *msg, const char *path)
45{
46 (void) level; /* unused */
Radek Krejci9ed7a192018-10-31 16:23:51 +010047 if (store) {
48 if (path && path[0]) {
49 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
50 } else {
51 strncpy(logbuf, msg, BUFSIZE - 1);
52 }
53 if (store > 0) {
54 --store;
55 }
Radek Krejci80dd33e2018-09-26 15:57:18 +020056 }
57}
Radek Krejcid5f2b5f2018-10-11 10:54:36 +020058#endif
Radek Krejci80dd33e2018-09-26 15:57:18 +020059
60static int
61logger_setup(void **state)
62{
63 (void) state; /* unused */
64#if ENABLE_LOGGER_CHECKING
65 ly_set_log_clb(logger, 1);
66#endif
67 return 0;
68}
69
70void
71logbuf_clean(void)
72{
73 logbuf[0] = '\0';
74}
75
76#if ENABLE_LOGGER_CHECKING
77# define logbuf_assert(str) assert_string_equal(logbuf, str)
78#else
79# define logbuf_assert(str)
80#endif
81
Radek Krejci2c02f3e2018-10-16 10:54:38 +020082#define TEST_DUP_GENERIC(PREFIX, MEMBER, VALUE1, VALUE2, FUNC, RESULT, LINE, CLEANUP) \
83 str = PREFIX MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
84 assert_int_equal(LY_EVALID, FUNC(&ctx, &str, RESULT)); \
85 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number "LINE"."); \
86 CLEANUP
87
Radek Krejci44ceedc2018-10-02 15:54:31 +020088static void
89test_helpers(void **state)
90{
91 (void) state; /* unused */
92
93 const char *str;
Radek Krejci404251e2018-10-09 12:06:44 +020094 char *buf, *p;
Radek Krejci44ceedc2018-10-02 15:54:31 +020095 size_t len, size;
96 int prefix;
97 struct ly_parser_ctx ctx;
98 ctx.ctx = NULL;
99 ctx.line = 1;
100
101 /* storing into buffer */
102 str = "abcd";
103 buf = NULL;
104 size = len = 0;
105 assert_int_equal(LY_SUCCESS, buf_add_char(NULL, &str, 2, &buf, &size, &len));
106 assert_int_not_equal(0, size);
107 assert_int_equal(2, len);
108 assert_string_equal("cd", str);
109 assert_false(strncmp("ab", buf, 2));
110 free(buf);
Radek Krejci404251e2018-10-09 12:06:44 +0200111 buf = NULL;
112
113 /* invalid first characters */
114 len = 0;
115 str = "2invalid";
116 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1));
117 str = ".invalid";
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 /* invalid following characters */
122 len = 3; /* number of characters read before the str content */
123 str = "!";
124 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1));
125 str = ":";
126 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1));
127 /* valid colon for prefixed identifiers */
128 len = size = 0;
129 p = NULL;
130 str = "x:id";
131 assert_int_equal(LY_SUCCESS, buf_store_char(&ctx, &str, Y_PREF_IDENTIF_ARG, &p, &len, &buf, &size, 0));
132 assert_int_equal(1, len);
133 assert_null(buf);
134 assert_string_equal(":id", str);
135 assert_int_equal('x', p[len - 1]);
136 assert_int_equal(LY_SUCCESS, buf_store_char(&ctx, &str, Y_PREF_IDENTIF_ARG, &p, &len, &buf, &size, 1));
137 assert_int_equal(2, len);
138 assert_string_equal("id", str);
139 assert_int_equal(':', p[len - 1]);
140 free(buf);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200141
142 /* checking identifiers */
143 assert_int_equal(LY_EVALID, check_identifierchar(&ctx, ':', 0, NULL));
144 logbuf_assert("Invalid identifier character ':'. Line number 1.");
145 assert_int_equal(LY_EVALID, check_identifierchar(&ctx, '#', 1, NULL));
146 logbuf_assert("Invalid identifier first character '#'. Line number 1.");
147
148 assert_int_equal(LY_SUCCESS, check_identifierchar(&ctx, 'a', 1, &prefix));
149 assert_int_equal(0, prefix);
150 assert_int_equal(LY_SUCCESS, check_identifierchar(&ctx, ':', 0, &prefix));
151 assert_int_equal(1, prefix);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200152 assert_int_equal(LY_EVALID, check_identifierchar(&ctx, ':', 0, &prefix));
153 assert_int_equal(1, prefix);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200154 assert_int_equal(LY_SUCCESS, check_identifierchar(&ctx, 'b', 0, &prefix));
155 assert_int_equal(2, prefix);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200156 /* second colon is invalid */
157 assert_int_equal(LY_EVALID, check_identifierchar(&ctx, ':', 0, &prefix));
158 logbuf_assert("Invalid identifier character ':'. Line number 1.");
Radek Krejci44ceedc2018-10-02 15:54:31 +0200159}
Radek Krejci80dd33e2018-09-26 15:57:18 +0200160
161static void
162test_comments(void **state)
163{
164 (void) state; /* unused */
165
Radek Krejci44ceedc2018-10-02 15:54:31 +0200166 struct ly_parser_ctx ctx;
Radek Krejci80dd33e2018-09-26 15:57:18 +0200167 const char *str, *p;
Radek Krejciefd22f62018-09-27 11:47:58 +0200168 char *word, *buf;
169 size_t len;
Radek Krejci80dd33e2018-09-26 15:57:18 +0200170
Radek Krejci44ceedc2018-10-02 15:54:31 +0200171 ctx.ctx = NULL;
172 ctx.line = 1;
173
Radek Krejciefd22f62018-09-27 11:47:58 +0200174 str = " // this is a text of / one * line */ comment\nargument";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200175 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200176 assert_string_equal("argument", word);
177 assert_null(buf);
178 assert_int_equal(8, len);
Radek Krejci80dd33e2018-09-26 15:57:18 +0200179
Radek Krejciefd22f62018-09-27 11:47:58 +0200180 str = "/* this is a \n * text // of / block * comment */\"arg\" + \"ume\" \n + \n \"nt\"";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200181 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200182 assert_string_equal("argument", word);
183 assert_ptr_equal(buf, word);
184 assert_int_equal(8, len);
185 free(word);
Radek Krejci80dd33e2018-09-26 15:57:18 +0200186
187 str = p = " this is one line comment on last line";
Radek Krejci44ceedc2018-10-02 15:54:31 +0200188 assert_int_equal(LY_SUCCESS, skip_comment(&ctx, &str, 1));
Radek Krejci80dd33e2018-09-26 15:57:18 +0200189 assert_true(str[0] == '\0');
190
191 str = p = " this is a not terminated comment x";
Radek Krejci44ceedc2018-10-02 15:54:31 +0200192 assert_int_equal(LY_EVALID, skip_comment(&ctx, &str, 2));
193 logbuf_assert("Unexpected end-of-file, non-terminated comment. Line number 5.");
Radek Krejci80dd33e2018-09-26 15:57:18 +0200194 assert_true(str[0] == '\0');
195}
196
Radek Krejciefd22f62018-09-27 11:47:58 +0200197static void
198test_arg(void **state)
199{
200 (void) state; /* unused */
201
Radek Krejci44ceedc2018-10-02 15:54:31 +0200202 struct ly_parser_ctx ctx;
Radek Krejciefd22f62018-09-27 11:47:58 +0200203 const char *str;
204 char *word, *buf;
205 size_t len;
206
Radek Krejci44ceedc2018-10-02 15:54:31 +0200207 ctx.ctx = NULL;
208 ctx.line = 1;
209
Radek Krejciefd22f62018-09-27 11:47:58 +0200210 /* missing argument */
211 str = ";";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200212 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_MAYBE_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200213 assert_null(word);
214
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200215 str = "{";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200216 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200217 logbuf_assert("Invalid character sequence \"{\", expected an argument. Line number 1.");
218
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200219 /* invalid escape sequence */
220 str = "\"\\s\"";
221 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200222 logbuf_assert("Double-quoted string unknown special character \'\\s\'. Line number 1.");
223 str = "\'\\s\'"; /* valid, since it is not an escape sequence in single quoted string */
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200224 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200225 assert_int_equal(2, len);
226 assert_string_equal("\\s\'", word);
227 assert_int_equal('\0', str[0]); /* input has been eaten */
228
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200229 /* invalid character after the argument */
230 str = "hello\"";
231 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
232 logbuf_assert("Invalid character sequence \"\"\", expected unquoted string character, optsep, semicolon or opening brace. Line number 1.");
233 str = "hello}";
234 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
235 logbuf_assert("Invalid character sequence \"}\", expected unquoted string character, optsep, semicolon or opening brace. Line number 1.");
236
237 str = "hello/x\t"; /* slash is not an invalid character */
238 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
239 assert_int_equal(7, len);
240 assert_string_equal("hello/x\t", word);
241
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200242 assert_null(buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200243
244 /* different quoting */
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200245 str = "hello ";
246 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200247 assert_null(buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200248 assert_int_equal(5, len);
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200249 assert_string_equal("hello ", word);
Radek Krejciefd22f62018-09-27 11:47:58 +0200250
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200251 str = "hello/*comment*/\n";
252 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200253 assert_null(buf);
254 assert_int_equal(5, len);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200255 assert_false(strncmp("hello", word, len));
256
257
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200258 str = "\"hello\\n\\t\\\"\\\\\";";
259 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200260 assert_null(buf);
261 assert_int_equal(9, len);
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200262 assert_string_equal("hello\\n\\t\\\"\\\\\";", word);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200263
264 ctx.indent = 14;
265 str = "\"hello \t\n\t\t world!\"";
266 /* - space and tabs before newline are stripped out
267 * - space and tabs after newline (indentation) are stripped out
268 */
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200269 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200270 assert_non_null(buf);
271 assert_ptr_equal(word, buf);
272 assert_int_equal(14, len);
273 assert_string_equal("hello\n world!", word);
274 free(buf);
275
276 ctx.indent = 14;
277 str = "\"hello\n \tworld!\"";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200278 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200279 assert_non_null(buf);
280 assert_ptr_equal(word, buf);
281 assert_int_equal(12, len);
282 assert_string_equal("hello\nworld!", word);
283 free(buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200284
285 str = "\'hello\'";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200286 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200287 assert_null(buf);
288 assert_int_equal(5, len);
289 assert_false(strncmp("hello", word, 5));
290
291 str = "\"hel\" +\t\n\"lo\"";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200292 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200293 assert_ptr_equal(word, buf);
294 assert_int_equal(5, len);
295 assert_string_equal("hello", word);
296 free(buf);
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200297 str = "\"hel\" +\t\nlo"; /* unquoted the second part */
298 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
299 logbuf_assert("Both string parts divided by '+' must be quoted. Line number 5.");
Radek Krejciefd22f62018-09-27 11:47:58 +0200300
301 str = "\'he\'\t\n+ \"llo\"";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200302 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200303 assert_ptr_equal(word, buf);
304 assert_int_equal(5, len);
305 assert_string_equal("hello", word);
306 free(buf);
307
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200308 str = " \t\n\"he\"+\'llo\'";
309 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200310 assert_ptr_equal(word, buf);
311 assert_int_equal(5, len);
312 assert_string_equal("hello", word);
313 free(buf);
314
Radek Krejci44ceedc2018-10-02 15:54:31 +0200315 /* missing argument */
316 str = ";";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200317 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
318 logbuf_assert("Invalid character sequence \";\", expected an argument. Line number 7.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200319}
320
321static void
322test_stmts(void **state)
323{
324 (void) state; /* unused */
325
326 struct ly_parser_ctx ctx;
327 const char *str, *p;
328 enum yang_keyword kw;
329 char *word;
330 size_t len;
331
332 ctx.ctx = NULL;
333 ctx.line = 1;
334
335 str = "\n// comment\n\tinput\t{";
336 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
337 assert_int_equal(YANG_INPUT, kw);
338 assert_int_equal(5, len);
339 assert_string_equal("input\t{", word);
340 assert_string_equal("\t{", str);
341
342 str = "\t /* comment */\t output\n\t{";
343 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
344 assert_int_equal(YANG_OUTPUT, kw);
345 assert_int_equal(6, len);
346 assert_string_equal("output\n\t{", word);
347 assert_string_equal("\n\t{", str);
348
349 str = "/input { "; /* invalid slash */
350 assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len));
351 logbuf_assert("Invalid identifier first character '/'. Line number 4.");
352
353 str = "not-a-statement-nor-extension { "; /* invalid identifier */
354 assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len));
355 logbuf_assert("Invalid character sequence \"not-a-statement-nor-extension\", expected a keyword. Line number 4.");
356
357 str = "path;"; /* missing sep after the keyword */
358 assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len));
359 logbuf_assert("Invalid character sequence \"path;\", expected a keyword followed by a separator. Line number 4.");
360
361 str = "action ";
362 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
363 assert_int_equal(YANG_ACTION, kw);
364 assert_int_equal(6, len);
365 str = "anydata ";
366 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
367 assert_int_equal(YANG_ANYDATA, kw);
368 assert_int_equal(7, len);
369 str = "anyxml ";
370 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
371 assert_int_equal(YANG_ANYXML, kw);
372 assert_int_equal(6, len);
373 str = "argument ";
374 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
375 assert_int_equal(YANG_ARGUMENT, kw);
376 assert_int_equal(8, len);
377 str = "augment ";
378 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
379 assert_int_equal(YANG_AUGMENT, kw);
380 assert_int_equal(7, len);
381 str = "base ";
382 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
383 assert_int_equal(YANG_BASE, kw);
384 assert_int_equal(4, len);
385 str = "belongs-to ";
386 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
387 assert_int_equal(YANG_BELONGS_TO, kw);
388 assert_int_equal(10, len);
389 str = "bit ";
390 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
391 assert_int_equal(YANG_BIT, kw);
392 assert_int_equal(3, len);
393 str = "case ";
394 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
395 assert_int_equal(YANG_CASE, kw);
396 assert_int_equal(4, len);
397 str = "choice ";
398 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
399 assert_int_equal(YANG_CHOICE, kw);
400 assert_int_equal(6, len);
401 str = "config ";
402 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
403 assert_int_equal(YANG_CONFIG, kw);
404 assert_int_equal(6, len);
405 str = "contact ";
406 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
407 assert_int_equal(YANG_CONTACT, kw);
408 assert_int_equal(7, len);
409 str = "container ";
410 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
411 assert_int_equal(YANG_CONTAINER, kw);
412 assert_int_equal(9, len);
413 str = "default ";
414 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
415 assert_int_equal(YANG_DEFAULT, kw);
416 assert_int_equal(7, len);
417 str = "description ";
418 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
419 assert_int_equal(YANG_DESCRIPTION, kw);
420 assert_int_equal(11, len);
421 str = "deviate ";
422 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
423 assert_int_equal(YANG_DEVIATE, kw);
424 assert_int_equal(7, len);
425 str = "deviation ";
426 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
427 assert_int_equal(YANG_DEVIATION, kw);
428 assert_int_equal(9, len);
429 str = "enum ";
430 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
431 assert_int_equal(YANG_ENUM, kw);
432 assert_int_equal(4, len);
433 str = "error-app-tag ";
434 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
435 assert_int_equal(YANG_ERROR_APP_TAG, kw);
436 assert_int_equal(13, len);
437 str = "error-message ";
438 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
439 assert_int_equal(YANG_ERROR_MESSAGE, kw);
440 assert_int_equal(13, len);
441 str = "extension ";
442 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
443 assert_int_equal(YANG_EXTENSION, kw);
444 assert_int_equal(9, len);
445 str = "feature ";
446 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
447 assert_int_equal(YANG_FEATURE, kw);
448 assert_int_equal(7, len);
449 str = "fraction-digits ";
450 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
451 assert_int_equal(YANG_FRACTION_DIGITS, kw);
452 assert_int_equal(15, len);
453 str = "grouping ";
454 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
455 assert_int_equal(YANG_GROUPING, kw);
456 assert_int_equal(8, len);
457 str = "identity ";
458 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
459 assert_int_equal(YANG_IDENTITY, kw);
460 assert_int_equal(8, len);
461 str = "if-feature ";
462 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
463 assert_int_equal(YANG_IF_FEATURE, kw);
464 assert_int_equal(10, len);
465 str = "import ";
466 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
467 assert_int_equal(YANG_IMPORT, kw);
468 assert_int_equal(6, len);
469 str = "include ";
470 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
471 assert_int_equal(YANG_INCLUDE, kw);
472 assert_int_equal(7, len);
473 str = "input{";
474 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
475 assert_int_equal(YANG_INPUT, kw);
476 assert_int_equal(5, len);
477 str = "key ";
478 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
479 assert_int_equal(YANG_KEY, kw);
480 assert_int_equal(3, len);
481 str = "leaf ";
482 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
483 assert_int_equal(YANG_LEAF, kw);
484 assert_int_equal(4, len);
485 str = "leaf-list ";
486 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
487 assert_int_equal(YANG_LEAF_LIST, kw);
488 assert_int_equal(9, len);
489 str = "length ";
490 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
491 assert_int_equal(YANG_LENGTH, kw);
492 assert_int_equal(6, len);
493 str = "list ";
494 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
495 assert_int_equal(YANG_LIST, kw);
496 assert_int_equal(4, len);
497 str = "mandatory ";
498 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
499 assert_int_equal(YANG_MANDATORY, kw);
500 assert_int_equal(9, len);
501 str = "max-elements ";
502 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
503 assert_int_equal(YANG_MAX_ELEMENTS, kw);
504 assert_int_equal(12, len);
505 str = "min-elements ";
506 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
507 assert_int_equal(YANG_MIN_ELEMENTS, kw);
508 assert_int_equal(12, len);
509 str = "modifier ";
510 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
511 assert_int_equal(YANG_MODIFIER, kw);
512 assert_int_equal(8, len);
513 str = "module ";
514 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
515 assert_int_equal(YANG_MODULE, kw);
516 assert_int_equal(6, len);
517 str = "must ";
518 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
519 assert_int_equal(YANG_MUST, kw);
520 assert_int_equal(4, len);
521 str = "namespace ";
522 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
523 assert_int_equal(YANG_NAMESPACE, kw);
524 assert_int_equal(9, len);
525 str = "notification ";
526 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
527 assert_int_equal(YANG_NOTIFICATION, kw);
528 assert_int_equal(12, len);
529 str = "ordered-by ";
530 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
531 assert_int_equal(YANG_ORDERED_BY, kw);
532 assert_int_equal(10, len);
533 str = "organization ";
534 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
535 assert_int_equal(YANG_ORGANIZATION, kw);
536 assert_int_equal(12, len);
537 str = "output ";
538 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
539 assert_int_equal(YANG_OUTPUT, kw);
540 assert_int_equal(6, len);
541 str = "path ";
542 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
543 assert_int_equal(YANG_PATH, kw);
544 assert_int_equal(4, len);
545 str = "pattern ";
546 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
547 assert_int_equal(YANG_PATTERN, kw);
548 assert_int_equal(7, len);
549 str = "position ";
550 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
551 assert_int_equal(YANG_POSITION, kw);
552 assert_int_equal(8, len);
553 str = "prefix ";
554 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
555 assert_int_equal(YANG_PREFIX, kw);
556 assert_int_equal(6, len);
557 str = "presence ";
558 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
559 assert_int_equal(YANG_PRESENCE, kw);
560 assert_int_equal(8, len);
561 str = "range ";
562 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
563 assert_int_equal(YANG_RANGE, kw);
564 assert_int_equal(5, len);
565 str = "reference ";
566 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
567 assert_int_equal(YANG_REFERENCE, kw);
568 assert_int_equal(9, len);
569 str = "refine ";
570 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
571 assert_int_equal(YANG_REFINE, kw);
572 assert_int_equal(6, len);
573 str = "require-instance ";
574 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
575 assert_int_equal(YANG_REQUIRE_INSTANCE, kw);
576 assert_int_equal(16, len);
577 str = "revision ";
578 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
579 assert_int_equal(YANG_REVISION, kw);
580 assert_int_equal(8, len);
581 str = "revision-date ";
582 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
583 assert_int_equal(YANG_REVISION_DATE, kw);
584 assert_int_equal(13, len);
585 str = "rpc ";
586 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
587 assert_int_equal(YANG_RPC, kw);
588 assert_int_equal(3, len);
589 str = "status ";
590 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
591 assert_int_equal(YANG_STATUS, kw);
592 assert_int_equal(6, len);
593 str = "submodule ";
594 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
595 assert_int_equal(YANG_SUBMODULE, kw);
596 assert_int_equal(9, len);
597 str = "type ";
598 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
599 assert_int_equal(YANG_TYPE, kw);
600 assert_int_equal(4, len);
601 str = "typedef ";
602 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
603 assert_int_equal(YANG_TYPEDEF, kw);
604 assert_int_equal(7, len);
605 str = "unique ";
606 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
607 assert_int_equal(YANG_UNIQUE, kw);
608 assert_int_equal(6, len);
609 str = "units ";
610 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
611 assert_int_equal(YANG_UNITS, kw);
612 assert_int_equal(5, len);
613 str = "uses ";
614 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
615 assert_int_equal(YANG_USES, kw);
616 assert_int_equal(4, len);
617 str = "value ";
618 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
619 assert_int_equal(YANG_VALUE, kw);
620 assert_int_equal(5, len);
621 str = "when ";
622 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
623 assert_int_equal(YANG_WHEN, kw);
624 assert_int_equal(4, len);
625 str = "yang-version ";
626 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
627 assert_int_equal(YANG_YANG_VERSION, kw);
628 assert_int_equal(12, len);
629 str = "yin-element ";
630 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
631 assert_int_equal(YANG_YIN_ELEMENT, kw);
632 assert_int_equal(11, len);
Radek Krejci626df482018-10-11 15:06:31 +0200633 str = ";config false;";
634 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
635 assert_int_equal(YANG_SEMICOLON, kw);
636 assert_int_equal(1, len);
637 assert_string_equal("config false;", str);
638 str = "{ config false;";
639 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
640 assert_int_equal(YANG_LEFT_BRACE, kw);
641 assert_int_equal(1, len);
642 assert_string_equal(" config false;", str);
643 str = "}";
644 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
645 assert_int_equal(YANG_RIGHT_BRACE, kw);
646 assert_int_equal(1, len);
647 assert_string_equal("", str);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200648
649 /* geenric extension */
650 str = p = "nacm:default-deny-write;";
651 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
652 assert_int_equal(YANG_CUSTOM, kw);
653 assert_int_equal(23, len);
654 assert_ptr_equal(p, word);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200655}
Radek Krejci44ceedc2018-10-02 15:54:31 +0200656
Radek Krejci9fcacc12018-10-11 15:59:11 +0200657static struct lysp_module *
Radek Krejci09306362018-10-15 15:26:01 +0200658mod_renew(struct ly_parser_ctx *ctx, struct lysp_module *mod, uint8_t submodule)
Radek Krejci9fcacc12018-10-11 15:59:11 +0200659{
660 lysp_module_free(mod);
661 mod = calloc(1, sizeof *mod);
662 mod->ctx = ctx->ctx;
Radek Krejci09306362018-10-15 15:26:01 +0200663 mod->submodule = submodule;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200664 assert_non_null(mod);
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100665 ctx->mod = mod;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200666 return mod;
667}
668
Radek Krejcid33273d2018-10-25 14:55:52 +0200669static LY_ERR test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
670 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
671 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
672{
673 *module_data = user_data;
674 *format = LYS_IN_YANG;
675 *free_module_data = NULL;
676 return LY_SUCCESS;
677}
678
Radek Krejci9fcacc12018-10-11 15:59:11 +0200679static void
680test_module(void **state)
681{
682 (void) state; /* unused */
683
684 struct ly_parser_ctx ctx;
685 struct lysp_module *mod;
686 const char *str;
687
688 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
689 assert_non_null(ctx.ctx);
690 ctx.line = 1;
Radek Krejcia042ea12018-10-13 07:52:15 +0200691 ctx.indent = 0;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200692
Radek Krejci09306362018-10-15 15:26:01 +0200693 mod = mod_renew(&ctx, NULL, 0);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200694
695 /* missing mandatory substatements */
696 str = " name {}";
697 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
698 assert_string_equal("name", mod->name);
699 logbuf_assert("Missing mandatory keyword \"namespace\" as a child of \"module\". Line number 1.");
Radek Krejci09306362018-10-15 15:26:01 +0200700 mod = mod_renew(&ctx, mod, 0);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200701
702 str = " name {namespace urn:x;}";
703 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
704 assert_string_equal("urn:x", mod->ns);
705 logbuf_assert("Missing mandatory keyword \"prefix\" as a child of \"module\". Line number 1.");
Radek Krejci09306362018-10-15 15:26:01 +0200706 mod = mod_renew(&ctx, mod, 0);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200707
708 str = " name {namespace urn:x;prefix \"x\";}";
709 assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod));
710 assert_string_equal("x", mod->prefix);
Radek Krejci09306362018-10-15 15:26:01 +0200711 mod = mod_renew(&ctx, mod, 0);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200712
Radek Krejci027d5802018-11-14 16:57:28 +0100713#define SCHEMA_BEGINNING " name {yang-version 1.1;namespace urn:x;prefix \"x\";"
714#define SCHEMA_BEGINNING2 " name {namespace urn:x;prefix \"x\";"
Radek Krejcia042ea12018-10-13 07:52:15 +0200715#define TEST_NODE(NODETYPE, INPUT, NAME) \
716 str = SCHEMA_BEGINNING INPUT; \
717 assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod)); \
718 assert_non_null(mod->data); \
719 assert_int_equal(NODETYPE, mod->data->nodetype); \
720 assert_string_equal(NAME, mod->data->name); \
Radek Krejci09306362018-10-15 15:26:01 +0200721 mod = mod_renew(&ctx, mod, 0);
Radek Krejcia042ea12018-10-13 07:52:15 +0200722#define TEST_GENERIC(INPUT, TARGET, TEST) \
723 str = SCHEMA_BEGINNING INPUT; \
724 assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod)); \
725 assert_non_null(TARGET); \
726 TEST; \
Radek Krejci09306362018-10-15 15:26:01 +0200727 mod = mod_renew(&ctx, mod, 0);
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200728#define TEST_DUP(MEMBER, VALUE1, VALUE2, LINE, SUBMODULE) \
729 TEST_DUP_GENERIC(SCHEMA_BEGINNING, MEMBER, VALUE1, VALUE2, \
730 parse_sub_module, mod, LINE, mod = mod_renew(&ctx, mod, SUBMODULE))
Radek Krejcia042ea12018-10-13 07:52:15 +0200731
732 /* duplicated namespace, prefix */
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200733 TEST_DUP("namespace", "y", "z", "1", 0);
734 TEST_DUP("prefix", "y", "z", "1", 0);
735 TEST_DUP("contact", "a", "b", "1", 0);
736 TEST_DUP("description", "a", "b", "1", 0);
737 TEST_DUP("organization", "a", "b", "1", 0);
738 TEST_DUP("reference", "a", "b", "1", 0);
Radek Krejcia042ea12018-10-13 07:52:15 +0200739
Radek Krejci70853c52018-10-15 14:46:16 +0200740 /* not allowed in module (submodule-specific) */
741 str = SCHEMA_BEGINNING "belongs-to master {prefix m;}}";
742 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
743 logbuf_assert("Invalid keyword \"belongs-to\" as a child of \"module\". Line number 1.");
Radek Krejci09306362018-10-15 15:26:01 +0200744 mod = mod_renew(&ctx, mod, 0);
Radek Krejci70853c52018-10-15 14:46:16 +0200745
Radek Krejcia042ea12018-10-13 07:52:15 +0200746 /* anydata */
747 TEST_NODE(LYS_ANYDATA, "anydata test;}", "test");
748 /* anyxml */
749 TEST_NODE(LYS_ANYXML, "anyxml test;}", "test");
750 /* augment */
751 TEST_GENERIC("augment /somepath;}", mod->augments,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200752 assert_string_equal("/somepath", mod->augments[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200753 /* choice */
754 TEST_NODE(LYS_CHOICE, "choice test;}", "test");
755 /* contact 0..1 */
756 TEST_GENERIC("contact \"firstname\" + \n\t\" surname\";}", mod->contact,
757 assert_string_equal("firstname surname", mod->contact));
758 /* container */
759 TEST_NODE(LYS_CONTAINER, "container test;}", "test");
760 /* description 0..1 */
761 TEST_GENERIC("description \'some description\';}", mod->dsc,
762 assert_string_equal("some description", mod->dsc));
763 /* deviation */
764 TEST_GENERIC("deviation /somepath {deviate not-supported;}}", mod->deviations,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200765 assert_string_equal("/somepath", mod->deviations[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200766 /* extension */
767 TEST_GENERIC("extension test;}", mod->extensions,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200768 assert_string_equal("test", mod->extensions[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200769 /* feature */
770 TEST_GENERIC("feature test;}", mod->features,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200771 assert_string_equal("test", mod->features[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200772 /* grouping */
773 TEST_GENERIC("grouping grp;}", mod->groupings,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200774 assert_string_equal("grp", mod->groupings[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200775 /* identity */
776 TEST_GENERIC("identity test;}", mod->identities,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200777 assert_string_equal("test", mod->identities[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200778 /* import */
Radek Krejci086c7132018-10-26 15:29:04 +0200779 ly_ctx_set_module_imp_clb(ctx.ctx, test_imp_clb, "module zzz { namespace urn:zzz; prefix z;}");
780 TEST_GENERIC("import zzz {prefix z;}}", mod->imports,
781 assert_string_equal("zzz", mod->imports[0].name));
Radek Krejci70853c52018-10-15 14:46:16 +0200782
Radek Krejcia042ea12018-10-13 07:52:15 +0200783 /* import - prefix collision */
Radek Krejci086c7132018-10-26 15:29:04 +0200784 str = SCHEMA_BEGINNING "import zzz {prefix x;}}";
Radek Krejcia042ea12018-10-13 07:52:15 +0200785 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
Radek Krejci70853c52018-10-15 14:46:16 +0200786 logbuf_assert("Prefix \"x\" already used as module prefix. Line number 2.");
Radek Krejci09306362018-10-15 15:26:01 +0200787 mod = mod_renew(&ctx, mod, 0);
Radek Krejci086c7132018-10-26 15:29:04 +0200788 str = SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix y;}}";
Radek Krejci86d106e2018-10-18 09:53:19 +0200789 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
Radek Krejci086c7132018-10-26 15:29:04 +0200790 logbuf_assert("Prefix \"y\" already used to import \"zzz\" module. Line number 2.");
Radek Krejci86d106e2018-10-18 09:53:19 +0200791 mod = mod_renew(&ctx, mod, 0);
Radek Krejci086c7132018-10-26 15:29:04 +0200792 str = "module" SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix z;}}";
793 assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
794 assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx));
795 logbuf_assert("Single revision of the module \"zzz\" referred twice.");
Radek Krejci70853c52018-10-15 14:46:16 +0200796
Radek Krejcia042ea12018-10-13 07:52:15 +0200797 /* include */
Radek Krejci9ed7a192018-10-31 16:23:51 +0100798 store = 1;
Radek Krejcid33273d2018-10-25 14:55:52 +0200799 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 +0200800 str = "module" SCHEMA_BEGINNING "include xxx;}";
801 assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
802 assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx));
803 logbuf_assert("Included \"xxx\" schema from \"name\" is actually not a submodule.");
Radek Krejci9ed7a192018-10-31 16:23:51 +0100804 store = -1;
Radek Krejcid33273d2018-10-25 14:55:52 +0200805
Radek Krejci9ed7a192018-10-31 16:23:51 +0100806 store = 1;
Radek Krejci313d9902018-11-08 09:42:58 +0100807 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 +0200808 str = "module" SCHEMA_BEGINNING "include xxx;}";
809 assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
810 assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx));
811 logbuf_assert("Included \"xxx\" submodule from \"name\" belongs-to a different module \"wrong-name\".");
Radek Krejci9ed7a192018-10-31 16:23:51 +0100812 store = -1;
Radek Krejcid33273d2018-10-25 14:55:52 +0200813
Radek Krejci313d9902018-11-08 09:42:58 +0100814 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 +0200815 TEST_GENERIC("include xxx;}", mod->includes,
Radek Krejci086c7132018-10-26 15:29:04 +0200816 assert_string_equal("xxx", mod->includes[0].name));
Radek Krejcid33273d2018-10-25 14:55:52 +0200817
Radek Krejcia042ea12018-10-13 07:52:15 +0200818 /* leaf */
819 TEST_NODE(LYS_LEAF, "leaf test {type string;}}", "test");
820 /* leaf-list */
821 TEST_NODE(LYS_LEAFLIST, "leaf-list test {type string;}}", "test");
822 /* list */
823 TEST_NODE(LYS_LIST, "list test {key a;leaf a {type string;}}}", "test");
824 /* notification */
825 TEST_GENERIC("notification test;}", mod->notifs,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200826 assert_string_equal("test", mod->notifs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200827 /* organization 0..1 */
828 TEST_GENERIC("organization \"CESNET a.l.e.\";}", mod->org,
829 assert_string_equal("CESNET a.l.e.", mod->org));
830 /* reference 0..1 */
831 TEST_GENERIC("reference RFC7950;}", mod->ref,
832 assert_string_equal("RFC7950", mod->ref));
833 /* revision */
834 TEST_GENERIC("revision 2018-10-12;}", mod->revs,
Radek Krejcib7db73a2018-10-24 14:18:40 +0200835 assert_string_equal("2018-10-12", mod->revs[0].date));
Radek Krejcia042ea12018-10-13 07:52:15 +0200836 /* rpc */
837 TEST_GENERIC("rpc test;}", mod->rpcs,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200838 assert_string_equal("test", mod->rpcs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200839 /* typedef */
840 TEST_GENERIC("typedef test{type string;}}", mod->typedefs,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200841 assert_string_equal("test", mod->typedefs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200842 /* uses */
843 TEST_NODE(LYS_USES, "uses test;}", "test");
844 /* yang-version */
Radek Krejci027d5802018-11-14 16:57:28 +0100845 str = SCHEMA_BEGINNING2 "\n\tyang-version 10;}";
Radek Krejcia042ea12018-10-13 07:52:15 +0200846 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
847 logbuf_assert("Invalid value \"10\" of \"yang-version\". Line number 3.");
Radek Krejci09306362018-10-15 15:26:01 +0200848 mod = mod_renew(&ctx, mod, 0);
Radek Krejci027d5802018-11-14 16:57:28 +0100849 str = SCHEMA_BEGINNING2 "yang-version 1.0;yang-version 1.1;}";
Radek Krejcia042ea12018-10-13 07:52:15 +0200850 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
851 logbuf_assert("Duplicate keyword \"yang-version\". Line number 3.");
Radek Krejci09306362018-10-15 15:26:01 +0200852 mod = mod_renew(&ctx, mod, 0);
Radek Krejci027d5802018-11-14 16:57:28 +0100853 str = SCHEMA_BEGINNING2 "yang-version 1.0;}";
Radek Krejcia042ea12018-10-13 07:52:15 +0200854 assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod));
855 assert_int_equal(1, mod->version);
Radek Krejci09306362018-10-15 15:26:01 +0200856 mod = mod_renew(&ctx, mod, 0);
Radek Krejci027d5802018-11-14 16:57:28 +0100857 str = SCHEMA_BEGINNING2 "yang-version \"1.1\";}";
Radek Krejcia042ea12018-10-13 07:52:15 +0200858 assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod));
859 assert_int_equal(2, mod->version);
Radek Krejci09306362018-10-15 15:26:01 +0200860 mod = mod_renew(&ctx, mod, 0);
861
Radek Krejci156ccaf2018-10-15 15:49:17 +0200862 /* extensions */
863 TEST_GENERIC("prefix:test;}", mod->exts,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200864 assert_string_equal("prefix:test", mod->exts[0].name);
865 assert_int_equal(LYEXT_SUBSTMT_SELF, mod->exts[0].insubstmt));
Radek Krejci156ccaf2018-10-15 15:49:17 +0200866 mod = mod_renew(&ctx, mod, 0);
867
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200868 /* invalid substatement */
869 str = SCHEMA_BEGINNING "must false;}";
870 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
871 logbuf_assert("Invalid keyword \"must\" as a child of \"module\". Line number 3.");
872 mod = mod_renew(&ctx, mod, 0);
873
Radek Krejci09306362018-10-15 15:26:01 +0200874 /* submodule */
875 mod->submodule = 1;
876
877 /* missing mandatory substatements */
878 str = " subname {}";
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100879 lydict_remove(ctx.ctx, mod->name);
Radek Krejci09306362018-10-15 15:26:01 +0200880 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
881 assert_string_equal("subname", mod->name);
882 logbuf_assert("Missing mandatory keyword \"belongs-to\" as a child of \"submodule\". Line number 3.");
883 mod = mod_renew(&ctx, mod, 1);
884
Radek Krejci313d9902018-11-08 09:42:58 +0100885 str = " subname {belongs-to name {prefix x;}}";
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100886 lydict_remove(ctx.ctx, mod->name);
Radek Krejci09306362018-10-15 15:26:01 +0200887 assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod));
888 assert_string_equal("name", mod->belongsto);
889 mod = mod_renew(&ctx, mod, 1);
890
891#undef SCHEMA_BEGINNING
Radek Krejci313d9902018-11-08 09:42:58 +0100892#define SCHEMA_BEGINNING " subname {belongs-to name {prefix x;}"
Radek Krejci09306362018-10-15 15:26:01 +0200893
894 /* duplicated namespace, prefix */
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200895 TEST_DUP("belongs-to", "module1", "module2", "3", 1);
Radek Krejci09306362018-10-15 15:26:01 +0200896
897 /* not allowed in submodule (module-specific) */
898 str = SCHEMA_BEGINNING "namespace \"urn:z\";}";
899 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
900 logbuf_assert("Invalid keyword \"namespace\" as a child of \"submodule\". Line number 3.");
901 mod = mod_renew(&ctx, mod, 1);
902 str = SCHEMA_BEGINNING "prefix m;}}";
903 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
904 logbuf_assert("Invalid keyword \"prefix\" as a child of \"submodule\". Line number 3.");
905 mod = mod_renew(&ctx, mod, 1);
Radek Krejcia042ea12018-10-13 07:52:15 +0200906
907#undef TEST_GENERIC
908#undef TEST_NODE
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200909#undef TEST_DUP
Radek Krejcia042ea12018-10-13 07:52:15 +0200910#undef SCHEMA_BEGINNING
911
Radek Krejci9fcacc12018-10-11 15:59:11 +0200912 lysp_module_free(mod);
913 ly_ctx_destroy(ctx.ctx, NULL);
Radek Krejciefd22f62018-09-27 11:47:58 +0200914}
915
Radek Krejci4c6d9bd2018-10-15 16:43:06 +0200916static void
917test_identity(void **state)
918{
919 (void) state; /* unused */
920
921 struct ly_parser_ctx ctx;
Radek Krejci027d5802018-11-14 16:57:28 +0100922 struct lysp_module *mod = NULL, m = {0};
Radek Krejci4c6d9bd2018-10-15 16:43:06 +0200923 struct lysp_ident *ident = NULL;
924 const char *str;
Radek Krejci4c6d9bd2018-10-15 16:43:06 +0200925
926 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
927 assert_non_null(ctx.ctx);
928 ctx.line = 1;
929 ctx.indent = 0;
Radek Krejci027d5802018-11-14 16:57:28 +0100930 ctx.mod = &m;
931 ctx.mod->version = 2; /* simulate YANG 1.1 */
Radek Krejci4c6d9bd2018-10-15 16:43:06 +0200932
933 /* invalid cardinality */
934#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200935 TEST_DUP_GENERIC(" test {", MEMBER, VALUE1, VALUE2, parse_identity, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +0200936 &ident, "1", FREE_ARRAY(ctx.ctx, ident, lysp_ident_free); ident = NULL)
Radek Krejci4c6d9bd2018-10-15 16:43:06 +0200937
938 TEST_DUP("description", "a", "b");
939 TEST_DUP("reference", "a", "b");
940 TEST_DUP("status", "current", "obsolete");
941
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200942 /* full content */
943 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 +0200944 assert_int_equal(LY_SUCCESS, parse_identity(&ctx, &str, &ident));
945 assert_non_null(ident);
946 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +0200947 FREE_ARRAY(ctx.ctx, ident, lysp_ident_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200948 ident = NULL;
949
950 /* invalid substatement */
951 str = " test {organization XXX;}";
952 assert_int_equal(LY_EVALID, parse_identity(&ctx, &str, &ident));
953 logbuf_assert("Invalid keyword \"organization\" as a child of \"identity\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +0200954 FREE_ARRAY(ctx.ctx, ident, lysp_ident_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200955 ident = NULL;
956
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100957 /* identity duplication */
958 str = "module a {namespace urn:a; prefix a; identity a; identity a;}";
Radek Krejci313d9902018-11-08 09:42:58 +0100959 assert_int_equal(LY_EVALID, yang_parse(&ctx, str, &mod));
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100960 logbuf_assert("Duplicate identifier \"a\" of identity statement. Line number 1.");
961 assert_null(mod);
962
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200963#undef TEST_DUP
964
965 ly_ctx_destroy(ctx.ctx, NULL);
966}
967
968static void
969test_feature(void **state)
970{
971 (void) state; /* unused */
972
973 struct ly_parser_ctx ctx;
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100974 struct lysp_module *mod = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200975 struct lysp_feature *features = NULL;
976 const char *str;
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200977
978 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
979 assert_non_null(ctx.ctx);
980 ctx.line = 1;
981 ctx.indent = 0;
982
983 /* invalid cardinality */
984#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
985 TEST_DUP_GENERIC(" test {", MEMBER, VALUE1, VALUE2, parse_feature, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +0200986 &features, "1", FREE_ARRAY(ctx.ctx, features, lysp_feature_free); features = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200987
988 TEST_DUP("description", "a", "b");
989 TEST_DUP("reference", "a", "b");
990 TEST_DUP("status", "current", "obsolete");
991
992 /* full content */
993 str = " test {description text;reference \'another text\';status current; if-feature x;if-feature y;prefix:ext;} ...";
994 assert_int_equal(LY_SUCCESS, parse_feature(&ctx, &str, &features));
995 assert_non_null(features);
996 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +0200997 FREE_ARRAY(ctx.ctx, features, lysp_feature_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200998 features = NULL;
999
1000 /* invalid substatement */
1001 str = " test {organization XXX;}";
1002 assert_int_equal(LY_EVALID, parse_feature(&ctx, &str, &features));
1003 logbuf_assert("Invalid keyword \"organization\" as a child of \"feature\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001004 FREE_ARRAY(ctx.ctx, features, lysp_feature_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001005 features = NULL;
1006
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001007 /* feature duplication */
1008 str = "module a {namespace urn:a; prefix a; feature a; feature a;}";
Radek Krejci313d9902018-11-08 09:42:58 +01001009 assert_int_equal(LY_EVALID, yang_parse(&ctx, str, &mod));
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001010 logbuf_assert("Duplicate identifier \"a\" of feature statement. Line number 1.");
1011 assert_null(mod);
1012
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001013#undef TEST_DUP
1014
1015 ly_ctx_destroy(ctx.ctx, NULL);
1016}
1017
1018static void
1019test_deviation(void **state)
1020{
1021 (void) state; /* unused */
1022
1023 struct ly_parser_ctx ctx;
1024 struct lysp_deviation *d = NULL;
1025 const char *str;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001026
1027 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1028 assert_non_null(ctx.ctx);
1029 ctx.line = 1;
1030 ctx.indent = 0;
1031
1032 /* invalid cardinality */
1033#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1034 TEST_DUP_GENERIC(" test {deviate not-supported;", MEMBER, VALUE1, VALUE2, parse_deviation, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001035 &d, "1", FREE_ARRAY(ctx.ctx, d, lysp_deviation_free); d = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001036
1037 TEST_DUP("description", "a", "b");
1038 TEST_DUP("reference", "a", "b");
1039
1040 /* full content */
1041 str = " test {deviate not-supported;description text;reference \'another text\';prefix:ext;} ...";
1042 assert_int_equal(LY_SUCCESS, parse_deviation(&ctx, &str, &d));
1043 assert_non_null(d);
1044 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001045 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001046 d = NULL;
1047
1048 /* missing mandatory substatement */
1049 str = " test {description text;}";
1050 assert_int_equal(LY_EVALID, parse_deviation(&ctx, &str, &d));
1051 logbuf_assert("Missing mandatory keyword \"deviate\" as a child of \"deviation\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001052 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001053 d = NULL;
1054
1055 /* invalid substatement */
1056 str = " test {deviate not-supported; status obsolete;}";
1057 assert_int_equal(LY_EVALID, parse_deviation(&ctx, &str, &d));
1058 logbuf_assert("Invalid keyword \"status\" as a child of \"deviation\". Line number 1.");
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#undef TEST_DUP
1063
1064 ly_ctx_destroy(ctx.ctx, NULL);
1065}
1066
1067static void
1068test_deviate(void **state)
1069{
1070 (void) state; /* unused */
1071
1072 struct ly_parser_ctx ctx;
1073 struct lysp_deviate *d = NULL;
1074 const char *str;
1075
1076 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1077 assert_non_null(ctx.ctx);
1078 ctx.line = 1;
1079 ctx.indent = 0;
1080
1081 /* invalid cardinality */
1082#define TEST_DUP(TYPE, MEMBER, VALUE1, VALUE2) \
1083 TEST_DUP_GENERIC(TYPE" {", MEMBER, VALUE1, VALUE2, parse_deviate, \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001084 &d, "1", lysp_deviate_free(ctx.ctx, d); free(d); d = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001085
1086 TEST_DUP("add", "config", "true", "false");
1087 TEST_DUP("replace", "default", "int8", "uint8");
1088 TEST_DUP("add", "mandatory", "true", "false");
1089 TEST_DUP("add", "max-elements", "1", "2");
1090 TEST_DUP("add", "min-elements", "1", "2");
1091 TEST_DUP("replace", "type", "int8", "uint8");
1092 TEST_DUP("add", "units", "kilometers", "miles");
1093
1094 /* full contents */
1095 str = " not-supported {prefix:ext;} ...";
1096 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1097 assert_non_null(d);
1098 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001099 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001100 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;} ...";
1101 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1102 assert_non_null(d);
1103 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001104 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001105 str = " delete {units meters; must 1; must 2; unique x; unique y; default a; default b; prefix:ext;} ...";
1106 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1107 assert_non_null(d);
1108 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001109 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001110 str = " replace {type string; units meters; default a; config true; mandatory true; min-elements 1; max-elements 2; prefix:ext;} ...";
1111 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1112 assert_non_null(d);
1113 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001114 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001115
1116 /* invalid substatements */
1117#define TEST_NOT_SUP(DEV, STMT, VALUE) \
1118 str = " "DEV" {"STMT" "VALUE";}..."; \
1119 assert_int_equal(LY_EVALID, parse_deviate(&ctx, &str, &d)); \
1120 logbuf_assert("Deviate \""DEV"\" does not support keyword \""STMT"\". Line number 1."); \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001121 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001122
1123 TEST_NOT_SUP("not-supported", "units", "meters");
1124 TEST_NOT_SUP("not-supported", "must", "1");
1125 TEST_NOT_SUP("not-supported", "unique", "x");
1126 TEST_NOT_SUP("not-supported", "default", "a");
1127 TEST_NOT_SUP("not-supported", "config", "true");
1128 TEST_NOT_SUP("not-supported", "mandatory", "true");
1129 TEST_NOT_SUP("not-supported", "min-elements", "1");
1130 TEST_NOT_SUP("not-supported", "max-elements", "2");
1131 TEST_NOT_SUP("not-supported", "type", "string");
1132 TEST_NOT_SUP("add", "type", "string");
1133 TEST_NOT_SUP("delete", "config", "true");
1134 TEST_NOT_SUP("delete", "mandatory", "true");
1135 TEST_NOT_SUP("delete", "min-elements", "1");
1136 TEST_NOT_SUP("delete", "max-elements", "2");
1137 TEST_NOT_SUP("delete", "type", "string");
1138 TEST_NOT_SUP("replace", "must", "1");
1139 TEST_NOT_SUP("replace", "unique", "a");
1140
1141 str = " nonsence; ...";
1142 assert_int_equal(LY_EVALID, parse_deviate(&ctx, &str, &d));
1143 logbuf_assert("Invalid value \"nonsence\" of \"deviate\". Line number 1.");
1144 assert_null(d);
1145
1146#undef TEST_NOT_SUP
1147#undef TEST_DUP
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001148
1149 ly_ctx_destroy(ctx.ctx, NULL);
1150}
1151
Radek Krejci8c370832018-11-02 15:10:03 +01001152static void
1153test_container(void **state)
1154{
1155 (void) state; /* unused */
1156
Radek Krejci313d9902018-11-08 09:42:58 +01001157 struct lysp_module mod = {0};
1158 struct ly_parser_ctx ctx = {0};
Radek Krejci8c370832018-11-02 15:10:03 +01001159 struct lysp_node_container *c = NULL;
1160 const char *str;
1161
1162 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1163 assert_non_null(ctx.ctx);
1164 ctx.line = 1;
Radek Krejci313d9902018-11-08 09:42:58 +01001165 ctx.mod = &mod;
Radek Krejci027d5802018-11-14 16:57:28 +01001166 ctx.mod->version = 2; /* simulate YANG 1.1 */
Radek Krejci8c370832018-11-02 15:10:03 +01001167
1168 /* invalid cardinality */
1169#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1170 str = "cont {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1171 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c)); \
1172 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001173 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001174
1175 TEST_DUP("config", "true", "false");
1176 TEST_DUP("description", "text1", "text2");
1177 TEST_DUP("presence", "true", "false");
1178 TEST_DUP("reference", "1", "2");
1179 TEST_DUP("status", "current", "obsolete");
1180 TEST_DUP("when", "true", "false");
1181#undef TEST_DUP
1182
1183 /* full content */
Radek Krejci313d9902018-11-08 09:42:58 +01001184 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;}"
1185 "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 +01001186 assert_int_equal(LY_SUCCESS, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1187 assert_non_null(c);
1188 assert_non_null(c->actions);
1189 assert_non_null(c->child);
1190 assert_string_equal("test", c->dsc);
1191 assert_non_null(c->exts);
1192 assert_non_null(c->groupings);
1193 assert_non_null(c->iffeatures);
1194 assert_non_null(c->musts);
1195 assert_non_null(c->notifs);
1196 assert_string_equal("true", c->presence);
1197 assert_string_equal("test", c->ref);
1198 assert_non_null(c->typedefs);
1199 assert_non_null(c->when);
1200 assert_null(c->parent);
1201 assert_null(c->next);
1202 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR, c->flags);
Radek Krejci313d9902018-11-08 09:42:58 +01001203 ly_set_erase(&ctx.tpdfs_nodes, NULL);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001204 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001205
1206 /* invalid */
1207 str = " cont {augment /root;} ...";
1208 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1209 logbuf_assert("Invalid keyword \"augment\" as a child of \"container\". Line number 1.");
Radek Krejci4f28eda2018-11-12 11:46:16 +01001210 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001211 str = " cont {nonsence true;} ...";
1212 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1213 logbuf_assert("Invalid character sequence \"nonsence\", expected a keyword. Line number 1.");
Radek Krejci4f28eda2018-11-12 11:46:16 +01001214 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001215
1216 ly_ctx_destroy(ctx.ctx, NULL);
1217}
1218
Radek Krejci80dd33e2018-09-26 15:57:18 +02001219int main(void)
1220{
1221 const struct CMUnitTest tests[] = {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001222 cmocka_unit_test_setup(test_helpers, logger_setup),
Radek Krejci80dd33e2018-09-26 15:57:18 +02001223 cmocka_unit_test_setup(test_comments, logger_setup),
Radek Krejciefd22f62018-09-27 11:47:58 +02001224 cmocka_unit_test_setup(test_arg, logger_setup),
Radek Krejcidcc7b322018-10-11 14:24:02 +02001225 cmocka_unit_test_setup(test_stmts, logger_setup),
Radek Krejci9fcacc12018-10-11 15:59:11 +02001226 cmocka_unit_test_setup(test_module, logger_setup),
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001227 cmocka_unit_test_setup(test_identity, logger_setup),
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001228 cmocka_unit_test_setup(test_feature, logger_setup),
1229 cmocka_unit_test_setup(test_deviation, logger_setup),
1230 cmocka_unit_test_setup(test_deviate, logger_setup),
Radek Krejci8c370832018-11-02 15:10:03 +01001231 cmocka_unit_test_setup(test_container, logger_setup),
Radek Krejci80dd33e2018-09-26 15:57:18 +02001232 };
1233
1234 return cmocka_run_group_tests(tests, NULL, NULL);
1235}