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; |
| 134 | |
| 135 | assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx)); |
| 136 | |
| 137 | assert_non_null(mod = lys_parse_mem(ctx, orig, LYS_IN_YANG)); |
Radek Krejci | 897ad2e | 2019-04-29 16:43:07 +0200 | [diff] [blame] | 138 | assert_int_equal(strlen(orig), lys_print_mem(&printed, mod, LYS_OUT_YANG, 0, 0)); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 139 | assert_string_equal(printed, orig); |
| 140 | free(printed); |
Radek Krejci | ffb410f | 2019-04-30 09:51:51 +0200 | [diff] [blame] | 141 | assert_int_equal(strlen(compiled), lys_print_mem(&printed, mod, LYS_OUT_YANG_COMPILED, 0, 0)); |
| 142 | assert_string_equal(printed, compiled); |
| 143 | free(printed); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 144 | |
| 145 | orig = "module b {\n" |
| 146 | " yang-version 1.1;\n" |
| 147 | " namespace \"urn:test:b\";\n" |
| 148 | " prefix b;\n\n" |
| 149 | " revision 2019-04-16 {\n" |
| 150 | " description\n" |
| 151 | " \"text\";\n" |
| 152 | " reference\n" |
| 153 | " \"text\";\n" |
| 154 | " }\n" |
| 155 | " revision 2019-04-15 {\n" |
| 156 | " description\n" |
| 157 | " \"initial revision\";\n" |
| 158 | " }\n\n" |
| 159 | " feature f1 {\n" |
| 160 | " status current;\n" |
| 161 | " description\n" |
| 162 | " \"text\";\n" |
| 163 | " reference\n" |
| 164 | " \"text\";\n" |
| 165 | " }\n\n" |
| 166 | " feature f2 {\n" |
| 167 | " if-feature \"not f1\";\n" |
| 168 | " }\n" |
| 169 | "}\n"; |
Radek Krejci | ffb410f | 2019-04-30 09:51:51 +0200 | [diff] [blame] | 170 | compiled = "module b {\n" |
| 171 | " yang-version 1.1;\n" |
| 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"; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 186 | assert_non_null(mod = lys_parse_mem(ctx, orig, LYS_IN_YANG)); |
Radek Krejci | 897ad2e | 2019-04-29 16:43:07 +0200 | [diff] [blame] | 187 | assert_int_equal(strlen(orig), lys_print_mem(&printed, mod, LYS_OUT_YANG, 0, 0)); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 188 | assert_string_equal(printed, orig); |
| 189 | free(printed); |
Radek Krejci | ffb410f | 2019-04-30 09:51:51 +0200 | [diff] [blame] | 190 | assert_int_equal(strlen(compiled), lys_print_mem(&printed, mod, LYS_OUT_YANG_COMPILED, 0, 0)); |
| 191 | assert_string_equal(printed, compiled); |
| 192 | free(printed); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 193 | |
Radek Krejci | ffb410f | 2019-04-30 09:51:51 +0200 | [diff] [blame] | 194 | orig = compiled ="module c {\n" |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 195 | " yang-version 1.1;\n" |
| 196 | " namespace \"urn:test:c\";\n" |
| 197 | " prefix c;\n\n" |
| 198 | " feature f1;\n\n" |
| 199 | " identity i1 {\n" |
| 200 | " if-feature \"f1\";\n" |
| 201 | " description\n" |
| 202 | " \"text\";\n" |
| 203 | " reference\n" |
| 204 | " \"text32\";\n" |
| 205 | " }\n\n" |
| 206 | " identity i2 {\n" |
| 207 | " base i1;\n" |
| 208 | " status obsolete;\n" |
| 209 | " }\n" |
| 210 | "}\n"; |
| 211 | assert_non_null(mod = lys_parse_mem(ctx, orig, LYS_IN_YANG)); |
Radek Krejci | 897ad2e | 2019-04-29 16:43:07 +0200 | [diff] [blame] | 212 | assert_int_equal(strlen(orig), lys_print_mem(&printed, mod, LYS_OUT_YANG, 0, 0)); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 213 | assert_string_equal(printed, orig); |
| 214 | free(printed); |
Radek Krejci | ffb410f | 2019-04-30 09:51:51 +0200 | [diff] [blame] | 215 | assert_int_equal(strlen(compiled), lys_print_mem(&printed, mod, LYS_OUT_YANG, 0, 0)); |
| 216 | assert_string_equal(printed, compiled); |
| 217 | free(printed); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 218 | |
| 219 | *state = NULL; |
| 220 | ly_ctx_destroy(ctx, NULL); |
| 221 | } |
| 222 | |
| 223 | /* TODO: include */ |
| 224 | |
| 225 | int main(void) |
| 226 | { |
| 227 | const struct CMUnitTest tests[] = { |
| 228 | cmocka_unit_test_setup_teardown(test_module, logger_setup, logger_teardown), |
| 229 | }; |
| 230 | |
| 231 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 232 | } |