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 | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 136 | size_t size = 0; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 137 | |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 138 | assert_int_equal(LY_SUCCESS, ly_out_new_memory(&printed, 0, &out)); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 139 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx)); |
| 140 | |
| 141 | assert_non_null(mod = lys_parse_mem(ctx, orig, LYS_IN_YANG)); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 142 | 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] | 143 | assert_string_equal(printed, orig); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 144 | ly_out_reset(out); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 145 | 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] | 146 | assert_string_equal(printed, compiled); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 147 | ly_out_reset(out); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 148 | |
| 149 | orig = "module b {\n" |
| 150 | " yang-version 1.1;\n" |
| 151 | " namespace \"urn:test:b\";\n" |
| 152 | " prefix b;\n\n" |
| 153 | " revision 2019-04-16 {\n" |
| 154 | " description\n" |
| 155 | " \"text\";\n" |
| 156 | " reference\n" |
| 157 | " \"text\";\n" |
| 158 | " }\n" |
| 159 | " revision 2019-04-15 {\n" |
| 160 | " description\n" |
| 161 | " \"initial revision\";\n" |
| 162 | " }\n\n" |
| 163 | " feature f1 {\n" |
| 164 | " status current;\n" |
| 165 | " description\n" |
| 166 | " \"text\";\n" |
| 167 | " reference\n" |
| 168 | " \"text\";\n" |
| 169 | " }\n\n" |
| 170 | " feature f2 {\n" |
| 171 | " if-feature \"not f1\";\n" |
| 172 | " }\n" |
| 173 | "}\n"; |
Radek Krejci | ffb410f | 2019-04-30 09:51:51 +0200 | [diff] [blame] | 174 | compiled = "module b {\n" |
| 175 | " yang-version 1.1;\n" |
| 176 | " namespace \"urn:test:b\";\n" |
| 177 | " prefix b;\n\n" |
| 178 | " revision 2019-04-16;\n\n" |
| 179 | " feature f1 {\n" |
| 180 | " status current;\n" |
| 181 | " description\n" |
| 182 | " \"text\";\n" |
| 183 | " reference\n" |
| 184 | " \"text\";\n" |
| 185 | " }\n\n" |
| 186 | " feature f2 {\n" |
| 187 | " if-feature \"not f1\";\n" |
| 188 | " }\n" |
| 189 | "}\n"; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 190 | assert_non_null(mod = lys_parse_mem(ctx, orig, LYS_IN_YANG)); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 191 | 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] | 192 | assert_string_equal(printed, orig); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 193 | ly_out_reset(out); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 194 | 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] | 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 | |
Radek Krejci | ffb410f | 2019-04-30 09:51:51 +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"; |
| 215 | assert_non_null(mod = lys_parse_mem(ctx, orig, LYS_IN_YANG)); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 216 | 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] | 217 | assert_string_equal(printed, orig); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 218 | ly_out_reset(out); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 219 | 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] | 220 | assert_string_equal(printed, compiled); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 221 | /* missing free(printed); which is done in the following lyp_free() */ |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 222 | |
| 223 | *state = NULL; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 224 | ly_out_free(out, NULL, 1); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 225 | ly_ctx_destroy(ctx, NULL); |
| 226 | } |
| 227 | |
| 228 | /* TODO: include */ |
| 229 | |
| 230 | int main(void) |
| 231 | { |
| 232 | const struct CMUnitTest tests[] = { |
| 233 | cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown), |
| 234 | }; |
| 235 | |
| 236 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 237 | } |