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 | |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 1370 | ctx.mod_version = 1; /* simulate YANG 1.0 */ |
| 1371 | str = " cont {action x;} ..."; |
| 1372 | assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c)); |
| 1373 | logbuf_assert("Invalid keyword \"action\" as a child of \"container\" - the statement is allowed only in YANG 1.1 modules. Line number 1."); |
| 1374 | lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL; |
| 1375 | |
Radek Krejci | 8c37083 | 2018-11-02 15:10:03 +0100 | [diff] [blame] | 1376 | ly_ctx_destroy(ctx.ctx, NULL); |
| 1377 | } |
| 1378 | |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 1379 | static void |
| 1380 | test_leaf(void **state) |
| 1381 | { |
| 1382 | *state = test_leaf; |
| 1383 | |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 1384 | struct ly_parser_ctx ctx = {0}; |
| 1385 | struct lysp_node_leaf *l = NULL; |
| 1386 | const char *str; |
| 1387 | |
| 1388 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 1389 | assert_non_null(ctx.ctx); |
| 1390 | ctx.line = 1; |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 1391 | //ctx.mod->version = 2; /* simulate YANG 1.1 */ |
| 1392 | |
| 1393 | /* invalid cardinality */ |
| 1394 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
| 1395 | str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
| 1396 | assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l)); \ |
| 1397 | logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \ |
| 1398 | lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL; |
| 1399 | |
| 1400 | TEST_DUP("config", "true", "false"); |
| 1401 | TEST_DUP("default", "x", "y"); |
| 1402 | TEST_DUP("description", "text1", "text2"); |
| 1403 | TEST_DUP("mandatory", "true", "false"); |
| 1404 | TEST_DUP("reference", "1", "2"); |
| 1405 | TEST_DUP("status", "current", "obsolete"); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 1406 | TEST_DUP("type", "int8", "uint8"); |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 1407 | TEST_DUP("units", "text1", "text2"); |
| 1408 | TEST_DUP("when", "true", "false"); |
| 1409 | #undef TEST_DUP |
| 1410 | |
| 1411 | /* full content - without mandatory which is mutual exclusive with default */ |
| 1412 | str = "l {config false;default \"xxx\";description test;if-feature f;" |
| 1413 | "must 'expr';reference test;status current;type string; units yyy;when true;m:ext;} ..."; |
| 1414 | assert_int_equal(LY_SUCCESS, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l)); |
| 1415 | assert_non_null(l); |
| 1416 | assert_int_equal(LYS_LEAF, l->nodetype); |
| 1417 | assert_string_equal("l", l->name); |
| 1418 | assert_string_equal("test", l->dsc); |
| 1419 | assert_string_equal("xxx", l->dflt); |
| 1420 | assert_string_equal("yyy", l->units); |
| 1421 | assert_string_equal("string", l->type.name); |
| 1422 | assert_non_null(l->exts); |
| 1423 | assert_non_null(l->iffeatures); |
| 1424 | assert_non_null(l->musts); |
| 1425 | assert_string_equal("test", l->ref); |
| 1426 | assert_non_null(l->when); |
| 1427 | assert_null(l->parent); |
| 1428 | assert_null(l->next); |
| 1429 | assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR, l->flags); |
| 1430 | lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL; |
| 1431 | |
| 1432 | /* full content - now with mandatory */ |
| 1433 | str = "l {mandatory true; type string;} ..."; |
| 1434 | assert_int_equal(LY_SUCCESS, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l)); |
| 1435 | assert_non_null(l); |
| 1436 | assert_int_equal(LYS_LEAF, l->nodetype); |
| 1437 | assert_string_equal("l", l->name); |
| 1438 | assert_string_equal("string", l->type.name); |
| 1439 | assert_int_equal(LYS_MAND_TRUE, l->flags); |
| 1440 | lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL; |
| 1441 | |
| 1442 | /* invalid */ |
| 1443 | str = " l {mandatory true; default xx; type string;} ..."; |
| 1444 | 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] | 1445 | 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] | 1446 | lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL; |
| 1447 | |
| 1448 | str = " l {description \"missing type\";} ..."; |
| 1449 | assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l)); |
| 1450 | logbuf_assert("Missing mandatory keyword \"type\" as a child of \"leaf\". Line number 1."); |
| 1451 | lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL; |
| 1452 | |
| 1453 | *state = NULL; |
| 1454 | ly_ctx_destroy(ctx.ctx, NULL); |
| 1455 | } |
| 1456 | |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 1457 | static void |
| 1458 | test_leaflist(void **state) |
| 1459 | { |
| 1460 | *state = test_leaf; |
| 1461 | |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 1462 | struct ly_parser_ctx ctx = {0}; |
| 1463 | struct lysp_node_leaflist *ll = NULL; |
| 1464 | const char *str; |
| 1465 | |
| 1466 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 1467 | assert_non_null(ctx.ctx); |
| 1468 | ctx.line = 1; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1469 | ctx.mod_version = 2; /* simulate YANG 1.1 */ |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 1470 | |
| 1471 | /* invalid cardinality */ |
| 1472 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
| 1473 | str = "ll {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
| 1474 | assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll)); \ |
| 1475 | logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \ |
| 1476 | lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL; |
| 1477 | |
| 1478 | TEST_DUP("config", "true", "false"); |
| 1479 | TEST_DUP("description", "text1", "text2"); |
| 1480 | TEST_DUP("max-elements", "10", "20"); |
| 1481 | TEST_DUP("min-elements", "10", "20"); |
| 1482 | TEST_DUP("ordered-by", "user", "system"); |
| 1483 | TEST_DUP("reference", "1", "2"); |
| 1484 | TEST_DUP("status", "current", "obsolete"); |
| 1485 | TEST_DUP("type", "int8", "uint8"); |
| 1486 | TEST_DUP("units", "text1", "text2"); |
| 1487 | TEST_DUP("when", "true", "false"); |
| 1488 | #undef TEST_DUP |
| 1489 | |
| 1490 | /* full content - without min-elements which is mutual exclusive with default */ |
| 1491 | str = "ll {config false;default \"xxx\"; default \"yyy\";description test;if-feature f;" |
| 1492 | "max-elements 10;must 'expr';ordered-by user;reference test;" |
| 1493 | "status current;type string; units zzz;when true;m:ext;} ..."; |
| 1494 | assert_int_equal(LY_SUCCESS, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll)); |
| 1495 | assert_non_null(ll); |
| 1496 | assert_int_equal(LYS_LEAFLIST, ll->nodetype); |
| 1497 | assert_string_equal("ll", ll->name); |
| 1498 | assert_string_equal("test", ll->dsc); |
| 1499 | assert_non_null(ll->dflts); |
| 1500 | assert_int_equal(2, LY_ARRAY_SIZE(ll->dflts)); |
| 1501 | assert_string_equal("xxx", ll->dflts[0]); |
| 1502 | assert_string_equal("yyy", ll->dflts[1]); |
| 1503 | assert_string_equal("zzz", ll->units); |
| 1504 | assert_int_equal(10, ll->max); |
| 1505 | assert_int_equal(0, ll->min); |
| 1506 | assert_string_equal("string", ll->type.name); |
| 1507 | assert_non_null(ll->exts); |
| 1508 | assert_non_null(ll->iffeatures); |
| 1509 | assert_non_null(ll->musts); |
| 1510 | assert_string_equal("test", ll->ref); |
| 1511 | assert_non_null(ll->when); |
| 1512 | assert_null(ll->parent); |
| 1513 | assert_null(ll->next); |
| 1514 | assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_USER | LYS_SET_MAX, ll->flags); |
| 1515 | lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL; |
| 1516 | |
| 1517 | /* full content - now with min-elements */ |
| 1518 | str = "ll {min-elements 10; type string;} ..."; |
| 1519 | assert_int_equal(LY_SUCCESS, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll)); |
| 1520 | assert_non_null(ll); |
| 1521 | assert_int_equal(LYS_LEAFLIST, ll->nodetype); |
| 1522 | assert_string_equal("ll", ll->name); |
| 1523 | assert_string_equal("string", ll->type.name); |
| 1524 | assert_int_equal(0, ll->max); |
| 1525 | assert_int_equal(10, ll->min); |
| 1526 | assert_int_equal(LYS_SET_MIN, ll->flags); |
| 1527 | lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL; |
| 1528 | |
| 1529 | /* invalid */ |
| 1530 | str = " ll {min-elements 1; default xx; type string;} ..."; |
| 1531 | 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] | 1532 | 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] | 1533 | lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL; |
| 1534 | |
| 1535 | str = " ll {description \"missing type\";} ..."; |
| 1536 | assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll)); |
| 1537 | logbuf_assert("Missing mandatory keyword \"type\" as a child of \"leaf-list\". Line number 1."); |
| 1538 | lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL; |
| 1539 | |
Radek Krejci | df6cad1 | 2018-11-28 17:10:55 +0100 | [diff] [blame] | 1540 | str = " ll {type string; min-elements 10; max-elements 1;} ..."; /* invalid combination of min/max */ |
| 1541 | assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll)); |
| 1542 | logbuf_assert("Invalid combination of min-elements and max-elements: min value 10 is bigger than the max value 1. Line number 1."); |
| 1543 | lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL; |
| 1544 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1545 | 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] | 1546 | str = " ll {default xx; type string;} ..."; |
| 1547 | assert_int_equal(LY_EVALID, parse_leaflist(&ctx, &str, NULL, (struct lysp_node**)&ll)); |
| 1548 | logbuf_assert("Invalid keyword \"default\" as a child of \"leaf-list\" - the statement is allowed only in YANG 1.1 modules. Line number 1."); |
| 1549 | lysp_node_free(ctx.ctx, (struct lysp_node*)ll); ll = NULL; |
| 1550 | |
| 1551 | *state = NULL; |
| 1552 | ly_ctx_destroy(ctx.ctx, NULL); |
| 1553 | } |
| 1554 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 1555 | static void |
| 1556 | test_list(void **state) |
| 1557 | { |
| 1558 | *state = test_list; |
| 1559 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 1560 | struct ly_parser_ctx ctx = {0}; |
| 1561 | struct lysp_node_list *l = NULL; |
| 1562 | const char *str; |
| 1563 | |
| 1564 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 1565 | assert_non_null(ctx.ctx); |
| 1566 | ctx.line = 1; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1567 | ctx.mod_version = 2; /* simulate YANG 1.1 */ |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 1568 | |
| 1569 | /* invalid cardinality */ |
| 1570 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
| 1571 | str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
| 1572 | assert_int_equal(LY_EVALID, parse_list(&ctx, &str, NULL, (struct lysp_node**)&l)); \ |
| 1573 | logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \ |
| 1574 | lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL; |
| 1575 | |
| 1576 | TEST_DUP("config", "true", "false"); |
| 1577 | TEST_DUP("description", "text1", "text2"); |
| 1578 | TEST_DUP("key", "one", "two"); |
| 1579 | TEST_DUP("max-elements", "10", "20"); |
| 1580 | TEST_DUP("min-elements", "10", "20"); |
| 1581 | TEST_DUP("ordered-by", "user", "system"); |
| 1582 | TEST_DUP("reference", "1", "2"); |
| 1583 | TEST_DUP("status", "current", "obsolete"); |
| 1584 | TEST_DUP("when", "true", "false"); |
| 1585 | #undef TEST_DUP |
| 1586 | |
| 1587 | /* full content */ |
| 1588 | 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;}" |
| 1589 | "leaf-list ll {type string;} list li;max-elements 10; min-elements 1;must 'expr';notification not; ordered-by system; reference test;" |
| 1590 | "status current;typedef t {type int8;}unique xxx;unique yyy;uses g;when true;m:ext;} ..."; |
| 1591 | assert_int_equal(LY_SUCCESS, parse_list(&ctx, &str, NULL, (struct lysp_node**)&l)); |
| 1592 | assert_non_null(l); |
| 1593 | assert_int_equal(LYS_LIST, l->nodetype); |
| 1594 | assert_string_equal("l", l->name); |
| 1595 | assert_string_equal("test", l->dsc); |
| 1596 | assert_string_equal("l", l->key); |
| 1597 | assert_non_null(l->uniques); |
| 1598 | assert_int_equal(2, LY_ARRAY_SIZE(l->uniques)); |
| 1599 | assert_string_equal("xxx", l->uniques[0]); |
| 1600 | assert_string_equal("yyy", l->uniques[1]); |
| 1601 | assert_int_equal(10, l->max); |
| 1602 | assert_int_equal(1, l->min); |
| 1603 | assert_non_null(l->exts); |
| 1604 | assert_non_null(l->iffeatures); |
| 1605 | assert_non_null(l->musts); |
| 1606 | assert_string_equal("test", l->ref); |
| 1607 | assert_non_null(l->when); |
| 1608 | assert_null(l->parent); |
| 1609 | assert_null(l->next); |
| 1610 | assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM | LYS_SET_MAX | LYS_SET_MIN, l->flags); |
| 1611 | ly_set_erase(&ctx.tpdfs_nodes, NULL); |
| 1612 | lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL; |
| 1613 | |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 1614 | /* invalid content */ |
| 1615 | ctx.mod_version = 1; /* simulate YANG 1.0 */ |
| 1616 | str = "l {action x;} ..."; |
| 1617 | assert_int_equal(LY_EVALID, parse_list(&ctx, &str, NULL, (struct lysp_node**)&l)); |
| 1618 | logbuf_assert("Invalid keyword \"action\" as a child of \"list\" - the statement is allowed only in YANG 1.1 modules. Line number 1."); |
| 1619 | lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL; |
| 1620 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 1621 | *state = NULL; |
| 1622 | ly_ctx_destroy(ctx.ctx, NULL); |
| 1623 | } |
| 1624 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1625 | static void |
| 1626 | test_choice(void **state) |
| 1627 | { |
| 1628 | *state = test_choice; |
| 1629 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1630 | struct ly_parser_ctx ctx = {0}; |
| 1631 | struct lysp_node_choice *ch = NULL; |
| 1632 | const char *str; |
| 1633 | |
| 1634 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 1635 | assert_non_null(ctx.ctx); |
| 1636 | ctx.line = 1; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1637 | ctx.mod_version = 2; /* simulate YANG 1.1 */ |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1638 | |
| 1639 | /* invalid cardinality */ |
| 1640 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
| 1641 | str = "ch {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
| 1642 | assert_int_equal(LY_EVALID, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch)); \ |
| 1643 | logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \ |
| 1644 | lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL; |
| 1645 | |
| 1646 | TEST_DUP("config", "true", "false"); |
| 1647 | TEST_DUP("default", "a", "b"); |
| 1648 | TEST_DUP("description", "text1", "text2"); |
| 1649 | TEST_DUP("mandatory", "true", "false"); |
| 1650 | TEST_DUP("reference", "1", "2"); |
| 1651 | TEST_DUP("status", "current", "obsolete"); |
| 1652 | TEST_DUP("when", "true", "false"); |
| 1653 | #undef TEST_DUP |
| 1654 | |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 1655 | /* full content - without default due to a collision with mandatory */ |
| 1656 | 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] | 1657 | "leaf-list ll {type string;} list li;mandatory true;reference test;status current;when true;m:ext;} ..."; |
| 1658 | assert_int_equal(LY_SUCCESS, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch)); |
| 1659 | assert_non_null(ch); |
| 1660 | assert_int_equal(LYS_CHOICE, ch->nodetype); |
| 1661 | assert_string_equal("ch", ch->name); |
| 1662 | assert_string_equal("test", ch->dsc); |
| 1663 | assert_non_null(ch->exts); |
| 1664 | assert_non_null(ch->iffeatures); |
| 1665 | assert_string_equal("test", ch->ref); |
| 1666 | assert_non_null(ch->when); |
| 1667 | assert_null(ch->parent); |
| 1668 | assert_null(ch->next); |
| 1669 | assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_MAND_TRUE, ch->flags); |
| 1670 | lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL; |
| 1671 | |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 1672 | /* full content - the default missing from the previous node */ |
| 1673 | str = "ch {default c;case c;} ..."; |
| 1674 | assert_int_equal(LY_SUCCESS, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch)); |
| 1675 | assert_non_null(ch); |
| 1676 | assert_int_equal(LYS_CHOICE, ch->nodetype); |
| 1677 | assert_string_equal("ch", ch->name); |
| 1678 | assert_string_equal("c", ch->dflt); |
| 1679 | assert_int_equal(0, ch->flags); |
| 1680 | lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL; |
| 1681 | |
| 1682 | /* invalid content */ |
| 1683 | str = "ch {mandatory true; default c1; case c1 {leaf x{type string;}}} ..."; |
| 1684 | assert_int_equal(LY_EVALID, parse_choice(&ctx, &str, NULL, (struct lysp_node**)&ch)); |
| 1685 | logbuf_assert("Invalid combination of keywords \"mandatory\" and \"default\" as substatements of \"choice\". Line number 1."); |
| 1686 | lysp_node_free(ctx.ctx, (struct lysp_node*)ch); ch = NULL; |
| 1687 | |
| 1688 | *state = NULL; |
| 1689 | ly_ctx_destroy(ctx.ctx, NULL); |
| 1690 | } |
| 1691 | |
| 1692 | static void |
| 1693 | test_case(void **state) |
| 1694 | { |
| 1695 | *state = test_case; |
| 1696 | |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 1697 | struct ly_parser_ctx ctx = {0}; |
| 1698 | struct lysp_node_case *cs = NULL; |
| 1699 | const char *str; |
| 1700 | |
| 1701 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 1702 | assert_non_null(ctx.ctx); |
| 1703 | ctx.line = 1; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1704 | ctx.mod_version = 2; /* simulate YANG 1.1 */ |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 1705 | |
| 1706 | /* invalid cardinality */ |
| 1707 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
| 1708 | str = "cs {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
| 1709 | assert_int_equal(LY_EVALID, parse_case(&ctx, &str, NULL, (struct lysp_node**)&cs)); \ |
| 1710 | logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \ |
| 1711 | lysp_node_free(ctx.ctx, (struct lysp_node*)cs); cs = NULL; |
| 1712 | |
| 1713 | TEST_DUP("description", "text1", "text2"); |
| 1714 | TEST_DUP("reference", "1", "2"); |
| 1715 | TEST_DUP("status", "current", "obsolete"); |
| 1716 | TEST_DUP("when", "true", "false"); |
| 1717 | #undef TEST_DUP |
| 1718 | |
| 1719 | /* full content */ |
| 1720 | str = "cs {anydata any;anyxml anyxml; choice ch;container c;description test;if-feature f;leaf l {type string;}" |
| 1721 | "leaf-list ll {type string;} list li;reference test;status current;uses grp;when true;m:ext;} ..."; |
| 1722 | assert_int_equal(LY_SUCCESS, parse_case(&ctx, &str, NULL, (struct lysp_node**)&cs)); |
| 1723 | assert_non_null(cs); |
| 1724 | assert_int_equal(LYS_CASE, cs->nodetype); |
| 1725 | assert_string_equal("cs", cs->name); |
| 1726 | assert_string_equal("test", cs->dsc); |
| 1727 | assert_non_null(cs->exts); |
| 1728 | assert_non_null(cs->iffeatures); |
| 1729 | assert_string_equal("test", cs->ref); |
| 1730 | assert_non_null(cs->when); |
| 1731 | assert_null(cs->parent); |
| 1732 | assert_null(cs->next); |
| 1733 | assert_int_equal(LYS_STATUS_CURR, cs->flags); |
| 1734 | lysp_node_free(ctx.ctx, (struct lysp_node*)cs); cs = NULL; |
| 1735 | |
| 1736 | /* invalid content */ |
| 1737 | str = "cs {config true} ..."; |
| 1738 | assert_int_equal(LY_EVALID, parse_case(&ctx, &str, NULL, (struct lysp_node**)&cs)); |
| 1739 | logbuf_assert("Invalid keyword \"config\" as a child of \"case\". Line number 1."); |
| 1740 | lysp_node_free(ctx.ctx, (struct lysp_node*)cs); cs = NULL; |
| 1741 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1742 | *state = NULL; |
| 1743 | ly_ctx_destroy(ctx.ctx, NULL); |
| 1744 | } |
| 1745 | |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 1746 | static void |
| 1747 | test_any(void **state, enum yang_keyword kw) |
| 1748 | { |
| 1749 | *state = test_any; |
| 1750 | |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 1751 | struct ly_parser_ctx ctx = {0}; |
| 1752 | struct lysp_node_anydata *any = NULL; |
| 1753 | const char *str; |
| 1754 | |
| 1755 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 1756 | assert_non_null(ctx.ctx); |
| 1757 | ctx.line = 1; |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 1758 | if (kw == YANG_ANYDATA) { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1759 | ctx.mod_version = 2; /* simulate YANG 1.1 */ |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 1760 | } else { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1761 | ctx.mod_version = 1; /* simulate YANG 1.0 */ |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 1762 | } |
| 1763 | |
| 1764 | /* invalid cardinality */ |
| 1765 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
| 1766 | str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
| 1767 | assert_int_equal(LY_EVALID, parse_any(&ctx, &str, kw, NULL, (struct lysp_node**)&any)); \ |
| 1768 | logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \ |
| 1769 | lysp_node_free(ctx.ctx, (struct lysp_node*)any); any = NULL; |
| 1770 | |
| 1771 | TEST_DUP("config", "true", "false"); |
| 1772 | TEST_DUP("description", "text1", "text2"); |
| 1773 | TEST_DUP("mandatory", "true", "false"); |
| 1774 | TEST_DUP("reference", "1", "2"); |
| 1775 | TEST_DUP("status", "current", "obsolete"); |
| 1776 | TEST_DUP("when", "true", "false"); |
| 1777 | #undef TEST_DUP |
| 1778 | |
| 1779 | /* full content */ |
| 1780 | str = "any {config true;description test;if-feature f;mandatory true;must 'expr';reference test;status current;when true;m:ext;} ..."; |
| 1781 | assert_int_equal(LY_SUCCESS, parse_any(&ctx, &str, kw, NULL, (struct lysp_node**)&any)); |
| 1782 | assert_non_null(any); |
| 1783 | assert_int_equal(kw == YANG_ANYDATA ? LYS_ANYDATA : LYS_ANYXML, any->nodetype); |
| 1784 | assert_string_equal("any", any->name); |
| 1785 | assert_string_equal("test", any->dsc); |
| 1786 | assert_non_null(any->exts); |
| 1787 | assert_non_null(any->iffeatures); |
| 1788 | assert_non_null(any->musts); |
| 1789 | assert_string_equal("test", any->ref); |
| 1790 | assert_non_null(any->when); |
| 1791 | assert_null(any->parent); |
| 1792 | assert_null(any->next); |
| 1793 | assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR | LYS_MAND_TRUE, any->flags); |
| 1794 | lysp_node_free(ctx.ctx, (struct lysp_node*)any); any = NULL; |
| 1795 | |
| 1796 | *state = NULL; |
| 1797 | ly_ctx_destroy(ctx.ctx, NULL); |
| 1798 | } |
| 1799 | |
| 1800 | static void |
| 1801 | test_anydata(void **state) |
| 1802 | { |
| 1803 | return test_any(state, YANG_ANYDATA); |
| 1804 | } |
| 1805 | |
| 1806 | static void |
| 1807 | test_anyxml(void **state) |
| 1808 | { |
| 1809 | return test_any(state, YANG_ANYXML); |
| 1810 | } |
| 1811 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1812 | static void |
| 1813 | test_grouping(void **state) |
| 1814 | { |
| 1815 | *state = test_grouping; |
| 1816 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1817 | struct ly_parser_ctx ctx = {0}; |
| 1818 | struct lysp_grp *grp = NULL; |
| 1819 | const char *str; |
| 1820 | |
| 1821 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 1822 | assert_non_null(ctx.ctx); |
| 1823 | ctx.line = 1; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1824 | ctx.mod_version = 2; /* simulate YANG 1.1 */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1825 | |
| 1826 | /* invalid cardinality */ |
| 1827 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
| 1828 | str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
| 1829 | assert_int_equal(LY_EVALID, parse_grouping(&ctx, &str, NULL, &grp)); \ |
| 1830 | logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \ |
| 1831 | FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL; |
| 1832 | |
| 1833 | TEST_DUP("description", "text1", "text2"); |
| 1834 | TEST_DUP("reference", "1", "2"); |
| 1835 | TEST_DUP("status", "current", "obsolete"); |
| 1836 | #undef TEST_DUP |
| 1837 | |
| 1838 | /* full content */ |
| 1839 | str = "grp {action x;anydata any;anyxml anyxml; choice ch;container c;description test;grouping g;leaf l {type string;}" |
| 1840 | "leaf-list ll {type string;} list li;notification not;reference test;status current;typedef t {type int8;}uses g;m:ext;} ..."; |
| 1841 | assert_int_equal(LY_SUCCESS, parse_grouping(&ctx, &str, NULL, &grp)); |
| 1842 | assert_non_null(grp); |
| 1843 | assert_int_equal(LYS_GROUPING, grp->nodetype); |
| 1844 | assert_string_equal("grp", grp->name); |
| 1845 | assert_string_equal("test", grp->dsc); |
| 1846 | assert_non_null(grp->exts); |
| 1847 | assert_string_equal("test", grp->ref); |
| 1848 | assert_null(grp->parent); |
| 1849 | assert_int_equal( LYS_STATUS_CURR, grp->flags); |
| 1850 | ly_set_erase(&ctx.tpdfs_nodes, NULL); |
| 1851 | FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL; |
| 1852 | |
| 1853 | /* invalid content */ |
| 1854 | str = "grp {config true} ..."; |
| 1855 | assert_int_equal(LY_EVALID, parse_grouping(&ctx, &str, NULL, &grp)); |
| 1856 | logbuf_assert("Invalid keyword \"config\" as a child of \"grouping\". Line number 1."); |
| 1857 | FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL; |
| 1858 | |
| 1859 | str = "grp {must 'expr'} ..."; |
| 1860 | assert_int_equal(LY_EVALID, parse_grouping(&ctx, &str, NULL, &grp)); |
| 1861 | logbuf_assert("Invalid keyword \"must\" as a child of \"grouping\". Line number 1."); |
| 1862 | FREE_ARRAY(ctx.ctx, grp, lysp_grp_free); grp = NULL; |
| 1863 | |
| 1864 | *state = NULL; |
| 1865 | ly_ctx_destroy(ctx.ctx, NULL); |
| 1866 | } |
| 1867 | |
| 1868 | static void |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 1869 | test_action(void **state) |
| 1870 | { |
| 1871 | *state = test_action; |
| 1872 | |
| 1873 | struct ly_parser_ctx ctx = {0}; |
| 1874 | struct lysp_action *rpcs = NULL; |
| 1875 | struct lysp_node_container *c = NULL; |
| 1876 | const char *str; |
| 1877 | |
| 1878 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 1879 | assert_non_null(ctx.ctx); |
| 1880 | ctx.line = 1; |
| 1881 | ctx.mod_version = 2; /* simulate YANG 1.1 */ |
| 1882 | |
| 1883 | /* invalid cardinality */ |
| 1884 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
| 1885 | str = "func {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
| 1886 | assert_int_equal(LY_EVALID, parse_action(&ctx, &str, NULL, &rpcs)); \ |
| 1887 | logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \ |
| 1888 | FREE_ARRAY(ctx.ctx, rpcs, lysp_action_free); rpcs = NULL; |
| 1889 | |
| 1890 | TEST_DUP("description", "text1", "text2"); |
| 1891 | TEST_DUP("input", "", ""); |
| 1892 | TEST_DUP("output", "", ""); |
| 1893 | TEST_DUP("reference", "1", "2"); |
| 1894 | TEST_DUP("status", "current", "obsolete"); |
| 1895 | #undef TEST_DUP |
| 1896 | |
| 1897 | /* full content */ |
| 1898 | str = "top;"; |
| 1899 | assert_int_equal(LY_SUCCESS, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c)); |
| 1900 | str = "func {description test;grouping grp;if-feature f;reference test;status current;typedef mytype {type int8;} m:ext;" |
| 1901 | "input {anydata a1; anyxml a2; choice ch; container c; grouping grp; leaf l {type int8;} leaf-list ll {type int8;}" |
| 1902 | " list li; must 1; typedef mytypei {type int8;} uses grp; m:ext;}" |
| 1903 | "output {anydata a1; anyxml a2; choice ch; container c; grouping grp; leaf l {type int8;} leaf-list ll {type int8;}" |
| 1904 | " list li; must 1; typedef mytypeo {type int8;} uses grp; m:ext;}} ..."; |
| 1905 | assert_int_equal(LY_SUCCESS, parse_action(&ctx, &str, (struct lysp_node*)c, &rpcs)); |
| 1906 | assert_non_null(rpcs); |
| 1907 | assert_int_equal(LYS_ACTION, rpcs->nodetype); |
| 1908 | assert_string_equal("func", rpcs->name); |
| 1909 | assert_string_equal("test", rpcs->dsc); |
| 1910 | assert_non_null(rpcs->exts); |
| 1911 | assert_non_null(rpcs->iffeatures); |
| 1912 | assert_string_equal("test", rpcs->ref); |
| 1913 | assert_non_null(rpcs->groupings); |
| 1914 | assert_non_null(rpcs->typedefs); |
| 1915 | assert_int_equal(LYS_STATUS_CURR, rpcs->flags); |
| 1916 | /* input */ |
| 1917 | assert_int_equal(rpcs->input.nodetype, LYS_INOUT); |
| 1918 | assert_non_null(rpcs->input.groupings); |
| 1919 | assert_non_null(rpcs->input.exts); |
| 1920 | assert_non_null(rpcs->input.musts); |
| 1921 | assert_non_null(rpcs->input.typedefs); |
| 1922 | assert_non_null(rpcs->input.data); |
| 1923 | /* output */ |
| 1924 | assert_int_equal(rpcs->output.nodetype, LYS_INOUT); |
| 1925 | assert_non_null(rpcs->output.groupings); |
| 1926 | assert_non_null(rpcs->output.exts); |
| 1927 | assert_non_null(rpcs->output.musts); |
| 1928 | assert_non_null(rpcs->output.typedefs); |
| 1929 | assert_non_null(rpcs->output.data); |
| 1930 | |
| 1931 | ly_set_erase(&ctx.tpdfs_nodes, NULL); |
| 1932 | FREE_ARRAY(ctx.ctx, rpcs, lysp_action_free); rpcs = NULL; |
| 1933 | |
| 1934 | /* invalid content */ |
| 1935 | str = "func {config true} ..."; |
| 1936 | assert_int_equal(LY_EVALID, parse_action(&ctx, &str, NULL, &rpcs)); |
| 1937 | logbuf_assert("Invalid keyword \"config\" as a child of \"rpc\". Line number 1."); |
| 1938 | FREE_ARRAY(ctx.ctx, rpcs, lysp_action_free); rpcs = NULL; |
| 1939 | |
| 1940 | *state = NULL; |
| 1941 | lysp_node_free(ctx.ctx, (struct lysp_node*)c); |
| 1942 | ly_ctx_destroy(ctx.ctx, NULL); |
| 1943 | } |
| 1944 | |
| 1945 | static void |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1946 | test_uses(void **state) |
| 1947 | { |
| 1948 | *state = test_uses; |
| 1949 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1950 | struct ly_parser_ctx ctx = {0}; |
| 1951 | struct lysp_node_uses *u = NULL; |
| 1952 | const char *str; |
| 1953 | |
| 1954 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 1955 | assert_non_null(ctx.ctx); |
| 1956 | ctx.line = 1; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 1957 | ctx.mod_version = 2; /* simulate YANG 1.1 */ |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 1958 | |
| 1959 | /* invalid cardinality */ |
| 1960 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
| 1961 | str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
| 1962 | assert_int_equal(LY_EVALID, parse_uses(&ctx, &str, NULL, (struct lysp_node**)&u)); \ |
| 1963 | logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \ |
| 1964 | lysp_node_free(ctx.ctx, (struct lysp_node*)u); u = NULL; |
| 1965 | |
| 1966 | TEST_DUP("description", "text1", "text2"); |
| 1967 | TEST_DUP("reference", "1", "2"); |
| 1968 | TEST_DUP("status", "current", "obsolete"); |
| 1969 | TEST_DUP("when", "true", "false"); |
| 1970 | #undef TEST_DUP |
| 1971 | |
| 1972 | /* full content */ |
| 1973 | str = "grpref {augment some/node;description test;if-feature f;reference test;refine some/other/node;status current;when true;m:ext;} ..."; |
| 1974 | assert_int_equal(LY_SUCCESS, parse_uses(&ctx, &str, NULL, (struct lysp_node**)&u)); |
| 1975 | assert_non_null(u); |
| 1976 | assert_int_equal(LYS_USES, u->nodetype); |
| 1977 | assert_string_equal("grpref", u->name); |
| 1978 | assert_string_equal("test", u->dsc); |
| 1979 | assert_non_null(u->exts); |
| 1980 | assert_non_null(u->iffeatures); |
| 1981 | assert_string_equal("test", u->ref); |
| 1982 | assert_non_null(u->augments); |
| 1983 | assert_non_null(u->refines); |
| 1984 | assert_non_null(u->when); |
| 1985 | assert_null(u->parent); |
| 1986 | assert_null(u->next); |
| 1987 | assert_int_equal(LYS_STATUS_CURR, u->flags); |
| 1988 | lysp_node_free(ctx.ctx, (struct lysp_node*)u); u = NULL; |
| 1989 | |
| 1990 | *state = NULL; |
| 1991 | ly_ctx_destroy(ctx.ctx, NULL); |
| 1992 | } |
Radek Krejci | 5a7c4a5 | 2019-02-12 15:45:11 +0100 | [diff] [blame] | 1993 | |
| 1994 | |
| 1995 | static void |
| 1996 | test_augment(void **state) |
| 1997 | { |
| 1998 | *state = test_augment; |
| 1999 | |
| 2000 | struct ly_parser_ctx ctx = {0}; |
| 2001 | struct lysp_augment *a = NULL; |
| 2002 | const char *str; |
| 2003 | |
| 2004 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 2005 | assert_non_null(ctx.ctx); |
| 2006 | ctx.line = 1; |
Radek Krejci | b4adbf1 | 2019-02-25 13:35:22 +0100 | [diff] [blame] | 2007 | ctx.mod_version = 2; /* simulate YANG 1.1 */ |
Radek Krejci | 5a7c4a5 | 2019-02-12 15:45:11 +0100 | [diff] [blame] | 2008 | |
| 2009 | /* invalid cardinality */ |
| 2010 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
| 2011 | str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
| 2012 | assert_int_equal(LY_EVALID, parse_augment(&ctx, &str, NULL, &a)); \ |
| 2013 | logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \ |
Radek Krejci | b4adbf1 | 2019-02-25 13:35:22 +0100 | [diff] [blame] | 2014 | FREE_ARRAY(ctx.ctx, a, lysp_augment_free); a = NULL; |
Radek Krejci | 5a7c4a5 | 2019-02-12 15:45:11 +0100 | [diff] [blame] | 2015 | |
| 2016 | TEST_DUP("description", "text1", "text2"); |
| 2017 | TEST_DUP("reference", "1", "2"); |
| 2018 | TEST_DUP("status", "current", "obsolete"); |
| 2019 | TEST_DUP("when", "true", "false"); |
| 2020 | #undef TEST_DUP |
| 2021 | |
| 2022 | /* full content */ |
| 2023 | str = "/target/nodeid {action x; anydata any;anyxml anyxml; case cs; choice ch;container c;description test;if-feature f;leaf l {type string;}" |
| 2024 | "leaf-list ll {type string;} list li;notification not;reference test;status current;uses g;when true;m:ext;} ..."; |
| 2025 | assert_int_equal(LY_SUCCESS, parse_augment(&ctx, &str, NULL, &a)); |
| 2026 | assert_non_null(a); |
| 2027 | assert_int_equal(LYS_AUGMENT, a->nodetype); |
| 2028 | assert_string_equal("/target/nodeid", a->nodeid); |
| 2029 | assert_string_equal("test", a->dsc); |
| 2030 | assert_non_null(a->exts); |
| 2031 | assert_non_null(a->iffeatures); |
| 2032 | assert_string_equal("test", a->ref); |
| 2033 | assert_non_null(a->when); |
| 2034 | assert_null(a->parent); |
| 2035 | assert_int_equal(LYS_STATUS_CURR, a->flags); |
Radek Krejci | b4adbf1 | 2019-02-25 13:35:22 +0100 | [diff] [blame] | 2036 | FREE_ARRAY(ctx.ctx, a, lysp_augment_free); a = NULL; |
Radek Krejci | 5a7c4a5 | 2019-02-12 15:45:11 +0100 | [diff] [blame] | 2037 | |
| 2038 | *state = NULL; |
| 2039 | ly_ctx_destroy(ctx.ctx, NULL); |
| 2040 | } |
| 2041 | |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 2042 | int main(void) |
| 2043 | { |
| 2044 | const struct CMUnitTest tests[] = { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 2045 | cmocka_unit_test_setup(test_helpers, logger_setup), |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 2046 | cmocka_unit_test_setup(test_comments, logger_setup), |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 2047 | cmocka_unit_test_setup(test_arg, logger_setup), |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 2048 | cmocka_unit_test_setup(test_stmts, logger_setup), |
Radek Krejci | 05b1398 | 2018-11-28 16:22:07 +0100 | [diff] [blame] | 2049 | cmocka_unit_test_setup_teardown(test_minmax, logger_setup, logger_teardown), |
Radek Krejci | 40544fa | 2019-01-11 09:38:37 +0100 | [diff] [blame] | 2050 | cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown), |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 2051 | cmocka_unit_test_setup_teardown(test_identity, logger_setup, logger_teardown), |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 2052 | cmocka_unit_test_setup(test_feature, logger_setup), |
| 2053 | cmocka_unit_test_setup(test_deviation, logger_setup), |
| 2054 | cmocka_unit_test_setup(test_deviate, logger_setup), |
Radek Krejci | 8c37083 | 2018-11-02 15:10:03 +0100 | [diff] [blame] | 2055 | cmocka_unit_test_setup(test_container, logger_setup), |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 2056 | cmocka_unit_test_setup_teardown(test_leaf, logger_setup, logger_teardown), |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 2057 | cmocka_unit_test_setup_teardown(test_leaflist, logger_setup, logger_teardown), |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2058 | cmocka_unit_test_setup_teardown(test_list, logger_setup, logger_teardown), |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2059 | cmocka_unit_test_setup_teardown(test_choice, logger_setup, logger_teardown), |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 2060 | cmocka_unit_test_setup_teardown(test_case, logger_setup, logger_teardown), |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 2061 | cmocka_unit_test_setup_teardown(test_anydata, logger_setup, logger_teardown), |
| 2062 | cmocka_unit_test_setup_teardown(test_anyxml, logger_setup, logger_teardown), |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 2063 | cmocka_unit_test_setup_teardown(test_action, logger_setup, logger_teardown), |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2064 | cmocka_unit_test_setup_teardown(test_grouping, logger_setup, logger_teardown), |
| 2065 | cmocka_unit_test_setup_teardown(test_uses, logger_setup, logger_teardown), |
Radek Krejci | b4adbf1 | 2019-02-25 13:35:22 +0100 | [diff] [blame] | 2066 | cmocka_unit_test_setup_teardown(test_augment, logger_setup, logger_teardown), |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 2067 | }; |
| 2068 | |
| 2069 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 2070 | } |