]> git.ipfire.org Git - thirdparty/kmod.git/blob - testsuite/testsuite.h
testsuite: add README
[thirdparty/kmod.git] / testsuite / testsuite.h
1 /*
2 * Copyright (C) 2012 ProFUSION embedded systems
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #ifndef _LIBKMOD_TESTSUITE_
19 #define _LIBKMOD_TESTSUITE_
20
21 #include <stdbool.h>
22 #include <stdarg.h>
23
24 struct test;
25 typedef int (*testfunc)(const struct test *t);
26
27 enum test_config {
28 /*
29 * Where's the roots dir for this test. It will LD_PRELOAD path.so in
30 * order to trap calls to functions using paths.
31 */
32 TC_ROOTFS = 0,
33
34 /*
35 * What's the desired string to be returned by `uname -r`. It will
36 * trap calls to uname(3P) by LD_PRELOAD'ing uname.so and then filling
37 * in the information in u.release.
38 */
39 TC_UNAME_R,
40
41 /*
42 * Fake calls to init_module(2), returning return-code and setting
43 * errno to err-code. Set this variable with the following format:
44 *
45 * modname:return-code:err-code
46 *
47 * When this variable is used, all calls to init_module() are trapped
48 * and by default the return code is 0. In other words, they fake
49 * "success" for all modules, except the ones in the list above, for
50 * which the return codes are used.
51 */
52 TC_INIT_MODULE_RETCODES,
53
54 /*
55 * Fake calls to delete_module(2), returning return-code and setting
56 * errno to err-code. Set this variable with the following format:
57 *
58 * modname:return-code:err-code
59 *
60 * When this variable is used, all calls to init_module() are trapped
61 * and by default the return code is 0. In other words, they fake
62 * "success" for all modules, except the ones in the list above, for
63 * which the return codes are used.
64 */
65 TC_DELETE_MODULE_RETCODES,
66
67 _TC_LAST,
68 };
69
70 #define S_TC_ROOTFS "TESTSUITE_ROOTFS"
71 #define S_TC_UNAME_R "TESTSUITE_UNAME_R"
72 #define S_TC_INIT_MODULE_RETCODES "TESTSUITE_INIT_MODULE_RETCODES"
73 #define S_TC_DELETE_MODULE_RETCODES "TESTSUITE_DELETE_MODULE_RETCODES"
74
75
76 struct test {
77 const char *name;
78 const char *description;
79 struct {
80 const char *stdout;
81 const char *stderr;
82 } output;
83 testfunc func;
84 const char *config[_TC_LAST];
85 bool need_spawn;
86 bool expected_fail;
87 };
88
89
90 const struct test *test_find(const struct test *tests[], const char *name);
91 int test_init(int argc, char *const argv[], const struct test *tests[]);
92 int test_spawn_prog(const char *prog, const char *const args[]);
93
94 int test_run(const struct test *t);
95
96 #define TS_EXPORT __attribute__ ((visibility("default")))
97
98 #define _LOG(prefix, fmt, ...) printf("TESTSUITE: " prefix fmt, ## __VA_ARGS__)
99 #define LOG(fmt, ...) _LOG("", fmt, ## __VA_ARGS__)
100 #define WARN(fmt, ...) _LOG("WARN: ", fmt, ## __VA_ARGS__)
101 #define ERR(fmt, ...) _LOG("ERR: ", fmt, ## __VA_ARGS__)
102
103 /* Test definitions */
104 #define DEFINE_TEST(_name) \
105 struct test s_name = { \
106 .name = #_name, \
107 .func = _name, \
108 }
109
110 #endif