aboutsummaryrefslogtreecommitdiff
path: root/components/service/test_runner/client/cpp/test_runner_client.cpp
blob: a7800025541c813c077002e469686a50fa5c2d6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
 * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include "test_runner_client.h"
#include <protocols/rpc/common/packed-c/status.h>
#include <protocols/service/test_runner/packed-c/opcodes.h>
#include <protocols/service/test_runner/packed-c/status.h>
#include <protocols/service/test_runner/packed-c/run_tests.h>
#include <protocols/service/test_runner/packed-c/list_tests.h>
#include <rpc_caller.h>
#include <common/tlv/tlv.h>
#include <cstddef>
#include <cstring>
#include <string>

test_runner_client::test_runner_client() :
    m_caller(NULL),
    m_err_rpc_status(TS_RPC_CALL_ACCEPTED)
{

}

test_runner_client::test_runner_client(struct rpc_caller *caller) :
    m_caller(caller),
    m_err_rpc_status(TS_RPC_CALL_ACCEPTED)
{

}

test_runner_client::~test_runner_client()
{

}

void test_runner_client::set_caller(struct rpc_caller *caller)
{
    m_caller = caller;
}

int test_runner_client::err_rpc_status() const
{
    return m_err_rpc_status;
}

int test_runner_client::run_tests(
    const struct test_spec &spec,
    struct test_summary &summary,
    std::vector<struct test_result> &results)
{
    return iterate_over_tests(spec, false, summary, results);
}

int test_runner_client::list_tests(
    const struct test_spec &spec,
    struct test_summary &summary,
    std::vector<struct test_result> &results)
{
    return iterate_over_tests(spec, true, summary, results);
}

int test_runner_client::iterate_over_tests(
    const struct test_spec &spec, bool list_only,
    struct test_summary &summary,
    std::vector<struct test_result> &results)
{
    int test_status = TS_TEST_RUNNER_STATUS_ERROR;
    m_err_rpc_status = TS_RPC_ERROR_RESOURCE_FAILURE;
    rpc_call_handle call_handle;
    uint8_t *req_buf;
    std::vector<uint8_t> req_param;

    serialize_test_spec(req_param, spec);

    size_t req_len = req_param.size();
    call_handle = rpc_caller_begin(m_caller, &req_buf, req_len);

    if (call_handle) {

        uint8_t *resp_buf;
        size_t resp_len;
        int opstatus;

        memcpy(req_buf, req_param.data(), req_len);

        uint32_t opcode = (list_only) ?
            TS_TEST_RUNNER_OPCODE_LIST_TESTS :
            TS_TEST_RUNNER_OPCODE_RUN_TESTS;

        m_err_rpc_status = rpc_caller_invoke(m_caller, call_handle,
            opcode, &opstatus, &resp_buf, &resp_len);

        if (m_err_rpc_status == TS_RPC_CALL_ACCEPTED) {

            test_status = opstatus;

            if (opstatus == TS_TEST_RUNNER_STATUS_SUCCESS) {

                test_status = deserialize_results(resp_buf, resp_len, summary, results);
            }
        }

        rpc_caller_end(m_caller, call_handle);
    }

    return test_status;
}

void test_runner_client::serialize_test_spec(
    std::vector<uint8_t> &serialized_data,
    const struct test_spec &spec) const
{
    size_t name_len = strlen(spec.name);
    size_t group_len = strlen(spec.group);
    size_t tlv_space = 0;

    /* First determine buffer space needed for TLV parameters */
    if (name_len)  tlv_space += tlv_required_space(name_len);
    if (group_len) tlv_space += tlv_required_space(group_len);

    /* Extend the params vector and write the tlv records */
    if (tlv_space) {

        serialized_data.resize(tlv_space);
        struct tlv_iterator iter;
        uint8_t *buf = serialized_data.data();

        tlv_iterator_begin(&iter, buf, tlv_space);

        if (name_len) {

            struct tlv_record record;
            record.tag = TS_TEST_RUNNER_TEST_SPEC_TAG_NAME;
            record.length = name_len;
            record.value = (const uint8_t*)spec.name;
            tlv_encode(&iter, &record);
        }

        if (group_len) {

            struct tlv_record record;
            record.tag = TS_TEST_RUNNER_TEST_SPEC_TAG_GROUP;
            record.length = group_len;
            record.value = (const uint8_t*)spec.group;
            tlv_encode(&iter, &record);
        }
    }
}

