Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 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 | |
Cyril Hrubis | 219b49e | 2017-02-14 11:45:34 +0100 | [diff] [blame^] | 21 | #ifdef __TEST_H__ |
| 22 | # error Oldlib test.h already included |
| 23 | #endif /* __TEST_H__ */ |
| 24 | |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 25 | #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 Kholmanskikh | db03638 | 2016-06-09 14:55:19 +0300 | [diff] [blame] | 38 | #include "tst_kvercmp.h" |
Xiao Yang | 3aa3151 | 2016-07-20 16:21:13 +0800 | [diff] [blame] | 39 | #include "tst_clone.h" |
Cyril Hrubis | 699238a | 2017-01-12 13:58:18 +0100 | [diff] [blame] | 40 | #include "tst_kernel.h" |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 41 | |
| 42 | /* |
| 43 | * Reports testcase result. |
| 44 | */ |
| 45 | void 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 Stancek | f72ca5b | 2016-10-06 13:34:39 +0200 | [diff] [blame] | 52 | void 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 Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 60 | /* |
| 61 | * Reports result and exits a test. |
| 62 | */ |
| 63 | void 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 | |
| 71 | pid_t safe_fork(const char *filename, unsigned int lineno); |
| 72 | #define SAFE_FORK() \ |
| 73 | safe_fork(__FILE__, __LINE__) |
| 74 | |
Stanislav Kholmanskikh | 8793795 | 2016-08-25 16:22:44 +0300 | [diff] [blame] | 75 | #define TST_TRACE(expr) \ |
| 76 | ({int ret = expr; \ |
| 77 | ret != 0 ? tst_res(TINFO, #expr " failed"), ret : ret; }) \ |
| 78 | |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 79 | #include "tst_safe_macros.h" |
| 80 | #include "tst_safe_file_ops.h" |
| 81 | #include "tst_safe_net.h" |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 82 | |
Stanislav Kholmanskikh | 6b56aa7 | 2016-08-04 17:16:31 +0300 | [diff] [blame] | 83 | /* |
| 84 | * Wait for all children and exit with TBROK if |
| 85 | * any of them returned a non-zero exit status. |
| 86 | */ |
| 87 | void tst_reap_children(void); |
| 88 | |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 89 | struct tst_option { |
| 90 | char *optstr; |
| 91 | char **arg; |
| 92 | char *help; |
| 93 | }; |
| 94 | |
Cyril Hrubis | 1e92d8a | 2016-08-03 16:31:46 +0200 | [diff] [blame] | 95 | /* |
| 96 | * Options parsing helpers. |
| 97 | * |
| 98 | * If str is NULL these are No-op. |
| 99 | * |
| 100 | * On failure non-zero (errno) is returned. |
| 101 | */ |
| 102 | int tst_parse_int(const char *str, int *val, int min, int max); |
Alexey Kodanev | dd90c00 | 2016-12-18 00:36:00 +0300 | [diff] [blame] | 103 | int tst_parse_long(const char *str, long *val, long min, long max); |
Cyril Hrubis | 1e92d8a | 2016-08-03 16:31:46 +0200 | [diff] [blame] | 104 | int tst_parse_float(const char *str, float *val, float min, float max); |
| 105 | |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 106 | struct 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 Wang | d47bb55 | 2016-09-27 14:51:23 +0800 | [diff] [blame] | 122 | unsigned int device_min_size; |
| 123 | |
Cyril Hrubis | 4aebb6c | 2016-06-07 13:40:41 +0200 | [diff] [blame] | 124 | /* override default timeout per test run */ |
| 125 | unsigned int timeout; |
| 126 | |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 127 | 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 | */ |
| 140 | void 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 | */ |
| 148 | void 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 | |
| 158 | extern long TEST_RETURN; |
| 159 | extern int TEST_ERRNO; |
| 160 | |
| 161 | /* |
| 162 | * Functions to convert ERRNO to its name and SIGNAL to its name. |
| 163 | */ |
| 164 | const char *tst_strerrno(int err); |
| 165 | const char *tst_strsig(int sig); |
| 166 | |
| 167 | #ifndef TST_NO_DEFAULT_MAIN |
| 168 | |
| 169 | static struct tst_test test; |
| 170 | |
Cyril Hrubis | 2ad59b7 | 2016-08-03 15:53:55 +0200 | [diff] [blame] | 171 | void tst_set_timeout(unsigned int timeout); |
| 172 | |
Cyril Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 173 | int main(int argc, char *argv[]) |
| 174 | { |
| 175 | tst_run_tcases(argc, argv, &test); |
| 176 | } |
| 177 | |
| 178 | #endif /* TST_NO_DEFAULT_MAIN */ |
| 179 | |
Xiao Yang | 0aa4a61 | 2016-08-17 17:16:36 +0800 | [diff] [blame] | 180 | #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 Hrubis | bbdb9f7 | 2016-03-16 15:53:57 +0100 | [diff] [blame] | 183 | /* |
| 184 | * This is a hack to make the testcases link without defining TCID |
| 185 | */ |
| 186 | const char *TCID; |
| 187 | |
| 188 | #endif /* TST_TEST_H__ */ |