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 | |
Radek Krejci | 70593c1 | 2020-06-13 20:48:09 +0200 | [diff] [blame] | 23 | #include "context.h" |
| 24 | #include "printer.h" |
| 25 | #include "printer_schema.h" |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 26 | |
| 27 | #define BUFSIZE 1024 |
| 28 | char logbuf[BUFSIZE] = {0}; |
| 29 | int store = -1; /* negative for infinite logging, positive for limited logging */ |
| 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 | if (store) { |
| 40 | if (path && path[0]) { |
| 41 | snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path); |
| 42 | } else { |
| 43 | strncpy(logbuf, msg, BUFSIZE - 1); |
| 44 | } |
| 45 | if (store > 0) { |
| 46 | --store; |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | #endif |
| 51 | |
| 52 | static int |
| 53 | logger_setup(void **state) |
| 54 | { |
| 55 | (void) state; /* unused */ |
| 56 | #if ENABLE_LOGGER_CHECKING |
| 57 | ly_set_log_clb(logger, 1); |
| 58 | #endif |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | static int |
| 63 | logger_teardown(void **state) |
| 64 | { |
| 65 | (void) state; /* unused */ |
| 66 | #if ENABLE_LOGGER_CHECKING |
| 67 | if (*state) { |
| 68 | fprintf(stderr, "%s\n", logbuf); |
| 69 | } |
| 70 | #endif |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | void |
| 75 | logbuf_clean(void) |
| 76 | { |
| 77 | logbuf[0] = '\0'; |
| 78 | } |
| 79 | |
| 80 | #if ENABLE_LOGGER_CHECKING |
| 81 | # define logbuf_assert(str) assert_string_equal(logbuf, str) |
| 82 | #else |
| 83 | # define logbuf_assert(str) |
| 84 | #endif |
| 85 | |
| 86 | |
| 87 | static void |
| 88 | test_module(void **state) |
| 89 | { |
| 90 | *state = test_module; |
| 91 | |
| 92 | struct ly_ctx *ctx = {0}; |
| 93 | const struct lys_module *mod; |
| 94 | const char *orig = "module a {\n" |
| 95 | " yang-version 1.1;\n" |
| 96 | " namespace \"urn:test:a\";\n" |
| 97 | " prefix a;\n\n" |
| 98 | " import ietf-yang-types {\n" |
| 99 | " prefix yt;\n" |
| 100 | " revision-date 2013-07-15;\n" |
| 101 | " description\n" |
| 102 | " \"YANG types\";\n" |
| 103 | " reference\n" |
| 104 | " \"RFC reference\";\n" |
| 105 | " }\n\n" |
| 106 | " organization\n" |
| 107 | " \"ORG\";\n" |
| 108 | " contact\n" |
| 109 | " \"Radek Krejci.\";\n" |
| 110 | " description\n" |
| 111 | " \"Long multiline\n" |
| 112 | " description.\";\n" |
| 113 | " reference\n" |
| 114 | " \"some reference\";\n" |
| 115 | "}\n"; |
Radek Krejci | ffb410f | 2019-04-30 09:51:51 +0200 | [diff] [blame] | 116 | char *compiled = "module a {\n" |
| 117 | " yang-version 1.1;\n" |
| 118 | " namespace \"urn:test:a\";\n" |
| 119 | " prefix a;\n\n" |
| 120 | " import ietf-yang-types {\n" |
| 121 | " prefix yt;\n" |
| 122 | " revision-date 2013-07-15;\n" |
| 123 | " }\n\n" |
| 124 | " organization\n" |
| 125 | " \"ORG\";\n" |
| 126 | " contact\n" |
| 127 | " \"Radek Krejci.\";\n" |
| 128 | " description\n" |
| 129 | " \"Long multiline\n" |
| 130 | " description.\";\n" |
| 131 | " reference\n" |
| 132 | " \"some reference\";\n" |
| 133 | "}\n"; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 134 | char *printed; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 135 | struct ly_out *out; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 136 | |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 137 | assert_int_equal(LY_SUCCESS, ly_out_new_memory(&printed, 0, &out)); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 138 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx)); |
| 139 | |
| 140 | assert_non_null(mod = lys_parse_mem(ctx, orig, LYS_IN_YANG)); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame^] | 141 | assert_int_equal(LY_SUCCESS, lys_print(out, mod, LYS_OUT_YANG, 0, 0)); |
| 142 | assert_int_equal(strlen(orig), ly_out_printed(out)); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 143 | assert_string_equal(printed, orig); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 144 | ly_out_reset(out); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame^] | 145 | assert_int_equal(LY_SUCCESS, lys_print(out, mod, LYS_OUT_YANG_COMPILED, 0, 0)); |
| 146 | assert_int_equal(strlen(compiled), ly_out_printed(out)); |
Radek Krejci | ffb410f | 2019-04-30 09:51:51 +0200 | [diff] [blame] | 147 | assert_string_equal(printed, compiled); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 148 | ly_out_reset(out); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 149 | |
| 150 | orig = "module b {\n" |
| 151 | " yang-version 1.1;\n" |
| 152 | " namespace \"urn:test:b\";\n" |
| 153 | " prefix b;\n\n" |
| 154 | " revision 2019-04-16 {\n" |
| 155 | " description\n" |
| 156 | " \"text\";\n" |
| 157 | " reference\n" |
| 158 | " \"text\";\n" |
| 159 | " }\n" |
| 160 | " revision 2019-04-15 {\n" |
| 161 | " description\n" |
| 162 | " \"initial revision\";\n" |
| 163 | " }\n\n" |
| 164 | " feature f1 {\n" |
| 165 | " status current;\n" |
| 166 | " description\n" |
| 167 | " \"text\";\n" |
| 168 | " reference\n" |
| 169 | " \"text\";\n" |
| 170 | " }\n\n" |
| 171 | " feature f2 {\n" |
| 172 | " if-feature \"not f1\";\n" |
| 173 | " }\n" |
| 174 | "}\n"; |
Radek Krejci | ffb410f | 2019-04-30 09:51:51 +0200 | [diff] [blame] | 175 | compiled = "module b {\n" |
| 176 | " yang-version 1.1;\n" |
| 177 | " namespace \"urn:test:b\";\n" |
| 178 | " prefix b;\n\n" |
| 179 | " revision 2019-04-16;\n\n" |
| 180 | " feature f1 {\n" |
| 181 | " status current;\n" |
| 182 | " description\n" |
| 183 | " \"text\";\n" |
| 184 | " reference\n" |
| 185 | " \"text\";\n" |
| 186 | " }\n\n" |
| 187 | " feature f2 {\n" |
| 188 | " if-feature \"not f1\";\n" |
| 189 | " }\n" |
| 190 | "}\n"; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 191 | assert_non_null(mod = lys_parse_mem(ctx, orig, LYS_IN_YANG)); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame^] | 192 | assert_int_equal(LY_SUCCESS, lys_print(out, mod, LYS_OUT_YANG, 0, 0)); |
| 193 | assert_int_equal(strlen(orig), ly_out_printed(out)); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 194 | assert_string_equal(printed, orig); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 195 | ly_out_reset(out); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame^] | 196 | assert_int_equal(LY_SUCCESS, lys_print(out, mod, LYS_OUT_YANG_COMPILED, 0, 0)); |
| 197 | assert_int_equal(strlen(compiled), ly_out_printed(out)); |
Radek Krejci | ffb410f | 2019-04-30 09:51:51 +0200 | [diff] [blame] | 198 | assert_string_equal(printed, compiled); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 199 | ly_out_reset(out); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 200 | |
Radek Krejci | ffb410f | 2019-04-30 09:51:51 +0200 | [diff] [blame] | 201 | orig = compiled ="module c {\n" |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 202 | " yang-version 1.1;\n" |
| 203 | " namespace \"urn:test:c\";\n" |
| 204 | " prefix c;\n\n" |
| 205 | " feature f1;\n\n" |
| 206 | " identity i1 {\n" |
| 207 | " if-feature \"f1\";\n" |
| 208 | " description\n" |
| 209 | " \"text\";\n" |
| 210 | " reference\n" |
| 211 | " \"text32\";\n" |
| 212 | " }\n\n" |
| 213 | " identity i2 {\n" |
| 214 | " base i1;\n" |
| 215 | " status obsolete;\n" |
| 216 | " }\n" |
| 217 | "}\n"; |
| 218 | assert_non_null(mod = lys_parse_mem(ctx, orig, LYS_IN_YANG)); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame^] | 219 | assert_int_equal(LY_SUCCESS, lys_print(out, mod, LYS_OUT_YANG, 0, 0)); |
| 220 | assert_int_equal(strlen(orig), ly_out_printed(out)); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 221 | assert_string_equal(printed, orig); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 222 | ly_out_reset(out); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame^] | 223 | assert_int_equal(LY_SUCCESS, lys_print(out, mod, LYS_OUT_YANG, 0, 0)); |
| 224 | assert_int_equal(strlen(compiled), ly_out_printed(out)); |
Radek Krejci | ffb410f | 2019-04-30 09:51:51 +0200 | [diff] [blame] | 225 | assert_string_equal(printed, compiled); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 226 | /* missing free(printed); which is done in the following lyp_free() */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 227 | |
| 228 | *state = NULL; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 229 | ly_out_free(out, NULL, 1); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 230 | ly_ctx_destroy(ctx, NULL); |
| 231 | } |
| 232 | |
| 233 | /* TODO: include */ |
| 234 | |
| 235 | int main(void) |
| 236 | { |
| 237 | const struct CMUnitTest tests[] = { |
| 238 | cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown), |
| 239 | }; |
| 240 | |
| 241 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 242 | } |