summaryrefslogtreecommitdiff
path: root/meta-arm/meta-arm/recipes-security/trusted-services/files/0020-Add-mock-for-libsp-sp_messaging.patch
blob: 3057051b6be6c9bd52206f8b5e4938ce1912b8ee (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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
From 1e5ce152214e22a7cd9617a5059e42c370351354 Mon Sep 17 00:00:00 2001
From: Imre Kis <imre.kis@arm.com>
Date: Fri, 17 Jun 2022 15:40:18 +0200
Subject: [PATCH 20/24] Add mock for libsp/sp_messaging

Add mock_sp_messaging for mocking sp_messaging part of libsp.

Signed-off-by: Imre Kis <imre.kis@arm.com>
Change-Id: I87478027c61b41028682b10e2535a7e14cf6922f

Upstream-Status: Pending [In review]
Signed-off-by: Anton Antonov <Anton.Antonov@arm.com>

---
 .../messaging/ffa/libsp/mock/component.cmake  |  1 +
 .../ffa/libsp/mock/mock_sp_messaging.cpp      | 86 +++++++++++++++++++
 .../ffa/libsp/mock/mock_sp_messaging.h        | 39 +++++++++
 .../mock/test/test_mock_sp_messaging.cpp      | 77 +++++++++++++++++
 components/messaging/ffa/libsp/tests.cmake    | 14 +++
 5 files changed, 217 insertions(+)
 create mode 100644 components/messaging/ffa/libsp/mock/mock_sp_messaging.cpp
 create mode 100644 components/messaging/ffa/libsp/mock/mock_sp_messaging.h
 create mode 100644 components/messaging/ffa/libsp/mock/test/test_mock_sp_messaging.cpp

diff --git a/components/messaging/ffa/libsp/mock/component.cmake b/components/messaging/ffa/libsp/mock/component.cmake
index eb0d28c..375cb46 100644
--- a/components/messaging/ffa/libsp/mock/component.cmake
+++ b/components/messaging/ffa/libsp/mock/component.cmake
@@ -14,6 +14,7 @@ target_sources(${TGT} PRIVATE
 	"${CMAKE_CURRENT_LIST_DIR}/mock_ffa_internal_api.cpp"
 	"${CMAKE_CURRENT_LIST_DIR}/mock_sp_discovery.cpp"
 	"${CMAKE_CURRENT_LIST_DIR}/mock_sp_memory_management.cpp"
+	"${CMAKE_CURRENT_LIST_DIR}/mock_sp_messaging.cpp"
 	"${CMAKE_CURRENT_LIST_DIR}/mock_sp_rxtx.cpp"
 	)
 
diff --git a/components/messaging/ffa/libsp/mock/mock_sp_messaging.cpp b/components/messaging/ffa/libsp/mock/mock_sp_messaging.cpp
new file mode 100644
index 0000000..522abb3
--- /dev/null
+++ b/components/messaging/ffa/libsp/mock/mock_sp_messaging.cpp
@@ -0,0 +1,86 @@
+// SPDX-License-Identifier: BSD-3-Clause
+/*
+ * Copyright (c) 2022, Arm Limited. All rights reserved.
+ */
+
+#include <CppUTestExt/MockSupport.h>
+#include "mock_sp_messaging.h"
+
+void expect_sp_msg_wait(const struct sp_msg *msg, sp_result result)
+{
+	mock()
+		.expectOneCall("sp_msg_wait")
+		.withOutputParameterReturning("msg", msg, sizeof(*msg))
+		.andReturnValue(result);
+}
+
+sp_result sp_msg_wait(struct sp_msg *msg)
+{
+	return mock()
+		.actualCall("sp_msg_wait")
+		.withOutputParameter("msg", msg)
+		.returnIntValue();
+}
+
+void expect_sp_msg_send_direct_req(const struct sp_msg *req,
+				   const struct sp_msg *resp,
+				   sp_result result)
+{
+	mock()
+		.expectOneCall("sp_msg_send_direct_req")
+		.withMemoryBufferParameter("req", (const unsigned char *)req, sizeof(*req))
+		.withOutputParameterReturning("resp", resp, sizeof(*resp))
+		.andReturnValue(result);
+}
+
+sp_result sp_msg_send_direct_req(const struct sp_msg *req, struct sp_msg *resp)
+{
+	return mock()
+		.actualCall("sp_msg_send_direct_req")
+		.withMemoryBufferParameter("req", (const unsigned char *)req, sizeof(*req))
+		.withOutputParameter("resp", resp)
+		.returnIntValue();
+}
+
+void expect_sp_msg_send_direct_resp(const struct sp_msg *resp,
+				    const struct sp_msg *req,
+				    sp_result result)
+{
+	mock()
+		.expectOneCall("sp_msg_send_direct_resp")
+		.withMemoryBufferParameter("resp", (const unsigned char *)resp, sizeof(*resp))
+		.withOutputParameterReturning("req", req, sizeof(*req))
+		.andReturnValue(result);
+}
+
+sp_result sp_msg_send_direct_resp(const struct sp_msg *resp,
+				  struct sp_msg *req)
+{
+	return mock()
+		.actualCall("sp_msg_send_direct_resp")
+		.withMemoryBufferParameter("resp", (const unsigned char *)resp, sizeof(*resp))
+		.withOutputParameter("req", req)
+		.returnIntValue();
+}
+
+#if FFA_DIRECT_MSG_ROUTING_EXTENSION
+void expect_sp_msg_send_rc_req(const struct sp_msg *req,
+			       const struct sp_msg *resp,
+			       sp_result result)
+{
+	mock()
+		.expectOneCall("sp_msg_send_rc_req")
+		.withMemoryBufferParameter("req", (const unsigned char *)req, sizeof(*req))
+		.withOutputParameterReturning("resp", resp, sizeof(*resp))
+		.andReturnValue(result);
+}
+
+sp_result sp_msg_send_rc_req(const struct sp_msg *req, struct sp_msg *resp)
+{
+	return mock()
+		.actualCall("sp_msg_send_rc_req")
+		.withMemoryBufferParameter("req", (const unsigned char *)req, sizeof(*req))
+		.withOutputParameter("resp", resp)
+		.returnIntValue();
+}
+#endif /* FFA_DIRECT_MSG_ROUTING_EXTENSION */
diff --git a/components/messaging/ffa/libsp/mock/mock_sp_messaging.h b/components/messaging/ffa/libsp/mock/mock_sp_messaging.h
new file mode 100644
index 0000000..8183012
--- /dev/null
+++ b/components/messaging/ffa/libsp/mock/mock_sp_messaging.h
@@ -0,0 +1,39 @@
+/* SPDX-License-Identifier: BSD-3-Clause */
+/*
+ * Copyright (c) 2022, Arm Limited and Contributors. All rights reserved.
+ */
+
+#ifndef LIBSP_MOCK_MOCK_SP_MESSAGING_H_
+#define LIBSP_MOCK_MOCK_SP_MESSAGING_H_
+
+#include "../include/sp_messaging.h"
+
+#include <stdint.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void expect_sp_msg_wait(const struct sp_msg *msg, sp_result result);
+
+
+void expect_sp_msg_send_direct_req(const struct sp_msg *req,
+				   const struct sp_msg *resp,
+				   sp_result result);
+
+
+void expect_sp_msg_send_direct_resp(const struct sp_msg *resp,
+				    const struct sp_msg *req,
+				    sp_result result);
+
+#if FFA_DIRECT_MSG_ROUTING_EXTENSION
+void expect_sp_msg_send_rc_req(const struct sp_msg *req,
+			       const struct sp_msg *resp,
+			       sp_result result);
+#endif /* FFA_DIRECT_MSG_ROUTING_EXTENSION */
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* LIBSP_MOCK_MOCK_SP_MESSAGING_H_ */
diff --git a/components/messaging/ffa/libsp/mock/test/test_mock_sp_messaging.cpp b/components/messaging/ffa/libsp/mock/test/test_mock_sp_messaging.cpp
new file mode 100644
index 0000000..bfc7959
--- /dev/null
+++ b/components/messaging/ffa/libsp/mock/test/test_mock_sp_messaging.cpp
@@ -0,0 +1,77 @@
+// SPDX-License-Identifier: BSD-3-Clause
+/*
+ * Copyright (c) 2022, Arm Limited. All rights reserved.
+ */
+
+#include <CppUTestExt/MockSupport.h>
+#include <CppUTest/TestHarness.h>
+#include "mock_sp_messaging.h"
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+
+static const struct sp_msg expected_req = {
+	.source_id = 0x0123,
+	.destination_id = 0x4567,
+	.args = {0x89abcdef, 0xfedcba98, 0x76543210, 0xabcdef01}
+};
+static const struct sp_msg expected_resp = {
+	.source_id = 0x1234,
+	.destination_id = 0x5678,
+	.args = {0x9abcdef8, 0xedcba98f, 0x65432107, 0xbcdef01a}
+};
+
+TEST_GROUP(mock_sp_messaging)
+{
+	TEST_SETUP()
+	{
+		memset(&req, 0x00, sizeof(req));
+		memset(&resp, 0x00, sizeof(resp));
+	}
+
+	TEST_TEARDOWN()
+	{
+		mock().checkExpectations();
+		mock().clear();
+	}
+
+	struct sp_msg req;
+	struct sp_msg resp;
+	static const sp_result result = -1;
+};
+
+TEST(mock_sp_messaging, sp_msg_wait)
+{
+	expect_sp_msg_wait(&expected_req, result);
+	LONGS_EQUAL(result, sp_msg_wait(&req));
+	MEMCMP_EQUAL(&expected_req, &req, sizeof(expected_req));
+}
+
+TEST(mock_sp_messaging, sp_msg_send_direct_req)
+{
+	req = expected_req;
+
+	expect_sp_msg_send_direct_req(&expected_req, &expected_resp, result);
+	LONGS_EQUAL(result, sp_msg_send_direct_req(&req, &resp));
+	MEMCMP_EQUAL(&expected_resp, &resp, sizeof(expected_resp));
+}
+
+TEST(mock_sp_messaging, sp_msg_send_direct_resp)
+{
+	resp = expected_resp;
+
+	expect_sp_msg_send_direct_resp(&expected_resp, &expected_req, result);
+	LONGS_EQUAL(result, sp_msg_send_direct_resp(&resp, &req));
+	MEMCMP_EQUAL(&expected_req, &req, sizeof(expected_req));
+}
+
+#if FFA_DIRECT_MSG_ROUTING_EXTENSION
+TEST(mock_sp_messaging, sp_msg_send_rc_req)
+{
+	req = expected_req;
+
+	expect_sp_msg_send_rc_req(&expected_req, &expected_resp, result);
+	LONGS_EQUAL(result, sp_msg_send_rc_req(&req, &resp));
+	MEMCMP_EQUAL(&expected_resp, &resp, sizeof(expected_resp));
+}
+#endif /* FFA_DIRECT_MSG_ROUTING_EXTENSION */
diff --git a/components/messaging/ffa/libsp/tests.cmake b/components/messaging/ffa/libsp/tests.cmake
index 63abb57..eb0b41e 100644
--- a/components/messaging/ffa/libsp/tests.cmake
+++ b/components/messaging/ffa/libsp/tests.cmake
@@ -176,6 +176,20 @@ unit_test_add_suite(
 		-DARM64
 )
 
+unit_test_add_suite(
+	NAME libsp_mock_sp_messaging
+	SOURCES
+		${CMAKE_CURRENT_LIST_DIR}/mock/test/test_mock_sp_messaging.cpp
+		${CMAKE_CURRENT_LIST_DIR}/mock/mock_sp_messaging.cpp
+	INCLUDE_DIRECTORIES
+		${CMAKE_CURRENT_LIST_DIR}/include/
+		${CMAKE_CURRENT_LIST_DIR}/mock
+		${UNIT_TEST_PROJECT_PATH}/components/common/utils/include
+	COMPILE_DEFINITIONS
+		-DARM64
+		-DFFA_DIRECT_MSG_ROUTING_EXTENSION=1
+)
+
 unit_test_add_suite(
 	NAME libsp_sp_messaging_with_routing_extension
 	SOURCES
-- 
2.17.1