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