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