blob: 10ba83ff1133b9c4702f93b0e90ac8b6e3ddcf5e [file] [log] [blame]
George Liu6492f522020-06-16 10:34:05 +08001#include "libpldm/base.h"
2#include "libpldm/file_io.h"
3
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +05304#include <string.h>
5
6#include <array>
7
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +05308#include <gtest/gtest.h>
9
Zahed Hossain223a73d2019-07-04 12:46:18 -050010constexpr auto hdrSize = sizeof(pldm_msg_hdr);
11
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053012TEST(ReadWriteFileMemory, testGoodDecodeRequest)
13{
Zahed Hossain223a73d2019-07-04 12:46:18 -050014 std::array<uint8_t, PLDM_RW_FILE_MEM_REQ_BYTES + hdrSize> requestMsg{};
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053015
16 // Random value for fileHandle, offset, length, address
17 uint32_t fileHandle = 0x12345678;
18 uint32_t offset = 0x87654321;
19 uint32_t length = 0x13245768;
20 uint64_t address = 0x124356879ACBDE0F;
Lei YU2d5c7452020-03-03 14:43:45 +080021 uint32_t fileHandleLe = htole32(fileHandle);
22 uint32_t offsetLe = htole32(offset);
23 uint32_t lengthLe = htole32(length);
24 uint64_t addressLe = htole64(address);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053025
Lei YU2d5c7452020-03-03 14:43:45 +080026 memcpy(requestMsg.data() + hdrSize, &fileHandleLe, sizeof(fileHandleLe));
27 memcpy(requestMsg.data() + sizeof(fileHandleLe) + hdrSize, &offsetLe,
28 sizeof(offsetLe));
29 memcpy(requestMsg.data() + sizeof(fileHandleLe) + sizeof(offsetLe) +
30 hdrSize,
31 &lengthLe, sizeof(lengthLe));
32 memcpy(requestMsg.data() + sizeof(fileHandleLe) + sizeof(offsetLe) +
33 sizeof(lengthLe) + hdrSize,
34 &addressLe, sizeof(addressLe));
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053035
36 uint32_t retFileHandle = 0;
37 uint32_t retOffset = 0;
38 uint32_t retLength = 0;
39 uint64_t retAddress = 0;
40
Zahed Hossain223a73d2019-07-04 12:46:18 -050041 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
42
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053043 // Invoke decode the read file memory request
Zahed Hossain223a73d2019-07-04 12:46:18 -050044 auto rc = decode_rw_file_memory_req(request, requestMsg.size() - hdrSize,
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053045 &retFileHandle, &retOffset, &retLength,
46 &retAddress);
47
48 ASSERT_EQ(rc, PLDM_SUCCESS);
49 ASSERT_EQ(fileHandle, retFileHandle);
50 ASSERT_EQ(offset, retOffset);
51 ASSERT_EQ(length, retLength);
52 ASSERT_EQ(address, retAddress);
53}
54
55TEST(ReadWriteFileMemory, testBadDecodeRequest)
56{
57 uint32_t fileHandle = 0;
58 uint32_t offset = 0;
59 uint32_t length = 0;
60 uint64_t address = 0;
61
62 // Request payload message is missing
63 auto rc = decode_rw_file_memory_req(NULL, 0, &fileHandle, &offset, &length,
64 &address);
65 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
66
67 std::array<uint8_t, PLDM_RW_FILE_MEM_REQ_BYTES> requestMsg{};
68
Zahed Hossain223a73d2019-07-04 12:46:18 -050069 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
70
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053071 // Address is NULL
Zahed Hossain223a73d2019-07-04 12:46:18 -050072 rc = decode_rw_file_memory_req(request, requestMsg.size() - hdrSize,
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053073 &fileHandle, &offset, &length, NULL);
74 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
75
76 // Payload length is invalid
Zahed Hossain223a73d2019-07-04 12:46:18 -050077 rc = decode_rw_file_memory_req(request, 0, &fileHandle, &offset, &length,
78 &address);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053079 ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
80}
81
82TEST(ReadWriteFileMemory, testGoodEncodeResponse)
83{
84 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_RW_FILE_MEM_RESP_BYTES>
85 responseMsg{};
86 uint32_t length = 0xFF00EE11;
Lei YU2d5c7452020-03-03 14:43:45 +080087 uint32_t lengthLe = htole32(length);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053088 pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
89
90 // ReadFileIntoMemory
91 auto rc = encode_rw_file_memory_resp(0, PLDM_READ_FILE_INTO_MEMORY,
92 PLDM_SUCCESS, length, response);
93
94 ASSERT_EQ(rc, PLDM_SUCCESS);
95 ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
96 ASSERT_EQ(response->hdr.instance_id, 0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050097 ASSERT_EQ(response->hdr.type, PLDM_OEM);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053098 ASSERT_EQ(response->hdr.command, PLDM_READ_FILE_INTO_MEMORY);
99 ASSERT_EQ(response->payload[0], PLDM_SUCCESS);
100 ASSERT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]),
Lei YU2d5c7452020-03-03 14:43:45 +0800101 &lengthLe, sizeof(lengthLe)));
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530102
103 // WriteFileFromMemory
104 rc = encode_rw_file_memory_resp(0, PLDM_WRITE_FILE_FROM_MEMORY,
105 PLDM_SUCCESS, length, response);
106
107 ASSERT_EQ(rc, PLDM_SUCCESS);
108 ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
109 ASSERT_EQ(response->hdr.instance_id, 0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500110 ASSERT_EQ(response->hdr.type, PLDM_OEM);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530111 ASSERT_EQ(response->hdr.command, PLDM_WRITE_FILE_FROM_MEMORY);
112 ASSERT_EQ(response->payload[0], PLDM_SUCCESS);
113 ASSERT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]),
Lei YU2d5c7452020-03-03 14:43:45 +0800114 &lengthLe, sizeof(lengthLe)));
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530115}
116
117TEST(ReadWriteFileMemory, testBadEncodeResponse)
118{
119 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_RW_FILE_MEM_RESP_BYTES>
120 responseMsg{};
121 uint32_t length = 0;
122 pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
123
124 // ReadFileIntoMemory
125 auto rc = encode_rw_file_memory_resp(0, PLDM_READ_FILE_INTO_MEMORY,
126 PLDM_ERROR, length, response);
127
128 ASSERT_EQ(rc, PLDM_SUCCESS);
129 ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
130 ASSERT_EQ(response->hdr.instance_id, 0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500131 ASSERT_EQ(response->hdr.type, PLDM_OEM);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530132 ASSERT_EQ(response->hdr.command, PLDM_READ_FILE_INTO_MEMORY);
133 ASSERT_EQ(response->payload[0], PLDM_ERROR);
134
135 // WriteFileFromMemory
136 rc = encode_rw_file_memory_resp(0, PLDM_WRITE_FILE_FROM_MEMORY, PLDM_ERROR,
137 length, response);
138
139 ASSERT_EQ(rc, PLDM_SUCCESS);
140 ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
141 ASSERT_EQ(response->hdr.instance_id, 0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500142 ASSERT_EQ(response->hdr.type, PLDM_OEM);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530143 ASSERT_EQ(response->hdr.command, PLDM_WRITE_FILE_FROM_MEMORY);
144 ASSERT_EQ(response->payload[0], PLDM_ERROR);
145}
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530146
Priyanga8b976652019-06-27 11:30:33 -0500147TEST(ReadWriteFileIntoMemory, testGoodDecodeResponse)
148{
Zahed Hossain223a73d2019-07-04 12:46:18 -0500149 std::array<uint8_t, PLDM_RW_FILE_MEM_RESP_BYTES + hdrSize> responseMsg{};
Priyanga8b976652019-06-27 11:30:33 -0500150 // Random value for length
151 uint32_t length = 0xFF00EE12;
Lei YU2d5c7452020-03-03 14:43:45 +0800152 uint32_t lengthLe = htole32(length);
Priyanga8b976652019-06-27 11:30:33 -0500153 uint8_t completionCode = 0;
154
Zahed Hossain223a73d2019-07-04 12:46:18 -0500155 memcpy(responseMsg.data() + hdrSize, &completionCode,
156 sizeof(completionCode));
Lei YU2d5c7452020-03-03 14:43:45 +0800157 memcpy(responseMsg.data() + sizeof(completionCode) + hdrSize, &lengthLe,
158 sizeof(lengthLe));
Priyanga8b976652019-06-27 11:30:33 -0500159
160 uint8_t retCompletionCode = 0;
161 uint32_t retLength = 0;
162
Zahed Hossain223a73d2019-07-04 12:46:18 -0500163 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
164
Priyanga8b976652019-06-27 11:30:33 -0500165 // Invoke decode the read file memory response
Zahed Hossain223a73d2019-07-04 12:46:18 -0500166 auto rc = decode_rw_file_memory_resp(response, responseMsg.size() - hdrSize,
Priyanga8b976652019-06-27 11:30:33 -0500167 &retCompletionCode, &retLength);
168 ASSERT_EQ(rc, PLDM_SUCCESS);
169 ASSERT_EQ(completionCode, retCompletionCode);
170 ASSERT_EQ(length, retLength);
171}
172
173TEST(ReadWriteFileIntoMemory, testBadDecodeResponse)
174{
175 uint32_t length = 0;
176 uint8_t completionCode = 0;
177
178 // Request payload message is missing
179 auto rc = decode_rw_file_memory_resp(NULL, 0, &completionCode, &length);
180 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
181
182 std::array<uint8_t, PLDM_RW_FILE_MEM_RESP_BYTES> responseMsg{};
183
Zahed Hossain223a73d2019-07-04 12:46:18 -0500184 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
185
Priyanga8b976652019-06-27 11:30:33 -0500186 // Payload length is invalid
Zahed Hossain223a73d2019-07-04 12:46:18 -0500187 rc = decode_rw_file_memory_resp(response, 0, &completionCode, &length);
Priyanga8b976652019-06-27 11:30:33 -0500188 ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
189}
190
191TEST(ReadWriteFileIntoMemory, testGoodEncodeRequest)
192{
193 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_RW_FILE_MEM_REQ_BYTES>
194 requestMsg{};
195
196 uint32_t fileHandle = 0x12345678;
197 uint32_t offset = 0x87654321;
198 uint32_t length = 0x13245768;
199 uint64_t address = 0x124356879ACBDE0F;
Lei YU2d5c7452020-03-03 14:43:45 +0800200 uint32_t fileHandleLe = htole32(fileHandle);
201 uint32_t offsetLe = htole32(offset);
202 uint32_t lengthLe = htole32(length);
203 uint64_t addressLe = htole64(address);
Priyanga8b976652019-06-27 11:30:33 -0500204
205 pldm_msg* request = reinterpret_cast<pldm_msg*>(requestMsg.data());
206
207 auto rc =
208 encode_rw_file_memory_req(0, PLDM_READ_FILE_INTO_MEMORY, fileHandle,
209 offset, length, address, request);
210
211 ASSERT_EQ(rc, PLDM_SUCCESS);
212 ASSERT_EQ(request->hdr.request, PLDM_REQUEST);
213 ASSERT_EQ(request->hdr.instance_id, 0);
214 ASSERT_EQ(request->hdr.type, PLDM_OEM);
215 ASSERT_EQ(request->hdr.command, PLDM_READ_FILE_INTO_MEMORY);
216
Lei YU2d5c7452020-03-03 14:43:45 +0800217 ASSERT_EQ(0, memcmp(request->payload, &fileHandleLe, sizeof(fileHandleLe)));
Priyanga8b976652019-06-27 11:30:33 -0500218
Lei YU2d5c7452020-03-03 14:43:45 +0800219 ASSERT_EQ(0, memcmp(request->payload + sizeof(fileHandleLe), &offsetLe,
220 sizeof(offsetLe)));
221 ASSERT_EQ(0,
222 memcmp(request->payload + sizeof(fileHandleLe) + sizeof(offsetLe),
223 &lengthLe, sizeof(lengthLe)));
224 ASSERT_EQ(0, memcmp(request->payload + sizeof(fileHandleLe) +
225 sizeof(offsetLe) + sizeof(lengthLe),
226 &addressLe, sizeof(addressLe)));
Priyanga8b976652019-06-27 11:30:33 -0500227}
228
229TEST(ReadWriteFileIntoMemory, testBadEncodeRequest)
230{
231 uint32_t fileHandle = 0;
232 uint32_t offset = 0;
233 uint32_t length = 0;
234 uint64_t address = 0;
235
236 auto rc =
237 encode_rw_file_memory_req(0, PLDM_READ_FILE_INTO_MEMORY, fileHandle,
238 offset, length, address, NULL);
239
240 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
241}
242
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530243TEST(GetFileTable, GoodDecodeRequest)
244{
Zahed Hossain223a73d2019-07-04 12:46:18 -0500245 std::array<uint8_t, PLDM_GET_FILE_TABLE_REQ_BYTES + hdrSize> requestMsg{};
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530246
247 // Random value for DataTransferHandle, TransferOperationFlag, TableType
248 uint32_t transferHandle = 0x12345678;
Lei YU2d5c7452020-03-03 14:43:45 +0800249 uint32_t transferHandleLe = htole32(transferHandle);
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530250 uint8_t transferOpFlag = 1;
251 uint8_t tableType = 1;
252
Lei YU2d5c7452020-03-03 14:43:45 +0800253 memcpy(requestMsg.data() + hdrSize, &transferHandleLe,
254 sizeof(transferHandleLe));
Zahed Hossain223a73d2019-07-04 12:46:18 -0500255 memcpy(requestMsg.data() + sizeof(transferHandle) + hdrSize,
256 &transferOpFlag, sizeof(transferOpFlag));
257 memcpy(requestMsg.data() + sizeof(transferHandle) + sizeof(transferOpFlag) +
258 hdrSize,
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530259 &tableType, sizeof(tableType));
260
261 uint32_t retTransferHandle = 0;
262 uint8_t retTransferOpFlag = 0;
263 uint8_t retTableType = 0;
264
Zahed Hossain223a73d2019-07-04 12:46:18 -0500265 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
266
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530267 // Invoke decode get file table request
Zahed Hossain223a73d2019-07-04 12:46:18 -0500268 auto rc = decode_get_file_table_req(request, requestMsg.size() - hdrSize,
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530269 &retTransferHandle, &retTransferOpFlag,
270 &retTableType);
271
272 ASSERT_EQ(rc, PLDM_SUCCESS);
273 ASSERT_EQ(transferHandle, retTransferHandle);
274 ASSERT_EQ(transferOpFlag, retTransferOpFlag);
275 ASSERT_EQ(tableType, retTableType);
276}
277
278TEST(GetFileTable, BadDecodeRequest)
279{
280 uint32_t transferHandle = 0;
281 uint8_t transferOpFlag = 0;
282 uint8_t tableType = 0;
283
284 // Request payload message is missing
285 auto rc = decode_get_file_table_req(nullptr, 0, &transferHandle,
286 &transferOpFlag, &tableType);
287 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
288
289 std::array<uint8_t, PLDM_GET_FILE_TABLE_REQ_BYTES> requestMsg{};
290
Zahed Hossain223a73d2019-07-04 12:46:18 -0500291 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
292
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530293 // TableType is NULL
Zahed Hossain223a73d2019-07-04 12:46:18 -0500294 rc = decode_get_file_table_req(request, requestMsg.size() - hdrSize,
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530295 &transferHandle, &transferOpFlag, nullptr);
296 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
297
298 // Payload length is invalid
Zahed Hossain223a73d2019-07-04 12:46:18 -0500299 rc = decode_get_file_table_req(request, 0, &transferHandle, &transferOpFlag,
300 &tableType);
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530301 ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
302}
303
304TEST(GetFileTable, GoodEncodeResponse)
305{
306 // Random value for NextDataTransferHandle and TransferFlag
307 uint8_t completionCode = 0;
308 uint32_t nextTransferHandle = 0x87654321;
Lei YU2d5c7452020-03-03 14:43:45 +0800309 uint32_t nextTransferHandleLe = htole32(nextTransferHandle);
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530310 uint8_t transferFlag = 5;
311 // Mock file table contents of size 5
312 std::array<uint8_t, 5> fileTable = {1, 2, 3, 4, 5};
313 constexpr size_t responseSize = sizeof(completionCode) +
314 sizeof(nextTransferHandle) +
315 sizeof(transferFlag) + fileTable.size();
316
317 std::array<uint8_t, sizeof(pldm_msg_hdr) + responseSize> responseMsg{};
318 pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
319
320 // GetFileTable
321 auto rc = encode_get_file_table_resp(0, PLDM_SUCCESS, nextTransferHandle,
322 transferFlag, fileTable.data(),
323 fileTable.size(), response);
324
325 ASSERT_EQ(rc, PLDM_SUCCESS);
326 ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
327 ASSERT_EQ(response->hdr.instance_id, 0);
328 ASSERT_EQ(response->hdr.type, PLDM_OEM);
329 ASSERT_EQ(response->hdr.command, PLDM_GET_FILE_TABLE);
330 ASSERT_EQ(response->payload[0], PLDM_SUCCESS);
331 ASSERT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]),
Lei YU2d5c7452020-03-03 14:43:45 +0800332 &nextTransferHandleLe, sizeof(nextTransferHandle)));
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530333 ASSERT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]) +
334 sizeof(nextTransferHandle),
335 &transferFlag, sizeof(transferFlag)));
336 ASSERT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]) +
337 sizeof(nextTransferHandle),
338 &transferFlag, sizeof(transferFlag)));
339 ASSERT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]) +
340 sizeof(nextTransferHandle) + sizeof(transferFlag),
341 fileTable.data(), fileTable.size()));
342}
343
344TEST(GetFileTable, BadEncodeResponse)
345{
346 uint8_t completionCode = 0;
347 uint32_t nextTransferHandle = 0;
348 uint8_t transferFlag = 0;
349 constexpr size_t responseSize = sizeof(completionCode) +
350 sizeof(nextTransferHandle) +
351 sizeof(transferFlag);
352
353 std::array<uint8_t, sizeof(pldm_msg_hdr) + responseSize> responseMsg{};
354 pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
355
356 // GetFileTable
357 auto rc = encode_get_file_table_resp(0, PLDM_ERROR, nextTransferHandle,
358 transferFlag, nullptr, 0, response);
359
360 ASSERT_EQ(rc, PLDM_SUCCESS);
361 ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
362 ASSERT_EQ(response->hdr.instance_id, 0);
363 ASSERT_EQ(response->hdr.type, PLDM_OEM);
364 ASSERT_EQ(response->hdr.command, PLDM_GET_FILE_TABLE);
365 ASSERT_EQ(response->payload[0], PLDM_ERROR);
366}
vkaverap2ffe3292019-06-24 00:08:13 -0500367
368TEST(ReadFile, testGoodDecodeRequest)
369{
370 std::array<uint8_t, PLDM_READ_FILE_REQ_BYTES + sizeof(pldm_msg_hdr)>
371 requestMsg{};
372
373 auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
374 size_t payload_length = requestMsg.size() - sizeof(pldm_msg_hdr);
375 auto request = reinterpret_cast<pldm_read_file_req*>(requestPtr->payload);
376
377 // Random value for fileHandle, offset and length
378 uint32_t fileHandle = 0x12345678;
379 uint32_t offset = 0x87654321;
380 uint32_t length = 0x13245768;
381
Lei YU2d5c7452020-03-03 14:43:45 +0800382 request->file_handle = htole32(fileHandle);
383 request->offset = htole32(offset);
384 request->length = htole32(length);
vkaverap2ffe3292019-06-24 00:08:13 -0500385
386 uint32_t retFileHandle = 0;
387 uint32_t retOffset = 0;
388 uint32_t retLength = 0;
389
390 // Invoke decode the read file request
391 auto rc = decode_read_file_req(requestPtr, payload_length, &retFileHandle,
392 &retOffset, &retLength);
393
394 ASSERT_EQ(rc, PLDM_SUCCESS);
395 ASSERT_EQ(fileHandle, retFileHandle);
396 ASSERT_EQ(offset, retOffset);
397 ASSERT_EQ(length, retLength);
398}
399
400TEST(WriteFile, testGoodDecodeRequest)
401{
402 // Random value for fileHandle, offset, length and file data
403 uint32_t fileHandle = 0x12345678;
404 uint32_t offset = 0x87654321;
405 uint32_t length = 0x467;
406
407 std::vector<uint8_t> requestMsg(PLDM_WRITE_FILE_REQ_BYTES +
408 sizeof(pldm_msg_hdr) + length);
409 auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
410 size_t payload_length = requestMsg.size() - sizeof(pldm_msg_hdr);
411 auto request = reinterpret_cast<pldm_write_file_req*>(requestPtr->payload);
412
413 size_t fileDataOffset =
414 sizeof(fileHandle) + sizeof(offset) + sizeof(length);
415
Lei YU2d5c7452020-03-03 14:43:45 +0800416 request->file_handle = htole32(fileHandle);
417 request->offset = htole32(offset);
418 request->length = htole32(length);
vkaverap2ffe3292019-06-24 00:08:13 -0500419
420 uint32_t retFileHandle = 0;
421 uint32_t retOffset = 0;
422 uint32_t retLength = 0;
423 size_t retFileDataOffset = 0;
424
425 // Invoke decode the write file request
426 auto rc = decode_write_file_req(requestPtr, payload_length, &retFileHandle,
427 &retOffset, &retLength, &retFileDataOffset);
428
429 ASSERT_EQ(rc, PLDM_SUCCESS);
430 ASSERT_EQ(fileHandle, retFileHandle);
431 ASSERT_EQ(offset, retOffset);
432 ASSERT_EQ(length, retLength);
433 ASSERT_EQ(fileDataOffset, retFileDataOffset);
434}
435
436TEST(ReadFile, testGoodDecodeResponse)
437{
438 // Random value for length
439 uint32_t length = 0x10;
440 uint8_t completionCode = PLDM_SUCCESS;
441
442 std::vector<uint8_t> responseMsg(PLDM_READ_FILE_RESP_BYTES +
443 sizeof(pldm_msg_hdr) + length);
444 auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
445 size_t payload_length = responseMsg.size() - sizeof(pldm_msg_hdr);
446 auto response =
447 reinterpret_cast<pldm_read_file_resp*>(responsePtr->payload);
448
449 response->completion_code = completionCode;
Lei YU2d5c7452020-03-03 14:43:45 +0800450 response->length = htole32(length);
vkaverap2ffe3292019-06-24 00:08:13 -0500451
452 size_t fileDataOffset = sizeof(completionCode) + sizeof(length);
453
454 uint32_t retLength = 0;
455 uint8_t retCompletionCode = 0;
456 size_t retFileDataOffset = 0;
457
458 // Invoke decode the read file response
459 auto rc =
460 decode_read_file_resp(responsePtr, payload_length, &retCompletionCode,
461 &retLength, &retFileDataOffset);
462
463 ASSERT_EQ(rc, PLDM_SUCCESS);
464 ASSERT_EQ(completionCode, retCompletionCode);
465 ASSERT_EQ(length, retLength);
466 ASSERT_EQ(fileDataOffset, retFileDataOffset);
467}
468
469TEST(WriteFile, testGoodDecodeResponse)
470{
471 std::array<uint8_t, PLDM_WRITE_FILE_RESP_BYTES + sizeof(pldm_msg_hdr)>
472 responseMsg{};
473 auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
474 size_t payload_length = responseMsg.size() - sizeof(pldm_msg_hdr);
475 auto response =
476 reinterpret_cast<pldm_write_file_resp*>(responsePtr->payload);
477
478 uint8_t completionCode = PLDM_SUCCESS;
479 uint32_t length = 0x4678;
480
481 response->completion_code = completionCode;
Lei YU2d5c7452020-03-03 14:43:45 +0800482 response->length = htole32(length);
vkaverap2ffe3292019-06-24 00:08:13 -0500483
484 uint32_t retLength = 0;
485 uint8_t retCompletionCode = 0;
486
487 // Invoke decode the write file response
488 auto rc = decode_write_file_resp(responsePtr, payload_length,
489 &retCompletionCode, &retLength);
490
491 ASSERT_EQ(rc, PLDM_SUCCESS);
492 ASSERT_EQ(completionCode, retCompletionCode);
493 ASSERT_EQ(length, retLength);
494}
495
496TEST(ReadWriteFile, testBadDecodeResponse)
497{
498 uint32_t length = 0;
499 uint8_t completionCode = 0;
500 size_t fileDataOffset = 0;
501
502 // Bad decode response for read file
503 std::vector<uint8_t> responseMsg(PLDM_READ_FILE_RESP_BYTES +
504 sizeof(pldm_msg_hdr) + length);
505 auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
506
507 // Request payload message is missing
508 auto rc = decode_read_file_resp(NULL, 0, &completionCode, &length,
509 &fileDataOffset);
510 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
511
512 // Payload length is invalid
513 rc = decode_read_file_resp(responsePtr, 0, &completionCode, &length,
514 &fileDataOffset);
515 ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
516
517 // Bad decode response for write file
518 std::array<uint8_t, PLDM_WRITE_FILE_RESP_BYTES + sizeof(pldm_msg_hdr)>
519 responseMsgWr{};
520 auto responseWr = reinterpret_cast<pldm_msg*>(responseMsgWr.data());
521
522 // Request payload message is missing
523 rc = decode_write_file_resp(NULL, 0, &completionCode, &length);
524 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
525
526 // Payload length is invalid
527 rc = decode_write_file_resp(responseWr, 0, &completionCode, &length);
528 ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
529}
530
531TEST(ReadWriteFile, testBadDecodeRequest)
532{
533 uint32_t fileHandle = 0;
534 uint32_t offset = 0;
535 uint32_t length = 0;
536
537 // Bad decode request for read file
538 std::array<uint8_t, PLDM_READ_FILE_REQ_BYTES + sizeof(pldm_msg_hdr)>
539 requestMsg{};
540 auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
541
542 // Request payload message is missing
543 auto rc = decode_read_file_req(NULL, 0, &fileHandle, &offset, &length);
544 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
545
546 // Payload length is invalid
547 rc = decode_read_file_req(requestPtr, 0, &fileHandle, &offset, &length);
548 ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
549
550 // Bad decode request for write file
551 size_t fileDataOffset = 0;
552 std::array<uint8_t, PLDM_WRITE_FILE_REQ_BYTES> requestMsgWr{};
553 auto requestWr = reinterpret_cast<pldm_msg*>(requestMsgWr.data());
554
555 // Request payload message is missing
556 rc = decode_write_file_req(NULL, 0, &fileHandle, &offset, &length,
557 &fileDataOffset);
558 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
559
560 // Payload length is invalid
561 rc = decode_write_file_req(requestWr, 0, &fileHandle, &offset, &length,
562 &fileDataOffset);
563 ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
564}
565
566TEST(ReadFile, testGoodEncodeResponse)
567{
568 // Good encode response for read file
569 uint32_t length = 0x4;
570
571 std::vector<uint8_t> responseMsg(PLDM_READ_FILE_RESP_BYTES +
572 sizeof(pldm_msg_hdr) + length);
573 auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
574 auto response =
575 reinterpret_cast<pldm_read_file_resp*>(responsePtr->payload);
576
577 // ReadFile
578 auto rc = encode_read_file_resp(0, PLDM_SUCCESS, length, responsePtr);
579
580 ASSERT_EQ(rc, PLDM_SUCCESS);
581 ASSERT_EQ(responsePtr->hdr.request, PLDM_RESPONSE);
582 ASSERT_EQ(responsePtr->hdr.instance_id, 0);
583 ASSERT_EQ(responsePtr->hdr.type, PLDM_OEM);
584 ASSERT_EQ(responsePtr->hdr.command, PLDM_READ_FILE);
585 ASSERT_EQ(response->completion_code, PLDM_SUCCESS);
Lei YU2d5c7452020-03-03 14:43:45 +0800586 ASSERT_EQ(le32toh(response->length), length);
vkaverap2ffe3292019-06-24 00:08:13 -0500587}
588
589TEST(WriteFile, testGoodEncodeResponse)
590{
591 uint32_t length = 0x467;
592
593 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_WRITE_FILE_RESP_BYTES>
594 responseMsg{};
595
596 auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
597 auto response =
598 reinterpret_cast<pldm_write_file_resp*>(responsePtr->payload);
599
600 // WriteFile
601 auto rc = encode_write_file_resp(0, PLDM_SUCCESS, length, responsePtr);
602 ASSERT_EQ(rc, PLDM_SUCCESS);
603 ASSERT_EQ(responsePtr->hdr.request, PLDM_RESPONSE);
604 ASSERT_EQ(responsePtr->hdr.instance_id, 0);
605 ASSERT_EQ(responsePtr->hdr.type, PLDM_OEM);
606 ASSERT_EQ(responsePtr->hdr.command, PLDM_WRITE_FILE);
607 ASSERT_EQ(response->completion_code, PLDM_SUCCESS);
Lei YU2d5c7452020-03-03 14:43:45 +0800608 ASSERT_EQ(le32toh(response->length), length);
vkaverap2ffe3292019-06-24 00:08:13 -0500609}
610
611TEST(ReadFile, testGoodEncodeRequest)
612{
613 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_READ_FILE_REQ_BYTES>
614 requestMsg{};
615
616 uint32_t fileHandle = 0x12345678;
617 uint32_t offset = 0x87654321;
618 uint32_t length = 0x13245768;
619 auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
620 auto request = reinterpret_cast<pldm_read_file_req*>(requestPtr->payload);
621
622 // ReadFile
623 auto rc = encode_read_file_req(0, fileHandle, offset, length, requestPtr);
624
625 ASSERT_EQ(rc, PLDM_SUCCESS);
626 ASSERT_EQ(requestPtr->hdr.request, PLDM_REQUEST);
627 ASSERT_EQ(requestPtr->hdr.instance_id, 0);
628 ASSERT_EQ(requestPtr->hdr.type, PLDM_OEM);
629 ASSERT_EQ(requestPtr->hdr.command, PLDM_READ_FILE);
Lei YU2d5c7452020-03-03 14:43:45 +0800630 ASSERT_EQ(le32toh(request->file_handle), fileHandle);
631 ASSERT_EQ(le32toh(request->offset), offset);
632 ASSERT_EQ(le32toh(request->length), length);
vkaverap2ffe3292019-06-24 00:08:13 -0500633}
634
635TEST(WriteFile, testGoodEncodeRequest)
636{
637 uint32_t fileHandle = 0x12345678;
638 uint32_t offset = 0x87654321;
639 uint32_t length = 0x456;
640
641 std::vector<uint8_t> requestMsg(PLDM_WRITE_FILE_REQ_BYTES +
642 sizeof(pldm_msg_hdr) + length);
643 auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
644 auto request = reinterpret_cast<pldm_write_file_req*>(requestPtr->payload);
645
646 // WriteFile
647 auto rc = encode_write_file_req(0, fileHandle, offset, length, requestPtr);
648
649 ASSERT_EQ(rc, PLDM_SUCCESS);
650 ASSERT_EQ(requestPtr->hdr.request, PLDM_REQUEST);
651 ASSERT_EQ(requestPtr->hdr.instance_id, 0);
652 ASSERT_EQ(requestPtr->hdr.type, PLDM_OEM);
653 ASSERT_EQ(requestPtr->hdr.command, PLDM_WRITE_FILE);
Lei YU2d5c7452020-03-03 14:43:45 +0800654 ASSERT_EQ(le32toh(request->file_handle), fileHandle);
655 ASSERT_EQ(le32toh(request->offset), offset);
656 ASSERT_EQ(le32toh(request->length), length);
vkaverap2ffe3292019-06-24 00:08:13 -0500657}
658
659TEST(ReadWriteFile, testBadEncodeRequest)
660{
661 // Bad encode request for read file
662 uint32_t fileHandle = 0;
663 uint32_t offset = 0;
664 uint32_t length = 0;
665
666 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_READ_FILE_REQ_BYTES>
667 requestMsg{};
668 auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
669
670 // ReadFile check invalid file length
671 auto rc = encode_read_file_req(0, fileHandle, offset, length, requestPtr);
672
Deepak Kodihalli3bf5c552020-04-20 06:16:01 -0500673 ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
vkaverap2ffe3292019-06-24 00:08:13 -0500674
675 // Bad encode request for write file
676 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_WRITE_FILE_REQ_BYTES>
677 requestMsgWr{};
678 auto requestWr = reinterpret_cast<pldm_msg*>(requestMsgWr.data());
679
680 // WriteFile check for invalid file length
681 rc = encode_write_file_req(0, fileHandle, offset, length, requestWr);
682
Deepak Kodihalli3bf5c552020-04-20 06:16:01 -0500683 ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
vkaverap2ffe3292019-06-24 00:08:13 -0500684}
685
686TEST(ReadWriteFile, testBadEncodeResponse)
687{
688 // Bad encode response for read file
689 uint32_t length = 0;
690
691 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_READ_FILE_RESP_BYTES>
692 responseMsg{};
693 auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
694
695 // ReadFile
696 auto rc = encode_read_file_resp(0, PLDM_ERROR, length, responsePtr);
697
698 ASSERT_EQ(rc, PLDM_SUCCESS);
699 ASSERT_EQ(responsePtr->hdr.request, PLDM_RESPONSE);
700 ASSERT_EQ(responsePtr->hdr.instance_id, 0);
701 ASSERT_EQ(responsePtr->hdr.type, PLDM_OEM);
702 ASSERT_EQ(responsePtr->hdr.command, PLDM_READ_FILE);
703 ASSERT_EQ(responsePtr->payload[0], PLDM_ERROR);
704
705 // Bad encode response for write file
706 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_WRITE_FILE_RESP_BYTES>
707 responseMsgWr{};
708 auto responseWr = reinterpret_cast<pldm_msg*>(responseMsgWr.data());
709
710 // WriteFile
711 rc = encode_write_file_resp(0, PLDM_ERROR, length, responseWr);
712
713 ASSERT_EQ(rc, PLDM_SUCCESS);
714 ASSERT_EQ(responseWr->hdr.request, PLDM_RESPONSE);
715 ASSERT_EQ(responseWr->hdr.instance_id, 0);
716 ASSERT_EQ(responseWr->hdr.type, PLDM_OEM);
717 ASSERT_EQ(responseWr->hdr.command, PLDM_WRITE_FILE);
718 ASSERT_EQ(responseWr->payload[0], PLDM_ERROR);
719}
vkaverap07404562019-08-05 22:57:11 -0500720
721TEST(ReadWriteFileByTypeMemory, testGoodDecodeRequest)
722{
723 std::array<uint8_t,
724 PLDM_RW_FILE_BY_TYPE_MEM_REQ_BYTES + sizeof(pldm_msg_hdr)>
725 requestMsg{};
726
727 auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
728 size_t payload_length = requestMsg.size() - sizeof(pldm_msg_hdr);
729 auto request = reinterpret_cast<pldm_read_write_file_by_type_memory_req*>(
730 requestPtr->payload);
731
732 // Random value for fileHandle, offset and length
733 uint16_t fileType = 0;
734 uint32_t fileHandle = 0x12345678;
735 uint32_t offset = 0x87654321;
736 uint32_t length = 0x13245768;
737 uint64_t address = 0x124356879ACBD456;
738
Lei YU2d5c7452020-03-03 14:43:45 +0800739 request->file_type = htole16(fileType);
740 request->file_handle = htole32(fileHandle);
741 request->offset = htole32(offset);
742 request->length = htole32(length);
743 request->address = htole64(address);
vkaverap07404562019-08-05 22:57:11 -0500744
745 uint16_t retFileType = 0x1;
746 uint32_t retFileHandle = 0;
747 uint32_t retOffset = 0;
748 uint32_t retLength = 0;
749 uint64_t retAddress = 0;
750
751 // Invoke decode the read file request
752 auto rc = decode_rw_file_by_type_memory_req(
753 requestPtr, payload_length, &retFileType, &retFileHandle, &retOffset,
754 &retLength, &retAddress);
755
756 ASSERT_EQ(rc, PLDM_SUCCESS);
757 ASSERT_EQ(fileType, retFileType);
758 ASSERT_EQ(fileHandle, retFileHandle);
759 ASSERT_EQ(offset, retOffset);
760 ASSERT_EQ(length, retLength);
761 ASSERT_EQ(address, retAddress);
762}
763
764TEST(ReadWriteFileByTypeMemory, testGoodDecodeResponse)
765{
766 std::array<uint8_t,
767 PLDM_RW_FILE_BY_TYPE_MEM_RESP_BYTES + sizeof(pldm_msg_hdr)>
768 responseMsg{};
769
770 auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
771 size_t payload_length = responseMsg.size() - sizeof(pldm_msg_hdr);
772 auto response = reinterpret_cast<pldm_read_write_file_by_type_memory_resp*>(
773 responsePtr->payload);
774
775 // Random value for completion code and length
776 uint8_t completionCode = 0x0;
777 uint32_t length = 0x13245768;
778
779 response->completion_code = completionCode;
Lei YU2d5c7452020-03-03 14:43:45 +0800780 response->length = htole32(length);
vkaverap07404562019-08-05 22:57:11 -0500781
782 uint8_t retCompletionCode = 0x1;
783 uint32_t retLength = 0;
784
785 // Invoke decode the read/write file response
786 auto rc = decode_rw_file_by_type_memory_resp(
787 responsePtr, payload_length, &retCompletionCode, &retLength);
788
789 ASSERT_EQ(rc, PLDM_SUCCESS);
790 ASSERT_EQ(completionCode, retCompletionCode);
791 ASSERT_EQ(length, retLength);
792}
793
794TEST(ReadWriteFileByTypeMemory, testBadDecodeRequest)
795{
796 uint16_t fileType = 0;
797 uint32_t fileHandle = 0;
798 uint32_t offset = 0;
799 uint32_t length = 0;
800 uint64_t address = 0;
801
802 // Request payload message is missing
803 auto rc = decode_rw_file_by_type_memory_req(NULL, 0, &fileType, &fileHandle,
804 &offset, &length, &address);
805 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
806
807 std::array<uint8_t,
808 PLDM_RW_FILE_BY_TYPE_MEM_REQ_BYTES + sizeof(pldm_msg_hdr)>
809 requestMsg{};
810
811 auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
812
813 // Address is NULL
814 rc = decode_rw_file_by_type_memory_req(
815 requestPtr, requestMsg.size() - hdrSize, &fileType, &fileHandle,
816 &offset, &length, NULL);
817 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
818
819 // Payload length is invalid
820 rc = decode_rw_file_by_type_memory_req(
821 requestPtr, 0, &fileType, &fileHandle, &offset, &length, &address);
822 ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
823}
824
825TEST(ReadWriteFileByTypeMemory, testBadDecodeResponse)
826{
827 uint32_t length = 0;
828 uint8_t completionCode = 0;
829
830 // Request payload message is missing
831 auto rc =
832 decode_rw_file_by_type_memory_resp(NULL, 0, &completionCode, &length);
833 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
834
835 std::array<uint8_t,
836 PLDM_RW_FILE_BY_TYPE_MEM_RESP_BYTES + sizeof(pldm_msg_hdr)>
837 responseMsg{};
838
839 auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
840
841 // Length is NULL
842 rc = decode_rw_file_by_type_memory_resp(
843 responsePtr, responseMsg.size() - hdrSize, &completionCode, NULL);
844 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
845
846 // Payload length is invalid
847 rc = decode_rw_file_by_type_memory_resp(responsePtr, 0, &completionCode,
848 &length);
849 ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
850}
851
852TEST(ReadWriteFileByTypeMemory, testGoodEncodeRequest)
853{
854 std::array<uint8_t,
855 sizeof(pldm_msg_hdr) + PLDM_RW_FILE_BY_TYPE_MEM_REQ_BYTES>
856 requestMsg{};
857
858 uint16_t fileType = 0;
859 uint32_t fileHandle = 0x12345678;
860 uint32_t offset = 0x87654321;
861 uint32_t length = 0x13245768;
862 uint64_t address = 0x124356879ACBDE0F;
Lei YU2d5c7452020-03-03 14:43:45 +0800863 uint16_t fileTypeLe = htole16(fileType);
864 uint32_t fileHandleLe = htole32(fileHandle);
865 uint32_t offsetLe = htole32(offset);
866 uint32_t lengthLe = htole32(length);
867 uint64_t addressLe = htole64(address);
vkaverap07404562019-08-05 22:57:11 -0500868
869 pldm_msg* request = reinterpret_cast<pldm_msg*>(requestMsg.data());
870
871 auto rc = encode_rw_file_by_type_memory_req(
872 0, PLDM_READ_FILE_BY_TYPE_INTO_MEMORY, fileType, fileHandle, offset,
873 length, address, request);
874
875 ASSERT_EQ(rc, PLDM_SUCCESS);
876 ASSERT_EQ(request->hdr.request, PLDM_REQUEST);
877 ASSERT_EQ(request->hdr.instance_id, 0);
878 ASSERT_EQ(request->hdr.type, PLDM_OEM);
879 ASSERT_EQ(request->hdr.command, PLDM_READ_FILE_BY_TYPE_INTO_MEMORY);
880
Lei YU2d5c7452020-03-03 14:43:45 +0800881 ASSERT_EQ(0, memcmp(request->payload, &fileTypeLe, sizeof(fileTypeLe)));
882 ASSERT_EQ(0, memcmp(request->payload + sizeof(fileTypeLe), &fileHandleLe,
883 sizeof(fileHandleLe)));
vkaverap07404562019-08-05 22:57:11 -0500884
Lei YU2d5c7452020-03-03 14:43:45 +0800885 ASSERT_EQ(
886 0, memcmp(request->payload + sizeof(fileTypeLe) + sizeof(fileHandleLe),
887 &offsetLe, sizeof(offsetLe)));
888 ASSERT_EQ(0, memcmp(request->payload + sizeof(fileTypeLe) +
889 sizeof(fileHandleLe) + sizeof(offsetLe),
890 &lengthLe, sizeof(lengthLe)));
891 ASSERT_EQ(0, memcmp(request->payload + sizeof(fileTypeLe) +
892 sizeof(fileHandleLe) + sizeof(offsetLe) +
893 sizeof(lengthLe),
894 &addressLe, sizeof(addressLe)));
vkaverap07404562019-08-05 22:57:11 -0500895}
896
897TEST(ReadWriteFileByTypeMemory, testGoodEncodeResponse)
898{
899 std::array<uint8_t,
900 sizeof(pldm_msg_hdr) + PLDM_RW_FILE_BY_TYPE_MEM_RESP_BYTES>
901 responseMsg{};
902
903 uint32_t length = 0x13245768;
Lei YU2d5c7452020-03-03 14:43:45 +0800904 uint32_t lengthLe = htole32(length);
vkaverap07404562019-08-05 22:57:11 -0500905 uint8_t completionCode = 0x0;
906
907 pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
908
909 auto rc = encode_rw_file_by_type_memory_resp(
910 0, PLDM_READ_FILE_BY_TYPE_INTO_MEMORY, completionCode, length,
911 response);
912
913 ASSERT_EQ(rc, PLDM_SUCCESS);
914 ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
915 ASSERT_EQ(response->hdr.instance_id, 0);
916 ASSERT_EQ(response->hdr.type, PLDM_OEM);
917 ASSERT_EQ(response->hdr.command, PLDM_READ_FILE_BY_TYPE_INTO_MEMORY);
918
919 ASSERT_EQ(
920 0, memcmp(response->payload, &completionCode, sizeof(completionCode)));
Lei YU2d5c7452020-03-03 14:43:45 +0800921 ASSERT_EQ(0, memcmp(response->payload + sizeof(completionCode), &lengthLe,
922 sizeof(lengthLe)));
vkaverap07404562019-08-05 22:57:11 -0500923}
924
925TEST(ReadWriteFileByTypeMemory, testBadEncodeResponse)
926{
927 std::array<uint8_t,
928 sizeof(pldm_msg_hdr) + PLDM_RW_FILE_BY_TYPE_MEM_RESP_BYTES>
929 responseMsg{};
930 uint32_t length = 0;
931 pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
932
933 // completion code is PLDM_ERROR
934 auto rc = encode_rw_file_by_type_memory_resp(
935 0, PLDM_READ_FILE_BY_TYPE_INTO_MEMORY, PLDM_ERROR, length, response);
936
937 ASSERT_EQ(rc, PLDM_SUCCESS);
938 ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
939 ASSERT_EQ(response->hdr.instance_id, 0);
940 ASSERT_EQ(response->hdr.type, PLDM_OEM);
941 ASSERT_EQ(response->hdr.command, PLDM_READ_FILE_BY_TYPE_INTO_MEMORY);
942 ASSERT_EQ(response->payload[0], PLDM_ERROR);
943
944 // response is NULL pointer
945 rc = encode_rw_file_by_type_memory_resp(
946 0, PLDM_READ_FILE_BY_TYPE_INTO_MEMORY, PLDM_SUCCESS, length, NULL);
947
948 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
949}
950
951TEST(ReadWriteFileByTypeMemory, testBadEncodeRequest)
952{
953 uint8_t fileType = 0;
954 uint32_t fileHandle = 0;
955 uint32_t offset = 0;
956 uint32_t length = 0;
957 uint64_t address = 0;
958
959 // request is NULL pointer
960 auto rc = encode_rw_file_by_type_memory_req(
961 0, PLDM_READ_FILE_BY_TYPE_INTO_MEMORY, fileType, fileHandle, offset,
962 length, address, NULL);
963
964 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
965}
vkaverapa9aac722019-08-22 02:10:15 -0500966
967TEST(NewFile, testGoodDecodeRequest)
968{
969 std::array<uint8_t, PLDM_NEW_FILE_REQ_BYTES + sizeof(pldm_msg_hdr)>
970 requestMsg{};
971
972 auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
973 size_t payload_length = requestMsg.size() - sizeof(pldm_msg_hdr);
974 auto request = reinterpret_cast<pldm_new_file_req*>(requestPtr->payload);
975
976 // Random value for fileHandle and length
977 uint16_t fileType = 0xFF;
978 uint32_t fileHandle = 0x12345678;
Deepak Kodihalli83388762020-01-28 04:09:58 -0600979 uint64_t length = 0x13245768;
vkaverapa9aac722019-08-22 02:10:15 -0500980
Lei YU2d5c7452020-03-03 14:43:45 +0800981 request->file_type = htole16(fileType);
982 request->file_handle = htole32(fileHandle);
983 request->length = htole64(length);
vkaverapa9aac722019-08-22 02:10:15 -0500984
985 uint16_t retFileType = 0xFF;
986 uint32_t retFileHandle = 0;
Deepak Kodihalli83388762020-01-28 04:09:58 -0600987 uint64_t retLength = 0;
vkaverapa9aac722019-08-22 02:10:15 -0500988
989 // Invoke decode the read file request
990 auto rc = decode_new_file_req(requestPtr, payload_length, &retFileType,
991 &retFileHandle, &retLength);
992
993 ASSERT_EQ(rc, PLDM_SUCCESS);
994 ASSERT_EQ(fileType, retFileType);
995 ASSERT_EQ(fileHandle, retFileHandle);
996 ASSERT_EQ(length, retLength);
997}
998
999TEST(NewFile, testGoodDecodeResponse)
1000{
1001 std::array<uint8_t, PLDM_NEW_FILE_RESP_BYTES + sizeof(pldm_msg_hdr)>
1002 responseMsg{};
1003
1004 auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
1005 size_t payload_length = responseMsg.size() - sizeof(pldm_msg_hdr);
1006 auto response = reinterpret_cast<pldm_new_file_resp*>(responsePtr->payload);
1007
1008 // Random value for completion code
1009 uint8_t completionCode = 0x0;
1010
1011 response->completion_code = completionCode;
1012
1013 uint8_t retCompletionCode = PLDM_SUCCESS;
1014
1015 // Invoke decode the read/write file response
1016 auto rc =
1017 decode_new_file_resp(responsePtr, payload_length, &retCompletionCode);
1018
1019 ASSERT_EQ(rc, PLDM_SUCCESS);
1020 ASSERT_EQ(completionCode, retCompletionCode);
1021}
1022
1023TEST(NewFile, testBadDecodeRequest)
1024{
1025 uint16_t fileType = 0;
1026 uint32_t fileHandle = 0;
Deepak Kodihalli83388762020-01-28 04:09:58 -06001027 uint64_t length = 0;
vkaverapa9aac722019-08-22 02:10:15 -05001028
1029 // Request payload message is missing
1030 auto rc = decode_new_file_req(NULL, 0, &fileType, &fileHandle, &length);
1031
1032 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1033
1034 std::array<uint8_t, PLDM_NEW_FILE_REQ_BYTES + sizeof(pldm_msg_hdr)>
1035 requestMsg{};
1036
1037 auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
1038
1039 // Payload length is invalid
1040 rc = decode_new_file_req(requestPtr, 0, &fileType, &fileHandle, &length);
1041 ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1042}
1043
1044TEST(NewFile, testBadDecodeResponse)
1045{
1046 uint8_t completionCode = 0;
1047
1048 // Request payload message is missing
1049 auto rc = decode_new_file_resp(NULL, 0, &completionCode);
1050 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1051
1052 std::array<uint8_t, PLDM_NEW_FILE_RESP_BYTES + sizeof(pldm_msg_hdr)>
1053 responseMsg{};
1054
1055 auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
1056
1057 // Payload length is invalid
1058 rc = decode_new_file_resp(responsePtr, 0, &completionCode);
1059 ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1060}
1061
1062TEST(NewFile, testGoodEncodeRequest)
1063{
1064 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_NEW_FILE_REQ_BYTES>
1065 requestMsg{};
1066
1067 uint16_t fileType = 0xFF;
1068 uint32_t fileHandle = 0x12345678;
1069 uint32_t length = 0x13245768;
Lei YU2d5c7452020-03-03 14:43:45 +08001070 uint16_t fileTypeLe = htole16(fileType);
1071 uint32_t fileHandleLe = htole32(fileHandle);
1072 uint32_t lengthLe = htole32(length);
vkaverapa9aac722019-08-22 02:10:15 -05001073
1074 pldm_msg* request = reinterpret_cast<pldm_msg*>(requestMsg.data());
1075
1076 auto rc = encode_new_file_req(0, fileType, fileHandle, length, request);
1077
1078 ASSERT_EQ(rc, PLDM_SUCCESS);
1079 ASSERT_EQ(request->hdr.request, PLDM_REQUEST);
1080 ASSERT_EQ(request->hdr.instance_id, 0);
1081 ASSERT_EQ(request->hdr.type, PLDM_OEM);
1082 ASSERT_EQ(request->hdr.command, PLDM_NEW_FILE_AVAILABLE);
Lei YU2d5c7452020-03-03 14:43:45 +08001083 ASSERT_EQ(0, memcmp(request->payload, &fileTypeLe, sizeof(fileTypeLe)));
1084 ASSERT_EQ(0, memcmp(request->payload + sizeof(fileTypeLe), &fileHandleLe,
1085 sizeof(fileHandleLe)));
1086 ASSERT_EQ(
1087 0, memcmp(request->payload + sizeof(fileTypeLe) + sizeof(fileHandleLe),
1088 &lengthLe, sizeof(lengthLe)));
vkaverapa9aac722019-08-22 02:10:15 -05001089}
1090
1091TEST(NewFile, testGoodEncodeResponse)
1092{
1093 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_NEW_FILE_RESP_BYTES>
1094 responseMsg{};
1095
1096 uint8_t completionCode = 0x0;
1097
1098 pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1099
1100 auto rc = encode_new_file_resp(0, completionCode, response);
1101
1102 ASSERT_EQ(rc, PLDM_SUCCESS);
1103 ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
1104 ASSERT_EQ(response->hdr.instance_id, 0);
1105 ASSERT_EQ(response->hdr.type, PLDM_OEM);
1106 ASSERT_EQ(response->hdr.command, PLDM_NEW_FILE_AVAILABLE);
1107 ASSERT_EQ(
1108 0, memcmp(response->payload, &completionCode, sizeof(completionCode)));
1109}
1110
1111TEST(NewFile, testBadEncodeResponse)
1112{
1113 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_NEW_FILE_RESP_BYTES>
1114 responseMsg{};
1115 pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1116
1117 // completion code is PLDM_ERROR
1118 auto rc = encode_new_file_resp(0, PLDM_ERROR, response);
1119
1120 ASSERT_EQ(rc, PLDM_SUCCESS);
1121 ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
1122 ASSERT_EQ(response->hdr.instance_id, 0);
1123 ASSERT_EQ(response->hdr.type, PLDM_OEM);
1124 ASSERT_EQ(response->hdr.command, PLDM_NEW_FILE_AVAILABLE);
1125 ASSERT_EQ(response->payload[0], PLDM_ERROR);
1126
1127 // response is NULL pointer
1128 rc = encode_new_file_resp(0, PLDM_SUCCESS, NULL);
1129
1130 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1131}
1132
1133TEST(NewFile, testBadEncodeRequest)
1134{
1135 uint8_t fileType = 0xFF;
1136 uint32_t fileHandle = 0;
1137 uint32_t length = 0;
1138
1139 // request is NULL pointer
1140 auto rc = encode_new_file_req(0, fileType, fileHandle, length, NULL);
1141
1142 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1143}
Deepak Kodihallidce1c992019-11-19 07:06:53 -06001144
1145TEST(ReadWriteFileByType, testGoodDecodeRequest)
1146{
1147 std::array<uint8_t, PLDM_RW_FILE_BY_TYPE_REQ_BYTES + sizeof(pldm_msg_hdr)>
1148 requestMsg{};
1149
1150 auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
1151 size_t payload_length = requestMsg.size() - sizeof(pldm_msg_hdr);
1152 auto request = reinterpret_cast<pldm_read_write_file_by_type_req*>(
1153 requestPtr->payload);
1154
1155 // Random value for fileHandle, offset and length
1156 uint16_t fileType = 0;
1157 uint32_t fileHandle = 0x12345678;
1158 uint32_t offset = 0x87654321;
1159 uint32_t length = 0x13245768;
1160
Lei YU2d5c7452020-03-03 14:43:45 +08001161 request->file_handle = htole32(fileHandle);
1162 request->offset = htole32(offset);
1163 request->length = htole32(length);
Deepak Kodihallidce1c992019-11-19 07:06:53 -06001164
1165 uint16_t retFileType = 0x1;
1166 uint32_t retFileHandle = 0;
1167 uint32_t retOffset = 0;
1168 uint32_t retLength = 0;
1169
1170 // Invoke decode the read file request
1171 auto rc =
1172 decode_rw_file_by_type_req(requestPtr, payload_length, &retFileType,
1173 &retFileHandle, &retOffset, &retLength);
1174
1175 ASSERT_EQ(rc, PLDM_SUCCESS);
1176 ASSERT_EQ(fileType, retFileType);
1177 ASSERT_EQ(fileHandle, retFileHandle);
1178 ASSERT_EQ(offset, retOffset);
1179 ASSERT_EQ(length, retLength);
1180}
1181
1182TEST(ReadWriteFileByType, testGoodDecodeResponse)
1183{
1184 std::array<uint8_t, PLDM_RW_FILE_BY_TYPE_RESP_BYTES + sizeof(pldm_msg_hdr)>
1185 responseMsg{};
1186
1187 auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
1188 size_t payload_length = responseMsg.size() - sizeof(pldm_msg_hdr);
1189 auto response = reinterpret_cast<pldm_read_write_file_by_type_resp*>(
1190 responsePtr->payload);
1191
1192 // Random value for completion code and length
1193 uint8_t completionCode = 0x0;
1194 uint32_t length = 0x13245768;
1195
1196 response->completion_code = completionCode;
Lei YU2d5c7452020-03-03 14:43:45 +08001197 response->length = htole32(length);
Deepak Kodihallidce1c992019-11-19 07:06:53 -06001198
1199 uint8_t retCompletionCode = 0x1;
1200 uint32_t retLength = 0;
1201
1202 // Invoke decode the read/write file response
1203 auto rc = decode_rw_file_by_type_resp(responsePtr, payload_length,
1204 &retCompletionCode, &retLength);
1205
1206 ASSERT_EQ(rc, PLDM_SUCCESS);
1207 ASSERT_EQ(completionCode, retCompletionCode);
1208 ASSERT_EQ(length, retLength);
1209}
1210
1211TEST(ReadWriteFileByType, testBadDecodeRequest)
1212{
1213 uint16_t fileType = 0;
1214 uint32_t fileHandle = 0;
1215 uint32_t offset = 0;
1216 uint32_t length = 0;
1217
1218 // Request payload message is missing
1219 auto rc = decode_rw_file_by_type_req(NULL, 0, &fileType, &fileHandle,
1220 &offset, &length);
1221 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1222
1223 std::array<uint8_t, PLDM_RW_FILE_BY_TYPE_REQ_BYTES + sizeof(pldm_msg_hdr)>
1224 requestMsg{};
1225
1226 auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
1227
1228 // Payload length is invalid
1229 rc = decode_rw_file_by_type_req(requestPtr, 0, &fileType, &fileHandle,
1230 &offset, &length);
1231 ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1232}
1233
1234TEST(ReadWriteFileByType, testBadDecodeResponse)
1235{
1236 uint32_t length = 0;
1237 uint8_t completionCode = 0;
1238
1239 // Request payload message is missing
1240 auto rc = decode_rw_file_by_type_resp(NULL, 0, &completionCode, &length);
1241 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1242
1243 std::array<uint8_t, PLDM_RW_FILE_BY_TYPE_RESP_BYTES + sizeof(pldm_msg_hdr)>
1244 responseMsg{};
1245
1246 auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
1247
1248 // Length is NULL
1249 rc = decode_rw_file_by_type_resp(responsePtr, responseMsg.size() - hdrSize,
1250 &completionCode, NULL);
1251 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1252
1253 // Payload length is invalid
1254 rc = decode_rw_file_by_type_resp(responsePtr, 0, &completionCode, &length);
1255 ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1256}
1257
1258TEST(ReadWriteFileByType, testGoodEncodeRequest)
1259{
1260 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_RW_FILE_BY_TYPE_REQ_BYTES>
1261 requestMsg{};
1262
1263 uint16_t fileType = 0;
1264 uint32_t fileHandle = 0x12345678;
1265 uint32_t offset = 0x87654321;
1266 uint32_t length = 0x13245768;
Lei YU2d5c7452020-03-03 14:43:45 +08001267 uint16_t fileTypeLe = htole16(fileType);
1268 uint32_t fileHandleLe = htole32(fileHandle);
1269 uint32_t offsetLe = htole32(offset);
1270 uint32_t lengthLe = htole32(length);
Deepak Kodihallidce1c992019-11-19 07:06:53 -06001271
1272 pldm_msg* request = reinterpret_cast<pldm_msg*>(requestMsg.data());
1273
1274 auto rc = encode_rw_file_by_type_req(0, PLDM_READ_FILE_BY_TYPE, fileType,
1275 fileHandle, offset, length, request);
1276
1277 ASSERT_EQ(rc, PLDM_SUCCESS);
1278 ASSERT_EQ(request->hdr.request, PLDM_REQUEST);
1279 ASSERT_EQ(request->hdr.instance_id, 0);
1280 ASSERT_EQ(request->hdr.type, PLDM_OEM);
1281 ASSERT_EQ(request->hdr.command, PLDM_READ_FILE_BY_TYPE);
1282
Lei YU2d5c7452020-03-03 14:43:45 +08001283 ASSERT_EQ(0, memcmp(request->payload, &fileTypeLe, sizeof(fileTypeLe)));
1284 ASSERT_EQ(0, memcmp(request->payload + sizeof(fileTypeLe), &fileHandleLe,
1285 sizeof(fileHandleLe)));
Deepak Kodihallidce1c992019-11-19 07:06:53 -06001286
Lei YU2d5c7452020-03-03 14:43:45 +08001287 ASSERT_EQ(
1288 0, memcmp(request->payload + sizeof(fileTypeLe) + sizeof(fileHandleLe),
1289 &offsetLe, sizeof(offsetLe)));
1290 ASSERT_EQ(0, memcmp(request->payload + sizeof(fileTypeLe) +
1291 sizeof(fileHandleLe) + sizeof(offsetLe),
1292 &lengthLe, sizeof(lengthLe)));
Deepak Kodihallidce1c992019-11-19 07:06:53 -06001293}
1294
1295TEST(ReadWriteFileByType, testGoodEncodeResponse)
1296{
1297 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_RW_FILE_BY_TYPE_RESP_BYTES>
1298 responseMsg{};
1299
1300 uint32_t length = 0x13245768;
Lei YU2d5c7452020-03-03 14:43:45 +08001301 uint32_t lengthLe = htole32(length);
Deepak Kodihallidce1c992019-11-19 07:06:53 -06001302 uint8_t completionCode = 0x0;
Deepak Kodihallidce1c992019-11-19 07:06:53 -06001303 pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1304
1305 auto rc = encode_rw_file_by_type_resp(0, PLDM_READ_FILE_BY_TYPE,
1306 completionCode, length, response);
1307
1308 ASSERT_EQ(rc, PLDM_SUCCESS);
1309 ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
1310 ASSERT_EQ(response->hdr.instance_id, 0);
1311 ASSERT_EQ(response->hdr.type, PLDM_OEM);
1312 ASSERT_EQ(response->hdr.command, PLDM_READ_FILE_BY_TYPE);
1313
1314 ASSERT_EQ(
1315 0, memcmp(response->payload, &completionCode, sizeof(completionCode)));
Lei YU2d5c7452020-03-03 14:43:45 +08001316 ASSERT_EQ(0, memcmp(response->payload + sizeof(completionCode), &lengthLe,
1317 sizeof(lengthLe)));
Deepak Kodihallidce1c992019-11-19 07:06:53 -06001318}
1319
1320TEST(ReadWriteFileByType, testBadEncodeResponse)
1321{
1322 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_RW_FILE_BY_TYPE_RESP_BYTES>
1323 responseMsg{};
1324 uint32_t length = 0;
1325 pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1326
1327 // completion code is PLDM_ERROR
1328 auto rc = encode_rw_file_by_type_resp(0, PLDM_READ_FILE_BY_TYPE, PLDM_ERROR,
1329 length, response);
1330
1331 ASSERT_EQ(rc, PLDM_SUCCESS);
1332 ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
1333 ASSERT_EQ(response->hdr.instance_id, 0);
1334 ASSERT_EQ(response->hdr.type, PLDM_OEM);
1335 ASSERT_EQ(response->hdr.command, PLDM_READ_FILE_BY_TYPE);
1336 ASSERT_EQ(response->payload[0], PLDM_ERROR);
1337
1338 // response is NULL pointer
1339 rc = encode_rw_file_by_type_resp(0, PLDM_READ_FILE_BY_TYPE, PLDM_SUCCESS,
1340 length, NULL);
1341
1342 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1343}
1344
1345TEST(ReadWriteFileByType, testBadEncodeRequest)
1346{
1347 uint8_t fileType = 0;
1348 uint32_t fileHandle = 0;
1349 uint32_t offset = 0;
1350 uint32_t length = 0;
1351
1352 // request is NULL pointer
1353 auto rc = encode_rw_file_by_type_req(0, PLDM_READ_FILE_BY_TYPE, fileType,
1354 fileHandle, offset, length, NULL);
1355
1356 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1357}
vkaverapf4e0a492019-11-19 01:47:35 -06001358
1359TEST(FileAck, testGoodDecodeRequest)
1360{
1361 std::array<uint8_t, PLDM_FILE_ACK_REQ_BYTES + sizeof(pldm_msg_hdr)>
1362 requestMsg{};
1363
1364 auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
1365 size_t payload_length = requestMsg.size() - sizeof(pldm_msg_hdr);
1366 auto request = reinterpret_cast<pldm_file_ack_req*>(requestPtr->payload);
1367
1368 // Random value for fileHandle
1369 uint16_t fileType = 0xFFFF;
1370 uint32_t fileHandle = 0x12345678;
Lei YU2d5c7452020-03-03 14:43:45 +08001371 uint8_t fileStatus = 0xFF;
vkaverapf4e0a492019-11-19 01:47:35 -06001372
Lei YU2d5c7452020-03-03 14:43:45 +08001373 request->file_type = htole16(fileType);
1374 request->file_handle = htole32(fileHandle);
vkaverapf4e0a492019-11-19 01:47:35 -06001375 request->file_status = fileStatus;
1376
1377 uint16_t retFileType = 0xFF;
1378 uint32_t retFileHandle = 0;
1379 uint8_t retFileStatus = 0;
1380
1381 // Invoke decode the read file request
1382 auto rc = decode_file_ack_req(requestPtr, payload_length, &retFileType,
1383 &retFileHandle, &retFileStatus);
1384
1385 ASSERT_EQ(rc, PLDM_SUCCESS);
1386 ASSERT_EQ(fileType, retFileType);
1387 ASSERT_EQ(fileHandle, retFileHandle);
1388 ASSERT_EQ(fileStatus, retFileStatus);
1389}
1390
1391TEST(FileAck, testGoodDecodeResponse)
1392{
1393 std::array<uint8_t, PLDM_FILE_ACK_RESP_BYTES + sizeof(pldm_msg_hdr)>
1394 responseMsg{};
1395
1396 auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
1397 size_t payload_length = responseMsg.size() - sizeof(pldm_msg_hdr);
1398 auto response = reinterpret_cast<pldm_file_ack_resp*>(responsePtr->payload);
1399
1400 // Random value for completion code
1401 uint8_t completionCode = 0x0;
1402
1403 response->completion_code = completionCode;
1404
1405 uint8_t retCompletionCode = PLDM_SUCCESS;
1406
1407 // Invoke decode the read/write file response
1408 auto rc =
1409 decode_file_ack_resp(responsePtr, payload_length, &retCompletionCode);
1410
1411 ASSERT_EQ(rc, PLDM_SUCCESS);
1412 ASSERT_EQ(completionCode, retCompletionCode);
1413}
1414
1415TEST(FileAck, testBadDecodeRequest)
1416{
1417 uint16_t fileType = 0;
1418 uint32_t fileHandle = 0;
1419 uint8_t fileStatus = 0;
1420
1421 // Request payload message is missing
1422 auto rc = decode_file_ack_req(NULL, 0, &fileType, &fileHandle, &fileStatus);
1423
1424 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1425
1426 std::array<uint8_t, PLDM_FILE_ACK_REQ_BYTES + sizeof(pldm_msg_hdr)>
1427 requestMsg{};
1428
1429 auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
1430
1431 // Payload length is invalid
1432 rc =
1433 decode_file_ack_req(requestPtr, 0, &fileType, &fileHandle, &fileStatus);
1434 ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1435}
1436
1437TEST(FileAck, testBadDecodeResponse)
1438{
1439 uint8_t completionCode = 0;
1440
1441 // Request payload message is missing
1442 auto rc = decode_file_ack_resp(NULL, 0, &completionCode);
1443 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1444
1445 std::array<uint8_t, PLDM_FILE_ACK_RESP_BYTES + sizeof(pldm_msg_hdr)>
1446 responseMsg{};
1447
1448 auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
1449
1450 // Payload length is invalid
1451 rc = decode_file_ack_resp(responsePtr, 0, &completionCode);
1452 ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1453}
1454
1455TEST(FileAck, testGoodEncodeRequest)
1456{
1457 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_FILE_ACK_REQ_BYTES>
1458 requestMsg{};
1459
1460 uint16_t fileType = 0xFFFF;
1461 uint32_t fileHandle = 0x12345678;
1462 uint8_t fileStatus = 0xFF;
Lei YU2d5c7452020-03-03 14:43:45 +08001463 uint16_t fileTypeLe = htole16(fileType);
1464 uint32_t fileHandleLe = htole32(fileHandle);
vkaverapf4e0a492019-11-19 01:47:35 -06001465
1466 pldm_msg* request = reinterpret_cast<pldm_msg*>(requestMsg.data());
1467
1468 auto rc = encode_file_ack_req(0, fileType, fileHandle, fileStatus, request);
1469
1470 ASSERT_EQ(rc, PLDM_SUCCESS);
1471 ASSERT_EQ(request->hdr.request, PLDM_REQUEST);
1472 ASSERT_EQ(request->hdr.instance_id, 0);
1473 ASSERT_EQ(request->hdr.type, PLDM_OEM);
1474 ASSERT_EQ(request->hdr.command, PLDM_FILE_ACK);
Lei YU2d5c7452020-03-03 14:43:45 +08001475 ASSERT_EQ(0, memcmp(request->payload, &fileTypeLe, sizeof(fileTypeLe)));
1476 ASSERT_EQ(0, memcmp(request->payload + sizeof(fileTypeLe), &fileHandleLe,
1477 sizeof(fileHandleLe)));
vkaverapf4e0a492019-11-19 01:47:35 -06001478}
1479
1480TEST(FileAck, testGoodEncodeResponse)
1481{
1482 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_FILE_ACK_RESP_BYTES>
1483 responseMsg{};
1484
1485 uint8_t completionCode = 0x0;
1486
1487 pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1488
1489 auto rc = encode_file_ack_resp(0, completionCode, response);
1490
1491 ASSERT_EQ(rc, PLDM_SUCCESS);
1492 ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
1493 ASSERT_EQ(response->hdr.instance_id, 0);
1494 ASSERT_EQ(response->hdr.type, PLDM_OEM);
1495 ASSERT_EQ(response->hdr.command, PLDM_FILE_ACK);
1496 ASSERT_EQ(
1497 0, memcmp(response->payload, &completionCode, sizeof(completionCode)));
1498}
1499
1500TEST(FileAck, testBadEncodeResponse)
1501{
1502 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_FILE_ACK_RESP_BYTES>
1503 responseMsg{};
1504 pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1505
1506 // completion code is PLDM_ERROR
1507 auto rc = encode_file_ack_resp(0, PLDM_ERROR, response);
1508
1509 ASSERT_EQ(rc, PLDM_SUCCESS);
1510 ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
1511 ASSERT_EQ(response->hdr.instance_id, 0);
1512 ASSERT_EQ(response->hdr.type, PLDM_OEM);
1513 ASSERT_EQ(response->hdr.command, PLDM_FILE_ACK);
1514 ASSERT_EQ(response->payload[0], PLDM_ERROR);
1515
1516 // response is NULL pointer
1517 rc = encode_file_ack_resp(0, PLDM_SUCCESS, NULL);
1518
1519 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1520}
1521
1522TEST(FileAck, testBadEncodeRequest)
1523{
1524 uint8_t fileType = 0xFF;
1525 uint32_t fileHandle = 0;
1526 uint8_t fileStatus = 0;
1527
1528 // request is NULL pointer
1529 auto rc = encode_file_ack_req(0, fileType, fileHandle, fileStatus, nullptr);
1530
1531 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1532}