1 // SPDX-License-Identifier: BSD-3-Clause
2 /*
3 * Copyright (c) 2020-2022, Arm Limited. All rights reserved.
4 */
5
6 #include <assert.h>
7 #include <CppUTest/TestHarness.h>
8 #include <CppUTestExt/MockSupport.h>
9 #include "mock_ffa_api.h"
10
11 const struct ffa_direct_msg expected_msg = { 0xffeeddcc, 0xaabb, 0x8899,
12 0x12345678, 0x23456789, 0x3456789a,
13 0x456789ab, 0x56789abc };
14
TEST_GROUP(mock_ffa_api)15 TEST_GROUP(mock_ffa_api)
16 {
17 TEST_TEARDOWN()
18 {
19 mock().checkExpectations();
20 mock().clear();
21 }
22
23 static const ffa_result result = -1;
24 };
25
TEST(mock_ffa_api,ffa_version)26 TEST(mock_ffa_api, ffa_version)
27 {
28 const uint32_t expected_version = 0x12345678U;
29 uint32_t version = 0;
30
31 expect_ffa_version(&expected_version, result);
32 LONGS_EQUAL(result, ffa_version(&version));
33 UNSIGNED_LONGS_EQUAL(expected_version, version);
34 }
35
TEST(mock_ffa_api,ffa_features)36 TEST(mock_ffa_api, ffa_features)
37 {
38 const uint32_t ffa_function_id = 0xfedcba98U;
39 const struct ffa_interface_properties expected_interface_properties = {
40 0xaabbccdd, 0x44556677
41 };
42 struct ffa_interface_properties interface_properties = { 0 };
43
44 expect_ffa_features(ffa_function_id, &expected_interface_properties,
45 result);
46 LONGS_EQUAL(result,
47 ffa_features(ffa_function_id, &interface_properties));
48 MEMCMP_EQUAL(&expected_interface_properties, &interface_properties,
49 sizeof(expected_interface_properties));
50 }
51
TEST(mock_ffa_api,ffa_rx_release)52 TEST(mock_ffa_api, ffa_rx_release)
53 {
54 expect_ffa_rx_release(result);
55 LONGS_EQUAL(result, ffa_rx_release());
56 }
57
TEST(mock_ffa_api,ffa_rxtx_map)58 TEST(mock_ffa_api, ffa_rxtx_map)
59 {
60 const char tx_buffer = 0;
61 const char rx_buffer = 0;
62 const uint32_t page_count = 0x89abcdefU;
63
64 expect_ffa_rxtx_map(&tx_buffer, &rx_buffer, page_count, result);
65 LONGS_EQUAL(result, ffa_rxtx_map(&tx_buffer, &rx_buffer, page_count));
66 }
67
TEST(mock_ffa_api,ffa_rxtx_unmap)68 TEST(mock_ffa_api, ffa_rxtx_unmap)
69 {
70 const uint16_t id = 0xffee;
71
72 expect_ffa_rxtx_unmap(id, result);
73 LONGS_EQUAL(result, ffa_rxtx_unmap(id));
74 }
75
76 #if CFG_FFA_VERSION == FFA_VERSION_1_0
TEST(mock_ffa_api,ffa_partition_info_get_v1_0)77 TEST(mock_ffa_api, ffa_partition_info_get_v1_0)
78 {
79 const struct ffa_uuid uuid = { 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54,
80 0x32, 0x10, 0x01, 0x23, 0x45, 0x67,
81 0x89, 0xab, 0xcd, 0xef };
82 const uint32_t expected_count = 0xff00ee11U;
83 uint32_t count = 0;
84
85 expect_ffa_partition_info_get(&uuid, &expected_count, result);
86 LONGS_EQUAL(result, ffa_partition_info_get(&uuid, &count));
87 UNSIGNED_LONGS_EQUAL(expected_count, count);
88 }
89 #elif CFG_FFA_VERSION >= FFA_VERSION_1_1
TEST(mock_ffa_api,ffa_partition_info_get_v1_1)90 TEST(mock_ffa_api, ffa_partition_info_get_v1_1)
91 {
92 const struct ffa_uuid uuid = { 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54,
93 0x32, 0x10, 0x01, 0x23, 0x45, 0x67,
94 0x89, 0xab, 0xcd, 0xef };
95 const uint32_t flags = 0xabcdef01;
96 const uint32_t expected_count = 0xff00ee11U;
97 const uint32_t expected_size = 0xff00ee11U;
98 uint32_t count = 0;
99 uint32_t size = 0;
100
101 expect_ffa_partition_info_get(&uuid, flags, &expected_count, &expected_size, result);
102 LONGS_EQUAL(result, ffa_partition_info_get(&uuid, flags, &count, &size));
103 UNSIGNED_LONGS_EQUAL(expected_count, count);
104 UNSIGNED_LONGS_EQUAL(expected_size, size);
105 }
106 #endif /* CFG_FFA_VERSION */
107
TEST(mock_ffa_api,ffa_id_get)108 TEST(mock_ffa_api, ffa_id_get)
109 {
110 const uint16_t expected_id = 0xabcd;
111 uint16_t id = 0;
112
113 expect_ffa_id_get(&expected_id, result);
114 LONGS_EQUAL(result, ffa_id_get(&id));
115 UNSIGNED_LONGS_EQUAL(expected_id, id);
116 }
117
TEST(mock_ffa_api,ffa_msg_wait)118 TEST(mock_ffa_api, ffa_msg_wait)
119 {
120 struct ffa_direct_msg msg = { 0 };
121
122 expect_ffa_msg_wait(&expected_msg, result);
123 LONGS_EQUAL(result, ffa_msg_wait(&msg));
124 MEMCMP_EQUAL(&expected_msg, &msg, sizeof(expected_msg));
125 }
126
TEST(mock_ffa_api,ffa_msg_send_direct_req_32)127 TEST(mock_ffa_api, ffa_msg_send_direct_req_32)
128 {
129 const uint16_t source = 0x1122;
130 const uint16_t dest = 0x2233;
131 const uint32_t a0 = 0x45678912;
132 const uint32_t a1 = 0x56789124;
133 const uint32_t a2 = 0x67891245;
134 const uint32_t a3 = 0x78912456;
135 const uint32_t a4 = 0x89124567;
136 struct ffa_direct_msg msg = { 0 };
137
138 expect_ffa_msg_send_direct_req_32(source, dest, a0, a1, a2, a3, a4,
139 &expected_msg, result);
140 LONGS_EQUAL(result, ffa_msg_send_direct_req_32(source, dest, a0, a1, a2,
141 a3, a4, &msg));
142 }
143
TEST(mock_ffa_api,ffa_msg_send_direct_req_64)144 TEST(mock_ffa_api, ffa_msg_send_direct_req_64)
145 {
146 const uint16_t source = 0x1122;
147 const uint16_t dest = 0x2233;
148 const uint64_t a0 = 0x4567891221987654;
149 const uint64_t a1 = 0x5678912442198765;
150 const uint64_t a2 = 0x6789124554219876;
151 const uint64_t a3 = 0x7891245665421987;
152 const uint64_t a4 = 0x8912456776542198;
153 struct ffa_direct_msg msg = { 0 };
154
155 expect_ffa_msg_send_direct_req_64(source, dest, a0, a1, a2, a3, a4,
156 &expected_msg, result);
157 LONGS_EQUAL(result, ffa_msg_send_direct_req_64(source, dest, a0, a1, a2,
158 a3, a4, &msg));
159 }
160
TEST(mock_ffa_api,ffa_msg_send_direct_resp_32)161 TEST(mock_ffa_api, ffa_msg_send_direct_resp_32)
162 {
163 const uint16_t source = 0x1122;
164 const uint16_t dest = 0x2233;
165 const uint32_t a0 = 0x45678912;
166 const uint32_t a1 = 0x56789124;
167 const uint32_t a2 = 0x67891245;
168 const uint32_t a3 = 0x78912456;
169 const uint32_t a4 = 0x89124567;
170 struct ffa_direct_msg msg = { 0 };
171
172 expect_ffa_msg_send_direct_resp_32(source, dest, a0, a1, a2, a3, a4,
173 &expected_msg, result);
174 LONGS_EQUAL(result, ffa_msg_send_direct_resp_32(source, dest, a0, a1,
175 a2, a3, a4, &msg));
176 }
177
TEST(mock_ffa_api,ffa_msg_send_direct_resp_64)178 TEST(mock_ffa_api, ffa_msg_send_direct_resp_64)
179 {
180 const uint16_t source = 0x1122;
181 const uint16_t dest = 0x2233;
182 const uint64_t a0 = 0x4567891221987654;
183 const uint64_t a1 = 0x5678912442198765;
184 const uint64_t a2 = 0x6789124554219876;
185 const uint64_t a3 = 0x7891245665421987;
186 const uint64_t a4 = 0x8912456776542198;
187 struct ffa_direct_msg msg = { 0 };
188
189 expect_ffa_msg_send_direct_resp_64(source, dest, a0, a1, a2, a3, a4,
190 &expected_msg, result);
191 LONGS_EQUAL(result, ffa_msg_send_direct_resp_64(source, dest, a0, a1,
192 a2, a3, a4, &msg));
193 }
194
TEST(mock_ffa_api,ffa_mem_donate)195 TEST(mock_ffa_api, ffa_mem_donate)
196 {
197 const uint32_t total_length = 0x12345678U;
198 const uint32_t fragment_length = 0x23456789U;
199 const char buffer = 0;
200 const uint32_t page_count = 0x3456789au;
201 const uint64_t expected_handle = 0xfedcba9876543210ULL;
202 uint64_t handle = 0;
203
204 expect_ffa_mem_donate(total_length, fragment_length, (void *)&buffer,
205 page_count, &expected_handle, result);
206 LONGS_EQUAL(result,
207 ffa_mem_donate(total_length, fragment_length,
208 (void *)&buffer, page_count, &handle));
209 UNSIGNED_LONGLONGS_EQUAL(expected_handle, handle);
210 }
211
TEST(mock_ffa_api,ffa_mem_donate_rxtx)212 TEST(mock_ffa_api, ffa_mem_donate_rxtx)
213 {
214 const uint32_t total_length = 0x12345678U;
215 const uint32_t fragment_length = 0x23456789U;
216 const uint64_t expected_handle = 0xfedcba9876543210ULL;
217 uint64_t handle = 0;
218
219 expect_ffa_mem_donate_rxtx(total_length, fragment_length,
220 &expected_handle, result);
221 LONGS_EQUAL(result, ffa_mem_donate_rxtx(total_length, fragment_length,
222 &handle));
223 UNSIGNED_LONGLONGS_EQUAL(expected_handle, handle);
224 }
225
TEST(mock_ffa_api,ffa_mem_lend)226 TEST(mock_ffa_api, ffa_mem_lend)
227 {
228 const uint32_t total_length = 0x12345678U;
229 const uint32_t fragment_length = 0x23456789U;
230 const char buffer = 0;
231 const uint32_t page_count = 0x3456789au;
232 const uint64_t expected_handle = 0xfedcba9876543210ULL;
233 uint64_t handle = 0;
234
235 expect_ffa_mem_lend(total_length, fragment_length, (void *)&buffer,
236 page_count, &expected_handle, result);
237 LONGS_EQUAL(result, ffa_mem_lend(total_length, fragment_length,
238 (void *)&buffer, page_count, &handle));
239 UNSIGNED_LONGLONGS_EQUAL(expected_handle, handle);
240 }
241
TEST(mock_ffa_api,ffa_mem_lend_rxtx)242 TEST(mock_ffa_api, ffa_mem_lend_rxtx)
243 {
244 const uint32_t total_length = 0x12345678U;
245 const uint32_t fragment_length = 0x23456789U;
246 const uint64_t expected_handle = 0xfedcba9876543210ULL;
247 uint64_t handle = 0;
248
249 expect_ffa_mem_lend_rxtx(total_length, fragment_length,
250 &expected_handle, result);
251 LONGS_EQUAL(result,
252 ffa_mem_lend_rxtx(total_length, fragment_length, &handle));
253 UNSIGNED_LONGLONGS_EQUAL(expected_handle, handle);
254 }
255
TEST(mock_ffa_api,ffa_mem_share)256 TEST(mock_ffa_api, ffa_mem_share)
257 {
258 const uint32_t total_length = 0x12345678U;
259 const uint32_t fragment_length = 0x23456789U;
260 const char buffer = 0;
261 const uint32_t page_count = 0x3456789au;
262 const uint64_t expected_handle = 0xfedcba9876543210ULL;
263 uint64_t handle = 0;
264
265 expect_ffa_mem_share(total_length, fragment_length, (void *)&buffer,
266 page_count, &expected_handle, result);
267 LONGS_EQUAL(result,
268 ffa_mem_share(total_length, fragment_length,
269 (void *)&buffer, page_count, &handle));
270 UNSIGNED_LONGLONGS_EQUAL(expected_handle, handle);
271 }
272
TEST(mock_ffa_api,ffa_mem_share_rxtx)273 TEST(mock_ffa_api, ffa_mem_share_rxtx)
274 {
275 const uint32_t total_length = 0x12345678U;
276 const uint32_t fragment_length = 0x23456789U;
277 const uint64_t expected_handle = 0xfedcba9876543210ULL;
278 uint64_t handle = 0;
279
280 expect_ffa_mem_share_rxtx(total_length, fragment_length,
281 &expected_handle, result);
282 LONGS_EQUAL(result,
283 ffa_mem_share_rxtx(total_length, fragment_length, &handle));
284 UNSIGNED_LONGLONGS_EQUAL(expected_handle, handle);
285 }
286
TEST(mock_ffa_api,ffa_mem_retrieve_req)287 TEST(mock_ffa_api, ffa_mem_retrieve_req)
288 {
289 const uint32_t total_length = 0x12345678U;
290 const uint32_t fragment_length = 0x23456789U;
291 const char buffer = 0;
292 const uint32_t page_count = 0x3456789aU;
293 const uint32_t expected_resp_total_length = 0xfedcba98U;
294 const uint32_t expected_resp_fragment_length = 0xedcba987U;
295 uint32_t resp_total_length = 0;
296 uint32_t resp_fragment_length = 0;
297
298 expect_ffa_mem_retrieve_req(total_length, fragment_length,
299 (void *)&buffer, page_count,
300 &expected_resp_total_length,
301 &expected_resp_fragment_length, result);
302 LONGS_EQUAL(result, ffa_mem_retrieve_req(total_length, fragment_length,
303 (void *)&buffer, page_count,
304 &resp_total_length,
305 &resp_fragment_length));
306 UNSIGNED_LONGS_EQUAL(expected_resp_total_length, resp_total_length);
307 UNSIGNED_LONGS_EQUAL(expected_resp_fragment_length,
308 resp_fragment_length);
309 }
310
TEST(mock_ffa_api,ffa_mem_retrieve_req_rxtx)311 TEST(mock_ffa_api, ffa_mem_retrieve_req_rxtx)
312 {
313 const uint32_t total_length = 0x12345678U;
314 const uint32_t fragment_length = 0x23456789U;
315 const uint32_t expected_resp_total_length = 0xfedcba98U;
316 const uint32_t expected_resp_fragment_length = 0xedcba987U;
317 uint32_t resp_total_length = 0;
318 uint32_t resp_fragment_length = 0;
319
320 expect_ffa_mem_retrieve_req_rxtx(total_length, fragment_length,
321 &expected_resp_total_length,
322 &expected_resp_fragment_length,
323 result);
324 LONGS_EQUAL(result, ffa_mem_retrieve_req_rxtx(
325 total_length, fragment_length,
326 &resp_total_length, &resp_fragment_length));
327 UNSIGNED_LONGS_EQUAL(expected_resp_total_length, resp_total_length);
328 UNSIGNED_LONGS_EQUAL(expected_resp_fragment_length,
329 resp_fragment_length);
330 }
331
TEST(mock_ffa_api,ffa_mem_relinquish)332 TEST(mock_ffa_api, ffa_mem_relinquish)
333 {
334 expect_ffa_mem_relinquish(result);
335 LONGS_EQUAL(result, ffa_mem_relinquish());
336 }
337
TEST(mock_ffa_api,ffa_mem_reclaim)338 TEST(mock_ffa_api, ffa_mem_reclaim)
339 {
340 const uint64_t handle = 0xfedcba9876543210ULL;
341 const uint32_t flags = 0xaaccbbddU;
342
343 expect_ffa_mem_reclaim(handle, flags, result);
344 LONGS_EQUAL(result, ffa_mem_reclaim(handle, flags));
345 }
346
TEST(mock_ffa_api,ffa_mem_perm_get)347 TEST(mock_ffa_api, ffa_mem_perm_get)
348 {
349 const void *base_address = (const void *)0x01234567;
350 uint32_t expected_mem_perm = 0x89abcdef;
351 uint32_t mem_perm = 0;
352
353 expect_ffa_mem_perm_get(base_address, &expected_mem_perm, result);
354 LONGS_EQUAL(result, ffa_mem_perm_get(base_address, &mem_perm));
355 UNSIGNED_LONGS_EQUAL(expected_mem_perm, mem_perm);
356 }
357
TEST(mock_ffa_api,ffa_mem_perm_set)358 TEST(mock_ffa_api, ffa_mem_perm_set)
359 {
360 const void *base_address = (const void *)0x01234567;
361 const uint32_t page_count = 0x76543210;
362 const uint32_t mem_perm = 0x89abcdef;
363
364 expect_ffa_mem_perm_set(base_address, page_count, mem_perm, result);
365 LONGS_EQUAL(result, ffa_mem_perm_set(base_address, page_count, mem_perm));
366 }
367
TEST(mock_ffa_api,ffa_console_log_32)368 TEST(mock_ffa_api, ffa_console_log_32)
369 {
370 const char *message = "log message";
371 const size_t length = 11;
372
373 expect_ffa_console_log_32(message, length, result);
374 LONGS_EQUAL(result, ffa_console_log_32(message, length));
375 }
376
TEST(mock_ffa_api,ffa_console_log_64)377 TEST(mock_ffa_api, ffa_console_log_64)
378 {
379 const char *message = "log message";
380 const size_t length = 11;
381
382 expect_ffa_console_log_64(message, length, result);
383 LONGS_EQUAL(result, ffa_console_log_64(message, length));
384 }
385