]>
Commit | Line | Data |
---|---|---|
e701e381 | 1 | /* |
e6b0e49b | 2 | * Copyright (C) 2012-2013 ProFUSION embedded systems |
e701e381 | 3 | * |
e1b1ab24 LDM |
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. | |
e701e381 LDM |
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 | |
e1b1ab24 LDM |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | * Lesser General Public License for more details. | |
e701e381 | 13 | * |
e1b1ab24 | 14 | * You should have received a copy of the GNU Lesser General Public |
dea2dfee | 15 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
e701e381 LDM |
16 | */ |
17 | ||
e8fd8fec | 18 | #pragma once |
80f9e023 LDM |
19 | |
20 | #include <stdbool.h> | |
21 | #include <stdarg.h> | |
22 | ||
576dd439 | 23 | #include <shared/macro.h> |
d96ca9c4 | 24 | |
80f9e023 LDM |
25 | struct test; |
26 | typedef int (*testfunc)(const struct test *t); | |
27 | ||
28 | enum test_config { | |
23e354bf LDM |
29 | /* |
30 | * Where's the roots dir for this test. It will LD_PRELOAD path.so in | |
31 | * order to trap calls to functions using paths. | |
32 | */ | |
80f9e023 | 33 | TC_ROOTFS = 0, |
23e354bf LDM |
34 | |
35 | /* | |
36 | * What's the desired string to be returned by `uname -r`. It will | |
37 | * trap calls to uname(3P) by LD_PRELOAD'ing uname.so and then filling | |
38 | * in the information in u.release. | |
39 | */ | |
80f9e023 | 40 | TC_UNAME_R, |
23e354bf LDM |
41 | |
42 | /* | |
43 | * Fake calls to init_module(2), returning return-code and setting | |
44 | * errno to err-code. Set this variable with the following format: | |
45 | * | |
46 | * modname:return-code:err-code | |
47 | * | |
48 | * When this variable is used, all calls to init_module() are trapped | |
49 | * and by default the return code is 0. In other words, they fake | |
50 | * "success" for all modules, except the ones in the list above, for | |
51 | * which the return codes are used. | |
52 | */ | |
53646fc5 | 53 | TC_INIT_MODULE_RETCODES, |
23e354bf LDM |
54 | |
55 | /* | |
56 | * Fake calls to delete_module(2), returning return-code and setting | |
57 | * errno to err-code. Set this variable with the following format: | |
58 | * | |
59 | * modname:return-code:err-code | |
60 | * | |
61 | * When this variable is used, all calls to init_module() are trapped | |
62 | * and by default the return code is 0. In other words, they fake | |
63 | * "success" for all modules, except the ones in the list above, for | |
64 | * which the return codes are used. | |
65 | */ | |
f6ef5d6b | 66 | TC_DELETE_MODULE_RETCODES, |
23e354bf | 67 | |
80f9e023 LDM |
68 | _TC_LAST, |
69 | }; | |
70 | ||
6afc9cd6 | 71 | #define S_TC_ROOTFS "TESTSUITE_ROOTFS" |
68cc4493 | 72 | #define S_TC_UNAME_R "TESTSUITE_UNAME_R" |
53646fc5 | 73 | #define S_TC_INIT_MODULE_RETCODES "TESTSUITE_INIT_MODULE_RETCODES" |
f6ef5d6b | 74 | #define S_TC_DELETE_MODULE_RETCODES "TESTSUITE_DELETE_MODULE_RETCODES" |
68cc4493 | 75 | |
34db3f2d LDM |
76 | struct keyval { |
77 | const char *key; | |
78 | const char *val; | |
79 | }; | |
68cc4493 | 80 | |
80f9e023 LDM |
81 | struct test { |
82 | const char *name; | |
83 | const char *description; | |
3dbb8dea | 84 | struct { |
3e451bfe | 85 | /* File with correct stdout */ |
bd4e7340 | 86 | const char *out; |
3e451bfe | 87 | /* File with correct stderr */ |
bd4e7340 | 88 | const char *err; |
3e451bfe LDM |
89 | |
90 | /* | |
91 | * Vector with pair of files | |
92 | * key = correct file | |
93 | * val = file to check | |
94 | */ | |
95 | const struct keyval *files; | |
3dbb8dea | 96 | } output; |
88ac4084 MM |
97 | /* comma-separated list of loaded modules at the end of the test */ |
98 | const char *modules_loaded; | |
80f9e023 LDM |
99 | testfunc func; |
100 | const char *config[_TC_LAST]; | |
f31d49c8 | 101 | const char *path; |
34db3f2d | 102 | const struct keyval *env_vars; |
ed2df4e9 | 103 | bool need_spawn; |
fa0046ba | 104 | bool expected_fail; |
c5798fea | 105 | } __attribute__((aligned(8))); |
80f9e023 LDM |
106 | |
107 | ||
c5798fea LDM |
108 | int test_init(const struct test *start, const struct test *stop, |
109 | int argc, char *const argv[]); | |
110 | const struct test *test_find(const struct test *start, const struct test *stop, | |
111 | const char *name); | |
95daea07 | 112 | int test_spawn_prog(const char *prog, const char *const args[]); |
80f9e023 | 113 | int test_run(const struct test *t); |
80f9e023 LDM |
114 | |
115 | #define TS_EXPORT __attribute__ ((visibility("default"))) | |
116 | ||
117 | #define _LOG(prefix, fmt, ...) printf("TESTSUITE: " prefix fmt, ## __VA_ARGS__) | |
118 | #define LOG(fmt, ...) _LOG("", fmt, ## __VA_ARGS__) | |
119 | #define WARN(fmt, ...) _LOG("WARN: ", fmt, ## __VA_ARGS__) | |
120 | #define ERR(fmt, ...) _LOG("ERR: ", fmt, ## __VA_ARGS__) | |
121 | ||
30471c80 LDM |
122 | #define assert_return(expr, r) \ |
123 | do { \ | |
124 | if ((!(expr))) { \ | |
2d1f8bde | 125 | ERR("Failed assertion: " #expr " %s:%d %s\n", \ |
30471c80 LDM |
126 | __FILE__, __LINE__, __PRETTY_FUNCTION__); \ |
127 | return (r); \ | |
128 | } \ | |
129 | } while (false) | |
130 | ||
131 | ||
80f9e023 | 132 | /* Test definitions */ |
c5d81989 | 133 | #define DEFINE_TEST(_name, ...) \ |
43289820 | 134 | static const struct test s##_name##UNIQ \ |
c5798fea | 135 | __attribute__((used, section("kmod_tests"), aligned(8))) = { \ |
80f9e023 LDM |
136 | .name = #_name, \ |
137 | .func = _name, \ | |
c5d81989 | 138 | ## __VA_ARGS__ \ |
c5798fea | 139 | }; |
80f9e023 | 140 | |
43289820 | 141 | #define TESTSUITE_MAIN() \ |
c5798fea LDM |
142 | extern struct test __start_kmod_tests[] __attribute__((weak, visibility("hidden"))); \ |
143 | extern struct test __stop_kmod_tests[] __attribute__((weak, visibility("hidden"))); \ | |
144 | int main(int argc, char *argv[]) \ | |
145 | { \ | |
146 | const struct test *t; \ | |
147 | int arg; \ | |
148 | \ | |
149 | arg = test_init(__start_kmod_tests, __stop_kmod_tests, argc, argv); \ | |
150 | if (arg == 0) \ | |
151 | return 0; \ | |
152 | \ | |
153 | if (arg < argc) { \ | |
154 | t = test_find(__start_kmod_tests, __stop_kmod_tests, argv[arg]); \ | |
155 | if (t == NULL) { \ | |
156 | fprintf(stderr, "could not find test %s\n", argv[arg]); \ | |
157 | exit(EXIT_FAILURE); \ | |
158 | } \ | |
159 | \ | |
160 | return test_run(t); \ | |
161 | } \ | |
162 | \ | |
163 | for (t = __start_kmod_tests; t < __stop_kmod_tests; t++) { \ | |
164 | if (test_run(t) != 0) \ | |
165 | exit(EXIT_FAILURE); \ | |
166 | } \ | |
167 | \ | |
168 | exit(EXIT_SUCCESS); \ | |
169 | } \ | |
e9fa9de3 | 170 | |
d96ca9c4 LDM |
171 | #ifdef noreturn |
172 | # define __noreturn noreturn | |
173 | #elif __STDC_VERSION__ >= 201112L | |
174 | # define __noreturn _Noreturn | |
175 | #else | |
176 | # define __noreturn __attribute__((noreturn)) | |
177 | #endif |