blob: 851f87cb0bf5be5a1e43b156fdb597e79b55853a [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 Krejcia042ea12018-10-13 07:52:15 +0200706#define SCHEMA_BEGINNING " name {namespace urn:x;prefix \"x\";"
707#define TEST_NODE(NODETYPE, INPUT, NAME) \
708 str = SCHEMA_BEGINNING INPUT; \
709 assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod)); \
710 assert_non_null(mod->data); \
711 assert_int_equal(NODETYPE, mod->data->nodetype); \
712 assert_string_equal(NAME, mod->data->name); \
Radek Krejci09306362018-10-15 15:26:01 +0200713 mod = mod_renew(&ctx, mod, 0);
Radek Krejcia042ea12018-10-13 07:52:15 +0200714#define TEST_GENERIC(INPUT, TARGET, TEST) \
715 str = SCHEMA_BEGINNING INPUT; \
716 assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod)); \
717 assert_non_null(TARGET); \
718 TEST; \
Radek Krejci09306362018-10-15 15:26:01 +0200719 mod = mod_renew(&ctx, mod, 0);
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200720#define TEST_DUP(MEMBER, VALUE1, VALUE2, LINE, SUBMODULE) \
721 TEST_DUP_GENERIC(SCHEMA_BEGINNING, MEMBER, VALUE1, VALUE2, \
722 parse_sub_module, mod, LINE, mod = mod_renew(&ctx, mod, SUBMODULE))
Radek Krejcia042ea12018-10-13 07:52:15 +0200723
724 /* duplicated namespace, prefix */
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200725 TEST_DUP("namespace", "y", "z", "1", 0);
726 TEST_DUP("prefix", "y", "z", "1", 0);
727 TEST_DUP("contact", "a", "b", "1", 0);
728 TEST_DUP("description", "a", "b", "1", 0);
729 TEST_DUP("organization", "a", "b", "1", 0);
730 TEST_DUP("reference", "a", "b", "1", 0);
Radek Krejcia042ea12018-10-13 07:52:15 +0200731
Radek Krejci70853c52018-10-15 14:46:16 +0200732 /* not allowed in module (submodule-specific) */
733 str = SCHEMA_BEGINNING "belongs-to master {prefix m;}}";
734 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
735 logbuf_assert("Invalid keyword \"belongs-to\" as a child of \"module\". Line number 1.");
Radek Krejci09306362018-10-15 15:26:01 +0200736 mod = mod_renew(&ctx, mod, 0);
Radek Krejci70853c52018-10-15 14:46:16 +0200737
Radek Krejcia042ea12018-10-13 07:52:15 +0200738 /* anydata */
739 TEST_NODE(LYS_ANYDATA, "anydata test;}", "test");
740 /* anyxml */
741 TEST_NODE(LYS_ANYXML, "anyxml test;}", "test");
742 /* augment */
743 TEST_GENERIC("augment /somepath;}", mod->augments,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200744 assert_string_equal("/somepath", mod->augments[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200745 /* choice */
746 TEST_NODE(LYS_CHOICE, "choice test;}", "test");
747 /* contact 0..1 */
748 TEST_GENERIC("contact \"firstname\" + \n\t\" surname\";}", mod->contact,
749 assert_string_equal("firstname surname", mod->contact));
750 /* container */
751 TEST_NODE(LYS_CONTAINER, "container test;}", "test");
752 /* description 0..1 */
753 TEST_GENERIC("description \'some description\';}", mod->dsc,
754 assert_string_equal("some description", mod->dsc));
755 /* deviation */
756 TEST_GENERIC("deviation /somepath {deviate not-supported;}}", mod->deviations,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200757 assert_string_equal("/somepath", mod->deviations[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200758 /* extension */
759 TEST_GENERIC("extension test;}", mod->extensions,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200760 assert_string_equal("test", mod->extensions[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200761 /* feature */
762 TEST_GENERIC("feature test;}", mod->features,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200763 assert_string_equal("test", mod->features[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200764 /* grouping */
765 TEST_GENERIC("grouping grp;}", mod->groupings,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200766 assert_string_equal("grp", mod->groupings[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200767 /* identity */
768 TEST_GENERIC("identity test;}", mod->identities,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200769 assert_string_equal("test", mod->identities[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200770 /* import */
Radek Krejci086c7132018-10-26 15:29:04 +0200771 ly_ctx_set_module_imp_clb(ctx.ctx, test_imp_clb, "module zzz { namespace urn:zzz; prefix z;}");
772 TEST_GENERIC("import zzz {prefix z;}}", mod->imports,
773 assert_string_equal("zzz", mod->imports[0].name));
Radek Krejci70853c52018-10-15 14:46:16 +0200774
Radek Krejcia042ea12018-10-13 07:52:15 +0200775 /* import - prefix collision */
Radek Krejci086c7132018-10-26 15:29:04 +0200776 str = SCHEMA_BEGINNING "import zzz {prefix x;}}";
Radek Krejcia042ea12018-10-13 07:52:15 +0200777 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
Radek Krejci70853c52018-10-15 14:46:16 +0200778 logbuf_assert("Prefix \"x\" already used as module prefix. Line number 2.");
Radek Krejci09306362018-10-15 15:26:01 +0200779 mod = mod_renew(&ctx, mod, 0);
Radek Krejci086c7132018-10-26 15:29:04 +0200780 str = SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix y;}}";
Radek Krejci86d106e2018-10-18 09:53:19 +0200781 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
Radek Krejci086c7132018-10-26 15:29:04 +0200782 logbuf_assert("Prefix \"y\" already used to import \"zzz\" module. Line number 2.");
Radek Krejci86d106e2018-10-18 09:53:19 +0200783 mod = mod_renew(&ctx, mod, 0);
Radek Krejci086c7132018-10-26 15:29:04 +0200784 str = "module" SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix z;}}";
785 assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG));
786 assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx));
787 logbuf_assert("Single revision of the module \"zzz\" referred twice.");
Radek Krejci70853c52018-10-15 14:46:16 +0200788
Radek Krejcia042ea12018-10-13 07:52:15 +0200789 /* include */
Radek Krejci9ed7a192018-10-31 16:23:51 +0100790 store = 1;
Radek Krejcid33273d2018-10-25 14:55:52 +0200791 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 +0200792 str = "module" SCHEMA_BEGINNING "include xxx;}";
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("Included \"xxx\" schema from \"name\" is actually not a submodule.");
Radek Krejci9ed7a192018-10-31 16:23:51 +0100796 store = -1;
Radek Krejcid33273d2018-10-25 14:55:52 +0200797
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, "submodule xxx {belongs-to wrong-name;}");
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\" submodule from \"name\" belongs-to a different module \"wrong-name\".");
Radek Krejci9ed7a192018-10-31 16:23:51 +0100804 store = -1;
Radek Krejcid33273d2018-10-25 14:55:52 +0200805
806 ly_ctx_set_module_imp_clb(ctx.ctx, test_imp_clb, "submodule xxx {belongs-to name;}");
807 TEST_GENERIC("include xxx;}", mod->includes,
Radek Krejci086c7132018-10-26 15:29:04 +0200808 assert_string_equal("xxx", mod->includes[0].name));
Radek Krejcid33273d2018-10-25 14:55:52 +0200809
Radek Krejcia042ea12018-10-13 07:52:15 +0200810 /* leaf */
811 TEST_NODE(LYS_LEAF, "leaf test {type string;}}", "test");
812 /* leaf-list */
813 TEST_NODE(LYS_LEAFLIST, "leaf-list test {type string;}}", "test");
814 /* list */
815 TEST_NODE(LYS_LIST, "list test {key a;leaf a {type string;}}}", "test");
816 /* notification */
817 TEST_GENERIC("notification test;}", mod->notifs,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200818 assert_string_equal("test", mod->notifs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200819 /* organization 0..1 */
820 TEST_GENERIC("organization \"CESNET a.l.e.\";}", mod->org,
821 assert_string_equal("CESNET a.l.e.", mod->org));
822 /* reference 0..1 */
823 TEST_GENERIC("reference RFC7950;}", mod->ref,
824 assert_string_equal("RFC7950", mod->ref));
825 /* revision */
826 TEST_GENERIC("revision 2018-10-12;}", mod->revs,
Radek Krejcib7db73a2018-10-24 14:18:40 +0200827 assert_string_equal("2018-10-12", mod->revs[0].date));
Radek Krejcia042ea12018-10-13 07:52:15 +0200828 /* rpc */
829 TEST_GENERIC("rpc test;}", mod->rpcs,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200830 assert_string_equal("test", mod->rpcs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200831 /* typedef */
832 TEST_GENERIC("typedef test{type string;}}", mod->typedefs,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200833 assert_string_equal("test", mod->typedefs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200834 /* uses */
835 TEST_NODE(LYS_USES, "uses test;}", "test");
836 /* yang-version */
837 str = SCHEMA_BEGINNING "\n\tyang-version 10;}";
838 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
839 logbuf_assert("Invalid value \"10\" of \"yang-version\". Line number 3.");
Radek Krejci09306362018-10-15 15:26:01 +0200840 mod = mod_renew(&ctx, mod, 0);
Radek Krejcia042ea12018-10-13 07:52:15 +0200841 str = SCHEMA_BEGINNING "yang-version 1.0;yang-version 1.1;}";
842 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
843 logbuf_assert("Duplicate keyword \"yang-version\". Line number 3.");
Radek Krejci09306362018-10-15 15:26:01 +0200844 mod = mod_renew(&ctx, mod, 0);
Radek Krejcia042ea12018-10-13 07:52:15 +0200845 str = SCHEMA_BEGINNING "yang-version 1.0;}";
846 assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod));
847 assert_int_equal(1, mod->version);
Radek Krejci09306362018-10-15 15:26:01 +0200848 mod = mod_renew(&ctx, mod, 0);
Radek Krejcia042ea12018-10-13 07:52:15 +0200849 str = SCHEMA_BEGINNING "yang-version \"1.1\";}";
850 assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod));
851 assert_int_equal(2, mod->version);
Radek Krejci09306362018-10-15 15:26:01 +0200852 mod = mod_renew(&ctx, mod, 0);
853
Radek Krejci156ccaf2018-10-15 15:49:17 +0200854 /* extensions */
855 TEST_GENERIC("prefix:test;}", mod->exts,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200856 assert_string_equal("prefix:test", mod->exts[0].name);
857 assert_int_equal(LYEXT_SUBSTMT_SELF, mod->exts[0].insubstmt));
Radek Krejci156ccaf2018-10-15 15:49:17 +0200858 mod = mod_renew(&ctx, mod, 0);
859
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200860 /* invalid substatement */
861 str = SCHEMA_BEGINNING "must false;}";
862 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
863 logbuf_assert("Invalid keyword \"must\" as a child of \"module\". Line number 3.");
864 mod = mod_renew(&ctx, mod, 0);
865
Radek Krejci09306362018-10-15 15:26:01 +0200866 /* submodule */
867 mod->submodule = 1;
868
869 /* missing mandatory substatements */
870 str = " subname {}";
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100871 lydict_remove(ctx.ctx, mod->name);
Radek Krejci09306362018-10-15 15:26:01 +0200872 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
873 assert_string_equal("subname", mod->name);
874 logbuf_assert("Missing mandatory keyword \"belongs-to\" as a child of \"submodule\". Line number 3.");
875 mod = mod_renew(&ctx, mod, 1);
876
877 str = " subname {belongs-to name;}";
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100878 lydict_remove(ctx.ctx, mod->name);
Radek Krejci09306362018-10-15 15:26:01 +0200879 assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod));
880 assert_string_equal("name", mod->belongsto);
881 mod = mod_renew(&ctx, mod, 1);
882
883#undef SCHEMA_BEGINNING
884#define SCHEMA_BEGINNING " subname {belongs-to name;"
885
886 /* duplicated namespace, prefix */
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200887 TEST_DUP("belongs-to", "module1", "module2", "3", 1);
Radek Krejci09306362018-10-15 15:26:01 +0200888
889 /* not allowed in submodule (module-specific) */
890 str = SCHEMA_BEGINNING "namespace \"urn:z\";}";
891 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
892 logbuf_assert("Invalid keyword \"namespace\" as a child of \"submodule\". Line number 3.");
893 mod = mod_renew(&ctx, mod, 1);
894 str = SCHEMA_BEGINNING "prefix m;}}";
895 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
896 logbuf_assert("Invalid keyword \"prefix\" as a child of \"submodule\". Line number 3.");
897 mod = mod_renew(&ctx, mod, 1);
Radek Krejcia042ea12018-10-13 07:52:15 +0200898
899#undef TEST_GENERIC
900#undef TEST_NODE
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200901#undef TEST_DUP
Radek Krejcia042ea12018-10-13 07:52:15 +0200902#undef SCHEMA_BEGINNING
903
Radek Krejci9fcacc12018-10-11 15:59:11 +0200904 lysp_module_free(mod);
905 ly_ctx_destroy(ctx.ctx, NULL);
Radek Krejciefd22f62018-09-27 11:47:58 +0200906}
907
Radek Krejci4c6d9bd2018-10-15 16:43:06 +0200908static void
909test_identity(void **state)
910{
911 (void) state; /* unused */
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200912 int dict = 1; /* magic variable for FREE macros */
Radek Krejci4c6d9bd2018-10-15 16:43:06 +0200913
914 struct ly_parser_ctx ctx;
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100915 struct lysp_module *mod = NULL;
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;
923
924 /* invalid cardinality */
925#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200926 TEST_DUP_GENERIC(" test {", MEMBER, VALUE1, VALUE2, parse_identity, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +0200927 &ident, "1", FREE_ARRAY(ctx.ctx, ident, lysp_ident_free); ident = NULL)
Radek Krejci4c6d9bd2018-10-15 16:43:06 +0200928
929 TEST_DUP("description", "a", "b");
930 TEST_DUP("reference", "a", "b");
931 TEST_DUP("status", "current", "obsolete");
932
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200933 /* full content */
934 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 +0200935 assert_int_equal(LY_SUCCESS, parse_identity(&ctx, &str, &ident));
936 assert_non_null(ident);
937 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +0200938 FREE_ARRAY(ctx.ctx, ident, lysp_ident_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200939 ident = NULL;
940
941 /* invalid substatement */
942 str = " test {organization XXX;}";
943 assert_int_equal(LY_EVALID, parse_identity(&ctx, &str, &ident));
944 logbuf_assert("Invalid keyword \"organization\" as a child of \"identity\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +0200945 FREE_ARRAY(ctx.ctx, ident, lysp_ident_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200946 ident = NULL;
947
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100948 /* identity duplication */
949 str = "module a {namespace urn:a; prefix a; identity a; identity a;}";
950 assert_int_equal(LY_EVALID, yang_parse(ctx.ctx, str, &mod));
951 logbuf_assert("Duplicate identifier \"a\" of identity statement. Line number 1.");
952 assert_null(mod);
953
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200954#undef TEST_DUP
955
956 ly_ctx_destroy(ctx.ctx, NULL);
957}
958
959static void
960test_feature(void **state)
961{
962 (void) state; /* unused */
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200963 int dict = 1; /* magic variable for FREE macros */
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200964
965 struct ly_parser_ctx ctx;
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100966 struct lysp_module *mod = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200967 struct lysp_feature *features = NULL;
968 const char *str;
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200969
970 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
971 assert_non_null(ctx.ctx);
972 ctx.line = 1;
973 ctx.indent = 0;
974
975 /* invalid cardinality */
976#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
977 TEST_DUP_GENERIC(" test {", MEMBER, VALUE1, VALUE2, parse_feature, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +0200978 &features, "1", FREE_ARRAY(ctx.ctx, features, lysp_feature_free); features = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200979
980 TEST_DUP("description", "a", "b");
981 TEST_DUP("reference", "a", "b");
982 TEST_DUP("status", "current", "obsolete");
983
984 /* full content */
985 str = " test {description text;reference \'another text\';status current; if-feature x;if-feature y;prefix:ext;} ...";
986 assert_int_equal(LY_SUCCESS, parse_feature(&ctx, &str, &features));
987 assert_non_null(features);
988 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +0200989 FREE_ARRAY(ctx.ctx, features, lysp_feature_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200990 features = NULL;
991
992 /* invalid substatement */
993 str = " test {organization XXX;}";
994 assert_int_equal(LY_EVALID, parse_feature(&ctx, &str, &features));
995 logbuf_assert("Invalid keyword \"organization\" as a child of \"feature\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +0200996 FREE_ARRAY(ctx.ctx, features, lysp_feature_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200997 features = NULL;
998
Radek Krejcifaa1eac2018-10-30 14:34:55 +0100999 /* feature duplication */
1000 str = "module a {namespace urn:a; prefix a; feature a; feature a;}";
1001 assert_int_equal(LY_EVALID, yang_parse(ctx.ctx, str, &mod));
1002 logbuf_assert("Duplicate identifier \"a\" of feature statement. Line number 1.");
1003 assert_null(mod);
1004
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001005#undef TEST_DUP
1006
1007 ly_ctx_destroy(ctx.ctx, NULL);
1008}
1009
1010static void
1011test_deviation(void **state)
1012{
1013 (void) state; /* unused */
Radek Krejcidd4e8d42018-10-16 14:55:43 +02001014 int dict = 1; /* magic variable for FREE macros */
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001015
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 Krejcidd4e8d42018-10-16 14:55:43 +02001077 &d, "1", lysp_deviate_free(ctx.ctx, d, 1); 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 Krejcidd4e8d42018-10-16 14:55:43 +02001092 lysp_deviate_free(ctx.ctx, d, 1); 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 Krejcidd4e8d42018-10-16 14:55:43 +02001097 lysp_deviate_free(ctx.ctx, d, 1); 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 Krejcidd4e8d42018-10-16 14:55:43 +02001102 lysp_deviate_free(ctx.ctx, d, 1); 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 Krejcidd4e8d42018-10-16 14:55:43 +02001107 lysp_deviate_free(ctx.ctx, d, 1); 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 Krejcidd4e8d42018-10-16 14:55:43 +02001114 lysp_deviate_free(ctx.ctx, d, 1); 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
1150 struct ly_parser_ctx ctx;
1151 struct lysp_node_container *c = NULL;
1152 const char *str;
1153
1154 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1155 assert_non_null(ctx.ctx);
1156 ctx.line = 1;
1157 ctx.indent = 0;
1158
1159 /* invalid cardinality */
1160#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
1161 str = "cont {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
1162 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c)); \
1163 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \
1164 lysp_node_free(ctx.ctx, (struct lysp_node*)c, 1); c = NULL;
1165
1166 TEST_DUP("config", "true", "false");
1167 TEST_DUP("description", "text1", "text2");
1168 TEST_DUP("presence", "true", "false");
1169 TEST_DUP("reference", "1", "2");
1170 TEST_DUP("status", "current", "obsolete");
1171 TEST_DUP("when", "true", "false");
1172#undef TEST_DUP
1173
1174 /* full content */
1175 str = "cont {action x;anydata any;anyxml anyxml; choice ch;config false;container c;description test;grouping g;if-feature f; leaf l;"
1176 "leaf-list ll; list li;must 'expr';notification not; presence true; reference test;status current;typedef t;uses g;when true;m:ext;} ...";
1177 assert_int_equal(LY_SUCCESS, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1178 assert_non_null(c);
1179 assert_non_null(c->actions);
1180 assert_non_null(c->child);
1181 assert_string_equal("test", c->dsc);
1182 assert_non_null(c->exts);
1183 assert_non_null(c->groupings);
1184 assert_non_null(c->iffeatures);
1185 assert_non_null(c->musts);
1186 assert_non_null(c->notifs);
1187 assert_string_equal("true", c->presence);
1188 assert_string_equal("test", c->ref);
1189 assert_non_null(c->typedefs);
1190 assert_non_null(c->when);
1191 assert_null(c->parent);
1192 assert_null(c->next);
1193 assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR, c->flags);
1194 lysp_node_free(ctx.ctx, (struct lysp_node*)c, 1); c = NULL;
1195
1196 /* invalid */
1197 str = " cont {augment /root;} ...";
1198 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1199 logbuf_assert("Invalid keyword \"augment\" as a child of \"container\". Line number 1.");
1200 lysp_node_free(ctx.ctx, (struct lysp_node*)c, 1); c = NULL;
1201 str = " cont {nonsence true;} ...";
1202 assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c));
1203 logbuf_assert("Invalid character sequence \"nonsence\", expected a keyword. Line number 1.");
1204 lysp_node_free(ctx.ctx, (struct lysp_node*)c, 1); c = NULL;
1205
1206 ly_ctx_destroy(ctx.ctx, NULL);
1207}
1208
Radek Krejci80dd33e2018-09-26 15:57:18 +02001209int main(void)
1210{
1211 const struct CMUnitTest tests[] = {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001212 cmocka_unit_test_setup(test_helpers, logger_setup),
Radek Krejci80dd33e2018-09-26 15:57:18 +02001213 cmocka_unit_test_setup(test_comments, logger_setup),
Radek Krejciefd22f62018-09-27 11:47:58 +02001214 cmocka_unit_test_setup(test_arg, logger_setup),
Radek Krejcidcc7b322018-10-11 14:24:02 +02001215 cmocka_unit_test_setup(test_stmts, logger_setup),
Radek Krejci9fcacc12018-10-11 15:59:11 +02001216 cmocka_unit_test_setup(test_module, logger_setup),
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001217 cmocka_unit_test_setup(test_identity, logger_setup),
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001218 cmocka_unit_test_setup(test_feature, logger_setup),
1219 cmocka_unit_test_setup(test_deviation, logger_setup),
1220 cmocka_unit_test_setup(test_deviate, logger_setup),
Radek Krejci8c370832018-11-02 15:10:03 +01001221 cmocka_unit_test_setup(test_container, logger_setup),
Radek Krejci80dd33e2018-09-26 15:57:18 +02001222 };
1223
1224 return cmocka_run_group_tests(tests, NULL, NULL);
1225}