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); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 115 | FREE_ARRAY(module->ctx, module->off_features, lysc_feature_free); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 116 | |
| 117 | memset(module, 0, sizeof *module); |
| 118 | module->ctx = ctx; |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 119 | module->implemented = 1; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | static void |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 123 | test_module(void **state) |
| 124 | { |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 125 | *state = test_module; |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 126 | |
| 127 | const char *str; |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 128 | struct ly_parser_ctx ctx = {0}; |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 129 | struct lys_module mod = {0}; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 130 | struct lysc_feature *f; |
| 131 | struct lysc_iffeature *iff; |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 132 | |
| 133 | str = "module test {namespace urn:test; prefix t;" |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 134 | "feature f1;feature f2 {if-feature f1;}}"; |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 135 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 136 | reset_mod(ctx.ctx, &mod); |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 137 | |
Radek Krejci | f8f882a | 2018-10-31 14:51:15 +0100 | [diff] [blame] | 138 | assert_int_equal(LY_EINVAL, lys_compile(NULL, 0)); |
| 139 | logbuf_assert("Invalid argument mod (lys_compile())."); |
| 140 | assert_int_equal(LY_EINVAL, lys_compile(&mod, 0)); |
| 141 | logbuf_assert("Invalid argument mod->parsed (lys_compile())."); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 142 | assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, str, &mod)); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 143 | mod.implemented = 0; |
| 144 | assert_int_equal(LY_SUCCESS, lys_compile(&mod, 0)); |
| 145 | assert_null(mod.compiled); |
| 146 | mod.implemented = 1; |
Radek Krejci | f8f882a | 2018-10-31 14:51:15 +0100 | [diff] [blame] | 147 | assert_int_equal(LY_SUCCESS, lys_compile(&mod, 0)); |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 148 | assert_non_null(mod.compiled); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 149 | assert_string_equal("test", mod.name); |
| 150 | assert_string_equal("urn:test", mod.ns); |
| 151 | assert_string_equal("t", mod.prefix); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 152 | /* features */ |
| 153 | assert_non_null(mod.compiled->features); |
| 154 | assert_int_equal(2, LY_ARRAY_SIZE(mod.compiled->features)); |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 155 | f = &mod.compiled->features[1]; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 156 | assert_non_null(f->iffeatures); |
| 157 | assert_int_equal(1, LY_ARRAY_SIZE(f->iffeatures)); |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 158 | iff = &f->iffeatures[0]; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 159 | assert_non_null(iff->expr); |
| 160 | assert_non_null(iff->features); |
| 161 | assert_int_equal(1, LY_ARRAY_SIZE(iff->features)); |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 162 | assert_ptr_equal(&mod.compiled->features[0], iff->features[0]); |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 163 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 164 | lysc_module_free(mod.compiled, NULL); |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 165 | |
Radek Krejci | f8f882a | 2018-10-31 14:51:15 +0100 | [diff] [blame] | 166 | assert_int_equal(LY_SUCCESS, lys_compile(&mod, LYSC_OPT_FREE_SP)); |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 167 | assert_non_null(mod.compiled); |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 168 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 169 | lysc_module_free(mod.compiled, NULL); |
| 170 | mod.compiled = NULL; |
| 171 | |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 172 | /* submodules cannot be compiled directly */ |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 173 | str = "submodule test {belongs-to xxx {prefix x;}}"; |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 174 | assert_int_equal(LY_EINVAL, yang_parse_module(&ctx, str, &mod)); |
| 175 | logbuf_assert("Input data contains submodule which cannot be parsed directly without its main module."); |
| 176 | assert_null(mod.parsed); |
| 177 | reset_mod(ctx.ctx, &mod); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 178 | |
| 179 | /* data definition name collision in top level */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 180 | assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, "module aa {namespace urn:aa;prefix aa;" |
| 181 | "leaf a {type string;} container a{presence x;}}", &mod)); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 182 | assert_int_equal(LY_EVALID, lys_compile(&mod, 0)); |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 183 | logbuf_assert("Duplicate identifier \"a\" of data definition/RPC/action/Notification statement."); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 184 | assert_null(mod.compiled); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 185 | reset_mod(ctx.ctx, &mod); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 186 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 187 | *state = NULL; |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 188 | ly_ctx_destroy(ctx.ctx, NULL); |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 189 | } |
| 190 | |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 191 | static void |
| 192 | test_feature(void **state) |
| 193 | { |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 194 | *state = test_feature; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 195 | |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 196 | struct ly_parser_ctx ctx = {0}; |
Radek Krejci | 1aefdf7 | 2018-11-01 11:01:39 +0100 | [diff] [blame] | 197 | struct lys_module mod = {0}, *modp; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 198 | const char *str; |
| 199 | struct lysc_feature *f, *f1; |
| 200 | |
| 201 | str = "module a {namespace urn:a;prefix a;yang-version 1.1;\n" |
| 202 | "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] | 203 | "feature orfeature {if-feature \"f1 or f2\";}\n" |
| 204 | "feature andfeature {if-feature \"f1 and f2\";}\n" |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 205 | "feature f6 {if-feature \"not f1\";}\n" |
Radek Krejci | dde18c5 | 2018-10-24 14:43:04 +0200 | [diff] [blame] | 206 | "feature f7 {if-feature \"(f2 and f3) or (not f1)\";}\n" |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 207 | "feature f8 {if-feature \"f1 or f2 or f3 or orfeature or andfeature\";}\n" |
| 208 | "feature f9 {if-feature \"not not f1\";}}"; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 209 | |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 210 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 211 | reset_mod(ctx.ctx, &mod); |
| 212 | |
| 213 | assert_int_equal(LY_SUCCESS, yang_parse_module(&ctx, str, &mod)); |
Radek Krejci | f8f882a | 2018-10-31 14:51:15 +0100 | [diff] [blame] | 214 | assert_int_equal(LY_SUCCESS, lys_compile(&mod, 0)); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 215 | assert_non_null(mod.compiled); |
| 216 | assert_non_null(mod.compiled->features); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 217 | assert_int_equal(9, LY_ARRAY_SIZE(mod.compiled->features)); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 218 | /* all features are disabled by default */ |
| 219 | LY_ARRAY_FOR(mod.compiled->features, struct lysc_feature, f) { |
| 220 | assert_int_equal(0, lysc_feature_value(f)); |
| 221 | } |
| 222 | /* enable f1 */ |
| 223 | assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f1")); |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 224 | f1 = &mod.compiled->features[0]; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 225 | assert_int_equal(1, lysc_feature_value(f1)); |
| 226 | |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 227 | /* enable orfeature */ |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 228 | f = &mod.compiled->features[3]; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 229 | assert_int_equal(0, lysc_feature_value(f)); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 230 | assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "orfeature")); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 231 | assert_int_equal(1, lysc_feature_value(f)); |
| 232 | |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 233 | /* enable andfeature - no possible since f2 is disabled */ |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 234 | f = &mod.compiled->features[4]; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 235 | assert_int_equal(0, lysc_feature_value(f)); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 236 | assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "andfeature")); |
| 237 | 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] | 238 | assert_int_equal(0, lysc_feature_value(f)); |
| 239 | |
| 240 | /* first enable f2, so f5 can be enabled then */ |
| 241 | assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f2")); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 242 | assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "andfeature")); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 243 | assert_int_equal(1, lysc_feature_value(f)); |
| 244 | |
| 245 | /* f1 is enabled, so f6 cannot be enabled */ |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 246 | f = &mod.compiled->features[5]; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 247 | assert_int_equal(0, lysc_feature_value(f)); |
| 248 | assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "f6")); |
| 249 | logbuf_assert("Feature \"f6\" cannot be enabled since it is disabled by its if-feature condition(s)."); |
| 250 | assert_int_equal(0, lysc_feature_value(f)); |
| 251 | |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 252 | /* so disable f1 - andfeature will became also disabled */ |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 253 | assert_int_equal(1, lysc_feature_value(f1)); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 254 | assert_int_equal(LY_SUCCESS, lys_feature_disable(&mod, "f1")); |
| 255 | assert_int_equal(0, lysc_feature_value(f1)); |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 256 | assert_int_equal(0, lysc_feature_value(&mod.compiled->features[4])); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 257 | /* while orfeature is stille enabled */ |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 258 | assert_int_equal(1, lysc_feature_value(&mod.compiled->features[3])); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 259 | /* and finally f6 can be enabled */ |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 260 | assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f6")); |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 261 | assert_int_equal(1, lysc_feature_value(&mod.compiled->features[5])); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 262 | |
| 263 | /* 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] | 264 | 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] | 265 | /* long evaluation of f8 to need to reallocate internal stack for operators */ |
| 266 | 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] | 267 | |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 268 | /* double negation of disabled f1 -> disabled */ |
| 269 | assert_int_equal(0, lysc_iffeature_value(&mod.compiled->features[8].iffeatures[0])); |
| 270 | |
Radek Krejci | 3892028 | 2018-11-01 09:24:16 +0100 | [diff] [blame] | 271 | /* disable all features */ |
| 272 | assert_int_equal(LY_SUCCESS, lys_feature_disable(&mod, "*")); |
| 273 | LY_ARRAY_FOR(mod.compiled->features, struct lysc_feature, f) { |
| 274 | assert_int_equal(0, lys_feature_value(&mod, f->name)); |
| 275 | } |
| 276 | /* re-setting already set feature */ |
| 277 | assert_int_equal(LY_SUCCESS, lys_feature_disable(&mod, "f1")); |
| 278 | assert_int_equal(0, lys_feature_value(&mod, "f1")); |
| 279 | |
| 280 | /* enabling feature that cannot be enabled due to its if-features */ |
Radek Krejci | 1aefdf7 | 2018-11-01 11:01:39 +0100 | [diff] [blame] | 281 | assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f1")); |
| 282 | assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "andfeature")); |
| 283 | 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] | 284 | assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "*")); |
| 285 | 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] | 286 | /* test if not changed */ |
| 287 | assert_int_equal(1, lys_feature_value(&mod, "f1")); |
| 288 | assert_int_equal(0, lys_feature_value(&mod, "f2")); |
Radek Krejci | 3892028 | 2018-11-01 09:24:16 +0100 | [diff] [blame] | 289 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 290 | assert_non_null(modp = lys_parse_mem(ctx.ctx, "module b {namespace urn:b;prefix b;" |
| 291 | "feature f1 {if-feature f2;}feature f2;}", LYS_IN_YANG)); |
| 292 | assert_non_null(modp->compiled); |
| 293 | assert_non_null(modp->compiled->features); |
| 294 | assert_int_equal(2, LY_ARRAY_SIZE(modp->compiled->features)); |
| 295 | assert_non_null(modp->compiled->features[0].iffeatures); |
| 296 | assert_int_equal(1, LY_ARRAY_SIZE(modp->compiled->features[0].iffeatures)); |
| 297 | assert_non_null(modp->compiled->features[0].iffeatures[0].features); |
| 298 | assert_int_equal(1, LY_ARRAY_SIZE(modp->compiled->features[0].iffeatures[0].features)); |
| 299 | assert_ptr_equal(&modp->compiled->features[1], modp->compiled->features[0].iffeatures[0].features[0]); |
| 300 | assert_non_null(modp->compiled->features); |
| 301 | assert_int_equal(2, LY_ARRAY_SIZE(modp->compiled->features)); |
| 302 | assert_non_null(modp->compiled->features[1].depfeatures); |
| 303 | assert_int_equal(1, LY_ARRAY_SIZE(modp->compiled->features[1].depfeatures)); |
| 304 | assert_ptr_equal(&modp->compiled->features[0], modp->compiled->features[1].depfeatures[0]); |
| 305 | |
Radek Krejci | 1aefdf7 | 2018-11-01 11:01:39 +0100 | [diff] [blame] | 306 | /* invalid reference */ |
Radek Krejci | 3892028 | 2018-11-01 09:24:16 +0100 | [diff] [blame] | 307 | assert_int_equal(LY_EINVAL, lys_feature_enable(&mod, "xxx")); |
| 308 | logbuf_assert("Feature \"xxx\" not found in module \"a\"."); |
| 309 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 310 | reset_mod(ctx.ctx, &mod); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 311 | |
| 312 | /* some invalid expressions */ |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 313 | 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] | 314 | assert_int_equal(LY_EVALID, lys_compile(&mod, 0)); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 315 | 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] | 316 | reset_mod(ctx.ctx, &mod); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 317 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 318 | 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] | 319 | assert_int_equal(LY_EVALID, lys_compile(&mod, 0)); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 320 | 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] | 321 | reset_mod(ctx.ctx, &mod); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 322 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 323 | 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] | 324 | assert_int_equal(LY_EVALID, lys_compile(&mod, 0)); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 325 | logbuf_assert("Invalid value \"or\" of if-feature - unexpected end of expression."); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 326 | reset_mod(ctx.ctx, &mod); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 327 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 328 | 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] | 329 | assert_int_equal(LY_EVALID, lys_compile(&mod, 0)); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 330 | 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] | 331 | reset_mod(ctx.ctx, &mod); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 332 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 333 | 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] | 334 | assert_int_equal(LY_EVALID, lys_compile(&mod, 0)); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 335 | 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] | 336 | reset_mod(ctx.ctx, &mod); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 337 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 338 | 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] | 339 | assert_int_equal(LY_EVALID, lys_compile(&mod, 0)); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 340 | logbuf_assert("Invalid value \"---\" of if-feature - unable to find feature \"---\"."); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 341 | reset_mod(ctx.ctx, &mod); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 342 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 343 | 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] | 344 | assert_int_equal(LY_EVALID, lys_compile(&mod, 0)); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 345 | 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] | 346 | reset_mod(ctx.ctx, &mod); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 347 | |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 348 | 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] | 349 | assert_int_equal(LY_EVALID, lys_compile(&mod, 0)); |
| 350 | logbuf_assert("Duplicate identifier \"f1\" of feature statement."); |
Radek Krejci | 0bcdaed | 2019-01-10 10:21:34 +0100 | [diff] [blame] | 351 | reset_mod(ctx.ctx, &mod); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 352 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 353 | ly_ctx_set_module_imp_clb(ctx.ctx, test_imp_clb, "submodule sz {belongs-to z {prefix z;} feature f1;}"); |
| 354 | 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] | 355 | logbuf_assert("Duplicate identifier \"f1\" of feature statement."); |
| 356 | |
Radek Krejci | 09a1fc5 | 2019-02-13 10:55:17 +0100 | [diff] [blame] | 357 | assert_null(lys_parse_mem(ctx.ctx, "module aa{namespace urn:aa; prefix aa; feature f1 {if-feature f2;} feature f2 {if-feature f1;}}", LYS_IN_YANG)); |
| 358 | logbuf_assert("Feature \"f1\" is indirectly referenced from itself."); |
| 359 | assert_null(lys_parse_mem(ctx.ctx, "module ab{namespace urn:ab; prefix ab; feature f1 {if-feature f1;}}", LYS_IN_YANG)); |
| 360 | logbuf_assert("Feature \"f1\" is referenced from itself."); |
| 361 | |
Radek Krejci | 1aefdf7 | 2018-11-01 11:01:39 +0100 | [diff] [blame] | 362 | /* import reference */ |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 363 | 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] | 364 | assert_int_equal(LY_SUCCESS, lys_feature_enable(modp, "f1")); |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 365 | 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] | 366 | assert_int_equal(LY_SUCCESS, lys_feature_enable(modp, "f2")); |
| 367 | assert_int_equal(0, lys_feature_value(modp, "f1")); |
| 368 | assert_int_equal(1, lys_feature_value(modp, "f2")); |
| 369 | |
Radek Krejci | d05cbd9 | 2018-12-05 14:26:40 +0100 | [diff] [blame] | 370 | *state = NULL; |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 371 | ly_ctx_destroy(ctx.ctx, NULL); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 372 | } |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 373 | |
Radek Krejci | 478020e | 2018-10-30 16:02:14 +0100 | [diff] [blame] | 374 | static void |
| 375 | test_identity(void **state) |
| 376 | { |
Radek Krejci | 7f2a536 | 2018-11-28 13:05:37 +0100 | [diff] [blame] | 377 | *state = test_identity; |
Radek Krejci | 478020e | 2018-10-30 16:02:14 +0100 | [diff] [blame] | 378 | |
| 379 | struct ly_ctx *ctx; |
Radek Krejci | 1aefdf7 | 2018-11-01 11:01:39 +0100 | [diff] [blame] | 380 | struct lys_module *mod1, *mod2; |
Radek Krejci | 478020e | 2018-10-30 16:02:14 +0100 | [diff] [blame] | 381 | |
| 382 | 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] | 383 | assert_non_null(mod1 = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a; identity a1;}", LYS_IN_YANG)); |
| 384 | assert_non_null(mod2 = lys_parse_mem(ctx, "module b {yang-version 1.1;namespace urn:b;prefix b; import a {prefix a;}" |
| 385 | "identity b1; identity b2; identity b3 {base b1; base b:b2; base a:a1;}" |
| 386 | "identity b4 {base b:b1; base b3;}}", LYS_IN_YANG)); |
Radek Krejci | 478020e | 2018-10-30 16:02:14 +0100 | [diff] [blame] | 387 | |
| 388 | assert_non_null(mod1->compiled); |
| 389 | assert_non_null(mod1->compiled->identities); |
| 390 | assert_non_null(mod2->compiled); |
| 391 | assert_non_null(mod2->compiled->identities); |
| 392 | |
| 393 | assert_non_null(mod1->compiled->identities[0].derived); |
| 394 | assert_int_equal(1, LY_ARRAY_SIZE(mod1->compiled->identities[0].derived)); |
| 395 | assert_ptr_equal(mod1->compiled->identities[0].derived[0], &mod2->compiled->identities[2]); |
| 396 | assert_non_null(mod2->compiled->identities[0].derived); |
| 397 | assert_int_equal(2, LY_ARRAY_SIZE(mod2->compiled->identities[0].derived)); |
| 398 | assert_ptr_equal(mod2->compiled->identities[0].derived[0], &mod2->compiled->identities[2]); |
| 399 | assert_ptr_equal(mod2->compiled->identities[0].derived[1], &mod2->compiled->identities[3]); |
| 400 | assert_non_null(mod2->compiled->identities[1].derived); |
| 401 | assert_int_equal(1, LY_ARRAY_SIZE(mod2->compiled->identities[1].derived)); |
| 402 | assert_ptr_equal(mod2->compiled->identities[1].derived[0], &mod2->compiled->identities[2]); |
| 403 | assert_non_null(mod2->compiled->identities[2].derived); |
| 404 | assert_int_equal(1, LY_ARRAY_SIZE(mod2->compiled->identities[2].derived)); |
| 405 | assert_ptr_equal(mod2->compiled->identities[2].derived[0], &mod2->compiled->identities[3]); |
| 406 | |
Radek Krejci | 4d483a8 | 2019-01-17 13:12:50 +0100 | [diff] [blame] | 407 | assert_non_null(mod2 = lys_parse_mem(ctx, "module c {yang-version 1.1;namespace urn:c;prefix c;" |
| 408 | "identity c2 {base c1;} identity c1;}", LYS_IN_YANG)); |
| 409 | assert_int_equal(1, LY_ARRAY_SIZE(mod2->compiled->identities[1].derived)); |
| 410 | assert_ptr_equal(mod2->compiled->identities[1].derived[0], &mod2->compiled->identities[0]); |
| 411 | |
| 412 | 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] | 413 | logbuf_assert("Duplicate identifier \"i1\" of identity statement."); |
| 414 | |
Radek Krejci | 4d483a8 | 2019-01-17 13:12:50 +0100 | [diff] [blame] | 415 | ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule sbb {belongs-to bb {prefix bb;} identity i1;}"); |
| 416 | 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] | 417 | logbuf_assert("Duplicate identifier \"i1\" of identity statement."); |
| 418 | |
Radek Krejci | 3822263 | 2019-02-12 16:55:05 +0100 | [diff] [blame] | 419 | assert_null(lys_parse_mem(ctx, "module cc{namespace urn:cc; prefix cc; identity i1 {base i2;}}", LYS_IN_YANG)); |
| 420 | logbuf_assert("Unable to find base (i2) of identity \"i1\"."); |
| 421 | |
| 422 | assert_null(lys_parse_mem(ctx, "module dd{namespace urn:dd; prefix dd; identity i1 {base i1;}}", LYS_IN_YANG)); |
| 423 | logbuf_assert("Identity \"i1\" is derived from itself."); |
| 424 | assert_null(lys_parse_mem(ctx, "module de{namespace urn:de; prefix de; identity i1 {base i2;}identity i2 {base i3;}identity i3 {base i1;}}", LYS_IN_YANG)); |
| 425 | logbuf_assert("Identity \"i1\" is indirectly derived from itself."); |
| 426 | |
Radek Krejci | 7f2a536 | 2018-11-28 13:05:37 +0100 | [diff] [blame] | 427 | *state = NULL; |
Radek Krejci | 478020e | 2018-10-30 16:02:14 +0100 | [diff] [blame] | 428 | ly_ctx_destroy(ctx, NULL); |
| 429 | } |
| 430 | |
Radek Krejci | bd8d9ba | 2018-11-02 16:06:26 +0100 | [diff] [blame] | 431 | static void |
| 432 | test_node_container(void **state) |
| 433 | { |
| 434 | (void) state; /* unused */ |
| 435 | |
| 436 | struct ly_ctx *ctx; |
| 437 | struct lys_module *mod; |
| 438 | struct lysc_node_container *cont; |
| 439 | |
| 440 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 441 | 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] | 442 | assert_non_null(mod->compiled); |
| 443 | assert_non_null((cont = (struct lysc_node_container*)mod->compiled->data)); |
| 444 | assert_int_equal(LYS_CONTAINER, cont->nodetype); |
| 445 | assert_string_equal("c", cont->name); |
| 446 | assert_true(cont->flags & LYS_CONFIG_W); |
| 447 | assert_true(cont->flags & LYS_STATUS_CURR); |
| 448 | |
Radek Krejci | 98094b3 | 2018-11-02 16:21:47 +0100 | [diff] [blame] | 449 | 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] | 450 | 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] | 451 | assert_non_null(mod->compiled); |
| 452 | assert_non_null((cont = (struct lysc_node_container*)mod->compiled->data)); |
| 453 | assert_true(cont->flags & LYS_CONFIG_R); |
Radek Krejci | 98094b3 | 2018-11-02 16:21:47 +0100 | [diff] [blame] | 454 | assert_true(cont->flags & LYS_STATUS_DEPRC); |
Radek Krejci | bd8d9ba | 2018-11-02 16:06:26 +0100 | [diff] [blame] | 455 | assert_non_null((cont = (struct lysc_node_container*)cont->child)); |
| 456 | assert_int_equal(LYS_CONTAINER, cont->nodetype); |
| 457 | assert_true(cont->flags & LYS_CONFIG_R); |
Radek Krejci | 98094b3 | 2018-11-02 16:21:47 +0100 | [diff] [blame] | 458 | assert_true(cont->flags & LYS_STATUS_DEPRC); |
Radek Krejci | bd8d9ba | 2018-11-02 16:06:26 +0100 | [diff] [blame] | 459 | assert_string_equal("child", cont->name); |
| 460 | |
| 461 | ly_ctx_destroy(ctx, NULL); |
| 462 | } |
| 463 | |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 464 | static void |
| 465 | test_node_leaflist(void **state) |
| 466 | { |
| 467 | *state = test_node_leaflist; |
| 468 | |
| 469 | struct ly_ctx *ctx; |
| 470 | struct lys_module *mod; |
| 471 | struct lysc_type *type; |
| 472 | struct lysc_node_leaflist *ll; |
| 473 | |
| 474 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 475 | |
| 476 | assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;" |
| 477 | "typedef mytype {type union {type leafref {path ../target;} type string;}}" |
| 478 | "leaf-list ll1 {type union {type decimal64 {fraction-digits 2;} type mytype;}}" |
| 479 | "leaf-list ll2 {type leafref {path ../target;}}" |
| 480 | "leaf target {type int8;}}", |
| 481 | LYS_IN_YANG)); |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 482 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 483 | assert_non_null(type); |
| 484 | assert_int_equal(1, type->refcount); |
| 485 | assert_int_equal(LY_TYPE_UNION, type->basetype); |
| 486 | assert_non_null(((struct lysc_type_union*)type)->types); |
| 487 | assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types)); |
| 488 | assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype); |
| 489 | assert_int_equal(LY_TYPE_LEAFREF, ((struct lysc_type_union*)type)->types[1]->basetype); |
| 490 | assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[2]->basetype); |
| 491 | assert_non_null(((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype); |
| 492 | assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype->basetype); |
| 493 | type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type; |
| 494 | assert_non_null(type); |
| 495 | assert_int_equal(1, type->refcount); |
| 496 | assert_int_equal(LY_TYPE_LEAFREF, type->basetype); |
| 497 | assert_non_null(((struct lysc_type_leafref*)type)->realtype); |
| 498 | assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)type)->realtype->basetype); |
| 499 | |
Radek Krejci | 6bb080b | 2018-11-28 17:23:41 +0100 | [diff] [blame] | 500 | 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] | 501 | assert_non_null(mod->compiled); |
| 502 | assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data)); |
| 503 | assert_int_equal(0, ll->min); |
| 504 | assert_int_equal((uint32_t)-1, ll->max); |
| 505 | |
Radek Krejci | 6bb080b | 2018-11-28 17:23:41 +0100 | [diff] [blame] | 506 | 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] | 507 | "leaf-list ll1 {type mytype;default 1; default 1; config false;}" |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 508 | "leaf-list ll2 {type mytype; ordered-by user;}}", LYS_IN_YANG)); |
Radek Krejci | 6bb080b | 2018-11-28 17:23:41 +0100 | [diff] [blame] | 509 | assert_non_null(mod->compiled); |
| 510 | assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data)); |
| 511 | assert_non_null(ll->dflts); |
| 512 | assert_int_equal(3, ll->type->refcount); |
| 513 | assert_int_equal(2, LY_ARRAY_SIZE(ll->dflts)); |
| 514 | assert_string_equal("1", ll->dflts[0]); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 515 | assert_string_equal("1", ll->dflts[1]); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 516 | assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM | LYS_SET_DFLT | LYS_SET_CONFIG, ll->flags); |
Radek Krejci | 6bb080b | 2018-11-28 17:23:41 +0100 | [diff] [blame] | 517 | assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data->next)); |
| 518 | assert_non_null(ll->dflts); |
| 519 | assert_int_equal(3, ll->type->refcount); |
| 520 | assert_int_equal(1, LY_ARRAY_SIZE(ll->dflts)); |
| 521 | assert_string_equal("10", ll->dflts[0]); |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 522 | assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_USER, ll->flags); |
| 523 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 524 | /* ordered-by is ignored for state data, RPC/action output parameters and notification content */ |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 525 | assert_non_null(mod = lys_parse_mem(ctx, "module d {yang-version 1.1;namespace urn:d;prefix d;" |
| 526 | "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] | 527 | /* but warning is present: */ |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 528 | logbuf_assert("The ordered-by statement is ignored in lists representing state data ()."); |
Radek Krejci | a6d5773 | 2018-11-29 13:40:37 +0100 | [diff] [blame] | 529 | assert_non_null(mod->compiled); |
| 530 | assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data)); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 531 | assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM | LYS_SET_CONFIG, ll->flags); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 532 | logbuf_clean(); |
| 533 | |
| 534 | assert_non_null(mod = lys_parse_mem(ctx, "module e {yang-version 1.1;namespace urn:e;prefix e;" |
| 535 | "rpc oper {output {leaf-list ll {type string; ordered-by user;}}}}", LYS_IN_YANG)); |
| 536 | logbuf_assert("The ordered-by statement is ignored in lists representing RPC/action output parameters ()."); |
| 537 | logbuf_clean(); |
| 538 | |
| 539 | assert_non_null(mod = lys_parse_mem(ctx, "module f {yang-version 1.1;namespace urn:f;prefix f;" |
| 540 | "notification event {leaf-list ll {type string; ordered-by user;}}}", LYS_IN_YANG)); |
| 541 | logbuf_assert("The ordered-by statement is ignored in lists representing notification content ()."); |
Radek Krejci | 6bb080b | 2018-11-28 17:23:41 +0100 | [diff] [blame] | 542 | |
| 543 | /* invalid */ |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 544 | 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] | 545 | logbuf_assert("Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules."); |
| 546 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 547 | 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] | 548 | logbuf_assert("Leaf-list of type \"empty\" must not have a default value (x)."); |
| 549 | |
| 550 | assert_non_null(mod = lys_parse_mem(ctx, "module cc {yang-version 1.1;namespace urn:cc;prefix cc;" |
| 551 | "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] | 552 | assert_non_null(mod->compiled); |
| 553 | assert_non_null((ll = (struct lysc_node_leaflist*)mod->compiled->data)); |
| 554 | assert_non_null(ll->dflts); |
| 555 | assert_int_equal(3, LY_ARRAY_SIZE(ll->dflts)); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 556 | assert_null(lys_parse_mem(ctx, "module dd {yang-version 1.1;namespace urn:dd;prefix dd;" |
| 557 | "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] | 558 | logbuf_assert("Configuration leaf-list has multiple defaults of the same value \"one\"."); |
| 559 | |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 560 | *state = NULL; |
| 561 | ly_ctx_destroy(ctx, NULL); |
| 562 | } |
| 563 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 564 | static void |
| 565 | test_node_list(void **state) |
| 566 | { |
| 567 | *state = test_node_list; |
| 568 | |
| 569 | struct ly_ctx *ctx; |
| 570 | struct lys_module *mod; |
| 571 | struct lysc_node_list *list; |
| 572 | |
| 573 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 574 | |
| 575 | assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;feature f;" |
| 576 | "list l1 {key \"x y\"; ordered-by user; leaf x {type string; when 1;}leaf y{type string;if-feature f;}}" |
| 577 | "list l2 {config false;leaf value {type string;}}}", LYS_IN_YANG)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 578 | list = (struct lysc_node_list*)mod->compiled->data; |
| 579 | assert_non_null(list); |
| 580 | assert_non_null(list->keys); |
| 581 | assert_int_equal(2, LY_ARRAY_SIZE(list->keys)); |
| 582 | assert_string_equal("x", list->keys[0]->name); |
| 583 | assert_string_equal("y", list->keys[1]->name); |
| 584 | assert_non_null(list->child); |
| 585 | assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR | LYS_ORDBY_USER, list->flags); |
| 586 | assert_true(list->child->flags & LYS_KEY); |
| 587 | assert_true(list->child->next->flags & LYS_KEY); |
| 588 | list = (struct lysc_node_list*)mod->compiled->data->next; |
| 589 | assert_non_null(list); |
| 590 | assert_null(list->keys); |
| 591 | assert_non_null(list->child); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 592 | assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_ORDBY_SYSTEM | LYS_SET_CONFIG, list->flags); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 593 | assert_false(list->child->flags & LYS_KEY); |
| 594 | |
| 595 | assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;" |
| 596 | "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] | 597 | "leaf a {type string; default x;} leaf d {type string;config false;}" |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 598 | "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] | 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); |
Radek Krejci | 665421c | 2018-12-05 08:03:39 +0100 | [diff] [blame] | 606 | assert_null(list->keys[0]->dflt); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 607 | assert_non_null(list->uniques); |
| 608 | assert_int_equal(2, LY_ARRAY_SIZE(list->uniques)); |
| 609 | assert_int_equal(2, LY_ARRAY_SIZE(list->uniques[0])); |
| 610 | assert_string_equal("a", list->uniques[0][0]->name); |
| 611 | assert_true(list->uniques[0][0]->flags & LYS_UNIQUE); |
| 612 | assert_string_equal("b", list->uniques[0][1]->name); |
| 613 | assert_true(list->uniques[0][1]->flags & LYS_UNIQUE); |
| 614 | assert_int_equal(2, LY_ARRAY_SIZE(list->uniques[1])); |
| 615 | assert_string_equal("e", list->uniques[1][0]->name); |
| 616 | assert_true(list->uniques[1][0]->flags & LYS_UNIQUE); |
| 617 | assert_string_equal("d", list->uniques[1][1]->name); |
| 618 | assert_true(list->uniques[1][1]->flags & LYS_UNIQUE); |
| 619 | |
Radek Krejci | 665421c | 2018-12-05 08:03:39 +0100 | [diff] [blame] | 620 | assert_non_null(mod = lys_parse_mem(ctx, "module c {yang-version 1.1;namespace urn:c;prefix c;" |
| 621 | "list l {key a;leaf a {type empty;}}}", LYS_IN_YANG)); |
Radek Krejci | 665421c | 2018-12-05 08:03:39 +0100 | [diff] [blame] | 622 | list = (struct lysc_node_list*)mod->compiled->data; |
| 623 | assert_non_null(list); |
| 624 | assert_string_equal("l", list->name); |
| 625 | assert_non_null(list->keys); |
| 626 | assert_int_equal(1, LY_ARRAY_SIZE(list->keys)); |
| 627 | assert_string_equal("a", list->keys[0]->name); |
| 628 | assert_true(list->keys[0]->flags & LYS_KEY); |
| 629 | assert_int_equal(LY_TYPE_EMPTY, list->keys[0]->type->basetype); |
| 630 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 631 | /* invalid */ |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 632 | 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] | 633 | logbuf_assert("Missing key in list representing configuration data."); |
| 634 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 635 | assert_null(lys_parse_mem(ctx, "module bb {yang-version 1.1; namespace urn:bb;prefix bb;" |
| 636 | "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] | 637 | logbuf_assert("List's key \"x\" must not have any \"when\" statement."); |
| 638 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 639 | assert_null(lys_parse_mem(ctx, "module cc {yang-version 1.1;namespace urn:cc;prefix cc;feature f;" |
| 640 | "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] | 641 | logbuf_assert("List's key \"x\" must not have any \"if-feature\" statement."); |
| 642 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 643 | assert_null(lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd;" |
| 644 | "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] | 645 | logbuf_assert("Key of the configuration list must not be status leaf."); |
| 646 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 647 | assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;" |
| 648 | "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] | 649 | logbuf_assert("Configuration node cannot be child of any state data node."); |
| 650 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 651 | assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;" |
| 652 | "list l {key x; leaf-list x {type string;}}}", LYS_IN_YANG)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 653 | logbuf_assert("The list's key \"x\" not found."); |
| 654 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 655 | assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;" |
| 656 | "list l {key x; unique y;leaf x {type string;} leaf-list y {type string;}}}", LYS_IN_YANG)); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 657 | logbuf_assert("Unique's descendant-schema-nodeid \"y\" refers to leaf-list node instead of a leaf."); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 658 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 659 | assert_null(lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;" |
| 660 | "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] | 661 | logbuf_assert("Unique statement \"x y\" refers to leafs with different config type."); |
| 662 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 663 | assert_null(lys_parse_mem(ctx, "module ii {namespace urn:ii;prefix ii;" |
| 664 | "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] | 665 | logbuf_assert("Invalid descendant-schema-nodeid value \"a:x\" - prefix \"a\" not defined in module \"ii\"."); |
| 666 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 667 | assert_null(lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;" |
| 668 | "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] | 669 | logbuf_assert("Invalid descendant-schema-nodeid value \"c/x\" - target node not found."); |
| 670 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 671 | assert_null( lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;" |
| 672 | "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] | 673 | logbuf_assert("Invalid descendant-schema-nodeid value \"c^\" - missing \"/\" as node-identifier separator."); |
| 674 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 675 | assert_null(lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;" |
| 676 | "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] | 677 | logbuf_assert("Duplicated key identifier \"x\"."); |
| 678 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 679 | assert_null(lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;" |
| 680 | "list l {key x;leaf x {type empty;}}}", LYS_IN_YANG)); |
Radek Krejci | 665421c | 2018-12-05 08:03:39 +0100 | [diff] [blame] | 681 | logbuf_assert("Key of a list can be of type \"empty\" only in YANG 1.1 modules."); |
| 682 | |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 683 | *state = NULL; |
| 684 | ly_ctx_destroy(ctx, NULL); |
| 685 | } |
| 686 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 687 | static void |
| 688 | test_node_choice(void **state) |
| 689 | { |
| 690 | *state = test_node_choice; |
| 691 | |
| 692 | struct ly_ctx *ctx; |
| 693 | struct lys_module *mod; |
| 694 | struct lysc_node_choice *ch; |
| 695 | struct lysc_node_case *cs; |
| 696 | |
| 697 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 698 | |
| 699 | 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] | 700 | "choice ch {default a:b; case a {leaf a1 {type string;}leaf a2 {type string;}}" |
| 701 | "leaf b {type string;}}}", LYS_IN_YANG)); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 702 | ch = (struct lysc_node_choice*)mod->compiled->data; |
| 703 | assert_non_null(ch); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 704 | assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR, ch->flags); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 705 | cs = ch->cases; |
| 706 | assert_non_null(cs); |
| 707 | assert_string_equal("a", cs->name); |
| 708 | assert_ptr_equal(ch, cs->parent); |
| 709 | assert_non_null(cs->child); |
Radek Krejci | 18f2b8a | 2018-12-12 16:41:21 +0100 | [diff] [blame] | 710 | assert_string_equal("a1", cs->child->name); |
| 711 | assert_non_null(cs->child->next); |
| 712 | assert_string_equal("a2", cs->child->next->name); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 713 | assert_ptr_equal(cs, cs->child->parent); |
| 714 | cs = (struct lysc_node_case*)cs->next; |
| 715 | assert_non_null(cs); |
| 716 | assert_string_equal("b", cs->name); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 717 | assert_int_equal(LYS_STATUS_CURR | LYS_SET_DFLT, cs->flags); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 718 | assert_ptr_equal(ch, cs->parent); |
| 719 | assert_non_null(cs->child); |
| 720 | assert_string_equal("b", cs->child->name); |
| 721 | assert_ptr_equal(cs, cs->child->parent); |
Radek Krejci | 18f2b8a | 2018-12-12 16:41:21 +0100 | [diff] [blame] | 722 | assert_ptr_equal(ch->cases->child->next, cs->child->prev); |
| 723 | assert_ptr_equal(ch->cases->child->next->next, cs->child); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 724 | assert_ptr_equal(ch->cases->child->prev, cs->child); |
Radek Krejci | a9026eb | 2018-12-12 16:04:47 +0100 | [diff] [blame] | 725 | assert_ptr_equal(ch->dflt, cs); |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 726 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 727 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;" |
| 728 | "choice ch {case a {leaf x {type string;}}leaf x {type string;}}}", LYS_IN_YANG)); |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 729 | logbuf_assert("Duplicate identifier \"x\" of data definition/RPC/action/Notification statement."); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 730 | assert_null(lys_parse_mem(ctx, "module aa2 {namespace urn:aa2;prefix aa;" |
| 731 | "choice ch {case a {leaf y {type string;}}case b {leaf y {type string;}}}}", LYS_IN_YANG)); |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 732 | logbuf_assert("Duplicate identifier \"y\" of data definition/RPC/action/Notification statement."); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 733 | assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;" |
| 734 | "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] | 735 | logbuf_assert("Duplicate identifier \"a\" of case statement."); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 736 | assert_null(lys_parse_mem(ctx, "module bb2 {namespace urn:bb2;prefix bb;" |
| 737 | "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] | 738 | logbuf_assert("Duplicate identifier \"b\" of case statement."); |
| 739 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 740 | assert_null(lys_parse_mem(ctx, "module ca {namespace urn:ca;prefix ca;" |
| 741 | "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] | 742 | logbuf_assert("Default case \"c\" not found."); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 743 | assert_null(lys_parse_mem(ctx, "module cb {namespace urn:cb;prefix cb; import a {prefix a;}" |
| 744 | "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] | 745 | 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] | 746 | assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc;" |
| 747 | "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] | 748 | logbuf_assert("Mandatory node \"x\" under the default case \"a\"."); |
| 749 | /* TODO check with mandatory nodes from augment placed into the case */ |
| 750 | |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 751 | *state = NULL; |
| 752 | ly_ctx_destroy(ctx, NULL); |
| 753 | } |
| 754 | |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 755 | static void |
| 756 | test_node_anydata(void **state) |
| 757 | { |
| 758 | *state = test_node_anydata; |
| 759 | |
| 760 | struct ly_ctx *ctx; |
| 761 | struct lys_module *mod; |
| 762 | struct lysc_node_anydata *any; |
| 763 | |
| 764 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 765 | |
| 766 | assert_non_null(mod = lys_parse_mem(ctx, "module a {yang-version 1.1;namespace urn:a;prefix a;" |
| 767 | "anydata any {config false;mandatory true;}}", LYS_IN_YANG)); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 768 | any = (struct lysc_node_anydata*)mod->compiled->data; |
| 769 | assert_non_null(any); |
| 770 | assert_int_equal(LYS_ANYDATA, any->nodetype); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 771 | assert_int_equal(LYS_CONFIG_R | LYS_STATUS_CURR | LYS_MAND_TRUE | LYS_SET_CONFIG, any->flags); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 772 | |
| 773 | logbuf_clean(); |
| 774 | assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;" |
| 775 | "anyxml any;}", LYS_IN_YANG)); |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 776 | any = (struct lysc_node_anydata*)mod->compiled->data; |
| 777 | assert_non_null(any); |
| 778 | assert_int_equal(LYS_ANYXML, any->nodetype); |
| 779 | assert_int_equal(LYS_CONFIG_W | LYS_STATUS_CURR, any->flags); |
| 780 | logbuf_assert("Use of anyxml to define configuration data is not recommended."); /* warning */ |
| 781 | |
| 782 | /* invalid */ |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 783 | 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] | 784 | logbuf_assert("Invalid keyword \"anydata\" as a child of \"module\" - the statement is allowed only in YANG 1.1 modules. Line number 1."); |
| 785 | |
| 786 | *state = NULL; |
| 787 | ly_ctx_destroy(ctx, NULL); |
| 788 | } |
| 789 | |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 790 | static void |
| 791 | test_action(void **state) |
| 792 | { |
| 793 | *state = test_action; |
| 794 | |
| 795 | struct ly_ctx *ctx; |
| 796 | struct lys_module *mod; |
| 797 | const struct lysc_action *rpc; |
| 798 | |
| 799 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 800 | |
| 801 | assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;" |
| 802 | "rpc a {input {leaf x {type int8;} leaf y {type int8;}} output {leaf result {type int16;}}}}", LYS_IN_YANG)); |
| 803 | rpc = mod->compiled->rpcs; |
| 804 | assert_non_null(rpc); |
| 805 | assert_int_equal(1, LY_ARRAY_SIZE(rpc)); |
| 806 | assert_int_equal(LYS_ACTION, rpc->nodetype); |
| 807 | assert_int_equal(LYS_STATUS_CURR, rpc->flags); |
| 808 | assert_string_equal("a", rpc->name); |
| 809 | |
| 810 | assert_non_null(mod = lys_parse_mem(ctx, "module b {yang-version 1.1; namespace urn:b;prefix b; container top {" |
| 811 | "action b {input {leaf x {type int8;} leaf y {type int8;}} output {leaf result {type int16;}}}}}", LYS_IN_YANG)); |
| 812 | rpc = lysc_node_actions(mod->compiled->data); |
| 813 | assert_non_null(rpc); |
| 814 | assert_int_equal(1, LY_ARRAY_SIZE(rpc)); |
| 815 | assert_int_equal(LYS_ACTION, rpc->nodetype); |
| 816 | assert_int_equal(LYS_STATUS_CURR, rpc->flags); |
| 817 | assert_string_equal("b", rpc->name); |
| 818 | |
| 819 | /* invalid */ |
| 820 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;container top {action x;}}", LYS_IN_YANG)); |
| 821 | logbuf_assert("Invalid keyword \"action\" as a child of \"container\" - the statement is allowed only in YANG 1.1 modules. Line number 1."); |
| 822 | |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 823 | assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;leaf x{type string;} rpc x;}", LYS_IN_YANG)); |
| 824 | logbuf_assert("Duplicate identifier \"x\" of data definition/RPC/action/Notification statement."); |
| 825 | assert_null(lys_parse_mem(ctx, "module cc {yang-version 1.1; namespace urn:cc;prefix cc;container c {leaf y {type string;} action y;}}", LYS_IN_YANG)); |
| 826 | logbuf_assert("Duplicate identifier \"y\" of data definition/RPC/action/Notification statement."); |
| 827 | assert_null(lys_parse_mem(ctx, "module dd {yang-version 1.1; namespace urn:dd;prefix dd;container c {action z; action z;}}", LYS_IN_YANG)); |
| 828 | logbuf_assert("Duplicate identifier \"z\" of data definition/RPC/action/Notification statement."); |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 829 | ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule eesub {belongs-to ee {prefix ee;} notification w;}"); |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 830 | assert_null(lys_parse_mem(ctx, "module ee {yang-version 1.1; namespace urn:ee;prefix ee;include eesub; rpc w;}", LYS_IN_YANG)); |
| 831 | logbuf_assert("Duplicate identifier \"w\" of data definition/RPC/action/Notification statement."); |
| 832 | |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 833 | assert_null(lys_parse_mem(ctx, "module ff {yang-version 1.1; namespace urn:ff;prefix ff; rpc test {input {container a {leaf b {type string;}}}}" |
| 834 | "augment /test/input/a {action invalid {input {leaf x {type string;}}}}}", LYS_IN_YANG)); |
| 835 | logbuf_assert("Action \"invalid\" is placed inside another RPC/action."); |
| 836 | |
| 837 | assert_null(lys_parse_mem(ctx, "module gg {yang-version 1.1; namespace urn:gg;prefix gg; notification test {container a {leaf b {type string;}}}" |
| 838 | "augment /test/a {action invalid {input {leaf x {type string;}}}}}", LYS_IN_YANG)); |
| 839 | logbuf_assert("Action \"invalid\" is placed inside Notification."); |
| 840 | |
| 841 | assert_null(lys_parse_mem(ctx, "module hh {yang-version 1.1; namespace urn:hh;prefix hh; notification test {container a {uses grp;}}" |
| 842 | "grouping grp {action invalid {input {leaf x {type string;}}}}}", LYS_IN_YANG)); |
| 843 | logbuf_assert("Action \"invalid\" is placed inside Notification."); |
| 844 | |
| 845 | *state = NULL; |
| 846 | ly_ctx_destroy(ctx, NULL); |
| 847 | } |
| 848 | |
| 849 | static void |
| 850 | test_notification(void **state) |
| 851 | { |
| 852 | *state = test_notification; |
| 853 | |
| 854 | struct ly_ctx *ctx; |
| 855 | struct lys_module *mod; |
| 856 | const struct lysc_notif *notif; |
| 857 | |
| 858 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 859 | |
| 860 | assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;" |
| 861 | "notification a1 {leaf x {type int8;}} notification a2;}", LYS_IN_YANG)); |
| 862 | notif = mod->compiled->notifs; |
| 863 | assert_non_null(notif); |
| 864 | assert_int_equal(2, LY_ARRAY_SIZE(notif)); |
| 865 | assert_int_equal(LYS_NOTIF, notif->nodetype); |
| 866 | assert_int_equal(LYS_STATUS_CURR, notif->flags); |
| 867 | assert_string_equal("a1", notif->name); |
| 868 | assert_non_null(notif->data); |
| 869 | assert_string_equal("x", notif->data->name); |
| 870 | assert_int_equal(LYS_NOTIF, notif[1].nodetype); |
| 871 | assert_int_equal(LYS_STATUS_CURR, notif[1].flags); |
| 872 | assert_string_equal("a2", notif[1].name); |
| 873 | assert_null(notif[1].data); |
| 874 | |
| 875 | assert_non_null(mod = lys_parse_mem(ctx, "module b {yang-version 1.1; namespace urn:b;prefix b; container top {" |
| 876 | "notification b1 {leaf x {type int8;}} notification b2;}}", LYS_IN_YANG)); |
| 877 | notif = lysc_node_notifs(mod->compiled->data); |
| 878 | assert_non_null(notif); |
| 879 | assert_int_equal(2, LY_ARRAY_SIZE(notif)); |
| 880 | assert_int_equal(LYS_NOTIF, notif->nodetype); |
| 881 | assert_int_equal(LYS_STATUS_CURR, notif->flags); |
| 882 | assert_string_equal("b1", notif->name); |
| 883 | assert_non_null(notif->data); |
| 884 | assert_string_equal("x", notif->data->name); |
| 885 | assert_int_equal(LYS_NOTIF, notif[1].nodetype); |
| 886 | assert_int_equal(LYS_STATUS_CURR, notif[1].flags); |
| 887 | assert_string_equal("b2", notif[1].name); |
| 888 | assert_null(notif[1].data); |
| 889 | |
| 890 | /* invalid */ |
| 891 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;container top {notification x;}}", LYS_IN_YANG)); |
| 892 | logbuf_assert("Invalid keyword \"notification\" as a child of \"container\" - the statement is allowed only in YANG 1.1 modules. Line number 1."); |
| 893 | |
| 894 | assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;leaf x{type string;} notification x;}", LYS_IN_YANG)); |
| 895 | logbuf_assert("Duplicate identifier \"x\" of data definition/RPC/action/Notification statement."); |
| 896 | assert_null(lys_parse_mem(ctx, "module cc {yang-version 1.1; namespace urn:cc;prefix cc;container c {leaf y {type string;} notification y;}}", LYS_IN_YANG)); |
| 897 | logbuf_assert("Duplicate identifier \"y\" of data definition/RPC/action/Notification statement."); |
| 898 | assert_null(lys_parse_mem(ctx, "module dd {yang-version 1.1; namespace urn:dd;prefix dd;container c {notification z; notification z;}}", LYS_IN_YANG)); |
| 899 | logbuf_assert("Duplicate identifier \"z\" of data definition/RPC/action/Notification statement."); |
| 900 | ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule eesub {belongs-to ee {prefix ee;} rpc w;}"); |
| 901 | assert_null(lys_parse_mem(ctx, "module ee {yang-version 1.1; namespace urn:ee;prefix ee;include eesub; notification w;}", LYS_IN_YANG)); |
| 902 | logbuf_assert("Duplicate identifier \"w\" of data definition/RPC/action/Notification statement."); |
| 903 | |
| 904 | assert_null(lys_parse_mem(ctx, "module ff {yang-version 1.1; namespace urn:ff;prefix ff; rpc test {input {container a {leaf b {type string;}}}}" |
| 905 | "augment /test/input/a {notification invalid {leaf x {type string;}}}}", LYS_IN_YANG)); |
| 906 | logbuf_assert("Notification \"invalid\" is placed inside RPC/action."); |
| 907 | |
| 908 | assert_null(lys_parse_mem(ctx, "module gg {yang-version 1.1; namespace urn:gg;prefix gg; notification test {container a {leaf b {type string;}}}" |
| 909 | "augment /test/a {notification invalid {leaf x {type string;}}}}", LYS_IN_YANG)); |
| 910 | logbuf_assert("Notification \"invalid\" is placed inside another Notification."); |
| 911 | |
| 912 | assert_null(lys_parse_mem(ctx, "module hh {yang-version 1.1; namespace urn:hh;prefix hh; rpc test {input {container a {uses grp;}}}" |
| 913 | "grouping grp {notification invalid {leaf x {type string;}}}}", LYS_IN_YANG)); |
| 914 | logbuf_assert("Notification \"invalid\" is placed inside RPC/action."); |
| 915 | |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 916 | *state = NULL; |
| 917 | ly_ctx_destroy(ctx, NULL); |
| 918 | } |
| 919 | |
Radek Krejci | 0f07bed | 2018-11-13 14:01:40 +0100 | [diff] [blame] | 920 | /** |
| 921 | * actually the same as length restriction (tested in test_type_length()), so just check the correct handling in appropriate types, |
| 922 | * do not test the expression itself |
| 923 | */ |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 924 | static void |
Radek Krejci | 0f07bed | 2018-11-13 14:01:40 +0100 | [diff] [blame] | 925 | test_type_range(void **state) |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 926 | { |
Radek Krejci | 0f07bed | 2018-11-13 14:01:40 +0100 | [diff] [blame] | 927 | *state = test_type_range; |
| 928 | |
| 929 | struct ly_ctx *ctx; |
| 930 | struct lys_module *mod; |
| 931 | struct lysc_type *type; |
| 932 | |
| 933 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 934 | |
| 935 | 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] | 936 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 937 | assert_non_null(type); |
| 938 | assert_int_equal(LY_TYPE_INT8, type->basetype); |
| 939 | assert_non_null(((struct lysc_type_num*)type)->range); |
| 940 | assert_non_null(((struct lysc_type_num*)type)->range->parts); |
| 941 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts)); |
| 942 | assert_int_equal(-128, ((struct lysc_type_num*)type)->range->parts[0].min_64); |
| 943 | assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64); |
| 944 | assert_int_equal(127, ((struct lysc_type_num*)type)->range->parts[1].min_64); |
| 945 | assert_int_equal(127, ((struct lysc_type_num*)type)->range->parts[1].max_64); |
| 946 | |
| 947 | 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] | 948 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 949 | assert_non_null(type); |
| 950 | assert_int_equal(LY_TYPE_INT16, type->basetype); |
| 951 | assert_non_null(((struct lysc_type_num*)type)->range); |
| 952 | assert_non_null(((struct lysc_type_num*)type)->range->parts); |
| 953 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts)); |
| 954 | assert_int_equal(-32768, ((struct lysc_type_num*)type)->range->parts[0].min_64); |
| 955 | assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64); |
| 956 | assert_int_equal(32767, ((struct lysc_type_num*)type)->range->parts[1].min_64); |
| 957 | assert_int_equal(32767, ((struct lysc_type_num*)type)->range->parts[1].max_64); |
| 958 | |
| 959 | 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] | 960 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 961 | assert_non_null(type); |
| 962 | assert_int_equal(LY_TYPE_INT32, type->basetype); |
| 963 | assert_non_null(((struct lysc_type_num*)type)->range); |
| 964 | assert_non_null(((struct lysc_type_num*)type)->range->parts); |
| 965 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts)); |
| 966 | assert_int_equal(INT64_C(-2147483648), ((struct lysc_type_num*)type)->range->parts[0].min_64); |
| 967 | assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64); |
| 968 | assert_int_equal(INT64_C(2147483647), ((struct lysc_type_num*)type)->range->parts[1].min_64); |
| 969 | assert_int_equal(INT64_C(2147483647), ((struct lysc_type_num*)type)->range->parts[1].max_64); |
| 970 | |
| 971 | 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] | 972 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 973 | assert_non_null(type); |
| 974 | assert_int_equal(LY_TYPE_INT64, type->basetype); |
| 975 | assert_non_null(((struct lysc_type_num*)type)->range); |
| 976 | assert_non_null(((struct lysc_type_num*)type)->range->parts); |
| 977 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts)); |
| 978 | assert_int_equal(INT64_C(-9223372036854775807) - INT64_C(1), ((struct lysc_type_num*)type)->range->parts[0].min_64); |
| 979 | assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64); |
| 980 | assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_num*)type)->range->parts[1].min_64); |
| 981 | assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_num*)type)->range->parts[1].max_64); |
| 982 | |
| 983 | 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] | 984 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 985 | assert_non_null(type); |
| 986 | assert_int_equal(LY_TYPE_UINT8, type->basetype); |
| 987 | assert_non_null(((struct lysc_type_num*)type)->range); |
| 988 | assert_non_null(((struct lysc_type_num*)type)->range->parts); |
| 989 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts)); |
| 990 | assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64); |
| 991 | assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64); |
| 992 | assert_int_equal(255, ((struct lysc_type_num*)type)->range->parts[1].min_u64); |
| 993 | assert_int_equal(255, ((struct lysc_type_num*)type)->range->parts[1].max_u64); |
| 994 | |
| 995 | 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] | 996 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 997 | assert_non_null(type); |
| 998 | assert_int_equal(LY_TYPE_UINT16, type->basetype); |
| 999 | assert_non_null(((struct lysc_type_num*)type)->range); |
| 1000 | assert_non_null(((struct lysc_type_num*)type)->range->parts); |
| 1001 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts)); |
| 1002 | assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64); |
| 1003 | assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64); |
| 1004 | assert_int_equal(65535, ((struct lysc_type_num*)type)->range->parts[1].min_u64); |
| 1005 | assert_int_equal(65535, ((struct lysc_type_num*)type)->range->parts[1].max_u64); |
| 1006 | |
| 1007 | 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] | 1008 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1009 | assert_non_null(type); |
| 1010 | assert_int_equal(LY_TYPE_UINT32, type->basetype); |
| 1011 | assert_non_null(((struct lysc_type_num*)type)->range); |
| 1012 | assert_non_null(((struct lysc_type_num*)type)->range->parts); |
| 1013 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts)); |
| 1014 | assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64); |
| 1015 | assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64); |
| 1016 | assert_int_equal(UINT64_C(4294967295), ((struct lysc_type_num*)type)->range->parts[1].min_u64); |
| 1017 | assert_int_equal(UINT64_C(4294967295), ((struct lysc_type_num*)type)->range->parts[1].max_u64); |
| 1018 | |
| 1019 | 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] | 1020 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1021 | assert_non_null(type); |
| 1022 | assert_int_equal(LY_TYPE_UINT64, type->basetype); |
| 1023 | assert_non_null(((struct lysc_type_num*)type)->range); |
| 1024 | assert_non_null(((struct lysc_type_num*)type)->range->parts); |
| 1025 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts)); |
| 1026 | assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64); |
| 1027 | assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64); |
| 1028 | assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_num*)type)->range->parts[1].min_u64); |
| 1029 | assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_num*)type)->range->parts[1].max_u64); |
| 1030 | |
| 1031 | assert_non_null(mod = lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;typedef mytype {type uint8 {range 10..100;}}" |
| 1032 | "typedef mytype2 {type mytype;} leaf l {type mytype2;}}", LYS_IN_YANG)); |
Radek Krejci | 0f07bed | 2018-11-13 14:01:40 +0100 | [diff] [blame] | 1033 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1034 | assert_non_null(type); |
| 1035 | assert_int_equal(3, type->refcount); |
| 1036 | assert_int_equal(LY_TYPE_UINT8, type->basetype); |
| 1037 | assert_non_null(((struct lysc_type_num*)type)->range); |
| 1038 | assert_non_null(((struct lysc_type_num*)type)->range->parts); |
| 1039 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts)); |
| 1040 | |
Radek Krejci | f139404 | 2019-01-08 10:53:34 +0100 | [diff] [blame] | 1041 | assert_non_null(mod = lys_parse_mem(ctx, "module j {namespace urn:j;prefix j;" |
| 1042 | "typedef mytype {type uint8 {range 1..100{description \"one to hundred\";reference A;}}}" |
| 1043 | "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] | 1044 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1045 | assert_non_null(type); |
| 1046 | assert_int_equal(1, type->refcount); |
| 1047 | assert_int_equal(LY_TYPE_UINT8, type->basetype); |
| 1048 | assert_non_null(((struct lysc_type_num*)type)->range); |
| 1049 | assert_string_equal("one to ten", ((struct lysc_type_num*)type)->range->dsc); |
| 1050 | assert_string_equal("B", ((struct lysc_type_num*)type)->range->ref); |
| 1051 | assert_non_null(((struct lysc_type_num*)type)->range->parts); |
| 1052 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts)); |
| 1053 | assert_int_equal(1, ((struct lysc_type_num*)type)->range->parts[0].min_u64); |
| 1054 | assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64); |
| 1055 | |
Radek Krejci | 0f07bed | 2018-11-13 14:01:40 +0100 | [diff] [blame] | 1056 | *state = NULL; |
| 1057 | ly_ctx_destroy(ctx, NULL); |
| 1058 | } |
| 1059 | |
| 1060 | static void |
| 1061 | test_type_length(void **state) |
| 1062 | { |
| 1063 | *state = test_type_length; |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 1064 | |
| 1065 | struct ly_ctx *ctx; |
| 1066 | struct lys_module *mod; |
| 1067 | struct lysc_type *type; |
| 1068 | |
| 1069 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 1070 | |
Radek Krejci | c5ddcc0 | 2018-11-12 13:28:18 +0100 | [diff] [blame] | 1071 | 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] | 1072 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1073 | assert_non_null(type); |
| 1074 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 1075 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
Radek Krejci | c5ddcc0 | 2018-11-12 13:28:18 +0100 | [diff] [blame] | 1076 | assert_string_equal("errortag", ((struct lysc_type_bin*)type)->length->eapptag); |
| 1077 | assert_string_equal("error", ((struct lysc_type_bin*)type)->length->emsg); |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 1078 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 1079 | assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
| 1080 | assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
| 1081 | |
| 1082 | 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] | 1083 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1084 | assert_non_null(type); |
| 1085 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 1086 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
| 1087 | 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] | 1088 | assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
| 1089 | 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] | 1090 | |
| 1091 | 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] | 1092 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1093 | assert_non_null(type); |
| 1094 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 1095 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
| 1096 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 1097 | 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] | 1098 | 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] | 1099 | |
| 1100 | 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] | 1101 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1102 | assert_non_null(type); |
| 1103 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 1104 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
| 1105 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 1106 | assert_int_equal(5, ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
| 1107 | assert_int_equal(5, ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
| 1108 | |
| 1109 | 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] | 1110 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1111 | assert_non_null(type); |
| 1112 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 1113 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
| 1114 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 1115 | assert_int_equal(1, ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
| 1116 | assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
| 1117 | |
| 1118 | 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] | 1119 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1120 | assert_non_null(type); |
| 1121 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 1122 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
| 1123 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 1124 | assert_int_equal(1, ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
| 1125 | assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
| 1126 | assert_int_equal(20, ((struct lysc_type_bin*)type)->length->parts[1].min_u64); |
| 1127 | assert_int_equal(30, ((struct lysc_type_bin*)type)->length->parts[1].max_u64); |
| 1128 | |
| 1129 | 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] | 1130 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1131 | assert_non_null(type); |
| 1132 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 1133 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
| 1134 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 1135 | assert_int_equal(16, ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
| 1136 | assert_int_equal(16, ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
| 1137 | assert_int_equal(32, ((struct lysc_type_bin*)type)->length->parts[1].min_u64); |
| 1138 | assert_int_equal(32, ((struct lysc_type_bin*)type)->length->parts[1].max_u64); |
| 1139 | |
Radek Krejci | b31c899 | 2018-11-12 16:29:16 +0100 | [diff] [blame] | 1140 | assert_non_null(mod = lys_parse_mem(ctx, "module h {namespace urn:h;prefix h;typedef mytype {type binary {length 10;}}" |
| 1141 | "leaf l {type mytype {length \"10\";}}}", LYS_IN_YANG)); |
Radek Krejci | b31c899 | 2018-11-12 16:29:16 +0100 | [diff] [blame] | 1142 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1143 | assert_non_null(type); |
| 1144 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 1145 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
| 1146 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 1147 | assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
| 1148 | assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
| 1149 | |
| 1150 | assert_non_null(mod = lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;typedef mytype {type binary {length 10..100;}}" |
| 1151 | "leaf l {type mytype {length \"50\";}}}", LYS_IN_YANG)); |
Radek Krejci | b31c899 | 2018-11-12 16:29:16 +0100 | [diff] [blame] | 1152 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1153 | assert_non_null(type); |
| 1154 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 1155 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
| 1156 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 1157 | assert_int_equal(50, ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
| 1158 | assert_int_equal(50, ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
| 1159 | |
| 1160 | assert_non_null(mod = lys_parse_mem(ctx, "module j {namespace urn:j;prefix j;typedef mytype {type binary {length 10..100;}}" |
| 1161 | "leaf l {type mytype {length \"10..30|60..100\";}}}", LYS_IN_YANG)); |
Radek Krejci | b31c899 | 2018-11-12 16:29:16 +0100 | [diff] [blame] | 1162 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1163 | assert_non_null(type); |
| 1164 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 1165 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
| 1166 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 1167 | assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
| 1168 | assert_int_equal(30, ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
| 1169 | assert_int_equal(60, ((struct lysc_type_bin*)type)->length->parts[1].min_u64); |
| 1170 | assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[1].max_u64); |
| 1171 | |
| 1172 | 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] | 1173 | "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] | 1174 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1175 | assert_non_null(type); |
Radek Krejci | 56aa27c | 2018-11-13 14:21:59 +0100 | [diff] [blame] | 1176 | assert_int_equal(1, type->refcount); |
Radek Krejci | b31c899 | 2018-11-12 16:29:16 +0100 | [diff] [blame] | 1177 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 1178 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
| 1179 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 1180 | 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] | 1181 | 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] | 1182 | type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type; |
| 1183 | assert_non_null(type); |
Radek Krejci | 56aa27c | 2018-11-13 14:21:59 +0100 | [diff] [blame] | 1184 | assert_int_equal(2, type->refcount); |
Radek Krejci | b31c899 | 2018-11-12 16:29:16 +0100 | [diff] [blame] | 1185 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 1186 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
| 1187 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 1188 | assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
| 1189 | assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
| 1190 | |
Radek Krejci | 56aa27c | 2018-11-13 14:21:59 +0100 | [diff] [blame] | 1191 | assert_non_null(mod = lys_parse_mem(ctx, "module l {namespace urn:l;prefix l;typedef mytype {type string {length 10..100;}}" |
| 1192 | "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] | 1193 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1194 | assert_non_null(type); |
| 1195 | assert_int_equal(LY_TYPE_STRING, type->basetype); |
Radek Krejci | 56aa27c | 2018-11-13 14:21:59 +0100 | [diff] [blame] | 1196 | assert_int_equal(1, type->refcount); |
Radek Krejci | cda7415 | 2018-11-13 15:27:32 +0100 | [diff] [blame] | 1197 | assert_non_null(((struct lysc_type_str*)type)->length); |
| 1198 | assert_non_null(((struct lysc_type_str*)type)->length->parts); |
| 1199 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->length->parts)); |
| 1200 | assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].min_u64); |
| 1201 | 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] | 1202 | |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1203 | assert_non_null(mod = lys_parse_mem(ctx, "module m {namespace urn:m;prefix m;typedef mytype {type string {length 10;}}" |
| 1204 | "leaf l {type mytype {length min..max;}}}", LYS_IN_YANG)); |
Radek Krejci | 5969f27 | 2018-11-23 10:03:58 +0100 | [diff] [blame] | 1205 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1206 | assert_non_null(type); |
| 1207 | assert_int_equal(LY_TYPE_STRING, type->basetype); |
| 1208 | assert_int_equal(1, type->refcount); |
| 1209 | assert_non_null(((struct lysc_type_str*)type)->length); |
| 1210 | assert_non_null(((struct lysc_type_str*)type)->length->parts); |
| 1211 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->length->parts)); |
| 1212 | assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].min_u64); |
| 1213 | assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].max_u64); |
| 1214 | |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 1215 | /* invalid values */ |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1216 | 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] | 1217 | 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] | 1218 | 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] | 1219 | logbuf_assert("Invalid length restriction - invalid value \"18446744073709551616\"."); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1220 | 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] | 1221 | logbuf_assert("Invalid length restriction - unexpected data after max keyword (.. 10)."); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1222 | 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] | 1223 | logbuf_assert("Invalid length restriction - values are not in ascending order (10)."); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1224 | 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] | 1225 | logbuf_assert("Invalid length restriction - values are not in ascending order (10)."); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1226 | 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] | 1227 | logbuf_assert("Invalid length restriction - unexpected data (x)."); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1228 | 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] | 1229 | logbuf_assert("Invalid length restriction - unexpected data before min keyword (50 | )."); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1230 | 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] | 1231 | logbuf_assert("Invalid length restriction - unexpected beginning of the expression (| 50)."); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1232 | 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] | 1233 | logbuf_assert("Invalid length restriction - unexpected end of the expression after \"..\" (10 ..)."); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1234 | 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] | 1235 | logbuf_assert("Invalid length restriction - unexpected \"..\" without a lower bound."); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1236 | 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] | 1237 | logbuf_assert("Invalid length restriction - unexpected end of the expression (10 |)."); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1238 | 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] | 1239 | logbuf_assert("Invalid length restriction - values are not in ascending order (15)."); |
Radek Krejci | b31c899 | 2018-11-12 16:29:16 +0100 | [diff] [blame] | 1240 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1241 | assert_null(lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;typedef mytype {type binary {length 10;}}" |
| 1242 | "leaf l {type mytype {length 11;}}}", LYS_IN_YANG)); |
Radek Krejci | b31c899 | 2018-11-12 16:29:16 +0100 | [diff] [blame] | 1243 | 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] | 1244 | assert_null(lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;typedef mytype {type binary {length 10..100;}}" |
| 1245 | "leaf l {type mytype {length 1..11;}}}", LYS_IN_YANG)); |
Radek Krejci | b31c899 | 2018-11-12 16:29:16 +0100 | [diff] [blame] | 1246 | 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] | 1247 | assert_null(lys_parse_mem(ctx, "module nn {namespace urn:nn;prefix nn;typedef mytype {type binary {length 10..100;}}" |
| 1248 | "leaf l {type mytype {length 20..110;}}}", LYS_IN_YANG)); |
Radek Krejci | b31c899 | 2018-11-12 16:29:16 +0100 | [diff] [blame] | 1249 | 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] | 1250 | assert_null(lys_parse_mem(ctx, "module oo {namespace urn:oo;prefix oo;typedef mytype {type binary {length 10..100;}}" |
| 1251 | "leaf l {type mytype {length 20..30|110..120;}}}", LYS_IN_YANG)); |
Radek Krejci | b31c899 | 2018-11-12 16:29:16 +0100 | [diff] [blame] | 1252 | 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] | 1253 | 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] | 1254 | "leaf l {type mytype {length 15;}}}", LYS_IN_YANG)); |
Radek Krejci | b31c899 | 2018-11-12 16:29:16 +0100 | [diff] [blame] | 1255 | 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] | 1256 | 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] | 1257 | "leaf l {type mytype {length 15..35;}}}", LYS_IN_YANG)); |
Radek Krejci | b31c899 | 2018-11-12 16:29:16 +0100 | [diff] [blame] | 1258 | 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] | 1259 | 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] | 1260 | "leaf l {type mytype {length 10..35;}}}", LYS_IN_YANG)); |
Radek Krejci | 6489bbe | 2018-11-12 17:05:19 +0100 | [diff] [blame] | 1261 | 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] | 1262 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1263 | 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] | 1264 | logbuf_assert("Invalid type restrictions for binary type."); |
| 1265 | |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 1266 | *state = NULL; |
| 1267 | ly_ctx_destroy(ctx, NULL); |
| 1268 | } |
| 1269 | |
Radek Krejci | cda7415 | 2018-11-13 15:27:32 +0100 | [diff] [blame] | 1270 | static void |
| 1271 | test_type_pattern(void **state) |
| 1272 | { |
| 1273 | *state = test_type_pattern; |
| 1274 | |
| 1275 | struct ly_ctx *ctx; |
| 1276 | struct lys_module *mod; |
| 1277 | struct lysc_type *type; |
| 1278 | |
| 1279 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 1280 | |
Radek Krejci | 027d580 | 2018-11-14 16:57:28 +0100 | [diff] [blame] | 1281 | 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] | 1282 | "pattern .* {error-app-tag errortag;error-message error;}" |
| 1283 | "pattern [0-9].*[0-9] {modifier invert-match;}}}}", LYS_IN_YANG)); |
Radek Krejci | cda7415 | 2018-11-13 15:27:32 +0100 | [diff] [blame] | 1284 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1285 | assert_non_null(type); |
| 1286 | assert_non_null(((struct lysc_type_str*)type)->patterns); |
| 1287 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns)); |
| 1288 | assert_string_equal("errortag", ((struct lysc_type_str*)type)->patterns[0]->eapptag); |
| 1289 | assert_string_equal("error", ((struct lysc_type_str*)type)->patterns[0]->emsg); |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1290 | assert_string_equal(".*", ((struct lysc_type_str*)type)->patterns[0]->expr); |
Radek Krejci | cda7415 | 2018-11-13 15:27:32 +0100 | [diff] [blame] | 1291 | assert_int_equal(0, ((struct lysc_type_str*)type)->patterns[0]->inverted); |
| 1292 | assert_null(((struct lysc_type_str*)type)->patterns[1]->eapptag); |
| 1293 | assert_null(((struct lysc_type_str*)type)->patterns[1]->emsg); |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1294 | assert_string_equal("[0-9].*[0-9]", ((struct lysc_type_str*)type)->patterns[1]->expr); |
Radek Krejci | cda7415 | 2018-11-13 15:27:32 +0100 | [diff] [blame] | 1295 | assert_int_equal(1, ((struct lysc_type_str*)type)->patterns[1]->inverted); |
| 1296 | |
| 1297 | assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;typedef mytype {type string {pattern '[0-9]*';}}" |
| 1298 | "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] | 1299 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1300 | assert_non_null(type); |
| 1301 | assert_int_equal(LY_TYPE_STRING, type->basetype); |
| 1302 | assert_int_equal(1, type->refcount); |
| 1303 | assert_non_null(((struct lysc_type_str*)type)->patterns); |
| 1304 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns)); |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1305 | assert_string_equal("[0-9]*", ((struct lysc_type_str*)type)->patterns[0]->expr); |
Radek Krejci | cda7415 | 2018-11-13 15:27:32 +0100 | [diff] [blame] | 1306 | assert_int_equal(3, ((struct lysc_type_str*)type)->patterns[0]->refcount); |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1307 | assert_string_equal("[0-4]*", ((struct lysc_type_str*)type)->patterns[1]->expr); |
Radek Krejci | cda7415 | 2018-11-13 15:27:32 +0100 | [diff] [blame] | 1308 | assert_int_equal(1, ((struct lysc_type_str*)type)->patterns[1]->refcount); |
| 1309 | |
Radek Krejci | 04bc1e9 | 2018-11-14 14:28:13 +0100 | [diff] [blame] | 1310 | assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c;typedef mytype {type string {pattern '[0-9]*';}}" |
| 1311 | "leaf l {type mytype {length 10;}}}", LYS_IN_YANG)); |
Radek Krejci | 04bc1e9 | 2018-11-14 14:28:13 +0100 | [diff] [blame] | 1312 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1313 | assert_non_null(type); |
| 1314 | assert_int_equal(LY_TYPE_STRING, type->basetype); |
| 1315 | assert_int_equal(1, type->refcount); |
| 1316 | assert_non_null(((struct lysc_type_str*)type)->patterns); |
| 1317 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns)); |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1318 | assert_string_equal("[0-9]*", ((struct lysc_type_str*)type)->patterns[0]->expr); |
Radek Krejci | 04bc1e9 | 2018-11-14 14:28:13 +0100 | [diff] [blame] | 1319 | assert_int_equal(2, ((struct lysc_type_str*)type)->patterns[0]->refcount); |
| 1320 | |
Radek Krejci | cda7415 | 2018-11-13 15:27:32 +0100 | [diff] [blame] | 1321 | /* test substitutions */ |
Radek Krejci | 04bc1e9 | 2018-11-14 14:28:13 +0100 | [diff] [blame] | 1322 | 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] | 1323 | "pattern '^\\p{IsLatinExtended-A}$';}}}", LYS_IN_YANG)); |
Radek Krejci | cda7415 | 2018-11-13 15:27:32 +0100 | [diff] [blame] | 1324 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1325 | assert_non_null(type); |
| 1326 | assert_non_null(((struct lysc_type_str*)type)->patterns); |
| 1327 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns)); |
Radek Krejci | 5457946 | 2019-04-30 12:47:06 +0200 | [diff] [blame] | 1328 | assert_string_equal("^\\p{IsLatinExtended-A}$", ((struct lysc_type_str*)type)->patterns[0]->expr); |
Radek Krejci | cda7415 | 2018-11-13 15:27:32 +0100 | [diff] [blame] | 1329 | /* TODO check some data "^Å™$" */ |
| 1330 | |
| 1331 | *state = NULL; |
| 1332 | ly_ctx_destroy(ctx, NULL); |
| 1333 | } |
| 1334 | |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1335 | static void |
| 1336 | test_type_enum(void **state) |
| 1337 | { |
| 1338 | *state = test_type_enum; |
| 1339 | |
| 1340 | struct ly_ctx *ctx; |
| 1341 | struct lys_module *mod; |
| 1342 | struct lysc_type *type; |
| 1343 | |
| 1344 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 1345 | |
Radek Krejci | 027d580 | 2018-11-14 16:57:28 +0100 | [diff] [blame] | 1346 | 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] | 1347 | "enum automin; enum min {value -2147483648;}enum one {if-feature f; value 1;}" |
| 1348 | "enum two; enum seven {value 7;}enum eight;}}}", LYS_IN_YANG)); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1349 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1350 | assert_non_null(type); |
| 1351 | assert_int_equal(LY_TYPE_ENUM, type->basetype); |
| 1352 | assert_non_null(((struct lysc_type_enum*)type)->enums); |
| 1353 | assert_int_equal(6, LY_ARRAY_SIZE(((struct lysc_type_enum*)type)->enums)); |
| 1354 | assert_non_null(((struct lysc_type_enum*)type)->enums[2].iffeatures); |
| 1355 | assert_string_equal("automin", ((struct lysc_type_enum*)type)->enums[0].name); |
| 1356 | assert_int_equal(0, ((struct lysc_type_enum*)type)->enums[0].value); |
| 1357 | assert_string_equal("min", ((struct lysc_type_enum*)type)->enums[1].name); |
| 1358 | assert_int_equal(-2147483648, ((struct lysc_type_enum*)type)->enums[1].value); |
| 1359 | assert_string_equal("one", ((struct lysc_type_enum*)type)->enums[2].name); |
| 1360 | assert_int_equal(1, ((struct lysc_type_enum*)type)->enums[2].value); |
| 1361 | assert_string_equal("two", ((struct lysc_type_enum*)type)->enums[3].name); |
| 1362 | assert_int_equal(2, ((struct lysc_type_enum*)type)->enums[3].value); |
| 1363 | assert_string_equal("seven", ((struct lysc_type_enum*)type)->enums[4].name); |
| 1364 | assert_int_equal(7, ((struct lysc_type_enum*)type)->enums[4].value); |
| 1365 | assert_string_equal("eight", ((struct lysc_type_enum*)type)->enums[5].name); |
| 1366 | assert_int_equal(8, ((struct lysc_type_enum*)type)->enums[5].value); |
| 1367 | |
Radek Krejci | 027d580 | 2018-11-14 16:57:28 +0100 | [diff] [blame] | 1368 | 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 {" |
| 1369 | "enum 11; enum min {value -2147483648;}enum x$&;" |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1370 | "enum two; enum seven {value 7;}enum eight;}} leaf l { type mytype {enum seven;enum eight;}}}", |
| 1371 | LYS_IN_YANG)); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1372 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1373 | assert_non_null(type); |
| 1374 | assert_int_equal(LY_TYPE_ENUM, type->basetype); |
| 1375 | assert_non_null(((struct lysc_type_enum*)type)->enums); |
| 1376 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_enum*)type)->enums)); |
| 1377 | assert_string_equal("seven", ((struct lysc_type_enum*)type)->enums[0].name); |
| 1378 | assert_int_equal(7, ((struct lysc_type_enum*)type)->enums[0].value); |
| 1379 | assert_string_equal("eight", ((struct lysc_type_enum*)type)->enums[1].name); |
| 1380 | assert_int_equal(8, ((struct lysc_type_enum*)type)->enums[1].value); |
| 1381 | |
| 1382 | |
| 1383 | /* invalid cases */ |
Radek Krejci | 027d580 | 2018-11-14 16:57:28 +0100 | [diff] [blame] | 1384 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; feature f; leaf l {type enumeration {" |
| 1385 | "enum one {if-feature f;}}}}", LYS_IN_YANG)); |
| 1386 | 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] | 1387 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {" |
| 1388 | "enum one {value -2147483649;}}}}", LYS_IN_YANG)); |
| 1389 | logbuf_assert("Invalid value \"-2147483649\" of \"value\". Line number 1."); |
| 1390 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {" |
| 1391 | "enum one {value 2147483648;}}}}", LYS_IN_YANG)); |
| 1392 | logbuf_assert("Invalid value \"2147483648\" of \"value\". Line number 1."); |
| 1393 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {" |
| 1394 | "enum one; enum one;}}}", LYS_IN_YANG)); |
| 1395 | logbuf_assert("Duplicate identifier \"one\" of enum statement. Line number 1."); |
| 1396 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {" |
| 1397 | "enum '';}}}", LYS_IN_YANG)); |
| 1398 | logbuf_assert("Enum name must not be zero-length. Line number 1."); |
| 1399 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {" |
| 1400 | "enum ' x';}}}", LYS_IN_YANG)); |
| 1401 | logbuf_assert("Enum name must not have any leading or trailing whitespaces (\" x\"). Line number 1."); |
| 1402 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {" |
| 1403 | "enum 'x ';}}}", LYS_IN_YANG)); |
| 1404 | logbuf_assert("Enum name must not have any leading or trailing whitespaces (\"x \"). Line number 1."); |
| 1405 | assert_non_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {" |
| 1406 | "enum 'inva\nlid';}}}", LYS_IN_YANG)); |
| 1407 | logbuf_assert("Control characters in enum name should be avoided (\"inva\nlid\", character number 5)."); |
| 1408 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1409 | 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] | 1410 | logbuf_assert("Missing enum substatement for enumeration type."); |
| 1411 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1412 | 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] | 1413 | "leaf l {type mytype {enum two;}}}", LYS_IN_YANG)); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1414 | logbuf_assert("Invalid enumeration - derived type adds new item \"two\"."); |
| 1415 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1416 | 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] | 1417 | "leaf l {type mytype {enum one {value 1;}}}}", LYS_IN_YANG)); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1418 | 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] | 1419 | 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] | 1420 | LYS_IN_YANG)); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1421 | logbuf_assert("Invalid enumeration - it is not possible to auto-assign enum value for \"y\" since the highest value is already 2147483647."); |
| 1422 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1423 | 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] | 1424 | LYS_IN_YANG)); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1425 | logbuf_assert("Invalid enumeration - value 1 collide in items \"y\" and \"x\"."); |
| 1426 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1427 | 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] | 1428 | "leaf l {type mytype {enum one;}}}", LYS_IN_YANG)); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1429 | logbuf_assert("Missing enum substatement for enumeration type mytype."); |
Radek Krejci | 04bc1e9 | 2018-11-14 14:28:13 +0100 | [diff] [blame] | 1430 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1431 | 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] | 1432 | "leaf l {type mytype {enum one;}}}", LYS_IN_YANG)); |
Radek Krejci | 027d580 | 2018-11-14 16:57:28 +0100 | [diff] [blame] | 1433 | logbuf_assert("Enumeration type can be subtyped only in YANG 1.1 modules."); |
| 1434 | |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1435 | *state = NULL; |
| 1436 | ly_ctx_destroy(ctx, NULL); |
| 1437 | } |
| 1438 | |
| 1439 | static void |
| 1440 | test_type_bits(void **state) |
| 1441 | { |
| 1442 | *state = test_type_bits; |
| 1443 | |
| 1444 | struct ly_ctx *ctx; |
| 1445 | struct lys_module *mod; |
| 1446 | struct lysc_type *type; |
| 1447 | |
| 1448 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 1449 | |
Radek Krejci | 027d580 | 2018-11-14 16:57:28 +0100 | [diff] [blame] | 1450 | 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] | 1451 | "bit automin; bit one {if-feature f; position 1;}" |
| 1452 | "bit two; bit seven {position 7;}bit eight;}}}", LYS_IN_YANG)); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1453 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1454 | assert_non_null(type); |
| 1455 | assert_int_equal(LY_TYPE_BITS, type->basetype); |
| 1456 | assert_non_null(((struct lysc_type_bits*)type)->bits); |
| 1457 | assert_int_equal(5, LY_ARRAY_SIZE(((struct lysc_type_bits*)type)->bits)); |
| 1458 | assert_non_null(((struct lysc_type_bits*)type)->bits[1].iffeatures); |
| 1459 | assert_string_equal("automin", ((struct lysc_type_bits*)type)->bits[0].name); |
| 1460 | assert_int_equal(0, ((struct lysc_type_bits*)type)->bits[0].position); |
| 1461 | assert_string_equal("one", ((struct lysc_type_bits*)type)->bits[1].name); |
| 1462 | assert_int_equal(1, ((struct lysc_type_bits*)type)->bits[1].position); |
| 1463 | assert_string_equal("two", ((struct lysc_type_bits*)type)->bits[2].name); |
| 1464 | assert_int_equal(2, ((struct lysc_type_bits*)type)->bits[2].position); |
| 1465 | assert_string_equal("seven", ((struct lysc_type_bits*)type)->bits[3].name); |
| 1466 | assert_int_equal(7, ((struct lysc_type_bits*)type)->bits[3].position); |
| 1467 | assert_string_equal("eight", ((struct lysc_type_bits*)type)->bits[4].name); |
| 1468 | assert_int_equal(8, ((struct lysc_type_bits*)type)->bits[4].position); |
| 1469 | |
Radek Krejci | 027d580 | 2018-11-14 16:57:28 +0100 | [diff] [blame] | 1470 | 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 {" |
| 1471 | "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] | 1472 | LYS_IN_YANG)); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1473 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1474 | assert_non_null(type); |
| 1475 | assert_int_equal(LY_TYPE_BITS, type->basetype); |
| 1476 | assert_non_null(((struct lysc_type_bits*)type)->bits); |
Radek Krejci | 027d580 | 2018-11-14 16:57:28 +0100 | [diff] [blame] | 1477 | assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_bits*)type)->bits)); |
| 1478 | assert_string_equal("automin", ((struct lysc_type_bits*)type)->bits[0].name); |
| 1479 | assert_int_equal(0, ((struct lysc_type_bits*)type)->bits[0].position); |
| 1480 | assert_string_equal("seven", ((struct lysc_type_bits*)type)->bits[1].name); |
| 1481 | assert_int_equal(7, ((struct lysc_type_bits*)type)->bits[1].position); |
| 1482 | assert_string_equal("eight", ((struct lysc_type_bits*)type)->bits[2].name); |
| 1483 | assert_int_equal(8, ((struct lysc_type_bits*)type)->bits[2].position); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1484 | |
| 1485 | |
| 1486 | /* invalid cases */ |
Radek Krejci | 027d580 | 2018-11-14 16:57:28 +0100 | [diff] [blame] | 1487 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; feature f; leaf l {type bits {" |
| 1488 | "bit one {if-feature f;}}}}", LYS_IN_YANG)); |
| 1489 | 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] | 1490 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {" |
| 1491 | "bit one {position -1;}}}}", LYS_IN_YANG)); |
| 1492 | logbuf_assert("Invalid value \"-1\" of \"position\". Line number 1."); |
| 1493 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {" |
| 1494 | "bit one {value 4294967296;}}}}", LYS_IN_YANG)); |
| 1495 | logbuf_assert("Invalid value \"4294967296\" of \"value\". Line number 1."); |
| 1496 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {" |
| 1497 | "bit one; bit one;}}}", LYS_IN_YANG)); |
| 1498 | logbuf_assert("Duplicate identifier \"one\" of bit statement. Line number 1."); |
| 1499 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {" |
| 1500 | "bit '11';}}}", LYS_IN_YANG)); |
| 1501 | logbuf_assert("Invalid identifier first character '1'. Line number 1."); |
| 1502 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {" |
| 1503 | "bit 'x1$1';}}}", LYS_IN_YANG)); |
| 1504 | logbuf_assert("Invalid identifier character '$'. Line number 1."); |
| 1505 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1506 | 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] | 1507 | logbuf_assert("Missing bit substatement for bits type."); |
| 1508 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1509 | 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] | 1510 | "leaf l {type mytype {bit two;}}}", LYS_IN_YANG)); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1511 | logbuf_assert("Invalid bits - derived type adds new item \"two\"."); |
| 1512 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1513 | 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] | 1514 | "leaf l {type mytype {bit one {position 1;}}}}", LYS_IN_YANG)); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1515 | 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] | 1516 | 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] | 1517 | LYS_IN_YANG)); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1518 | logbuf_assert("Invalid bits - it is not possible to auto-assign bit position for \"y\" since the highest value is already 4294967295."); |
| 1519 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1520 | 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] | 1521 | LYS_IN_YANG)); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1522 | logbuf_assert("Invalid bits - position 1 collide in items \"y\" and \"x\"."); |
| 1523 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1524 | 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] | 1525 | "leaf l {type mytype {bit one;}}}", LYS_IN_YANG)); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1526 | logbuf_assert("Missing bit substatement for bits type mytype."); |
Radek Krejci | 04bc1e9 | 2018-11-14 14:28:13 +0100 | [diff] [blame] | 1527 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1528 | 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] | 1529 | "leaf l {type mytype {bit one;}}}", LYS_IN_YANG)); |
Radek Krejci | 027d580 | 2018-11-14 16:57:28 +0100 | [diff] [blame] | 1530 | logbuf_assert("Bits type can be subtyped only in YANG 1.1 modules."); |
| 1531 | |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1532 | *state = NULL; |
| 1533 | ly_ctx_destroy(ctx, NULL); |
| 1534 | } |
| 1535 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1536 | static void |
| 1537 | test_type_dec64(void **state) |
| 1538 | { |
| 1539 | *state = test_type_dec64; |
| 1540 | |
| 1541 | struct ly_ctx *ctx; |
| 1542 | struct lys_module *mod; |
| 1543 | struct lysc_type *type; |
| 1544 | |
| 1545 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 1546 | |
| 1547 | assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;leaf l {type decimal64 {" |
| 1548 | "fraction-digits 2;range min..max;}}}", LYS_IN_YANG)); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1549 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1550 | assert_non_null(type); |
| 1551 | assert_int_equal(LY_TYPE_DEC64, type->basetype); |
| 1552 | assert_int_equal(2, ((struct lysc_type_dec*)type)->fraction_digits); |
| 1553 | assert_non_null(((struct lysc_type_dec*)type)->range); |
| 1554 | assert_non_null(((struct lysc_type_dec*)type)->range->parts); |
| 1555 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_dec*)type)->range->parts)); |
| 1556 | assert_int_equal(INT64_C(-9223372036854775807) - INT64_C(1), ((struct lysc_type_dec*)type)->range->parts[0].min_64); |
| 1557 | assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_dec*)type)->range->parts[0].max_64); |
| 1558 | |
| 1559 | assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;typedef mytype {type decimal64 {" |
| 1560 | "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] | 1561 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1562 | assert_non_null(type); |
| 1563 | assert_int_equal(LY_TYPE_DEC64, type->basetype); |
| 1564 | assert_int_equal(2, ((struct lysc_type_dec*)type)->fraction_digits); |
| 1565 | assert_non_null(((struct lysc_type_dec*)type)->range); |
| 1566 | assert_non_null(((struct lysc_type_dec*)type)->range->parts); |
| 1567 | assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_dec*)type)->range->parts)); |
| 1568 | assert_int_equal(314, ((struct lysc_type_dec*)type)->range->parts[0].min_64); |
| 1569 | assert_int_equal(314, ((struct lysc_type_dec*)type)->range->parts[0].max_64); |
| 1570 | assert_int_equal(510, ((struct lysc_type_dec*)type)->range->parts[1].min_64); |
| 1571 | assert_int_equal(510, ((struct lysc_type_dec*)type)->range->parts[1].max_64); |
| 1572 | assert_int_equal(1000, ((struct lysc_type_dec*)type)->range->parts[2].min_64); |
| 1573 | assert_int_equal(1000, ((struct lysc_type_dec*)type)->range->parts[2].max_64); |
| 1574 | |
| 1575 | /* invalid cases */ |
| 1576 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64 {fraction-digits 0;}}}", LYS_IN_YANG)); |
| 1577 | logbuf_assert("Invalid value \"0\" of \"fraction-digits\". Line number 1."); |
| 1578 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64 {fraction-digits -1;}}}", LYS_IN_YANG)); |
| 1579 | logbuf_assert("Invalid value \"-1\" of \"fraction-digits\". Line number 1."); |
| 1580 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type decimal64 {fraction-digits 19;}}}", LYS_IN_YANG)); |
| 1581 | logbuf_assert("Value \"19\" is out of \"fraction-digits\" bounds. Line number 1."); |
| 1582 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1583 | 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] | 1584 | logbuf_assert("Missing fraction-digits substatement for decimal64 type."); |
| 1585 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1586 | 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] | 1587 | logbuf_assert("Missing fraction-digits substatement for decimal64 type mytype."); |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 1588 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1589 | 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] | 1590 | "range '3.142';}}}", LYS_IN_YANG)); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1591 | logbuf_assert("Range boundary \"3.142\" of decimal64 type exceeds defined number (2) of fraction digits."); |
| 1592 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1593 | 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] | 1594 | "range '4 | 3.14';}}}", LYS_IN_YANG)); |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1595 | logbuf_assert("Invalid range restriction - values are not in ascending order (3.14)."); |
| 1596 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1597 | 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] | 1598 | "leaf l {type mytype {fraction-digits 3;}}}", LYS_IN_YANG)); |
Radek Krejci | 643c824 | 2018-11-15 17:51:11 +0100 | [diff] [blame] | 1599 | logbuf_assert("Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type."); |
| 1600 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1601 | 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] | 1602 | "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] | 1603 | logbuf_assert("Invalid fraction-digits substatement for type \"mytype2\" not directly derived from decimal64 built-in type."); |
| 1604 | |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 1605 | *state = NULL; |
| 1606 | ly_ctx_destroy(ctx, NULL); |
| 1607 | } |
| 1608 | |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 1609 | static void |
| 1610 | test_type_instanceid(void **state) |
| 1611 | { |
| 1612 | *state = test_type_instanceid; |
| 1613 | |
| 1614 | struct ly_ctx *ctx; |
| 1615 | struct lys_module *mod; |
| 1616 | struct lysc_type *type; |
| 1617 | |
| 1618 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 1619 | |
| 1620 | assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;typedef mytype {type instance-identifier {require-instance false;}}" |
| 1621 | "leaf l1 {type instance-identifier {require-instance true;}}" |
| 1622 | "leaf l2 {type mytype;} leaf l3 {type instance-identifier;}}", LYS_IN_YANG)); |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 1623 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1624 | assert_non_null(type); |
| 1625 | assert_int_equal(LY_TYPE_INST, type->basetype); |
| 1626 | assert_int_equal(1, ((struct lysc_type_instanceid*)type)->require_instance); |
| 1627 | |
| 1628 | type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type; |
| 1629 | assert_non_null(type); |
| 1630 | assert_int_equal(LY_TYPE_INST, type->basetype); |
| 1631 | assert_int_equal(0, ((struct lysc_type_instanceid*)type)->require_instance); |
| 1632 | |
| 1633 | type = ((struct lysc_node_leaf*)mod->compiled->data->next->next)->type; |
| 1634 | assert_non_null(type); |
| 1635 | assert_int_equal(LY_TYPE_INST, type->basetype); |
| 1636 | assert_int_equal(1, ((struct lysc_type_instanceid*)type)->require_instance); |
| 1637 | |
| 1638 | /* invalid cases */ |
| 1639 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type instance-identifier {require-instance yes;}}}", LYS_IN_YANG)); |
| 1640 | logbuf_assert("Invalid value \"yes\" of \"require-instance\". Line number 1."); |
| 1641 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1642 | 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] | 1643 | logbuf_assert("Invalid type restrictions for instance-identifier type."); |
| 1644 | |
| 1645 | *state = NULL; |
| 1646 | ly_ctx_destroy(ctx, NULL); |
| 1647 | } |
| 1648 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1649 | static void |
| 1650 | test_type_identityref(void **state) |
| 1651 | { |
| 1652 | *state = test_type_identityref; |
| 1653 | |
| 1654 | struct ly_ctx *ctx; |
| 1655 | struct lys_module *mod; |
| 1656 | struct lysc_type *type; |
| 1657 | |
| 1658 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 1659 | |
| 1660 | 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;}" |
| 1661 | "typedef mytype {type identityref {base i;}}" |
Radek Krejci | b83ea3e | 2018-11-23 14:29:13 +0100 | [diff] [blame] | 1662 | "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] | 1663 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1664 | assert_non_null(type); |
| 1665 | assert_int_equal(LY_TYPE_IDENT, type->basetype); |
| 1666 | assert_non_null(((struct lysc_type_identityref*)type)->bases); |
| 1667 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_identityref*)type)->bases)); |
| 1668 | assert_string_equal("i", ((struct lysc_type_identityref*)type)->bases[0]->name); |
| 1669 | |
| 1670 | type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type; |
| 1671 | assert_non_null(type); |
| 1672 | assert_int_equal(LY_TYPE_IDENT, type->basetype); |
| 1673 | assert_non_null(((struct lysc_type_identityref*)type)->bases); |
| 1674 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_identityref*)type)->bases)); |
| 1675 | assert_string_equal("k", ((struct lysc_type_identityref*)type)->bases[0]->name); |
| 1676 | assert_string_equal("j", ((struct lysc_type_identityref*)type)->bases[1]->name); |
| 1677 | |
Radek Krejci | b83ea3e | 2018-11-23 14:29:13 +0100 | [diff] [blame] | 1678 | assert_non_null(mod = lys_parse_mem(ctx, "module b {yang-version 1.1;namespace urn:b;prefix b;import a {prefix a;}" |
| 1679 | "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] | 1680 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1681 | assert_non_null(type); |
| 1682 | assert_int_equal(LY_TYPE_IDENT, type->basetype); |
| 1683 | assert_non_null(((struct lysc_type_identityref*)type)->bases); |
| 1684 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_identityref*)type)->bases)); |
| 1685 | assert_string_equal("k", ((struct lysc_type_identityref*)type)->bases[0]->name); |
| 1686 | assert_string_equal("j", ((struct lysc_type_identityref*)type)->bases[1]->name); |
| 1687 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1688 | /* invalid cases */ |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1689 | 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] | 1690 | logbuf_assert("Missing base substatement for identityref type."); |
| 1691 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1692 | 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] | 1693 | "leaf l {type mytype;}}", LYS_IN_YANG)); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1694 | logbuf_assert("Missing base substatement for identityref type mytype."); |
| 1695 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1696 | 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] | 1697 | "leaf l {type mytype {base i;}}}", LYS_IN_YANG)); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 1698 | 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] | 1699 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1700 | 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] | 1701 | "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] | 1702 | 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] | 1703 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1704 | 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] | 1705 | "leaf l {type identityref {base i;base j;}}}", LYS_IN_YANG)); |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1706 | logbuf_assert("Multiple bases in identityref type are allowed only in YANG 1.1 modules."); |
| 1707 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1708 | 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] | 1709 | logbuf_assert("Unable to find base (j) of identityref."); |
| 1710 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1711 | 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] | 1712 | logbuf_assert("Invalid prefix used for base (x:j) of identityref."); |
| 1713 | |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 1714 | *state = NULL; |
| 1715 | ly_ctx_destroy(ctx, NULL); |
| 1716 | } |
| 1717 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1718 | static void |
| 1719 | test_type_leafref(void **state) |
| 1720 | { |
| 1721 | *state = test_type_leafref; |
| 1722 | |
| 1723 | struct ly_ctx *ctx; |
| 1724 | struct lys_module *mod; |
| 1725 | struct lysc_type *type; |
| 1726 | const char *path, *name, *prefix; |
| 1727 | size_t prefix_len, name_len; |
| 1728 | int parent_times, has_predicate; |
| 1729 | |
| 1730 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 1731 | |
| 1732 | /* lys_path_token() */ |
| 1733 | path = "invalid_path"; |
| 1734 | parent_times = 0; |
| 1735 | assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)); |
| 1736 | path = ".."; |
| 1737 | parent_times = 0; |
| 1738 | assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)); |
| 1739 | path = "..["; |
| 1740 | parent_times = 0; |
| 1741 | assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)); |
| 1742 | path = "../"; |
| 1743 | parent_times = 0; |
| 1744 | assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)); |
| 1745 | path = "/"; |
| 1746 | parent_times = 0; |
| 1747 | assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)); |
| 1748 | |
| 1749 | path = "../../pref:id/xxx[predicate]/invalid!!!"; |
| 1750 | parent_times = 0; |
| 1751 | assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)); |
| 1752 | assert_string_equal("/xxx[predicate]/invalid!!!", path); |
| 1753 | assert_int_equal(4, prefix_len); |
| 1754 | assert_int_equal(0, strncmp("pref", prefix, prefix_len)); |
| 1755 | assert_int_equal(2, name_len); |
| 1756 | assert_int_equal(0, strncmp("id", name, name_len)); |
| 1757 | assert_int_equal(2, parent_times); |
| 1758 | assert_int_equal(0, has_predicate); |
| 1759 | assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)); |
| 1760 | assert_string_equal("[predicate]/invalid!!!", path); |
| 1761 | assert_int_equal(0, prefix_len); |
| 1762 | assert_null(prefix); |
| 1763 | assert_int_equal(3, name_len); |
| 1764 | assert_int_equal(0, strncmp("xxx", name, name_len)); |
| 1765 | assert_int_equal(1, has_predicate); |
| 1766 | path += 11; |
| 1767 | assert_int_equal(LY_EINVAL, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)); |
| 1768 | assert_string_equal("!!!", path); |
| 1769 | assert_int_equal(0, prefix_len); |
| 1770 | assert_null(prefix); |
| 1771 | assert_int_equal(7, name_len); |
| 1772 | assert_int_equal(0, strncmp("invalid", name, name_len)); |
| 1773 | |
| 1774 | path = "/absolute/prefix:path"; |
| 1775 | parent_times = 0; |
| 1776 | assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)); |
| 1777 | assert_string_equal("/prefix:path", path); |
| 1778 | assert_int_equal(0, prefix_len); |
| 1779 | assert_null(prefix); |
| 1780 | assert_int_equal(8, name_len); |
| 1781 | assert_int_equal(0, strncmp("absolute", name, name_len)); |
| 1782 | assert_int_equal(-1, parent_times); |
| 1783 | assert_int_equal(0, has_predicate); |
| 1784 | assert_int_equal(LY_SUCCESS, lys_path_token(&path, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)); |
| 1785 | assert_int_equal(0, *path); |
| 1786 | assert_int_equal(6, prefix_len); |
| 1787 | assert_int_equal(0, strncmp("prefix", prefix, prefix_len)); |
| 1788 | assert_int_equal(4, name_len); |
| 1789 | assert_int_equal(0, strncmp("path", name, name_len)); |
| 1790 | assert_int_equal(0, has_predicate); |
| 1791 | |
| 1792 | /* complete leafref paths */ |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 1793 | 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] | 1794 | "leaf ref1 {type leafref {path /a:target1;}} leaf ref2 {type leafref {path /a/target2; require-instance false;}}" |
| 1795 | "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] | 1796 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1797 | assert_non_null(type); |
| 1798 | assert_int_equal(LY_TYPE_LEAFREF, type->basetype); |
| 1799 | assert_string_equal("/a:target1", ((struct lysc_type_leafref*)type)->path); |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 1800 | assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context); |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 1801 | assert_non_null(((struct lysc_type_leafref*)type)->realtype); |
| 1802 | 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] | 1803 | assert_int_equal(1, ((struct lysc_type_leafref*)type)->require_instance); |
| 1804 | type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type; |
| 1805 | assert_non_null(type); |
| 1806 | assert_int_equal(LY_TYPE_LEAFREF, type->basetype); |
| 1807 | assert_string_equal("/a/target2", ((struct lysc_type_leafref*)type)->path); |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 1808 | assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context); |
| 1809 | assert_non_null(((struct lysc_type_leafref*)type)->realtype); |
| 1810 | 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] | 1811 | assert_int_equal(0, ((struct lysc_type_leafref*)type)->require_instance); |
| 1812 | |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 1813 | 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] | 1814 | "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] | 1815 | "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] | 1816 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1817 | assert_non_null(type); |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 1818 | assert_int_equal(1, type->refcount); |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 1819 | assert_int_equal(LY_TYPE_LEAFREF, type->basetype); |
| 1820 | assert_string_equal("/b:target", ((struct lysc_type_leafref* )type)->path); |
| 1821 | assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context); |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 1822 | assert_non_null(((struct lysc_type_leafref*)type)->realtype); |
| 1823 | 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] | 1824 | assert_int_equal(1, ((struct lysc_type_leafref* )type)->require_instance); |
| 1825 | |
| 1826 | /* prefixes are reversed to check using correct context of the path! */ |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 1827 | 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] | 1828 | "typedef mytype3 {type c:mytype {require-instance false;}}" |
| 1829 | "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] | 1830 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1831 | assert_non_null(type); |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 1832 | assert_int_equal(1, type->refcount); |
Radek Krejci | 2f4a029 | 2018-11-22 15:41:53 +0100 | [diff] [blame] | 1833 | assert_int_equal(LY_TYPE_LEAFREF, type->basetype); |
| 1834 | assert_string_equal("/b:target", ((struct lysc_type_leafref* )type)->path); |
| 1835 | assert_ptr_not_equal(mod, ((struct lysc_type_leafref*)type)->path_context); |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 1836 | assert_non_null(((struct lysc_type_leafref*)type)->realtype); |
| 1837 | 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] | 1838 | assert_int_equal(0, ((struct lysc_type_leafref* )type)->require_instance); |
| 1839 | type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type; |
| 1840 | assert_non_null(type); |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 1841 | assert_int_equal(1, type->refcount); |
Radek Krejci | 96a0bfd | 2018-11-22 15:25:06 +0100 | [diff] [blame] | 1842 | assert_int_equal(LY_TYPE_LEAFREF, type->basetype); |
| 1843 | assert_string_equal("/b:target", ((struct lysc_type_leafref* )type)->path); |
| 1844 | assert_ptr_not_equal(mod, ((struct lysc_type_leafref*)type)->path_context); |
| 1845 | assert_int_equal(1, ((struct lysc_type_leafref* )type)->require_instance); |
| 1846 | |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 1847 | /* non-prefixed nodes in path are supposed to be from the module where the leafref type is instantiated */ |
| 1848 | assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d; import b {prefix b;}" |
| 1849 | "leaf ref {type b:mytype3;}leaf target {type int8;}}", LYS_IN_YANG)); |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 1850 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1851 | assert_non_null(type); |
| 1852 | assert_int_equal(1, type->refcount); |
| 1853 | assert_int_equal(LY_TYPE_LEAFREF, type->basetype); |
| 1854 | assert_string_equal("/target", ((struct lysc_type_leafref* )type)->path); |
| 1855 | assert_ptr_not_equal(mod, ((struct lysc_type_leafref*)type)->path_context); |
| 1856 | assert_non_null(((struct lysc_type_leafref*)type)->realtype); |
| 1857 | assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)type)->realtype->basetype); |
| 1858 | assert_int_equal(1, ((struct lysc_type_leafref* )type)->require_instance); |
| 1859 | |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 1860 | /* conditional leafrefs */ |
Radek Krejci | 4ce6893 | 2018-11-28 12:53:36 +0100 | [diff] [blame] | 1861 | 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] | 1862 | "leaf ref1 {if-feature 'f1 and f2';type leafref {path /target;}}" |
| 1863 | "leaf target {if-feature f1; type boolean;}}", LYS_IN_YANG)); |
Radek Krejci | 58d171e | 2018-11-23 13:50:55 +0100 | [diff] [blame] | 1864 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1865 | assert_non_null(type); |
| 1866 | assert_int_equal(1, type->refcount); |
| 1867 | assert_int_equal(LY_TYPE_LEAFREF, type->basetype); |
| 1868 | assert_string_equal("/target", ((struct lysc_type_leafref* )type)->path); |
| 1869 | assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context); |
| 1870 | assert_non_null(((struct lysc_type_leafref*)type)->realtype); |
| 1871 | assert_int_equal(LY_TYPE_BOOL, ((struct lysc_type_leafref*)type)->realtype->basetype); |
| 1872 | |
Radek Krejci | 7adf4ff | 2018-12-05 08:55:00 +0100 | [diff] [blame] | 1873 | assert_non_null(mod = lys_parse_mem(ctx, "module f {namespace urn:f;prefix f;" |
| 1874 | "list interface{key name;leaf name{type string;}list address {key ip;leaf ip {type string;}}}" |
| 1875 | "container default-address{leaf ifname{type leafref{ path \"../../interface/name\";}}" |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 1876 | "leaf address {type leafref{ path \"../../interface[ name = current()/../ifname ]/address/ip\";}}}}", |
Radek Krejci | 7adf4ff | 2018-12-05 08:55:00 +0100 | [diff] [blame] | 1877 | LYS_IN_YANG)); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 1878 | type = ((struct lysc_node_leaf*)(*lysc_node_children_p(mod->compiled->data->prev, 0))->prev)->type; |
Radek Krejci | 7adf4ff | 2018-12-05 08:55:00 +0100 | [diff] [blame] | 1879 | assert_non_null(type); |
| 1880 | assert_int_equal(1, type->refcount); |
| 1881 | assert_int_equal(LY_TYPE_LEAFREF, type->basetype); |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 1882 | 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] | 1883 | assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context); |
| 1884 | assert_non_null(((struct lysc_type_leafref*)type)->realtype); |
| 1885 | 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] | 1886 | |
Radek Krejci | 20e8a18 | 2018-12-07 11:43:21 +0100 | [diff] [blame] | 1887 | assert_non_null(mod = lys_parse_mem(ctx, "module g {namespace urn:g;prefix g;" |
| 1888 | "leaf source {type leafref {path \"/endpoint-parent[id=current()/../field]/endpoint/name\";}}" |
| 1889 | "leaf field {type int32;}list endpoint-parent {key id;leaf id {type int32;}" |
| 1890 | "list endpoint {key name;leaf name {type string;}}}}", LYS_IN_YANG)); |
Radek Krejci | 20e8a18 | 2018-12-07 11:43:21 +0100 | [diff] [blame] | 1891 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1892 | assert_non_null(type); |
| 1893 | assert_int_equal(1, type->refcount); |
| 1894 | assert_int_equal(LY_TYPE_LEAFREF, type->basetype); |
| 1895 | assert_string_equal("/endpoint-parent[id=current()/../field]/endpoint/name", ((struct lysc_type_leafref* )type)->path); |
| 1896 | assert_ptr_equal(mod, ((struct lysc_type_leafref*)type)->path_context); |
| 1897 | assert_non_null(((struct lysc_type_leafref*)type)->realtype); |
| 1898 | assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_leafref*)type)->realtype->basetype); |
| 1899 | |
Radek Krejci | 3d92956 | 2019-02-21 11:25:55 +0100 | [diff] [blame] | 1900 | /* leafref to imported (not yet implemented) module */ |
| 1901 | ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module h {namespace urn:h;prefix h; leaf h {type uint16;}}"); |
| 1902 | assert_non_null(mod = lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;import h {prefix h;}" |
| 1903 | "leaf i {type leafref {path /h:h;}}}", LYS_IN_YANG)); |
| 1904 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1905 | assert_non_null(type); |
| 1906 | assert_int_equal(LY_TYPE_LEAFREF, type->basetype); |
| 1907 | assert_non_null(((struct lysc_type_leafref*)type)->realtype); |
| 1908 | assert_int_equal(LY_TYPE_UINT16, ((struct lysc_type_leafref*)type)->realtype->basetype); |
| 1909 | assert_non_null(mod = ly_ctx_get_module_implemented(ctx, "h")); |
| 1910 | assert_int_equal(1, mod->implemented); |
| 1911 | assert_non_null(mod->compiled->data); |
| 1912 | assert_string_equal("h", mod->compiled->data->name); |
| 1913 | |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 1914 | ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module j {namespace urn:j;prefix j; leaf j {type string;}}"); |
| 1915 | assert_non_null(mod = lys_parse_mem(ctx, "module k {namespace urn:k;prefix k;import j {prefix j;}" |
| 1916 | "leaf i {type leafref {path \"/ilist[name = current()/../j:j]/value\";}}" |
| 1917 | "list ilist {key name; leaf name {type string;} leaf value {type uint16;}}}", LYS_IN_YANG)); |
| 1918 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 1919 | assert_non_null(type); |
| 1920 | assert_int_equal(LY_TYPE_LEAFREF, type->basetype); |
| 1921 | assert_non_null(((struct lysc_type_leafref*)type)->realtype); |
| 1922 | assert_int_equal(LY_TYPE_UINT16, ((struct lysc_type_leafref*)type)->realtype->basetype); |
| 1923 | assert_non_null(mod = ly_ctx_get_module_implemented(ctx, "j")); |
| 1924 | assert_int_equal(1, mod->implemented); |
| 1925 | assert_non_null(mod->compiled->data); |
| 1926 | assert_string_equal("j", mod->compiled->data->name); |
| 1927 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1928 | /* invalid paths */ |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1929 | 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] | 1930 | "leaf ref1 {type leafref {path ../a/invalid;}}}", LYS_IN_YANG)); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1931 | logbuf_assert("Invalid leafref path - unable to find \"../a/invalid\"."); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1932 | 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] | 1933 | "leaf ref1 {type leafref {path ../../toohigh;}}}", LYS_IN_YANG)); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1934 | logbuf_assert("Invalid leafref path \"../../toohigh\" - too many \"..\" in the path."); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1935 | 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] | 1936 | "leaf ref1 {type leafref {path /a:invalid;}}}", LYS_IN_YANG)); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1937 | 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] | 1938 | 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] | 1939 | "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] | 1940 | 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] | 1941 | 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] | 1942 | "leaf ref1 {type leafref {path /a!invalid;}}}", LYS_IN_YANG)); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1943 | logbuf_assert("Invalid leafref path at character 3 (/a!invalid)."); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1944 | 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] | 1945 | "leaf ref1 {type leafref {path /a;}}}", LYS_IN_YANG)); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1946 | 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] | 1947 | 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] | 1948 | "leaf ref1 {type leafref {path /a/target2;}}}", LYS_IN_YANG)); |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 1949 | 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] | 1950 | 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] | 1951 | "leaf ref1 {type leafref;}}", LYS_IN_YANG)); |
Radek Krejci | 2f4a029 | 2018-11-22 15:41:53 +0100 | [diff] [blame] | 1952 | logbuf_assert("Missing path substatement for leafref type."); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1953 | 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] | 1954 | "leaf ref1 {type mytype;}}", LYS_IN_YANG)); |
Radek Krejci | 2f4a029 | 2018-11-22 15:41:53 +0100 | [diff] [blame] | 1955 | logbuf_assert("Missing path substatement for leafref type mytype."); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1956 | 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] | 1957 | "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] | 1958 | logbuf_assert("Invalid leafref path \"/target\" - set of features applicable to the leafref target is not a subset of features " |
| 1959 | "applicable to the leafref itself."); |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1960 | 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] | 1961 | "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] | 1962 | 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] | 1963 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1964 | 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] | 1965 | "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] | 1966 | 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] | 1967 | 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] | 1968 | "leaf ref {type mytype;}leaf target {type string;}}", LYS_IN_YANG)); |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 1969 | logbuf_assert("Leafref type \"mytype\" can be restricted by require-instance statement only in YANG 1.1 modules."); |
| 1970 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1971 | 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] | 1972 | "list interface{key name;leaf name{type string;}leaf ip {type string;}}" |
| 1973 | "leaf ifname{type leafref{ path \"../interface/name\";}}" |
| 1974 | "leaf address {type leafref{ path \"/interface[name is current()/../ifname]/ip\";}}}", |
| 1975 | LYS_IN_YANG)); |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 1976 | logbuf_assert("Invalid leafref path predicate \"[name i\" - missing \"=\" after node-identifier."); |
| 1977 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1978 | 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] | 1979 | "list interface{key name;leaf name{type string;}leaf ip {type string;}}" |
| 1980 | "leaf ifname{type leafref{ path \"../interface/name\";}}" |
| 1981 | "leaf address {type leafref{ path \"/interface[name=current()/../ifname/ip\";}}}", |
| 1982 | LYS_IN_YANG)); |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 1983 | logbuf_assert("Invalid leafref path predicate \"[name=current()/../ifname/ip\" - missing predicate termination."); |
| 1984 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1985 | 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] | 1986 | "list interface{key name;leaf name{type string;}leaf ip {type string;}}" |
| 1987 | "leaf ifname{type leafref{ path \"../interface/name\";}}" |
| 1988 | "leaf address {type leafref{ path \"/interface[x:name=current()/../ifname]/ip\";}}}", |
| 1989 | LYS_IN_YANG)); |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 1990 | logbuf_assert("Invalid leafref path predicate \"[x:name=current()/../ifname]\" - prefix \"x\" not defined in module \"pp\"."); |
| 1991 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1992 | 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] | 1993 | "list interface{key name;leaf name{type string;}leaf ip {type string;}}" |
| 1994 | "leaf ifname{type leafref{ path \"../interface/name\";}}" |
| 1995 | "leaf address {type leafref{ path \"/interface[id=current()/../ifname]/ip\";}}}", |
| 1996 | LYS_IN_YANG)); |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 1997 | logbuf_assert("Invalid leafref path predicate \"[id=current()/../ifname]\" - predicate's key node \"id\" not found."); |
| 1998 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 1999 | 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] | 2000 | "list interface{key name;leaf name{type string;}leaf ip {type string;}}" |
| 2001 | "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}" |
| 2002 | "leaf address {type leafref{ path \"/interface[name=current() / .. / ifname][name=current()/../test]/ip\";}}}", |
| 2003 | LYS_IN_YANG)); |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2004 | logbuf_assert("Invalid leafref path predicate \"[name=current()/../test]\" - multiple equality tests for the key \"name\"."); |
| 2005 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2006 | 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] | 2007 | "list interface{key name;leaf name{type string;}leaf ip {type string;}}" |
| 2008 | "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}" |
| 2009 | "leaf address {type leafref{ path \"/interface[name = ../ifname]/ip\";}}}", |
| 2010 | LYS_IN_YANG)); |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2011 | logbuf_assert("Invalid leafref path predicate \"[name = ../ifname]\" - missing current-function-invocation."); |
| 2012 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2013 | 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] | 2014 | "list interface{key name;leaf name{type string;}leaf ip {type string;}}" |
| 2015 | "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}" |
| 2016 | "leaf address {type leafref{ path \"/interface[name = current()../ifname]/ip\";}}}", |
| 2017 | LYS_IN_YANG)); |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2018 | logbuf_assert("Invalid leafref path predicate \"[name = current()../ifname]\" - missing \"/\" after current-function-invocation."); |
| 2019 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2020 | 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] | 2021 | "list interface{key name;leaf name{type string;}leaf ip {type string;}}" |
| 2022 | "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}" |
| 2023 | "leaf address {type leafref{ path \"/interface[name = current()/..ifname]/ip\";}}}", |
| 2024 | LYS_IN_YANG)); |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2025 | logbuf_assert("Invalid leafref path predicate \"[name = current()/..ifname]\" - missing \"/\" in \"../\" rel-path-keyexpr pattern."); |
| 2026 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2027 | 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] | 2028 | "list interface{key name;leaf name{type string;}leaf ip {type string;}}" |
| 2029 | "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}" |
| 2030 | "leaf address {type leafref{ path \"/interface[name = current()/ifname]/ip\";}}}", |
| 2031 | LYS_IN_YANG)); |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2032 | logbuf_assert("Invalid leafref path predicate \"[name = current()/ifname]\" - at least one \"..\" is expected in rel-path-keyexpr."); |
| 2033 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2034 | 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] | 2035 | "list interface{key name;leaf name{type string;}leaf ip {type string;}}" |
| 2036 | "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}" |
| 2037 | "leaf address {type leafref{ path \"/interface[name = current()/../]/ip\";}}}", |
| 2038 | LYS_IN_YANG)); |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2039 | logbuf_assert("Invalid leafref path predicate \"[name = current()/../]\" - at least one node-identifier is expected in rel-path-keyexpr."); |
| 2040 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2041 | 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] | 2042 | "list interface{key name;leaf name{type string;}leaf ip {type string;}}" |
| 2043 | "leaf ifname{type leafref{ path \"../interface/name\";}}leaf test{type string;}" |
| 2044 | "leaf address {type leafref{ path \"/interface[name = current()/../$node]/ip\";}}}", |
| 2045 | LYS_IN_YANG)); |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2046 | logbuf_assert("Invalid node identifier in leafref path predicate - character 22 (of [name = current()/../$node])."); |
| 2047 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2048 | 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] | 2049 | "list interface{key name;leaf name{type string;}leaf ip {type string;}}" |
| 2050 | "leaf ifname{type leafref{ path \"../interface/name\";}}" |
| 2051 | "leaf address {type leafref{ path \"/interface[name=current()/../x:ifname]/ip\";}}}", |
| 2052 | LYS_IN_YANG)); |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2053 | logbuf_assert("Invalid leafref path predicate \"[name=current()/../x:ifname]\" - unable to find module of the node \"ifname\" in rel-path-keyexpr."); |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2054 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2055 | 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] | 2056 | "list interface{key name;leaf name{type string;}leaf ip {type string;}}" |
| 2057 | "leaf ifname{type leafref{ path \"../interface/name\";}}" |
| 2058 | "leaf address {type leafref{ path \"/interface[name=current()/../xxx]/ip\";}}}", |
| 2059 | LYS_IN_YANG)); |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2060 | logbuf_assert("Invalid leafref path predicate \"[name=current()/../xxx]\" - unable to find node \"current()/../xxx\" in the rel-path-keyexpr."); |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2061 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2062 | 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] | 2063 | "list interface{key name;leaf name{type string;}leaf ip {type string;}}" |
| 2064 | "leaf ifname{type leafref{ path \"../interface/name\";}}container c;" |
| 2065 | "leaf address {type leafref{ path \"/interface[name=current()/../c]/ip\";}}}", |
| 2066 | LYS_IN_YANG)); |
Radek Krejci | 92cc851 | 2019-04-25 09:57:06 +0200 | [diff] [blame] | 2067 | logbuf_assert("Invalid leafref path predicate \"[name=current()/../c]\" - rel-path-keyexpr \"current()/../c\" refers container instead of leaf."); |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2068 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2069 | 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] | 2070 | "list interface{key name;leaf name{type string;}leaf ip {type string;}container c;}" |
| 2071 | "leaf ifname{type leafref{ path \"../interface/name\";}}" |
| 2072 | "leaf address {type leafref{ path \"/interface[c=current()/../ifname]/ip\";}}}", |
| 2073 | LYS_IN_YANG)); |
Radek Krejci | bade82a | 2018-12-05 10:13:48 +0100 | [diff] [blame] | 2074 | logbuf_assert("Invalid leafref path predicate \"[c=current()/../ifname]\" - predicate's key node \"c\" not found."); |
| 2075 | |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2076 | /* circular chain */ |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2077 | 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] | 2078 | "leaf ref1 {type leafref {path /ref2;}}" |
| 2079 | "leaf ref2 {type leafref {path /ref3;}}" |
Radek Krejci | 0c3f1ad | 2018-11-23 14:19:38 +0100 | [diff] [blame] | 2080 | "leaf ref3 {type leafref {path /ref4;}}" |
| 2081 | "leaf ref4 {type leafref {path /ref1;}}}", LYS_IN_YANG)); |
Radek Krejci | 412ddfa | 2018-11-23 11:44:11 +0100 | [diff] [blame] | 2082 | logbuf_assert("Invalid leafref path \"/ref1\" - circular chain of leafrefs detected."); |
| 2083 | |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 2084 | *state = NULL; |
| 2085 | ly_ctx_destroy(ctx, NULL); |
| 2086 | } |
| 2087 | |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 2088 | static void |
| 2089 | test_type_empty(void **state) |
| 2090 | { |
| 2091 | *state = test_type_empty; |
| 2092 | |
| 2093 | struct ly_ctx *ctx; |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 2094 | |
| 2095 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 2096 | |
| 2097 | /* invalid */ |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2098 | 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] | 2099 | "leaf l {type empty; default x;}}", LYS_IN_YANG)); |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 2100 | logbuf_assert("Leaf of type \"empty\" must not have a default value (x)."); |
| 2101 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2102 | 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] | 2103 | "leaf l {type mytype;}}", LYS_IN_YANG)); |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 2104 | logbuf_assert("Invalid type \"mytype\" - \"empty\" type must not have a default value (x)."); |
| 2105 | |
| 2106 | *state = NULL; |
| 2107 | ly_ctx_destroy(ctx, NULL); |
| 2108 | } |
| 2109 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2110 | |
| 2111 | static void |
| 2112 | test_type_union(void **state) |
| 2113 | { |
| 2114 | *state = test_type_union; |
| 2115 | |
| 2116 | struct ly_ctx *ctx; |
| 2117 | struct lys_module *mod; |
| 2118 | struct lysc_type *type; |
| 2119 | |
| 2120 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 2121 | |
| 2122 | assert_non_null(mod = lys_parse_mem(ctx, "module a {yang-version 1.1;namespace urn:a;prefix a; typedef mybasetype {type string;}" |
| 2123 | "typedef mytype {type union {type int8; type mybasetype;}}" |
| 2124 | "leaf l {type mytype;}}", LYS_IN_YANG)); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2125 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 2126 | assert_non_null(type); |
| 2127 | assert_int_equal(2, type->refcount); |
| 2128 | assert_int_equal(LY_TYPE_UNION, type->basetype); |
| 2129 | assert_non_null(((struct lysc_type_union*)type)->types); |
| 2130 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types)); |
| 2131 | assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_union*)type)->types[0]->basetype); |
| 2132 | assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[1]->basetype); |
| 2133 | |
| 2134 | assert_non_null(mod = lys_parse_mem(ctx, "module b {yang-version 1.1;namespace urn:b;prefix b; typedef mybasetype {type string;}" |
| 2135 | "typedef mytype {type union {type int8; type mybasetype;}}" |
| 2136 | "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] | 2137 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 2138 | assert_non_null(type); |
| 2139 | assert_int_equal(1, type->refcount); |
| 2140 | assert_int_equal(LY_TYPE_UNION, type->basetype); |
| 2141 | assert_non_null(((struct lysc_type_union*)type)->types); |
| 2142 | assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types)); |
| 2143 | assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype); |
| 2144 | assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_union*)type)->types[1]->basetype); |
| 2145 | assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[2]->basetype); |
| 2146 | |
| 2147 | assert_non_null(mod = lys_parse_mem(ctx, "module c {yang-version 1.1;namespace urn:c;prefix c; typedef mybasetype {type string;}" |
| 2148 | "typedef mytype {type union {type leafref {path ../target;} type mybasetype;}}" |
Radek Krejci | 6be5ff1 | 2018-11-26 13:18:15 +0100 | [diff] [blame] | 2149 | "leaf l {type union {type decimal64 {fraction-digits 2;} type mytype;}}" |
| 2150 | "leaf target {type leafref {path ../realtarget;}} leaf realtarget {type int8;}}", |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2151 | LYS_IN_YANG)); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2152 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 2153 | assert_non_null(type); |
| 2154 | assert_int_equal(1, type->refcount); |
| 2155 | assert_int_equal(LY_TYPE_UNION, type->basetype); |
| 2156 | assert_non_null(((struct lysc_type_union*)type)->types); |
| 2157 | assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types)); |
| 2158 | assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype); |
| 2159 | assert_int_equal(LY_TYPE_LEAFREF, ((struct lysc_type_union*)type)->types[1]->basetype); |
| 2160 | assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[2]->basetype); |
| 2161 | assert_non_null(((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype); |
| 2162 | assert_int_equal(LY_TYPE_INT8, ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[1])->realtype->basetype); |
| 2163 | |
Radek Krejci | 6be5ff1 | 2018-11-26 13:18:15 +0100 | [diff] [blame] | 2164 | /* invalid unions */ |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2165 | 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] | 2166 | "leaf l {type mytype;}}", LYS_IN_YANG)); |
Radek Krejci | 6be5ff1 | 2018-11-26 13:18:15 +0100 | [diff] [blame] | 2167 | logbuf_assert("Missing type substatement for union type mytype."); |
| 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;leaf l {type union;}}", LYS_IN_YANG)); |
| 2170 | logbuf_assert("Missing type substatement for union type."); |
Radek Krejci | 6be5ff1 | 2018-11-26 13:18:15 +0100 | [diff] [blame] | 2171 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2172 | 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] | 2173 | "leaf l {type mytype {type string;}}}", LYS_IN_YANG)); |
Radek Krejci | 6be5ff1 | 2018-11-26 13:18:15 +0100 | [diff] [blame] | 2174 | logbuf_assert("Invalid type substatement for the type not directly derived from union built-in type."); |
| 2175 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2176 | 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] | 2177 | "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] | 2178 | logbuf_assert("Invalid type substatement for the type \"mytype2\" not directly derived from union built-in type."); |
| 2179 | |
Radek Krejci | 99b5b2a | 2019-04-30 16:57:04 +0200 | [diff] [blame] | 2180 | assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee;typedef mytype {type union{type mytype; type string;}}" |
| 2181 | "leaf l {type mytype;}}", LYS_IN_YANG)); |
| 2182 | logbuf_assert("Invalid \"mytype\" type reference - circular chain of types detected."); |
| 2183 | assert_null(lys_parse_mem(ctx, "module ef {namespace urn:ef;prefix ef;typedef mytype {type mytype2;}" |
| 2184 | "typedef mytype2 {type mytype;} leaf l {type mytype;}}", LYS_IN_YANG)); |
| 2185 | logbuf_assert("Invalid \"mytype\" type reference - circular chain of types detected."); |
| 2186 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2187 | *state = NULL; |
| 2188 | ly_ctx_destroy(ctx, NULL); |
| 2189 | } |
| 2190 | |
| 2191 | static void |
| 2192 | test_type_dflt(void **state) |
| 2193 | { |
| 2194 | *state = test_type_union; |
| 2195 | |
| 2196 | struct ly_ctx *ctx; |
| 2197 | struct lys_module *mod; |
| 2198 | struct lysc_type *type; |
| 2199 | |
| 2200 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 2201 | |
| 2202 | /* default is not inherited from union's types */ |
| 2203 | assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a; typedef mybasetype {type string;default hello;units xxx;}" |
| 2204 | "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] | 2205 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 2206 | assert_non_null(type); |
| 2207 | assert_int_equal(1, type->refcount); |
| 2208 | assert_int_equal(LY_TYPE_UNION, type->basetype); |
| 2209 | assert_non_null(((struct lysc_type_union*)type)->types); |
| 2210 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_union*)type)->types)); |
| 2211 | assert_int_equal(LY_TYPE_DEC64, ((struct lysc_type_union*)type)->types[0]->basetype); |
| 2212 | assert_int_equal(LY_TYPE_STRING, ((struct lysc_type_union*)type)->types[1]->basetype); |
| 2213 | assert_null(((struct lysc_node_leaf*)mod->compiled->data)->dflt); |
| 2214 | assert_null(((struct lysc_node_leaf*)mod->compiled->data)->units); |
| 2215 | |
| 2216 | assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b; typedef mybasetype {type string;default hello;units xxx;}" |
| 2217 | "leaf l {type mybasetype;}}", LYS_IN_YANG)); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2218 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 2219 | assert_non_null(type); |
| 2220 | assert_int_equal(2, type->refcount); |
| 2221 | assert_int_equal(LY_TYPE_STRING, type->basetype); |
| 2222 | assert_string_equal("hello", ((struct lysc_node_leaf*)mod->compiled->data)->dflt); |
| 2223 | assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data)->units); |
| 2224 | |
| 2225 | assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c; typedef mybasetype {type string;default hello;units xxx;}" |
| 2226 | "leaf l {type mybasetype; default goodbye;units yyy;}}", LYS_IN_YANG)); |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2227 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 2228 | assert_non_null(type); |
| 2229 | assert_int_equal(2, type->refcount); |
| 2230 | assert_int_equal(LY_TYPE_STRING, type->basetype); |
| 2231 | assert_string_equal("goodbye", ((struct lysc_node_leaf*)mod->compiled->data)->dflt); |
| 2232 | assert_string_equal("yyy", ((struct lysc_node_leaf*)mod->compiled->data)->units); |
| 2233 | |
Radek Krejci | 6be5ff1 | 2018-11-26 13:18:15 +0100 | [diff] [blame] | 2234 | assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d; typedef mybasetype {type string;default hello;units xxx;}" |
| 2235 | "typedef mytype {type mybasetype;}leaf l1 {type mytype; default goodbye;units yyy;}" |
| 2236 | "leaf l2 {type mytype;}}", LYS_IN_YANG)); |
Radek Krejci | 6be5ff1 | 2018-11-26 13:18:15 +0100 | [diff] [blame] | 2237 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 2238 | assert_non_null(type); |
| 2239 | assert_int_equal(4, type->refcount); |
| 2240 | assert_int_equal(LY_TYPE_STRING, type->basetype); |
| 2241 | assert_string_equal("goodbye", ((struct lysc_node_leaf*)mod->compiled->data)->dflt); |
| 2242 | assert_string_equal("yyy", ((struct lysc_node_leaf*)mod->compiled->data)->units); |
| 2243 | type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type; |
| 2244 | assert_non_null(type); |
| 2245 | assert_int_equal(4, type->refcount); |
| 2246 | assert_int_equal(LY_TYPE_STRING, type->basetype); |
| 2247 | assert_string_equal("hello", ((struct lysc_node_leaf*)mod->compiled->data->next)->dflt); |
| 2248 | assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data->next)->units); |
| 2249 | |
| 2250 | assert_non_null(mod = lys_parse_mem(ctx, "module e {namespace urn:e;prefix e; typedef mybasetype {type string;}" |
| 2251 | "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] | 2252 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 2253 | assert_non_null(type); |
| 2254 | assert_int_equal(3, type->refcount); |
| 2255 | assert_int_equal(LY_TYPE_STRING, type->basetype); |
| 2256 | assert_string_equal("hello", ((struct lysc_node_leaf*)mod->compiled->data)->dflt); |
| 2257 | assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data)->units); |
| 2258 | |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 2259 | /* mandatory leaf does not takes default value from type */ |
| 2260 | assert_non_null(mod = lys_parse_mem(ctx, "module f {namespace urn:f;prefix f;typedef mytype {type string; default hello;units xxx;}" |
| 2261 | "leaf l {type mytype; mandatory true;}}", LYS_IN_YANG)); |
Radek Krejci | b1a5dcc | 2018-11-26 14:50:05 +0100 | [diff] [blame] | 2262 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 2263 | assert_non_null(type); |
| 2264 | assert_int_equal(LY_TYPE_STRING, type->basetype); |
| 2265 | assert_null(((struct lysc_node_leaf*)mod->compiled->data)->dflt); |
| 2266 | assert_string_equal("xxx", ((struct lysc_node_leaf*)mod->compiled->data)->units); |
| 2267 | |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 2268 | *state = NULL; |
| 2269 | ly_ctx_destroy(ctx, NULL); |
| 2270 | } |
| 2271 | |
Radek Krejci | 665421c | 2018-12-05 08:03:39 +0100 | [diff] [blame] | 2272 | static void |
| 2273 | test_status(void **state) |
| 2274 | { |
| 2275 | *state = test_status; |
| 2276 | |
| 2277 | struct ly_ctx *ctx; |
Radek Krejci | 665421c | 2018-12-05 08:03:39 +0100 | [diff] [blame] | 2278 | |
| 2279 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 2280 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2281 | 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] | 2282 | "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] | 2283 | logbuf_assert("A \"current\" status is in conflict with the parent's \"deprecated\" status."); |
| 2284 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2285 | 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] | 2286 | "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] | 2287 | logbuf_assert("A \"current\" status is in conflict with the parent's \"obsolete\" status."); |
| 2288 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2289 | 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] | 2290 | "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] | 2291 | logbuf_assert("A \"deprecated\" status is in conflict with the parent's \"obsolete\" status."); |
| 2292 | |
| 2293 | *state = NULL; |
| 2294 | ly_ctx_destroy(ctx, NULL); |
| 2295 | } |
| 2296 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2297 | static void |
| 2298 | test_uses(void **state) |
| 2299 | { |
| 2300 | *state = test_uses; |
| 2301 | |
| 2302 | struct ly_ctx *ctx; |
| 2303 | struct lys_module *mod; |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 2304 | const struct lysc_node *parent, *child; |
Radek Krejci | 32b6ecd | 2019-04-12 10:41:24 +0200 | [diff] [blame] | 2305 | const struct lysc_node_container *cont; |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2306 | |
| 2307 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 2308 | |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 2309 | ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module grp {namespace urn:grp;prefix g; typedef mytype {type string;} feature f;" |
| 2310 | "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] | 2311 | assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;import grp {prefix g;}" |
| 2312 | "grouping grp_a_top {leaf a1 {type int8;}}" |
| 2313 | "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] | 2314 | assert_non_null((parent = mod->compiled->data)); |
| 2315 | assert_int_equal(LYS_CONTAINER, parent->nodetype); |
| 2316 | assert_non_null((child = ((struct lysc_node_container*)parent)->child)); |
| 2317 | assert_string_equal("a2", child->name); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 2318 | assert_ptr_equal(mod, child->module); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2319 | assert_non_null((child = child->next)); |
| 2320 | assert_string_equal("a1", child->name); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 2321 | assert_ptr_equal(mod, child->module); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2322 | assert_non_null((child = child->next)); |
| 2323 | assert_string_equal("x", child->name); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 2324 | assert_ptr_equal(mod, child->module); |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 2325 | assert_non_null((child = child->next)); |
| 2326 | assert_string_equal("y", child->name); |
| 2327 | assert_non_null(child->iffeatures); |
| 2328 | assert_int_equal(1, LY_ARRAY_SIZE(child->iffeatures)); |
| 2329 | assert_int_equal(1, LY_ARRAY_SIZE(child->iffeatures[0].features)); |
| 2330 | assert_string_equal("f", child->iffeatures[0].features[0]->name); |
| 2331 | assert_int_equal(LY_EINVAL, lys_feature_enable(mod->compiled->imports[0].module, "f")); |
| 2332 | logbuf_assert("Module \"grp\" is not implemented so all its features are permanently disabled without a chance to change it."); |
| 2333 | assert_int_equal(0, lysc_iffeature_value(&child->iffeatures[0])); |
| 2334 | |
| 2335 | /* make the imported module implemented and enable the feature */ |
| 2336 | assert_non_null(mod = ly_ctx_get_module(ctx, "grp", NULL)); |
| 2337 | assert_int_equal(LY_SUCCESS, ly_ctx_module_implement(ctx, mod)); |
| 2338 | assert_int_equal(LY_SUCCESS, lys_feature_enable(mod, "f")); |
| 2339 | assert_string_equal("f", child->iffeatures[0].features[0]->name); |
| 2340 | assert_int_equal(1, lysc_iffeature_value(&child->iffeatures[0])); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2341 | |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 2342 | ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule bsub {belongs-to b {prefix b;} grouping grp {leaf b {when 1; type string;}}}"); |
| 2343 | assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;include bsub;uses grp {when 2;}}", LYS_IN_YANG)); |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 2344 | assert_non_null(mod->compiled->data); |
| 2345 | assert_int_equal(LYS_LEAF, mod->compiled->data->nodetype); |
| 2346 | assert_string_equal("b", mod->compiled->data->name); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 2347 | assert_int_equal(2, LY_ARRAY_SIZE(mod->compiled->data->when)); |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 2348 | |
Radek Krejci | 7f6a381 | 2019-01-07 13:48:57 +0100 | [diff] [blame] | 2349 | logbuf_clean(); |
| 2350 | assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:ii;prefix ii;" |
| 2351 | "grouping grp {leaf l {type string;}leaf k {type string; status obsolete;}}" |
| 2352 | "uses grp {status deprecated;}}", LYS_IN_YANG)); |
Radek Krejci | 7f6a381 | 2019-01-07 13:48:57 +0100 | [diff] [blame] | 2353 | assert_int_equal(LYS_LEAF, mod->compiled->data->nodetype); |
| 2354 | assert_string_equal("l", mod->compiled->data->name); |
| 2355 | assert_true(LYS_STATUS_DEPRC & mod->compiled->data->flags); |
| 2356 | assert_int_equal(LYS_LEAF, mod->compiled->data->next->nodetype); |
| 2357 | assert_string_equal("k", mod->compiled->data->next->name); |
| 2358 | assert_true(LYS_STATUS_OBSLT & mod->compiled->data->next->flags); |
| 2359 | logbuf_assert(""); /* no warning about inheriting deprecated flag from uses */ |
| 2360 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 2361 | assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d; grouping grp {container g;}" |
| 2362 | "container top {uses grp {augment g {leaf x {type int8;}}}}}", LYS_IN_YANG)); |
| 2363 | assert_non_null(mod->compiled->data); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 2364 | assert_non_null(child = lysc_node_children(mod->compiled->data, 0)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 2365 | assert_string_equal("g", child->name); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 2366 | assert_non_null(child = lysc_node_children(child, 0)); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 2367 | assert_string_equal("x", child->name); |
| 2368 | |
Radek Krejci | 32b6ecd | 2019-04-12 10:41:24 +0200 | [diff] [blame] | 2369 | assert_non_null(mod = lys_parse_mem(ctx, "module e {yang-version 1.1;namespace urn:e;prefix e; grouping grp {action g { description \"super g\";}}" |
| 2370 | "container top {action e; uses grp {refine g {description \"ultra g\";}}}}", LYS_IN_YANG)); |
| 2371 | assert_non_null(mod->compiled->data); |
| 2372 | cont = (const struct lysc_node_container*)mod->compiled->data; |
| 2373 | assert_non_null(cont->actions); |
| 2374 | assert_int_equal(2, LY_ARRAY_SIZE(cont->actions)); |
| 2375 | assert_string_equal("e", cont->actions[1].name); |
| 2376 | assert_string_equal("g", cont->actions[0].name); |
| 2377 | assert_string_equal("ultra g", cont->actions[0].dsc); |
| 2378 | |
| 2379 | assert_non_null(mod = lys_parse_mem(ctx, "module f {yang-version 1.1;namespace urn:f;prefix f; grouping grp {notification g { description \"super g\";}}" |
| 2380 | "container top {notification f; uses grp {refine g {description \"ultra g\";}}}}", LYS_IN_YANG)); |
| 2381 | assert_non_null(mod->compiled->data); |
| 2382 | cont = (const struct lysc_node_container*)mod->compiled->data; |
| 2383 | assert_non_null(cont->notifs); |
| 2384 | assert_int_equal(2, LY_ARRAY_SIZE(cont->notifs)); |
| 2385 | assert_string_equal("f", cont->notifs[1].name); |
| 2386 | assert_string_equal("g", cont->notifs[0].name); |
| 2387 | assert_string_equal("ultra g", cont->notifs[0].dsc); |
| 2388 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2389 | /* invalid */ |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2390 | 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] | 2391 | logbuf_assert("Grouping \"missinggrp\" referenced by a uses statement not found."); |
| 2392 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2393 | 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] | 2394 | "grouping grp {leaf a{type string;}uses grp1;}" |
| 2395 | "grouping grp1 {leaf b {type string;}uses grp2;}" |
| 2396 | "grouping grp2 {leaf c {type string;}uses grp;}}", LYS_IN_YANG)); |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2397 | logbuf_assert("Grouping \"grp\" references itself through a uses statement."); |
| 2398 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2399 | 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] | 2400 | logbuf_assert("Invalid prefix used for grouping reference (a:missingprefix)."); |
| 2401 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2402 | 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] | 2403 | "leaf a {type string;}uses grp;}", LYS_IN_YANG)); |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 2404 | logbuf_assert("Duplicate identifier \"a\" of data definition/RPC/action/Notification statement."); |
Radek Krejci | 76b3e96 | 2018-12-14 17:01:25 +0100 | [diff] [blame] | 2405 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2406 | 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] | 2407 | "uses grp {status obsolete;}}", LYS_IN_YANG)); |
Radek Krejci | 7f6a381 | 2019-01-07 13:48:57 +0100 | [diff] [blame] | 2408 | logbuf_assert("A \"deprecated\" status is in conflict with the parent's \"obsolete\" status."); |
| 2409 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 2410 | assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;grouping grp {leaf l {type string;}}" |
| 2411 | "leaf l {type int8;}uses grp;}", LYS_IN_YANG)); |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 2412 | logbuf_assert("Duplicate identifier \"l\" of data definition/RPC/action/Notification statement."); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 2413 | assert_null(lys_parse_mem(ctx, "module fg {namespace urn:fg;prefix fg;grouping grp {leaf m {type string;}}" |
| 2414 | "uses grp;leaf m {type int8;}}", LYS_IN_YANG)); |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 2415 | logbuf_assert("Duplicate identifier \"m\" of data definition/RPC/action/Notification statement."); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 2416 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 2417 | |
| 2418 | assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg; grouping grp {container g;}" |
| 2419 | "leaf g {type string;}" |
| 2420 | "container top {uses grp {augment /g {leaf x {type int8;}}}}}", LYS_IN_YANG)); |
| 2421 | logbuf_assert("Invalid descendant-schema-nodeid value \"/g\" - absolute-schema-nodeid used."); |
| 2422 | |
Radek Krejci | 32b6ecd | 2019-04-12 10:41:24 +0200 | [diff] [blame] | 2423 | assert_non_null(mod = lys_parse_mem(ctx, "module hh {yang-version 1.1;namespace urn:hh;prefix hh;" |
| 2424 | "grouping grp {notification g { description \"super g\";}}" |
| 2425 | "container top {notification h; uses grp {refine h {description \"ultra h\";}}}}", LYS_IN_YANG)); |
| 2426 | logbuf_assert("Invalid descendant-schema-nodeid value \"h\" - target node not found."); |
| 2427 | |
| 2428 | assert_non_null(mod = lys_parse_mem(ctx, "module ii {yang-version 1.1;namespace urn:ii;prefix ii;" |
| 2429 | "grouping grp {action g { description \"super g\";}}" |
| 2430 | "container top {action i; uses grp {refine i {description \"ultra i\";}}}}", LYS_IN_YANG)); |
| 2431 | logbuf_assert("Invalid descendant-schema-nodeid value \"i\" - target node not found."); |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 2432 | |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2433 | *state = NULL; |
| 2434 | ly_ctx_destroy(ctx, NULL); |
| 2435 | } |
| 2436 | |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2437 | static void |
| 2438 | test_refine(void **state) |
| 2439 | { |
| 2440 | *state = test_refine; |
| 2441 | |
| 2442 | struct ly_ctx *ctx; |
| 2443 | struct lys_module *mod; |
| 2444 | struct lysc_node *parent, *child; |
| 2445 | |
| 2446 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 2447 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2448 | 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!;}" |
| 2449 | "grouping grp {container c {leaf l {type mytype; default goodbye;}" |
| 2450 | "leaf-list ll {type mytype; default goodbye; max-elements 6;}" |
| 2451 | "choice ch {default a; leaf a {type int8;}leaf b{type uint8;}}" |
| 2452 | "leaf x {type mytype; mandatory true; must 1;}" |
| 2453 | "anydata a {mandatory false; if-feature f; description original; reference original;}" |
| 2454 | "container c {config false; leaf l {type string;}}}}}", LYS_IN_YANG)); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2455 | |
Radek Krejci | f008908 | 2019-01-07 16:42:01 +0100 | [diff] [blame] | 2456 | 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] | 2457 | "uses g:grp {refine c/l {default hello; config false;}" |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 2458 | "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] | 2459 | "refine c/ch {default b;config true; if-feature fa;}" |
Radek Krejci | f340454 | 2019-01-08 13:28:42 +0100 | [diff] [blame] | 2460 | "refine c/x {mandatory false; must ../ll;description refined; reference refined;}" |
| 2461 | "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] | 2462 | "refine c/c {config true;presence indispensable;}}}", LYS_IN_YANG)); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2463 | assert_non_null((parent = mod->compiled->data)); |
| 2464 | assert_int_equal(LYS_CONTAINER, parent->nodetype); |
| 2465 | assert_string_equal("c", parent->name); |
| 2466 | assert_non_null((child = ((struct lysc_node_container*)parent)->child)); |
| 2467 | assert_int_equal(LYS_LEAF, child->nodetype); |
| 2468 | assert_string_equal("l", child->name); |
| 2469 | assert_string_equal("hello", ((struct lysc_node_leaf*)child)->dflt); |
| 2470 | assert_int_equal(LYS_CONFIG_R, child->flags & LYS_CONFIG_MASK); |
| 2471 | assert_non_null(child = child->next); |
| 2472 | assert_int_equal(LYS_LEAFLIST, child->nodetype); |
| 2473 | assert_string_equal("ll", child->name); |
| 2474 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_node_leaflist*)child)->dflts)); |
| 2475 | assert_string_equal("hello", ((struct lysc_node_leaflist*)child)->dflts[0]); |
| 2476 | assert_string_equal("world", ((struct lysc_node_leaflist*)child)->dflts[1]); |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 2477 | assert_int_equal(2, ((struct lysc_node_leaflist*)child)->min); |
| 2478 | assert_int_equal(5, ((struct lysc_node_leaflist*)child)->max); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2479 | assert_non_null(child = child->next); |
| 2480 | assert_int_equal(LYS_CHOICE, child->nodetype); |
| 2481 | assert_string_equal("ch", child->name); |
| 2482 | assert_string_equal("b", ((struct lysc_node_choice*)child)->dflt->name); |
| 2483 | assert_true(LYS_SET_DFLT & ((struct lysc_node_choice*)child)->dflt->flags); |
| 2484 | 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] | 2485 | assert_non_null(child->iffeatures); |
| 2486 | assert_int_equal(1, LY_ARRAY_SIZE(child->iffeatures)); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2487 | assert_non_null(child = child->next); |
| 2488 | assert_int_equal(LYS_LEAF, child->nodetype); |
| 2489 | assert_string_equal("x", child->name); |
| 2490 | assert_false(LYS_MAND_TRUE & child->flags); |
| 2491 | assert_string_equal("cheers!", ((struct lysc_node_leaf*)child)->dflt); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 2492 | assert_non_null(((struct lysc_node_leaf*)child)->musts); |
| 2493 | 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] | 2494 | assert_string_equal("refined", child->dsc); |
| 2495 | assert_string_equal("refined", child->ref); |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 2496 | assert_non_null(child = child->next); |
Radek Krejci | 7f6a381 | 2019-01-07 13:48:57 +0100 | [diff] [blame] | 2497 | assert_int_equal(LYS_ANYDATA, child->nodetype); |
| 2498 | assert_string_equal("a", child->name); |
| 2499 | assert_true(LYS_MAND_TRUE & child->flags); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 2500 | assert_non_null(((struct lysc_node_anydata*)child)->musts); |
| 2501 | 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] | 2502 | assert_non_null(child->iffeatures); |
| 2503 | assert_int_equal(2, LY_ARRAY_SIZE(child->iffeatures)); |
Radek Krejci | f340454 | 2019-01-08 13:28:42 +0100 | [diff] [blame] | 2504 | assert_string_equal("refined", child->dsc); |
| 2505 | assert_string_equal("refined", child->ref); |
Radek Krejci | 7f6a381 | 2019-01-07 13:48:57 +0100 | [diff] [blame] | 2506 | assert_non_null(child = child->next); |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 2507 | assert_int_equal(LYS_CONTAINER, child->nodetype); |
| 2508 | assert_string_equal("c", child->name); |
Radek Krejci | 7f6a381 | 2019-01-07 13:48:57 +0100 | [diff] [blame] | 2509 | assert_true(LYS_PRESENCE & child->flags); |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 2510 | assert_true(LYS_CONFIG_W & child->flags); |
| 2511 | assert_true(LYS_CONFIG_W & ((struct lysc_node_container*)child)->child->flags); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2512 | |
| 2513 | 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] | 2514 | "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] | 2515 | 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] | 2516 | assert_int_equal(LYS_LEAF, child->nodetype); |
| 2517 | assert_string_equal("x", child->name); |
| 2518 | assert_false(LYS_MAND_TRUE & child->flags); |
| 2519 | assert_string_equal("hello", ((struct lysc_node_leaf*)child)->dflt); |
| 2520 | |
| 2521 | /* invalid */ |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2522 | 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] | 2523 | "uses g:grp {refine c {default hello;}}}", LYS_IN_YANG)); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2524 | logbuf_assert("Invalid refine of default in \"c\" - container cannot hold default value(s)."); |
| 2525 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2526 | 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] | 2527 | "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] | 2528 | logbuf_assert("Invalid refine of default in \"c/l\" - leaf cannot hold 2 default values."); |
| 2529 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2530 | 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] | 2531 | "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] | 2532 | logbuf_assert("Invalid refine of default in leaf-list - the default statement is allowed only in YANG 1.1 modules."); |
| 2533 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2534 | 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] | 2535 | "uses g:grp {refine c/ll {mandatory true;}}}", LYS_IN_YANG)); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2536 | logbuf_assert("Invalid refine of mandatory in \"c/ll\" - leaf-list cannot hold mandatory statement."); |
| 2537 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2538 | 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] | 2539 | "uses g:grp {refine c/l {mandatory true;}}}", LYS_IN_YANG)); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2540 | 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] | 2541 | 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] | 2542 | "uses g:grp {refine c/ch {mandatory true;}}}", LYS_IN_YANG)); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2543 | logbuf_assert("Invalid refine of mandatory in \"c/ch\" - choice already has \"default\" statement."); |
| 2544 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2545 | 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] | 2546 | "uses g:grp {refine c/ch/a/a {mandatory true;}}}", LYS_IN_YANG)); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 2547 | logbuf_assert("Invalid refine of mandatory in \"c/ch/a/a\" under the default case."); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2548 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2549 | 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] | 2550 | "uses g:grp {refine c/x {default hello;}}}", LYS_IN_YANG)); |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2551 | logbuf_assert("Invalid refine of default in \"c/x\" - the node is mandatory."); |
| 2552 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2553 | 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] | 2554 | "uses g:grp {refine c/c/l {config true;}}}", LYS_IN_YANG)); |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 2555 | logbuf_assert("Invalid refine of config in \"c/c/l\" - configuration node cannot be child of any state data node."); |
| 2556 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2557 | 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] | 2558 | "uses grp {status obsolete;}}", LYS_IN_YANG)); |
Radek Krejci | b1b5915 | 2019-01-07 13:21:56 +0100 | [diff] [blame] | 2559 | logbuf_assert("A \"deprecated\" status is in conflict with the parent's \"obsolete\" status."); |
| 2560 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2561 | 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] | 2562 | "uses g:grp {refine c/x {presence nonsence;}}}", LYS_IN_YANG)); |
Radek Krejci | 9a54f1f | 2019-01-07 13:47:55 +0100 | [diff] [blame] | 2563 | logbuf_assert("Invalid refine of presence statement in \"c/x\" - leaf cannot hold the presence statement."); |
| 2564 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2565 | 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] | 2566 | "uses g:grp {refine c/ch {must 1;}}}", LYS_IN_YANG)); |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 2567 | logbuf_assert("Invalid refine of must statement in \"c/ch\" - choice cannot hold any must statement."); |
| 2568 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2569 | 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] | 2570 | "uses g:grp {refine c/x {min-elements 1;}}}", LYS_IN_YANG)); |
Radek Krejci | 6b22ab7 | 2019-01-07 15:39:20 +0100 | [diff] [blame] | 2571 | logbuf_assert("Invalid refine of min-elements statement in \"c/x\" - leaf cannot hold this statement."); |
| 2572 | |
Radek Krejci | 096235c | 2019-01-11 11:12:19 +0100 | [diff] [blame] | 2573 | 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] | 2574 | "uses g:grp {refine c/ll {min-elements 10;}}}", LYS_IN_YANG)); |
Radek Krejci | f2271f1 | 2019-01-07 16:42:23 +0100 | [diff] [blame] | 2575 | logbuf_assert("Invalid refine of min-elements statement in \"c/ll\" - \"min-elements\" is bigger than \"max-elements\"."); |
| 2576 | |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 2577 | *state = NULL; |
| 2578 | ly_ctx_destroy(ctx, NULL); |
| 2579 | } |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 2580 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 2581 | static void |
| 2582 | test_augment(void **state) |
| 2583 | { |
| 2584 | *state = test_augment; |
| 2585 | |
| 2586 | struct ly_ctx *ctx; |
| 2587 | struct lys_module *mod; |
| 2588 | const struct lysc_node *node; |
| 2589 | const struct lysc_node_choice *ch; |
| 2590 | const struct lysc_node_case *c; |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 2591 | const struct lysc_action *rpc; |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 2592 | |
| 2593 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 2594 | |
| 2595 | ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module a {namespace urn:a;prefix a; typedef atype {type string;}" |
| 2596 | "container top {leaf a {type string;}}}"); |
| 2597 | assert_non_null(lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;import a {prefix a;}" |
| 2598 | "leaf b {type a:atype;}}", LYS_IN_YANG)); |
| 2599 | ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module c {namespace urn:c;prefix c; import a {prefix a;}" |
| 2600 | "augment /a:top/ { container c {leaf c {type a:atype;}}}}"); |
| 2601 | assert_non_null(lys_parse_mem(ctx, "module d {namespace urn:d;prefix d;import a {prefix a;} import c {prefix c;}" |
| 2602 | "augment /a:top/c:c/ { leaf d {type a:atype;} leaf c {type string;}}}", LYS_IN_YANG)); |
| 2603 | assert_non_null((mod = ly_ctx_get_module_implemented(ctx, "a"))); |
| 2604 | assert_non_null(ly_ctx_get_module_implemented(ctx, "b")); |
| 2605 | assert_non_null(ly_ctx_get_module_implemented(ctx, "c")); |
| 2606 | assert_non_null(ly_ctx_get_module_implemented(ctx, "d")); |
| 2607 | assert_non_null(node = mod->compiled->data); |
| 2608 | assert_string_equal(node->name, "top"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 2609 | assert_non_null(node = lysc_node_children(node, 0)); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 2610 | assert_string_equal(node->name, "a"); |
| 2611 | assert_non_null(node = node->next); |
| 2612 | assert_string_equal(node->name, "c"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 2613 | assert_non_null(node = lysc_node_children(node, 0)); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 2614 | assert_string_equal(node->name, "c"); |
| 2615 | assert_non_null(node = node->next); |
| 2616 | assert_string_equal(node->name, "d"); |
| 2617 | assert_non_null(node = node->next); |
| 2618 | assert_string_equal(node->name, "c"); |
| 2619 | |
| 2620 | assert_non_null((mod = lys_parse_mem(ctx, "module e {namespace urn:e;prefix e;choice ch {leaf a {type string;}}" |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 2621 | "augment /ch/c {when 1; leaf lc2 {type uint16;}}" |
| 2622 | "augment /ch { when 1; leaf b {type int8;} case c {leaf lc1 {type uint8;}}}}", LYS_IN_YANG))); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 2623 | assert_non_null((ch = (const struct lysc_node_choice*)mod->compiled->data)); |
| 2624 | assert_null(mod->compiled->data->next); |
| 2625 | assert_string_equal("ch", ch->name); |
| 2626 | assert_non_null(c = ch->cases); |
| 2627 | assert_string_equal("a", c->name); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 2628 | assert_null(c->when); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 2629 | assert_string_equal("a", c->child->name); |
| 2630 | assert_non_null(c = (const struct lysc_node_case*)c->next); |
| 2631 | assert_string_equal("b", c->name); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 2632 | assert_non_null(c->when); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 2633 | assert_string_equal("b", c->child->name); |
| 2634 | assert_non_null(c = (const struct lysc_node_case*)c->next); |
| 2635 | assert_string_equal("c", c->name); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 2636 | assert_non_null(c->when); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 2637 | assert_string_equal("lc1", ((const struct lysc_node_case*)c)->child->name); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 2638 | assert_null(((const struct lysc_node_case*)c)->child->when); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 2639 | assert_string_equal("lc2", ((const struct lysc_node_case*)c)->child->next->name); |
Radek Krejci | 00b874b | 2019-02-12 10:54:50 +0100 | [diff] [blame] | 2640 | assert_non_null(((const struct lysc_node_case*)c)->child->next->when); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 2641 | assert_ptr_equal(ch->cases->child->prev, ((const struct lysc_node_case*)c)->child->next); |
| 2642 | assert_null(c->next); |
| 2643 | |
| 2644 | assert_non_null((mod = lys_parse_mem(ctx, "module f {namespace urn:f;prefix f;grouping g {leaf a {type string;}}" |
| 2645 | "container c;" |
| 2646 | "augment /c {uses g;}}", LYS_IN_YANG))); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 2647 | assert_non_null(node = lysc_node_children(mod->compiled->data, 0)); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 2648 | assert_string_equal(node->name, "a"); |
| 2649 | |
Radek Krejci | 339f529 | 2019-02-11 16:42:34 +0100 | [diff] [blame] | 2650 | ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "submodule gsub {belongs-to g {prefix g;}" |
| 2651 | "augment /c {container sub;}}"); |
| 2652 | assert_non_null(mod = lys_parse_mem(ctx, "module g {namespace urn:g;prefix g;include gsub; container c;" |
| 2653 | "augment /c/sub {leaf main {type string;}}}", LYS_IN_YANG)); |
| 2654 | assert_non_null(mod->compiled->data); |
| 2655 | assert_string_equal("c", mod->compiled->data->name); |
| 2656 | assert_non_null(node = ((struct lysc_node_container*)mod->compiled->data)->child); |
| 2657 | assert_string_equal("sub", node->name); |
| 2658 | assert_non_null(node = ((struct lysc_node_container*)node)->child); |
| 2659 | assert_string_equal("main", node->name); |
| 2660 | |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 2661 | ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module himp {namespace urn:hi;prefix hi;container top; rpc func;}"); |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 2662 | assert_non_null(mod = lys_parse_mem(ctx, "module h {namespace urn:h;prefix h;import himp {prefix hi;}container top;" |
| 2663 | "augment /hi:top {container p {presence XXX; leaf x {mandatory true;type string;}}}" |
| 2664 | "augment /hi:top {list ll {key x;leaf x {type string;}leaf y {mandatory true; type string;}}}" |
| 2665 | "augment /hi:top {leaf l {type string; mandatory true; config false;}}" |
| 2666 | "augment /top {leaf l {type string; mandatory true;}}}", LYS_IN_YANG)); |
| 2667 | assert_non_null(node = mod->compiled->data); |
| 2668 | assert_non_null(node = ((struct lysc_node_container*)node)->child); |
| 2669 | assert_string_equal("l", node->name); |
| 2670 | assert_true(node->flags & LYS_MAND_TRUE); |
| 2671 | assert_non_null(mod = ly_ctx_get_module_implemented(ctx, "himp")); |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 2672 | assert_non_null(node = mod->compiled->data); |
| 2673 | assert_non_null(node = ((struct lysc_node_container*)node)->child); |
| 2674 | assert_string_equal("p", node->name); |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 2675 | assert_non_null(node = node->next); |
| 2676 | assert_string_equal("ll", node->name); |
| 2677 | assert_non_null(node = node->next); |
| 2678 | assert_string_equal("l", node->name); |
| 2679 | assert_true(node->flags & LYS_CONFIG_R); |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 2680 | |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 2681 | assert_non_null(lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;import himp {prefix hi;}" |
| 2682 | "augment /hi:func/input {leaf x {type string;}}" |
| 2683 | "augment /hi:func/output {leaf y {type string;}}}", LYS_IN_YANG)); |
| 2684 | assert_non_null(mod = ly_ctx_get_module_implemented(ctx, "himp")); |
| 2685 | assert_non_null(rpc = mod->compiled->rpcs); |
| 2686 | assert_int_equal(1, LY_ARRAY_SIZE(rpc)); |
| 2687 | assert_non_null(rpc->input.data); |
| 2688 | assert_string_equal("x", rpc->input.data->name); |
| 2689 | assert_null(rpc->input.data->next); |
| 2690 | assert_non_null(rpc->output.data); |
| 2691 | assert_string_equal("y", rpc->output.data->name); |
| 2692 | assert_null(rpc->output.data->next); |
| 2693 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 2694 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; container c {leaf a {type string;}}" |
| 2695 | "augment /x {leaf a {type int8;}}}", LYS_IN_YANG)); |
| 2696 | logbuf_assert("Invalid absolute-schema-nodeid value \"/x\" - target node not found."); |
| 2697 | |
| 2698 | assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; container c {leaf a {type string;}}" |
| 2699 | "augment /c {leaf a {type int8;}}}", LYS_IN_YANG)); |
Radek Krejci | 8cce853 | 2019-03-05 11:27:45 +0100 | [diff] [blame] | 2700 | logbuf_assert("Duplicate identifier \"a\" of data definition/RPC/action/Notification statement."); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 2701 | |
| 2702 | |
Radek Krejci | 339f529 | 2019-02-11 16:42:34 +0100 | [diff] [blame] | 2703 | assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc; container c {leaf a {type string;}}" |
| 2704 | "augment /c/a {leaf a {type int8;}}}", LYS_IN_YANG)); |
| 2705 | logbuf_assert("Augment's absolute-schema-nodeid \"/c/a\" refers to a leaf node which is not an allowed augment's target."); |
| 2706 | |
| 2707 | assert_null(lys_parse_mem(ctx, "module dd {namespace urn:dd;prefix dd; container c {leaf a {type string;}}" |
| 2708 | "augment /c {case b {leaf d {type int8;}}}}", LYS_IN_YANG)); |
| 2709 | logbuf_assert("Invalid augment (/c) of container node which is not allowed to contain case node \"b\"."); |
| 2710 | |
Radek Krejci | 733988a | 2019-02-15 15:12:44 +0100 | [diff] [blame] | 2711 | assert_null(lys_parse_mem(ctx, "module ee {namespace urn:ee;prefix ee; import himp {prefix hi;}" |
| 2712 | "augment /hi:top {container c {leaf d {mandatory true; type int8;}}}}", LYS_IN_YANG)); |
| 2713 | logbuf_assert("Invalid augment (/hi:top) adding mandatory node \"c\" without making it conditional via when statement."); |
Radek Krejci | fe90963 | 2019-02-12 15:34:42 +0100 | [diff] [blame] | 2714 | |
Radek Krejci | 3641f56 | 2019-02-13 15:38:40 +0100 | [diff] [blame] | 2715 | assert_null(lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff; container top;" |
| 2716 | "augment ../top {leaf x {type int8;}}}", LYS_IN_YANG)); |
| 2717 | logbuf_assert("Invalid absolute-schema-nodeid value \"../top\" - missing starting \"/\"."); |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 2718 | |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 2719 | assert_null(lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg; rpc func;" |
| 2720 | "augment /func {leaf x {type int8;}}}", LYS_IN_YANG)); |
| 2721 | logbuf_assert("Augment's absolute-schema-nodeid \"/func\" refers to a RPC/action node which is not an allowed augment's target."); |
| 2722 | |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 2723 | *state = NULL; |
| 2724 | ly_ctx_destroy(ctx, NULL); |
| 2725 | } |
| 2726 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 2727 | static void |
| 2728 | test_deviation(void **state) |
| 2729 | { |
| 2730 | *state = test_deviation; |
| 2731 | |
| 2732 | struct ly_ctx *ctx; |
| 2733 | struct lys_module *mod; |
| 2734 | const struct lysc_node *node; |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 2735 | const struct lysc_node_list *list; |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 2736 | |
| 2737 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 2738 | |
| 2739 | ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module a {namespace urn:a;prefix a;" |
Radek Krejci | 88ef64b | 2019-02-18 16:02:06 +0100 | [diff] [blame] | 2740 | "container top {leaf a {type string;} leaf b {type string;} leaf c {type string;}}" |
| 2741 | "choice ch {default c; case b {leaf b{type string;}} case a {leaf a{type string;} leaf x {type string;}}" |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 2742 | " case c {leaf c{type string;}}}" |
| 2743 | "rpc func1 { input { leaf x {type int8;}} output {leaf y {type int8;}}}" |
| 2744 | "rpc func2;}"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 2745 | assert_non_null(lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;import a {prefix a;}" |
Radek Krejci | 88ef64b | 2019-02-18 16:02:06 +0100 | [diff] [blame] | 2746 | "deviation /a:top/a:b {deviate not-supported;}" |
| 2747 | "deviation /a:ch/a:a/a:x {deviate not-supported;}" |
| 2748 | "deviation /a:ch/a:c/a:c {deviate not-supported;}" |
| 2749 | "deviation /a:ch/a:b {deviate not-supported;}" |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 2750 | "deviation /a:ch/a:a/a:a {deviate not-supported;}" |
| 2751 | "deviation /a:func1/a:input {deviate not-supported;}" |
| 2752 | "deviation /a:func1/a:output {deviate not-supported;}" |
| 2753 | "deviation /a:func2 {deviate not-supported;}}", LYS_IN_YANG)); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 2754 | assert_non_null((mod = ly_ctx_get_module_implemented(ctx, "a"))); |
| 2755 | assert_non_null(node = mod->compiled->data); |
| 2756 | assert_string_equal(node->name, "top"); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 2757 | assert_non_null(node = lysc_node_children(node, 0)); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 2758 | assert_string_equal(node->name, "a"); |
| 2759 | assert_non_null(node = node->next); |
| 2760 | assert_string_equal(node->name, "c"); |
Radek Krejci | 88ef64b | 2019-02-18 16:02:06 +0100 | [diff] [blame] | 2761 | assert_null(node = node->next); |
| 2762 | assert_non_null(node = mod->compiled->data->next); |
| 2763 | assert_string_equal("ch", node->name); |
| 2764 | assert_null(((struct lysc_node_choice*)node)->dflt); |
| 2765 | assert_null(((struct lysc_node_choice*)node)->cases); |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 2766 | assert_int_equal(1, LY_ARRAY_SIZE(mod->compiled->rpcs)); |
| 2767 | assert_null(mod->compiled->rpcs[0].input.data); |
| 2768 | assert_null(mod->compiled->rpcs[0].output.data); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 2769 | |
| 2770 | assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c; typedef mytype {type string; units kilometers;}" |
| 2771 | "leaf c1 {type mytype;} leaf c2 {type mytype; units meters;} leaf c3 {type mytype; units meters;}" |
| 2772 | "deviation /c1 {deviate add {units meters;}}" |
| 2773 | "deviation /c2 {deviate delete {units meters;}}" |
| 2774 | "deviation /c3 {deviate replace {units centimeters;}}}", LYS_IN_YANG)); |
| 2775 | assert_non_null(node = mod->compiled->data); |
| 2776 | assert_string_equal("c1", node->name); |
| 2777 | assert_string_equal("meters", ((struct lysc_node_leaf*)node)->units); |
| 2778 | assert_non_null(node = node->next); |
| 2779 | assert_string_equal("c2", node->name); |
| 2780 | assert_null(((struct lysc_node_leaf*)node)->units); |
| 2781 | assert_non_null(node = node->next); |
| 2782 | assert_string_equal("c3", node->name); |
| 2783 | assert_string_equal("centimeters", ((struct lysc_node_leaf*)node)->units); |
| 2784 | |
| 2785 | assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d; leaf c1 {type string; must 1;}" |
Radek Krejci | b4532d1 | 2019-02-15 16:13:29 +0100 | [diff] [blame] | 2786 | "container c2 {presence yes; must 1; must 2;} leaf c3 {type string; must 1; must 3;}" |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 2787 | "deviation /c1 {deviate add {must 3;}}" |
| 2788 | "deviation /c2 {deviate delete {must 2;}}" |
| 2789 | "deviation /c3 {deviate delete {must 3; must 1;}}}", LYS_IN_YANG)); |
| 2790 | assert_non_null(node = mod->compiled->data); |
| 2791 | assert_string_equal("c1", node->name); |
| 2792 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_node_leaf*)node)->musts)); |
| 2793 | assert_string_equal("3", ((struct lysc_node_leaf*)node)->musts[1].cond->expr); |
| 2794 | assert_non_null(node = node->next); |
| 2795 | assert_string_equal("c2", node->name); |
Radek Krejci | b4532d1 | 2019-02-15 16:13:29 +0100 | [diff] [blame] | 2796 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_node_container*)node)->musts)); |
| 2797 | assert_string_equal("1", ((struct lysc_node_container*)node)->musts[0].cond->expr); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 2798 | assert_non_null(node = node->next); |
| 2799 | assert_string_equal("c3", node->name); |
| 2800 | assert_null(((struct lysc_node_leaf*)node)->musts); |
| 2801 | |
Radek Krejci | b4532d1 | 2019-02-15 16:13:29 +0100 | [diff] [blame] | 2802 | ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module e {yang-version 1.1; namespace urn:e;prefix e; typedef mytype {type string; default nothing;}" |
Radek Krejci | 468a94f | 2019-02-15 14:52:36 +0100 | [diff] [blame] | 2803 | "choice a {default a;leaf a {type string;} leaf b {type string;} leaf c {type string; mandatory true;}}" |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 2804 | "choice b {default a;leaf a {type string;} leaf b {type string;}}" |
| 2805 | "leaf c {default hello; type string;}" |
Radek Krejci | b4532d1 | 2019-02-15 16:13:29 +0100 | [diff] [blame] | 2806 | "leaf-list d {default hello; default world; type string;}" |
| 2807 | "leaf c2 {type mytype;} leaf-list d2 {type mytype;}}"); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 2808 | assert_non_null(lys_parse_mem(ctx, "module f {yang-version 1.1; namespace urn:f;prefix f;import e {prefix x;}" |
| 2809 | "deviation /x:a {deviate delete {default a;}}" |
| 2810 | "deviation /x:b {deviate delete {default x:a;}}" |
| 2811 | "deviation /x:c {deviate delete {default hello;}}" |
| 2812 | "deviation /x:d {deviate delete {default world;}}}", LYS_IN_YANG)); |
| 2813 | assert_non_null((mod = ly_ctx_get_module_implemented(ctx, "e"))); |
| 2814 | assert_non_null(node = mod->compiled->data); |
| 2815 | assert_null(((struct lysc_node_choice*)node)->dflt); |
| 2816 | assert_non_null(node = node->next); |
| 2817 | assert_null(((struct lysc_node_choice*)node)->dflt); |
| 2818 | assert_non_null(node = node->next); |
| 2819 | assert_null(((struct lysc_node_leaf*)node)->dflt); |
| 2820 | assert_non_null(node = node->next); |
| 2821 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_node_leaflist*)node)->dflts)); |
| 2822 | assert_string_equal("hello", ((struct lysc_node_leaflist*)node)->dflts[0]); |
Radek Krejci | b4532d1 | 2019-02-15 16:13:29 +0100 | [diff] [blame] | 2823 | assert_non_null(node = node->next); |
| 2824 | assert_string_equal("nothing", ((struct lysc_node_leaf*)node)->dflt); |
| 2825 | assert_non_null(node = node->next); |
| 2826 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_node_leaflist*)node)->dflts)); |
| 2827 | assert_string_equal("nothing", ((struct lysc_node_leaflist*)node)->dflts[0]); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 2828 | |
| 2829 | assert_non_null(lys_parse_mem(ctx, "module g {yang-version 1.1; namespace urn:g;prefix g;import e {prefix x;}" |
| 2830 | "deviation /x:b {deviate add {default x:b;}}" |
| 2831 | "deviation /x:c {deviate add {default bye;}}" |
Radek Krejci | b4532d1 | 2019-02-15 16:13:29 +0100 | [diff] [blame] | 2832 | "deviation /x:d {deviate add {default all; default people;}}" |
| 2833 | "deviation /x:c2 {deviate add {default hi;}}" |
| 2834 | "deviation /x:d2 {deviate add {default hi; default all;}}}", LYS_IN_YANG)); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 2835 | assert_non_null((mod = ly_ctx_get_module_implemented(ctx, "e"))); |
| 2836 | assert_non_null(node = mod->compiled->data); |
| 2837 | assert_null(((struct lysc_node_choice*)node)->dflt); |
| 2838 | assert_non_null(node = node->next); |
| 2839 | assert_non_null(((struct lysc_node_choice*)node)->dflt); |
| 2840 | assert_string_equal("b", ((struct lysc_node_choice*)node)->dflt->name); |
| 2841 | assert_non_null(node = node->next); |
| 2842 | assert_non_null(((struct lysc_node_leaf*)node)->dflt); |
| 2843 | assert_string_equal("bye", ((struct lysc_node_leaf*)node)->dflt); |
| 2844 | assert_non_null(node = node->next); |
| 2845 | assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_node_leaflist*)node)->dflts)); |
| 2846 | assert_string_equal("hello", ((struct lysc_node_leaflist*)node)->dflts[0]); |
| 2847 | assert_string_equal("all", ((struct lysc_node_leaflist*)node)->dflts[1]); |
| 2848 | assert_string_equal("people", ((struct lysc_node_leaflist*)node)->dflts[2]); |
Radek Krejci | b4532d1 | 2019-02-15 16:13:29 +0100 | [diff] [blame] | 2849 | assert_non_null(node = node->next); |
| 2850 | assert_non_null(((struct lysc_node_leaf*)node)->dflt); |
| 2851 | assert_string_equal("hi", ((struct lysc_node_leaf*)node)->dflt); |
| 2852 | assert_non_null(node = node->next); |
| 2853 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_node_leaflist*)node)->dflts)); |
| 2854 | assert_string_equal("hi", ((struct lysc_node_leaflist*)node)->dflts[0]); |
| 2855 | assert_string_equal("all", ((struct lysc_node_leaflist*)node)->dflts[1]); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 2856 | |
| 2857 | assert_non_null(lys_parse_mem(ctx, "module h {yang-version 1.1; namespace urn:h;prefix h;import e {prefix x;}" |
| 2858 | "deviation /x:b {deviate replace {default x:a;}}" |
| 2859 | "deviation /x:c {deviate replace {default hello;}}}", LYS_IN_YANG)); |
| 2860 | assert_non_null((mod = ly_ctx_get_module_implemented(ctx, "e"))); |
| 2861 | assert_non_null(node = mod->compiled->data); |
| 2862 | assert_null(((struct lysc_node_choice*)node)->dflt); |
| 2863 | assert_non_null(node = node->next); |
| 2864 | assert_non_null(((struct lysc_node_choice*)node)->dflt); |
| 2865 | assert_string_equal("a", ((struct lysc_node_choice*)node)->dflt->name); |
| 2866 | assert_non_null(node = node->next); |
| 2867 | assert_non_null(((struct lysc_node_leaf*)node)->dflt); |
| 2868 | assert_string_equal("hello", ((struct lysc_node_leaf*)node)->dflt); |
| 2869 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 2870 | ly_ctx_set_module_imp_clb(ctx, test_imp_clb, "module i {namespace urn:i;prefix i;" |
| 2871 | "list l1 {key a; leaf a {type string;} leaf b {type string;} leaf c {type string;}}" |
| 2872 | "list l2 {key a; unique \"b c\"; unique \"d\"; leaf a {type string;} leaf b {type string;}" |
| 2873 | " leaf c {type string;} leaf d {type string;}}}"); |
| 2874 | assert_non_null(lys_parse_mem(ctx, "module j {namespace urn:j;prefix j;import i {prefix i;}" |
| 2875 | "augment /i:l1 {leaf j_c {type string;}}" |
| 2876 | "deviation /i:l1 {deviate add {unique \"i:b j_c\"; }}" |
| 2877 | "deviation /i:l1 {deviate add {unique \"i:c\";}}" |
| 2878 | "deviation /i:l2 {deviate delete {unique \"d\"; unique \"b c\";}}}", LYS_IN_YANG)); |
| 2879 | assert_non_null((mod = ly_ctx_get_module_implemented(ctx, "i"))); |
| 2880 | assert_non_null(list = (struct lysc_node_list*)mod->compiled->data); |
| 2881 | assert_string_equal("l1", list->name); |
| 2882 | assert_int_equal(2, LY_ARRAY_SIZE(list->uniques)); |
| 2883 | assert_int_equal(2, LY_ARRAY_SIZE(list->uniques[0])); |
| 2884 | assert_string_equal("b", list->uniques[0][0]->name); |
| 2885 | assert_string_equal("j_c", list->uniques[0][1]->name); |
| 2886 | assert_int_equal(1, LY_ARRAY_SIZE(list->uniques[1])); |
| 2887 | assert_string_equal("c", list->uniques[1][0]->name); |
| 2888 | assert_non_null(list = (struct lysc_node_list*)list->next); |
| 2889 | assert_string_equal("l2", list->name); |
| 2890 | assert_null(list->uniques); |
| 2891 | |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 2892 | assert_non_null(mod = lys_parse_mem(ctx, "module k {namespace urn:k;prefix k; leaf a {type string;}" |
| 2893 | "container top {leaf x {type string;} leaf y {type string; config false;}}" |
| 2894 | "deviation /a {deviate add {config false; }}" |
| 2895 | "deviation /top {deviate add {config false;}}}", LYS_IN_YANG)); |
| 2896 | assert_non_null(node = mod->compiled->data); |
| 2897 | assert_string_equal("a", node->name); |
| 2898 | assert_true(node->flags & LYS_CONFIG_R); |
| 2899 | assert_non_null(node = node->next); |
| 2900 | assert_string_equal("top", node->name); |
| 2901 | assert_true(node->flags & LYS_CONFIG_R); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 2902 | assert_non_null(node = lysc_node_children(node, 0)); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 2903 | assert_string_equal("x", node->name); |
| 2904 | assert_true(node->flags & LYS_CONFIG_R); |
| 2905 | assert_non_null(node = node->next); |
| 2906 | assert_string_equal("y", node->name); |
| 2907 | assert_true(node->flags & LYS_CONFIG_R); |
| 2908 | |
| 2909 | assert_non_null(mod = lys_parse_mem(ctx, "module l {namespace urn:l;prefix l; leaf a {config false; type string;}" |
| 2910 | "container top {config false; leaf x {type string;}}" |
| 2911 | "deviation /a {deviate replace {config true;}}" |
| 2912 | "deviation /top {deviate replace {config true;}}}", LYS_IN_YANG)); |
| 2913 | assert_non_null(node = mod->compiled->data); |
| 2914 | assert_string_equal("a", node->name); |
| 2915 | assert_true(node->flags & LYS_CONFIG_W); |
| 2916 | assert_non_null(node = node->next); |
| 2917 | assert_string_equal("top", node->name); |
| 2918 | assert_true(node->flags & LYS_CONFIG_W); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 2919 | assert_non_null(node = lysc_node_children(node, 0)); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 2920 | assert_string_equal("x", node->name); |
| 2921 | assert_true(node->flags & LYS_CONFIG_W); |
| 2922 | |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 2923 | assert_non_null(mod = lys_parse_mem(ctx, "module m {namespace urn:m;prefix m;" |
| 2924 | "container a {leaf a {type string;}}" |
| 2925 | "container b {leaf b {mandatory true; type string;}}" |
| 2926 | "deviation /a/a {deviate add {mandatory true;}}" |
| 2927 | "deviation /b/b {deviate replace {mandatory false;}}}", LYS_IN_YANG)); |
| 2928 | assert_non_null(node = mod->compiled->data); |
| 2929 | assert_string_equal("a", node->name); |
| 2930 | assert_true((node->flags & LYS_MAND_MASK) == LYS_MAND_TRUE); |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 2931 | assert_true((lysc_node_children(node, 0)->flags & LYS_MAND_MASK) == LYS_MAND_TRUE); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 2932 | assert_non_null(node = node->next); |
| 2933 | assert_string_equal("b", node->name); |
| 2934 | assert_false(node->flags & LYS_MAND_MASK); /* just unset on container */ |
Radek Krejci | 6eeb58f | 2019-02-22 16:29:37 +0100 | [diff] [blame] | 2935 | assert_true((lysc_node_children(node, 0)->flags & LYS_MAND_MASK) == LYS_MAND_FALSE); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 2936 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 2937 | assert_non_null(mod = lys_parse_mem(ctx, "module n {yang-version 1.1; namespace urn:n;prefix n;" |
| 2938 | "leaf a {default test; type string;}" |
| 2939 | "leaf b {mandatory true; type string;}" |
| 2940 | "deviation /a {deviate add {mandatory true;} deviate delete {default test;}}" |
| 2941 | "deviation /b {deviate add {default test;} deviate replace {mandatory false;}}}", LYS_IN_YANG)); |
| 2942 | assert_non_null(node = mod->compiled->data); |
| 2943 | assert_string_equal("a", node->name); |
| 2944 | assert_null(((struct lysc_node_leaf*)node)->dflt); |
| 2945 | assert_true((node->flags & LYS_MAND_MASK) == LYS_MAND_TRUE); |
| 2946 | assert_non_null(node = node->next); |
| 2947 | assert_string_equal("b", node->name); |
| 2948 | assert_non_null(((struct lysc_node_leaf*)node)->dflt); |
| 2949 | assert_true((node->flags & LYS_MAND_MASK) == LYS_MAND_FALSE); |
| 2950 | |
| 2951 | assert_non_null(mod = lys_parse_mem(ctx, "module o {namespace urn:o;prefix o;" |
| 2952 | "leaf-list a {type string;}" |
| 2953 | "list b {config false;}" |
| 2954 | "leaf-list c {min-elements 1; max-elements 10; type string;}" |
| 2955 | "list d {min-elements 10; max-elements 100; config false;}" |
| 2956 | "deviation /a {deviate add {min-elements 1; max-elements 10;}}" |
| 2957 | "deviation /b {deviate add {min-elements 10; max-elements 100;}}" |
| 2958 | "deviation /c {deviate replace {min-elements 10; max-elements 100;}}" |
| 2959 | "deviation /d {deviate replace {min-elements 1; max-elements 10;}}}", LYS_IN_YANG)); |
| 2960 | assert_non_null(node = mod->compiled->data); |
| 2961 | assert_string_equal("a", node->name); |
| 2962 | assert_int_equal(1, ((struct lysc_node_leaflist*)node)->min); |
| 2963 | assert_int_equal(10, ((struct lysc_node_leaflist*)node)->max); |
| 2964 | assert_non_null(node = node->next); |
| 2965 | assert_string_equal("b", node->name); |
| 2966 | assert_int_equal(10, ((struct lysc_node_list*)node)->min); |
| 2967 | assert_int_equal(100, ((struct lysc_node_list*)node)->max); |
| 2968 | assert_non_null(node = node->next); |
| 2969 | assert_string_equal("c", node->name); |
| 2970 | assert_int_equal(10, ((struct lysc_node_leaflist*)node)->min); |
| 2971 | assert_int_equal(100, ((struct lysc_node_leaflist*)node)->max); |
| 2972 | assert_non_null(node = node->next); |
| 2973 | assert_string_equal("d", node->name); |
| 2974 | assert_int_equal(1, ((struct lysc_node_list*)node)->min); |
| 2975 | assert_int_equal(10, ((struct lysc_node_list*)node)->max); |
| 2976 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 2977 | assert_non_null(mod = lys_parse_mem(ctx, "module p {yang-version 1.1; namespace urn:p;prefix p; typedef mytype {type int8; default 1;}" |
| 2978 | "leaf a {type string; default 10;} leaf-list b {type string;}" |
| 2979 | "deviation /a {deviate replace {type mytype;}}" |
| 2980 | "deviation /b {deviate replace {type mytype;}}}", LYS_IN_YANG)); |
| 2981 | assert_non_null(node = mod->compiled->data); |
| 2982 | assert_string_equal("a", node->name); |
| 2983 | assert_int_equal(LY_TYPE_INT8, ((struct lysc_node_leaf*)node)->type->basetype); |
| 2984 | assert_string_equal("10", ((struct lysc_node_leaf*)node)->dflt); |
| 2985 | assert_non_null(node = node->next); |
| 2986 | assert_string_equal("b", node->name); |
| 2987 | assert_int_equal(LY_TYPE_INT8, ((struct lysc_node_leaflist*)node)->type->basetype); |
| 2988 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_node_leaflist*)node)->dflts)); |
| 2989 | assert_string_equal("1", ((struct lysc_node_leaflist*)node)->dflts[0]); |
| 2990 | |
Radek Krejci | 88ef64b | 2019-02-18 16:02:06 +0100 | [diff] [blame] | 2991 | assert_null(lys_parse_mem(ctx, "module aa1 {namespace urn:aa1;prefix aa1;import a {prefix a;}" |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 2992 | "deviation /a:top/a:z {deviate not-supported;}}", LYS_IN_YANG)); |
| 2993 | logbuf_assert("Invalid absolute-schema-nodeid value \"/a:top/a:z\" - target node not found."); |
Radek Krejci | 88ef64b | 2019-02-18 16:02:06 +0100 | [diff] [blame] | 2994 | assert_non_null(lys_parse_mem(ctx, "module aa2 {namespace urn:aa2;prefix aa2;import a {prefix a;}" |
| 2995 | "deviation /a:top/a:a {deviate not-supported;}" |
| 2996 | "deviation /a:top/a:a {deviate add {default error;}}}", LYS_IN_YANG)); |
| 2997 | /* warning */ |
| 2998 | logbuf_assert("Useless multiple (2) deviates on node \"/a:top/a:a\" since the node is not-supported."); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 2999 | |
| 3000 | assert_null(lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;import a {prefix a;}" |
| 3001 | "deviation a:top/a:a {deviate not-supported;}}", LYS_IN_YANG)); |
| 3002 | logbuf_assert("Invalid absolute-schema-nodeid value \"a:top/a:a\" - missing starting \"/\"."); |
| 3003 | |
| 3004 | assert_null(lys_parse_mem(ctx, "module cc {namespace urn:cc;prefix cc; container c;" |
| 3005 | "deviation /c {deviate add {units meters;}}}", LYS_IN_YANG)); |
| 3006 | logbuf_assert("Invalid deviation (/c) of container node - it is not possible to add \"units\" property."); |
| 3007 | assert_null(lys_parse_mem(ctx, "module cd {namespace urn:cd;prefix cd; leaf c {type string; units centimeters;}" |
| 3008 | "deviation /c {deviate add {units meters;}}}", LYS_IN_YANG)); |
| 3009 | logbuf_assert("Invalid deviation (/c) adding \"units\" property which already exists (with value \"centimeters\")."); |
| 3010 | |
| 3011 | assert_null(lys_parse_mem(ctx, "module dd1 {namespace urn:dd1;prefix dd1; container c;" |
| 3012 | "deviation /c {deviate delete {units meters;}}}", LYS_IN_YANG)); |
| 3013 | logbuf_assert("Invalid deviation (/c) of container node - it is not possible to delete \"units\" property."); |
| 3014 | assert_null(lys_parse_mem(ctx, "module dd2 {namespace urn:dd2;prefix dd2; leaf c {type string;}" |
| 3015 | "deviation /c {deviate delete {units meters;}}}", LYS_IN_YANG)); |
| 3016 | logbuf_assert("Invalid deviation (/c) deleting \"units\" property \"meters\" which is not present."); |
| 3017 | assert_null(lys_parse_mem(ctx, "module dd3 {namespace urn:dd3;prefix dd3; leaf c {type string; units centimeters;}" |
| 3018 | "deviation /c {deviate delete {units meters;}}}", LYS_IN_YANG)); |
| 3019 | logbuf_assert("Invalid deviation (/c) deleting \"units\" property \"meters\" which does not match the target's property value \"centimeters\"."); |
| 3020 | |
| 3021 | assert_null(lys_parse_mem(ctx, "module ee1 {namespace urn:ee1;prefix ee1; container c;" |
| 3022 | "deviation /c {deviate replace {units meters;}}}", LYS_IN_YANG)); |
| 3023 | logbuf_assert("Invalid deviation (/c) of container node - it is not possible to replace \"units\" property."); |
| 3024 | assert_null(lys_parse_mem(ctx, "module ee2 {namespace urn:ee2;prefix ee2; leaf c {type string;}" |
| 3025 | "deviation /c {deviate replace {units meters;}}}", LYS_IN_YANG)); |
| 3026 | logbuf_assert("Invalid deviation (/c) replacing \"units\" property \"meters\" which is not present."); |
| 3027 | |
| 3028 | /* the default is already deleted in /e:a byt module f */ |
| 3029 | assert_null(lys_parse_mem(ctx, "module ff1 {namespace urn:ff1;prefix ff1; import e {prefix e;}" |
| 3030 | "deviation /e:a {deviate delete {default x:a;}}}", LYS_IN_YANG)); |
| 3031 | logbuf_assert("Invalid deviation (/e:a) deleting \"default\" property \"x:a\" which is not present."); |
| 3032 | assert_null(lys_parse_mem(ctx, "module ff2 {namespace urn:ff2;prefix ff2; import e {prefix e;}" |
| 3033 | "deviation /e:b {deviate delete {default x:a;}}}", LYS_IN_YANG)); |
| 3034 | logbuf_assert("Invalid deviation (/e:b) deleting \"default\" property \"x:a\" of choice. " |
| 3035 | "The prefix does not match any imported module of the deviation module."); |
Radek Krejci | 88ef64b | 2019-02-18 16:02:06 +0100 | [diff] [blame] | 3036 | assert_null(lys_parse_mem(ctx, "module ff3 {namespace urn:ff3;prefix ff3; import e {prefix e;}" |
| 3037 | "deviation /e:b {deviate delete {default e:b;}}}", LYS_IN_YANG)); |
| 3038 | logbuf_assert("Invalid deviation (/e:b) deleting \"default\" property \"e:b\" of choice does not match the default case name \"a\"."); |
| 3039 | assert_null(lys_parse_mem(ctx, "module ff4 {namespace urn:ff4;prefix ff4; import e {prefix e;}" |
| 3040 | "deviation /e:b {deviate delete {default ff4:a;}}}", LYS_IN_YANG)); |
| 3041 | logbuf_assert("Invalid deviation (/e:b) deleting \"default\" property \"ff4:a\" of choice. The prefix does not match the default case's module."); |
| 3042 | assert_null(lys_parse_mem(ctx, "module ff5 {namespace urn:ff5;prefix ff5; anyxml a;" |
| 3043 | "deviation /a {deviate delete {default x;}}}", LYS_IN_YANG)); |
| 3044 | logbuf_assert("Invalid deviation (/a) of anyxml node - it is not possible to delete \"default\" property."); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3045 | |
| 3046 | assert_null(lys_parse_mem(ctx, "module gg1 {namespace urn:gg1;prefix gg1; import e {prefix e;}" |
| 3047 | "deviation /e:b {deviate add {default e:a;}}}", LYS_IN_YANG)); |
| 3048 | logbuf_assert("Invalid deviation (/e:b) adding \"default\" property which already exists (with value \"a\")."); |
| 3049 | assert_null(lys_parse_mem(ctx, "module gg2 {namespace urn:gg2;prefix gg2; import e {prefix e;}" |
Radek Krejci | 468a94f | 2019-02-15 14:52:36 +0100 | [diff] [blame] | 3050 | "deviation /e:a {deviate add {default x:a;}}}", LYS_IN_YANG)); |
| 3051 | logbuf_assert("Invalid deviation (/e:a) adding \"default\" property \"x:a\" of choice. " |
| 3052 | "The prefix does not match any imported module of the deviation module."); |
| 3053 | assert_null(lys_parse_mem(ctx, "module gg3 {namespace urn:gg3;prefix gg3; import e {prefix e;}" |
| 3054 | "deviation /e:a {deviate add {default a;}}}", LYS_IN_YANG)); |
| 3055 | logbuf_assert("Invalid deviation (/e:a) adding \"default\" property \"a\" of choice - the specified case does not exists."); |
| 3056 | assert_null(lys_parse_mem(ctx, "module gg4 {namespace urn:gg4;prefix gg4; import e {prefix e;}" |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3057 | "deviation /e:c {deviate add {default hi;}}}", LYS_IN_YANG)); |
| 3058 | logbuf_assert("Invalid deviation (/e:c) adding \"default\" property which already exists (with value \"hello\")."); |
Radek Krejci | 468a94f | 2019-02-15 14:52:36 +0100 | [diff] [blame] | 3059 | assert_null(lys_parse_mem(ctx, "module gg4 {namespace urn:gg4;prefix gg4; import e {prefix e;}" |
| 3060 | "deviation /e:a {deviate add {default e:c;}}}", LYS_IN_YANG)); |
| 3061 | logbuf_assert("Invalid deviation (/e:a) adding \"default\" property \"e:c\" of choice - mandatory node \"c\" under the default case."); |
Radek Krejci | 88ef64b | 2019-02-18 16:02:06 +0100 | [diff] [blame] | 3062 | assert_null(lys_parse_mem(ctx, "module gg5 {namespace urn:gg5;prefix gg5; leaf x {type string; mandatory true;}" |
| 3063 | "deviation /x {deviate add {default error;}}}", LYS_IN_YANG)); |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 3064 | logbuf_assert("Invalid deviation (/x) combining default value and mandatory leaf."); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3065 | |
| 3066 | assert_null(lys_parse_mem(ctx, "module hh1 {yang-version 1.1; namespace urn:hh1;prefix hh1; import e {prefix e;}" |
| 3067 | "deviation /e:d {deviate replace {default hi;}}}", LYS_IN_YANG)); |
| 3068 | logbuf_assert("Invalid deviation (/e:d) of leaf-list node - it is not possible to replace \"default\" property."); |
| 3069 | |
Radek Krejci | 7af6424 | 2019-02-18 13:07:53 +0100 | [diff] [blame] | 3070 | assert_null(lys_parse_mem(ctx, "module ii1 {namespace urn:ii1;prefix ii1; import i {prefix i;}" |
| 3071 | "deviation /i:l1 {deviate delete {unique x;}}}", LYS_IN_YANG)); |
| 3072 | logbuf_assert("Invalid deviation (/i:l1) deleting \"unique\" property \"x\" which does not match any of the target's property values."); |
| 3073 | assert_null(lys_parse_mem(ctx, "module ii2 {namespace urn:ii2;prefix ii2; import i {prefix i;} leaf x { type string;}" |
| 3074 | "deviation /i:l2 {deviate delete {unique d;}}}", LYS_IN_YANG)); |
| 3075 | logbuf_assert("Invalid deviation (/i:l2) deleting \"unique\" property \"d\" which does not match any of the target's property values."); |
| 3076 | assert_null(lys_parse_mem(ctx, "module ii3 {namespace urn:ii3;prefix ii3; leaf x { type string;}" |
| 3077 | "deviation /x {deviate delete {unique d;}}}", LYS_IN_YANG)); |
| 3078 | logbuf_assert("Invalid deviation (/x) of leaf node - it is not possible to delete \"unique\" property."); |
| 3079 | assert_null(lys_parse_mem(ctx, "module ii4 {namespace urn:ii4;prefix ii4; leaf x { type string;}" |
| 3080 | "deviation /x {deviate add {unique d;}}}", LYS_IN_YANG)); |
| 3081 | logbuf_assert("Invalid deviation (/x) of leaf node - it is not possible to add \"unique\" property."); |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3082 | |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 3083 | assert_null(lys_parse_mem(ctx, "module jj1 {namespace urn:jj1;prefix jj1; choice ch {case a {leaf a{type string;}}}" |
| 3084 | "deviation /ch/a {deviate add {config false;}}}", LYS_IN_YANG)); |
| 3085 | logbuf_assert("Invalid deviation (/ch/a) of case node - it is not possible to add \"config\" property."); |
| 3086 | assert_null(lys_parse_mem(ctx, "module jj2 {namespace urn:jj2;prefix jj2; container top {config false; leaf x {type string;}}" |
| 3087 | "deviation /top/x {deviate add {config true;}}}", LYS_IN_YANG)); |
| 3088 | logbuf_assert("Invalid deviation of config in \"/top/x\" - configuration node cannot be child of any state data node."); |
| 3089 | assert_null(lys_parse_mem(ctx, "module jj3 {namespace urn:jj3;prefix jj3; container top {leaf x {type string;}}" |
| 3090 | "deviation /top/x {deviate replace {config false;}}}", LYS_IN_YANG)); |
| 3091 | logbuf_assert("Invalid deviation (/top/x) replacing \"config\" property \"config false\" which is not present."); |
| 3092 | assert_null(lys_parse_mem(ctx, "module jj4 {namespace urn:jj4;prefix jj4; choice ch {case a {leaf a{type string;}}}" |
| 3093 | "deviation /ch/a {deviate replace {config false;}}}", LYS_IN_YANG)); |
| 3094 | logbuf_assert("Invalid deviation (/ch/a) of case node - it is not possible to replace \"config\" property."); |
| 3095 | assert_null(lys_parse_mem(ctx, "module jj5 {namespace urn:jj5;prefix jj5; container top {leaf x {type string; config true;}}" |
| 3096 | "deviation /top {deviate add {config false;}}}", LYS_IN_YANG)); |
| 3097 | logbuf_assert("Invalid deviation of config in \"/top\" - configuration node cannot be child of any state data node."); |
Radek Krejci | f1421c2 | 2019-02-19 13:05:20 +0100 | [diff] [blame] | 3098 | assert_null(lys_parse_mem(ctx, "module jj6 {namespace urn:jj6;prefix jj6; leaf x {config false; type string;}" |
| 3099 | "deviation /x {deviate add {config true;}}}", LYS_IN_YANG)); |
| 3100 | logbuf_assert("Invalid deviation (/x) adding \"config\" property which already exists (with value \"config false\")."); |
| 3101 | |
| 3102 | assert_null(lys_parse_mem(ctx, "module kk1 {namespace urn:kk1;prefix kk1; container top {leaf a{type string;}}" |
| 3103 | "deviation /top {deviate add {mandatory true;}}}", LYS_IN_YANG)); |
| 3104 | logbuf_assert("Invalid deviation of mandatory in \"/top\" - container cannot hold mandatory statement."); |
| 3105 | assert_null(lys_parse_mem(ctx, "module kk2 {namespace urn:kk2;prefix kk2; container top {leaf a{type string;}}" |
| 3106 | "deviation /top {deviate replace {mandatory true;}}}", LYS_IN_YANG)); |
| 3107 | logbuf_assert("Invalid deviation (/top) replacing \"mandatory\" property \"mandatory true\" which is not present."); |
| 3108 | assert_null(lys_parse_mem(ctx, "module kk3 {namespace urn:kk3;prefix kk3; container top {leaf x {type string;}}" |
| 3109 | "deviation /top/x {deviate replace {mandatory true;}}}", LYS_IN_YANG)); |
| 3110 | logbuf_assert("Invalid deviation (/top/x) replacing \"mandatory\" property \"mandatory true\" which is not present."); |
| 3111 | assert_null(lys_parse_mem(ctx, "module kk4 {namespace urn:kk4;prefix kk4; leaf x {mandatory true; type string;}" |
| 3112 | "deviation /x {deviate add {mandatory false;}}}", LYS_IN_YANG)); |
| 3113 | logbuf_assert("Invalid deviation (/x) adding \"mandatory\" property which already exists (with value \"mandatory true\")."); |
Radek Krejci | 93dcc39 | 2019-02-19 10:43:38 +0100 | [diff] [blame] | 3114 | |
Radek Krejci | 551b12c | 2019-02-19 16:11:21 +0100 | [diff] [blame] | 3115 | assert_null(lys_parse_mem(ctx, "module ll1 {namespace urn:ll1;prefix ll1; leaf x {default test; type string;}" |
| 3116 | "deviation /x {deviate add {mandatory true;}}}", LYS_IN_YANG)); |
| 3117 | logbuf_assert("Invalid deviation (/x) combining default value and mandatory leaf."); |
| 3118 | assert_null(lys_parse_mem(ctx, "module ll2 {yang-version 1.1; namespace urn:ll2;prefix ll2; leaf-list x {default test; type string;}" |
| 3119 | "deviation /x {deviate add {min-elements 1;}}}", LYS_IN_YANG)); |
| 3120 | logbuf_assert("Invalid deviation (/x) combining default value and mandatory leaf-list."); |
| 3121 | assert_null(lys_parse_mem(ctx, "module ll2 {namespace urn:ll2;prefix ll2; choice ch {default a; leaf a {type string;} leaf b {type string;}}" |
| 3122 | "deviation /ch {deviate add {mandatory true;}}}", LYS_IN_YANG)); |
| 3123 | logbuf_assert("Invalid deviation (/ch) combining default case and mandatory choice."); |
| 3124 | |
| 3125 | assert_null(lys_parse_mem(ctx, "module mm1 {namespace urn:mm1;prefix mm1; leaf-list x {min-elements 10; type string;}" |
| 3126 | "deviation /x {deviate add {max-elements 5;}}}", LYS_IN_YANG)); |
| 3127 | logbuf_assert("Invalid combination of min-elements and max-elements after deviation (/x): min value 10 is bigger than max value 5."); |
| 3128 | assert_null(lys_parse_mem(ctx, "module mm2 {namespace urn:mm2;prefix mm2; leaf-list x {max-elements 10; type string;}" |
| 3129 | "deviation /x {deviate add {min-elements 20;}}}", LYS_IN_YANG)); |
| 3130 | logbuf_assert("Invalid combination of min-elements and max-elements after deviation (/x): min value 20 is bigger than max value 10."); |
| 3131 | assert_null(lys_parse_mem(ctx, "module mm3 {namespace urn:mm3;prefix mm3; list x {min-elements 5; max-elements 10; config false;}" |
| 3132 | "deviation /x {deviate replace {max-elements 1;}}}", LYS_IN_YANG)); |
| 3133 | logbuf_assert("Invalid combination of min-elements and max-elements after deviation (/x): min value 5 is bigger than max value 1."); |
| 3134 | assert_null(lys_parse_mem(ctx, "module mm4 {namespace urn:mm4;prefix mm4; list x {min-elements 5; max-elements 10; config false;}" |
| 3135 | "deviation /x {deviate replace {min-elements 20;}}}", LYS_IN_YANG)); |
| 3136 | logbuf_assert("Invalid combination of min-elements and max-elements after deviation (/x): min value 20 is bigger than max value 10."); |
| 3137 | assert_null(lys_parse_mem(ctx, "module mm5 {namespace urn:mm5;prefix mm5; leaf-list x {type string; min-elements 5;}" |
| 3138 | "deviation /x {deviate add {min-elements 1;}}}", LYS_IN_YANG)); |
| 3139 | logbuf_assert("Invalid deviation (/x) adding \"min-elements\" property which already exists (with value \"5\")."); |
| 3140 | assert_null(lys_parse_mem(ctx, "module mm6 {namespace urn:mm6;prefix mm6; list x {config false; min-elements 5;}" |
| 3141 | "deviation /x {deviate add {min-elements 1;}}}", LYS_IN_YANG)); |
| 3142 | logbuf_assert("Invalid deviation (/x) adding \"min-elements\" property which already exists (with value \"5\")."); |
| 3143 | assert_null(lys_parse_mem(ctx, "module mm7 {namespace urn:mm7;prefix mm7; leaf-list x {type string; max-elements 5;}" |
| 3144 | "deviation /x {deviate add {max-elements 1;}}}", LYS_IN_YANG)); |
| 3145 | logbuf_assert("Invalid deviation (/x) adding \"max-elements\" property which already exists (with value \"5\")."); |
| 3146 | assert_null(lys_parse_mem(ctx, "module mm8 {namespace urn:mm8;prefix mm8; list x {config false; max-elements 5;}" |
| 3147 | "deviation /x {deviate add {max-elements 1;}}}", LYS_IN_YANG)); |
| 3148 | logbuf_assert("Invalid deviation (/x) adding \"max-elements\" property which already exists (with value \"5\")."); |
| 3149 | assert_null(lys_parse_mem(ctx, "module mm9 {namespace urn:mm9;prefix mm9; leaf-list x {type string;}" |
| 3150 | "deviation /x {deviate replace {min-elements 1;}}}", LYS_IN_YANG)); |
| 3151 | logbuf_assert("Invalid deviation (/x) replacing with \"min-elements\" property \"1\" which is not present."); |
| 3152 | assert_null(lys_parse_mem(ctx, "module mm10 {namespace urn:mm10;prefix mm10; list x {config false;}" |
| 3153 | "deviation /x {deviate replace {min-elements 1;}}}", LYS_IN_YANG)); |
| 3154 | logbuf_assert("Invalid deviation (/x) replacing with \"min-elements\" property \"1\" which is not present."); |
| 3155 | assert_null(lys_parse_mem(ctx, "module mm11 {namespace urn:mm11;prefix mm11; leaf-list x {type string;}" |
| 3156 | "deviation /x {deviate replace {max-elements 1;}}}", LYS_IN_YANG)); |
| 3157 | logbuf_assert("Invalid deviation (/x) replacing with \"max-elements\" property \"1\" which is not present."); |
| 3158 | assert_null(lys_parse_mem(ctx, "module mm12 {namespace urn:mm12;prefix mm12; list x {config false; }" |
| 3159 | "deviation /x {deviate replace {max-elements 1;}}}", LYS_IN_YANG)); |
| 3160 | logbuf_assert("Invalid deviation (/x) replacing with \"max-elements\" property \"1\" which is not present."); |
| 3161 | |
Radek Krejci | 33f7289 | 2019-02-21 10:36:58 +0100 | [diff] [blame] | 3162 | assert_null(lys_parse_mem(ctx, "module nn1 {namespace urn:nn1;prefix nn1; anyxml x;" |
| 3163 | "deviation /x {deviate replace {type string;}}}", LYS_IN_YANG)); |
| 3164 | logbuf_assert("Invalid deviation (/x) of anyxml node - it is not possible to replace \"type\" property."); |
| 3165 | assert_null(lys_parse_mem(ctx, "module nn2 {namespace urn:nn2;prefix nn2; leaf-list x {type string;}" |
| 3166 | "deviation /x {deviate replace {type empty;}}}", LYS_IN_YANG)); |
| 3167 | logbuf_assert("Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules."); |
| 3168 | |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3169 | *state = NULL; |
| 3170 | ly_ctx_destroy(ctx, NULL); |
| 3171 | } |
| 3172 | |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 3173 | int main(void) |
| 3174 | { |
| 3175 | const struct CMUnitTest tests[] = { |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 3176 | cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown), |
| 3177 | cmocka_unit_test_setup_teardown(test_feature, logger_setup, logger_teardown), |
| 3178 | cmocka_unit_test_setup_teardown(test_identity, logger_setup, logger_teardown), |
Radek Krejci | 0f07bed | 2018-11-13 14:01:40 +0100 | [diff] [blame] | 3179 | cmocka_unit_test_setup_teardown(test_type_length, logger_setup, logger_teardown), |
| 3180 | cmocka_unit_test_setup_teardown(test_type_range, logger_setup, logger_teardown), |
Radek Krejci | cda7415 | 2018-11-13 15:27:32 +0100 | [diff] [blame] | 3181 | cmocka_unit_test_setup_teardown(test_type_pattern, logger_setup, logger_teardown), |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 3182 | cmocka_unit_test_setup_teardown(test_type_enum, logger_setup, logger_teardown), |
| 3183 | cmocka_unit_test_setup_teardown(test_type_bits, logger_setup, logger_teardown), |
Radek Krejci | 6cba429 | 2018-11-15 17:33:29 +0100 | [diff] [blame] | 3184 | cmocka_unit_test_setup_teardown(test_type_dec64, logger_setup, logger_teardown), |
Radek Krejci | 16c0f82 | 2018-11-16 10:46:10 +0100 | [diff] [blame] | 3185 | cmocka_unit_test_setup_teardown(test_type_instanceid, logger_setup, logger_teardown), |
Radek Krejci | 555cb5b | 2018-11-16 14:54:33 +0100 | [diff] [blame] | 3186 | cmocka_unit_test_setup_teardown(test_type_identityref, logger_setup, logger_teardown), |
Radek Krejci | a304538 | 2018-11-22 14:30:31 +0100 | [diff] [blame] | 3187 | cmocka_unit_test_setup_teardown(test_type_leafref, logger_setup, logger_teardown), |
Radek Krejci | 4369923 | 2018-11-23 14:59:46 +0100 | [diff] [blame] | 3188 | cmocka_unit_test_setup_teardown(test_type_empty, logger_setup, logger_teardown), |
Radek Krejci | cdfecd9 | 2018-11-26 11:27:32 +0100 | [diff] [blame] | 3189 | cmocka_unit_test_setup_teardown(test_type_union, logger_setup, logger_teardown), |
| 3190 | cmocka_unit_test_setup_teardown(test_type_dflt, logger_setup, logger_teardown), |
Radek Krejci | 665421c | 2018-12-05 08:03:39 +0100 | [diff] [blame] | 3191 | cmocka_unit_test_setup_teardown(test_status, logger_setup, logger_teardown), |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 3192 | cmocka_unit_test_setup_teardown(test_node_container, logger_setup, logger_teardown), |
Radek Krejci | 0e5d838 | 2018-11-28 16:37:53 +0100 | [diff] [blame] | 3193 | cmocka_unit_test_setup_teardown(test_node_leaflist, logger_setup, logger_teardown), |
Radek Krejci | 9bb94eb | 2018-12-04 16:48:35 +0100 | [diff] [blame] | 3194 | cmocka_unit_test_setup_teardown(test_node_list, logger_setup, logger_teardown), |
Radek Krejci | 056d0a8 | 2018-12-06 16:57:25 +0100 | [diff] [blame] | 3195 | cmocka_unit_test_setup_teardown(test_node_choice, logger_setup, logger_teardown), |
Radek Krejci | 9800fb8 | 2018-12-13 14:26:23 +0100 | [diff] [blame] | 3196 | cmocka_unit_test_setup_teardown(test_node_anydata, logger_setup, logger_teardown), |
Radek Krejci | f538ce5 | 2019-03-05 10:46:14 +0100 | [diff] [blame] | 3197 | cmocka_unit_test_setup_teardown(test_action, logger_setup, logger_teardown), |
Radek Krejci | fc11bd7 | 2019-04-11 16:00:05 +0200 | [diff] [blame] | 3198 | cmocka_unit_test_setup_teardown(test_notification, logger_setup, logger_teardown), |
Radek Krejci | e86bf77 | 2018-12-14 11:39:53 +0100 | [diff] [blame] | 3199 | cmocka_unit_test_setup_teardown(test_uses, logger_setup, logger_teardown), |
Radek Krejci | 01342af | 2019-01-03 15:18:08 +0100 | [diff] [blame] | 3200 | cmocka_unit_test_setup_teardown(test_refine, logger_setup, logger_teardown), |
Radek Krejci | 95710c9 | 2019-02-11 15:49:55 +0100 | [diff] [blame] | 3201 | cmocka_unit_test_setup_teardown(test_augment, logger_setup, logger_teardown), |
Radek Krejci | ccd20f1 | 2019-02-15 14:12:27 +0100 | [diff] [blame] | 3202 | cmocka_unit_test_setup_teardown(test_deviation, logger_setup, logger_teardown), |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 3203 | }; |
| 3204 | |
| 3205 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 3206 | } |