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 
44 /*
45  * Reports testcase result.
46  */
47 void tst_res_(const char *file, const int lineno, int ttype,
48               const char *fmt, ...)
49               __attribute__ ((format (printf, 4, 5)));
50 
51 #define tst_res(ttype, arg_fmt, ...) \
52 	tst_res_(__FILE__, __LINE__, (ttype), (arg_fmt), ##__VA_ARGS__)
53 
54 void tst_resm_hexd_(const char *file, const int lineno, int ttype,
55 	const void *buf, size_t size, const char *arg_fmt, ...)
56 	__attribute__ ((format (printf, 6, 7)));
57 
58 #define tst_res_hexd(ttype, buf, size, arg_fmt, ...) \
59 	tst_resm_hexd_(__FILE__, __LINE__, (ttype), (buf), (size), \
60 			(arg_fmt), ##__VA_ARGS__)
61 
62 /*
63  * Reports result and exits a test.
64  */
65 void tst_brk_(const char *file, const int lineno, int ttype,
66               const char *fmt, ...)
67               __attribute__ ((format (printf, 4, 5)));
68 
69 #define tst_brk(ttype, arg_fmt, ...) \
70 	tst_brk_(__FILE__, __LINE__, (ttype), (arg_fmt), ##__VA_ARGS__)
71 
72 pid_t safe_fork(const char *filename, unsigned int lineno);
73 #define SAFE_FORK() \
74 	safe_fork(__FILE__, __LINE__)
75 
76 #define TST_TRACE(expr)	                                            \
77 	({int ret = expr;                                           \
78 	  ret != 0 ? tst_res(TINFO, #expr " failed"), ret : ret; }) \
79 
80 #include "tst_safe_macros.h"
81 #include "tst_safe_file_ops.h"
82 #include "tst_safe_net.h"
83 
84 /*
85  * Wait for all children and exit with TBROK if
86  * any of them returned a non-zero exit status.
87  */
88 void tst_reap_children(void);
89 
90 struct tst_option {
91 	char *optstr;
92 	char **arg;
93 	char *help;
94 };
95 
96 /*
97  * Options parsing helpers.
98  *
99  * If str is NULL these are No-op.
100  *
101  * On failure non-zero (errno) is returned.
102  */
103 int tst_parse_int(const char *str, int *val, int min, int max);
104 int tst_parse_long(const char *str, long *val, long min, long max);
105 int tst_parse_float(const char *str, float *val, float min, float max);
106 
107 struct tst_test {
108 	/* number of tests available in test() function */
109 	unsigned int tcnt;
110 
111 	struct tst_option *options;
112 
113 	const char *min_kver;
114 
115 	/* If set the test is compiled out */
116 	const char *tconf_msg;
117 
118 	int needs_tmpdir:1;
119 	int needs_root:1;
120 	int forks_child:1;
121 	int needs_device:1;
122 	int needs_checkpoints:1;
123 	int format_device:1;
124 	int mount_device:1;
125 	int needs_rofs:1;
126 	/*
127 	 * If set the test function will be executed for all available
128 	 * filesystems and the current filesytem type would be set in the
129 	 * tst_device->fs_type.
130 	 *
131 	 * The test setup and cleanup are executed before/after __EACH__ call
132 	 * to the test function.
133 	 */
134 	int all_filesystems:1;
135 
136 	/* Minimal device size in megabytes */
137 	unsigned int dev_min_size;
138 
139 	/* Device filesystem type override NULL == default */
140 	const char *dev_fs_type;
141 
142 	/* Options passed to SAFE_MKFS() when format_device is set */
143 	const char *const *dev_fs_opts;
144 	const char *dev_extra_opt;
145 
146 	/* Device mount options, used if mount_device is set */
147 	const char *mntpoint;
148 	unsigned int mnt_flags;
149 	void *mnt_data;
150 
151 	/* override default timeout per test run, disabled == -1 */
152 	int timeout;
153 
154 	void (*setup)(void);
155 	void (*cleanup)(void);
156 
157 	void (*test)(unsigned int test_nr);
158 	void (*test_all)(void);
159 
160 	/* Syscall name used by the timer measurement library */
161 	const char *scall;
162 
163 	/* Sampling function for timer measurement testcases */
164 	int (*sample)(int clk_id, long long usec);
165 
166 	/* NULL terminated array of resource file names */
167 	const char *const *resource_files;
168 };
169 
170 /*
171  * Runs tests.
172  */
173 void tst_run_tcases(int argc, char *argv[], struct tst_test *self)
174                     __attribute__ ((noreturn));
175 
176 /*
177  * Does library initialization for child processes started by exec()
178  *
179  * The LTP_IPC_PATH variable must be passed to the program environment.
180  */
181 void tst_reinit(void);
182 
183 //TODO Clean?
184 #define TEST(SCALL) \
185 	do { \
186 		errno = 0; \
187 		TEST_RETURN = SCALL; \
188 		TEST_ERRNO = errno; \
189 	} while (0)
190 
191 extern long TEST_RETURN;
192 extern int TEST_ERRNO;
193 
194 /*
195  * Functions to convert ERRNO to its name and SIGNAL to its name.
196  */
197 const char *tst_strerrno(int err);
198 const char *tst_strsig(int sig);
199 /*
200  * Returns string describing status as returned by wait().
201  *
202  * BEWARE: Not thread safe.
203  */
204 const char *tst_strstatus(int status);
205 
206 void tst_set_timeout(int timeout);
207 
208 #ifndef TST_NO_DEFAULT_MAIN
209 
210 static struct tst_test test;
211 
main(int argc,char * argv[])212 int main(int argc, char *argv[])
213 {
214 	tst_run_tcases(argc, argv, &test);
215 }
216 
217 #endif /* TST_NO_DEFAULT_MAIN */
218 
219 #define TST_TEST_TCONF(message)                                 \
220         static struct tst_test test = { .tconf_msg = message  } \
221 /*
222  * This is a hack to make the testcases link without defining TCID
223  */
224 const char *TCID;
225 
226 #endif	/* TST_TEST_H__ */
227