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