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 | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 364 | static void |
Radek Krejci | b31c899 | 2018-11-12 16:29:16 +0100 | [diff] [blame] | 365 | test_node_type_length(void **state) |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 366 | { |
Radek Krejci | b31c899 | 2018-11-12 16:29:16 +0100 | [diff] [blame] | 367 | *state = test_node_type_length; |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 368 | |
| 369 | struct ly_ctx *ctx; |
| 370 | struct lys_module *mod; |
| 371 | struct lysc_type *type; |
| 372 | |
| 373 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, LY_CTX_DISABLE_SEARCHDIRS, &ctx)); |
| 374 | |
Radek Krejci | c5ddcc0 | 2018-11-12 13:28:18 +0100 | [diff] [blame] | 375 | 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] | 376 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 377 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 378 | assert_non_null(type); |
| 379 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 380 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
Radek Krejci | c5ddcc0 | 2018-11-12 13:28:18 +0100 | [diff] [blame] | 381 | assert_string_equal("errortag", ((struct lysc_type_bin*)type)->length->eapptag); |
| 382 | assert_string_equal("error", ((struct lysc_type_bin*)type)->length->emsg); |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 383 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 384 | assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
| 385 | assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
| 386 | |
| 387 | assert_non_null(mod = lys_parse_mem(ctx, "module b {namespace urn:b;prefix b;leaf l {type binary {length max;}}}", LYS_IN_YANG)); |
| 388 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 389 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 390 | assert_non_null(type); |
| 391 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 392 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
| 393 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 394 | assert_int_equal(__UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
| 395 | assert_int_equal(__UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
| 396 | |
| 397 | 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)); |
| 398 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 399 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 400 | assert_non_null(type); |
| 401 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 402 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
| 403 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 404 | assert_int_equal(0, ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
| 405 | assert_int_equal(__UINT64_C(18446744073709551615), ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
| 406 | |
| 407 | assert_non_null(mod = lys_parse_mem(ctx, "module d {namespace urn:d;prefix d;leaf l {type binary {length 5;}}}", LYS_IN_YANG)); |
| 408 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 409 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 410 | assert_non_null(type); |
| 411 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 412 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
| 413 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 414 | assert_int_equal(5, ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
| 415 | assert_int_equal(5, ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
| 416 | |
| 417 | 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)); |
| 418 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 419 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 420 | assert_non_null(type); |
| 421 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 422 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
| 423 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 424 | assert_int_equal(1, ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
| 425 | assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
| 426 | |
| 427 | 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)); |
| 428 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 429 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 430 | assert_non_null(type); |
| 431 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 432 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
| 433 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 434 | assert_int_equal(1, ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
| 435 | assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
| 436 | assert_int_equal(20, ((struct lysc_type_bin*)type)->length->parts[1].min_u64); |
| 437 | assert_int_equal(30, ((struct lysc_type_bin*)type)->length->parts[1].max_u64); |
| 438 | |
| 439 | 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)); |
| 440 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 441 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 442 | assert_non_null(type); |
| 443 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 444 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
| 445 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 446 | assert_int_equal(16, ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
| 447 | assert_int_equal(16, ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
| 448 | assert_int_equal(32, ((struct lysc_type_bin*)type)->length->parts[1].min_u64); |
| 449 | assert_int_equal(32, ((struct lysc_type_bin*)type)->length->parts[1].max_u64); |
| 450 | |
Radek Krejci | b31c899 | 2018-11-12 16:29:16 +0100 | [diff] [blame] | 451 | assert_non_null(mod = lys_parse_mem(ctx, "module h {namespace urn:h;prefix h;typedef mytype {type binary {length 10;}}" |
| 452 | "leaf l {type mytype {length \"10\";}}}", LYS_IN_YANG)); |
| 453 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 454 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 455 | assert_non_null(type); |
| 456 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 457 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
| 458 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 459 | assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
| 460 | assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
| 461 | |
| 462 | assert_non_null(mod = lys_parse_mem(ctx, "module i {namespace urn:i;prefix i;typedef mytype {type binary {length 10..100;}}" |
| 463 | "leaf l {type mytype {length \"50\";}}}", LYS_IN_YANG)); |
| 464 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 465 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 466 | assert_non_null(type); |
| 467 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 468 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
| 469 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 470 | assert_int_equal(50, ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
| 471 | assert_int_equal(50, ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
| 472 | |
| 473 | assert_non_null(mod = lys_parse_mem(ctx, "module j {namespace urn:j;prefix j;typedef mytype {type binary {length 10..100;}}" |
| 474 | "leaf l {type mytype {length \"10..30|60..100\";}}}", LYS_IN_YANG)); |
| 475 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 476 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 477 | assert_non_null(type); |
| 478 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 479 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
| 480 | assert_int_equal(2, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 481 | assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
| 482 | assert_int_equal(30, ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
| 483 | assert_int_equal(60, ((struct lysc_type_bin*)type)->length->parts[1].min_u64); |
| 484 | assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[1].max_u64); |
| 485 | |
| 486 | assert_non_null(mod = lys_parse_mem(ctx, "module k {namespace urn:k;prefix k;typedef mytype {type binary {length 10..100;}}" |
| 487 | "leaf l {type mytype {length \"10..100\";}}leaf ll {type mytype;}}", LYS_IN_YANG)); |
| 488 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 489 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 490 | assert_non_null(type); |
| 491 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 492 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
| 493 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 494 | assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
| 495 | assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
| 496 | type = ((struct lysc_node_leaf*)mod->compiled->data->next)->type; |
| 497 | assert_non_null(type); |
| 498 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 499 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
| 500 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 501 | assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
| 502 | assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
| 503 | |
Radek Krejci | 9a191e6 | 2018-11-13 13:19:56 +0100 | [diff] [blame^] | 504 | assert_non_null(mod = lys_parse_mem(ctx, "module l {namespace urn:l;prefix l;typedef mytype {type binary {length 10..100;}}" |
| 505 | "typedef mytype2 {type mytype;} leaf l {type mytype2;}}", LYS_IN_YANG)); |
| 506 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 507 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 508 | assert_non_null(type); |
| 509 | assert_int_equal(LY_TYPE_BINARY, type->basetype); |
| 510 | assert_int_equal(3, type->refcount); |
| 511 | assert_non_null(((struct lysc_type_bin*)type)->length); |
| 512 | assert_non_null(((struct lysc_type_bin*)type)->length->parts); |
| 513 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 514 | assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
| 515 | assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
| 516 | assert_non_null(mod = lys_parse_mem(ctx, "module m {namespace urn:m;prefix m;typedef mytype {type string {length 10..100;}}" |
| 517 | "typedef mytype2 {type mytype;} leaf l {type mytype2;}}", LYS_IN_YANG)); |
| 518 | assert_int_equal(LY_SUCCESS, lys_compile(mod, 0)); |
| 519 | type = ((struct lysc_node_leaf*)mod->compiled->data)->type; |
| 520 | assert_non_null(type); |
| 521 | assert_int_equal(LY_TYPE_STRING, type->basetype); |
| 522 | assert_int_equal(3, type->refcount); |
| 523 | assert_non_null(((struct lysc_type_str*)type)->length); |
| 524 | assert_non_null(((struct lysc_type_str*)type)->length->parts); |
| 525 | assert_int_equal(1, LY_ARRAY_SIZE(((struct lysc_type_bin*)type)->length->parts)); |
| 526 | assert_int_equal(10, ((struct lysc_type_bin*)type)->length->parts[0].min_u64); |
| 527 | assert_int_equal(100, ((struct lysc_type_bin*)type)->length->parts[0].max_u64); |
| 528 | |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 529 | /* invalid values */ |
| 530 | assert_non_null(mod = lys_parse_mem(ctx, "module aa {namespace urn:aa;prefix aa;leaf l {type binary {length -10;}}}", LYS_IN_YANG)); |
| 531 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 532 | logbuf_assert("Invalid length restriction - value \"-10\" does not fit the type limitations."); |
| 533 | assert_non_null(mod = lys_parse_mem(ctx, "module bb {namespace urn:bb;prefix bb;leaf l {type binary {length 18446744073709551616;}}}", LYS_IN_YANG)); |
| 534 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 535 | logbuf_assert("Invalid length restriction - invalid value \"18446744073709551616\"."); |
| 536 | 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)); |
| 537 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 538 | logbuf_assert("Invalid length restriction - unexpected data after max keyword (.. 10)."); |
| 539 | 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)); |
| 540 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 541 | logbuf_assert("Invalid length restriction - values are not in ascending order (10)."); |
| 542 | 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)); |
| 543 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 544 | logbuf_assert("Invalid length restriction - values are not in ascending order (10)."); |
| 545 | assert_non_null(mod = lys_parse_mem(ctx, "module ff {namespace urn:ff;prefix ff;leaf l {type binary {length \"x\";}}}", LYS_IN_YANG)); |
| 546 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 547 | logbuf_assert("Invalid length restriction - unexpected data (x)."); |
Radek Krejci | b31c899 | 2018-11-12 16:29:16 +0100 | [diff] [blame] | 548 | 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)); |
| 549 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 550 | logbuf_assert("Invalid length restriction - unexpected data before min keyword (50 | )."); |
| 551 | assert_non_null(mod = lys_parse_mem(ctx, "module hh {namespace urn:hh;prefix hh;leaf l {type binary {length \"| 50\";}}}", LYS_IN_YANG)); |
| 552 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 553 | logbuf_assert("Invalid length restriction - unexpected beginning of the expression (| 50)."); |
| 554 | assert_non_null(mod = lys_parse_mem(ctx, "module ii {namespace urn:ii;prefix ii;leaf l {type binary {length \"10 ..\";}}}", LYS_IN_YANG)); |
| 555 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 556 | logbuf_assert("Invalid length restriction - unexpected end of the expression after \"..\" (10 ..)."); |
| 557 | assert_non_null(mod = lys_parse_mem(ctx, "module jj {namespace urn:jj;prefix jj;leaf l {type binary {length \".. 10\";}}}", LYS_IN_YANG)); |
| 558 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 559 | logbuf_assert("Invalid length restriction - unexpected \"..\" without a lower bound."); |
| 560 | assert_non_null(mod = lys_parse_mem(ctx, "module kk {namespace urn:kk;prefix kk;leaf l {type binary {length \"10 |\";}}}", LYS_IN_YANG)); |
| 561 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 562 | logbuf_assert("Invalid length restriction - unexpected end of the expression (10 |)."); |
Radek Krejci | 6489bbe | 2018-11-12 17:05:19 +0100 | [diff] [blame] | 563 | 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)); |
| 564 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 565 | logbuf_assert("Invalid length restriction - values are not in ascending order (15)."); |
Radek Krejci | b31c899 | 2018-11-12 16:29:16 +0100 | [diff] [blame] | 566 | |
| 567 | assert_non_null(mod = lys_parse_mem(ctx, "module ll {namespace urn:ll;prefix ll;typedef mytype {type binary {length 10;}}" |
| 568 | "leaf l {type mytype {length 11;}}}", LYS_IN_YANG)); |
| 569 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 570 | logbuf_assert("Invalid length restriction - the derived restriction (11) is not equally or more limiting."); |
| 571 | assert_non_null(mod = lys_parse_mem(ctx, "module mm {namespace urn:mm;prefix mm;typedef mytype {type binary {length 10..100;}}" |
| 572 | "leaf l {type mytype {length 1..11;}}}", LYS_IN_YANG)); |
| 573 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 574 | logbuf_assert("Invalid length restriction - the derived restriction (1..11) is not equally or more limiting."); |
| 575 | assert_non_null(mod = lys_parse_mem(ctx, "module nn {namespace urn:nn;prefix nn;typedef mytype {type binary {length 10..100;}}" |
| 576 | "leaf l {type mytype {length 20..110;}}}", LYS_IN_YANG)); |
| 577 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 578 | logbuf_assert("Invalid length restriction - the derived restriction (20..110) is not equally or more limiting."); |
| 579 | assert_non_null(mod = lys_parse_mem(ctx, "module oo {namespace urn:oo;prefix oo;typedef mytype {type binary {length 10..100;}}" |
| 580 | "leaf l {type mytype {length 20..30|110..120;}}}", LYS_IN_YANG)); |
| 581 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 582 | logbuf_assert("Invalid length restriction - the derived restriction (20..30|110..120) is not equally or more limiting."); |
| 583 | assert_non_null(mod = lys_parse_mem(ctx, "module pp {namespace urn:pp;prefix pp;typedef mytype {type binary {length 10..11;}}" |
| 584 | "leaf l {type mytype {length 15;}}}", LYS_IN_YANG)); |
| 585 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 586 | logbuf_assert("Invalid length restriction - the derived restriction (15) is not equally or more limiting."); |
| 587 | assert_non_null(mod = lys_parse_mem(ctx, "module qq {namespace urn:qq;prefix qq;typedef mytype {type binary {length 10..20|30..40;}}" |
| 588 | "leaf l {type mytype {length 15..35;}}}", LYS_IN_YANG)); |
| 589 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 590 | 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] | 591 | assert_non_null(mod = lys_parse_mem(ctx, "module rr {namespace urn:rr;prefix rr;typedef mytype {type binary {length 10;}}" |
| 592 | "leaf l {type mytype {length 10..35;}}}", LYS_IN_YANG)); |
| 593 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 594 | 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] | 595 | |
Radek Krejci | 495903e | 2018-11-13 11:30:45 +0100 | [diff] [blame] | 596 | 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)); |
| 597 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 598 | logbuf_assert("Invalid type restrictions for binary type."); |
| 599 | |
| 600 | assert_non_null(mod = lys_parse_mem(ctx, "module tt {namespace urn:tt;prefix tt;typedef mytype {type string {length 10;}}" |
| 601 | "leaf l {type mytype {length min..max;}}}", LYS_IN_YANG)); |
| 602 | assert_int_equal(LY_EVALID, lys_compile(mod, 0)); |
| 603 | 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] | 604 | |
| 605 | *state = NULL; |
| 606 | ly_ctx_destroy(ctx, NULL); |
| 607 | } |
| 608 | |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 609 | int main(void) |
| 610 | { |
| 611 | const struct CMUnitTest tests[] = { |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 612 | cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown), |
| 613 | cmocka_unit_test_setup_teardown(test_feature, logger_setup, logger_teardown), |
| 614 | cmocka_unit_test_setup_teardown(test_identity, logger_setup, logger_teardown), |
Radek Krejci | b31c899 | 2018-11-12 16:29:16 +0100 | [diff] [blame] | 615 | cmocka_unit_test_setup_teardown(test_node_type_length, logger_setup, logger_teardown), |
Radek Krejci | 4f28eda | 2018-11-12 11:46:16 +0100 | [diff] [blame] | 616 | cmocka_unit_test_setup_teardown(test_node_container, logger_setup, logger_teardown), |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 617 | }; |
| 618 | |
| 619 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 620 | } |