]> git.ipfire.org Git - thirdparty/kmod.git/blame - testsuite/testsuite.h
Move static keyword to DEFINE_TEST macro
[thirdparty/kmod.git] / testsuite / testsuite.h
CommitLineData
e701e381 1/*
e6b0e49b 2 * Copyright (C) 2012-2013 ProFUSION embedded systems
e701e381 3 *
e1b1ab24
LDM
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
e701e381
LDM
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
e1b1ab24
LDM
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
e701e381 13 *
e1b1ab24
LDM
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
e701e381
LDM
17 */
18
e8fd8fec 19#pragma once
80f9e023
LDM
20
21#include <stdbool.h>
22#include <stdarg.h>
23
576dd439 24#include <shared/macro.h>
d96ca9c4 25
80f9e023
LDM
26struct test;
27typedef int (*testfunc)(const struct test *t);
28
29enum test_config {
23e354bf
LDM
30 /*
31 * Where's the roots dir for this test. It will LD_PRELOAD path.so in
32 * order to trap calls to functions using paths.
33 */
80f9e023 34 TC_ROOTFS = 0,
23e354bf
LDM
35
36 /*
37 * What's the desired string to be returned by `uname -r`. It will
38 * trap calls to uname(3P) by LD_PRELOAD'ing uname.so and then filling
39 * in the information in u.release.
40 */
80f9e023 41 TC_UNAME_R,
23e354bf
LDM
42
43 /*
44 * Fake calls to init_module(2), returning return-code and setting
45 * errno to err-code. Set this variable with the following format:
46 *
47 * modname:return-code:err-code
48 *
49 * When this variable is used, all calls to init_module() are trapped
50 * and by default the return code is 0. In other words, they fake
51 * "success" for all modules, except the ones in the list above, for
52 * which the return codes are used.
53 */
53646fc5 54 TC_INIT_MODULE_RETCODES,
23e354bf
LDM
55
56 /*
57 * Fake calls to delete_module(2), returning return-code and setting
58 * errno to err-code. Set this variable with the following format:
59 *
60 * modname:return-code:err-code
61 *
62 * When this variable is used, all calls to init_module() are trapped
63 * and by default the return code is 0. In other words, they fake
64 * "success" for all modules, except the ones in the list above, for
65 * which the return codes are used.
66 */
f6ef5d6b 67 TC_DELETE_MODULE_RETCODES,
23e354bf 68
80f9e023
LDM
69 _TC_LAST,
70};
71
6afc9cd6 72#define S_TC_ROOTFS "TESTSUITE_ROOTFS"
68cc4493 73#define S_TC_UNAME_R "TESTSUITE_UNAME_R"
53646fc5 74#define S_TC_INIT_MODULE_RETCODES "TESTSUITE_INIT_MODULE_RETCODES"
f6ef5d6b 75#define S_TC_DELETE_MODULE_RETCODES "TESTSUITE_DELETE_MODULE_RETCODES"
68cc4493 76
34db3f2d
LDM
77struct keyval {
78 const char *key;
79 const char *val;
80};
68cc4493 81
80f9e023
LDM
82struct test {
83 const char *name;
84 const char *description;
3dbb8dea 85 struct {
3e451bfe 86 /* File with correct stdout */
bd4e7340 87 const char *out;
3e451bfe 88 /* File with correct stderr */
bd4e7340 89 const char *err;
3e451bfe
LDM
90
91 /*
92 * Vector with pair of files
93 * key = correct file
94 * val = file to check
95 */
96 const struct keyval *files;
3dbb8dea 97 } output;
88ac4084
MM
98 /* comma-separated list of loaded modules at the end of the test */
99 const char *modules_loaded;
80f9e023
LDM
100 testfunc func;
101 const char *config[_TC_LAST];
f31d49c8 102 const char *path;
34db3f2d 103 const struct keyval *env_vars;
ed2df4e9 104 bool need_spawn;
fa0046ba 105 bool expected_fail;
80f9e023
LDM
106};
107
108
109const struct test *test_find(const struct test *tests[], const char *name);
110int test_init(int argc, char *const argv[], const struct test *tests[]);
95daea07 111int test_spawn_prog(const char *prog, const char *const args[]);
80f9e023
LDM
112
113int test_run(const struct test *t);
80f9e023
LDM
114
115#define TS_EXPORT __attribute__ ((visibility("default")))
116
117#define _LOG(prefix, fmt, ...) printf("TESTSUITE: " prefix fmt, ## __VA_ARGS__)
118#define LOG(fmt, ...) _LOG("", fmt, ## __VA_ARGS__)
119#define WARN(fmt, ...) _LOG("WARN: ", fmt, ## __VA_ARGS__)
120#define ERR(fmt, ...) _LOG("ERR: ", fmt, ## __VA_ARGS__)
121
30471c80
LDM
122#define assert_return(expr, r) \
123 do { \
124 if ((!(expr))) { \
a5a41f8d 125 ERR("Failed assertion: " #expr "%s:%d %s", \
30471c80
LDM
126 __FILE__, __LINE__, __PRETTY_FUNCTION__); \
127 return (r); \
128 } \
129 } while (false)
130
131
80f9e023 132/* Test definitions */
c5d81989 133#define DEFINE_TEST(_name, ...) \
f1155c15 134 static const struct test s##_name = { \
80f9e023
LDM
135 .name = #_name, \
136 .func = _name, \
c5d81989 137 ## __VA_ARGS__ \
80f9e023
LDM
138 }
139
e9fa9de3
LDM
140#define TESTSUITE_MAIN(_tests) \
141 int main(int argc, char *argv[]) \
142 { \
143 const struct test *t; \
144 int arg; \
145 size_t i; \
146 \
147 arg = test_init(argc, argv, tests); \
148 if (arg == 0) \
149 return 0; \
150 \
151 if (arg < argc) { \
152 t = test_find(tests, argv[arg]); \
153 if (t == NULL) { \
154 fprintf(stderr, "could not find test %s\n", argv[arg]);\
155 exit(EXIT_FAILURE); \
156 } \
157 \
158 return test_run(t); \
159 } \
160 \
161 for (i = 0; tests[i] != NULL; i++) { \
162 if (test_run(tests[i]) != 0) \
163 exit(EXIT_FAILURE); \
164 } \
165 \
166 exit(EXIT_SUCCESS); \
167 } \
168
d96ca9c4
LDM
169#ifdef noreturn
170# define __noreturn noreturn
171#elif __STDC_VERSION__ >= 201112L
172# define __noreturn _Noreturn
173#else
174# define __noreturn __attribute__((noreturn))
175#endif