Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 1 | /* |
| 2 | * @file test_schema.c |
| 3 | * @author: Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief unit tests for schema related functions |
| 5 | * |
| 6 | * Copyright (c) 2018-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 | 18abde4 | 2020-06-13 20:04:39 +0200 | [diff] [blame] | 15 | #include "test_schema.h" |
| 16 | |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 17 | #include <stdarg.h> |
| 18 | #include <stddef.h> |
| 19 | #include <stdio.h> |
| 20 | #include <setjmp.h> |
| 21 | #include <cmocka.h> |
| 22 | |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 23 | #include <string.h> |
| 24 | |
Radek Krejci | 18abde4 | 2020-06-13 20:04:39 +0200 | [diff] [blame] | 25 | #include "log.h" |
| 26 | #include "parser_schema.h" |
Radek Krejci | 70593c1 | 2020-06-13 20:48:09 +0200 | [diff] [blame] | 27 | #include "tests/config.h" |
Radek Krejci | 18abde4 | 2020-06-13 20:04:39 +0200 | [diff] [blame] | 28 | #include "tree_schema.h" |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 29 | |
Radek Krejci | 18abde4 | 2020-06-13 20:04:39 +0200 | [diff] [blame] | 30 | #if ENABLE_LOGGER_CHECKING |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 31 | |
| 32 | #define BUFSIZE 1024 |
| 33 | char logbuf[BUFSIZE] = {0}; |
| 34 | int store = -1; /* negative for infinite logging, positive for limited logging */ |
| 35 | |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 36 | static void |
| 37 | logger(LY_LOG_LEVEL level, const char *msg, const char *path) |
| 38 | { |
| 39 | (void) level; /* unused */ |
| 40 | if (store) { |
| 41 | if (path && path[0]) { |
| 42 | snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path); |
| 43 | } else { |
| 44 | strncpy(logbuf, msg, BUFSIZE - 1); |
| 45 | } |
| 46 | if (store > 0) { |
| 47 | --store; |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | #endif |
| 52 | |
| 53 | static int |
| 54 | logger_setup(void **state) |
| 55 | { |
| 56 | (void) state; /* unused */ |
| 57 | |
| 58 | #if ENABLE_LOGGER_CHECKING |
| 59 | /* setup logger */ |
| 60 | ly_set_log_clb(logger, 1); |
| 61 | #endif |
| 62 | |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | static int |
| 67 | logger_teardown(void **state) |
| 68 | { |
| 69 | (void) state; /* unused */ |
| 70 | #if ENABLE_LOGGER_CHECKING |
| 71 | if (*state) { |
| 72 | fprintf(stderr, "%s\n", logbuf); |
| 73 | } |
| 74 | #endif |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | void |
| 79 | logbuf_clean(void) |
| 80 | { |
Radek Krejci | 18abde4 | 2020-06-13 20:04:39 +0200 | [diff] [blame] | 81 | #if ENABLE_LOGGER_CHECKING |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 82 | logbuf[0] = '\0'; |
Radek Krejci | 18abde4 | 2020-06-13 20:04:39 +0200 | [diff] [blame] | 83 | #endif |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 84 | } |
| 85 | |
Radek Krejci | 18abde4 | 2020-06-13 20:04:39 +0200 | [diff] [blame] | 86 | LY_ERR |
| 87 | test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name), |
| 88 | const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format, |
| 89 | const char **module_data, void (**free_module_data)(void *model_data, void *user_data)) |
| 90 | { |
| 91 | *module_data = user_data; |
| 92 | *format = LYS_IN_YANG; |
| 93 | *free_module_data = NULL; |
| 94 | return LY_SUCCESS; |
| 95 | } |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 96 | |
| 97 | /** |
Radek Krejci | 18abde4 | 2020-06-13 20:04:39 +0200 | [diff] [blame] | 98 | * DECLARE OTHER SCHEMA TESTS |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 99 | */ |
Radek Krejci | 18abde4 | 2020-06-13 20:04:39 +0200 | [diff] [blame] | 100 | /* test_schema_common.c */ |
| 101 | void test_getnext(void **state); |
| 102 | void test_date(void **state); |
| 103 | void test_revisions(void **state); |
| 104 | void test_typedef(void **state); |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame^] | 105 | void test_accessible_tree(void **state); |
Radek Krejci | 18abde4 | 2020-06-13 20:04:39 +0200 | [diff] [blame] | 106 | |
| 107 | /* test_schema_stmts.c */ |
| 108 | void test_identity(void **state); |
| 109 | void test_feature(void **state); |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 110 | |
| 111 | int main(void) |
| 112 | { |
| 113 | const struct CMUnitTest tests[] = { |
| 114 | /** test_schema_common.c */ |
| 115 | cmocka_unit_test_setup_teardown(test_getnext, logger_setup, logger_teardown), |
| 116 | cmocka_unit_test_setup_teardown(test_date, logger_setup, logger_teardown), |
| 117 | cmocka_unit_test_setup_teardown(test_revisions, logger_setup, logger_teardown), |
| 118 | cmocka_unit_test_setup_teardown(test_typedef, logger_setup, logger_teardown), |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame^] | 119 | cmocka_unit_test_setup_teardown(test_accessible_tree, logger_setup, logger_teardown), |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 120 | |
| 121 | /** test_schema_stmts.c */ |
| 122 | cmocka_unit_test_setup_teardown(test_identity, logger_setup, logger_teardown), |
| 123 | cmocka_unit_test_setup_teardown(test_feature, logger_setup, logger_teardown), |
| 124 | }; |
| 125 | |
| 126 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 127 | } |