1 /*
2  * Copyright (c) 2015-2016 Cyril Hrubis <chrubis@suse.cz>
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 TST_TEST_H__
19 #define TST_TEST_H__
20 
21 #ifdef __TEST_H__
22 # error Oldlib test.h already included
23 #endif /* __TEST_H__ */
24 
25 #include <unistd.h>
26 #include <limits.h>
27 
28 #include "tst_common.h"
29 #include "tst_res_flags.h"
30 #include "tst_checkpoint.h"
31 #include "tst_device.h"
32 #include "tst_mkfs.h"
33 #include "tst_fs.h"
34 #include "tst_pid.h"
35 #include "tst_cmd.h"
36 #include "tst_cpu.h"
37 #include "tst_process_state.h"
38 #include "tst_atomic.h"
39 #include "tst_kvercmp.h"
40 #include "tst_clone.h"
41 #include "tst_kernel.h"
42 #include "tst_minmax.h"
43 #include "tst_get_bad_addr.h"
44 #include "tst_path_has_mnt_flags.h"
45 #include "tst_sys_conf.h"
46 
47 /*
48  * Reports testcase result.
49  */
50 void tst_res_(const char *file, const int lineno, int ttype,
51               const char *fmt, ...)
52               __attribute__ ((format (printf, 4, 5)));
53 
54 #define tst_res(ttype, arg_fmt, ...) \
55 	tst_res_(__FILE__, __LINE__, (ttype), (arg_fmt), ##__VA_ARGS__)
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_test {
118 	/* number of tests available in test() function */
119 	unsigned int tcnt;
120 
121 	struct tst_option *options;
122 
123 	const char *min_kver;
124 
125 	/* If set the test is compiled out */
126 	const char *tconf_msg;
127 
128 	int needs_tmpdir:1;
129 	int needs_root:1;
130 	int forks_child:1;
131 	int needs_device:1;
132 	int needs_checkpoints:1;
133 	int format_device:1;
134 	int mount_device:1;
135 	int needs_rofs:1;
136 	int child_needs_reinit:1;
137 	int needs_devfs:1;
138 	/*
139 	 * If set the test function will be executed for all available
140 	 * filesystems and the current filesytem type would be set in the
141 	 * tst_device->fs_type.
142 	 *
143 	 * The test setup and cleanup are executed before/after __EACH__ call
144 	 * to the test function.
145 	 */
146 	int all_filesystems:1;
147 
148 	/* Minimal device size in megabytes */
149 	unsigned int dev_min_size;
150 
151 	/* Device filesystem type override NULL == default */
152 	const char *dev_fs_type;
153 
154 	/* Options passed to SAFE_MKFS() when format_device is set */
155 	const char *const *dev_fs_opts;
156 	const char *const *dev_extra_opts;
157 
158 	/* Device mount options, used if mount_device is set */
159 	const char *mntpoint;
160 	unsigned int mnt_flags;
161 	void *mnt_data;
162 
163 	/* override default timeout per test run, disabled == -1 */
164 	int timeout;
165 
166 	void (*setup)(void);
167 	void (*cleanup)(void);
168 
169 	void (*test)(unsigned int test_nr);
170 	void (*test_all)(void);
171 
172 	/* Syscall name used by the timer measurement library */
173 	const char *scall;
174 
175 	/* Sampling function for timer measurement testcases */
176 	int (*sample)(int clk_id, long long usec);
177 
178 	/* NULL terminated array of resource file names */
179 	const char *const *resource_files;
180 
181 	/* NULL terminated array of needed kernel drivers */
182 	const char * const *needs_drivers;
183 
184 	/*
185 	 * NULL terminated array of (/proc, /sys) files to save
186 	 * before setup and restore after cleanup
187 	 */
188 	const char * const *save_restore;
189 };
190 
191 /*
192  * Runs tests.
193  */
194 void tst_run_tcases(int argc, char *argv[], struct tst_test *self)
195                     __attribute__ ((noreturn));
196 
197 /*
198  * Does library initialization for child processes started by exec()
199  *
200  * The LTP_IPC_PATH variable must be passed to the program environment.
201  */
202 void tst_reinit(void);
203 
204 //TODO Clean?
205 #define TEST(SCALL) \
206 	do { \
207 		errno = 0; \
208 		TST_RET = SCALL; \
209 		TST_ERR = errno; \
210 	} while (0)
211 
212 extern long TST_RET;
213 extern int TST_ERR;
214 
215 extern void *TST_RET_PTR;
216 
217 #define TESTPTR(SCALL) \
218 	do { \
219 		errno = 0; \
220 		TST_RET_PTR = (void*)SCALL; \
221 		TST_ERR = errno; \
222 	} while (0)
223 
224 /*
225  * Functions to convert ERRNO to its name and SIGNAL to its name.
226  */
227 const char *tst_strerrno(int err);
228 const char *tst_strsig(int sig);
229 /*
230  * Returns string describing status as returned by wait().
231  *
232  * BEWARE: Not thread safe.
233  */
234 const char *tst_strstatus(int status);
235 
236 unsigned int tst_timeout_remaining(void);
237 void tst_set_timeout(int timeout);
238 
239 #ifndef TST_NO_DEFAULT_MAIN
240 
241 static struct tst_test test;
242 
main(int argc,char * argv[])243 int main(int argc, char *argv[])
244 {
245 	tst_run_tcases(argc, argv, &test);
246 }
247 
248 #endif /* TST_NO_DEFAULT_MAIN */
249 
250 #define TST_TEST_TCONF(message)                                 \
251         static struct tst_test test = { .tconf_msg = message  } \
252 /*
253  * This is a hack to make the testcases link without defining TCID
254  */
255 const char *TCID;
256 
257 #endif	/* TST_TEST_H__ */
258