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