int test_runner_client::deserialize_results(
    const uint8_t *resp_buf, size_t resp_len,
    struct test_summary &summary,
    std::vector<struct test_result> &results) const
{
    int test_status = TS_TEST_RUNNER_STATUS_SUCCESS;
    size_t fixed_size = sizeof(ts_test_runner_result_summary);

    if (resp_len >= fixed_size) {

        /* Deserialize fixed size summary structure */
        struct ts_test_runner_result_summary packed_summary;
        memcpy(&packed_summary, resp_buf, fixed_size);

        summary.num_tests = packed_summary.num_tests;
        summary.num_passed = packed_summary.num_passed;
        summary.num_failed = packed_summary.num_failed;
        summary.num_results = 0;

        /* Deserialize any test result records */
        struct tlv_const_iterator tlv_iter;
        struct tlv_record result_record;
        tlv_const_iterator_begin(&tlv_iter, &resp_buf[fixed_size], resp_len - fixed_size);

        while (tlv_find_decode(&tlv_iter, TS_TEST_RUNNER_TEST_RESULT_TAG, &result_record)) {

            struct test_result result;

            test_status = deserialize_result(result_record.value, result_record.length, result);

            if (test_status == TS_TEST_RUNNER_STATUS_SUCCESS) {

                results.push_back(result);
                ++summary.num_results;
            }
            else {
                /* Failed to decode result record */
                break;
            }
        }
    }
    else {
        /* Failed to mandatory test summary */
        test_status = TS_TEST_RUNNER_STATUS_INVALID_TEST_RESULTS;
    }

    return test_status;
}

int test_runner_client::deserialize_result(
    const uint8_t *value_buf, size_t value_len,
    struct test_result &result) const
{
    int test_status = TS_TEST_RUNNER_STATUS_SUCCESS;
    size_t fixed_size = sizeof(ts_test_runner_test_result);

    if (value_len >= fixed_size) {

        struct ts_test_runner_test_result packed_result;
        memcpy(&packed_result, value_buf, fixed_size);

        result.run_state = (enum test_run_state)packed_result.run_state;
        result.fail_line = packed_result.fail_line;
        result.name[0] = 0;
        result.group[0] = 0;

        /* Deserialize name and group if present */
        struct tlv_const_iterator req_iter;
        struct tlv_record decoded_record;

        tlv_const_iterator_begin(&req_iter, &value_buf[fixed_size], value_len - fixed_size);

        if (tlv_find_decode(&req_iter, TS_TEST_RUNNER_TEST_RESULT_TAG_NAME, &decoded_record)) {

            if ((decoded_record.length > 0) && (decoded_record.length < TEST_NAME_MAX_LEN)) {

                memcpy(result.name, decoded_record.value, decoded_record.length);
                result.name[decoded_record.length] = 0;
            }
        }

        if (tlv_find_decode(&req_iter, TS_TEST_RUNNER_TEST_RESULT_TAG_GROUP, &decoded_record)) {

            if ((decoded_record.length > 0) && (decoded_record.length < TEST_GROUP_MAX_LEN)) {

                memcpy(result.group, decoded_record.value, decoded_record.length);
                result.group[decoded_record.length] = 0;
            }
        }
    }
    else {
        /* Invalid test result */
        test_status = TS_TEST_RUNNER_STATUS_INVALID_TEST_RESULTS;
    }

    return test_status;
}