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" |
| 21 | #include "../../src/tree_schema.c" |
| 22 | #include "../../src/context.c" |
| 23 | #include "../../src/hash_table.c" |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 24 | |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 25 | #include <stdarg.h> |
| 26 | #include <stddef.h> |
| 27 | #include <setjmp.h> |
| 28 | #include <cmocka.h> |
| 29 | |
| 30 | #include <stdio.h> |
| 31 | #include <string.h> |
| 32 | |
| 33 | #include "libyang.h" |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 34 | |
| 35 | #define BUFSIZE 1024 |
| 36 | char logbuf[BUFSIZE] = {0}; |
| 37 | |
| 38 | /* set to 0 to printing error messages to stderr instead of checking them in code */ |
| 39 | #define ENABLE_LOGGER_CHECKING 1 |
| 40 | |
| 41 | #if ENABLE_LOGGER_CHECKING |
| 42 | static void |
| 43 | logger(LY_LOG_LEVEL level, const char *msg, const char *path) |
| 44 | { |
| 45 | (void) level; /* unused */ |
| 46 | |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 47 | if (path && path[0]) { |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 48 | snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path); |
| 49 | } else { |
| 50 | strncpy(logbuf, msg, BUFSIZE - 1); |
| 51 | } |
| 52 | } |
| 53 | #endif |
| 54 | |
| 55 | static int |
| 56 | logger_setup(void **state) |
| 57 | { |
| 58 | (void) state; /* unused */ |
| 59 | #if ENABLE_LOGGER_CHECKING |
| 60 | ly_set_log_clb(logger, 1); |
| 61 | #endif |
| 62 | return 0; |
| 63 | } |
| 64 | |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 65 | static int |
| 66 | logger_teardown(void **state) |
| 67 | { |
| 68 | (void) state; /* unused */ |
| 69 | #if ENABLE_LOGGER_CHECKING |
| 70 | if (*state) { |
| 71 | fprintf(stderr, "%s\n", logbuf); |
| 72 | } |
| 73 | #endif |
| 74 | return 0; |
| 75 | } |
| 76 | |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 77 | void |
| 78 | logbuf_clean(void) |
| 79 | { |
| 80 | logbuf[0] = '\0'; |
| 81 | } |
| 82 | |
| 83 | #if ENABLE_LOGGER_CHECKING |
| 84 | # define logbuf_assert(str) assert_string_equal(logbuf, str) |
| 85 | #else |
| 86 | # define logbuf_assert(str) |
| 87 | #endif |
| 88 | |
| 89 | static void |
| 90 | test_module(void **state) |
| 91 | { |
| 92 | (void) state; /* unused */ |
| 93 | |
| 94 | const char *str; |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 95 | struct ly_parser_ctx ctx = {0}; |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 96 | struct lys_module mod = {0}; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 97 | struct lysc_feature *f; |
| 98 | struct lysc_iffeature *iff; |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 99 | |
| 100 | str = "module test {namespace urn:test; prefix t;" |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 101 | "feature f1;feature f2 {if-feature f1;}}"; |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 102 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 103 | |
Radek Krejci | f8f882a | 2018-10-31 14:51:15 +0100 | [diff] [blame] | 104 | assert_int_equal(LY_EINVAL, lys_compile(NULL, 0)); |
| 105 | logbuf_assert("Invalid argument mod (lys_compile())."); |
| 106 | assert_int_equal(LY_EINVAL, lys_compile(&mod, 0)); |
| 107 | logbuf_assert("Invalid argument mod->parsed (lys_compile())."); |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 108 | assert_int_equal(LY_SUCCESS, yang_parse(&ctx, str, &mod.parsed)); |
Radek Krejci | f8f882a | 2018-10-31 14:51:15 +0100 | [diff] [blame] | 109 | assert_int_equal(LY_SUCCESS, lys_compile(&mod, 0)); |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 110 | assert_non_null(mod.compiled); |
| 111 | assert_ptr_equal(mod.parsed->name, mod.compiled->name); |
| 112 | assert_ptr_equal(mod.parsed->ns, mod.compiled->ns); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 113 | /* features */ |
| 114 | assert_non_null(mod.compiled->features); |
| 115 | assert_int_equal(2, LY_ARRAY_SIZE(mod.compiled->features)); |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 116 | f = &mod.compiled->features[1]; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 117 | assert_non_null(f->iffeatures); |
| 118 | assert_int_equal(1, LY_ARRAY_SIZE(f->iffeatures)); |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 119 | iff = &f->iffeatures[0]; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 120 | assert_non_null(iff->expr); |
| 121 | assert_non_null(iff->features); |
| 122 | assert_int_equal(1, LY_ARRAY_SIZE(iff->features)); |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 123 | assert_ptr_equal(&mod.compiled->features[0], iff->features[0]); |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 124 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 125 | lysc_module_free(mod.compiled, NULL); |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 126 | |
Radek Krejci | f8f882a | 2018-10-31 14:51:15 +0100 | [diff] [blame] | 127 | assert_int_equal(LY_SUCCESS, lys_compile(&mod, LYSC_OPT_FREE_SP)); |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 128 | assert_non_null(mod.compiled); |
| 129 | assert_string_equal("test", mod.compiled->name); |
| 130 | assert_string_equal("urn:test", mod.compiled->ns); |
| 131 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 132 | lysc_module_free(mod.compiled, NULL); |
| 133 | mod.compiled = NULL; |
| 134 | |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 135 | /* submodules cannot be compiled directly */ |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 136 | str = "submodule test {belongs-to xxx {prefix x;}}"; |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 137 | assert_int_equal(LY_SUCCESS, yang_parse(&ctx, str, &mod.parsed)); |
Radek Krejci | f8f882a | 2018-10-31 14:51:15 +0100 | [diff] [blame] | 138 | assert_int_equal(LY_EINVAL, lys_compile(&mod, 0)); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 139 | logbuf_assert("Submodules (test) are not supposed to be compiled, compile only the main modules."); |
| 140 | assert_null(mod.compiled); |
| 141 | |
| 142 | lysp_module_free(mod.parsed); |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 143 | ly_ctx_destroy(ctx.ctx, NULL); |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 144 | } |
| 145 | |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 146 | static void |
| 147 | test_feature(void **state) |
| 148 | { |
| 149 | (void) state; /* unused */ |
| 150 | |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 151 | struct ly_parser_ctx ctx = {0}; |
Radek Krejci | 1aefdf7 | 2018-11-01 11:01:39 +0100 | [diff] [blame] | 152 | struct lys_module mod = {0}, *modp; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 153 | const char *str; |
| 154 | struct lysc_feature *f, *f1; |
| 155 | |
| 156 | str = "module a {namespace urn:a;prefix a;yang-version 1.1;\n" |
| 157 | "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] | 158 | "feature orfeature {if-feature \"f1 or f2\";}\n" |
| 159 | "feature andfeature {if-feature \"f1 and f2\";}\n" |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 160 | "feature f6 {if-feature \"not f1\";}\n" |
Radek Krejci | dde18c5 | 2018-10-24 14:43:04 +0200 | [diff] [blame] | 161 | "feature f7 {if-feature \"(f2 and f3) or (not f1)\";}\n" |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 162 | "feature f8 {if-feature \"f1 or f2 or f3 or orfeature or andfeature\";}\n" |
| 163 | "feature f9 {if-feature \"not not f1\";}}"; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 164 | |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 165 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 166 | assert_int_equal(LY_SUCCESS, yang_parse(&ctx, str, &mod.parsed)); |
Radek Krejci | f8f882a | 2018-10-31 14:51:15 +0100 | [diff] [blame] | 167 | assert_int_equal(LY_SUCCESS, lys_compile(&mod, 0)); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 168 | assert_non_null(mod.compiled); |
| 169 | assert_non_null(mod.compiled->features); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 170 | assert_int_equal(9, LY_ARRAY_SIZE(mod.compiled->features)); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 171 | /* all features are disabled by default */ |
| 172 | LY_ARRAY_FOR(mod.compiled->features, struct lysc_feature, f) { |
| 173 | assert_int_equal(0, lysc_feature_value(f)); |
| 174 | } |
| 175 | /* enable f1 */ |
| 176 | assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f1")); |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 177 | f1 = &mod.compiled->features[0]; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 178 | assert_int_equal(1, lysc_feature_value(f1)); |
| 179 | |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 180 | /* enable orfeature */ |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 181 | f = &mod.compiled->features[3]; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 182 | assert_int_equal(0, lysc_feature_value(f)); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 183 | assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "orfeature")); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 184 | assert_int_equal(1, lysc_feature_value(f)); |
| 185 | |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 186 | /* enable andfeature - no possible since f2 is disabled */ |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 187 | f = &mod.compiled->features[4]; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 188 | assert_int_equal(0, lysc_feature_value(f)); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 189 | assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "andfeature")); |
| 190 | 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] | 191 | assert_int_equal(0, lysc_feature_value(f)); |
| 192 | |
| 193 | /* first enable f2, so f5 can be enabled then */ |
| 194 | assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f2")); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 195 | assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "andfeature")); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 196 | assert_int_equal(1, lysc_feature_value(f)); |
| 197 | |
| 198 | /* f1 is enabled, so f6 cannot be enabled */ |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 199 | f = &mod.compiled->features[5]; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 200 | assert_int_equal(0, lysc_feature_value(f)); |
| 201 | assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "f6")); |
| 202 | logbuf_assert("Feature \"f6\" cannot be enabled since it is disabled by its if-feature condition(s)."); |
| 203 | assert_int_equal(0, lysc_feature_value(f)); |
| 204 | |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 205 | /* so disable f1 - andfeature will became also disabled */ |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 206 | assert_int_equal(1, lysc_feature_value(f1)); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 207 | assert_int_equal(LY_SUCCESS, lys_feature_disable(&mod, "f1")); |
| 208 | assert_int_equal(0, lysc_feature_value(f1)); |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 209 | assert_int_equal(0, lysc_feature_value(&mod.compiled->features[4])); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 210 | /* while orfeature is stille enabled */ |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 211 | assert_int_equal(1, lysc_feature_value(&mod.compiled->features[3])); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 212 | /* and finally f6 can be enabled */ |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 213 | assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f6")); |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 214 | assert_int_equal(1, lysc_feature_value(&mod.compiled->features[5])); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 215 | |
| 216 | /* 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] | 217 | 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] | 218 | /* long evaluation of f8 to need to reallocate internal stack for operators */ |
| 219 | 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] | 220 | |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 221 | /* double negation of disabled f1 -> disabled */ |
| 222 | assert_int_equal(0, lysc_iffeature_value(&mod.compiled->features[8].iffeatures[0])); |
| 223 | |
Radek Krejci | 3892028 | 2018-11-01 09:24:16 +0100 | [diff] [blame] | 224 | /* disable all features */ |
| 225 | assert_int_equal(LY_SUCCESS, lys_feature_disable(&mod, "*")); |
| 226 | LY_ARRAY_FOR(mod.compiled->features, struct lysc_feature, f) { |
| 227 | assert_int_equal(0, lys_feature_value(&mod, f->name)); |
| 228 | } |
| 229 | /* re-setting already set feature */ |
| 230 | assert_int_equal(LY_SUCCESS, lys_feature_disable(&mod, "f1")); |
| 231 | assert_int_equal(0, lys_feature_value(&mod, "f1")); |
| 232 | |
| 233 | /* enabling feature that cannot be enabled due to its if-features */ |
Radek Krejci | 1aefdf7 | 2018-11-01 11:01:39 +0100 | [diff] [blame] | 234 | assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f1")); |
| 235 | assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "andfeature")); |
| 236 | logbuf_assert("Feature \"andfeature\" cannot be enabled since it is disabled by its if-feature condition(s)."); |
Radek Krejci | ca3db00 | 2018-11-01 10:31:01 +0100 | [diff] [blame] | 237 | assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "*")); |
| 238 | 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] | 239 | /* test if not changed */ |
| 240 | assert_int_equal(1, lys_feature_value(&mod, "f1")); |
| 241 | assert_int_equal(0, lys_feature_value(&mod, "f2")); |
Radek Krejci | 3892028 | 2018-11-01 09:24:16 +0100 | [diff] [blame] | 242 | |
Radek Krejci | 1aefdf7 | 2018-11-01 11:01:39 +0100 | [diff] [blame] | 243 | /* invalid reference */ |
Radek Krejci | 3892028 | 2018-11-01 09:24:16 +0100 | [diff] [blame] | 244 | assert_int_equal(LY_EINVAL, lys_feature_enable(&mod, "xxx")); |
| 245 | logbuf_assert("Feature \"xxx\" not found in module \"a\"."); |
| 246 | |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 247 | lysc_module_free(mod.compiled, NULL); |
| 248 | lysp_module_free(mod.parsed); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 249 | |
| 250 | /* some invalid expressions */ |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 251 | 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] | 252 | assert_int_equal(LY_EVALID, lys_compile(&mod, 0)); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 253 | logbuf_assert("Invalid value \"f1\" of if-feature - unable to find feature \"f1\"."); |
| 254 | lysp_module_free(mod.parsed); |
| 255 | |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 256 | 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] | 257 | assert_int_equal(LY_EVALID, lys_compile(&mod, 0)); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 258 | logbuf_assert("Invalid value \"f and\" of if-feature - unexpected end of expression."); |
| 259 | lysp_module_free(mod.parsed); |
| 260 | |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 261 | 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] | 262 | assert_int_equal(LY_EVALID, lys_compile(&mod, 0)); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 263 | logbuf_assert("Invalid value \"or\" of if-feature - unexpected end of expression."); |
| 264 | lysp_module_free(mod.parsed); |
| 265 | |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 266 | 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] | 267 | assert_int_equal(LY_EVALID, lys_compile(&mod, 0)); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 268 | logbuf_assert("Invalid value \"(f1\" of if-feature - non-matching opening and closing parentheses."); |
| 269 | lysp_module_free(mod.parsed); |
| 270 | |
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 f1; feature f2{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 - non-matching opening and closing parentheses."); |
| 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 ---;}}", &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 \"---\" of if-feature - unable to find feature \"---\"."); |
| 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{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] | 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 \"not f1\" of if-feature - YANG 1.1 expression in YANG 1.0 module."); |
| 284 | lysp_module_free(mod.parsed); |
| 285 | |
Radek Krejci | 1aefdf7 | 2018-11-01 11:01:39 +0100 | [diff] [blame] | 286 | /* import reference */ |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 287 | 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] | 288 | assert_int_equal(LY_SUCCESS, lys_compile(modp, 0)); |
| 289 | assert_int_equal(LY_SUCCESS, lys_feature_enable(modp, "f1")); |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 290 | assert_non_null(modp = lys_parse_mem(ctx.ctx, "module b{namespace urn:b; prefix b; 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] | 291 | assert_int_equal(LY_SUCCESS, lys_compile(modp, 0)); |
| 292 | assert_int_equal(LY_SUCCESS, lys_feature_enable(modp, "f2")); |
| 293 | assert_int_equal(0, lys_feature_value(modp, "f1")); |
| 294 | assert_int_equal(1, lys_feature_value(modp, "f2")); |
| 295 | |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 296 | ly_ctx_destroy(ctx.ctx, NULL); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 297 | } |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 298 | |
Radek Krejci | 478020e | 2018-10-30 16:02:14 +0100 | [diff] [blame] | 299 | static void |
| 300 | test_identity(void **state) |
| 301 | { |
| 302 | (void) state; /* unused */ |
| 303 | |
| 304 | struct ly_ctx *ctx; |
Radek Krejci | 1aefdf7 | 2018-11-01 11:01:39 +0100 | [diff] [blame] | 305 | struct lys_module *mod1, *mod2; |
Radek Krejci | 478020e | 2018-10-30 16:02:14 +0100 | [diff] [blame] | 306 | 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] | 307 | 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] | 308 | |
| 309 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 310 | assert_non_null(mod1 = lys_parse_mem(ctx, mod1_str, LYS_IN_YANG)); |
| 311 | assert_non_null(mod2 = lys_parse_mem(ctx, mod2_str, LYS_IN_YANG)); |
Radek Krejci | f8f882a | 2018-10-31 14:51:15 +0100 | [diff] [blame] | 312 | assert_int_equal(LY_SUCCESS, lys_compile(mod2, 0)); |
Radek Krejci | 478020e | 2018-10-30 16:02:14 +0100 | [diff] [blame] | 313 | |
| 314 | assert_non_null(mod1->compiled); |
| 315 | assert_non_null(mod1->compiled->identities); |
| 316 | assert_non_null(mod2->compiled); |
| 317 | assert_non_null(mod2->compiled->identities); |
| 318 | |
| 319 | assert_non_null(mod1->compiled->identities[0].derived); |
| 320 | assert_int_equal(1, LY_ARRAY_SIZE(mod1->compiled->identities[0].derived)); |
| 321 | assert_ptr_equal(mod1->compiled->identities[0].derived[0], &mod2->compiled->identities[2]); |
| 322 | assert_non_null(mod2->compiled->identities[0].derived); |
| 323 | assert_int_equal(2, LY_ARRAY_SIZE(mod2->compiled->identities[0].derived)); |
| 324 | assert_ptr_equal(mod2->compiled->identities[0].derived[0], &mod2->compiled->identities[2]); |
| 325 | assert_ptr_equal(mod2->compiled->identities[0].derived[1], &mod2->compiled->identities[3]); |
| 326 | assert_non_null(mod2->compiled->identities[1].derived); |
| 327 | assert_int_equal(1, LY_ARRAY_SIZE(mod2->compiled->identities[1].derived)); |
| 328 | assert_ptr_equal(mod2->compiled->identities[1].derived[0], &mod2->compiled->identities[2]); |
| 329 | assert_non_null(mod2->compiled->identities[2].derived); |
| 330 | assert_int_equal(1, LY_ARRAY_SIZE(mod2->compiled->identities[2].derived)); |
| 331 | assert_ptr_equal(mod2->compiled->identities[2].derived[0], &mod2->compiled->identities[3]); |
| 332 | |
Radek Krejci | 478020e | 2018-10-30 16:02:14 +0100 | [diff] [blame] | 333 | ly_ctx_destroy(ctx, NULL); |
| 334 | } |
| 335 | |
Radek Krejci | bd8d9ba | 2018-11-02 16:06:26 +0100 | [diff] [blame] | 336 | static void |
| 337 | test_node_container(void **state) |
| 338 | { |
| 339 | (void) state; /* unused */ |
| 340 | |
| 341 | struct ly_ctx *ctx; |
| 342 | struct lys_module *mod; |
| 343 | struct lysc_node_container *cont; |
| 344 | |
| 345 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 346 | assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;container c;}", LYS_IN_YANG)); |
| 347 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 348 | assert_non_null(mod->compiled); |
| 349 | assert_non_null((cont = (struct lysc_node_container*)mod->compiled->data)); |
| 350 | assert_int_equal(LYS_CONTAINER, cont->nodetype); |
| 351 | assert_string_equal("c", cont->name); |
| 352 | assert_true(cont->flags & LYS_CONFIG_W); |
| 353 | assert_true(cont->flags & LYS_STATUS_CURR); |
| 354 | |
Radek Krejci | 98094b3 | 2018-11-02 16:21:47 +0100 | [diff] [blame] | 355 | 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] | 356 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
Radek Krejci | 98094b3 | 2018-11-02 16:21:47 +0100 | [diff] [blame] | 357 | 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] | 358 | assert_non_null(mod->compiled); |
| 359 | assert_non_null((cont = (struct lysc_node_container*)mod->compiled->data)); |
| 360 | assert_true(cont->flags & LYS_CONFIG_R); |
Radek Krejci | 98094b3 | 2018-11-02 16:21:47 +0100 | [diff] [blame] | 361 | assert_true(cont->flags & LYS_STATUS_DEPRC); |
Radek Krejci | bd8d9ba | 2018-11-02 16:06:26 +0100 | [diff] [blame] | 362 | assert_non_null((cont = (struct lysc_node_container*)cont->child)); |
| 363 | assert_int_equal(LYS_CONTAINER, cont->nodetype); |
| 364 | assert_true(cont->flags & LYS_CONFIG_R); |
Radek Krejci | 98094b3 | 2018-11-02 16:21:47 +0100 | [diff] [blame] | 365 | assert_true(cont->flags & LYS_STATUS_DEPRC); |
Radek Krejci | bd8d9ba | 2018-11-02 16:06:26 +0100 | [diff] [blame] | 366 | assert_string_equal("child", cont->name); |
| 367 | |
| 368 | ly_ctx_destroy(ctx, NULL); |
| 369 | } |
| 370 | |
Radek Krejci | 0f07bed | 2018-11-13 14:01:40 +0100 | [diff] [blame] | 371 | /** |
| 372 | * actually the same as length restriction (tested in test_type_length()), so just check the correct handling in appropriate types, |
| 373 | * do not test the expression itself |
| 374 | */ |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 375 | static void |
Radek Krejci | 0f07bed | 2018-11-13 14:01:40 +0100 | [diff] [blame] | 376 | test_type_range(void **state) |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 377 | { |
Radek Krejci | 0f07bed | 2018-11-13 14:01:40 +0100 | [diff] [blame] | 378 | *state = test_type_range; |
| 379 | |
| 380 | struct ly_ctx *ctx; |
| 381 | struct lys_module *mod; |
| 382 | struct lysc_type *type; |
| 383 | |
| 384 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 385 | |
| 386 | 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)); |
| 387 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 388 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 389 | assert_non_null(type); |
| 390 | assert_int_equal(LY_TYPE_INT8, type->basetype); |
| 391 | assert_non_null(((struct lysc_type_num*)type)->range); |
| 392 | assert_non_null(((struct lysc_type_num*)type)->range->parts); |
| 393 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts)); |
| 394 | assert_int_equal(-128, ((struct lysc_type_num*)type)->range->parts[0].min_64); |
| 395 | assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64); |
| 396 | assert_int_equal(127, ((struct lysc_type_num*)type)->range->parts[1].min_64); |
| 397 | assert_int_equal(127, ((struct lysc_type_num*)type)->range->parts[1].max_64); |
| 398 | |
| 399 | 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)); |
| 400 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 401 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 402 | assert_non_null(type); |
| 403 | assert_int_equal(LY_TYPE_INT16, type->basetype); |
| 404 | assert_non_null(((struct lysc_type_num*)type)->range); |
| 405 | assert_non_null(((struct lysc_type_num*)type)->range->parts); |
| 406 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts)); |
| 407 | assert_int_equal(-32768, ((struct lysc_type_num*)type)->range->parts[0].min_64); |
| 408 | assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64); |
| 409 | assert_int_equal(32767, ((struct lysc_type_num*)type)->range->parts[1].min_64); |
| 410 | assert_int_equal(32767, ((struct lysc_type_num*)type)->range->parts[1].max_64); |
| 411 | |
| 412 | 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)); |
| 413 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 414 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 415 | assert_non_null(type); |
| 416 | assert_int_equal(LY_TYPE_INT32, type->basetype); |
| 417 | assert_non_null(((struct lysc_type_num*)type)->range); |
| 418 | assert_non_null(((struct lysc_type_num*)type)->range->parts); |
| 419 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts)); |
| 420 | assert_int_equal(INT64_C(-2147483648), ((struct lysc_type_num*)type)->range->parts[0].min_64); |
| 421 | assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64); |
| 422 | assert_int_equal(INT64_C(2147483647), ((struct lysc_type_num*)type)->range->parts[1].min_64); |
| 423 | assert_int_equal(INT64_C(2147483647), ((struct lysc_type_num*)type)->range->parts[1].max_64); |
| 424 | |
| 425 | 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)); |
| 426 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 427 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 428 | assert_non_null(type); |
| 429 | assert_int_equal(LY_TYPE_INT64, type->basetype); |
| 430 | assert_non_null(((struct lysc_type_num*)type)->range); |
| 431 | assert_non_null(((struct lysc_type_num*)type)->range->parts); |
| 432 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts)); |
| 433 | assert_int_equal(INT64_C(-9223372036854775807) - INT64_C(1), ((struct lysc_type_num*)type)->range->parts[0].min_64); |
| 434 | assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64); |
| 435 | assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_num*)type)->range->parts[1].min_64); |
| 436 | assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_num*)type)->range->parts[1].max_64); |
| 437 | |
| 438 | 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)); |
| 439 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 440 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 441 | assert_non_null(type); |
| 442 | assert_int_equal(LY_TYPE_UINT8, type->basetype); |
| 443 | assert_non_null(((struct lysc_type_num*)type)->range); |
| 444 | assert_non_null(((struct lysc_type_num*)type)->range->parts); |
| 445 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts)); |
| 446 | assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64); |
| 447 | assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64); |
| 448 | assert_int_equal(255, ((struct lysc_type_num*)type)->range->parts[1].min_u64); |
| 449 | assert_int_equal(255, ((struct lysc_type_num*)type)->range->parts[1].max_u64); |
| 450 | |
| 451 | 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)); |
| 452 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 453 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 454 | assert_non_null(type); |
| 455 | assert_int_equal(LY_TYPE_UINT16, type->basetype); |
| 456 | assert_non_null(((struct lysc_type_num*)type)->range); |
| 457 | assert_non_null(((struct lysc_type_num*)type)->range->parts); |
| 458 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts)); |
| 459 | assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64); |
| 460 | assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64); |
| 461 | assert_int_equal(65535, ((struct lysc_type_num*)type)->range->parts[1].min_u64); |
| 462 | assert_int_equal(65535, ((struct lysc_type_num*)type)->range->parts[1].max_u64); |
| 463 | |
| 464 | 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)); |
| 465 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 466 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 467 | assert_non_null(type); |
| 468 | assert_int_equal(LY_TYPE_UINT32, type->basetype); |
| 469 | assert_non_null(((struct lysc_type_num*)type)->range); |
| 470 | assert_non_null(((struct lysc_type_num*)type)->range->parts); |
| 471 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts)); |
| 472 | assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64); |
| 473 | assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64); |
| 474 | assert_int_equal(UINT64_C(4294967295), ((struct lysc_type_num*)type)->range->parts[1].min_u64); |
| 475 | assert_int_equal(UINT64_C(4294967295), ((struct lysc_type_num*)type)->range->parts[1].max_u64); |
| 476 | |
| 477 | 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)); |
| 478 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 479 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 480 | assert_non_null(type); |
| 481 | assert_int_equal(LY_TYPE_UINT64, type->basetype); |
| 482 | assert_non_null(((struct lysc_type_num*)type)->range); |
| 483 | assert_non_null(((struct lysc_type_num*)type)->range->parts); |
| 484 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts)); |
| 485 | assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64); |
| 486 | assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64); |
| 487 | assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_num*)type)->range->parts[1].min_u64); |
| 488 | assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_num*)type)->range->parts[1].max_u64); |
| 489 | |
| 490 | assert_non_null(mod = lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;typedef mytype {type uint8 {range 10..100;}}" |
| 491 | "typedef mytype2 {type mytype;} leaf l {type mytype2;}}", LYS_IN_YANG)); |
| 492 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 493 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 494 | assert_non_null(type); |
| 495 | assert_int_equal(3, type->refcount); |
| 496 | assert_int_equal(LY_TYPE_UINT8, type->basetype); |
| 497 | assert_non_null(((struct lysc_type_num*)type)->range); |
| 498 | assert_non_null(((struct lysc_type_num*)type)->range->parts); |
| 499 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts)); |
| 500 | |
| 501 | *state = NULL; |
| 502 | ly_ctx_destroy(ctx, NULL); |
| 503 | } |
| 504 | |
| 505 | static void |
| 506 | test_type_length(void **state) |
| 507 | { |
| 508 | *state = test_type_length; |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 509 | |
| 510 | struct ly_ctx *ctx; |
| 511 | struct lys_module *mod; |
| 512 | struct lysc_type *type; |
| 513 | |
| 514 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 515 | |
Radek Krejci | c5ddcc0 | 2018-11-12 13:28:18 +0100 | [diff] [blame] | 516 | 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] | 517 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 518 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 519 | assert_non_null(type); |
| 520 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 521 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
Radek Krejci | c5ddcc0 | 2018-11-12 13:28:18 +0100 | [diff] [blame] | 522 | assert_string_equal("errortag", ((struct lysc_type_bin*)type)->length->eapptag); |
| 523 | assert_string_equal("error", ((struct lysc_type_bin*)type)->length->emsg); |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 524 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 525 | assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
| 526 | assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
| 527 | |
| 528 | assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;leaf l {type binary {length max;}}}", LYS_IN_YANG)); |
| 529 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 530 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 531 | assert_non_null(type); |
| 532 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 533 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
| 534 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 535 | assert_int_equal(__UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
| 536 | assert_int_equal(__UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
| 537 | |
| 538 | 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)); |
| 539 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 540 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 541 | assert_non_null(type); |
| 542 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 543 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
| 544 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 545 | assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
| 546 | assert_int_equal(__UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
| 547 | |
| 548 | assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d;leaf l {type binary {length 5;}}}", LYS_IN_YANG)); |
| 549 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 550 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 551 | assert_non_null(type); |
| 552 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 553 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
| 554 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 555 | assert_int_equal(5, ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
| 556 | assert_int_equal(5, ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
| 557 | |
| 558 | 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)); |
| 559 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 560 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 561 | assert_non_null(type); |
| 562 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 563 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
| 564 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 565 | assert_int_equal(1, ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
| 566 | assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
| 567 | |
| 568 | 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)); |
| 569 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 570 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 571 | assert_non_null(type); |
| 572 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 573 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
| 574 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 575 | assert_int_equal(1, ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
| 576 | assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
| 577 | assert_int_equal(20, ((struct lysc_type_bin*)type)->length->parts[1].min_u64); |
| 578 | assert_int_equal(30, ((struct lysc_type_bin*)type)->length->parts[1].max_u64); |
| 579 | |
| 580 | 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)); |
| 581 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 582 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 583 | assert_non_null(type); |
| 584 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 585 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
| 586 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 587 | assert_int_equal(16, ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
| 588 | assert_int_equal(16, ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
| 589 | assert_int_equal(32, ((struct lysc_type_bin*)type)->length->parts[1].min_u64); |
| 590 | assert_int_equal(32, ((struct lysc_type_bin*)type)->length->parts[1].max_u64); |
| 591 | |
Radek Krejci | b31c899 | 2018-11-12 16:29:16 +0100 | [diff] [blame] | 592 | assert_non_null(mod = lys_parse_mem(ctx, "module h {namespace urn:h;prefix h;typedef mytype {type binary {length 10;}}" |
| 593 | "leaf l {type mytype {length \"10\";}}}", LYS_IN_YANG)); |
| 594 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 595 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 596 | assert_non_null(type); |
| 597 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 598 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
| 599 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 600 | assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
| 601 | assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
| 602 | |
| 603 | assert_non_null(mod = lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;typedef mytype {type binary {length 10..100;}}" |
| 604 | "leaf l {type mytype {length \"50\";}}}", LYS_IN_YANG)); |
| 605 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 606 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 607 | assert_non_null(type); |
| 608 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 609 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
| 610 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 611 | assert_int_equal(50, ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
| 612 | assert_int_equal(50, ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
| 613 | |
| 614 | assert_non_null(mod = lys_parse_mem(ctx, "module j {namespace urn:j;prefix j;typedef mytype {type binary {length 10..100;}}" |
| 615 | "leaf l {type mytype {length \"10..30|60..100\";}}}", LYS_IN_YANG)); |
| 616 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 617 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 618 | assert_non_null(type); |
| 619 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 620 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
| 621 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 622 | assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
| 623 | assert_int_equal(30, ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
| 624 | assert_int_equal(60, ((struct lysc_type_bin*)type)->length->parts[1].min_u64); |
| 625 | assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[1].max_u64); |
| 626 | |
| 627 | 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] | 628 | "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] | 629 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 630 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 631 | assert_non_null(type); |
Radek Krejci | 56aa27c | 2018-11-13 14:21:59 +0100 | [diff] [blame] | 632 | assert_int_equal(1, type->refcount); |
Radek Krejci | b31c899 | 2018-11-12 16:29:16 +0100 | [diff] [blame] | 633 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 634 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
| 635 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 636 | 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] | 637 | 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] | 638 | type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type; |
| 639 | assert_non_null(type); |
Radek Krejci | 56aa27c | 2018-11-13 14:21:59 +0100 | [diff] [blame] | 640 | assert_int_equal(2, type->refcount); |
Radek Krejci | b31c899 | 2018-11-12 16:29:16 +0100 | [diff] [blame] | 641 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 642 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
| 643 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 644 | assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
| 645 | assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
| 646 | |
Radek Krejci | 56aa27c | 2018-11-13 14:21:59 +0100 | [diff] [blame] | 647 | assert_non_null(mod = lys_parse_mem(ctx, "module l {namespace urn:l;prefix l;typedef mytype {type string {length 10..100;}}" |
| 648 | "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] | 649 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 650 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 651 | assert_non_null(type); |
| 652 | assert_int_equal(LY_TYPE_STRING, type->basetype); |
Radek Krejci | 56aa27c | 2018-11-13 14:21:59 +0100 | [diff] [blame] | 653 | assert_int_equal(1, type->refcount); |
Radek Krejci | cda7415 | 2018-11-13 15:27:32 +0100 | [diff] [blame] | 654 | assert_non_null(((struct lysc_type_str*)type)->length); |
| 655 | assert_non_null(((struct lysc_type_str*)type)->length->parts); |
| 656 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->length->parts)); |
| 657 | assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].min_u64); |
| 658 | 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] | 659 | |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 660 | /* invalid values */ |
| 661 | assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;leaf l {type binary {length -10;}}}", LYS_IN_YANG)); |
| 662 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 663 | logbuf_assert("Invalid length restriction - value \"-10\" does not fit the type limitations."); |
| 664 | assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;leaf l {type binary {length 18446744073709551616;}}}", LYS_IN_YANG)); |
| 665 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 666 | logbuf_assert("Invalid length restriction - invalid value \"18446744073709551616\"."); |
| 667 | 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)); |
| 668 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 669 | logbuf_assert("Invalid length restriction - unexpected data after max keyword (.. 10)."); |
| 670 | 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)); |
| 671 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 672 | logbuf_assert("Invalid length restriction - values are not in ascending order (10)."); |
| 673 | 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)); |
| 674 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 675 | logbuf_assert("Invalid length restriction - values are not in ascending order (10)."); |
| 676 | assert_non_null(mod = lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;leaf l {type binary {length \"x\";}}}", LYS_IN_YANG)); |
| 677 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 678 | logbuf_assert("Invalid length restriction - unexpected data (x)."); |
Radek Krejci | b31c899 | 2018-11-12 16:29:16 +0100 | [diff] [blame] | 679 | 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)); |
| 680 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 681 | logbuf_assert("Invalid length restriction - unexpected data before min keyword (50 | )."); |
| 682 | assert_non_null(mod = lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;leaf l {type binary {length \"| 50\";}}}", LYS_IN_YANG)); |
| 683 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 684 | logbuf_assert("Invalid length restriction - unexpected beginning of the expression (| 50)."); |
| 685 | assert_non_null(mod = lys_parse_mem(ctx, "module ii {namespace urn:ii;prefix ii;leaf l {type binary {length \"10 ..\";}}}", LYS_IN_YANG)); |
| 686 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 687 | logbuf_assert("Invalid length restriction - unexpected end of the expression after \"..\" (10 ..)."); |
| 688 | assert_non_null(mod = lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;leaf l {type binary {length \".. 10\";}}}", LYS_IN_YANG)); |
| 689 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 690 | logbuf_assert("Invalid length restriction - unexpected \"..\" without a lower bound."); |
| 691 | assert_non_null(mod = lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;leaf l {type binary {length \"10 |\";}}}", LYS_IN_YANG)); |
| 692 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 693 | logbuf_assert("Invalid length restriction - unexpected end of the expression (10 |)."); |
Radek Krejci | 6489bbe | 2018-11-12 17:05:19 +0100 | [diff] [blame] | 694 | 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)); |
| 695 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 696 | logbuf_assert("Invalid length restriction - values are not in ascending order (15)."); |
Radek Krejci | b31c899 | 2018-11-12 16:29:16 +0100 | [diff] [blame] | 697 | |
| 698 | assert_non_null(mod = lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;typedef mytype {type binary {length 10;}}" |
| 699 | "leaf l {type mytype {length 11;}}}", LYS_IN_YANG)); |
| 700 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 701 | logbuf_assert("Invalid length restriction - the derived restriction (11) is not equally or more limiting."); |
| 702 | assert_non_null(mod = lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;typedef mytype {type binary {length 10..100;}}" |
| 703 | "leaf l {type mytype {length 1..11;}}}", LYS_IN_YANG)); |
| 704 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 705 | logbuf_assert("Invalid length restriction - the derived restriction (1..11) is not equally or more limiting."); |
| 706 | assert_non_null(mod = lys_parse_mem(ctx, "module nn {namespace urn:nn;prefix nn;typedef mytype {type binary {length 10..100;}}" |
| 707 | "leaf l {type mytype {length 20..110;}}}", LYS_IN_YANG)); |
| 708 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 709 | logbuf_assert("Invalid length restriction - the derived restriction (20..110) is not equally or more limiting."); |
| 710 | assert_non_null(mod = lys_parse_mem(ctx, "module oo {namespace urn:oo;prefix oo;typedef mytype {type binary {length 10..100;}}" |
| 711 | "leaf l {type mytype {length 20..30|110..120;}}}", LYS_IN_YANG)); |
| 712 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 713 | logbuf_assert("Invalid length restriction - the derived restriction (20..30|110..120) is not equally or more limiting."); |
| 714 | assert_non_null(mod = lys_parse_mem(ctx, "module pp {namespace urn:pp;prefix pp;typedef mytype {type binary {length 10..11;}}" |
| 715 | "leaf l {type mytype {length 15;}}}", LYS_IN_YANG)); |
| 716 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 717 | logbuf_assert("Invalid length restriction - the derived restriction (15) is not equally or more limiting."); |
| 718 | assert_non_null(mod = lys_parse_mem(ctx, "module qq {namespace urn:qq;prefix qq;typedef mytype {type binary {length 10..20|30..40;}}" |
| 719 | "leaf l {type mytype {length 15..35;}}}", LYS_IN_YANG)); |
| 720 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 721 | 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] | 722 | assert_non_null(mod = lys_parse_mem(ctx, "module rr {namespace urn:rr;prefix rr;typedef mytype {type binary {length 10;}}" |
| 723 | "leaf l {type mytype {length 10..35;}}}", LYS_IN_YANG)); |
| 724 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 725 | 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] | 726 | |
Radek Krejci | 495903e | 2018-11-13 11:30:45 +0100 | [diff] [blame] | 727 | 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)); |
| 728 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 729 | logbuf_assert("Invalid type restrictions for binary type."); |
| 730 | |
| 731 | assert_non_null(mod = lys_parse_mem(ctx, "module tt {namespace urn:tt;prefix tt;typedef mytype {type string {length 10;}}" |
| 732 | "leaf l {type mytype {length min..max;}}}", LYS_IN_YANG)); |
| 733 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 734 | logbuf_assert("Invalid length restriction - the derived restriction (min..max) is not equally or more limiting."); |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 735 | |
| 736 | *state = NULL; |
| 737 | ly_ctx_destroy(ctx, NULL); |
| 738 | } |
| 739 | |
Radek Krejci | cda7415 | 2018-11-13 15:27:32 +0100 | [diff] [blame] | 740 | static void |
| 741 | test_type_pattern(void **state) |
| 742 | { |
| 743 | *state = test_type_pattern; |
| 744 | |
| 745 | struct ly_ctx *ctx; |
| 746 | struct lys_module *mod; |
| 747 | struct lysc_type *type; |
| 748 | |
| 749 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 750 | |
Radek Krejci | 027d580 | 2018-11-14 16:57:28 +0100 | [diff] [blame] | 751 | 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] | 752 | "pattern .* {error-app-tag errortag;error-message error;}" |
| 753 | "pattern [0-9].*[0-9] {modifier invert-match;}}}}", LYS_IN_YANG)); |
| 754 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 755 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 756 | assert_non_null(type); |
| 757 | assert_non_null(((struct lysc_type_str*)type)->patterns); |
| 758 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns)); |
| 759 | assert_string_equal("errortag", ((struct lysc_type_str*)type)->patterns[0]->eapptag); |
| 760 | assert_string_equal("error", ((struct lysc_type_str*)type)->patterns[0]->emsg); |
| 761 | assert_int_equal(0, ((struct lysc_type_str*)type)->patterns[0]->inverted); |
| 762 | assert_null(((struct lysc_type_str*)type)->patterns[1]->eapptag); |
| 763 | assert_null(((struct lysc_type_str*)type)->patterns[1]->emsg); |
| 764 | assert_int_equal(1, ((struct lysc_type_str*)type)->patterns[1]->inverted); |
| 765 | |
| 766 | assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;typedef mytype {type string {pattern '[0-9]*';}}" |
| 767 | "typedef mytype2 {type mytype {length 10;}} leaf l {type mytype2 {pattern '[0-4]*';}}}", LYS_IN_YANG)); |
| 768 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 769 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 770 | assert_non_null(type); |
| 771 | assert_int_equal(LY_TYPE_STRING, type->basetype); |
| 772 | assert_int_equal(1, type->refcount); |
| 773 | assert_non_null(((struct lysc_type_str*)type)->patterns); |
| 774 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns)); |
| 775 | assert_int_equal(3, ((struct lysc_type_str*)type)->patterns[0]->refcount); |
| 776 | assert_int_equal(1, ((struct lysc_type_str*)type)->patterns[1]->refcount); |
| 777 | |
Radek Krejci | 04bc1e9 | 2018-11-14 14:28:13 +0100 | [diff] [blame] | 778 | assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c;typedef mytype {type string {pattern '[0-9]*';}}" |
| 779 | "leaf l {type mytype {length 10;}}}", 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_STRING, type->basetype); |
| 784 | assert_int_equal(1, type->refcount); |
| 785 | assert_non_null(((struct lysc_type_str*)type)->patterns); |
| 786 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns)); |
| 787 | assert_int_equal(2, ((struct lysc_type_str*)type)->patterns[0]->refcount); |
| 788 | |
Radek Krejci | cda7415 | 2018-11-13 15:27:32 +0100 | [diff] [blame] | 789 | /* test substitutions */ |
Radek Krejci | 04bc1e9 | 2018-11-14 14:28:13 +0100 | [diff] [blame] | 790 | 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] | 791 | "pattern '^\\p{IsLatinExtended-A}$';}}}", LYS_IN_YANG)); |
| 792 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 793 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 794 | assert_non_null(type); |
| 795 | assert_non_null(((struct lysc_type_str*)type)->patterns); |
| 796 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns)); |
| 797 | /* TODO check some data "^Å™$" */ |
| 798 | |
| 799 | *state = NULL; |
| 800 | ly_ctx_destroy(ctx, NULL); |
| 801 | } |
| 802 | |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 803 | static void |
| 804 | test_type_enum(void **state) |
| 805 | { |
| 806 | *state = test_type_enum; |
| 807 | |
| 808 | struct ly_ctx *ctx; |
| 809 | struct lys_module *mod; |
| 810 | struct lysc_type *type; |
| 811 | |
| 812 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 813 | |
Radek Krejci | 027d580 | 2018-11-14 16:57:28 +0100 | [diff] [blame] | 814 | 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] | 815 | "enum automin; enum min {value -2147483648;}enum one {if-feature f; value 1;}" |
| 816 | "enum two; enum seven {value 7;}enum eight;}}}", LYS_IN_YANG)); |
| 817 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 818 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 819 | assert_non_null(type); |
| 820 | assert_int_equal(LY_TYPE_ENUM, type->basetype); |
| 821 | assert_non_null(((struct lysc_type_enum*)type)->enums); |
| 822 | assert_int_equal(6, LY_ARRAY_SIZE(((struct lysc_type_enum*)type)->enums)); |
| 823 | assert_non_null(((struct lysc_type_enum*)type)->enums[2].iffeatures); |
| 824 | assert_string_equal("automin", ((struct lysc_type_enum*)type)->enums[0].name); |
| 825 | assert_int_equal(0, ((struct lysc_type_enum*)type)->enums[0].value); |
| 826 | assert_string_equal("min", ((struct lysc_type_enum*)type)->enums[1].name); |
| 827 | assert_int_equal(-2147483648, ((struct lysc_type_enum*)type)->enums[1].value); |
| 828 | assert_string_equal("one", ((struct lysc_type_enum*)type)->enums[2].name); |
| 829 | assert_int_equal(1, ((struct lysc_type_enum*)type)->enums[2].value); |
| 830 | assert_string_equal("two", ((struct lysc_type_enum*)type)->enums[3].name); |
| 831 | assert_int_equal(2, ((struct lysc_type_enum*)type)->enums[3].value); |
| 832 | assert_string_equal("seven", ((struct lysc_type_enum*)type)->enums[4].name); |
| 833 | assert_int_equal(7, ((struct lysc_type_enum*)type)->enums[4].value); |
| 834 | assert_string_equal("eight", ((struct lysc_type_enum*)type)->enums[5].name); |
| 835 | assert_int_equal(8, ((struct lysc_type_enum*)type)->enums[5].value); |
| 836 | |
Radek Krejci | 027d580 | 2018-11-14 16:57:28 +0100 | [diff] [blame] | 837 | 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 {" |
| 838 | "enum 11; enum min {value -2147483648;}enum x$&;" |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 839 | "enum two; enum seven {value 7;}enum eight;}} leaf l { type mytype {enum seven;enum eight;}}}", |
| 840 | LYS_IN_YANG)); |
| 841 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 842 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 843 | assert_non_null(type); |
| 844 | assert_int_equal(LY_TYPE_ENUM, type->basetype); |
| 845 | assert_non_null(((struct lysc_type_enum*)type)->enums); |
| 846 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_enum*)type)->enums)); |
| 847 | assert_string_equal("seven", ((struct lysc_type_enum*)type)->enums[0].name); |
| 848 | assert_int_equal(7, ((struct lysc_type_enum*)type)->enums[0].value); |
| 849 | assert_string_equal("eight", ((struct lysc_type_enum*)type)->enums[1].name); |
| 850 | assert_int_equal(8, ((struct lysc_type_enum*)type)->enums[1].value); |
| 851 | |
| 852 | |
| 853 | /* invalid cases */ |
Radek Krejci | 027d580 | 2018-11-14 16:57:28 +0100 | [diff] [blame] | 854 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; feature f; leaf l {type enumeration {" |
| 855 | "enum one {if-feature f;}}}}", LYS_IN_YANG)); |
| 856 | 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] | 857 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {" |
| 858 | "enum one {value -2147483649;}}}}", LYS_IN_YANG)); |
| 859 | logbuf_assert("Invalid value \"-2147483649\" of \"value\". Line number 1."); |
| 860 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {" |
| 861 | "enum one {value 2147483648;}}}}", LYS_IN_YANG)); |
| 862 | logbuf_assert("Invalid value \"2147483648\" of \"value\". Line number 1."); |
| 863 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {" |
| 864 | "enum one; enum one;}}}", LYS_IN_YANG)); |
| 865 | logbuf_assert("Duplicate identifier \"one\" of enum statement. Line number 1."); |
| 866 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {" |
| 867 | "enum '';}}}", LYS_IN_YANG)); |
| 868 | logbuf_assert("Enum name must not be zero-length. Line number 1."); |
| 869 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {" |
| 870 | "enum ' x';}}}", LYS_IN_YANG)); |
| 871 | logbuf_assert("Enum name must not have any leading or trailing whitespaces (\" x\"). Line number 1."); |
| 872 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {" |
| 873 | "enum 'x ';}}}", LYS_IN_YANG)); |
| 874 | logbuf_assert("Enum name must not have any leading or trailing whitespaces (\"x \"). Line number 1."); |
| 875 | assert_non_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type enumeration {" |
| 876 | "enum 'inva\nlid';}}}", LYS_IN_YANG)); |
| 877 | logbuf_assert("Control characters in enum name should be avoided (\"inva\nlid\", character number 5)."); |
| 878 | |
| 879 | assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; leaf l {type enumeration;}}", LYS_IN_YANG)); |
| 880 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 881 | logbuf_assert("Missing enum substatement for enumeration type."); |
| 882 | |
Radek Krejci | 027d580 | 2018-11-14 16:57:28 +0100 | [diff] [blame] | 883 | 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] | 884 | "leaf l {type mytype {enum two;}}}", LYS_IN_YANG)); |
| 885 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 886 | logbuf_assert("Invalid enumeration - derived type adds new item \"two\"."); |
| 887 | |
Radek Krejci | 027d580 | 2018-11-14 16:57:28 +0100 | [diff] [blame] | 888 | 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] | 889 | "leaf l {type mytype {enum one {value 1;}}}}", LYS_IN_YANG)); |
| 890 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 891 | logbuf_assert("Invalid enumeration - value of the item \"one\" has changed from 0 to 1 in the derived type."); |
| 892 | 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;}}}", |
| 893 | LYS_IN_YANG)); |
| 894 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 895 | logbuf_assert("Invalid enumeration - it is not possible to auto-assign enum value for \"y\" since the highest value is already 2147483647."); |
| 896 | |
| 897 | 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;}}}}", |
| 898 | LYS_IN_YANG)); |
| 899 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 900 | logbuf_assert("Invalid enumeration - value 1 collide in items \"y\" and \"x\"."); |
| 901 | |
Radek Krejci | 04bc1e9 | 2018-11-14 14:28:13 +0100 | [diff] [blame] | 902 | assert_non_null(mod = lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;typedef mytype {type enumeration;}" |
| 903 | "leaf l {type mytype {enum one;}}}", LYS_IN_YANG)); |
| 904 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 905 | logbuf_assert("Missing enum substatement for enumeration type \"mytype\"."); |
| 906 | |
Radek Krejci | 027d580 | 2018-11-14 16:57:28 +0100 | [diff] [blame] | 907 | assert_non_null(mod = lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh; typedef mytype {type enumeration {enum one;}}" |
| 908 | "leaf l {type mytype {enum one;}}}", LYS_IN_YANG)); |
| 909 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 910 | logbuf_assert("Enumeration type can be subtyped only in YANG 1.1 modules."); |
| 911 | |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 912 | *state = NULL; |
| 913 | ly_ctx_destroy(ctx, NULL); |
| 914 | } |
| 915 | |
| 916 | static void |
| 917 | test_type_bits(void **state) |
| 918 | { |
| 919 | *state = test_type_bits; |
| 920 | |
| 921 | struct ly_ctx *ctx; |
| 922 | struct lys_module *mod; |
| 923 | struct lysc_type *type; |
| 924 | |
| 925 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 926 | |
Radek Krejci | 027d580 | 2018-11-14 16:57:28 +0100 | [diff] [blame] | 927 | 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] | 928 | "bit automin; bit one {if-feature f; position 1;}" |
| 929 | "bit two; bit seven {position 7;}bit eight;}}}", LYS_IN_YANG)); |
| 930 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 931 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 932 | assert_non_null(type); |
| 933 | assert_int_equal(LY_TYPE_BITS, type->basetype); |
| 934 | assert_non_null(((struct lysc_type_bits*)type)->bits); |
| 935 | assert_int_equal(5, LY_ARRAY_SIZE(((struct lysc_type_bits*)type)->bits)); |
| 936 | assert_non_null(((struct lysc_type_bits*)type)->bits[1].iffeatures); |
| 937 | assert_string_equal("automin", ((struct lysc_type_bits*)type)->bits[0].name); |
| 938 | assert_int_equal(0, ((struct lysc_type_bits*)type)->bits[0].position); |
| 939 | assert_string_equal("one", ((struct lysc_type_bits*)type)->bits[1].name); |
| 940 | assert_int_equal(1, ((struct lysc_type_bits*)type)->bits[1].position); |
| 941 | assert_string_equal("two", ((struct lysc_type_bits*)type)->bits[2].name); |
| 942 | assert_int_equal(2, ((struct lysc_type_bits*)type)->bits[2].position); |
| 943 | assert_string_equal("seven", ((struct lysc_type_bits*)type)->bits[3].name); |
| 944 | assert_int_equal(7, ((struct lysc_type_bits*)type)->bits[3].position); |
| 945 | assert_string_equal("eight", ((struct lysc_type_bits*)type)->bits[4].name); |
| 946 | assert_int_equal(8, ((struct lysc_type_bits*)type)->bits[4].position); |
| 947 | |
Radek Krejci | 027d580 | 2018-11-14 16:57:28 +0100 | [diff] [blame] | 948 | 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 {" |
| 949 | "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] | 950 | LYS_IN_YANG)); |
| 951 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 952 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 953 | assert_non_null(type); |
| 954 | assert_int_equal(LY_TYPE_BITS, type->basetype); |
| 955 | assert_non_null(((struct lysc_type_bits*)type)->bits); |
Radek Krejci | 027d580 | 2018-11-14 16:57:28 +0100 | [diff] [blame] | 956 | assert_int_equal(3, LY_ARRAY_SIZE(((struct lysc_type_bits*)type)->bits)); |
| 957 | assert_string_equal("automin", ((struct lysc_type_bits*)type)->bits[0].name); |
| 958 | assert_int_equal(0, ((struct lysc_type_bits*)type)->bits[0].position); |
| 959 | assert_string_equal("seven", ((struct lysc_type_bits*)type)->bits[1].name); |
| 960 | assert_int_equal(7, ((struct lysc_type_bits*)type)->bits[1].position); |
| 961 | assert_string_equal("eight", ((struct lysc_type_bits*)type)->bits[2].name); |
| 962 | assert_int_equal(8, ((struct lysc_type_bits*)type)->bits[2].position); |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 963 | |
| 964 | |
| 965 | /* invalid cases */ |
Radek Krejci | 027d580 | 2018-11-14 16:57:28 +0100 | [diff] [blame] | 966 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; feature f; leaf l {type bits {" |
| 967 | "bit one {if-feature f;}}}}", LYS_IN_YANG)); |
| 968 | 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] | 969 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {" |
| 970 | "bit one {position -1;}}}}", LYS_IN_YANG)); |
| 971 | logbuf_assert("Invalid value \"-1\" of \"position\". Line number 1."); |
| 972 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {" |
| 973 | "bit one {value 4294967296;}}}}", LYS_IN_YANG)); |
| 974 | logbuf_assert("Invalid value \"4294967296\" of \"value\". Line number 1."); |
| 975 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {" |
| 976 | "bit one; bit one;}}}", LYS_IN_YANG)); |
| 977 | logbuf_assert("Duplicate identifier \"one\" of bit statement. Line number 1."); |
| 978 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {" |
| 979 | "bit '11';}}}", LYS_IN_YANG)); |
| 980 | logbuf_assert("Invalid identifier first character '1'. Line number 1."); |
| 981 | assert_null(lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa; leaf l {type bits {" |
| 982 | "bit 'x1$1';}}}", LYS_IN_YANG)); |
| 983 | logbuf_assert("Invalid identifier character '$'. Line number 1."); |
| 984 | |
| 985 | assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb; leaf l {type bits;}}", LYS_IN_YANG)); |
| 986 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 987 | logbuf_assert("Missing bit substatement for bits type."); |
| 988 | |
Radek Krejci | 027d580 | 2018-11-14 16:57:28 +0100 | [diff] [blame] | 989 | 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] | 990 | "leaf l {type mytype {bit two;}}}", LYS_IN_YANG)); |
| 991 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 992 | logbuf_assert("Invalid bits - derived type adds new item \"two\"."); |
| 993 | |
Radek Krejci | 027d580 | 2018-11-14 16:57:28 +0100 | [diff] [blame] | 994 | 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] | 995 | "leaf l {type mytype {bit one {position 1;}}}}", LYS_IN_YANG)); |
| 996 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 997 | logbuf_assert("Invalid bits - position of the item \"one\" has changed from 0 to 1 in the derived type."); |
| 998 | 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;}}}", |
| 999 | LYS_IN_YANG)); |
| 1000 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 1001 | logbuf_assert("Invalid bits - it is not possible to auto-assign bit position for \"y\" since the highest value is already 4294967295."); |
| 1002 | |
| 1003 | 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;}}}}", |
| 1004 | LYS_IN_YANG)); |
| 1005 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 1006 | logbuf_assert("Invalid bits - position 1 collide in items \"y\" and \"x\"."); |
| 1007 | |
Radek Krejci | 04bc1e9 | 2018-11-14 14:28:13 +0100 | [diff] [blame] | 1008 | assert_non_null(mod = lys_parse_mem(ctx, "module gg {namespace urn:gg;prefix gg;typedef mytype {type bits;}" |
| 1009 | "leaf l {type mytype {bit one;}}}", LYS_IN_YANG)); |
| 1010 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 1011 | logbuf_assert("Missing bit substatement for bits type \"mytype\"."); |
| 1012 | |
Radek Krejci | 027d580 | 2018-11-14 16:57:28 +0100 | [diff] [blame] | 1013 | assert_non_null(mod = lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh; typedef mytype {type bits {bit one;}}" |
| 1014 | "leaf l {type mytype {bit one;}}}", LYS_IN_YANG)); |
| 1015 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 1016 | logbuf_assert("Bits type can be subtyped only in YANG 1.1 modules."); |
| 1017 | |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1018 | *state = NULL; |
| 1019 | ly_ctx_destroy(ctx, NULL); |
| 1020 | } |
| 1021 | |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 1022 | int main(void) |
| 1023 | { |
| 1024 | const struct CMUnitTest tests[] = { |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 1025 | cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown), |
| 1026 | cmocka_unit_test_setup_teardown(test_feature, logger_setup, logger_teardown), |
| 1027 | cmocka_unit_test_setup_teardown(test_identity, logger_setup, logger_teardown), |
Radek Krejci | 0f07bed | 2018-11-13 14:01:40 +0100 | [diff] [blame] | 1028 | cmocka_unit_test_setup_teardown(test_type_length, logger_setup, logger_teardown), |
| 1029 | cmocka_unit_test_setup_teardown(test_type_range, logger_setup, logger_teardown), |
Radek Krejci | cda7415 | 2018-11-13 15:27:32 +0100 | [diff] [blame] | 1030 | cmocka_unit_test_setup_teardown(test_type_pattern, logger_setup, logger_teardown), |
Radek Krejci | 8b76466 | 2018-11-14 14:15:13 +0100 | [diff] [blame] | 1031 | cmocka_unit_test_setup_teardown(test_type_enum, logger_setup, logger_teardown), |
| 1032 | cmocka_unit_test_setup_teardown(test_type_bits, logger_setup, logger_teardown), |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 1033 | cmocka_unit_test_setup_teardown(test_node_container, logger_setup, logger_teardown), |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 1034 | }; |
| 1035 | |
| 1036 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 1037 | } |