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 | |
| 15 | #define _BSD_SOURCE |
| 16 | #define _DEFAULT_SOURCE |
| 17 | #include <stdarg.h> |
| 18 | #include <stddef.h> |
| 19 | #include <setjmp.h> |
| 20 | #include <cmocka.h> |
| 21 | |
| 22 | #include <stdio.h> |
| 23 | #include <string.h> |
| 24 | |
| 25 | #include "libyang.h" |
| 26 | #include "../../src/parser_yang.c" |
Radek Krejci | 4c6d9bd | 2018-10-15 16:43:06 +0200 | [diff] [blame] | 27 | #include "../../src/tree_schema.c" |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 28 | |
| 29 | #define BUFSIZE 1024 |
| 30 | char logbuf[BUFSIZE] = {0}; |
| 31 | |
| 32 | /* 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] | 33 | #define ENABLE_LOGGER_CHECKING 1 |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 34 | |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 35 | #if ENABLE_LOGGER_CHECKING |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 36 | static void |
| 37 | logger(LY_LOG_LEVEL level, const char *msg, const char *path) |
| 38 | { |
| 39 | (void) level; /* unused */ |
| 40 | |
| 41 | if (path) { |
| 42 | snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path); |
| 43 | } else { |
| 44 | strncpy(logbuf, msg, BUFSIZE - 1); |
| 45 | } |
| 46 | } |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 47 | #endif |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 48 | |
| 49 | static int |
| 50 | logger_setup(void **state) |
| 51 | { |
| 52 | (void) state; /* unused */ |
| 53 | #if ENABLE_LOGGER_CHECKING |
| 54 | ly_set_log_clb(logger, 1); |
| 55 | #endif |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | void |
| 60 | logbuf_clean(void) |
| 61 | { |
| 62 | logbuf[0] = '\0'; |
| 63 | } |
| 64 | |
| 65 | #if ENABLE_LOGGER_CHECKING |
| 66 | # define logbuf_assert(str) assert_string_equal(logbuf, str) |
| 67 | #else |
| 68 | # define logbuf_assert(str) |
| 69 | #endif |
| 70 | |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 71 | #define TEST_DUP_GENERIC(PREFIX, MEMBER, VALUE1, VALUE2, FUNC, RESULT, LINE, CLEANUP) \ |
| 72 | str = PREFIX MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
| 73 | assert_int_equal(LY_EVALID, FUNC(&ctx, &str, RESULT)); \ |
| 74 | logbuf_assert("Duplicate keyword \""MEMBER"\". Line number "LINE"."); \ |
| 75 | CLEANUP |
| 76 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 77 | static void |
| 78 | test_helpers(void **state) |
| 79 | { |
| 80 | (void) state; /* unused */ |
| 81 | |
| 82 | const char *str; |
Radek Krejci | 404251e | 2018-10-09 12:06:44 +0200 | [diff] [blame] | 83 | char *buf, *p; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 84 | size_t len, size; |
| 85 | int prefix; |
| 86 | struct ly_parser_ctx ctx; |
| 87 | ctx.ctx = NULL; |
| 88 | ctx.line = 1; |
| 89 | |
| 90 | /* storing into buffer */ |
| 91 | str = "abcd"; |
| 92 | buf = NULL; |
| 93 | size = len = 0; |
| 94 | assert_int_equal(LY_SUCCESS, buf_add_char(NULL, &str, 2, &buf, &size, &len)); |
| 95 | assert_int_not_equal(0, size); |
| 96 | assert_int_equal(2, len); |
| 97 | assert_string_equal("cd", str); |
| 98 | assert_false(strncmp("ab", buf, 2)); |
| 99 | free(buf); |
Radek Krejci | 404251e | 2018-10-09 12:06:44 +0200 | [diff] [blame] | 100 | buf = NULL; |
| 101 | |
| 102 | /* invalid first characters */ |
| 103 | len = 0; |
| 104 | str = "2invalid"; |
| 105 | assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1)); |
| 106 | str = ".invalid"; |
| 107 | assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1)); |
| 108 | str = "-invalid"; |
| 109 | assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1)); |
| 110 | /* invalid following characters */ |
| 111 | len = 3; /* number of characters read before the str content */ |
| 112 | str = "!"; |
| 113 | assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1)); |
| 114 | str = ":"; |
| 115 | assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1)); |
| 116 | /* valid colon for prefixed identifiers */ |
| 117 | len = size = 0; |
| 118 | p = NULL; |
| 119 | str = "x:id"; |
| 120 | assert_int_equal(LY_SUCCESS, buf_store_char(&ctx, &str, Y_PREF_IDENTIF_ARG, &p, &len, &buf, &size, 0)); |
| 121 | assert_int_equal(1, len); |
| 122 | assert_null(buf); |
| 123 | assert_string_equal(":id", str); |
| 124 | assert_int_equal('x', p[len - 1]); |
| 125 | assert_int_equal(LY_SUCCESS, buf_store_char(&ctx, &str, Y_PREF_IDENTIF_ARG, &p, &len, &buf, &size, 1)); |
| 126 | assert_int_equal(2, len); |
| 127 | assert_string_equal("id", str); |
| 128 | assert_int_equal(':', p[len - 1]); |
| 129 | free(buf); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 130 | |
| 131 | /* checking identifiers */ |
| 132 | assert_int_equal(LY_EVALID, check_identifierchar(&ctx, ':', 0, NULL)); |
| 133 | logbuf_assert("Invalid identifier character ':'. Line number 1."); |
| 134 | assert_int_equal(LY_EVALID, check_identifierchar(&ctx, '#', 1, NULL)); |
| 135 | logbuf_assert("Invalid identifier first character '#'. Line number 1."); |
| 136 | |
| 137 | assert_int_equal(LY_SUCCESS, check_identifierchar(&ctx, 'a', 1, &prefix)); |
| 138 | assert_int_equal(0, prefix); |
| 139 | assert_int_equal(LY_SUCCESS, check_identifierchar(&ctx, ':', 0, &prefix)); |
| 140 | assert_int_equal(1, prefix); |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 141 | assert_int_equal(LY_EVALID, check_identifierchar(&ctx, ':', 0, &prefix)); |
| 142 | assert_int_equal(1, prefix); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 143 | assert_int_equal(LY_SUCCESS, check_identifierchar(&ctx, 'b', 0, &prefix)); |
| 144 | assert_int_equal(2, prefix); |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 145 | /* second colon is invalid */ |
| 146 | assert_int_equal(LY_EVALID, check_identifierchar(&ctx, ':', 0, &prefix)); |
| 147 | logbuf_assert("Invalid identifier character ':'. Line number 1."); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 148 | } |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 149 | |
| 150 | static void |
| 151 | test_comments(void **state) |
| 152 | { |
| 153 | (void) state; /* unused */ |
| 154 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 155 | struct ly_parser_ctx ctx; |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 156 | const char *str, *p; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 157 | char *word, *buf; |
| 158 | size_t len; |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 159 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 160 | ctx.ctx = NULL; |
| 161 | ctx.line = 1; |
| 162 | |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 163 | str = " // this is a text of / one * line */ comment\nargument"; |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 164 | 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] | 165 | assert_string_equal("argument", word); |
| 166 | assert_null(buf); |
| 167 | assert_int_equal(8, len); |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 168 | |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 169 | 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] | 170 | 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] | 171 | assert_string_equal("argument", word); |
| 172 | assert_ptr_equal(buf, word); |
| 173 | assert_int_equal(8, len); |
| 174 | free(word); |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 175 | |
| 176 | str = p = " this is one line comment on last line"; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 177 | assert_int_equal(LY_SUCCESS, skip_comment(&ctx, &str, 1)); |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 178 | assert_true(str[0] == '\0'); |
| 179 | |
| 180 | str = p = " this is a not terminated comment x"; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 181 | assert_int_equal(LY_EVALID, skip_comment(&ctx, &str, 2)); |
| 182 | logbuf_assert("Unexpected end-of-file, non-terminated comment. Line number 5."); |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 183 | assert_true(str[0] == '\0'); |
| 184 | } |
| 185 | |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 186 | static void |
| 187 | test_arg(void **state) |
| 188 | { |
| 189 | (void) state; /* unused */ |
| 190 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 191 | struct ly_parser_ctx ctx; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 192 | const char *str; |
| 193 | char *word, *buf; |
| 194 | size_t len; |
| 195 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 196 | ctx.ctx = NULL; |
| 197 | ctx.line = 1; |
| 198 | |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 199 | /* missing argument */ |
| 200 | str = ";"; |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 201 | 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] | 202 | assert_null(word); |
| 203 | |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 204 | str = "{"; |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 205 | 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] | 206 | logbuf_assert("Invalid character sequence \"{\", expected an argument. Line number 1."); |
| 207 | |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 208 | /* invalid escape sequence */ |
| 209 | str = "\"\\s\""; |
| 210 | 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] | 211 | logbuf_assert("Double-quoted string unknown special character \'\\s\'. Line number 1."); |
| 212 | str = "\'\\s\'"; /* valid, since it is not an escape sequence in single quoted string */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 213 | 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] | 214 | assert_int_equal(2, len); |
| 215 | assert_string_equal("\\s\'", word); |
| 216 | assert_int_equal('\0', str[0]); /* input has been eaten */ |
| 217 | |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 218 | /* invalid character after the argument */ |
| 219 | str = "hello\""; |
| 220 | assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len)); |
| 221 | logbuf_assert("Invalid character sequence \"\"\", expected unquoted string character, optsep, semicolon or opening brace. Line number 1."); |
| 222 | str = "hello}"; |
| 223 | assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len)); |
| 224 | logbuf_assert("Invalid character sequence \"}\", expected unquoted string character, optsep, semicolon or opening brace. Line number 1."); |
| 225 | |
| 226 | str = "hello/x\t"; /* slash is not an invalid character */ |
| 227 | assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len)); |
| 228 | assert_int_equal(7, len); |
| 229 | assert_string_equal("hello/x\t", word); |
| 230 | |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 231 | assert_null(buf); |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 232 | |
| 233 | /* different quoting */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 234 | str = "hello "; |
| 235 | 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] | 236 | assert_null(buf); |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 237 | assert_int_equal(5, len); |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 238 | assert_string_equal("hello ", word); |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 239 | |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 240 | str = "hello/*comment*/\n"; |
| 241 | 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] | 242 | assert_null(buf); |
| 243 | assert_int_equal(5, len); |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 244 | assert_false(strncmp("hello", word, len)); |
| 245 | |
| 246 | |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 247 | str = "\"hello\\n\\t\\\"\\\\\";"; |
| 248 | 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] | 249 | assert_null(buf); |
| 250 | assert_int_equal(9, len); |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 251 | assert_string_equal("hello\\n\\t\\\"\\\\\";", word); |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 252 | |
| 253 | ctx.indent = 14; |
| 254 | str = "\"hello \t\n\t\t world!\""; |
| 255 | /* - space and tabs before newline are stripped out |
| 256 | * - space and tabs after newline (indentation) are stripped out |
| 257 | */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 258 | 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] | 259 | assert_non_null(buf); |
| 260 | assert_ptr_equal(word, buf); |
| 261 | assert_int_equal(14, len); |
| 262 | assert_string_equal("hello\n world!", word); |
| 263 | free(buf); |
| 264 | |
| 265 | ctx.indent = 14; |
| 266 | str = "\"hello\n \tworld!\""; |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 267 | 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] | 268 | assert_non_null(buf); |
| 269 | assert_ptr_equal(word, buf); |
| 270 | assert_int_equal(12, len); |
| 271 | assert_string_equal("hello\nworld!", word); |
| 272 | free(buf); |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 273 | |
| 274 | str = "\'hello\'"; |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 275 | 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] | 276 | assert_null(buf); |
| 277 | assert_int_equal(5, len); |
| 278 | assert_false(strncmp("hello", word, 5)); |
| 279 | |
| 280 | str = "\"hel\" +\t\n\"lo\""; |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 281 | 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] | 282 | assert_ptr_equal(word, buf); |
| 283 | assert_int_equal(5, len); |
| 284 | assert_string_equal("hello", word); |
| 285 | free(buf); |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 286 | str = "\"hel\" +\t\nlo"; /* unquoted the second part */ |
| 287 | assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len)); |
| 288 | logbuf_assert("Both string parts divided by '+' must be quoted. Line number 5."); |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 289 | |
| 290 | str = "\'he\'\t\n+ \"llo\""; |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 291 | 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] | 292 | assert_ptr_equal(word, buf); |
| 293 | assert_int_equal(5, len); |
| 294 | assert_string_equal("hello", word); |
| 295 | free(buf); |
| 296 | |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 297 | str = " \t\n\"he\"+\'llo\'"; |
| 298 | 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] | 299 | assert_ptr_equal(word, buf); |
| 300 | assert_int_equal(5, len); |
| 301 | assert_string_equal("hello", word); |
| 302 | free(buf); |
| 303 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 304 | /* missing argument */ |
| 305 | str = ";"; |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 306 | assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len)); |
| 307 | logbuf_assert("Invalid character sequence \";\", expected an argument. Line number 7."); |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | static void |
| 311 | test_stmts(void **state) |
| 312 | { |
| 313 | (void) state; /* unused */ |
| 314 | |
| 315 | struct ly_parser_ctx ctx; |
| 316 | const char *str, *p; |
| 317 | enum yang_keyword kw; |
| 318 | char *word; |
| 319 | size_t len; |
| 320 | |
| 321 | ctx.ctx = NULL; |
| 322 | ctx.line = 1; |
| 323 | |
| 324 | str = "\n// comment\n\tinput\t{"; |
| 325 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 326 | assert_int_equal(YANG_INPUT, kw); |
| 327 | assert_int_equal(5, len); |
| 328 | assert_string_equal("input\t{", word); |
| 329 | assert_string_equal("\t{", str); |
| 330 | |
| 331 | str = "\t /* comment */\t output\n\t{"; |
| 332 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 333 | assert_int_equal(YANG_OUTPUT, kw); |
| 334 | assert_int_equal(6, len); |
| 335 | assert_string_equal("output\n\t{", word); |
| 336 | assert_string_equal("\n\t{", str); |
| 337 | |
| 338 | str = "/input { "; /* invalid slash */ |
| 339 | assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 340 | logbuf_assert("Invalid identifier first character '/'. Line number 4."); |
| 341 | |
| 342 | str = "not-a-statement-nor-extension { "; /* invalid identifier */ |
| 343 | assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 344 | logbuf_assert("Invalid character sequence \"not-a-statement-nor-extension\", expected a keyword. Line number 4."); |
| 345 | |
| 346 | str = "path;"; /* missing sep after the keyword */ |
| 347 | assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 348 | logbuf_assert("Invalid character sequence \"path;\", expected a keyword followed by a separator. Line number 4."); |
| 349 | |
| 350 | str = "action "; |
| 351 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 352 | assert_int_equal(YANG_ACTION, kw); |
| 353 | assert_int_equal(6, len); |
| 354 | str = "anydata "; |
| 355 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 356 | assert_int_equal(YANG_ANYDATA, kw); |
| 357 | assert_int_equal(7, len); |
| 358 | str = "anyxml "; |
| 359 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 360 | assert_int_equal(YANG_ANYXML, kw); |
| 361 | assert_int_equal(6, len); |
| 362 | str = "argument "; |
| 363 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 364 | assert_int_equal(YANG_ARGUMENT, kw); |
| 365 | assert_int_equal(8, len); |
| 366 | str = "augment "; |
| 367 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 368 | assert_int_equal(YANG_AUGMENT, kw); |
| 369 | assert_int_equal(7, len); |
| 370 | str = "base "; |
| 371 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 372 | assert_int_equal(YANG_BASE, kw); |
| 373 | assert_int_equal(4, len); |
| 374 | str = "belongs-to "; |
| 375 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 376 | assert_int_equal(YANG_BELONGS_TO, kw); |
| 377 | assert_int_equal(10, len); |
| 378 | str = "bit "; |
| 379 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 380 | assert_int_equal(YANG_BIT, kw); |
| 381 | assert_int_equal(3, len); |
| 382 | str = "case "; |
| 383 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 384 | assert_int_equal(YANG_CASE, kw); |
| 385 | assert_int_equal(4, len); |
| 386 | str = "choice "; |
| 387 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 388 | assert_int_equal(YANG_CHOICE, kw); |
| 389 | assert_int_equal(6, len); |
| 390 | str = "config "; |
| 391 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 392 | assert_int_equal(YANG_CONFIG, kw); |
| 393 | assert_int_equal(6, len); |
| 394 | str = "contact "; |
| 395 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 396 | assert_int_equal(YANG_CONTACT, kw); |
| 397 | assert_int_equal(7, len); |
| 398 | str = "container "; |
| 399 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 400 | assert_int_equal(YANG_CONTAINER, kw); |
| 401 | assert_int_equal(9, len); |
| 402 | str = "default "; |
| 403 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 404 | assert_int_equal(YANG_DEFAULT, kw); |
| 405 | assert_int_equal(7, len); |
| 406 | str = "description "; |
| 407 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 408 | assert_int_equal(YANG_DESCRIPTION, kw); |
| 409 | assert_int_equal(11, len); |
| 410 | str = "deviate "; |
| 411 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 412 | assert_int_equal(YANG_DEVIATE, kw); |
| 413 | assert_int_equal(7, len); |
| 414 | str = "deviation "; |
| 415 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 416 | assert_int_equal(YANG_DEVIATION, kw); |
| 417 | assert_int_equal(9, len); |
| 418 | str = "enum "; |
| 419 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 420 | assert_int_equal(YANG_ENUM, kw); |
| 421 | assert_int_equal(4, len); |
| 422 | str = "error-app-tag "; |
| 423 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 424 | assert_int_equal(YANG_ERROR_APP_TAG, kw); |
| 425 | assert_int_equal(13, len); |
| 426 | str = "error-message "; |
| 427 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 428 | assert_int_equal(YANG_ERROR_MESSAGE, kw); |
| 429 | assert_int_equal(13, len); |
| 430 | str = "extension "; |
| 431 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 432 | assert_int_equal(YANG_EXTENSION, kw); |
| 433 | assert_int_equal(9, len); |
| 434 | str = "feature "; |
| 435 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 436 | assert_int_equal(YANG_FEATURE, kw); |
| 437 | assert_int_equal(7, len); |
| 438 | str = "fraction-digits "; |
| 439 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 440 | assert_int_equal(YANG_FRACTION_DIGITS, kw); |
| 441 | assert_int_equal(15, len); |
| 442 | str = "grouping "; |
| 443 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 444 | assert_int_equal(YANG_GROUPING, kw); |
| 445 | assert_int_equal(8, len); |
| 446 | str = "identity "; |
| 447 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 448 | assert_int_equal(YANG_IDENTITY, kw); |
| 449 | assert_int_equal(8, len); |
| 450 | str = "if-feature "; |
| 451 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 452 | assert_int_equal(YANG_IF_FEATURE, kw); |
| 453 | assert_int_equal(10, len); |
| 454 | str = "import "; |
| 455 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 456 | assert_int_equal(YANG_IMPORT, kw); |
| 457 | assert_int_equal(6, len); |
| 458 | str = "include "; |
| 459 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 460 | assert_int_equal(YANG_INCLUDE, kw); |
| 461 | assert_int_equal(7, len); |
| 462 | str = "input{"; |
| 463 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 464 | assert_int_equal(YANG_INPUT, kw); |
| 465 | assert_int_equal(5, len); |
| 466 | str = "key "; |
| 467 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 468 | assert_int_equal(YANG_KEY, kw); |
| 469 | assert_int_equal(3, len); |
| 470 | str = "leaf "; |
| 471 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 472 | assert_int_equal(YANG_LEAF, kw); |
| 473 | assert_int_equal(4, len); |
| 474 | str = "leaf-list "; |
| 475 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 476 | assert_int_equal(YANG_LEAF_LIST, kw); |
| 477 | assert_int_equal(9, len); |
| 478 | str = "length "; |
| 479 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 480 | assert_int_equal(YANG_LENGTH, kw); |
| 481 | assert_int_equal(6, len); |
| 482 | str = "list "; |
| 483 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 484 | assert_int_equal(YANG_LIST, kw); |
| 485 | assert_int_equal(4, len); |
| 486 | str = "mandatory "; |
| 487 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 488 | assert_int_equal(YANG_MANDATORY, kw); |
| 489 | assert_int_equal(9, len); |
| 490 | str = "max-elements "; |
| 491 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 492 | assert_int_equal(YANG_MAX_ELEMENTS, kw); |
| 493 | assert_int_equal(12, len); |
| 494 | str = "min-elements "; |
| 495 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 496 | assert_int_equal(YANG_MIN_ELEMENTS, kw); |
| 497 | assert_int_equal(12, len); |
| 498 | str = "modifier "; |
| 499 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 500 | assert_int_equal(YANG_MODIFIER, kw); |
| 501 | assert_int_equal(8, len); |
| 502 | str = "module "; |
| 503 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 504 | assert_int_equal(YANG_MODULE, kw); |
| 505 | assert_int_equal(6, len); |
| 506 | str = "must "; |
| 507 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 508 | assert_int_equal(YANG_MUST, kw); |
| 509 | assert_int_equal(4, len); |
| 510 | str = "namespace "; |
| 511 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 512 | assert_int_equal(YANG_NAMESPACE, kw); |
| 513 | assert_int_equal(9, len); |
| 514 | str = "notification "; |
| 515 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 516 | assert_int_equal(YANG_NOTIFICATION, kw); |
| 517 | assert_int_equal(12, len); |
| 518 | str = "ordered-by "; |
| 519 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 520 | assert_int_equal(YANG_ORDERED_BY, kw); |
| 521 | assert_int_equal(10, len); |
| 522 | str = "organization "; |
| 523 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 524 | assert_int_equal(YANG_ORGANIZATION, kw); |
| 525 | assert_int_equal(12, len); |
| 526 | str = "output "; |
| 527 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 528 | assert_int_equal(YANG_OUTPUT, kw); |
| 529 | assert_int_equal(6, len); |
| 530 | str = "path "; |
| 531 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 532 | assert_int_equal(YANG_PATH, kw); |
| 533 | assert_int_equal(4, len); |
| 534 | str = "pattern "; |
| 535 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 536 | assert_int_equal(YANG_PATTERN, kw); |
| 537 | assert_int_equal(7, len); |
| 538 | str = "position "; |
| 539 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 540 | assert_int_equal(YANG_POSITION, kw); |
| 541 | assert_int_equal(8, len); |
| 542 | str = "prefix "; |
| 543 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 544 | assert_int_equal(YANG_PREFIX, kw); |
| 545 | assert_int_equal(6, len); |
| 546 | str = "presence "; |
| 547 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 548 | assert_int_equal(YANG_PRESENCE, kw); |
| 549 | assert_int_equal(8, len); |
| 550 | str = "range "; |
| 551 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 552 | assert_int_equal(YANG_RANGE, kw); |
| 553 | assert_int_equal(5, len); |
| 554 | str = "reference "; |
| 555 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 556 | assert_int_equal(YANG_REFERENCE, kw); |
| 557 | assert_int_equal(9, len); |
| 558 | str = "refine "; |
| 559 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 560 | assert_int_equal(YANG_REFINE, kw); |
| 561 | assert_int_equal(6, len); |
| 562 | str = "require-instance "; |
| 563 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 564 | assert_int_equal(YANG_REQUIRE_INSTANCE, kw); |
| 565 | assert_int_equal(16, len); |
| 566 | str = "revision "; |
| 567 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 568 | assert_int_equal(YANG_REVISION, kw); |
| 569 | assert_int_equal(8, len); |
| 570 | str = "revision-date "; |
| 571 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 572 | assert_int_equal(YANG_REVISION_DATE, kw); |
| 573 | assert_int_equal(13, len); |
| 574 | str = "rpc "; |
| 575 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 576 | assert_int_equal(YANG_RPC, kw); |
| 577 | assert_int_equal(3, len); |
| 578 | str = "status "; |
| 579 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 580 | assert_int_equal(YANG_STATUS, kw); |
| 581 | assert_int_equal(6, len); |
| 582 | str = "submodule "; |
| 583 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 584 | assert_int_equal(YANG_SUBMODULE, kw); |
| 585 | assert_int_equal(9, len); |
| 586 | str = "type "; |
| 587 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 588 | assert_int_equal(YANG_TYPE, kw); |
| 589 | assert_int_equal(4, len); |
| 590 | str = "typedef "; |
| 591 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 592 | assert_int_equal(YANG_TYPEDEF, kw); |
| 593 | assert_int_equal(7, len); |
| 594 | str = "unique "; |
| 595 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 596 | assert_int_equal(YANG_UNIQUE, kw); |
| 597 | assert_int_equal(6, len); |
| 598 | str = "units "; |
| 599 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 600 | assert_int_equal(YANG_UNITS, kw); |
| 601 | assert_int_equal(5, len); |
| 602 | str = "uses "; |
| 603 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 604 | assert_int_equal(YANG_USES, kw); |
| 605 | assert_int_equal(4, len); |
| 606 | str = "value "; |
| 607 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 608 | assert_int_equal(YANG_VALUE, kw); |
| 609 | assert_int_equal(5, len); |
| 610 | str = "when "; |
| 611 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 612 | assert_int_equal(YANG_WHEN, kw); |
| 613 | assert_int_equal(4, len); |
| 614 | str = "yang-version "; |
| 615 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 616 | assert_int_equal(YANG_YANG_VERSION, kw); |
| 617 | assert_int_equal(12, len); |
| 618 | str = "yin-element "; |
| 619 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 620 | assert_int_equal(YANG_YIN_ELEMENT, kw); |
| 621 | assert_int_equal(11, len); |
Radek Krejci | 626df48 | 2018-10-11 15:06:31 +0200 | [diff] [blame] | 622 | str = ";config false;"; |
| 623 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 624 | assert_int_equal(YANG_SEMICOLON, kw); |
| 625 | assert_int_equal(1, len); |
| 626 | assert_string_equal("config false;", str); |
| 627 | str = "{ config false;"; |
| 628 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 629 | assert_int_equal(YANG_LEFT_BRACE, kw); |
| 630 | assert_int_equal(1, len); |
| 631 | assert_string_equal(" config false;", str); |
| 632 | str = "}"; |
| 633 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 634 | assert_int_equal(YANG_RIGHT_BRACE, kw); |
| 635 | assert_int_equal(1, len); |
| 636 | assert_string_equal("", str); |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 637 | |
| 638 | /* geenric extension */ |
| 639 | str = p = "nacm:default-deny-write;"; |
| 640 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 641 | assert_int_equal(YANG_CUSTOM, kw); |
| 642 | assert_int_equal(23, len); |
| 643 | assert_ptr_equal(p, word); |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 644 | } |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 645 | |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 646 | static struct lysp_module * |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 647 | 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] | 648 | { |
| 649 | lysp_module_free(mod); |
| 650 | mod = calloc(1, sizeof *mod); |
| 651 | mod->ctx = ctx->ctx; |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 652 | mod->submodule = submodule; |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 653 | assert_non_null(mod); |
| 654 | return mod; |
| 655 | } |
| 656 | |
| 657 | static void |
| 658 | test_module(void **state) |
| 659 | { |
| 660 | (void) state; /* unused */ |
| 661 | |
| 662 | struct ly_parser_ctx ctx; |
| 663 | struct lysp_module *mod; |
| 664 | const char *str; |
| 665 | |
| 666 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 667 | assert_non_null(ctx.ctx); |
| 668 | ctx.line = 1; |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 669 | ctx.indent = 0; |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 670 | |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 671 | mod = mod_renew(&ctx, NULL, 0); |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 672 | |
| 673 | /* missing mandatory substatements */ |
| 674 | str = " name {}"; |
| 675 | assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod)); |
| 676 | assert_string_equal("name", mod->name); |
| 677 | logbuf_assert("Missing mandatory keyword \"namespace\" as a child of \"module\". Line number 1."); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 678 | mod = mod_renew(&ctx, mod, 0); |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 679 | |
| 680 | str = " name {namespace urn:x;}"; |
| 681 | assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod)); |
| 682 | assert_string_equal("urn:x", mod->ns); |
| 683 | logbuf_assert("Missing mandatory keyword \"prefix\" as a child of \"module\". Line number 1."); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 684 | mod = mod_renew(&ctx, mod, 0); |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 685 | |
| 686 | str = " name {namespace urn:x;prefix \"x\";}"; |
| 687 | assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod)); |
| 688 | assert_string_equal("x", mod->prefix); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 689 | mod = mod_renew(&ctx, mod, 0); |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 690 | |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 691 | #define SCHEMA_BEGINNING " name {namespace urn:x;prefix \"x\";" |
| 692 | #define TEST_NODE(NODETYPE, INPUT, NAME) \ |
| 693 | str = SCHEMA_BEGINNING INPUT; \ |
| 694 | assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod)); \ |
| 695 | assert_non_null(mod->data); \ |
| 696 | assert_int_equal(NODETYPE, mod->data->nodetype); \ |
| 697 | assert_string_equal(NAME, mod->data->name); \ |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 698 | mod = mod_renew(&ctx, mod, 0); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 699 | #define TEST_GENERIC(INPUT, TARGET, TEST) \ |
| 700 | str = SCHEMA_BEGINNING INPUT; \ |
| 701 | assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod)); \ |
| 702 | assert_non_null(TARGET); \ |
| 703 | TEST; \ |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 704 | mod = mod_renew(&ctx, mod, 0); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 705 | #define TEST_DUP(MEMBER, VALUE1, VALUE2, LINE, SUBMODULE) \ |
| 706 | TEST_DUP_GENERIC(SCHEMA_BEGINNING, MEMBER, VALUE1, VALUE2, \ |
| 707 | parse_sub_module, mod, LINE, mod = mod_renew(&ctx, mod, SUBMODULE)) |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 708 | |
| 709 | /* duplicated namespace, prefix */ |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 710 | TEST_DUP("namespace", "y", "z", "1", 0); |
| 711 | TEST_DUP("prefix", "y", "z", "1", 0); |
| 712 | TEST_DUP("contact", "a", "b", "1", 0); |
| 713 | TEST_DUP("description", "a", "b", "1", 0); |
| 714 | TEST_DUP("organization", "a", "b", "1", 0); |
| 715 | TEST_DUP("reference", "a", "b", "1", 0); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 716 | |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 717 | /* not allowed in module (submodule-specific) */ |
| 718 | str = SCHEMA_BEGINNING "belongs-to master {prefix m;}}"; |
| 719 | assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod)); |
| 720 | logbuf_assert("Invalid keyword \"belongs-to\" as a child of \"module\". Line number 1."); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 721 | mod = mod_renew(&ctx, mod, 0); |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 722 | |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 723 | /* anydata */ |
| 724 | TEST_NODE(LYS_ANYDATA, "anydata test;}", "test"); |
| 725 | /* anyxml */ |
| 726 | TEST_NODE(LYS_ANYXML, "anyxml test;}", "test"); |
| 727 | /* augment */ |
| 728 | TEST_GENERIC("augment /somepath;}", mod->augments, |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame^] | 729 | 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] | 730 | /* choice */ |
| 731 | TEST_NODE(LYS_CHOICE, "choice test;}", "test"); |
| 732 | /* contact 0..1 */ |
| 733 | TEST_GENERIC("contact \"firstname\" + \n\t\" surname\";}", mod->contact, |
| 734 | assert_string_equal("firstname surname", mod->contact)); |
| 735 | /* container */ |
| 736 | TEST_NODE(LYS_CONTAINER, "container test;}", "test"); |
| 737 | /* description 0..1 */ |
| 738 | TEST_GENERIC("description \'some description\';}", mod->dsc, |
| 739 | assert_string_equal("some description", mod->dsc)); |
| 740 | /* deviation */ |
| 741 | TEST_GENERIC("deviation /somepath {deviate not-supported;}}", mod->deviations, |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame^] | 742 | 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] | 743 | /* extension */ |
| 744 | TEST_GENERIC("extension test;}", mod->extensions, |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame^] | 745 | 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] | 746 | /* feature */ |
| 747 | TEST_GENERIC("feature test;}", mod->features, |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame^] | 748 | 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] | 749 | /* grouping */ |
| 750 | TEST_GENERIC("grouping grp;}", mod->groupings, |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame^] | 751 | 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] | 752 | /* identity */ |
| 753 | TEST_GENERIC("identity test;}", mod->identities, |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame^] | 754 | 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] | 755 | /* import */ |
| 756 | TEST_GENERIC("import test {prefix z;}}", mod->imports, |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame^] | 757 | 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] | 758 | |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 759 | /* import - prefix collision */ |
| 760 | str = SCHEMA_BEGINNING "import test {prefix x;}}"; |
| 761 | assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod)); |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 762 | logbuf_assert("Prefix \"x\" already used as module prefix. Line number 2."); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 763 | mod = mod_renew(&ctx, mod, 0); |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 764 | |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 765 | /* include */ |
Radek Krejci | 469f70d | 2018-10-15 15:27:29 +0200 | [diff] [blame] | 766 | TEST_GENERIC("include test;}", mod->includes, |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame^] | 767 | 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] | 768 | /* leaf */ |
| 769 | TEST_NODE(LYS_LEAF, "leaf test {type string;}}", "test"); |
| 770 | /* leaf-list */ |
| 771 | TEST_NODE(LYS_LEAFLIST, "leaf-list test {type string;}}", "test"); |
| 772 | /* list */ |
| 773 | TEST_NODE(LYS_LIST, "list test {key a;leaf a {type string;}}}", "test"); |
| 774 | /* notification */ |
| 775 | TEST_GENERIC("notification test;}", mod->notifs, |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame^] | 776 | 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] | 777 | /* organization 0..1 */ |
| 778 | TEST_GENERIC("organization \"CESNET a.l.e.\";}", mod->org, |
| 779 | assert_string_equal("CESNET a.l.e.", mod->org)); |
| 780 | /* reference 0..1 */ |
| 781 | TEST_GENERIC("reference RFC7950;}", mod->ref, |
| 782 | assert_string_equal("RFC7950", mod->ref)); |
| 783 | /* revision */ |
| 784 | TEST_GENERIC("revision 2018-10-12;}", mod->revs, |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame^] | 785 | 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] | 786 | /* rpc */ |
| 787 | TEST_GENERIC("rpc test;}", mod->rpcs, |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame^] | 788 | 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] | 789 | /* typedef */ |
| 790 | TEST_GENERIC("typedef test{type string;}}", mod->typedefs, |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame^] | 791 | 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] | 792 | /* uses */ |
| 793 | TEST_NODE(LYS_USES, "uses test;}", "test"); |
| 794 | /* yang-version */ |
| 795 | str = SCHEMA_BEGINNING "\n\tyang-version 10;}"; |
| 796 | assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod)); |
| 797 | logbuf_assert("Invalid value \"10\" of \"yang-version\". Line number 3."); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 798 | mod = mod_renew(&ctx, mod, 0); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 799 | str = SCHEMA_BEGINNING "yang-version 1.0;yang-version 1.1;}"; |
| 800 | assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod)); |
| 801 | logbuf_assert("Duplicate keyword \"yang-version\". Line number 3."); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 802 | mod = mod_renew(&ctx, mod, 0); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 803 | str = SCHEMA_BEGINNING "yang-version 1.0;}"; |
| 804 | assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod)); |
| 805 | assert_int_equal(1, mod->version); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 806 | mod = mod_renew(&ctx, mod, 0); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 807 | str = SCHEMA_BEGINNING "yang-version \"1.1\";}"; |
| 808 | assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod)); |
| 809 | assert_int_equal(2, mod->version); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 810 | mod = mod_renew(&ctx, mod, 0); |
| 811 | |
Radek Krejci | 156ccaf | 2018-10-15 15:49:17 +0200 | [diff] [blame] | 812 | /* extensions */ |
| 813 | TEST_GENERIC("prefix:test;}", mod->exts, |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame^] | 814 | assert_string_equal("prefix:test", LY_ARRAY_INDEX(mod->exts, 0, struct lysp_ext_instance)->name); |
| 815 | 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] | 816 | mod = mod_renew(&ctx, mod, 0); |
| 817 | |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 818 | /* invalid substatement */ |
| 819 | str = SCHEMA_BEGINNING "must false;}"; |
| 820 | assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod)); |
| 821 | logbuf_assert("Invalid keyword \"must\" as a child of \"module\". Line number 3."); |
| 822 | mod = mod_renew(&ctx, mod, 0); |
| 823 | |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 824 | /* submodule */ |
| 825 | mod->submodule = 1; |
| 826 | |
| 827 | /* missing mandatory substatements */ |
| 828 | str = " subname {}"; |
| 829 | assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod)); |
| 830 | assert_string_equal("subname", mod->name); |
| 831 | logbuf_assert("Missing mandatory keyword \"belongs-to\" as a child of \"submodule\". Line number 3."); |
| 832 | mod = mod_renew(&ctx, mod, 1); |
| 833 | |
| 834 | str = " subname {belongs-to name;}"; |
| 835 | assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod)); |
| 836 | assert_string_equal("name", mod->belongsto); |
| 837 | mod = mod_renew(&ctx, mod, 1); |
| 838 | |
| 839 | #undef SCHEMA_BEGINNING |
| 840 | #define SCHEMA_BEGINNING " subname {belongs-to name;" |
| 841 | |
| 842 | /* duplicated namespace, prefix */ |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 843 | TEST_DUP("belongs-to", "module1", "module2", "3", 1); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 844 | |
| 845 | /* not allowed in submodule (module-specific) */ |
| 846 | str = SCHEMA_BEGINNING "namespace \"urn:z\";}"; |
| 847 | assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod)); |
| 848 | logbuf_assert("Invalid keyword \"namespace\" as a child of \"submodule\". Line number 3."); |
| 849 | mod = mod_renew(&ctx, mod, 1); |
| 850 | str = SCHEMA_BEGINNING "prefix m;}}"; |
| 851 | assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod)); |
| 852 | logbuf_assert("Invalid keyword \"prefix\" as a child of \"submodule\". Line number 3."); |
| 853 | mod = mod_renew(&ctx, mod, 1); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 854 | |
| 855 | #undef TEST_GENERIC |
| 856 | #undef TEST_NODE |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 857 | #undef TEST_DUP |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 858 | #undef SCHEMA_BEGINNING |
| 859 | |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 860 | lysp_module_free(mod); |
| 861 | ly_ctx_destroy(ctx.ctx, NULL); |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 862 | } |
| 863 | |
Radek Krejci | 4c6d9bd | 2018-10-15 16:43:06 +0200 | [diff] [blame] | 864 | static void |
| 865 | test_identity(void **state) |
| 866 | { |
| 867 | (void) state; /* unused */ |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 868 | int dict = 1; /* magic variable for FREE macros */ |
Radek Krejci | 4c6d9bd | 2018-10-15 16:43:06 +0200 | [diff] [blame] | 869 | |
| 870 | struct ly_parser_ctx ctx; |
| 871 | struct lysp_ident *ident = NULL; |
| 872 | const char *str; |
Radek Krejci | 4c6d9bd | 2018-10-15 16:43:06 +0200 | [diff] [blame] | 873 | |
| 874 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 875 | assert_non_null(ctx.ctx); |
| 876 | ctx.line = 1; |
| 877 | ctx.indent = 0; |
| 878 | |
| 879 | /* invalid cardinality */ |
| 880 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 881 | TEST_DUP_GENERIC(" test {", MEMBER, VALUE1, VALUE2, parse_identity, \ |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame^] | 882 | &ident, "1", FREE_ARRAY(ctx.ctx, ident, lysp_ident_free); ident = NULL) |
Radek Krejci | 4c6d9bd | 2018-10-15 16:43:06 +0200 | [diff] [blame] | 883 | |
| 884 | TEST_DUP("description", "a", "b"); |
| 885 | TEST_DUP("reference", "a", "b"); |
| 886 | TEST_DUP("status", "current", "obsolete"); |
| 887 | |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 888 | /* full content */ |
| 889 | 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] | 890 | assert_int_equal(LY_SUCCESS, parse_identity(&ctx, &str, &ident)); |
| 891 | assert_non_null(ident); |
| 892 | assert_string_equal(" ...", str); |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame^] | 893 | FREE_ARRAY(ctx.ctx, ident, lysp_ident_free); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 894 | ident = NULL; |
| 895 | |
| 896 | /* invalid substatement */ |
| 897 | str = " test {organization XXX;}"; |
| 898 | assert_int_equal(LY_EVALID, parse_identity(&ctx, &str, &ident)); |
| 899 | 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^] | 900 | FREE_ARRAY(ctx.ctx, ident, lysp_ident_free); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 901 | ident = NULL; |
| 902 | |
| 903 | #undef TEST_DUP |
| 904 | |
| 905 | ly_ctx_destroy(ctx.ctx, NULL); |
| 906 | } |
| 907 | |
| 908 | static void |
| 909 | test_feature(void **state) |
| 910 | { |
| 911 | (void) state; /* unused */ |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 912 | int dict = 1; /* magic variable for FREE macros */ |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 913 | |
| 914 | struct ly_parser_ctx ctx; |
| 915 | struct lysp_feature *features = NULL; |
| 916 | const char *str; |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 917 | |
| 918 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 919 | assert_non_null(ctx.ctx); |
| 920 | ctx.line = 1; |
| 921 | ctx.indent = 0; |
| 922 | |
| 923 | /* invalid cardinality */ |
| 924 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
| 925 | TEST_DUP_GENERIC(" test {", MEMBER, VALUE1, VALUE2, parse_feature, \ |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame^] | 926 | &features, "1", FREE_ARRAY(ctx.ctx, features, lysp_feature_free); features = NULL) |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 927 | |
| 928 | TEST_DUP("description", "a", "b"); |
| 929 | TEST_DUP("reference", "a", "b"); |
| 930 | TEST_DUP("status", "current", "obsolete"); |
| 931 | |
| 932 | /* full content */ |
| 933 | str = " test {description text;reference \'another text\';status current; if-feature x;if-feature y;prefix:ext;} ..."; |
| 934 | assert_int_equal(LY_SUCCESS, parse_feature(&ctx, &str, &features)); |
| 935 | assert_non_null(features); |
| 936 | assert_string_equal(" ...", str); |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame^] | 937 | FREE_ARRAY(ctx.ctx, features, lysp_feature_free); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 938 | features = NULL; |
| 939 | |
| 940 | /* invalid substatement */ |
| 941 | str = " test {organization XXX;}"; |
| 942 | assert_int_equal(LY_EVALID, parse_feature(&ctx, &str, &features)); |
| 943 | 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^] | 944 | FREE_ARRAY(ctx.ctx, features, lysp_feature_free); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 945 | features = NULL; |
| 946 | |
| 947 | #undef TEST_DUP |
| 948 | |
| 949 | ly_ctx_destroy(ctx.ctx, NULL); |
| 950 | } |
| 951 | |
| 952 | static void |
| 953 | test_deviation(void **state) |
| 954 | { |
| 955 | (void) state; /* unused */ |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 956 | int dict = 1; /* magic variable for FREE macros */ |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 957 | |
| 958 | struct ly_parser_ctx ctx; |
| 959 | struct lysp_deviation *d = NULL; |
| 960 | const char *str; |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 961 | |
| 962 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 963 | assert_non_null(ctx.ctx); |
| 964 | ctx.line = 1; |
| 965 | ctx.indent = 0; |
| 966 | |
| 967 | /* invalid cardinality */ |
| 968 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
| 969 | TEST_DUP_GENERIC(" test {deviate not-supported;", MEMBER, VALUE1, VALUE2, parse_deviation, \ |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame^] | 970 | &d, "1", FREE_ARRAY(ctx.ctx, d, lysp_deviation_free); d = NULL) |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 971 | |
| 972 | TEST_DUP("description", "a", "b"); |
| 973 | TEST_DUP("reference", "a", "b"); |
| 974 | |
| 975 | /* full content */ |
| 976 | str = " test {deviate not-supported;description text;reference \'another text\';prefix:ext;} ..."; |
| 977 | assert_int_equal(LY_SUCCESS, parse_deviation(&ctx, &str, &d)); |
| 978 | assert_non_null(d); |
| 979 | assert_string_equal(" ...", str); |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame^] | 980 | FREE_ARRAY(ctx.ctx, d, lysp_deviation_free); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 981 | d = NULL; |
| 982 | |
| 983 | /* missing mandatory substatement */ |
| 984 | str = " test {description text;}"; |
| 985 | assert_int_equal(LY_EVALID, parse_deviation(&ctx, &str, &d)); |
| 986 | 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^] | 987 | FREE_ARRAY(ctx.ctx, d, lysp_deviation_free); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 988 | d = NULL; |
| 989 | |
| 990 | /* invalid substatement */ |
| 991 | str = " test {deviate not-supported; status obsolete;}"; |
| 992 | assert_int_equal(LY_EVALID, parse_deviation(&ctx, &str, &d)); |
| 993 | 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^] | 994 | FREE_ARRAY(ctx.ctx, d, lysp_deviation_free); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 995 | d = NULL; |
| 996 | |
| 997 | #undef TEST_DUP |
| 998 | |
| 999 | ly_ctx_destroy(ctx.ctx, NULL); |
| 1000 | } |
| 1001 | |
| 1002 | static void |
| 1003 | test_deviate(void **state) |
| 1004 | { |
| 1005 | (void) state; /* unused */ |
| 1006 | |
| 1007 | struct ly_parser_ctx ctx; |
| 1008 | struct lysp_deviate *d = NULL; |
| 1009 | const char *str; |
| 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(TYPE, MEMBER, VALUE1, VALUE2) \ |
| 1018 | TEST_DUP_GENERIC(TYPE" {", MEMBER, VALUE1, VALUE2, parse_deviate, \ |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 1019 | &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] | 1020 | |
| 1021 | TEST_DUP("add", "config", "true", "false"); |
| 1022 | TEST_DUP("replace", "default", "int8", "uint8"); |
| 1023 | TEST_DUP("add", "mandatory", "true", "false"); |
| 1024 | TEST_DUP("add", "max-elements", "1", "2"); |
| 1025 | TEST_DUP("add", "min-elements", "1", "2"); |
| 1026 | TEST_DUP("replace", "type", "int8", "uint8"); |
| 1027 | TEST_DUP("add", "units", "kilometers", "miles"); |
| 1028 | |
| 1029 | /* full contents */ |
| 1030 | str = " not-supported {prefix:ext;} ..."; |
| 1031 | assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d)); |
| 1032 | assert_non_null(d); |
| 1033 | assert_string_equal(" ...", str); |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 1034 | lysp_deviate_free(ctx.ctx, d, 1); free(d); d = NULL; |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1035 | 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;} ..."; |
| 1036 | assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d)); |
| 1037 | assert_non_null(d); |
| 1038 | assert_string_equal(" ...", str); |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 1039 | lysp_deviate_free(ctx.ctx, d, 1); free(d); d = NULL; |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1040 | str = " delete {units meters; must 1; must 2; unique x; unique y; default a; default b; prefix:ext;} ..."; |
| 1041 | assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d)); |
| 1042 | assert_non_null(d); |
| 1043 | assert_string_equal(" ...", str); |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 1044 | lysp_deviate_free(ctx.ctx, d, 1); free(d); d = NULL; |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1045 | str = " replace {type string; units meters; default a; config true; mandatory true; min-elements 1; max-elements 2; prefix:ext;} ..."; |
| 1046 | assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d)); |
| 1047 | assert_non_null(d); |
| 1048 | assert_string_equal(" ...", str); |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 1049 | lysp_deviate_free(ctx.ctx, d, 1); free(d); d = NULL; |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1050 | |
| 1051 | /* invalid substatements */ |
| 1052 | #define TEST_NOT_SUP(DEV, STMT, VALUE) \ |
| 1053 | str = " "DEV" {"STMT" "VALUE";}..."; \ |
| 1054 | assert_int_equal(LY_EVALID, parse_deviate(&ctx, &str, &d)); \ |
| 1055 | logbuf_assert("Deviate \""DEV"\" does not support keyword \""STMT"\". Line number 1."); \ |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 1056 | lysp_deviate_free(ctx.ctx, d, 1); free(d); d = NULL |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1057 | |
| 1058 | TEST_NOT_SUP("not-supported", "units", "meters"); |
| 1059 | TEST_NOT_SUP("not-supported", "must", "1"); |
| 1060 | TEST_NOT_SUP("not-supported", "unique", "x"); |
| 1061 | TEST_NOT_SUP("not-supported", "default", "a"); |
| 1062 | TEST_NOT_SUP("not-supported", "config", "true"); |
| 1063 | TEST_NOT_SUP("not-supported", "mandatory", "true"); |
| 1064 | TEST_NOT_SUP("not-supported", "min-elements", "1"); |
| 1065 | TEST_NOT_SUP("not-supported", "max-elements", "2"); |
| 1066 | TEST_NOT_SUP("not-supported", "type", "string"); |
| 1067 | TEST_NOT_SUP("add", "type", "string"); |
| 1068 | TEST_NOT_SUP("delete", "config", "true"); |
| 1069 | TEST_NOT_SUP("delete", "mandatory", "true"); |
| 1070 | TEST_NOT_SUP("delete", "min-elements", "1"); |
| 1071 | TEST_NOT_SUP("delete", "max-elements", "2"); |
| 1072 | TEST_NOT_SUP("delete", "type", "string"); |
| 1073 | TEST_NOT_SUP("replace", "must", "1"); |
| 1074 | TEST_NOT_SUP("replace", "unique", "a"); |
| 1075 | |
| 1076 | str = " nonsence; ..."; |
| 1077 | assert_int_equal(LY_EVALID, parse_deviate(&ctx, &str, &d)); |
| 1078 | logbuf_assert("Invalid value \"nonsence\" of \"deviate\". Line number 1."); |
| 1079 | assert_null(d); |
| 1080 | |
| 1081 | #undef TEST_NOT_SUP |
| 1082 | #undef TEST_DUP |
Radek Krejci | 4c6d9bd | 2018-10-15 16:43:06 +0200 | [diff] [blame] | 1083 | |
| 1084 | ly_ctx_destroy(ctx.ctx, NULL); |
| 1085 | } |
| 1086 | |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 1087 | int main(void) |
| 1088 | { |
| 1089 | const struct CMUnitTest tests[] = { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1090 | cmocka_unit_test_setup(test_helpers, logger_setup), |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 1091 | cmocka_unit_test_setup(test_comments, logger_setup), |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1092 | cmocka_unit_test_setup(test_arg, logger_setup), |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 1093 | cmocka_unit_test_setup(test_stmts, logger_setup), |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 1094 | cmocka_unit_test_setup(test_module, logger_setup), |
Radek Krejci | 4c6d9bd | 2018-10-15 16:43:06 +0200 | [diff] [blame] | 1095 | cmocka_unit_test_setup(test_identity, logger_setup), |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1096 | cmocka_unit_test_setup(test_feature, logger_setup), |
| 1097 | cmocka_unit_test_setup(test_deviation, logger_setup), |
| 1098 | cmocka_unit_test_setup(test_deviate, logger_setup), |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 1099 | }; |
| 1100 | |
| 1101 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 1102 | } |