Delphine CC Chiu | 22fad39 | 2023-10-27 11:05:01 +0800 | [diff] [blame] | 1 | /* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */ |
| 2 | #include <endian.h> |
| 3 | #include <libpldm/oem/meta/file_io.h> |
| 4 | |
| 5 | #include "msgbuf.h" |
| 6 | |
| 7 | #include <gtest/gtest.h> |
| 8 | |
| 9 | static constexpr size_t oemMetaDecodeWriteFileIoReqBytes = 9; |
| 10 | static constexpr size_t postCodeSize = 4; |
| 11 | static constexpr size_t invalidDataSize = 0; |
| 12 | |
| 13 | TEST(DecodeOemMetaFileIoReq, testGoodDecodeRequest) |
| 14 | { |
| 15 | struct pldm_msgbuf _ctx; |
| 16 | struct pldm_msgbuf* ctx = &_ctx; |
| 17 | uint8_t fileHandle = 0x00; |
| 18 | int32_t dataLengthLE = 0x04; |
Pavithra Barithaya | dc7d3b5 | 2024-02-06 23:46:49 -0600 | [diff] [blame] | 19 | uint8_t postCode[4] = {0x93, 0xe0, 0x00, 0xea}; |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 20 | int rc; |
Delphine CC Chiu | 22fad39 | 2023-10-27 11:05:01 +0800 | [diff] [blame] | 21 | |
| 22 | constexpr auto hdrSize = sizeof(pldm_msg_hdr); |
| 23 | |
| 24 | uint8_t buf[hdrSize + sizeof(uint8_t) + sizeof(int32_t) + |
| 25 | (postCodeSize * sizeof(uint8_t))] = {}; |
| 26 | |
Andrew Jeffery | c8df31c | 2024-05-21 16:47:43 +0930 | [diff] [blame] | 27 | ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, &buf[hdrSize], sizeof(buf) - hdrSize), |
Andrew Jeffery | 32df16c | 2024-05-17 14:28:18 +0930 | [diff] [blame] | 28 | 0); |
Delphine CC Chiu | 22fad39 | 2023-10-27 11:05:01 +0800 | [diff] [blame] | 29 | |
| 30 | pldm_msgbuf_insert_uint8(ctx, fileHandle); |
| 31 | pldm_msgbuf_insert_int32(ctx, dataLengthLE); |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 32 | rc = pldm_msgbuf_insert_array_uint8(ctx, sizeof(postCode), postCode, |
| 33 | sizeof(postCode)); |
| 34 | ASSERT_EQ(rc, PLDM_SUCCESS); |
Delphine CC Chiu | 22fad39 | 2023-10-27 11:05:01 +0800 | [diff] [blame] | 35 | |
| 36 | std::array<uint8_t, oemMetaDecodeWriteFileIoReqBytes> retDataField{}; |
| 37 | |
| 38 | uint8_t retfileHandle = 0; |
| 39 | uint32_t retFileDataCnt = 0; |
| 40 | |
| 41 | auto request = reinterpret_cast<pldm_msg*>(buf); |
| 42 | |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 43 | rc = decode_oem_meta_file_io_req(request, sizeof(buf) - hdrSize, |
| 44 | &retfileHandle, &retFileDataCnt, |
| 45 | retDataField.data()); |
Delphine CC Chiu | 22fad39 | 2023-10-27 11:05:01 +0800 | [diff] [blame] | 46 | |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 47 | ASSERT_EQ(rc, PLDM_SUCCESS); |
Delphine CC Chiu | 22fad39 | 2023-10-27 11:05:01 +0800 | [diff] [blame] | 48 | EXPECT_EQ(retfileHandle, fileHandle); |
| 49 | EXPECT_EQ(retFileDataCnt, dataLengthLE); |
| 50 | EXPECT_EQ(retDataField[0], postCode[0]); |
| 51 | EXPECT_EQ(retDataField[1], postCode[1]); |
| 52 | EXPECT_EQ(retDataField[2], postCode[2]); |
| 53 | EXPECT_EQ(retDataField[3], postCode[3]); |
| 54 | } |
| 55 | |
| 56 | TEST(DecodeOemMetaFileIoReq, testInvalidFieldsDecodeRequest) |
| 57 | { |
| 58 | std::array<uint8_t, oemMetaDecodeWriteFileIoReqBytes> requestMsg{}; |
| 59 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 60 | |
| 61 | auto rc = decode_oem_meta_file_io_req(request, requestMsg.size(), NULL, |
| 62 | NULL, NULL); |
| 63 | |
| 64 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 65 | } |
| 66 | |
| 67 | TEST(DecodeOemMetaFileIoReq, testInvalidLengthDecodeRequest) |
| 68 | { |
| 69 | uint8_t retfileHandle = 0; |
| 70 | uint32_t retFileDataCnt = 0; |
| 71 | uint8_t buf[1] = {}; |
| 72 | std::array<uint8_t, oemMetaDecodeWriteFileIoReqBytes> retDataField{}; |
| 73 | |
| 74 | auto request = reinterpret_cast<pldm_msg*>(buf); |
| 75 | |
| 76 | auto rc = decode_oem_meta_file_io_req(request, 0, &retfileHandle, |
| 77 | &retFileDataCnt, retDataField.data()); |
| 78 | |
| 79 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 80 | } |
| 81 | |
| 82 | TEST(DecodeOemMetaFileIoReq, testInvalidDataRequest) |
| 83 | { |
| 84 | uint8_t buf[1] = {}; |
| 85 | uint8_t retfileHandle = 0; |
| 86 | uint32_t retFileDataCnt = 0; |
| 87 | |
| 88 | std::array<uint8_t, invalidDataSize> retDataField{}; |
| 89 | |
| 90 | auto request = reinterpret_cast<pldm_msg*>(buf); |
| 91 | |
| 92 | auto rc = decode_oem_meta_file_io_req(request, sizeof(buf), &retfileHandle, |
| 93 | &retFileDataCnt, retDataField.data()); |
| 94 | |
| 95 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
Andrew Jeffery | 32df16c | 2024-05-17 14:28:18 +0930 | [diff] [blame] | 96 | } |