Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 1 | /* |
| 2 | * @file test_parser_yang.c |
| 3 | * @author: Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief unit tests for functions from parser_yang.c |
| 5 | * |
| 6 | * Copyright (c) 2018 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 14 | |
Radek Krejci | f3f4784 | 2018-11-15 11:22:15 +0100 | [diff] [blame] | 15 | #include "../../src/common.c" |
| 16 | #include "../../src/log.c" |
| 17 | #include "../../src/set.c" |
| 18 | #include "../../src/xpath.c" |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 19 | #include "../../src/parser_yang.c" |
Radek Krejci | f3f4784 | 2018-11-15 11:22:15 +0100 | [diff] [blame] | 20 | #include "../../src/tree_schema_helpers.c" |
Radek Krejci | 19a9610 | 2018-11-15 13:38:09 +0100 | [diff] [blame] | 21 | #include "../../src/tree_schema_free.c" |
| 22 | #include "../../src/tree_schema_compile.c" |
Radek Krejci | f3f4784 | 2018-11-15 11:22:15 +0100 | [diff] [blame] | 23 | #include "../../src/tree_schema.c" |
| 24 | #include "../../src/context.c" |
| 25 | #include "../../src/hash_table.c" |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 26 | |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 27 | #include <stdarg.h> |
| 28 | #include <stddef.h> |
| 29 | #include <setjmp.h> |
| 30 | #include <cmocka.h> |
| 31 | |
| 32 | #include <stdio.h> |
| 33 | #include <string.h> |
| 34 | |
| 35 | #include "libyang.h" |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 36 | |
| 37 | #define BUFSIZE 1024 |
| 38 | char logbuf[BUFSIZE] = {0}; |
| 39 | |
| 40 | /* set to 0 to printing error messages to stderr instead of checking them in code */ |
| 41 | #define ENABLE_LOGGER_CHECKING 1 |
| 42 | |
| 43 | #if ENABLE_LOGGER_CHECKING |
| 44 | static void |
| 45 | logger(LY_LOG_LEVEL level, const char *msg, const char *path) |
| 46 | { |
| 47 | (void) level; /* unused */ |
| 48 | |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 49 | if (path && path[0]) { |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 50 | snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path); |
| 51 | } else { |
| 52 | strncpy(logbuf, msg, BUFSIZE - 1); |
| 53 | } |
| 54 | } |
| 55 | #endif |
| 56 | |
| 57 | static int |
| 58 | logger_setup(void **state) |
| 59 | { |
| 60 | (void) state; /* unused */ |
| 61 | #if ENABLE_LOGGER_CHECKING |
| 62 | ly_set_log_clb(logger, 1); |
| 63 | #endif |
| 64 | return 0; |
| 65 | } |
| 66 | |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 67 | static int |
| 68 | logger_teardown(void **state) |
| 69 | { |
| 70 | (void) state; /* unused */ |
| 71 | #if ENABLE_LOGGER_CHECKING |
| 72 | if (*state) { |
| 73 | fprintf(stderr, "%s\n", logbuf); |
| 74 | } |
| 75 | #endif |
| 76 | return 0; |
| 77 | } |
| 78 | |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 79 | void |
| 80 | logbuf_clean(void) |
| 81 | { |
| 82 | logbuf[0] = '\0'; |
| 83 | } |
| 84 | |
| 85 | #if ENABLE_LOGGER_CHECKING |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 86 | # define logbuf_assert(str) assert_string_equal(logbuf, str);logbuf_clean() |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 87 | #else |
| 88 | # define logbuf_assert(str) |
| 89 | #endif |
| 90 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 91 | static LY_ERR test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name), |
| 92 | const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format, |
| 93 | const char **module_data, void (**free_module_data)(void *model_data, void *user_data)) |
| 94 | { |
| 95 | *module_data = user_data; |
| 96 | *format = LYS_IN_YANG; |
| 97 | *free_module_data = NULL; |
| 98 | return LY_SUCCESS; |
| 99 | } |
| 100 | |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 101 | static void |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 102 | reset_mod(struct ly_ctx *ctx, struct lys_module *module) |
| 103 | { |
| 104 | lysc_module_free(module->compiled, NULL); |
| 105 | lysp_module_free(module->parsed); |
| 106 | |
| 107 | FREE_STRING(module->ctx, module->name); |
| 108 | FREE_STRING(module->ctx, module->ns); |
| 109 | FREE_STRING(module->ctx, module->prefix); |
| 110 | FREE_STRING(module->ctx, module->filepath); |
| 111 | FREE_STRING(module->ctx, module->org); |
| 112 | FREE_STRING(module->ctx, module->contact); |
| 113 | FREE_STRING(module->ctx, module->dsc); |
| 114 | FREE_STRING(module->ctx, module->ref); |
| 115 | |
| 116 | memset(module, 0, sizeof *module); |
| 117 | module->ctx = ctx; |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 118 | module->implemented = 1; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | static void |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 122 | test_module(void **state) |
| 123 | { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 124 | *state = test_module; |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 125 | |
| 126 | const char *str; |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 127 | struct ly_parser_ctx ctx = {0}; |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 128 | struct lys_module mod = {0}; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 129 | struct lysc_feature *f; |
| 130 | struct lysc_iffeature *iff; |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 131 | |
| 132 | str = "module test {namespace urn:test; prefix t;" |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 133 | "feature f1;feature f2 {if-feature f1;}}"; |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 134 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 135 | reset_mod(ctx.ctx, &mod); |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 136 | |
Radek Krejci | f8f882a | 2018-10-31 14:51:15 +0100 | [diff] [blame] | 137 | assert_int_equal(LY_EINVAL, lys_compile(NULL, 0)); |
| 138 | logbuf_assert("Invalid argument mod (lys_compile())."); |
| 139 | assert_int_equal(LY_EINVAL, lys_compile(&mod, 0)); |
| 140 | logbuf_assert("Invalid argument mod->parsed (lys_compile())."); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 141 | assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, str, &mod)); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 142 | mod.implemented = 0; |
| 143 | assert_int_equal(LY_SUCCESS, lys_compile(&mod, 0)); |
| 144 | assert_null(mod.compiled); |
| 145 | mod.implemented = 1; |
Radek Krejci | f8f882a | 2018-10-31 14:51:15 +0100 | [diff] [blame] | 146 | assert_int_equal(LY_SUCCESS, lys_compile(&mod, 0)); |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 147 | assert_non_null(mod.compiled); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 148 | assert_string_equal("test", mod.name); |
| 149 | assert_string_equal("urn:test", mod.ns); |
| 150 | assert_string_equal("t", mod.prefix); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 151 | /* features */ |
| 152 | assert_non_null(mod.compiled->features); |
| 153 | assert_int_equal(2, LY_ARRAY_SIZE(mod.compiled->features)); |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 154 | f = &mod.compiled->features[1]; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 155 | assert_non_null(f->iffeatures); |
| 156 | assert_int_equal(1, LY_ARRAY_SIZE(f->iffeatures)); |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 157 | iff = &f->iffeatures[0]; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 158 | assert_non_null(iff->expr); |
| 159 | assert_non_null(iff->features); |
| 160 | assert_int_equal(1, LY_ARRAY_SIZE(iff->features)); |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 161 | assert_ptr_equal(&mod.compiled->features[0], iff->features[0]); |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 162 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 163 | lysc_module_free(mod.compiled, NULL); |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 164 | |
Radek Krejci | f8f882a | 2018-10-31 14:51:15 +0100 | [diff] [blame] | 165 | assert_int_equal(LY_SUCCESS, lys_compile(&mod, LYSC_OPT_FREE_SP)); |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 166 | assert_non_null(mod.compiled); |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 167 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 168 | lysc_module_free(mod.compiled, NULL); |
| 169 | mod.compiled = NULL; |
| 170 | |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 171 | /* submodules cannot be compiled directly */ |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 172 | str = "submodule test {belongs-to xxx {prefix x;}}"; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 173 | assert_int_equal(LY_EINVAL, yang_parse_module(&ctx, str, &mod)); |
| 174 | logbuf_assert("Input data contains submodule which cannot be parsed directly without its main module."); |
| 175 | assert_null(mod.parsed); |
| 176 | reset_mod(ctx.ctx, &mod); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 177 | |
| 178 | /* data definition name collision in top level */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 179 | assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module aa {namespace urn:aa;prefix aa;" |
| 180 | "leaf a {type string;} container a{presence x;}}", &mod)); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 181 | assert_int_equal(LY_EVALID, lys_compile(&mod, 0)); |
| 182 | logbuf_assert("Duplicate identifier \"a\" of data definition statement."); |
| 183 | assert_null(mod.compiled); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 184 | reset_mod(ctx.ctx, &mod); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 185 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 186 | *state = NULL; |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 187 | ly_ctx_destroy(ctx.ctx, NULL); |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 188 | } |
| 189 | |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 190 | static void |
| 191 | test_feature(void **state) |
| 192 | { |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 193 | *state = test_feature; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 194 | |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 195 | struct ly_parser_ctx ctx = {0}; |
Radek Krejci | 1aefdf7 | 2018-11-01 11:01:39 +0100 | [diff] [blame] | 196 | struct lys_module mod = {0}, *modp; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 197 | const char *str; |
| 198 | struct lysc_feature *f, *f1; |
| 199 | |
| 200 | str = "module a {namespace urn:a;prefix a;yang-version 1.1;\n" |
| 201 | "feature f1 {description test1;reference test2;status current;} feature f2; feature f3;\n" |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 202 | "feature orfeature {if-feature \"f1 or f2\";}\n" |
| 203 | "feature andfeature {if-feature \"f1 and f2\";}\n" |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 204 | "feature f6 {if-feature \"not f1\";}\n" |
Radek Krejci | dde18c5 | 2018-10-24 14:43:04 +0200 | [diff] [blame] | 205 | "feature f7 {if-feature \"(f2 and f3) or (not f1)\";}\n" |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 206 | "feature f8 {if-feature \"f1 or f2 or f3 or orfeature or andfeature\";}\n" |
| 207 | "feature f9 {if-feature \"not not f1\";}}"; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 208 | |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 209 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 210 | reset_mod(ctx.ctx, &mod); |
| 211 | |
| 212 | assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, str, &mod)); |
Radek Krejci | f8f882a | 2018-10-31 14:51:15 +0100 | [diff] [blame] | 213 | assert_int_equal(LY_SUCCESS, lys_compile(&mod, 0)); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 214 | assert_non_null(mod.compiled); |
| 215 | assert_non_null(mod.compiled->features); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 216 | assert_int_equal(9, LY_ARRAY_SIZE(mod.compiled->features)); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 217 | /* all features are disabled by default */ |
| 218 | LY_ARRAY_FOR(mod.compiled->features, struct lysc_feature, f) { |
| 219 | assert_int_equal(0, lysc_feature_value(f)); |
| 220 | } |
| 221 | /* enable f1 */ |
| 222 | assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f1")); |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 223 | f1 = &mod.compiled->features[0]; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 224 | assert_int_equal(1, lysc_feature_value(f1)); |
| 225 | |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 226 | /* enable orfeature */ |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 227 | f = &mod.compiled->features[3]; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 228 | assert_int_equal(0, lysc_feature_value(f)); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 229 | assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "orfeature")); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 230 | assert_int_equal(1, lysc_feature_value(f)); |
| 231 | |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 232 | /* enable andfeature - no possible since f2 is disabled */ |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 233 | f = &mod.compiled->features[4]; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 234 | assert_int_equal(0, lysc_feature_value(f)); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 235 | assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "andfeature")); |
| 236 | logbuf_assert("Feature \"andfeature\" cannot be enabled since it is disabled by its if-feature condition(s)."); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 237 | assert_int_equal(0, lysc_feature_value(f)); |
| 238 | |
| 239 | /* first enable f2, so f5 can be enabled then */ |
| 240 | assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f2")); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 241 | assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "andfeature")); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 242 | assert_int_equal(1, lysc_feature_value(f)); |
| 243 | |
| 244 | /* f1 is enabled, so f6 cannot be enabled */ |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 245 | f = &mod.compiled->features[5]; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 246 | assert_int_equal(0, lysc_feature_value(f)); |
| 247 | assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "f6")); |
| 248 | logbuf_assert("Feature \"f6\" cannot be enabled since it is disabled by its if-feature condition(s)."); |
| 249 | assert_int_equal(0, lysc_feature_value(f)); |
| 250 | |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 251 | /* so disable f1 - andfeature will became also disabled */ |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 252 | assert_int_equal(1, lysc_feature_value(f1)); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 253 | assert_int_equal(LY_SUCCESS, lys_feature_disable(&mod, "f1")); |
| 254 | assert_int_equal(0, lysc_feature_value(f1)); |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 255 | assert_int_equal(0, lysc_feature_value(&mod.compiled->features[4])); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 256 | /* while orfeature is stille enabled */ |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 257 | assert_int_equal(1, lysc_feature_value(&mod.compiled->features[3])); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 258 | /* and finally f6 can be enabled */ |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 259 | assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f6")); |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 260 | assert_int_equal(1, lysc_feature_value(&mod.compiled->features[5])); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 261 | |
| 262 | /* complex evaluation of f7: f1 and f3 are disabled, while f2 is enabled */ |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 263 | assert_int_equal(1, lysc_iffeature_value(&mod.compiled->features[6].iffeatures[0])); |
Radek Krejci | dde18c5 | 2018-10-24 14:43:04 +0200 | [diff] [blame] | 264 | /* long evaluation of f8 to need to reallocate internal stack for operators */ |
| 265 | assert_int_equal(1, lysc_iffeature_value(&mod.compiled->features[7].iffeatures[0])); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 266 | |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 267 | /* double negation of disabled f1 -> disabled */ |
| 268 | assert_int_equal(0, lysc_iffeature_value(&mod.compiled->features[8].iffeatures[0])); |
| 269 | |
Radek Krejci | 3892028 | 2018-11-01 09:24:16 +0100 | [diff] [blame] | 270 | /* disable all features */ |
| 271 | assert_int_equal(LY_SUCCESS, lys_feature_disable(&mod, "*")); |
| 272 | LY_ARRAY_FOR(mod.compiled->features, struct lysc_feature, f) { |
| 273 | assert_int_equal(0, lys_feature_value(&mod, f->name)); |
| 274 | } |
| 275 | /* re-setting already set feature */ |
| 276 | assert_int_equal(LY_SUCCESS, lys_feature_disable(&mod, "f1")); |
| 277 | assert_int_equal(0, lys_feature_value(&mod, "f1")); |
| 278 | |
| 279 | /* enabling feature that cannot be enabled due to its if-features */ |
Radek Krejci | 1aefdf7 | 2018-11-01 11:01:39 +0100 | [diff] [blame] | 280 | assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f1")); |
| 281 | assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "andfeature")); |
| 282 | logbuf_assert("Feature \"andfeature\" cannot be enabled since it is disabled by its if-feature condition(s)."); |
Radek Krejci | ca3db00 | 2018-11-01 10:31:01 +0100 | [diff] [blame] | 283 | assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "*")); |
| 284 | logbuf_assert("Feature \"f6\" cannot be enabled since it is disabled by its if-feature condition(s)."); |
Radek Krejci | 1aefdf7 | 2018-11-01 11:01:39 +0100 | [diff] [blame] | 285 | /* test if not changed */ |
| 286 | assert_int_equal(1, lys_feature_value(&mod, "f1")); |
| 287 | assert_int_equal(0, lys_feature_value(&mod, "f2")); |
Radek Krejci | 3892028 | 2018-11-01 09:24:16 +0100 | [diff] [blame] | 288 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 289 | assert_non_null(modp = lys_parse_mem(ctx.ctx, "module b {namespace urn:b;prefix b;" |
| 290 | "feature f1 {if-feature f2;}feature f2;}", LYS_IN_YANG)); |
| 291 | assert_non_null(modp->compiled); |
| 292 | assert_non_null(modp->compiled->features); |
| 293 | assert_int_equal(2, LY_ARRAY_SIZE(modp->compiled->features)); |
| 294 | assert_non_null(modp->compiled->features[0].iffeatures); |
| 295 | assert_int_equal(1, LY_ARRAY_SIZE(modp->compiled->features[0].iffeatures)); |
| 296 | assert_non_null(modp->compiled->features[0].iffeatures[0].features); |
| 297 | assert_int_equal(1, LY_ARRAY_SIZE(modp->compiled->features[0].iffeatures[0].features)); |
| 298 | assert_ptr_equal(&modp->compiled->features[1], modp->compiled->features[0].iffeatures[0].features[0]); |
| 299 | assert_non_null(modp->compiled->features); |
| 300 | assert_int_equal(2, LY_ARRAY_SIZE(modp->compiled->features)); |
| 301 | assert_non_null(modp->compiled->features[1].depfeatures); |
| 302 | assert_int_equal(1, LY_ARRAY_SIZE(modp->compiled->features[1].depfeatures)); |
| 303 | assert_ptr_equal(&modp->compiled->features[0], modp->compiled->features[1].depfeatures[0]); |
| 304 | |
Radek Krejci | 1aefdf7 | 2018-11-01 11:01:39 +0100 | [diff] [blame] | 305 | /* invalid reference */ |
Radek Krejci | 3892028 | 2018-11-01 09:24:16 +0100 | [diff] [blame] | 306 | assert_int_equal(LY_EINVAL, lys_feature_enable(&mod, "xxx")); |
| 307 | logbuf_assert("Feature \"xxx\" not found in module \"a\"."); |
| 308 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 309 | reset_mod(ctx.ctx, &mod); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 310 | |
| 311 | /* some invalid expressions */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 312 | assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module b{yang-version 1.1;namespace urn:b; prefix b; feature f{if-feature f1;}}", &mod)); |
Radek Krejci | f8f882a | 2018-10-31 14:51:15 +0100 | [diff] [blame] | 313 | assert_int_equal(LY_EVALID, lys_compile(&mod, 0)); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 314 | logbuf_assert("Invalid value \"f1\" of if-feature - unable to find feature \"f1\"."); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 315 | reset_mod(ctx.ctx, &mod); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 316 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 317 | assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module b{yang-version 1.1;namespace urn:b; prefix b; feature f1; feature f2{if-feature 'f and';}}", &mod)); |
Radek Krejci | f8f882a | 2018-10-31 14:51:15 +0100 | [diff] [blame] | 318 | assert_int_equal(LY_EVALID, lys_compile(&mod, 0)); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 319 | logbuf_assert("Invalid value \"f and\" of if-feature - unexpected end of expression."); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 320 | reset_mod(ctx.ctx, &mod); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 321 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 322 | assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module b{yang-version 1.1;namespace urn:b; prefix b; feature f{if-feature 'or';}}", &mod)); |
Radek Krejci | f8f882a | 2018-10-31 14:51:15 +0100 | [diff] [blame] | 323 | assert_int_equal(LY_EVALID, lys_compile(&mod, 0)); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 324 | logbuf_assert("Invalid value \"or\" of if-feature - unexpected end of expression."); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 325 | reset_mod(ctx.ctx, &mod); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 326 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 327 | assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module b{yang-version 1.1;namespace urn:b; prefix b; feature f1; feature f2{if-feature '(f1';}}", &mod)); |
Radek Krejci | f8f882a | 2018-10-31 14:51:15 +0100 | [diff] [blame] | 328 | assert_int_equal(LY_EVALID, lys_compile(&mod, 0)); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 329 | logbuf_assert("Invalid value \"(f1\" of if-feature - non-matching opening and closing parentheses."); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 330 | reset_mod(ctx.ctx, &mod); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 331 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 332 | assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module b{yang-version 1.1;namespace urn:b; prefix b; feature f1; feature f2{if-feature 'f1)';}}", &mod)); |
Radek Krejci | f8f882a | 2018-10-31 14:51:15 +0100 | [diff] [blame] | 333 | assert_int_equal(LY_EVALID, lys_compile(&mod, 0)); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 334 | logbuf_assert("Invalid value \"f1)\" of if-feature - non-matching opening and closing parentheses."); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 335 | reset_mod(ctx.ctx, &mod); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 336 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 337 | assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module b{yang-version 1.1;namespace urn:b; prefix b; feature f1; feature f2{if-feature ---;}}", &mod)); |
Radek Krejci | f8f882a | 2018-10-31 14:51:15 +0100 | [diff] [blame] | 338 | assert_int_equal(LY_EVALID, lys_compile(&mod, 0)); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 339 | logbuf_assert("Invalid value \"---\" of if-feature - unable to find feature \"---\"."); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 340 | reset_mod(ctx.ctx, &mod); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 341 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 342 | assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module b{namespace urn:b; prefix b; feature f1; feature f2{if-feature 'not f1';}}", &mod)); |
Radek Krejci | f8f882a | 2018-10-31 14:51:15 +0100 | [diff] [blame] | 343 | assert_int_equal(LY_EVALID, lys_compile(&mod, 0)); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 344 | logbuf_assert("Invalid value \"not f1\" of if-feature - YANG 1.1 expression in YANG 1.0 module."); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 345 | reset_mod(ctx.ctx, &mod); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 346 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 347 | assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module b{namespace urn:b; prefix b; feature f1; feature f1;}", &mod)); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 348 | assert_int_equal(LY_EVALID, lys_compile(&mod, 0)); |
| 349 | logbuf_assert("Duplicate identifier \"f1\" of feature statement."); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 350 | reset_mod(ctx.ctx, &mod); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 351 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 352 | ly_ctx_set_module_imp_clb(ctx.ctx, test_imp_clb, "submodule sz {belongs-to z {prefix z;} feature f1;}"); |
| 353 | assert_null(lys_parse_mem(ctx.ctx, "module z{namespace urn:z; prefix z; include sz;feature f1;}", LYS_IN_YANG)); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 354 | logbuf_assert("Duplicate identifier \"f1\" of feature statement."); |
| 355 | |
Radek Krejci | 1aefdf7 | 2018-11-01 11:01:39 +0100 | [diff] [blame] | 356 | /* import reference */ |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 357 | assert_non_null(modp = lys_parse_mem(ctx.ctx, str, LYS_IN_YANG)); |
Radek Krejci | 1aefdf7 | 2018-11-01 11:01:39 +0100 | [diff] [blame] | 358 | assert_int_equal(LY_SUCCESS, lys_feature_enable(modp, "f1")); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 359 | assert_non_null(modp = lys_parse_mem(ctx.ctx, "module c{namespace urn:c; prefix c; import a {prefix a;} feature f1; feature f2{if-feature 'a:f1';}}", LYS_IN_YANG)); |
Radek Krejci | 1aefdf7 | 2018-11-01 11:01:39 +0100 | [diff] [blame] | 360 | assert_int_equal(LY_SUCCESS, lys_feature_enable(modp, "f2")); |
| 361 | assert_int_equal(0, lys_feature_value(modp, "f1")); |
| 362 | assert_int_equal(1, lys_feature_value(modp, "f2")); |
| 363 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 364 | *state = NULL; |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 365 | ly_ctx_destroy(ctx.ctx, NULL); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 366 | } |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 367 | |
Radek Krejci | 478020e | 2018-10-30 16:02:14 +0100 | [diff] [blame] | 368 | static void |
| 369 | test_identity(void **state) |
| 370 | { |
Radek Krejci | 7f2a536 | 2018-11-28 13:05:37 +0100 | [diff] [blame] | 371 | *state = test_identity; |
Radek Krejci | 478020e | 2018-10-30 16:02:14 +0100 | [diff] [blame] | 372 | |
| 373 | struct ly_ctx *ctx; |
Radek Krejci | 1aefdf7 | 2018-11-01 11:01:39 +0100 | [diff] [blame] | 374 | struct lys_module *mod1, *mod2; |
Radek Krejci | 478020e | 2018-10-30 16:02:14 +0100 | [diff] [blame] | 375 | |
| 376 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
Radek Krejci | 4d483a8 | 2019-01-17 13:12:50 +0100 | [diff] [blame^] | 377 | assert_non_null(mod1 = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a; identity a1;}", LYS_IN_YANG)); |
| 378 | assert_non_null(mod2 = lys_parse_mem(ctx, "module b {yang-version 1.1;namespace urn:b;prefix b; import a {prefix a;}" |
| 379 | "identity b1; identity b2; identity b3 {base b1; base b:b2; base a:a1;}" |
| 380 | "identity b4 {base b:b1; base b3;}}", LYS_IN_YANG)); |
Radek Krejci | 478020e | 2018-10-30 16:02:14 +0100 | [diff] [blame] | 381 | |
| 382 | assert_non_null(mod1->compiled); |
| 383 | assert_non_null(mod1->compiled->identities); |
| 384 | assert_non_null(mod2->compiled); |
| 385 | assert_non_null(mod2->compiled->identities); |
| 386 | |
| 387 | assert_non_null(mod1->compiled->identities[0].derived); |
| 388 | assert_int_equal(1, LY_ARRAY_SIZE(mod1->compiled->identities[0].derived)); |
| 389 | assert_ptr_equal(mod1->compiled->identities[0].derived[0], &mod2->compiled->identities[2]); |
| 390 | assert_non_null(mod2->compiled->identities[0].derived); |
| 391 | assert_int_equal(2, LY_ARRAY_SIZE(mod2->compiled->identities[0].derived)); |
| 392 | assert_ptr_equal(mod2->compiled->identities[0].derived[0], &mod2->compiled->identities[2]); |
| 393 | assert_ptr_equal(mod2->compiled->identities[0].derived[1], &mod2->compiled->identities[3]); |
| 394 | assert_non_null(mod2->compiled->identities[1].derived); |
| 395 | assert_int_equal(1, LY_ARRAY_SIZE(mod2->compiled->identities[1].derived)); |
| 396 | assert_ptr_equal(mod2->compiled->identities[1].derived[0], &mod2->compiled->identities[2]); |
| 397 | assert_non_null(mod2->compiled->identities[2].derived); |
| 398 | assert_int_equal(1, LY_ARRAY_SIZE(mod2->compiled->identities[2].derived)); |
| 399 | assert_ptr_equal(mod2->compiled->identities[2].derived[0], &mod2->compiled->identities[3]); |
| 400 | |
Radek Krejci | 4d483a8 | 2019-01-17 13:12:50 +0100 | [diff] [blame^] | 401 | assert_non_null(mod2 = lys_parse_mem(ctx, "module c {yang-version 1.1;namespace urn:c;prefix c;" |
| 402 | "identity c2 {base c1;} identity c1;}", LYS_IN_YANG)); |
| 403 | assert_int_equal(1, LY_ARRAY_SIZE(mod2->compiled->identities[1].derived)); |
| 404 | assert_ptr_equal(mod2->compiled->identities[1].derived[0], &mod2->compiled->identities[0]); |
| 405 | |
| 406 | assert_null(lys_parse_mem(ctx, "module aa{namespace urn:aa; prefix aa; identity i1;identity i1;}", LYS_IN_YANG)); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 407 | logbuf_assert("Duplicate identifier \"i1\" of identity statement."); |
| 408 | |
Radek Krejci | 4d483a8 | 2019-01-17 13:12:50 +0100 | [diff] [blame^] | 409 | ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule sbb {belongs-to bb {prefix bb;} identity i1;}"); |
| 410 | assert_null(lys_parse_mem(ctx, "module bb{namespace urn:bb; prefix bb; include sbb;identity i1;}", LYS_IN_YANG)); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 411 | logbuf_assert("Duplicate identifier \"i1\" of identity statement."); |
| 412 | |
Radek Krejci | 7f2a536 | 2018-11-28 13:05:37 +0100 | [diff] [blame] | 413 | *state = NULL; |
Radek Krejci | 478020e | 2018-10-30 16:02:14 +0100 | [diff] [blame] | 414 | ly_ctx_destroy(ctx, NULL); |
| 415 | } |
| 416 | |
Radek Krejci | bd8d9ba | 2018-11-02 16:06:26 +0100 | [diff] [blame] | 417 | static void |
| 418 | test_node_container(void **state) |
| 419 | { |
| 420 | (void) state; /* unused */ |
| 421 | |
| 422 | struct ly_ctx *ctx; |
| 423 | struct lys_module *mod; |
| 424 | struct lysc_node_container *cont; |
| 425 | |
| 426 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 427 | assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;container c;}", LYS_IN_YANG)); |
Radek Krejci | bd8d9ba | 2018-11-02 16:06:26 +0100 | [diff] [blame] | 428 | assert_non_null(mod->compiled); |
| 429 | assert_non_null((cont = (struct lysc_node_container*)mod->compiled->data)); |
| 430 | assert_int_equal(LYS_CONTAINER, cont->nodetype); |
| 431 | assert_string_equal("c", cont->name); |
| 432 | assert_true(cont->flags & LYS_CONFIG_W); |
| 433 | assert_true(cont->flags & LYS_STATUS_CURR); |
| 434 | |
Radek Krejci | 98094b3 | 2018-11-02 16:21:47 +0100 | [diff] [blame] | 435 | assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;container c {config false; status deprecated; container child;}}", LYS_IN_YANG)); |
Radek Krejci | 98094b3 | 2018-11-02 16:21:47 +0100 | [diff] [blame] | 436 | logbuf_assert("Missing explicit \"deprecated\" status that was already specified in parent, inheriting."); |
Radek Krejci | bd8d9ba | 2018-11-02 16:06:26 +0100 | [diff] [blame] | 437 | assert_non_null(mod->compiled); |
| 438 | assert_non_null((cont = (struct lysc_node_container*)mod->compiled->data)); |
| 439 | assert_true(cont->flags & LYS_CONFIG_R); |
Radek Krejci | 98094b3 | 2018-11-02 16:21:47 +0100 | [diff] [blame] | 440 | assert_true(cont->flags & LYS_STATUS_DEPRC); |
Radek Krejci | bd8d9ba | 2018-11-02 16:06:26 +0100 | [diff] [blame] | 441 | assert_non_null((cont = (struct lysc_node_container*)cont->child)); |
| 442 | assert_int_equal(LYS_CONTAINER, cont->nodetype); |
| 443 | assert_true(cont->flags & LYS_CONFIG_R); |
Radek Krejci | 98094b3 | 2018-11-02 16:21:47 +0100 | [diff] [blame] | 444 | assert_true(cont->flags & LYS_STATUS_DEPRC); |
Radek Krejci | bd8d9ba | 2018-11-02 16:06:26 +0100 | [diff] [blame] | 445 | assert_string_equal("child", cont->name); |
| 446 | |
| 447 | ly_ctx_destroy(ctx, NULL); |
| 448 | } |
| 449 | |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 450 | static void |
| 451 | test_node_leaflist(void **state) |
| 452 | { |
| 453 | *state = test_node_leaflist; |
| 454 | |
| 455 | struct ly_ctx *ctx; |
| 456 | struct lys_module *mod; |
| 457 | struct lysc_type *type; |
| 458 | struct lysc_node_leaflist *ll; |
| 459 | |
| 460 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 461 | |
| 462 | assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;" |
| 463 | "typedef mytype {type union {type leafref {path ../target;} type string;}}" |
| 464 | "leaf-list ll1 {type union {type decimal64 {fraction-digits 2;} type mytype;}}" |
| 465 | "leaf-list ll2 {type leafref {path ../target;}}" |
| 466 | "leaf target {type int8;}}", |
| 467 | LYS_IN_YANG)); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 468 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 469 | assert_non_null(type); |
| 470 | assert_int_equal(1, type->refcount); |
| 471 | assert_int_equal(LY_TYPE_UNION, type->basetype); |
| 472 | assert_non_null(((struct lysc_type_union*)type)->types); |
| 473 | assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types)); |
| 474 | assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype); |
| 475 | assert_int_equal(LY_TYPE_LEAFREF, ((struct lysc_type_union*)type)->types[1]->basetype); |
| 476 | assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[2]->basetype); |
| 477 | assert_non_null(((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype); |
| 478 | assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype->basetype); |
| 479 | type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type; |
| 480 | assert_non_null(type); |
| 481 | assert_int_equal(1, type->refcount); |
| 482 | assert_int_equal(LY_TYPE_LEAFREF, type->basetype); |
| 483 | assert_non_null(((struct lysc_type_leafref*)type)->realtype); |
| 484 | assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)type)->realtype->basetype); |
| 485 | |
Radek Krejci | 6bb080b | 2018-11-28 17:23:41 +0100 | [diff] [blame] | 486 | assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;leaf-list ll {type string;}}", LYS_IN_YANG)); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 487 | assert_non_null(mod->compiled); |
| 488 | assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data)); |
| 489 | assert_int_equal(0, ll->min); |
| 490 | assert_int_equal((uint32_t)-1, ll->max); |
| 491 | |
Radek Krejci | 6bb080b | 2018-11-28 17:23:41 +0100 | [diff] [blame] | 492 | assert_non_null(mod = lys_parse_mem(ctx, "module c {yang-version 1.1;namespace urn:c;prefix c;typedef mytype {type int8;default 10;}" |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 493 | "leaf-list ll1 {type mytype;default 1; default 1; config false;}" |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 494 | "leaf-list ll2 {type mytype; ordered-by user;}}", LYS_IN_YANG)); |
Radek Krejci | 6bb080b | 2018-11-28 17:23:41 +0100 | [diff] [blame] | 495 | assert_non_null(mod->compiled); |
| 496 | assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data)); |
| 497 | assert_non_null(ll->dflts); |
| 498 | assert_int_equal(3, ll->type->refcount); |
| 499 | assert_int_equal(2, LY_ARRAY_SIZE(ll->dflts)); |
| 500 | assert_string_equal("1", ll->dflts[0]); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 501 | assert_string_equal("1", ll->dflts[1]); |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 502 | assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, ll->flags); |
Radek Krejci | 6bb080b | 2018-11-28 17:23:41 +0100 | [diff] [blame] | 503 | assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data->next)); |
| 504 | assert_non_null(ll->dflts); |
| 505 | assert_int_equal(3, ll->type->refcount); |
| 506 | assert_int_equal(1, LY_ARRAY_SIZE(ll->dflts)); |
| 507 | assert_string_equal("10", ll->dflts[0]); |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 508 | assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_USER, ll->flags); |
| 509 | |
| 510 | /* ordered-by is ignored for state data, RPC/action output parameters and notification content |
| 511 | * TODO test also, RPC output parameters and notification content */ |
| 512 | assert_non_null(mod = lys_parse_mem(ctx, "module d {yang-version 1.1;namespace urn:d;prefix d;" |
| 513 | "leaf-list ll {config false; type string; ordered-by user;}}", LYS_IN_YANG)); |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 514 | /* but warning is present: */ |
| 515 | logbuf_assert("The ordered-by statement is ignored in lists representing state data, RPC/action output parameters or notification content ()."); |
| 516 | assert_non_null(mod->compiled); |
| 517 | assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data)); |
| 518 | assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, ll->flags); |
Radek Krejci | 6bb080b | 2018-11-28 17:23:41 +0100 | [diff] [blame] | 519 | |
| 520 | /* invalid */ |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 521 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;leaf-list ll {type empty;}}", LYS_IN_YANG)); |
Radek Krejci | 6bb080b | 2018-11-28 17:23:41 +0100 | [diff] [blame] | 522 | logbuf_assert("Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules."); |
| 523 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 524 | assert_null(lys_parse_mem(ctx, "module bb {yang-version 1.1;namespace urn:bb;prefix bb;leaf-list ll {type empty; default x;}}", LYS_IN_YANG)); |
Radek Krejci | 6bb080b | 2018-11-28 17:23:41 +0100 | [diff] [blame] | 525 | logbuf_assert("Leaf-list of type \"empty\" must not have a default value (x)."); |
| 526 | |
| 527 | assert_non_null(mod = lys_parse_mem(ctx, "module cc {yang-version 1.1;namespace urn:cc;prefix cc;" |
| 528 | "leaf-list ll {config false;type string; default one;default two;default one;}}", LYS_IN_YANG)); |
Radek Krejci | 6bb080b | 2018-11-28 17:23:41 +0100 | [diff] [blame] | 529 | assert_non_null(mod->compiled); |
| 530 | assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data)); |
| 531 | assert_non_null(ll->dflts); |
| 532 | assert_int_equal(3, LY_ARRAY_SIZE(ll->dflts)); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 533 | assert_null(lys_parse_mem(ctx, "module dd {yang-version 1.1;namespace urn:dd;prefix dd;" |
| 534 | "leaf-list ll {type string; default one;default two;default one;}}", LYS_IN_YANG)); |
Radek Krejci | 6bb080b | 2018-11-28 17:23:41 +0100 | [diff] [blame] | 535 | logbuf_assert("Configuration leaf-list has multiple defaults of the same value \"one\"."); |
| 536 | |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 537 | *state = NULL; |
| 538 | ly_ctx_destroy(ctx, NULL); |
| 539 | } |
| 540 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 541 | static void |
| 542 | test_node_list(void **state) |
| 543 | { |
| 544 | *state = test_node_list; |
| 545 | |
| 546 | struct ly_ctx *ctx; |
| 547 | struct lys_module *mod; |
| 548 | struct lysc_node_list *list; |
| 549 | |
| 550 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 551 | |
| 552 | assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;feature f;" |
| 553 | "list l1 {key \"x y\"; ordered-by user; leaf x {type string; when 1;}leaf y{type string;if-feature f;}}" |
| 554 | "list l2 {config false;leaf value {type string;}}}", LYS_IN_YANG)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 555 | list = (struct lysc_node_list*)mod->compiled->data; |
| 556 | assert_non_null(list); |
| 557 | assert_non_null(list->keys); |
| 558 | assert_int_equal(2, LY_ARRAY_SIZE(list->keys)); |
| 559 | assert_string_equal("x", list->keys[0]->name); |
| 560 | assert_string_equal("y", list->keys[1]->name); |
| 561 | assert_non_null(list->child); |
| 562 | assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_USER, list->flags); |
| 563 | assert_true(list->child->flags & LYS_KEY); |
| 564 | assert_true(list->child->next->flags & LYS_KEY); |
| 565 | list = (struct lysc_node_list*)mod->compiled->data->next; |
| 566 | assert_non_null(list); |
| 567 | assert_null(list->keys); |
| 568 | assert_non_null(list->child); |
| 569 | assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM, list->flags); |
| 570 | assert_false(list->child->flags & LYS_KEY); |
| 571 | |
| 572 | assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;" |
| 573 | "list l {key a; unique \"a c/b:b\"; unique \"c/e d\";" |
Radek Krejci | 665421c | 2018-12-05 08:03:39 +0100 | [diff] [blame] | 574 | "leaf a {type string; default x;} leaf d {type string;config false;}" |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 575 | "container c {leaf b {type string;}leaf e{type string;config false;}}}}", LYS_IN_YANG)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 576 | list = (struct lysc_node_list*)mod->compiled->data; |
| 577 | assert_non_null(list); |
| 578 | assert_string_equal("l", list->name); |
| 579 | assert_non_null(list->keys); |
| 580 | assert_int_equal(1, LY_ARRAY_SIZE(list->keys)); |
| 581 | assert_string_equal("a", list->keys[0]->name); |
| 582 | assert_true(list->keys[0]->flags & LYS_KEY); |
Radek Krejci | 665421c | 2018-12-05 08:03:39 +0100 | [diff] [blame] | 583 | assert_null(list->keys[0]->dflt); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 584 | assert_non_null(list->uniques); |
| 585 | assert_int_equal(2, LY_ARRAY_SIZE(list->uniques)); |
| 586 | assert_int_equal(2, LY_ARRAY_SIZE(list->uniques[0])); |
| 587 | assert_string_equal("a", list->uniques[0][0]->name); |
| 588 | assert_true(list->uniques[0][0]->flags & LYS_UNIQUE); |
| 589 | assert_string_equal("b", list->uniques[0][1]->name); |
| 590 | assert_true(list->uniques[0][1]->flags & LYS_UNIQUE); |
| 591 | assert_int_equal(2, LY_ARRAY_SIZE(list->uniques[1])); |
| 592 | assert_string_equal("e", list->uniques[1][0]->name); |
| 593 | assert_true(list->uniques[1][0]->flags & LYS_UNIQUE); |
| 594 | assert_string_equal("d", list->uniques[1][1]->name); |
| 595 | assert_true(list->uniques[1][1]->flags & LYS_UNIQUE); |
| 596 | |
Radek Krejci | 665421c | 2018-12-05 08:03:39 +0100 | [diff] [blame] | 597 | assert_non_null(mod = lys_parse_mem(ctx, "module c {yang-version 1.1;namespace urn:c;prefix c;" |
| 598 | "list l {key a;leaf a {type empty;}}}", LYS_IN_YANG)); |
Radek Krejci | 665421c | 2018-12-05 08:03:39 +0100 | [diff] [blame] | 599 | list = (struct lysc_node_list*)mod->compiled->data; |
| 600 | assert_non_null(list); |
| 601 | assert_string_equal("l", list->name); |
| 602 | assert_non_null(list->keys); |
| 603 | assert_int_equal(1, LY_ARRAY_SIZE(list->keys)); |
| 604 | assert_string_equal("a", list->keys[0]->name); |
| 605 | assert_true(list->keys[0]->flags & LYS_KEY); |
| 606 | assert_int_equal(LY_TYPE_EMPTY, list->keys[0]->type->basetype); |
| 607 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 608 | /* invalid */ |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 609 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;list l;}", LYS_IN_YANG)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 610 | logbuf_assert("Missing key in list representing configuration data."); |
| 611 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 612 | assert_null(lys_parse_mem(ctx, "module bb {yang-version 1.1; namespace urn:bb;prefix bb;" |
| 613 | "list l {key x; leaf x {type string; when 1;}}}", LYS_IN_YANG)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 614 | logbuf_assert("List's key \"x\" must not have any \"when\" statement."); |
| 615 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 616 | assert_null(lys_parse_mem(ctx, "module cc {yang-version 1.1;namespace urn:cc;prefix cc;feature f;" |
| 617 | "list l {key x; leaf x {type string; if-feature f;}}}", LYS_IN_YANG)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 618 | logbuf_assert("List's key \"x\" must not have any \"if-feature\" statement."); |
| 619 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 620 | assert_null(lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd;" |
| 621 | "list l {key x; leaf x {type string; config false;}}}", LYS_IN_YANG)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 622 | logbuf_assert("Key of the configuration list must not be status leaf."); |
| 623 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 624 | assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;" |
| 625 | "list l {config false;key x; leaf x {type string; config true;}}}", LYS_IN_YANG)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 626 | logbuf_assert("Configuration node cannot be child of any state data node."); |
| 627 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 628 | assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;" |
| 629 | "list l {key x; leaf-list x {type string;}}}", LYS_IN_YANG)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 630 | logbuf_assert("The list's key \"x\" not found."); |
| 631 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 632 | assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;" |
| 633 | "list l {key x; unique y;leaf x {type string;} leaf-list y {type string;}}}", LYS_IN_YANG)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 634 | logbuf_assert("Unique's descendant-schema-nodeid \"y\" refers to a leaf-list node instead of a leaf."); |
| 635 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 636 | assert_null(lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;" |
| 637 | "list l {key x; unique \"x y\";leaf x {type string;} leaf y {config false; type string;}}}", LYS_IN_YANG)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 638 | logbuf_assert("Unique statement \"x y\" refers to leafs with different config type."); |
| 639 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 640 | assert_null(lys_parse_mem(ctx, "module ii {namespace urn:ii;prefix ii;" |
| 641 | "list l {key x; unique a:x;leaf x {type string;}}}", LYS_IN_YANG)); |
Radek Krejci | 665421c | 2018-12-05 08:03:39 +0100 | [diff] [blame] | 642 | logbuf_assert("Invalid descendant-schema-nodeid value \"a:x\" - prefix \"a\" not defined in module \"ii\"."); |
| 643 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 644 | assert_null(lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;" |
| 645 | "list l {key x; unique c/x;leaf x {type string;}container c {leaf y {type string;}}}}", LYS_IN_YANG)); |
Radek Krejci | 665421c | 2018-12-05 08:03:39 +0100 | [diff] [blame] | 646 | logbuf_assert("Invalid descendant-schema-nodeid value \"c/x\" - target node not found."); |
| 647 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 648 | assert_null( lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;" |
| 649 | "list l {key x; unique c^y;leaf x {type string;}container c {leaf y {type string;}}}}", LYS_IN_YANG)); |
Radek Krejci | 665421c | 2018-12-05 08:03:39 +0100 | [diff] [blame] | 650 | logbuf_assert("Invalid descendant-schema-nodeid value \"c^\" - missing \"/\" as node-identifier separator."); |
| 651 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 652 | assert_null(lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;" |
| 653 | "list l {key \"x y x\";leaf x {type string;}leaf y {type string;}}}", LYS_IN_YANG)); |
Radek Krejci | 665421c | 2018-12-05 08:03:39 +0100 | [diff] [blame] | 654 | logbuf_assert("Duplicated key identifier \"x\"."); |
| 655 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 656 | assert_null(lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;" |
| 657 | "list l {key x;leaf x {type empty;}}}", LYS_IN_YANG)); |
Radek Krejci | 665421c | 2018-12-05 08:03:39 +0100 | [diff] [blame] | 658 | logbuf_assert("Key of a list can be of type \"empty\" only in YANG 1.1 modules."); |
| 659 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 660 | *state = NULL; |
| 661 | ly_ctx_destroy(ctx, NULL); |
| 662 | } |
| 663 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 664 | static void |
| 665 | test_node_choice(void **state) |
| 666 | { |
| 667 | *state = test_node_choice; |
| 668 | |
| 669 | struct ly_ctx *ctx; |
| 670 | struct lys_module *mod; |
| 671 | struct lysc_node_choice *ch; |
| 672 | struct lysc_node_case *cs; |
| 673 | |
| 674 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 675 | |
| 676 | assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;feature f;" |
Radek Krejci | 18f2b8a | 2018-12-12 16:41:21 +0100 | [diff] [blame] | 677 | "choice ch {default a:b; case a {leaf a1 {type string;}leaf a2 {type string;}}" |
| 678 | "leaf b {type string;}}}", LYS_IN_YANG)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 679 | ch = (struct lysc_node_choice*)mod->compiled->data; |
| 680 | assert_non_null(ch); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 681 | assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR, ch->flags); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 682 | cs = ch->cases; |
| 683 | assert_non_null(cs); |
| 684 | assert_string_equal("a", cs->name); |
| 685 | assert_ptr_equal(ch, cs->parent); |
| 686 | assert_non_null(cs->child); |
Radek Krejci | 18f2b8a | 2018-12-12 16:41:21 +0100 | [diff] [blame] | 687 | assert_string_equal("a1", cs->child->name); |
| 688 | assert_non_null(cs->child->next); |
| 689 | assert_string_equal("a2", cs->child->next->name); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 690 | assert_ptr_equal(cs, cs->child->parent); |
| 691 | cs = (struct lysc_node_case*)cs->next; |
| 692 | assert_non_null(cs); |
| 693 | assert_string_equal("b", cs->name); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 694 | assert_int_equal(LYS_STATUS_CURR | LYS_SET_DFLT, cs->flags); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 695 | assert_ptr_equal(ch, cs->parent); |
| 696 | assert_non_null(cs->child); |
| 697 | assert_string_equal("b", cs->child->name); |
| 698 | assert_ptr_equal(cs, cs->child->parent); |
Radek Krejci | 18f2b8a | 2018-12-12 16:41:21 +0100 | [diff] [blame] | 699 | assert_ptr_equal(ch->cases->child->next, cs->child->prev); |
| 700 | assert_ptr_equal(ch->cases->child->next->next, cs->child); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 701 | assert_ptr_equal(ch->cases->child->prev, cs->child); |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 702 | assert_ptr_equal(ch->dflt, cs); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 703 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 704 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;" |
| 705 | "choice ch {case a {leaf x {type string;}}leaf x {type string;}}}", LYS_IN_YANG)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 706 | logbuf_assert("Duplicate identifier \"x\" of data definition statement."); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 707 | assert_null(lys_parse_mem(ctx, "module aa2 {namespace urn:aa2;prefix aa;" |
| 708 | "choice ch {case a {leaf y {type string;}}case b {leaf y {type string;}}}}", LYS_IN_YANG)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 709 | logbuf_assert("Duplicate identifier \"y\" of data definition statement."); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 710 | assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;" |
| 711 | "choice ch {case a {leaf x {type string;}}leaf a {type string;}}}", LYS_IN_YANG)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 712 | logbuf_assert("Duplicate identifier \"a\" of case statement."); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 713 | assert_null(lys_parse_mem(ctx, "module bb2 {namespace urn:bb2;prefix bb;" |
| 714 | "choice ch {case b {leaf x {type string;}}case b {leaf y {type string;}}}}", LYS_IN_YANG)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 715 | logbuf_assert("Duplicate identifier \"b\" of case statement."); |
| 716 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 717 | assert_null(lys_parse_mem(ctx, "module ca {namespace urn:ca;prefix ca;" |
| 718 | "choice ch {default c;case a {leaf x {type string;}}case b {leaf y {type string;}}}}", LYS_IN_YANG)); |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 719 | logbuf_assert("Default case \"c\" not found."); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 720 | assert_null(lys_parse_mem(ctx, "module cb {namespace urn:cb;prefix cb; import a {prefix a;}" |
| 721 | "choice ch {default a:a;case a {leaf x {type string;}}case b {leaf y {type string;}}}}", LYS_IN_YANG)); |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 722 | logbuf_assert("Invalid default case referencing a case from different YANG module (by prefix \"a\")."); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 723 | assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;" |
| 724 | "choice ch {default a;case a {leaf x {mandatory true;type string;}}}}", LYS_IN_YANG)); |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 725 | logbuf_assert("Mandatory node \"x\" under the default case \"a\"."); |
| 726 | /* TODO check with mandatory nodes from augment placed into the case */ |
| 727 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 728 | *state = NULL; |
| 729 | ly_ctx_destroy(ctx, NULL); |
| 730 | } |
| 731 | |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 732 | static void |
| 733 | test_node_anydata(void **state) |
| 734 | { |
| 735 | *state = test_node_anydata; |
| 736 | |
| 737 | struct ly_ctx *ctx; |
| 738 | struct lys_module *mod; |
| 739 | struct lysc_node_anydata *any; |
| 740 | |
| 741 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 742 | |
| 743 | assert_non_null(mod = lys_parse_mem(ctx, "module a {yang-version 1.1;namespace urn:a;prefix a;" |
| 744 | "anydata any {config false;mandatory true;}}", LYS_IN_YANG)); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 745 | any = (struct lysc_node_anydata*)mod->compiled->data; |
| 746 | assert_non_null(any); |
| 747 | assert_int_equal(LYS_ANYDATA, any->nodetype); |
| 748 | assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_MAND_TRUE, any->flags); |
| 749 | |
| 750 | logbuf_clean(); |
| 751 | assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;" |
| 752 | "anyxml any;}", LYS_IN_YANG)); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 753 | any = (struct lysc_node_anydata*)mod->compiled->data; |
| 754 | assert_non_null(any); |
| 755 | assert_int_equal(LYS_ANYXML, any->nodetype); |
| 756 | assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR, any->flags); |
| 757 | logbuf_assert("Use of anyxml to define configuration data is not recommended."); /* warning */ |
| 758 | |
| 759 | /* invalid */ |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 760 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;anydata any;}", LYS_IN_YANG)); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 761 | logbuf_assert("Invalid keyword \"anydata\" as a child of \"module\" - the statement is allowed only in YANG 1.1 modules. Line number 1."); |
| 762 | |
| 763 | *state = NULL; |
| 764 | ly_ctx_destroy(ctx, NULL); |
| 765 | } |
| 766 | |
Radek Krejci | 0f07bed | 2018-11-13 14:01:40 +0100 | [diff] [blame] | 767 | /** |
| 768 | * actually the same as length restriction (tested in test_type_length()), so just check the correct handling in appropriate types, |
| 769 | * do not test the expression itself |
| 770 | */ |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 771 | static void |
Radek Krejci | 0f07bed | 2018-11-13 14:01:40 +0100 | [diff] [blame] | 772 | test_type_range(void **state) |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 773 | { |
Radek Krejci | 0f07bed | 2018-11-13 14:01:40 +0100 | [diff] [blame] | 774 | *state = test_type_range; |
| 775 | |
| 776 | struct ly_ctx *ctx; |
| 777 | struct lys_module *mod; |
| 778 | struct lysc_type *type; |
| 779 | |
| 780 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 781 | |
| 782 | assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;leaf l {type int8 {range min..10|max;}}}", LYS_IN_YANG)); |
Radek Krejci | 0f07bed | 2018-11-13 14:01:40 +0100 | [diff] [blame] | 783 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 784 | assert_non_null(type); |
| 785 | assert_int_equal(LY_TYPE_INT8, type->basetype); |
| 786 | assert_non_null(((struct lysc_type_num*)type)->range); |
| 787 | assert_non_null(((struct lysc_type_num*)type)->range->parts); |
| 788 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts)); |
| 789 | assert_int_equal(-128, ((struct lysc_type_num*)type)->range->parts[0].min_64); |
| 790 | assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64); |
| 791 | assert_int_equal(127, ((struct lysc_type_num*)type)->range->parts[1].min_64); |
| 792 | assert_int_equal(127, ((struct lysc_type_num*)type)->range->parts[1].max_64); |
| 793 | |
| 794 | assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;leaf l {type int16 {range min..10|max;}}}", LYS_IN_YANG)); |
Radek Krejci | 0f07bed | 2018-11-13 14:01:40 +0100 | [diff] [blame] | 795 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 796 | assert_non_null(type); |
| 797 | assert_int_equal(LY_TYPE_INT16, type->basetype); |
| 798 | assert_non_null(((struct lysc_type_num*)type)->range); |
| 799 | assert_non_null(((struct lysc_type_num*)type)->range->parts); |
| 800 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts)); |
| 801 | assert_int_equal(-32768, ((struct lysc_type_num*)type)->range->parts[0].min_64); |
| 802 | assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64); |
| 803 | assert_int_equal(32767, ((struct lysc_type_num*)type)->range->parts[1].min_64); |
| 804 | assert_int_equal(32767, ((struct lysc_type_num*)type)->range->parts[1].max_64); |
| 805 | |
| 806 | assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c;leaf l {type int32 {range min..10|max;}}}", LYS_IN_YANG)); |
Radek Krejci | 0f07bed | 2018-11-13 14:01:40 +0100 | [diff] [blame] | 807 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 808 | assert_non_null(type); |
| 809 | assert_int_equal(LY_TYPE_INT32, type->basetype); |
| 810 | assert_non_null(((struct lysc_type_num*)type)->range); |
| 811 | assert_non_null(((struct lysc_type_num*)type)->range->parts); |
| 812 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts)); |
| 813 | assert_int_equal(INT64_C(-2147483648), ((struct lysc_type_num*)type)->range->parts[0].min_64); |
| 814 | assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64); |
| 815 | assert_int_equal(INT64_C(2147483647), ((struct lysc_type_num*)type)->range->parts[1].min_64); |
| 816 | assert_int_equal(INT64_C(2147483647), ((struct lysc_type_num*)type)->range->parts[1].max_64); |
| 817 | |
| 818 | assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d;leaf l {type int64 {range min..10|max;}}}", LYS_IN_YANG)); |
Radek Krejci | 0f07bed | 2018-11-13 14:01:40 +0100 | [diff] [blame] | 819 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 820 | assert_non_null(type); |
| 821 | assert_int_equal(LY_TYPE_INT64, type->basetype); |
| 822 | assert_non_null(((struct lysc_type_num*)type)->range); |
| 823 | assert_non_null(((struct lysc_type_num*)type)->range->parts); |
| 824 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts)); |
| 825 | assert_int_equal(INT64_C(-9223372036854775807) - INT64_C(1), ((struct lysc_type_num*)type)->range->parts[0].min_64); |
| 826 | assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64); |
| 827 | assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_num*)type)->range->parts[1].min_64); |
| 828 | assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_num*)type)->range->parts[1].max_64); |
| 829 | |
| 830 | assert_non_null(mod = lys_parse_mem(ctx, "module e {namespace urn:e;prefix e;leaf l {type uint8 {range min..10|max;}}}", LYS_IN_YANG)); |
Radek Krejci | 0f07bed | 2018-11-13 14:01:40 +0100 | [diff] [blame] | 831 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 832 | assert_non_null(type); |
| 833 | assert_int_equal(LY_TYPE_UINT8, type->basetype); |
| 834 | assert_non_null(((struct lysc_type_num*)type)->range); |
| 835 | assert_non_null(((struct lysc_type_num*)type)->range->parts); |
| 836 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts)); |
| 837 | assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64); |
| 838 | assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64); |
| 839 | assert_int_equal(255, ((struct lysc_type_num*)type)->range->parts[1].min_u64); |
| 840 | assert_int_equal(255, ((struct lysc_type_num*)type)->range->parts[1].max_u64); |
| 841 | |
| 842 | assert_non_null(mod = lys_parse_mem(ctx, "module f {namespace urn:f;prefix f;leaf l {type uint16 {range min..10|max;}}}", LYS_IN_YANG)); |
Radek Krejci | 0f07bed | 2018-11-13 14:01:40 +0100 | [diff] [blame] | 843 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 844 | assert_non_null(type); |
| 845 | assert_int_equal(LY_TYPE_UINT16, type->basetype); |
| 846 | assert_non_null(((struct lysc_type_num*)type)->range); |
| 847 | assert_non_null(((struct lysc_type_num*)type)->range->parts); |
| 848 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts)); |
| 849 | assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64); |
| 850 | assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64); |
| 851 | assert_int_equal(65535, ((struct lysc_type_num*)type)->range->parts[1].min_u64); |
| 852 | assert_int_equal(65535, ((struct lysc_type_num*)type)->range->parts[1].max_u64); |
| 853 | |
| 854 | assert_non_null(mod = lys_parse_mem(ctx, "module g {namespace urn:g;prefix g;leaf l {type uint32 {range min..10|max;}}}", LYS_IN_YANG)); |
Radek Krejci | 0f07bed | 2018-11-13 14:01:40 +0100 | [diff] [blame] | 855 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 856 | assert_non_null(type); |
| 857 | assert_int_equal(LY_TYPE_UINT32, type->basetype); |
| 858 | assert_non_null(((struct lysc_type_num*)type)->range); |
| 859 | assert_non_null(((struct lysc_type_num*)type)->range->parts); |
| 860 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts)); |
| 861 | assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64); |
| 862 | assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64); |
| 863 | assert_int_equal(UINT64_C(4294967295), ((struct lysc_type_num*)type)->range->parts[1].min_u64); |
| 864 | assert_int_equal(UINT64_C(4294967295), ((struct lysc_type_num*)type)->range->parts[1].max_u64); |
| 865 | |
| 866 | assert_non_null(mod = lys_parse_mem(ctx, "module h {namespace urn:h;prefix h;leaf l {type uint64 {range min..10|max;}}}", LYS_IN_YANG)); |
Radek Krejci | 0f07bed | 2018-11-13 14:01:40 +0100 | [diff] [blame] | 867 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 868 | assert_non_null(type); |
| 869 | assert_int_equal(LY_TYPE_UINT64, type->basetype); |
| 870 | assert_non_null(((struct lysc_type_num*)type)->range); |
| 871 | assert_non_null(((struct lysc_type_num*)type)->range->parts); |
| 872 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts)); |
| 873 | assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64); |
| 874 | assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64); |
| 875 | assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_num*)type)->range->parts[1].min_u64); |
| 876 | assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_num*)type)->range->parts[1].max_u64); |
| 877 | |
| 878 | assert_non_null(mod = lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;typedef mytype {type uint8 {range 10..100;}}" |
| 879 | "typedef mytype2 {type mytype;} leaf l {type mytype2;}}", LYS_IN_YANG)); |
Radek Krejci | 0f07bed | 2018-11-13 14:01:40 +0100 | [diff] [blame] | 880 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 881 | assert_non_null(type); |
| 882 | assert_int_equal(3, type->refcount); |
| 883 | assert_int_equal(LY_TYPE_UINT8, type->basetype); |
| 884 | assert_non_null(((struct lysc_type_num*)type)->range); |
| 885 | assert_non_null(((struct lysc_type_num*)type)->range->parts); |
| 886 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts)); |
| 887 | |
Radek Krejci | f139404 | 2019-01-08 10:53:34 +0100 | [diff] [blame] | 888 | assert_non_null(mod = lys_parse_mem(ctx, "module j {namespace urn:j;prefix j;" |
| 889 | "typedef mytype {type uint8 {range 1..100{description \"one to hundred\";reference A;}}}" |
| 890 | "leaf l {type mytype {range 1..10 {description \"one to ten\";reference B;}}}}", LYS_IN_YANG)); |
Radek Krejci | f139404 | 2019-01-08 10:53:34 +0100 | [diff] [blame] | 891 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 892 | assert_non_null(type); |
| 893 | assert_int_equal(1, type->refcount); |
| 894 | assert_int_equal(LY_TYPE_UINT8, type->basetype); |
| 895 | assert_non_null(((struct lysc_type_num*)type)->range); |
| 896 | assert_string_equal("one to ten", ((struct lysc_type_num*)type)->range->dsc); |
| 897 | assert_string_equal("B", ((struct lysc_type_num*)type)->range->ref); |
| 898 | assert_non_null(((struct lysc_type_num*)type)->range->parts); |
| 899 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts)); |
| 900 | assert_int_equal(1, ((struct lysc_type_num*)type)->range->parts[0].min_u64); |
| 901 | assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64); |
| 902 | |
Radek Krejci | 0f07bed | 2018-11-13 14:01:40 +0100 | [diff] [blame] | 903 | *state = NULL; |
| 904 | ly_ctx_destroy(ctx, NULL); |
| 905 | } |
| 906 | |
| 907 | static void |
| 908 | test_type_length(void **state) |
| 909 | { |
| 910 | *state = test_type_length; |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 911 | |
| 912 | struct ly_ctx *ctx; |
| 913 | struct lys_module *mod; |
| 914 | struct lysc_type *type; |
| 915 | |
| 916 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 917 | |
Radek Krejci | c5ddcc0 | 2018-11-12 13:28:18 +0100 | [diff] [blame] | 918 | assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;leaf l {type binary {length min {error-app-tag errortag;error-message error;}}}}", LYS_IN_YANG)); |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 919 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 920 | assert_non_null(type); |
| 921 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 922 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
Radek Krejci | c5ddcc0 | 2018-11-12 13:28:18 +0100 | [diff] [blame] | 923 | assert_string_equal("errortag", ((struct lysc_type_bin*)type)->length->eapptag); |
| 924 | assert_string_equal("error", ((struct lysc_type_bin*)type)->length->emsg); |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 925 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 926 | assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
| 927 | assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
| 928 | |
| 929 | assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;leaf l {type binary {length max;}}}", LYS_IN_YANG)); |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 930 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 931 | assert_non_null(type); |
| 932 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 933 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
| 934 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
Radek Krejci | 0fe2075 | 2018-11-27 11:33:24 +0100 | [diff] [blame] | 935 | assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
| 936 | assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 937 | |
| 938 | assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c;leaf l {type binary {length min..max;}}}", LYS_IN_YANG)); |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 939 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 940 | assert_non_null(type); |
| 941 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 942 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
| 943 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 944 | assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
Radek Krejci | 0fe2075 | 2018-11-27 11:33:24 +0100 | [diff] [blame] | 945 | assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 946 | |
| 947 | assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d;leaf l {type binary {length 5;}}}", LYS_IN_YANG)); |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 948 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 949 | assert_non_null(type); |
| 950 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 951 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
| 952 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 953 | assert_int_equal(5, ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
| 954 | assert_int_equal(5, ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
| 955 | |
| 956 | assert_non_null(mod = lys_parse_mem(ctx, "module e {namespace urn:e;prefix e;leaf l {type binary {length 1..10;}}}", LYS_IN_YANG)); |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 957 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 958 | assert_non_null(type); |
| 959 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 960 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
| 961 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 962 | assert_int_equal(1, ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
| 963 | assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
| 964 | |
| 965 | assert_non_null(mod = lys_parse_mem(ctx, "module f {namespace urn:f;prefix f;leaf l {type binary {length 1..10|20..30;}}}", LYS_IN_YANG)); |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 966 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 967 | assert_non_null(type); |
| 968 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 969 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
| 970 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 971 | assert_int_equal(1, ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
| 972 | assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
| 973 | assert_int_equal(20, ((struct lysc_type_bin*)type)->length->parts[1].min_u64); |
| 974 | assert_int_equal(30, ((struct lysc_type_bin*)type)->length->parts[1].max_u64); |
| 975 | |
| 976 | assert_non_null(mod = lys_parse_mem(ctx, "module g {namespace urn:g;prefix g;leaf l {type binary {length \"16 | 32\";}}}", LYS_IN_YANG)); |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 977 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 978 | assert_non_null(type); |
| 979 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 980 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
| 981 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 982 | assert_int_equal(16, ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
| 983 | assert_int_equal(16, ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
| 984 | assert_int_equal(32, ((struct lysc_type_bin*)type)->length->parts[1].min_u64); |
| 985 | assert_int_equal(32, ((struct lysc_type_bin*)type)->length->parts[1].max_u64); |
| 986 | |
Radek Krejci | b31c899 | 2018-11-12 16:29:16 +0100 | [diff] [blame] | 987 | assert_non_null(mod = lys_parse_mem(ctx, "module h {namespace urn:h;prefix h;typedef mytype {type binary {length 10;}}" |
| 988 | "leaf l {type mytype {length \"10\";}}}", LYS_IN_YANG)); |
Radek Krejci | b31c899 | 2018-11-12 16:29:16 +0100 | [diff] [blame] | 989 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 990 | assert_non_null(type); |
| 991 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 992 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
| 993 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 994 | assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
| 995 | assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
| 996 | |
| 997 | assert_non_null(mod = lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;typedef mytype {type binary {length 10..100;}}" |
| 998 | "leaf l {type mytype {length \"50\";}}}", LYS_IN_YANG)); |
Radek Krejci | b31c899 | 2018-11-12 16:29:16 +0100 | [diff] [blame] | 999 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1000 | assert_non_null(type); |
| 1001 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 1002 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
| 1003 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 1004 | assert_int_equal(50, ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
| 1005 | assert_int_equal(50, ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
| 1006 | |
| 1007 | assert_non_null(mod = lys_parse_mem(ctx, "module j {namespace urn:j;prefix j;typedef mytype {type binary {length 10..100;}}" |
| 1008 | "leaf l {type mytype {length \"10..30|60..100\";}}}", LYS_IN_YANG)); |
Radek Krejci | b31c899 | 2018-11-12 16:29:16 +0100 | [diff] [blame] | 1009 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1010 | assert_non_null(type); |
| 1011 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 1012 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
| 1013 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 1014 | assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
| 1015 | assert_int_equal(30, ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
| 1016 | assert_int_equal(60, ((struct lysc_type_bin*)type)->length->parts[1].min_u64); |
| 1017 | assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[1].max_u64); |
| 1018 | |
| 1019 | assert_non_null(mod = lys_parse_mem(ctx, "module k {namespace urn:k;prefix k;typedef mytype {type binary {length 10..100;}}" |
Radek Krejci | 56aa27c | 2018-11-13 14:21:59 +0100 | [diff] [blame] | 1020 | "leaf l {type mytype {length \"10..80\";}}leaf ll {type mytype;}}", LYS_IN_YANG)); |
Radek Krejci | b31c899 | 2018-11-12 16:29:16 +0100 | [diff] [blame] | 1021 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1022 | assert_non_null(type); |
Radek Krejci | 56aa27c | 2018-11-13 14:21:59 +0100 | [diff] [blame] | 1023 | assert_int_equal(1, type->refcount); |
Radek Krejci | b31c899 | 2018-11-12 16:29:16 +0100 | [diff] [blame] | 1024 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 1025 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
| 1026 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 1027 | assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
Radek Krejci | 56aa27c | 2018-11-13 14:21:59 +0100 | [diff] [blame] | 1028 | assert_int_equal(80, ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
Radek Krejci | b31c899 | 2018-11-12 16:29:16 +0100 | [diff] [blame] | 1029 | type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type; |
| 1030 | assert_non_null(type); |
Radek Krejci | 56aa27c | 2018-11-13 14:21:59 +0100 | [diff] [blame] | 1031 | assert_int_equal(2, type->refcount); |
Radek Krejci | b31c899 | 2018-11-12 16:29:16 +0100 | [diff] [blame] | 1032 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 1033 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
| 1034 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 1035 | assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
| 1036 | assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
| 1037 | |
Radek Krejci | 56aa27c | 2018-11-13 14:21:59 +0100 | [diff] [blame] | 1038 | assert_non_null(mod = lys_parse_mem(ctx, "module l {namespace urn:l;prefix l;typedef mytype {type string {length 10..100;}}" |
| 1039 | "typedef mytype2 {type mytype {pattern '[0-9]*';}} leaf l {type mytype2 {pattern '[0-4]*';}}}", LYS_IN_YANG)); |
Radek Krejci | 9a191e6 | 2018-11-13 13:19:56 +0100 | [diff] [blame] | 1040 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1041 | assert_non_null(type); |
| 1042 | assert_int_equal(LY_TYPE_STRING, type->basetype); |
Radek Krejci | 56aa27c | 2018-11-13 14:21:59 +0100 | [diff] [blame] | 1043 | assert_int_equal(1, type->refcount); |
Radek Krejci | cda7415 | 2018-11-13 15:27:32 +0100 | [diff] [blame] | 1044 | assert_non_null(((struct lysc_type_str*)type)->length); |
| 1045 | assert_non_null(((struct lysc_type_str*)type)->length->parts); |
| 1046 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->length->parts)); |
| 1047 | assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].min_u64); |
| 1048 | assert_int_equal(100, ((struct lysc_type_str*)type)->length->parts[0].max_u64); |
Radek Krejci | 9a191e6 | 2018-11-13 13:19:56 +0100 | [diff] [blame] | 1049 | |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1050 | assert_non_null(mod = lys_parse_mem(ctx, "module m {namespace urn:m;prefix m;typedef mytype {type string {length 10;}}" |
| 1051 | "leaf l {type mytype {length min..max;}}}", LYS_IN_YANG)); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1052 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1053 | assert_non_null(type); |
| 1054 | assert_int_equal(LY_TYPE_STRING, type->basetype); |
| 1055 | assert_int_equal(1, type->refcount); |
| 1056 | assert_non_null(((struct lysc_type_str*)type)->length); |
| 1057 | assert_non_null(((struct lysc_type_str*)type)->length->parts); |
| 1058 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->length->parts)); |
| 1059 | assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].min_u64); |
| 1060 | assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].max_u64); |
| 1061 | |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 1062 | /* invalid values */ |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1063 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;leaf l {type binary {length -10;}}}", LYS_IN_YANG)); |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 1064 | logbuf_assert("Invalid length restriction - value \"-10\" does not fit the type limitations."); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1065 | assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;leaf l {type binary {length 18446744073709551616;}}}", LYS_IN_YANG)); |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 1066 | logbuf_assert("Invalid length restriction - invalid value \"18446744073709551616\"."); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1067 | assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;leaf l {type binary {length \"max .. 10\";}}}", LYS_IN_YANG)); |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 1068 | logbuf_assert("Invalid length restriction - unexpected data after max keyword (.. 10)."); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1069 | assert_null(lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd;leaf l {type binary {length 50..10;}}}", LYS_IN_YANG)); |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 1070 | logbuf_assert("Invalid length restriction - values are not in ascending order (10)."); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1071 | assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;leaf l {type binary {length \"50 | 10\";}}}", LYS_IN_YANG)); |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 1072 | logbuf_assert("Invalid length restriction - values are not in ascending order (10)."); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1073 | assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;leaf l {type binary {length \"x\";}}}", LYS_IN_YANG)); |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 1074 | logbuf_assert("Invalid length restriction - unexpected data (x)."); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1075 | assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;leaf l {type binary {length \"50 | min\";}}}", LYS_IN_YANG)); |
Radek Krejci | b31c899 | 2018-11-12 16:29:16 +0100 | [diff] [blame] | 1076 | logbuf_assert("Invalid length restriction - unexpected data before min keyword (50 | )."); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1077 | assert_null(lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;leaf l {type binary {length \"| 50\";}}}", LYS_IN_YANG)); |
Radek Krejci | b31c899 | 2018-11-12 16:29:16 +0100 | [diff] [blame] | 1078 | logbuf_assert("Invalid length restriction - unexpected beginning of the expression (| 50)."); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1079 | assert_null(lys_parse_mem(ctx, "module ii {namespace urn:ii;prefix ii;leaf l {type binary {length \"10 ..\";}}}", LYS_IN_YANG)); |
Radek Krejci | b31c899 | 2018-11-12 16:29:16 +0100 | [diff] [blame] | 1080 | logbuf_assert("Invalid length restriction - unexpected end of the expression after \"..\" (10 ..)."); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1081 | assert_null(lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;leaf l {type binary {length \".. 10\";}}}", LYS_IN_YANG)); |
Radek Krejci | b31c899 | 2018-11-12 16:29:16 +0100 | [diff] [blame] | 1082 | logbuf_assert("Invalid length restriction - unexpected \"..\" without a lower bound."); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1083 | assert_null(lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;leaf l {type binary {length \"10 |\";}}}", LYS_IN_YANG)); |
Radek Krejci | b31c899 | 2018-11-12 16:29:16 +0100 | [diff] [blame] | 1084 | logbuf_assert("Invalid length restriction - unexpected end of the expression (10 |)."); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1085 | assert_null(lys_parse_mem(ctx, "module kl {namespace urn:kl;prefix kl;leaf l {type binary {length \"10..20 | 15..30\";}}}", LYS_IN_YANG)); |
Radek Krejci | 6489bbe | 2018-11-12 17:05:19 +0100 | [diff] [blame] | 1086 | logbuf_assert("Invalid length restriction - values are not in ascending order (15)."); |
Radek Krejci | b31c899 | 2018-11-12 16:29:16 +0100 | [diff] [blame] | 1087 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1088 | assert_null(lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;typedef mytype {type binary {length 10;}}" |
| 1089 | "leaf l {type mytype {length 11;}}}", LYS_IN_YANG)); |
Radek Krejci | b31c899 | 2018-11-12 16:29:16 +0100 | [diff] [blame] | 1090 | logbuf_assert("Invalid length restriction - the derived restriction (11) is not equally or more limiting."); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1091 | assert_null(lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;typedef mytype {type binary {length 10..100;}}" |
| 1092 | "leaf l {type mytype {length 1..11;}}}", LYS_IN_YANG)); |
Radek Krejci | b31c899 | 2018-11-12 16:29:16 +0100 | [diff] [blame] | 1093 | logbuf_assert("Invalid length restriction - the derived restriction (1..11) is not equally or more limiting."); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1094 | assert_null(lys_parse_mem(ctx, "module nn {namespace urn:nn;prefix nn;typedef mytype {type binary {length 10..100;}}" |
| 1095 | "leaf l {type mytype {length 20..110;}}}", LYS_IN_YANG)); |
Radek Krejci | b31c899 | 2018-11-12 16:29:16 +0100 | [diff] [blame] | 1096 | logbuf_assert("Invalid length restriction - the derived restriction (20..110) is not equally or more limiting."); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1097 | assert_null(lys_parse_mem(ctx, "module oo {namespace urn:oo;prefix oo;typedef mytype {type binary {length 10..100;}}" |
| 1098 | "leaf l {type mytype {length 20..30|110..120;}}}", LYS_IN_YANG)); |
Radek Krejci | b31c899 | 2018-11-12 16:29:16 +0100 | [diff] [blame] | 1099 | logbuf_assert("Invalid length restriction - the derived restriction (20..30|110..120) is not equally or more limiting."); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1100 | assert_null(lys_parse_mem(ctx, "module pp {namespace urn:pp;prefix pp;typedef mytype {type binary {length 10..11;}}" |
Radek Krejci | b31c899 | 2018-11-12 16:29:16 +0100 | [diff] [blame] | 1101 | "leaf l {type mytype {length 15;}}}", LYS_IN_YANG)); |
Radek Krejci | b31c899 | 2018-11-12 16:29:16 +0100 | [diff] [blame] | 1102 | logbuf_assert("Invalid length restriction - the derived restriction (15) is not equally or more limiting."); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1103 | assert_null(lys_parse_mem(ctx, "module qq {namespace urn:qq;prefix qq;typedef mytype {type binary {length 10..20|30..40;}}" |
Radek Krejci | b31c899 | 2018-11-12 16:29:16 +0100 | [diff] [blame] | 1104 | "leaf l {type mytype {length 15..35;}}}", LYS_IN_YANG)); |
Radek Krejci | b31c899 | 2018-11-12 16:29:16 +0100 | [diff] [blame] | 1105 | logbuf_assert("Invalid length restriction - the derived restriction (15..35) is not equally or more limiting."); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1106 | assert_null(lys_parse_mem(ctx, "module rr {namespace urn:rr;prefix rr;typedef mytype {type binary {length 10;}}" |
Radek Krejci | 6489bbe | 2018-11-12 17:05:19 +0100 | [diff] [blame] | 1107 | "leaf l {type mytype {length 10..35;}}}", LYS_IN_YANG)); |
Radek Krejci | 6489bbe | 2018-11-12 17:05:19 +0100 | [diff] [blame] | 1108 | logbuf_assert("Invalid length restriction - the derived restriction (10..35) is not equally or more limiting."); |
Radek Krejci | b31c899 | 2018-11-12 16:29:16 +0100 | [diff] [blame] | 1109 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1110 | assert_null(lys_parse_mem(ctx, "module ss {namespace urn:rr;prefix rr;leaf l {type binary {pattern '[0-9]*';}}}", LYS_IN_YANG)); |
Radek Krejci | 495903e | 2018-11-13 11:30:45 +0100 | [diff] [blame] | 1111 | logbuf_assert("Invalid type restrictions for binary type."); |
| 1112 | |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 1113 | *state = NULL; |
| 1114 | ly_ctx_destroy(ctx, NULL); |
| 1115 | } |
| 1116 | |
Radek Krejci | cda7415 | 2018-11-13 15:27:32 +0100 | [diff] [blame] | 1117 | static void |
| 1118 | test_type_pattern(void **state) |
| 1119 | { |
| 1120 | *state = test_type_pattern; |
| 1121 | |
| 1122 | struct ly_ctx *ctx; |
| 1123 | struct lys_module *mod; |
| 1124 | struct lysc_type *type; |
| 1125 | |
| 1126 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 1127 | |
Radek Krejci | 027d580 | 2018-11-14 16:57:28 +0100 | [diff] [blame] | 1128 | assert_non_null(mod = lys_parse_mem(ctx, "module a {yang-version 1.1; namespace urn:a;prefix a;leaf l {type string {" |
Radek Krejci | cda7415 | 2018-11-13 15:27:32 +0100 | [diff] [blame] | 1129 | "pattern .* {error-app-tag errortag;error-message error;}" |
| 1130 | "pattern [0-9].*[0-9] {modifier invert-match;}}}}", LYS_IN_YANG)); |
Radek Krejci | cda7415 | 2018-11-13 15:27:32 +0100 | [diff] [blame] | 1131 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1132 | assert_non_null(type); |
| 1133 | assert_non_null(((struct lysc_type_str*)type)->patterns); |
| 1134 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns)); |
| 1135 | assert_string_equal("errortag", ((struct lysc_type_str*)type)->patterns[0]->eapptag); |
| 1136 | assert_string_equal("error", ((struct lysc_type_str*)type)->patterns[0]->emsg); |
| 1137 | assert_int_equal(0, ((struct lysc_type_str*)type)->patterns[0]->inverted); |
| 1138 | assert_null(((struct lysc_type_str*)type)->patterns[1]->eapptag); |
| 1139 | assert_null(((struct lysc_type_str*)type)->patterns[1]->emsg); |
| 1140 | assert_int_equal(1, ((struct lysc_type_str*)type)->patterns[1]->inverted); |
| 1141 | |
| 1142 | assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;typedef mytype {type string {pattern '[0-9]*';}}" |
| 1143 | "typedef mytype2 {type mytype {length 10;}} leaf l {type mytype2 {pattern '[0-4]*';}}}", LYS_IN_YANG)); |
Radek Krejci | cda7415 | 2018-11-13 15:27:32 +0100 | [diff] [blame] | 1144 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1145 | assert_non_null(type); |
| 1146 | assert_int_equal(LY_TYPE_STRING, type->basetype); |
| 1147 | assert_int_equal(1, type->refcount); |
| 1148 | assert_non_null(((struct lysc_type_str*)type)->patterns); |
| 1149 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns)); |
| 1150 | assert_int_equal(3, ((struct lysc_type_str*)type)->patterns[0]->refcount); |
| 1151 | assert_int_equal(1, ((struct lysc_type_str*)type)->patterns[1]->refcount); |
| 1152 | |
Radek Krejci | 04bc1e9 | 2018-11-14 14:28:13 +0100 | [diff] [blame] | 1153 | assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c;typedef mytype {type string {pattern '[0-9]*';}}" |
| 1154 | "leaf l {type mytype {length 10;}}}", LYS_IN_YANG)); |
Radek Krejci | 04bc1e9 | 2018-11-14 14:28:13 +0100 | [diff] [blame] | 1155 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1156 | assert_non_null(type); |
| 1157 | assert_int_equal(LY_TYPE_STRING, type->basetype); |
| 1158 | assert_int_equal(1, type->refcount); |
| 1159 | assert_non_null(((struct lysc_type_str*)type)->patterns); |
| 1160 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns)); |
| 1161 | assert_int_equal(2, ((struct lysc_type_str*)type)->patterns[0]->refcount); |
| 1162 | |
Radek Krejci | cda7415 | 2018-11-13 15:27:32 +0100 | [diff] [blame] | 1163 | /* test substitutions */ |
Radek Krejci | 04bc1e9 | 2018-11-14 14:28:13 +0100 | [diff] [blame] | 1164 | assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d;leaf l {type string {" |
Radek Krejci | cda7415 | 2018-11-13 15:27:32 +0100 | [diff] [blame] | 1165 | "pattern '^\\p{IsLatinExtended-A}$';}}}", LYS_IN_YANG)); |
Radek Krejci | cda7415 | 2018-11-13 15:27:32 +0100 | [diff] [blame] | 1166 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1167 | assert_non_null(type); |
| 1168 | assert_non_null(((struct lysc_type_str*)type)->patterns); |
| 1169 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns)); |
| 1170 | /* TODO check some data "^Å™$" */ |
| 1171 | |
| 1172 | *state = NULL; |
| 1173 | ly_ctx_destroy(ctx, NULL); |
| 1174 | } |
| 1175 | |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1176 | static void |
| 1177 | test_type_enum(void **state) |
| 1178 | { |
| 1179 | *state = test_type_enum; |
| 1180 | |
| 1181 | struct ly_ctx *ctx; |
| 1182 | struct lys_module *mod; |
| 1183 | struct lysc_type *type; |
| 1184 | |
| 1185 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 1186 | |
Radek Krejci | 027d580 | 2018-11-14 16:57:28 +0100 | [diff] [blame] | 1187 | assert_non_null(mod = lys_parse_mem(ctx, "module a {yang-version 1.1; namespace urn:a;prefix a;feature f; leaf l {type enumeration {" |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1188 | "enum automin; enum min {value -2147483648;}enum one {if-feature f; value 1;}" |
| 1189 | "enum two; enum seven {value 7;}enum eight;}}}", LYS_IN_YANG)); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1190 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1191 | assert_non_null(type); |
| 1192 | assert_int_equal(LY_TYPE_ENUM, type->basetype); |
| 1193 | assert_non_null(((struct lysc_type_enum*)type)->enums); |
| 1194 | assert_int_equal(6, LY_ARRAY_SIZE(((struct lysc_type_enum*)type)->enums)); |
| 1195 | assert_non_null(((struct lysc_type_enum*)type)->enums[2].iffeatures); |
| 1196 | assert_string_equal("automin", ((struct lysc_type_enum*)type)->enums[0].name); |
| 1197 | assert_int_equal(0, ((struct lysc_type_enum*)type)->enums[0].value); |
| 1198 | assert_string_equal("min", ((struct lysc_type_enum*)type)->enums[1].name); |
| 1199 | assert_int_equal(-2147483648, ((struct lysc_type_enum*)type)->enums[1].value); |
| 1200 | assert_string_equal("one", ((struct lysc_type_enum*)type)->enums[2].name); |
| 1201 | assert_int_equal(1, ((struct lysc_type_enum*)type)->enums[2].value); |
| 1202 | assert_string_equal("two", ((struct lysc_type_enum*)type)->enums[3].name); |
| 1203 | assert_int_equal(2, ((struct lysc_type_enum*)type)->enums[3].value); |
| 1204 | assert_string_equal("seven", ((struct lysc_type_enum*)type)->enums[4].name); |
| 1205 | assert_int_equal(7, ((struct lysc_type_enum*)type)->enums[4].value); |
| 1206 | assert_string_equal("eight", ((struct lysc_type_enum*)type)->enums[5].name); |
| 1207 | assert_int_equal(8, ((struct lysc_type_enum*)type)->enums[5].value); |
| 1208 | |
Radek Krejci | 027d580 | 2018-11-14 16:57:28 +0100 | [diff] [blame] | 1209 | assert_non_null(mod = lys_parse_mem(ctx, "module b {yang-version 1.1; namespace urn:b;prefix b;feature f; typedef mytype {type enumeration {" |
| 1210 | "enum 11; enum min {value -2147483648;}enum x$&;" |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1211 | "enum two; enum seven {value 7;}enum eight;}} leaf l { type mytype {enum seven;enum eight;}}}", |
| 1212 | LYS_IN_YANG)); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1213 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1214 | assert_non_null(type); |
| 1215 | assert_int_equal(LY_TYPE_ENUM, type->basetype); |
| 1216 | assert_non_null(((struct lysc_type_enum*)type)->enums); |
| 1217 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_enum*)type)->enums)); |
| 1218 | assert_string_equal("seven", ((struct lysc_type_enum*)type)->enums[0].name); |
| 1219 | assert_int_equal(7, ((struct lysc_type_enum*)type)->enums[0].value); |
| 1220 | assert_string_equal("eight", ((struct lysc_type_enum*)type)->enums[1].name); |
| 1221 | assert_int_equal(8, ((struct lysc_type_enum*)type)->enums[1].value); |
| 1222 | |
| 1223 | |
| 1224 | /* invalid cases */ |
Radek Krejci | 027d580 | 2018-11-14 16:57:28 +0100 | [diff] [blame] | 1225 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; feature f; leaf l {type enumeration {" |
| 1226 | "enum one {if-feature f;}}}}", LYS_IN_YANG)); |
| 1227 | logbuf_assert("Invalid keyword \"if-feature\" as a child of \"enum\" - the statement is allowed only in YANG 1.1 modules. Line number 1."); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1228 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {" |
| 1229 | "enum one {value -2147483649;}}}}", LYS_IN_YANG)); |
| 1230 | logbuf_assert("Invalid value \"-2147483649\" of \"value\". Line number 1."); |
| 1231 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {" |
| 1232 | "enum one {value 2147483648;}}}}", LYS_IN_YANG)); |
| 1233 | logbuf_assert("Invalid value \"2147483648\" of \"value\". Line number 1."); |
| 1234 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {" |
| 1235 | "enum one; enum one;}}}", LYS_IN_YANG)); |
| 1236 | logbuf_assert("Duplicate identifier \"one\" of enum statement. Line number 1."); |
| 1237 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {" |
| 1238 | "enum '';}}}", LYS_IN_YANG)); |
| 1239 | logbuf_assert("Enum name must not be zero-length. Line number 1."); |
| 1240 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {" |
| 1241 | "enum ' x';}}}", LYS_IN_YANG)); |
| 1242 | logbuf_assert("Enum name must not have any leading or trailing whitespaces (\" x\"). Line number 1."); |
| 1243 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {" |
| 1244 | "enum 'x ';}}}", LYS_IN_YANG)); |
| 1245 | logbuf_assert("Enum name must not have any leading or trailing whitespaces (\"x \"). Line number 1."); |
| 1246 | assert_non_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {" |
| 1247 | "enum 'inva\nlid';}}}", LYS_IN_YANG)); |
| 1248 | logbuf_assert("Control characters in enum name should be avoided (\"inva\nlid\", character number 5)."); |
| 1249 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1250 | assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; leaf l {type enumeration;}}", LYS_IN_YANG)); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1251 | logbuf_assert("Missing enum substatement for enumeration type."); |
| 1252 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1253 | assert_null(lys_parse_mem(ctx, "module cc {yang-version 1.1;namespace urn:cc;prefix cc;typedef mytype {type enumeration {enum one;}}" |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1254 | "leaf l {type mytype {enum two;}}}", LYS_IN_YANG)); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1255 | logbuf_assert("Invalid enumeration - derived type adds new item \"two\"."); |
| 1256 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1257 | assert_null(lys_parse_mem(ctx, "module dd {yang-version 1.1;namespace urn:dd;prefix dd;typedef mytype {type enumeration {enum one;}}" |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1258 | "leaf l {type mytype {enum one {value 1;}}}}", LYS_IN_YANG)); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1259 | logbuf_assert("Invalid enumeration - value of the item \"one\" has changed from 0 to 1 in the derived type."); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1260 | assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;leaf l {type enumeration {enum x {value 2147483647;}enum y;}}}", |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1261 | LYS_IN_YANG)); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1262 | logbuf_assert("Invalid enumeration - it is not possible to auto-assign enum value for \"y\" since the highest value is already 2147483647."); |
| 1263 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1264 | assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;leaf l {type enumeration {enum x {value 1;}enum y {value 1;}}}}", |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1265 | LYS_IN_YANG)); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1266 | logbuf_assert("Invalid enumeration - value 1 collide in items \"y\" and \"x\"."); |
| 1267 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1268 | assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;typedef mytype {type enumeration;}" |
Radek Krejci | 04bc1e9 | 2018-11-14 14:28:13 +0100 | [diff] [blame] | 1269 | "leaf l {type mytype {enum one;}}}", LYS_IN_YANG)); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1270 | logbuf_assert("Missing enum substatement for enumeration type mytype."); |
Radek Krejci | 04bc1e9 | 2018-11-14 14:28:13 +0100 | [diff] [blame] | 1271 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1272 | assert_null(lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh; typedef mytype {type enumeration {enum one;}}" |
Radek Krejci | 027d580 | 2018-11-14 16:57:28 +0100 | [diff] [blame] | 1273 | "leaf l {type mytype {enum one;}}}", LYS_IN_YANG)); |
Radek Krejci | 027d580 | 2018-11-14 16:57:28 +0100 | [diff] [blame] | 1274 | logbuf_assert("Enumeration type can be subtyped only in YANG 1.1 modules."); |
| 1275 | |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1276 | *state = NULL; |
| 1277 | ly_ctx_destroy(ctx, NULL); |
| 1278 | } |
| 1279 | |
| 1280 | static void |
| 1281 | test_type_bits(void **state) |
| 1282 | { |
| 1283 | *state = test_type_bits; |
| 1284 | |
| 1285 | struct ly_ctx *ctx; |
| 1286 | struct lys_module *mod; |
| 1287 | struct lysc_type *type; |
| 1288 | |
| 1289 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 1290 | |
Radek Krejci | 027d580 | 2018-11-14 16:57:28 +0100 | [diff] [blame] | 1291 | assert_non_null(mod = lys_parse_mem(ctx, "module a {yang-version 1.1; namespace urn:a;prefix a;feature f; leaf l {type bits {" |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1292 | "bit automin; bit one {if-feature f; position 1;}" |
| 1293 | "bit two; bit seven {position 7;}bit eight;}}}", LYS_IN_YANG)); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1294 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1295 | assert_non_null(type); |
| 1296 | assert_int_equal(LY_TYPE_BITS, type->basetype); |
| 1297 | assert_non_null(((struct lysc_type_bits*)type)->bits); |
| 1298 | assert_int_equal(5, LY_ARRAY_SIZE(((struct lysc_type_bits*)type)->bits)); |
| 1299 | assert_non_null(((struct lysc_type_bits*)type)->bits[1].iffeatures); |
| 1300 | assert_string_equal("automin", ((struct lysc_type_bits*)type)->bits[0].name); |
| 1301 | assert_int_equal(0, ((struct lysc_type_bits*)type)->bits[0].position); |
| 1302 | assert_string_equal("one", ((struct lysc_type_bits*)type)->bits[1].name); |
| 1303 | assert_int_equal(1, ((struct lysc_type_bits*)type)->bits[1].position); |
| 1304 | assert_string_equal("two", ((struct lysc_type_bits*)type)->bits[2].name); |
| 1305 | assert_int_equal(2, ((struct lysc_type_bits*)type)->bits[2].position); |
| 1306 | assert_string_equal("seven", ((struct lysc_type_bits*)type)->bits[3].name); |
| 1307 | assert_int_equal(7, ((struct lysc_type_bits*)type)->bits[3].position); |
| 1308 | assert_string_equal("eight", ((struct lysc_type_bits*)type)->bits[4].name); |
| 1309 | assert_int_equal(8, ((struct lysc_type_bits*)type)->bits[4].position); |
| 1310 | |
Radek Krejci | 027d580 | 2018-11-14 16:57:28 +0100 | [diff] [blame] | 1311 | assert_non_null(mod = lys_parse_mem(ctx, "module b {yang-version 1.1;namespace urn:b;prefix b;feature f; typedef mytype {type bits {" |
| 1312 | "bit automin; bit one;bit two; bit seven {value 7;}bit eight;}} leaf l { type mytype {bit eight;bit seven;bit automin;}}}", |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1313 | LYS_IN_YANG)); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1314 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1315 | assert_non_null(type); |
| 1316 | assert_int_equal(LY_TYPE_BITS, type->basetype); |
| 1317 | assert_non_null(((struct lysc_type_bits*)type)->bits); |
Radek Krejci | 027d580 | 2018-11-14 16:57:28 +0100 | [diff] [blame] | 1318 | assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_bits*)type)->bits)); |
| 1319 | assert_string_equal("automin", ((struct lysc_type_bits*)type)->bits[0].name); |
| 1320 | assert_int_equal(0, ((struct lysc_type_bits*)type)->bits[0].position); |
| 1321 | assert_string_equal("seven", ((struct lysc_type_bits*)type)->bits[1].name); |
| 1322 | assert_int_equal(7, ((struct lysc_type_bits*)type)->bits[1].position); |
| 1323 | assert_string_equal("eight", ((struct lysc_type_bits*)type)->bits[2].name); |
| 1324 | assert_int_equal(8, ((struct lysc_type_bits*)type)->bits[2].position); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1325 | |
| 1326 | |
| 1327 | /* invalid cases */ |
Radek Krejci | 027d580 | 2018-11-14 16:57:28 +0100 | [diff] [blame] | 1328 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; feature f; leaf l {type bits {" |
| 1329 | "bit one {if-feature f;}}}}", LYS_IN_YANG)); |
| 1330 | logbuf_assert("Invalid keyword \"if-feature\" as a child of \"bit\" - the statement is allowed only in YANG 1.1 modules. Line number 1."); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1331 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {" |
| 1332 | "bit one {position -1;}}}}", LYS_IN_YANG)); |
| 1333 | logbuf_assert("Invalid value \"-1\" of \"position\". Line number 1."); |
| 1334 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {" |
| 1335 | "bit one {value 4294967296;}}}}", LYS_IN_YANG)); |
| 1336 | logbuf_assert("Invalid value \"4294967296\" of \"value\". Line number 1."); |
| 1337 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {" |
| 1338 | "bit one; bit one;}}}", LYS_IN_YANG)); |
| 1339 | logbuf_assert("Duplicate identifier \"one\" of bit statement. Line number 1."); |
| 1340 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {" |
| 1341 | "bit '11';}}}", LYS_IN_YANG)); |
| 1342 | logbuf_assert("Invalid identifier first character '1'. Line number 1."); |
| 1343 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {" |
| 1344 | "bit 'x1$1';}}}", LYS_IN_YANG)); |
| 1345 | logbuf_assert("Invalid identifier character '$'. Line number 1."); |
| 1346 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1347 | assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; leaf l {type bits;}}", LYS_IN_YANG)); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1348 | logbuf_assert("Missing bit substatement for bits type."); |
| 1349 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1350 | assert_null(lys_parse_mem(ctx, "module cc {yang-version 1.1;namespace urn:cc;prefix cc;typedef mytype {type bits {bit one;}}" |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1351 | "leaf l {type mytype {bit two;}}}", LYS_IN_YANG)); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1352 | logbuf_assert("Invalid bits - derived type adds new item \"two\"."); |
| 1353 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1354 | assert_null(lys_parse_mem(ctx, "module dd {yang-version 1.1;namespace urn:dd;prefix dd;typedef mytype {type bits {bit one;}}" |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1355 | "leaf l {type mytype {bit one {position 1;}}}}", LYS_IN_YANG)); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1356 | logbuf_assert("Invalid bits - position of the item \"one\" has changed from 0 to 1 in the derived type."); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1357 | assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;leaf l {type bits {bit x {position 4294967295;}bit y;}}}", |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1358 | LYS_IN_YANG)); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1359 | logbuf_assert("Invalid bits - it is not possible to auto-assign bit position for \"y\" since the highest value is already 4294967295."); |
| 1360 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1361 | assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;leaf l {type bits {bit x {value 1;}bit y {value 1;}}}}", |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1362 | LYS_IN_YANG)); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1363 | logbuf_assert("Invalid bits - position 1 collide in items \"y\" and \"x\"."); |
| 1364 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1365 | assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;typedef mytype {type bits;}" |
Radek Krejci | 04bc1e9 | 2018-11-14 14:28:13 +0100 | [diff] [blame] | 1366 | "leaf l {type mytype {bit one;}}}", LYS_IN_YANG)); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1367 | logbuf_assert("Missing bit substatement for bits type mytype."); |
Radek Krejci | 04bc1e9 | 2018-11-14 14:28:13 +0100 | [diff] [blame] | 1368 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1369 | assert_null(lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh; typedef mytype {type bits {bit one;}}" |
Radek Krejci | 027d580 | 2018-11-14 16:57:28 +0100 | [diff] [blame] | 1370 | "leaf l {type mytype {bit one;}}}", LYS_IN_YANG)); |
Radek Krejci | 027d580 | 2018-11-14 16:57:28 +0100 | [diff] [blame] | 1371 | logbuf_assert("Bits type can be subtyped only in YANG 1.1 modules."); |
| 1372 | |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1373 | *state = NULL; |
| 1374 | ly_ctx_destroy(ctx, NULL); |
| 1375 | } |
| 1376 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1377 | static void |
| 1378 | test_type_dec64(void **state) |
| 1379 | { |
| 1380 | *state = test_type_dec64; |
| 1381 | |
| 1382 | struct ly_ctx *ctx; |
| 1383 | struct lys_module *mod; |
| 1384 | struct lysc_type *type; |
| 1385 | |
| 1386 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 1387 | |
| 1388 | assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;leaf l {type decimal64 {" |
| 1389 | "fraction-digits 2;range min..max;}}}", LYS_IN_YANG)); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1390 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1391 | assert_non_null(type); |
| 1392 | assert_int_equal(LY_TYPE_DEC64, type->basetype); |
| 1393 | assert_int_equal(2, ((struct lysc_type_dec*)type)->fraction_digits); |
| 1394 | assert_non_null(((struct lysc_type_dec*)type)->range); |
| 1395 | assert_non_null(((struct lysc_type_dec*)type)->range->parts); |
| 1396 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_dec*)type)->range->parts)); |
| 1397 | assert_int_equal(INT64_C(-9223372036854775807) - INT64_C(1), ((struct lysc_type_dec*)type)->range->parts[0].min_64); |
| 1398 | assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_dec*)type)->range->parts[0].max_64); |
| 1399 | |
| 1400 | assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;typedef mytype {type decimal64 {" |
| 1401 | "fraction-digits 2;range '3.14 | 5.1 | 10';}}leaf l {type mytype;}}", LYS_IN_YANG)); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1402 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1403 | assert_non_null(type); |
| 1404 | assert_int_equal(LY_TYPE_DEC64, type->basetype); |
| 1405 | assert_int_equal(2, ((struct lysc_type_dec*)type)->fraction_digits); |
| 1406 | assert_non_null(((struct lysc_type_dec*)type)->range); |
| 1407 | assert_non_null(((struct lysc_type_dec*)type)->range->parts); |
| 1408 | assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_dec*)type)->range->parts)); |
| 1409 | assert_int_equal(314, ((struct lysc_type_dec*)type)->range->parts[0].min_64); |
| 1410 | assert_int_equal(314, ((struct lysc_type_dec*)type)->range->parts[0].max_64); |
| 1411 | assert_int_equal(510, ((struct lysc_type_dec*)type)->range->parts[1].min_64); |
| 1412 | assert_int_equal(510, ((struct lysc_type_dec*)type)->range->parts[1].max_64); |
| 1413 | assert_int_equal(1000, ((struct lysc_type_dec*)type)->range->parts[2].min_64); |
| 1414 | assert_int_equal(1000, ((struct lysc_type_dec*)type)->range->parts[2].max_64); |
| 1415 | |
| 1416 | /* invalid cases */ |
| 1417 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64 {fraction-digits 0;}}}", LYS_IN_YANG)); |
| 1418 | logbuf_assert("Invalid value \"0\" of \"fraction-digits\". Line number 1."); |
| 1419 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64 {fraction-digits -1;}}}", LYS_IN_YANG)); |
| 1420 | logbuf_assert("Invalid value \"-1\" of \"fraction-digits\". Line number 1."); |
| 1421 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64 {fraction-digits 19;}}}", LYS_IN_YANG)); |
| 1422 | logbuf_assert("Value \"19\" is out of \"fraction-digits\" bounds. Line number 1."); |
| 1423 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1424 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64;}}", LYS_IN_YANG)); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1425 | logbuf_assert("Missing fraction-digits substatement for decimal64 type."); |
| 1426 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1427 | assert_null(lys_parse_mem(ctx, "module ab {namespace urn:ab;prefix ab; typedef mytype {type decimal64;}leaf l {type mytype;}}", LYS_IN_YANG)); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1428 | logbuf_assert("Missing fraction-digits substatement for decimal64 type mytype."); |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 1429 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1430 | assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; leaf l {type decimal64 {fraction-digits 2;" |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1431 | "range '3.142';}}}", LYS_IN_YANG)); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1432 | logbuf_assert("Range boundary \"3.142\" of decimal64 type exceeds defined number (2) of fraction digits."); |
| 1433 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1434 | assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc; leaf l {type decimal64 {fraction-digits 2;" |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1435 | "range '4 | 3.14';}}}", LYS_IN_YANG)); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1436 | logbuf_assert("Invalid range restriction - values are not in ascending order (3.14)."); |
| 1437 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1438 | assert_null(lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd; typedef mytype {type decimal64 {fraction-digits 2;}}" |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 1439 | "leaf l {type mytype {fraction-digits 3;}}}", LYS_IN_YANG)); |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 1440 | logbuf_assert("Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type."); |
| 1441 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1442 | assert_null(lys_parse_mem(ctx, "module de {namespace urn:de;prefix de; typedef mytype {type decimal64 {fraction-digits 2;}}" |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 1443 | "typedef mytype2 {type mytype {fraction-digits 3;}}leaf l {type mytype2;}}", LYS_IN_YANG)); |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 1444 | logbuf_assert("Invalid fraction-digits substatement for type \"mytype2\" not directly derived from decimal64 built-in type."); |
| 1445 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1446 | *state = NULL; |
| 1447 | ly_ctx_destroy(ctx, NULL); |
| 1448 | } |
| 1449 | |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 1450 | static void |
| 1451 | test_type_instanceid(void **state) |
| 1452 | { |
| 1453 | *state = test_type_instanceid; |
| 1454 | |
| 1455 | struct ly_ctx *ctx; |
| 1456 | struct lys_module *mod; |
| 1457 | struct lysc_type *type; |
| 1458 | |
| 1459 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 1460 | |
| 1461 | assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;typedef mytype {type instance-identifier {require-instance false;}}" |
| 1462 | "leaf l1 {type instance-identifier {require-instance true;}}" |
| 1463 | "leaf l2 {type mytype;} leaf l3 {type instance-identifier;}}", LYS_IN_YANG)); |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 1464 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1465 | assert_non_null(type); |
| 1466 | assert_int_equal(LY_TYPE_INST, type->basetype); |
| 1467 | assert_int_equal(1, ((struct lysc_type_instanceid*)type)->require_instance); |
| 1468 | |
| 1469 | type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type; |
| 1470 | assert_non_null(type); |
| 1471 | assert_int_equal(LY_TYPE_INST, type->basetype); |
| 1472 | assert_int_equal(0, ((struct lysc_type_instanceid*)type)->require_instance); |
| 1473 | |
| 1474 | type = ((struct lysc_node_leaf*)mod->compiled->data->next->next)->type; |
| 1475 | assert_non_null(type); |
| 1476 | assert_int_equal(LY_TYPE_INST, type->basetype); |
| 1477 | assert_int_equal(1, ((struct lysc_type_instanceid*)type)->require_instance); |
| 1478 | |
| 1479 | /* invalid cases */ |
| 1480 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type instance-identifier {require-instance yes;}}}", LYS_IN_YANG)); |
| 1481 | logbuf_assert("Invalid value \"yes\" of \"require-instance\". Line number 1."); |
| 1482 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1483 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type instance-identifier {fraction-digits 1;}}}", LYS_IN_YANG)); |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 1484 | logbuf_assert("Invalid type restrictions for instance-identifier type."); |
| 1485 | |
| 1486 | *state = NULL; |
| 1487 | ly_ctx_destroy(ctx, NULL); |
| 1488 | } |
| 1489 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1490 | static void |
| 1491 | test_type_identityref(void **state) |
| 1492 | { |
| 1493 | *state = test_type_identityref; |
| 1494 | |
| 1495 | struct ly_ctx *ctx; |
| 1496 | struct lys_module *mod; |
| 1497 | struct lysc_type *type; |
| 1498 | |
| 1499 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 1500 | |
| 1501 | assert_non_null(mod = lys_parse_mem(ctx, "module a {yang-version 1.1;namespace urn:a;prefix a;identity i; identity j; identity k {base i;}" |
| 1502 | "typedef mytype {type identityref {base i;}}" |
Radek Krejci | b83ea3e | 2018-11-23 14:29:13 +0100 | [diff] [blame] | 1503 | "leaf l1 {type mytype;} leaf l2 {type identityref {base a:k; base j;}}}", LYS_IN_YANG)); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1504 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1505 | assert_non_null(type); |
| 1506 | assert_int_equal(LY_TYPE_IDENT, type->basetype); |
| 1507 | assert_non_null(((struct lysc_type_identityref*)type)->bases); |
| 1508 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_identityref*)type)->bases)); |
| 1509 | assert_string_equal("i", ((struct lysc_type_identityref*)type)->bases[0]->name); |
| 1510 | |
| 1511 | type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type; |
| 1512 | assert_non_null(type); |
| 1513 | assert_int_equal(LY_TYPE_IDENT, type->basetype); |
| 1514 | assert_non_null(((struct lysc_type_identityref*)type)->bases); |
| 1515 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_identityref*)type)->bases)); |
| 1516 | assert_string_equal("k", ((struct lysc_type_identityref*)type)->bases[0]->name); |
| 1517 | assert_string_equal("j", ((struct lysc_type_identityref*)type)->bases[1]->name); |
| 1518 | |
Radek Krejci | b83ea3e | 2018-11-23 14:29:13 +0100 | [diff] [blame] | 1519 | assert_non_null(mod = lys_parse_mem(ctx, "module b {yang-version 1.1;namespace urn:b;prefix b;import a {prefix a;}" |
| 1520 | "leaf l {type identityref {base a:k; base a:j;}}}", LYS_IN_YANG)); |
Radek Krejci | b83ea3e | 2018-11-23 14:29:13 +0100 | [diff] [blame] | 1521 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1522 | assert_non_null(type); |
| 1523 | assert_int_equal(LY_TYPE_IDENT, type->basetype); |
| 1524 | assert_non_null(((struct lysc_type_identityref*)type)->bases); |
| 1525 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_identityref*)type)->bases)); |
| 1526 | assert_string_equal("k", ((struct lysc_type_identityref*)type)->bases[0]->name); |
| 1527 | assert_string_equal("j", ((struct lysc_type_identityref*)type)->bases[1]->name); |
| 1528 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1529 | /* invalid cases */ |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1530 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type identityref;}}", LYS_IN_YANG)); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1531 | logbuf_assert("Missing base substatement for identityref type."); |
| 1532 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1533 | assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; typedef mytype {type identityref;}" |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1534 | "leaf l {type mytype;}}", LYS_IN_YANG)); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1535 | logbuf_assert("Missing base substatement for identityref type mytype."); |
| 1536 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1537 | assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc; identity i; typedef mytype {type identityref {base i;}}" |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1538 | "leaf l {type mytype {base i;}}}", LYS_IN_YANG)); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 1539 | logbuf_assert("Invalid base substatement for the type not directly derived from identityref built-in type."); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1540 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1541 | assert_null(lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd; identity i; typedef mytype {type identityref {base i;}}" |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1542 | "typedef mytype2 {type mytype {base i;}}leaf l {type mytype2;}}", LYS_IN_YANG)); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 1543 | logbuf_assert("Invalid base substatement for the type \"mytype2\" not directly derived from identityref built-in type."); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1544 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1545 | assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee; identity i; identity j;" |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1546 | "leaf l {type identityref {base i;base j;}}}", LYS_IN_YANG)); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1547 | logbuf_assert("Multiple bases in identityref type are allowed only in YANG 1.1 modules."); |
| 1548 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1549 | assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff; identity i;leaf l {type identityref {base j;}}}", LYS_IN_YANG)); |
Radek Krejci | 322614f | 2018-11-16 15:05:44 +0100 | [diff] [blame] | 1550 | logbuf_assert("Unable to find base (j) of identityref."); |
| 1551 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1552 | assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;leaf l {type identityref {base x:j;}}}", LYS_IN_YANG)); |
Radek Krejci | 322614f | 2018-11-16 15:05:44 +0100 | [diff] [blame] | 1553 | logbuf_assert("Invalid prefix used for base (x:j) of identityref."); |
| 1554 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1555 | *state = NULL; |
| 1556 | ly_ctx_destroy(ctx, NULL); |
| 1557 | } |
| 1558 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1559 | static void |
| 1560 | test_type_leafref(void **state) |
| 1561 | { |
| 1562 | *state = test_type_leafref; |
| 1563 | |
| 1564 | struct ly_ctx *ctx; |
| 1565 | struct lys_module *mod; |
| 1566 | struct lysc_type *type; |
| 1567 | const char *path, *name, *prefix; |
| 1568 | size_t prefix_len, name_len; |
| 1569 | int parent_times, has_predicate; |
| 1570 | |
| 1571 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 1572 | |
| 1573 | /* lys_path_token() */ |
| 1574 | path = "invalid_path"; |
| 1575 | parent_times = 0; |
| 1576 | assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)); |
| 1577 | path = ".."; |
| 1578 | parent_times = 0; |
| 1579 | assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)); |
| 1580 | path = "..["; |
| 1581 | parent_times = 0; |
| 1582 | assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)); |
| 1583 | path = "../"; |
| 1584 | parent_times = 0; |
| 1585 | assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)); |
| 1586 | path = "/"; |
| 1587 | parent_times = 0; |
| 1588 | assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)); |
| 1589 | |
| 1590 | path = "../../pref:id/xxx[predicate]/invalid!!!"; |
| 1591 | parent_times = 0; |
| 1592 | assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)); |
| 1593 | assert_string_equal("/xxx[predicate]/invalid!!!", path); |
| 1594 | assert_int_equal(4, prefix_len); |
| 1595 | assert_int_equal(0, strncmp("pref", prefix, prefix_len)); |
| 1596 | assert_int_equal(2, name_len); |
| 1597 | assert_int_equal(0, strncmp("id", name, name_len)); |
| 1598 | assert_int_equal(2, parent_times); |
| 1599 | assert_int_equal(0, has_predicate); |
| 1600 | assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)); |
| 1601 | assert_string_equal("[predicate]/invalid!!!", path); |
| 1602 | assert_int_equal(0, prefix_len); |
| 1603 | assert_null(prefix); |
| 1604 | assert_int_equal(3, name_len); |
| 1605 | assert_int_equal(0, strncmp("xxx", name, name_len)); |
| 1606 | assert_int_equal(1, has_predicate); |
| 1607 | path += 11; |
| 1608 | assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)); |
| 1609 | assert_string_equal("!!!", path); |
| 1610 | assert_int_equal(0, prefix_len); |
| 1611 | assert_null(prefix); |
| 1612 | assert_int_equal(7, name_len); |
| 1613 | assert_int_equal(0, strncmp("invalid", name, name_len)); |
| 1614 | |
| 1615 | path = "/absolute/prefix:path"; |
| 1616 | parent_times = 0; |
| 1617 | assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)); |
| 1618 | assert_string_equal("/prefix:path", path); |
| 1619 | assert_int_equal(0, prefix_len); |
| 1620 | assert_null(prefix); |
| 1621 | assert_int_equal(8, name_len); |
| 1622 | assert_int_equal(0, strncmp("absolute", name, name_len)); |
| 1623 | assert_int_equal(-1, parent_times); |
| 1624 | assert_int_equal(0, has_predicate); |
| 1625 | assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)); |
| 1626 | assert_int_equal(0, *path); |
| 1627 | assert_int_equal(6, prefix_len); |
| 1628 | assert_int_equal(0, strncmp("prefix", prefix, prefix_len)); |
| 1629 | assert_int_equal(4, name_len); |
| 1630 | assert_int_equal(0, strncmp("path", name, name_len)); |
| 1631 | assert_int_equal(0, has_predicate); |
| 1632 | |
| 1633 | /* complete leafref paths */ |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 1634 | assert_non_null(mod = lys_parse_mem(ctx, "module a {yang-version 1.1;namespace urn:a;prefix a;" |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1635 | "leaf ref1 {type leafref {path /a:target1;}} leaf ref2 {type leafref {path /a/target2; require-instance false;}}" |
| 1636 | "leaf target1 {type string;}container a {leaf target2 {type uint8;}}}", LYS_IN_YANG)); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1637 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1638 | assert_non_null(type); |
| 1639 | assert_int_equal(LY_TYPE_LEAFREF, type->basetype); |
| 1640 | assert_string_equal("/a:target1", ((struct lysc_type_leafref*)type)->path); |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 1641 | assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context); |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 1642 | assert_non_null(((struct lysc_type_leafref*)type)->realtype); |
| 1643 | assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1644 | assert_int_equal(1, ((struct lysc_type_leafref*)type)->require_instance); |
| 1645 | type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type; |
| 1646 | assert_non_null(type); |
| 1647 | assert_int_equal(LY_TYPE_LEAFREF, type->basetype); |
| 1648 | assert_string_equal("/a/target2", ((struct lysc_type_leafref*)type)->path); |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 1649 | assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context); |
| 1650 | assert_non_null(((struct lysc_type_leafref*)type)->realtype); |
| 1651 | assert_int_equal(LY_TYPE_UINT8, ((struct lysc_type_leafref*)type)->realtype->basetype); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1652 | assert_int_equal(0, ((struct lysc_type_leafref*)type)->require_instance); |
| 1653 | |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 1654 | assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b; typedef mytype {type leafref {path /b:target;}}" |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 1655 | "typedef mytype2 {type mytype;} typedef mytype3 {type leafref {path /target;}} leaf ref {type mytype2;}" |
Radek Krejci | 6be5ff1 | 2018-11-26 13:18:15 +0100 | [diff] [blame] | 1656 | "leaf target {type leafref {path ../realtarget;}} leaf realtarget {type string;}}", LYS_IN_YANG)); |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 1657 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1658 | assert_non_null(type); |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 1659 | assert_int_equal(1, type->refcount); |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 1660 | assert_int_equal(LY_TYPE_LEAFREF, type->basetype); |
| 1661 | assert_string_equal("/b:target", ((struct lysc_type_leafref* )type)->path); |
| 1662 | assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context); |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 1663 | assert_non_null(((struct lysc_type_leafref*)type)->realtype); |
| 1664 | assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype); |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 1665 | assert_int_equal(1, ((struct lysc_type_leafref* )type)->require_instance); |
| 1666 | |
| 1667 | /* prefixes are reversed to check using correct context of the path! */ |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 1668 | assert_non_null(mod = lys_parse_mem(ctx, "module c {yang-version 1.1;namespace urn:c;prefix b; import b {prefix c;}" |
Radek Krejci | 2f4a029 | 2018-11-22 15:41:53 +0100 | [diff] [blame] | 1669 | "typedef mytype3 {type c:mytype {require-instance false;}}" |
| 1670 | "leaf ref1 {type b:mytype3;}leaf ref2 {type c:mytype2;}}", LYS_IN_YANG)); |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 1671 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1672 | assert_non_null(type); |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 1673 | assert_int_equal(1, type->refcount); |
Radek Krejci | 2f4a029 | 2018-11-22 15:41:53 +0100 | [diff] [blame] | 1674 | assert_int_equal(LY_TYPE_LEAFREF, type->basetype); |
| 1675 | assert_string_equal("/b:target", ((struct lysc_type_leafref* )type)->path); |
| 1676 | assert_ptr_not_equal(mod, ((struct lysc_type_leafref*)type)->path_context); |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 1677 | assert_non_null(((struct lysc_type_leafref*)type)->realtype); |
| 1678 | assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype); |
Radek Krejci | 2f4a029 | 2018-11-22 15:41:53 +0100 | [diff] [blame] | 1679 | assert_int_equal(0, ((struct lysc_type_leafref* )type)->require_instance); |
| 1680 | type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type; |
| 1681 | assert_non_null(type); |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 1682 | assert_int_equal(1, type->refcount); |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 1683 | assert_int_equal(LY_TYPE_LEAFREF, type->basetype); |
| 1684 | assert_string_equal("/b:target", ((struct lysc_type_leafref* )type)->path); |
| 1685 | assert_ptr_not_equal(mod, ((struct lysc_type_leafref*)type)->path_context); |
| 1686 | assert_int_equal(1, ((struct lysc_type_leafref* )type)->require_instance); |
| 1687 | |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 1688 | /* non-prefixed nodes in path are supposed to be from the module where the leafref type is instantiated */ |
| 1689 | assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d; import b {prefix b;}" |
| 1690 | "leaf ref {type b:mytype3;}leaf target {type int8;}}", LYS_IN_YANG)); |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 1691 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1692 | assert_non_null(type); |
| 1693 | assert_int_equal(1, type->refcount); |
| 1694 | assert_int_equal(LY_TYPE_LEAFREF, type->basetype); |
| 1695 | assert_string_equal("/target", ((struct lysc_type_leafref* )type)->path); |
| 1696 | assert_ptr_not_equal(mod, ((struct lysc_type_leafref*)type)->path_context); |
| 1697 | assert_non_null(((struct lysc_type_leafref*)type)->realtype); |
| 1698 | assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)type)->realtype->basetype); |
| 1699 | assert_int_equal(1, ((struct lysc_type_leafref* )type)->require_instance); |
| 1700 | |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 1701 | /* conditional leafrefs */ |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 1702 | assert_non_null(mod = lys_parse_mem(ctx, "module e {yang-version 1.1;namespace urn:e;prefix e;feature f1; feature f2;" |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 1703 | "leaf ref1 {if-feature 'f1 and f2';type leafref {path /target;}}" |
| 1704 | "leaf target {if-feature f1; type boolean;}}", LYS_IN_YANG)); |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 1705 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1706 | assert_non_null(type); |
| 1707 | assert_int_equal(1, type->refcount); |
| 1708 | assert_int_equal(LY_TYPE_LEAFREF, type->basetype); |
| 1709 | assert_string_equal("/target", ((struct lysc_type_leafref* )type)->path); |
| 1710 | assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context); |
| 1711 | assert_non_null(((struct lysc_type_leafref*)type)->realtype); |
| 1712 | assert_int_equal(LY_TYPE_BOOL, ((struct lysc_type_leafref*)type)->realtype->basetype); |
| 1713 | |
Radek Krejci | 7adf4ff | 2018-12-05 08:55:00 +0100 | [diff] [blame] | 1714 | assert_non_null(mod = lys_parse_mem(ctx, "module f {namespace urn:f;prefix f;" |
| 1715 | "list interface{key name;leaf name{type string;}list address {key ip;leaf ip {type string;}}}" |
| 1716 | "container default-address{leaf ifname{type leafref{ path \"../../interface/name\";}}" |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 1717 | "leaf address {type leafref{ path \"../../interface[ name = current()/../ifname ]/address/ip\";}}}}", |
Radek Krejci | 7adf4ff | 2018-12-05 08:55:00 +0100 | [diff] [blame] | 1718 | LYS_IN_YANG)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 1719 | type = ((struct lysc_node_leaf*)(*lysc_node_children_p(mod->compiled->data->prev))->prev)->type; |
Radek Krejci | 7adf4ff | 2018-12-05 08:55:00 +0100 | [diff] [blame] | 1720 | assert_non_null(type); |
| 1721 | assert_int_equal(1, type->refcount); |
| 1722 | assert_int_equal(LY_TYPE_LEAFREF, type->basetype); |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 1723 | assert_string_equal("../../interface[ name = current()/../ifname ]/address/ip", ((struct lysc_type_leafref* )type)->path); |
Radek Krejci | 7adf4ff | 2018-12-05 08:55:00 +0100 | [diff] [blame] | 1724 | assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context); |
| 1725 | assert_non_null(((struct lysc_type_leafref*)type)->realtype); |
| 1726 | assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1727 | |
Radek Krejci | 20e8a18 | 2018-12-07 11:43:21 +0100 | [diff] [blame] | 1728 | assert_non_null(mod = lys_parse_mem(ctx, "module g {namespace urn:g;prefix g;" |
| 1729 | "leaf source {type leafref {path \"/endpoint-parent[id=current()/../field]/endpoint/name\";}}" |
| 1730 | "leaf field {type int32;}list endpoint-parent {key id;leaf id {type int32;}" |
| 1731 | "list endpoint {key name;leaf name {type string;}}}}", LYS_IN_YANG)); |
Radek Krejci | 20e8a18 | 2018-12-07 11:43:21 +0100 | [diff] [blame] | 1732 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1733 | assert_non_null(type); |
| 1734 | assert_int_equal(1, type->refcount); |
| 1735 | assert_int_equal(LY_TYPE_LEAFREF, type->basetype); |
| 1736 | assert_string_equal("/endpoint-parent[id=current()/../field]/endpoint/name", ((struct lysc_type_leafref* )type)->path); |
| 1737 | assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context); |
| 1738 | assert_non_null(((struct lysc_type_leafref*)type)->realtype); |
| 1739 | assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype); |
| 1740 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1741 | /* invalid paths */ |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1742 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;container a {leaf target2 {type uint8;}}" |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1743 | "leaf ref1 {type leafref {path ../a/invalid;}}}", LYS_IN_YANG)); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1744 | logbuf_assert("Invalid leafref path - unable to find \"../a/invalid\"."); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1745 | assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;container a {leaf target2 {type uint8;}}" |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1746 | "leaf ref1 {type leafref {path ../../toohigh;}}}", LYS_IN_YANG)); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1747 | logbuf_assert("Invalid leafref path \"../../toohigh\" - too many \"..\" in the path."); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1748 | assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;container a {leaf target2 {type uint8;}}" |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1749 | "leaf ref1 {type leafref {path /a:invalid;}}}", LYS_IN_YANG)); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1750 | logbuf_assert("Invalid leafref path - unable to find module connected with the prefix of the node \"/a:invalid\"."); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1751 | assert_null(lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd;leaf target1 {type string;}container a {leaf target2 {type uint8;}}" |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1752 | "leaf ref1 {type leafref {path '/a[target2 = current()/../target1]/target2';}}}", LYS_IN_YANG)); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1753 | logbuf_assert("Invalid leafref path - node \"/a\" is expected to be a list, but it is container."); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1754 | assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;container a {leaf target2 {type uint8;}}" |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1755 | "leaf ref1 {type leafref {path /a!invalid;}}}", LYS_IN_YANG)); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1756 | logbuf_assert("Invalid leafref path at character 3 (/a!invalid)."); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1757 | assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;container a {leaf target2 {type uint8;}}" |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1758 | "leaf ref1 {type leafref {path /a;}}}", LYS_IN_YANG)); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1759 | logbuf_assert("Invalid leafref path \"/a\" - target node is container instead of leaf or leaf-list."); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1760 | assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;container a {leaf target2 {type uint8; status deprecated;}}" |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1761 | "leaf ref1 {type leafref {path /a/target2;}}}", LYS_IN_YANG)); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1762 | logbuf_assert("A current definition \"ref1\" is not allowed to reference deprecated definition \"target2\"."); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1763 | assert_null(lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;" |
Radek Krejci | 2f4a029 | 2018-11-22 15:41:53 +0100 | [diff] [blame] | 1764 | "leaf ref1 {type leafref;}}", LYS_IN_YANG)); |
Radek Krejci | 2f4a029 | 2018-11-22 15:41:53 +0100 | [diff] [blame] | 1765 | logbuf_assert("Missing path substatement for leafref type."); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1766 | assert_null(lys_parse_mem(ctx, "module ii {namespace urn:ii;prefix ii;typedef mytype {type leafref;}" |
Radek Krejci | 2f4a029 | 2018-11-22 15:41:53 +0100 | [diff] [blame] | 1767 | "leaf ref1 {type mytype;}}", LYS_IN_YANG)); |
Radek Krejci | 2f4a029 | 2018-11-22 15:41:53 +0100 | [diff] [blame] | 1768 | logbuf_assert("Missing path substatement for leafref type mytype."); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1769 | assert_null(lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;feature f;" |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 1770 | "leaf ref {type leafref {path /target;}}leaf target {if-feature f;type string;}}", LYS_IN_YANG)); |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 1771 | logbuf_assert("Invalid leafref path \"/target\" - set of features applicable to the leafref target is not a subset of features " |
| 1772 | "applicable to the leafref itself."); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1773 | assert_null(lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;" |
Radek Krejci | b57cf1e | 2018-11-23 14:07:05 +0100 | [diff] [blame] | 1774 | "leaf ref {type leafref {path /target;}}leaf target {type string;config false;}}", LYS_IN_YANG)); |
Radek Krejci | b57cf1e | 2018-11-23 14:07:05 +0100 | [diff] [blame] | 1775 | logbuf_assert("Invalid leafref path \"/target\" - target is supposed to represent configuration data (as the leafref does), but it does not."); |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 1776 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1777 | assert_null(lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;" |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 1778 | "leaf ref {type leafref {path /target; require-instance true;}}leaf target {type string;}}", LYS_IN_YANG)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 1779 | logbuf_assert("Leafref type can be restricted by require-instance statement only in YANG 1.1 modules."); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1780 | assert_null(lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;typedef mytype {type leafref {path /target;require-instance false;}}" |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 1781 | "leaf ref {type mytype;}leaf target {type string;}}", LYS_IN_YANG)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 1782 | logbuf_assert("Leafref type \"mytype\" can be restricted by require-instance statement only in YANG 1.1 modules."); |
| 1783 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1784 | assert_null(lys_parse_mem(ctx, "module nn {namespace urn:nn;prefix nn;" |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 1785 | "list interface{key name;leaf name{type string;}leaf ip {type string;}}" |
| 1786 | "leaf ifname{type leafref{ path \"../interface/name\";}}" |
| 1787 | "leaf address {type leafref{ path \"/interface[name is current()/../ifname]/ip\";}}}", |
| 1788 | LYS_IN_YANG)); |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 1789 | logbuf_assert("Invalid leafref path predicate \"[name i\" - missing \"=\" after node-identifier."); |
| 1790 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1791 | assert_null(lys_parse_mem(ctx, "module oo {namespace urn:oo;prefix oo;" |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 1792 | "list interface{key name;leaf name{type string;}leaf ip {type string;}}" |
| 1793 | "leaf ifname{type leafref{ path \"../interface/name\";}}" |
| 1794 | "leaf address {type leafref{ path \"/interface[name=current()/../ifname/ip\";}}}", |
| 1795 | LYS_IN_YANG)); |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 1796 | logbuf_assert("Invalid leafref path predicate \"[name=current()/../ifname/ip\" - missing predicate termination."); |
| 1797 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1798 | assert_null(lys_parse_mem(ctx, "module pp {namespace urn:pp;prefix pp;" |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 1799 | "list interface{key name;leaf name{type string;}leaf ip {type string;}}" |
| 1800 | "leaf ifname{type leafref{ path \"../interface/name\";}}" |
| 1801 | "leaf address {type leafref{ path \"/interface[x:name=current()/../ifname]/ip\";}}}", |
| 1802 | LYS_IN_YANG)); |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 1803 | logbuf_assert("Invalid leafref path predicate \"[x:name=current()/../ifname]\" - prefix \"x\" not defined in module \"pp\"."); |
| 1804 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1805 | assert_null(lys_parse_mem(ctx, "module qq {namespace urn:qq;prefix qq;" |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 1806 | "list interface{key name;leaf name{type string;}leaf ip {type string;}}" |
| 1807 | "leaf ifname{type leafref{ path \"../interface/name\";}}" |
| 1808 | "leaf address {type leafref{ path \"/interface[id=current()/../ifname]/ip\";}}}", |
| 1809 | LYS_IN_YANG)); |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 1810 | logbuf_assert("Invalid leafref path predicate \"[id=current()/../ifname]\" - predicate's key node \"id\" not found."); |
| 1811 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1812 | assert_null(lys_parse_mem(ctx, "module rr {namespace urn:rr;prefix rr;" |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 1813 | "list interface{key name;leaf name{type string;}leaf ip {type string;}}" |
| 1814 | "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}" |
| 1815 | "leaf address {type leafref{ path \"/interface[name=current() / .. / ifname][name=current()/../test]/ip\";}}}", |
| 1816 | LYS_IN_YANG)); |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 1817 | logbuf_assert("Invalid leafref path predicate \"[name=current()/../test]\" - multiple equality tests for the key \"name\"."); |
| 1818 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1819 | assert_null(lys_parse_mem(ctx, "module ss {namespace urn:ss;prefix ss;" |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 1820 | "list interface{key name;leaf name{type string;}leaf ip {type string;}}" |
| 1821 | "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}" |
| 1822 | "leaf address {type leafref{ path \"/interface[name = ../ifname]/ip\";}}}", |
| 1823 | LYS_IN_YANG)); |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 1824 | logbuf_assert("Invalid leafref path predicate \"[name = ../ifname]\" - missing current-function-invocation."); |
| 1825 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1826 | assert_null(lys_parse_mem(ctx, "module tt {namespace urn:tt;prefix tt;" |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 1827 | "list interface{key name;leaf name{type string;}leaf ip {type string;}}" |
| 1828 | "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}" |
| 1829 | "leaf address {type leafref{ path \"/interface[name = current()../ifname]/ip\";}}}", |
| 1830 | LYS_IN_YANG)); |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 1831 | logbuf_assert("Invalid leafref path predicate \"[name = current()../ifname]\" - missing \"/\" after current-function-invocation."); |
| 1832 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1833 | assert_null(lys_parse_mem(ctx, "module uu {namespace urn:uu;prefix uu;" |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 1834 | "list interface{key name;leaf name{type string;}leaf ip {type string;}}" |
| 1835 | "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}" |
| 1836 | "leaf address {type leafref{ path \"/interface[name = current()/..ifname]/ip\";}}}", |
| 1837 | LYS_IN_YANG)); |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 1838 | logbuf_assert("Invalid leafref path predicate \"[name = current()/..ifname]\" - missing \"/\" in \"../\" rel-path-keyexpr pattern."); |
| 1839 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1840 | assert_null(lys_parse_mem(ctx, "module vv {namespace urn:vv;prefix vv;" |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 1841 | "list interface{key name;leaf name{type string;}leaf ip {type string;}}" |
| 1842 | "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}" |
| 1843 | "leaf address {type leafref{ path \"/interface[name = current()/ifname]/ip\";}}}", |
| 1844 | LYS_IN_YANG)); |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 1845 | logbuf_assert("Invalid leafref path predicate \"[name = current()/ifname]\" - at least one \"..\" is expected in rel-path-keyexpr."); |
| 1846 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1847 | assert_null(lys_parse_mem(ctx, "module ww {namespace urn:ww;prefix ww;" |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 1848 | "list interface{key name;leaf name{type string;}leaf ip {type string;}}" |
| 1849 | "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}" |
| 1850 | "leaf address {type leafref{ path \"/interface[name = current()/../]/ip\";}}}", |
| 1851 | LYS_IN_YANG)); |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 1852 | logbuf_assert("Invalid leafref path predicate \"[name = current()/../]\" - at least one node-identifier is expected in rel-path-keyexpr."); |
| 1853 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1854 | assert_null(lys_parse_mem(ctx, "module xx {namespace urn:xx;prefix xx;" |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 1855 | "list interface{key name;leaf name{type string;}leaf ip {type string;}}" |
| 1856 | "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}" |
| 1857 | "leaf address {type leafref{ path \"/interface[name = current()/../$node]/ip\";}}}", |
| 1858 | LYS_IN_YANG)); |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 1859 | logbuf_assert("Invalid node identifier in leafref path predicate - character 22 (of [name = current()/../$node])."); |
| 1860 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1861 | assert_null(lys_parse_mem(ctx, "module yy {namespace urn:yy;prefix yy;" |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 1862 | "list interface{key name;leaf name{type string;}leaf ip {type string;}}" |
| 1863 | "leaf ifname{type leafref{ path \"../interface/name\";}}" |
| 1864 | "leaf address {type leafref{ path \"/interface[name=current()/../x:ifname]/ip\";}}}", |
| 1865 | LYS_IN_YANG)); |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 1866 | logbuf_assert("Invalid leafref path predicate \"[name=current()/../x:ifname]\" - unable to find module of the node \"ifname\" in rel-path_keyexpr."); |
| 1867 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1868 | assert_null(lys_parse_mem(ctx, "module zz {namespace urn:zz;prefix zz;" |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 1869 | "list interface{key name;leaf name{type string;}leaf ip {type string;}}" |
| 1870 | "leaf ifname{type leafref{ path \"../interface/name\";}}" |
| 1871 | "leaf address {type leafref{ path \"/interface[name=current()/../xxx]/ip\";}}}", |
| 1872 | LYS_IN_YANG)); |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 1873 | logbuf_assert("Invalid leafref path predicate \"[name=current()/../xxx]\" - unable to find node \"current()/../xxx\" in the rel-path_keyexpr."); |
| 1874 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1875 | assert_null(lys_parse_mem(ctx, "module zza {namespace urn:zza;prefix zza;" |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 1876 | "list interface{key name;leaf name{type string;}leaf ip {type string;}}" |
| 1877 | "leaf ifname{type leafref{ path \"../interface/name\";}}container c;" |
| 1878 | "leaf address {type leafref{ path \"/interface[name=current()/../c]/ip\";}}}", |
| 1879 | LYS_IN_YANG)); |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 1880 | logbuf_assert("Invalid leafref path predicate \"[name=current()/../c]\" - rel-path_keyexpr \"current()/../c\" refers container instead of leaf."); |
| 1881 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1882 | assert_null(lys_parse_mem(ctx, "module zzb {namespace urn:zzb;prefix zzb;" |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 1883 | "list interface{key name;leaf name{type string;}leaf ip {type string;}container c;}" |
| 1884 | "leaf ifname{type leafref{ path \"../interface/name\";}}" |
| 1885 | "leaf address {type leafref{ path \"/interface[c=current()/../ifname]/ip\";}}}", |
| 1886 | LYS_IN_YANG)); |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 1887 | logbuf_assert("Invalid leafref path predicate \"[c=current()/../ifname]\" - predicate's key node \"c\" not found."); |
| 1888 | |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 1889 | /* circular chain */ |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1890 | assert_null(lys_parse_mem(ctx, "module aaa {namespace urn:aaa;prefix aaa;" |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 1891 | "leaf ref1 {type leafref {path /ref2;}}" |
| 1892 | "leaf ref2 {type leafref {path /ref3;}}" |
Radek Krejci | 0c3f1ad | 2018-11-23 14:19:38 +0100 | [diff] [blame] | 1893 | "leaf ref3 {type leafref {path /ref4;}}" |
| 1894 | "leaf ref4 {type leafref {path /ref1;}}}", LYS_IN_YANG)); |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 1895 | logbuf_assert("Invalid leafref path \"/ref1\" - circular chain of leafrefs detected."); |
| 1896 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1897 | *state = NULL; |
| 1898 | ly_ctx_destroy(ctx, NULL); |
| 1899 | } |
| 1900 | |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 1901 | static void |
| 1902 | test_type_empty(void **state) |
| 1903 | { |
| 1904 | *state = test_type_empty; |
| 1905 | |
| 1906 | struct ly_ctx *ctx; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 1907 | |
| 1908 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 1909 | |
| 1910 | /* invalid */ |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1911 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;" |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 1912 | "leaf l {type empty; default x;}}", LYS_IN_YANG)); |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 1913 | logbuf_assert("Leaf of type \"empty\" must not have a default value (x)."); |
| 1914 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1915 | assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;typedef mytype {type empty; default x;}" |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 1916 | "leaf l {type mytype;}}", LYS_IN_YANG)); |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 1917 | logbuf_assert("Invalid type \"mytype\" - \"empty\" type must not have a default value (x)."); |
| 1918 | |
| 1919 | *state = NULL; |
| 1920 | ly_ctx_destroy(ctx, NULL); |
| 1921 | } |
| 1922 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 1923 | |
| 1924 | static void |
| 1925 | test_type_union(void **state) |
| 1926 | { |
| 1927 | *state = test_type_union; |
| 1928 | |
| 1929 | struct ly_ctx *ctx; |
| 1930 | struct lys_module *mod; |
| 1931 | struct lysc_type *type; |
| 1932 | |
| 1933 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 1934 | |
| 1935 | assert_non_null(mod = lys_parse_mem(ctx, "module a {yang-version 1.1;namespace urn:a;prefix a; typedef mybasetype {type string;}" |
| 1936 | "typedef mytype {type union {type int8; type mybasetype;}}" |
| 1937 | "leaf l {type mytype;}}", LYS_IN_YANG)); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 1938 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1939 | assert_non_null(type); |
| 1940 | assert_int_equal(2, type->refcount); |
| 1941 | assert_int_equal(LY_TYPE_UNION, type->basetype); |
| 1942 | assert_non_null(((struct lysc_type_union*)type)->types); |
| 1943 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types)); |
| 1944 | assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_union*)type)->types[0]->basetype); |
| 1945 | assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[1]->basetype); |
| 1946 | |
| 1947 | assert_non_null(mod = lys_parse_mem(ctx, "module b {yang-version 1.1;namespace urn:b;prefix b; typedef mybasetype {type string;}" |
| 1948 | "typedef mytype {type union {type int8; type mybasetype;}}" |
| 1949 | "leaf l {type union {type decimal64 {fraction-digits 2;} type mytype;}}}", LYS_IN_YANG)); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 1950 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1951 | assert_non_null(type); |
| 1952 | assert_int_equal(1, type->refcount); |
| 1953 | assert_int_equal(LY_TYPE_UNION, type->basetype); |
| 1954 | assert_non_null(((struct lysc_type_union*)type)->types); |
| 1955 | assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types)); |
| 1956 | assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype); |
| 1957 | assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_union*)type)->types[1]->basetype); |
| 1958 | assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[2]->basetype); |
| 1959 | |
| 1960 | assert_non_null(mod = lys_parse_mem(ctx, "module c {yang-version 1.1;namespace urn:c;prefix c; typedef mybasetype {type string;}" |
| 1961 | "typedef mytype {type union {type leafref {path ../target;} type mybasetype;}}" |
Radek Krejci | 6be5ff1 | 2018-11-26 13:18:15 +0100 | [diff] [blame] | 1962 | "leaf l {type union {type decimal64 {fraction-digits 2;} type mytype;}}" |
| 1963 | "leaf target {type leafref {path ../realtarget;}} leaf realtarget {type int8;}}", |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 1964 | LYS_IN_YANG)); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 1965 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1966 | assert_non_null(type); |
| 1967 | assert_int_equal(1, type->refcount); |
| 1968 | assert_int_equal(LY_TYPE_UNION, type->basetype); |
| 1969 | assert_non_null(((struct lysc_type_union*)type)->types); |
| 1970 | assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types)); |
| 1971 | assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype); |
| 1972 | assert_int_equal(LY_TYPE_LEAFREF, ((struct lysc_type_union*)type)->types[1]->basetype); |
| 1973 | assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[2]->basetype); |
| 1974 | assert_non_null(((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype); |
| 1975 | assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype->basetype); |
| 1976 | |
Radek Krejci | 6be5ff1 | 2018-11-26 13:18:15 +0100 | [diff] [blame] | 1977 | /* invalid unions */ |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1978 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;typedef mytype {type union;}" |
Radek Krejci | 6be5ff1 | 2018-11-26 13:18:15 +0100 | [diff] [blame] | 1979 | "leaf l {type mytype;}}", LYS_IN_YANG)); |
Radek Krejci | 6be5ff1 | 2018-11-26 13:18:15 +0100 | [diff] [blame] | 1980 | logbuf_assert("Missing type substatement for union type mytype."); |
| 1981 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1982 | assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;leaf l {type union;}}", LYS_IN_YANG)); |
| 1983 | logbuf_assert("Missing type substatement for union type."); |
Radek Krejci | 6be5ff1 | 2018-11-26 13:18:15 +0100 | [diff] [blame] | 1984 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1985 | assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;typedef mytype {type union{type int8; type string;}}" |
Radek Krejci | 6be5ff1 | 2018-11-26 13:18:15 +0100 | [diff] [blame] | 1986 | "leaf l {type mytype {type string;}}}", LYS_IN_YANG)); |
Radek Krejci | 6be5ff1 | 2018-11-26 13:18:15 +0100 | [diff] [blame] | 1987 | logbuf_assert("Invalid type substatement for the type not directly derived from union built-in type."); |
| 1988 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1989 | assert_null(lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd;typedef mytype {type union{type int8; type string;}}" |
Radek Krejci | 6be5ff1 | 2018-11-26 13:18:15 +0100 | [diff] [blame] | 1990 | "typedef mytype2 {type mytype {type string;}}leaf l {type mytype2;}}", LYS_IN_YANG)); |
Radek Krejci | 6be5ff1 | 2018-11-26 13:18:15 +0100 | [diff] [blame] | 1991 | logbuf_assert("Invalid type substatement for the type \"mytype2\" not directly derived from union built-in type."); |
| 1992 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 1993 | *state = NULL; |
| 1994 | ly_ctx_destroy(ctx, NULL); |
| 1995 | } |
| 1996 | |
| 1997 | static void |
| 1998 | test_type_dflt(void **state) |
| 1999 | { |
| 2000 | *state = test_type_union; |
| 2001 | |
| 2002 | struct ly_ctx *ctx; |
| 2003 | struct lys_module *mod; |
| 2004 | struct lysc_type *type; |
| 2005 | |
| 2006 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 2007 | |
| 2008 | /* default is not inherited from union's types */ |
| 2009 | assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a; typedef mybasetype {type string;default hello;units xxx;}" |
| 2010 | "leaf l {type union {type decimal64 {fraction-digits 2;} type mybasetype;}}}", LYS_IN_YANG)); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2011 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 2012 | assert_non_null(type); |
| 2013 | assert_int_equal(1, type->refcount); |
| 2014 | assert_int_equal(LY_TYPE_UNION, type->basetype); |
| 2015 | assert_non_null(((struct lysc_type_union*)type)->types); |
| 2016 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types)); |
| 2017 | assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype); |
| 2018 | assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[1]->basetype); |
| 2019 | assert_null(((struct lysc_node_leaf*)mod->compiled->data)->dflt); |
| 2020 | assert_null(((struct lysc_node_leaf*)mod->compiled->data)->units); |
| 2021 | |
| 2022 | assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b; typedef mybasetype {type string;default hello;units xxx;}" |
| 2023 | "leaf l {type mybasetype;}}", LYS_IN_YANG)); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2024 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 2025 | assert_non_null(type); |
| 2026 | assert_int_equal(2, type->refcount); |
| 2027 | assert_int_equal(LY_TYPE_STRING, type->basetype); |
| 2028 | assert_string_equal("hello", ((struct lysc_node_leaf*)mod->compiled->data)->dflt); |
| 2029 | assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data)->units); |
| 2030 | |
| 2031 | assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c; typedef mybasetype {type string;default hello;units xxx;}" |
| 2032 | "leaf l {type mybasetype; default goodbye;units yyy;}}", LYS_IN_YANG)); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2033 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 2034 | assert_non_null(type); |
| 2035 | assert_int_equal(2, type->refcount); |
| 2036 | assert_int_equal(LY_TYPE_STRING, type->basetype); |
| 2037 | assert_string_equal("goodbye", ((struct lysc_node_leaf*)mod->compiled->data)->dflt); |
| 2038 | assert_string_equal("yyy", ((struct lysc_node_leaf*)mod->compiled->data)->units); |
| 2039 | |
Radek Krejci | 6be5ff1 | 2018-11-26 13:18:15 +0100 | [diff] [blame] | 2040 | assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d; typedef mybasetype {type string;default hello;units xxx;}" |
| 2041 | "typedef mytype {type mybasetype;}leaf l1 {type mytype; default goodbye;units yyy;}" |
| 2042 | "leaf l2 {type mytype;}}", LYS_IN_YANG)); |
Radek Krejci | 6be5ff1 | 2018-11-26 13:18:15 +0100 | [diff] [blame] | 2043 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 2044 | assert_non_null(type); |
| 2045 | assert_int_equal(4, type->refcount); |
| 2046 | assert_int_equal(LY_TYPE_STRING, type->basetype); |
| 2047 | assert_string_equal("goodbye", ((struct lysc_node_leaf*)mod->compiled->data)->dflt); |
| 2048 | assert_string_equal("yyy", ((struct lysc_node_leaf*)mod->compiled->data)->units); |
| 2049 | type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type; |
| 2050 | assert_non_null(type); |
| 2051 | assert_int_equal(4, type->refcount); |
| 2052 | assert_int_equal(LY_TYPE_STRING, type->basetype); |
| 2053 | assert_string_equal("hello", ((struct lysc_node_leaf*)mod->compiled->data->next)->dflt); |
| 2054 | assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data->next)->units); |
| 2055 | |
| 2056 | assert_non_null(mod = lys_parse_mem(ctx, "module e {namespace urn:e;prefix e; typedef mybasetype {type string;}" |
| 2057 | "typedef mytype {type mybasetype; default hello;units xxx;}leaf l {type mytype;}}", LYS_IN_YANG)); |
Radek Krejci | 6be5ff1 | 2018-11-26 13:18:15 +0100 | [diff] [blame] | 2058 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 2059 | assert_non_null(type); |
| 2060 | assert_int_equal(3, type->refcount); |
| 2061 | assert_int_equal(LY_TYPE_STRING, type->basetype); |
| 2062 | assert_string_equal("hello", ((struct lysc_node_leaf*)mod->compiled->data)->dflt); |
| 2063 | assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data)->units); |
| 2064 | |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 2065 | /* mandatory leaf does not takes default value from type */ |
| 2066 | assert_non_null(mod = lys_parse_mem(ctx, "module f {namespace urn:f;prefix f;typedef mytype {type string; default hello;units xxx;}" |
| 2067 | "leaf l {type mytype; mandatory true;}}", LYS_IN_YANG)); |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 2068 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 2069 | assert_non_null(type); |
| 2070 | assert_int_equal(LY_TYPE_STRING, type->basetype); |
| 2071 | assert_null(((struct lysc_node_leaf*)mod->compiled->data)->dflt); |
| 2072 | assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data)->units); |
| 2073 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2074 | *state = NULL; |
| 2075 | ly_ctx_destroy(ctx, NULL); |
| 2076 | } |
| 2077 | |
Radek Krejci | 665421c | 2018-12-05 08:03:39 +0100 | [diff] [blame] | 2078 | static void |
| 2079 | test_status(void **state) |
| 2080 | { |
| 2081 | *state = test_status; |
| 2082 | |
| 2083 | struct ly_ctx *ctx; |
Radek Krejci | 665421c | 2018-12-05 08:03:39 +0100 | [diff] [blame] | 2084 | |
| 2085 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 2086 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2087 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;" |
Radek Krejci | 665421c | 2018-12-05 08:03:39 +0100 | [diff] [blame] | 2088 | "container c {status deprecated; leaf l {status current; type string;}}}", LYS_IN_YANG)); |
Radek Krejci | 665421c | 2018-12-05 08:03:39 +0100 | [diff] [blame] | 2089 | logbuf_assert("A \"current\" status is in conflict with the parent's \"deprecated\" status."); |
| 2090 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2091 | assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;" |
Radek Krejci | 665421c | 2018-12-05 08:03:39 +0100 | [diff] [blame] | 2092 | "container c {status obsolete; leaf l {status current; type string;}}}", LYS_IN_YANG)); |
Radek Krejci | 665421c | 2018-12-05 08:03:39 +0100 | [diff] [blame] | 2093 | logbuf_assert("A \"current\" status is in conflict with the parent's \"obsolete\" status."); |
| 2094 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2095 | assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;" |
Radek Krejci | 665421c | 2018-12-05 08:03:39 +0100 | [diff] [blame] | 2096 | "container c {status obsolete; leaf l {status deprecated; type string;}}}", LYS_IN_YANG)); |
Radek Krejci | 665421c | 2018-12-05 08:03:39 +0100 | [diff] [blame] | 2097 | logbuf_assert("A \"deprecated\" status is in conflict with the parent's \"obsolete\" status."); |
| 2098 | |
| 2099 | *state = NULL; |
| 2100 | ly_ctx_destroy(ctx, NULL); |
| 2101 | } |
| 2102 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2103 | static void |
| 2104 | test_uses(void **state) |
| 2105 | { |
| 2106 | *state = test_uses; |
| 2107 | |
| 2108 | struct ly_ctx *ctx; |
| 2109 | struct lys_module *mod; |
| 2110 | struct lysc_node *parent, *child; |
| 2111 | |
| 2112 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 2113 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 2114 | ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module grp {namespace urn:grp;prefix g; typedef mytype {type string;} feature f;" |
| 2115 | "grouping grp {leaf x {type mytype;} leaf y {type string; if-feature f;}}}"); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2116 | assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;import grp {prefix g;}" |
| 2117 | "grouping grp_a_top {leaf a1 {type int8;}}" |
| 2118 | "container a {uses grp_a; uses grp_a_top; uses g:grp; grouping grp_a {leaf a2 {type uint8;}}}}", LYS_IN_YANG)); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2119 | assert_non_null((parent = mod->compiled->data)); |
| 2120 | assert_int_equal(LYS_CONTAINER, parent->nodetype); |
| 2121 | assert_non_null((child = ((struct lysc_node_container*)parent)->child)); |
| 2122 | assert_string_equal("a2", child->name); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 2123 | assert_ptr_equal(mod, child->module); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2124 | assert_non_null((child = child->next)); |
| 2125 | assert_string_equal("a1", child->name); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 2126 | assert_ptr_equal(mod, child->module); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2127 | assert_non_null((child = child->next)); |
| 2128 | assert_string_equal("x", child->name); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 2129 | assert_ptr_equal(mod, child->module); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 2130 | assert_non_null((child = child->next)); |
| 2131 | assert_string_equal("y", child->name); |
| 2132 | assert_non_null(child->iffeatures); |
| 2133 | assert_int_equal(1, LY_ARRAY_SIZE(child->iffeatures)); |
| 2134 | assert_int_equal(1, LY_ARRAY_SIZE(child->iffeatures[0].features)); |
| 2135 | assert_string_equal("f", child->iffeatures[0].features[0]->name); |
| 2136 | assert_int_equal(LY_EINVAL, lys_feature_enable(mod->compiled->imports[0].module, "f")); |
| 2137 | logbuf_assert("Module \"grp\" is not implemented so all its features are permanently disabled without a chance to change it."); |
| 2138 | assert_int_equal(0, lysc_iffeature_value(&child->iffeatures[0])); |
| 2139 | |
| 2140 | /* make the imported module implemented and enable the feature */ |
| 2141 | assert_non_null(mod = ly_ctx_get_module(ctx, "grp", NULL)); |
| 2142 | assert_int_equal(LY_SUCCESS, ly_ctx_module_implement(ctx, mod)); |
| 2143 | assert_int_equal(LY_SUCCESS, lys_feature_enable(mod, "f")); |
| 2144 | assert_string_equal("f", child->iffeatures[0].features[0]->name); |
| 2145 | assert_int_equal(1, lysc_iffeature_value(&child->iffeatures[0])); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2146 | |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 2147 | ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule bsub {belongs-to b {prefix b;} grouping grp {leaf b {type string;}}}"); |
| 2148 | assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;include bsub;uses grp;}", LYS_IN_YANG)); |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 2149 | assert_non_null(mod->compiled->data); |
| 2150 | assert_int_equal(LYS_LEAF, mod->compiled->data->nodetype); |
| 2151 | assert_string_equal("b", mod->compiled->data->name); |
| 2152 | |
Radek Krejci | 7f6a381 | 2019-01-07 13:48:57 +0100 | [diff] [blame] | 2153 | logbuf_clean(); |
| 2154 | assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:ii;prefix ii;" |
| 2155 | "grouping grp {leaf l {type string;}leaf k {type string; status obsolete;}}" |
| 2156 | "uses grp {status deprecated;}}", LYS_IN_YANG)); |
Radek Krejci | 7f6a381 | 2019-01-07 13:48:57 +0100 | [diff] [blame] | 2157 | assert_int_equal(LYS_LEAF, mod->compiled->data->nodetype); |
| 2158 | assert_string_equal("l", mod->compiled->data->name); |
| 2159 | assert_true(LYS_STATUS_DEPRC & mod->compiled->data->flags); |
| 2160 | assert_int_equal(LYS_LEAF, mod->compiled->data->next->nodetype); |
| 2161 | assert_string_equal("k", mod->compiled->data->next->name); |
| 2162 | assert_true(LYS_STATUS_OBSLT & mod->compiled->data->next->flags); |
| 2163 | logbuf_assert(""); /* no warning about inheriting deprecated flag from uses */ |
| 2164 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2165 | /* invalid */ |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2166 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;uses missinggrp;}", LYS_IN_YANG)); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2167 | logbuf_assert("Grouping \"missinggrp\" referenced by a uses statement not found."); |
| 2168 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2169 | assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;uses grp;" |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2170 | "grouping grp {leaf a{type string;}uses grp1;}" |
| 2171 | "grouping grp1 {leaf b {type string;}uses grp2;}" |
| 2172 | "grouping grp2 {leaf c {type string;}uses grp;}}", LYS_IN_YANG)); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2173 | logbuf_assert("Grouping \"grp\" references itself through a uses statement."); |
| 2174 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2175 | assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;uses a:missingprefix;}", LYS_IN_YANG)); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 2176 | logbuf_assert("Invalid prefix used for grouping reference (a:missingprefix)."); |
| 2177 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2178 | assert_null(lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd;grouping grp{leaf a{type string;}}" |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 2179 | "leaf a {type string;}uses grp;}", LYS_IN_YANG)); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 2180 | logbuf_assert("Duplicate identifier \"a\" of data definition statement."); |
| 2181 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2182 | assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;grouping grp {leaf l {type string; status deprecated;}}" |
Radek Krejci | 7f6a381 | 2019-01-07 13:48:57 +0100 | [diff] [blame] | 2183 | "uses grp {status obsolete;}}", LYS_IN_YANG)); |
Radek Krejci | 7f6a381 | 2019-01-07 13:48:57 +0100 | [diff] [blame] | 2184 | logbuf_assert("A \"deprecated\" status is in conflict with the parent's \"obsolete\" status."); |
| 2185 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2186 | *state = NULL; |
| 2187 | ly_ctx_destroy(ctx, NULL); |
| 2188 | } |
| 2189 | |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2190 | static void |
| 2191 | test_refine(void **state) |
| 2192 | { |
| 2193 | *state = test_refine; |
| 2194 | |
| 2195 | struct ly_ctx *ctx; |
| 2196 | struct lys_module *mod; |
| 2197 | struct lysc_node *parent, *child; |
| 2198 | |
| 2199 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 2200 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2201 | assert_non_null(lys_parse_mem(ctx, "module grp {yang-version 1.1;namespace urn:grp;prefix g; feature f;typedef mytype {type string; default cheers!;}" |
| 2202 | "grouping grp {container c {leaf l {type mytype; default goodbye;}" |
| 2203 | "leaf-list ll {type mytype; default goodbye; max-elements 6;}" |
| 2204 | "choice ch {default a; leaf a {type int8;}leaf b{type uint8;}}" |
| 2205 | "leaf x {type mytype; mandatory true; must 1;}" |
| 2206 | "anydata a {mandatory false; if-feature f; description original; reference original;}" |
| 2207 | "container c {config false; leaf l {type string;}}}}}", LYS_IN_YANG)); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2208 | |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 2209 | assert_non_null(mod = lys_parse_mem(ctx, "module a {yang-version 1.1;namespace urn:a;prefix a;import grp {prefix g;}feature fa;" |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2210 | "uses g:grp {refine c/l {default hello; config false;}" |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 2211 | "refine c/ll {default hello;default world; min-elements 2; max-elements 5;}" |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 2212 | "refine c/ch {default b;config true; if-feature fa;}" |
Radek Krejci | f340454 | 2019-01-08 13:28:42 +0100 | [diff] [blame] | 2213 | "refine c/x {mandatory false; must ../ll;description refined; reference refined;}" |
| 2214 | "refine c/a {mandatory true; must 1; if-feature fa;description refined; reference refined;}" |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 2215 | "refine c/c {config true;presence indispensable;}}}", LYS_IN_YANG)); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2216 | assert_non_null((parent = mod->compiled->data)); |
| 2217 | assert_int_equal(LYS_CONTAINER, parent->nodetype); |
| 2218 | assert_string_equal("c", parent->name); |
| 2219 | assert_non_null((child = ((struct lysc_node_container*)parent)->child)); |
| 2220 | assert_int_equal(LYS_LEAF, child->nodetype); |
| 2221 | assert_string_equal("l", child->name); |
| 2222 | assert_string_equal("hello", ((struct lysc_node_leaf*)child)->dflt); |
| 2223 | assert_int_equal(LYS_CONFIG_R, child->flags & LYS_CONFIG_MASK); |
| 2224 | assert_non_null(child = child->next); |
| 2225 | assert_int_equal(LYS_LEAFLIST, child->nodetype); |
| 2226 | assert_string_equal("ll", child->name); |
| 2227 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_node_leaflist*)child)->dflts)); |
| 2228 | assert_string_equal("hello", ((struct lysc_node_leaflist*)child)->dflts[0]); |
| 2229 | assert_string_equal("world", ((struct lysc_node_leaflist*)child)->dflts[1]); |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 2230 | assert_int_equal(2, ((struct lysc_node_leaflist*)child)->min); |
| 2231 | assert_int_equal(5, ((struct lysc_node_leaflist*)child)->max); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2232 | assert_non_null(child = child->next); |
| 2233 | assert_int_equal(LYS_CHOICE, child->nodetype); |
| 2234 | assert_string_equal("ch", child->name); |
| 2235 | assert_string_equal("b", ((struct lysc_node_choice*)child)->dflt->name); |
| 2236 | assert_true(LYS_SET_DFLT & ((struct lysc_node_choice*)child)->dflt->flags); |
| 2237 | assert_false(LYS_SET_DFLT & ((struct lysc_node_choice*)child)->cases[0].flags); |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 2238 | assert_non_null(child->iffeatures); |
| 2239 | assert_int_equal(1, LY_ARRAY_SIZE(child->iffeatures)); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2240 | assert_non_null(child = child->next); |
| 2241 | assert_int_equal(LYS_LEAF, child->nodetype); |
| 2242 | assert_string_equal("x", child->name); |
| 2243 | assert_false(LYS_MAND_TRUE & child->flags); |
| 2244 | assert_string_equal("cheers!", ((struct lysc_node_leaf*)child)->dflt); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 2245 | assert_non_null(((struct lysc_node_leaf*)child)->musts); |
| 2246 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_node_leaf*)child)->musts)); |
Radek Krejci | f340454 | 2019-01-08 13:28:42 +0100 | [diff] [blame] | 2247 | assert_string_equal("refined", child->dsc); |
| 2248 | assert_string_equal("refined", child->ref); |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 2249 | assert_non_null(child = child->next); |
Radek Krejci | 7f6a381 | 2019-01-07 13:48:57 +0100 | [diff] [blame] | 2250 | assert_int_equal(LYS_ANYDATA, child->nodetype); |
| 2251 | assert_string_equal("a", child->name); |
| 2252 | assert_true(LYS_MAND_TRUE & child->flags); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 2253 | assert_non_null(((struct lysc_node_anydata*)child)->musts); |
| 2254 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_node_anydata*)child)->musts)); |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 2255 | assert_non_null(child->iffeatures); |
| 2256 | assert_int_equal(2, LY_ARRAY_SIZE(child->iffeatures)); |
Radek Krejci | f340454 | 2019-01-08 13:28:42 +0100 | [diff] [blame] | 2257 | assert_string_equal("refined", child->dsc); |
| 2258 | assert_string_equal("refined", child->ref); |
Radek Krejci | 7f6a381 | 2019-01-07 13:48:57 +0100 | [diff] [blame] | 2259 | assert_non_null(child = child->next); |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 2260 | assert_int_equal(LYS_CONTAINER, child->nodetype); |
| 2261 | assert_string_equal("c", child->name); |
Radek Krejci | 7f6a381 | 2019-01-07 13:48:57 +0100 | [diff] [blame] | 2262 | assert_true(LYS_PRESENCE & child->flags); |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 2263 | assert_true(LYS_CONFIG_W & child->flags); |
| 2264 | assert_true(LYS_CONFIG_W & ((struct lysc_node_container*)child)->child->flags); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2265 | |
| 2266 | assert_non_null(mod = lys_parse_mem(ctx, "module b {yang-version 1.1;namespace urn:b;prefix b;import grp {prefix g;}" |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 2267 | "uses g:grp {status deprecated; refine c/x {default hello; mandatory false;}}}", LYS_IN_YANG)); |
Radek Krejci | 7f6a381 | 2019-01-07 13:48:57 +0100 | [diff] [blame] | 2268 | assert_non_null((child = ((struct lysc_node_container*)mod->compiled->data)->child->prev->prev->prev)); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2269 | assert_int_equal(LYS_LEAF, child->nodetype); |
| 2270 | assert_string_equal("x", child->name); |
| 2271 | assert_false(LYS_MAND_TRUE & child->flags); |
| 2272 | assert_string_equal("hello", ((struct lysc_node_leaf*)child)->dflt); |
| 2273 | |
| 2274 | /* invalid */ |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2275 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;import grp {prefix g;}" |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2276 | "uses g:grp {refine c {default hello;}}}", LYS_IN_YANG)); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2277 | logbuf_assert("Invalid refine of default in \"c\" - container cannot hold default value(s)."); |
| 2278 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2279 | assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;import grp {prefix g;}" |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2280 | "uses g:grp {refine c/l {default hello; default world;}}}", LYS_IN_YANG)); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2281 | logbuf_assert("Invalid refine of default in \"c/l\" - leaf cannot hold 2 default values."); |
| 2282 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2283 | assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;import grp {prefix g;}" |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2284 | "uses g:grp {refine c/ll {default hello; default world;}}}", LYS_IN_YANG)); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2285 | logbuf_assert("Invalid refine of default in leaf-list - the default statement is allowed only in YANG 1.1 modules."); |
| 2286 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2287 | assert_null(lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd;import grp {prefix g;}" |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2288 | "uses g:grp {refine c/ll {mandatory true;}}}", LYS_IN_YANG)); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2289 | logbuf_assert("Invalid refine of mandatory in \"c/ll\" - leaf-list cannot hold mandatory statement."); |
| 2290 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2291 | assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;import grp {prefix g;}" |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2292 | "uses g:grp {refine c/l {mandatory true;}}}", LYS_IN_YANG)); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2293 | logbuf_assert("Invalid refine of mandatory in \"c/l\" - leaf already has \"default\" statement."); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2294 | assert_null(lys_parse_mem(ctx, "module ef {namespace urn:ef;prefix ef;import grp {prefix g;}" |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2295 | "uses g:grp {refine c/ch {mandatory true;}}}", LYS_IN_YANG)); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2296 | logbuf_assert("Invalid refine of mandatory in \"c/ch\" - choice already has \"default\" statement."); |
| 2297 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2298 | assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;import grp {prefix g;}" |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2299 | "uses g:grp {refine c/ch/a/a {mandatory true;}}}", LYS_IN_YANG)); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2300 | logbuf_assert("Invalid refine of mandatory in \"c/ch/a/a\" - leaf under the default case."); |
| 2301 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2302 | assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;import grp {prefix g;}" |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2303 | "uses g:grp {refine c/x {default hello;}}}", LYS_IN_YANG)); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2304 | logbuf_assert("Invalid refine of default in \"c/x\" - the node is mandatory."); |
| 2305 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2306 | assert_null(lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;import grp {prefix g;}" |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 2307 | "uses g:grp {refine c/c/l {config true;}}}", LYS_IN_YANG)); |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 2308 | logbuf_assert("Invalid refine of config in \"c/c/l\" - configuration node cannot be child of any state data node."); |
| 2309 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2310 | assert_null(lys_parse_mem(ctx, "module ii {namespace urn:ii;prefix ii;grouping grp {leaf l {type string; status deprecated;}}" |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 2311 | "uses grp {status obsolete;}}", LYS_IN_YANG)); |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 2312 | logbuf_assert("A \"deprecated\" status is in conflict with the parent's \"obsolete\" status."); |
| 2313 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2314 | assert_null(lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;import grp {prefix g;}" |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 2315 | "uses g:grp {refine c/x {presence nonsence;}}}", LYS_IN_YANG)); |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 2316 | logbuf_assert("Invalid refine of presence statement in \"c/x\" - leaf cannot hold the presence statement."); |
| 2317 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2318 | assert_null(lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;import grp {prefix g;}" |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 2319 | "uses g:grp {refine c/ch {must 1;}}}", LYS_IN_YANG)); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 2320 | logbuf_assert("Invalid refine of must statement in \"c/ch\" - choice cannot hold any must statement."); |
| 2321 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2322 | assert_null(lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;import grp {prefix g;}" |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 2323 | "uses g:grp {refine c/x {min-elements 1;}}}", LYS_IN_YANG)); |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 2324 | logbuf_assert("Invalid refine of min-elements statement in \"c/x\" - leaf cannot hold this statement."); |
| 2325 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2326 | assert_null(lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;import grp {prefix g;}" |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 2327 | "uses g:grp {refine c/ll {min-elements 10;}}}", LYS_IN_YANG)); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 2328 | logbuf_assert("Invalid refine of min-elements statement in \"c/ll\" - \"min-elements\" is bigger than \"max-elements\"."); |
| 2329 | |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2330 | *state = NULL; |
| 2331 | ly_ctx_destroy(ctx, NULL); |
| 2332 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2333 | |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 2334 | int main(void) |
| 2335 | { |
| 2336 | const struct CMUnitTest tests[] = { |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 2337 | cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown), |
| 2338 | cmocka_unit_test_setup_teardown(test_feature, logger_setup, logger_teardown), |
| 2339 | cmocka_unit_test_setup_teardown(test_identity, logger_setup, logger_teardown), |
Radek Krejci | 0f07bed | 2018-11-13 14:01:40 +0100 | [diff] [blame] | 2340 | cmocka_unit_test_setup_teardown(test_type_length, logger_setup, logger_teardown), |
| 2341 | cmocka_unit_test_setup_teardown(test_type_range, logger_setup, logger_teardown), |
Radek Krejci | cda7415 | 2018-11-13 15:27:32 +0100 | [diff] [blame] | 2342 | cmocka_unit_test_setup_teardown(test_type_pattern, logger_setup, logger_teardown), |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 2343 | cmocka_unit_test_setup_teardown(test_type_enum, logger_setup, logger_teardown), |
| 2344 | cmocka_unit_test_setup_teardown(test_type_bits, logger_setup, logger_teardown), |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 2345 | cmocka_unit_test_setup_teardown(test_type_dec64, logger_setup, logger_teardown), |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 2346 | cmocka_unit_test_setup_teardown(test_type_instanceid, logger_setup, logger_teardown), |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 2347 | cmocka_unit_test_setup_teardown(test_type_identityref, logger_setup, logger_teardown), |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2348 | cmocka_unit_test_setup_teardown(test_type_leafref, logger_setup, logger_teardown), |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 2349 | cmocka_unit_test_setup_teardown(test_type_empty, logger_setup, logger_teardown), |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2350 | cmocka_unit_test_setup_teardown(test_type_union, logger_setup, logger_teardown), |
| 2351 | cmocka_unit_test_setup_teardown(test_type_dflt, logger_setup, logger_teardown), |
Radek Krejci | 665421c | 2018-12-05 08:03:39 +0100 | [diff] [blame] | 2352 | cmocka_unit_test_setup_teardown(test_status, logger_setup, logger_teardown), |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 2353 | cmocka_unit_test_setup_teardown(test_node_container, logger_setup, logger_teardown), |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 2354 | cmocka_unit_test_setup_teardown(test_node_leaflist, logger_setup, logger_teardown), |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 2355 | cmocka_unit_test_setup_teardown(test_node_list, logger_setup, logger_teardown), |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 2356 | cmocka_unit_test_setup_teardown(test_node_choice, logger_setup, logger_teardown), |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 2357 | cmocka_unit_test_setup_teardown(test_node_anydata, logger_setup, logger_teardown), |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2358 | cmocka_unit_test_setup_teardown(test_uses, logger_setup, logger_teardown), |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2359 | cmocka_unit_test_setup_teardown(test_refine, logger_setup, logger_teardown), |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 2360 | }; |
| 2361 | |
| 2362 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 2363 | } |