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}; |
Delphine CC Chiu | 22fad39 | 2023-10-27 11:05:01 +0800 | [diff] [blame] | 20 | |
| 21 | constexpr auto hdrSize = sizeof(pldm_msg_hdr); |
| 22 | |
| 23 | uint8_t buf[hdrSize + sizeof(uint8_t) + sizeof(int32_t) + |
| 24 | (postCodeSize * sizeof(uint8_t))] = {}; |
| 25 | |
| 26 | pldm_msgbuf_init(ctx, 0, &buf[hdrSize], sizeof(buf) - hdrSize); |
| 27 | |
| 28 | pldm_msgbuf_insert_uint8(ctx, fileHandle); |
| 29 | pldm_msgbuf_insert_int32(ctx, dataLengthLE); |
| 30 | pldm_msgbuf_insert_array_uint8(ctx, postCode, sizeof(postCode)); |
| 31 | |
| 32 | std::array<uint8_t, oemMetaDecodeWriteFileIoReqBytes> retDataField{}; |
| 33 | |
| 34 | uint8_t retfileHandle = 0; |
| 35 | uint32_t retFileDataCnt = 0; |
| 36 | |
| 37 | auto request = reinterpret_cast<pldm_msg*>(buf); |
| 38 | |
| 39 | auto rc = decode_oem_meta_file_io_req(request, sizeof(buf) - hdrSize, |
| 40 | &retfileHandle, &retFileDataCnt, |
| 41 | retDataField.data()); |
| 42 | |
| 43 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 44 | EXPECT_EQ(retfileHandle, fileHandle); |
| 45 | EXPECT_EQ(retFileDataCnt, dataLengthLE); |
| 46 | EXPECT_EQ(retDataField[0], postCode[0]); |
| 47 | EXPECT_EQ(retDataField[1], postCode[1]); |
| 48 | EXPECT_EQ(retDataField[2], postCode[2]); |
| 49 | EXPECT_EQ(retDataField[3], postCode[3]); |
| 50 | } |
| 51 | |
| 52 | TEST(DecodeOemMetaFileIoReq, testInvalidFieldsDecodeRequest) |
| 53 | { |
| 54 | std::array<uint8_t, oemMetaDecodeWriteFileIoReqBytes> requestMsg{}; |
| 55 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 56 | |
| 57 | auto rc = decode_oem_meta_file_io_req(request, requestMsg.size(), NULL, |
| 58 | NULL, NULL); |
| 59 | |
| 60 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 61 | } |
| 62 | |
| 63 | TEST(DecodeOemMetaFileIoReq, testInvalidLengthDecodeRequest) |
| 64 | { |
| 65 | uint8_t retfileHandle = 0; |
| 66 | uint32_t retFileDataCnt = 0; |
| 67 | uint8_t buf[1] = {}; |
| 68 | std::array<uint8_t, oemMetaDecodeWriteFileIoReqBytes> retDataField{}; |
| 69 | |
| 70 | auto request = reinterpret_cast<pldm_msg*>(buf); |
| 71 | |
| 72 | auto rc = decode_oem_meta_file_io_req(request, 0, &retfileHandle, |
| 73 | &retFileDataCnt, retDataField.data()); |
| 74 | |
| 75 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 76 | } |
| 77 | |
| 78 | TEST(DecodeOemMetaFileIoReq, testInvalidDataRequest) |
| 79 | { |
| 80 | uint8_t buf[1] = {}; |
| 81 | uint8_t retfileHandle = 0; |
| 82 | uint32_t retFileDataCnt = 0; |
| 83 | |
| 84 | std::array<uint8_t, invalidDataSize> retDataField{}; |
| 85 | |
| 86 | auto request = reinterpret_cast<pldm_msg*>(buf); |
| 87 | |
| 88 | auto rc = decode_oem_meta_file_io_req(request, sizeof(buf), &retfileHandle, |
| 89 | &retFileDataCnt, retDataField.data()); |
| 90 | |
| 91 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 92 | } |