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 | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 671 | static struct lysp_module * |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 672 | mod_renew(struct ly_parser_ctx *ctx, struct lysp_module *mod, uint8_t submodule) |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 673 | { |
| 674 | lysp_module_free(mod); |
| 675 | mod = calloc(1, sizeof *mod); |
| 676 | mod->ctx = ctx->ctx; |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 677 | mod->submodule = submodule; |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 678 | assert_non_null(mod); |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 679 | ctx->mod = mod; |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 680 | return mod; |
| 681 | } |
| 682 | |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 683 | static LY_ERR test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name), |
| 684 | const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format, |
| 685 | const char **module_data, void (**free_module_data)(void *model_data, void *user_data)) |
| 686 | { |
| 687 | *module_data = user_data; |
| 688 | *format = LYS_IN_YANG; |
| 689 | *free_module_data = NULL; |
| 690 | return LY_SUCCESS; |
| 691 | } |
| 692 | |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 693 | static void |
| 694 | test_module(void **state) |
| 695 | { |
| 696 | (void) state; /* unused */ |
| 697 | |
| 698 | struct ly_parser_ctx ctx; |
| 699 | struct lysp_module *mod; |
| 700 | const char *str; |
| 701 | |
| 702 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 703 | assert_non_null(ctx.ctx); |
| 704 | ctx.line = 1; |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 705 | ctx.indent = 0; |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 706 | |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 707 | mod = mod_renew(&ctx, NULL, 0); |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 708 | |
| 709 | /* missing mandatory substatements */ |
| 710 | str = " name {}"; |
| 711 | assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod)); |
| 712 | assert_string_equal("name", mod->name); |
| 713 | logbuf_assert("Missing mandatory keyword \"namespace\" as a child of \"module\". Line number 1."); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 714 | mod = mod_renew(&ctx, mod, 0); |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 715 | |
| 716 | str = " name {namespace urn:x;}"; |
| 717 | assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod)); |
| 718 | assert_string_equal("urn:x", mod->ns); |
| 719 | logbuf_assert("Missing mandatory keyword \"prefix\" as a child of \"module\". Line number 1."); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 720 | mod = mod_renew(&ctx, mod, 0); |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 721 | |
| 722 | str = " name {namespace urn:x;prefix \"x\";}"; |
| 723 | assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod)); |
| 724 | assert_string_equal("x", mod->prefix); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 725 | mod = mod_renew(&ctx, mod, 0); |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 726 | |
Radek Krejci | 027d580 | 2018-11-14 16:57:28 +0100 | [diff] [blame] | 727 | #define SCHEMA_BEGINNING " name {yang-version 1.1;namespace urn:x;prefix \"x\";" |
| 728 | #define SCHEMA_BEGINNING2 " name {namespace urn:x;prefix \"x\";" |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 729 | #define TEST_NODE(NODETYPE, INPUT, NAME) \ |
| 730 | str = SCHEMA_BEGINNING INPUT; \ |
| 731 | assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod)); \ |
| 732 | assert_non_null(mod->data); \ |
| 733 | assert_int_equal(NODETYPE, mod->data->nodetype); \ |
| 734 | assert_string_equal(NAME, mod->data->name); \ |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 735 | mod = mod_renew(&ctx, mod, 0); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 736 | #define TEST_GENERIC(INPUT, TARGET, TEST) \ |
| 737 | str = SCHEMA_BEGINNING INPUT; \ |
| 738 | assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod)); \ |
| 739 | assert_non_null(TARGET); \ |
| 740 | TEST; \ |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 741 | mod = mod_renew(&ctx, mod, 0); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 742 | #define TEST_DUP(MEMBER, VALUE1, VALUE2, LINE, SUBMODULE) \ |
| 743 | TEST_DUP_GENERIC(SCHEMA_BEGINNING, MEMBER, VALUE1, VALUE2, \ |
| 744 | parse_sub_module, mod, LINE, mod = mod_renew(&ctx, mod, SUBMODULE)) |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 745 | |
| 746 | /* duplicated namespace, prefix */ |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 747 | TEST_DUP("namespace", "y", "z", "1", 0); |
| 748 | TEST_DUP("prefix", "y", "z", "1", 0); |
| 749 | TEST_DUP("contact", "a", "b", "1", 0); |
| 750 | TEST_DUP("description", "a", "b", "1", 0); |
| 751 | TEST_DUP("organization", "a", "b", "1", 0); |
| 752 | TEST_DUP("reference", "a", "b", "1", 0); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 753 | |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 754 | /* not allowed in module (submodule-specific) */ |
| 755 | str = SCHEMA_BEGINNING "belongs-to master {prefix m;}}"; |
| 756 | assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod)); |
| 757 | logbuf_assert("Invalid keyword \"belongs-to\" as a child of \"module\". Line number 1."); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 758 | mod = mod_renew(&ctx, mod, 0); |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 759 | |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 760 | /* anydata */ |
| 761 | TEST_NODE(LYS_ANYDATA, "anydata test;}", "test"); |
| 762 | /* anyxml */ |
| 763 | TEST_NODE(LYS_ANYXML, "anyxml test;}", "test"); |
| 764 | /* augment */ |
| 765 | TEST_GENERIC("augment /somepath;}", mod->augments, |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 766 | assert_string_equal("/somepath", mod->augments[0].nodeid)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 767 | /* choice */ |
| 768 | TEST_NODE(LYS_CHOICE, "choice test;}", "test"); |
| 769 | /* contact 0..1 */ |
| 770 | TEST_GENERIC("contact \"firstname\" + \n\t\" surname\";}", mod->contact, |
| 771 | assert_string_equal("firstname surname", mod->contact)); |
| 772 | /* container */ |
| 773 | TEST_NODE(LYS_CONTAINER, "container test;}", "test"); |
| 774 | /* description 0..1 */ |
| 775 | TEST_GENERIC("description \'some description\';}", mod->dsc, |
| 776 | assert_string_equal("some description", mod->dsc)); |
| 777 | /* deviation */ |
| 778 | TEST_GENERIC("deviation /somepath {deviate not-supported;}}", mod->deviations, |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 779 | assert_string_equal("/somepath", mod->deviations[0].nodeid)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 780 | /* extension */ |
| 781 | TEST_GENERIC("extension test;}", mod->extensions, |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 782 | assert_string_equal("test", mod->extensions[0].name)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 783 | /* feature */ |
| 784 | TEST_GENERIC("feature test;}", mod->features, |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 785 | assert_string_equal("test", mod->features[0].name)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 786 | /* grouping */ |
| 787 | TEST_GENERIC("grouping grp;}", mod->groupings, |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 788 | assert_string_equal("grp", mod->groupings[0].name)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 789 | /* identity */ |
| 790 | TEST_GENERIC("identity test;}", mod->identities, |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 791 | assert_string_equal("test", mod->identities[0].name)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 792 | /* import */ |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 793 | ly_ctx_set_module_imp_clb(ctx.ctx, test_imp_clb, "module zzz { namespace urn:zzz; prefix z;}"); |
| 794 | TEST_GENERIC("import zzz {prefix z;}}", mod->imports, |
| 795 | assert_string_equal("zzz", mod->imports[0].name)); |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 796 | |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 797 | /* import - prefix collision */ |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 798 | str = SCHEMA_BEGINNING "import zzz {prefix x;}}"; |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 799 | assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod)); |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 800 | logbuf_assert("Prefix \"x\" already used as module prefix. Line number 2."); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 801 | mod = mod_renew(&ctx, mod, 0); |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 802 | str = SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix y;}}"; |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 803 | assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod)); |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 804 | logbuf_assert("Prefix \"y\" already used to import \"zzz\" module. Line number 2."); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 805 | mod = mod_renew(&ctx, mod, 0); |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 806 | str = "module" SCHEMA_BEGINNING "import zzz {prefix y;}import zzz {prefix z;}}"; |
| 807 | assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG)); |
| 808 | assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx)); |
| 809 | logbuf_assert("Single revision of the module \"zzz\" referred twice."); |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 810 | |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 811 | /* include */ |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 812 | store = 1; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 813 | 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] | 814 | str = "module" SCHEMA_BEGINNING "include xxx;}"; |
| 815 | assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG)); |
| 816 | assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx)); |
| 817 | logbuf_assert("Included \"xxx\" schema from \"name\" is actually not a submodule."); |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 818 | store = -1; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 819 | |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 820 | store = 1; |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 821 | 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] | 822 | str = "module" SCHEMA_BEGINNING "include xxx;}"; |
| 823 | assert_null(lys_parse_mem(ctx.ctx, str, LYS_IN_YANG)); |
| 824 | assert_int_equal(LY_EVALID, ly_errcode(ctx.ctx)); |
| 825 | 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] | 826 | store = -1; |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 827 | |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 828 | 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] | 829 | TEST_GENERIC("include xxx;}", mod->includes, |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 830 | assert_string_equal("xxx", mod->includes[0].name)); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 831 | |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 832 | /* leaf */ |
| 833 | TEST_NODE(LYS_LEAF, "leaf test {type string;}}", "test"); |
| 834 | /* leaf-list */ |
| 835 | TEST_NODE(LYS_LEAFLIST, "leaf-list test {type string;}}", "test"); |
| 836 | /* list */ |
| 837 | TEST_NODE(LYS_LIST, "list test {key a;leaf a {type string;}}}", "test"); |
| 838 | /* notification */ |
| 839 | TEST_GENERIC("notification test;}", mod->notifs, |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 840 | assert_string_equal("test", mod->notifs[0].name)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 841 | /* organization 0..1 */ |
| 842 | TEST_GENERIC("organization \"CESNET a.l.e.\";}", mod->org, |
| 843 | assert_string_equal("CESNET a.l.e.", mod->org)); |
| 844 | /* reference 0..1 */ |
| 845 | TEST_GENERIC("reference RFC7950;}", mod->ref, |
| 846 | assert_string_equal("RFC7950", mod->ref)); |
| 847 | /* revision */ |
| 848 | TEST_GENERIC("revision 2018-10-12;}", mod->revs, |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 849 | assert_string_equal("2018-10-12", mod->revs[0].date)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 850 | /* rpc */ |
| 851 | TEST_GENERIC("rpc test;}", mod->rpcs, |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 852 | assert_string_equal("test", mod->rpcs[0].name)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 853 | /* typedef */ |
| 854 | TEST_GENERIC("typedef test{type string;}}", mod->typedefs, |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 855 | assert_string_equal("test", mod->typedefs[0].name)); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 856 | /* uses */ |
| 857 | TEST_NODE(LYS_USES, "uses test;}", "test"); |
| 858 | /* yang-version */ |
Radek Krejci | 027d580 | 2018-11-14 16:57:28 +0100 | [diff] [blame] | 859 | str = SCHEMA_BEGINNING2 "\n\tyang-version 10;}"; |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 860 | assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod)); |
| 861 | logbuf_assert("Invalid value \"10\" of \"yang-version\". Line number 3."); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 862 | mod = mod_renew(&ctx, mod, 0); |
Radek Krejci | 027d580 | 2018-11-14 16:57:28 +0100 | [diff] [blame] | 863 | str = SCHEMA_BEGINNING2 "yang-version 1.0;yang-version 1.1;}"; |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 864 | assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod)); |
| 865 | logbuf_assert("Duplicate keyword \"yang-version\". Line number 3."); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 866 | mod = mod_renew(&ctx, mod, 0); |
Radek Krejci | 027d580 | 2018-11-14 16:57:28 +0100 | [diff] [blame] | 867 | str = SCHEMA_BEGINNING2 "yang-version 1.0;}"; |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 868 | assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod)); |
| 869 | assert_int_equal(1, mod->version); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 870 | mod = mod_renew(&ctx, mod, 0); |
Radek Krejci | 027d580 | 2018-11-14 16:57:28 +0100 | [diff] [blame] | 871 | str = SCHEMA_BEGINNING2 "yang-version \"1.1\";}"; |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 872 | assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod)); |
| 873 | assert_int_equal(2, mod->version); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 874 | mod = mod_renew(&ctx, mod, 0); |
| 875 | |
Radek Krejci | 156ccaf | 2018-10-15 15:49:17 +0200 | [diff] [blame] | 876 | /* extensions */ |
| 877 | TEST_GENERIC("prefix:test;}", mod->exts, |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 878 | assert_string_equal("prefix:test", mod->exts[0].name); |
| 879 | assert_int_equal(LYEXT_SUBSTMT_SELF, mod->exts[0].insubstmt)); |
Radek Krejci | 156ccaf | 2018-10-15 15:49:17 +0200 | [diff] [blame] | 880 | mod = mod_renew(&ctx, mod, 0); |
| 881 | |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 882 | /* invalid substatement */ |
| 883 | str = SCHEMA_BEGINNING "must false;}"; |
| 884 | assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod)); |
| 885 | logbuf_assert("Invalid keyword \"must\" as a child of \"module\". Line number 3."); |
| 886 | mod = mod_renew(&ctx, mod, 0); |
| 887 | |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 888 | /* submodule */ |
| 889 | mod->submodule = 1; |
| 890 | |
| 891 | /* missing mandatory substatements */ |
| 892 | str = " subname {}"; |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 893 | lydict_remove(ctx.ctx, mod->name); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 894 | assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod)); |
| 895 | assert_string_equal("subname", mod->name); |
| 896 | logbuf_assert("Missing mandatory keyword \"belongs-to\" as a child of \"submodule\". Line number 3."); |
| 897 | mod = mod_renew(&ctx, mod, 1); |
| 898 | |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 899 | str = " subname {belongs-to name {prefix x;}}"; |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 900 | lydict_remove(ctx.ctx, mod->name); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 901 | assert_int_equal(LY_SUCCESS, parse_sub_module(&ctx, &str, mod)); |
| 902 | assert_string_equal("name", mod->belongsto); |
| 903 | mod = mod_renew(&ctx, mod, 1); |
| 904 | |
| 905 | #undef SCHEMA_BEGINNING |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 906 | #define SCHEMA_BEGINNING " subname {belongs-to name {prefix x;}" |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 907 | |
| 908 | /* duplicated namespace, prefix */ |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 909 | TEST_DUP("belongs-to", "module1", "module2", "3", 1); |
Radek Krejci | 0930636 | 2018-10-15 15:26:01 +0200 | [diff] [blame] | 910 | |
| 911 | /* not allowed in submodule (module-specific) */ |
| 912 | str = SCHEMA_BEGINNING "namespace \"urn:z\";}"; |
| 913 | assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod)); |
| 914 | logbuf_assert("Invalid keyword \"namespace\" as a child of \"submodule\". Line number 3."); |
| 915 | mod = mod_renew(&ctx, mod, 1); |
| 916 | str = SCHEMA_BEGINNING "prefix m;}}"; |
| 917 | assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod)); |
| 918 | logbuf_assert("Invalid keyword \"prefix\" as a child of \"submodule\". Line number 3."); |
| 919 | mod = mod_renew(&ctx, mod, 1); |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 920 | |
| 921 | #undef TEST_GENERIC |
| 922 | #undef TEST_NODE |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 923 | #undef TEST_DUP |
Radek Krejci | a042ea1 | 2018-10-13 07:52:15 +0200 | [diff] [blame] | 924 | #undef SCHEMA_BEGINNING |
| 925 | |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 926 | lysp_module_free(mod); |
| 927 | ly_ctx_destroy(ctx.ctx, NULL); |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 928 | } |
| 929 | |
Radek Krejci | 4c6d9bd | 2018-10-15 16:43:06 +0200 | [diff] [blame] | 930 | static void |
| 931 | test_identity(void **state) |
| 932 | { |
| 933 | (void) state; /* unused */ |
| 934 | |
| 935 | struct ly_parser_ctx ctx; |
Radek Krejci | 027d580 | 2018-11-14 16:57:28 +0100 | [diff] [blame] | 936 | struct lysp_module *mod = NULL, m = {0}; |
Radek Krejci | 4c6d9bd | 2018-10-15 16:43:06 +0200 | [diff] [blame] | 937 | struct lysp_ident *ident = NULL; |
| 938 | const char *str; |
Radek Krejci | 4c6d9bd | 2018-10-15 16:43:06 +0200 | [diff] [blame] | 939 | |
| 940 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 941 | assert_non_null(ctx.ctx); |
| 942 | ctx.line = 1; |
| 943 | ctx.indent = 0; |
Radek Krejci | 027d580 | 2018-11-14 16:57:28 +0100 | [diff] [blame] | 944 | ctx.mod = &m; |
| 945 | ctx.mod->version = 2; /* simulate YANG 1.1 */ |
Radek Krejci | 4c6d9bd | 2018-10-15 16:43:06 +0200 | [diff] [blame] | 946 | |
| 947 | /* invalid cardinality */ |
| 948 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 949 | TEST_DUP_GENERIC(" test {", MEMBER, VALUE1, VALUE2, parse_identity, \ |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 950 | &ident, "1", FREE_ARRAY(ctx.ctx, ident, lysp_ident_free); ident = NULL) |
Radek Krejci | 4c6d9bd | 2018-10-15 16:43:06 +0200 | [diff] [blame] | 951 | |
| 952 | TEST_DUP("description", "a", "b"); |
| 953 | TEST_DUP("reference", "a", "b"); |
| 954 | TEST_DUP("status", "current", "obsolete"); |
| 955 | |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 956 | /* full content */ |
| 957 | 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] | 958 | assert_int_equal(LY_SUCCESS, parse_identity(&ctx, &str, &ident)); |
| 959 | assert_non_null(ident); |
| 960 | assert_string_equal(" ...", str); |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 961 | FREE_ARRAY(ctx.ctx, ident, lysp_ident_free); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 962 | ident = NULL; |
| 963 | |
| 964 | /* invalid substatement */ |
| 965 | str = " test {organization XXX;}"; |
| 966 | assert_int_equal(LY_EVALID, parse_identity(&ctx, &str, &ident)); |
| 967 | 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] | 968 | FREE_ARRAY(ctx.ctx, ident, lysp_ident_free); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 969 | ident = NULL; |
| 970 | |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 971 | /* identity duplication */ |
| 972 | str = "module a {namespace urn:a; prefix a; identity a; identity a;}"; |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 973 | assert_int_equal(LY_EVALID, yang_parse(&ctx, str, &mod)); |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 974 | logbuf_assert("Duplicate identifier \"a\" of identity statement. Line number 1."); |
| 975 | assert_null(mod); |
| 976 | |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 977 | #undef TEST_DUP |
| 978 | |
| 979 | ly_ctx_destroy(ctx.ctx, NULL); |
| 980 | } |
| 981 | |
| 982 | static void |
| 983 | test_feature(void **state) |
| 984 | { |
| 985 | (void) state; /* unused */ |
| 986 | |
| 987 | struct ly_parser_ctx ctx; |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 988 | struct lysp_module *mod = NULL; |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 989 | struct lysp_feature *features = NULL; |
| 990 | const char *str; |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 991 | |
| 992 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 993 | assert_non_null(ctx.ctx); |
| 994 | ctx.line = 1; |
| 995 | ctx.indent = 0; |
| 996 | |
| 997 | /* invalid cardinality */ |
| 998 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
| 999 | TEST_DUP_GENERIC(" test {", MEMBER, VALUE1, VALUE2, parse_feature, \ |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 1000 | &features, "1", FREE_ARRAY(ctx.ctx, features, lysp_feature_free); features = NULL) |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1001 | |
| 1002 | TEST_DUP("description", "a", "b"); |
| 1003 | TEST_DUP("reference", "a", "b"); |
| 1004 | TEST_DUP("status", "current", "obsolete"); |
| 1005 | |
| 1006 | /* full content */ |
| 1007 | str = " test {description text;reference \'another text\';status current; if-feature x;if-feature y;prefix:ext;} ..."; |
| 1008 | assert_int_equal(LY_SUCCESS, parse_feature(&ctx, &str, &features)); |
| 1009 | assert_non_null(features); |
| 1010 | assert_string_equal(" ...", str); |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 1011 | FREE_ARRAY(ctx.ctx, features, lysp_feature_free); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1012 | features = NULL; |
| 1013 | |
| 1014 | /* invalid substatement */ |
| 1015 | str = " test {organization XXX;}"; |
| 1016 | assert_int_equal(LY_EVALID, parse_feature(&ctx, &str, &features)); |
| 1017 | 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] | 1018 | FREE_ARRAY(ctx.ctx, features, lysp_feature_free); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1019 | features = NULL; |
| 1020 | |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 1021 | /* feature duplication */ |
| 1022 | str = "module a {namespace urn:a; prefix a; feature a; feature a;}"; |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 1023 | assert_int_equal(LY_EVALID, yang_parse(&ctx, str, &mod)); |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 1024 | logbuf_assert("Duplicate identifier \"a\" of feature statement. Line number 1."); |
| 1025 | assert_null(mod); |
| 1026 | |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1027 | #undef TEST_DUP |
| 1028 | |
| 1029 | ly_ctx_destroy(ctx.ctx, NULL); |
| 1030 | } |
| 1031 | |
| 1032 | static void |
| 1033 | test_deviation(void **state) |
| 1034 | { |
| 1035 | (void) state; /* unused */ |
| 1036 | |
| 1037 | struct ly_parser_ctx ctx; |
| 1038 | struct lysp_deviation *d = NULL; |
| 1039 | const char *str; |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1040 | |
| 1041 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 1042 | assert_non_null(ctx.ctx); |
| 1043 | ctx.line = 1; |
| 1044 | ctx.indent = 0; |
| 1045 | |
| 1046 | /* invalid cardinality */ |
| 1047 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
| 1048 | TEST_DUP_GENERIC(" test {deviate not-supported;", MEMBER, VALUE1, VALUE2, parse_deviation, \ |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 1049 | &d, "1", FREE_ARRAY(ctx.ctx, d, lysp_deviation_free); d = NULL) |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1050 | |
| 1051 | TEST_DUP("description", "a", "b"); |
| 1052 | TEST_DUP("reference", "a", "b"); |
| 1053 | |
| 1054 | /* full content */ |
| 1055 | str = " test {deviate not-supported;description text;reference \'another text\';prefix:ext;} ..."; |
| 1056 | assert_int_equal(LY_SUCCESS, parse_deviation(&ctx, &str, &d)); |
| 1057 | assert_non_null(d); |
| 1058 | assert_string_equal(" ...", str); |
Radek Krejci | e53a8dc | 2018-10-17 12:52:40 +0200 | [diff] [blame] | 1059 | FREE_ARRAY(ctx.ctx, d, lysp_deviation_free); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1060 | d = NULL; |
| 1061 | |
| 1062 | /* missing mandatory substatement */ |
| 1063 | str = " test {description text;}"; |
| 1064 | assert_int_equal(LY_EVALID, parse_deviation(&ctx, &str, &d)); |
| 1065 | 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] | 1066 | FREE_ARRAY(ctx.ctx, d, lysp_deviation_free); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1067 | d = NULL; |
| 1068 | |
| 1069 | /* invalid substatement */ |
| 1070 | str = " test {deviate not-supported; status obsolete;}"; |
| 1071 | assert_int_equal(LY_EVALID, parse_deviation(&ctx, &str, &d)); |
| 1072 | 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] | 1073 | FREE_ARRAY(ctx.ctx, d, lysp_deviation_free); |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1074 | d = NULL; |
| 1075 | |
| 1076 | #undef TEST_DUP |
| 1077 | |
| 1078 | ly_ctx_destroy(ctx.ctx, NULL); |
| 1079 | } |
| 1080 | |
| 1081 | static void |
| 1082 | test_deviate(void **state) |
| 1083 | { |
| 1084 | (void) state; /* unused */ |
| 1085 | |
| 1086 | struct ly_parser_ctx ctx; |
| 1087 | struct lysp_deviate *d = NULL; |
| 1088 | const char *str; |
| 1089 | |
| 1090 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 1091 | assert_non_null(ctx.ctx); |
| 1092 | ctx.line = 1; |
| 1093 | ctx.indent = 0; |
| 1094 | |
| 1095 | /* invalid cardinality */ |
| 1096 | #define TEST_DUP(TYPE, MEMBER, VALUE1, VALUE2) \ |
| 1097 | TEST_DUP_GENERIC(TYPE" {", MEMBER, VALUE1, VALUE2, parse_deviate, \ |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 1098 | &d, "1", lysp_deviate_free(ctx.ctx, d); free(d); d = NULL) |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1099 | |
| 1100 | TEST_DUP("add", "config", "true", "false"); |
| 1101 | TEST_DUP("replace", "default", "int8", "uint8"); |
| 1102 | TEST_DUP("add", "mandatory", "true", "false"); |
| 1103 | TEST_DUP("add", "max-elements", "1", "2"); |
| 1104 | TEST_DUP("add", "min-elements", "1", "2"); |
| 1105 | TEST_DUP("replace", "type", "int8", "uint8"); |
| 1106 | TEST_DUP("add", "units", "kilometers", "miles"); |
| 1107 | |
| 1108 | /* full contents */ |
| 1109 | str = " not-supported {prefix:ext;} ..."; |
| 1110 | assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d)); |
| 1111 | assert_non_null(d); |
| 1112 | assert_string_equal(" ...", str); |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 1113 | lysp_deviate_free(ctx.ctx, d); free(d); d = NULL; |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1114 | 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;} ..."; |
| 1115 | assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d)); |
| 1116 | assert_non_null(d); |
| 1117 | assert_string_equal(" ...", str); |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 1118 | lysp_deviate_free(ctx.ctx, d); free(d); d = NULL; |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1119 | str = " delete {units meters; must 1; must 2; unique x; unique y; default a; default b; prefix:ext;} ..."; |
| 1120 | assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d)); |
| 1121 | assert_non_null(d); |
| 1122 | assert_string_equal(" ...", str); |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 1123 | lysp_deviate_free(ctx.ctx, d); free(d); d = NULL; |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1124 | str = " replace {type string; units meters; default a; config true; mandatory true; min-elements 1; max-elements 2; prefix:ext;} ..."; |
| 1125 | assert_int_equal(LY_SUCCESS, parse_deviate(&ctx, &str, &d)); |
| 1126 | assert_non_null(d); |
| 1127 | assert_string_equal(" ...", str); |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 1128 | lysp_deviate_free(ctx.ctx, d); free(d); d = NULL; |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1129 | |
| 1130 | /* invalid substatements */ |
| 1131 | #define TEST_NOT_SUP(DEV, STMT, VALUE) \ |
| 1132 | str = " "DEV" {"STMT" "VALUE";}..."; \ |
| 1133 | assert_int_equal(LY_EVALID, parse_deviate(&ctx, &str, &d)); \ |
| 1134 | logbuf_assert("Deviate \""DEV"\" does not support keyword \""STMT"\". Line number 1."); \ |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 1135 | lysp_deviate_free(ctx.ctx, d); free(d); d = NULL |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1136 | |
| 1137 | TEST_NOT_SUP("not-supported", "units", "meters"); |
| 1138 | TEST_NOT_SUP("not-supported", "must", "1"); |
| 1139 | TEST_NOT_SUP("not-supported", "unique", "x"); |
| 1140 | TEST_NOT_SUP("not-supported", "default", "a"); |
| 1141 | TEST_NOT_SUP("not-supported", "config", "true"); |
| 1142 | TEST_NOT_SUP("not-supported", "mandatory", "true"); |
| 1143 | TEST_NOT_SUP("not-supported", "min-elements", "1"); |
| 1144 | TEST_NOT_SUP("not-supported", "max-elements", "2"); |
| 1145 | TEST_NOT_SUP("not-supported", "type", "string"); |
| 1146 | TEST_NOT_SUP("add", "type", "string"); |
| 1147 | TEST_NOT_SUP("delete", "config", "true"); |
| 1148 | TEST_NOT_SUP("delete", "mandatory", "true"); |
| 1149 | TEST_NOT_SUP("delete", "min-elements", "1"); |
| 1150 | TEST_NOT_SUP("delete", "max-elements", "2"); |
| 1151 | TEST_NOT_SUP("delete", "type", "string"); |
| 1152 | TEST_NOT_SUP("replace", "must", "1"); |
| 1153 | TEST_NOT_SUP("replace", "unique", "a"); |
| 1154 | |
| 1155 | str = " nonsence; ..."; |
| 1156 | assert_int_equal(LY_EVALID, parse_deviate(&ctx, &str, &d)); |
| 1157 | logbuf_assert("Invalid value \"nonsence\" of \"deviate\". Line number 1."); |
| 1158 | assert_null(d); |
| 1159 | |
| 1160 | #undef TEST_NOT_SUP |
| 1161 | #undef TEST_DUP |
Radek Krejci | 4c6d9bd | 2018-10-15 16:43:06 +0200 | [diff] [blame] | 1162 | |
| 1163 | ly_ctx_destroy(ctx.ctx, NULL); |
| 1164 | } |
| 1165 | |
Radek Krejci | 8c37083 | 2018-11-02 15:10:03 +0100 | [diff] [blame] | 1166 | static void |
| 1167 | test_container(void **state) |
| 1168 | { |
| 1169 | (void) state; /* unused */ |
| 1170 | |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 1171 | struct lysp_module mod = {0}; |
| 1172 | struct ly_parser_ctx ctx = {0}; |
Radek Krejci | 8c37083 | 2018-11-02 15:10:03 +0100 | [diff] [blame] | 1173 | struct lysp_node_container *c = NULL; |
| 1174 | const char *str; |
| 1175 | |
| 1176 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 1177 | assert_non_null(ctx.ctx); |
| 1178 | ctx.line = 1; |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 1179 | ctx.mod = &mod; |
Radek Krejci | 027d580 | 2018-11-14 16:57:28 +0100 | [diff] [blame] | 1180 | ctx.mod->version = 2; /* simulate YANG 1.1 */ |
Radek Krejci | 8c37083 | 2018-11-02 15:10:03 +0100 | [diff] [blame] | 1181 | |
| 1182 | /* invalid cardinality */ |
| 1183 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
| 1184 | str = "cont {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
| 1185 | assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c)); \ |
| 1186 | logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \ |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 1187 | lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL; |
Radek Krejci | 8c37083 | 2018-11-02 15:10:03 +0100 | [diff] [blame] | 1188 | |
| 1189 | TEST_DUP("config", "true", "false"); |
| 1190 | TEST_DUP("description", "text1", "text2"); |
| 1191 | TEST_DUP("presence", "true", "false"); |
| 1192 | TEST_DUP("reference", "1", "2"); |
| 1193 | TEST_DUP("status", "current", "obsolete"); |
| 1194 | TEST_DUP("when", "true", "false"); |
| 1195 | #undef TEST_DUP |
| 1196 | |
| 1197 | /* full content */ |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 1198 | 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;}" |
| 1199 | "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] | 1200 | assert_int_equal(LY_SUCCESS, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c)); |
| 1201 | assert_non_null(c); |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame^] | 1202 | assert_int_equal(LYS_CONTAINER, c->nodetype); |
| 1203 | assert_string_equal("cont", c->name); |
Radek Krejci | 8c37083 | 2018-11-02 15:10:03 +0100 | [diff] [blame] | 1204 | assert_non_null(c->actions); |
| 1205 | assert_non_null(c->child); |
| 1206 | assert_string_equal("test", c->dsc); |
| 1207 | assert_non_null(c->exts); |
| 1208 | assert_non_null(c->groupings); |
| 1209 | assert_non_null(c->iffeatures); |
| 1210 | assert_non_null(c->musts); |
| 1211 | assert_non_null(c->notifs); |
| 1212 | assert_string_equal("true", c->presence); |
| 1213 | assert_string_equal("test", c->ref); |
| 1214 | assert_non_null(c->typedefs); |
| 1215 | assert_non_null(c->when); |
| 1216 | assert_null(c->parent); |
| 1217 | assert_null(c->next); |
| 1218 | assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR, c->flags); |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 1219 | ly_set_erase(&ctx.tpdfs_nodes, NULL); |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 1220 | lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL; |
Radek Krejci | 8c37083 | 2018-11-02 15:10:03 +0100 | [diff] [blame] | 1221 | |
| 1222 | /* invalid */ |
| 1223 | str = " cont {augment /root;} ..."; |
| 1224 | assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c)); |
| 1225 | 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] | 1226 | lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL; |
Radek Krejci | 8c37083 | 2018-11-02 15:10:03 +0100 | [diff] [blame] | 1227 | str = " cont {nonsence true;} ..."; |
| 1228 | assert_int_equal(LY_EVALID, parse_container(&ctx, &str, NULL, (struct lysp_node**)&c)); |
| 1229 | logbuf_assert("Invalid character sequence \"nonsence\", expected a keyword. Line number 1."); |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 1230 | lysp_node_free(ctx.ctx, (struct lysp_node*)c); c = NULL; |
Radek Krejci | 8c37083 | 2018-11-02 15:10:03 +0100 | [diff] [blame] | 1231 | |
| 1232 | ly_ctx_destroy(ctx.ctx, NULL); |
| 1233 | } |
| 1234 | |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame^] | 1235 | static void |
| 1236 | test_leaf(void **state) |
| 1237 | { |
| 1238 | *state = test_leaf; |
| 1239 | |
| 1240 | struct lysp_module mod = {0}; |
| 1241 | struct ly_parser_ctx ctx = {0}; |
| 1242 | struct lysp_node_leaf *l = NULL; |
| 1243 | const char *str; |
| 1244 | |
| 1245 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 1246 | assert_non_null(ctx.ctx); |
| 1247 | ctx.line = 1; |
| 1248 | ctx.mod = &mod; |
| 1249 | //ctx.mod->version = 2; /* simulate YANG 1.1 */ |
| 1250 | |
| 1251 | /* invalid cardinality */ |
| 1252 | #define TEST_DUP(MEMBER, VALUE1, VALUE2) \ |
| 1253 | str = "l {" MEMBER" "VALUE1";"MEMBER" "VALUE2";} ..."; \ |
| 1254 | assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l)); \ |
| 1255 | logbuf_assert("Duplicate keyword \""MEMBER"\". Line number 1."); \ |
| 1256 | lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL; |
| 1257 | |
| 1258 | TEST_DUP("config", "true", "false"); |
| 1259 | TEST_DUP("default", "x", "y"); |
| 1260 | TEST_DUP("description", "text1", "text2"); |
| 1261 | TEST_DUP("mandatory", "true", "false"); |
| 1262 | TEST_DUP("reference", "1", "2"); |
| 1263 | TEST_DUP("status", "current", "obsolete"); |
| 1264 | TEST_DUP("units", "text1", "text2"); |
| 1265 | TEST_DUP("when", "true", "false"); |
| 1266 | #undef TEST_DUP |
| 1267 | |
| 1268 | /* full content - without mandatory which is mutual exclusive with default */ |
| 1269 | str = "l {config false;default \"xxx\";description test;if-feature f;" |
| 1270 | "must 'expr';reference test;status current;type string; units yyy;when true;m:ext;} ..."; |
| 1271 | assert_int_equal(LY_SUCCESS, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l)); |
| 1272 | assert_non_null(l); |
| 1273 | assert_int_equal(LYS_LEAF, l->nodetype); |
| 1274 | assert_string_equal("l", l->name); |
| 1275 | assert_string_equal("test", l->dsc); |
| 1276 | assert_string_equal("xxx", l->dflt); |
| 1277 | assert_string_equal("yyy", l->units); |
| 1278 | assert_string_equal("string", l->type.name); |
| 1279 | assert_non_null(l->exts); |
| 1280 | assert_non_null(l->iffeatures); |
| 1281 | assert_non_null(l->musts); |
| 1282 | assert_string_equal("test", l->ref); |
| 1283 | assert_non_null(l->when); |
| 1284 | assert_null(l->parent); |
| 1285 | assert_null(l->next); |
| 1286 | assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR, l->flags); |
| 1287 | lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL; |
| 1288 | |
| 1289 | /* full content - now with mandatory */ |
| 1290 | str = "l {mandatory true; type string;} ..."; |
| 1291 | assert_int_equal(LY_SUCCESS, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l)); |
| 1292 | assert_non_null(l); |
| 1293 | assert_int_equal(LYS_LEAF, l->nodetype); |
| 1294 | assert_string_equal("l", l->name); |
| 1295 | assert_string_equal("string", l->type.name); |
| 1296 | assert_int_equal(LYS_MAND_TRUE, l->flags); |
| 1297 | lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL; |
| 1298 | |
| 1299 | /* invalid */ |
| 1300 | str = " l {mandatory true; default xx; type string;} ..."; |
| 1301 | assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l)); |
| 1302 | logbuf_assert("Invalid combination of keywords \"mandatory\" and \"default\" as children of \"leaf\". Line number 1."); |
| 1303 | lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL; |
| 1304 | |
| 1305 | str = " l {description \"missing type\";} ..."; |
| 1306 | assert_int_equal(LY_EVALID, parse_leaf(&ctx, &str, NULL, (struct lysp_node**)&l)); |
| 1307 | logbuf_assert("Missing mandatory keyword \"type\" as a child of \"leaf\". Line number 1."); |
| 1308 | lysp_node_free(ctx.ctx, (struct lysp_node*)l); l = NULL; |
| 1309 | |
| 1310 | *state = NULL; |
| 1311 | ly_ctx_destroy(ctx.ctx, NULL); |
| 1312 | } |
| 1313 | |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 1314 | int main(void) |
| 1315 | { |
| 1316 | const struct CMUnitTest tests[] = { |
Radek Krejci | 44ceedc | 2018-10-02 15:54:31 +0200 | [diff] [blame] | 1317 | cmocka_unit_test_setup(test_helpers, logger_setup), |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 1318 | cmocka_unit_test_setup(test_comments, logger_setup), |
Radek Krejci | efd22f6 | 2018-09-27 11:47:58 +0200 | [diff] [blame] | 1319 | cmocka_unit_test_setup(test_arg, logger_setup), |
Radek Krejci | dcc7b32 | 2018-10-11 14:24:02 +0200 | [diff] [blame] | 1320 | cmocka_unit_test_setup(test_stmts, logger_setup), |
Radek Krejci | 9fcacc1 | 2018-10-11 15:59:11 +0200 | [diff] [blame] | 1321 | cmocka_unit_test_setup(test_module, logger_setup), |
Radek Krejci | 4c6d9bd | 2018-10-15 16:43:06 +0200 | [diff] [blame] | 1322 | cmocka_unit_test_setup(test_identity, logger_setup), |
Radek Krejci | 2c02f3e | 2018-10-16 10:54:38 +0200 | [diff] [blame] | 1323 | cmocka_unit_test_setup(test_feature, logger_setup), |
| 1324 | cmocka_unit_test_setup(test_deviation, logger_setup), |
| 1325 | cmocka_unit_test_setup(test_deviate, logger_setup), |
Radek Krejci | 8c37083 | 2018-11-02 15:10:03 +0100 | [diff] [blame] | 1326 | cmocka_unit_test_setup(test_container, logger_setup), |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame^] | 1327 | cmocka_unit_test_setup_teardown(test_leaf, logger_setup, logger_teardown), |
Radek Krejci | 80dd33e | 2018-09-26 15:57:18 +0200 | [diff] [blame] | 1328 | }; |
| 1329 | |
| 1330 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 1331 | } |