blob: fa9e4c7625cddd4876f4117839c0f6994f4c6a03 [file] [log] [blame]
Andrew Scullf0551c82018-12-15 20:38:47 +00001/*
2 * Copyright 2018 Google LLC
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <stdnoreturn.h>
20
21/*
22 * Log with the HFTEST_LOG_PREFIX and a new line. The zero is added so there is
23 * always at least one variadic argument.
24 */
25#define HFTEST_LOG(...) HFTEST_LOG_IMPL(__VA_ARGS__, 0)
26#define HFTEST_LOG_IMPL(format, ...) \
27 dlog("%s" format "\n", HFTEST_LOG_PREFIX, __VA_ARGS__)
28
29/* Helper to wrap the argument in quotes. */
30#define HFTEST_STR(str) #str
31
32/*
33 * Sections are names such that when the linker sorts them, all entries for the
34 * same test suite are contiguous and the set up and tear down entries come
35 * before the tests. This order simplifies test discovery in the running image.
36 */
37#define HFTEST_SET_UP_SECTION(suite_name) \
38 HFTEST_STR(.hftest.suite.suite_name .1set_up)
39#define HFTEST_TEAR_DOWN_SECTION(suite_name) \
40 HFTEST_STR(.hftest.suite.suite_name .1tear_down)
41#define HFTEST_TEST_SECTION(suite_name, test_name) \
42 HFTEST_STR(.hftest.suite.suite_name .2test.test_name)
43#define HFTEST_SERVICE_SECTION(service_name) \
44 HFTEST_STR(.hftest.service.service_name)
45
46/* Helpers to construct unique identifiers. */
47#define HFTEST_SET_UP_STRUCT(suite_name) hftest_set_up_##suite_name
48#define HFTEST_TEAR_DOWN_STRUCT(suite_name) hftest_tear_down_##suite_name
49#define HFTEST_TEST_STRUCT(suite_name, test_name) \
50 hftest_test_##suite_name##_##test_name
51#define HFTEST_SERVICE_STRUCT(service_name) hftest_service_##service_name
52
53#define HFTEST_SET_UP_FN(suite_name) hftest_set_up_fn_##suite_name
54#define HFTEST_TEAR_DOWN_FN(suite_name) hftest_tear_down_fn_##suite_name
55#define HFTEST_TEST_FN(suite_name, test_name) \
56 hftest_test_fn_##suite_name##_##test_name
57#define HFTEST_SERVICE_FN(service_name) hftest_service_fn_##service_name
58
59/* Register test functions. */
60#define HFTEST_SET_UP(suite_name) \
61 static void HFTEST_SET_UP_FN(suite_name)(void); \
62 const struct hftest_test __attribute__((used)) \
63 __attribute__((section(HFTEST_SET_UP_SECTION(suite_name)))) \
64 HFTEST_SET_UP_STRUCT(suite_name) = { \
65 .suite = #suite_name, \
66 .kind = HFTEST_KIND_SET_UP, \
67 .fn = HFTEST_SET_UP_FN(suite_name), \
68 }; \
69 static void HFTEST_SET_UP_FN(suite_name)(void)
70
71#define HFTEST_TEAR_DOWN(suite_name) \
72 static void HFTEST_TEAR_DOWN_FN(suite_name)(void); \
73 const struct hftest_test __attribute__((used)) \
74 __attribute__((section(HFTEST_TEAR_DOWN_SECTION(suite_name)))) \
75 HFTEST_TEAR_DOWN_STRUCT(suite_name) = { \
76 .suite = #suite_name, \
77 .kind = HFTEST_KIND_TEAR_DOWN, \
78 .fn = HFTEST_TEAR_DOWN_FN(suite_name), \
79 }; \
80 static void HFTEST_TEAR_DOWN_FN(suite_name)(void)
81
82#define HFTEST_TEST(suite_name, test_name) \
83 static void HFTEST_TEST_FN(suite_name, test_name)(void); \
84 const struct hftest_test __attribute__((used)) __attribute__( \
85 (section(HFTEST_TEST_SECTION(suite_name, test_name)))) \
86 HFTEST_TEST_STRUCT(suite_name, test_name) = { \
87 .suite = #suite_name, \
88 .kind = HFTEST_KIND_TEST, \
89 .name = #test_name, \
90 .fn = HFTEST_TEST_FN(suite_name, test_name), \
91 }; \
92 static void HFTEST_TEST_FN(suite_name, test_name)(void)
93
94#define HFTEST_TEST_SERVICE(service_name) \
95 static void HFTEST_SERVICE_FN(service_name)(void); \
96 const struct hftest_test __attribute__((used)) \
97 __attribute__((section(HFTEST_SERVICE_SECTION(service_name)))) \
98 HFTEST_SERVICE_STRUCT(service_name) = { \
99 .kind = HFTEST_KIND_SERVICE, \
100 .name = #service_name, \
101 .fn = HFTEST_SERVICE_FN(service_name), \
102 }; \
103 static void HFTEST_SERVICE_FN(service_name)(void)
104
105/* Context for tests. */
106struct hftest_context {
107 uint32_t failures;
108 noreturn void (*abort)(void);
109
110 /* These are used in services. */
111 void *send;
112 void *recv;
113};
114
115struct hftest_context *hftest_get_context(void);
116
117/* A test case. */
118typedef void (*hftest_test_fn)(void);
119
120enum hftest_kind {
121 HFTEST_KIND_SET_UP = 0,
122 HFTEST_KIND_TEST = 1,
123 HFTEST_KIND_TEAR_DOWN = 2,
124 HFTEST_KIND_SERVICE = 3,
125};
126
127/**
128 * The .hftest section contains an array of this struct which describes the test
129 * functions contained in the image allowing the image to inspect the tests it
130 * contains.
131 */
132struct hftest_test {
133 const char *suite;
134 enum hftest_kind kind;
135 const char *name;
136 hftest_test_fn fn;
137};
138
139/*
140 * This union can store any of the primitive types supported by the assertion
141 * macros.
142 *
143 * It does not include pointers as comparison of pointers is not often needed
144 * and could be a mistake for string comparison. If pointer comparison is needed
145 * and explicit assertion such as ASSERT_PTR_EQ() would be more appropriate.
146 */
147union hftest_any {
148 bool b;
149 char c;
150 signed char sc;
151 unsigned char uc;
152 signed short ss;
153 unsigned short us;
154 signed int si;
155 unsigned int ui;
156 signed long int sli;
157 unsigned long int uli;
158 signed long long int slli;
159 unsigned long long int ulli;
160};
161
162/* _Generic formatting doesn't seem to be supported so doing this manually. */
163/* clang-format off */
164
165/* Select the union member to match the type of the expression. */
166#define hftest_any_get(any, x) \
167 _Generic((x), \
168 bool: (any).b, \
169 char: (any).c, \
170 signed char: (any).sc, \
171 unsigned char: (any).uc, \
172 signed short: (any).ss, \
173 unsigned short: (any).us, \
174 signed int: (any).si, \
175 unsigned int: (any).ui, \
176 signed long int: (any).sli, \
177 unsigned long int: (any).uli, \
178 signed long long int: (any).slli, \
179 unsigned long long int: (any).ulli)
180
181/*
182 * dlog format specifier for types. Note, these aren't the standard specifiers
183 * for the types.
184 */
185#define hftest_dlog_format(x) \
186 _Generic((x), \
187 bool: "%u", \
188 char: "%c", \
189 signed char: "%d", \
190 unsigned char: "%u", \
191 signed short: "%d", \
192 unsigned short: "%u", \
193 signed int: "%d", \
194 unsigned int: "%u", \
195 signed long int: "%d", \
196 unsigned long int: "%u", \
197 signed long long int: "%d", \
198 unsigned long long int: "%u")
199
200/* clang-format on */
201
202#define HFTEST_LOG_FAILURE() \
203 dlog(HFTEST_LOG_PREFIX "Failure: %s:%u\n", __FILE__, __LINE__);
204
205#define HFTEST_ASSERT_OP(lhs, rhs, op, fatal) \
206 do { \
207 union hftest_any lhs_value; \
208 union hftest_any rhs_value; \
209 hftest_any_get(lhs_value, lhs) = (lhs); \
210 hftest_any_get(rhs_value, rhs) = (rhs); \
211 if (!(hftest_any_get(lhs_value, lhs) \
212 op hftest_any_get(rhs_value, rhs))) { \
213 struct hftest_context *ctx = hftest_get_context(); \
214 ++ctx->failures; \
215 HFTEST_LOG_FAILURE(); \
216 dlog(HFTEST_LOG_PREFIX HFTEST_LOG_INDENT \
217 "%s %s %s (%s=", \
218 #lhs, #op, #rhs, #lhs); \
219 dlog(hftest_dlog_format(lhs), \
220 hftest_any_get(lhs_value, lhs)); \
221 dlog(", %s=", #rhs); \
222 dlog(hftest_dlog_format(rhs), \
223 hftest_any_get(rhs_value, rhs)); \
224 dlog(")\n"); \
225 if (fatal) { \
226 ctx->abort(); \
227 } \
228 } \
229 } while (0)
230
Andrew Walbranb90daf12018-12-11 14:25:54 +0000231#define HFTEST_FAIL(message, fatal) \
232 do { \
233 struct hftest_context *ctx = hftest_get_context(); \
234 ++ctx->failures; \
235 HFTEST_LOG_FAILURE(); \
236 dlog(HFTEST_LOG_PREFIX HFTEST_LOG_INDENT "%s\n", message); \
237 if (fatal) { \
238 ctx->abort(); \
239 } \
240 } while (0)
241
Andrew Scullf0551c82018-12-15 20:38:47 +0000242/**
243 * Select the service to run in a service VM.
244 */
245#define HFTEST_SERVICE_SELECT(vm_id, service, send_buffer) \
246 do { \
247 struct hf_vcpu_run_return run_res; \
248 \
249 /* \
250 * Let the service configure its mailbox and wait for a \
251 * message. \
252 */ \
253 run_res = hf_vcpu_run(vm_id, 0); \
254 ASSERT_EQ(run_res.code, HF_VCPU_RUN_WAIT_FOR_INTERRUPT); \
255 \
256 /* Send the selected service to run and let it be handled. */ \
257 memcpy(send_buffer, service, strlen(service)); \
Wedson Almeida Filho17c997f2019-01-09 18:50:09 +0000258 ASSERT_EQ(hf_mailbox_send(vm_id, strlen(service), false), 0); \
Andrew Scullf0551c82018-12-15 20:38:47 +0000259 run_res = hf_vcpu_run(vm_id, 0); \
260 ASSERT_EQ(run_res.code, HF_VCPU_RUN_YIELD); \
261 } while (0)
262
263#define HFTEST_SERVICE_SEND_BUFFER() hftest_get_context()->send
264#define HFTEST_SERVICE_RECV_BUFFER() hftest_get_context()->recv