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" |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 23 | #include "../../src/tree_schema_free.c" |
| 24 | #include "../../src/tree_schema_compile.c" |
Radek Krejci | f3f4784 | 2018-11-15 11:22:15 +0100 | [diff] [blame] | 25 | #include "../../src/tree_schema.c" |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 26 | |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 27 | #include <stdarg.h> |
| 28 | #include <stddef.h> |
| 29 | #include <setjmp.h> |
| 30 | #include <cmocka.h> |
| 31 | |
| 32 | #include <stdio.h> |
| 33 | #include <string.h> |
| 34 | |
| 35 | #include "libyang.h" |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 36 | |
| 37 | #define BUFSIZE 1024 |
| 38 | char logbuf[BUFSIZE] = {0}; |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 39 | int store = -1; /* negative for infinite logging, positive for limited logging */ |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 40 | |
| 41 | /* 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] | 42 | #define ENABLE_LOGGER_CHECKING 1 |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 43 | |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 44 | #if ENABLE_LOGGER_CHECKING |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 45 | static void |
| 46 | logger(LY_LOG_LEVEL level, const char *msg, const char *path) |
| 47 | { |
| 48 | (void) level; /* unused */ |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 49 | if (store) { |
| 50 | if (path && path[0]) { |
| 51 | snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path); |
| 52 | } else { |
| 53 | strncpy(logbuf, msg, BUFSIZE - 1); |
| 54 | } |
| 55 | if (store > 0) { |
| 56 | --store; |
| 57 | } |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 58 | } |
| 59 | } |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 60 | #endif |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 61 | |
| 62 | static int |
| 63 | logger_setup(void **state) |
| 64 | { |
| 65 | (void) state; /* unused */ |
| 66 | #if ENABLE_LOGGER_CHECKING |
| 67 | ly_set_log_clb(logger, 1); |
| 68 | #endif |
| 69 | return 0; |
| 70 | } |
| 71 | |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 72 | static int |
| 73 | logger_teardown(void **state) |
| 74 | { |
| 75 | (void) state; /* unused */ |
| 76 | #if ENABLE_LOGGER_CHECKING |
| 77 | if (*state) { |
| 78 | fprintf(stderr, "%s\n", logbuf); |
| 79 | } |
| 80 | #endif |
| 81 | return 0; |
| 82 | } |
| 83 | |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 84 | void |
| 85 | logbuf_clean(void) |
| 86 | { |
| 87 | logbuf[0] = '\0'; |
| 88 | } |
| 89 | |
| 90 | #if ENABLE_LOGGER_CHECKING |
| 91 | # define logbuf_assert(str) assert_string_equal(logbuf, str) |
| 92 | #else |
| 93 | # define logbuf_assert(str) |
| 94 | #endif |
| 95 | |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 96 | #define TEST_DUP_GENERIC(PREFIX, MEMBER, VALUE1, VALUE2, FUNC, RESULT, LINE, CLEANUP) \ |
| 97 | str = PREFIX MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
| 98 | assert_int_equal(LY_EVALID, FUNC(&ctx, &str, RESULT)); \ |
| 99 | logbuf_assert("Duplicate keyword \""MEMBER"\". Line number "LINE"."); \ |
| 100 | CLEANUP |
| 101 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 102 | static void |
| 103 | test_helpers(void **state) |
| 104 | { |
| 105 | (void) state; /* unused */ |
| 106 | |
| 107 | const char *str; |
Radek Krejci | 404251e | 2018-10-09 12:06:44 +0200 | [diff] [blame] | 108 | char *buf, *p; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 109 | size_t len, size; |
| 110 | int prefix; |
| 111 | struct ly_parser_ctx ctx; |
| 112 | ctx.ctx = NULL; |
| 113 | ctx.line = 1; |
| 114 | |
| 115 | /* storing into buffer */ |
| 116 | str = "abcd"; |
| 117 | buf = NULL; |
| 118 | size = len = 0; |
| 119 | assert_int_equal(LY_SUCCESS, buf_add_char(NULL, &str, 2, &buf, &size, &len)); |
| 120 | assert_int_not_equal(0, size); |
| 121 | assert_int_equal(2, len); |
| 122 | assert_string_equal("cd", str); |
| 123 | assert_false(strncmp("ab", buf, 2)); |
| 124 | free(buf); |
Radek Krejci | 404251e | 2018-10-09 12:06:44 +0200 | [diff] [blame] | 125 | buf = NULL; |
| 126 | |
| 127 | /* invalid first characters */ |
| 128 | len = 0; |
| 129 | str = "2invalid"; |
| 130 | assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1)); |
| 131 | str = ".invalid"; |
| 132 | assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1)); |
| 133 | str = "-invalid"; |
| 134 | assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1)); |
| 135 | /* invalid following characters */ |
| 136 | len = 3; /* number of characters read before the str content */ |
| 137 | str = "!"; |
| 138 | assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1)); |
| 139 | str = ":"; |
| 140 | assert_int_equal(LY_EVALID, buf_store_char(&ctx, &str, Y_IDENTIF_ARG, &p, &len, &buf, &size, 1)); |
| 141 | /* valid colon for prefixed identifiers */ |
| 142 | len = size = 0; |
| 143 | p = NULL; |
| 144 | str = "x:id"; |
| 145 | assert_int_equal(LY_SUCCESS, buf_store_char(&ctx, &str, Y_PREF_IDENTIF_ARG, &p, &len, &buf, &size, 0)); |
| 146 | assert_int_equal(1, len); |
| 147 | assert_null(buf); |
| 148 | assert_string_equal(":id", str); |
| 149 | assert_int_equal('x', p[len - 1]); |
| 150 | assert_int_equal(LY_SUCCESS, buf_store_char(&ctx, &str, Y_PREF_IDENTIF_ARG, &p, &len, &buf, &size, 1)); |
| 151 | assert_int_equal(2, len); |
| 152 | assert_string_equal("id", str); |
| 153 | assert_int_equal(':', p[len - 1]); |
| 154 | free(buf); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 155 | |
| 156 | /* checking identifiers */ |
| 157 | assert_int_equal(LY_EVALID, check_identifierchar(&ctx, ':', 0, NULL)); |
| 158 | logbuf_assert("Invalid identifier character ':'. Line number 1."); |
| 159 | assert_int_equal(LY_EVALID, check_identifierchar(&ctx, '#', 1, NULL)); |
| 160 | logbuf_assert("Invalid identifier first character '#'. Line number 1."); |
| 161 | |
| 162 | assert_int_equal(LY_SUCCESS, check_identifierchar(&ctx, 'a', 1, &prefix)); |
| 163 | assert_int_equal(0, prefix); |
| 164 | assert_int_equal(LY_SUCCESS, check_identifierchar(&ctx, ':', 0, &prefix)); |
| 165 | assert_int_equal(1, prefix); |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 166 | assert_int_equal(LY_EVALID, check_identifierchar(&ctx, ':', 0, &prefix)); |
| 167 | assert_int_equal(1, prefix); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 168 | assert_int_equal(LY_SUCCESS, check_identifierchar(&ctx, 'b', 0, &prefix)); |
| 169 | assert_int_equal(2, prefix); |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 170 | /* second colon is invalid */ |
| 171 | assert_int_equal(LY_EVALID, check_identifierchar(&ctx, ':', 0, &prefix)); |
| 172 | logbuf_assert("Invalid identifier character ':'. Line number 1."); |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 173 | } |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 174 | |
| 175 | static void |
| 176 | test_comments(void **state) |
| 177 | { |
| 178 | (void) state; /* unused */ |
| 179 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 180 | struct ly_parser_ctx ctx; |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 181 | const char *str, *p; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 182 | char *word, *buf; |
| 183 | size_t len; |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 184 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 185 | ctx.ctx = NULL; |
| 186 | ctx.line = 1; |
| 187 | |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 188 | str = " // this is a text of / one * line */ comment\nargument"; |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 189 | 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] | 190 | assert_string_equal("argument", word); |
| 191 | assert_null(buf); |
| 192 | assert_int_equal(8, len); |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 193 | |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 194 | 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] | 195 | 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] | 196 | assert_string_equal("argument", word); |
| 197 | assert_ptr_equal(buf, word); |
| 198 | assert_int_equal(8, len); |
| 199 | free(word); |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 200 | |
| 201 | str = p = " this is one line comment on last line"; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 202 | assert_int_equal(LY_SUCCESS, skip_comment(&ctx, &str, 1)); |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 203 | assert_true(str[0] == '\0'); |
| 204 | |
| 205 | str = p = " this is a not terminated comment x"; |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 206 | assert_int_equal(LY_EVALID, skip_comment(&ctx, &str, 2)); |
| 207 | logbuf_assert("Unexpected end-of-file, non-terminated comment. Line number 5."); |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 208 | assert_true(str[0] == '\0'); |
| 209 | } |
| 210 | |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 211 | static void |
| 212 | test_arg(void **state) |
| 213 | { |
| 214 | (void) state; /* unused */ |
| 215 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 216 | struct ly_parser_ctx ctx; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 217 | const char *str; |
| 218 | char *word, *buf; |
| 219 | size_t len; |
| 220 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 221 | ctx.ctx = NULL; |
| 222 | ctx.line = 1; |
| 223 | |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 224 | /* missing argument */ |
| 225 | str = ";"; |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 226 | 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] | 227 | assert_null(word); |
| 228 | |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 229 | str = "{"; |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 230 | 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] | 231 | logbuf_assert("Invalid character sequence \"{\", expected an argument. Line number 1."); |
| 232 | |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 233 | /* invalid escape sequence */ |
| 234 | str = "\"\\s\""; |
| 235 | 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] | 236 | logbuf_assert("Double-quoted string unknown special character \'\\s\'. Line number 1."); |
| 237 | 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] | 238 | 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] | 239 | assert_int_equal(2, len); |
| 240 | assert_string_equal("\\s\'", word); |
| 241 | assert_int_equal('\0', str[0]); /* input has been eaten */ |
| 242 | |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 243 | /* invalid character after the argument */ |
| 244 | str = "hello\""; |
| 245 | assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len)); |
| 246 | logbuf_assert("Invalid character sequence \"\"\", expected unquoted string character, optsep, semicolon or opening brace. Line number 1."); |
| 247 | str = "hello}"; |
| 248 | assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len)); |
| 249 | logbuf_assert("Invalid character sequence \"}\", expected unquoted string character, optsep, semicolon or opening brace. Line number 1."); |
| 250 | |
| 251 | str = "hello/x\t"; /* slash is not an invalid character */ |
| 252 | assert_int_equal(LY_SUCCESS, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len)); |
| 253 | assert_int_equal(7, len); |
| 254 | assert_string_equal("hello/x\t", word); |
| 255 | |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 256 | assert_null(buf); |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 257 | |
| 258 | /* different quoting */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 259 | str = "hello "; |
| 260 | 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] | 261 | assert_null(buf); |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 262 | assert_int_equal(5, len); |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 263 | assert_string_equal("hello ", word); |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 264 | |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 265 | str = "hello/*comment*/\n"; |
| 266 | 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] | 267 | assert_null(buf); |
| 268 | assert_int_equal(5, len); |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 269 | assert_false(strncmp("hello", word, len)); |
| 270 | |
| 271 | |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 272 | str = "\"hello\\n\\t\\\"\\\\\";"; |
| 273 | 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] | 274 | assert_null(buf); |
| 275 | assert_int_equal(9, len); |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 276 | assert_string_equal("hello\\n\\t\\\"\\\\\";", word); |
Radek Krejci | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 277 | |
| 278 | ctx.indent = 14; |
| 279 | str = "\"hello \t\n\t\t world!\""; |
| 280 | /* - space and tabs before newline are stripped out |
| 281 | * - space and tabs after newline (indentation) are stripped out |
| 282 | */ |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 283 | 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] | 284 | assert_non_null(buf); |
| 285 | assert_ptr_equal(word, buf); |
| 286 | assert_int_equal(14, len); |
| 287 | assert_string_equal("hello\n world!", word); |
| 288 | free(buf); |
| 289 | |
| 290 | ctx.indent = 14; |
| 291 | str = "\"hello\n \tworld!\""; |
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 | d5f2b5f | 2018-10-11 10:54:36 +0200 | [diff] [blame] | 293 | assert_non_null(buf); |
| 294 | assert_ptr_equal(word, buf); |
| 295 | assert_int_equal(12, len); |
| 296 | assert_string_equal("hello\nworld!", word); |
| 297 | free(buf); |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 298 | |
| 299 | str = "\'hello\'"; |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 300 | 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] | 301 | assert_null(buf); |
| 302 | assert_int_equal(5, len); |
| 303 | assert_false(strncmp("hello", word, 5)); |
| 304 | |
| 305 | str = "\"hel\" +\t\n\"lo\""; |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 306 | 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] | 307 | assert_ptr_equal(word, buf); |
| 308 | assert_int_equal(5, len); |
| 309 | assert_string_equal("hello", word); |
| 310 | free(buf); |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 311 | str = "\"hel\" +\t\nlo"; /* unquoted the second part */ |
| 312 | assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len)); |
| 313 | 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] | 314 | |
| 315 | str = "\'he\'\t\n+ \"llo\""; |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 316 | 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] | 317 | assert_ptr_equal(word, buf); |
| 318 | assert_int_equal(5, len); |
| 319 | assert_string_equal("hello", word); |
| 320 | free(buf); |
| 321 | |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 322 | str = " \t\n\"he\"+\'llo\'"; |
| 323 | 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] | 324 | assert_ptr_equal(word, buf); |
| 325 | assert_int_equal(5, len); |
| 326 | assert_string_equal("hello", word); |
| 327 | free(buf); |
| 328 | |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 329 | /* missing argument */ |
| 330 | str = ";"; |
Radek Krejci | fc62d7e | 2018-10-11 12:56:42 +0200 | [diff] [blame] | 331 | assert_int_equal(LY_EVALID, get_argument(&ctx, &str, Y_STR_ARG, &word, &buf, &len)); |
| 332 | logbuf_assert("Invalid character sequence \";\", expected an argument. Line number 7."); |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | static void |
| 336 | test_stmts(void **state) |
| 337 | { |
| 338 | (void) state; /* unused */ |
| 339 | |
| 340 | struct ly_parser_ctx ctx; |
| 341 | const char *str, *p; |
| 342 | enum yang_keyword kw; |
| 343 | char *word; |
| 344 | size_t len; |
| 345 | |
| 346 | ctx.ctx = NULL; |
| 347 | ctx.line = 1; |
| 348 | |
| 349 | str = "\n// comment\n\tinput\t{"; |
| 350 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 351 | assert_int_equal(YANG_INPUT, kw); |
| 352 | assert_int_equal(5, len); |
| 353 | assert_string_equal("input\t{", word); |
| 354 | assert_string_equal("\t{", str); |
| 355 | |
| 356 | str = "\t /* comment */\t output\n\t{"; |
| 357 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 358 | assert_int_equal(YANG_OUTPUT, kw); |
| 359 | assert_int_equal(6, len); |
| 360 | assert_string_equal("output\n\t{", word); |
| 361 | assert_string_equal("\n\t{", str); |
| 362 | |
| 363 | str = "/input { "; /* invalid slash */ |
| 364 | assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 365 | logbuf_assert("Invalid identifier first character '/'. Line number 4."); |
| 366 | |
| 367 | str = "not-a-statement-nor-extension { "; /* invalid identifier */ |
| 368 | assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 369 | logbuf_assert("Invalid character sequence \"not-a-statement-nor-extension\", expected a keyword. Line number 4."); |
| 370 | |
| 371 | str = "path;"; /* missing sep after the keyword */ |
| 372 | assert_int_equal(LY_EVALID, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 373 | logbuf_assert("Invalid character sequence \"path;\", expected a keyword followed by a separator. Line number 4."); |
| 374 | |
| 375 | str = "action "; |
| 376 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 377 | assert_int_equal(YANG_ACTION, kw); |
| 378 | assert_int_equal(6, len); |
| 379 | str = "anydata "; |
| 380 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 381 | assert_int_equal(YANG_ANYDATA, kw); |
| 382 | assert_int_equal(7, len); |
| 383 | str = "anyxml "; |
| 384 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 385 | assert_int_equal(YANG_ANYXML, kw); |
| 386 | assert_int_equal(6, len); |
| 387 | str = "argument "; |
| 388 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 389 | assert_int_equal(YANG_ARGUMENT, kw); |
| 390 | assert_int_equal(8, len); |
| 391 | str = "augment "; |
| 392 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 393 | assert_int_equal(YANG_AUGMENT, kw); |
| 394 | assert_int_equal(7, len); |
| 395 | str = "base "; |
| 396 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 397 | assert_int_equal(YANG_BASE, kw); |
| 398 | assert_int_equal(4, len); |
| 399 | str = "belongs-to "; |
| 400 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 401 | assert_int_equal(YANG_BELONGS_TO, kw); |
| 402 | assert_int_equal(10, len); |
| 403 | str = "bit "; |
| 404 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 405 | assert_int_equal(YANG_BIT, kw); |
| 406 | assert_int_equal(3, len); |
| 407 | str = "case "; |
| 408 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 409 | assert_int_equal(YANG_CASE, kw); |
| 410 | assert_int_equal(4, len); |
| 411 | str = "choice "; |
| 412 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 413 | assert_int_equal(YANG_CHOICE, kw); |
| 414 | assert_int_equal(6, len); |
| 415 | str = "config "; |
| 416 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 417 | assert_int_equal(YANG_CONFIG, kw); |
| 418 | assert_int_equal(6, len); |
| 419 | str = "contact "; |
| 420 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 421 | assert_int_equal(YANG_CONTACT, kw); |
| 422 | assert_int_equal(7, len); |
| 423 | str = "container "; |
| 424 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 425 | assert_int_equal(YANG_CONTAINER, kw); |
| 426 | assert_int_equal(9, len); |
| 427 | str = "default "; |
| 428 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 429 | assert_int_equal(YANG_DEFAULT, kw); |
| 430 | assert_int_equal(7, len); |
| 431 | str = "description "; |
| 432 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 433 | assert_int_equal(YANG_DESCRIPTION, kw); |
| 434 | assert_int_equal(11, len); |
| 435 | str = "deviate "; |
| 436 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 437 | assert_int_equal(YANG_DEVIATE, kw); |
| 438 | assert_int_equal(7, len); |
| 439 | str = "deviation "; |
| 440 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 441 | assert_int_equal(YANG_DEVIATION, kw); |
| 442 | assert_int_equal(9, len); |
| 443 | str = "enum "; |
| 444 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 445 | assert_int_equal(YANG_ENUM, kw); |
| 446 | assert_int_equal(4, len); |
| 447 | str = "error-app-tag "; |
| 448 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 449 | assert_int_equal(YANG_ERROR_APP_TAG, kw); |
| 450 | assert_int_equal(13, len); |
| 451 | str = "error-message "; |
| 452 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 453 | assert_int_equal(YANG_ERROR_MESSAGE, kw); |
| 454 | assert_int_equal(13, len); |
| 455 | str = "extension "; |
| 456 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 457 | assert_int_equal(YANG_EXTENSION, kw); |
| 458 | assert_int_equal(9, len); |
| 459 | str = "feature "; |
| 460 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 461 | assert_int_equal(YANG_FEATURE, kw); |
| 462 | assert_int_equal(7, len); |
| 463 | str = "fraction-digits "; |
| 464 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 465 | assert_int_equal(YANG_FRACTION_DIGITS, kw); |
| 466 | assert_int_equal(15, len); |
| 467 | str = "grouping "; |
| 468 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 469 | assert_int_equal(YANG_GROUPING, kw); |
| 470 | assert_int_equal(8, len); |
| 471 | str = "identity "; |
| 472 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 473 | assert_int_equal(YANG_IDENTITY, kw); |
| 474 | assert_int_equal(8, len); |
| 475 | str = "if-feature "; |
| 476 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 477 | assert_int_equal(YANG_IF_FEATURE, kw); |
| 478 | assert_int_equal(10, len); |
| 479 | str = "import "; |
| 480 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 481 | assert_int_equal(YANG_IMPORT, kw); |
| 482 | assert_int_equal(6, len); |
| 483 | str = "include "; |
| 484 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 485 | assert_int_equal(YANG_INCLUDE, kw); |
| 486 | assert_int_equal(7, len); |
| 487 | str = "input{"; |
| 488 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 489 | assert_int_equal(YANG_INPUT, kw); |
| 490 | assert_int_equal(5, len); |
| 491 | str = "key "; |
| 492 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 493 | assert_int_equal(YANG_KEY, kw); |
| 494 | assert_int_equal(3, len); |
| 495 | str = "leaf "; |
| 496 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 497 | assert_int_equal(YANG_LEAF, kw); |
| 498 | assert_int_equal(4, len); |
| 499 | str = "leaf-list "; |
| 500 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 501 | assert_int_equal(YANG_LEAF_LIST, kw); |
| 502 | assert_int_equal(9, len); |
| 503 | str = "length "; |
| 504 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 505 | assert_int_equal(YANG_LENGTH, kw); |
| 506 | assert_int_equal(6, len); |
| 507 | str = "list "; |
| 508 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 509 | assert_int_equal(YANG_LIST, kw); |
| 510 | assert_int_equal(4, len); |
| 511 | str = "mandatory "; |
| 512 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 513 | assert_int_equal(YANG_MANDATORY, kw); |
| 514 | assert_int_equal(9, len); |
| 515 | str = "max-elements "; |
| 516 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 517 | assert_int_equal(YANG_MAX_ELEMENTS, kw); |
| 518 | assert_int_equal(12, len); |
| 519 | str = "min-elements "; |
| 520 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 521 | assert_int_equal(YANG_MIN_ELEMENTS, kw); |
| 522 | assert_int_equal(12, len); |
| 523 | str = "modifier "; |
| 524 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 525 | assert_int_equal(YANG_MODIFIER, kw); |
| 526 | assert_int_equal(8, len); |
| 527 | str = "module "; |
| 528 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 529 | assert_int_equal(YANG_MODULE, kw); |
| 530 | assert_int_equal(6, len); |
| 531 | str = "must "; |
| 532 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 533 | assert_int_equal(YANG_MUST, kw); |
| 534 | assert_int_equal(4, len); |
| 535 | str = "namespace "; |
| 536 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 537 | assert_int_equal(YANG_NAMESPACE, kw); |
| 538 | assert_int_equal(9, len); |
| 539 | str = "notification "; |
| 540 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 541 | assert_int_equal(YANG_NOTIFICATION, kw); |
| 542 | assert_int_equal(12, len); |
| 543 | str = "ordered-by "; |
| 544 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 545 | assert_int_equal(YANG_ORDERED_BY, kw); |
| 546 | assert_int_equal(10, len); |
| 547 | str = "organization "; |
| 548 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 549 | assert_int_equal(YANG_ORGANIZATION, kw); |
| 550 | assert_int_equal(12, len); |
| 551 | str = "output "; |
| 552 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 553 | assert_int_equal(YANG_OUTPUT, kw); |
| 554 | assert_int_equal(6, len); |
| 555 | str = "path "; |
| 556 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 557 | assert_int_equal(YANG_PATH, kw); |
| 558 | assert_int_equal(4, len); |
| 559 | str = "pattern "; |
| 560 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 561 | assert_int_equal(YANG_PATTERN, kw); |
| 562 | assert_int_equal(7, len); |
| 563 | str = "position "; |
| 564 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 565 | assert_int_equal(YANG_POSITION, kw); |
| 566 | assert_int_equal(8, len); |
| 567 | str = "prefix "; |
| 568 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 569 | assert_int_equal(YANG_PREFIX, kw); |
| 570 | assert_int_equal(6, len); |
| 571 | str = "presence "; |
| 572 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 573 | assert_int_equal(YANG_PRESENCE, kw); |
| 574 | assert_int_equal(8, len); |
| 575 | str = "range "; |
| 576 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 577 | assert_int_equal(YANG_RANGE, kw); |
| 578 | assert_int_equal(5, len); |
| 579 | str = "reference "; |
| 580 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 581 | assert_int_equal(YANG_REFERENCE, kw); |
| 582 | assert_int_equal(9, len); |
| 583 | str = "refine "; |
| 584 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 585 | assert_int_equal(YANG_REFINE, kw); |
| 586 | assert_int_equal(6, len); |
| 587 | str = "require-instance "; |
| 588 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 589 | assert_int_equal(YANG_REQUIRE_INSTANCE, kw); |
| 590 | assert_int_equal(16, len); |
| 591 | str = "revision "; |
| 592 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 593 | assert_int_equal(YANG_REVISION, kw); |
| 594 | assert_int_equal(8, len); |
| 595 | str = "revision-date "; |
| 596 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 597 | assert_int_equal(YANG_REVISION_DATE, kw); |
| 598 | assert_int_equal(13, len); |
| 599 | str = "rpc "; |
| 600 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 601 | assert_int_equal(YANG_RPC, kw); |
| 602 | assert_int_equal(3, len); |
| 603 | str = "status "; |
| 604 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 605 | assert_int_equal(YANG_STATUS, kw); |
| 606 | assert_int_equal(6, len); |
| 607 | str = "submodule "; |
| 608 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 609 | assert_int_equal(YANG_SUBMODULE, kw); |
| 610 | assert_int_equal(9, len); |
| 611 | str = "type "; |
| 612 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 613 | assert_int_equal(YANG_TYPE, kw); |
| 614 | assert_int_equal(4, len); |
| 615 | str = "typedef "; |
| 616 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 617 | assert_int_equal(YANG_TYPEDEF, kw); |
| 618 | assert_int_equal(7, len); |
| 619 | str = "unique "; |
| 620 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 621 | assert_int_equal(YANG_UNIQUE, kw); |
| 622 | assert_int_equal(6, len); |
| 623 | str = "units "; |
| 624 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 625 | assert_int_equal(YANG_UNITS, kw); |
| 626 | assert_int_equal(5, len); |
| 627 | str = "uses "; |
| 628 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 629 | assert_int_equal(YANG_USES, kw); |
| 630 | assert_int_equal(4, len); |
| 631 | str = "value "; |
| 632 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 633 | assert_int_equal(YANG_VALUE, kw); |
| 634 | assert_int_equal(5, len); |
| 635 | str = "when "; |
| 636 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 637 | assert_int_equal(YANG_WHEN, kw); |
| 638 | assert_int_equal(4, len); |
| 639 | str = "yang-version "; |
| 640 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 641 | assert_int_equal(YANG_YANG_VERSION, kw); |
| 642 | assert_int_equal(12, len); |
| 643 | str = "yin-element "; |
| 644 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 645 | assert_int_equal(YANG_YIN_ELEMENT, kw); |
| 646 | assert_int_equal(11, len); |
Radek Krejci | 626df48 | 2018-10-11 15:06:31 +0200 | [diff] [blame] | 647 | str = ";config false;"; |
| 648 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 649 | assert_int_equal(YANG_SEMICOLON, kw); |
| 650 | assert_int_equal(1, len); |
| 651 | assert_string_equal("config false;", str); |
| 652 | str = "{ config false;"; |
| 653 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 654 | assert_int_equal(YANG_LEFT_BRACE, kw); |
| 655 | assert_int_equal(1, len); |
| 656 | assert_string_equal(" config false;", str); |
| 657 | str = "}"; |
| 658 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 659 | assert_int_equal(YANG_RIGHT_BRACE, kw); |
| 660 | assert_int_equal(1, len); |
| 661 | assert_string_equal("", str); |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 662 | |
| 663 | /* geenric extension */ |
| 664 | str = p = "nacm:default-deny-write;"; |
| 665 | assert_int_equal(LY_SUCCESS, get_keyword(&ctx, &str, &kw, &word, &len)); |
| 666 | assert_int_equal(YANG_CUSTOM, kw); |
| 667 | assert_int_equal(23, len); |
| 668 | assert_ptr_equal(p, word); |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 669 | } |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 670 | |
Radek Krejci | 05b1398 | 2018-11-28 16:22:07 +0100 | [diff] [blame] | 671 | static void |
| 672 | test_minmax(void **state) |
| 673 | { |
| 674 | *state = test_minmax; |
| 675 | |
Radek Krejci | 05b1398 | 2018-11-28 16:22:07 +0100 | [diff] [blame] | 676 | struct ly_parser_ctx ctx = {0}; |
| 677 | uint16_t flags = 0; |
| 678 | uint32_t value = 0; |
| 679 | struct lysp_ext_instance *ext = NULL; |
| 680 | const char *str; |
| 681 | |
| 682 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 683 | assert_non_null(ctx.ctx); |
| 684 | ctx.line = 1; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 685 | ctx.mod_version = 2; /* simulate YANG 1.1 */ |
Radek Krejci | 05b1398 | 2018-11-28 16:22:07 +0100 | [diff] [blame] | 686 | |
Radek Krejci | df6cad1 | 2018-11-28 17:10:55 +0100 | [diff] [blame] | 687 | str = " 1invalid; ..."; |
Radek Krejci | 05b1398 | 2018-11-28 16:22:07 +0100 | [diff] [blame] | 688 | assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext)); |
Radek Krejci | df6cad1 | 2018-11-28 17:10:55 +0100 | [diff] [blame] | 689 | logbuf_assert("Invalid value \"1invalid\" of \"min-elements\". Line number 1."); |
Radek Krejci | 05b1398 | 2018-11-28 16:22:07 +0100 | [diff] [blame] | 690 | |
| 691 | flags = value = 0; |
| 692 | str = " -1; ..."; |
| 693 | assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext)); |
| 694 | logbuf_assert("Invalid value \"-1\" of \"min-elements\". Line number 1."); |
| 695 | |
Radek Krejci | df6cad1 | 2018-11-28 17:10:55 +0100 | [diff] [blame] | 696 | /* implementation limit */ |
| 697 | flags = value = 0; |
| 698 | str = " 4294967296; ..."; |
| 699 | assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext)); |
| 700 | logbuf_assert("Value \"4294967296\" is out of \"min-elements\" bounds. Line number 1."); |
| 701 | |
Radek Krejci | 05b1398 | 2018-11-28 16:22:07 +0100 | [diff] [blame] | 702 | flags = value = 0; |
| 703 | str = " 1; ..."; |
| 704 | assert_int_equal(LY_SUCCESS, parse_minelements(&ctx, &str, &value, &flags, &ext)); |
| 705 | assert_int_equal(LYS_SET_MIN, flags); |
| 706 | assert_int_equal(1, value); |
| 707 | |
| 708 | flags = value = 0; |
| 709 | str = " 1 {m:ext;} ..."; |
| 710 | assert_int_equal(LY_SUCCESS, parse_minelements(&ctx, &str, &value, &flags, &ext)); |
| 711 | assert_int_equal(LYS_SET_MIN, flags); |
| 712 | assert_int_equal(1, value); |
| 713 | assert_non_null(ext); |
| 714 | FREE_ARRAY(ctx.ctx, ext, lysp_ext_instance_free); |
| 715 | ext = NULL; |
| 716 | |
| 717 | flags = value = 0; |
| 718 | str = " 1 {config true;} ..."; |
| 719 | assert_int_equal(LY_EVALID, parse_minelements(&ctx, &str, &value, &flags, &ext)); |
| 720 | logbuf_assert("Invalid keyword \"config\" as a child of \"min-elements\". Line number 1."); |
| 721 | |
Radek Krejci | df6cad1 | 2018-11-28 17:10:55 +0100 | [diff] [blame] | 722 | str = " 1invalid; ..."; |
Radek Krejci | 05b1398 | 2018-11-28 16:22:07 +0100 | [diff] [blame] | 723 | assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext)); |
Radek Krejci | df6cad1 | 2018-11-28 17:10:55 +0100 | [diff] [blame] | 724 | logbuf_assert("Invalid value \"1invalid\" of \"max-elements\". Line number 1."); |
Radek Krejci | 05b1398 | 2018-11-28 16:22:07 +0100 | [diff] [blame] | 725 | |
| 726 | flags = value = 0; |
| 727 | str = " -1; ..."; |
| 728 | assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext)); |
| 729 | logbuf_assert("Invalid value \"-1\" of \"max-elements\". Line number 1."); |
| 730 | |
Radek Krejci | df6cad1 | 2018-11-28 17:10:55 +0100 | [diff] [blame] | 731 | /* implementation limit */ |
| 732 | flags = value = 0; |
| 733 | str = " 4294967296; ..."; |
| 734 | assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext)); |
| 735 | logbuf_assert("Value \"4294967296\" is out of \"max-elements\" bounds. Line number 1."); |
| 736 | |
Radek Krejci | 05b1398 | 2018-11-28 16:22:07 +0100 | [diff] [blame] | 737 | flags = value = 0; |
| 738 | str = " 1; ..."; |
| 739 | assert_int_equal(LY_SUCCESS, parse_maxelements(&ctx, &str, &value, &flags, &ext)); |
| 740 | assert_int_equal(LYS_SET_MAX, flags); |
| 741 | assert_int_equal(1, value); |
| 742 | |
| 743 | flags = value = 0; |
| 744 | str = " unbounded; ..."; |
| 745 | assert_int_equal(LY_SUCCESS, parse_maxelements(&ctx, &str, &value, &flags, &ext)); |
| 746 | assert_int_equal(LYS_SET_MAX, flags); |
| 747 | assert_int_equal(0, value); |
| 748 | |
| 749 | flags = value = 0; |
| 750 | str = " 1 {m:ext;} ..."; |
| 751 | assert_int_equal(LY_SUCCESS, parse_maxelements(&ctx, &str, &value, &flags, &ext)); |
| 752 | assert_int_equal(LYS_SET_MAX, flags); |
| 753 | assert_int_equal(1, value); |
| 754 | assert_non_null(ext); |
| 755 | FREE_ARRAY(ctx.ctx, ext, lysp_ext_instance_free); |
| 756 | ext = NULL; |
| 757 | |
| 758 | flags = value = 0; |
| 759 | str = " 1 {config true;} ..."; |
| 760 | assert_int_equal(LY_EVALID, parse_maxelements(&ctx, &str, &value, &flags, &ext)); |
| 761 | logbuf_assert("Invalid keyword \"config\" as a child of \"max-elements\". Line number 1."); |
| 762 | |
| 763 | *state = NULL; |
| 764 | ly_ctx_destroy(ctx.ctx, NULL); |
| 765 | } |
| 766 | |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 767 | static struct lysp_module * |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 768 | mod_renew(struct ly_parser_ctx *ctx) |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 769 | { |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 770 | struct lysp_module *mod_p; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 771 | static struct lys_module mod = {0}; |
| 772 | |
| 773 | lysc_module_free(mod.compiled, NULL); |
| 774 | lysp_module_free(mod.parsed); |
| 775 | FREE_STRING(mod.ctx, mod.name); |
| 776 | FREE_STRING(mod.ctx, mod.ns); |
| 777 | FREE_STRING(mod.ctx, mod.prefix); |
| 778 | FREE_STRING(mod.ctx, mod.filepath); |
| 779 | FREE_STRING(mod.ctx, mod.org); |
| 780 | FREE_STRING(mod.ctx, mod.contact); |
| 781 | FREE_STRING(mod.ctx, mod.dsc); |
| 782 | FREE_STRING(mod.ctx, mod.ref); |
| 783 | memset(&mod, 0, sizeof mod); |
| 784 | mod.ctx = ctx->ctx; |
| 785 | |
| 786 | mod_p = calloc(1, sizeof *mod_p); |
| 787 | mod.parsed = mod_p; |
| 788 | mod_p->mod = &mod; |
| 789 | assert_non_null(mod_p); |
| 790 | return mod_p; |
| 791 | } |
| 792 | |
| 793 | static struct lysp_submodule * |
| 794 | submod_renew(struct ly_parser_ctx *ctx, struct lysp_submodule *submod) |
| 795 | { |
| 796 | lysp_submodule_free(ctx->ctx, submod); |
| 797 | submod = calloc(1, sizeof *submod); |
| 798 | assert_non_null(submod); |
| 799 | return submod; |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 800 | } |
| 801 | |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 802 | static LY_ERR test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name), |
| 803 | const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format, |
| 804 | const char **module_data, void (**free_module_data)(void *model_data, void *user_data)) |
| 805 | { |
| 806 | *module_data = user_data; |
| 807 | *format = LYS_IN_YANG; |
| 808 | *free_module_data = NULL; |
| 809 | return LY_SUCCESS; |
| 810 | } |
| 811 | |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 812 | static void |
| 813 | test_module(void **state) |
| 814 | { |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 815 | *state = test_module; |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 816 | |
| 817 | struct ly_parser_ctx ctx; |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 818 | struct lysp_module *mod = NULL; |
| 819 | struct lysp_submodule *submod = NULL; |
| 820 | struct lys_module *m; |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 821 | const char *str; |
| 822 | |
| 823 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 824 | assert_non_null(ctx.ctx); |
| 825 | ctx.line = 1; |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 826 | ctx.indent = 0; |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 827 | |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 828 | mod = mod_renew(&ctx); |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 829 | |
| 830 | /* missing mandatory substatements */ |
| 831 | str = " name {}"; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 832 | assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod)); |
| 833 | assert_string_equal("name", mod->mod->name); |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 834 | logbuf_assert("Missing mandatory keyword \"namespace\" as a child of \"module\". Line number 1."); |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 835 | mod = mod_renew(&ctx); |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 836 | |
| 837 | str = " name {namespace urn:x;}"; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 838 | assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod)); |
| 839 | assert_string_equal("urn:x", mod->mod->ns); |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 840 | logbuf_assert("Missing mandatory keyword \"prefix\" as a child of \"module\". Line number 1."); |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 841 | mod = mod_renew(&ctx); |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 842 | |
| 843 | str = " name {namespace urn:x;prefix \"x\";}"; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 844 | assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod)); |
| 845 | assert_string_equal("x", mod->mod->prefix); |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 846 | mod = mod_renew(&ctx); |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 847 | |
Radek Krejci | 027d580 | 2018-11-14 16:57:28 +0100 | [diff] [blame] | 848 | #define SCHEMA_BEGINNING " name {yang-version 1.1;namespace urn:x;prefix \"x\";" |
| 849 | #define SCHEMA_BEGINNING2 " name {namespace urn:x;prefix \"x\";" |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 850 | #define TEST_NODE(NODETYPE, INPUT, NAME) \ |
| 851 | str = SCHEMA_BEGINNING INPUT; \ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 852 | assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod)); \ |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 853 | assert_non_null(mod->data); \ |
| 854 | assert_int_equal(NODETYPE, mod->data->nodetype); \ |
| 855 | assert_string_equal(NAME, mod->data->name); \ |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 856 | mod = mod_renew(&ctx); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 857 | #define TEST_GENERIC(INPUT, TARGET, TEST) \ |
| 858 | str = SCHEMA_BEGINNING INPUT; \ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 859 | assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod)); \ |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 860 | assert_non_null(TARGET); \ |
| 861 | TEST; \ |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 862 | mod = mod_renew(&ctx); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 863 | #define TEST_DUP(MEMBER, VALUE1, VALUE2, LINE) \ |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 864 | TEST_DUP_GENERIC(SCHEMA_BEGINNING, MEMBER, VALUE1, VALUE2, \ |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 865 | parse_module, mod, LINE, mod = mod_renew(&ctx)) |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 866 | |
| 867 | /* duplicated namespace, prefix */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 868 | TEST_DUP("namespace", "y", "z", "1"); |
| 869 | TEST_DUP("prefix", "y", "z", "1"); |
| 870 | TEST_DUP("contact", "a", "b", "1"); |
| 871 | TEST_DUP("description", "a", "b", "1"); |
| 872 | TEST_DUP("organization", "a", "b", "1"); |
| 873 | TEST_DUP("reference", "a", "b", "1"); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 874 | |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 875 | /* not allowed in module (submodule-specific) */ |
| 876 | str = SCHEMA_BEGINNING "belongs-to master {prefix m;}}"; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 877 | assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod)); |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 878 | logbuf_assert("Invalid keyword \"belongs-to\" as a child of \"module\". Line number 1."); |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 879 | mod = mod_renew(&ctx); |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 880 | |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 881 | /* anydata */ |
| 882 | TEST_NODE(LYS_ANYDATA, "anydata test;}", "test"); |
| 883 | /* anyxml */ |
| 884 | TEST_NODE(LYS_ANYXML, "anyxml test;}", "test"); |
| 885 | /* augment */ |
| 886 | TEST_GENERIC("augment /somepath;}", mod->augments, |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 887 | assert_string_equal("/somepath", mod->augments[0].nodeid)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 888 | /* choice */ |
| 889 | TEST_NODE(LYS_CHOICE, "choice test;}", "test"); |
| 890 | /* contact 0..1 */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 891 | TEST_GENERIC("contact \"firstname\" + \n\t\" surname\";}", mod->mod->contact, |
| 892 | assert_string_equal("firstname surname", mod->mod->contact)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 893 | /* container */ |
| 894 | TEST_NODE(LYS_CONTAINER, "container test;}", "test"); |
| 895 | /* description 0..1 */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 896 | TEST_GENERIC("description \'some description\';}", mod->mod->dsc, |
| 897 | assert_string_equal("some description", mod->mod->dsc)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 898 | /* deviation */ |
| 899 | TEST_GENERIC("deviation /somepath {deviate not-supported;}}", mod->deviations, |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 900 | assert_string_equal("/somepath", mod->deviations[0].nodeid)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 901 | /* extension */ |
| 902 | TEST_GENERIC("extension test;}", mod->extensions, |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 903 | assert_string_equal("test", mod->extensions[0].name)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 904 | /* feature */ |
| 905 | TEST_GENERIC("feature test;}", mod->features, |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 906 | assert_string_equal("test", mod->features[0].name)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 907 | /* grouping */ |
| 908 | TEST_GENERIC("grouping grp;}", mod->groupings, |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 909 | assert_string_equal("grp", mod->groupings[0].name)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 910 | /* identity */ |
| 911 | TEST_GENERIC("identity test;}", mod->identities, |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 912 | assert_string_equal("test", mod->identities[0].name)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 913 | /* import */ |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 914 | ly_ctx_set_module_imp_clb(ctx.ctx, test_imp_clb, "module zzz { namespace urn:zzz; prefix z;}"); |
| 915 | TEST_GENERIC("import zzz {prefix z;}}", mod->imports, |
| 916 | assert_string_equal("zzz", mod->imports[0].name)); |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 917 | |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 918 | /* import - prefix collision */ |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 919 | str = SCHEMA_BEGINNING "import zzz {prefix x;}}"; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 920 | assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod)); |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 921 | logbuf_assert("Prefix \"x\" already used as module prefix. Line number 2."); |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 922 | mod = mod_renew(&ctx); |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 923 | str = SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix y;}}"; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 924 | assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod)); |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 925 | logbuf_assert("Prefix \"y\" already used to import \"zzz\" module. Line number 2."); |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 926 | mod = mod_renew(&ctx); |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 927 | str = "module" SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix z;}}"; |
| 928 | assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG)); |
| 929 | assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx)); |
| 930 | logbuf_assert("Single revision of the module \"zzz\" referred twice."); |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 931 | |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 932 | /* include */ |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 933 | store = 1; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 934 | 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] | 935 | str = "module" SCHEMA_BEGINNING "include xxx;}"; |
| 936 | assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG)); |
| 937 | assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx)); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 938 | logbuf_assert("Input data contains module in situation when a submodule is expected."); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 939 | store = -1; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 940 | |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 941 | store = 1; |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 942 | 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] | 943 | str = "module" SCHEMA_BEGINNING "include xxx;}"; |
| 944 | assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG)); |
| 945 | assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx)); |
| 946 | 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] | 947 | store = -1; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 948 | |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 949 | 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] | 950 | TEST_GENERIC("include xxx;}", mod->includes, |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 951 | assert_string_equal("xxx", mod->includes[0].name)); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 952 | |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 953 | /* leaf */ |
| 954 | TEST_NODE(LYS_LEAF, "leaf test {type string;}}", "test"); |
| 955 | /* leaf-list */ |
| 956 | TEST_NODE(LYS_LEAFLIST, "leaf-list test {type string;}}", "test"); |
| 957 | /* list */ |
| 958 | TEST_NODE(LYS_LIST, "list test {key a;leaf a {type string;}}}", "test"); |
| 959 | /* notification */ |
| 960 | TEST_GENERIC("notification test;}", mod->notifs, |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 961 | assert_string_equal("test", mod->notifs[0].name)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 962 | /* organization 0..1 */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 963 | TEST_GENERIC("organization \"CESNET a.l.e.\";}", mod->mod->org, |
| 964 | assert_string_equal("CESNET a.l.e.", mod->mod->org)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 965 | /* reference 0..1 */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 966 | TEST_GENERIC("reference RFC7950;}", mod->mod->ref, |
| 967 | assert_string_equal("RFC7950", mod->mod->ref)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 968 | /* revision */ |
| 969 | TEST_GENERIC("revision 2018-10-12;}", mod->revs, |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 970 | assert_string_equal("2018-10-12", mod->revs[0].date)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 971 | /* rpc */ |
| 972 | TEST_GENERIC("rpc test;}", mod->rpcs, |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 973 | assert_string_equal("test", mod->rpcs[0].name)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 974 | /* typedef */ |
| 975 | TEST_GENERIC("typedef test{type string;}}", mod->typedefs, |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 976 | assert_string_equal("test", mod->typedefs[0].name)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 977 | /* uses */ |
| 978 | TEST_NODE(LYS_USES, "uses test;}", "test"); |
| 979 | /* yang-version */ |
Radek Krejci | 027d580 | 2018-11-14 16:57:28 +0100 | [diff] [blame] | 980 | str = SCHEMA_BEGINNING2 "\n\tyang-version 10;}"; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 981 | assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 982 | logbuf_assert("Invalid value \"10\" of \"yang-version\". Line number 3."); |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 983 | mod = mod_renew(&ctx); |
Radek Krejci | 027d580 | 2018-11-14 16:57:28 +0100 | [diff] [blame] | 984 | str = SCHEMA_BEGINNING2 "yang-version 1.0;yang-version 1.1;}"; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 985 | assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 986 | logbuf_assert("Duplicate keyword \"yang-version\". Line number 3."); |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 987 | mod = mod_renew(&ctx); |
Radek Krejci | 027d580 | 2018-11-14 16:57:28 +0100 | [diff] [blame] | 988 | str = SCHEMA_BEGINNING2 "yang-version 1.0;}"; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 989 | assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod)); |
| 990 | assert_int_equal(1, mod->mod->version); |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 991 | mod = mod_renew(&ctx); |
Radek Krejci | 027d580 | 2018-11-14 16:57:28 +0100 | [diff] [blame] | 992 | str = SCHEMA_BEGINNING2 "yang-version \"1.1\";}"; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 993 | assert_int_equal(LY_SUCCESS, parse_module(&ctx, &str, mod)); |
| 994 | assert_int_equal(2, mod->mod->version); |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 995 | mod = mod_renew(&ctx); |
| 996 | |
| 997 | str = "module " SCHEMA_BEGINNING "} module q {namespace urn:q;prefixq;}"; |
| 998 | m = mod->mod; |
| 999 | free(mod); |
| 1000 | m->parsed = NULL; |
| 1001 | assert_int_equal(LY_EVALID, yang_parse_module(&ctx, str, m)); |
| 1002 | logbuf_assert("Invalid character sequence \"module\", expected end-of-file. Line number 3."); |
| 1003 | mod = mod_renew(&ctx); |
| 1004 | |
| 1005 | str = "prefix " SCHEMA_BEGINNING "}"; |
| 1006 | m = mod->mod; |
| 1007 | free(mod); |
| 1008 | m->parsed = NULL; |
| 1009 | assert_int_equal(LY_EVALID, yang_parse_module(&ctx, str, m)); |
| 1010 | logbuf_assert("Invalid keyword \"prefix\", expected \"module\" or \"submodule\". Line number 3."); |
| 1011 | mod = mod_renew(&ctx); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 1012 | |
Radek Krejci | 156ccaf | 2018-10-15 15:49:17 +0200 | [diff] [blame] | 1013 | /* extensions */ |
| 1014 | TEST_GENERIC("prefix:test;}", mod->exts, |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 1015 | assert_string_equal("prefix:test", mod->exts[0].name); |
| 1016 | assert_int_equal(LYEXT_SUBSTMT_SELF, mod->exts[0].insubstmt)); |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 1017 | mod = mod_renew(&ctx); |
Radek Krejci | 156ccaf | 2018-10-15 15:49:17 +0200 | [diff] [blame] | 1018 | |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1019 | /* invalid substatement */ |
| 1020 | str = SCHEMA_BEGINNING "must false;}"; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1021 | assert_int_equal(LY_EVALID, parse_module(&ctx, &str, mod)); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1022 | logbuf_assert("Invalid keyword \"must\" as a child of \"module\". Line number 3."); |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 1023 | mod = mod_renew(&ctx); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1024 | |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 1025 | /* submodule */ |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 1026 | submod = submod_renew(&ctx, submod); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 1027 | |
| 1028 | /* missing mandatory substatements */ |
| 1029 | str = " subname {}"; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1030 | lydict_remove(ctx.ctx, submod->name); |
| 1031 | assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod)); |
| 1032 | assert_string_equal("subname", submod->name); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 1033 | logbuf_assert("Missing mandatory keyword \"belongs-to\" as a child of \"submodule\". Line number 3."); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1034 | submod = submod_renew(&ctx, submod); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 1035 | |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 1036 | str = " subname {belongs-to name {prefix x;}}"; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1037 | lydict_remove(ctx.ctx, submod->name); |
| 1038 | assert_int_equal(LY_SUCCESS, parse_submodule(&ctx, &str, submod)); |
| 1039 | assert_string_equal("name", submod->belongsto); |
| 1040 | submod = submod_renew(&ctx, submod); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 1041 | |
| 1042 | #undef SCHEMA_BEGINNING |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 1043 | #define SCHEMA_BEGINNING " subname {belongs-to name {prefix x;}" |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 1044 | |
| 1045 | /* duplicated namespace, prefix */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1046 | str = " subname {belongs-to name {prefix x;}belongs-to module1;belongs-to module2;} ..."; |
| 1047 | assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod)); \ |
| 1048 | logbuf_assert("Duplicate keyword \"belongs-to\". Line number 3."); \ |
| 1049 | submod = submod_renew(&ctx, submod); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 1050 | |
| 1051 | /* not allowed in submodule (module-specific) */ |
| 1052 | str = SCHEMA_BEGINNING "namespace \"urn:z\";}"; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1053 | assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod)); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 1054 | logbuf_assert("Invalid keyword \"namespace\" as a child of \"submodule\". Line number 3."); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1055 | submod = submod_renew(&ctx, submod); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 1056 | str = SCHEMA_BEGINNING "prefix m;}}"; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1057 | assert_int_equal(LY_EVALID, parse_submodule(&ctx, &str, submod)); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 1058 | logbuf_assert("Invalid keyword \"prefix\" as a child of \"submodule\". Line number 3."); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1059 | submod = submod_renew(&ctx, submod); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 1060 | |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 1061 | str = "submodule " SCHEMA_BEGINNING "} module q {namespace urn:q;prefixq;}"; |
| 1062 | lysp_submodule_free(ctx.ctx, submod); |
| 1063 | submod = NULL; |
| 1064 | assert_int_equal(LY_EVALID, yang_parse_submodule(&ctx, str, &submod)); |
| 1065 | logbuf_assert("Invalid character sequence \"module\", expected end-of-file. Line number 3."); |
| 1066 | |
| 1067 | str = "prefix " SCHEMA_BEGINNING "}"; |
| 1068 | assert_int_equal(LY_EVALID, yang_parse_submodule(&ctx, str, &submod)); |
| 1069 | logbuf_assert("Invalid keyword \"prefix\", expected \"module\" or \"submodule\". Line number 3."); |
| 1070 | submod = submod_renew(&ctx, submod); |
| 1071 | |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 1072 | #undef TEST_GENERIC |
| 1073 | #undef TEST_NODE |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1074 | #undef TEST_DUP |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 1075 | #undef SCHEMA_BEGINNING |
| 1076 | |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 1077 | lysp_module_free(mod); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1078 | lysp_submodule_free(ctx.ctx, submod); |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 1079 | ly_ctx_destroy(ctx.ctx, NULL); |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 1080 | |
| 1081 | *state = NULL; |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1082 | } |
| 1083 | |
Radek Krejci | 4c6d9bd | 2018-10-15 16:43:06 +0200 | [diff] [blame] | 1084 | static void |
| 1085 | test_identity(void **state) |
| 1086 | { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1087 | *state = test_identity; |
Radek Krejci | 4c6d9bd | 2018-10-15 16:43:06 +0200 | [diff] [blame] | 1088 | |
| 1089 | struct ly_parser_ctx ctx; |
| 1090 | struct lysp_ident *ident = NULL; |
| 1091 | const char *str; |
Radek Krejci | 4c6d9bd | 2018-10-15 16:43:06 +0200 | [diff] [blame] | 1092 | |
| 1093 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 1094 | assert_non_null(ctx.ctx); |
| 1095 | ctx.line = 1; |
| 1096 | ctx.indent = 0; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1097 | ctx.mod_version = 2; /* simulate YANG 1.1 */ |
Radek Krejci | 4c6d9bd | 2018-10-15 16:43:06 +0200 | [diff] [blame] | 1098 | |
| 1099 | /* invalid cardinality */ |
| 1100 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1101 | TEST_DUP_GENERIC(" test {", MEMBER, VALUE1, VALUE2, parse_identity, \ |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 1102 | &ident, "1", FREE_ARRAY(ctx.ctx, ident, lysp_ident_free); ident = NULL) |
Radek Krejci | 4c6d9bd | 2018-10-15 16:43:06 +0200 | [diff] [blame] | 1103 | |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 1104 | TEST_DUP("description", "a", "b"); |
Radek Krejci | 4c6d9bd | 2018-10-15 16:43:06 +0200 | [diff] [blame] | 1105 | TEST_DUP("reference", "a", "b"); |
| 1106 | TEST_DUP("status", "current", "obsolete"); |
| 1107 | |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1108 | /* full content */ |
| 1109 | 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] | 1110 | assert_int_equal(LY_SUCCESS, parse_identity(&ctx, &str, &ident)); |
| 1111 | assert_non_null(ident); |
| 1112 | assert_string_equal(" ...", str); |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 1113 | FREE_ARRAY(ctx.ctx, ident, lysp_ident_free); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1114 | ident = NULL; |
| 1115 | |
| 1116 | /* invalid substatement */ |
| 1117 | str = " test {organization XXX;}"; |
| 1118 | assert_int_equal(LY_EVALID, parse_identity(&ctx, &str, &ident)); |
| 1119 | 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] | 1120 | FREE_ARRAY(ctx.ctx, ident, lysp_ident_free); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1121 | ident = NULL; |
| 1122 | |
| 1123 | #undef TEST_DUP |
| 1124 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1125 | *state = NULL; |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1126 | ly_ctx_destroy(ctx.ctx, NULL); |
| 1127 | } |
| 1128 | |
| 1129 | static void |
| 1130 | test_feature(void **state) |
| 1131 | { |
| 1132 | (void) state; /* unused */ |
| 1133 | |
| 1134 | struct ly_parser_ctx ctx; |
| 1135 | struct lysp_feature *features = NULL; |
| 1136 | const char *str; |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1137 | |
| 1138 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 1139 | assert_non_null(ctx.ctx); |
| 1140 | ctx.line = 1; |
| 1141 | ctx.indent = 0; |
| 1142 | |
| 1143 | /* invalid cardinality */ |
| 1144 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
| 1145 | TEST_DUP_GENERIC(" test {", MEMBER, VALUE1, VALUE2, parse_feature, \ |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 1146 | &features, "1", FREE_ARRAY(ctx.ctx, features, lysp_feature_free); features = NULL) |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1147 | |
| 1148 | TEST_DUP("description", "a", "b"); |
| 1149 | TEST_DUP("reference", "a", "b"); |
| 1150 | TEST_DUP("status", "current", "obsolete"); |
| 1151 | |
| 1152 | /* full content */ |
| 1153 | str = " test {description text;reference \'another text\';status current; if-feature x;if-feature y;prefix:ext;} ..."; |
| 1154 | assert_int_equal(LY_SUCCESS, parse_feature(&ctx, &str, &features)); |
| 1155 | assert_non_null(features); |
| 1156 | assert_string_equal(" ...", str); |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 1157 | FREE_ARRAY(ctx.ctx, features, lysp_feature_free); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1158 | features = NULL; |
| 1159 | |
| 1160 | /* invalid substatement */ |
| 1161 | str = " test {organization XXX;}"; |
| 1162 | assert_int_equal(LY_EVALID, parse_feature(&ctx, &str, &features)); |
| 1163 | 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] | 1164 | FREE_ARRAY(ctx.ctx, features, lysp_feature_free); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1165 | features = NULL; |
| 1166 | |
| 1167 | #undef TEST_DUP |
| 1168 | |
| 1169 | ly_ctx_destroy(ctx.ctx, NULL); |
| 1170 | } |
| 1171 | |
| 1172 | static void |
| 1173 | test_deviation(void **state) |
| 1174 | { |
| 1175 | (void) state; /* unused */ |
| 1176 | |
| 1177 | struct ly_parser_ctx ctx; |
| 1178 | struct lysp_deviation *d = NULL; |
| 1179 | const char *str; |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1180 | |
| 1181 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 1182 | assert_non_null(ctx.ctx); |
| 1183 | ctx.line = 1; |
| 1184 | ctx.indent = 0; |
| 1185 | |
| 1186 | /* invalid cardinality */ |
| 1187 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
| 1188 | TEST_DUP_GENERIC(" test {deviate not-supported;", MEMBER, VALUE1, VALUE2, parse_deviation, \ |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 1189 | &d, "1", FREE_ARRAY(ctx.ctx, d, lysp_deviation_free); d = NULL) |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1190 | |
| 1191 | TEST_DUP("description", "a", "b"); |
| 1192 | TEST_DUP("reference", "a", "b"); |
| 1193 | |
| 1194 | /* full content */ |
| 1195 | str = " test {deviate not-supported;description text;reference \'another text\';prefix:ext;} ..."; |
| 1196 | assert_int_equal(LY_SUCCESS, parse_deviation(&ctx, &str, &d)); |
| 1197 | assert_non_null(d); |
| 1198 | assert_string_equal(" ...", str); |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 1199 | FREE_ARRAY(ctx.ctx, d, lysp_deviation_free); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1200 | d = NULL; |
| 1201 | |
| 1202 | /* missing mandatory substatement */ |
| 1203 | str = " test {description text;}"; |
| 1204 | assert_int_equal(LY_EVALID, parse_deviation(&ctx, &str, &d)); |
| 1205 | 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] | 1206 | FREE_ARRAY(ctx.ctx, d, lysp_deviation_free); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1207 | d = NULL; |
| 1208 | |
| 1209 | /* invalid substatement */ |
| 1210 | str = " test {deviate not-supported; status obsolete;}"; |
| 1211 | assert_int_equal(LY_EVALID, parse_deviation(&ctx, &str, &d)); |
| 1212 | 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] | 1213 | FREE_ARRAY(ctx.ctx, d, lysp_deviation_free); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1214 | d = NULL; |
| 1215 | |
| 1216 | #undef TEST_DUP |
| 1217 | |
| 1218 | ly_ctx_destroy(ctx.ctx, NULL); |
| 1219 | } |
| 1220 | |
| 1221 | static void |
| 1222 | test_deviate(void **state) |
| 1223 | { |
| 1224 | (void) state; /* unused */ |
| 1225 | |
| 1226 | struct ly_parser_ctx ctx; |
| 1227 | struct lysp_deviate *d = NULL; |
| 1228 | const char *str; |
| 1229 | |
| 1230 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 1231 | assert_non_null(ctx.ctx); |
| 1232 | ctx.line = 1; |
| 1233 | ctx.indent = 0; |
| 1234 | |
| 1235 | /* invalid cardinality */ |
| 1236 | #define TEST_DUP(TYPE, MEMBER, VALUE1, VALUE2) \ |
| 1237 | TEST_DUP_GENERIC(TYPE" {", MEMBER, VALUE1, VALUE2, parse_deviate, \ |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 1238 | &d, "1", lysp_deviate_free(ctx.ctx, d); free(d); d = NULL) |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1239 | |
| 1240 | TEST_DUP("add", "config", "true", "false"); |
| 1241 | TEST_DUP("replace", "default", "int8", "uint8"); |
| 1242 | TEST_DUP("add", "mandatory", "true", "false"); |
| 1243 | TEST_DUP("add", "max-elements", "1", "2"); |
| 1244 | TEST_DUP("add", "min-elements", "1", "2"); |
| 1245 | TEST_DUP("replace", "type", "int8", "uint8"); |
| 1246 | TEST_DUP("add", "units", "kilometers", "miles"); |
| 1247 | |
| 1248 | /* full contents */ |
| 1249 | str = " not-supported {prefix:ext;} ..."; |
| 1250 | assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d)); |
| 1251 | assert_non_null(d); |
| 1252 | assert_string_equal(" ...", str); |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 1253 | lysp_deviate_free(ctx.ctx, d); free(d); d = NULL; |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1254 | 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;} ..."; |
| 1255 | assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d)); |
| 1256 | assert_non_null(d); |
| 1257 | assert_string_equal(" ...", str); |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 1258 | lysp_deviate_free(ctx.ctx, d); free(d); d = NULL; |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1259 | str = " delete {units meters; must 1; must 2; unique x; unique y; default a; default b; prefix:ext;} ..."; |
| 1260 | assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d)); |
| 1261 | assert_non_null(d); |
| 1262 | assert_string_equal(" ...", str); |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 1263 | lysp_deviate_free(ctx.ctx, d); free(d); d = NULL; |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1264 | str = " replace {type string; units meters; default a; config true; mandatory true; min-elements 1; max-elements 2; prefix:ext;} ..."; |
| 1265 | assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d)); |
| 1266 | assert_non_null(d); |
| 1267 | assert_string_equal(" ...", str); |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 1268 | lysp_deviate_free(ctx.ctx, d); free(d); d = NULL; |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1269 | |
| 1270 | /* invalid substatements */ |
| 1271 | #define TEST_NOT_SUP(DEV, STMT, VALUE) \ |
| 1272 | str = " "DEV" {"STMT" "VALUE";}..."; \ |
| 1273 | assert_int_equal(LY_EVALID, parse_deviate(&ctx, &str, &d)); \ |
| 1274 | logbuf_assert("Deviate \""DEV"\" does not support keyword \""STMT"\". Line number 1."); \ |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 1275 | lysp_deviate_free(ctx.ctx, d); free(d); d = NULL |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1276 | |
| 1277 | TEST_NOT_SUP("not-supported", "units", "meters"); |
| 1278 | TEST_NOT_SUP("not-supported", "must", "1"); |
| 1279 | TEST_NOT_SUP("not-supported", "unique", "x"); |
| 1280 | TEST_NOT_SUP("not-supported", "default", "a"); |
| 1281 | TEST_NOT_SUP("not-supported", "config", "true"); |
| 1282 | TEST_NOT_SUP("not-supported", "mandatory", "true"); |
| 1283 | TEST_NOT_SUP("not-supported", "min-elements", "1"); |
| 1284 | TEST_NOT_SUP("not-supported", "max-elements", "2"); |
| 1285 | TEST_NOT_SUP("not-supported", "type", "string"); |
| 1286 | TEST_NOT_SUP("add", "type", "string"); |
| 1287 | TEST_NOT_SUP("delete", "config", "true"); |
| 1288 | TEST_NOT_SUP("delete", "mandatory", "true"); |
| 1289 | TEST_NOT_SUP("delete", "min-elements", "1"); |
| 1290 | TEST_NOT_SUP("delete", "max-elements", "2"); |
| 1291 | TEST_NOT_SUP("delete", "type", "string"); |
| 1292 | TEST_NOT_SUP("replace", "must", "1"); |
| 1293 | TEST_NOT_SUP("replace", "unique", "a"); |
| 1294 | |
| 1295 | str = " nonsence; ..."; |
| 1296 | assert_int_equal(LY_EVALID, parse_deviate(&ctx, &str, &d)); |
| 1297 | logbuf_assert("Invalid value \"nonsence\" of \"deviate\". Line number 1."); |
| 1298 | assert_null(d); |
| 1299 | |
| 1300 | #undef TEST_NOT_SUP |
| 1301 | #undef TEST_DUP |
Radek Krejci | 4c6d9bd | 2018-10-15 16:43:06 +0200 | [diff] [blame] | 1302 | |
| 1303 | ly_ctx_destroy(ctx.ctx, NULL); |
| 1304 | } |
| 1305 | |
Radek Krejci | 8c37083 | 2018-11-02 15:10:03 +0100 | [diff] [blame] | 1306 | static void |
| 1307 | test_container(void **state) |
| 1308 | { |
| 1309 | (void) state; /* unused */ |
| 1310 | |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 1311 | struct ly_parser_ctx ctx = {0}; |
Radek Krejci | 8c37083 | 2018-11-02 15:10:03 +0100 | [diff] [blame] | 1312 | struct lysp_node_container *c = NULL; |
| 1313 | const char *str; |
| 1314 | |
| 1315 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 1316 | assert_non_null(ctx.ctx); |
| 1317 | ctx.line = 1; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1318 | ctx.mod_version = 2; /* simulate YANG 1.1 */ |
Radek Krejci | 8c37083 | 2018-11-02 15:10:03 +0100 | [diff] [blame] | 1319 | |
| 1320 | /* invalid cardinality */ |
| 1321 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
| 1322 | str = "cont {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
| 1323 | assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c)); \ |
| 1324 | logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \ |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 1325 | lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL; |
Radek Krejci | 8c37083 | 2018-11-02 15:10:03 +0100 | [diff] [blame] | 1326 | |
| 1327 | TEST_DUP("config", "true", "false"); |
| 1328 | TEST_DUP("description", "text1", "text2"); |
| 1329 | TEST_DUP("presence", "true", "false"); |
| 1330 | TEST_DUP("reference", "1", "2"); |
| 1331 | TEST_DUP("status", "current", "obsolete"); |
| 1332 | TEST_DUP("when", "true", "false"); |
| 1333 | #undef TEST_DUP |
| 1334 | |
| 1335 | /* full content */ |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 1336 | 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;}" |
| 1337 | "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] | 1338 | assert_int_equal(LY_SUCCESS, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c)); |
| 1339 | assert_non_null(c); |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 1340 | assert_int_equal(LYS_CONTAINER, c->nodetype); |
| 1341 | assert_string_equal("cont", c->name); |
Radek Krejci | 8c37083 | 2018-11-02 15:10:03 +0100 | [diff] [blame] | 1342 | assert_non_null(c->actions); |
| 1343 | assert_non_null(c->child); |
| 1344 | assert_string_equal("test", c->dsc); |
| 1345 | assert_non_null(c->exts); |
| 1346 | assert_non_null(c->groupings); |
| 1347 | assert_non_null(c->iffeatures); |
| 1348 | assert_non_null(c->musts); |
| 1349 | assert_non_null(c->notifs); |
| 1350 | assert_string_equal("true", c->presence); |
| 1351 | assert_string_equal("test", c->ref); |
| 1352 | assert_non_null(c->typedefs); |
| 1353 | assert_non_null(c->when); |
| 1354 | assert_null(c->parent); |
| 1355 | assert_null(c->next); |
| 1356 | assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR, c->flags); |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 1357 | ly_set_erase(&ctx.tpdfs_nodes, NULL); |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 1358 | lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL; |
Radek Krejci | 8c37083 | 2018-11-02 15:10:03 +0100 | [diff] [blame] | 1359 | |
| 1360 | /* invalid */ |
| 1361 | str = " cont {augment /root;} ..."; |
| 1362 | assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c)); |
| 1363 | 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] | 1364 | lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL; |
Radek Krejci | 8c37083 | 2018-11-02 15:10:03 +0100 | [diff] [blame] | 1365 | str = " cont {nonsence true;} ..."; |
| 1366 | assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c)); |
| 1367 | logbuf_assert("Invalid character sequence \"nonsence\", expected a keyword. Line number 1."); |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 1368 | lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL; |
Radek Krejci | 8c37083 | 2018-11-02 15:10:03 +0100 | [diff] [blame] | 1369 | |
| 1370 | ly_ctx_destroy(ctx.ctx, NULL); |
| 1371 | } |
| 1372 | |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 1373 | static void |
| 1374 | test_leaf(void **state) |
| 1375 | { |
| 1376 | *state = test_leaf; |
| 1377 | |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 1378 | struct ly_parser_ctx ctx = {0}; |
| 1379 | struct lysp_node_leaf *l = NULL; |
| 1380 | const char *str; |
| 1381 | |
| 1382 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 1383 | assert_non_null(ctx.ctx); |
| 1384 | ctx.line = 1; |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 1385 | //ctx.mod->version = 2; /* simulate YANG 1.1 */ |
| 1386 | |
| 1387 | /* invalid cardinality */ |
| 1388 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
| 1389 | str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
| 1390 | assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l)); \ |
| 1391 | logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \ |
| 1392 | lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL; |
| 1393 | |
| 1394 | TEST_DUP("config", "true", "false"); |
| 1395 | TEST_DUP("default", "x", "y"); |
| 1396 | TEST_DUP("description", "text1", "text2"); |
| 1397 | TEST_DUP("mandatory", "true", "false"); |
| 1398 | TEST_DUP("reference", "1", "2"); |
| 1399 | TEST_DUP("status", "current", "obsolete"); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 1400 | TEST_DUP("type", "int8", "uint8"); |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 1401 | TEST_DUP("units", "text1", "text2"); |
| 1402 | TEST_DUP("when", "true", "false"); |
| 1403 | #undef TEST_DUP |
| 1404 | |
| 1405 | /* full content - without mandatory which is mutual exclusive with default */ |
| 1406 | str = "l {config false;default \"xxx\";description test;if-feature f;" |
| 1407 | "must 'expr';reference test;status current;type string; units yyy;when true;m:ext;} ..."; |
| 1408 | assert_int_equal(LY_SUCCESS, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l)); |
| 1409 | assert_non_null(l); |
| 1410 | assert_int_equal(LYS_LEAF, l->nodetype); |
| 1411 | assert_string_equal("l", l->name); |
| 1412 | assert_string_equal("test", l->dsc); |
| 1413 | assert_string_equal("xxx", l->dflt); |
| 1414 | assert_string_equal("yyy", l->units); |
| 1415 | assert_string_equal("string", l->type.name); |
| 1416 | assert_non_null(l->exts); |
| 1417 | assert_non_null(l->iffeatures); |
| 1418 | assert_non_null(l->musts); |
| 1419 | assert_string_equal("test", l->ref); |
| 1420 | assert_non_null(l->when); |
| 1421 | assert_null(l->parent); |
| 1422 | assert_null(l->next); |
| 1423 | assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR, l->flags); |
| 1424 | lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL; |
| 1425 | |
| 1426 | /* full content - now with mandatory */ |
| 1427 | str = "l {mandatory true; type string;} ..."; |
| 1428 | assert_int_equal(LY_SUCCESS, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l)); |
| 1429 | assert_non_null(l); |
| 1430 | assert_int_equal(LYS_LEAF, l->nodetype); |
| 1431 | assert_string_equal("l", l->name); |
| 1432 | assert_string_equal("string", l->type.name); |
| 1433 | assert_int_equal(LYS_MAND_TRUE, l->flags); |
| 1434 | lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL; |
| 1435 | |
| 1436 | /* invalid */ |
| 1437 | str = " l {mandatory true; default xx; type string;} ..."; |
| 1438 | assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l)); |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 1439 | logbuf_assert("Invalid combination of keywords \"mandatory\" and \"default\" as substatements of \"leaf\". Line number 1."); |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 1440 | lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL; |
| 1441 | |
| 1442 | str = " l {description \"missing type\";} ..."; |
| 1443 | assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l)); |
| 1444 | logbuf_assert("Missing mandatory keyword \"type\" as a child of \"leaf\". Line number 1."); |
| 1445 | lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL; |
| 1446 | |
| 1447 | *state = NULL; |
| 1448 | ly_ctx_destroy(ctx.ctx, NULL); |
| 1449 | } |
| 1450 | |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 1451 | static void |
| 1452 | test_leaflist(void **state) |
| 1453 | { |
| 1454 | *state = test_leaf; |
| 1455 | |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 1456 | struct ly_parser_ctx ctx = {0}; |
| 1457 | struct lysp_node_leaflist *ll = NULL; |
| 1458 | const char *str; |
| 1459 | |
| 1460 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 1461 | assert_non_null(ctx.ctx); |
| 1462 | ctx.line = 1; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1463 | ctx.mod_version = 2; /* simulate YANG 1.1 */ |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 1464 | |
| 1465 | /* invalid cardinality */ |
| 1466 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
| 1467 | str = "ll {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
| 1468 | assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll)); \ |
| 1469 | logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \ |
| 1470 | lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL; |
| 1471 | |
| 1472 | TEST_DUP("config", "true", "false"); |
| 1473 | TEST_DUP("description", "text1", "text2"); |
| 1474 | TEST_DUP("max-elements", "10", "20"); |
| 1475 | TEST_DUP("min-elements", "10", "20"); |
| 1476 | TEST_DUP("ordered-by", "user", "system"); |
| 1477 | TEST_DUP("reference", "1", "2"); |
| 1478 | TEST_DUP("status", "current", "obsolete"); |
| 1479 | TEST_DUP("type", "int8", "uint8"); |
| 1480 | TEST_DUP("units", "text1", "text2"); |
| 1481 | TEST_DUP("when", "true", "false"); |
| 1482 | #undef TEST_DUP |
| 1483 | |
| 1484 | /* full content - without min-elements which is mutual exclusive with default */ |
| 1485 | str = "ll {config false;default \"xxx\"; default \"yyy\";description test;if-feature f;" |
| 1486 | "max-elements 10;must 'expr';ordered-by user;reference test;" |
| 1487 | "status current;type string; units zzz;when true;m:ext;} ..."; |
| 1488 | assert_int_equal(LY_SUCCESS, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll)); |
| 1489 | assert_non_null(ll); |
| 1490 | assert_int_equal(LYS_LEAFLIST, ll->nodetype); |
| 1491 | assert_string_equal("ll", ll->name); |
| 1492 | assert_string_equal("test", ll->dsc); |
| 1493 | assert_non_null(ll->dflts); |
| 1494 | assert_int_equal(2, LY_ARRAY_SIZE(ll->dflts)); |
| 1495 | assert_string_equal("xxx", ll->dflts[0]); |
| 1496 | assert_string_equal("yyy", ll->dflts[1]); |
| 1497 | assert_string_equal("zzz", ll->units); |
| 1498 | assert_int_equal(10, ll->max); |
| 1499 | assert_int_equal(0, ll->min); |
| 1500 | assert_string_equal("string", ll->type.name); |
| 1501 | assert_non_null(ll->exts); |
| 1502 | assert_non_null(ll->iffeatures); |
| 1503 | assert_non_null(ll->musts); |
| 1504 | assert_string_equal("test", ll->ref); |
| 1505 | assert_non_null(ll->when); |
| 1506 | assert_null(ll->parent); |
| 1507 | assert_null(ll->next); |
| 1508 | assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_USER | LYS_SET_MAX, ll->flags); |
| 1509 | lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL; |
| 1510 | |
| 1511 | /* full content - now with min-elements */ |
| 1512 | str = "ll {min-elements 10; type string;} ..."; |
| 1513 | assert_int_equal(LY_SUCCESS, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll)); |
| 1514 | assert_non_null(ll); |
| 1515 | assert_int_equal(LYS_LEAFLIST, ll->nodetype); |
| 1516 | assert_string_equal("ll", ll->name); |
| 1517 | assert_string_equal("string", ll->type.name); |
| 1518 | assert_int_equal(0, ll->max); |
| 1519 | assert_int_equal(10, ll->min); |
| 1520 | assert_int_equal(LYS_SET_MIN, ll->flags); |
| 1521 | lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL; |
| 1522 | |
| 1523 | /* invalid */ |
| 1524 | str = " ll {min-elements 1; default xx; type string;} ..."; |
| 1525 | assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll)); |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 1526 | logbuf_assert("Invalid combination of keywords \"min-elements\" and \"default\" as substatements of \"leaf-list\". Line number 1."); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 1527 | lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL; |
| 1528 | |
| 1529 | str = " ll {description \"missing type\";} ..."; |
| 1530 | assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll)); |
| 1531 | logbuf_assert("Missing mandatory keyword \"type\" as a child of \"leaf-list\". Line number 1."); |
| 1532 | lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL; |
| 1533 | |
Radek Krejci | df6cad1 | 2018-11-28 17:10:55 +0100 | [diff] [blame] | 1534 | str = " ll {type string; min-elements 10; max-elements 1;} ..."; /* invalid combination of min/max */ |
| 1535 | assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll)); |
| 1536 | logbuf_assert("Invalid combination of min-elements and max-elements: min value 10 is bigger than the max value 1. Line number 1."); |
| 1537 | lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL; |
| 1538 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1539 | ctx.mod_version = 1; /* simulate YANG 1.0 - default statement is not allowed */ |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 1540 | str = " ll {default xx; type string;} ..."; |
| 1541 | assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll)); |
| 1542 | logbuf_assert("Invalid keyword \"default\" as a child of \"leaf-list\" - the statement is allowed only in YANG 1.1 modules. Line number 1."); |
| 1543 | lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL; |
| 1544 | |
| 1545 | *state = NULL; |
| 1546 | ly_ctx_destroy(ctx.ctx, NULL); |
| 1547 | } |
| 1548 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 1549 | static void |
| 1550 | test_list(void **state) |
| 1551 | { |
| 1552 | *state = test_list; |
| 1553 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 1554 | struct ly_parser_ctx ctx = {0}; |
| 1555 | struct lysp_node_list *l = NULL; |
| 1556 | const char *str; |
| 1557 | |
| 1558 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 1559 | assert_non_null(ctx.ctx); |
| 1560 | ctx.line = 1; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1561 | ctx.mod_version = 2; /* simulate YANG 1.1 */ |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 1562 | |
| 1563 | /* invalid cardinality */ |
| 1564 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
| 1565 | str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
| 1566 | assert_int_equal(LY_EVALID, parse_list(&ctx, &str, NULL, (struct lysp_node**)&l)); \ |
| 1567 | logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \ |
| 1568 | lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL; |
| 1569 | |
| 1570 | TEST_DUP("config", "true", "false"); |
| 1571 | TEST_DUP("description", "text1", "text2"); |
| 1572 | TEST_DUP("key", "one", "two"); |
| 1573 | TEST_DUP("max-elements", "10", "20"); |
| 1574 | TEST_DUP("min-elements", "10", "20"); |
| 1575 | TEST_DUP("ordered-by", "user", "system"); |
| 1576 | TEST_DUP("reference", "1", "2"); |
| 1577 | TEST_DUP("status", "current", "obsolete"); |
| 1578 | TEST_DUP("when", "true", "false"); |
| 1579 | #undef TEST_DUP |
| 1580 | |
| 1581 | /* full content */ |
| 1582 | str = "l {action x;anydata any;anyxml anyxml; choice ch;config false;container c;description test;grouping g;if-feature f; key l; leaf l {type string;}" |
| 1583 | "leaf-list ll {type string;} list li;max-elements 10; min-elements 1;must 'expr';notification not; ordered-by system; reference test;" |
| 1584 | "status current;typedef t {type int8;}unique xxx;unique yyy;uses g;when true;m:ext;} ..."; |
| 1585 | assert_int_equal(LY_SUCCESS, parse_list(&ctx, &str, NULL, (struct lysp_node**)&l)); |
| 1586 | assert_non_null(l); |
| 1587 | assert_int_equal(LYS_LIST, l->nodetype); |
| 1588 | assert_string_equal("l", l->name); |
| 1589 | assert_string_equal("test", l->dsc); |
| 1590 | assert_string_equal("l", l->key); |
| 1591 | assert_non_null(l->uniques); |
| 1592 | assert_int_equal(2, LY_ARRAY_SIZE(l->uniques)); |
| 1593 | assert_string_equal("xxx", l->uniques[0]); |
| 1594 | assert_string_equal("yyy", l->uniques[1]); |
| 1595 | assert_int_equal(10, l->max); |
| 1596 | assert_int_equal(1, l->min); |
| 1597 | assert_non_null(l->exts); |
| 1598 | assert_non_null(l->iffeatures); |
| 1599 | assert_non_null(l->musts); |
| 1600 | assert_string_equal("test", l->ref); |
| 1601 | assert_non_null(l->when); |
| 1602 | assert_null(l->parent); |
| 1603 | assert_null(l->next); |
| 1604 | assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM | LYS_SET_MAX | LYS_SET_MIN, l->flags); |
| 1605 | ly_set_erase(&ctx.tpdfs_nodes, NULL); |
| 1606 | lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL; |
| 1607 | |
| 1608 | *state = NULL; |
| 1609 | ly_ctx_destroy(ctx.ctx, NULL); |
| 1610 | } |
| 1611 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1612 | static void |
| 1613 | test_choice(void **state) |
| 1614 | { |
| 1615 | *state = test_choice; |
| 1616 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1617 | struct ly_parser_ctx ctx = {0}; |
| 1618 | struct lysp_node_choice *ch = NULL; |
| 1619 | const char *str; |
| 1620 | |
| 1621 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 1622 | assert_non_null(ctx.ctx); |
| 1623 | ctx.line = 1; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1624 | ctx.mod_version = 2; /* simulate YANG 1.1 */ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1625 | |
| 1626 | /* invalid cardinality */ |
| 1627 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
| 1628 | str = "ch {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
| 1629 | assert_int_equal(LY_EVALID, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch)); \ |
| 1630 | logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \ |
| 1631 | lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL; |
| 1632 | |
| 1633 | TEST_DUP("config", "true", "false"); |
| 1634 | TEST_DUP("default", "a", "b"); |
| 1635 | TEST_DUP("description", "text1", "text2"); |
| 1636 | TEST_DUP("mandatory", "true", "false"); |
| 1637 | TEST_DUP("reference", "1", "2"); |
| 1638 | TEST_DUP("status", "current", "obsolete"); |
| 1639 | TEST_DUP("when", "true", "false"); |
| 1640 | #undef TEST_DUP |
| 1641 | |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 1642 | /* full content - without default due to a collision with mandatory */ |
| 1643 | str = "ch {anydata any;anyxml anyxml; case c;choice ch;config false;container c;description test;if-feature f;leaf l {type string;}" |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1644 | "leaf-list ll {type string;} list li;mandatory true;reference test;status current;when true;m:ext;} ..."; |
| 1645 | assert_int_equal(LY_SUCCESS, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch)); |
| 1646 | assert_non_null(ch); |
| 1647 | assert_int_equal(LYS_CHOICE, ch->nodetype); |
| 1648 | assert_string_equal("ch", ch->name); |
| 1649 | assert_string_equal("test", ch->dsc); |
| 1650 | assert_non_null(ch->exts); |
| 1651 | assert_non_null(ch->iffeatures); |
| 1652 | assert_string_equal("test", ch->ref); |
| 1653 | assert_non_null(ch->when); |
| 1654 | assert_null(ch->parent); |
| 1655 | assert_null(ch->next); |
| 1656 | assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_MAND_TRUE, ch->flags); |
| 1657 | lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL; |
| 1658 | |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 1659 | /* full content - the default missing from the previous node */ |
| 1660 | str = "ch {default c;case c;} ..."; |
| 1661 | assert_int_equal(LY_SUCCESS, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch)); |
| 1662 | assert_non_null(ch); |
| 1663 | assert_int_equal(LYS_CHOICE, ch->nodetype); |
| 1664 | assert_string_equal("ch", ch->name); |
| 1665 | assert_string_equal("c", ch->dflt); |
| 1666 | assert_int_equal(0, ch->flags); |
| 1667 | lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL; |
| 1668 | |
| 1669 | /* invalid content */ |
| 1670 | str = "ch {mandatory true; default c1; case c1 {leaf x{type string;}}} ..."; |
| 1671 | assert_int_equal(LY_EVALID, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch)); |
| 1672 | logbuf_assert("Invalid combination of keywords \"mandatory\" and \"default\" as substatements of \"choice\". Line number 1."); |
| 1673 | lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL; |
| 1674 | |
| 1675 | *state = NULL; |
| 1676 | ly_ctx_destroy(ctx.ctx, NULL); |
| 1677 | } |
| 1678 | |
| 1679 | static void |
| 1680 | test_case(void **state) |
| 1681 | { |
| 1682 | *state = test_case; |
| 1683 | |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 1684 | struct ly_parser_ctx ctx = {0}; |
| 1685 | struct lysp_node_case *cs = NULL; |
| 1686 | const char *str; |
| 1687 | |
| 1688 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 1689 | assert_non_null(ctx.ctx); |
| 1690 | ctx.line = 1; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1691 | ctx.mod_version = 2; /* simulate YANG 1.1 */ |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 1692 | |
| 1693 | /* invalid cardinality */ |
| 1694 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
| 1695 | str = "cs {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
| 1696 | assert_int_equal(LY_EVALID, parse_case(&ctx, &str, NULL, (struct lysp_node**)&cs)); \ |
| 1697 | logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \ |
| 1698 | lysp_node_free(ctx.ctx, (struct lysp_node*)cs); cs = NULL; |
| 1699 | |
| 1700 | TEST_DUP("description", "text1", "text2"); |
| 1701 | TEST_DUP("reference", "1", "2"); |
| 1702 | TEST_DUP("status", "current", "obsolete"); |
| 1703 | TEST_DUP("when", "true", "false"); |
| 1704 | #undef TEST_DUP |
| 1705 | |
| 1706 | /* full content */ |
| 1707 | str = "cs {anydata any;anyxml anyxml; choice ch;container c;description test;if-feature f;leaf l {type string;}" |
| 1708 | "leaf-list ll {type string;} list li;reference test;status current;uses grp;when true;m:ext;} ..."; |
| 1709 | assert_int_equal(LY_SUCCESS, parse_case(&ctx, &str, NULL, (struct lysp_node**)&cs)); |
| 1710 | assert_non_null(cs); |
| 1711 | assert_int_equal(LYS_CASE, cs->nodetype); |
| 1712 | assert_string_equal("cs", cs->name); |
| 1713 | assert_string_equal("test", cs->dsc); |
| 1714 | assert_non_null(cs->exts); |
| 1715 | assert_non_null(cs->iffeatures); |
| 1716 | assert_string_equal("test", cs->ref); |
| 1717 | assert_non_null(cs->when); |
| 1718 | assert_null(cs->parent); |
| 1719 | assert_null(cs->next); |
| 1720 | assert_int_equal(LYS_STATUS_CURR, cs->flags); |
| 1721 | lysp_node_free(ctx.ctx, (struct lysp_node*)cs); cs = NULL; |
| 1722 | |
| 1723 | /* invalid content */ |
| 1724 | str = "cs {config true} ..."; |
| 1725 | assert_int_equal(LY_EVALID, parse_case(&ctx, &str, NULL, (struct lysp_node**)&cs)); |
| 1726 | logbuf_assert("Invalid keyword \"config\" as a child of \"case\". Line number 1."); |
| 1727 | lysp_node_free(ctx.ctx, (struct lysp_node*)cs); cs = NULL; |
| 1728 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1729 | *state = NULL; |
| 1730 | ly_ctx_destroy(ctx.ctx, NULL); |
| 1731 | } |
| 1732 | |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 1733 | static void |
| 1734 | test_any(void **state, enum yang_keyword kw) |
| 1735 | { |
| 1736 | *state = test_any; |
| 1737 | |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 1738 | struct ly_parser_ctx ctx = {0}; |
| 1739 | struct lysp_node_anydata *any = NULL; |
| 1740 | const char *str; |
| 1741 | |
| 1742 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 1743 | assert_non_null(ctx.ctx); |
| 1744 | ctx.line = 1; |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 1745 | if (kw == YANG_ANYDATA) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1746 | ctx.mod_version = 2; /* simulate YANG 1.1 */ |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 1747 | } else { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1748 | ctx.mod_version = 1; /* simulate YANG 1.0 */ |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 1749 | } |
| 1750 | |
| 1751 | /* invalid cardinality */ |
| 1752 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
| 1753 | str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
| 1754 | assert_int_equal(LY_EVALID, parse_any(&ctx, &str, kw, NULL, (struct lysp_node**)&any)); \ |
| 1755 | logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \ |
| 1756 | lysp_node_free(ctx.ctx, (struct lysp_node*)any); any = NULL; |
| 1757 | |
| 1758 | TEST_DUP("config", "true", "false"); |
| 1759 | TEST_DUP("description", "text1", "text2"); |
| 1760 | TEST_DUP("mandatory", "true", "false"); |
| 1761 | TEST_DUP("reference", "1", "2"); |
| 1762 | TEST_DUP("status", "current", "obsolete"); |
| 1763 | TEST_DUP("when", "true", "false"); |
| 1764 | #undef TEST_DUP |
| 1765 | |
| 1766 | /* full content */ |
| 1767 | str = "any {config true;description test;if-feature f;mandatory true;must 'expr';reference test;status current;when true;m:ext;} ..."; |
| 1768 | assert_int_equal(LY_SUCCESS, parse_any(&ctx, &str, kw, NULL, (struct lysp_node**)&any)); |
| 1769 | assert_non_null(any); |
| 1770 | assert_int_equal(kw == YANG_ANYDATA ? LYS_ANYDATA : LYS_ANYXML, any->nodetype); |
| 1771 | assert_string_equal("any", any->name); |
| 1772 | assert_string_equal("test", any->dsc); |
| 1773 | assert_non_null(any->exts); |
| 1774 | assert_non_null(any->iffeatures); |
| 1775 | assert_non_null(any->musts); |
| 1776 | assert_string_equal("test", any->ref); |
| 1777 | assert_non_null(any->when); |
| 1778 | assert_null(any->parent); |
| 1779 | assert_null(any->next); |
| 1780 | assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR | LYS_MAND_TRUE, any->flags); |
| 1781 | lysp_node_free(ctx.ctx, (struct lysp_node*)any); any = NULL; |
| 1782 | |
| 1783 | *state = NULL; |
| 1784 | ly_ctx_destroy(ctx.ctx, NULL); |
| 1785 | } |
| 1786 | |
| 1787 | static void |
| 1788 | test_anydata(void **state) |
| 1789 | { |
| 1790 | return test_any(state, YANG_ANYDATA); |
| 1791 | } |
| 1792 | |
| 1793 | static void |
| 1794 | test_anyxml(void **state) |
| 1795 | { |
| 1796 | return test_any(state, YANG_ANYXML); |
| 1797 | } |
| 1798 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1799 | static void |
| 1800 | test_grouping(void **state) |
| 1801 | { |
| 1802 | *state = test_grouping; |
| 1803 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1804 | struct ly_parser_ctx ctx = {0}; |
| 1805 | struct lysp_grp *grp = NULL; |
| 1806 | const char *str; |
| 1807 | |
| 1808 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 1809 | assert_non_null(ctx.ctx); |
| 1810 | ctx.line = 1; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1811 | ctx.mod_version = 2; /* simulate YANG 1.1 */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1812 | |
| 1813 | /* invalid cardinality */ |
| 1814 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
| 1815 | str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
| 1816 | assert_int_equal(LY_EVALID, parse_grouping(&ctx, &str, NULL, &grp)); \ |
| 1817 | logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \ |
| 1818 | FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL; |
| 1819 | |
| 1820 | TEST_DUP("description", "text1", "text2"); |
| 1821 | TEST_DUP("reference", "1", "2"); |
| 1822 | TEST_DUP("status", "current", "obsolete"); |
| 1823 | #undef TEST_DUP |
| 1824 | |
| 1825 | /* full content */ |
| 1826 | str = "grp {action x;anydata any;anyxml anyxml; choice ch;container c;description test;grouping g;leaf l {type string;}" |
| 1827 | "leaf-list ll {type string;} list li;notification not;reference test;status current;typedef t {type int8;}uses g;m:ext;} ..."; |
| 1828 | assert_int_equal(LY_SUCCESS, parse_grouping(&ctx, &str, NULL, &grp)); |
| 1829 | assert_non_null(grp); |
| 1830 | assert_int_equal(LYS_GROUPING, grp->nodetype); |
| 1831 | assert_string_equal("grp", grp->name); |
| 1832 | assert_string_equal("test", grp->dsc); |
| 1833 | assert_non_null(grp->exts); |
| 1834 | assert_string_equal("test", grp->ref); |
| 1835 | assert_null(grp->parent); |
| 1836 | assert_int_equal( LYS_STATUS_CURR, grp->flags); |
| 1837 | ly_set_erase(&ctx.tpdfs_nodes, NULL); |
| 1838 | FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL; |
| 1839 | |
| 1840 | /* invalid content */ |
| 1841 | str = "grp {config true} ..."; |
| 1842 | assert_int_equal(LY_EVALID, parse_grouping(&ctx, &str, NULL, &grp)); |
| 1843 | logbuf_assert("Invalid keyword \"config\" as a child of \"grouping\". Line number 1."); |
| 1844 | FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL; |
| 1845 | |
| 1846 | str = "grp {must 'expr'} ..."; |
| 1847 | assert_int_equal(LY_EVALID, parse_grouping(&ctx, &str, NULL, &grp)); |
| 1848 | logbuf_assert("Invalid keyword \"must\" as a child of \"grouping\". Line number 1."); |
| 1849 | FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL; |
| 1850 | |
| 1851 | *state = NULL; |
| 1852 | ly_ctx_destroy(ctx.ctx, NULL); |
| 1853 | } |
| 1854 | |
| 1855 | static void |
| 1856 | test_uses(void **state) |
| 1857 | { |
| 1858 | *state = test_uses; |
| 1859 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1860 | struct ly_parser_ctx ctx = {0}; |
| 1861 | struct lysp_node_uses *u = NULL; |
| 1862 | const char *str; |
| 1863 | |
| 1864 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 1865 | assert_non_null(ctx.ctx); |
| 1866 | ctx.line = 1; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1867 | ctx.mod_version = 2; /* simulate YANG 1.1 */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1868 | |
| 1869 | /* invalid cardinality */ |
| 1870 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
| 1871 | str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
| 1872 | assert_int_equal(LY_EVALID, parse_uses(&ctx, &str, NULL, (struct lysp_node**)&u)); \ |
| 1873 | logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \ |
| 1874 | lysp_node_free(ctx.ctx, (struct lysp_node*)u); u = NULL; |
| 1875 | |
| 1876 | TEST_DUP("description", "text1", "text2"); |
| 1877 | TEST_DUP("reference", "1", "2"); |
| 1878 | TEST_DUP("status", "current", "obsolete"); |
| 1879 | TEST_DUP("when", "true", "false"); |
| 1880 | #undef TEST_DUP |
| 1881 | |
| 1882 | /* full content */ |
| 1883 | str = "grpref {augment some/node;description test;if-feature f;reference test;refine some/other/node;status current;when true;m:ext;} ..."; |
| 1884 | assert_int_equal(LY_SUCCESS, parse_uses(&ctx, &str, NULL, (struct lysp_node**)&u)); |
| 1885 | assert_non_null(u); |
| 1886 | assert_int_equal(LYS_USES, u->nodetype); |
| 1887 | assert_string_equal("grpref", u->name); |
| 1888 | assert_string_equal("test", u->dsc); |
| 1889 | assert_non_null(u->exts); |
| 1890 | assert_non_null(u->iffeatures); |
| 1891 | assert_string_equal("test", u->ref); |
| 1892 | assert_non_null(u->augments); |
| 1893 | assert_non_null(u->refines); |
| 1894 | assert_non_null(u->when); |
| 1895 | assert_null(u->parent); |
| 1896 | assert_null(u->next); |
| 1897 | assert_int_equal(LYS_STATUS_CURR, u->flags); |
| 1898 | lysp_node_free(ctx.ctx, (struct lysp_node*)u); u = NULL; |
| 1899 | |
| 1900 | *state = NULL; |
| 1901 | ly_ctx_destroy(ctx.ctx, NULL); |
| 1902 | } |
Radek Krejci | 5a7c4a5 | 2019-02-12 15:45:11 +0100 | [diff] [blame] | 1903 | |
| 1904 | |
| 1905 | static void |
| 1906 | test_augment(void **state) |
| 1907 | { |
| 1908 | *state = test_augment; |
| 1909 | |
| 1910 | struct ly_parser_ctx ctx = {0}; |
| 1911 | struct lysp_augment *a = NULL; |
| 1912 | const char *str; |
| 1913 | |
| 1914 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 1915 | assert_non_null(ctx.ctx); |
| 1916 | ctx.line = 1; |
| 1917 | //ctx.mod_version = 2; /* simulate YANG 1.1 */ |
| 1918 | |
| 1919 | /* invalid cardinality */ |
| 1920 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
| 1921 | str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
| 1922 | assert_int_equal(LY_EVALID, parse_augment(&ctx, &str, NULL, &a)); \ |
| 1923 | logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \ |
| 1924 | lysp_augment_free(ctx.ctx, a); a = NULL; |
| 1925 | |
| 1926 | TEST_DUP("description", "text1", "text2"); |
| 1927 | TEST_DUP("reference", "1", "2"); |
| 1928 | TEST_DUP("status", "current", "obsolete"); |
| 1929 | TEST_DUP("when", "true", "false"); |
| 1930 | #undef TEST_DUP |
| 1931 | |
| 1932 | /* full content */ |
| 1933 | str = "/target/nodeid {action x; anydata any;anyxml anyxml; case cs; choice ch;container c;description test;if-feature f;leaf l {type string;}" |
| 1934 | "leaf-list ll {type string;} list li;notification not;reference test;status current;uses g;when true;m:ext;} ..."; |
| 1935 | assert_int_equal(LY_SUCCESS, parse_augment(&ctx, &str, NULL, &a)); |
| 1936 | assert_non_null(a); |
| 1937 | assert_int_equal(LYS_AUGMENT, a->nodetype); |
| 1938 | assert_string_equal("/target/nodeid", a->nodeid); |
| 1939 | assert_string_equal("test", a->dsc); |
| 1940 | assert_non_null(a->exts); |
| 1941 | assert_non_null(a->iffeatures); |
| 1942 | assert_string_equal("test", a->ref); |
| 1943 | assert_non_null(a->when); |
| 1944 | assert_null(a->parent); |
| 1945 | assert_int_equal(LYS_STATUS_CURR, a->flags); |
| 1946 | lysp_augment_free(ctx.ctx, a); a = NULL; |
| 1947 | |
| 1948 | *state = NULL; |
| 1949 | ly_ctx_destroy(ctx.ctx, NULL); |
| 1950 | } |
| 1951 | |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 1952 | int main(void) |
| 1953 | { |
| 1954 | const struct CMUnitTest tests[] = { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1955 | cmocka_unit_test_setup(test_helpers, logger_setup), |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 1956 | cmocka_unit_test_setup(test_comments, logger_setup), |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1957 | cmocka_unit_test_setup(test_arg, logger_setup), |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 1958 | cmocka_unit_test_setup(test_stmts, logger_setup), |
Radek Krejci | 05b1398 | 2018-11-28 16:22:07 +0100 | [diff] [blame] | 1959 | cmocka_unit_test_setup_teardown(test_minmax, logger_setup, logger_teardown), |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 1960 | cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown), |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1961 | cmocka_unit_test_setup_teardown(test_identity, logger_setup, logger_teardown), |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1962 | cmocka_unit_test_setup(test_feature, logger_setup), |
| 1963 | cmocka_unit_test_setup(test_deviation, logger_setup), |
| 1964 | cmocka_unit_test_setup(test_deviate, logger_setup), |
Radek Krejci | 8c37083 | 2018-11-02 15:10:03 +0100 | [diff] [blame] | 1965 | cmocka_unit_test_setup(test_container, logger_setup), |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 1966 | cmocka_unit_test_setup_teardown(test_leaf, logger_setup, logger_teardown), |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 1967 | cmocka_unit_test_setup_teardown(test_leaflist, logger_setup, logger_teardown), |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 1968 | cmocka_unit_test_setup_teardown(test_list, logger_setup, logger_teardown), |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1969 | cmocka_unit_test_setup_teardown(test_choice, logger_setup, logger_teardown), |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 1970 | cmocka_unit_test_setup_teardown(test_case, logger_setup, logger_teardown), |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 1971 | cmocka_unit_test_setup_teardown(test_anydata, logger_setup, logger_teardown), |
| 1972 | cmocka_unit_test_setup_teardown(test_anyxml, logger_setup, logger_teardown), |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1973 | cmocka_unit_test_setup_teardown(test_grouping, logger_setup, logger_teardown), |
| 1974 | cmocka_unit_test_setup_teardown(test_uses, logger_setup, logger_teardown), |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 1975 | }; |
| 1976 | |
| 1977 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 1978 | } |