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