1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2015-2016 Cyril Hrubis <chrubis@suse.cz>
4 * Copyright (c) Linux Test Project, 2016-2019
5 */
6
7 #ifndef TST_TEST_H__
8 #define TST_TEST_H__
9
10 #ifdef __TEST_H__
11 # error Oldlib test.h already included
12 #endif /* __TEST_H__ */
13
14 #include <unistd.h>
15 #include <limits.h>
16 #include <string.h>
17 #include <errno.h>
18
19 #include "tst_common.h"
20 #include "tst_res_flags.h"
21 #include "tst_test_macros.h"
22 #include "tst_checkpoint.h"
23 #include "tst_device.h"
24 #include "tst_mkfs.h"
25 #include "tst_fs.h"
26 #include "tst_pid.h"
27 #include "tst_cmd.h"
28 #include "tst_cpu.h"
29 #include "tst_process_state.h"
30 #include "tst_atomic.h"
31 #include "tst_kvercmp.h"
32 #include "tst_kernel.h"
33 #include "tst_minmax.h"
34 #include "tst_get_bad_addr.h"
35 #include "tst_path_has_mnt_flags.h"
36 #include "tst_sys_conf.h"
37 #include "tst_coredump.h"
38 #include "tst_buffers.h"
39 #include "tst_capability.h"
40 #include "tst_hugepage.h"
41 #include "tst_assert.h"
42 #include "tst_lockdown.h"
43 #include "tst_fips.h"
44 #include "tst_taint.h"
45
46 /*
47 * Reports testcase result.
48 */
49 void tst_res_(const char *file, const int lineno, int ttype,
50 const char *fmt, ...)
51 __attribute__ ((format (printf, 4, 5)));
52
53 #define tst_res(ttype, arg_fmt, ...) \
54 ({ \
55 TST_RES_SUPPORTS_TCONF_TFAIL_TINFO_TPASS_TWARN(!((TTYPE_RESULT(ttype) ?: TCONF) & \
56 (TCONF | TFAIL | TINFO | TPASS | TWARN))); \
57 tst_res_(__FILE__, __LINE__, (ttype), (arg_fmt), ##__VA_ARGS__);\
58 })
59
60 void tst_resm_hexd_(const char *file, const int lineno, int ttype,
61 const void *buf, size_t size, const char *arg_fmt, ...)
62 __attribute__ ((format (printf, 6, 7)));
63
64 #define tst_res_hexd(ttype, buf, size, arg_fmt, ...) \
65 tst_resm_hexd_(__FILE__, __LINE__, (ttype), (buf), (size), \
66 (arg_fmt), ##__VA_ARGS__)
67
68 /*
69 * Reports result and exits a test.
70 */
71 void tst_brk_(const char *file, const int lineno, int ttype,
72 const char *fmt, ...)
73 __attribute__ ((format (printf, 4, 5)));
74
75 #define tst_brk(ttype, arg_fmt, ...) \
76 ({ \
77 TST_BRK_SUPPORTS_ONLY_TCONF_TBROK(!((ttype) & \
78 (TBROK | TCONF | TFAIL))); \
79 tst_brk_(__FILE__, __LINE__, (ttype), (arg_fmt), ##__VA_ARGS__);\
80 })
81
82 /* flush stderr and stdout */
83 void tst_flush(void);
84
85 pid_t safe_fork(const char *filename, unsigned int lineno);
86 #define SAFE_FORK() \
87 safe_fork(__FILE__, __LINE__)
88
89 #define TST_TRACE(expr) \
90 ({int ret = expr; \
91 ret != 0 ? tst_res(TINFO, #expr " failed"), ret : ret; }) \
92
93 #include "tst_safe_macros.h"
94 #include "tst_safe_file_ops.h"
95 #include "tst_safe_net.h"
96 #include "tst_clone.h"
97
98 /*
99 * Wait for all children and exit with TBROK if
100 * any of them returned a non-zero exit status.
101 */
102 void tst_reap_children(void);
103
104 struct tst_option {
105 char *optstr;
106 char **arg;
107 char *help;
108 };
109
110 /*
111 * Options parsing helpers.
112 *
113 * If str is NULL these are No-op.
114 *
115 * On failure non-zero (errno) is returned.
116 */
117 int tst_parse_int(const char *str, int *val, int min, int max);
118 int tst_parse_long(const char *str, long *val, long min, long max);
119 int tst_parse_float(const char *str, float *val, float min, float max);
120
121 struct tst_tag {
122 const char *name;
123 const char *value;
124 };
125
126 extern unsigned int tst_variant;
127
128 struct tst_test {
129 /* number of tests available in test() function */
130 unsigned int tcnt;
131
132 struct tst_option *options;
133
134 const char *min_kver;
135
136 /* If set the test is compiled out */
137 const char *tconf_msg;
138
139 int needs_tmpdir:1;
140 int needs_root:1;
141 int forks_child:1;
142 int needs_device:1;
143 int needs_checkpoints:1;
144 int needs_overlay:1;
145 int format_device:1;
146 int mount_device:1;
147 int needs_rofs:1;
148 int child_needs_reinit:1;
149 int needs_devfs:1;
150 int restore_wallclock:1;
151 /*
152 * If set the test function will be executed for all available
153 * filesystems and the current filesytem type would be set in the
154 * tst_device->fs_type.
155 *
156 * The test setup and cleanup are executed before/after __EACH__ call
157 * to the test function.
158 */
159 int all_filesystems:1;
160
161 /*
162 * The skip_filesystem is a NULL terminated list of filesystems the
163 * test does not support. It can also be used to disable whole class of
164 * filesystems with a special keyworks such as "fuse".
165 */
166 const char *const *skip_filesystems;
167
168 /* Minimum number of online CPU required by the test */
169 unsigned long min_cpus;
170
171 /*
172 * If set non-zero number of request_hugepages, test will try to reserve the
173 * expected number of hugepage for testing in setup phase. If system does not
174 * have enough hpage for using, it will try the best to reserve 80% available
175 * number of hpages. With success test stores the reserved hugepage number in
176 * 'tst_hugepages. For the system without hugetlb supporting, variable
177 * 'tst_hugepages' will be set to 0.
178 *
179 * Also, we do cleanup and restore work for the hpages resetting automatically.
180 */
181 unsigned long request_hugepages;
182
183 /*
184 * If set to non-zero, call tst_taint_init(taint_check) during setup
185 * and check kernel taint at the end of the test. If all_filesystems
186 * is non-zero, taint check will be performed after each FS test and
187 * testing will be terminated by TBROK if taint is detected.
188 */
189 unsigned int taint_check;
190
191 /*
192 * If set non-zero denotes number of test variant, the test is executed
193 * variants times each time with tst_variant set to different number.
194 *
195 * This allows us to run the same test for different settings. The
196 * intended use is to test different syscall wrappers/variants but the
197 * API is generic and does not limit the usage in any way.
198 */
199 unsigned int test_variants;
200
201 /* Minimal device size in megabytes */
202 unsigned int dev_min_size;
203
204 /* Device filesystem type override NULL == default */
205 const char *dev_fs_type;
206
207 /* Options passed to SAFE_MKFS() when format_device is set */
208 const char *const *dev_fs_opts;
209 const char *const *dev_extra_opts;
210
211 /* Device mount options, used if mount_device is set */
212 const char *mntpoint;
213 unsigned int mnt_flags;
214 void *mnt_data;
215
216 /* override default timeout per test run, disabled == -1 */
217 int timeout;
218
219 void (*setup)(void);
220 void (*cleanup)(void);
221
222 void (*test)(unsigned int test_nr);
223 void (*test_all)(void);
224
225 /* Syscall name used by the timer measurement library */
226 const char *scall;
227
228 /* Sampling function for timer measurement testcases */
229 int (*sample)(int clk_id, long long usec);
230
231 /* NULL terminated array of resource file names */
232 const char *const *resource_files;
233
234 /* NULL terminated array of needed kernel drivers */
235 const char * const *needs_drivers;
236
237 /*
238 * NULL terminated array of (/proc, /sys) files to save
239 * before setup and restore after cleanup
240 */
241 const char * const *save_restore;
242
243 /*
244 * NULL terminated array of kernel config options required for the
245 * test.
246 */
247 const char *const *needs_kconfigs;
248
249 /*
250 * NULL-terminated array to be allocated buffers.
251 */
252 struct tst_buffers *bufs;
253
254 /*
255 * NULL-terminated array of capability settings
256 */
257 struct tst_cap *caps;
258
259 /*
260 * {NULL, NULL} terminated array of tags.
261 */
262 const struct tst_tag *tags;
263
264 /* NULL terminated array of required commands */
265 const char *const *needs_cmds;
266 };
267
268 /*
269 * Runs tests.
270 */
271 void tst_run_tcases(int argc, char *argv[], struct tst_test *self)
272 __attribute__ ((noreturn));
273
274 /*
275 * Does library initialization for child processes started by exec()
276 *
277 * The LTP_IPC_PATH variable must be passed to the program environment.
278 */
279 void tst_reinit(void);
280
281 /*
282 * Functions to convert ERRNO to its name and SIGNAL to its name.
283 */
284 const char *tst_strerrno(int err);
285 const char *tst_strsig(int sig);
286 /*
287 * Returns string describing status as returned by wait().
288 *
289 * BEWARE: Not thread safe.
290 */
291 const char *tst_strstatus(int status);
292
293 unsigned int tst_timeout_remaining(void);
294 unsigned int tst_multiply_timeout(unsigned int timeout);
295 void tst_set_timeout(int timeout);
296
297
298 /*
299 * Returns path to the test temporary directory in a newly allocated buffer.
300 */
301 char *tst_get_tmpdir(void);
302
303 #ifndef TST_NO_DEFAULT_MAIN
304
305 static struct tst_test test;
306
main(int argc,char * argv[])307 int main(int argc, char *argv[])
308 {
309 tst_run_tcases(argc, argv, &test);
310 }
311
312 #endif /* TST_NO_DEFAULT_MAIN */
313
314 #define TST_TEST_TCONF(message) \
315 static struct tst_test test = { .tconf_msg = message } \
316
317 #endif /* TST_TEST_H__ */
318