blob: 20d82a32b16e1641ea82655986a42e859700d213 [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 Krejci86d106e2018-10-18 09:53:19 +020015#include "../../src/tree_schema.c"
16#include "../../src/parser_yang.c"
17
Radek Krejci80dd33e2018-09-26 15:57:18 +020018#include <stdarg.h>
19#include <stddef.h>
20#include <setjmp.h>
21#include <cmocka.h>
22
23#include <stdio.h>
24#include <string.h>
25
26#include "libyang.h"
Radek Krejci80dd33e2018-09-26 15:57:18 +020027
28#define BUFSIZE 1024
29char logbuf[BUFSIZE] = {0};
Radek Krejci9ed7a192018-10-31 16:23:51 +010030int store = -1; /* negative for infinite logging, positive for limited logging */
Radek Krejci80dd33e2018-09-26 15:57:18 +020031
32/* set to 0 to printing error messages to stderr instead of checking them in code */
Radek Krejci70853c52018-10-15 14:46:16 +020033#define ENABLE_LOGGER_CHECKING 1
Radek Krejci80dd33e2018-09-26 15:57:18 +020034
Radek Krejcid5f2b5f2018-10-11 10:54:36 +020035#if ENABLE_LOGGER_CHECKING
Radek Krejci80dd33e2018-09-26 15:57:18 +020036static void
37logger(LY_LOG_LEVEL level, const char *msg, const char *path)
38{
39 (void) level; /* unused */
Radek Krejci9ed7a192018-10-31 16:23:51 +010040 if (store) {
41 if (path && path[0]) {
42 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
43 } else {
44 strncpy(logbuf, msg, BUFSIZE - 1);
45 }
46 if (store > 0) {
47 --store;
48 }
Radek Krejci80dd33e2018-09-26 15:57:18 +020049 }
50}
Radek Krejcid5f2b5f2018-10-11 10:54:36 +020051#endif
Radek Krejci80dd33e2018-09-26 15:57:18 +020052
53static int
54logger_setup(void **state)
55{
56 (void) state; /* unused */
57#if ENABLE_LOGGER_CHECKING
58 ly_set_log_clb(logger, 1);
59#endif
60 return 0;
61}
62
63void
64logbuf_clean(void)
65{
66 logbuf[0] = '\0';
67}
68
69#if ENABLE_LOGGER_CHECKING
70# define logbuf_assert(str) assert_string_equal(logbuf, str)
71#else
72# define logbuf_assert(str)
73#endif
74
Radek Krejci2c02f3e2018-10-16 10:54:38 +020075#define TEST_DUP_GENERIC(PREFIX, MEMBER, VALUE1, VALUE2, FUNC, RESULT, LINE, CLEANUP) \
76 str = PREFIX MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
77 assert_int_equal(LY_EVALID, FUNC(&ctx, &str, RESULT)); \
78 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number "LINE"."); \
79 CLEANUP
80
Radek Krejci44ceedc2018-10-02 15:54:31 +020081static void
82test_helpers(void **state)
83{
84 (void) state; /* unused */
85
86 const char *str;
Radek Krejci404251e2018-10-09 12:06:44 +020087 char *buf, *p;
Radek Krejci44ceedc2018-10-02 15:54:31 +020088 size_t len, size;
89 int prefix;
90 struct ly_parser_ctx ctx;
91 ctx.ctx = NULL;
92 ctx.line = 1;
93
94 /* storing into buffer */
95 str = "abcd";
96 buf = NULL;
97 size = len = 0;
98 assert_int_equal(LY_SUCCESS, buf_add_char(NULL, &str, 2, &buf, &size, &len));
99 assert_int_not_equal(0, size);
100 assert_int_equal(2, len);
101 assert_string_equal("cd", str);
102 assert_false(strncmp("ab", buf, 2));
103 free(buf);
Radek Krejci404251e2018-10-09 12:06:44 +0200104 buf = NULL;
105
106 /* invalid first characters */
107 len = 0;
108 str = "2invalid";
109 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1));
110 str = ".invalid";
111 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1));
112 str = "-invalid";
113 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1));
114 /* invalid following characters */
115 len = 3; /* number of characters read before the str content */
116 str = "!";
117 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1));
118 str = ":";
119 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1));
120 /* valid colon for prefixed identifiers */
121 len = size = 0;
122 p = NULL;
123 str = "x:id";
124 assert_int_equal(LY_SUCCESS, buf_store_char(&ctx, &str, Y_PREF_IDENTIF_ARG, &p, &len, &buf, &size, 0));
125 assert_int_equal(1, len);
126 assert_null(buf);
127 assert_string_equal(":id", str);
128 assert_int_equal('x', p[len - 1]);
129 assert_int_equal(LY_SUCCESS, buf_store_char(&ctx, &str, Y_PREF_IDENTIF_ARG, &p, &len, &buf, &size, 1));
130 assert_int_equal(2, len);
131 assert_string_equal("id", str);
132 assert_int_equal(':', p[len - 1]);
133 free(buf);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200134
135 /* checking identifiers */
136 assert_int_equal(LY_EVALID, check_identifierchar(&ctx, ':', 0, NULL));
137 logbuf_assert("Invalid identifier character ':'. Line number 1.");
138 assert_int_equal(LY_EVALID, check_identifierchar(&ctx, '#', 1, NULL));
139 logbuf_assert("Invalid identifier first character '#'. Line number 1.");
140
141 assert_int_equal(LY_SUCCESS, check_identifierchar(&ctx, 'a', 1, &prefix));
142 assert_int_equal(0, prefix);
143 assert_int_equal(LY_SUCCESS, check_identifierchar(&ctx, ':', 0, &prefix));
144 assert_int_equal(1, prefix);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200145 assert_int_equal(LY_EVALID, check_identifierchar(&ctx, ':', 0, &prefix));
146 assert_int_equal(1, prefix);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200147 assert_int_equal(LY_SUCCESS, check_identifierchar(&ctx, 'b', 0, &prefix));
148 assert_int_equal(2, prefix);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200149 /* second colon is invalid */
150 assert_int_equal(LY_EVALID, check_identifierchar(&ctx, ':', 0, &prefix));
151 logbuf_assert("Invalid identifier character ':'. Line number 1.");
Radek Krejci44ceedc2018-10-02 15:54:31 +0200152}
Radek Krejci80dd33e2018-09-26 15:57:18 +0200153
154static void
155test_comments(void **state)
156{
157 (void) state; /* unused */
158
Radek Krejci44ceedc2018-10-02 15:54:31 +0200159 struct ly_parser_ctx ctx;
Radek Krejci80dd33e2018-09-26 15:57:18 +0200160 const char *str, *p;
Radek Krejciefd22f62018-09-27 11:47:58 +0200161 char *word, *buf;
162 size_t len;
Radek Krejci80dd33e2018-09-26 15:57:18 +0200163
Radek Krejci44ceedc2018-10-02 15:54:31 +0200164 ctx.ctx = NULL;
165 ctx.line = 1;
166
Radek Krejciefd22f62018-09-27 11:47:58 +0200167 str = " // this is a text of / one * line */ comment\nargument";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200168 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200169 assert_string_equal("argument", word);
170 assert_null(buf);
171 assert_int_equal(8, len);
Radek Krejci80dd33e2018-09-26 15:57:18 +0200172
Radek Krejciefd22f62018-09-27 11:47:58 +0200173 str = "/* this is a \n * text // of / block * comment */\"arg\" + \"ume\" \n + \n \"nt\"";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200174 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200175 assert_string_equal("argument", word);
176 assert_ptr_equal(buf, word);
177 assert_int_equal(8, len);
178 free(word);
Radek Krejci80dd33e2018-09-26 15:57:18 +0200179
180 str = p = " this is one line comment on last line";
Radek Krejci44ceedc2018-10-02 15:54:31 +0200181 assert_int_equal(LY_SUCCESS, skip_comment(&ctx, &str, 1));
Radek Krejci80dd33e2018-09-26 15:57:18 +0200182 assert_true(str[0] == '\0');
183
184 str = p = " this is a not terminated comment x";
Radek Krejci44ceedc2018-10-02 15:54:31 +0200185 assert_int_equal(LY_EVALID, skip_comment(&ctx, &str, 2));
186 logbuf_assert("Unexpected end-of-file, non-terminated comment. Line number 5.");
Radek Krejci80dd33e2018-09-26 15:57:18 +0200187 assert_true(str[0] == '\0');
188}
189
Radek Krejciefd22f62018-09-27 11:47:58 +0200190static void
191test_arg(void **state)
192{
193 (void) state; /* unused */
194
Radek Krejci44ceedc2018-10-02 15:54:31 +0200195 struct ly_parser_ctx ctx;
Radek Krejciefd22f62018-09-27 11:47:58 +0200196 const char *str;
197 char *word, *buf;
198 size_t len;
199
Radek Krejci44ceedc2018-10-02 15:54:31 +0200200 ctx.ctx = NULL;
201 ctx.line = 1;
202
Radek Krejciefd22f62018-09-27 11:47:58 +0200203 /* missing argument */
204 str = ";";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200205 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_MAYBE_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200206 assert_null(word);
207
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200208 str = "{";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200209 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200210 logbuf_assert("Invalid character sequence \"{\", expected an argument. Line number 1.");
211
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200212 /* invalid escape sequence */
213 str = "\"\\s\"";
214 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200215 logbuf_assert("Double-quoted string unknown special character \'\\s\'. Line number 1.");
216 str = "\'\\s\'"; /* valid, since it is not an escape sequence in single quoted string */
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200217 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200218 assert_int_equal(2, len);
219 assert_string_equal("\\s\'", word);
220 assert_int_equal('\0', str[0]); /* input has been eaten */
221
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200222 /* invalid character after the argument */
223 str = "hello\"";
224 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
225 logbuf_assert("Invalid character sequence \"\"\", expected unquoted string character, optsep, semicolon or opening brace. Line number 1.");
226 str = "hello}";
227 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
228 logbuf_assert("Invalid character sequence \"}\", expected unquoted string character, optsep, semicolon or opening brace. Line number 1.");
229
230 str = "hello/x\t"; /* slash is not an invalid character */
231 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
232 assert_int_equal(7, len);
233 assert_string_equal("hello/x\t", word);
234
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200235 assert_null(buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200236
237 /* different quoting */
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200238 str = "hello ";
239 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200240 assert_null(buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200241 assert_int_equal(5, len);
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200242 assert_string_equal("hello ", word);
Radek Krejciefd22f62018-09-27 11:47:58 +0200243
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200244 str = "hello/*comment*/\n";
245 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200246 assert_null(buf);
247 assert_int_equal(5, len);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200248 assert_false(strncmp("hello", word, len));
249
250
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200251 str = "\"hello\\n\\t\\\"\\\\\";";
252 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200253 assert_null(buf);
254 assert_int_equal(9, len);
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200255 assert_string_equal("hello\\n\\t\\\"\\\\\";", word);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200256
257 ctx.indent = 14;
258 str = "\"hello \t\n\t\t world!\"";
259 /* - space and tabs before newline are stripped out
260 * - space and tabs after newline (indentation) are stripped out
261 */
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200262 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200263 assert_non_null(buf);
264 assert_ptr_equal(word, buf);
265 assert_int_equal(14, len);
266 assert_string_equal("hello\n world!", word);
267 free(buf);
268
269 ctx.indent = 14;
270 str = "\"hello\n \tworld!\"";
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(12, len);
275 assert_string_equal("hello\nworld!", word);
276 free(buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200277
278 str = "\'hello\'";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200279 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200280 assert_null(buf);
281 assert_int_equal(5, len);
282 assert_false(strncmp("hello", word, 5));
283
284 str = "\"hel\" +\t\n\"lo\"";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200285 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200286 assert_ptr_equal(word, buf);
287 assert_int_equal(5, len);
288 assert_string_equal("hello", word);
289 free(buf);
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200290 str = "\"hel\" +\t\nlo"; /* unquoted the second part */
291 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
292 logbuf_assert("Both string parts divided by '+' must be quoted. Line number 5.");
Radek Krejciefd22f62018-09-27 11:47:58 +0200293
294 str = "\'he\'\t\n+ \"llo\"";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200295 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200296 assert_ptr_equal(word, buf);
297 assert_int_equal(5, len);
298 assert_string_equal("hello", word);
299 free(buf);
300
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200301 str = " \t\n\"he\"+\'llo\'";
302 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 Krejci44ceedc2018-10-02 15:54:31 +0200308 /* missing argument */
309 str = ";";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200310 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
311 logbuf_assert("Invalid character sequence \";\", expected an argument. Line number 7.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200312}
313
314static void
315test_stmts(void **state)
316{
317 (void) state; /* unused */
318
319 struct ly_parser_ctx ctx;
320 const char *str, *p;
321 enum yang_keyword kw;
322 char *word;
323 size_t len;
324
325 ctx.ctx = NULL;
326 ctx.line = 1;
327
328 str = "\n// comment\n\tinput\t{";
329 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
330 assert_int_equal(YANG_INPUT, kw);
331 assert_int_equal(5, len);
332 assert_string_equal("input\t{", word);
333 assert_string_equal("\t{", str);
334
335 str = "\t /* comment */\t output\n\t{";
336 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
337 assert_int_equal(YANG_OUTPUT, kw);
338 assert_int_equal(6, len);
339 assert_string_equal("output\n\t{", word);
340 assert_string_equal("\n\t{", str);
341
342 str = "/input { "; /* invalid slash */
343 assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len));
344 logbuf_assert("Invalid identifier first character '/'. Line number 4.");
345
346 str = "not-a-statement-nor-extension { "; /* invalid identifier */
347 assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len));
348 logbuf_assert("Invalid character sequence \"not-a-statement-nor-extension\", expected a keyword. Line number 4.");
349
350 str = "path;"; /* missing sep after the keyword */
351 assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len));
352 logbuf_assert("Invalid character sequence \"path;\", expected a keyword followed by a separator. Line number 4.");
353
354 str = "action ";
355 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
356 assert_int_equal(YANG_ACTION, kw);
357 assert_int_equal(6, len);
358 str = "anydata ";
359 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
360 assert_int_equal(YANG_ANYDATA, kw);
361 assert_int_equal(7, len);
362 str = "anyxml ";
363 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
364 assert_int_equal(YANG_ANYXML, kw);
365 assert_int_equal(6, len);
366 str = "argument ";
367 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
368 assert_int_equal(YANG_ARGUMENT, kw);
369 assert_int_equal(8, len);
370 str = "augment ";
371 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
372 assert_int_equal(YANG_AUGMENT, kw);
373 assert_int_equal(7, len);
374 str = "base ";
375 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
376 assert_int_equal(YANG_BASE, kw);
377 assert_int_equal(4, len);
378 str = "belongs-to ";
379 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
380 assert_int_equal(YANG_BELONGS_TO, kw);
381 assert_int_equal(10, len);
382 str = "bit ";
383 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
384 assert_int_equal(YANG_BIT, kw);
385 assert_int_equal(3, len);
386 str = "case ";
387 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
388 assert_int_equal(YANG_CASE, kw);
389 assert_int_equal(4, len);
390 str = "choice ";
391 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
392 assert_int_equal(YANG_CHOICE, kw);
393 assert_int_equal(6, len);
394 str = "config ";
395 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
396 assert_int_equal(YANG_CONFIG, kw);
397 assert_int_equal(6, len);
398 str = "contact ";
399 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
400 assert_int_equal(YANG_CONTACT, kw);
401 assert_int_equal(7, len);
402 str = "container ";
403 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
404 assert_int_equal(YANG_CONTAINER, kw);
405 assert_int_equal(9, len);
406 str = "default ";
407 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
408 assert_int_equal(YANG_DEFAULT, kw);
409 assert_int_equal(7, len);
410 str = "description ";
411 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
412 assert_int_equal(YANG_DESCRIPTION, kw);
413 assert_int_equal(11, len);
414 str = "deviate ";
415 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
416 assert_int_equal(YANG_DEVIATE, kw);
417 assert_int_equal(7, len);
418 str = "deviation ";
419 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
420 assert_int_equal(YANG_DEVIATION, kw);
421 assert_int_equal(9, len);
422 str = "enum ";
423 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
424 assert_int_equal(YANG_ENUM, kw);
425 assert_int_equal(4, len);
426 str = "error-app-tag ";
427 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
428 assert_int_equal(YANG_ERROR_APP_TAG, kw);
429 assert_int_equal(13, len);
430 str = "error-message ";
431 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
432 assert_int_equal(YANG_ERROR_MESSAGE, kw);
433 assert_int_equal(13, len);
434 str = "extension ";
435 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
436 assert_int_equal(YANG_EXTENSION, kw);
437 assert_int_equal(9, len);
438 str = "feature ";
439 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
440 assert_int_equal(YANG_FEATURE, kw);
441 assert_int_equal(7, len);
442 str = "fraction-digits ";
443 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
444 assert_int_equal(YANG_FRACTION_DIGITS, kw);
445 assert_int_equal(15, len);
446 str = "grouping ";
447 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
448 assert_int_equal(YANG_GROUPING, kw);
449 assert_int_equal(8, len);
450 str = "identity ";
451 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
452 assert_int_equal(YANG_IDENTITY, kw);
453 assert_int_equal(8, len);
454 str = "if-feature ";
455 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
456 assert_int_equal(YANG_IF_FEATURE, kw);
457 assert_int_equal(10, len);
458 str = "import ";
459 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
460 assert_int_equal(YANG_IMPORT, kw);
461 assert_int_equal(6, len);
462 str = "include ";
463 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
464 assert_int_equal(YANG_INCLUDE, kw);
465 assert_int_equal(7, len);
466 str = "input{";
467 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
468 assert_int_equal(YANG_INPUT, kw);
469 assert_int_equal(5, len);
470 str = "key ";
471 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
472 assert_int_equal(YANG_KEY, kw);
473 assert_int_equal(3, len);
474 str = "leaf ";
475 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
476 assert_int_equal(YANG_LEAF, kw);
477 assert_int_equal(4, len);
478 str = "leaf-list ";
479 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
480 assert_int_equal(YANG_LEAF_LIST, kw);
481 assert_int_equal(9, len);
482 str = "length ";
483 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
484 assert_int_equal(YANG_LENGTH, kw);
485 assert_int_equal(6, len);
486 str = "list ";
487 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
488 assert_int_equal(YANG_LIST, kw);
489 assert_int_equal(4, len);
490 str = "mandatory ";
491 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
492 assert_int_equal(YANG_MANDATORY, kw);
493 assert_int_equal(9, len);
494 str = "max-elements ";
495 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
496 assert_int_equal(YANG_MAX_ELEMENTS, kw);
497 assert_int_equal(12, len);
498 str = "min-elements ";
499 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
500 assert_int_equal(YANG_MIN_ELEMENTS, kw);
501 assert_int_equal(12, len);
502 str = "modifier ";
503 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
504 assert_int_equal(YANG_MODIFIER, kw);
505 assert_int_equal(8, len);
506 str = "module ";
507 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
508 assert_int_equal(YANG_MODULE, kw);
509 assert_int_equal(6, len);
510 str = "must ";
511 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
512 assert_int_equal(YANG_MUST, kw);
513 assert_int_equal(4, len);
514 str = "namespace ";
515 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
516 assert_int_equal(YANG_NAMESPACE, kw);
517 assert_int_equal(9, len);
518 str = "notification ";
519 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
520 assert_int_equal(YANG_NOTIFICATION, kw);
521 assert_int_equal(12, len);
522 str = "ordered-by ";
523 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
524 assert_int_equal(YANG_ORDERED_BY, kw);
525 assert_int_equal(10, len);
526 str = "organization ";
527 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
528 assert_int_equal(YANG_ORGANIZATION, kw);
529 assert_int_equal(12, len);
530 str = "output ";
531 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
532 assert_int_equal(YANG_OUTPUT, kw);
533 assert_int_equal(6, len);
534 str = "path ";
535 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
536 assert_int_equal(YANG_PATH, kw);
537 assert_int_equal(4, len);
538 str = "pattern ";
539 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
540 assert_int_equal(YANG_PATTERN, kw);
541 assert_int_equal(7, len);
542 str = "position ";
543 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
544 assert_int_equal(YANG_POSITION, kw);
545 assert_int_equal(8, len);
546 str = "prefix ";
547 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
548 assert_int_equal(YANG_PREFIX, kw);
549 assert_int_equal(6, len);
550 str = "presence ";
551 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
552 assert_int_equal(YANG_PRESENCE, kw);
553 assert_int_equal(8, len);
554 str = "range ";
555 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
556 assert_int_equal(YANG_RANGE, kw);
557 assert_int_equal(5, len);
558 str = "reference ";
559 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
560 assert_int_equal(YANG_REFERENCE, kw);
561 assert_int_equal(9, len);
562 str = "refine ";
563 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
564 assert_int_equal(YANG_REFINE, kw);
565 assert_int_equal(6, len);
566 str = "require-instance ";
567 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
568 assert_int_equal(YANG_REQUIRE_INSTANCE, kw);
569 assert_int_equal(16, len);
570 str = "revision ";
571 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
572 assert_int_equal(YANG_REVISION, kw);
573 assert_int_equal(8, len);
574 str = "revision-date ";
575 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
576 assert_int_equal(YANG_REVISION_DATE, kw);
577 assert_int_equal(13, len);
578 str = "rpc ";
579 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
580 assert_int_equal(YANG_RPC, kw);
581 assert_int_equal(3, len);
582 str = "status ";
583 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
584 assert_int_equal(YANG_STATUS, kw);
585 assert_int_equal(6, len);
586 str = "submodule ";
587 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
588 assert_int_equal(YANG_SUBMODULE, kw);
589 assert_int_equal(9, len);
590 str = "type ";
591 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
592 assert_int_equal(YANG_TYPE, kw);
593 assert_int_equal(4, len);
594 str = "typedef ";
595 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
596 assert_int_equal(YANG_TYPEDEF, kw);
597 assert_int_equal(7, len);
598 str = "unique ";
599 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
600 assert_int_equal(YANG_UNIQUE, kw);
601 assert_int_equal(6, len);
602 str = "units ";
603 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
604 assert_int_equal(YANG_UNITS, kw);
605 assert_int_equal(5, len);
606 str = "uses ";
607 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
608 assert_int_equal(YANG_USES, kw);
609 assert_int_equal(4, len);
610 str = "value ";
611 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
612 assert_int_equal(YANG_VALUE, kw);
613 assert_int_equal(5, len);
614 str = "when ";
615 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
616 assert_int_equal(YANG_WHEN, kw);
617 assert_int_equal(4, len);
618 str = "yang-version ";
619 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
620 assert_int_equal(YANG_YANG_VERSION, kw);
621 assert_int_equal(12, len);
622 str = "yin-element ";
623 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
624 assert_int_equal(YANG_YIN_ELEMENT, kw);
625 assert_int_equal(11, len);
Radek Krejci626df482018-10-11 15:06:31 +0200626 str = ";config false;";
627 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
628 assert_int_equal(YANG_SEMICOLON, kw);
629 assert_int_equal(1, len);
630 assert_string_equal("config false;", str);
631 str = "{ config false;";
632 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
633 assert_int_equal(YANG_LEFT_BRACE, kw);
634 assert_int_equal(1, len);
635 assert_string_equal(" config false;", str);
636 str = "}";
637 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
638 assert_int_equal(YANG_RIGHT_BRACE, kw);
639 assert_int_equal(1, len);
640 assert_string_equal("", str);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200641
642 /* geenric extension */
643 str = p = "nacm:default-deny-write;";
644 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
645 assert_int_equal(YANG_CUSTOM, kw);
646 assert_int_equal(23, len);
647 assert_ptr_equal(p, word);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200648}
Radek Krejci44ceedc2018-10-02 15:54:31 +0200649
Radek Krejci9fcacc12018-10-11 15:59:11 +0200650static struct lysp_module *
Radek Krejci09306362018-10-15 15:26:01 +0200651mod_renew(struct ly_parser_ctx *ctx, struct lysp_module *mod, uint8_t submodule)
Radek Krejci9fcacc12018-10-11 15:59:11 +0200652{
653 lysp_module_free(mod);
654 mod = calloc(1, sizeof *mod);
655 mod->ctx = ctx->ctx;
Radek Krejci09306362018-10-15 15:26:01 +0200656 mod->submodule = submodule;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200657 assert_non_null(mod);
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100658 ctx->mod = mod;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200659 return mod;
660}
661
Radek Krejcid33273d2018-10-25 14:55:52 +0200662static LY_ERR test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
663 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format,
664 const char **module_data, void (**free_module_data)(void *model_data, void *user_data))
665{
666 *module_data = user_data;
667 *format = LYS_IN_YANG;
668 *free_module_data = NULL;
669 return LY_SUCCESS;
670}
671
Radek Krejci9fcacc12018-10-11 15:59:11 +0200672static void
673test_module(void **state)
674{
675 (void) state; /* unused */
676
677 struct ly_parser_ctx ctx;
678 struct lysp_module *mod;
679 const char *str;
680
681 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
682 assert_non_null(ctx.ctx);
683 ctx.line = 1;
Radek Krejcia042ea12018-10-13 07:52:15 +0200684 ctx.indent = 0;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200685
Radek Krejci09306362018-10-15 15:26:01 +0200686 mod = mod_renew(&ctx, NULL, 0);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200687
688 /* missing mandatory substatements */
689 str = " name {}";
690 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
691 assert_string_equal("name", mod->name);
692 logbuf_assert("Missing mandatory keyword \"namespace\" as a child of \"module\". Line number 1.");
Radek Krejci09306362018-10-15 15:26:01 +0200693 mod = mod_renew(&ctx, mod, 0);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200694
695 str = " name {namespace urn:x;}";
696 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
697 assert_string_equal("urn:x", mod->ns);
698 logbuf_assert("Missing mandatory keyword \"prefix\" as a child of \"module\". Line number 1.");
Radek Krejci09306362018-10-15 15:26:01 +0200699 mod = mod_renew(&ctx, mod, 0);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200700
701 str = " name {namespace urn:x;prefix \"x\";}";
702 assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod));
703 assert_string_equal("x", mod->prefix);
Radek Krejci09306362018-10-15 15:26:01 +0200704 mod = mod_renew(&ctx, mod, 0);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200705
Radek Krejci027d5802018-11-14 16:57:28 +0100706#define SCHEMA_BEGINNING " name {yang-version 1.1;namespace urn:x;prefix \"x\";"
707#define SCHEMA_BEGINNING2 " name {namespace urn:x;prefix \"x\";"
Radek Krejcia042ea12018-10-13 07:52:15 +0200708#define TEST_NODE(NODETYPE, INPUT, NAME) \
709 str = SCHEMA_BEGINNING INPUT; \
710 assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod)); \
711 assert_non_null(mod->data); \
712 assert_int_equal(NODETYPE, mod->data->nodetype); \
713 assert_string_equal(NAME, mod->data->name); \
Radek Krejci09306362018-10-15 15:26:01 +0200714 mod = mod_renew(&ctx, mod, 0);
Radek Krejcia042ea12018-10-13 07:52:15 +0200715#define TEST_GENERIC(INPUT, TARGET, TEST) \
716 str = SCHEMA_BEGINNING INPUT; \
717 assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod)); \
718 assert_non_null(TARGET); \
719 TEST; \
Radek Krejci09306362018-10-15 15:26:01 +0200720 mod = mod_renew(&ctx, mod, 0);
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200721#define TEST_DUP(MEMBER, VALUE1, VALUE2, LINE, SUBMODULE) \
722 TEST_DUP_GENERIC(SCHEMA_BEGINNING, MEMBER, VALUE1, VALUE2, \
723 parse_sub_module, mod, LINE, mod = mod_renew(&ctx, mod, SUBMODULE))
Radek Krejcia042ea12018-10-13 07:52:15 +0200724
725 /* duplicated namespace, prefix */
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200726 TEST_DUP("namespace", "y", "z", "1", 0);
727 TEST_DUP("prefix", "y", "z", "1", 0);
728 TEST_DUP("contact", "a", "b", "1", 0);
729 TEST_DUP("description", "a", "b", "1", 0);
730 TEST_DUP("organization", "a", "b", "1", 0);
731 TEST_DUP("reference", "a", "b", "1", 0);
Radek Krejcia042ea12018-10-13 07:52:15 +0200732
Radek Krejci70853c52018-10-15 14:46:16 +0200733 /* not allowed in module (submodule-specific) */
734 str = SCHEMA_BEGINNING "belongs-to master {prefix m;}}";
735 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
736 logbuf_assert("Invalid keyword \"belongs-to\" as a child of \"module\". Line number 1.");
Radek Krejci09306362018-10-15 15:26:01 +0200737 mod = mod_renew(&ctx, mod, 0);
Radek Krejci70853c52018-10-15 14:46:16 +0200738
Radek Krejcia042ea12018-10-13 07:52:15 +0200739 /* anydata */
740 TEST_NODE(LYS_ANYDATA, "anydata test;}", "test");
741 /* anyxml */
742 TEST_NODE(LYS_ANYXML, "anyxml test;}", "test");
743 /* augment */
744 TEST_GENERIC("augment /somepath;}", mod->augments,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200745 assert_string_equal("/somepath", mod->augments[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200746 /* choice */
747 TEST_NODE(LYS_CHOICE, "choice test;}", "test");
748 /* contact 0..1 */
749 TEST_GENERIC("contact \"firstname\" + \n\t\" surname\";}", mod->contact,
750 assert_string_equal("firstname surname", mod->contact));
751 /* container */
752 TEST_NODE(LYS_CONTAINER, "container test;}", "test");
753 /* description 0..1 */
754 TEST_GENERIC("description \'some description\';}", mod->dsc,
755 assert_string_equal("some description", mod->dsc));
756 /* deviation */
757 TEST_GENERIC("deviation /somepath {deviate not-supported;}}", mod->deviations,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200758 assert_string_equal("/somepath", mod->deviations[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200759 /* extension */
760 TEST_GENERIC("extension test;}", mod->extensions,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200761 assert_string_equal("test", mod->extensions[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200762 /* feature */
763 TEST_GENERIC("feature test;}", mod->features,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200764 assert_string_equal("test", mod->features[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200765 /* grouping */
766 TEST_GENERIC("grouping grp;}", mod->groupings,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200767 assert_string_equal("grp", mod->groupings[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200768 /* identity */
769 TEST_GENERIC("identity test;}", mod->identities,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200770 assert_string_equal("test", mod->identities[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200771 /* import */
Radek Krejci086c7132018-10-26 15:29:04 +0200772 ly_ctx_set_module_imp_clb(ctx.ctx, test_imp_clb, "module zzz { namespace urn:zzz; prefix z;}");
773 TEST_GENERIC("import zzz {prefix z;}}", mod->imports,
774 assert_string_equal("zzz", mod->imports[0].name));
Radek Krejci70853c52018-10-15 14:46:16 +0200775
Radek Krejcia042ea12018-10-13 07:52:15 +0200776 /* import - prefix collision */
Radek Krejci086c7132018-10-26 15:29:04 +0200777 str = SCHEMA_BEGINNING "import zzz {prefix x;}}";
Radek Krejcia042ea12018-10-13 07:52:15 +0200778 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
Radek Krejci70853c52018-10-15 14:46:16 +0200779 logbuf_assert("Prefix \"x\" already used as module prefix. Line number 2.");
Radek Krejci09306362018-10-15 15:26:01 +0200780 mod = mod_renew(&ctx, mod, 0);
Radek Krejci086c7132018-10-26 15:29:04 +0200781 str = SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix y;}}";
Radek Krejci86d106e2018-10-18 09:53:19 +0200782 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
Radek Krejci086c7132018-10-26 15:29:04 +0200783 logbuf_assert("Prefix \"y\" already used to import \"zzz\" module. Line number 2.");
Radek Krejci86d106e2018-10-18 09:53:19 +0200784 mod = mod_renew(&ctx, mod, 0);
Radek Krejci086c7132018-10-26 15:29:04 +0200785 str = "module" SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix z;}}";
786 assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
787 assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx));
788 logbuf_assert("Single revision of the module \"zzz\" referred twice.");
Radek Krejci70853c52018-10-15 14:46:16 +0200789
Radek Krejcia042ea12018-10-13 07:52:15 +0200790 /* include */
Radek Krejci9ed7a192018-10-31 16:23:51 +0100791 store = 1;
Radek Krejcid33273d2018-10-25 14:55:52 +0200792 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 +0200793 str = "module" SCHEMA_BEGINNING "include xxx;}";
794 assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
795 assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx));
796 logbuf_assert("Included \"xxx\" schema from \"name\" is actually not a submodule.");
Radek Krejci9ed7a192018-10-31 16:23:51 +0100797 store = -1;
Radek Krejcid33273d2018-10-25 14:55:52 +0200798
Radek Krejci9ed7a192018-10-31 16:23:51 +0100799 store = 1;
Radek Krejci313d9902018-11-08 09:42:58 +0100800 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 +0200801 str = "module" SCHEMA_BEGINNING "include xxx;}";
802 assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
803 assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx));
804 logbuf_assert("Included \"xxx\" submodule from \"name\" belongs-to a different module \"wrong-name\".");
Radek Krejci9ed7a192018-10-31 16:23:51 +0100805 store = -1;
Radek Krejcid33273d2018-10-25 14:55:52 +0200806
Radek Krejci313d9902018-11-08 09:42:58 +0100807 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 +0200808 TEST_GENERIC("include xxx;}", mod->includes,
Radek Krejci086c7132018-10-26 15:29:04 +0200809 assert_string_equal("xxx", mod->includes[0].name));
Radek Krejcid33273d2018-10-25 14:55:52 +0200810
Radek Krejcia042ea12018-10-13 07:52:15 +0200811 /* leaf */
812 TEST_NODE(LYS_LEAF, "leaf test {type string;}}", "test");
813 /* leaf-list */
814 TEST_NODE(LYS_LEAFLIST, "leaf-list test {type string;}}", "test");
815 /* list */
816 TEST_NODE(LYS_LIST, "list test {key a;leaf a {type string;}}}", "test");
817 /* notification */
818 TEST_GENERIC("notification test;}", mod->notifs,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200819 assert_string_equal("test", mod->notifs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200820 /* organization 0..1 */
821 TEST_GENERIC("organization \"CESNET a.l.e.\";}", mod->org,
822 assert_string_equal("CESNET a.l.e.", mod->org));
823 /* reference 0..1 */
824 TEST_GENERIC("reference RFC7950;}", mod->ref,
825 assert_string_equal("RFC7950", mod->ref));
826 /* revision */
827 TEST_GENERIC("revision 2018-10-12;}", mod->revs,
Radek Krejcib7db73a2018-10-24 14:18:40 +0200828 assert_string_equal("2018-10-12", mod->revs[0].date));
Radek Krejcia042ea12018-10-13 07:52:15 +0200829 /* rpc */
830 TEST_GENERIC("rpc test;}", mod->rpcs,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200831 assert_string_equal("test", mod->rpcs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200832 /* typedef */
833 TEST_GENERIC("typedef test{type string;}}", mod->typedefs,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200834 assert_string_equal("test", mod->typedefs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200835 /* uses */
836 TEST_NODE(LYS_USES, "uses test;}", "test");
837 /* yang-version */
Radek Krejci027d5802018-11-14 16:57:28 +0100838 str = SCHEMA_BEGINNING2 "\n\tyang-version 10;}";
Radek Krejcia042ea12018-10-13 07:52:15 +0200839 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
840 logbuf_assert("Invalid value \"10\" of \"yang-version\". Line number 3.");
Radek Krejci09306362018-10-15 15:26:01 +0200841 mod = mod_renew(&ctx, mod, 0);
Radek Krejci027d5802018-11-14 16:57:28 +0100842 str = SCHEMA_BEGINNING2 "yang-version 1.0;yang-version 1.1;}";
Radek Krejcia042ea12018-10-13 07:52:15 +0200843 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
844 logbuf_assert("Duplicate keyword \"yang-version\". Line number 3.");
Radek Krejci09306362018-10-15 15:26:01 +0200845 mod = mod_renew(&ctx, mod, 0);
Radek Krejci027d5802018-11-14 16:57:28 +0100846 str = SCHEMA_BEGINNING2 "yang-version 1.0;}";
Radek Krejcia042ea12018-10-13 07:52:15 +0200847 assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod));
848 assert_int_equal(1, mod->version);
Radek Krejci09306362018-10-15 15:26:01 +0200849 mod = mod_renew(&ctx, mod, 0);
Radek Krejci027d5802018-11-14 16:57:28 +0100850 str = SCHEMA_BEGINNING2 "yang-version \"1.1\";}";
Radek Krejcia042ea12018-10-13 07:52:15 +0200851 assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod));
852 assert_int_equal(2, mod->version);
Radek Krejci09306362018-10-15 15:26:01 +0200853 mod = mod_renew(&ctx, mod, 0);
854
Radek Krejci156ccaf2018-10-15 15:49:17 +0200855 /* extensions */
856 TEST_GENERIC("prefix:test;}", mod->exts,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200857 assert_string_equal("prefix:test", mod->exts[0].name);
858 assert_int_equal(LYEXT_SUBSTMT_SELF, mod->exts[0].insubstmt));
Radek Krejci156ccaf2018-10-15 15:49:17 +0200859 mod = mod_renew(&ctx, mod, 0);
860
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200861 /* invalid substatement */
862 str = SCHEMA_BEGINNING "must false;}";
863 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
864 logbuf_assert("Invalid keyword \"must\" as a child of \"module\". Line number 3.");
865 mod = mod_renew(&ctx, mod, 0);
866
Radek Krejci09306362018-10-15 15:26:01 +0200867 /* submodule */
868 mod->submodule = 1;
869
870 /* missing mandatory substatements */
871 str = " subname {}";
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100872 lydict_remove(ctx.ctx, mod->name);
Radek Krejci09306362018-10-15 15:26:01 +0200873 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
874 assert_string_equal("subname", mod->name);
875 logbuf_assert("Missing mandatory keyword \"belongs-to\" as a child of \"submodule\". Line number 3.");
876 mod = mod_renew(&ctx, mod, 1);
877
Radek Krejci313d9902018-11-08 09:42:58 +0100878 str = " subname {belongs-to name {prefix x;}}";
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_SUCCESS, parse_sub_module(&ctx, &str, mod));
881 assert_string_equal("name", mod->belongsto);
882 mod = mod_renew(&ctx, mod, 1);
883
884#undef SCHEMA_BEGINNING
Radek Krejci313d9902018-11-08 09:42:58 +0100885#define SCHEMA_BEGINNING " subname {belongs-to name {prefix x;}"
Radek Krejci09306362018-10-15 15:26:01 +0200886
887 /* duplicated namespace, prefix */
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200888 TEST_DUP("belongs-to", "module1", "module2", "3", 1);
Radek Krejci09306362018-10-15 15:26:01 +0200889
890 /* not allowed in submodule (module-specific) */
891 str = SCHEMA_BEGINNING "namespace \"urn:z\";}";
892 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
893 logbuf_assert("Invalid keyword \"namespace\" as a child of \"submodule\". Line number 3.");
894 mod = mod_renew(&ctx, mod, 1);
895 str = SCHEMA_BEGINNING "prefix m;}}";
896 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
897 logbuf_assert("Invalid keyword \"prefix\" as a child of \"submodule\". Line number 3.");
898 mod = mod_renew(&ctx, mod, 1);
Radek Krejcia042ea12018-10-13 07:52:15 +0200899
900#undef TEST_GENERIC
901#undef TEST_NODE
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200902#undef TEST_DUP
Radek Krejcia042ea12018-10-13 07:52:15 +0200903#undef SCHEMA_BEGINNING
904
Radek Krejci9fcacc12018-10-11 15:59:11 +0200905 lysp_module_free(mod);
906 ly_ctx_destroy(ctx.ctx, NULL);
Radek Krejciefd22f62018-09-27 11:47:58 +0200907}
908
Radek Krejci4c6d9bd2018-10-15 16:43:06 +0200909static void
910test_identity(void **state)
911{
912 (void) state; /* unused */
913
914 struct ly_parser_ctx ctx;
Radek Krejci027d5802018-11-14 16:57:28 +0100915 struct lysp_module *mod = NULL, m = {0};
Radek Krejci4c6d9bd2018-10-15 16:43:06 +0200916 struct lysp_ident *ident = NULL;
917 const char *str;
Radek Krejci4c6d9bd2018-10-15 16:43:06 +0200918
919 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
920 assert_non_null(ctx.ctx);
921 ctx.line = 1;
922 ctx.indent = 0;
Radek Krejci027d5802018-11-14 16:57:28 +0100923 ctx.mod = &m;
924 ctx.mod->version = 2; /* simulate YANG 1.1 */
Radek Krejci4c6d9bd2018-10-15 16:43:06 +0200925
926 /* invalid cardinality */
927#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200928 TEST_DUP_GENERIC(" test {", MEMBER, VALUE1, VALUE2, parse_identity, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +0200929 &ident, "1", FREE_ARRAY(ctx.ctx, ident, lysp_ident_free); ident = NULL)
Radek Krejci4c6d9bd2018-10-15 16:43:06 +0200930
931 TEST_DUP("description", "a", "b");
932 TEST_DUP("reference", "a", "b");
933 TEST_DUP("status", "current", "obsolete");
934
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200935 /* full content */
936 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 +0200937 assert_int_equal(LY_SUCCESS, parse_identity(&ctx, &str, &ident));
938 assert_non_null(ident);
939 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +0200940 FREE_ARRAY(ctx.ctx, ident, lysp_ident_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200941 ident = NULL;
942
943 /* invalid substatement */
944 str = " test {organization XXX;}";
945 assert_int_equal(LY_EVALID, parse_identity(&ctx, &str, &ident));
946 logbuf_assert("Invalid keyword \"organization\" as a child of \"identity\". Line number 1.");
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
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100950 /* identity duplication */
951 str = "module a {namespace urn:a; prefix a; identity a; identity a;}";
Radek Krejci313d9902018-11-08 09:42:58 +0100952 assert_int_equal(LY_EVALID, yang_parse(&ctx, str, &mod));
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100953 logbuf_assert("Duplicate identifier \"a\" of identity statement. Line number 1.");
954 assert_null(mod);
955
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200956#undef TEST_DUP
957
958 ly_ctx_destroy(ctx.ctx, NULL);
959}
960
961static void
962test_feature(void **state)
963{
964 (void) state; /* unused */
965
966 struct ly_parser_ctx ctx;
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100967 struct lysp_module *mod = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200968 struct lysp_feature *features = NULL;
969 const char *str;
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200970
971 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
972 assert_non_null(ctx.ctx);
973 ctx.line = 1;
974 ctx.indent = 0;
975
976 /* invalid cardinality */
977#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
978 TEST_DUP_GENERIC(" test {", MEMBER, VALUE1, VALUE2, parse_feature, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +0200979 &features, "1", FREE_ARRAY(ctx.ctx, features, lysp_feature_free); features = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200980
981 TEST_DUP("description", "a", "b");
982 TEST_DUP("reference", "a", "b");
983 TEST_DUP("status", "current", "obsolete");
984
985 /* full content */
986 str = " test {description text;reference \'another text\';status current; if-feature x;if-feature y;prefix:ext;} ...";
987 assert_int_equal(LY_SUCCESS, parse_feature(&ctx, &str, &features));
988 assert_non_null(features);
989 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +0200990 FREE_ARRAY(ctx.ctx, features, lysp_feature_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200991 features = NULL;
992
993 /* invalid substatement */
994 str = " test {organization XXX;}";
995 assert_int_equal(LY_EVALID, parse_feature(&ctx, &str, &features));
996 logbuf_assert("Invalid keyword \"organization\" as a child of \"feature\". Line number 1.");
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
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001000 /* feature duplication */
1001 str = "module a {namespace urn:a; prefix a; feature a; feature a;}";
Radek Krejci313d9902018-11-08 09:42:58 +01001002 assert_int_equal(LY_EVALID, yang_parse(&ctx, str, &mod));
Radek Krejcifaa1eac2018-10-30 14:34:55 +01001003 logbuf_assert("Duplicate identifier \"a\" of feature statement. Line number 1.");
1004 assert_null(mod);
1005
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001006#undef TEST_DUP
1007
1008 ly_ctx_destroy(ctx.ctx, NULL);
1009}
1010
1011static void
1012test_deviation(void **state)
1013{
1014 (void) state; /* unused */
1015
1016 struct ly_parser_ctx ctx;
1017 struct lysp_deviation *d = NULL;
1018 const char *str;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001019
1020 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1021 assert_non_null(ctx.ctx);
1022 ctx.line = 1;
1023 ctx.indent = 0;
1024
1025 /* invalid cardinality */
1026#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1027 TEST_DUP_GENERIC(" test {deviate not-supported;", MEMBER, VALUE1, VALUE2, parse_deviation, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001028 &d, "1", FREE_ARRAY(ctx.ctx, d, lysp_deviation_free); d = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001029
1030 TEST_DUP("description", "a", "b");
1031 TEST_DUP("reference", "a", "b");
1032
1033 /* full content */
1034 str = " test {deviate not-supported;description text;reference \'another text\';prefix:ext;} ...";
1035 assert_int_equal(LY_SUCCESS, parse_deviation(&ctx, &str, &d));
1036 assert_non_null(d);
1037 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +02001038 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001039 d = NULL;
1040
1041 /* missing mandatory substatement */
1042 str = " test {description text;}";
1043 assert_int_equal(LY_EVALID, parse_deviation(&ctx, &str, &d));
1044 logbuf_assert("Missing mandatory keyword \"deviate\" as a child of \"deviation\". Line number 1.");
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 /* invalid substatement */
1049 str = " test {deviate not-supported; status obsolete;}";
1050 assert_int_equal(LY_EVALID, parse_deviation(&ctx, &str, &d));
1051 logbuf_assert("Invalid keyword \"status\" 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#undef TEST_DUP
1056
1057 ly_ctx_destroy(ctx.ctx, NULL);
1058}
1059
1060static void
1061test_deviate(void **state)
1062{
1063 (void) state; /* unused */
1064
1065 struct ly_parser_ctx ctx;
1066 struct lysp_deviate *d = NULL;
1067 const char *str;
1068
1069 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1070 assert_non_null(ctx.ctx);
1071 ctx.line = 1;
1072 ctx.indent = 0;
1073
1074 /* invalid cardinality */
1075#define TEST_DUP(TYPE, MEMBER, VALUE1, VALUE2) \
1076 TEST_DUP_GENERIC(TYPE" {", MEMBER, VALUE1, VALUE2, parse_deviate, \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001077 &d, "1", lysp_deviate_free(ctx.ctx, d); free(d); d = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001078
1079 TEST_DUP("add", "config", "true", "false");
1080 TEST_DUP("replace", "default", "int8", "uint8");
1081 TEST_DUP("add", "mandatory", "true", "false");
1082 TEST_DUP("add", "max-elements", "1", "2");
1083 TEST_DUP("add", "min-elements", "1", "2");
1084 TEST_DUP("replace", "type", "int8", "uint8");
1085 TEST_DUP("add", "units", "kilometers", "miles");
1086
1087 /* full contents */
1088 str = " not-supported {prefix:ext;} ...";
1089 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1090 assert_non_null(d);
1091 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001092 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001093 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;} ...";
1094 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1095 assert_non_null(d);
1096 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001097 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001098 str = " delete {units meters; must 1; must 2; unique x; unique y; default a; default b; prefix:ext;} ...";
1099 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1100 assert_non_null(d);
1101 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001102 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001103 str = " replace {type string; units meters; default a; config true; mandatory true; min-elements 1; max-elements 2; prefix:ext;} ...";
1104 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1105 assert_non_null(d);
1106 assert_string_equal(" ...", str);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001107 lysp_deviate_free(ctx.ctx, d); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001108
1109 /* invalid substatements */
1110#define TEST_NOT_SUP(DEV, STMT, VALUE) \
1111 str = " "DEV" {"STMT" "VALUE";}..."; \
1112 assert_int_equal(LY_EVALID, parse_deviate(&ctx, &str, &d)); \
1113 logbuf_assert("Deviate \""DEV"\" does not support keyword \""STMT"\". Line number 1."); \
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 TEST_NOT_SUP("not-supported", "units", "meters");
1117 TEST_NOT_SUP("not-supported", "must", "1");
1118 TEST_NOT_SUP("not-supported", "unique", "x");
1119 TEST_NOT_SUP("not-supported", "default", "a");
1120 TEST_NOT_SUP("not-supported", "config", "true");
1121 TEST_NOT_SUP("not-supported", "mandatory", "true");
1122 TEST_NOT_SUP("not-supported", "min-elements", "1");
1123 TEST_NOT_SUP("not-supported", "max-elements", "2");
1124 TEST_NOT_SUP("not-supported", "type", "string");
1125 TEST_NOT_SUP("add", "type", "string");
1126 TEST_NOT_SUP("delete", "config", "true");
1127 TEST_NOT_SUP("delete", "mandatory", "true");
1128 TEST_NOT_SUP("delete", "min-elements", "1");
1129 TEST_NOT_SUP("delete", "max-elements", "2");
1130 TEST_NOT_SUP("delete", "type", "string");
1131 TEST_NOT_SUP("replace", "must", "1");
1132 TEST_NOT_SUP("replace", "unique", "a");
1133
1134 str = " nonsence; ...";
1135 assert_int_equal(LY_EVALID, parse_deviate(&ctx, &str, &d));
1136 logbuf_assert("Invalid value \"nonsence\" of \"deviate\". Line number 1.");
1137 assert_null(d);
1138
1139#undef TEST_NOT_SUP
1140#undef TEST_DUP
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001141
1142 ly_ctx_destroy(ctx.ctx, NULL);
1143}
1144
Radek Krejci8c370832018-11-02 15:10:03 +01001145static void
1146test_container(void **state)
1147{
1148 (void) state; /* unused */
1149
Radek Krejci313d9902018-11-08 09:42:58 +01001150 struct lysp_module mod = {0};
1151 struct ly_parser_ctx ctx = {0};
Radek Krejci8c370832018-11-02 15:10:03 +01001152 struct lysp_node_container *c = NULL;
1153 const char *str;
1154
1155 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1156 assert_non_null(ctx.ctx);
1157 ctx.line = 1;
Radek Krejci313d9902018-11-08 09:42:58 +01001158 ctx.mod = &mod;
Radek Krejci027d5802018-11-14 16:57:28 +01001159 ctx.mod->version = 2; /* simulate YANG 1.1 */
Radek Krejci8c370832018-11-02 15:10:03 +01001160
1161 /* invalid cardinality */
1162#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1163 str = "cont {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1164 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c)); \
1165 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
Radek Krejci4f28eda2018-11-12 11:46:16 +01001166 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001167
1168 TEST_DUP("config", "true", "false");
1169 TEST_DUP("description", "text1", "text2");
1170 TEST_DUP("presence", "true", "false");
1171 TEST_DUP("reference", "1", "2");
1172 TEST_DUP("status", "current", "obsolete");
1173 TEST_DUP("when", "true", "false");
1174#undef TEST_DUP
1175
1176 /* full content */
Radek Krejci313d9902018-11-08 09:42:58 +01001177 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;}"
1178 "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 +01001179 assert_int_equal(LY_SUCCESS, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1180 assert_non_null(c);
1181 assert_non_null(c->actions);
1182 assert_non_null(c->child);
1183 assert_string_equal("test", c->dsc);
1184 assert_non_null(c->exts);
1185 assert_non_null(c->groupings);
1186 assert_non_null(c->iffeatures);
1187 assert_non_null(c->musts);
1188 assert_non_null(c->notifs);
1189 assert_string_equal("true", c->presence);
1190 assert_string_equal("test", c->ref);
1191 assert_non_null(c->typedefs);
1192 assert_non_null(c->when);
1193 assert_null(c->parent);
1194 assert_null(c->next);
1195 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR, c->flags);
Radek Krejci313d9902018-11-08 09:42:58 +01001196 ly_set_erase(&ctx.tpdfs_nodes, NULL);
Radek Krejci4f28eda2018-11-12 11:46:16 +01001197 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001198
1199 /* invalid */
1200 str = " cont {augment /root;} ...";
1201 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1202 logbuf_assert("Invalid keyword \"augment\" as a child of \"container\". Line number 1.");
Radek Krejci4f28eda2018-11-12 11:46:16 +01001203 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001204 str = " cont {nonsence true;} ...";
1205 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1206 logbuf_assert("Invalid character sequence \"nonsence\", expected a keyword. Line number 1.");
Radek Krejci4f28eda2018-11-12 11:46:16 +01001207 lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL;
Radek Krejci8c370832018-11-02 15:10:03 +01001208
1209 ly_ctx_destroy(ctx.ctx, NULL);
1210}
1211
Radek Krejci80dd33e2018-09-26 15:57:18 +02001212int main(void)
1213{
1214 const struct CMUnitTest tests[] = {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001215 cmocka_unit_test_setup(test_helpers, logger_setup),
Radek Krejci80dd33e2018-09-26 15:57:18 +02001216 cmocka_unit_test_setup(test_comments, logger_setup),
Radek Krejciefd22f62018-09-27 11:47:58 +02001217 cmocka_unit_test_setup(test_arg, logger_setup),
Radek Krejcidcc7b322018-10-11 14:24:02 +02001218 cmocka_unit_test_setup(test_stmts, logger_setup),
Radek Krejci9fcacc12018-10-11 15:59:11 +02001219 cmocka_unit_test_setup(test_module, logger_setup),
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001220 cmocka_unit_test_setup(test_identity, logger_setup),
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001221 cmocka_unit_test_setup(test_feature, logger_setup),
1222 cmocka_unit_test_setup(test_deviation, logger_setup),
1223 cmocka_unit_test_setup(test_deviate, logger_setup),
Radek Krejci8c370832018-11-02 15:10:03 +01001224 cmocka_unit_test_setup(test_container, logger_setup),
Radek Krejci80dd33e2018-09-26 15:57:18 +02001225 };
1226
1227 return cmocka_run_group_tests(tests, NULL, NULL);
1228}