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