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