Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 1 | /* |
Andrew Walbran | 692b325 | 2019-03-07 15:51:31 +0000 | [diff] [blame] | 2 | * Copyright 2018 The Hafnium Authors. |
Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 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 | |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 17 | #include "hftest_common.h" |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 18 | |
Andrew Walbran | 4a53ba6 | 2019-03-05 17:26:12 +0000 | [diff] [blame] | 19 | #include "hf/arch/std.h" |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 20 | #include "hf/arch/vm/power_mgmt.h" |
| 21 | |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 22 | #include "hf/memiter.h" |
| 23 | |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 24 | #include "hftest.h" |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 25 | |
| 26 | HFTEST_ENABLE(); |
| 27 | |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 28 | static struct hftest_test hftest_constructed[HFTEST_MAX_TESTS]; |
| 29 | static size_t hftest_count; |
| 30 | static struct hftest_test *hftest_list; |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 31 | |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 32 | static struct hftest_context global_context; |
| 33 | |
| 34 | struct hftest_context *hftest_get_context(void) |
| 35 | { |
| 36 | return &global_context; |
| 37 | } |
| 38 | |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 39 | /** |
| 40 | * Adds the given test information to the global list, to be used by |
| 41 | * `hftest_use_registered_list`. |
| 42 | */ |
| 43 | void hftest_register(struct hftest_test test) |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 44 | { |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 45 | if (hftest_count < HFTEST_MAX_TESTS) { |
| 46 | hftest_constructed[hftest_count++] = test; |
| 47 | } else { |
Andrew Walbran | 78a63b7 | 2019-03-18 17:28:22 +0000 | [diff] [blame] | 48 | HFTEST_FAIL(true, "Too many tests"); |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Uses the list of tests registered by `hftest_register(...)` as the ones to |
| 54 | * run. |
| 55 | */ |
| 56 | void 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 | */ |
| 64 | void 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 | */ |
| 73 | void hftest_json(void) |
| 74 | { |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 75 | const char *suite = NULL; |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 76 | size_t i; |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 77 | size_t suites_in_image = 0; |
| 78 | size_t tests_in_suite = 0; |
| 79 | |
| 80 | HFTEST_LOG("{"); |
| 81 | HFTEST_LOG(" \"suites\": ["); |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 82 | for (i = 0; i < hftest_count; ++i) { |
| 83 | struct hftest_test *test = &hftest_list[i]; |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 84 | 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 Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 107 | /* |
| 108 | * It's easier to put the comma at the start of the line |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 109 | * than the end even |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 110 | * though the JSON looks a bit funky. |
| 111 | */ |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 112 | 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 Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 125 | /** |
| 126 | * Logs a failure message and shut down. |
| 127 | */ |
Andrew Scull | a59f9bc | 2019-04-03 15:24:35 +0100 | [diff] [blame^] | 128 | noreturn void abort(void) |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 129 | { |
| 130 | HFTEST_LOG("FAIL"); |
Andrew Walbran | c6903d1 | 2019-03-05 18:28:20 +0000 | [diff] [blame] | 131 | arch_power_off(); |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 134 | static void run_test(hftest_test_fn set_up, hftest_test_fn test, |
| 135 | hftest_test_fn tear_down) |
| 136 | { |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 137 | /* Prepare the context. */ |
| 138 | struct hftest_context *ctx = hftest_get_context(); |
| 139 | memset(ctx, 0, sizeof(*ctx)); |
| 140 | ctx->abort = abort; |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 141 | |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 142 | /* Run any set up functions. */ |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 143 | if (set_up) { |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 144 | set_up(); |
| 145 | if (ctx->failures) { |
| 146 | abort(); |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 150 | /* Run the test. */ |
| 151 | test(); |
| 152 | if (ctx->failures) { |
| 153 | abort(); |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 154 | } |
| 155 | |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 156 | /* Run any tear down functions. */ |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 157 | if (tear_down) { |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 158 | tear_down(); |
| 159 | if (ctx->failures) { |
| 160 | abort(); |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 164 | HFTEST_LOG("FINISHED"); |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 165 | } |
| 166 | |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 167 | /** |
| 168 | * Runs the given test case. |
| 169 | */ |
| 170 | void hftest_run(struct memiter suite_name, struct memiter test_name) |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 171 | { |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 172 | size_t i; |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 173 | 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 Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 178 | for (i = 0; i < hftest_count; ++i) { |
| 179 | struct hftest_test *test = &hftest_list[i]; |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 180 | /* 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 Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 199 | /* |
| 200 | * The first entries in the suite are the set up and tear down |
| 201 | * functions. |
| 202 | */ |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 203 | 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 Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 217 | default: |
| 218 | /* Ignore other kinds. */ |
| 219 | break; |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 220 | } |
| 221 | } |
| 222 | |
| 223 | HFTEST_LOG("Unable to find requested tests."); |
| 224 | } |
| 225 | |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame] | 226 | /** |
| 227 | * Writes out usage information. |
| 228 | */ |
| 229 | void hftest_help(void) |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 230 | { |
| 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 | } |