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