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