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