1 /*
2 * Copyright (c) 2023, Arm Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7 #include <CppUTestExt/MockSupport.h>
8 #include "rpmb_backend_mock.h"
9
rpmb_backend_mock_expect_get_dev_info(void * context,uint32_t dev_id,const struct rpmb_dev_info * dev_info,psa_status_t result)10 void rpmb_backend_mock_expect_get_dev_info(void *context, uint32_t dev_id,
11 const struct rpmb_dev_info *dev_info,
12 psa_status_t result)
13 {
14 mock().expectOneCall("get_dev_info").
15 onObject(context).
16 withUnsignedIntParameter("dev_id", dev_id).
17 withOutputParameterReturning("dev_info", dev_info, sizeof(*dev_info)).
18 andReturnValue(result);
19 }
20
rpmb_backend_mock_get_dev_info(void * context,uint32_t dev_id,struct rpmb_dev_info * dev_info)21 static psa_status_t rpmb_backend_mock_get_dev_info(void *context, uint32_t dev_id,
22 struct rpmb_dev_info *dev_info)
23 {
24 return mock().actualCall("get_dev_info").
25 onObject(context).
26 withUnsignedIntParameter("dev_id", dev_id).
27 withOutputParameter("dev_info", dev_info).
28 returnIntValue();
29 }
30
rpmb_backend_mock_expect_data_request(void * context,uint32_t dev_id,const struct rpmb_data_frame * request_frames,size_t request_frame_count,const struct rpmb_data_frame * response_frames,size_t response_frame_count_in,size_t * response_frame_count_out,psa_status_t result)31 void rpmb_backend_mock_expect_data_request(
32 void *context, uint32_t dev_id, const struct rpmb_data_frame *request_frames,
33 size_t request_frame_count, const struct rpmb_data_frame *response_frames,
34 size_t response_frame_count_in, size_t *response_frame_count_out, psa_status_t result)
35 {
36 size_t request_size = sizeof(*request_frames) * request_frame_count;
37 size_t response_size = sizeof(*response_frames) * (*response_frame_count_out);
38
39 mock().expectOneCall("data_request").
40 onObject(context).
41 withUnsignedIntParameter("dev_id", dev_id).
42 withMemoryBufferParameter("request_frames", (const unsigned char *)request_frames,
43 request_size).
44 withUnsignedIntParameter("request_frame_count", request_frame_count).
45 withOutputParameterReturning("response_frames", response_frames, response_size).
46 withUnsignedIntParameter("response_frame_count_in", response_frame_count_in).
47 withOutputParameterReturning("response_frame_count_out", response_frame_count_out,
48 sizeof(*response_frame_count_out)).
49 andReturnValue(result);
50 }
51
rpmb_backend_mock_data_request(void * context,uint32_t dev_id,const struct rpmb_data_frame * request_frames,size_t request_frame_count,struct rpmb_data_frame * response_frames,size_t * response_frame_count)52 static psa_status_t rpmb_backend_mock_data_request(
53 void *context, uint32_t dev_id, const struct rpmb_data_frame *request_frames,
54 size_t request_frame_count, struct rpmb_data_frame *response_frames,
55 size_t *response_frame_count)
56 {
57 size_t request_size = sizeof(*request_frames) * request_frame_count;
58
59 return mock().actualCall("data_request").
60 onObject(context).
61 withUnsignedIntParameter("dev_id", dev_id).
62 withMemoryBufferParameter("request_frames", (const unsigned char *)request_frames,
63 request_size).
64 withUnsignedIntParameter("request_frame_count", request_frame_count).
65 withOutputParameter("response_frames", response_frames).
66 withUnsignedIntParameter("response_frame_count_in", *response_frame_count).
67 withOutputParameter("response_frame_count_out", response_frame_count).
68 returnIntValue();
69 }
70
rpmb_backend_mock_init(struct rpmb_backend_mock * context)71 struct rpmb_backend *rpmb_backend_mock_init(struct rpmb_backend_mock *context)
72 {
73 static const struct rpmb_backend_interface interface = {
74 rpmb_backend_mock_get_dev_info,
75 rpmb_backend_mock_data_request,
76 };
77
78 if (!context)
79 return NULL;
80
81 context->backend.context = context;
82 context->backend.interface = &interface;
83
84 return &context->backend;
85 }
86
rpmb_backend_mock_deinit(struct rpmb_backend_mock * context)87 void rpmb_backend_mock_deinit(struct rpmb_backend_mock *context)
88 {
89 *context = (struct rpmb_backend_mock){ 0 };
90 }
91