blob: d8cc806de451e137823283e7e36260c296bac57b [file] [log] [blame]
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +01001/*
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
Cyril Hrubis219b49e2017-02-14 11:45:34 +010021#ifdef __TEST_H__
22# error Oldlib test.h already included
23#endif /* __TEST_H__ */
24
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010025#include <unistd.h>
26
27#include "tst_common.h"
28#include "tst_res_flags.h"
29#include "tst_checkpoint.h"
30#include "tst_device.h"
31#include "tst_mkfs.h"
32#include "tst_fs.h"
33#include "tst_pid.h"
34#include "tst_cmd.h"
35#include "tst_cpu.h"
36#include "tst_process_state.h"
37#include "tst_atomic.h"
Stanislav Kholmanskikhdb036382016-06-09 14:55:19 +030038#include "tst_kvercmp.h"
Xiao Yang3aa31512016-07-20 16:21:13 +080039#include "tst_clone.h"
Cyril Hrubis699238a2017-01-12 13:58:18 +010040#include "tst_kernel.h"
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010041
42/*
43 * Reports testcase result.
44 */
45void tst_res_(const char *file, const int lineno, int ttype,
46 const char *fmt, ...)
47 __attribute__ ((format (printf, 4, 5)));
48
49#define tst_res(ttype, arg_fmt, ...) \
50 tst_res_(__FILE__, __LINE__, (ttype), (arg_fmt), ##__VA_ARGS__)
51
Jan Stancekf72ca5b2016-10-06 13:34:39 +020052void tst_resm_hexd_(const char *file, const int lineno, int ttype,
53 const void *buf, size_t size, const char *arg_fmt, ...)
54 __attribute__ ((format (printf, 6, 7)));
55
56#define tst_res_hexd(ttype, buf, size, arg_fmt, ...) \
57 tst_resm_hexd_(__FILE__, __LINE__, (ttype), (buf), (size), \
58 (arg_fmt), ##__VA_ARGS__)
59
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010060/*
61 * Reports result and exits a test.
62 */
63void tst_brk_(const char *file, const int lineno, int ttype,
64 const char *fmt, ...)
65 __attribute__ ((format (printf, 4, 5)))
66 __attribute__ ((noreturn));
67
68#define tst_brk(ttype, arg_fmt, ...) \
69 tst_brk_(__FILE__, __LINE__, (ttype), (arg_fmt), ##__VA_ARGS__)
70
71pid_t safe_fork(const char *filename, unsigned int lineno);
72#define SAFE_FORK() \
73 safe_fork(__FILE__, __LINE__)
74
Stanislav Kholmanskikh87937952016-08-25 16:22:44 +030075#define TST_TRACE(expr) \
76 ({int ret = expr; \
77 ret != 0 ? tst_res(TINFO, #expr " failed"), ret : ret; }) \
78
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010079#include "tst_safe_macros.h"
80#include "tst_safe_file_ops.h"
81#include "tst_safe_net.h"
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010082
Stanislav Kholmanskikh6b56aa72016-08-04 17:16:31 +030083/*
84 * Wait for all children and exit with TBROK if
85 * any of them returned a non-zero exit status.
86 */
87void tst_reap_children(void);
88
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +010089struct tst_option {
90 char *optstr;
91 char **arg;
92 char *help;
93};
94
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +020095/*
96 * Options parsing helpers.
97 *
98 * If str is NULL these are No-op.
99 *
100 * On failure non-zero (errno) is returned.
101 */
102int tst_parse_int(const char *str, int *val, int min, int max);
Alexey Kodanevdd90c002016-12-18 00:36:00 +0300103int tst_parse_long(const char *str, long *val, long min, long max);
Cyril Hrubis1e92d8a2016-08-03 16:31:46 +0200104int tst_parse_float(const char *str, float *val, float min, float max);
105
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100106struct tst_test {
107 /* test id usually the same as test filename without file suffix */
108 const char *tid;
109 /* number of tests available in test() function */
110 unsigned int tcnt;
111
112 struct tst_option *options;
113
114 const char *min_kver;
115
116 int needs_tmpdir:1;
117 int needs_root:1;
118 int forks_child:1;
119 int needs_device:1;
120 int needs_checkpoints:1;
121
Li Wangd47bb552016-09-27 14:51:23 +0800122 unsigned int device_min_size;
123
Cyril Hrubis4aebb6c2016-06-07 13:40:41 +0200124 /* override default timeout per test run */
125 unsigned int timeout;
126
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100127 void (*setup)(void);
128 void (*cleanup)(void);
129
130 void (*test)(unsigned int test_nr);
131 void (*test_all)(void);
132
133 /* NULL terminated array of resource file names */
134 const char *const *resource_files;
135};
136
137/*
138 * Runs tests.
139 */
140void tst_run_tcases(int argc, char *argv[], struct tst_test *self)
141 __attribute__ ((noreturn));
142
143/*
144 * Does library initialization for child processes started by exec()
145 *
146 * The LTP_IPC_PATH variable must be passed to the program environment.
147 */
148void tst_reinit(void);
149
150//TODO Clean?
151#define TEST(SCALL) \
152 do { \
153 errno = 0; \
154 TEST_RETURN = SCALL; \
155 TEST_ERRNO = errno; \
156 } while (0)
157
158extern long TEST_RETURN;
159extern int TEST_ERRNO;
160
161/*
162 * Functions to convert ERRNO to its name and SIGNAL to its name.
163 */
164const char *tst_strerrno(int err);
165const char *tst_strsig(int sig);
166
167#ifndef TST_NO_DEFAULT_MAIN
168
169static struct tst_test test;
170
Cyril Hrubis2ad59b72016-08-03 15:53:55 +0200171void tst_set_timeout(unsigned int timeout);
172
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100173int main(int argc, char *argv[])
174{
175 tst_run_tcases(argc, argv, &test);
176}
177
178#endif /* TST_NO_DEFAULT_MAIN */
179
Xiao Yang0aa4a612016-08-17 17:16:36 +0800180#define TST_TEST_TCONF(message) \
181 static void tst_do_test(void) { tst_brk(TCONF, "%s", message); }; \
182 static struct tst_test test = { .test_all = tst_do_test, .tid = "" } \
Cyril Hrubisbbdb9f72016-03-16 15:53:57 +0100183/*
184 * This is a hack to make the testcases link without defining TCID
185 */
186const char *TCID;
187
188#endif /* TST_TEST_H__ */