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 | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 15 | #include "../../src/tree_schema.c" |
| 16 | #include "../../src/parser_yang.c" |
| 17 | |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 18 | #include <stdarg.h> |
| 19 | #include <stddef.h> |
| 20 | #include <setjmp.h> |
| 21 | #include <cmocka.h> |
| 22 | |
| 23 | #include <stdio.h> |
| 24 | #include <string.h> |
| 25 | |
| 26 | #include "libyang.h" |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 27 | |
| 28 | #define BUFSIZE 1024 |
| 29 | char logbuf[BUFSIZE] = {0}; |
| 30 | |
| 31 | /* set to 0 to printing error messages to stderr instead of checking them in code */ |
| 32 | #define ENABLE_LOGGER_CHECKING 1 |
| 33 | |
| 34 | #if ENABLE_LOGGER_CHECKING |
| 35 | static void |
| 36 | logger(LY_LOG_LEVEL level, const char *msg, const char *path) |
| 37 | { |
| 38 | (void) level; /* unused */ |
| 39 | |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 40 | if (path && path[0]) { |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 41 | snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path); |
| 42 | } else { |
| 43 | strncpy(logbuf, msg, BUFSIZE - 1); |
| 44 | } |
| 45 | } |
| 46 | #endif |
| 47 | |
| 48 | static int |
| 49 | logger_setup(void **state) |
| 50 | { |
| 51 | (void) state; /* unused */ |
| 52 | #if ENABLE_LOGGER_CHECKING |
| 53 | ly_set_log_clb(logger, 1); |
| 54 | #endif |
| 55 | return 0; |
| 56 | } |
| 57 | |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 58 | static int |
| 59 | logger_teardown(void **state) |
| 60 | { |
| 61 | (void) state; /* unused */ |
| 62 | #if ENABLE_LOGGER_CHECKING |
| 63 | if (*state) { |
| 64 | fprintf(stderr, "%s\n", logbuf); |
| 65 | } |
| 66 | #endif |
| 67 | return 0; |
| 68 | } |
| 69 | |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 70 | void |
| 71 | logbuf_clean(void) |
| 72 | { |
| 73 | logbuf[0] = '\0'; |
| 74 | } |
| 75 | |
| 76 | #if ENABLE_LOGGER_CHECKING |
| 77 | # define logbuf_assert(str) assert_string_equal(logbuf, str) |
| 78 | #else |
| 79 | # define logbuf_assert(str) |
| 80 | #endif |
| 81 | |
| 82 | static void |
| 83 | test_module(void **state) |
| 84 | { |
| 85 | (void) state; /* unused */ |
| 86 | |
| 87 | const char *str; |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 88 | struct ly_parser_ctx ctx = {0}; |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 89 | struct lys_module mod = {0}; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 90 | struct lysc_feature *f; |
| 91 | struct lysc_iffeature *iff; |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 92 | |
| 93 | str = "module test {namespace urn:test; prefix t;" |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 94 | "feature f1;feature f2 {if-feature f1;}}"; |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 95 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 96 | |
Radek Krejci | f8f882a | 2018-10-31 14:51:15 +0100 | [diff] [blame] | 97 | assert_int_equal(LY_EINVAL, lys_compile(NULL, 0)); |
| 98 | logbuf_assert("Invalid argument mod (lys_compile())."); |
| 99 | assert_int_equal(LY_EINVAL, lys_compile(&mod, 0)); |
| 100 | logbuf_assert("Invalid argument mod->parsed (lys_compile())."); |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 101 | assert_int_equal(LY_SUCCESS, yang_parse(&ctx, str, &mod.parsed)); |
Radek Krejci | f8f882a | 2018-10-31 14:51:15 +0100 | [diff] [blame] | 102 | assert_int_equal(LY_SUCCESS, lys_compile(&mod, 0)); |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 103 | assert_non_null(mod.compiled); |
| 104 | assert_ptr_equal(mod.parsed->name, mod.compiled->name); |
| 105 | assert_ptr_equal(mod.parsed->ns, mod.compiled->ns); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 106 | /* features */ |
| 107 | assert_non_null(mod.compiled->features); |
| 108 | assert_int_equal(2, LY_ARRAY_SIZE(mod.compiled->features)); |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 109 | f = &mod.compiled->features[1]; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 110 | assert_non_null(f->iffeatures); |
| 111 | assert_int_equal(1, LY_ARRAY_SIZE(f->iffeatures)); |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 112 | iff = &f->iffeatures[0]; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 113 | assert_non_null(iff->expr); |
| 114 | assert_non_null(iff->features); |
| 115 | assert_int_equal(1, LY_ARRAY_SIZE(iff->features)); |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 116 | assert_ptr_equal(&mod.compiled->features[0], iff->features[0]); |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 117 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 118 | lysc_module_free(mod.compiled, NULL); |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 119 | |
Radek Krejci | f8f882a | 2018-10-31 14:51:15 +0100 | [diff] [blame] | 120 | assert_int_equal(LY_SUCCESS, lys_compile(&mod, LYSC_OPT_FREE_SP)); |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 121 | assert_non_null(mod.compiled); |
| 122 | assert_string_equal("test", mod.compiled->name); |
| 123 | assert_string_equal("urn:test", mod.compiled->ns); |
| 124 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 125 | lysc_module_free(mod.compiled, NULL); |
| 126 | mod.compiled = NULL; |
| 127 | |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 128 | /* submodules cannot be compiled directly */ |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 129 | str = "submodule test {belongs-to xxx {prefix x;}}"; |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 130 | assert_int_equal(LY_SUCCESS, yang_parse(&ctx, str, &mod.parsed)); |
Radek Krejci | f8f882a | 2018-10-31 14:51:15 +0100 | [diff] [blame] | 131 | assert_int_equal(LY_EINVAL, lys_compile(&mod, 0)); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 132 | logbuf_assert("Submodules (test) are not supposed to be compiled, compile only the main modules."); |
| 133 | assert_null(mod.compiled); |
| 134 | |
| 135 | lysp_module_free(mod.parsed); |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 136 | ly_ctx_destroy(ctx.ctx, NULL); |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 137 | } |
| 138 | |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 139 | static void |
| 140 | test_feature(void **state) |
| 141 | { |
| 142 | (void) state; /* unused */ |
| 143 | |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 144 | struct ly_parser_ctx ctx = {0}; |
Radek Krejci | 1aefdf7 | 2018-11-01 11:01:39 +0100 | [diff] [blame] | 145 | struct lys_module mod = {0}, *modp; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 146 | const char *str; |
| 147 | struct lysc_feature *f, *f1; |
| 148 | |
| 149 | str = "module a {namespace urn:a;prefix a;yang-version 1.1;\n" |
| 150 | "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] | 151 | "feature orfeature {if-feature \"f1 or f2\";}\n" |
| 152 | "feature andfeature {if-feature \"f1 and f2\";}\n" |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 153 | "feature f6 {if-feature \"not f1\";}\n" |
Radek Krejci | dde18c5 | 2018-10-24 14:43:04 +0200 | [diff] [blame] | 154 | "feature f7 {if-feature \"(f2 and f3) or (not f1)\";}\n" |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 155 | "feature f8 {if-feature \"f1 or f2 or f3 or orfeature or andfeature\";}\n" |
| 156 | "feature f9 {if-feature \"not not f1\";}}"; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 157 | |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 158 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx.ctx)); |
| 159 | assert_int_equal(LY_SUCCESS, yang_parse(&ctx, str, &mod.parsed)); |
Radek Krejci | f8f882a | 2018-10-31 14:51:15 +0100 | [diff] [blame] | 160 | assert_int_equal(LY_SUCCESS, lys_compile(&mod, 0)); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 161 | assert_non_null(mod.compiled); |
| 162 | assert_non_null(mod.compiled->features); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 163 | assert_int_equal(9, LY_ARRAY_SIZE(mod.compiled->features)); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 164 | /* all features are disabled by default */ |
| 165 | LY_ARRAY_FOR(mod.compiled->features, struct lysc_feature, f) { |
| 166 | assert_int_equal(0, lysc_feature_value(f)); |
| 167 | } |
| 168 | /* enable f1 */ |
| 169 | assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f1")); |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 170 | f1 = &mod.compiled->features[0]; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 171 | assert_int_equal(1, lysc_feature_value(f1)); |
| 172 | |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 173 | /* enable orfeature */ |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 174 | f = &mod.compiled->features[3]; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 175 | assert_int_equal(0, lysc_feature_value(f)); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 176 | assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "orfeature")); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 177 | assert_int_equal(1, lysc_feature_value(f)); |
| 178 | |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 179 | /* enable andfeature - no possible since f2 is disabled */ |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 180 | f = &mod.compiled->features[4]; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 181 | assert_int_equal(0, lysc_feature_value(f)); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 182 | assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "andfeature")); |
| 183 | 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] | 184 | assert_int_equal(0, lysc_feature_value(f)); |
| 185 | |
| 186 | /* first enable f2, so f5 can be enabled then */ |
| 187 | assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f2")); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 188 | assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "andfeature")); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 189 | assert_int_equal(1, lysc_feature_value(f)); |
| 190 | |
| 191 | /* f1 is enabled, so f6 cannot be enabled */ |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 192 | f = &mod.compiled->features[5]; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 193 | assert_int_equal(0, lysc_feature_value(f)); |
| 194 | assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "f6")); |
| 195 | logbuf_assert("Feature \"f6\" cannot be enabled since it is disabled by its if-feature condition(s)."); |
| 196 | assert_int_equal(0, lysc_feature_value(f)); |
| 197 | |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 198 | /* so disable f1 - andfeature will became also disabled */ |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 199 | assert_int_equal(1, lysc_feature_value(f1)); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 200 | assert_int_equal(LY_SUCCESS, lys_feature_disable(&mod, "f1")); |
| 201 | assert_int_equal(0, lysc_feature_value(f1)); |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 202 | assert_int_equal(0, lysc_feature_value(&mod.compiled->features[4])); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 203 | /* while orfeature is stille enabled */ |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 204 | assert_int_equal(1, lysc_feature_value(&mod.compiled->features[3])); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 205 | /* and finally f6 can be enabled */ |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 206 | assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f6")); |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 207 | assert_int_equal(1, lysc_feature_value(&mod.compiled->features[5])); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 208 | |
| 209 | /* 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] | 210 | 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] | 211 | /* long evaluation of f8 to need to reallocate internal stack for operators */ |
| 212 | 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] | 213 | |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 214 | /* double negation of disabled f1 -> disabled */ |
| 215 | assert_int_equal(0, lysc_iffeature_value(&mod.compiled->features[8].iffeatures[0])); |
| 216 | |
Radek Krejci | 3892028 | 2018-11-01 09:24:16 +0100 | [diff] [blame] | 217 | /* disable all features */ |
| 218 | assert_int_equal(LY_SUCCESS, lys_feature_disable(&mod, "*")); |
| 219 | LY_ARRAY_FOR(mod.compiled->features, struct lysc_feature, f) { |
| 220 | assert_int_equal(0, lys_feature_value(&mod, f->name)); |
| 221 | } |
| 222 | /* re-setting already set feature */ |
| 223 | assert_int_equal(LY_SUCCESS, lys_feature_disable(&mod, "f1")); |
| 224 | assert_int_equal(0, lys_feature_value(&mod, "f1")); |
| 225 | |
| 226 | /* enabling feature that cannot be enabled due to its if-features */ |
Radek Krejci | 1aefdf7 | 2018-11-01 11:01:39 +0100 | [diff] [blame] | 227 | assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f1")); |
| 228 | assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "andfeature")); |
| 229 | 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] | 230 | assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "*")); |
| 231 | 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] | 232 | /* test if not changed */ |
| 233 | assert_int_equal(1, lys_feature_value(&mod, "f1")); |
| 234 | assert_int_equal(0, lys_feature_value(&mod, "f2")); |
Radek Krejci | 3892028 | 2018-11-01 09:24:16 +0100 | [diff] [blame] | 235 | |
Radek Krejci | 1aefdf7 | 2018-11-01 11:01:39 +0100 | [diff] [blame] | 236 | /* invalid reference */ |
Radek Krejci | 3892028 | 2018-11-01 09:24:16 +0100 | [diff] [blame] | 237 | assert_int_equal(LY_EINVAL, lys_feature_enable(&mod, "xxx")); |
| 238 | logbuf_assert("Feature \"xxx\" not found in module \"a\"."); |
| 239 | |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 240 | lysc_module_free(mod.compiled, NULL); |
| 241 | lysp_module_free(mod.parsed); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 242 | |
| 243 | /* some invalid expressions */ |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 244 | 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] | 245 | assert_int_equal(LY_EVALID, lys_compile(&mod, 0)); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 246 | logbuf_assert("Invalid value \"f1\" of if-feature - unable to find feature \"f1\"."); |
| 247 | lysp_module_free(mod.parsed); |
| 248 | |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 249 | 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] | 250 | assert_int_equal(LY_EVALID, lys_compile(&mod, 0)); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 251 | logbuf_assert("Invalid value \"f and\" of if-feature - unexpected end of expression."); |
| 252 | lysp_module_free(mod.parsed); |
| 253 | |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 254 | 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] | 255 | assert_int_equal(LY_EVALID, lys_compile(&mod, 0)); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 256 | logbuf_assert("Invalid value \"or\" of if-feature - unexpected end of expression."); |
| 257 | lysp_module_free(mod.parsed); |
| 258 | |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 259 | 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] | 260 | assert_int_equal(LY_EVALID, lys_compile(&mod, 0)); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 261 | logbuf_assert("Invalid value \"(f1\" of if-feature - non-matching opening and closing parentheses."); |
| 262 | lysp_module_free(mod.parsed); |
| 263 | |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 264 | 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] | 265 | assert_int_equal(LY_EVALID, lys_compile(&mod, 0)); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 266 | logbuf_assert("Invalid value \"f1)\" of if-feature - non-matching opening and closing parentheses."); |
| 267 | lysp_module_free(mod.parsed); |
| 268 | |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 269 | 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] | 270 | assert_int_equal(LY_EVALID, lys_compile(&mod, 0)); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 271 | logbuf_assert("Invalid value \"---\" of if-feature - unable to find feature \"---\"."); |
| 272 | lysp_module_free(mod.parsed); |
| 273 | |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 274 | 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] | 275 | assert_int_equal(LY_EVALID, lys_compile(&mod, 0)); |
Radek Krejci | 87616bb | 2018-10-31 13:30:52 +0100 | [diff] [blame] | 276 | logbuf_assert("Invalid value \"not f1\" of if-feature - YANG 1.1 expression in YANG 1.0 module."); |
| 277 | lysp_module_free(mod.parsed); |
| 278 | |
Radek Krejci | 1aefdf7 | 2018-11-01 11:01:39 +0100 | [diff] [blame] | 279 | /* import reference */ |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 280 | 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] | 281 | assert_int_equal(LY_SUCCESS, lys_compile(modp, 0)); |
| 282 | assert_int_equal(LY_SUCCESS, lys_feature_enable(modp, "f1")); |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 283 | 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] | 284 | assert_int_equal(LY_SUCCESS, lys_compile(modp, 0)); |
| 285 | assert_int_equal(LY_SUCCESS, lys_feature_enable(modp, "f2")); |
| 286 | assert_int_equal(0, lys_feature_value(modp, "f1")); |
| 287 | assert_int_equal(1, lys_feature_value(modp, "f2")); |
| 288 | |
Radek Krejci | 313d990 | 2018-11-08 09:42:58 +0100 | [diff] [blame] | 289 | ly_ctx_destroy(ctx.ctx, NULL); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 290 | } |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 291 | |
Radek Krejci | 478020e | 2018-10-30 16:02:14 +0100 | [diff] [blame] | 292 | static void |
| 293 | test_identity(void **state) |
| 294 | { |
| 295 | (void) state; /* unused */ |
| 296 | |
| 297 | struct ly_ctx *ctx; |
Radek Krejci | 1aefdf7 | 2018-11-01 11:01:39 +0100 | [diff] [blame] | 298 | struct lys_module *mod1, *mod2; |
Radek Krejci | 478020e | 2018-10-30 16:02:14 +0100 | [diff] [blame] | 299 | const char *mod1_str = "module a {namespace urn:a;prefix a; identity a1;}"; |
| 300 | const char *mod2_str = "module b {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;}}"; |
| 301 | |
| 302 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 303 | assert_non_null(mod1 = lys_parse_mem(ctx, mod1_str, LYS_IN_YANG)); |
| 304 | 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] | 305 | assert_int_equal(LY_SUCCESS, lys_compile(mod2, 0)); |
Radek Krejci | 478020e | 2018-10-30 16:02:14 +0100 | [diff] [blame] | 306 | |
| 307 | assert_non_null(mod1->compiled); |
| 308 | assert_non_null(mod1->compiled->identities); |
| 309 | assert_non_null(mod2->compiled); |
| 310 | assert_non_null(mod2->compiled->identities); |
| 311 | |
| 312 | assert_non_null(mod1->compiled->identities[0].derived); |
| 313 | assert_int_equal(1, LY_ARRAY_SIZE(mod1->compiled->identities[0].derived)); |
| 314 | assert_ptr_equal(mod1->compiled->identities[0].derived[0], &mod2->compiled->identities[2]); |
| 315 | assert_non_null(mod2->compiled->identities[0].derived); |
| 316 | assert_int_equal(2, LY_ARRAY_SIZE(mod2->compiled->identities[0].derived)); |
| 317 | assert_ptr_equal(mod2->compiled->identities[0].derived[0], &mod2->compiled->identities[2]); |
| 318 | assert_ptr_equal(mod2->compiled->identities[0].derived[1], &mod2->compiled->identities[3]); |
| 319 | assert_non_null(mod2->compiled->identities[1].derived); |
| 320 | assert_int_equal(1, LY_ARRAY_SIZE(mod2->compiled->identities[1].derived)); |
| 321 | assert_ptr_equal(mod2->compiled->identities[1].derived[0], &mod2->compiled->identities[2]); |
| 322 | assert_non_null(mod2->compiled->identities[2].derived); |
| 323 | assert_int_equal(1, LY_ARRAY_SIZE(mod2->compiled->identities[2].derived)); |
| 324 | assert_ptr_equal(mod2->compiled->identities[2].derived[0], &mod2->compiled->identities[3]); |
| 325 | |
Radek Krejci | 478020e | 2018-10-30 16:02:14 +0100 | [diff] [blame] | 326 | ly_ctx_destroy(ctx, NULL); |
| 327 | } |
| 328 | |
Radek Krejci | bd8d9ba | 2018-11-02 16:06:26 +0100 | [diff] [blame] | 329 | static void |
| 330 | test_node_container(void **state) |
| 331 | { |
| 332 | (void) state; /* unused */ |
| 333 | |
| 334 | struct ly_ctx *ctx; |
| 335 | struct lys_module *mod; |
| 336 | struct lysc_node_container *cont; |
| 337 | |
| 338 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 339 | assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;container c;}", LYS_IN_YANG)); |
| 340 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 341 | assert_non_null(mod->compiled); |
| 342 | assert_non_null((cont = (struct lysc_node_container*)mod->compiled->data)); |
| 343 | assert_int_equal(LYS_CONTAINER, cont->nodetype); |
| 344 | assert_string_equal("c", cont->name); |
| 345 | assert_true(cont->flags & LYS_CONFIG_W); |
| 346 | assert_true(cont->flags & LYS_STATUS_CURR); |
| 347 | |
Radek Krejci | 98094b3 | 2018-11-02 16:21:47 +0100 | [diff] [blame] | 348 | 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] | 349 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
Radek Krejci | 98094b3 | 2018-11-02 16:21:47 +0100 | [diff] [blame] | 350 | 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] | 351 | assert_non_null(mod->compiled); |
| 352 | assert_non_null((cont = (struct lysc_node_container*)mod->compiled->data)); |
| 353 | assert_true(cont->flags & LYS_CONFIG_R); |
Radek Krejci | 98094b3 | 2018-11-02 16:21:47 +0100 | [diff] [blame] | 354 | assert_true(cont->flags & LYS_STATUS_DEPRC); |
Radek Krejci | bd8d9ba | 2018-11-02 16:06:26 +0100 | [diff] [blame] | 355 | assert_non_null((cont = (struct lysc_node_container*)cont->child)); |
| 356 | assert_int_equal(LYS_CONTAINER, cont->nodetype); |
| 357 | assert_true(cont->flags & LYS_CONFIG_R); |
Radek Krejci | 98094b3 | 2018-11-02 16:21:47 +0100 | [diff] [blame] | 358 | assert_true(cont->flags & LYS_STATUS_DEPRC); |
Radek Krejci | bd8d9ba | 2018-11-02 16:06:26 +0100 | [diff] [blame] | 359 | assert_string_equal("child", cont->name); |
| 360 | |
| 361 | ly_ctx_destroy(ctx, NULL); |
| 362 | } |
| 363 | |
Radek Krejci | 0f07bed | 2018-11-13 14:01:40 +0100 | [diff] [blame] | 364 | /** |
| 365 | * actually the same as length restriction (tested in test_type_length()), so just check the correct handling in appropriate types, |
| 366 | * do not test the expression itself |
| 367 | */ |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 368 | static void |
Radek Krejci | 0f07bed | 2018-11-13 14:01:40 +0100 | [diff] [blame] | 369 | test_type_range(void **state) |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 370 | { |
Radek Krejci | 0f07bed | 2018-11-13 14:01:40 +0100 | [diff] [blame] | 371 | *state = test_type_range; |
| 372 | |
| 373 | struct ly_ctx *ctx; |
| 374 | struct lys_module *mod; |
| 375 | struct lysc_type *type; |
| 376 | |
| 377 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 378 | |
| 379 | 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)); |
| 380 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 381 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 382 | assert_non_null(type); |
| 383 | assert_int_equal(LY_TYPE_INT8, type->basetype); |
| 384 | assert_non_null(((struct lysc_type_num*)type)->range); |
| 385 | assert_non_null(((struct lysc_type_num*)type)->range->parts); |
| 386 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts)); |
| 387 | assert_int_equal(-128, ((struct lysc_type_num*)type)->range->parts[0].min_64); |
| 388 | assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64); |
| 389 | assert_int_equal(127, ((struct lysc_type_num*)type)->range->parts[1].min_64); |
| 390 | assert_int_equal(127, ((struct lysc_type_num*)type)->range->parts[1].max_64); |
| 391 | |
| 392 | 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)); |
| 393 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 394 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 395 | assert_non_null(type); |
| 396 | assert_int_equal(LY_TYPE_INT16, type->basetype); |
| 397 | assert_non_null(((struct lysc_type_num*)type)->range); |
| 398 | assert_non_null(((struct lysc_type_num*)type)->range->parts); |
| 399 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts)); |
| 400 | assert_int_equal(-32768, ((struct lysc_type_num*)type)->range->parts[0].min_64); |
| 401 | assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64); |
| 402 | assert_int_equal(32767, ((struct lysc_type_num*)type)->range->parts[1].min_64); |
| 403 | assert_int_equal(32767, ((struct lysc_type_num*)type)->range->parts[1].max_64); |
| 404 | |
| 405 | 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)); |
| 406 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 407 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 408 | assert_non_null(type); |
| 409 | assert_int_equal(LY_TYPE_INT32, type->basetype); |
| 410 | assert_non_null(((struct lysc_type_num*)type)->range); |
| 411 | assert_non_null(((struct lysc_type_num*)type)->range->parts); |
| 412 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts)); |
| 413 | assert_int_equal(INT64_C(-2147483648), ((struct lysc_type_num*)type)->range->parts[0].min_64); |
| 414 | assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64); |
| 415 | assert_int_equal(INT64_C(2147483647), ((struct lysc_type_num*)type)->range->parts[1].min_64); |
| 416 | assert_int_equal(INT64_C(2147483647), ((struct lysc_type_num*)type)->range->parts[1].max_64); |
| 417 | |
| 418 | 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)); |
| 419 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 420 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 421 | assert_non_null(type); |
| 422 | assert_int_equal(LY_TYPE_INT64, type->basetype); |
| 423 | assert_non_null(((struct lysc_type_num*)type)->range); |
| 424 | assert_non_null(((struct lysc_type_num*)type)->range->parts); |
| 425 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts)); |
| 426 | assert_int_equal(INT64_C(-9223372036854775807) - INT64_C(1), ((struct lysc_type_num*)type)->range->parts[0].min_64); |
| 427 | assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_64); |
| 428 | assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_num*)type)->range->parts[1].min_64); |
| 429 | assert_int_equal(INT64_C(9223372036854775807), ((struct lysc_type_num*)type)->range->parts[1].max_64); |
| 430 | |
| 431 | 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)); |
| 432 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 433 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 434 | assert_non_null(type); |
| 435 | assert_int_equal(LY_TYPE_UINT8, type->basetype); |
| 436 | assert_non_null(((struct lysc_type_num*)type)->range); |
| 437 | assert_non_null(((struct lysc_type_num*)type)->range->parts); |
| 438 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts)); |
| 439 | assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64); |
| 440 | assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64); |
| 441 | assert_int_equal(255, ((struct lysc_type_num*)type)->range->parts[1].min_u64); |
| 442 | assert_int_equal(255, ((struct lysc_type_num*)type)->range->parts[1].max_u64); |
| 443 | |
| 444 | 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)); |
| 445 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 446 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 447 | assert_non_null(type); |
| 448 | assert_int_equal(LY_TYPE_UINT16, type->basetype); |
| 449 | assert_non_null(((struct lysc_type_num*)type)->range); |
| 450 | assert_non_null(((struct lysc_type_num*)type)->range->parts); |
| 451 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts)); |
| 452 | assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64); |
| 453 | assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64); |
| 454 | assert_int_equal(65535, ((struct lysc_type_num*)type)->range->parts[1].min_u64); |
| 455 | assert_int_equal(65535, ((struct lysc_type_num*)type)->range->parts[1].max_u64); |
| 456 | |
| 457 | 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)); |
| 458 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 459 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 460 | assert_non_null(type); |
| 461 | assert_int_equal(LY_TYPE_UINT32, type->basetype); |
| 462 | assert_non_null(((struct lysc_type_num*)type)->range); |
| 463 | assert_non_null(((struct lysc_type_num*)type)->range->parts); |
| 464 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts)); |
| 465 | assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64); |
| 466 | assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64); |
| 467 | assert_int_equal(UINT64_C(4294967295), ((struct lysc_type_num*)type)->range->parts[1].min_u64); |
| 468 | assert_int_equal(UINT64_C(4294967295), ((struct lysc_type_num*)type)->range->parts[1].max_u64); |
| 469 | |
| 470 | 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)); |
| 471 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 472 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 473 | assert_non_null(type); |
| 474 | assert_int_equal(LY_TYPE_UINT64, type->basetype); |
| 475 | assert_non_null(((struct lysc_type_num*)type)->range); |
| 476 | assert_non_null(((struct lysc_type_num*)type)->range->parts); |
| 477 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts)); |
| 478 | assert_int_equal(0, ((struct lysc_type_num*)type)->range->parts[0].min_u64); |
| 479 | assert_int_equal(10, ((struct lysc_type_num*)type)->range->parts[0].max_u64); |
| 480 | assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_num*)type)->range->parts[1].min_u64); |
| 481 | assert_int_equal(UINT64_C(18446744073709551615), ((struct lysc_type_num*)type)->range->parts[1].max_u64); |
| 482 | |
| 483 | assert_non_null(mod = lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;typedef mytype {type uint8 {range 10..100;}}" |
| 484 | "typedef mytype2 {type mytype;} leaf l {type mytype2;}}", LYS_IN_YANG)); |
| 485 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 486 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 487 | assert_non_null(type); |
| 488 | assert_int_equal(3, type->refcount); |
| 489 | assert_int_equal(LY_TYPE_UINT8, type->basetype); |
| 490 | assert_non_null(((struct lysc_type_num*)type)->range); |
| 491 | assert_non_null(((struct lysc_type_num*)type)->range->parts); |
| 492 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_num*)type)->range->parts)); |
| 493 | |
| 494 | *state = NULL; |
| 495 | ly_ctx_destroy(ctx, NULL); |
| 496 | } |
| 497 | |
| 498 | static void |
| 499 | test_type_length(void **state) |
| 500 | { |
| 501 | *state = test_type_length; |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 502 | |
| 503 | struct ly_ctx *ctx; |
| 504 | struct lys_module *mod; |
| 505 | struct lysc_type *type; |
| 506 | |
| 507 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 508 | |
Radek Krejci | c5ddcc0 | 2018-11-12 13:28:18 +0100 | [diff] [blame] | 509 | 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] | 510 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 511 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 512 | assert_non_null(type); |
| 513 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 514 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
Radek Krejci | c5ddcc0 | 2018-11-12 13:28:18 +0100 | [diff] [blame] | 515 | assert_string_equal("errortag", ((struct lysc_type_bin*)type)->length->eapptag); |
| 516 | assert_string_equal("error", ((struct lysc_type_bin*)type)->length->emsg); |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 517 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 518 | assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
| 519 | assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
| 520 | |
| 521 | assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;leaf l {type binary {length max;}}}", LYS_IN_YANG)); |
| 522 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 523 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 524 | assert_non_null(type); |
| 525 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 526 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
| 527 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 528 | assert_int_equal(__UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
| 529 | assert_int_equal(__UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
| 530 | |
| 531 | 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)); |
| 532 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 533 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 534 | assert_non_null(type); |
| 535 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 536 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
| 537 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 538 | assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
| 539 | assert_int_equal(__UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
| 540 | |
| 541 | assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d;leaf l {type binary {length 5;}}}", LYS_IN_YANG)); |
| 542 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 543 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 544 | assert_non_null(type); |
| 545 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 546 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
| 547 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 548 | assert_int_equal(5, ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
| 549 | assert_int_equal(5, ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
| 550 | |
| 551 | 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)); |
| 552 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 553 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 554 | assert_non_null(type); |
| 555 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 556 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
| 557 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 558 | assert_int_equal(1, ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
| 559 | assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
| 560 | |
| 561 | 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)); |
| 562 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 563 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 564 | assert_non_null(type); |
| 565 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 566 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
| 567 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 568 | assert_int_equal(1, ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
| 569 | assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
| 570 | assert_int_equal(20, ((struct lysc_type_bin*)type)->length->parts[1].min_u64); |
| 571 | assert_int_equal(30, ((struct lysc_type_bin*)type)->length->parts[1].max_u64); |
| 572 | |
| 573 | 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)); |
| 574 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 575 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 576 | assert_non_null(type); |
| 577 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 578 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
| 579 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 580 | assert_int_equal(16, ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
| 581 | assert_int_equal(16, ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
| 582 | assert_int_equal(32, ((struct lysc_type_bin*)type)->length->parts[1].min_u64); |
| 583 | assert_int_equal(32, ((struct lysc_type_bin*)type)->length->parts[1].max_u64); |
| 584 | |
Radek Krejci | b31c899 | 2018-11-12 16:29:16 +0100 | [diff] [blame] | 585 | assert_non_null(mod = lys_parse_mem(ctx, "module h {namespace urn:h;prefix h;typedef mytype {type binary {length 10;}}" |
| 586 | "leaf l {type mytype {length \"10\";}}}", LYS_IN_YANG)); |
| 587 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 588 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 589 | assert_non_null(type); |
| 590 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 591 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
| 592 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 593 | assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
| 594 | assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
| 595 | |
| 596 | assert_non_null(mod = lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;typedef mytype {type binary {length 10..100;}}" |
| 597 | "leaf l {type mytype {length \"50\";}}}", LYS_IN_YANG)); |
| 598 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 599 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 600 | assert_non_null(type); |
| 601 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 602 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
| 603 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 604 | assert_int_equal(50, ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
| 605 | assert_int_equal(50, ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
| 606 | |
| 607 | assert_non_null(mod = lys_parse_mem(ctx, "module j {namespace urn:j;prefix j;typedef mytype {type binary {length 10..100;}}" |
| 608 | "leaf l {type mytype {length \"10..30|60..100\";}}}", LYS_IN_YANG)); |
| 609 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 610 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 611 | assert_non_null(type); |
| 612 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 613 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
| 614 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 615 | assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
| 616 | assert_int_equal(30, ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
| 617 | assert_int_equal(60, ((struct lysc_type_bin*)type)->length->parts[1].min_u64); |
| 618 | assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[1].max_u64); |
| 619 | |
| 620 | 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] | 621 | "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] | 622 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 623 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 624 | assert_non_null(type); |
Radek Krejci | 56aa27c | 2018-11-13 14:21:59 +0100 | [diff] [blame] | 625 | assert_int_equal(1, type->refcount); |
Radek Krejci | b31c899 | 2018-11-12 16:29:16 +0100 | [diff] [blame] | 626 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 627 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
| 628 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 629 | 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] | 630 | 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] | 631 | type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type; |
| 632 | assert_non_null(type); |
Radek Krejci | 56aa27c | 2018-11-13 14:21:59 +0100 | [diff] [blame] | 633 | assert_int_equal(2, type->refcount); |
Radek Krejci | b31c899 | 2018-11-12 16:29:16 +0100 | [diff] [blame] | 634 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 635 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
| 636 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 637 | assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
| 638 | assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
| 639 | |
Radek Krejci | 56aa27c | 2018-11-13 14:21:59 +0100 | [diff] [blame] | 640 | assert_non_null(mod = lys_parse_mem(ctx, "module l {namespace urn:l;prefix l;typedef mytype {type string {length 10..100;}}" |
| 641 | "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] | 642 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 643 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 644 | assert_non_null(type); |
| 645 | assert_int_equal(LY_TYPE_STRING, type->basetype); |
Radek Krejci | 56aa27c | 2018-11-13 14:21:59 +0100 | [diff] [blame] | 646 | assert_int_equal(1, type->refcount); |
Radek Krejci | cda7415 | 2018-11-13 15:27:32 +0100 | [diff] [blame^] | 647 | assert_non_null(((struct lysc_type_str*)type)->length); |
| 648 | assert_non_null(((struct lysc_type_str*)type)->length->parts); |
| 649 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->length->parts)); |
| 650 | assert_int_equal(10, ((struct lysc_type_str*)type)->length->parts[0].min_u64); |
| 651 | 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] | 652 | |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 653 | /* invalid values */ |
| 654 | assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;leaf l {type binary {length -10;}}}", LYS_IN_YANG)); |
| 655 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 656 | logbuf_assert("Invalid length restriction - value \"-10\" does not fit the type limitations."); |
| 657 | assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;leaf l {type binary {length 18446744073709551616;}}}", LYS_IN_YANG)); |
| 658 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 659 | logbuf_assert("Invalid length restriction - invalid value \"18446744073709551616\"."); |
| 660 | 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)); |
| 661 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 662 | logbuf_assert("Invalid length restriction - unexpected data after max keyword (.. 10)."); |
| 663 | 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)); |
| 664 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 665 | logbuf_assert("Invalid length restriction - values are not in ascending order (10)."); |
| 666 | 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)); |
| 667 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 668 | logbuf_assert("Invalid length restriction - values are not in ascending order (10)."); |
| 669 | assert_non_null(mod = lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;leaf l {type binary {length \"x\";}}}", LYS_IN_YANG)); |
| 670 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 671 | logbuf_assert("Invalid length restriction - unexpected data (x)."); |
Radek Krejci | b31c899 | 2018-11-12 16:29:16 +0100 | [diff] [blame] | 672 | 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)); |
| 673 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 674 | logbuf_assert("Invalid length restriction - unexpected data before min keyword (50 | )."); |
| 675 | assert_non_null(mod = lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;leaf l {type binary {length \"| 50\";}}}", LYS_IN_YANG)); |
| 676 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 677 | logbuf_assert("Invalid length restriction - unexpected beginning of the expression (| 50)."); |
| 678 | assert_non_null(mod = lys_parse_mem(ctx, "module ii {namespace urn:ii;prefix ii;leaf l {type binary {length \"10 ..\";}}}", LYS_IN_YANG)); |
| 679 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 680 | logbuf_assert("Invalid length restriction - unexpected end of the expression after \"..\" (10 ..)."); |
| 681 | assert_non_null(mod = lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;leaf l {type binary {length \".. 10\";}}}", LYS_IN_YANG)); |
| 682 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 683 | logbuf_assert("Invalid length restriction - unexpected \"..\" without a lower bound."); |
| 684 | assert_non_null(mod = lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;leaf l {type binary {length \"10 |\";}}}", LYS_IN_YANG)); |
| 685 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 686 | logbuf_assert("Invalid length restriction - unexpected end of the expression (10 |)."); |
Radek Krejci | 6489bbe | 2018-11-12 17:05:19 +0100 | [diff] [blame] | 687 | 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)); |
| 688 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 689 | logbuf_assert("Invalid length restriction - values are not in ascending order (15)."); |
Radek Krejci | b31c899 | 2018-11-12 16:29:16 +0100 | [diff] [blame] | 690 | |
| 691 | assert_non_null(mod = lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;typedef mytype {type binary {length 10;}}" |
| 692 | "leaf l {type mytype {length 11;}}}", LYS_IN_YANG)); |
| 693 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 694 | logbuf_assert("Invalid length restriction - the derived restriction (11) is not equally or more limiting."); |
| 695 | assert_non_null(mod = lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;typedef mytype {type binary {length 10..100;}}" |
| 696 | "leaf l {type mytype {length 1..11;}}}", LYS_IN_YANG)); |
| 697 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 698 | logbuf_assert("Invalid length restriction - the derived restriction (1..11) is not equally or more limiting."); |
| 699 | assert_non_null(mod = lys_parse_mem(ctx, "module nn {namespace urn:nn;prefix nn;typedef mytype {type binary {length 10..100;}}" |
| 700 | "leaf l {type mytype {length 20..110;}}}", LYS_IN_YANG)); |
| 701 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 702 | logbuf_assert("Invalid length restriction - the derived restriction (20..110) is not equally or more limiting."); |
| 703 | assert_non_null(mod = lys_parse_mem(ctx, "module oo {namespace urn:oo;prefix oo;typedef mytype {type binary {length 10..100;}}" |
| 704 | "leaf l {type mytype {length 20..30|110..120;}}}", LYS_IN_YANG)); |
| 705 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 706 | logbuf_assert("Invalid length restriction - the derived restriction (20..30|110..120) is not equally or more limiting."); |
| 707 | assert_non_null(mod = lys_parse_mem(ctx, "module pp {namespace urn:pp;prefix pp;typedef mytype {type binary {length 10..11;}}" |
| 708 | "leaf l {type mytype {length 15;}}}", LYS_IN_YANG)); |
| 709 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 710 | logbuf_assert("Invalid length restriction - the derived restriction (15) is not equally or more limiting."); |
| 711 | assert_non_null(mod = lys_parse_mem(ctx, "module qq {namespace urn:qq;prefix qq;typedef mytype {type binary {length 10..20|30..40;}}" |
| 712 | "leaf l {type mytype {length 15..35;}}}", LYS_IN_YANG)); |
| 713 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 714 | 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] | 715 | assert_non_null(mod = lys_parse_mem(ctx, "module rr {namespace urn:rr;prefix rr;typedef mytype {type binary {length 10;}}" |
| 716 | "leaf l {type mytype {length 10..35;}}}", LYS_IN_YANG)); |
| 717 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 718 | 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] | 719 | |
Radek Krejci | 495903e | 2018-11-13 11:30:45 +0100 | [diff] [blame] | 720 | 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)); |
| 721 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 722 | logbuf_assert("Invalid type restrictions for binary type."); |
| 723 | |
| 724 | assert_non_null(mod = lys_parse_mem(ctx, "module tt {namespace urn:tt;prefix tt;typedef mytype {type string {length 10;}}" |
| 725 | "leaf l {type mytype {length min..max;}}}", LYS_IN_YANG)); |
| 726 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 727 | 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] | 728 | |
| 729 | *state = NULL; |
| 730 | ly_ctx_destroy(ctx, NULL); |
| 731 | } |
| 732 | |
Radek Krejci | cda7415 | 2018-11-13 15:27:32 +0100 | [diff] [blame^] | 733 | static void |
| 734 | test_type_pattern(void **state) |
| 735 | { |
| 736 | *state = test_type_pattern; |
| 737 | |
| 738 | struct ly_ctx *ctx; |
| 739 | struct lys_module *mod; |
| 740 | struct lysc_type *type; |
| 741 | |
| 742 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 743 | |
| 744 | assert_non_null(mod = lys_parse_mem(ctx, "module a {namespace urn:a;prefix a;leaf l {type string {" |
| 745 | "pattern .* {error-app-tag errortag;error-message error;}" |
| 746 | "pattern [0-9].*[0-9] {modifier invert-match;}}}}", LYS_IN_YANG)); |
| 747 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 748 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 749 | assert_non_null(type); |
| 750 | assert_non_null(((struct lysc_type_str*)type)->patterns); |
| 751 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns)); |
| 752 | assert_string_equal("errortag", ((struct lysc_type_str*)type)->patterns[0]->eapptag); |
| 753 | assert_string_equal("error", ((struct lysc_type_str*)type)->patterns[0]->emsg); |
| 754 | assert_int_equal(0, ((struct lysc_type_str*)type)->patterns[0]->inverted); |
| 755 | assert_null(((struct lysc_type_str*)type)->patterns[1]->eapptag); |
| 756 | assert_null(((struct lysc_type_str*)type)->patterns[1]->emsg); |
| 757 | assert_int_equal(1, ((struct lysc_type_str*)type)->patterns[1]->inverted); |
| 758 | |
| 759 | assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;typedef mytype {type string {pattern '[0-9]*';}}" |
| 760 | "typedef mytype2 {type mytype {length 10;}} leaf l {type mytype2 {pattern '[0-4]*';}}}", LYS_IN_YANG)); |
| 761 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 762 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 763 | assert_non_null(type); |
| 764 | assert_int_equal(LY_TYPE_STRING, type->basetype); |
| 765 | assert_int_equal(1, type->refcount); |
| 766 | assert_non_null(((struct lysc_type_str*)type)->patterns); |
| 767 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns)); |
| 768 | assert_int_equal(3, ((struct lysc_type_str*)type)->patterns[0]->refcount); |
| 769 | assert_int_equal(1, ((struct lysc_type_str*)type)->patterns[1]->refcount); |
| 770 | |
| 771 | /* test substitutions */ |
| 772 | assert_non_null(mod = lys_parse_mem(ctx, "module c {namespace urn:c;prefix c;leaf l {type string {" |
| 773 | "pattern '^\\p{IsLatinExtended-A}$';}}}", LYS_IN_YANG)); |
| 774 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 775 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 776 | assert_non_null(type); |
| 777 | assert_non_null(((struct lysc_type_str*)type)->patterns); |
| 778 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_str*)type)->patterns)); |
| 779 | /* TODO check some data "^ř$" */ |
| 780 | |
| 781 | *state = NULL; |
| 782 | ly_ctx_destroy(ctx, NULL); |
| 783 | } |
| 784 | |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 785 | int main(void) |
| 786 | { |
| 787 | const struct CMUnitTest tests[] = { |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 788 | cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown), |
| 789 | cmocka_unit_test_setup_teardown(test_feature, logger_setup, logger_teardown), |
| 790 | cmocka_unit_test_setup_teardown(test_identity, logger_setup, logger_teardown), |
Radek Krejci | 0f07bed | 2018-11-13 14:01:40 +0100 | [diff] [blame] | 791 | cmocka_unit_test_setup_teardown(test_type_length, logger_setup, logger_teardown), |
| 792 | cmocka_unit_test_setup_teardown(test_type_range, logger_setup, logger_teardown), |
Radek Krejci | cda7415 | 2018-11-13 15:27:32 +0100 | [diff] [blame^] | 793 | cmocka_unit_test_setup_teardown(test_type_pattern, logger_setup, logger_teardown), |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 794 | cmocka_unit_test_setup_teardown(test_node_container, logger_setup, logger_teardown), |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 795 | }; |
| 796 | |
| 797 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 798 | } |