1 /* 2 * Copyright (c) 2015-2016 Cyril Hrubis <chrubis (at) 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 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" 38 #include "tst_kvercmp.h" 39 #include "tst_clone.h" 40 #include "tst_kernel.h" 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 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 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 67 #define tst_brk(ttype, arg_fmt, ...) \ 68 tst_brk_(__FILE__, __LINE__, (ttype), (arg_fmt), ##__VA_ARGS__) 69 70 pid_t safe_fork(const char *filename, unsigned int lineno); 71 #define SAFE_FORK() \ 72 safe_fork(__FILE__, __LINE__) 73 74 #define TST_TRACE(expr) \ 75 ({int ret = expr; \ 76 ret != 0 ? tst_res(TINFO, #expr " failed"), ret : ret; }) \ 77 78 #include "tst_safe_macros.h" 79 #include "tst_safe_file_ops.h" 80 #include "tst_safe_net.h" 81 82 /* 83 * Wait for all children and exit with TBROK if 84 * any of them returned a non-zero exit status. 85 */ 86 void tst_reap_children(void); 87 88 struct tst_option { 89 char *optstr; 90 char **arg; 91 char *help; 92 }; 93 94 /* 95 * Options parsing helpers. 96 * 97 * If str is NULL these are No-op. 98 * 99 * On failure non-zero (errno) is returned. 100 */ 101 int tst_parse_int(const char *str, int *val, int min, int max); 102 int tst_parse_long(const char *str, long *val, long min, long max); 103 int tst_parse_float(const char *str, float *val, float min, float max); 104 105 struct tst_test { 106 /* test id usually the same as test filename without file suffix */ 107 const char *tid; 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 int needs_tmpdir:1; 116 int needs_root:1; 117 int forks_child:1; 118 int needs_device:1; 119 int needs_checkpoints:1; 120 int format_device:1; 121 int mount_device:1; 122 123 /* Minimal device size in megabytes */ 124 unsigned int dev_min_size; 125 126 /* Device filesystem type override NULL == default */ 127 const char *dev_fs_type; 128 129 /* Options passed to SAFE_MKFS() when format_device is set */ 130 const char *const *dev_fs_opts; 131 const char *dev_extra_opt; 132 133 /* Device mount options, used if mount_device is set */ 134 const char *mntpoint; 135 unsigned int mnt_flags; 136 void *mnt_data; 137 138 /* override default timeout per test run */ 139 unsigned int timeout; 140 141 void (*setup)(void); 142 void (*cleanup)(void); 143 144 void (*test)(unsigned int test_nr); 145 void (*test_all)(void); 146 147 /* NULL terminated array of resource file names */ 148 const char *const *resource_files; 149 }; 150 151 /* 152 * Runs tests. 153 */ 154 void tst_run_tcases(int argc, char *argv[], struct tst_test *self) 155 __attribute__ ((noreturn)); 156 157 /* 158 * Does library initialization for child processes started by exec() 159 * 160 * The LTP_IPC_PATH variable must be passed to the program environment. 161 */ 162 void tst_reinit(void); 163 164 //TODO Clean? 165 #define TEST(SCALL) \ 166 do { \ 167 errno = 0; \ 168 TEST_RETURN = SCALL; \ 169 TEST_ERRNO = errno; \ 170 } while (0) 171 172 extern long TEST_RETURN; 173 extern int TEST_ERRNO; 174 175 /* 176 * Functions to convert ERRNO to its name and SIGNAL to its name. 177 */ 178 const char *tst_strerrno(int err); 179 const char *tst_strsig(int sig); 180 181 #ifndef TST_NO_DEFAULT_MAIN 182 183 static struct tst_test test; 184 185 void tst_set_timeout(unsigned int timeout); 186 187 int main(int argc, char *argv[]) 188 { 189 tst_run_tcases(argc, argv, &test); 190 } 191 192 #endif /* TST_NO_DEFAULT_MAIN */ 193 194 #define TST_TEST_TCONF(message) \ 195 static void tst_do_test(void) { tst_brk(TCONF, "%s", message); }; \ 196 static struct tst_test test = { .test_all = tst_do_test, .tid = "" } \ 197 /* 198 * This is a hack to make the testcases link without defining TCID 199 */ 200 const char *TCID; 201 202 #endif /* TST_TEST_H__ */ 203