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" |
Radek Krejci | ffb410f | 2019-04-30 09:51:51 +0200 | [diff] [blame] | 118 | " namespace \"urn:test:a\";\n" |
| 119 | " prefix a;\n\n" |
Radek Krejci | ffb410f | 2019-04-30 09:51:51 +0200 | [diff] [blame] | 120 | " organization\n" |
| 121 | " \"ORG\";\n" |
| 122 | " contact\n" |
| 123 | " \"Radek Krejci.\";\n" |
| 124 | " description\n" |
| 125 | " \"Long multiline\n" |
| 126 | " description.\";\n" |
| 127 | " reference\n" |
| 128 | " \"some reference\";\n" |
| 129 | "}\n"; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 130 | char *printed; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 131 | struct ly_out *out; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 132 | |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 133 | assert_int_equal(LY_SUCCESS, ly_out_new_memory(&printed, 0, &out)); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 134 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx)); |
| 135 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 136 | 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] | 137 | 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] | 138 | assert_int_equal(strlen(orig), ly_out_printed(out)); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 139 | assert_string_equal(printed, orig); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 140 | ly_out_reset(out); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 141 | 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] | 142 | assert_int_equal(strlen(compiled), ly_out_printed(out)); |
Radek Krejci | ffb410f | 2019-04-30 09:51:51 +0200 | [diff] [blame] | 143 | assert_string_equal(printed, compiled); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 144 | ly_out_reset(out); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 145 | |
| 146 | orig = "module b {\n" |
| 147 | " yang-version 1.1;\n" |
| 148 | " namespace \"urn:test:b\";\n" |
| 149 | " prefix b;\n\n" |
| 150 | " revision 2019-04-16 {\n" |
| 151 | " description\n" |
| 152 | " \"text\";\n" |
| 153 | " reference\n" |
| 154 | " \"text\";\n" |
| 155 | " }\n" |
| 156 | " revision 2019-04-15 {\n" |
| 157 | " description\n" |
| 158 | " \"initial revision\";\n" |
| 159 | " }\n\n" |
| 160 | " feature f1 {\n" |
| 161 | " status current;\n" |
| 162 | " description\n" |
| 163 | " \"text\";\n" |
| 164 | " reference\n" |
| 165 | " \"text\";\n" |
| 166 | " }\n\n" |
| 167 | " feature f2 {\n" |
| 168 | " if-feature \"not f1\";\n" |
| 169 | " }\n" |
| 170 | "}\n"; |
Radek Krejci | ffb410f | 2019-04-30 09:51:51 +0200 | [diff] [blame] | 171 | compiled = "module b {\n" |
Radek Krejci | ffb410f | 2019-04-30 09:51:51 +0200 | [diff] [blame] | 172 | " namespace \"urn:test:b\";\n" |
| 173 | " prefix b;\n\n" |
| 174 | " revision 2019-04-16;\n\n" |
| 175 | " feature f1 {\n" |
| 176 | " status current;\n" |
| 177 | " description\n" |
| 178 | " \"text\";\n" |
| 179 | " reference\n" |
| 180 | " \"text\";\n" |
| 181 | " }\n\n" |
| 182 | " feature f2 {\n" |
| 183 | " if-feature \"not f1\";\n" |
| 184 | " }\n" |
| 185 | "}\n"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 186 | 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] | 187 | 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] | 188 | assert_int_equal(strlen(orig), ly_out_printed(out)); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 189 | assert_string_equal(printed, orig); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 190 | ly_out_reset(out); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 191 | 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] | 192 | assert_int_equal(strlen(compiled), ly_out_printed(out)); |
Radek Krejci | ffb410f | 2019-04-30 09:51:51 +0200 | [diff] [blame] | 193 | assert_string_equal(printed, compiled); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 194 | ly_out_reset(out); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 195 | |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 196 | orig = compiled = "module c {\n" |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 197 | " yang-version 1.1;\n" |
| 198 | " namespace \"urn:test:c\";\n" |
| 199 | " prefix c;\n\n" |
| 200 | " feature f1;\n\n" |
| 201 | " identity i1 {\n" |
| 202 | " if-feature \"f1\";\n" |
| 203 | " description\n" |
| 204 | " \"text\";\n" |
| 205 | " reference\n" |
| 206 | " \"text32\";\n" |
| 207 | " }\n\n" |
| 208 | " identity i2 {\n" |
| 209 | " base i1;\n" |
| 210 | " status obsolete;\n" |
| 211 | " }\n" |
| 212 | "}\n"; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 213 | 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] | 214 | 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] | 215 | assert_int_equal(strlen(orig), ly_out_printed(out)); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 216 | assert_string_equal(printed, orig); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 217 | ly_out_reset(out); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 218 | 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] | 219 | assert_int_equal(strlen(compiled), ly_out_printed(out)); |
Radek Krejci | ffb410f | 2019-04-30 09:51:51 +0200 | [diff] [blame] | 220 | assert_string_equal(printed, compiled); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 221 | |
| 222 | *state = NULL; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 223 | ly_out_free(out, NULL, 1); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 224 | ly_ctx_destroy(ctx, NULL); |
| 225 | } |
| 226 | |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 227 | static LY_ERR test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name), |
| 228 | const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format, |
| 229 | const char **module_data, void (**free_module_data)(void *model_data, void *user_data)) |
| 230 | { |
| 231 | *module_data = user_data; |
| 232 | *format = LYS_IN_YANG; |
| 233 | *free_module_data = NULL; |
| 234 | return LY_SUCCESS; |
| 235 | } |
| 236 | |
| 237 | static void |
| 238 | test_submodule(void **state) |
| 239 | { |
| 240 | *state = test_submodule; |
| 241 | |
| 242 | struct ly_ctx *ctx = {0}; |
| 243 | const struct lys_module *mod; |
| 244 | const char *mod_yang = "module a {\n" |
| 245 | " yang-version 1.1;\n" |
| 246 | " namespace \"urn:test:a\";\n" |
| 247 | " prefix a;\n\n" |
| 248 | " include a-sub;\n" |
| 249 | "}\n"; |
| 250 | char *submod_yang = "submodule a-sub {\n" |
| 251 | " yang-version 1.1;\n" |
| 252 | " belongs-to a {\n" |
| 253 | " prefix a;\n" |
| 254 | " }\n\n" |
| 255 | " import ietf-yang-types {\n" |
| 256 | " prefix yt;\n" |
| 257 | " revision-date 2013-07-15;\n" |
| 258 | " }\n\n" |
| 259 | " organization\n" |
| 260 | " \"ORG\";\n" |
| 261 | " contact\n" |
| 262 | " \"Radek Krejci.\";\n" |
| 263 | " description\n" |
| 264 | " \"Long multiline\n" |
| 265 | " description.\";\n" |
| 266 | " reference\n" |
| 267 | " \"some reference\";\n" |
| 268 | "}\n"; |
| 269 | char *printed; |
| 270 | struct ly_out *out; |
| 271 | |
| 272 | assert_int_equal(LY_SUCCESS, ly_out_new_memory(&printed, 0, &out)); |
| 273 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx)); |
| 274 | |
| 275 | ly_ctx_set_module_imp_clb(ctx, test_imp_clb, submod_yang); |
| 276 | assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, mod_yang, LYS_IN_YANG, &mod)); |
| 277 | assert_int_equal(LY_SUCCESS, lys_print_submodule(out, mod, mod->parsed->includes[0].submodule, LYS_OUT_YANG, 0, 0)); |
| 278 | assert_int_equal(strlen(submod_yang), ly_out_printed(out)); |
| 279 | assert_string_equal(printed, submod_yang); |
| 280 | |
| 281 | *state = NULL; |
| 282 | ly_out_free(out, NULL, 1); |
| 283 | ly_ctx_destroy(ctx, NULL); |
| 284 | } |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 285 | |
| 286 | int main(void) |
| 287 | { |
| 288 | const struct CMUnitTest tests[] = { |
| 289 | cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown), |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 290 | cmocka_unit_test_setup_teardown(test_submodule, logger_setup, logger_teardown), |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 291 | }; |
| 292 | |
| 293 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 294 | } |