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 | |
| 40 | if (path) { |
| 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 | |
| 58 | void |
| 59 | logbuf_clean(void) |
| 60 | { |
| 61 | logbuf[0] = '\0'; |
| 62 | } |
| 63 | |
| 64 | #if ENABLE_LOGGER_CHECKING |
| 65 | # define logbuf_assert(str) assert_string_equal(logbuf, str) |
| 66 | #else |
| 67 | # define logbuf_assert(str) |
| 68 | #endif |
| 69 | |
| 70 | static void |
| 71 | test_module(void **state) |
| 72 | { |
| 73 | (void) state; /* unused */ |
| 74 | |
| 75 | const char *str; |
| 76 | struct ly_ctx *ctx; |
| 77 | struct lys_module mod = {0}; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 78 | struct lysc_feature *f; |
| 79 | struct lysc_iffeature *iff; |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 80 | |
| 81 | str = "module test {namespace urn:test; prefix t;" |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 82 | "feature f1;feature f2 {if-feature f1;}}"; |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 83 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx)); |
| 84 | |
| 85 | assert_int_equal(LY_EINVAL, lys_compile(NULL, 0, NULL)); |
| 86 | logbuf_assert("Invalid argument sc (lys_compile())."); |
| 87 | assert_int_equal(LY_EINVAL, lys_compile(NULL, 0, &mod.compiled)); |
| 88 | logbuf_assert("Invalid argument sp (lys_compile())."); |
| 89 | assert_int_equal(LY_SUCCESS, yang_parse(ctx, str, &mod.parsed)); |
| 90 | assert_int_equal(LY_SUCCESS, lys_compile(mod.parsed, 0, &mod.compiled)); |
| 91 | assert_non_null(mod.compiled); |
| 92 | assert_ptr_equal(mod.parsed->name, mod.compiled->name); |
| 93 | assert_ptr_equal(mod.parsed->ns, mod.compiled->ns); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 94 | /* features */ |
| 95 | assert_non_null(mod.compiled->features); |
| 96 | assert_int_equal(2, LY_ARRAY_SIZE(mod.compiled->features)); |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 97 | f = &mod.compiled->features[1]; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 98 | assert_non_null(f->iffeatures); |
| 99 | assert_int_equal(1, LY_ARRAY_SIZE(f->iffeatures)); |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 100 | iff = &f->iffeatures[0]; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 101 | assert_non_null(iff->expr); |
| 102 | assert_non_null(iff->features); |
| 103 | assert_int_equal(1, LY_ARRAY_SIZE(iff->features)); |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 104 | assert_ptr_equal(&mod.compiled->features[0], iff->features[0]); |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 105 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 106 | lysc_module_free(mod.compiled, NULL); |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 107 | |
| 108 | assert_int_equal(LY_SUCCESS, lys_compile(mod.parsed, LYSC_OPT_FREE_SP, &mod.compiled)); |
| 109 | assert_non_null(mod.compiled); |
| 110 | assert_string_equal("test", mod.compiled->name); |
| 111 | assert_string_equal("urn:test", mod.compiled->ns); |
| 112 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 113 | lysc_module_free(mod.compiled, NULL); |
| 114 | mod.compiled = NULL; |
| 115 | |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 116 | /* submodules cannot be compiled directly */ |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 117 | str = "submodule test {belongs-to xxx {prefix x;}}"; |
| 118 | assert_int_equal(LY_SUCCESS, yang_parse(ctx, str, &mod.parsed)); |
| 119 | assert_int_equal(LY_EINVAL, lys_compile(mod.parsed, 0, &mod.compiled)); |
| 120 | logbuf_assert("Submodules (test) are not supposed to be compiled, compile only the main modules."); |
| 121 | assert_null(mod.compiled); |
| 122 | |
| 123 | lysp_module_free(mod.parsed); |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 124 | ly_ctx_destroy(ctx, NULL); |
| 125 | } |
| 126 | |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 127 | static void |
| 128 | test_feature(void **state) |
| 129 | { |
| 130 | (void) state; /* unused */ |
| 131 | |
| 132 | struct ly_ctx *ctx; |
| 133 | struct lys_module mod = {0}; |
| 134 | const char *str; |
| 135 | struct lysc_feature *f, *f1; |
| 136 | |
| 137 | str = "module a {namespace urn:a;prefix a;yang-version 1.1;\n" |
| 138 | "feature f1 {description test1;reference test2;status current;} feature f2; feature f3;\n" |
| 139 | "feature f4 {if-feature \"f1 or f2\";}\n" |
| 140 | "feature f5 {if-feature \"f1 and f2\";}\n" |
| 141 | "feature f6 {if-feature \"not f1\";}\n" |
| 142 | "feature f7 {if-feature \"(f2 and f3) or (not f1)\";}}"; |
| 143 | |
| 144 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx)); |
| 145 | assert_int_equal(LY_SUCCESS, yang_parse(ctx, str, &mod.parsed)); |
| 146 | assert_int_equal(LY_SUCCESS, lys_compile(mod.parsed, 0, &mod.compiled)); |
| 147 | assert_non_null(mod.compiled); |
| 148 | assert_non_null(mod.compiled->features); |
| 149 | assert_int_equal(7, LY_ARRAY_SIZE(mod.compiled->features)); |
| 150 | /* all features are disabled by default */ |
| 151 | LY_ARRAY_FOR(mod.compiled->features, struct lysc_feature, f) { |
| 152 | assert_int_equal(0, lysc_feature_value(f)); |
| 153 | } |
| 154 | /* enable f1 */ |
| 155 | assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f1")); |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 156 | f1 = &mod.compiled->features[0]; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 157 | assert_int_equal(1, lysc_feature_value(f1)); |
| 158 | |
| 159 | /* enable f4 */ |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 160 | f = &mod.compiled->features[3]; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 161 | assert_int_equal(0, lysc_feature_value(f)); |
| 162 | assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f4")); |
| 163 | assert_int_equal(1, lysc_feature_value(f)); |
| 164 | |
| 165 | /* enable f5 - no possible since f2 is disabled */ |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 166 | f = &mod.compiled->features[4]; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 167 | assert_int_equal(0, lysc_feature_value(f)); |
| 168 | assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "f5")); |
| 169 | logbuf_assert("Feature \"f5\" cannot be enabled since it is disabled by its if-feature condition(s)."); |
| 170 | assert_int_equal(0, lysc_feature_value(f)); |
| 171 | |
| 172 | /* first enable f2, so f5 can be enabled then */ |
| 173 | assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f2")); |
| 174 | assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f5")); |
| 175 | assert_int_equal(1, lysc_feature_value(f)); |
| 176 | |
| 177 | /* f1 is enabled, so f6 cannot be enabled */ |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 178 | f = &mod.compiled->features[5]; |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 179 | assert_int_equal(0, lysc_feature_value(f)); |
| 180 | assert_int_equal(LY_EDENIED, lys_feature_enable(&mod, "f6")); |
| 181 | logbuf_assert("Feature \"f6\" cannot be enabled since it is disabled by its if-feature condition(s)."); |
| 182 | assert_int_equal(0, lysc_feature_value(f)); |
| 183 | |
| 184 | /* so disable f1 - f5 will became also disabled */ |
| 185 | assert_int_equal(1, lysc_feature_value(f1)); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 186 | assert_int_equal(LY_SUCCESS, lys_feature_disable(&mod, "f1")); |
| 187 | assert_int_equal(0, lysc_feature_value(f1)); |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 188 | assert_int_equal(0, lysc_feature_value(&mod.compiled->features[4])); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 189 | /* while f4 is stille enabled */ |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 190 | assert_int_equal(1, lysc_feature_value(&mod.compiled->features[3])); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 191 | /* and finally f6 can be enabled */ |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 192 | assert_int_equal(LY_SUCCESS, lys_feature_enable(&mod, "f6")); |
Radek Krejci | 2c4e717 | 2018-10-19 15:56:26 +0200 | [diff] [blame] | 193 | assert_int_equal(1, lysc_feature_value(&mod.compiled->features[5])); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 194 | |
| 195 | /* 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] | 196 | assert_int_equal(1, lysc_iffeature_value(&mod.compiled->features[6].iffeatures[0])); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 197 | |
| 198 | lysc_module_free(mod.compiled, NULL); |
| 199 | lysp_module_free(mod.parsed); |
| 200 | ly_ctx_destroy(ctx, NULL); |
| 201 | } |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 202 | |
| 203 | int main(void) |
| 204 | { |
| 205 | const struct CMUnitTest tests[] = { |
| 206 | cmocka_unit_test_setup(test_module, logger_setup), |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 207 | cmocka_unit_test_setup(test_feature, logger_setup), |
Radek Krejci | dd4e8d4 | 2018-10-16 14:55:43 +0200 | [diff] [blame] | 208 | }; |
| 209 | |
| 210 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 211 | } |