blob: 9a03332eeb56904effe9196278534241984e828f [file] [log] [blame]
Radek Krejci3a4889a2020-05-19 17:01:58 +02001/*
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 Krejci18abde42020-06-13 20:04:39 +020015#include "test_schema.h"
16
Radek Krejci3a4889a2020-05-19 17:01:58 +020017#include <stdarg.h>
18#include <stddef.h>
19#include <stdio.h>
20#include <setjmp.h>
21#include <cmocka.h>
22
Radek Krejcica376bd2020-06-11 16:04:06 +020023#include <string.h>
24
Radek Krejci18abde42020-06-13 20:04:39 +020025#include "log.h"
26#include "parser_schema.h"
Radek Krejci70593c12020-06-13 20:48:09 +020027#include "tests/config.h"
Radek Krejci18abde42020-06-13 20:04:39 +020028#include "tree_schema.h"
Radek Krejci3a4889a2020-05-19 17:01:58 +020029
Radek Krejci18abde42020-06-13 20:04:39 +020030#if ENABLE_LOGGER_CHECKING
Radek Krejci3a4889a2020-05-19 17:01:58 +020031
32#define BUFSIZE 1024
33char logbuf[BUFSIZE] = {0};
34int store = -1; /* negative for infinite logging, positive for limited logging */
35
Radek Krejci3a4889a2020-05-19 17:01:58 +020036static void
37logger(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
53static int
54logger_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
66static int
67logger_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
78void
79logbuf_clean(void)
80{
Radek Krejci18abde42020-06-13 20:04:39 +020081#if ENABLE_LOGGER_CHECKING
Radek Krejci3a4889a2020-05-19 17:01:58 +020082 logbuf[0] = '\0';
Radek Krejci18abde42020-06-13 20:04:39 +020083#endif
Radek Krejci3a4889a2020-05-19 17:01:58 +020084}
85
Radek Krejci18abde42020-06-13 20:04:39 +020086LY_ERR
87test_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 Krejci3a4889a2020-05-19 17:01:58 +020096
97/**
Radek Krejci18abde42020-06-13 20:04:39 +020098 * DECLARE OTHER SCHEMA TESTS
Radek Krejci3a4889a2020-05-19 17:01:58 +020099 */
Radek Krejci18abde42020-06-13 20:04:39 +0200100/* test_schema_common.c */
101void test_getnext(void **state);
102void test_date(void **state);
103void test_revisions(void **state);
104void test_typedef(void **state);
105
106/* test_schema_stmts.c */
107void test_identity(void **state);
108void test_feature(void **state);
Radek Krejci3a4889a2020-05-19 17:01:58 +0200109
110int main(void)
111{
112 const struct CMUnitTest tests[] = {
113 /** test_schema_common.c */
114 cmocka_unit_test_setup_teardown(test_getnext, logger_setup, logger_teardown),
115 cmocka_unit_test_setup_teardown(test_date, logger_setup, logger_teardown),
116 cmocka_unit_test_setup_teardown(test_revisions, logger_setup, logger_teardown),
117 cmocka_unit_test_setup_teardown(test_typedef, logger_setup, logger_teardown),
118
119 /** test_schema_stmts.c */
120 cmocka_unit_test_setup_teardown(test_identity, logger_setup, logger_teardown),
121 cmocka_unit_test_setup_teardown(test_feature, logger_setup, logger_teardown),
122 };
123
124 return cmocka_run_group_tests(tests, NULL, NULL);
125}