blob: e9d814a32e4ff0fe088585fc7a32a297402b12d8 [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
15#define _BSD_SOURCE
16#define _DEFAULT_SOURCE
17#include <stdarg.h>
18#include <stddef.h>
19#include <setjmp.h>
20#include <cmocka.h>
21
22#include <stdio.h>
23#include <string.h>
24
25#include "libyang.h"
26#include "../../src/parser_yang.c"
Radek Krejci4c6d9bd2018-10-15 16:43:06 +020027#include "../../src/tree_schema.c"
Radek Krejci80dd33e2018-09-26 15:57:18 +020028
29#define BUFSIZE 1024
30char logbuf[BUFSIZE] = {0};
31
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 */
40
41 if (path) {
42 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
43 } else {
44 strncpy(logbuf, msg, BUFSIZE - 1);
45 }
46}
Radek Krejcid5f2b5f2018-10-11 10:54:36 +020047#endif
Radek Krejci80dd33e2018-09-26 15:57:18 +020048
49static int
50logger_setup(void **state)
51{
52 (void) state; /* unused */
53#if ENABLE_LOGGER_CHECKING
54 ly_set_log_clb(logger, 1);
55#endif
56 return 0;
57}
58
59void
60logbuf_clean(void)
61{
62 logbuf[0] = '\0';
63}
64
65#if ENABLE_LOGGER_CHECKING
66# define logbuf_assert(str) assert_string_equal(logbuf, str)
67#else
68# define logbuf_assert(str)
69#endif
70
Radek Krejci2c02f3e2018-10-16 10:54:38 +020071#define TEST_DUP_GENERIC(PREFIX, MEMBER, VALUE1, VALUE2, FUNC, RESULT, LINE, CLEANUP) \
72 str = PREFIX MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \
73 assert_int_equal(LY_EVALID, FUNC(&ctx, &str, RESULT)); \
74 logbuf_assert("Duplicate keyword \""MEMBER"\". Line number "LINE"."); \
75 CLEANUP
76
Radek Krejci44ceedc2018-10-02 15:54:31 +020077static void
78test_helpers(void **state)
79{
80 (void) state; /* unused */
81
82 const char *str;
Radek Krejci404251e2018-10-09 12:06:44 +020083 char *buf, *p;
Radek Krejci44ceedc2018-10-02 15:54:31 +020084 size_t len, size;
85 int prefix;
86 struct ly_parser_ctx ctx;
87 ctx.ctx = NULL;
88 ctx.line = 1;
89
90 /* storing into buffer */
91 str = "abcd";
92 buf = NULL;
93 size = len = 0;
94 assert_int_equal(LY_SUCCESS, buf_add_char(NULL, &str, 2, &buf, &size, &len));
95 assert_int_not_equal(0, size);
96 assert_int_equal(2, len);
97 assert_string_equal("cd", str);
98 assert_false(strncmp("ab", buf, 2));
99 free(buf);
Radek Krejci404251e2018-10-09 12:06:44 +0200100 buf = NULL;
101
102 /* invalid first characters */
103 len = 0;
104 str = "2invalid";
105 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1));
106 str = ".invalid";
107 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1));
108 str = "-invalid";
109 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1));
110 /* invalid following characters */
111 len = 3; /* number of characters read before the str content */
112 str = "!";
113 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1));
114 str = ":";
115 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1));
116 /* valid colon for prefixed identifiers */
117 len = size = 0;
118 p = NULL;
119 str = "x:id";
120 assert_int_equal(LY_SUCCESS, buf_store_char(&ctx, &str, Y_PREF_IDENTIF_ARG, &p, &len, &buf, &size, 0));
121 assert_int_equal(1, len);
122 assert_null(buf);
123 assert_string_equal(":id", str);
124 assert_int_equal('x', p[len - 1]);
125 assert_int_equal(LY_SUCCESS, buf_store_char(&ctx, &str, Y_PREF_IDENTIF_ARG, &p, &len, &buf, &size, 1));
126 assert_int_equal(2, len);
127 assert_string_equal("id", str);
128 assert_int_equal(':', p[len - 1]);
129 free(buf);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200130
131 /* checking identifiers */
132 assert_int_equal(LY_EVALID, check_identifierchar(&ctx, ':', 0, NULL));
133 logbuf_assert("Invalid identifier character ':'. Line number 1.");
134 assert_int_equal(LY_EVALID, check_identifierchar(&ctx, '#', 1, NULL));
135 logbuf_assert("Invalid identifier first character '#'. Line number 1.");
136
137 assert_int_equal(LY_SUCCESS, check_identifierchar(&ctx, 'a', 1, &prefix));
138 assert_int_equal(0, prefix);
139 assert_int_equal(LY_SUCCESS, check_identifierchar(&ctx, ':', 0, &prefix));
140 assert_int_equal(1, prefix);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200141 assert_int_equal(LY_EVALID, check_identifierchar(&ctx, ':', 0, &prefix));
142 assert_int_equal(1, prefix);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200143 assert_int_equal(LY_SUCCESS, check_identifierchar(&ctx, 'b', 0, &prefix));
144 assert_int_equal(2, prefix);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200145 /* second colon is invalid */
146 assert_int_equal(LY_EVALID, check_identifierchar(&ctx, ':', 0, &prefix));
147 logbuf_assert("Invalid identifier character ':'. Line number 1.");
Radek Krejci44ceedc2018-10-02 15:54:31 +0200148}
Radek Krejci80dd33e2018-09-26 15:57:18 +0200149
150static void
151test_comments(void **state)
152{
153 (void) state; /* unused */
154
Radek Krejci44ceedc2018-10-02 15:54:31 +0200155 struct ly_parser_ctx ctx;
Radek Krejci80dd33e2018-09-26 15:57:18 +0200156 const char *str, *p;
Radek Krejciefd22f62018-09-27 11:47:58 +0200157 char *word, *buf;
158 size_t len;
Radek Krejci80dd33e2018-09-26 15:57:18 +0200159
Radek Krejci44ceedc2018-10-02 15:54:31 +0200160 ctx.ctx = NULL;
161 ctx.line = 1;
162
Radek Krejciefd22f62018-09-27 11:47:58 +0200163 str = " // this is a text of / one * line */ comment\nargument";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200164 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200165 assert_string_equal("argument", word);
166 assert_null(buf);
167 assert_int_equal(8, len);
Radek Krejci80dd33e2018-09-26 15:57:18 +0200168
Radek Krejciefd22f62018-09-27 11:47:58 +0200169 str = "/* this is a \n * text // of / block * comment */\"arg\" + \"ume\" \n + \n \"nt\"";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200170 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200171 assert_string_equal("argument", word);
172 assert_ptr_equal(buf, word);
173 assert_int_equal(8, len);
174 free(word);
Radek Krejci80dd33e2018-09-26 15:57:18 +0200175
176 str = p = " this is one line comment on last line";
Radek Krejci44ceedc2018-10-02 15:54:31 +0200177 assert_int_equal(LY_SUCCESS, skip_comment(&ctx, &str, 1));
Radek Krejci80dd33e2018-09-26 15:57:18 +0200178 assert_true(str[0] == '\0');
179
180 str = p = " this is a not terminated comment x";
Radek Krejci44ceedc2018-10-02 15:54:31 +0200181 assert_int_equal(LY_EVALID, skip_comment(&ctx, &str, 2));
182 logbuf_assert("Unexpected end-of-file, non-terminated comment. Line number 5.");
Radek Krejci80dd33e2018-09-26 15:57:18 +0200183 assert_true(str[0] == '\0');
184}
185
Radek Krejciefd22f62018-09-27 11:47:58 +0200186static void
187test_arg(void **state)
188{
189 (void) state; /* unused */
190
Radek Krejci44ceedc2018-10-02 15:54:31 +0200191 struct ly_parser_ctx ctx;
Radek Krejciefd22f62018-09-27 11:47:58 +0200192 const char *str;
193 char *word, *buf;
194 size_t len;
195
Radek Krejci44ceedc2018-10-02 15:54:31 +0200196 ctx.ctx = NULL;
197 ctx.line = 1;
198
Radek Krejciefd22f62018-09-27 11:47:58 +0200199 /* missing argument */
200 str = ";";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200201 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_MAYBE_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200202 assert_null(word);
203
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200204 str = "{";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200205 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200206 logbuf_assert("Invalid character sequence \"{\", expected an argument. Line number 1.");
207
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200208 /* invalid escape sequence */
209 str = "\"\\s\"";
210 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200211 logbuf_assert("Double-quoted string unknown special character \'\\s\'. Line number 1.");
212 str = "\'\\s\'"; /* valid, since it is not an escape sequence in single quoted string */
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200213 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200214 assert_int_equal(2, len);
215 assert_string_equal("\\s\'", word);
216 assert_int_equal('\0', str[0]); /* input has been eaten */
217
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200218 /* invalid character after the argument */
219 str = "hello\"";
220 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
221 logbuf_assert("Invalid character sequence \"\"\", expected unquoted string character, optsep, semicolon or opening brace. Line number 1.");
222 str = "hello}";
223 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
224 logbuf_assert("Invalid character sequence \"}\", expected unquoted string character, optsep, semicolon or opening brace. Line number 1.");
225
226 str = "hello/x\t"; /* slash is not an invalid character */
227 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
228 assert_int_equal(7, len);
229 assert_string_equal("hello/x\t", word);
230
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200231 assert_null(buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200232
233 /* different quoting */
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200234 str = "hello ";
235 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200236 assert_null(buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200237 assert_int_equal(5, len);
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200238 assert_string_equal("hello ", word);
Radek Krejciefd22f62018-09-27 11:47:58 +0200239
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200240 str = "hello/*comment*/\n";
241 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200242 assert_null(buf);
243 assert_int_equal(5, len);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200244 assert_false(strncmp("hello", word, len));
245
246
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200247 str = "\"hello\\n\\t\\\"\\\\\";";
248 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200249 assert_null(buf);
250 assert_int_equal(9, len);
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200251 assert_string_equal("hello\\n\\t\\\"\\\\\";", word);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200252
253 ctx.indent = 14;
254 str = "\"hello \t\n\t\t world!\"";
255 /* - space and tabs before newline are stripped out
256 * - space and tabs after newline (indentation) are stripped out
257 */
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200258 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200259 assert_non_null(buf);
260 assert_ptr_equal(word, buf);
261 assert_int_equal(14, len);
262 assert_string_equal("hello\n world!", word);
263 free(buf);
264
265 ctx.indent = 14;
266 str = "\"hello\n \tworld!\"";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200267 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200268 assert_non_null(buf);
269 assert_ptr_equal(word, buf);
270 assert_int_equal(12, len);
271 assert_string_equal("hello\nworld!", word);
272 free(buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200273
274 str = "\'hello\'";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200275 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200276 assert_null(buf);
277 assert_int_equal(5, len);
278 assert_false(strncmp("hello", word, 5));
279
280 str = "\"hel\" +\t\n\"lo\"";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200281 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200282 assert_ptr_equal(word, buf);
283 assert_int_equal(5, len);
284 assert_string_equal("hello", word);
285 free(buf);
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200286 str = "\"hel\" +\t\nlo"; /* unquoted the second part */
287 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
288 logbuf_assert("Both string parts divided by '+' must be quoted. Line number 5.");
Radek Krejciefd22f62018-09-27 11:47:58 +0200289
290 str = "\'he\'\t\n+ \"llo\"";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200291 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200292 assert_ptr_equal(word, buf);
293 assert_int_equal(5, len);
294 assert_string_equal("hello", word);
295 free(buf);
296
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200297 str = " \t\n\"he\"+\'llo\'";
298 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200299 assert_ptr_equal(word, buf);
300 assert_int_equal(5, len);
301 assert_string_equal("hello", word);
302 free(buf);
303
Radek Krejci44ceedc2018-10-02 15:54:31 +0200304 /* missing argument */
305 str = ";";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200306 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
307 logbuf_assert("Invalid character sequence \";\", expected an argument. Line number 7.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200308}
309
310static void
311test_stmts(void **state)
312{
313 (void) state; /* unused */
314
315 struct ly_parser_ctx ctx;
316 const char *str, *p;
317 enum yang_keyword kw;
318 char *word;
319 size_t len;
320
321 ctx.ctx = NULL;
322 ctx.line = 1;
323
324 str = "\n// comment\n\tinput\t{";
325 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
326 assert_int_equal(YANG_INPUT, kw);
327 assert_int_equal(5, len);
328 assert_string_equal("input\t{", word);
329 assert_string_equal("\t{", str);
330
331 str = "\t /* comment */\t output\n\t{";
332 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
333 assert_int_equal(YANG_OUTPUT, kw);
334 assert_int_equal(6, len);
335 assert_string_equal("output\n\t{", word);
336 assert_string_equal("\n\t{", str);
337
338 str = "/input { "; /* invalid slash */
339 assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len));
340 logbuf_assert("Invalid identifier first character '/'. Line number 4.");
341
342 str = "not-a-statement-nor-extension { "; /* invalid identifier */
343 assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len));
344 logbuf_assert("Invalid character sequence \"not-a-statement-nor-extension\", expected a keyword. Line number 4.");
345
346 str = "path;"; /* missing sep after the keyword */
347 assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len));
348 logbuf_assert("Invalid character sequence \"path;\", expected a keyword followed by a separator. Line number 4.");
349
350 str = "action ";
351 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
352 assert_int_equal(YANG_ACTION, kw);
353 assert_int_equal(6, len);
354 str = "anydata ";
355 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
356 assert_int_equal(YANG_ANYDATA, kw);
357 assert_int_equal(7, len);
358 str = "anyxml ";
359 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
360 assert_int_equal(YANG_ANYXML, kw);
361 assert_int_equal(6, len);
362 str = "argument ";
363 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
364 assert_int_equal(YANG_ARGUMENT, kw);
365 assert_int_equal(8, len);
366 str = "augment ";
367 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
368 assert_int_equal(YANG_AUGMENT, kw);
369 assert_int_equal(7, len);
370 str = "base ";
371 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
372 assert_int_equal(YANG_BASE, kw);
373 assert_int_equal(4, len);
374 str = "belongs-to ";
375 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
376 assert_int_equal(YANG_BELONGS_TO, kw);
377 assert_int_equal(10, len);
378 str = "bit ";
379 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
380 assert_int_equal(YANG_BIT, kw);
381 assert_int_equal(3, len);
382 str = "case ";
383 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
384 assert_int_equal(YANG_CASE, kw);
385 assert_int_equal(4, len);
386 str = "choice ";
387 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
388 assert_int_equal(YANG_CHOICE, kw);
389 assert_int_equal(6, len);
390 str = "config ";
391 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
392 assert_int_equal(YANG_CONFIG, kw);
393 assert_int_equal(6, len);
394 str = "contact ";
395 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
396 assert_int_equal(YANG_CONTACT, kw);
397 assert_int_equal(7, len);
398 str = "container ";
399 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
400 assert_int_equal(YANG_CONTAINER, kw);
401 assert_int_equal(9, len);
402 str = "default ";
403 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
404 assert_int_equal(YANG_DEFAULT, kw);
405 assert_int_equal(7, len);
406 str = "description ";
407 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
408 assert_int_equal(YANG_DESCRIPTION, kw);
409 assert_int_equal(11, len);
410 str = "deviate ";
411 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
412 assert_int_equal(YANG_DEVIATE, kw);
413 assert_int_equal(7, len);
414 str = "deviation ";
415 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
416 assert_int_equal(YANG_DEVIATION, kw);
417 assert_int_equal(9, len);
418 str = "enum ";
419 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
420 assert_int_equal(YANG_ENUM, kw);
421 assert_int_equal(4, len);
422 str = "error-app-tag ";
423 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
424 assert_int_equal(YANG_ERROR_APP_TAG, kw);
425 assert_int_equal(13, len);
426 str = "error-message ";
427 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
428 assert_int_equal(YANG_ERROR_MESSAGE, kw);
429 assert_int_equal(13, len);
430 str = "extension ";
431 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
432 assert_int_equal(YANG_EXTENSION, kw);
433 assert_int_equal(9, len);
434 str = "feature ";
435 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
436 assert_int_equal(YANG_FEATURE, kw);
437 assert_int_equal(7, len);
438 str = "fraction-digits ";
439 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
440 assert_int_equal(YANG_FRACTION_DIGITS, kw);
441 assert_int_equal(15, len);
442 str = "grouping ";
443 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
444 assert_int_equal(YANG_GROUPING, kw);
445 assert_int_equal(8, len);
446 str = "identity ";
447 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
448 assert_int_equal(YANG_IDENTITY, kw);
449 assert_int_equal(8, len);
450 str = "if-feature ";
451 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
452 assert_int_equal(YANG_IF_FEATURE, kw);
453 assert_int_equal(10, len);
454 str = "import ";
455 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
456 assert_int_equal(YANG_IMPORT, kw);
457 assert_int_equal(6, len);
458 str = "include ";
459 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
460 assert_int_equal(YANG_INCLUDE, kw);
461 assert_int_equal(7, len);
462 str = "input{";
463 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
464 assert_int_equal(YANG_INPUT, kw);
465 assert_int_equal(5, len);
466 str = "key ";
467 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
468 assert_int_equal(YANG_KEY, kw);
469 assert_int_equal(3, len);
470 str = "leaf ";
471 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
472 assert_int_equal(YANG_LEAF, kw);
473 assert_int_equal(4, len);
474 str = "leaf-list ";
475 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
476 assert_int_equal(YANG_LEAF_LIST, kw);
477 assert_int_equal(9, len);
478 str = "length ";
479 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
480 assert_int_equal(YANG_LENGTH, kw);
481 assert_int_equal(6, len);
482 str = "list ";
483 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
484 assert_int_equal(YANG_LIST, kw);
485 assert_int_equal(4, len);
486 str = "mandatory ";
487 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
488 assert_int_equal(YANG_MANDATORY, kw);
489 assert_int_equal(9, len);
490 str = "max-elements ";
491 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
492 assert_int_equal(YANG_MAX_ELEMENTS, kw);
493 assert_int_equal(12, len);
494 str = "min-elements ";
495 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
496 assert_int_equal(YANG_MIN_ELEMENTS, kw);
497 assert_int_equal(12, len);
498 str = "modifier ";
499 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
500 assert_int_equal(YANG_MODIFIER, kw);
501 assert_int_equal(8, len);
502 str = "module ";
503 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
504 assert_int_equal(YANG_MODULE, kw);
505 assert_int_equal(6, len);
506 str = "must ";
507 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
508 assert_int_equal(YANG_MUST, kw);
509 assert_int_equal(4, len);
510 str = "namespace ";
511 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
512 assert_int_equal(YANG_NAMESPACE, kw);
513 assert_int_equal(9, len);
514 str = "notification ";
515 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
516 assert_int_equal(YANG_NOTIFICATION, kw);
517 assert_int_equal(12, len);
518 str = "ordered-by ";
519 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
520 assert_int_equal(YANG_ORDERED_BY, kw);
521 assert_int_equal(10, len);
522 str = "organization ";
523 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
524 assert_int_equal(YANG_ORGANIZATION, kw);
525 assert_int_equal(12, len);
526 str = "output ";
527 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
528 assert_int_equal(YANG_OUTPUT, kw);
529 assert_int_equal(6, len);
530 str = "path ";
531 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
532 assert_int_equal(YANG_PATH, kw);
533 assert_int_equal(4, len);
534 str = "pattern ";
535 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
536 assert_int_equal(YANG_PATTERN, kw);
537 assert_int_equal(7, len);
538 str = "position ";
539 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
540 assert_int_equal(YANG_POSITION, kw);
541 assert_int_equal(8, len);
542 str = "prefix ";
543 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
544 assert_int_equal(YANG_PREFIX, kw);
545 assert_int_equal(6, len);
546 str = "presence ";
547 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
548 assert_int_equal(YANG_PRESENCE, kw);
549 assert_int_equal(8, len);
550 str = "range ";
551 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
552 assert_int_equal(YANG_RANGE, kw);
553 assert_int_equal(5, len);
554 str = "reference ";
555 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
556 assert_int_equal(YANG_REFERENCE, kw);
557 assert_int_equal(9, len);
558 str = "refine ";
559 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
560 assert_int_equal(YANG_REFINE, kw);
561 assert_int_equal(6, len);
562 str = "require-instance ";
563 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
564 assert_int_equal(YANG_REQUIRE_INSTANCE, kw);
565 assert_int_equal(16, len);
566 str = "revision ";
567 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
568 assert_int_equal(YANG_REVISION, kw);
569 assert_int_equal(8, len);
570 str = "revision-date ";
571 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
572 assert_int_equal(YANG_REVISION_DATE, kw);
573 assert_int_equal(13, len);
574 str = "rpc ";
575 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
576 assert_int_equal(YANG_RPC, kw);
577 assert_int_equal(3, len);
578 str = "status ";
579 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
580 assert_int_equal(YANG_STATUS, kw);
581 assert_int_equal(6, len);
582 str = "submodule ";
583 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
584 assert_int_equal(YANG_SUBMODULE, kw);
585 assert_int_equal(9, len);
586 str = "type ";
587 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
588 assert_int_equal(YANG_TYPE, kw);
589 assert_int_equal(4, len);
590 str = "typedef ";
591 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
592 assert_int_equal(YANG_TYPEDEF, kw);
593 assert_int_equal(7, len);
594 str = "unique ";
595 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
596 assert_int_equal(YANG_UNIQUE, kw);
597 assert_int_equal(6, len);
598 str = "units ";
599 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
600 assert_int_equal(YANG_UNITS, kw);
601 assert_int_equal(5, len);
602 str = "uses ";
603 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
604 assert_int_equal(YANG_USES, kw);
605 assert_int_equal(4, len);
606 str = "value ";
607 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
608 assert_int_equal(YANG_VALUE, kw);
609 assert_int_equal(5, len);
610 str = "when ";
611 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
612 assert_int_equal(YANG_WHEN, kw);
613 assert_int_equal(4, len);
614 str = "yang-version ";
615 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
616 assert_int_equal(YANG_YANG_VERSION, kw);
617 assert_int_equal(12, len);
618 str = "yin-element ";
619 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
620 assert_int_equal(YANG_YIN_ELEMENT, kw);
621 assert_int_equal(11, len);
Radek Krejci626df482018-10-11 15:06:31 +0200622 str = ";config false;";
623 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
624 assert_int_equal(YANG_SEMICOLON, kw);
625 assert_int_equal(1, len);
626 assert_string_equal("config false;", str);
627 str = "{ config false;";
628 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
629 assert_int_equal(YANG_LEFT_BRACE, kw);
630 assert_int_equal(1, len);
631 assert_string_equal(" config false;", str);
632 str = "}";
633 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
634 assert_int_equal(YANG_RIGHT_BRACE, kw);
635 assert_int_equal(1, len);
636 assert_string_equal("", str);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200637
638 /* geenric extension */
639 str = p = "nacm:default-deny-write;";
640 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
641 assert_int_equal(YANG_CUSTOM, kw);
642 assert_int_equal(23, len);
643 assert_ptr_equal(p, word);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200644}
Radek Krejci44ceedc2018-10-02 15:54:31 +0200645
Radek Krejci9fcacc12018-10-11 15:59:11 +0200646static struct lysp_module *
Radek Krejci09306362018-10-15 15:26:01 +0200647mod_renew(struct ly_parser_ctx *ctx, struct lysp_module *mod, uint8_t submodule)
Radek Krejci9fcacc12018-10-11 15:59:11 +0200648{
649 lysp_module_free(mod);
650 mod = calloc(1, sizeof *mod);
651 mod->ctx = ctx->ctx;
Radek Krejci09306362018-10-15 15:26:01 +0200652 mod->submodule = submodule;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200653 assert_non_null(mod);
654 return mod;
655}
656
657static void
658test_module(void **state)
659{
660 (void) state; /* unused */
661
662 struct ly_parser_ctx ctx;
663 struct lysp_module *mod;
664 const char *str;
665
666 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
667 assert_non_null(ctx.ctx);
668 ctx.line = 1;
Radek Krejcia042ea12018-10-13 07:52:15 +0200669 ctx.indent = 0;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200670
Radek Krejci09306362018-10-15 15:26:01 +0200671 mod = mod_renew(&ctx, NULL, 0);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200672
673 /* missing mandatory substatements */
674 str = " name {}";
675 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
676 assert_string_equal("name", mod->name);
677 logbuf_assert("Missing mandatory keyword \"namespace\" as a child of \"module\". Line number 1.");
Radek Krejci09306362018-10-15 15:26:01 +0200678 mod = mod_renew(&ctx, mod, 0);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200679
680 str = " name {namespace urn:x;}";
681 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
682 assert_string_equal("urn:x", mod->ns);
683 logbuf_assert("Missing mandatory keyword \"prefix\" as a child of \"module\". Line number 1.");
Radek Krejci09306362018-10-15 15:26:01 +0200684 mod = mod_renew(&ctx, mod, 0);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200685
686 str = " name {namespace urn:x;prefix \"x\";}";
687 assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod));
688 assert_string_equal("x", mod->prefix);
Radek Krejci09306362018-10-15 15:26:01 +0200689 mod = mod_renew(&ctx, mod, 0);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200690
Radek Krejcia042ea12018-10-13 07:52:15 +0200691#define SCHEMA_BEGINNING " name {namespace urn:x;prefix \"x\";"
692#define TEST_NODE(NODETYPE, INPUT, NAME) \
693 str = SCHEMA_BEGINNING INPUT; \
694 assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod)); \
695 assert_non_null(mod->data); \
696 assert_int_equal(NODETYPE, mod->data->nodetype); \
697 assert_string_equal(NAME, mod->data->name); \
Radek Krejci09306362018-10-15 15:26:01 +0200698 mod = mod_renew(&ctx, mod, 0);
Radek Krejcia042ea12018-10-13 07:52:15 +0200699#define TEST_GENERIC(INPUT, TARGET, TEST) \
700 str = SCHEMA_BEGINNING INPUT; \
701 assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod)); \
702 assert_non_null(TARGET); \
703 TEST; \
Radek Krejci09306362018-10-15 15:26:01 +0200704 mod = mod_renew(&ctx, mod, 0);
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200705#define TEST_DUP(MEMBER, VALUE1, VALUE2, LINE, SUBMODULE) \
706 TEST_DUP_GENERIC(SCHEMA_BEGINNING, MEMBER, VALUE1, VALUE2, \
707 parse_sub_module, mod, LINE, mod = mod_renew(&ctx, mod, SUBMODULE))
Radek Krejcia042ea12018-10-13 07:52:15 +0200708
709 /* duplicated namespace, prefix */
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200710 TEST_DUP("namespace", "y", "z", "1", 0);
711 TEST_DUP("prefix", "y", "z", "1", 0);
712 TEST_DUP("contact", "a", "b", "1", 0);
713 TEST_DUP("description", "a", "b", "1", 0);
714 TEST_DUP("organization", "a", "b", "1", 0);
715 TEST_DUP("reference", "a", "b", "1", 0);
Radek Krejcia042ea12018-10-13 07:52:15 +0200716
Radek Krejci70853c52018-10-15 14:46:16 +0200717 /* not allowed in module (submodule-specific) */
718 str = SCHEMA_BEGINNING "belongs-to master {prefix m;}}";
719 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
720 logbuf_assert("Invalid keyword \"belongs-to\" as a child of \"module\". Line number 1.");
Radek Krejci09306362018-10-15 15:26:01 +0200721 mod = mod_renew(&ctx, mod, 0);
Radek Krejci70853c52018-10-15 14:46:16 +0200722
Radek Krejcia042ea12018-10-13 07:52:15 +0200723 /* anydata */
724 TEST_NODE(LYS_ANYDATA, "anydata test;}", "test");
725 /* anyxml */
726 TEST_NODE(LYS_ANYXML, "anyxml test;}", "test");
727 /* augment */
728 TEST_GENERIC("augment /somepath;}", mod->augments,
729 assert_string_equal("/somepath", mod->augments[0].nodeid));
730 /* choice */
731 TEST_NODE(LYS_CHOICE, "choice test;}", "test");
732 /* contact 0..1 */
733 TEST_GENERIC("contact \"firstname\" + \n\t\" surname\";}", mod->contact,
734 assert_string_equal("firstname surname", mod->contact));
735 /* container */
736 TEST_NODE(LYS_CONTAINER, "container test;}", "test");
737 /* description 0..1 */
738 TEST_GENERIC("description \'some description\';}", mod->dsc,
739 assert_string_equal("some description", mod->dsc));
740 /* deviation */
741 TEST_GENERIC("deviation /somepath {deviate not-supported;}}", mod->deviations,
742 assert_string_equal("/somepath", mod->deviations[0].nodeid));
743 /* extension */
744 TEST_GENERIC("extension test;}", mod->extensions,
745 assert_string_equal("test", mod->extensions[0].name));
746 /* feature */
747 TEST_GENERIC("feature test;}", mod->features,
748 assert_string_equal("test", mod->features[0].name));
749 /* grouping */
750 TEST_GENERIC("grouping grp;}", mod->groupings,
751 assert_string_equal("grp", mod->groupings[0].name));
752 /* identity */
753 TEST_GENERIC("identity test;}", mod->identities,
754 assert_string_equal("test", mod->identities[0].name));
755 /* import */
756 TEST_GENERIC("import test {prefix z;}}", mod->imports,
757 assert_string_equal("test", mod->imports[0].name));
Radek Krejci70853c52018-10-15 14:46:16 +0200758
Radek Krejcia042ea12018-10-13 07:52:15 +0200759 /* import - prefix collision */
760 str = SCHEMA_BEGINNING "import test {prefix x;}}";
761 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
Radek Krejci70853c52018-10-15 14:46:16 +0200762 logbuf_assert("Prefix \"x\" already used as module prefix. Line number 2.");
Radek Krejci09306362018-10-15 15:26:01 +0200763 mod = mod_renew(&ctx, mod, 0);
Radek Krejci70853c52018-10-15 14:46:16 +0200764
Radek Krejcia042ea12018-10-13 07:52:15 +0200765 /* include */
Radek Krejci469f70d2018-10-15 15:27:29 +0200766 TEST_GENERIC("include test;}", mod->includes,
767 assert_string_equal("test", mod->includes[0].name));
Radek Krejcia042ea12018-10-13 07:52:15 +0200768 /* leaf */
769 TEST_NODE(LYS_LEAF, "leaf test {type string;}}", "test");
770 /* leaf-list */
771 TEST_NODE(LYS_LEAFLIST, "leaf-list test {type string;}}", "test");
772 /* list */
773 TEST_NODE(LYS_LIST, "list test {key a;leaf a {type string;}}}", "test");
774 /* notification */
775 TEST_GENERIC("notification test;}", mod->notifs,
776 assert_string_equal("test", mod->notifs[0].name));
777 /* organization 0..1 */
778 TEST_GENERIC("organization \"CESNET a.l.e.\";}", mod->org,
779 assert_string_equal("CESNET a.l.e.", mod->org));
780 /* reference 0..1 */
781 TEST_GENERIC("reference RFC7950;}", mod->ref,
782 assert_string_equal("RFC7950", mod->ref));
783 /* revision */
784 TEST_GENERIC("revision 2018-10-12;}", mod->revs,
785 assert_string_equal("2018-10-12", mod->revs[0].rev));
786 /* rpc */
787 TEST_GENERIC("rpc test;}", mod->rpcs,
788 assert_string_equal("test", mod->rpcs[0].name));
789 /* typedef */
790 TEST_GENERIC("typedef test{type string;}}", mod->typedefs,
791 assert_string_equal("test", mod->typedefs[0].name));
792 /* uses */
793 TEST_NODE(LYS_USES, "uses test;}", "test");
794 /* yang-version */
795 str = SCHEMA_BEGINNING "\n\tyang-version 10;}";
796 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
797 logbuf_assert("Invalid value \"10\" of \"yang-version\". Line number 3.");
Radek Krejci09306362018-10-15 15:26:01 +0200798 mod = mod_renew(&ctx, mod, 0);
Radek Krejcia042ea12018-10-13 07:52:15 +0200799 str = SCHEMA_BEGINNING "yang-version 1.0;yang-version 1.1;}";
800 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
801 logbuf_assert("Duplicate keyword \"yang-version\". Line number 3.");
Radek Krejci09306362018-10-15 15:26:01 +0200802 mod = mod_renew(&ctx, mod, 0);
Radek Krejcia042ea12018-10-13 07:52:15 +0200803 str = SCHEMA_BEGINNING "yang-version 1.0;}";
804 assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod));
805 assert_int_equal(1, mod->version);
Radek Krejci09306362018-10-15 15:26:01 +0200806 mod = mod_renew(&ctx, mod, 0);
Radek Krejcia042ea12018-10-13 07:52:15 +0200807 str = SCHEMA_BEGINNING "yang-version \"1.1\";}";
808 assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod));
809 assert_int_equal(2, mod->version);
Radek Krejci09306362018-10-15 15:26:01 +0200810 mod = mod_renew(&ctx, mod, 0);
811
Radek Krejci156ccaf2018-10-15 15:49:17 +0200812 /* extensions */
813 TEST_GENERIC("prefix:test;}", mod->exts,
814 assert_string_equal("prefix:test", mod->exts[0].name);
815 assert_int_equal(LYEXT_SUBSTMT_SELF, mod->exts[0].insubstmt));
816 mod = mod_renew(&ctx, mod, 0);
817
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200818 /* invalid substatement */
819 str = SCHEMA_BEGINNING "must false;}";
820 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
821 logbuf_assert("Invalid keyword \"must\" as a child of \"module\". Line number 3.");
822 mod = mod_renew(&ctx, mod, 0);
823
Radek Krejci09306362018-10-15 15:26:01 +0200824 /* submodule */
825 mod->submodule = 1;
826
827 /* missing mandatory substatements */
828 str = " subname {}";
829 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
830 assert_string_equal("subname", mod->name);
831 logbuf_assert("Missing mandatory keyword \"belongs-to\" as a child of \"submodule\". Line number 3.");
832 mod = mod_renew(&ctx, mod, 1);
833
834 str = " subname {belongs-to name;}";
835 assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod));
836 assert_string_equal("name", mod->belongsto);
837 mod = mod_renew(&ctx, mod, 1);
838
839#undef SCHEMA_BEGINNING
840#define SCHEMA_BEGINNING " subname {belongs-to name;"
841
842 /* duplicated namespace, prefix */
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200843 TEST_DUP("belongs-to", "module1", "module2", "3", 1);
Radek Krejci09306362018-10-15 15:26:01 +0200844
845 /* not allowed in submodule (module-specific) */
846 str = SCHEMA_BEGINNING "namespace \"urn:z\";}";
847 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
848 logbuf_assert("Invalid keyword \"namespace\" as a child of \"submodule\". Line number 3.");
849 mod = mod_renew(&ctx, mod, 1);
850 str = SCHEMA_BEGINNING "prefix m;}}";
851 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
852 logbuf_assert("Invalid keyword \"prefix\" as a child of \"submodule\". Line number 3.");
853 mod = mod_renew(&ctx, mod, 1);
Radek Krejcia042ea12018-10-13 07:52:15 +0200854
855#undef TEST_GENERIC
856#undef TEST_NODE
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200857#undef TEST_DUP
Radek Krejcia042ea12018-10-13 07:52:15 +0200858#undef SCHEMA_BEGINNING
859
Radek Krejci9fcacc12018-10-11 15:59:11 +0200860 lysp_module_free(mod);
861 ly_ctx_destroy(ctx.ctx, NULL);
Radek Krejciefd22f62018-09-27 11:47:58 +0200862}
863
Radek Krejci4c6d9bd2018-10-15 16:43:06 +0200864static void
865test_identity(void **state)
866{
867 (void) state; /* unused */
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200868 int dict = 1; /* magic variable for FREE macros */
Radek Krejci4c6d9bd2018-10-15 16:43:06 +0200869
870 struct ly_parser_ctx ctx;
871 struct lysp_ident *ident = NULL;
872 const char *str;
873 unsigned int u;
874
875 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
876 assert_non_null(ctx.ctx);
877 ctx.line = 1;
878 ctx.indent = 0;
879
880 /* invalid cardinality */
881#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200882 TEST_DUP_GENERIC(" test {", MEMBER, VALUE1, VALUE2, parse_identity, \
883 &ident, "1", FREE_ARRAY(ctx.ctx, ident, u, lysp_ident_free); ident = NULL)
Radek Krejci4c6d9bd2018-10-15 16:43:06 +0200884
885 TEST_DUP("description", "a", "b");
886 TEST_DUP("reference", "a", "b");
887 TEST_DUP("status", "current", "obsolete");
888
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200889 /* full content */
890 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 +0200891 assert_int_equal(LY_SUCCESS, parse_identity(&ctx, &str, &ident));
892 assert_non_null(ident);
893 assert_string_equal(" ...", str);
894 FREE_ARRAY(ctx.ctx, ident, u, lysp_ident_free);
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200895 ident = NULL;
896
897 /* invalid substatement */
898 str = " test {organization XXX;}";
899 assert_int_equal(LY_EVALID, parse_identity(&ctx, &str, &ident));
900 logbuf_assert("Invalid keyword \"organization\" as a child of \"identity\". Line number 1.");
901 FREE_ARRAY(ctx.ctx, ident, u, lysp_ident_free);
902 ident = NULL;
903
904#undef TEST_DUP
905
906 ly_ctx_destroy(ctx.ctx, NULL);
907}
908
909static void
910test_feature(void **state)
911{
912 (void) state; /* unused */
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200913 int dict = 1; /* magic variable for FREE macros */
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200914
915 struct ly_parser_ctx ctx;
916 struct lysp_feature *features = NULL;
917 const char *str;
918 unsigned int u;
919
920 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
921 assert_non_null(ctx.ctx);
922 ctx.line = 1;
923 ctx.indent = 0;
924
925 /* invalid cardinality */
926#define TEST_DUP(MEMBER, VALUE1, VALUE2) \
927 TEST_DUP_GENERIC(" test {", MEMBER, VALUE1, VALUE2, parse_feature, \
928 &features, "1", FREE_ARRAY(ctx.ctx, features, u, lysp_feature_free); features = NULL)
929
930 TEST_DUP("description", "a", "b");
931 TEST_DUP("reference", "a", "b");
932 TEST_DUP("status", "current", "obsolete");
933
934 /* full content */
935 str = " test {description text;reference \'another text\';status current; if-feature x;if-feature y;prefix:ext;} ...";
936 assert_int_equal(LY_SUCCESS, parse_feature(&ctx, &str, &features));
937 assert_non_null(features);
938 assert_string_equal(" ...", str);
939 FREE_ARRAY(ctx.ctx, features, u, lysp_feature_free);
940 features = NULL;
941
942 /* invalid substatement */
943 str = " test {organization XXX;}";
944 assert_int_equal(LY_EVALID, parse_feature(&ctx, &str, &features));
945 logbuf_assert("Invalid keyword \"organization\" as a child of \"feature\". Line number 1.");
946 FREE_ARRAY(ctx.ctx, features, u, lysp_feature_free);
947 features = NULL;
948
949#undef TEST_DUP
950
951 ly_ctx_destroy(ctx.ctx, NULL);
952}
953
954static void
955test_deviation(void **state)
956{
957 (void) state; /* unused */
Radek Krejcidd4e8d42018-10-16 14:55:43 +0200958 int dict = 1; /* magic variable for FREE macros */
Radek Krejci2c02f3e2018-10-16 10:54:38 +0200959
960 struct ly_parser_ctx ctx;
961 struct lysp_deviation *d = NULL;
962 const char *str;
963 unsigned int u;
964
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, \
973 &d, "1", FREE_ARRAY(ctx.ctx, d, u, lysp_deviation_free); d = NULL)
974
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);
983 FREE_ARRAY(ctx.ctx, d, u, lysp_deviation_free);
984 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.");
990 FREE_ARRAY(ctx.ctx, d, u, lysp_deviation_free);
991 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.");
997 FREE_ARRAY(ctx.ctx, d, u, lysp_deviation_free);
998 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}