blob: 66d81f67eaf3370d8840657168dabb5df436803d [file] [log] [blame]
julhal013ec4c322021-02-05 17:30:49 +00001/*
Julian Hall8fdedb02022-09-08 15:44:13 +01002 * Copyright (c) 2021-2022, Arm Limited and Contributors. All rights reserved.
julhal013ec4c322021-02-05 17:30:49 +00003 *
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
14remote_test_runner::remote_test_runner() :
15 m_client(NULL)
16{
17
18}
19
20remote_test_runner::remote_test_runner(test_runner_client *client) :
21 m_client(client)
22{
23
24}
25
26remote_test_runner::~remote_test_runner()
27{
28
29}
30
31void remote_test_runner::set_client(test_runner_client *client)
32{
33 m_client = client;
34}
35
36int 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 Hall8fdedb02022-09-08 15:44:13 +010049 memset(&summary, 0, sizeof(summary));
julhal013ec4c322021-02-05 17:30:49 +000050
Julian Hall8fdedb02022-09-08 15:44:13 +010051 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);
julhal013ec4c322021-02-05 17:30:49 +000061 }
62 else {
63
julhal013ec4c322021-02-05 17:30:49 +000064 printf("Tests failed to run with error: %d\n", test_status);
65 }
66
67 return test_status;
68}
69
70void 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
82std::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
98bool 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
110void 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
121void remote_test_runner::output_list(const struct test_summary &summary,
122 const std::vector<struct test_result> &results)
123{
Julian Hall8fdedb02022-09-08 15:44:13 +0100124 for (int i = 0; i < results.size(); ++i) {
julhal013ec4c322021-02-05 17:30:49 +0000125
Julian Hall8fdedb02022-09-08 15:44:13 +0100126 printf("TEST(%s, %s)\n", results[i].group, results[i].name);
127 }
128
129 output_summary(summary);
julhal013ec4c322021-02-05 17:30:49 +0000130}
131
132void 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");
julhal0137e1aea2021-02-09 15:22:20 +0000146 printf("\tline number: %d\n", results[i].failure.line_num);
147 printf("\tinfo: 0x%016lx\n", results[i].failure.info);
julhal013ec4c322021-02-05 17:30:49 +0000148 }
149 else {
150
151 printf("did not run\n");
152 }
153 }
154
155 output_summary(summary);
156}