Lucas De Marchi | e701e38 | 2012-01-26 17:01:41 -0200 | [diff] [blame] | 1 | /* |
Lucas De Marchi | e6b0e49 | 2013-01-16 11:27:21 -0200 | [diff] [blame] | 2 | * Copyright (C) 2012-2013 ProFUSION embedded systems |
Lucas De Marchi | e701e38 | 2012-01-26 17:01:41 -0200 | [diff] [blame] | 3 | * |
Lucas De Marchi | e1b1ab2 | 2012-07-10 09:42:24 -0300 | [diff] [blame] | 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Lesser General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2.1 of the License, or (at your option) any later version. |
Lucas De Marchi | e701e38 | 2012-01-26 17:01:41 -0200 | [diff] [blame] | 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 |
Lucas De Marchi | e1b1ab2 | 2012-07-10 09:42:24 -0300 | [diff] [blame] | 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Lesser General Public License for more details. |
Lucas De Marchi | e701e38 | 2012-01-26 17:01:41 -0200 | [diff] [blame] | 13 | * |
Lucas De Marchi | e1b1ab2 | 2012-07-10 09:42:24 -0300 | [diff] [blame] | 14 | * You should have received a copy of the GNU Lesser General Public |
Lucas De Marchi | dea2dfe | 2014-12-25 23:32:03 -0200 | [diff] [blame] | 15 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
Lucas De Marchi | e701e38 | 2012-01-26 17:01:41 -0200 | [diff] [blame] | 16 | */ |
| 17 | |
Lucas De Marchi | e8fd8fe | 2012-07-18 10:19:48 -0300 | [diff] [blame] | 18 | #pragma once |
Lucas De Marchi | 80f9e02 | 2012-01-24 20:59:54 -0200 | [diff] [blame] | 19 | |
| 20 | #include <stdbool.h> |
| 21 | #include <stdarg.h> |
Lucas De Marchi | a6421a0 | 2016-08-10 14:51:57 -0300 | [diff] [blame^] | 22 | #include <stdio.h> |
Lucas De Marchi | 80f9e02 | 2012-01-24 20:59:54 -0200 | [diff] [blame] | 23 | |
Lucas De Marchi | 576dd43 | 2014-10-02 22:03:19 -0300 | [diff] [blame] | 24 | #include <shared/macro.h> |
Lucas De Marchi | d96ca9c | 2013-12-17 19:10:16 -0200 | [diff] [blame] | 25 | |
Lucas De Marchi | 80f9e02 | 2012-01-24 20:59:54 -0200 | [diff] [blame] | 26 | struct test; |
| 27 | typedef int (*testfunc)(const struct test *t); |
| 28 | |
| 29 | enum test_config { |
Lucas De Marchi | 23e354b | 2012-02-06 16:50:54 -0200 | [diff] [blame] | 30 | /* |
| 31 | * Where's the roots dir for this test. It will LD_PRELOAD path.so in |
| 32 | * order to trap calls to functions using paths. |
| 33 | */ |
Lucas De Marchi | 80f9e02 | 2012-01-24 20:59:54 -0200 | [diff] [blame] | 34 | TC_ROOTFS = 0, |
Lucas De Marchi | 23e354b | 2012-02-06 16:50:54 -0200 | [diff] [blame] | 35 | |
| 36 | /* |
| 37 | * What's the desired string to be returned by `uname -r`. It will |
| 38 | * trap calls to uname(3P) by LD_PRELOAD'ing uname.so and then filling |
| 39 | * in the information in u.release. |
| 40 | */ |
Lucas De Marchi | 80f9e02 | 2012-01-24 20:59:54 -0200 | [diff] [blame] | 41 | TC_UNAME_R, |
Lucas De Marchi | 23e354b | 2012-02-06 16:50:54 -0200 | [diff] [blame] | 42 | |
| 43 | /* |
| 44 | * Fake calls to init_module(2), returning return-code and setting |
| 45 | * errno to err-code. Set this variable with the following format: |
| 46 | * |
| 47 | * modname:return-code:err-code |
| 48 | * |
| 49 | * When this variable is used, all calls to init_module() are trapped |
| 50 | * and by default the return code is 0. In other words, they fake |
| 51 | * "success" for all modules, except the ones in the list above, for |
| 52 | * which the return codes are used. |
| 53 | */ |
Lucas De Marchi | 53646fc | 2012-01-26 02:09:28 -0200 | [diff] [blame] | 54 | TC_INIT_MODULE_RETCODES, |
Lucas De Marchi | 23e354b | 2012-02-06 16:50:54 -0200 | [diff] [blame] | 55 | |
| 56 | /* |
| 57 | * Fake calls to delete_module(2), returning return-code and setting |
| 58 | * errno to err-code. Set this variable with the following format: |
| 59 | * |
| 60 | * modname:return-code:err-code |
| 61 | * |
| 62 | * When this variable is used, all calls to init_module() are trapped |
| 63 | * and by default the return code is 0. In other words, they fake |
| 64 | * "success" for all modules, except the ones in the list above, for |
| 65 | * which the return codes are used. |
| 66 | */ |
Lucas De Marchi | f6ef5d6 | 2012-01-26 16:10:41 -0200 | [diff] [blame] | 67 | TC_DELETE_MODULE_RETCODES, |
Lucas De Marchi | 23e354b | 2012-02-06 16:50:54 -0200 | [diff] [blame] | 68 | |
Lucas De Marchi | 80f9e02 | 2012-01-24 20:59:54 -0200 | [diff] [blame] | 69 | _TC_LAST, |
| 70 | }; |
| 71 | |
Lucas De Marchi | 6afc9cd | 2012-01-25 02:44:45 -0200 | [diff] [blame] | 72 | #define S_TC_ROOTFS "TESTSUITE_ROOTFS" |
Lucas De Marchi | 68cc449 | 2012-01-24 22:04:46 -0200 | [diff] [blame] | 73 | #define S_TC_UNAME_R "TESTSUITE_UNAME_R" |
Lucas De Marchi | 53646fc | 2012-01-26 02:09:28 -0200 | [diff] [blame] | 74 | #define S_TC_INIT_MODULE_RETCODES "TESTSUITE_INIT_MODULE_RETCODES" |
Lucas De Marchi | f6ef5d6 | 2012-01-26 16:10:41 -0200 | [diff] [blame] | 75 | #define S_TC_DELETE_MODULE_RETCODES "TESTSUITE_DELETE_MODULE_RETCODES" |
Lucas De Marchi | 68cc449 | 2012-01-24 22:04:46 -0200 | [diff] [blame] | 76 | |
Lucas De Marchi | 34db3f2 | 2012-06-06 01:42:30 -0300 | [diff] [blame] | 77 | struct keyval { |
| 78 | const char *key; |
| 79 | const char *val; |
| 80 | }; |
Lucas De Marchi | 68cc449 | 2012-01-24 22:04:46 -0200 | [diff] [blame] | 81 | |
Lucas De Marchi | 80f9e02 | 2012-01-24 20:59:54 -0200 | [diff] [blame] | 82 | struct test { |
| 83 | const char *name; |
| 84 | const char *description; |
Lucas De Marchi | 3dbb8de | 2012-01-25 17:46:52 -0200 | [diff] [blame] | 85 | struct { |
Lucas De Marchi | 3e451bf | 2012-10-04 00:21:52 -0300 | [diff] [blame] | 86 | /* File with correct stdout */ |
John Spencer | bd4e734 | 2013-08-27 01:38:11 +0200 | [diff] [blame] | 87 | const char *out; |
Lucas De Marchi | 3e451bf | 2012-10-04 00:21:52 -0300 | [diff] [blame] | 88 | /* File with correct stderr */ |
John Spencer | bd4e734 | 2013-08-27 01:38:11 +0200 | [diff] [blame] | 89 | const char *err; |
Lucas De Marchi | 3e451bf | 2012-10-04 00:21:52 -0300 | [diff] [blame] | 90 | |
| 91 | /* |
| 92 | * Vector with pair of files |
| 93 | * key = correct file |
| 94 | * val = file to check |
| 95 | */ |
| 96 | const struct keyval *files; |
Lucas De Marchi | 3dbb8de | 2012-01-25 17:46:52 -0200 | [diff] [blame] | 97 | } output; |
Michal Marek | 88ac408 | 2014-02-28 13:05:28 +0100 | [diff] [blame] | 98 | /* comma-separated list of loaded modules at the end of the test */ |
| 99 | const char *modules_loaded; |
Lucas De Marchi | 80f9e02 | 2012-01-24 20:59:54 -0200 | [diff] [blame] | 100 | testfunc func; |
| 101 | const char *config[_TC_LAST]; |
Dave Reisner | f31d49c | 2012-02-14 21:49:26 -0500 | [diff] [blame] | 102 | const char *path; |
Lucas De Marchi | 34db3f2 | 2012-06-06 01:42:30 -0300 | [diff] [blame] | 103 | const struct keyval *env_vars; |
Lucas De Marchi | ed2df4e | 2012-01-24 23:28:39 -0200 | [diff] [blame] | 104 | bool need_spawn; |
Dave Reisner | fa0046b | 2012-01-30 17:52:25 -0500 | [diff] [blame] | 105 | bool expected_fail; |
Lucas De Marchi | f6dc239 | 2015-02-21 13:39:36 -0200 | [diff] [blame] | 106 | bool print_outputs; |
Lucas De Marchi | c5798fe | 2014-10-09 14:14:58 -0300 | [diff] [blame] | 107 | } __attribute__((aligned(8))); |
Lucas De Marchi | 80f9e02 | 2012-01-24 20:59:54 -0200 | [diff] [blame] | 108 | |
| 109 | |
Lucas De Marchi | c5798fe | 2014-10-09 14:14:58 -0300 | [diff] [blame] | 110 | int test_init(const struct test *start, const struct test *stop, |
| 111 | int argc, char *const argv[]); |
| 112 | const struct test *test_find(const struct test *start, const struct test *stop, |
| 113 | const char *name); |
Lucas De Marchi | 95daea0 | 2012-01-25 20:32:48 -0200 | [diff] [blame] | 114 | int test_spawn_prog(const char *prog, const char *const args[]); |
Lucas De Marchi | 80f9e02 | 2012-01-24 20:59:54 -0200 | [diff] [blame] | 115 | int test_run(const struct test *t); |
Lucas De Marchi | 80f9e02 | 2012-01-24 20:59:54 -0200 | [diff] [blame] | 116 | |
| 117 | #define TS_EXPORT __attribute__ ((visibility("default"))) |
| 118 | |
| 119 | #define _LOG(prefix, fmt, ...) printf("TESTSUITE: " prefix fmt, ## __VA_ARGS__) |
| 120 | #define LOG(fmt, ...) _LOG("", fmt, ## __VA_ARGS__) |
| 121 | #define WARN(fmt, ...) _LOG("WARN: ", fmt, ## __VA_ARGS__) |
| 122 | #define ERR(fmt, ...) _LOG("ERR: ", fmt, ## __VA_ARGS__) |
| 123 | |
Lucas De Marchi | 30471c8 | 2014-06-06 02:19:01 -0300 | [diff] [blame] | 124 | #define assert_return(expr, r) \ |
| 125 | do { \ |
| 126 | if ((!(expr))) { \ |
Lucas De Marchi | 2d1f8bd | 2015-01-14 16:28:16 -0200 | [diff] [blame] | 127 | ERR("Failed assertion: " #expr " %s:%d %s\n", \ |
Lucas De Marchi | 30471c8 | 2014-06-06 02:19:01 -0300 | [diff] [blame] | 128 | __FILE__, __LINE__, __PRETTY_FUNCTION__); \ |
| 129 | return (r); \ |
| 130 | } \ |
| 131 | } while (false) |
| 132 | |
| 133 | |
Lucas De Marchi | 80f9e02 | 2012-01-24 20:59:54 -0200 | [diff] [blame] | 134 | /* Test definitions */ |
Lucas De Marchi | c5d8198 | 2012-02-07 10:46:46 -0200 | [diff] [blame] | 135 | #define DEFINE_TEST(_name, ...) \ |
Lucas De Marchi | 4328982 | 2014-10-09 14:29:04 -0300 | [diff] [blame] | 136 | static const struct test s##_name##UNIQ \ |
Lucas De Marchi | c5798fe | 2014-10-09 14:14:58 -0300 | [diff] [blame] | 137 | __attribute__((used, section("kmod_tests"), aligned(8))) = { \ |
Lucas De Marchi | 80f9e02 | 2012-01-24 20:59:54 -0200 | [diff] [blame] | 138 | .name = #_name, \ |
| 139 | .func = _name, \ |
Lucas De Marchi | c5d8198 | 2012-02-07 10:46:46 -0200 | [diff] [blame] | 140 | ## __VA_ARGS__ \ |
Lucas De Marchi | c5798fe | 2014-10-09 14:14:58 -0300 | [diff] [blame] | 141 | }; |
Lucas De Marchi | 80f9e02 | 2012-01-24 20:59:54 -0200 | [diff] [blame] | 142 | |
Lucas De Marchi | 4328982 | 2014-10-09 14:29:04 -0300 | [diff] [blame] | 143 | #define TESTSUITE_MAIN() \ |
Lucas De Marchi | c5798fe | 2014-10-09 14:14:58 -0300 | [diff] [blame] | 144 | extern struct test __start_kmod_tests[] __attribute__((weak, visibility("hidden"))); \ |
| 145 | extern struct test __stop_kmod_tests[] __attribute__((weak, visibility("hidden"))); \ |
| 146 | int main(int argc, char *argv[]) \ |
| 147 | { \ |
| 148 | const struct test *t; \ |
| 149 | int arg; \ |
| 150 | \ |
| 151 | arg = test_init(__start_kmod_tests, __stop_kmod_tests, argc, argv); \ |
| 152 | if (arg == 0) \ |
| 153 | return 0; \ |
| 154 | \ |
| 155 | if (arg < argc) { \ |
| 156 | t = test_find(__start_kmod_tests, __stop_kmod_tests, argv[arg]); \ |
| 157 | if (t == NULL) { \ |
| 158 | fprintf(stderr, "could not find test %s\n", argv[arg]); \ |
| 159 | exit(EXIT_FAILURE); \ |
| 160 | } \ |
| 161 | \ |
| 162 | return test_run(t); \ |
| 163 | } \ |
| 164 | \ |
| 165 | for (t = __start_kmod_tests; t < __stop_kmod_tests; t++) { \ |
| 166 | if (test_run(t) != 0) \ |
| 167 | exit(EXIT_FAILURE); \ |
| 168 | } \ |
| 169 | \ |
| 170 | exit(EXIT_SUCCESS); \ |
| 171 | } \ |
Lucas De Marchi | e9fa9de | 2012-02-07 10:09:20 -0200 | [diff] [blame] | 172 | |
Lucas De Marchi | d96ca9c | 2013-12-17 19:10:16 -0200 | [diff] [blame] | 173 | #ifdef noreturn |
| 174 | # define __noreturn noreturn |
| 175 | #elif __STDC_VERSION__ >= 201112L |
| 176 | # define __noreturn _Noreturn |
| 177 | #else |
| 178 | # define __noreturn __attribute__((noreturn)) |
| 179 | #endif |