Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 1 | /* |
| 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 Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 15 | #include "../../src/tree_schema.c" |
| 16 | #include "../../src/parser_yang.c" |
| 17 | |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 18 | #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 Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 27 | |
| 28 | #define BUFSIZE 1024 |
| 29 | char logbuf[BUFSIZE] = {0}; |
| 30 | |
| 31 | /* set to 0 to printing error messages to stderr instead of checking them in code */ |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 32 | #define ENABLE_LOGGER_CHECKING 1 |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 33 | |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 34 | #if ENABLE_LOGGER_CHECKING |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 35 | static void |
| 36 | logger(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 Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 46 | #endif |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 47 | |
| 48 | static int |
| 49 | logger_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 | |
| 58 | void |
| 59 | logbuf_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 Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 70 | #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 Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 76 | static void |
| 77 | test_helpers(void **state) |
| 78 | { |
| 79 | (void) state; /* unused */ |
| 80 | |
| 81 | const char *str; |
Radek Krejci | 404251e | 2018-10-09 12:06:44 +0200 | [diff] [blame] | 82 | char *buf, *p; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 83 | 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 Krejci | 404251e | 2018-10-09 12:06:44 +0200 | [diff] [blame] | 99 | 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 Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 129 | |
| 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 Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 140 | assert_int_equal(LY_EVALID, check_identifierchar(&ctx, ':', 0, &prefix)); |
| 141 | assert_int_equal(1, prefix); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 142 | assert_int_equal(LY_SUCCESS, check_identifierchar(&ctx, 'b', 0, &prefix)); |
| 143 | assert_int_equal(2, prefix); |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 144 | /* 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 Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 147 | } |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 148 | |
| 149 | static void |
| 150 | test_comments(void **state) |
| 151 | { |
| 152 | (void) state; /* unused */ |
| 153 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 154 | struct ly_parser_ctx ctx; |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 155 | const char *str, *p; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 156 | char *word, *buf; |
| 157 | size_t len; |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 158 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 159 | ctx.ctx = NULL; |
| 160 | ctx.line = 1; |
| 161 | |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 162 | str = " // this is a text of / one * line */ comment\nargument"; |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 163 | assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len)); |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 164 | assert_string_equal("argument", word); |
| 165 | assert_null(buf); |
| 166 | assert_int_equal(8, len); |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 167 | |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 168 | str = "/* this is a \n * text // of / block * comment */\"arg\" + \"ume\" \n + \n \"nt\""; |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 169 | assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len)); |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 170 | assert_string_equal("argument", word); |
| 171 | assert_ptr_equal(buf, word); |
| 172 | assert_int_equal(8, len); |
| 173 | free(word); |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 174 | |
| 175 | str = p = " this is one line comment on last line"; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 176 | assert_int_equal(LY_SUCCESS, skip_comment(&ctx, &str, 1)); |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 177 | assert_true(str[0] == '\0'); |
| 178 | |
| 179 | str = p = " this is a not terminated comment x"; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 180 | assert_int_equal(LY_EVALID, skip_comment(&ctx, &str, 2)); |
| 181 | logbuf_assert("Unexpected end-of-file, non-terminated comment. Line number 5."); |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 182 | assert_true(str[0] == '\0'); |
| 183 | } |
| 184 | |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 185 | static void |
| 186 | test_arg(void **state) |
| 187 | { |
| 188 | (void) state; /* unused */ |
| 189 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 190 | struct ly_parser_ctx ctx; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 191 | const char *str; |
| 192 | char *word, *buf; |
| 193 | size_t len; |
| 194 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 195 | ctx.ctx = NULL; |
| 196 | ctx.line = 1; |
| 197 | |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 198 | /* missing argument */ |
| 199 | str = ";"; |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 200 | assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_MAYBE_STR_ARG, &word, &buf, &len)); |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 201 | assert_null(word); |
| 202 | |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 203 | str = "{"; |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 204 | assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len)); |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 205 | logbuf_assert("Invalid character sequence \"{\", expected an argument. Line number 1."); |
| 206 | |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 207 | /* invalid escape sequence */ |
| 208 | str = "\"\\s\""; |
| 209 | assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len)); |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 210 | 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 Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 212 | assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len)); |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 213 | assert_int_equal(2, len); |
| 214 | assert_string_equal("\\s\'", word); |
| 215 | assert_int_equal('\0', str[0]); /* input has been eaten */ |
| 216 | |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 217 | /* 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 Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 230 | assert_null(buf); |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 231 | |
| 232 | /* different quoting */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 233 | str = "hello "; |
| 234 | assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len)); |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 235 | assert_null(buf); |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 236 | assert_int_equal(5, len); |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 237 | assert_string_equal("hello ", word); |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 238 | |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 239 | str = "hello/*comment*/\n"; |
| 240 | assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len)); |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 241 | assert_null(buf); |
| 242 | assert_int_equal(5, len); |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 243 | assert_false(strncmp("hello", word, len)); |
| 244 | |
| 245 | |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 246 | str = "\"hello\\n\\t\\\"\\\\\";"; |
| 247 | assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len)); |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 248 | assert_null(buf); |
| 249 | assert_int_equal(9, len); |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 250 | assert_string_equal("hello\\n\\t\\\"\\\\\";", word); |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 251 | |
| 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 Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 257 | assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len)); |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 258 | 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 Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 266 | assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len)); |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 267 | 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 Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 272 | |
| 273 | str = "\'hello\'"; |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 274 | assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len)); |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 275 | assert_null(buf); |
| 276 | assert_int_equal(5, len); |
| 277 | assert_false(strncmp("hello", word, 5)); |
| 278 | |
| 279 | str = "\"hel\" +\t\n\"lo\""; |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 280 | assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len)); |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 281 | assert_ptr_equal(word, buf); |
| 282 | assert_int_equal(5, len); |
| 283 | assert_string_equal("hello", word); |
| 284 | free(buf); |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 285 | 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 Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 288 | |
| 289 | str = "\'he\'\t\n+ \"llo\""; |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 290 | assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len)); |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 291 | assert_ptr_equal(word, buf); |
| 292 | assert_int_equal(5, len); |
| 293 | assert_string_equal("hello", word); |
| 294 | free(buf); |
| 295 | |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 296 | str = " \t\n\"he\"+\'llo\'"; |
| 297 | assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len)); |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 298 | assert_ptr_equal(word, buf); |
| 299 | assert_int_equal(5, len); |
| 300 | assert_string_equal("hello", word); |
| 301 | free(buf); |
| 302 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 303 | /* missing argument */ |
| 304 | str = ";"; |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 305 | 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 Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | static void |
| 310 | test_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 Krejci | 626df48 | 2018-10-11 15:06:31 +0200 | [diff] [blame] | 621 | 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 Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 636 | |
| 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 Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 643 | } |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 644 | |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 645 | static struct lysp_module * |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 646 | mod_renew(struct ly_parser_ctx *ctx, struct lysp_module *mod, uint8_t submodule) |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 647 | { |
| 648 | lysp_module_free(mod); |
| 649 | mod = calloc(1, sizeof *mod); |
| 650 | mod->ctx = ctx->ctx; |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 651 | mod->submodule = submodule; |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 652 | assert_non_null(mod); |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 653 | ctx->mod = mod; |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 654 | return mod; |
| 655 | } |
| 656 | |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 657 | static LY_ERR test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name), |
| 658 | const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format, |
| 659 | const char **module_data, void (**free_module_data)(void *model_data, void *user_data)) |
| 660 | { |
| 661 | *module_data = user_data; |
| 662 | *format = LYS_IN_YANG; |
| 663 | *free_module_data = NULL; |
| 664 | return LY_SUCCESS; |
| 665 | } |
| 666 | |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 667 | static void |
| 668 | test_module(void **state) |
| 669 | { |
| 670 | (void) state; /* unused */ |
| 671 | |
| 672 | struct ly_parser_ctx ctx; |
| 673 | struct lysp_module *mod; |
| 674 | const char *str; |
| 675 | |
| 676 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 677 | assert_non_null(ctx.ctx); |
| 678 | ctx.line = 1; |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 679 | ctx.indent = 0; |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 680 | |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 681 | mod = mod_renew(&ctx, NULL, 0); |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 682 | |
| 683 | /* missing mandatory substatements */ |
| 684 | str = " name {}"; |
| 685 | assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod)); |
| 686 | assert_string_equal("name", mod->name); |
| 687 | logbuf_assert("Missing mandatory keyword \"namespace\" as a child of \"module\". Line number 1."); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 688 | mod = mod_renew(&ctx, mod, 0); |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 689 | |
| 690 | str = " name {namespace urn:x;}"; |
| 691 | assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod)); |
| 692 | assert_string_equal("urn:x", mod->ns); |
| 693 | logbuf_assert("Missing mandatory keyword \"prefix\" as a child of \"module\". Line number 1."); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 694 | mod = mod_renew(&ctx, mod, 0); |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 695 | |
| 696 | str = " name {namespace urn:x;prefix \"x\";}"; |
| 697 | assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod)); |
| 698 | assert_string_equal("x", mod->prefix); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 699 | mod = mod_renew(&ctx, mod, 0); |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 700 | |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 701 | #define SCHEMA_BEGINNING " name {namespace urn:x;prefix \"x\";" |
| 702 | #define TEST_NODE(NODETYPE, INPUT, NAME) \ |
| 703 | str = SCHEMA_BEGINNING INPUT; \ |
| 704 | assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod)); \ |
| 705 | assert_non_null(mod->data); \ |
| 706 | assert_int_equal(NODETYPE, mod->data->nodetype); \ |
| 707 | assert_string_equal(NAME, mod->data->name); \ |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 708 | mod = mod_renew(&ctx, mod, 0); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 709 | #define TEST_GENERIC(INPUT, TARGET, TEST) \ |
| 710 | str = SCHEMA_BEGINNING INPUT; \ |
| 711 | assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod)); \ |
| 712 | assert_non_null(TARGET); \ |
| 713 | TEST; \ |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 714 | mod = mod_renew(&ctx, mod, 0); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 715 | #define TEST_DUP(MEMBER, VALUE1, VALUE2, LINE, SUBMODULE) \ |
| 716 | TEST_DUP_GENERIC(SCHEMA_BEGINNING, MEMBER, VALUE1, VALUE2, \ |
| 717 | parse_sub_module, mod, LINE, mod = mod_renew(&ctx, mod, SUBMODULE)) |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 718 | |
| 719 | /* duplicated namespace, prefix */ |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 720 | TEST_DUP("namespace", "y", "z", "1", 0); |
| 721 | TEST_DUP("prefix", "y", "z", "1", 0); |
| 722 | TEST_DUP("contact", "a", "b", "1", 0); |
| 723 | TEST_DUP("description", "a", "b", "1", 0); |
| 724 | TEST_DUP("organization", "a", "b", "1", 0); |
| 725 | TEST_DUP("reference", "a", "b", "1", 0); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 726 | |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 727 | /* not allowed in module (submodule-specific) */ |
| 728 | str = SCHEMA_BEGINNING "belongs-to master {prefix m;}}"; |
| 729 | assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod)); |
| 730 | logbuf_assert("Invalid keyword \"belongs-to\" as a child of \"module\". Line number 1."); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 731 | mod = mod_renew(&ctx, mod, 0); |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 732 | |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 733 | /* anydata */ |
| 734 | TEST_NODE(LYS_ANYDATA, "anydata test;}", "test"); |
| 735 | /* anyxml */ |
| 736 | TEST_NODE(LYS_ANYXML, "anyxml test;}", "test"); |
| 737 | /* augment */ |
| 738 | TEST_GENERIC("augment /somepath;}", mod->augments, |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 739 | assert_string_equal("/somepath", mod->augments[0].nodeid)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 740 | /* choice */ |
| 741 | TEST_NODE(LYS_CHOICE, "choice test;}", "test"); |
| 742 | /* contact 0..1 */ |
| 743 | TEST_GENERIC("contact \"firstname\" + \n\t\" surname\";}", mod->contact, |
| 744 | assert_string_equal("firstname surname", mod->contact)); |
| 745 | /* container */ |
| 746 | TEST_NODE(LYS_CONTAINER, "container test;}", "test"); |
| 747 | /* description 0..1 */ |
| 748 | TEST_GENERIC("description \'some description\';}", mod->dsc, |
| 749 | assert_string_equal("some description", mod->dsc)); |
| 750 | /* deviation */ |
| 751 | TEST_GENERIC("deviation /somepath {deviate not-supported;}}", mod->deviations, |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 752 | assert_string_equal("/somepath", mod->deviations[0].nodeid)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 753 | /* extension */ |
| 754 | TEST_GENERIC("extension test;}", mod->extensions, |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 755 | assert_string_equal("test", mod->extensions[0].name)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 756 | /* feature */ |
| 757 | TEST_GENERIC("feature test;}", mod->features, |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 758 | assert_string_equal("test", mod->features[0].name)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 759 | /* grouping */ |
| 760 | TEST_GENERIC("grouping grp;}", mod->groupings, |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 761 | assert_string_equal("grp", mod->groupings[0].name)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 762 | /* identity */ |
| 763 | TEST_GENERIC("identity test;}", mod->identities, |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 764 | assert_string_equal("test", mod->identities[0].name)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 765 | /* import */ |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 766 | ly_ctx_set_module_imp_clb(ctx.ctx, test_imp_clb, "module zzz { namespace urn:zzz; prefix z;}"); |
| 767 | TEST_GENERIC("import zzz {prefix z;}}", mod->imports, |
| 768 | assert_string_equal("zzz", mod->imports[0].name)); |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 769 | |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 770 | /* import - prefix collision */ |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 771 | str = SCHEMA_BEGINNING "import zzz {prefix x;}}"; |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 772 | assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod)); |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 773 | logbuf_assert("Prefix \"x\" already used as module prefix. Line number 2."); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 774 | mod = mod_renew(&ctx, mod, 0); |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 775 | str = SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix y;}}"; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 776 | assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod)); |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 777 | logbuf_assert("Prefix \"y\" already used to import \"zzz\" module. Line number 2."); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 778 | mod = mod_renew(&ctx, mod, 0); |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 779 | str = "module" SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix z;}}"; |
| 780 | assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG)); |
| 781 | assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx)); |
| 782 | logbuf_assert("Single revision of the module \"zzz\" referred twice."); |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 783 | |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 784 | /* include */ |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 785 | ly_ctx_set_module_imp_clb(ctx.ctx, test_imp_clb, "module xxx { namespace urn:xxx; prefix x;}"); |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 786 | str = "module" SCHEMA_BEGINNING "include xxx;}"; |
| 787 | assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG)); |
| 788 | assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx)); |
| 789 | logbuf_assert("Included \"xxx\" schema from \"name\" is actually not a submodule."); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 790 | |
| 791 | ly_ctx_set_module_imp_clb(ctx.ctx, test_imp_clb, "submodule xxx {belongs-to wrong-name;}"); |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 792 | str = "module" SCHEMA_BEGINNING "include xxx;}"; |
| 793 | assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG)); |
| 794 | assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx)); |
| 795 | logbuf_assert("Included \"xxx\" submodule from \"name\" belongs-to a different module \"wrong-name\"."); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 796 | |
| 797 | ly_ctx_set_module_imp_clb(ctx.ctx, test_imp_clb, "submodule xxx {belongs-to name;}"); |
| 798 | TEST_GENERIC("include xxx;}", mod->includes, |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 799 | assert_string_equal("xxx", mod->includes[0].name)); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 800 | |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 801 | /* leaf */ |
| 802 | TEST_NODE(LYS_LEAF, "leaf test {type string;}}", "test"); |
| 803 | /* leaf-list */ |
| 804 | TEST_NODE(LYS_LEAFLIST, "leaf-list test {type string;}}", "test"); |
| 805 | /* list */ |
| 806 | TEST_NODE(LYS_LIST, "list test {key a;leaf a {type string;}}}", "test"); |
| 807 | /* notification */ |
| 808 | TEST_GENERIC("notification test;}", mod->notifs, |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 809 | assert_string_equal("test", mod->notifs[0].name)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 810 | /* organization 0..1 */ |
| 811 | TEST_GENERIC("organization \"CESNET a.l.e.\";}", mod->org, |
| 812 | assert_string_equal("CESNET a.l.e.", mod->org)); |
| 813 | /* reference 0..1 */ |
| 814 | TEST_GENERIC("reference RFC7950;}", mod->ref, |
| 815 | assert_string_equal("RFC7950", mod->ref)); |
| 816 | /* revision */ |
| 817 | TEST_GENERIC("revision 2018-10-12;}", mod->revs, |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 818 | assert_string_equal("2018-10-12", mod->revs[0].date)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 819 | /* rpc */ |
| 820 | TEST_GENERIC("rpc test;}", mod->rpcs, |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 821 | assert_string_equal("test", mod->rpcs[0].name)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 822 | /* typedef */ |
| 823 | TEST_GENERIC("typedef test{type string;}}", mod->typedefs, |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 824 | assert_string_equal("test", mod->typedefs[0].name)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 825 | /* uses */ |
| 826 | TEST_NODE(LYS_USES, "uses test;}", "test"); |
| 827 | /* yang-version */ |
| 828 | str = SCHEMA_BEGINNING "\n\tyang-version 10;}"; |
| 829 | assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod)); |
| 830 | logbuf_assert("Invalid value \"10\" of \"yang-version\". Line number 3."); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 831 | mod = mod_renew(&ctx, mod, 0); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 832 | str = SCHEMA_BEGINNING "yang-version 1.0;yang-version 1.1;}"; |
| 833 | assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod)); |
| 834 | logbuf_assert("Duplicate keyword \"yang-version\". Line number 3."); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 835 | mod = mod_renew(&ctx, mod, 0); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 836 | str = SCHEMA_BEGINNING "yang-version 1.0;}"; |
| 837 | assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod)); |
| 838 | assert_int_equal(1, mod->version); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 839 | mod = mod_renew(&ctx, mod, 0); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 840 | str = SCHEMA_BEGINNING "yang-version \"1.1\";}"; |
| 841 | assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod)); |
| 842 | assert_int_equal(2, mod->version); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 843 | mod = mod_renew(&ctx, mod, 0); |
| 844 | |
Radek Krejci | 156ccaf | 2018-10-15 15:49:17 +0200 | [diff] [blame] | 845 | /* extensions */ |
| 846 | TEST_GENERIC("prefix:test;}", mod->exts, |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 847 | assert_string_equal("prefix:test", mod->exts[0].name); |
| 848 | assert_int_equal(LYEXT_SUBSTMT_SELF, mod->exts[0].insubstmt)); |
Radek Krejci | 156ccaf | 2018-10-15 15:49:17 +0200 | [diff] [blame] | 849 | mod = mod_renew(&ctx, mod, 0); |
| 850 | |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 851 | /* invalid substatement */ |
| 852 | str = SCHEMA_BEGINNING "must false;}"; |
| 853 | assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod)); |
| 854 | logbuf_assert("Invalid keyword \"must\" as a child of \"module\". Line number 3."); |
| 855 | mod = mod_renew(&ctx, mod, 0); |
| 856 | |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 857 | /* submodule */ |
| 858 | mod->submodule = 1; |
| 859 | |
| 860 | /* missing mandatory substatements */ |
| 861 | str = " subname {}"; |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 862 | lydict_remove(ctx.ctx, mod->name); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 863 | assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod)); |
| 864 | assert_string_equal("subname", mod->name); |
| 865 | logbuf_assert("Missing mandatory keyword \"belongs-to\" as a child of \"submodule\". Line number 3."); |
| 866 | mod = mod_renew(&ctx, mod, 1); |
| 867 | |
| 868 | str = " subname {belongs-to name;}"; |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 869 | lydict_remove(ctx.ctx, mod->name); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 870 | assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod)); |
| 871 | assert_string_equal("name", mod->belongsto); |
| 872 | mod = mod_renew(&ctx, mod, 1); |
| 873 | |
| 874 | #undef SCHEMA_BEGINNING |
| 875 | #define SCHEMA_BEGINNING " subname {belongs-to name;" |
| 876 | |
| 877 | /* duplicated namespace, prefix */ |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 878 | TEST_DUP("belongs-to", "module1", "module2", "3", 1); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 879 | |
| 880 | /* not allowed in submodule (module-specific) */ |
| 881 | str = SCHEMA_BEGINNING "namespace \"urn:z\";}"; |
| 882 | assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod)); |
| 883 | logbuf_assert("Invalid keyword \"namespace\" as a child of \"submodule\". Line number 3."); |
| 884 | mod = mod_renew(&ctx, mod, 1); |
| 885 | str = SCHEMA_BEGINNING "prefix m;}}"; |
| 886 | assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod)); |
| 887 | logbuf_assert("Invalid keyword \"prefix\" as a child of \"submodule\". Line number 3."); |
| 888 | mod = mod_renew(&ctx, mod, 1); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 889 | |
| 890 | #undef TEST_GENERIC |
| 891 | #undef TEST_NODE |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 892 | #undef TEST_DUP |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 893 | #undef SCHEMA_BEGINNING |
| 894 | |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 895 | lysp_module_free(mod); |
| 896 | ly_ctx_destroy(ctx.ctx, NULL); |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 897 | } |
| 898 | |
Radek Krejci | 4c6d9bd | 2018-10-15 16:43:06 +0200 | [diff] [blame] | 899 | static void |
| 900 | test_identity(void **state) |
| 901 | { |
| 902 | (void) state; /* unused */ |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 903 | int dict = 1; /* magic variable for FREE macros */ |
Radek Krejci | 4c6d9bd | 2018-10-15 16:43:06 +0200 | [diff] [blame] | 904 | |
| 905 | struct ly_parser_ctx ctx; |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 906 | struct lysp_module *mod = NULL; |
Radek Krejci | 4c6d9bd | 2018-10-15 16:43:06 +0200 | [diff] [blame] | 907 | struct lysp_ident *ident = NULL; |
| 908 | const char *str; |
Radek Krejci | 4c6d9bd | 2018-10-15 16:43:06 +0200 | [diff] [blame] | 909 | |
| 910 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 911 | assert_non_null(ctx.ctx); |
| 912 | ctx.line = 1; |
| 913 | ctx.indent = 0; |
| 914 | |
| 915 | /* invalid cardinality */ |
| 916 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 917 | TEST_DUP_GENERIC(" test {", MEMBER, VALUE1, VALUE2, parse_identity, \ |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 918 | &ident, "1", FREE_ARRAY(ctx.ctx, ident, lysp_ident_free); ident = NULL) |
Radek Krejci | 4c6d9bd | 2018-10-15 16:43:06 +0200 | [diff] [blame] | 919 | |
| 920 | TEST_DUP("description", "a", "b"); |
| 921 | TEST_DUP("reference", "a", "b"); |
| 922 | TEST_DUP("status", "current", "obsolete"); |
| 923 | |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 924 | /* full content */ |
| 925 | str = " test {base \"a\";base b; description text;reference \'another text\';status current; if-feature x;if-feature y;prefix:ext;} ..."; |
Radek Krejci | 4c6d9bd | 2018-10-15 16:43:06 +0200 | [diff] [blame] | 926 | assert_int_equal(LY_SUCCESS, parse_identity(&ctx, &str, &ident)); |
| 927 | assert_non_null(ident); |
| 928 | assert_string_equal(" ...", str); |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 929 | FREE_ARRAY(ctx.ctx, ident, lysp_ident_free); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 930 | ident = NULL; |
| 931 | |
| 932 | /* invalid substatement */ |
| 933 | str = " test {organization XXX;}"; |
| 934 | assert_int_equal(LY_EVALID, parse_identity(&ctx, &str, &ident)); |
| 935 | logbuf_assert("Invalid keyword \"organization\" as a child of \"identity\". Line number 1."); |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 936 | FREE_ARRAY(ctx.ctx, ident, lysp_ident_free); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 937 | ident = NULL; |
| 938 | |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 939 | /* identity duplication */ |
| 940 | str = "module a {namespace urn:a; prefix a; identity a; identity a;}"; |
| 941 | assert_int_equal(LY_EVALID, yang_parse(ctx.ctx, str, &mod)); |
| 942 | logbuf_assert("Duplicate identifier \"a\" of identity statement. Line number 1."); |
| 943 | assert_null(mod); |
| 944 | |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 945 | #undef TEST_DUP |
| 946 | |
| 947 | ly_ctx_destroy(ctx.ctx, NULL); |
| 948 | } |
| 949 | |
| 950 | static void |
| 951 | test_feature(void **state) |
| 952 | { |
| 953 | (void) state; /* unused */ |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 954 | int dict = 1; /* magic variable for FREE macros */ |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 955 | |
| 956 | struct ly_parser_ctx ctx; |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 957 | struct lysp_module *mod = NULL; |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 958 | struct lysp_feature *features = NULL; |
| 959 | const char *str; |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 960 | |
| 961 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 962 | assert_non_null(ctx.ctx); |
| 963 | ctx.line = 1; |
| 964 | ctx.indent = 0; |
| 965 | |
| 966 | /* invalid cardinality */ |
| 967 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
| 968 | TEST_DUP_GENERIC(" test {", MEMBER, VALUE1, VALUE2, parse_feature, \ |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 969 | &features, "1", FREE_ARRAY(ctx.ctx, features, lysp_feature_free); features = NULL) |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 970 | |
| 971 | TEST_DUP("description", "a", "b"); |
| 972 | TEST_DUP("reference", "a", "b"); |
| 973 | TEST_DUP("status", "current", "obsolete"); |
| 974 | |
| 975 | /* full content */ |
| 976 | str = " test {description text;reference \'another text\';status current; if-feature x;if-feature y;prefix:ext;} ..."; |
| 977 | assert_int_equal(LY_SUCCESS, parse_feature(&ctx, &str, &features)); |
| 978 | assert_non_null(features); |
| 979 | assert_string_equal(" ...", str); |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 980 | FREE_ARRAY(ctx.ctx, features, lysp_feature_free); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 981 | features = NULL; |
| 982 | |
| 983 | /* invalid substatement */ |
| 984 | str = " test {organization XXX;}"; |
| 985 | assert_int_equal(LY_EVALID, parse_feature(&ctx, &str, &features)); |
| 986 | logbuf_assert("Invalid keyword \"organization\" as a child of \"feature\". Line number 1."); |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 987 | FREE_ARRAY(ctx.ctx, features, lysp_feature_free); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 988 | features = NULL; |
| 989 | |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 990 | /* feature duplication */ |
| 991 | str = "module a {namespace urn:a; prefix a; feature a; feature a;}"; |
| 992 | assert_int_equal(LY_EVALID, yang_parse(ctx.ctx, str, &mod)); |
| 993 | logbuf_assert("Duplicate identifier \"a\" of feature statement. Line number 1."); |
| 994 | assert_null(mod); |
| 995 | |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 996 | #undef TEST_DUP |
| 997 | |
| 998 | ly_ctx_destroy(ctx.ctx, NULL); |
| 999 | } |
| 1000 | |
| 1001 | static void |
| 1002 | test_deviation(void **state) |
| 1003 | { |
| 1004 | (void) state; /* unused */ |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 1005 | int dict = 1; /* magic variable for FREE macros */ |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1006 | |
| 1007 | struct ly_parser_ctx ctx; |
| 1008 | struct lysp_deviation *d = NULL; |
| 1009 | const char *str; |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1010 | |
| 1011 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 1012 | assert_non_null(ctx.ctx); |
| 1013 | ctx.line = 1; |
| 1014 | ctx.indent = 0; |
| 1015 | |
| 1016 | /* invalid cardinality */ |
| 1017 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
| 1018 | TEST_DUP_GENERIC(" test {deviate not-supported;", MEMBER, VALUE1, VALUE2, parse_deviation, \ |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 1019 | &d, "1", FREE_ARRAY(ctx.ctx, d, lysp_deviation_free); d = NULL) |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1020 | |
| 1021 | TEST_DUP("description", "a", "b"); |
| 1022 | TEST_DUP("reference", "a", "b"); |
| 1023 | |
| 1024 | /* full content */ |
| 1025 | str = " test {deviate not-supported;description text;reference \'another text\';prefix:ext;} ..."; |
| 1026 | assert_int_equal(LY_SUCCESS, parse_deviation(&ctx, &str, &d)); |
| 1027 | assert_non_null(d); |
| 1028 | assert_string_equal(" ...", str); |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 1029 | FREE_ARRAY(ctx.ctx, d, lysp_deviation_free); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1030 | d = NULL; |
| 1031 | |
| 1032 | /* missing mandatory substatement */ |
| 1033 | str = " test {description text;}"; |
| 1034 | assert_int_equal(LY_EVALID, parse_deviation(&ctx, &str, &d)); |
| 1035 | logbuf_assert("Missing mandatory keyword \"deviate\" as a child of \"deviation\". Line number 1."); |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 1036 | FREE_ARRAY(ctx.ctx, d, lysp_deviation_free); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1037 | d = NULL; |
| 1038 | |
| 1039 | /* invalid substatement */ |
| 1040 | str = " test {deviate not-supported; status obsolete;}"; |
| 1041 | assert_int_equal(LY_EVALID, parse_deviation(&ctx, &str, &d)); |
| 1042 | logbuf_assert("Invalid keyword \"status\" as a child of \"deviation\". Line number 1."); |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 1043 | FREE_ARRAY(ctx.ctx, d, lysp_deviation_free); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1044 | d = NULL; |
| 1045 | |
| 1046 | #undef TEST_DUP |
| 1047 | |
| 1048 | ly_ctx_destroy(ctx.ctx, NULL); |
| 1049 | } |
| 1050 | |
| 1051 | static void |
| 1052 | test_deviate(void **state) |
| 1053 | { |
| 1054 | (void) state; /* unused */ |
| 1055 | |
| 1056 | struct ly_parser_ctx ctx; |
| 1057 | struct lysp_deviate *d = NULL; |
| 1058 | const char *str; |
| 1059 | |
| 1060 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 1061 | assert_non_null(ctx.ctx); |
| 1062 | ctx.line = 1; |
| 1063 | ctx.indent = 0; |
| 1064 | |
| 1065 | /* invalid cardinality */ |
| 1066 | #define TEST_DUP(TYPE, MEMBER, VALUE1, VALUE2) \ |
| 1067 | TEST_DUP_GENERIC(TYPE" {", MEMBER, VALUE1, VALUE2, parse_deviate, \ |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 1068 | &d, "1", lysp_deviate_free(ctx.ctx, d, 1); free(d); d = NULL) |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1069 | |
| 1070 | TEST_DUP("add", "config", "true", "false"); |
| 1071 | TEST_DUP("replace", "default", "int8", "uint8"); |
| 1072 | TEST_DUP("add", "mandatory", "true", "false"); |
| 1073 | TEST_DUP("add", "max-elements", "1", "2"); |
| 1074 | TEST_DUP("add", "min-elements", "1", "2"); |
| 1075 | TEST_DUP("replace", "type", "int8", "uint8"); |
| 1076 | TEST_DUP("add", "units", "kilometers", "miles"); |
| 1077 | |
| 1078 | /* full contents */ |
| 1079 | str = " not-supported {prefix:ext;} ..."; |
| 1080 | assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d)); |
| 1081 | assert_non_null(d); |
| 1082 | assert_string_equal(" ...", str); |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 1083 | lysp_deviate_free(ctx.ctx, d, 1); free(d); d = NULL; |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1084 | 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;} ..."; |
| 1085 | assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d)); |
| 1086 | assert_non_null(d); |
| 1087 | assert_string_equal(" ...", str); |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 1088 | lysp_deviate_free(ctx.ctx, d, 1); free(d); d = NULL; |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1089 | str = " delete {units meters; must 1; must 2; unique x; unique y; default a; default b; prefix:ext;} ..."; |
| 1090 | assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d)); |
| 1091 | assert_non_null(d); |
| 1092 | assert_string_equal(" ...", str); |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 1093 | lysp_deviate_free(ctx.ctx, d, 1); free(d); d = NULL; |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1094 | str = " replace {type string; units meters; default a; config true; mandatory true; min-elements 1; max-elements 2; prefix:ext;} ..."; |
| 1095 | assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d)); |
| 1096 | assert_non_null(d); |
| 1097 | assert_string_equal(" ...", str); |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 1098 | lysp_deviate_free(ctx.ctx, d, 1); free(d); d = NULL; |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1099 | |
| 1100 | /* invalid substatements */ |
| 1101 | #define TEST_NOT_SUP(DEV, STMT, VALUE) \ |
| 1102 | str = " "DEV" {"STMT" "VALUE";}..."; \ |
| 1103 | assert_int_equal(LY_EVALID, parse_deviate(&ctx, &str, &d)); \ |
| 1104 | logbuf_assert("Deviate \""DEV"\" does not support keyword \""STMT"\". Line number 1."); \ |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 1105 | lysp_deviate_free(ctx.ctx, d, 1); free(d); d = NULL |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1106 | |
| 1107 | TEST_NOT_SUP("not-supported", "units", "meters"); |
| 1108 | TEST_NOT_SUP("not-supported", "must", "1"); |
| 1109 | TEST_NOT_SUP("not-supported", "unique", "x"); |
| 1110 | TEST_NOT_SUP("not-supported", "default", "a"); |
| 1111 | TEST_NOT_SUP("not-supported", "config", "true"); |
| 1112 | TEST_NOT_SUP("not-supported", "mandatory", "true"); |
| 1113 | TEST_NOT_SUP("not-supported", "min-elements", "1"); |
| 1114 | TEST_NOT_SUP("not-supported", "max-elements", "2"); |
| 1115 | TEST_NOT_SUP("not-supported", "type", "string"); |
| 1116 | TEST_NOT_SUP("add", "type", "string"); |
| 1117 | TEST_NOT_SUP("delete", "config", "true"); |
| 1118 | TEST_NOT_SUP("delete", "mandatory", "true"); |
| 1119 | TEST_NOT_SUP("delete", "min-elements", "1"); |
| 1120 | TEST_NOT_SUP("delete", "max-elements", "2"); |
| 1121 | TEST_NOT_SUP("delete", "type", "string"); |
| 1122 | TEST_NOT_SUP("replace", "must", "1"); |
| 1123 | TEST_NOT_SUP("replace", "unique", "a"); |
| 1124 | |
| 1125 | str = " nonsence; ..."; |
| 1126 | assert_int_equal(LY_EVALID, parse_deviate(&ctx, &str, &d)); |
| 1127 | logbuf_assert("Invalid value \"nonsence\" of \"deviate\". Line number 1."); |
| 1128 | assert_null(d); |
| 1129 | |
| 1130 | #undef TEST_NOT_SUP |
| 1131 | #undef TEST_DUP |
Radek Krejci | 4c6d9bd | 2018-10-15 16:43:06 +0200 | [diff] [blame] | 1132 | |
| 1133 | ly_ctx_destroy(ctx.ctx, NULL); |
| 1134 | } |
| 1135 | |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 1136 | int main(void) |
| 1137 | { |
| 1138 | const struct CMUnitTest tests[] = { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1139 | cmocka_unit_test_setup(test_helpers, logger_setup), |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 1140 | cmocka_unit_test_setup(test_comments, logger_setup), |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1141 | cmocka_unit_test_setup(test_arg, logger_setup), |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 1142 | cmocka_unit_test_setup(test_stmts, logger_setup), |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 1143 | cmocka_unit_test_setup(test_module, logger_setup), |
Radek Krejci | 4c6d9bd | 2018-10-15 16:43:06 +0200 | [diff] [blame] | 1144 | cmocka_unit_test_setup(test_identity, logger_setup), |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1145 | cmocka_unit_test_setup(test_feature, logger_setup), |
| 1146 | cmocka_unit_test_setup(test_deviation, logger_setup), |
| 1147 | cmocka_unit_test_setup(test_deviate, logger_setup), |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 1148 | }; |
| 1149 | |
| 1150 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 1151 | } |