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" |
| 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 | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 70 | static void |
| 71 | test_helpers(void **state) |
| 72 | { |
| 73 | (void) state; /* unused */ |
| 74 | |
| 75 | const char *str; |
Radek Krejci | 404251e | 2018-10-09 12:06:44 +0200 | [diff] [blame] | 76 | char *buf, *p; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 77 | size_t len, size; |
| 78 | int prefix; |
| 79 | struct ly_parser_ctx ctx; |
| 80 | ctx.ctx = NULL; |
| 81 | ctx.line = 1; |
| 82 | |
| 83 | /* storing into buffer */ |
| 84 | str = "abcd"; |
| 85 | buf = NULL; |
| 86 | size = len = 0; |
| 87 | assert_int_equal(LY_SUCCESS, buf_add_char(NULL, &str, 2, &buf, &size, &len)); |
| 88 | assert_int_not_equal(0, size); |
| 89 | assert_int_equal(2, len); |
| 90 | assert_string_equal("cd", str); |
| 91 | assert_false(strncmp("ab", buf, 2)); |
| 92 | free(buf); |
Radek Krejci | 404251e | 2018-10-09 12:06:44 +0200 | [diff] [blame] | 93 | buf = NULL; |
| 94 | |
| 95 | /* invalid first characters */ |
| 96 | len = 0; |
| 97 | str = "2invalid"; |
| 98 | assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1)); |
| 99 | str = ".invalid"; |
| 100 | assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1)); |
| 101 | str = "-invalid"; |
| 102 | assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1)); |
| 103 | /* invalid following characters */ |
| 104 | len = 3; /* number of characters read before the str content */ |
| 105 | str = "!"; |
| 106 | assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1)); |
| 107 | str = ":"; |
| 108 | assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1)); |
| 109 | /* valid colon for prefixed identifiers */ |
| 110 | len = size = 0; |
| 111 | p = NULL; |
| 112 | str = "x:id"; |
| 113 | assert_int_equal(LY_SUCCESS, buf_store_char(&ctx, &str, Y_PREF_IDENTIF_ARG, &p, &len, &buf, &size, 0)); |
| 114 | assert_int_equal(1, len); |
| 115 | assert_null(buf); |
| 116 | assert_string_equal(":id", str); |
| 117 | assert_int_equal('x', p[len - 1]); |
| 118 | assert_int_equal(LY_SUCCESS, buf_store_char(&ctx, &str, Y_PREF_IDENTIF_ARG, &p, &len, &buf, &size, 1)); |
| 119 | assert_int_equal(2, len); |
| 120 | assert_string_equal("id", str); |
| 121 | assert_int_equal(':', p[len - 1]); |
| 122 | free(buf); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 123 | |
| 124 | /* checking identifiers */ |
| 125 | assert_int_equal(LY_EVALID, check_identifierchar(&ctx, ':', 0, NULL)); |
| 126 | logbuf_assert("Invalid identifier character ':'. Line number 1."); |
| 127 | assert_int_equal(LY_EVALID, check_identifierchar(&ctx, '#', 1, NULL)); |
| 128 | logbuf_assert("Invalid identifier first character '#'. Line number 1."); |
| 129 | |
| 130 | assert_int_equal(LY_SUCCESS, check_identifierchar(&ctx, 'a', 1, &prefix)); |
| 131 | assert_int_equal(0, prefix); |
| 132 | assert_int_equal(LY_SUCCESS, check_identifierchar(&ctx, ':', 0, &prefix)); |
| 133 | assert_int_equal(1, prefix); |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 134 | assert_int_equal(LY_EVALID, check_identifierchar(&ctx, ':', 0, &prefix)); |
| 135 | assert_int_equal(1, prefix); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 136 | assert_int_equal(LY_SUCCESS, check_identifierchar(&ctx, 'b', 0, &prefix)); |
| 137 | assert_int_equal(2, prefix); |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 138 | /* second colon is invalid */ |
| 139 | assert_int_equal(LY_EVALID, check_identifierchar(&ctx, ':', 0, &prefix)); |
| 140 | logbuf_assert("Invalid identifier character ':'. Line number 1."); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 141 | } |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 142 | |
| 143 | static void |
| 144 | test_comments(void **state) |
| 145 | { |
| 146 | (void) state; /* unused */ |
| 147 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 148 | struct ly_parser_ctx ctx; |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 149 | const char *str, *p; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 150 | char *word, *buf; |
| 151 | size_t len; |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 152 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 153 | ctx.ctx = NULL; |
| 154 | ctx.line = 1; |
| 155 | |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 156 | str = " // this is a text of / one * line */ comment\nargument"; |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 157 | 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] | 158 | assert_string_equal("argument", word); |
| 159 | assert_null(buf); |
| 160 | assert_int_equal(8, len); |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 161 | |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 162 | 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] | 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_ptr_equal(buf, word); |
| 166 | assert_int_equal(8, len); |
| 167 | free(word); |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 168 | |
| 169 | str = p = " this is one line comment on last line"; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 170 | assert_int_equal(LY_SUCCESS, skip_comment(&ctx, &str, 1)); |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 171 | assert_true(str[0] == '\0'); |
| 172 | |
| 173 | str = p = " this is a not terminated comment x"; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 174 | assert_int_equal(LY_EVALID, skip_comment(&ctx, &str, 2)); |
| 175 | logbuf_assert("Unexpected end-of-file, non-terminated comment. Line number 5."); |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 176 | assert_true(str[0] == '\0'); |
| 177 | } |
| 178 | |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 179 | static void |
| 180 | test_arg(void **state) |
| 181 | { |
| 182 | (void) state; /* unused */ |
| 183 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 184 | struct ly_parser_ctx ctx; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 185 | const char *str; |
| 186 | char *word, *buf; |
| 187 | size_t len; |
| 188 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 189 | ctx.ctx = NULL; |
| 190 | ctx.line = 1; |
| 191 | |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 192 | /* missing argument */ |
| 193 | str = ";"; |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 194 | 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] | 195 | assert_null(word); |
| 196 | |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 197 | str = "{"; |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 198 | 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] | 199 | logbuf_assert("Invalid character sequence \"{\", expected an argument. Line number 1."); |
| 200 | |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 201 | /* invalid escape sequence */ |
| 202 | str = "\"\\s\""; |
| 203 | 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] | 204 | logbuf_assert("Double-quoted string unknown special character \'\\s\'. Line number 1."); |
| 205 | str = "\'\\s\'"; /* valid, since it is not an escape sequence in single quoted string */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 206 | 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] | 207 | assert_int_equal(2, len); |
| 208 | assert_string_equal("\\s\'", word); |
| 209 | assert_int_equal('\0', str[0]); /* input has been eaten */ |
| 210 | |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 211 | /* invalid character after the argument */ |
| 212 | str = "hello\""; |
| 213 | assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len)); |
| 214 | logbuf_assert("Invalid character sequence \"\"\", expected unquoted string character, optsep, semicolon or opening brace. Line number 1."); |
| 215 | str = "hello}"; |
| 216 | assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len)); |
| 217 | logbuf_assert("Invalid character sequence \"}\", expected unquoted string character, optsep, semicolon or opening brace. Line number 1."); |
| 218 | |
| 219 | str = "hello/x\t"; /* slash is not an invalid character */ |
| 220 | assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len)); |
| 221 | assert_int_equal(7, len); |
| 222 | assert_string_equal("hello/x\t", word); |
| 223 | |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 224 | assert_null(buf); |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 225 | |
| 226 | /* different quoting */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 227 | str = "hello "; |
| 228 | 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] | 229 | assert_null(buf); |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 230 | assert_int_equal(5, len); |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 231 | assert_string_equal("hello ", word); |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 232 | |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 233 | str = "hello/*comment*/\n"; |
| 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); |
| 236 | assert_int_equal(5, len); |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 237 | assert_false(strncmp("hello", word, len)); |
| 238 | |
| 239 | |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 240 | str = "\"hello\\n\\t\\\"\\\\\";"; |
| 241 | 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] | 242 | assert_null(buf); |
| 243 | assert_int_equal(9, len); |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 244 | assert_string_equal("hello\\n\\t\\\"\\\\\";", word); |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 245 | |
| 246 | ctx.indent = 14; |
| 247 | str = "\"hello \t\n\t\t world!\""; |
| 248 | /* - space and tabs before newline are stripped out |
| 249 | * - space and tabs after newline (indentation) are stripped out |
| 250 | */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 251 | 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] | 252 | assert_non_null(buf); |
| 253 | assert_ptr_equal(word, buf); |
| 254 | assert_int_equal(14, len); |
| 255 | assert_string_equal("hello\n world!", word); |
| 256 | free(buf); |
| 257 | |
| 258 | ctx.indent = 14; |
| 259 | str = "\"hello\n \tworld!\""; |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 260 | 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] | 261 | assert_non_null(buf); |
| 262 | assert_ptr_equal(word, buf); |
| 263 | assert_int_equal(12, len); |
| 264 | assert_string_equal("hello\nworld!", word); |
| 265 | free(buf); |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 266 | |
| 267 | str = "\'hello\'"; |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 268 | 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] | 269 | assert_null(buf); |
| 270 | assert_int_equal(5, len); |
| 271 | assert_false(strncmp("hello", word, 5)); |
| 272 | |
| 273 | str = "\"hel\" +\t\n\"lo\""; |
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_ptr_equal(word, buf); |
| 276 | assert_int_equal(5, len); |
| 277 | assert_string_equal("hello", word); |
| 278 | free(buf); |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 279 | str = "\"hel\" +\t\nlo"; /* unquoted the second part */ |
| 280 | assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len)); |
| 281 | logbuf_assert("Both string parts divided by '+' must be quoted. Line number 5."); |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 282 | |
| 283 | str = "\'he\'\t\n+ \"llo\""; |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 284 | 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] | 285 | assert_ptr_equal(word, buf); |
| 286 | assert_int_equal(5, len); |
| 287 | assert_string_equal("hello", word); |
| 288 | free(buf); |
| 289 | |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 290 | str = " \t\n\"he\"+\'llo\'"; |
| 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 | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 297 | /* missing argument */ |
| 298 | str = ";"; |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 299 | assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len)); |
| 300 | logbuf_assert("Invalid character sequence \";\", expected an argument. Line number 7."); |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | static void |
| 304 | test_stmts(void **state) |
| 305 | { |
| 306 | (void) state; /* unused */ |
| 307 | |
| 308 | struct ly_parser_ctx ctx; |
| 309 | const char *str, *p; |
| 310 | enum yang_keyword kw; |
| 311 | char *word; |
| 312 | size_t len; |
| 313 | |
| 314 | ctx.ctx = NULL; |
| 315 | ctx.line = 1; |
| 316 | |
| 317 | str = "\n// comment\n\tinput\t{"; |
| 318 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 319 | assert_int_equal(YANG_INPUT, kw); |
| 320 | assert_int_equal(5, len); |
| 321 | assert_string_equal("input\t{", word); |
| 322 | assert_string_equal("\t{", str); |
| 323 | |
| 324 | str = "\t /* comment */\t output\n\t{"; |
| 325 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 326 | assert_int_equal(YANG_OUTPUT, kw); |
| 327 | assert_int_equal(6, len); |
| 328 | assert_string_equal("output\n\t{", word); |
| 329 | assert_string_equal("\n\t{", str); |
| 330 | |
| 331 | str = "/input { "; /* invalid slash */ |
| 332 | assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 333 | logbuf_assert("Invalid identifier first character '/'. Line number 4."); |
| 334 | |
| 335 | str = "not-a-statement-nor-extension { "; /* invalid identifier */ |
| 336 | assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 337 | logbuf_assert("Invalid character sequence \"not-a-statement-nor-extension\", expected a keyword. Line number 4."); |
| 338 | |
| 339 | str = "path;"; /* missing sep after the keyword */ |
| 340 | assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 341 | logbuf_assert("Invalid character sequence \"path;\", expected a keyword followed by a separator. Line number 4."); |
| 342 | |
| 343 | str = "action "; |
| 344 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 345 | assert_int_equal(YANG_ACTION, kw); |
| 346 | assert_int_equal(6, len); |
| 347 | str = "anydata "; |
| 348 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 349 | assert_int_equal(YANG_ANYDATA, kw); |
| 350 | assert_int_equal(7, len); |
| 351 | str = "anyxml "; |
| 352 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 353 | assert_int_equal(YANG_ANYXML, kw); |
| 354 | assert_int_equal(6, len); |
| 355 | str = "argument "; |
| 356 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 357 | assert_int_equal(YANG_ARGUMENT, kw); |
| 358 | assert_int_equal(8, len); |
| 359 | str = "augment "; |
| 360 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 361 | assert_int_equal(YANG_AUGMENT, kw); |
| 362 | assert_int_equal(7, len); |
| 363 | str = "base "; |
| 364 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 365 | assert_int_equal(YANG_BASE, kw); |
| 366 | assert_int_equal(4, len); |
| 367 | str = "belongs-to "; |
| 368 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 369 | assert_int_equal(YANG_BELONGS_TO, kw); |
| 370 | assert_int_equal(10, len); |
| 371 | str = "bit "; |
| 372 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 373 | assert_int_equal(YANG_BIT, kw); |
| 374 | assert_int_equal(3, len); |
| 375 | str = "case "; |
| 376 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 377 | assert_int_equal(YANG_CASE, kw); |
| 378 | assert_int_equal(4, len); |
| 379 | str = "choice "; |
| 380 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 381 | assert_int_equal(YANG_CHOICE, kw); |
| 382 | assert_int_equal(6, len); |
| 383 | str = "config "; |
| 384 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 385 | assert_int_equal(YANG_CONFIG, kw); |
| 386 | assert_int_equal(6, len); |
| 387 | str = "contact "; |
| 388 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 389 | assert_int_equal(YANG_CONTACT, kw); |
| 390 | assert_int_equal(7, len); |
| 391 | str = "container "; |
| 392 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 393 | assert_int_equal(YANG_CONTAINER, kw); |
| 394 | assert_int_equal(9, len); |
| 395 | str = "default "; |
| 396 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 397 | assert_int_equal(YANG_DEFAULT, kw); |
| 398 | assert_int_equal(7, len); |
| 399 | str = "description "; |
| 400 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 401 | assert_int_equal(YANG_DESCRIPTION, kw); |
| 402 | assert_int_equal(11, len); |
| 403 | str = "deviate "; |
| 404 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 405 | assert_int_equal(YANG_DEVIATE, kw); |
| 406 | assert_int_equal(7, len); |
| 407 | str = "deviation "; |
| 408 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 409 | assert_int_equal(YANG_DEVIATION, kw); |
| 410 | assert_int_equal(9, len); |
| 411 | str = "enum "; |
| 412 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 413 | assert_int_equal(YANG_ENUM, kw); |
| 414 | assert_int_equal(4, len); |
| 415 | str = "error-app-tag "; |
| 416 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 417 | assert_int_equal(YANG_ERROR_APP_TAG, kw); |
| 418 | assert_int_equal(13, len); |
| 419 | str = "error-message "; |
| 420 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 421 | assert_int_equal(YANG_ERROR_MESSAGE, kw); |
| 422 | assert_int_equal(13, len); |
| 423 | str = "extension "; |
| 424 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 425 | assert_int_equal(YANG_EXTENSION, kw); |
| 426 | assert_int_equal(9, len); |
| 427 | str = "feature "; |
| 428 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 429 | assert_int_equal(YANG_FEATURE, kw); |
| 430 | assert_int_equal(7, len); |
| 431 | str = "fraction-digits "; |
| 432 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 433 | assert_int_equal(YANG_FRACTION_DIGITS, kw); |
| 434 | assert_int_equal(15, len); |
| 435 | str = "grouping "; |
| 436 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 437 | assert_int_equal(YANG_GROUPING, kw); |
| 438 | assert_int_equal(8, len); |
| 439 | str = "identity "; |
| 440 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 441 | assert_int_equal(YANG_IDENTITY, kw); |
| 442 | assert_int_equal(8, len); |
| 443 | str = "if-feature "; |
| 444 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 445 | assert_int_equal(YANG_IF_FEATURE, kw); |
| 446 | assert_int_equal(10, len); |
| 447 | str = "import "; |
| 448 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 449 | assert_int_equal(YANG_IMPORT, kw); |
| 450 | assert_int_equal(6, len); |
| 451 | str = "include "; |
| 452 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 453 | assert_int_equal(YANG_INCLUDE, kw); |
| 454 | assert_int_equal(7, len); |
| 455 | str = "input{"; |
| 456 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 457 | assert_int_equal(YANG_INPUT, kw); |
| 458 | assert_int_equal(5, len); |
| 459 | str = "key "; |
| 460 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 461 | assert_int_equal(YANG_KEY, kw); |
| 462 | assert_int_equal(3, len); |
| 463 | str = "leaf "; |
| 464 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 465 | assert_int_equal(YANG_LEAF, kw); |
| 466 | assert_int_equal(4, len); |
| 467 | str = "leaf-list "; |
| 468 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 469 | assert_int_equal(YANG_LEAF_LIST, kw); |
| 470 | assert_int_equal(9, len); |
| 471 | str = "length "; |
| 472 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 473 | assert_int_equal(YANG_LENGTH, kw); |
| 474 | assert_int_equal(6, len); |
| 475 | str = "list "; |
| 476 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 477 | assert_int_equal(YANG_LIST, kw); |
| 478 | assert_int_equal(4, len); |
| 479 | str = "mandatory "; |
| 480 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 481 | assert_int_equal(YANG_MANDATORY, kw); |
| 482 | assert_int_equal(9, len); |
| 483 | str = "max-elements "; |
| 484 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 485 | assert_int_equal(YANG_MAX_ELEMENTS, kw); |
| 486 | assert_int_equal(12, len); |
| 487 | str = "min-elements "; |
| 488 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 489 | assert_int_equal(YANG_MIN_ELEMENTS, kw); |
| 490 | assert_int_equal(12, len); |
| 491 | str = "modifier "; |
| 492 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 493 | assert_int_equal(YANG_MODIFIER, kw); |
| 494 | assert_int_equal(8, len); |
| 495 | str = "module "; |
| 496 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 497 | assert_int_equal(YANG_MODULE, kw); |
| 498 | assert_int_equal(6, len); |
| 499 | str = "must "; |
| 500 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 501 | assert_int_equal(YANG_MUST, kw); |
| 502 | assert_int_equal(4, len); |
| 503 | str = "namespace "; |
| 504 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 505 | assert_int_equal(YANG_NAMESPACE, kw); |
| 506 | assert_int_equal(9, len); |
| 507 | str = "notification "; |
| 508 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 509 | assert_int_equal(YANG_NOTIFICATION, kw); |
| 510 | assert_int_equal(12, len); |
| 511 | str = "ordered-by "; |
| 512 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 513 | assert_int_equal(YANG_ORDERED_BY, kw); |
| 514 | assert_int_equal(10, len); |
| 515 | str = "organization "; |
| 516 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 517 | assert_int_equal(YANG_ORGANIZATION, kw); |
| 518 | assert_int_equal(12, len); |
| 519 | str = "output "; |
| 520 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 521 | assert_int_equal(YANG_OUTPUT, kw); |
| 522 | assert_int_equal(6, len); |
| 523 | str = "path "; |
| 524 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 525 | assert_int_equal(YANG_PATH, kw); |
| 526 | assert_int_equal(4, len); |
| 527 | str = "pattern "; |
| 528 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 529 | assert_int_equal(YANG_PATTERN, kw); |
| 530 | assert_int_equal(7, len); |
| 531 | str = "position "; |
| 532 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 533 | assert_int_equal(YANG_POSITION, kw); |
| 534 | assert_int_equal(8, len); |
| 535 | str = "prefix "; |
| 536 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 537 | assert_int_equal(YANG_PREFIX, kw); |
| 538 | assert_int_equal(6, len); |
| 539 | str = "presence "; |
| 540 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 541 | assert_int_equal(YANG_PRESENCE, kw); |
| 542 | assert_int_equal(8, len); |
| 543 | str = "range "; |
| 544 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 545 | assert_int_equal(YANG_RANGE, kw); |
| 546 | assert_int_equal(5, len); |
| 547 | str = "reference "; |
| 548 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 549 | assert_int_equal(YANG_REFERENCE, kw); |
| 550 | assert_int_equal(9, len); |
| 551 | str = "refine "; |
| 552 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 553 | assert_int_equal(YANG_REFINE, kw); |
| 554 | assert_int_equal(6, len); |
| 555 | str = "require-instance "; |
| 556 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 557 | assert_int_equal(YANG_REQUIRE_INSTANCE, kw); |
| 558 | assert_int_equal(16, len); |
| 559 | str = "revision "; |
| 560 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 561 | assert_int_equal(YANG_REVISION, kw); |
| 562 | assert_int_equal(8, len); |
| 563 | str = "revision-date "; |
| 564 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 565 | assert_int_equal(YANG_REVISION_DATE, kw); |
| 566 | assert_int_equal(13, len); |
| 567 | str = "rpc "; |
| 568 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 569 | assert_int_equal(YANG_RPC, kw); |
| 570 | assert_int_equal(3, len); |
| 571 | str = "status "; |
| 572 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 573 | assert_int_equal(YANG_STATUS, kw); |
| 574 | assert_int_equal(6, len); |
| 575 | str = "submodule "; |
| 576 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 577 | assert_int_equal(YANG_SUBMODULE, kw); |
| 578 | assert_int_equal(9, len); |
| 579 | str = "type "; |
| 580 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 581 | assert_int_equal(YANG_TYPE, kw); |
| 582 | assert_int_equal(4, len); |
| 583 | str = "typedef "; |
| 584 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 585 | assert_int_equal(YANG_TYPEDEF, kw); |
| 586 | assert_int_equal(7, len); |
| 587 | str = "unique "; |
| 588 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 589 | assert_int_equal(YANG_UNIQUE, kw); |
| 590 | assert_int_equal(6, len); |
| 591 | str = "units "; |
| 592 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 593 | assert_int_equal(YANG_UNITS, kw); |
| 594 | assert_int_equal(5, len); |
| 595 | str = "uses "; |
| 596 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 597 | assert_int_equal(YANG_USES, kw); |
| 598 | assert_int_equal(4, len); |
| 599 | str = "value "; |
| 600 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 601 | assert_int_equal(YANG_VALUE, kw); |
| 602 | assert_int_equal(5, len); |
| 603 | str = "when "; |
| 604 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 605 | assert_int_equal(YANG_WHEN, kw); |
| 606 | assert_int_equal(4, len); |
| 607 | str = "yang-version "; |
| 608 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 609 | assert_int_equal(YANG_YANG_VERSION, kw); |
| 610 | assert_int_equal(12, len); |
| 611 | str = "yin-element "; |
| 612 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 613 | assert_int_equal(YANG_YIN_ELEMENT, kw); |
| 614 | assert_int_equal(11, len); |
Radek Krejci | 626df48 | 2018-10-11 15:06:31 +0200 | [diff] [blame] | 615 | str = ";config false;"; |
| 616 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 617 | assert_int_equal(YANG_SEMICOLON, kw); |
| 618 | assert_int_equal(1, len); |
| 619 | assert_string_equal("config false;", str); |
| 620 | str = "{ config false;"; |
| 621 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 622 | assert_int_equal(YANG_LEFT_BRACE, kw); |
| 623 | assert_int_equal(1, len); |
| 624 | assert_string_equal(" config false;", str); |
| 625 | str = "}"; |
| 626 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 627 | assert_int_equal(YANG_RIGHT_BRACE, kw); |
| 628 | assert_int_equal(1, len); |
| 629 | assert_string_equal("", str); |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 630 | |
| 631 | /* geenric extension */ |
| 632 | str = p = "nacm:default-deny-write;"; |
| 633 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 634 | assert_int_equal(YANG_CUSTOM, kw); |
| 635 | assert_int_equal(23, len); |
| 636 | assert_ptr_equal(p, word); |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 637 | } |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 638 | |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 639 | static struct lysp_module * |
| 640 | mod_renew(struct ly_parser_ctx *ctx, struct lysp_module *mod) |
| 641 | { |
| 642 | lysp_module_free(mod); |
| 643 | mod = calloc(1, sizeof *mod); |
| 644 | mod->ctx = ctx->ctx; |
| 645 | assert_non_null(mod); |
| 646 | return mod; |
| 647 | } |
| 648 | |
| 649 | static void |
| 650 | test_module(void **state) |
| 651 | { |
| 652 | (void) state; /* unused */ |
| 653 | |
| 654 | struct ly_parser_ctx ctx; |
| 655 | struct lysp_module *mod; |
| 656 | const char *str; |
| 657 | |
| 658 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 659 | assert_non_null(ctx.ctx); |
| 660 | ctx.line = 1; |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 661 | ctx.indent = 0; |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 662 | |
| 663 | mod = mod_renew(&ctx, NULL); |
| 664 | |
| 665 | /* missing mandatory substatements */ |
| 666 | str = " name {}"; |
| 667 | assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod)); |
| 668 | assert_string_equal("name", mod->name); |
| 669 | logbuf_assert("Missing mandatory keyword \"namespace\" as a child of \"module\". Line number 1."); |
| 670 | mod = mod_renew(&ctx, mod); |
| 671 | |
| 672 | str = " name {namespace urn:x;}"; |
| 673 | assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod)); |
| 674 | assert_string_equal("urn:x", mod->ns); |
| 675 | logbuf_assert("Missing mandatory keyword \"prefix\" as a child of \"module\". Line number 1."); |
| 676 | mod = mod_renew(&ctx, mod); |
| 677 | |
| 678 | str = " name {namespace urn:x;prefix \"x\";}"; |
| 679 | assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod)); |
| 680 | assert_string_equal("x", mod->prefix); |
| 681 | mod = mod_renew(&ctx, mod); |
| 682 | |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 683 | #define SCHEMA_BEGINNING " name {namespace urn:x;prefix \"x\";" |
| 684 | #define TEST_NODE(NODETYPE, INPUT, NAME) \ |
| 685 | str = SCHEMA_BEGINNING INPUT; \ |
| 686 | assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod)); \ |
| 687 | assert_non_null(mod->data); \ |
| 688 | assert_int_equal(NODETYPE, mod->data->nodetype); \ |
| 689 | assert_string_equal(NAME, mod->data->name); \ |
| 690 | mod = mod_renew(&ctx, mod); |
| 691 | #define TEST_GENERIC(INPUT, TARGET, TEST) \ |
| 692 | str = SCHEMA_BEGINNING INPUT; \ |
| 693 | assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod)); \ |
| 694 | assert_non_null(TARGET); \ |
| 695 | TEST; \ |
| 696 | mod = mod_renew(&ctx, mod); |
| 697 | |
| 698 | /* duplicated namespace, prefix */ |
| 699 | str = SCHEMA_BEGINNING "namespace y;}"; |
| 700 | assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod)); |
| 701 | logbuf_assert("Duplicate keyword \"namespace\". Line number 1."); |
| 702 | mod = mod_renew(&ctx, mod); |
| 703 | str = SCHEMA_BEGINNING "prefix y;}"; |
| 704 | assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod)); |
| 705 | logbuf_assert("Duplicate keyword \"prefix\". Line number 1."); |
| 706 | mod = mod_renew(&ctx, mod); |
| 707 | |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame^] | 708 | /* not allowed in module (submodule-specific) */ |
| 709 | str = SCHEMA_BEGINNING "belongs-to master {prefix m;}}"; |
| 710 | assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod)); |
| 711 | logbuf_assert("Invalid keyword \"belongs-to\" as a child of \"module\". Line number 1."); |
| 712 | mod = mod_renew(&ctx, mod); |
| 713 | |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 714 | /* anydata */ |
| 715 | TEST_NODE(LYS_ANYDATA, "anydata test;}", "test"); |
| 716 | /* anyxml */ |
| 717 | TEST_NODE(LYS_ANYXML, "anyxml test;}", "test"); |
| 718 | /* augment */ |
| 719 | TEST_GENERIC("augment /somepath;}", mod->augments, |
| 720 | assert_string_equal("/somepath", mod->augments[0].nodeid)); |
| 721 | /* choice */ |
| 722 | TEST_NODE(LYS_CHOICE, "choice test;}", "test"); |
| 723 | /* contact 0..1 */ |
| 724 | TEST_GENERIC("contact \"firstname\" + \n\t\" surname\";}", mod->contact, |
| 725 | assert_string_equal("firstname surname", mod->contact)); |
| 726 | /* container */ |
| 727 | TEST_NODE(LYS_CONTAINER, "container test;}", "test"); |
| 728 | /* description 0..1 */ |
| 729 | TEST_GENERIC("description \'some description\';}", mod->dsc, |
| 730 | assert_string_equal("some description", mod->dsc)); |
| 731 | /* deviation */ |
| 732 | TEST_GENERIC("deviation /somepath {deviate not-supported;}}", mod->deviations, |
| 733 | assert_string_equal("/somepath", mod->deviations[0].nodeid)); |
| 734 | /* extension */ |
| 735 | TEST_GENERIC("extension test;}", mod->extensions, |
| 736 | assert_string_equal("test", mod->extensions[0].name)); |
| 737 | /* feature */ |
| 738 | TEST_GENERIC("feature test;}", mod->features, |
| 739 | assert_string_equal("test", mod->features[0].name)); |
| 740 | /* grouping */ |
| 741 | TEST_GENERIC("grouping grp;}", mod->groupings, |
| 742 | assert_string_equal("grp", mod->groupings[0].name)); |
| 743 | /* identity */ |
| 744 | TEST_GENERIC("identity test;}", mod->identities, |
| 745 | assert_string_equal("test", mod->identities[0].name)); |
| 746 | /* import */ |
| 747 | TEST_GENERIC("import test {prefix z;}}", mod->imports, |
| 748 | assert_string_equal("test", mod->imports[0].name)); |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame^] | 749 | |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 750 | /* import - prefix collision */ |
| 751 | str = SCHEMA_BEGINNING "import test {prefix x;}}"; |
| 752 | assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod)); |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame^] | 753 | logbuf_assert("Prefix \"x\" already used as module prefix. Line number 2."); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 754 | mod = mod_renew(&ctx, mod); |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame^] | 755 | |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 756 | /* include */ |
| 757 | TEST_GENERIC("rpc test;}", mod->rpcs, |
| 758 | assert_string_equal("test", mod->rpcs[0].name)); |
| 759 | /* leaf */ |
| 760 | TEST_NODE(LYS_LEAF, "leaf test {type string;}}", "test"); |
| 761 | /* leaf-list */ |
| 762 | TEST_NODE(LYS_LEAFLIST, "leaf-list test {type string;}}", "test"); |
| 763 | /* list */ |
| 764 | TEST_NODE(LYS_LIST, "list test {key a;leaf a {type string;}}}", "test"); |
| 765 | /* notification */ |
| 766 | TEST_GENERIC("notification test;}", mod->notifs, |
| 767 | assert_string_equal("test", mod->notifs[0].name)); |
| 768 | /* organization 0..1 */ |
| 769 | TEST_GENERIC("organization \"CESNET a.l.e.\";}", mod->org, |
| 770 | assert_string_equal("CESNET a.l.e.", mod->org)); |
| 771 | /* reference 0..1 */ |
| 772 | TEST_GENERIC("reference RFC7950;}", mod->ref, |
| 773 | assert_string_equal("RFC7950", mod->ref)); |
| 774 | /* revision */ |
| 775 | TEST_GENERIC("revision 2018-10-12;}", mod->revs, |
| 776 | assert_string_equal("2018-10-12", mod->revs[0].rev)); |
| 777 | /* rpc */ |
| 778 | TEST_GENERIC("rpc test;}", mod->rpcs, |
| 779 | assert_string_equal("test", mod->rpcs[0].name)); |
| 780 | /* typedef */ |
| 781 | TEST_GENERIC("typedef test{type string;}}", mod->typedefs, |
| 782 | assert_string_equal("test", mod->typedefs[0].name)); |
| 783 | /* uses */ |
| 784 | TEST_NODE(LYS_USES, "uses test;}", "test"); |
| 785 | /* yang-version */ |
| 786 | str = SCHEMA_BEGINNING "\n\tyang-version 10;}"; |
| 787 | assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod)); |
| 788 | logbuf_assert("Invalid value \"10\" of \"yang-version\". Line number 3."); |
| 789 | mod = mod_renew(&ctx, mod); |
| 790 | str = SCHEMA_BEGINNING "yang-version 1.0;yang-version 1.1;}"; |
| 791 | assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod)); |
| 792 | logbuf_assert("Duplicate keyword \"yang-version\". Line number 3."); |
| 793 | mod = mod_renew(&ctx, mod); |
| 794 | str = SCHEMA_BEGINNING "yang-version 1.0;}"; |
| 795 | assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod)); |
| 796 | assert_int_equal(1, mod->version); |
| 797 | mod = mod_renew(&ctx, mod); |
| 798 | str = SCHEMA_BEGINNING "yang-version \"1.1\";}"; |
| 799 | assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod)); |
| 800 | assert_int_equal(2, mod->version); |
| 801 | mod = mod_renew(&ctx, mod); |
| 802 | |
| 803 | #undef TEST_GENERIC |
| 804 | #undef TEST_NODE |
| 805 | #undef SCHEMA_BEGINNING |
| 806 | |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 807 | lysp_module_free(mod); |
| 808 | ly_ctx_destroy(ctx.ctx, NULL); |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 809 | } |
| 810 | |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 811 | int main(void) |
| 812 | { |
| 813 | const struct CMUnitTest tests[] = { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 814 | cmocka_unit_test_setup(test_helpers, logger_setup), |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 815 | cmocka_unit_test_setup(test_comments, logger_setup), |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 816 | cmocka_unit_test_setup(test_arg, logger_setup), |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 817 | cmocka_unit_test_setup(test_stmts, logger_setup), |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 818 | cmocka_unit_test_setup(test_module, logger_setup), |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 819 | }; |
| 820 | |
| 821 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 822 | } |