blob: 871aa546eb6d7b8041f8e35cb486638296d23b08 [file] [log] [blame]
Andrew Scull18834872018-10-12 11:48:09 +01001/*
Andrew Walbran692b3252019-03-07 15:51:31 +00002 * Copyright 2018 The Hafnium Authors.
Andrew Scull18834872018-10-12 11:48:09 +01003 *
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
Andrew Walbranbc342d42019-02-05 16:56:02 +000017#include "hftest_common.h"
Andrew Scullbc7189d2018-08-14 09:35:13 +010018
Andrew Walbran4a53ba62019-03-05 17:26:12 +000019#include "hf/arch/std.h"
Andrew Scullf0551c82018-12-15 20:38:47 +000020#include "hf/arch/vm/power_mgmt.h"
21
Andrew Scullbc7189d2018-08-14 09:35:13 +010022#include "hf/memiter.h"
23
Andrew Walbranbc342d42019-02-05 16:56:02 +000024#include "hftest.h"
Andrew Scullbc7189d2018-08-14 09:35:13 +010025
26HFTEST_ENABLE();
27
Andrew Walbranbc342d42019-02-05 16:56:02 +000028static struct hftest_test hftest_constructed[HFTEST_MAX_TESTS];
29static size_t hftest_count;
30static struct hftest_test *hftest_list;
Andrew Scullbc7189d2018-08-14 09:35:13 +010031
Andrew Scullf0551c82018-12-15 20:38:47 +000032static struct hftest_context global_context;
33
34struct hftest_context *hftest_get_context(void)
35{
36 return &global_context;
37}
38
Andrew Walbranbc342d42019-02-05 16:56:02 +000039/**
40 * Adds the given test information to the global list, to be used by
41 * `hftest_use_registered_list`.
42 */
43void hftest_register(struct hftest_test test)
Andrew Scullbc7189d2018-08-14 09:35:13 +010044{
Andrew Walbranbc342d42019-02-05 16:56:02 +000045 if (hftest_count < HFTEST_MAX_TESTS) {
46 hftest_constructed[hftest_count++] = test;
47 } else {
48 HFTEST_FAIL("Too many tests", true);
49 }
50}
51
52/**
53 * Uses the list of tests registered by `hftest_register(...)` as the ones to
54 * run.
55 */
56void hftest_use_registered_list(void)
57{
58 hftest_list = hftest_constructed;
59}
60
61/**
62 * Uses the given list of tests as the ones to run.
63 */
64void hftest_use_list(struct hftest_test list[], size_t count)
65{
66 hftest_list = list;
67 hftest_count = count;
68}
69
70/**
71 * Writes out a JSON structure describing the available tests.
72 */
73void hftest_json(void)
74{
Andrew Scullbc7189d2018-08-14 09:35:13 +010075 const char *suite = NULL;
Andrew Walbranbc342d42019-02-05 16:56:02 +000076 size_t i;
Andrew Scullbc7189d2018-08-14 09:35:13 +010077 size_t suites_in_image = 0;
78 size_t tests_in_suite = 0;
79
80 HFTEST_LOG("{");
81 HFTEST_LOG(" \"suites\": [");
Andrew Walbranbc342d42019-02-05 16:56:02 +000082 for (i = 0; i < hftest_count; ++i) {
83 struct hftest_test *test = &hftest_list[i];
Andrew Scullbc7189d2018-08-14 09:35:13 +010084 if (test->suite != suite) {
85 /* Close out previously open suite. */
86 if (tests_in_suite) {
87 HFTEST_LOG(" ]");
88 HFTEST_LOG(" },");
89 }
90 /* Move onto new suite. */
91 ++suites_in_image;
92 suite = test->suite;
93 tests_in_suite = 0;
94 HFTEST_LOG(" {");
95 HFTEST_LOG(" \"name\": \"%s\",", test->suite);
96 }
97 if (test->kind == HFTEST_KIND_SET_UP) {
98 HFTEST_LOG(" \"setup\": true,");
99 }
100 if (test->kind == HFTEST_KIND_TEAR_DOWN) {
101 HFTEST_LOG(" \"teardown\": true,");
102 }
103 if (test->kind == HFTEST_KIND_TEST) {
104 if (!tests_in_suite) {
105 HFTEST_LOG(" \"tests\": [");
106 }
Andrew Scullf0551c82018-12-15 20:38:47 +0000107 /*
108 * It's easier to put the comma at the start of the line
Andrew Scullbc7189d2018-08-14 09:35:13 +0100109 * than the end even
Andrew Scullf0551c82018-12-15 20:38:47 +0000110 * though the JSON looks a bit funky.
111 */
Andrew Scullbc7189d2018-08-14 09:35:13 +0100112 HFTEST_LOG(" %c\"%s\"",
113 tests_in_suite ? ',' : ' ', test->name);
114 ++tests_in_suite;
115 }
116 }
117 if (tests_in_suite) {
118 HFTEST_LOG(" ]");
119 HFTEST_LOG(" }");
120 }
121 HFTEST_LOG(" ]");
122 HFTEST_LOG("}");
123}
124
Andrew Walbranbc342d42019-02-05 16:56:02 +0000125/**
126 * Logs a failure message and shut down.
127 */
Andrew Scullf0551c82018-12-15 20:38:47 +0000128static noreturn void abort(void)
129{
130 HFTEST_LOG("FAIL");
Andrew Walbranc6903d12019-03-05 18:28:20 +0000131 arch_power_off();
Andrew Scullf0551c82018-12-15 20:38:47 +0000132}
133
Andrew Scullbc7189d2018-08-14 09:35:13 +0100134static void run_test(hftest_test_fn set_up, hftest_test_fn test,
135 hftest_test_fn tear_down)
136{
Andrew Scullf0551c82018-12-15 20:38:47 +0000137 /* Prepare the context. */
138 struct hftest_context *ctx = hftest_get_context();
139 memset(ctx, 0, sizeof(*ctx));
140 ctx->abort = abort;
Andrew Scullbc7189d2018-08-14 09:35:13 +0100141
Andrew Scullf0551c82018-12-15 20:38:47 +0000142 /* Run any set up functions. */
Andrew Scullbc7189d2018-08-14 09:35:13 +0100143 if (set_up) {
Andrew Scullf0551c82018-12-15 20:38:47 +0000144 set_up();
145 if (ctx->failures) {
146 abort();
Andrew Scullbc7189d2018-08-14 09:35:13 +0100147 }
148 }
149
Andrew Scullf0551c82018-12-15 20:38:47 +0000150 /* Run the test. */
151 test();
152 if (ctx->failures) {
153 abort();
Andrew Scullbc7189d2018-08-14 09:35:13 +0100154 }
155
Andrew Scullf0551c82018-12-15 20:38:47 +0000156 /* Run any tear down functions. */
Andrew Scullbc7189d2018-08-14 09:35:13 +0100157 if (tear_down) {
Andrew Scullf0551c82018-12-15 20:38:47 +0000158 tear_down();
159 if (ctx->failures) {
160 abort();
Andrew Scullbc7189d2018-08-14 09:35:13 +0100161 }
162 }
163
Andrew Scullf0551c82018-12-15 20:38:47 +0000164 HFTEST_LOG("FINISHED");
Andrew Scullbc7189d2018-08-14 09:35:13 +0100165}
166
Andrew Walbranbc342d42019-02-05 16:56:02 +0000167/**
168 * Runs the given test case.
169 */
170void hftest_run(struct memiter suite_name, struct memiter test_name)
Andrew Scullbc7189d2018-08-14 09:35:13 +0100171{
Andrew Walbranbc342d42019-02-05 16:56:02 +0000172 size_t i;
Andrew Scullbc7189d2018-08-14 09:35:13 +0100173 bool found_suite = false;
174 const char *suite = NULL;
175 hftest_test_fn suite_set_up = NULL;
176 hftest_test_fn suite_tear_down = NULL;
177
Andrew Walbranbc342d42019-02-05 16:56:02 +0000178 for (i = 0; i < hftest_count; ++i) {
179 struct hftest_test *test = &hftest_list[i];
Andrew Scullbc7189d2018-08-14 09:35:13 +0100180 /* Find the test suite. */
181 if (found_suite) {
182 if (test->suite != suite) {
183 /* Test wasn't in the suite. */
184 break;
185 }
186 } else {
187 if (test->suite == suite) {
188 /* This isn't the right suite so keep going. */
189 continue;
190 }
191 /* Examine a new suite. */
192 suite = test->suite;
193 if (memiter_iseq(&suite_name, test->suite)) {
194 found_suite = true;
195 }
196 }
197
198 switch (test->kind) {
Andrew Scullf0551c82018-12-15 20:38:47 +0000199 /*
200 * The first entries in the suite are the set up and tear down
201 * functions.
202 */
Andrew Scullbc7189d2018-08-14 09:35:13 +0100203 case HFTEST_KIND_SET_UP:
204 suite_set_up = test->fn;
205 break;
206 case HFTEST_KIND_TEAR_DOWN:
207 suite_tear_down = test->fn;
208 break;
209 /* Find the test. */
210 case HFTEST_KIND_TEST:
211 if (memiter_iseq(&test_name, test->name)) {
212 run_test(suite_set_up, test->fn,
213 suite_tear_down);
214 return;
215 }
216 break;
Andrew Scullf0551c82018-12-15 20:38:47 +0000217 default:
218 /* Ignore other kinds. */
219 break;
Andrew Scullbc7189d2018-08-14 09:35:13 +0100220 }
221 }
222
223 HFTEST_LOG("Unable to find requested tests.");
224}
225
Andrew Walbranbc342d42019-02-05 16:56:02 +0000226/**
227 * Writes out usage information.
228 */
229void hftest_help(void)
Andrew Scullbc7189d2018-08-14 09:35:13 +0100230{
231 HFTEST_LOG("usage:");
232 HFTEST_LOG("");
233 HFTEST_LOG(" help");
234 HFTEST_LOG("");
235 HFTEST_LOG(" Show this help.");
236 HFTEST_LOG("");
237 HFTEST_LOG(" json");
238 HFTEST_LOG("");
239 HFTEST_LOG(
240 " Print a directory of test suites and tests in "
241 "JSON "
242 "format.");
243 HFTEST_LOG("");
244 HFTEST_LOG(" run <suite> <test>");
245 HFTEST_LOG("");
246 HFTEST_LOG(" Run the named test from the named test suite.");
247}