blob: ae59fb24009a8b40adc7ce8ac7cf81e4bd305a50 [file] [log] [blame]
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +05301#include <string.h>
2
3#include <array>
4
5#include "libpldm/base.h"
6#include "libpldm/file_io.h"
7
8#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;
21
Zahed Hossain223a73d2019-07-04 12:46:18 -050022 memcpy(requestMsg.data() + hdrSize, &fileHandle, sizeof(fileHandle));
23 memcpy(requestMsg.data() + sizeof(fileHandle) + hdrSize, &offset,
24 sizeof(offset));
25 memcpy(requestMsg.data() + sizeof(fileHandle) + sizeof(offset) + hdrSize,
26 &length, sizeof(length));
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053027 memcpy(requestMsg.data() + sizeof(fileHandle) + sizeof(offset) +
Zahed Hossain223a73d2019-07-04 12:46:18 -050028 sizeof(length) + hdrSize,
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053029 &address, sizeof(address));
30
31 uint32_t retFileHandle = 0;
32 uint32_t retOffset = 0;
33 uint32_t retLength = 0;
34 uint64_t retAddress = 0;
35
Zahed Hossain223a73d2019-07-04 12:46:18 -050036 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
37
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053038 // Invoke decode the read file memory request
Zahed Hossain223a73d2019-07-04 12:46:18 -050039 auto rc = decode_rw_file_memory_req(request, requestMsg.size() - hdrSize,
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053040 &retFileHandle, &retOffset, &retLength,
41 &retAddress);
42
43 ASSERT_EQ(rc, PLDM_SUCCESS);
44 ASSERT_EQ(fileHandle, retFileHandle);
45 ASSERT_EQ(offset, retOffset);
46 ASSERT_EQ(length, retLength);
47 ASSERT_EQ(address, retAddress);
48}
49
50TEST(ReadWriteFileMemory, testBadDecodeRequest)
51{
52 uint32_t fileHandle = 0;
53 uint32_t offset = 0;
54 uint32_t length = 0;
55 uint64_t address = 0;
56
57 // Request payload message is missing
58 auto rc = decode_rw_file_memory_req(NULL, 0, &fileHandle, &offset, &length,
59 &address);
60 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
61
62 std::array<uint8_t, PLDM_RW_FILE_MEM_REQ_BYTES> requestMsg{};
63
Zahed Hossain223a73d2019-07-04 12:46:18 -050064 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
65
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053066 // Address is NULL
Zahed Hossain223a73d2019-07-04 12:46:18 -050067 rc = decode_rw_file_memory_req(request, requestMsg.size() - hdrSize,
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053068 &fileHandle, &offset, &length, NULL);
69 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
70
71 // Payload length is invalid
Zahed Hossain223a73d2019-07-04 12:46:18 -050072 rc = decode_rw_file_memory_req(request, 0, &fileHandle, &offset, &length,
73 &address);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053074 ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
75}
76
77TEST(ReadWriteFileMemory, testGoodEncodeResponse)
78{
79 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_RW_FILE_MEM_RESP_BYTES>
80 responseMsg{};
81 uint32_t length = 0xFF00EE11;
82 pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
83
84 // ReadFileIntoMemory
85 auto rc = encode_rw_file_memory_resp(0, PLDM_READ_FILE_INTO_MEMORY,
86 PLDM_SUCCESS, length, response);
87
88 ASSERT_EQ(rc, PLDM_SUCCESS);
89 ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
90 ASSERT_EQ(response->hdr.instance_id, 0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050091 ASSERT_EQ(response->hdr.type, PLDM_OEM);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053092 ASSERT_EQ(response->hdr.command, PLDM_READ_FILE_INTO_MEMORY);
93 ASSERT_EQ(response->payload[0], PLDM_SUCCESS);
94 ASSERT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]),
95 &length, sizeof(length)));
96
97 // WriteFileFromMemory
98 rc = encode_rw_file_memory_resp(0, PLDM_WRITE_FILE_FROM_MEMORY,
99 PLDM_SUCCESS, length, response);
100
101 ASSERT_EQ(rc, PLDM_SUCCESS);
102 ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
103 ASSERT_EQ(response->hdr.instance_id, 0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500104 ASSERT_EQ(response->hdr.type, PLDM_OEM);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530105 ASSERT_EQ(response->hdr.command, PLDM_WRITE_FILE_FROM_MEMORY);
106 ASSERT_EQ(response->payload[0], PLDM_SUCCESS);
107 ASSERT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]),
108 &length, sizeof(length)));
109}
110
111TEST(ReadWriteFileMemory, testBadEncodeResponse)
112{
113 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_RW_FILE_MEM_RESP_BYTES>
114 responseMsg{};
115 uint32_t length = 0;
116 pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
117
118 // ReadFileIntoMemory
119 auto rc = encode_rw_file_memory_resp(0, PLDM_READ_FILE_INTO_MEMORY,
120 PLDM_ERROR, length, response);
121
122 ASSERT_EQ(rc, PLDM_SUCCESS);
123 ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
124 ASSERT_EQ(response->hdr.instance_id, 0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500125 ASSERT_EQ(response->hdr.type, PLDM_OEM);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530126 ASSERT_EQ(response->hdr.command, PLDM_READ_FILE_INTO_MEMORY);
127 ASSERT_EQ(response->payload[0], PLDM_ERROR);
128
129 // WriteFileFromMemory
130 rc = encode_rw_file_memory_resp(0, PLDM_WRITE_FILE_FROM_MEMORY, PLDM_ERROR,
131 length, response);
132
133 ASSERT_EQ(rc, PLDM_SUCCESS);
134 ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
135 ASSERT_EQ(response->hdr.instance_id, 0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500136 ASSERT_EQ(response->hdr.type, PLDM_OEM);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530137 ASSERT_EQ(response->hdr.command, PLDM_WRITE_FILE_FROM_MEMORY);
138 ASSERT_EQ(response->payload[0], PLDM_ERROR);
139}
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530140
Priyanga8b976652019-06-27 11:30:33 -0500141TEST(ReadWriteFileIntoMemory, testGoodDecodeResponse)
142{
Zahed Hossain223a73d2019-07-04 12:46:18 -0500143 std::array<uint8_t, PLDM_RW_FILE_MEM_RESP_BYTES + hdrSize> responseMsg{};
Priyanga8b976652019-06-27 11:30:33 -0500144 // Random value for length
145 uint32_t length = 0xFF00EE12;
146 uint8_t completionCode = 0;
147
Zahed Hossain223a73d2019-07-04 12:46:18 -0500148 memcpy(responseMsg.data() + hdrSize, &completionCode,
149 sizeof(completionCode));
150 memcpy(responseMsg.data() + sizeof(completionCode) + hdrSize, &length,
Priyanga8b976652019-06-27 11:30:33 -0500151 sizeof(length));
152
153 uint8_t retCompletionCode = 0;
154 uint32_t retLength = 0;
155
Zahed Hossain223a73d2019-07-04 12:46:18 -0500156 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
157
Priyanga8b976652019-06-27 11:30:33 -0500158 // Invoke decode the read file memory response
Zahed Hossain223a73d2019-07-04 12:46:18 -0500159 auto rc = decode_rw_file_memory_resp(response, responseMsg.size() - hdrSize,
Priyanga8b976652019-06-27 11:30:33 -0500160 &retCompletionCode, &retLength);
161 ASSERT_EQ(rc, PLDM_SUCCESS);
162 ASSERT_EQ(completionCode, retCompletionCode);
163 ASSERT_EQ(length, retLength);
164}
165
166TEST(ReadWriteFileIntoMemory, testBadDecodeResponse)
167{
168 uint32_t length = 0;
169 uint8_t completionCode = 0;
170
171 // Request payload message is missing
172 auto rc = decode_rw_file_memory_resp(NULL, 0, &completionCode, &length);
173 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
174
175 std::array<uint8_t, PLDM_RW_FILE_MEM_RESP_BYTES> responseMsg{};
176
Zahed Hossain223a73d2019-07-04 12:46:18 -0500177 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
178
Priyanga8b976652019-06-27 11:30:33 -0500179 // Payload length is invalid
Zahed Hossain223a73d2019-07-04 12:46:18 -0500180 rc = decode_rw_file_memory_resp(response, 0, &completionCode, &length);
Priyanga8b976652019-06-27 11:30:33 -0500181 ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
182}
183
184TEST(ReadWriteFileIntoMemory, testGoodEncodeRequest)
185{
186 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_RW_FILE_MEM_REQ_BYTES>
187 requestMsg{};
188
189 uint32_t fileHandle = 0x12345678;
190 uint32_t offset = 0x87654321;
191 uint32_t length = 0x13245768;
192 uint64_t address = 0x124356879ACBDE0F;
193
194 pldm_msg* request = reinterpret_cast<pldm_msg*>(requestMsg.data());
195
196 auto rc =
197 encode_rw_file_memory_req(0, PLDM_READ_FILE_INTO_MEMORY, fileHandle,
198 offset, length, address, request);
199
200 ASSERT_EQ(rc, PLDM_SUCCESS);
201 ASSERT_EQ(request->hdr.request, PLDM_REQUEST);
202 ASSERT_EQ(request->hdr.instance_id, 0);
203 ASSERT_EQ(request->hdr.type, PLDM_OEM);
204 ASSERT_EQ(request->hdr.command, PLDM_READ_FILE_INTO_MEMORY);
205
206 ASSERT_EQ(0, memcmp(request->payload, &fileHandle, sizeof(fileHandle)));
207
208 ASSERT_EQ(0, memcmp(request->payload + sizeof(fileHandle), &offset,
209 sizeof(offset)));
210 ASSERT_EQ(0, memcmp(request->payload + sizeof(fileHandle) + sizeof(offset),
211 &length, sizeof(length)));
212 ASSERT_EQ(0, memcmp(request->payload + sizeof(fileHandle) + sizeof(offset) +
213 sizeof(length),
214 &address, sizeof(address)));
215}
216
217TEST(ReadWriteFileIntoMemory, testBadEncodeRequest)
218{
219 uint32_t fileHandle = 0;
220 uint32_t offset = 0;
221 uint32_t length = 0;
222 uint64_t address = 0;
223
224 auto rc =
225 encode_rw_file_memory_req(0, PLDM_READ_FILE_INTO_MEMORY, fileHandle,
226 offset, length, address, NULL);
227
228 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
229}
230
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530231TEST(GetFileTable, GoodDecodeRequest)
232{
Zahed Hossain223a73d2019-07-04 12:46:18 -0500233 std::array<uint8_t, PLDM_GET_FILE_TABLE_REQ_BYTES + hdrSize> requestMsg{};
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530234
235 // Random value for DataTransferHandle, TransferOperationFlag, TableType
236 uint32_t transferHandle = 0x12345678;
237 uint8_t transferOpFlag = 1;
238 uint8_t tableType = 1;
239
Zahed Hossain223a73d2019-07-04 12:46:18 -0500240 memcpy(requestMsg.data() + hdrSize, &transferHandle,
241 sizeof(transferHandle));
242 memcpy(requestMsg.data() + sizeof(transferHandle) + hdrSize,
243 &transferOpFlag, sizeof(transferOpFlag));
244 memcpy(requestMsg.data() + sizeof(transferHandle) + sizeof(transferOpFlag) +
245 hdrSize,
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530246 &tableType, sizeof(tableType));
247
248 uint32_t retTransferHandle = 0;
249 uint8_t retTransferOpFlag = 0;
250 uint8_t retTableType = 0;
251
Zahed Hossain223a73d2019-07-04 12:46:18 -0500252 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
253
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530254 // Invoke decode get file table request
Zahed Hossain223a73d2019-07-04 12:46:18 -0500255 auto rc = decode_get_file_table_req(request, requestMsg.size() - hdrSize,
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530256 &retTransferHandle, &retTransferOpFlag,
257 &retTableType);
258
259 ASSERT_EQ(rc, PLDM_SUCCESS);
260 ASSERT_EQ(transferHandle, retTransferHandle);
261 ASSERT_EQ(transferOpFlag, retTransferOpFlag);
262 ASSERT_EQ(tableType, retTableType);
263}
264
265TEST(GetFileTable, BadDecodeRequest)
266{
267 uint32_t transferHandle = 0;
268 uint8_t transferOpFlag = 0;
269 uint8_t tableType = 0;
270
271 // Request payload message is missing
272 auto rc = decode_get_file_table_req(nullptr, 0, &transferHandle,
273 &transferOpFlag, &tableType);
274 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
275
276 std::array<uint8_t, PLDM_GET_FILE_TABLE_REQ_BYTES> requestMsg{};
277
Zahed Hossain223a73d2019-07-04 12:46:18 -0500278 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
279
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530280 // TableType is NULL
Zahed Hossain223a73d2019-07-04 12:46:18 -0500281 rc = decode_get_file_table_req(request, requestMsg.size() - hdrSize,
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530282 &transferHandle, &transferOpFlag, nullptr);
283 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
284
285 // Payload length is invalid
Zahed Hossain223a73d2019-07-04 12:46:18 -0500286 rc = decode_get_file_table_req(request, 0, &transferHandle, &transferOpFlag,
287 &tableType);
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530288 ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
289}
290
291TEST(GetFileTable, GoodEncodeResponse)
292{
293 // Random value for NextDataTransferHandle and TransferFlag
294 uint8_t completionCode = 0;
295 uint32_t nextTransferHandle = 0x87654321;
296 uint8_t transferFlag = 5;
297 // Mock file table contents of size 5
298 std::array<uint8_t, 5> fileTable = {1, 2, 3, 4, 5};
299 constexpr size_t responseSize = sizeof(completionCode) +
300 sizeof(nextTransferHandle) +
301 sizeof(transferFlag) + fileTable.size();
302
303 std::array<uint8_t, sizeof(pldm_msg_hdr) + responseSize> responseMsg{};
304 pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
305
306 // GetFileTable
307 auto rc = encode_get_file_table_resp(0, PLDM_SUCCESS, nextTransferHandle,
308 transferFlag, fileTable.data(),
309 fileTable.size(), response);
310
311 ASSERT_EQ(rc, PLDM_SUCCESS);
312 ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
313 ASSERT_EQ(response->hdr.instance_id, 0);
314 ASSERT_EQ(response->hdr.type, PLDM_OEM);
315 ASSERT_EQ(response->hdr.command, PLDM_GET_FILE_TABLE);
316 ASSERT_EQ(response->payload[0], PLDM_SUCCESS);
317 ASSERT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]),
318 &nextTransferHandle, sizeof(nextTransferHandle)));
319 ASSERT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]) +
320 sizeof(nextTransferHandle),
321 &transferFlag, sizeof(transferFlag)));
322 ASSERT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]) +
323 sizeof(nextTransferHandle),
324 &transferFlag, sizeof(transferFlag)));
325 ASSERT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]) +
326 sizeof(nextTransferHandle) + sizeof(transferFlag),
327 fileTable.data(), fileTable.size()));
328}
329
330TEST(GetFileTable, BadEncodeResponse)
331{
332 uint8_t completionCode = 0;
333 uint32_t nextTransferHandle = 0;
334 uint8_t transferFlag = 0;
335 constexpr size_t responseSize = sizeof(completionCode) +
336 sizeof(nextTransferHandle) +
337 sizeof(transferFlag);
338
339 std::array<uint8_t, sizeof(pldm_msg_hdr) + responseSize> responseMsg{};
340 pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
341
342 // GetFileTable
343 auto rc = encode_get_file_table_resp(0, PLDM_ERROR, nextTransferHandle,
344 transferFlag, nullptr, 0, response);
345
346 ASSERT_EQ(rc, PLDM_SUCCESS);
347 ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
348 ASSERT_EQ(response->hdr.instance_id, 0);
349 ASSERT_EQ(response->hdr.type, PLDM_OEM);
350 ASSERT_EQ(response->hdr.command, PLDM_GET_FILE_TABLE);
351 ASSERT_EQ(response->payload[0], PLDM_ERROR);
352}
vkaverap2ffe3292019-06-24 00:08:13 -0500353
354TEST(ReadFile, testGoodDecodeRequest)
355{
356 std::array<uint8_t, PLDM_READ_FILE_REQ_BYTES + sizeof(pldm_msg_hdr)>
357 requestMsg{};
358
359 auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
360 size_t payload_length = requestMsg.size() - sizeof(pldm_msg_hdr);
361 auto request = reinterpret_cast<pldm_read_file_req*>(requestPtr->payload);
362
363 // Random value for fileHandle, offset and length
364 uint32_t fileHandle = 0x12345678;
365 uint32_t offset = 0x87654321;
366 uint32_t length = 0x13245768;
367
368 request->file_handle = fileHandle;
369 request->offset = offset;
370 request->length = length;
371
372 uint32_t retFileHandle = 0;
373 uint32_t retOffset = 0;
374 uint32_t retLength = 0;
375
376 // Invoke decode the read file request
377 auto rc = decode_read_file_req(requestPtr, payload_length, &retFileHandle,
378 &retOffset, &retLength);
379
380 ASSERT_EQ(rc, PLDM_SUCCESS);
381 ASSERT_EQ(fileHandle, retFileHandle);
382 ASSERT_EQ(offset, retOffset);
383 ASSERT_EQ(length, retLength);
384}
385
386TEST(WriteFile, testGoodDecodeRequest)
387{
388 // Random value for fileHandle, offset, length and file data
389 uint32_t fileHandle = 0x12345678;
390 uint32_t offset = 0x87654321;
391 uint32_t length = 0x467;
392
393 std::vector<uint8_t> requestMsg(PLDM_WRITE_FILE_REQ_BYTES +
394 sizeof(pldm_msg_hdr) + length);
395 auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
396 size_t payload_length = requestMsg.size() - sizeof(pldm_msg_hdr);
397 auto request = reinterpret_cast<pldm_write_file_req*>(requestPtr->payload);
398
399 size_t fileDataOffset =
400 sizeof(fileHandle) + sizeof(offset) + sizeof(length);
401
402 request->file_handle = fileHandle;
403 request->offset = offset;
404 request->length = length;
405
406 uint32_t retFileHandle = 0;
407 uint32_t retOffset = 0;
408 uint32_t retLength = 0;
409 size_t retFileDataOffset = 0;
410
411 // Invoke decode the write file request
412 auto rc = decode_write_file_req(requestPtr, payload_length, &retFileHandle,
413 &retOffset, &retLength, &retFileDataOffset);
414
415 ASSERT_EQ(rc, PLDM_SUCCESS);
416 ASSERT_EQ(fileHandle, retFileHandle);
417 ASSERT_EQ(offset, retOffset);
418 ASSERT_EQ(length, retLength);
419 ASSERT_EQ(fileDataOffset, retFileDataOffset);
420}
421
422TEST(ReadFile, testGoodDecodeResponse)
423{
424 // Random value for length
425 uint32_t length = 0x10;
426 uint8_t completionCode = PLDM_SUCCESS;
427
428 std::vector<uint8_t> responseMsg(PLDM_READ_FILE_RESP_BYTES +
429 sizeof(pldm_msg_hdr) + length);
430 auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
431 size_t payload_length = responseMsg.size() - sizeof(pldm_msg_hdr);
432 auto response =
433 reinterpret_cast<pldm_read_file_resp*>(responsePtr->payload);
434
435 response->completion_code = completionCode;
436 response->length = length;
437
438 size_t fileDataOffset = sizeof(completionCode) + sizeof(length);
439
440 uint32_t retLength = 0;
441 uint8_t retCompletionCode = 0;
442 size_t retFileDataOffset = 0;
443
444 // Invoke decode the read file response
445 auto rc =
446 decode_read_file_resp(responsePtr, payload_length, &retCompletionCode,
447 &retLength, &retFileDataOffset);
448
449 ASSERT_EQ(rc, PLDM_SUCCESS);
450 ASSERT_EQ(completionCode, retCompletionCode);
451 ASSERT_EQ(length, retLength);
452 ASSERT_EQ(fileDataOffset, retFileDataOffset);
453}
454
455TEST(WriteFile, testGoodDecodeResponse)
456{
457 std::array<uint8_t, PLDM_WRITE_FILE_RESP_BYTES + sizeof(pldm_msg_hdr)>
458 responseMsg{};
459 auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
460 size_t payload_length = responseMsg.size() - sizeof(pldm_msg_hdr);
461 auto response =
462 reinterpret_cast<pldm_write_file_resp*>(responsePtr->payload);
463
464 uint8_t completionCode = PLDM_SUCCESS;
465 uint32_t length = 0x4678;
466
467 response->completion_code = completionCode;
468 response->length = length;
469
470 uint32_t retLength = 0;
471 uint8_t retCompletionCode = 0;
472
473 // Invoke decode the write file response
474 auto rc = decode_write_file_resp(responsePtr, payload_length,
475 &retCompletionCode, &retLength);
476
477 ASSERT_EQ(rc, PLDM_SUCCESS);
478 ASSERT_EQ(completionCode, retCompletionCode);
479 ASSERT_EQ(length, retLength);
480}
481
482TEST(ReadWriteFile, testBadDecodeResponse)
483{
484 uint32_t length = 0;
485 uint8_t completionCode = 0;
486 size_t fileDataOffset = 0;
487
488 // Bad decode response for read file
489 std::vector<uint8_t> responseMsg(PLDM_READ_FILE_RESP_BYTES +
490 sizeof(pldm_msg_hdr) + length);
491 auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
492
493 // Request payload message is missing
494 auto rc = decode_read_file_resp(NULL, 0, &completionCode, &length,
495 &fileDataOffset);
496 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
497
498 // Payload length is invalid
499 rc = decode_read_file_resp(responsePtr, 0, &completionCode, &length,
500 &fileDataOffset);
501 ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
502
503 // Bad decode response for write file
504 std::array<uint8_t, PLDM_WRITE_FILE_RESP_BYTES + sizeof(pldm_msg_hdr)>
505 responseMsgWr{};
506 auto responseWr = reinterpret_cast<pldm_msg*>(responseMsgWr.data());
507
508 // Request payload message is missing
509 rc = decode_write_file_resp(NULL, 0, &completionCode, &length);
510 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
511
512 // Payload length is invalid
513 rc = decode_write_file_resp(responseWr, 0, &completionCode, &length);
514 ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
515}
516
517TEST(ReadWriteFile, testBadDecodeRequest)
518{
519 uint32_t fileHandle = 0;
520 uint32_t offset = 0;
521 uint32_t length = 0;
522
523 // Bad decode request for read file
524 std::array<uint8_t, PLDM_READ_FILE_REQ_BYTES + sizeof(pldm_msg_hdr)>
525 requestMsg{};
526 auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
527
528 // Request payload message is missing
529 auto rc = decode_read_file_req(NULL, 0, &fileHandle, &offset, &length);
530 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
531
532 // Payload length is invalid
533 rc = decode_read_file_req(requestPtr, 0, &fileHandle, &offset, &length);
534 ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
535
536 // Bad decode request for write file
537 size_t fileDataOffset = 0;
538 std::array<uint8_t, PLDM_WRITE_FILE_REQ_BYTES> requestMsgWr{};
539 auto requestWr = reinterpret_cast<pldm_msg*>(requestMsgWr.data());
540
541 // Request payload message is missing
542 rc = decode_write_file_req(NULL, 0, &fileHandle, &offset, &length,
543 &fileDataOffset);
544 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
545
546 // Payload length is invalid
547 rc = decode_write_file_req(requestWr, 0, &fileHandle, &offset, &length,
548 &fileDataOffset);
549 ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
550}
551
552TEST(ReadFile, testGoodEncodeResponse)
553{
554 // Good encode response for read file
555 uint32_t length = 0x4;
556
557 std::vector<uint8_t> responseMsg(PLDM_READ_FILE_RESP_BYTES +
558 sizeof(pldm_msg_hdr) + length);
559 auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
560 auto response =
561 reinterpret_cast<pldm_read_file_resp*>(responsePtr->payload);
562
563 // ReadFile
564 auto rc = encode_read_file_resp(0, PLDM_SUCCESS, length, responsePtr);
565
566 ASSERT_EQ(rc, PLDM_SUCCESS);
567 ASSERT_EQ(responsePtr->hdr.request, PLDM_RESPONSE);
568 ASSERT_EQ(responsePtr->hdr.instance_id, 0);
569 ASSERT_EQ(responsePtr->hdr.type, PLDM_OEM);
570 ASSERT_EQ(responsePtr->hdr.command, PLDM_READ_FILE);
571 ASSERT_EQ(response->completion_code, PLDM_SUCCESS);
572 ASSERT_EQ(response->length, length);
573}
574
575TEST(WriteFile, testGoodEncodeResponse)
576{
577 uint32_t length = 0x467;
578
579 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_WRITE_FILE_RESP_BYTES>
580 responseMsg{};
581
582 auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
583 auto response =
584 reinterpret_cast<pldm_write_file_resp*>(responsePtr->payload);
585
586 // WriteFile
587 auto rc = encode_write_file_resp(0, PLDM_SUCCESS, length, responsePtr);
588 ASSERT_EQ(rc, PLDM_SUCCESS);
589 ASSERT_EQ(responsePtr->hdr.request, PLDM_RESPONSE);
590 ASSERT_EQ(responsePtr->hdr.instance_id, 0);
591 ASSERT_EQ(responsePtr->hdr.type, PLDM_OEM);
592 ASSERT_EQ(responsePtr->hdr.command, PLDM_WRITE_FILE);
593 ASSERT_EQ(response->completion_code, PLDM_SUCCESS);
594 ASSERT_EQ(response->length, length);
595}
596
597TEST(ReadFile, testGoodEncodeRequest)
598{
599 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_READ_FILE_REQ_BYTES>
600 requestMsg{};
601
602 uint32_t fileHandle = 0x12345678;
603 uint32_t offset = 0x87654321;
604 uint32_t length = 0x13245768;
605 auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
606 auto request = reinterpret_cast<pldm_read_file_req*>(requestPtr->payload);
607
608 // ReadFile
609 auto rc = encode_read_file_req(0, fileHandle, offset, length, requestPtr);
610
611 ASSERT_EQ(rc, PLDM_SUCCESS);
612 ASSERT_EQ(requestPtr->hdr.request, PLDM_REQUEST);
613 ASSERT_EQ(requestPtr->hdr.instance_id, 0);
614 ASSERT_EQ(requestPtr->hdr.type, PLDM_OEM);
615 ASSERT_EQ(requestPtr->hdr.command, PLDM_READ_FILE);
616 ASSERT_EQ(request->file_handle, fileHandle);
617 ASSERT_EQ(request->offset, offset);
618 ASSERT_EQ(request->length, length);
619}
620
621TEST(WriteFile, testGoodEncodeRequest)
622{
623 uint32_t fileHandle = 0x12345678;
624 uint32_t offset = 0x87654321;
625 uint32_t length = 0x456;
626
627 std::vector<uint8_t> requestMsg(PLDM_WRITE_FILE_REQ_BYTES +
628 sizeof(pldm_msg_hdr) + length);
629 auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
630 auto request = reinterpret_cast<pldm_write_file_req*>(requestPtr->payload);
631
632 // WriteFile
633 auto rc = encode_write_file_req(0, fileHandle, offset, length, requestPtr);
634
635 ASSERT_EQ(rc, PLDM_SUCCESS);
636 ASSERT_EQ(requestPtr->hdr.request, PLDM_REQUEST);
637 ASSERT_EQ(requestPtr->hdr.instance_id, 0);
638 ASSERT_EQ(requestPtr->hdr.type, PLDM_OEM);
639 ASSERT_EQ(requestPtr->hdr.command, PLDM_WRITE_FILE);
640 ASSERT_EQ(request->file_handle, fileHandle);
641 ASSERT_EQ(request->offset, offset);
642 ASSERT_EQ(request->length, length);
643}
644
645TEST(ReadWriteFile, testBadEncodeRequest)
646{
647 // Bad encode request for read file
648 uint32_t fileHandle = 0;
649 uint32_t offset = 0;
650 uint32_t length = 0;
651
652 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_READ_FILE_REQ_BYTES>
653 requestMsg{};
654 auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
655
656 // ReadFile check invalid file length
657 auto rc = encode_read_file_req(0, fileHandle, offset, length, requestPtr);
658
659 ASSERT_EQ(rc, PLDM_INVALID_READ_LENGTH);
660
661 // Bad encode request for write file
662 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_WRITE_FILE_REQ_BYTES>
663 requestMsgWr{};
664 auto requestWr = reinterpret_cast<pldm_msg*>(requestMsgWr.data());
665
666 // WriteFile check for invalid file length
667 rc = encode_write_file_req(0, fileHandle, offset, length, requestWr);
668
669 ASSERT_EQ(rc, PLDM_INVALID_WRITE_LENGTH);
670}
671
672TEST(ReadWriteFile, testBadEncodeResponse)
673{
674 // Bad encode response for read file
675 uint32_t length = 0;
676
677 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_READ_FILE_RESP_BYTES>
678 responseMsg{};
679 auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
680
681 // ReadFile
682 auto rc = encode_read_file_resp(0, PLDM_ERROR, length, responsePtr);
683
684 ASSERT_EQ(rc, PLDM_SUCCESS);
685 ASSERT_EQ(responsePtr->hdr.request, PLDM_RESPONSE);
686 ASSERT_EQ(responsePtr->hdr.instance_id, 0);
687 ASSERT_EQ(responsePtr->hdr.type, PLDM_OEM);
688 ASSERT_EQ(responsePtr->hdr.command, PLDM_READ_FILE);
689 ASSERT_EQ(responsePtr->payload[0], PLDM_ERROR);
690
691 // Bad encode response for write file
692 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_WRITE_FILE_RESP_BYTES>
693 responseMsgWr{};
694 auto responseWr = reinterpret_cast<pldm_msg*>(responseMsgWr.data());
695
696 // WriteFile
697 rc = encode_write_file_resp(0, PLDM_ERROR, length, responseWr);
698
699 ASSERT_EQ(rc, PLDM_SUCCESS);
700 ASSERT_EQ(responseWr->hdr.request, PLDM_RESPONSE);
701 ASSERT_EQ(responseWr->hdr.instance_id, 0);
702 ASSERT_EQ(responseWr->hdr.type, PLDM_OEM);
703 ASSERT_EQ(responseWr->hdr.command, PLDM_WRITE_FILE);
704 ASSERT_EQ(responseWr->payload[0], PLDM_ERROR);
705}
vkaverap07404562019-08-05 22:57:11 -0500706
707TEST(ReadWriteFileByTypeMemory, testGoodDecodeRequest)
708{
709 std::array<uint8_t,
710 PLDM_RW_FILE_BY_TYPE_MEM_REQ_BYTES + sizeof(pldm_msg_hdr)>
711 requestMsg{};
712
713 auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
714 size_t payload_length = requestMsg.size() - sizeof(pldm_msg_hdr);
715 auto request = reinterpret_cast<pldm_read_write_file_by_type_memory_req*>(
716 requestPtr->payload);
717
718 // Random value for fileHandle, offset and length
719 uint16_t fileType = 0;
720 uint32_t fileHandle = 0x12345678;
721 uint32_t offset = 0x87654321;
722 uint32_t length = 0x13245768;
723 uint64_t address = 0x124356879ACBD456;
724
725 request->file_type = fileType;
726 request->file_handle = fileHandle;
727 request->offset = offset;
728 request->length = length;
729 request->address = address;
730
731 uint16_t retFileType = 0x1;
732 uint32_t retFileHandle = 0;
733 uint32_t retOffset = 0;
734 uint32_t retLength = 0;
735 uint64_t retAddress = 0;
736
737 // Invoke decode the read file request
738 auto rc = decode_rw_file_by_type_memory_req(
739 requestPtr, payload_length, &retFileType, &retFileHandle, &retOffset,
740 &retLength, &retAddress);
741
742 ASSERT_EQ(rc, PLDM_SUCCESS);
743 ASSERT_EQ(fileType, retFileType);
744 ASSERT_EQ(fileHandle, retFileHandle);
745 ASSERT_EQ(offset, retOffset);
746 ASSERT_EQ(length, retLength);
747 ASSERT_EQ(address, retAddress);
748}
749
750TEST(ReadWriteFileByTypeMemory, testGoodDecodeResponse)
751{
752 std::array<uint8_t,
753 PLDM_RW_FILE_BY_TYPE_MEM_RESP_BYTES + sizeof(pldm_msg_hdr)>
754 responseMsg{};
755
756 auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
757 size_t payload_length = responseMsg.size() - sizeof(pldm_msg_hdr);
758 auto response = reinterpret_cast<pldm_read_write_file_by_type_memory_resp*>(
759 responsePtr->payload);
760
761 // Random value for completion code and length
762 uint8_t completionCode = 0x0;
763 uint32_t length = 0x13245768;
764
765 response->completion_code = completionCode;
766 response->length = length;
767
768 uint8_t retCompletionCode = 0x1;
769 uint32_t retLength = 0;
770
771 // Invoke decode the read/write file response
772 auto rc = decode_rw_file_by_type_memory_resp(
773 responsePtr, payload_length, &retCompletionCode, &retLength);
774
775 ASSERT_EQ(rc, PLDM_SUCCESS);
776 ASSERT_EQ(completionCode, retCompletionCode);
777 ASSERT_EQ(length, retLength);
778}
779
780TEST(ReadWriteFileByTypeMemory, testBadDecodeRequest)
781{
782 uint16_t fileType = 0;
783 uint32_t fileHandle = 0;
784 uint32_t offset = 0;
785 uint32_t length = 0;
786 uint64_t address = 0;
787
788 // Request payload message is missing
789 auto rc = decode_rw_file_by_type_memory_req(NULL, 0, &fileType, &fileHandle,
790 &offset, &length, &address);
791 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
792
793 std::array<uint8_t,
794 PLDM_RW_FILE_BY_TYPE_MEM_REQ_BYTES + sizeof(pldm_msg_hdr)>
795 requestMsg{};
796
797 auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
798
799 // Address is NULL
800 rc = decode_rw_file_by_type_memory_req(
801 requestPtr, requestMsg.size() - hdrSize, &fileType, &fileHandle,
802 &offset, &length, NULL);
803 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
804
805 // Payload length is invalid
806 rc = decode_rw_file_by_type_memory_req(
807 requestPtr, 0, &fileType, &fileHandle, &offset, &length, &address);
808 ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
809}
810
811TEST(ReadWriteFileByTypeMemory, testBadDecodeResponse)
812{
813 uint32_t length = 0;
814 uint8_t completionCode = 0;
815
816 // Request payload message is missing
817 auto rc =
818 decode_rw_file_by_type_memory_resp(NULL, 0, &completionCode, &length);
819 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
820
821 std::array<uint8_t,
822 PLDM_RW_FILE_BY_TYPE_MEM_RESP_BYTES + sizeof(pldm_msg_hdr)>
823 responseMsg{};
824
825 auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
826
827 // Length is NULL
828 rc = decode_rw_file_by_type_memory_resp(
829 responsePtr, responseMsg.size() - hdrSize, &completionCode, NULL);
830 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
831
832 // Payload length is invalid
833 rc = decode_rw_file_by_type_memory_resp(responsePtr, 0, &completionCode,
834 &length);
835 ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
836}
837
838TEST(ReadWriteFileByTypeMemory, testGoodEncodeRequest)
839{
840 std::array<uint8_t,
841 sizeof(pldm_msg_hdr) + PLDM_RW_FILE_BY_TYPE_MEM_REQ_BYTES>
842 requestMsg{};
843
844 uint16_t fileType = 0;
845 uint32_t fileHandle = 0x12345678;
846 uint32_t offset = 0x87654321;
847 uint32_t length = 0x13245768;
848 uint64_t address = 0x124356879ACBDE0F;
849
850 pldm_msg* request = reinterpret_cast<pldm_msg*>(requestMsg.data());
851
852 auto rc = encode_rw_file_by_type_memory_req(
853 0, PLDM_READ_FILE_BY_TYPE_INTO_MEMORY, fileType, fileHandle, offset,
854 length, address, request);
855
856 ASSERT_EQ(rc, PLDM_SUCCESS);
857 ASSERT_EQ(request->hdr.request, PLDM_REQUEST);
858 ASSERT_EQ(request->hdr.instance_id, 0);
859 ASSERT_EQ(request->hdr.type, PLDM_OEM);
860 ASSERT_EQ(request->hdr.command, PLDM_READ_FILE_BY_TYPE_INTO_MEMORY);
861
862 ASSERT_EQ(0, memcmp(request->payload, &fileType, sizeof(fileType)));
863 ASSERT_EQ(0, memcmp(request->payload + sizeof(fileType), &fileHandle,
864 sizeof(fileHandle)));
865
866 ASSERT_EQ(0,
867 memcmp(request->payload + sizeof(fileType) + sizeof(fileHandle),
868 &offset, sizeof(offset)));
869 ASSERT_EQ(0, memcmp(request->payload + sizeof(fileType) +
870 sizeof(fileHandle) + sizeof(offset),
871 &length, sizeof(length)));
872 ASSERT_EQ(0,
873 memcmp(request->payload + sizeof(fileType) + sizeof(fileHandle) +
874 sizeof(offset) + sizeof(length),
875 &address, sizeof(address)));
876}
877
878TEST(ReadWriteFileByTypeMemory, testGoodEncodeResponse)
879{
880 std::array<uint8_t,
881 sizeof(pldm_msg_hdr) + PLDM_RW_FILE_BY_TYPE_MEM_RESP_BYTES>
882 responseMsg{};
883
884 uint32_t length = 0x13245768;
885 uint8_t completionCode = 0x0;
886
887 pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
888
889 auto rc = encode_rw_file_by_type_memory_resp(
890 0, PLDM_READ_FILE_BY_TYPE_INTO_MEMORY, completionCode, length,
891 response);
892
893 ASSERT_EQ(rc, PLDM_SUCCESS);
894 ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
895 ASSERT_EQ(response->hdr.instance_id, 0);
896 ASSERT_EQ(response->hdr.type, PLDM_OEM);
897 ASSERT_EQ(response->hdr.command, PLDM_READ_FILE_BY_TYPE_INTO_MEMORY);
898
899 ASSERT_EQ(
900 0, memcmp(response->payload, &completionCode, sizeof(completionCode)));
901 ASSERT_EQ(0, memcmp(response->payload + sizeof(completionCode), &length,
902 sizeof(length)));
903}
904
905TEST(ReadWriteFileByTypeMemory, testBadEncodeResponse)
906{
907 std::array<uint8_t,
908 sizeof(pldm_msg_hdr) + PLDM_RW_FILE_BY_TYPE_MEM_RESP_BYTES>
909 responseMsg{};
910 uint32_t length = 0;
911 pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
912
913 // completion code is PLDM_ERROR
914 auto rc = encode_rw_file_by_type_memory_resp(
915 0, PLDM_READ_FILE_BY_TYPE_INTO_MEMORY, PLDM_ERROR, length, response);
916
917 ASSERT_EQ(rc, PLDM_SUCCESS);
918 ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
919 ASSERT_EQ(response->hdr.instance_id, 0);
920 ASSERT_EQ(response->hdr.type, PLDM_OEM);
921 ASSERT_EQ(response->hdr.command, PLDM_READ_FILE_BY_TYPE_INTO_MEMORY);
922 ASSERT_EQ(response->payload[0], PLDM_ERROR);
923
924 // response is NULL pointer
925 rc = encode_rw_file_by_type_memory_resp(
926 0, PLDM_READ_FILE_BY_TYPE_INTO_MEMORY, PLDM_SUCCESS, length, NULL);
927
928 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
929}
930
931TEST(ReadWriteFileByTypeMemory, testBadEncodeRequest)
932{
933 uint8_t fileType = 0;
934 uint32_t fileHandle = 0;
935 uint32_t offset = 0;
936 uint32_t length = 0;
937 uint64_t address = 0;
938
939 // request is NULL pointer
940 auto rc = encode_rw_file_by_type_memory_req(
941 0, PLDM_READ_FILE_BY_TYPE_INTO_MEMORY, fileType, fileHandle, offset,
942 length, address, NULL);
943
944 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
945}
vkaverapa9aac722019-08-22 02:10:15 -0500946
947TEST(NewFile, testGoodDecodeRequest)
948{
949 std::array<uint8_t, PLDM_NEW_FILE_REQ_BYTES + sizeof(pldm_msg_hdr)>
950 requestMsg{};
951
952 auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
953 size_t payload_length = requestMsg.size() - sizeof(pldm_msg_hdr);
954 auto request = reinterpret_cast<pldm_new_file_req*>(requestPtr->payload);
955
956 // Random value for fileHandle and length
957 uint16_t fileType = 0xFF;
958 uint32_t fileHandle = 0x12345678;
959 uint32_t length = 0x13245768;
960
961 request->file_type = fileType;
962 request->file_handle = fileHandle;
963 request->length = length;
964
965 uint16_t retFileType = 0xFF;
966 uint32_t retFileHandle = 0;
967 uint32_t retLength = 0;
968
969 // Invoke decode the read file request
970 auto rc = decode_new_file_req(requestPtr, payload_length, &retFileType,
971 &retFileHandle, &retLength);
972
973 ASSERT_EQ(rc, PLDM_SUCCESS);
974 ASSERT_EQ(fileType, retFileType);
975 ASSERT_EQ(fileHandle, retFileHandle);
976 ASSERT_EQ(length, retLength);
977}
978
979TEST(NewFile, testGoodDecodeResponse)
980{
981 std::array<uint8_t, PLDM_NEW_FILE_RESP_BYTES + sizeof(pldm_msg_hdr)>
982 responseMsg{};
983
984 auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
985 size_t payload_length = responseMsg.size() - sizeof(pldm_msg_hdr);
986 auto response = reinterpret_cast<pldm_new_file_resp*>(responsePtr->payload);
987
988 // Random value for completion code
989 uint8_t completionCode = 0x0;
990
991 response->completion_code = completionCode;
992
993 uint8_t retCompletionCode = PLDM_SUCCESS;
994
995 // Invoke decode the read/write file response
996 auto rc =
997 decode_new_file_resp(responsePtr, payload_length, &retCompletionCode);
998
999 ASSERT_EQ(rc, PLDM_SUCCESS);
1000 ASSERT_EQ(completionCode, retCompletionCode);
1001}
1002
1003TEST(NewFile, testBadDecodeRequest)
1004{
1005 uint16_t fileType = 0;
1006 uint32_t fileHandle = 0;
1007 uint32_t length = 0;
1008
1009 // Request payload message is missing
1010 auto rc = decode_new_file_req(NULL, 0, &fileType, &fileHandle, &length);
1011
1012 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1013
1014 std::array<uint8_t, PLDM_NEW_FILE_REQ_BYTES + sizeof(pldm_msg_hdr)>
1015 requestMsg{};
1016
1017 auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
1018
1019 // Payload length is invalid
1020 rc = decode_new_file_req(requestPtr, 0, &fileType, &fileHandle, &length);
1021 ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1022}
1023
1024TEST(NewFile, testBadDecodeResponse)
1025{
1026 uint8_t completionCode = 0;
1027
1028 // Request payload message is missing
1029 auto rc = decode_new_file_resp(NULL, 0, &completionCode);
1030 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1031
1032 std::array<uint8_t, PLDM_NEW_FILE_RESP_BYTES + sizeof(pldm_msg_hdr)>
1033 responseMsg{};
1034
1035 auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
1036
1037 // Payload length is invalid
1038 rc = decode_new_file_resp(responsePtr, 0, &completionCode);
1039 ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1040}
1041
1042TEST(NewFile, testGoodEncodeRequest)
1043{
1044 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_NEW_FILE_REQ_BYTES>
1045 requestMsg{};
1046
1047 uint16_t fileType = 0xFF;
1048 uint32_t fileHandle = 0x12345678;
1049 uint32_t length = 0x13245768;
1050
1051 pldm_msg* request = reinterpret_cast<pldm_msg*>(requestMsg.data());
1052
1053 auto rc = encode_new_file_req(0, fileType, fileHandle, length, request);
1054
1055 ASSERT_EQ(rc, PLDM_SUCCESS);
1056 ASSERT_EQ(request->hdr.request, PLDM_REQUEST);
1057 ASSERT_EQ(request->hdr.instance_id, 0);
1058 ASSERT_EQ(request->hdr.type, PLDM_OEM);
1059 ASSERT_EQ(request->hdr.command, PLDM_NEW_FILE_AVAILABLE);
1060 ASSERT_EQ(0, memcmp(request->payload, &fileType, sizeof(fileType)));
1061 ASSERT_EQ(0, memcmp(request->payload + sizeof(fileType), &fileHandle,
1062 sizeof(fileHandle)));
1063 ASSERT_EQ(0,
1064 memcmp(request->payload + sizeof(fileType) + sizeof(fileHandle),
1065 &length, sizeof(length)));
1066}
1067
1068TEST(NewFile, testGoodEncodeResponse)
1069{
1070 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_NEW_FILE_RESP_BYTES>
1071 responseMsg{};
1072
1073 uint8_t completionCode = 0x0;
1074
1075 pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1076
1077 auto rc = encode_new_file_resp(0, completionCode, response);
1078
1079 ASSERT_EQ(rc, PLDM_SUCCESS);
1080 ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
1081 ASSERT_EQ(response->hdr.instance_id, 0);
1082 ASSERT_EQ(response->hdr.type, PLDM_OEM);
1083 ASSERT_EQ(response->hdr.command, PLDM_NEW_FILE_AVAILABLE);
1084 ASSERT_EQ(
1085 0, memcmp(response->payload, &completionCode, sizeof(completionCode)));
1086}
1087
1088TEST(NewFile, testBadEncodeResponse)
1089{
1090 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_NEW_FILE_RESP_BYTES>
1091 responseMsg{};
1092 pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1093
1094 // completion code is PLDM_ERROR
1095 auto rc = encode_new_file_resp(0, PLDM_ERROR, response);
1096
1097 ASSERT_EQ(rc, PLDM_SUCCESS);
1098 ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
1099 ASSERT_EQ(response->hdr.instance_id, 0);
1100 ASSERT_EQ(response->hdr.type, PLDM_OEM);
1101 ASSERT_EQ(response->hdr.command, PLDM_NEW_FILE_AVAILABLE);
1102 ASSERT_EQ(response->payload[0], PLDM_ERROR);
1103
1104 // response is NULL pointer
1105 rc = encode_new_file_resp(0, PLDM_SUCCESS, NULL);
1106
1107 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1108}
1109
1110TEST(NewFile, testBadEncodeRequest)
1111{
1112 uint8_t fileType = 0xFF;
1113 uint32_t fileHandle = 0;
1114 uint32_t length = 0;
1115
1116 // request is NULL pointer
1117 auto rc = encode_new_file_req(0, fileType, fileHandle, length, NULL);
1118
1119 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1120}
Deepak Kodihallidce1c992019-11-19 07:06:53 -06001121
1122TEST(ReadWriteFileByType, testGoodDecodeRequest)
1123{
1124 std::array<uint8_t, PLDM_RW_FILE_BY_TYPE_REQ_BYTES + sizeof(pldm_msg_hdr)>
1125 requestMsg{};
1126
1127 auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
1128 size_t payload_length = requestMsg.size() - sizeof(pldm_msg_hdr);
1129 auto request = reinterpret_cast<pldm_read_write_file_by_type_req*>(
1130 requestPtr->payload);
1131
1132 // Random value for fileHandle, offset and length
1133 uint16_t fileType = 0;
1134 uint32_t fileHandle = 0x12345678;
1135 uint32_t offset = 0x87654321;
1136 uint32_t length = 0x13245768;
1137
1138 request->file_type = fileType;
1139 request->file_handle = fileHandle;
1140 request->offset = offset;
1141 request->length = length;
1142
1143 uint16_t retFileType = 0x1;
1144 uint32_t retFileHandle = 0;
1145 uint32_t retOffset = 0;
1146 uint32_t retLength = 0;
1147
1148 // Invoke decode the read file request
1149 auto rc =
1150 decode_rw_file_by_type_req(requestPtr, payload_length, &retFileType,
1151 &retFileHandle, &retOffset, &retLength);
1152
1153 ASSERT_EQ(rc, PLDM_SUCCESS);
1154 ASSERT_EQ(fileType, retFileType);
1155 ASSERT_EQ(fileHandle, retFileHandle);
1156 ASSERT_EQ(offset, retOffset);
1157 ASSERT_EQ(length, retLength);
1158}
1159
1160TEST(ReadWriteFileByType, testGoodDecodeResponse)
1161{
1162 std::array<uint8_t, PLDM_RW_FILE_BY_TYPE_RESP_BYTES + sizeof(pldm_msg_hdr)>
1163 responseMsg{};
1164
1165 auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
1166 size_t payload_length = responseMsg.size() - sizeof(pldm_msg_hdr);
1167 auto response = reinterpret_cast<pldm_read_write_file_by_type_resp*>(
1168 responsePtr->payload);
1169
1170 // Random value for completion code and length
1171 uint8_t completionCode = 0x0;
1172 uint32_t length = 0x13245768;
1173
1174 response->completion_code = completionCode;
1175 response->length = length;
1176
1177 uint8_t retCompletionCode = 0x1;
1178 uint32_t retLength = 0;
1179
1180 // Invoke decode the read/write file response
1181 auto rc = decode_rw_file_by_type_resp(responsePtr, payload_length,
1182 &retCompletionCode, &retLength);
1183
1184 ASSERT_EQ(rc, PLDM_SUCCESS);
1185 ASSERT_EQ(completionCode, retCompletionCode);
1186 ASSERT_EQ(length, retLength);
1187}
1188
1189TEST(ReadWriteFileByType, testBadDecodeRequest)
1190{
1191 uint16_t fileType = 0;
1192 uint32_t fileHandle = 0;
1193 uint32_t offset = 0;
1194 uint32_t length = 0;
1195
1196 // Request payload message is missing
1197 auto rc = decode_rw_file_by_type_req(NULL, 0, &fileType, &fileHandle,
1198 &offset, &length);
1199 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1200
1201 std::array<uint8_t, PLDM_RW_FILE_BY_TYPE_REQ_BYTES + sizeof(pldm_msg_hdr)>
1202 requestMsg{};
1203
1204 auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
1205
1206 // Payload length is invalid
1207 rc = decode_rw_file_by_type_req(requestPtr, 0, &fileType, &fileHandle,
1208 &offset, &length);
1209 ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1210}
1211
1212TEST(ReadWriteFileByType, testBadDecodeResponse)
1213{
1214 uint32_t length = 0;
1215 uint8_t completionCode = 0;
1216
1217 // Request payload message is missing
1218 auto rc = decode_rw_file_by_type_resp(NULL, 0, &completionCode, &length);
1219 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1220
1221 std::array<uint8_t, PLDM_RW_FILE_BY_TYPE_RESP_BYTES + sizeof(pldm_msg_hdr)>
1222 responseMsg{};
1223
1224 auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
1225
1226 // Length is NULL
1227 rc = decode_rw_file_by_type_resp(responsePtr, responseMsg.size() - hdrSize,
1228 &completionCode, NULL);
1229 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1230
1231 // Payload length is invalid
1232 rc = decode_rw_file_by_type_resp(responsePtr, 0, &completionCode, &length);
1233 ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1234}
1235
1236TEST(ReadWriteFileByType, testGoodEncodeRequest)
1237{
1238 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_RW_FILE_BY_TYPE_REQ_BYTES>
1239 requestMsg{};
1240
1241 uint16_t fileType = 0;
1242 uint32_t fileHandle = 0x12345678;
1243 uint32_t offset = 0x87654321;
1244 uint32_t length = 0x13245768;
1245
1246 pldm_msg* request = reinterpret_cast<pldm_msg*>(requestMsg.data());
1247
1248 auto rc = encode_rw_file_by_type_req(0, PLDM_READ_FILE_BY_TYPE, fileType,
1249 fileHandle, offset, length, request);
1250
1251 ASSERT_EQ(rc, PLDM_SUCCESS);
1252 ASSERT_EQ(request->hdr.request, PLDM_REQUEST);
1253 ASSERT_EQ(request->hdr.instance_id, 0);
1254 ASSERT_EQ(request->hdr.type, PLDM_OEM);
1255 ASSERT_EQ(request->hdr.command, PLDM_READ_FILE_BY_TYPE);
1256
1257 ASSERT_EQ(0, memcmp(request->payload, &fileType, sizeof(fileType)));
1258 ASSERT_EQ(0, memcmp(request->payload + sizeof(fileType), &fileHandle,
1259 sizeof(fileHandle)));
1260
1261 ASSERT_EQ(0,
1262 memcmp(request->payload + sizeof(fileType) + sizeof(fileHandle),
1263 &offset, sizeof(offset)));
1264 ASSERT_EQ(0, memcmp(request->payload + sizeof(fileType) +
1265 sizeof(fileHandle) + sizeof(offset),
1266 &length, sizeof(length)));
1267}
1268
1269TEST(ReadWriteFileByType, testGoodEncodeResponse)
1270{
1271 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_RW_FILE_BY_TYPE_RESP_BYTES>
1272 responseMsg{};
1273
1274 uint32_t length = 0x13245768;
1275 uint8_t completionCode = 0x0;
Deepak Kodihallidce1c992019-11-19 07:06:53 -06001276 pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1277
1278 auto rc = encode_rw_file_by_type_resp(0, PLDM_READ_FILE_BY_TYPE,
1279 completionCode, length, response);
1280
1281 ASSERT_EQ(rc, PLDM_SUCCESS);
1282 ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
1283 ASSERT_EQ(response->hdr.instance_id, 0);
1284 ASSERT_EQ(response->hdr.type, PLDM_OEM);
1285 ASSERT_EQ(response->hdr.command, PLDM_READ_FILE_BY_TYPE);
1286
1287 ASSERT_EQ(
1288 0, memcmp(response->payload, &completionCode, sizeof(completionCode)));
1289 ASSERT_EQ(0, memcmp(response->payload + sizeof(completionCode), &length,
1290 sizeof(length)));
1291}
1292
1293TEST(ReadWriteFileByType, testBadEncodeResponse)
1294{
1295 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_RW_FILE_BY_TYPE_RESP_BYTES>
1296 responseMsg{};
1297 uint32_t length = 0;
1298 pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1299
1300 // completion code is PLDM_ERROR
1301 auto rc = encode_rw_file_by_type_resp(0, PLDM_READ_FILE_BY_TYPE, PLDM_ERROR,
1302 length, response);
1303
1304 ASSERT_EQ(rc, PLDM_SUCCESS);
1305 ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
1306 ASSERT_EQ(response->hdr.instance_id, 0);
1307 ASSERT_EQ(response->hdr.type, PLDM_OEM);
1308 ASSERT_EQ(response->hdr.command, PLDM_READ_FILE_BY_TYPE);
1309 ASSERT_EQ(response->payload[0], PLDM_ERROR);
1310
1311 // response is NULL pointer
1312 rc = encode_rw_file_by_type_resp(0, PLDM_READ_FILE_BY_TYPE, PLDM_SUCCESS,
1313 length, NULL);
1314
1315 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1316}
1317
1318TEST(ReadWriteFileByType, testBadEncodeRequest)
1319{
1320 uint8_t fileType = 0;
1321 uint32_t fileHandle = 0;
1322 uint32_t offset = 0;
1323 uint32_t length = 0;
1324
1325 // request is NULL pointer
1326 auto rc = encode_rw_file_by_type_req(0, PLDM_READ_FILE_BY_TYPE, fileType,
1327 fileHandle, offset, length, NULL);
1328
1329 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1330}
vkaverapf4e0a492019-11-19 01:47:35 -06001331
1332TEST(FileAck, testGoodDecodeRequest)
1333{
1334 std::array<uint8_t, PLDM_FILE_ACK_REQ_BYTES + sizeof(pldm_msg_hdr)>
1335 requestMsg{};
1336
1337 auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
1338 size_t payload_length = requestMsg.size() - sizeof(pldm_msg_hdr);
1339 auto request = reinterpret_cast<pldm_file_ack_req*>(requestPtr->payload);
1340
1341 // Random value for fileHandle
1342 uint16_t fileType = 0xFFFF;
1343 uint32_t fileHandle = 0x12345678;
1344 uint32_t fileStatus = 0xFF;
1345
1346 request->file_type = fileType;
1347 request->file_handle = fileHandle;
1348 request->file_status = fileStatus;
1349
1350 uint16_t retFileType = 0xFF;
1351 uint32_t retFileHandle = 0;
1352 uint8_t retFileStatus = 0;
1353
1354 // Invoke decode the read file request
1355 auto rc = decode_file_ack_req(requestPtr, payload_length, &retFileType,
1356 &retFileHandle, &retFileStatus);
1357
1358 ASSERT_EQ(rc, PLDM_SUCCESS);
1359 ASSERT_EQ(fileType, retFileType);
1360 ASSERT_EQ(fileHandle, retFileHandle);
1361 ASSERT_EQ(fileStatus, retFileStatus);
1362}
1363
1364TEST(FileAck, testGoodDecodeResponse)
1365{
1366 std::array<uint8_t, PLDM_FILE_ACK_RESP_BYTES + sizeof(pldm_msg_hdr)>
1367 responseMsg{};
1368
1369 auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
1370 size_t payload_length = responseMsg.size() - sizeof(pldm_msg_hdr);
1371 auto response = reinterpret_cast<pldm_file_ack_resp*>(responsePtr->payload);
1372
1373 // Random value for completion code
1374 uint8_t completionCode = 0x0;
1375
1376 response->completion_code = completionCode;
1377
1378 uint8_t retCompletionCode = PLDM_SUCCESS;
1379
1380 // Invoke decode the read/write file response
1381 auto rc =
1382 decode_file_ack_resp(responsePtr, payload_length, &retCompletionCode);
1383
1384 ASSERT_EQ(rc, PLDM_SUCCESS);
1385 ASSERT_EQ(completionCode, retCompletionCode);
1386}
1387
1388TEST(FileAck, testBadDecodeRequest)
1389{
1390 uint16_t fileType = 0;
1391 uint32_t fileHandle = 0;
1392 uint8_t fileStatus = 0;
1393
1394 // Request payload message is missing
1395 auto rc = decode_file_ack_req(NULL, 0, &fileType, &fileHandle, &fileStatus);
1396
1397 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1398
1399 std::array<uint8_t, PLDM_FILE_ACK_REQ_BYTES + sizeof(pldm_msg_hdr)>
1400 requestMsg{};
1401
1402 auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
1403
1404 // Payload length is invalid
1405 rc =
1406 decode_file_ack_req(requestPtr, 0, &fileType, &fileHandle, &fileStatus);
1407 ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1408}
1409
1410TEST(FileAck, testBadDecodeResponse)
1411{
1412 uint8_t completionCode = 0;
1413
1414 // Request payload message is missing
1415 auto rc = decode_file_ack_resp(NULL, 0, &completionCode);
1416 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1417
1418 std::array<uint8_t, PLDM_FILE_ACK_RESP_BYTES + sizeof(pldm_msg_hdr)>
1419 responseMsg{};
1420
1421 auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
1422
1423 // Payload length is invalid
1424 rc = decode_file_ack_resp(responsePtr, 0, &completionCode);
1425 ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1426}
1427
1428TEST(FileAck, testGoodEncodeRequest)
1429{
1430 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_FILE_ACK_REQ_BYTES>
1431 requestMsg{};
1432
1433 uint16_t fileType = 0xFFFF;
1434 uint32_t fileHandle = 0x12345678;
1435 uint8_t fileStatus = 0xFF;
1436
1437 pldm_msg* request = reinterpret_cast<pldm_msg*>(requestMsg.data());
1438
1439 auto rc = encode_file_ack_req(0, fileType, fileHandle, fileStatus, request);
1440
1441 ASSERT_EQ(rc, PLDM_SUCCESS);
1442 ASSERT_EQ(request->hdr.request, PLDM_REQUEST);
1443 ASSERT_EQ(request->hdr.instance_id, 0);
1444 ASSERT_EQ(request->hdr.type, PLDM_OEM);
1445 ASSERT_EQ(request->hdr.command, PLDM_FILE_ACK);
1446 ASSERT_EQ(0, memcmp(request->payload, &fileType, sizeof(fileType)));
1447 ASSERT_EQ(0, memcmp(request->payload + sizeof(fileType), &fileHandle,
1448 sizeof(fileHandle)));
1449}
1450
1451TEST(FileAck, testGoodEncodeResponse)
1452{
1453 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_FILE_ACK_RESP_BYTES>
1454 responseMsg{};
1455
1456 uint8_t completionCode = 0x0;
1457
1458 pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1459
1460 auto rc = encode_file_ack_resp(0, completionCode, response);
1461
1462 ASSERT_EQ(rc, PLDM_SUCCESS);
1463 ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
1464 ASSERT_EQ(response->hdr.instance_id, 0);
1465 ASSERT_EQ(response->hdr.type, PLDM_OEM);
1466 ASSERT_EQ(response->hdr.command, PLDM_FILE_ACK);
1467 ASSERT_EQ(
1468 0, memcmp(response->payload, &completionCode, sizeof(completionCode)));
1469}
1470
1471TEST(FileAck, testBadEncodeResponse)
1472{
1473 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_FILE_ACK_RESP_BYTES>
1474 responseMsg{};
1475 pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1476
1477 // completion code is PLDM_ERROR
1478 auto rc = encode_file_ack_resp(0, PLDM_ERROR, response);
1479
1480 ASSERT_EQ(rc, PLDM_SUCCESS);
1481 ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
1482 ASSERT_EQ(response->hdr.instance_id, 0);
1483 ASSERT_EQ(response->hdr.type, PLDM_OEM);
1484 ASSERT_EQ(response->hdr.command, PLDM_FILE_ACK);
1485 ASSERT_EQ(response->payload[0], PLDM_ERROR);
1486
1487 // response is NULL pointer
1488 rc = encode_file_ack_resp(0, PLDM_SUCCESS, NULL);
1489
1490 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1491}
1492
1493TEST(FileAck, testBadEncodeRequest)
1494{
1495 uint8_t fileType = 0xFF;
1496 uint32_t fileHandle = 0;
1497 uint8_t fileStatus = 0;
1498
1499 // request is NULL pointer
1500 auto rc = encode_file_ack_req(0, fileType, fileHandle, fileStatus, nullptr);
1501
1502 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1503}