Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 1 | /* |
| 2 | * @file test_printer_yang.c |
| 3 | * @author: Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief unit tests for functions from printer_yang.c |
| 5 | * |
| 6 | * Copyright (c) 2019 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 | |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 15 | #include "common.h" |
Radek Krejci | 70593c1 | 2020-06-13 20:48:09 +0200 | [diff] [blame] | 16 | #include "context.h" |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 17 | #include "out.h" |
Radek Krejci | 70593c1 | 2020-06-13 20:48:09 +0200 | [diff] [blame] | 18 | #include "printer_schema.h" |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 19 | #include "tree_schema.h" |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 20 | #include "utests.h" |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 21 | |
| 22 | #define BUFSIZE 1024 |
| 23 | char logbuf[BUFSIZE] = {0}; |
| 24 | int store = -1; /* negative for infinite logging, positive for limited logging */ |
| 25 | |
| 26 | /* set to 0 to printing error messages to stderr instead of checking them in code */ |
| 27 | #define ENABLE_LOGGER_CHECKING 1 |
| 28 | |
| 29 | #if ENABLE_LOGGER_CHECKING |
| 30 | static void |
| 31 | logger(LY_LOG_LEVEL level, const char *msg, const char *path) |
| 32 | { |
| 33 | (void) level; /* unused */ |
| 34 | if (store) { |
| 35 | if (path && path[0]) { |
| 36 | snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path); |
| 37 | } else { |
| 38 | strncpy(logbuf, msg, BUFSIZE - 1); |
| 39 | } |
| 40 | if (store > 0) { |
| 41 | --store; |
| 42 | } |
| 43 | } |
| 44 | } |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 45 | |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 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 | 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 | |
| 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 | |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 82 | static void |
| 83 | test_module(void **state) |
| 84 | { |
| 85 | *state = test_module; |
| 86 | |
| 87 | struct ly_ctx *ctx = {0}; |
| 88 | const struct lys_module *mod; |
| 89 | const char *orig = "module a {\n" |
| 90 | " yang-version 1.1;\n" |
| 91 | " namespace \"urn:test:a\";\n" |
| 92 | " prefix a;\n\n" |
| 93 | " import ietf-yang-types {\n" |
| 94 | " prefix yt;\n" |
| 95 | " revision-date 2013-07-15;\n" |
| 96 | " description\n" |
| 97 | " \"YANG types\";\n" |
| 98 | " reference\n" |
| 99 | " \"RFC reference\";\n" |
| 100 | " }\n\n" |
| 101 | " organization\n" |
| 102 | " \"ORG\";\n" |
| 103 | " contact\n" |
| 104 | " \"Radek Krejci.\";\n" |
| 105 | " description\n" |
| 106 | " \"Long multiline\n" |
| 107 | " description.\";\n" |
| 108 | " reference\n" |
| 109 | " \"some reference\";\n" |
| 110 | "}\n"; |
Radek Krejci | ffb410f | 2019-04-30 09:51:51 +0200 | [diff] [blame] | 111 | char *compiled = "module a {\n" |
Radek Krejci | ffb410f | 2019-04-30 09:51:51 +0200 | [diff] [blame] | 112 | " namespace \"urn:test:a\";\n" |
| 113 | " prefix a;\n\n" |
Radek Krejci | ffb410f | 2019-04-30 09:51:51 +0200 | [diff] [blame] | 114 | " organization\n" |
| 115 | " \"ORG\";\n" |
| 116 | " contact\n" |
| 117 | " \"Radek Krejci.\";\n" |
| 118 | " description\n" |
| 119 | " \"Long multiline\n" |
| 120 | " description.\";\n" |
| 121 | " reference\n" |
| 122 | " \"some reference\";\n" |
| 123 | "}\n"; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 124 | char *printed; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 125 | struct ly_out *out; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 126 | |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 127 | assert_int_equal(LY_SUCCESS, ly_out_new_memory(&printed, 0, &out)); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 128 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx)); |
| 129 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 130 | assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, orig, LYS_IN_YANG, &mod)); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 131 | assert_int_equal(LY_SUCCESS, lys_print_module(out, mod, LYS_OUT_YANG, 0, 0)); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 132 | assert_int_equal(strlen(orig), ly_out_printed(out)); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 133 | assert_string_equal(printed, orig); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 134 | ly_out_reset(out); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 135 | assert_int_equal(LY_SUCCESS, lys_print_module(out, mod, LYS_OUT_YANG_COMPILED, 0, 0)); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 136 | assert_int_equal(strlen(compiled), ly_out_printed(out)); |
Radek Krejci | ffb410f | 2019-04-30 09:51:51 +0200 | [diff] [blame] | 137 | assert_string_equal(printed, compiled); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 138 | ly_out_reset(out); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 139 | |
| 140 | orig = "module b {\n" |
| 141 | " yang-version 1.1;\n" |
| 142 | " namespace \"urn:test:b\";\n" |
| 143 | " prefix b;\n\n" |
| 144 | " revision 2019-04-16 {\n" |
| 145 | " description\n" |
| 146 | " \"text\";\n" |
| 147 | " reference\n" |
| 148 | " \"text\";\n" |
| 149 | " }\n" |
| 150 | " revision 2019-04-15 {\n" |
| 151 | " description\n" |
| 152 | " \"initial revision\";\n" |
| 153 | " }\n\n" |
| 154 | " feature f1 {\n" |
| 155 | " status current;\n" |
| 156 | " description\n" |
| 157 | " \"text\";\n" |
| 158 | " reference\n" |
| 159 | " \"text\";\n" |
| 160 | " }\n\n" |
| 161 | " feature f2 {\n" |
| 162 | " if-feature \"not f1\";\n" |
| 163 | " }\n" |
| 164 | "}\n"; |
Radek Krejci | ffb410f | 2019-04-30 09:51:51 +0200 | [diff] [blame] | 165 | compiled = "module b {\n" |
Radek Krejci | ffb410f | 2019-04-30 09:51:51 +0200 | [diff] [blame] | 166 | " namespace \"urn:test:b\";\n" |
| 167 | " prefix b;\n\n" |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 168 | " revision 2019-04-16;\n" |
Radek Krejci | ffb410f | 2019-04-30 09:51:51 +0200 | [diff] [blame] | 169 | "}\n"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 170 | assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, orig, LYS_IN_YANG, &mod)); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 171 | assert_int_equal(LY_SUCCESS, lys_print_module(out, mod, LYS_OUT_YANG, 0, 0)); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 172 | assert_int_equal(strlen(orig), ly_out_printed(out)); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 173 | assert_string_equal(printed, orig); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 174 | ly_out_reset(out); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 175 | assert_int_equal(LY_SUCCESS, lys_print_module(out, mod, LYS_OUT_YANG_COMPILED, 0, 0)); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 176 | assert_int_equal(strlen(compiled), ly_out_printed(out)); |
Radek Krejci | ffb410f | 2019-04-30 09:51:51 +0200 | [diff] [blame] | 177 | assert_string_equal(printed, compiled); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 178 | ly_out_reset(out); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 179 | |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 180 | orig = "module c {\n" |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 181 | " yang-version 1.1;\n" |
| 182 | " namespace \"urn:test:c\";\n" |
| 183 | " prefix c;\n\n" |
| 184 | " feature f1;\n\n" |
| 185 | " identity i1 {\n" |
| 186 | " if-feature \"f1\";\n" |
| 187 | " description\n" |
| 188 | " \"text\";\n" |
| 189 | " reference\n" |
| 190 | " \"text32\";\n" |
| 191 | " }\n\n" |
| 192 | " identity i2 {\n" |
| 193 | " base i1;\n" |
| 194 | " status obsolete;\n" |
| 195 | " }\n" |
| 196 | "}\n"; |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 197 | compiled = "module c {\n" |
| 198 | " namespace \"urn:test:c\";\n" |
| 199 | " prefix c;\n" |
| 200 | "}\n"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 201 | assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, orig, LYS_IN_YANG, &mod)); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 202 | assert_int_equal(LY_SUCCESS, lys_print_module(out, mod, LYS_OUT_YANG, 0, 0)); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 203 | assert_int_equal(strlen(orig), ly_out_printed(out)); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 204 | assert_string_equal(printed, orig); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 205 | ly_out_reset(out); |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 206 | assert_int_equal(LY_SUCCESS, lys_print_module(out, mod, LYS_OUT_YANG_COMPILED, 0, 0)); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 207 | assert_int_equal(strlen(compiled), ly_out_printed(out)); |
Radek Krejci | ffb410f | 2019-04-30 09:51:51 +0200 | [diff] [blame] | 208 | assert_string_equal(printed, compiled); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 209 | |
| 210 | *state = NULL; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 211 | ly_out_free(out, NULL, 1); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 212 | ly_ctx_destroy(ctx, NULL); |
| 213 | } |
| 214 | |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 215 | static LY_ERR |
| 216 | test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name), |
| 217 | const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format, |
| 218 | const char **module_data, void (**free_module_data)(void *model_data, void *user_data)) |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 219 | { |
| 220 | *module_data = user_data; |
| 221 | *format = LYS_IN_YANG; |
| 222 | *free_module_data = NULL; |
| 223 | return LY_SUCCESS; |
| 224 | } |
| 225 | |
| 226 | static void |
| 227 | test_submodule(void **state) |
| 228 | { |
| 229 | *state = test_submodule; |
| 230 | |
| 231 | struct ly_ctx *ctx = {0}; |
| 232 | const struct lys_module *mod; |
| 233 | const char *mod_yang = "module a {\n" |
| 234 | " yang-version 1.1;\n" |
| 235 | " namespace \"urn:test:a\";\n" |
| 236 | " prefix a;\n\n" |
| 237 | " include a-sub;\n" |
| 238 | "}\n"; |
| 239 | char *submod_yang = "submodule a-sub {\n" |
| 240 | " yang-version 1.1;\n" |
| 241 | " belongs-to a {\n" |
| 242 | " prefix a;\n" |
| 243 | " }\n\n" |
| 244 | " import ietf-yang-types {\n" |
| 245 | " prefix yt;\n" |
| 246 | " revision-date 2013-07-15;\n" |
| 247 | " }\n\n" |
| 248 | " organization\n" |
| 249 | " \"ORG\";\n" |
| 250 | " contact\n" |
| 251 | " \"Radek Krejci.\";\n" |
| 252 | " description\n" |
| 253 | " \"Long multiline\n" |
| 254 | " description.\";\n" |
| 255 | " reference\n" |
| 256 | " \"some reference\";\n" |
| 257 | "}\n"; |
| 258 | char *printed; |
| 259 | struct ly_out *out; |
| 260 | |
| 261 | assert_int_equal(LY_SUCCESS, ly_out_new_memory(&printed, 0, &out)); |
| 262 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx)); |
| 263 | |
| 264 | ly_ctx_set_module_imp_clb(ctx, test_imp_clb, submod_yang); |
| 265 | assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, mod_yang, LYS_IN_YANG, &mod)); |
| 266 | assert_int_equal(LY_SUCCESS, lys_print_submodule(out, mod, mod->parsed->includes[0].submodule, LYS_OUT_YANG, 0, 0)); |
| 267 | assert_int_equal(strlen(submod_yang), ly_out_printed(out)); |
| 268 | assert_string_equal(printed, submod_yang); |
| 269 | |
| 270 | *state = NULL; |
| 271 | ly_out_free(out, NULL, 1); |
| 272 | ly_ctx_destroy(ctx, NULL); |
| 273 | } |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 274 | |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 275 | int |
| 276 | main(void) |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 277 | { |
| 278 | const struct CMUnitTest tests[] = { |
| 279 | cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown), |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 280 | cmocka_unit_test_setup_teardown(test_submodule, logger_setup, logger_teardown), |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 281 | }; |
| 282 | |
| 283 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 284 | } |