julhal01 | 3ec4c32 | 2021-02-05 17:30:49 +0000 | [diff] [blame] | 1 | /* |
Julian Hall | 8fdedb0 | 2022-09-08 15:44:13 +0100 | [diff] [blame] | 2 | * Copyright (c) 2021-2022, Arm Limited and Contributors. All rights reserved. |
julhal01 | 3ec4c32 | 2021-02-05 17:30:49 +0000 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include "remote_test_runner.h" |
| 8 | #include <protocols/service/test_runner/packed-c/status.h> |
| 9 | #include <vector> |
| 10 | #include <string> |
| 11 | #include <cstring> |
| 12 | #include <cstdio> |
| 13 | |
| 14 | remote_test_runner::remote_test_runner() : |
| 15 | m_client(NULL) |
| 16 | { |
| 17 | |
| 18 | } |
| 19 | |
| 20 | remote_test_runner::remote_test_runner(test_runner_client *client) : |
| 21 | m_client(client) |
| 22 | { |
| 23 | |
| 24 | } |
| 25 | |
| 26 | remote_test_runner::~remote_test_runner() |
| 27 | { |
| 28 | |
| 29 | } |
| 30 | |
| 31 | void remote_test_runner::set_client(test_runner_client *client) |
| 32 | { |
| 33 | m_client = client; |
| 34 | } |
| 35 | |
| 36 | int remote_test_runner::execute(int argc, char *argv[]) |
| 37 | { |
| 38 | int test_status = TS_TEST_RUNNER_STATUS_ERROR; |
| 39 | struct test_spec spec; |
| 40 | |
| 41 | /* Parse command line parameters */ |
| 42 | bool list_only = option_selected("-l", argc, argv); |
| 43 | parse_test_spec_params(argc, argv, spec); |
| 44 | |
| 45 | /* Run or list tests qualified bu spec */ |
| 46 | struct test_summary summary; |
| 47 | std::vector<struct test_result> results; |
| 48 | |
Julian Hall | 8fdedb0 | 2022-09-08 15:44:13 +0100 | [diff] [blame] | 49 | memset(&summary, 0, sizeof(summary)); |
julhal01 | 3ec4c32 | 2021-02-05 17:30:49 +0000 | [diff] [blame] | 50 | |
Julian Hall | 8fdedb0 | 2022-09-08 15:44:13 +0100 | [diff] [blame] | 51 | test_status = (list_only) ? |
| 52 | m_client->list_tests(spec, summary, results) : |
| 53 | m_client->run_tests(spec, summary, results); |
| 54 | |
| 55 | if (test_status == TS_TEST_RUNNER_STATUS_SUCCESS) { |
| 56 | |
| 57 | if (list_only) |
| 58 | output_list(summary, results); |
| 59 | else |
| 60 | output_results(summary, results); |
julhal01 | 3ec4c32 | 2021-02-05 17:30:49 +0000 | [diff] [blame] | 61 | } |
| 62 | else { |
| 63 | |
julhal01 | 3ec4c32 | 2021-02-05 17:30:49 +0000 | [diff] [blame] | 64 | printf("Tests failed to run with error: %d\n", test_status); |
| 65 | } |
| 66 | |
| 67 | return test_status; |
| 68 | } |
| 69 | |
| 70 | void remote_test_runner::parse_test_spec_params(int argc, char *argv[], struct test_spec &spec) const |
| 71 | { |
| 72 | std::string name = parse_option("-n", argc, argv); |
| 73 | std::string group = parse_option("-g", argc, argv); |
| 74 | |
| 75 | memset(spec.name, 0, TEST_NAME_MAX_LEN); |
| 76 | name.copy(spec.name, TEST_NAME_MAX_LEN - 1); |
| 77 | |
| 78 | memset(spec.group, 0, TEST_GROUP_MAX_LEN); |
| 79 | group.copy(spec.group, TEST_GROUP_MAX_LEN - 1); |
| 80 | } |
| 81 | |
| 82 | std::string remote_test_runner::parse_option(const char *option_switch, int argc, char *argv[]) const |
| 83 | { |
| 84 | std::string option; |
| 85 | |
| 86 | for (int i = 1; i + 1 < argc; ++i) { |
| 87 | |
| 88 | if (strcmp(argv[i], option_switch) == 0) { |
| 89 | |
| 90 | option = std::string(argv[i +1]); |
| 91 | break; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | return option; |
| 96 | } |
| 97 | |
| 98 | bool remote_test_runner::option_selected(const char *option_switch, int argc, char *argv[]) const |
| 99 | { |
| 100 | bool selected = false; |
| 101 | |
| 102 | for (int i = 1; (i < argc) && !selected; ++i) { |
| 103 | |
| 104 | selected = (strcmp(argv[i], option_switch) == 0); |
| 105 | } |
| 106 | |
| 107 | return selected; |
| 108 | } |
| 109 | |
| 110 | void remote_test_runner::output_summary(const struct test_summary &summary) |
| 111 | { |
| 112 | printf("\n"); |
| 113 | |
| 114 | if (summary.num_failed == 0) printf("OK ("); |
| 115 | else printf("Errors (%d failures, ", summary.num_failed); |
| 116 | |
| 117 | printf("%d tests, %d ran)\n", summary.num_tests, summary.num_failed + summary.num_passed); |
| 118 | } |
| 119 | |
| 120 | |
| 121 | void remote_test_runner::output_list(const struct test_summary &summary, |
| 122 | const std::vector<struct test_result> &results) |
| 123 | { |
Julian Hall | 8fdedb0 | 2022-09-08 15:44:13 +0100 | [diff] [blame] | 124 | for (int i = 0; i < results.size(); ++i) { |
julhal01 | 3ec4c32 | 2021-02-05 17:30:49 +0000 | [diff] [blame] | 125 | |
Julian Hall | 8fdedb0 | 2022-09-08 15:44:13 +0100 | [diff] [blame] | 126 | printf("TEST(%s, %s)\n", results[i].group, results[i].name); |
| 127 | } |
| 128 | |
| 129 | output_summary(summary); |
julhal01 | 3ec4c32 | 2021-02-05 17:30:49 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | void remote_test_runner::output_results(const struct test_summary &summary, |
| 133 | const std::vector<struct test_result> &results) |
| 134 | { |
| 135 | for (int i = 0; i < results.size(); ++i) { |
| 136 | |
| 137 | printf("TEST(%s, %s) ", results[i].group, results[i].name); |
| 138 | |
| 139 | if (results[i].run_state == TEST_RUN_STATE_PASSED) { |
| 140 | |
| 141 | printf("OK\n"); |
| 142 | } |
| 143 | else if (results[i].run_state == TEST_RUN_STATE_FAILED) { |
| 144 | |
| 145 | printf("error\n"); |
julhal01 | 37e1aea | 2021-02-09 15:22:20 +0000 | [diff] [blame] | 146 | printf("\tline number: %d\n", results[i].failure.line_num); |
| 147 | printf("\tinfo: 0x%016lx\n", results[i].failure.info); |
julhal01 | 3ec4c32 | 2021-02-05 17:30:49 +0000 | [diff] [blame] | 148 | } |
| 149 | else { |
| 150 | |
| 151 | printf("did not run\n"); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | output_summary(summary); |
| 156 | } |