blob: 21b4dc064244831bb7065ad84245944aa87baece [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"
27
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 Krejci44ceedc2018-10-02 15:54:31 +020070static void
71test_helpers(void **state)
72{
73 (void) state; /* unused */
74
75 const char *str;
Radek Krejci404251e2018-10-09 12:06:44 +020076 char *buf, *p;
Radek Krejci44ceedc2018-10-02 15:54:31 +020077 size_t len, size;
78 int prefix;
79 struct ly_parser_ctx ctx;
80 ctx.ctx = NULL;
81 ctx.line = 1;
82
83 /* storing into buffer */
84 str = "abcd";
85 buf = NULL;
86 size = len = 0;
87 assert_int_equal(LY_SUCCESS, buf_add_char(NULL, &str, 2, &buf, &size, &len));
88 assert_int_not_equal(0, size);
89 assert_int_equal(2, len);
90 assert_string_equal("cd", str);
91 assert_false(strncmp("ab", buf, 2));
92 free(buf);
Radek Krejci404251e2018-10-09 12:06:44 +020093 buf = NULL;
94
95 /* invalid first characters */
96 len = 0;
97 str = "2invalid";
98 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1));
99 str = ".invalid";
100 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1));
101 str = "-invalid";
102 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1));
103 /* invalid following characters */
104 len = 3; /* number of characters read before the str content */
105 str = "!";
106 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1));
107 str = ":";
108 assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1));
109 /* valid colon for prefixed identifiers */
110 len = size = 0;
111 p = NULL;
112 str = "x:id";
113 assert_int_equal(LY_SUCCESS, buf_store_char(&ctx, &str, Y_PREF_IDENTIF_ARG, &p, &len, &buf, &size, 0));
114 assert_int_equal(1, len);
115 assert_null(buf);
116 assert_string_equal(":id", str);
117 assert_int_equal('x', p[len - 1]);
118 assert_int_equal(LY_SUCCESS, buf_store_char(&ctx, &str, Y_PREF_IDENTIF_ARG, &p, &len, &buf, &size, 1));
119 assert_int_equal(2, len);
120 assert_string_equal("id", str);
121 assert_int_equal(':', p[len - 1]);
122 free(buf);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200123
124 /* checking identifiers */
125 assert_int_equal(LY_EVALID, check_identifierchar(&ctx, ':', 0, NULL));
126 logbuf_assert("Invalid identifier character ':'. Line number 1.");
127 assert_int_equal(LY_EVALID, check_identifierchar(&ctx, '#', 1, NULL));
128 logbuf_assert("Invalid identifier first character '#'. Line number 1.");
129
130 assert_int_equal(LY_SUCCESS, check_identifierchar(&ctx, 'a', 1, &prefix));
131 assert_int_equal(0, prefix);
132 assert_int_equal(LY_SUCCESS, check_identifierchar(&ctx, ':', 0, &prefix));
133 assert_int_equal(1, prefix);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200134 assert_int_equal(LY_EVALID, check_identifierchar(&ctx, ':', 0, &prefix));
135 assert_int_equal(1, prefix);
Radek Krejci44ceedc2018-10-02 15:54:31 +0200136 assert_int_equal(LY_SUCCESS, check_identifierchar(&ctx, 'b', 0, &prefix));
137 assert_int_equal(2, prefix);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200138 /* second colon is invalid */
139 assert_int_equal(LY_EVALID, check_identifierchar(&ctx, ':', 0, &prefix));
140 logbuf_assert("Invalid identifier character ':'. Line number 1.");
Radek Krejci44ceedc2018-10-02 15:54:31 +0200141}
Radek Krejci80dd33e2018-09-26 15:57:18 +0200142
143static void
144test_comments(void **state)
145{
146 (void) state; /* unused */
147
Radek Krejci44ceedc2018-10-02 15:54:31 +0200148 struct ly_parser_ctx ctx;
Radek Krejci80dd33e2018-09-26 15:57:18 +0200149 const char *str, *p;
Radek Krejciefd22f62018-09-27 11:47:58 +0200150 char *word, *buf;
151 size_t len;
Radek Krejci80dd33e2018-09-26 15:57:18 +0200152
Radek Krejci44ceedc2018-10-02 15:54:31 +0200153 ctx.ctx = NULL;
154 ctx.line = 1;
155
Radek Krejciefd22f62018-09-27 11:47:58 +0200156 str = " // this is a text of / one * line */ comment\nargument";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200157 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200158 assert_string_equal("argument", word);
159 assert_null(buf);
160 assert_int_equal(8, len);
Radek Krejci80dd33e2018-09-26 15:57:18 +0200161
Radek Krejciefd22f62018-09-27 11:47:58 +0200162 str = "/* this is a \n * text // of / block * comment */\"arg\" + \"ume\" \n + \n \"nt\"";
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_ptr_equal(buf, word);
166 assert_int_equal(8, len);
167 free(word);
Radek Krejci80dd33e2018-09-26 15:57:18 +0200168
169 str = p = " this is one line comment on last line";
Radek Krejci44ceedc2018-10-02 15:54:31 +0200170 assert_int_equal(LY_SUCCESS, skip_comment(&ctx, &str, 1));
Radek Krejci80dd33e2018-09-26 15:57:18 +0200171 assert_true(str[0] == '\0');
172
173 str = p = " this is a not terminated comment x";
Radek Krejci44ceedc2018-10-02 15:54:31 +0200174 assert_int_equal(LY_EVALID, skip_comment(&ctx, &str, 2));
175 logbuf_assert("Unexpected end-of-file, non-terminated comment. Line number 5.");
Radek Krejci80dd33e2018-09-26 15:57:18 +0200176 assert_true(str[0] == '\0');
177}
178
Radek Krejciefd22f62018-09-27 11:47:58 +0200179static void
180test_arg(void **state)
181{
182 (void) state; /* unused */
183
Radek Krejci44ceedc2018-10-02 15:54:31 +0200184 struct ly_parser_ctx ctx;
Radek Krejciefd22f62018-09-27 11:47:58 +0200185 const char *str;
186 char *word, *buf;
187 size_t len;
188
Radek Krejci44ceedc2018-10-02 15:54:31 +0200189 ctx.ctx = NULL;
190 ctx.line = 1;
191
Radek Krejciefd22f62018-09-27 11:47:58 +0200192 /* missing argument */
193 str = ";";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200194 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_MAYBE_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200195 assert_null(word);
196
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200197 str = "{";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200198 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200199 logbuf_assert("Invalid character sequence \"{\", expected an argument. Line number 1.");
200
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200201 /* invalid escape sequence */
202 str = "\"\\s\"";
203 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200204 logbuf_assert("Double-quoted string unknown special character \'\\s\'. Line number 1.");
205 str = "\'\\s\'"; /* valid, since it is not an escape sequence in single quoted string */
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200206 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200207 assert_int_equal(2, len);
208 assert_string_equal("\\s\'", word);
209 assert_int_equal('\0', str[0]); /* input has been eaten */
210
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200211 /* invalid character after the argument */
212 str = "hello\"";
213 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
214 logbuf_assert("Invalid character sequence \"\"\", expected unquoted string character, optsep, semicolon or opening brace. Line number 1.");
215 str = "hello}";
216 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
217 logbuf_assert("Invalid character sequence \"}\", expected unquoted string character, optsep, semicolon or opening brace. Line number 1.");
218
219 str = "hello/x\t"; /* slash is not an invalid character */
220 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
221 assert_int_equal(7, len);
222 assert_string_equal("hello/x\t", word);
223
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200224 assert_null(buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200225
226 /* different quoting */
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200227 str = "hello ";
228 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200229 assert_null(buf);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200230 assert_int_equal(5, len);
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200231 assert_string_equal("hello ", word);
Radek Krejciefd22f62018-09-27 11:47:58 +0200232
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200233 str = "hello/*comment*/\n";
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);
236 assert_int_equal(5, len);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200237 assert_false(strncmp("hello", word, len));
238
239
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200240 str = "\"hello\\n\\t\\\"\\\\\";";
241 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200242 assert_null(buf);
243 assert_int_equal(9, len);
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200244 assert_string_equal("hello\\n\\t\\\"\\\\\";", word);
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200245
246 ctx.indent = 14;
247 str = "\"hello \t\n\t\t world!\"";
248 /* - space and tabs before newline are stripped out
249 * - space and tabs after newline (indentation) are stripped out
250 */
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200251 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200252 assert_non_null(buf);
253 assert_ptr_equal(word, buf);
254 assert_int_equal(14, len);
255 assert_string_equal("hello\n world!", word);
256 free(buf);
257
258 ctx.indent = 14;
259 str = "\"hello\n \tworld!\"";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200260 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejcid5f2b5f2018-10-11 10:54:36 +0200261 assert_non_null(buf);
262 assert_ptr_equal(word, buf);
263 assert_int_equal(12, len);
264 assert_string_equal("hello\nworld!", word);
265 free(buf);
Radek Krejciefd22f62018-09-27 11:47:58 +0200266
267 str = "\'hello\'";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200268 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200269 assert_null(buf);
270 assert_int_equal(5, len);
271 assert_false(strncmp("hello", word, 5));
272
273 str = "\"hel\" +\t\n\"lo\"";
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_ptr_equal(word, buf);
276 assert_int_equal(5, len);
277 assert_string_equal("hello", word);
278 free(buf);
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200279 str = "\"hel\" +\t\nlo"; /* unquoted the second part */
280 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
281 logbuf_assert("Both string parts divided by '+' must be quoted. Line number 5.");
Radek Krejciefd22f62018-09-27 11:47:58 +0200282
283 str = "\'he\'\t\n+ \"llo\"";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200284 assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
Radek Krejciefd22f62018-09-27 11:47:58 +0200285 assert_ptr_equal(word, buf);
286 assert_int_equal(5, len);
287 assert_string_equal("hello", word);
288 free(buf);
289
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200290 str = " \t\n\"he\"+\'llo\'";
291 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 Krejci44ceedc2018-10-02 15:54:31 +0200297 /* missing argument */
298 str = ";";
Radek Krejcifc62d7e2018-10-11 12:56:42 +0200299 assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len));
300 logbuf_assert("Invalid character sequence \";\", expected an argument. Line number 7.");
Radek Krejcidcc7b322018-10-11 14:24:02 +0200301}
302
303static void
304test_stmts(void **state)
305{
306 (void) state; /* unused */
307
308 struct ly_parser_ctx ctx;
309 const char *str, *p;
310 enum yang_keyword kw;
311 char *word;
312 size_t len;
313
314 ctx.ctx = NULL;
315 ctx.line = 1;
316
317 str = "\n// comment\n\tinput\t{";
318 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
319 assert_int_equal(YANG_INPUT, kw);
320 assert_int_equal(5, len);
321 assert_string_equal("input\t{", word);
322 assert_string_equal("\t{", str);
323
324 str = "\t /* comment */\t output\n\t{";
325 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
326 assert_int_equal(YANG_OUTPUT, kw);
327 assert_int_equal(6, len);
328 assert_string_equal("output\n\t{", word);
329 assert_string_equal("\n\t{", str);
330
331 str = "/input { "; /* invalid slash */
332 assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len));
333 logbuf_assert("Invalid identifier first character '/'. Line number 4.");
334
335 str = "not-a-statement-nor-extension { "; /* invalid identifier */
336 assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len));
337 logbuf_assert("Invalid character sequence \"not-a-statement-nor-extension\", expected a keyword. Line number 4.");
338
339 str = "path;"; /* missing sep after the keyword */
340 assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len));
341 logbuf_assert("Invalid character sequence \"path;\", expected a keyword followed by a separator. Line number 4.");
342
343 str = "action ";
344 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
345 assert_int_equal(YANG_ACTION, kw);
346 assert_int_equal(6, len);
347 str = "anydata ";
348 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
349 assert_int_equal(YANG_ANYDATA, kw);
350 assert_int_equal(7, len);
351 str = "anyxml ";
352 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
353 assert_int_equal(YANG_ANYXML, kw);
354 assert_int_equal(6, len);
355 str = "argument ";
356 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
357 assert_int_equal(YANG_ARGUMENT, kw);
358 assert_int_equal(8, len);
359 str = "augment ";
360 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
361 assert_int_equal(YANG_AUGMENT, kw);
362 assert_int_equal(7, len);
363 str = "base ";
364 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
365 assert_int_equal(YANG_BASE, kw);
366 assert_int_equal(4, len);
367 str = "belongs-to ";
368 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
369 assert_int_equal(YANG_BELONGS_TO, kw);
370 assert_int_equal(10, len);
371 str = "bit ";
372 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
373 assert_int_equal(YANG_BIT, kw);
374 assert_int_equal(3, len);
375 str = "case ";
376 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
377 assert_int_equal(YANG_CASE, kw);
378 assert_int_equal(4, len);
379 str = "choice ";
380 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
381 assert_int_equal(YANG_CHOICE, kw);
382 assert_int_equal(6, len);
383 str = "config ";
384 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
385 assert_int_equal(YANG_CONFIG, kw);
386 assert_int_equal(6, len);
387 str = "contact ";
388 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
389 assert_int_equal(YANG_CONTACT, kw);
390 assert_int_equal(7, len);
391 str = "container ";
392 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
393 assert_int_equal(YANG_CONTAINER, kw);
394 assert_int_equal(9, len);
395 str = "default ";
396 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
397 assert_int_equal(YANG_DEFAULT, kw);
398 assert_int_equal(7, len);
399 str = "description ";
400 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
401 assert_int_equal(YANG_DESCRIPTION, kw);
402 assert_int_equal(11, len);
403 str = "deviate ";
404 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
405 assert_int_equal(YANG_DEVIATE, kw);
406 assert_int_equal(7, len);
407 str = "deviation ";
408 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
409 assert_int_equal(YANG_DEVIATION, kw);
410 assert_int_equal(9, len);
411 str = "enum ";
412 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
413 assert_int_equal(YANG_ENUM, kw);
414 assert_int_equal(4, len);
415 str = "error-app-tag ";
416 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
417 assert_int_equal(YANG_ERROR_APP_TAG, kw);
418 assert_int_equal(13, len);
419 str = "error-message ";
420 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
421 assert_int_equal(YANG_ERROR_MESSAGE, kw);
422 assert_int_equal(13, len);
423 str = "extension ";
424 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
425 assert_int_equal(YANG_EXTENSION, kw);
426 assert_int_equal(9, len);
427 str = "feature ";
428 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
429 assert_int_equal(YANG_FEATURE, kw);
430 assert_int_equal(7, len);
431 str = "fraction-digits ";
432 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
433 assert_int_equal(YANG_FRACTION_DIGITS, kw);
434 assert_int_equal(15, len);
435 str = "grouping ";
436 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
437 assert_int_equal(YANG_GROUPING, kw);
438 assert_int_equal(8, len);
439 str = "identity ";
440 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
441 assert_int_equal(YANG_IDENTITY, kw);
442 assert_int_equal(8, len);
443 str = "if-feature ";
444 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
445 assert_int_equal(YANG_IF_FEATURE, kw);
446 assert_int_equal(10, len);
447 str = "import ";
448 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
449 assert_int_equal(YANG_IMPORT, kw);
450 assert_int_equal(6, len);
451 str = "include ";
452 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
453 assert_int_equal(YANG_INCLUDE, kw);
454 assert_int_equal(7, len);
455 str = "input{";
456 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
457 assert_int_equal(YANG_INPUT, kw);
458 assert_int_equal(5, len);
459 str = "key ";
460 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
461 assert_int_equal(YANG_KEY, kw);
462 assert_int_equal(3, len);
463 str = "leaf ";
464 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
465 assert_int_equal(YANG_LEAF, kw);
466 assert_int_equal(4, len);
467 str = "leaf-list ";
468 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
469 assert_int_equal(YANG_LEAF_LIST, kw);
470 assert_int_equal(9, len);
471 str = "length ";
472 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
473 assert_int_equal(YANG_LENGTH, kw);
474 assert_int_equal(6, len);
475 str = "list ";
476 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
477 assert_int_equal(YANG_LIST, kw);
478 assert_int_equal(4, len);
479 str = "mandatory ";
480 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
481 assert_int_equal(YANG_MANDATORY, kw);
482 assert_int_equal(9, len);
483 str = "max-elements ";
484 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
485 assert_int_equal(YANG_MAX_ELEMENTS, kw);
486 assert_int_equal(12, len);
487 str = "min-elements ";
488 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
489 assert_int_equal(YANG_MIN_ELEMENTS, kw);
490 assert_int_equal(12, len);
491 str = "modifier ";
492 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
493 assert_int_equal(YANG_MODIFIER, kw);
494 assert_int_equal(8, len);
495 str = "module ";
496 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
497 assert_int_equal(YANG_MODULE, kw);
498 assert_int_equal(6, len);
499 str = "must ";
500 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
501 assert_int_equal(YANG_MUST, kw);
502 assert_int_equal(4, len);
503 str = "namespace ";
504 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
505 assert_int_equal(YANG_NAMESPACE, kw);
506 assert_int_equal(9, len);
507 str = "notification ";
508 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
509 assert_int_equal(YANG_NOTIFICATION, kw);
510 assert_int_equal(12, len);
511 str = "ordered-by ";
512 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
513 assert_int_equal(YANG_ORDERED_BY, kw);
514 assert_int_equal(10, len);
515 str = "organization ";
516 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
517 assert_int_equal(YANG_ORGANIZATION, kw);
518 assert_int_equal(12, len);
519 str = "output ";
520 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
521 assert_int_equal(YANG_OUTPUT, kw);
522 assert_int_equal(6, len);
523 str = "path ";
524 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
525 assert_int_equal(YANG_PATH, kw);
526 assert_int_equal(4, len);
527 str = "pattern ";
528 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
529 assert_int_equal(YANG_PATTERN, kw);
530 assert_int_equal(7, len);
531 str = "position ";
532 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
533 assert_int_equal(YANG_POSITION, kw);
534 assert_int_equal(8, len);
535 str = "prefix ";
536 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
537 assert_int_equal(YANG_PREFIX, kw);
538 assert_int_equal(6, len);
539 str = "presence ";
540 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
541 assert_int_equal(YANG_PRESENCE, kw);
542 assert_int_equal(8, len);
543 str = "range ";
544 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
545 assert_int_equal(YANG_RANGE, kw);
546 assert_int_equal(5, len);
547 str = "reference ";
548 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
549 assert_int_equal(YANG_REFERENCE, kw);
550 assert_int_equal(9, len);
551 str = "refine ";
552 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
553 assert_int_equal(YANG_REFINE, kw);
554 assert_int_equal(6, len);
555 str = "require-instance ";
556 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
557 assert_int_equal(YANG_REQUIRE_INSTANCE, kw);
558 assert_int_equal(16, len);
559 str = "revision ";
560 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
561 assert_int_equal(YANG_REVISION, kw);
562 assert_int_equal(8, len);
563 str = "revision-date ";
564 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
565 assert_int_equal(YANG_REVISION_DATE, kw);
566 assert_int_equal(13, len);
567 str = "rpc ";
568 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
569 assert_int_equal(YANG_RPC, kw);
570 assert_int_equal(3, len);
571 str = "status ";
572 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
573 assert_int_equal(YANG_STATUS, kw);
574 assert_int_equal(6, len);
575 str = "submodule ";
576 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
577 assert_int_equal(YANG_SUBMODULE, kw);
578 assert_int_equal(9, len);
579 str = "type ";
580 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
581 assert_int_equal(YANG_TYPE, kw);
582 assert_int_equal(4, len);
583 str = "typedef ";
584 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
585 assert_int_equal(YANG_TYPEDEF, kw);
586 assert_int_equal(7, len);
587 str = "unique ";
588 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
589 assert_int_equal(YANG_UNIQUE, kw);
590 assert_int_equal(6, len);
591 str = "units ";
592 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
593 assert_int_equal(YANG_UNITS, kw);
594 assert_int_equal(5, len);
595 str = "uses ";
596 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
597 assert_int_equal(YANG_USES, kw);
598 assert_int_equal(4, len);
599 str = "value ";
600 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
601 assert_int_equal(YANG_VALUE, kw);
602 assert_int_equal(5, len);
603 str = "when ";
604 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
605 assert_int_equal(YANG_WHEN, kw);
606 assert_int_equal(4, len);
607 str = "yang-version ";
608 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
609 assert_int_equal(YANG_YANG_VERSION, kw);
610 assert_int_equal(12, len);
611 str = "yin-element ";
612 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
613 assert_int_equal(YANG_YIN_ELEMENT, kw);
614 assert_int_equal(11, len);
Radek Krejci626df482018-10-11 15:06:31 +0200615 str = ";config false;";
616 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
617 assert_int_equal(YANG_SEMICOLON, kw);
618 assert_int_equal(1, len);
619 assert_string_equal("config false;", str);
620 str = "{ config false;";
621 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
622 assert_int_equal(YANG_LEFT_BRACE, kw);
623 assert_int_equal(1, len);
624 assert_string_equal(" config false;", str);
625 str = "}";
626 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
627 assert_int_equal(YANG_RIGHT_BRACE, kw);
628 assert_int_equal(1, len);
629 assert_string_equal("", str);
Radek Krejcidcc7b322018-10-11 14:24:02 +0200630
631 /* geenric extension */
632 str = p = "nacm:default-deny-write;";
633 assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len));
634 assert_int_equal(YANG_CUSTOM, kw);
635 assert_int_equal(23, len);
636 assert_ptr_equal(p, word);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200637}
Radek Krejci44ceedc2018-10-02 15:54:31 +0200638
Radek Krejci9fcacc12018-10-11 15:59:11 +0200639static struct lysp_module *
Radek Krejci09306362018-10-15 15:26:01 +0200640mod_renew(struct ly_parser_ctx *ctx, struct lysp_module *mod, uint8_t submodule)
Radek Krejci9fcacc12018-10-11 15:59:11 +0200641{
642 lysp_module_free(mod);
643 mod = calloc(1, sizeof *mod);
644 mod->ctx = ctx->ctx;
Radek Krejci09306362018-10-15 15:26:01 +0200645 mod->submodule = submodule;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200646 assert_non_null(mod);
647 return mod;
648}
649
650static void
651test_module(void **state)
652{
653 (void) state; /* unused */
654
655 struct ly_parser_ctx ctx;
656 struct lysp_module *mod;
657 const char *str;
658
659 assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx));
660 assert_non_null(ctx.ctx);
661 ctx.line = 1;
Radek Krejcia042ea12018-10-13 07:52:15 +0200662 ctx.indent = 0;
Radek Krejci9fcacc12018-10-11 15:59:11 +0200663
Radek Krejci09306362018-10-15 15:26:01 +0200664 mod = mod_renew(&ctx, NULL, 0);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200665
666 /* missing mandatory substatements */
667 str = " name {}";
668 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
669 assert_string_equal("name", mod->name);
670 logbuf_assert("Missing mandatory keyword \"namespace\" as a child of \"module\". Line number 1.");
Radek Krejci09306362018-10-15 15:26:01 +0200671 mod = mod_renew(&ctx, mod, 0);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200672
673 str = " name {namespace urn:x;}";
674 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
675 assert_string_equal("urn:x", mod->ns);
676 logbuf_assert("Missing mandatory keyword \"prefix\" 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;prefix \"x\";}";
680 assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod));
681 assert_string_equal("x", mod->prefix);
Radek Krejci09306362018-10-15 15:26:01 +0200682 mod = mod_renew(&ctx, mod, 0);
Radek Krejci9fcacc12018-10-11 15:59:11 +0200683
Radek Krejcia042ea12018-10-13 07:52:15 +0200684#define SCHEMA_BEGINNING " name {namespace urn:x;prefix \"x\";"
685#define TEST_NODE(NODETYPE, INPUT, NAME) \
686 str = SCHEMA_BEGINNING INPUT; \
687 assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod)); \
688 assert_non_null(mod->data); \
689 assert_int_equal(NODETYPE, mod->data->nodetype); \
690 assert_string_equal(NAME, mod->data->name); \
Radek Krejci09306362018-10-15 15:26:01 +0200691 mod = mod_renew(&ctx, mod, 0);
Radek Krejcia042ea12018-10-13 07:52:15 +0200692#define TEST_GENERIC(INPUT, TARGET, TEST) \
693 str = SCHEMA_BEGINNING INPUT; \
694 assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod)); \
695 assert_non_null(TARGET); \
696 TEST; \
Radek Krejci09306362018-10-15 15:26:01 +0200697 mod = mod_renew(&ctx, mod, 0);
Radek Krejcia042ea12018-10-13 07:52:15 +0200698
699 /* duplicated namespace, prefix */
700 str = SCHEMA_BEGINNING "namespace y;}";
701 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
702 logbuf_assert("Duplicate keyword \"namespace\". Line number 1.");
Radek Krejci09306362018-10-15 15:26:01 +0200703 mod = mod_renew(&ctx, mod, 0);
Radek Krejcia042ea12018-10-13 07:52:15 +0200704 str = SCHEMA_BEGINNING "prefix y;}";
705 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
706 logbuf_assert("Duplicate keyword \"prefix\". Line number 1.");
Radek Krejci09306362018-10-15 15:26:01 +0200707 mod = mod_renew(&ctx, mod, 0);
Radek Krejcia042ea12018-10-13 07:52:15 +0200708
Radek Krejci70853c52018-10-15 14:46:16 +0200709 /* not allowed in module (submodule-specific) */
710 str = SCHEMA_BEGINNING "belongs-to master {prefix m;}}";
711 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
712 logbuf_assert("Invalid keyword \"belongs-to\" as a child of \"module\". Line number 1.");
Radek Krejci09306362018-10-15 15:26:01 +0200713 mod = mod_renew(&ctx, mod, 0);
Radek Krejci70853c52018-10-15 14:46:16 +0200714
Radek Krejcia042ea12018-10-13 07:52:15 +0200715 /* anydata */
716 TEST_NODE(LYS_ANYDATA, "anydata test;}", "test");
717 /* anyxml */
718 TEST_NODE(LYS_ANYXML, "anyxml test;}", "test");
719 /* augment */
720 TEST_GENERIC("augment /somepath;}", mod->augments,
721 assert_string_equal("/somepath", mod->augments[0].nodeid));
722 /* choice */
723 TEST_NODE(LYS_CHOICE, "choice test;}", "test");
724 /* contact 0..1 */
725 TEST_GENERIC("contact \"firstname\" + \n\t\" surname\";}", mod->contact,
726 assert_string_equal("firstname surname", mod->contact));
727 /* container */
728 TEST_NODE(LYS_CONTAINER, "container test;}", "test");
729 /* description 0..1 */
730 TEST_GENERIC("description \'some description\';}", mod->dsc,
731 assert_string_equal("some description", mod->dsc));
732 /* deviation */
733 TEST_GENERIC("deviation /somepath {deviate not-supported;}}", mod->deviations,
734 assert_string_equal("/somepath", mod->deviations[0].nodeid));
735 /* extension */
736 TEST_GENERIC("extension test;}", mod->extensions,
737 assert_string_equal("test", mod->extensions[0].name));
738 /* feature */
739 TEST_GENERIC("feature test;}", mod->features,
740 assert_string_equal("test", mod->features[0].name));
741 /* grouping */
742 TEST_GENERIC("grouping grp;}", mod->groupings,
743 assert_string_equal("grp", mod->groupings[0].name));
744 /* identity */
745 TEST_GENERIC("identity test;}", mod->identities,
746 assert_string_equal("test", mod->identities[0].name));
747 /* import */
748 TEST_GENERIC("import test {prefix z;}}", mod->imports,
749 assert_string_equal("test", mod->imports[0].name));
Radek Krejci70853c52018-10-15 14:46:16 +0200750
Radek Krejcia042ea12018-10-13 07:52:15 +0200751 /* import - prefix collision */
752 str = SCHEMA_BEGINNING "import test {prefix x;}}";
753 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
Radek Krejci70853c52018-10-15 14:46:16 +0200754 logbuf_assert("Prefix \"x\" already used as module prefix. Line number 2.");
Radek Krejci09306362018-10-15 15:26:01 +0200755 mod = mod_renew(&ctx, mod, 0);
Radek Krejci70853c52018-10-15 14:46:16 +0200756
Radek Krejcia042ea12018-10-13 07:52:15 +0200757 /* include */
758 TEST_GENERIC("rpc test;}", mod->rpcs,
759 assert_string_equal("test", mod->rpcs[0].name));
760 /* leaf */
761 TEST_NODE(LYS_LEAF, "leaf test {type string;}}", "test");
762 /* leaf-list */
763 TEST_NODE(LYS_LEAFLIST, "leaf-list test {type string;}}", "test");
764 /* list */
765 TEST_NODE(LYS_LIST, "list test {key a;leaf a {type string;}}}", "test");
766 /* notification */
767 TEST_GENERIC("notification test;}", mod->notifs,
768 assert_string_equal("test", mod->notifs[0].name));
769 /* organization 0..1 */
770 TEST_GENERIC("organization \"CESNET a.l.e.\";}", mod->org,
771 assert_string_equal("CESNET a.l.e.", mod->org));
772 /* reference 0..1 */
773 TEST_GENERIC("reference RFC7950;}", mod->ref,
774 assert_string_equal("RFC7950", mod->ref));
775 /* revision */
776 TEST_GENERIC("revision 2018-10-12;}", mod->revs,
777 assert_string_equal("2018-10-12", mod->revs[0].rev));
778 /* rpc */
779 TEST_GENERIC("rpc test;}", mod->rpcs,
780 assert_string_equal("test", mod->rpcs[0].name));
781 /* typedef */
782 TEST_GENERIC("typedef test{type string;}}", mod->typedefs,
783 assert_string_equal("test", mod->typedefs[0].name));
784 /* uses */
785 TEST_NODE(LYS_USES, "uses test;}", "test");
786 /* yang-version */
787 str = SCHEMA_BEGINNING "\n\tyang-version 10;}";
788 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
789 logbuf_assert("Invalid value \"10\" of \"yang-version\". Line number 3.");
Radek Krejci09306362018-10-15 15:26:01 +0200790 mod = mod_renew(&ctx, mod, 0);
Radek Krejcia042ea12018-10-13 07:52:15 +0200791 str = SCHEMA_BEGINNING "yang-version 1.0;yang-version 1.1;}";
792 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
793 logbuf_assert("Duplicate keyword \"yang-version\". Line number 3.");
Radek Krejci09306362018-10-15 15:26:01 +0200794 mod = mod_renew(&ctx, mod, 0);
Radek Krejcia042ea12018-10-13 07:52:15 +0200795 str = SCHEMA_BEGINNING "yang-version 1.0;}";
796 assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod));
797 assert_int_equal(1, mod->version);
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.1\";}";
800 assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod));
801 assert_int_equal(2, mod->version);
Radek Krejci09306362018-10-15 15:26:01 +0200802 mod = mod_renew(&ctx, mod, 0);
803
804 /* submodule */
805 mod->submodule = 1;
806
807 /* missing mandatory substatements */
808 str = " subname {}";
809 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
810 assert_string_equal("subname", mod->name);
811 logbuf_assert("Missing mandatory keyword \"belongs-to\" as a child of \"submodule\". Line number 3.");
812 mod = mod_renew(&ctx, mod, 1);
813
814 str = " subname {belongs-to name;}";
815 assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod));
816 assert_string_equal("name", mod->belongsto);
817 mod = mod_renew(&ctx, mod, 1);
818
819#undef SCHEMA_BEGINNING
820#define SCHEMA_BEGINNING " subname {belongs-to name;"
821
822 /* duplicated namespace, prefix */
823 str = SCHEMA_BEGINNING "belongs-to othermodule;}";
824 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
825 logbuf_assert("Duplicate keyword \"belongs-to\". Line number 3.");
826 mod = mod_renew(&ctx, mod, 1);
827
828 /* not allowed in submodule (module-specific) */
829 str = SCHEMA_BEGINNING "namespace \"urn:z\";}";
830 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
831 logbuf_assert("Invalid keyword \"namespace\" as a child of \"submodule\". Line number 3.");
832 mod = mod_renew(&ctx, mod, 1);
833 str = SCHEMA_BEGINNING "prefix m;}}";
834 assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
835 logbuf_assert("Invalid keyword \"prefix\" as a child of \"submodule\". Line number 3.");
836 mod = mod_renew(&ctx, mod, 1);
Radek Krejcia042ea12018-10-13 07:52:15 +0200837
838#undef TEST_GENERIC
839#undef TEST_NODE
840#undef SCHEMA_BEGINNING
841
Radek Krejci9fcacc12018-10-11 15:59:11 +0200842 lysp_module_free(mod);
843 ly_ctx_destroy(ctx.ctx, NULL);
Radek Krejciefd22f62018-09-27 11:47:58 +0200844}
845
Radek Krejci80dd33e2018-09-26 15:57:18 +0200846int main(void)
847{
848 const struct CMUnitTest tests[] = {
Radek Krejci44ceedc2018-10-02 15:54:31 +0200849 cmocka_unit_test_setup(test_helpers, logger_setup),
Radek Krejci80dd33e2018-09-26 15:57:18 +0200850 cmocka_unit_test_setup(test_comments, logger_setup),
Radek Krejciefd22f62018-09-27 11:47:58 +0200851 cmocka_unit_test_setup(test_arg, logger_setup),
Radek Krejcidcc7b322018-10-11 14:24:02 +0200852 cmocka_unit_test_setup(test_stmts, logger_setup),
Radek Krejci9fcacc12018-10-11 15:59:11 +0200853 cmocka_unit_test_setup(test_module, logger_setup),
Radek Krejci80dd33e2018-09-26 15:57:18 +0200854 };
855
856 return cmocka_run_group_tests(tests, NULL, NULL);
857}