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); |
| 653 | return mod; |
| 654 | } |
| 655 | |
| 656 | static void |
| 657 | test_module(void **state) |
| 658 | { |
| 659 | (void) state; /* unused */ |
| 660 | |
| 661 | struct ly_parser_ctx ctx; |
| 662 | struct lysp_module *mod; |
| 663 | const char *str; |
| 664 | |
| 665 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 666 | assert_non_null(ctx.ctx); |
| 667 | ctx.line = 1; |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 668 | ctx.indent = 0; |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 669 | |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 670 | mod = mod_renew(&ctx, NULL, 0); |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 671 | |
| 672 | /* missing mandatory substatements */ |
| 673 | str = " name {}"; |
| 674 | assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod)); |
| 675 | assert_string_equal("name", mod->name); |
| 676 | logbuf_assert("Missing mandatory keyword \"namespace\" as a child of \"module\". Line number 1."); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 677 | mod = mod_renew(&ctx, mod, 0); |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 678 | |
| 679 | str = " name {namespace urn:x;}"; |
| 680 | assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod)); |
| 681 | assert_string_equal("urn:x", mod->ns); |
| 682 | logbuf_assert("Missing mandatory keyword \"prefix\" as a child of \"module\". Line number 1."); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 683 | mod = mod_renew(&ctx, mod, 0); |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 684 | |
| 685 | str = " name {namespace urn:x;prefix \"x\";}"; |
| 686 | assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod)); |
| 687 | assert_string_equal("x", mod->prefix); |
Radek 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 | |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 690 | #define SCHEMA_BEGINNING " name {namespace urn:x;prefix \"x\";" |
| 691 | #define TEST_NODE(NODETYPE, INPUT, NAME) \ |
| 692 | str = SCHEMA_BEGINNING INPUT; \ |
| 693 | assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod)); \ |
| 694 | assert_non_null(mod->data); \ |
| 695 | assert_int_equal(NODETYPE, mod->data->nodetype); \ |
| 696 | assert_string_equal(NAME, mod->data->name); \ |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 697 | mod = mod_renew(&ctx, mod, 0); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 698 | #define TEST_GENERIC(INPUT, TARGET, TEST) \ |
| 699 | str = SCHEMA_BEGINNING INPUT; \ |
| 700 | assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod)); \ |
| 701 | assert_non_null(TARGET); \ |
| 702 | TEST; \ |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 703 | mod = mod_renew(&ctx, mod, 0); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 704 | #define TEST_DUP(MEMBER, VALUE1, VALUE2, LINE, SUBMODULE) \ |
| 705 | TEST_DUP_GENERIC(SCHEMA_BEGINNING, MEMBER, VALUE1, VALUE2, \ |
| 706 | parse_sub_module, mod, LINE, mod = mod_renew(&ctx, mod, SUBMODULE)) |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 707 | |
| 708 | /* duplicated namespace, prefix */ |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 709 | TEST_DUP("namespace", "y", "z", "1", 0); |
| 710 | TEST_DUP("prefix", "y", "z", "1", 0); |
| 711 | TEST_DUP("contact", "a", "b", "1", 0); |
| 712 | TEST_DUP("description", "a", "b", "1", 0); |
| 713 | TEST_DUP("organization", "a", "b", "1", 0); |
| 714 | TEST_DUP("reference", "a", "b", "1", 0); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 715 | |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 716 | /* not allowed in module (submodule-specific) */ |
| 717 | str = SCHEMA_BEGINNING "belongs-to master {prefix m;}}"; |
| 718 | assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod)); |
| 719 | logbuf_assert("Invalid keyword \"belongs-to\" as a child of \"module\". Line number 1."); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 720 | mod = mod_renew(&ctx, mod, 0); |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 721 | |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 722 | /* anydata */ |
| 723 | TEST_NODE(LYS_ANYDATA, "anydata test;}", "test"); |
| 724 | /* anyxml */ |
| 725 | TEST_NODE(LYS_ANYXML, "anyxml test;}", "test"); |
| 726 | /* augment */ |
| 727 | TEST_GENERIC("augment /somepath;}", mod->augments, |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 728 | assert_string_equal("/somepath", LY_ARRAY_INDEX(mod->augments, 0, struct lysp_augment)->nodeid)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 729 | /* choice */ |
| 730 | TEST_NODE(LYS_CHOICE, "choice test;}", "test"); |
| 731 | /* contact 0..1 */ |
| 732 | TEST_GENERIC("contact \"firstname\" + \n\t\" surname\";}", mod->contact, |
| 733 | assert_string_equal("firstname surname", mod->contact)); |
| 734 | /* container */ |
| 735 | TEST_NODE(LYS_CONTAINER, "container test;}", "test"); |
| 736 | /* description 0..1 */ |
| 737 | TEST_GENERIC("description \'some description\';}", mod->dsc, |
| 738 | assert_string_equal("some description", mod->dsc)); |
| 739 | /* deviation */ |
| 740 | TEST_GENERIC("deviation /somepath {deviate not-supported;}}", mod->deviations, |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 741 | assert_string_equal("/somepath", LY_ARRAY_INDEX(mod->deviations, 0, struct lysp_deviation)->nodeid)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 742 | /* extension */ |
| 743 | TEST_GENERIC("extension test;}", mod->extensions, |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 744 | assert_string_equal("test", LY_ARRAY_INDEX(mod->extensions, 0, struct lysp_ext)->name)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 745 | /* feature */ |
| 746 | TEST_GENERIC("feature test;}", mod->features, |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 747 | assert_string_equal("test", LY_ARRAY_INDEX(mod->features, 0, struct lysp_feature)->name)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 748 | /* grouping */ |
| 749 | TEST_GENERIC("grouping grp;}", mod->groupings, |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 750 | assert_string_equal("grp", LY_ARRAY_INDEX(mod->groupings, 0, struct lysp_grp)->name)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 751 | /* identity */ |
| 752 | TEST_GENERIC("identity test;}", mod->identities, |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 753 | assert_string_equal("test", LY_ARRAY_INDEX(mod->identities, 0, struct lysp_ident)->name)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 754 | /* import */ |
| 755 | TEST_GENERIC("import test {prefix z;}}", mod->imports, |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 756 | assert_string_equal("test", LY_ARRAY_INDEX(mod->imports, 0, struct lysp_import)->name)); |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 757 | |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 758 | /* import - prefix collision */ |
| 759 | str = SCHEMA_BEGINNING "import test {prefix x;}}"; |
| 760 | assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod)); |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 761 | logbuf_assert("Prefix \"x\" already used as module prefix. Line number 2."); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 762 | mod = mod_renew(&ctx, mod, 0); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 763 | str = SCHEMA_BEGINNING "import test1 {prefix y;}import test2 {prefix y;}}"; |
| 764 | assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod)); |
| 765 | logbuf_assert("Prefix \"y\" already used to import \"test1\" module. Line number 2."); |
| 766 | mod = mod_renew(&ctx, mod, 0); |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 767 | |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 768 | /* include */ |
Radek Krejci | 469f70d | 2018-10-15 15:27:29 +0200 | [diff] [blame] | 769 | TEST_GENERIC("include test;}", mod->includes, |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 770 | assert_string_equal("test", LY_ARRAY_INDEX(mod->includes, 0, struct lysp_include)->name)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 771 | /* leaf */ |
| 772 | TEST_NODE(LYS_LEAF, "leaf test {type string;}}", "test"); |
| 773 | /* leaf-list */ |
| 774 | TEST_NODE(LYS_LEAFLIST, "leaf-list test {type string;}}", "test"); |
| 775 | /* list */ |
| 776 | TEST_NODE(LYS_LIST, "list test {key a;leaf a {type string;}}}", "test"); |
| 777 | /* notification */ |
| 778 | TEST_GENERIC("notification test;}", mod->notifs, |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 779 | assert_string_equal("test", LY_ARRAY_INDEX(mod->notifs, 0, struct lysp_notif)->name)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 780 | /* organization 0..1 */ |
| 781 | TEST_GENERIC("organization \"CESNET a.l.e.\";}", mod->org, |
| 782 | assert_string_equal("CESNET a.l.e.", mod->org)); |
| 783 | /* reference 0..1 */ |
| 784 | TEST_GENERIC("reference RFC7950;}", mod->ref, |
| 785 | assert_string_equal("RFC7950", mod->ref)); |
| 786 | /* revision */ |
| 787 | TEST_GENERIC("revision 2018-10-12;}", mod->revs, |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 788 | assert_string_equal("2018-10-12", LY_ARRAY_INDEX(mod->revs, 0, struct lysp_revision)->rev)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 789 | /* rpc */ |
| 790 | TEST_GENERIC("rpc test;}", mod->rpcs, |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 791 | assert_string_equal("test", LY_ARRAY_INDEX(mod->rpcs, 0, struct lysp_action)->name)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 792 | /* typedef */ |
| 793 | TEST_GENERIC("typedef test{type string;}}", mod->typedefs, |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 794 | assert_string_equal("test", LY_ARRAY_INDEX(mod->typedefs, 0, struct lysp_tpdf)->name)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 795 | /* uses */ |
| 796 | TEST_NODE(LYS_USES, "uses test;}", "test"); |
| 797 | /* yang-version */ |
| 798 | str = SCHEMA_BEGINNING "\n\tyang-version 10;}"; |
| 799 | assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod)); |
| 800 | logbuf_assert("Invalid value \"10\" of \"yang-version\". Line number 3."); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 801 | mod = mod_renew(&ctx, mod, 0); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 802 | str = SCHEMA_BEGINNING "yang-version 1.0;yang-version 1.1;}"; |
| 803 | assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod)); |
| 804 | logbuf_assert("Duplicate keyword \"yang-version\". Line number 3."); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 805 | mod = mod_renew(&ctx, mod, 0); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 806 | str = SCHEMA_BEGINNING "yang-version 1.0;}"; |
| 807 | assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod)); |
| 808 | assert_int_equal(1, mod->version); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 809 | mod = mod_renew(&ctx, mod, 0); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 810 | str = SCHEMA_BEGINNING "yang-version \"1.1\";}"; |
| 811 | assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod)); |
| 812 | assert_int_equal(2, mod->version); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 813 | mod = mod_renew(&ctx, mod, 0); |
| 814 | |
Radek Krejci | 156ccaf | 2018-10-15 15:49:17 +0200 | [diff] [blame] | 815 | /* extensions */ |
| 816 | TEST_GENERIC("prefix:test;}", mod->exts, |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 817 | assert_string_equal("prefix:test", LY_ARRAY_INDEX(mod->exts, 0, struct lysp_ext_instance)->name); |
| 818 | assert_int_equal(LYEXT_SUBSTMT_SELF, LY_ARRAY_INDEX(mod->exts, 0, struct lysp_ext_instance)->insubstmt)); |
Radek Krejci | 156ccaf | 2018-10-15 15:49:17 +0200 | [diff] [blame] | 819 | mod = mod_renew(&ctx, mod, 0); |
| 820 | |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 821 | /* invalid substatement */ |
| 822 | str = SCHEMA_BEGINNING "must false;}"; |
| 823 | assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod)); |
| 824 | logbuf_assert("Invalid keyword \"must\" as a child of \"module\". Line number 3."); |
| 825 | mod = mod_renew(&ctx, mod, 0); |
| 826 | |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 827 | /* submodule */ |
| 828 | mod->submodule = 1; |
| 829 | |
| 830 | /* missing mandatory substatements */ |
| 831 | str = " subname {}"; |
| 832 | assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod)); |
| 833 | assert_string_equal("subname", mod->name); |
| 834 | logbuf_assert("Missing mandatory keyword \"belongs-to\" as a child of \"submodule\". Line number 3."); |
| 835 | mod = mod_renew(&ctx, mod, 1); |
| 836 | |
| 837 | str = " subname {belongs-to name;}"; |
| 838 | assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod)); |
| 839 | assert_string_equal("name", mod->belongsto); |
| 840 | mod = mod_renew(&ctx, mod, 1); |
| 841 | |
| 842 | #undef SCHEMA_BEGINNING |
| 843 | #define SCHEMA_BEGINNING " subname {belongs-to name;" |
| 844 | |
| 845 | /* duplicated namespace, prefix */ |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 846 | TEST_DUP("belongs-to", "module1", "module2", "3", 1); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 847 | |
| 848 | /* not allowed in submodule (module-specific) */ |
| 849 | str = SCHEMA_BEGINNING "namespace \"urn:z\";}"; |
| 850 | assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod)); |
| 851 | logbuf_assert("Invalid keyword \"namespace\" as a child of \"submodule\". Line number 3."); |
| 852 | mod = mod_renew(&ctx, mod, 1); |
| 853 | str = SCHEMA_BEGINNING "prefix m;}}"; |
| 854 | assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod)); |
| 855 | logbuf_assert("Invalid keyword \"prefix\" as a child of \"submodule\". Line number 3."); |
| 856 | mod = mod_renew(&ctx, mod, 1); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 857 | |
| 858 | #undef TEST_GENERIC |
| 859 | #undef TEST_NODE |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 860 | #undef TEST_DUP |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 861 | #undef SCHEMA_BEGINNING |
| 862 | |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 863 | lysp_module_free(mod); |
| 864 | ly_ctx_destroy(ctx.ctx, NULL); |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 865 | } |
| 866 | |
Radek Krejci | 4c6d9bd | 2018-10-15 16:43:06 +0200 | [diff] [blame] | 867 | static void |
| 868 | test_identity(void **state) |
| 869 | { |
| 870 | (void) state; /* unused */ |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 871 | int dict = 1; /* magic variable for FREE macros */ |
Radek Krejci | 4c6d9bd | 2018-10-15 16:43:06 +0200 | [diff] [blame] | 872 | |
| 873 | struct ly_parser_ctx ctx; |
| 874 | struct lysp_ident *ident = NULL; |
| 875 | const char *str; |
Radek Krejci | 4c6d9bd | 2018-10-15 16:43:06 +0200 | [diff] [blame] | 876 | |
| 877 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 878 | assert_non_null(ctx.ctx); |
| 879 | ctx.line = 1; |
| 880 | ctx.indent = 0; |
| 881 | |
| 882 | /* invalid cardinality */ |
| 883 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 884 | TEST_DUP_GENERIC(" test {", MEMBER, VALUE1, VALUE2, parse_identity, \ |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 885 | &ident, "1", FREE_ARRAY(ctx.ctx, ident, lysp_ident_free); ident = NULL) |
Radek Krejci | 4c6d9bd | 2018-10-15 16:43:06 +0200 | [diff] [blame] | 886 | |
| 887 | TEST_DUP("description", "a", "b"); |
| 888 | TEST_DUP("reference", "a", "b"); |
| 889 | TEST_DUP("status", "current", "obsolete"); |
| 890 | |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 891 | /* full content */ |
| 892 | str = " test {base \"a\";base b; description text;reference \'another text\';status current; if-feature x;if-feature y;prefix:ext;} ..."; |
Radek Krejci | 4c6d9bd | 2018-10-15 16:43:06 +0200 | [diff] [blame] | 893 | assert_int_equal(LY_SUCCESS, parse_identity(&ctx, &str, &ident)); |
| 894 | assert_non_null(ident); |
| 895 | assert_string_equal(" ...", str); |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 896 | FREE_ARRAY(ctx.ctx, ident, lysp_ident_free); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 897 | ident = NULL; |
| 898 | |
| 899 | /* invalid substatement */ |
| 900 | str = " test {organization XXX;}"; |
| 901 | assert_int_equal(LY_EVALID, parse_identity(&ctx, &str, &ident)); |
| 902 | logbuf_assert("Invalid keyword \"organization\" as a child of \"identity\". Line number 1."); |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 903 | FREE_ARRAY(ctx.ctx, ident, lysp_ident_free); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 904 | ident = NULL; |
| 905 | |
| 906 | #undef TEST_DUP |
| 907 | |
| 908 | ly_ctx_destroy(ctx.ctx, NULL); |
| 909 | } |
| 910 | |
| 911 | static void |
| 912 | test_feature(void **state) |
| 913 | { |
| 914 | (void) state; /* unused */ |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 915 | int dict = 1; /* magic variable for FREE macros */ |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 916 | |
| 917 | struct ly_parser_ctx ctx; |
| 918 | struct lysp_feature *features = NULL; |
| 919 | const char *str; |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 920 | |
| 921 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 922 | assert_non_null(ctx.ctx); |
| 923 | ctx.line = 1; |
| 924 | ctx.indent = 0; |
| 925 | |
| 926 | /* invalid cardinality */ |
| 927 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
| 928 | TEST_DUP_GENERIC(" test {", MEMBER, VALUE1, VALUE2, parse_feature, \ |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 929 | &features, "1", FREE_ARRAY(ctx.ctx, features, lysp_feature_free); features = NULL) |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 930 | |
| 931 | TEST_DUP("description", "a", "b"); |
| 932 | TEST_DUP("reference", "a", "b"); |
| 933 | TEST_DUP("status", "current", "obsolete"); |
| 934 | |
| 935 | /* full content */ |
| 936 | str = " test {description text;reference \'another text\';status current; if-feature x;if-feature y;prefix:ext;} ..."; |
| 937 | assert_int_equal(LY_SUCCESS, parse_feature(&ctx, &str, &features)); |
| 938 | assert_non_null(features); |
| 939 | assert_string_equal(" ...", str); |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 940 | FREE_ARRAY(ctx.ctx, features, lysp_feature_free); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 941 | features = NULL; |
| 942 | |
| 943 | /* invalid substatement */ |
| 944 | str = " test {organization XXX;}"; |
| 945 | assert_int_equal(LY_EVALID, parse_feature(&ctx, &str, &features)); |
| 946 | logbuf_assert("Invalid keyword \"organization\" as a child of \"feature\". Line number 1."); |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 947 | FREE_ARRAY(ctx.ctx, features, lysp_feature_free); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 948 | features = NULL; |
| 949 | |
| 950 | #undef TEST_DUP |
| 951 | |
| 952 | ly_ctx_destroy(ctx.ctx, NULL); |
| 953 | } |
| 954 | |
| 955 | static void |
| 956 | test_deviation(void **state) |
| 957 | { |
| 958 | (void) state; /* unused */ |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 959 | int dict = 1; /* magic variable for FREE macros */ |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 960 | |
| 961 | struct ly_parser_ctx ctx; |
| 962 | struct lysp_deviation *d = NULL; |
| 963 | const char *str; |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 964 | |
| 965 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 966 | assert_non_null(ctx.ctx); |
| 967 | ctx.line = 1; |
| 968 | ctx.indent = 0; |
| 969 | |
| 970 | /* invalid cardinality */ |
| 971 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
| 972 | TEST_DUP_GENERIC(" test {deviate not-supported;", MEMBER, VALUE1, VALUE2, parse_deviation, \ |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 973 | &d, "1", FREE_ARRAY(ctx.ctx, d, lysp_deviation_free); d = NULL) |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 974 | |
| 975 | TEST_DUP("description", "a", "b"); |
| 976 | TEST_DUP("reference", "a", "b"); |
| 977 | |
| 978 | /* full content */ |
| 979 | str = " test {deviate not-supported;description text;reference \'another text\';prefix:ext;} ..."; |
| 980 | assert_int_equal(LY_SUCCESS, parse_deviation(&ctx, &str, &d)); |
| 981 | assert_non_null(d); |
| 982 | assert_string_equal(" ...", str); |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 983 | FREE_ARRAY(ctx.ctx, d, lysp_deviation_free); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 984 | d = NULL; |
| 985 | |
| 986 | /* missing mandatory substatement */ |
| 987 | str = " test {description text;}"; |
| 988 | assert_int_equal(LY_EVALID, parse_deviation(&ctx, &str, &d)); |
| 989 | logbuf_assert("Missing mandatory keyword \"deviate\" as a child of \"deviation\". Line number 1."); |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 990 | FREE_ARRAY(ctx.ctx, d, lysp_deviation_free); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 991 | d = NULL; |
| 992 | |
| 993 | /* invalid substatement */ |
| 994 | str = " test {deviate not-supported; status obsolete;}"; |
| 995 | assert_int_equal(LY_EVALID, parse_deviation(&ctx, &str, &d)); |
| 996 | logbuf_assert("Invalid keyword \"status\" as a child of \"deviation\". Line number 1."); |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 997 | FREE_ARRAY(ctx.ctx, d, lysp_deviation_free); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 998 | d = NULL; |
| 999 | |
| 1000 | #undef TEST_DUP |
| 1001 | |
| 1002 | ly_ctx_destroy(ctx.ctx, NULL); |
| 1003 | } |
| 1004 | |
| 1005 | static void |
| 1006 | test_deviate(void **state) |
| 1007 | { |
| 1008 | (void) state; /* unused */ |
| 1009 | |
| 1010 | struct ly_parser_ctx ctx; |
| 1011 | struct lysp_deviate *d = NULL; |
| 1012 | const char *str; |
| 1013 | |
| 1014 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 1015 | assert_non_null(ctx.ctx); |
| 1016 | ctx.line = 1; |
| 1017 | ctx.indent = 0; |
| 1018 | |
| 1019 | /* invalid cardinality */ |
| 1020 | #define TEST_DUP(TYPE, MEMBER, VALUE1, VALUE2) \ |
| 1021 | TEST_DUP_GENERIC(TYPE" {", MEMBER, VALUE1, VALUE2, parse_deviate, \ |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 1022 | &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] | 1023 | |
| 1024 | TEST_DUP("add", "config", "true", "false"); |
| 1025 | TEST_DUP("replace", "default", "int8", "uint8"); |
| 1026 | TEST_DUP("add", "mandatory", "true", "false"); |
| 1027 | TEST_DUP("add", "max-elements", "1", "2"); |
| 1028 | TEST_DUP("add", "min-elements", "1", "2"); |
| 1029 | TEST_DUP("replace", "type", "int8", "uint8"); |
| 1030 | TEST_DUP("add", "units", "kilometers", "miles"); |
| 1031 | |
| 1032 | /* full contents */ |
| 1033 | str = " not-supported {prefix:ext;} ..."; |
| 1034 | assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d)); |
| 1035 | assert_non_null(d); |
| 1036 | assert_string_equal(" ...", str); |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 1037 | lysp_deviate_free(ctx.ctx, d, 1); free(d); d = NULL; |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1038 | str = " add {units meters; must 1; must 2; unique x; unique y; default a; default b; config true; mandatory true; min-elements 1; max-elements 2; prefix:ext;} ..."; |
| 1039 | assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d)); |
| 1040 | assert_non_null(d); |
| 1041 | assert_string_equal(" ...", str); |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 1042 | lysp_deviate_free(ctx.ctx, d, 1); free(d); d = NULL; |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1043 | str = " delete {units meters; must 1; must 2; unique x; unique y; default a; default b; prefix:ext;} ..."; |
| 1044 | assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d)); |
| 1045 | assert_non_null(d); |
| 1046 | assert_string_equal(" ...", str); |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 1047 | lysp_deviate_free(ctx.ctx, d, 1); free(d); d = NULL; |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1048 | str = " replace {type string; units meters; default a; config true; mandatory true; min-elements 1; max-elements 2; prefix:ext;} ..."; |
| 1049 | assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d)); |
| 1050 | assert_non_null(d); |
| 1051 | assert_string_equal(" ...", str); |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 1052 | lysp_deviate_free(ctx.ctx, d, 1); free(d); d = NULL; |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1053 | |
| 1054 | /* invalid substatements */ |
| 1055 | #define TEST_NOT_SUP(DEV, STMT, VALUE) \ |
| 1056 | str = " "DEV" {"STMT" "VALUE";}..."; \ |
| 1057 | assert_int_equal(LY_EVALID, parse_deviate(&ctx, &str, &d)); \ |
| 1058 | logbuf_assert("Deviate \""DEV"\" does not support keyword \""STMT"\". Line number 1."); \ |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 1059 | lysp_deviate_free(ctx.ctx, d, 1); free(d); d = NULL |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1060 | |
| 1061 | TEST_NOT_SUP("not-supported", "units", "meters"); |
| 1062 | TEST_NOT_SUP("not-supported", "must", "1"); |
| 1063 | TEST_NOT_SUP("not-supported", "unique", "x"); |
| 1064 | TEST_NOT_SUP("not-supported", "default", "a"); |
| 1065 | TEST_NOT_SUP("not-supported", "config", "true"); |
| 1066 | TEST_NOT_SUP("not-supported", "mandatory", "true"); |
| 1067 | TEST_NOT_SUP("not-supported", "min-elements", "1"); |
| 1068 | TEST_NOT_SUP("not-supported", "max-elements", "2"); |
| 1069 | TEST_NOT_SUP("not-supported", "type", "string"); |
| 1070 | TEST_NOT_SUP("add", "type", "string"); |
| 1071 | TEST_NOT_SUP("delete", "config", "true"); |
| 1072 | TEST_NOT_SUP("delete", "mandatory", "true"); |
| 1073 | TEST_NOT_SUP("delete", "min-elements", "1"); |
| 1074 | TEST_NOT_SUP("delete", "max-elements", "2"); |
| 1075 | TEST_NOT_SUP("delete", "type", "string"); |
| 1076 | TEST_NOT_SUP("replace", "must", "1"); |
| 1077 | TEST_NOT_SUP("replace", "unique", "a"); |
| 1078 | |
| 1079 | str = " nonsence; ..."; |
| 1080 | assert_int_equal(LY_EVALID, parse_deviate(&ctx, &str, &d)); |
| 1081 | logbuf_assert("Invalid value \"nonsence\" of \"deviate\". Line number 1."); |
| 1082 | assert_null(d); |
| 1083 | |
| 1084 | #undef TEST_NOT_SUP |
| 1085 | #undef TEST_DUP |
Radek Krejci | 4c6d9bd | 2018-10-15 16:43:06 +0200 | [diff] [blame] | 1086 | |
| 1087 | ly_ctx_destroy(ctx.ctx, NULL); |
| 1088 | } |
| 1089 | |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 1090 | int main(void) |
| 1091 | { |
| 1092 | const struct CMUnitTest tests[] = { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1093 | cmocka_unit_test_setup(test_helpers, logger_setup), |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 1094 | cmocka_unit_test_setup(test_comments, logger_setup), |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1095 | cmocka_unit_test_setup(test_arg, logger_setup), |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 1096 | cmocka_unit_test_setup(test_stmts, logger_setup), |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 1097 | cmocka_unit_test_setup(test_module, logger_setup), |
Radek Krejci | 4c6d9bd | 2018-10-15 16:43:06 +0200 | [diff] [blame] | 1098 | cmocka_unit_test_setup(test_identity, logger_setup), |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1099 | cmocka_unit_test_setup(test_feature, logger_setup), |
| 1100 | cmocka_unit_test_setup(test_deviation, logger_setup), |
| 1101 | cmocka_unit_test_setup(test_deviate, logger_setup), |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 1102 | }; |
| 1103 | |
| 1104 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 1105 | } |