]>
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 LDM |
14 | * You should have received a copy of the GNU Lesser General Public |
15 | * License along with this library; if not, write to the Free Software | |
16 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
e701e381 LDM |
17 | */ |
18 | ||
e8fd8fec | 19 | #pragma once |
80f9e023 LDM |
20 | |
21 | #include <stdbool.h> | |
22 | #include <stdarg.h> | |
23 | ||
24 | struct test; | |
25 | typedef int (*testfunc)(const struct test *t); | |
26 | ||
27 | enum test_config { | |
23e354bf LDM |
28 | /* |
29 | * Where's the roots dir for this test. It will LD_PRELOAD path.so in | |
30 | * order to trap calls to functions using paths. | |
31 | */ | |
80f9e023 | 32 | TC_ROOTFS = 0, |
23e354bf LDM |
33 | |
34 | /* | |
35 | * What's the desired string to be returned by `uname -r`. It will | |
36 | * trap calls to uname(3P) by LD_PRELOAD'ing uname.so and then filling | |
37 | * in the information in u.release. | |
38 | */ | |
80f9e023 | 39 | TC_UNAME_R, |
23e354bf LDM |
40 | |
41 | /* | |
42 | * Fake calls to init_module(2), returning return-code and setting | |
43 | * errno to err-code. Set this variable with the following format: | |
44 | * | |
45 | * modname:return-code:err-code | |
46 | * | |
47 | * When this variable is used, all calls to init_module() are trapped | |
48 | * and by default the return code is 0. In other words, they fake | |
49 | * "success" for all modules, except the ones in the list above, for | |
50 | * which the return codes are used. | |
51 | */ | |
53646fc5 | 52 | TC_INIT_MODULE_RETCODES, |
23e354bf LDM |
53 | |
54 | /* | |
55 | * Fake calls to delete_module(2), returning return-code and setting | |
56 | * errno to err-code. Set this variable with the following format: | |
57 | * | |
58 | * modname:return-code:err-code | |
59 | * | |
60 | * When this variable is used, all calls to init_module() are trapped | |
61 | * and by default the return code is 0. In other words, they fake | |
62 | * "success" for all modules, except the ones in the list above, for | |
63 | * which the return codes are used. | |
64 | */ | |
f6ef5d6b | 65 | TC_DELETE_MODULE_RETCODES, |
23e354bf | 66 | |
80f9e023 LDM |
67 | _TC_LAST, |
68 | }; | |
69 | ||
6afc9cd6 | 70 | #define S_TC_ROOTFS "TESTSUITE_ROOTFS" |
68cc4493 | 71 | #define S_TC_UNAME_R "TESTSUITE_UNAME_R" |
53646fc5 | 72 | #define S_TC_INIT_MODULE_RETCODES "TESTSUITE_INIT_MODULE_RETCODES" |
f6ef5d6b | 73 | #define S_TC_DELETE_MODULE_RETCODES "TESTSUITE_DELETE_MODULE_RETCODES" |
68cc4493 | 74 | |
34db3f2d LDM |
75 | struct keyval { |
76 | const char *key; | |
77 | const char *val; | |
78 | }; | |
68cc4493 | 79 | |
80f9e023 LDM |
80 | struct test { |
81 | const char *name; | |
82 | const char *description; | |
3dbb8dea | 83 | struct { |
3e451bfe | 84 | /* File with correct stdout */ |
3dbb8dea | 85 | const char *stdout; |
3e451bfe | 86 | /* File with correct stderr */ |
3dbb8dea | 87 | const char *stderr; |
3e451bfe LDM |
88 | |
89 | /* | |
90 | * Vector with pair of files | |
91 | * key = correct file | |
92 | * val = file to check | |
93 | */ | |
94 | const struct keyval *files; | |
3dbb8dea | 95 | } output; |
80f9e023 LDM |
96 | testfunc func; |
97 | const char *config[_TC_LAST]; | |
f31d49c8 | 98 | const char *path; |
34db3f2d | 99 | const struct keyval *env_vars; |
ed2df4e9 | 100 | bool need_spawn; |
fa0046ba | 101 | bool expected_fail; |
80f9e023 LDM |
102 | }; |
103 | ||
104 | ||
105 | const struct test *test_find(const struct test *tests[], const char *name); | |
106 | int test_init(int argc, char *const argv[], const struct test *tests[]); | |
95daea07 | 107 | int test_spawn_prog(const char *prog, const char *const args[]); |
80f9e023 LDM |
108 | |
109 | int test_run(const struct test *t); | |
80f9e023 LDM |
110 | |
111 | #define TS_EXPORT __attribute__ ((visibility("default"))) | |
112 | ||
113 | #define _LOG(prefix, fmt, ...) printf("TESTSUITE: " prefix fmt, ## __VA_ARGS__) | |
114 | #define LOG(fmt, ...) _LOG("", fmt, ## __VA_ARGS__) | |
115 | #define WARN(fmt, ...) _LOG("WARN: ", fmt, ## __VA_ARGS__) | |
116 | #define ERR(fmt, ...) _LOG("ERR: ", fmt, ## __VA_ARGS__) | |
117 | ||
118 | /* Test definitions */ | |
c5d81989 LDM |
119 | #define DEFINE_TEST(_name, ...) \ |
120 | const struct test s##_name = { \ | |
80f9e023 LDM |
121 | .name = #_name, \ |
122 | .func = _name, \ | |
c5d81989 | 123 | ## __VA_ARGS__ \ |
80f9e023 LDM |
124 | } |
125 | ||
e9fa9de3 LDM |
126 | #define TESTSUITE_MAIN(_tests) \ |
127 | int main(int argc, char *argv[]) \ | |
128 | { \ | |
129 | const struct test *t; \ | |
130 | int arg; \ | |
131 | size_t i; \ | |
132 | \ | |
133 | arg = test_init(argc, argv, tests); \ | |
134 | if (arg == 0) \ | |
135 | return 0; \ | |
136 | \ | |
137 | if (arg < argc) { \ | |
138 | t = test_find(tests, argv[arg]); \ | |
139 | if (t == NULL) { \ | |
140 | fprintf(stderr, "could not find test %s\n", argv[arg]);\ | |
141 | exit(EXIT_FAILURE); \ | |
142 | } \ | |
143 | \ | |
144 | return test_run(t); \ | |
145 | } \ | |
146 | \ | |
147 | for (i = 0; tests[i] != NULL; i++) { \ | |
148 | if (test_run(tests[i]) != 0) \ | |
149 | exit(EXIT_FAILURE); \ | |
150 | } \ | |
151 | \ | |
152 | exit(EXIT_SUCCESS); \ | |
153 | } \ | |
154 | ||
32d29b35 | 155 | #define __noreturn __attribute__((noreturn)) |