blob: decaa4328a699ea7db168766de24667efc9326f4 [file] [log] [blame]
Delphine CC Chiu22fad392023-10-27 11:05:01 +08001/* 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
9static constexpr size_t oemMetaDecodeWriteFileIoReqBytes = 9;
10static constexpr size_t postCodeSize = 4;
11static constexpr size_t invalidDataSize = 0;
12
13TEST(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 Barithayadc7d3b52024-02-06 23:46:49 -060019 uint8_t postCode[4] = {0x93, 0xe0, 0x00, 0xea};
Delphine CC Chiu22fad392023-10-27 11:05:01 +080020
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 Jeffery32df16c2024-05-17 14:28:18 +093026 ASSERT_EQ(pldm_msgbuf_init(ctx, 0, &buf[hdrSize], sizeof(buf) - hdrSize),
27 0);
Delphine CC Chiu22fad392023-10-27 11:05:01 +080028
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
53TEST(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
64TEST(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
79TEST(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 Jeffery32df16c2024-05-17 14:28:18 +093093}