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 | * |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 6 | * Copyright (c) 2019-2020 CESNET, z.s.p.o. |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 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 | */ |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 14 | #define _UTEST_MAIN_ |
| 15 | #include "utests.h" |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 16 | |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 17 | #include "common.h" |
Radek Krejci | 70593c1 | 2020-06-13 20:48:09 +0200 | [diff] [blame] | 18 | #include "context.h" |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 19 | #include "out.h" |
Radek Krejci | 70593c1 | 2020-06-13 20:48:09 +0200 | [diff] [blame] | 20 | #include "printer_schema.h" |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 21 | #include "tree_schema.h" |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 22 | |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 23 | static void |
| 24 | test_module(void **state) |
| 25 | { |
Michal Vasko | 4de7d07 | 2021-07-09 09:13:18 +0200 | [diff] [blame^] | 26 | struct lys_module *mod; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 27 | const char *orig = "module a {\n" |
| 28 | " yang-version 1.1;\n" |
| 29 | " namespace \"urn:test:a\";\n" |
| 30 | " prefix a;\n\n" |
| 31 | " import ietf-yang-types {\n" |
| 32 | " prefix yt;\n" |
| 33 | " revision-date 2013-07-15;\n" |
| 34 | " description\n" |
| 35 | " \"YANG types\";\n" |
| 36 | " reference\n" |
| 37 | " \"RFC reference\";\n" |
| 38 | " }\n\n" |
| 39 | " organization\n" |
| 40 | " \"ORG\";\n" |
| 41 | " contact\n" |
| 42 | " \"Radek Krejci.\";\n" |
| 43 | " description\n" |
| 44 | " \"Long multiline\n" |
| 45 | " description.\";\n" |
| 46 | " reference\n" |
| 47 | " \"some reference\";\n" |
| 48 | "}\n"; |
Radek Krejci | ffb410f | 2019-04-30 09:51:51 +0200 | [diff] [blame] | 49 | char *compiled = "module a {\n" |
Radek Krejci | ffb410f | 2019-04-30 09:51:51 +0200 | [diff] [blame] | 50 | " namespace \"urn:test:a\";\n" |
| 51 | " prefix a;\n\n" |
Radek Krejci | ffb410f | 2019-04-30 09:51:51 +0200 | [diff] [blame] | 52 | " organization\n" |
| 53 | " \"ORG\";\n" |
| 54 | " contact\n" |
| 55 | " \"Radek Krejci.\";\n" |
| 56 | " description\n" |
| 57 | " \"Long multiline\n" |
| 58 | " description.\";\n" |
| 59 | " reference\n" |
| 60 | " \"some reference\";\n" |
| 61 | "}\n"; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 62 | char *printed; |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 63 | struct ly_out *out; |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 64 | |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 65 | assert_int_equal(LY_SUCCESS, ly_out_new_memory(&printed, 0, &out)); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 66 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 67 | UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 68 | 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] | 69 | assert_int_equal(strlen(orig), ly_out_printed(out)); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 70 | assert_string_equal(printed, orig); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 71 | ly_out_reset(out); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 72 | 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] | 73 | assert_int_equal(strlen(compiled), ly_out_printed(out)); |
Radek Krejci | ffb410f | 2019-04-30 09:51:51 +0200 | [diff] [blame] | 74 | assert_string_equal(printed, compiled); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 75 | ly_out_reset(out); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 76 | |
| 77 | orig = "module b {\n" |
| 78 | " yang-version 1.1;\n" |
| 79 | " namespace \"urn:test:b\";\n" |
| 80 | " prefix b;\n\n" |
| 81 | " revision 2019-04-16 {\n" |
| 82 | " description\n" |
| 83 | " \"text\";\n" |
| 84 | " reference\n" |
| 85 | " \"text\";\n" |
| 86 | " }\n" |
| 87 | " revision 2019-04-15 {\n" |
| 88 | " description\n" |
| 89 | " \"initial revision\";\n" |
| 90 | " }\n\n" |
| 91 | " feature f1 {\n" |
| 92 | " status current;\n" |
| 93 | " description\n" |
| 94 | " \"text\";\n" |
| 95 | " reference\n" |
| 96 | " \"text\";\n" |
| 97 | " }\n\n" |
| 98 | " feature f2 {\n" |
| 99 | " if-feature \"not f1\";\n" |
| 100 | " }\n" |
| 101 | "}\n"; |
Radek Krejci | ffb410f | 2019-04-30 09:51:51 +0200 | [diff] [blame] | 102 | compiled = "module b {\n" |
Radek Krejci | ffb410f | 2019-04-30 09:51:51 +0200 | [diff] [blame] | 103 | " namespace \"urn:test:b\";\n" |
| 104 | " prefix b;\n\n" |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 105 | " revision 2019-04-16;\n" |
Radek Krejci | ffb410f | 2019-04-30 09:51:51 +0200 | [diff] [blame] | 106 | "}\n"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 107 | UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 108 | 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] | 109 | assert_int_equal(strlen(orig), ly_out_printed(out)); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 110 | assert_string_equal(printed, orig); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 111 | ly_out_reset(out); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 112 | 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] | 113 | assert_int_equal(strlen(compiled), ly_out_printed(out)); |
Radek Krejci | ffb410f | 2019-04-30 09:51:51 +0200 | [diff] [blame] | 114 | assert_string_equal(printed, compiled); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 115 | ly_out_reset(out); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 116 | |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 117 | orig = "module c {\n" |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 118 | " yang-version 1.1;\n" |
| 119 | " namespace \"urn:test:c\";\n" |
| 120 | " prefix c;\n\n" |
| 121 | " feature f1;\n\n" |
| 122 | " identity i1 {\n" |
| 123 | " if-feature \"f1\";\n" |
| 124 | " description\n" |
| 125 | " \"text\";\n" |
| 126 | " reference\n" |
| 127 | " \"text32\";\n" |
| 128 | " }\n\n" |
| 129 | " identity i2 {\n" |
| 130 | " base i1;\n" |
| 131 | " status obsolete;\n" |
| 132 | " }\n" |
| 133 | "}\n"; |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 134 | compiled = "module c {\n" |
| 135 | " namespace \"urn:test:c\";\n" |
| 136 | " prefix c;\n" |
| 137 | "}\n"; |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 138 | UTEST_ADD_MODULE(orig, LYS_IN_YANG, NULL, &mod); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 139 | 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] | 140 | assert_int_equal(strlen(orig), ly_out_printed(out)); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 141 | assert_string_equal(printed, orig); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 142 | ly_out_reset(out); |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 143 | 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] | 144 | assert_int_equal(strlen(compiled), ly_out_printed(out)); |
Radek Krejci | ffb410f | 2019-04-30 09:51:51 +0200 | [diff] [blame] | 145 | assert_string_equal(printed, compiled); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 146 | |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 147 | ly_out_free(out, NULL, 1); |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 148 | } |
| 149 | |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 150 | static LY_ERR |
| 151 | test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name), |
| 152 | const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format, |
| 153 | const char **module_data, void (**free_module_data)(void *model_data, void *user_data)) |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 154 | { |
| 155 | *module_data = user_data; |
| 156 | *format = LYS_IN_YANG; |
| 157 | *free_module_data = NULL; |
| 158 | return LY_SUCCESS; |
| 159 | } |
| 160 | |
| 161 | static void |
| 162 | test_submodule(void **state) |
| 163 | { |
Michal Vasko | 4de7d07 | 2021-07-09 09:13:18 +0200 | [diff] [blame^] | 164 | struct lys_module *mod; |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 165 | const char *mod_yang = "module a {\n" |
| 166 | " yang-version 1.1;\n" |
| 167 | " namespace \"urn:test:a\";\n" |
| 168 | " prefix a;\n\n" |
| 169 | " include a-sub;\n" |
| 170 | "}\n"; |
| 171 | char *submod_yang = "submodule a-sub {\n" |
| 172 | " yang-version 1.1;\n" |
| 173 | " belongs-to a {\n" |
| 174 | " prefix a;\n" |
| 175 | " }\n\n" |
| 176 | " import ietf-yang-types {\n" |
| 177 | " prefix yt;\n" |
| 178 | " revision-date 2013-07-15;\n" |
| 179 | " }\n\n" |
| 180 | " organization\n" |
| 181 | " \"ORG\";\n" |
| 182 | " contact\n" |
| 183 | " \"Radek Krejci.\";\n" |
| 184 | " description\n" |
| 185 | " \"Long multiline\n" |
| 186 | " description.\";\n" |
| 187 | " reference\n" |
| 188 | " \"some reference\";\n" |
| 189 | "}\n"; |
| 190 | char *printed; |
| 191 | struct ly_out *out; |
| 192 | |
| 193 | assert_int_equal(LY_SUCCESS, ly_out_new_memory(&printed, 0, &out)); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 194 | |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 195 | ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb, submod_yang); |
| 196 | UTEST_ADD_MODULE(mod_yang, LYS_IN_YANG, NULL, &mod); |
Michal Vasko | 7997d5a | 2021-02-22 10:55:56 +0100 | [diff] [blame] | 197 | assert_int_equal(LY_SUCCESS, lys_print_submodule(out, mod->parsed->includes[0].submodule, LYS_OUT_YANG, 0, 0)); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 198 | assert_int_equal(strlen(submod_yang), ly_out_printed(out)); |
| 199 | assert_string_equal(printed, submod_yang); |
| 200 | |
| 201 | *state = NULL; |
| 202 | ly_out_free(out, NULL, 1); |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 203 | } |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 204 | |
Radek Krejci | b4ac5a9 | 2020-11-23 17:54:33 +0100 | [diff] [blame] | 205 | int |
| 206 | main(void) |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 207 | { |
| 208 | const struct CMUnitTest tests[] = { |
Radek Iša | 56ca9e4 | 2020-09-08 18:42:00 +0200 | [diff] [blame] | 209 | UTEST(test_module), |
| 210 | UTEST(test_submodule), |
Radek Krejci | d3ca063 | 2019-04-16 16:54:54 +0200 | [diff] [blame] | 211 | }; |
| 212 | |
| 213 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 214 | } |