blob: 8f5085cd886fbfb45ce4c3f5e521a9b5f99a294a [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};
30
31/* set to 0 to printing error messages to stderr instead of checking them in code */
Radek Krejci70853c52018-10-15 14:46:16 +020032#define ENABLE_LOGGER_CHECKING 1
Radek Krejci80dd33e2018-09-26 15:57:18 +020033
Radek Krejcid5f2b5f2018-10-11 10:54:36 +020034#if ENABLE_LOGGER_CHECKING
Radek Krejci80dd33e2018-09-26 15:57:18 +020035static void
36logger(LY_LOG_LEVEL level, const char *msg, const char *path)
37{
38 (void) level; /* unused */
39
40 if (path) {
41 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
42 } else {
43 strncpy(logbuf, msg, BUFSIZE - 1);
44 }
45}
Radek Krejcid5f2b5f2018-10-11 10:54:36 +020046#endif
Radek Krejci80dd33e2018-09-26 15:57:18 +020047
48static int
49logger_setup(void **state)
50{
51 (void) state; /* unused */
52#if ENABLE_LOGGER_CHECKING
53 ly_set_log_clb(logger, 1);
54#endif
55 return 0;
56}
57
58void
59logbuf_clean(void)
60{
61 logbuf[0] = '\0';
62}
63
64#if ENABLE_LOGGER_CHECKING
65# define logbuf_assert(str) assert_string_equal(logbuf, str)
66#else
67# define logbuf_assert(str)
68#endif
69
Radek Krejci2c02f3e2018-10-16 10:54:38 +020070#define TEST_DUP_GENERIC(PREFIX, MEMBER, VALUE1, VALUE2, FUNC, RESULT, LINE, CLEANUP) \
71 str = PREFIX MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
72 assert_int_equal(LY_EVALID, FUNC(&ctx, &str, RESULT)); \
73 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number "LINE"."); \
74 CLEANUP
75
Radek Krejci44ceedc2018-10-02 15:54:31 +020076static void
77test_helpers(void **state)
78{
79 (void) state; /* unused */
80
81 const char *str;
Radek Krejci404251e2018-10-09 12:06:44 +020082 char *buf, *p;
Radek Krejci44ceedc2018-10-02 15:54:31 +020083 size_t len, size;
84 int prefix;
85 struct ly_parser_ctx ctx;
86 ctx.ctx = NULL;
87 ctx.line = 1;
88
89 /* storing into buffer */
90 str = "abcd";
91 buf = NULL;
92 size = len = 0;
93 assert_int_equal(LY_SUCCESS, buf_add_char(NULL, &str, 2, &buf, &size, &len));
94 assert_int_not_equal(0, size);
95 assert_int_equal(2, len);
96 assert_string_equal("cd", str);
97 assert_false(strncmp("ab", buf, 2));
98 free(buf);
Radek Krejci404251e2018-10-09 12:06:44 +020099 buf = NULL;
100
101 /* invalid first characters */
102 len = 0;
103 str = "2invalid";
104 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1));
105 str = ".invalid";
106 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1));
107 str = "-invalid";
108 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1));
109 /* invalid following characters */
110 len = 3; /* number of characters read before the str content */
111 str = "!";
112 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1));
113 str = ":";
114 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1));
115 /* valid colon for prefixed identifiers */
116 len = size = 0;
117 p = NULL;
118 str = "x:id";
119 assert_int_equal(LY_SUCCESS, buf_store_char(&ctx, &str, Y_PREF_IDENTIF_ARG, &p, &len, &buf, &size, 0));
120 assert_int_equal(1, len);
121 assert_null(buf);
122 assert_string_equal(":id", str);
123 assert_int_equal('x', p[len - 1]);
124 assert_int_equal(LY_SUCCESS, buf_store_char(&ctx, &str, Y_PREF_IDENTIF_ARG, &p, &len, &buf, &size, 1));
125 assert_int_equal(2, len);
126 assert_string_equal("id", str);
127 assert_int_equal(':', p[len - 1]);
128 free(buf);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200129
130 /* checking identifiers */
131 assert_int_equal(LY_EVALID, check_identifierchar(&ctx, ':', 0, NULL));
132 logbuf_assert("Invalid identifier character ':'. Line number 1.");
133 assert_int_equal(LY_EVALID, check_identifierchar(&ctx, '#', 1, NULL));
134 logbuf_assert("Invalid identifier first character '#'. Line number 1.");
135
136 assert_int_equal(LY_SUCCESS, check_identifierchar(&ctx, 'a', 1, &prefix));
137 assert_int_equal(0, prefix);
138 assert_int_equal(LY_SUCCESS, check_identifierchar(&ctx, ':', 0, &prefix));
139 assert_int_equal(1, prefix);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200140 assert_int_equal(LY_EVALID, check_identifierchar(&ctx, ':', 0, &prefix));
141 assert_int_equal(1, prefix);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200142 assert_int_equal(LY_SUCCESS, check_identifierchar(&ctx, 'b', 0, &prefix));
143 assert_int_equal(2, prefix);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200144 /* second colon is invalid */
145 assert_int_equal(LY_EVALID, check_identifierchar(&ctx, ':', 0, &prefix));
146 logbuf_assert("Invalid identifier character ':'. Line number 1.");
Radek Krejci44ceedc2018-10-02 15:54:31 +0200147}
Radek Krejci80dd33e2018-09-26 15:57:18 +0200148
149static void
150test_comments(void **state)
151{
152 (void) state; /* unused */
153
Radek Krejci44ceedc2018-10-02 15:54:31 +0200154 struct ly_parser_ctx ctx;
Radek Krejci80dd33e2018-09-26 15:57:18 +0200155 const char *str, *p;
Radek Krejciefd22f62018-09-27 11:47:58 +0200156 char *word, *buf;
157 size_t len;
Radek Krejci80dd33e2018-09-26 15:57:18 +0200158
Radek Krejci44ceedc2018-10-02 15:54:31 +0200159 ctx.ctx = NULL;
160 ctx.line = 1;
161
Radek Krejciefd22f62018-09-27 11:47:58 +0200162 str = " // this is a text of / one * line */ comment\nargument";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200163 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200164 assert_string_equal("argument", word);
165 assert_null(buf);
166 assert_int_equal(8, len);
Radek Krejci80dd33e2018-09-26 15:57:18 +0200167
Radek Krejciefd22f62018-09-27 11:47:58 +0200168 str = "/* this is a \n * text // of / block * comment */\"arg\" + \"ume\" \n + \n \"nt\"";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200169 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200170 assert_string_equal("argument", word);
171 assert_ptr_equal(buf, word);
172 assert_int_equal(8, len);
173 free(word);
Radek Krejci80dd33e2018-09-26 15:57:18 +0200174
175 str = p = " this is one line comment on last line";
Radek Krejci44ceedc2018-10-02 15:54:31 +0200176 assert_int_equal(LY_SUCCESS, skip_comment(&ctx, &str, 1));
Radek Krejci80dd33e2018-09-26 15:57:18 +0200177 assert_true(str[0] == '\0');
178
179 str = p = " this is a not terminated comment x";
Radek Krejci44ceedc2018-10-02 15:54:31 +0200180 assert_int_equal(LY_EVALID, skip_comment(&ctx, &str, 2));
181 logbuf_assert("Unexpected end-of-file, non-terminated comment. Line number 5.");
Radek Krejci80dd33e2018-09-26 15:57:18 +0200182 assert_true(str[0] == '\0');
183}
184
Radek Krejciefd22f62018-09-27 11:47:58 +0200185static void
186test_arg(void **state)
187{
188 (void) state; /* unused */
189
Radek Krejci44ceedc2018-10-02 15:54:31 +0200190 struct ly_parser_ctx ctx;
Radek Krejciefd22f62018-09-27 11:47:58 +0200191 const char *str;
192 char *word, *buf;
193 size_t len;
194
Radek Krejci44ceedc2018-10-02 15:54:31 +0200195 ctx.ctx = NULL;
196 ctx.line = 1;
197
Radek Krejciefd22f62018-09-27 11:47:58 +0200198 /* missing argument */
199 str = ";";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200200 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_MAYBE_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200201 assert_null(word);
202
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200203 str = "{";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200204 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200205 logbuf_assert("Invalid character sequence \"{\", expected an argument. Line number 1.");
206
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200207 /* invalid escape sequence */
208 str = "\"\\s\"";
209 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("Double-quoted string unknown special character \'\\s\'. Line number 1.");
211 str = "\'\\s\'"; /* valid, since it is not an escape sequence in single quoted string */
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200212 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200213 assert_int_equal(2, len);
214 assert_string_equal("\\s\'", word);
215 assert_int_equal('\0', str[0]); /* input has been eaten */
216
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200217 /* invalid character after the argument */
218 str = "hello\"";
219 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
220 logbuf_assert("Invalid character sequence \"\"\", expected unquoted string character, optsep, semicolon or opening brace. Line number 1.");
221 str = "hello}";
222 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
223 logbuf_assert("Invalid character sequence \"}\", expected unquoted string character, optsep, semicolon or opening brace. Line number 1.");
224
225 str = "hello/x\t"; /* slash is not an invalid character */
226 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
227 assert_int_equal(7, len);
228 assert_string_equal("hello/x\t", word);
229
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200230 assert_null(buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200231
232 /* different quoting */
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200233 str = "hello ";
234 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200235 assert_null(buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200236 assert_int_equal(5, len);
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200237 assert_string_equal("hello ", word);
Radek Krejciefd22f62018-09-27 11:47:58 +0200238
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200239 str = "hello/*comment*/\n";
240 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200241 assert_null(buf);
242 assert_int_equal(5, len);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200243 assert_false(strncmp("hello", word, len));
244
245
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200246 str = "\"hello\\n\\t\\\"\\\\\";";
247 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200248 assert_null(buf);
249 assert_int_equal(9, len);
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200250 assert_string_equal("hello\\n\\t\\\"\\\\\";", word);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200251
252 ctx.indent = 14;
253 str = "\"hello \t\n\t\t world!\"";
254 /* - space and tabs before newline are stripped out
255 * - space and tabs after newline (indentation) are stripped out
256 */
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200257 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200258 assert_non_null(buf);
259 assert_ptr_equal(word, buf);
260 assert_int_equal(14, len);
261 assert_string_equal("hello\n world!", word);
262 free(buf);
263
264 ctx.indent = 14;
265 str = "\"hello\n \tworld!\"";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200266 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200267 assert_non_null(buf);
268 assert_ptr_equal(word, buf);
269 assert_int_equal(12, len);
270 assert_string_equal("hello\nworld!", word);
271 free(buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200272
273 str = "\'hello\'";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200274 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200275 assert_null(buf);
276 assert_int_equal(5, len);
277 assert_false(strncmp("hello", word, 5));
278
279 str = "\"hel\" +\t\n\"lo\"";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200280 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200281 assert_ptr_equal(word, buf);
282 assert_int_equal(5, len);
283 assert_string_equal("hello", word);
284 free(buf);
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200285 str = "\"hel\" +\t\nlo"; /* unquoted the second part */
286 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
287 logbuf_assert("Both string parts divided by '+' must be quoted. Line number 5.");
Radek Krejciefd22f62018-09-27 11:47:58 +0200288
289 str = "\'he\'\t\n+ \"llo\"";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200290 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200291 assert_ptr_equal(word, buf);
292 assert_int_equal(5, len);
293 assert_string_equal("hello", word);
294 free(buf);
295
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200296 str = " \t\n\"he\"+\'llo\'";
297 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200298 assert_ptr_equal(word, buf);
299 assert_int_equal(5, len);
300 assert_string_equal("hello", word);
301 free(buf);
302
Radek Krejci44ceedc2018-10-02 15:54:31 +0200303 /* missing argument */
304 str = ";";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200305 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
306 logbuf_assert("Invalid character sequence \";\", expected an argument. Line number 7.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200307}
308
309static void
310test_stmts(void **state)
311{
312 (void) state; /* unused */
313
314 struct ly_parser_ctx ctx;
315 const char *str, *p;
316 enum yang_keyword kw;
317 char *word;
318 size_t len;
319
320 ctx.ctx = NULL;
321 ctx.line = 1;
322
323 str = "\n// comment\n\tinput\t{";
324 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
325 assert_int_equal(YANG_INPUT, kw);
326 assert_int_equal(5, len);
327 assert_string_equal("input\t{", word);
328 assert_string_equal("\t{", str);
329
330 str = "\t /* comment */\t output\n\t{";
331 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
332 assert_int_equal(YANG_OUTPUT, kw);
333 assert_int_equal(6, len);
334 assert_string_equal("output\n\t{", word);
335 assert_string_equal("\n\t{", str);
336
337 str = "/input { "; /* invalid slash */
338 assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len));
339 logbuf_assert("Invalid identifier first character '/'. Line number 4.");
340
341 str = "not-a-statement-nor-extension { "; /* invalid identifier */
342 assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len));
343 logbuf_assert("Invalid character sequence \"not-a-statement-nor-extension\", expected a keyword. Line number 4.");
344
345 str = "path;"; /* missing sep after the keyword */
346 assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len));
347 logbuf_assert("Invalid character sequence \"path;\", expected a keyword followed by a separator. Line number 4.");
348
349 str = "action ";
350 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
351 assert_int_equal(YANG_ACTION, kw);
352 assert_int_equal(6, len);
353 str = "anydata ";
354 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
355 assert_int_equal(YANG_ANYDATA, kw);
356 assert_int_equal(7, len);
357 str = "anyxml ";
358 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
359 assert_int_equal(YANG_ANYXML, kw);
360 assert_int_equal(6, len);
361 str = "argument ";
362 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
363 assert_int_equal(YANG_ARGUMENT, kw);
364 assert_int_equal(8, len);
365 str = "augment ";
366 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
367 assert_int_equal(YANG_AUGMENT, kw);
368 assert_int_equal(7, len);
369 str = "base ";
370 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
371 assert_int_equal(YANG_BASE, kw);
372 assert_int_equal(4, len);
373 str = "belongs-to ";
374 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
375 assert_int_equal(YANG_BELONGS_TO, kw);
376 assert_int_equal(10, len);
377 str = "bit ";
378 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
379 assert_int_equal(YANG_BIT, kw);
380 assert_int_equal(3, len);
381 str = "case ";
382 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
383 assert_int_equal(YANG_CASE, kw);
384 assert_int_equal(4, len);
385 str = "choice ";
386 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
387 assert_int_equal(YANG_CHOICE, kw);
388 assert_int_equal(6, len);
389 str = "config ";
390 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
391 assert_int_equal(YANG_CONFIG, kw);
392 assert_int_equal(6, len);
393 str = "contact ";
394 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
395 assert_int_equal(YANG_CONTACT, kw);
396 assert_int_equal(7, len);
397 str = "container ";
398 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
399 assert_int_equal(YANG_CONTAINER, kw);
400 assert_int_equal(9, len);
401 str = "default ";
402 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
403 assert_int_equal(YANG_DEFAULT, kw);
404 assert_int_equal(7, len);
405 str = "description ";
406 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
407 assert_int_equal(YANG_DESCRIPTION, kw);
408 assert_int_equal(11, len);
409 str = "deviate ";
410 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
411 assert_int_equal(YANG_DEVIATE, kw);
412 assert_int_equal(7, len);
413 str = "deviation ";
414 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
415 assert_int_equal(YANG_DEVIATION, kw);
416 assert_int_equal(9, len);
417 str = "enum ";
418 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
419 assert_int_equal(YANG_ENUM, kw);
420 assert_int_equal(4, len);
421 str = "error-app-tag ";
422 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
423 assert_int_equal(YANG_ERROR_APP_TAG, kw);
424 assert_int_equal(13, len);
425 str = "error-message ";
426 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
427 assert_int_equal(YANG_ERROR_MESSAGE, kw);
428 assert_int_equal(13, len);
429 str = "extension ";
430 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
431 assert_int_equal(YANG_EXTENSION, kw);
432 assert_int_equal(9, len);
433 str = "feature ";
434 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
435 assert_int_equal(YANG_FEATURE, kw);
436 assert_int_equal(7, len);
437 str = "fraction-digits ";
438 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
439 assert_int_equal(YANG_FRACTION_DIGITS, kw);
440 assert_int_equal(15, len);
441 str = "grouping ";
442 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
443 assert_int_equal(YANG_GROUPING, kw);
444 assert_int_equal(8, len);
445 str = "identity ";
446 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
447 assert_int_equal(YANG_IDENTITY, kw);
448 assert_int_equal(8, len);
449 str = "if-feature ";
450 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
451 assert_int_equal(YANG_IF_FEATURE, kw);
452 assert_int_equal(10, len);
453 str = "import ";
454 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
455 assert_int_equal(YANG_IMPORT, kw);
456 assert_int_equal(6, len);
457 str = "include ";
458 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
459 assert_int_equal(YANG_INCLUDE, kw);
460 assert_int_equal(7, len);
461 str = "input{";
462 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
463 assert_int_equal(YANG_INPUT, kw);
464 assert_int_equal(5, len);
465 str = "key ";
466 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
467 assert_int_equal(YANG_KEY, kw);
468 assert_int_equal(3, len);
469 str = "leaf ";
470 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
471 assert_int_equal(YANG_LEAF, kw);
472 assert_int_equal(4, len);
473 str = "leaf-list ";
474 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
475 assert_int_equal(YANG_LEAF_LIST, kw);
476 assert_int_equal(9, len);
477 str = "length ";
478 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
479 assert_int_equal(YANG_LENGTH, kw);
480 assert_int_equal(6, len);
481 str = "list ";
482 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
483 assert_int_equal(YANG_LIST, kw);
484 assert_int_equal(4, len);
485 str = "mandatory ";
486 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
487 assert_int_equal(YANG_MANDATORY, kw);
488 assert_int_equal(9, len);
489 str = "max-elements ";
490 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
491 assert_int_equal(YANG_MAX_ELEMENTS, kw);
492 assert_int_equal(12, len);
493 str = "min-elements ";
494 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
495 assert_int_equal(YANG_MIN_ELEMENTS, kw);
496 assert_int_equal(12, len);
497 str = "modifier ";
498 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
499 assert_int_equal(YANG_MODIFIER, kw);
500 assert_int_equal(8, len);
501 str = "module ";
502 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
503 assert_int_equal(YANG_MODULE, kw);
504 assert_int_equal(6, len);
505 str = "must ";
506 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
507 assert_int_equal(YANG_MUST, kw);
508 assert_int_equal(4, len);
509 str = "namespace ";
510 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
511 assert_int_equal(YANG_NAMESPACE, kw);
512 assert_int_equal(9, len);
513 str = "notification ";
514 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
515 assert_int_equal(YANG_NOTIFICATION, kw);
516 assert_int_equal(12, len);
517 str = "ordered-by ";
518 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
519 assert_int_equal(YANG_ORDERED_BY, kw);
520 assert_int_equal(10, len);
521 str = "organization ";
522 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
523 assert_int_equal(YANG_ORGANIZATION, kw);
524 assert_int_equal(12, len);
525 str = "output ";
526 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
527 assert_int_equal(YANG_OUTPUT, kw);
528 assert_int_equal(6, len);
529 str = "path ";
530 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
531 assert_int_equal(YANG_PATH, kw);
532 assert_int_equal(4, len);
533 str = "pattern ";
534 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
535 assert_int_equal(YANG_PATTERN, kw);
536 assert_int_equal(7, len);
537 str = "position ";
538 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
539 assert_int_equal(YANG_POSITION, kw);
540 assert_int_equal(8, len);
541 str = "prefix ";
542 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
543 assert_int_equal(YANG_PREFIX, kw);
544 assert_int_equal(6, len);
545 str = "presence ";
546 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
547 assert_int_equal(YANG_PRESENCE, kw);
548 assert_int_equal(8, len);
549 str = "range ";
550 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
551 assert_int_equal(YANG_RANGE, kw);
552 assert_int_equal(5, len);
553 str = "reference ";
554 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
555 assert_int_equal(YANG_REFERENCE, kw);
556 assert_int_equal(9, len);
557 str = "refine ";
558 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
559 assert_int_equal(YANG_REFINE, kw);
560 assert_int_equal(6, len);
561 str = "require-instance ";
562 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
563 assert_int_equal(YANG_REQUIRE_INSTANCE, kw);
564 assert_int_equal(16, len);
565 str = "revision ";
566 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
567 assert_int_equal(YANG_REVISION, kw);
568 assert_int_equal(8, len);
569 str = "revision-date ";
570 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
571 assert_int_equal(YANG_REVISION_DATE, kw);
572 assert_int_equal(13, len);
573 str = "rpc ";
574 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
575 assert_int_equal(YANG_RPC, kw);
576 assert_int_equal(3, len);
577 str = "status ";
578 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
579 assert_int_equal(YANG_STATUS, kw);
580 assert_int_equal(6, len);
581 str = "submodule ";
582 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
583 assert_int_equal(YANG_SUBMODULE, kw);
584 assert_int_equal(9, len);
585 str = "type ";
586 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
587 assert_int_equal(YANG_TYPE, kw);
588 assert_int_equal(4, len);
589 str = "typedef ";
590 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
591 assert_int_equal(YANG_TYPEDEF, kw);
592 assert_int_equal(7, len);
593 str = "unique ";
594 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
595 assert_int_equal(YANG_UNIQUE, kw);
596 assert_int_equal(6, len);
597 str = "units ";
598 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
599 assert_int_equal(YANG_UNITS, kw);
600 assert_int_equal(5, len);
601 str = "uses ";
602 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
603 assert_int_equal(YANG_USES, kw);
604 assert_int_equal(4, len);
605 str = "value ";
606 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
607 assert_int_equal(YANG_VALUE, kw);
608 assert_int_equal(5, len);
609 str = "when ";
610 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
611 assert_int_equal(YANG_WHEN, kw);
612 assert_int_equal(4, len);
613 str = "yang-version ";
614 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
615 assert_int_equal(YANG_YANG_VERSION, kw);
616 assert_int_equal(12, len);
617 str = "yin-element ";
618 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
619 assert_int_equal(YANG_YIN_ELEMENT, kw);
620 assert_int_equal(11, len);
Radek Krejci626df482018-10-11 15:06:31 +0200621 str = ";config false;";
622 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
623 assert_int_equal(YANG_SEMICOLON, kw);
624 assert_int_equal(1, len);
625 assert_string_equal("config false;", str);
626 str = "{ config false;";
627 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
628 assert_int_equal(YANG_LEFT_BRACE, kw);
629 assert_int_equal(1, len);
630 assert_string_equal(" config false;", str);
631 str = "}";
632 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
633 assert_int_equal(YANG_RIGHT_BRACE, kw);
634 assert_int_equal(1, len);
635 assert_string_equal("", str);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200636
637 /* geenric extension */
638 str = p = "nacm:default-deny-write;";
639 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
640 assert_int_equal(YANG_CUSTOM, kw);
641 assert_int_equal(23, len);
642 assert_ptr_equal(p, word);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200643}
Radek Krejci44ceedc2018-10-02 15:54:31 +0200644
Radek Krejci9fcacc12018-10-11 15:59:11 +0200645static struct lysp_module *
Radek Krejci09306362018-10-15 15:26:01 +0200646mod_renew(struct ly_parser_ctx *ctx, struct lysp_module *mod, uint8_t submodule)
Radek Krejci9fcacc12018-10-11 15:59:11 +0200647{
648 lysp_module_free(mod);
649 mod = calloc(1, sizeof *mod);
650 mod->ctx = ctx->ctx;
Radek Krejci09306362018-10-15 15:26:01 +0200651 mod->submodule = submodule;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200652 assert_non_null(mod);
653 return mod;
654}
655
656static void
657test_module(void **state)
658{
659 (void) state; /* unused */
660
661 struct ly_parser_ctx ctx;
662 struct lysp_module *mod;
663 const char *str;
664
665 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
666 assert_non_null(ctx.ctx);
667 ctx.line = 1;
Radek Krejcia042ea12018-10-13 07:52:15 +0200668 ctx.indent = 0;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200669
Radek Krejci09306362018-10-15 15:26:01 +0200670 mod = mod_renew(&ctx, NULL, 0);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200671
672 /* missing mandatory substatements */
673 str = " name {}";
674 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
675 assert_string_equal("name", mod->name);
676 logbuf_assert("Missing mandatory keyword \"namespace\" as a child of \"module\". Line number 1.");
Radek Krejci09306362018-10-15 15:26:01 +0200677 mod = mod_renew(&ctx, mod, 0);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200678
679 str = " name {namespace urn:x;}";
680 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
681 assert_string_equal("urn:x", mod->ns);
682 logbuf_assert("Missing mandatory keyword \"prefix\" as a child of \"module\". Line number 1.");
Radek Krejci09306362018-10-15 15:26:01 +0200683 mod = mod_renew(&ctx, mod, 0);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200684
685 str = " name {namespace urn:x;prefix \"x\";}";
686 assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod));
687 assert_string_equal("x", mod->prefix);
Radek Krejci09306362018-10-15 15:26:01 +0200688 mod = mod_renew(&ctx, mod, 0);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200689
Radek Krejcia042ea12018-10-13 07:52:15 +0200690#define SCHEMA_BEGINNING " name {namespace urn:x;prefix \"x\";"
691#define TEST_NODE(NODETYPE, INPUT, NAME) \
692 str = SCHEMA_BEGINNING INPUT; \
693 assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod)); \
694 assert_non_null(mod->data); \
695 assert_int_equal(NODETYPE, mod->data->nodetype); \
696 assert_string_equal(NAME, mod->data->name); \
Radek Krejci09306362018-10-15 15:26:01 +0200697 mod = mod_renew(&ctx, mod, 0);
Radek Krejcia042ea12018-10-13 07:52:15 +0200698#define TEST_GENERIC(INPUT, TARGET, TEST) \
699 str = SCHEMA_BEGINNING INPUT; \
700 assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod)); \
701 assert_non_null(TARGET); \
702 TEST; \
Radek Krejci09306362018-10-15 15:26:01 +0200703 mod = mod_renew(&ctx, mod, 0);
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200704#define TEST_DUP(MEMBER, VALUE1, VALUE2, LINE, SUBMODULE) \
705 TEST_DUP_GENERIC(SCHEMA_BEGINNING, MEMBER, VALUE1, VALUE2, \
706 parse_sub_module, mod, LINE, mod = mod_renew(&ctx, mod, SUBMODULE))
Radek Krejcia042ea12018-10-13 07:52:15 +0200707
708 /* duplicated namespace, prefix */
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200709 TEST_DUP("namespace", "y", "z", "1", 0);
710 TEST_DUP("prefix", "y", "z", "1", 0);
711 TEST_DUP("contact", "a", "b", "1", 0);
712 TEST_DUP("description", "a", "b", "1", 0);
713 TEST_DUP("organization", "a", "b", "1", 0);
714 TEST_DUP("reference", "a", "b", "1", 0);
Radek Krejcia042ea12018-10-13 07:52:15 +0200715
Radek Krejci70853c52018-10-15 14:46:16 +0200716 /* not allowed in module (submodule-specific) */
717 str = SCHEMA_BEGINNING "belongs-to master {prefix m;}}";
718 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
719 logbuf_assert("Invalid keyword \"belongs-to\" as a child of \"module\". Line number 1.");
Radek Krejci09306362018-10-15 15:26:01 +0200720 mod = mod_renew(&ctx, mod, 0);
Radek Krejci70853c52018-10-15 14:46:16 +0200721
Radek Krejcia042ea12018-10-13 07:52:15 +0200722 /* anydata */
723 TEST_NODE(LYS_ANYDATA, "anydata test;}", "test");
724 /* anyxml */
725 TEST_NODE(LYS_ANYXML, "anyxml test;}", "test");
726 /* augment */
727 TEST_GENERIC("augment /somepath;}", mod->augments,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200728 assert_string_equal("/somepath", mod->augments[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200729 /* choice */
730 TEST_NODE(LYS_CHOICE, "choice test;}", "test");
731 /* contact 0..1 */
732 TEST_GENERIC("contact \"firstname\" + \n\t\" surname\";}", mod->contact,
733 assert_string_equal("firstname surname", mod->contact));
734 /* container */
735 TEST_NODE(LYS_CONTAINER, "container test;}", "test");
736 /* description 0..1 */
737 TEST_GENERIC("description \'some description\';}", mod->dsc,
738 assert_string_equal("some description", mod->dsc));
739 /* deviation */
740 TEST_GENERIC("deviation /somepath {deviate not-supported;}}", mod->deviations,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200741 assert_string_equal("/somepath", mod->deviations[0].nodeid));
Radek Krejcia042ea12018-10-13 07:52:15 +0200742 /* extension */
743 TEST_GENERIC("extension test;}", mod->extensions,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200744 assert_string_equal("test", mod->extensions[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200745 /* feature */
746 TEST_GENERIC("feature test;}", mod->features,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200747 assert_string_equal("test", mod->features[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200748 /* grouping */
749 TEST_GENERIC("grouping grp;}", mod->groupings,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200750 assert_string_equal("grp", mod->groupings[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200751 /* identity */
752 TEST_GENERIC("identity test;}", mod->identities,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200753 assert_string_equal("test", mod->identities[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200754 /* import */
755 TEST_GENERIC("import test {prefix z;}}", mod->imports,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200756 assert_string_equal("test", mod->imports[0].name));
Radek Krejci70853c52018-10-15 14:46:16 +0200757
Radek Krejcia042ea12018-10-13 07:52:15 +0200758 /* import - prefix collision */
759 str = SCHEMA_BEGINNING "import test {prefix x;}}";
760 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
Radek Krejci70853c52018-10-15 14:46:16 +0200761 logbuf_assert("Prefix \"x\" already used as module prefix. Line number 2.");
Radek Krejci09306362018-10-15 15:26:01 +0200762 mod = mod_renew(&ctx, mod, 0);
Radek Krejci86d106e2018-10-18 09:53:19 +0200763 str = SCHEMA_BEGINNING "import test1 {prefix y;}import test2 {prefix y;}}";
764 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
765 logbuf_assert("Prefix \"y\" already used to import \"test1\" module. Line number 2.");
766 mod = mod_renew(&ctx, mod, 0);
Radek Krejci70853c52018-10-15 14:46:16 +0200767
Radek Krejcia042ea12018-10-13 07:52:15 +0200768 /* include */
Radek Krejci469f70d2018-10-15 15:27:29 +0200769 TEST_GENERIC("include test;}", mod->includes,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200770 assert_string_equal("test", mod->includes[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200771 /* leaf */
772 TEST_NODE(LYS_LEAF, "leaf test {type string;}}", "test");
773 /* leaf-list */
774 TEST_NODE(LYS_LEAFLIST, "leaf-list test {type string;}}", "test");
775 /* list */
776 TEST_NODE(LYS_LIST, "list test {key a;leaf a {type string;}}}", "test");
777 /* notification */
778 TEST_GENERIC("notification test;}", mod->notifs,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200779 assert_string_equal("test", mod->notifs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200780 /* organization 0..1 */
781 TEST_GENERIC("organization \"CESNET a.l.e.\";}", mod->org,
782 assert_string_equal("CESNET a.l.e.", mod->org));
783 /* reference 0..1 */
784 TEST_GENERIC("reference RFC7950;}", mod->ref,
785 assert_string_equal("RFC7950", mod->ref));
786 /* revision */
787 TEST_GENERIC("revision 2018-10-12;}", mod->revs,
Radek Krejcib7db73a2018-10-24 14:18:40 +0200788 assert_string_equal("2018-10-12", mod->revs[0].date));
Radek Krejcia042ea12018-10-13 07:52:15 +0200789 /* rpc */
790 TEST_GENERIC("rpc test;}", mod->rpcs,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200791 assert_string_equal("test", mod->rpcs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200792 /* typedef */
793 TEST_GENERIC("typedef test{type string;}}", mod->typedefs,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200794 assert_string_equal("test", mod->typedefs[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200795 /* uses */
796 TEST_NODE(LYS_USES, "uses test;}", "test");
797 /* yang-version */
798 str = SCHEMA_BEGINNING "\n\tyang-version 10;}";
799 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
800 logbuf_assert("Invalid value \"10\" of \"yang-version\". Line number 3.");
Radek Krejci09306362018-10-15 15:26:01 +0200801 mod = mod_renew(&ctx, mod, 0);
Radek Krejcia042ea12018-10-13 07:52:15 +0200802 str = SCHEMA_BEGINNING "yang-version 1.0;yang-version 1.1;}";
803 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
804 logbuf_assert("Duplicate keyword \"yang-version\". Line number 3.");
Radek Krejci09306362018-10-15 15:26:01 +0200805 mod = mod_renew(&ctx, mod, 0);
Radek Krejcia042ea12018-10-13 07:52:15 +0200806 str = SCHEMA_BEGINNING "yang-version 1.0;}";
807 assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod));
808 assert_int_equal(1, mod->version);
Radek Krejci09306362018-10-15 15:26:01 +0200809 mod = mod_renew(&ctx, mod, 0);
Radek Krejcia042ea12018-10-13 07:52:15 +0200810 str = SCHEMA_BEGINNING "yang-version \"1.1\";}";
811 assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod));
812 assert_int_equal(2, mod->version);
Radek Krejci09306362018-10-15 15:26:01 +0200813 mod = mod_renew(&ctx, mod, 0);
814
Radek Krejci156ccaf2018-10-15 15:49:17 +0200815 /* extensions */
816 TEST_GENERIC("prefix:test;}", mod->exts,
Radek Krejci2c4e7172018-10-19 15:56:26 +0200817 assert_string_equal("prefix:test", mod->exts[0].name);
818 assert_int_equal(LYEXT_SUBSTMT_SELF, mod->exts[0].insubstmt));
Radek Krejci156ccaf2018-10-15 15:49:17 +0200819 mod = mod_renew(&ctx, mod, 0);
820
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200821 /* invalid substatement */
822 str = SCHEMA_BEGINNING "must false;}";
823 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
824 logbuf_assert("Invalid keyword \"must\" as a child of \"module\". Line number 3.");
825 mod = mod_renew(&ctx, mod, 0);
826
Radek Krejci09306362018-10-15 15:26:01 +0200827 /* submodule */
828 mod->submodule = 1;
829
830 /* missing mandatory substatements */
831 str = " subname {}";
832 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
833 assert_string_equal("subname", mod->name);
834 logbuf_assert("Missing mandatory keyword \"belongs-to\" as a child of \"submodule\". Line number 3.");
835 mod = mod_renew(&ctx, mod, 1);
836
837 str = " subname {belongs-to name;}";
838 assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod));
839 assert_string_equal("name", mod->belongsto);
840 mod = mod_renew(&ctx, mod, 1);
841
842#undef SCHEMA_BEGINNING
843#define SCHEMA_BEGINNING " subname {belongs-to name;"
844
845 /* duplicated namespace, prefix */
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200846 TEST_DUP("belongs-to", "module1", "module2", "3", 1);
Radek Krejci09306362018-10-15 15:26:01 +0200847
848 /* not allowed in submodule (module-specific) */
849 str = SCHEMA_BEGINNING "namespace \"urn:z\";}";
850 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
851 logbuf_assert("Invalid keyword \"namespace\" as a child of \"submodule\". Line number 3.");
852 mod = mod_renew(&ctx, mod, 1);
853 str = SCHEMA_BEGINNING "prefix m;}}";
854 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
855 logbuf_assert("Invalid keyword \"prefix\" as a child of \"submodule\". Line number 3.");
856 mod = mod_renew(&ctx, mod, 1);
Radek Krejcia042ea12018-10-13 07:52:15 +0200857
858#undef TEST_GENERIC
859#undef TEST_NODE
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200860#undef TEST_DUP
Radek Krejcia042ea12018-10-13 07:52:15 +0200861#undef SCHEMA_BEGINNING
862
Radek Krejci9fcacc12018-10-11 15:59:11 +0200863 lysp_module_free(mod);
864 ly_ctx_destroy(ctx.ctx, NULL);
Radek Krejciefd22f62018-09-27 11:47:58 +0200865}
866
Radek Krejci4c6d9bd2018-10-15 16:43:06 +0200867static void
868test_identity(void **state)
869{
870 (void) state; /* unused */
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200871 int dict = 1; /* magic variable for FREE macros */
Radek Krejci4c6d9bd2018-10-15 16:43:06 +0200872
873 struct ly_parser_ctx ctx;
874 struct lysp_ident *ident = NULL;
875 const char *str;
Radek Krejci4c6d9bd2018-10-15 16:43:06 +0200876
877 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
878 assert_non_null(ctx.ctx);
879 ctx.line = 1;
880 ctx.indent = 0;
881
882 /* invalid cardinality */
883#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200884 TEST_DUP_GENERIC(" test {", MEMBER, VALUE1, VALUE2, parse_identity, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +0200885 &ident, "1", FREE_ARRAY(ctx.ctx, ident, lysp_ident_free); ident = NULL)
Radek Krejci4c6d9bd2018-10-15 16:43:06 +0200886
887 TEST_DUP("description", "a", "b");
888 TEST_DUP("reference", "a", "b");
889 TEST_DUP("status", "current", "obsolete");
890
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200891 /* full content */
892 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 +0200893 assert_int_equal(LY_SUCCESS, parse_identity(&ctx, &str, &ident));
894 assert_non_null(ident);
895 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +0200896 FREE_ARRAY(ctx.ctx, ident, lysp_ident_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200897 ident = NULL;
898
899 /* invalid substatement */
900 str = " test {organization XXX;}";
901 assert_int_equal(LY_EVALID, parse_identity(&ctx, &str, &ident));
902 logbuf_assert("Invalid keyword \"organization\" as a child of \"identity\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +0200903 FREE_ARRAY(ctx.ctx, ident, lysp_ident_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200904 ident = NULL;
905
906#undef TEST_DUP
907
908 ly_ctx_destroy(ctx.ctx, NULL);
909}
910
911static void
912test_feature(void **state)
913{
914 (void) state; /* unused */
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200915 int dict = 1; /* magic variable for FREE macros */
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200916
917 struct ly_parser_ctx ctx;
918 struct lysp_feature *features = NULL;
919 const char *str;
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200920
921 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
922 assert_non_null(ctx.ctx);
923 ctx.line = 1;
924 ctx.indent = 0;
925
926 /* invalid cardinality */
927#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
928 TEST_DUP_GENERIC(" test {", MEMBER, VALUE1, VALUE2, parse_feature, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +0200929 &features, "1", FREE_ARRAY(ctx.ctx, features, lysp_feature_free); features = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200930
931 TEST_DUP("description", "a", "b");
932 TEST_DUP("reference", "a", "b");
933 TEST_DUP("status", "current", "obsolete");
934
935 /* full content */
936 str = " test {description text;reference \'another text\';status current; if-feature x;if-feature y;prefix:ext;} ...";
937 assert_int_equal(LY_SUCCESS, parse_feature(&ctx, &str, &features));
938 assert_non_null(features);
939 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +0200940 FREE_ARRAY(ctx.ctx, features, lysp_feature_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200941 features = NULL;
942
943 /* invalid substatement */
944 str = " test {organization XXX;}";
945 assert_int_equal(LY_EVALID, parse_feature(&ctx, &str, &features));
946 logbuf_assert("Invalid keyword \"organization\" as a child of \"feature\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +0200947 FREE_ARRAY(ctx.ctx, features, lysp_feature_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200948 features = NULL;
949
950#undef TEST_DUP
951
952 ly_ctx_destroy(ctx.ctx, NULL);
953}
954
955static void
956test_deviation(void **state)
957{
958 (void) state; /* unused */
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200959 int dict = 1; /* magic variable for FREE macros */
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200960
961 struct ly_parser_ctx ctx;
962 struct lysp_deviation *d = NULL;
963 const char *str;
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200964
965 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
966 assert_non_null(ctx.ctx);
967 ctx.line = 1;
968 ctx.indent = 0;
969
970 /* invalid cardinality */
971#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
972 TEST_DUP_GENERIC(" test {deviate not-supported;", MEMBER, VALUE1, VALUE2, parse_deviation, \
Radek Krejcie53a8dc2018-10-17 12:52:40 +0200973 &d, "1", FREE_ARRAY(ctx.ctx, d, lysp_deviation_free); d = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200974
975 TEST_DUP("description", "a", "b");
976 TEST_DUP("reference", "a", "b");
977
978 /* full content */
979 str = " test {deviate not-supported;description text;reference \'another text\';prefix:ext;} ...";
980 assert_int_equal(LY_SUCCESS, parse_deviation(&ctx, &str, &d));
981 assert_non_null(d);
982 assert_string_equal(" ...", str);
Radek Krejcie53a8dc2018-10-17 12:52:40 +0200983 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200984 d = NULL;
985
986 /* missing mandatory substatement */
987 str = " test {description text;}";
988 assert_int_equal(LY_EVALID, parse_deviation(&ctx, &str, &d));
989 logbuf_assert("Missing mandatory keyword \"deviate\" as a child of \"deviation\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +0200990 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200991 d = NULL;
992
993 /* invalid substatement */
994 str = " test {deviate not-supported; status obsolete;}";
995 assert_int_equal(LY_EVALID, parse_deviation(&ctx, &str, &d));
996 logbuf_assert("Invalid keyword \"status\" as a child of \"deviation\". Line number 1.");
Radek Krejcie53a8dc2018-10-17 12:52:40 +0200997 FREE_ARRAY(ctx.ctx, d, lysp_deviation_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200998 d = NULL;
999
1000#undef TEST_DUP
1001
1002 ly_ctx_destroy(ctx.ctx, NULL);
1003}
1004
1005static void
1006test_deviate(void **state)
1007{
1008 (void) state; /* unused */
1009
1010 struct ly_parser_ctx ctx;
1011 struct lysp_deviate *d = NULL;
1012 const char *str;
1013
1014 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
1015 assert_non_null(ctx.ctx);
1016 ctx.line = 1;
1017 ctx.indent = 0;
1018
1019 /* invalid cardinality */
1020#define TEST_DUP(TYPE, MEMBER, VALUE1, VALUE2) \
1021 TEST_DUP_GENERIC(TYPE" {", MEMBER, VALUE1, VALUE2, parse_deviate, \
Radek Krejcidd4e8d42018-10-16 14:55:43 +02001022 &d, "1", lysp_deviate_free(ctx.ctx, d, 1); free(d); d = NULL)
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001023
1024 TEST_DUP("add", "config", "true", "false");
1025 TEST_DUP("replace", "default", "int8", "uint8");
1026 TEST_DUP("add", "mandatory", "true", "false");
1027 TEST_DUP("add", "max-elements", "1", "2");
1028 TEST_DUP("add", "min-elements", "1", "2");
1029 TEST_DUP("replace", "type", "int8", "uint8");
1030 TEST_DUP("add", "units", "kilometers", "miles");
1031
1032 /* full contents */
1033 str = " not-supported {prefix:ext;} ...";
1034 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1035 assert_non_null(d);
1036 assert_string_equal(" ...", str);
Radek Krejcidd4e8d42018-10-16 14:55:43 +02001037 lysp_deviate_free(ctx.ctx, d, 1); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001038 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;} ...";
1039 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1040 assert_non_null(d);
1041 assert_string_equal(" ...", str);
Radek Krejcidd4e8d42018-10-16 14:55:43 +02001042 lysp_deviate_free(ctx.ctx, d, 1); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001043 str = " delete {units meters; must 1; must 2; unique x; unique y; default a; default b; prefix:ext;} ...";
1044 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1045 assert_non_null(d);
1046 assert_string_equal(" ...", str);
Radek Krejcidd4e8d42018-10-16 14:55:43 +02001047 lysp_deviate_free(ctx.ctx, d, 1); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001048 str = " replace {type string; units meters; default a; config true; mandatory true; min-elements 1; max-elements 2; prefix:ext;} ...";
1049 assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d));
1050 assert_non_null(d);
1051 assert_string_equal(" ...", str);
Radek Krejcidd4e8d42018-10-16 14:55:43 +02001052 lysp_deviate_free(ctx.ctx, d, 1); free(d); d = NULL;
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001053
1054 /* invalid substatements */
1055#define TEST_NOT_SUP(DEV, STMT, VALUE) \
1056 str = " "DEV" {"STMT" "VALUE";}..."; \
1057 assert_int_equal(LY_EVALID, parse_deviate(&ctx, &str, &d)); \
1058 logbuf_assert("Deviate \""DEV"\" does not support keyword \""STMT"\". Line number 1."); \
Radek Krejcidd4e8d42018-10-16 14:55:43 +02001059 lysp_deviate_free(ctx.ctx, d, 1); free(d); d = NULL
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001060
1061 TEST_NOT_SUP("not-supported", "units", "meters");
1062 TEST_NOT_SUP("not-supported", "must", "1");
1063 TEST_NOT_SUP("not-supported", "unique", "x");
1064 TEST_NOT_SUP("not-supported", "default", "a");
1065 TEST_NOT_SUP("not-supported", "config", "true");
1066 TEST_NOT_SUP("not-supported", "mandatory", "true");
1067 TEST_NOT_SUP("not-supported", "min-elements", "1");
1068 TEST_NOT_SUP("not-supported", "max-elements", "2");
1069 TEST_NOT_SUP("not-supported", "type", "string");
1070 TEST_NOT_SUP("add", "type", "string");
1071 TEST_NOT_SUP("delete", "config", "true");
1072 TEST_NOT_SUP("delete", "mandatory", "true");
1073 TEST_NOT_SUP("delete", "min-elements", "1");
1074 TEST_NOT_SUP("delete", "max-elements", "2");
1075 TEST_NOT_SUP("delete", "type", "string");
1076 TEST_NOT_SUP("replace", "must", "1");
1077 TEST_NOT_SUP("replace", "unique", "a");
1078
1079 str = " nonsence; ...";
1080 assert_int_equal(LY_EVALID, parse_deviate(&ctx, &str, &d));
1081 logbuf_assert("Invalid value \"nonsence\" of \"deviate\". Line number 1.");
1082 assert_null(d);
1083
1084#undef TEST_NOT_SUP
1085#undef TEST_DUP
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001086
1087 ly_ctx_destroy(ctx.ctx, NULL);
1088}
1089
Radek Krejci80dd33e2018-09-26 15:57:18 +02001090int main(void)
1091{
1092 const struct CMUnitTest tests[] = {
Radek Krejci44ceedc2018-10-02 15:54:31 +02001093 cmocka_unit_test_setup(test_helpers, logger_setup),
Radek Krejci80dd33e2018-09-26 15:57:18 +02001094 cmocka_unit_test_setup(test_comments, logger_setup),
Radek Krejciefd22f62018-09-27 11:47:58 +02001095 cmocka_unit_test_setup(test_arg, logger_setup),
Radek Krejcidcc7b322018-10-11 14:24:02 +02001096 cmocka_unit_test_setup(test_stmts, logger_setup),
Radek Krejci9fcacc12018-10-11 15:59:11 +02001097 cmocka_unit_test_setup(test_module, logger_setup),
Radek Krejci4c6d9bd2018-10-15 16:43:06 +02001098 cmocka_unit_test_setup(test_identity, logger_setup),
Radek Krejci2c02f3e2018-10-16 10:54:38 +02001099 cmocka_unit_test_setup(test_feature, logger_setup),
1100 cmocka_unit_test_setup(test_deviation, logger_setup),
1101 cmocka_unit_test_setup(test_deviate, logger_setup),
Radek Krejci80dd33e2018-09-26 15:57:18 +02001102 };
1103
1104 return cmocka_run_group_tests(tests, NULL, NULL);
1105}