blob: 1f11fc5b74baf6ce894175c4b24b2545b43175e3 [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};
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +000020 int rc;
Delphine CC Chiu22fad392023-10-27 11:05:01 +080021
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 Jefferyc8df31c2024-05-21 16:47:43 +093027 ASSERT_EQ(pldm_msgbuf_init_cc(ctx, 0, &buf[hdrSize], sizeof(buf) - hdrSize),
Andrew Jeffery32df16c2024-05-17 14:28:18 +093028 0);
Delphine CC Chiu22fad392023-10-27 11:05:01 +080029
30 pldm_msgbuf_insert_uint8(ctx, fileHandle);
31 pldm_msgbuf_insert_int32(ctx, dataLengthLE);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +000032 rc = pldm_msgbuf_insert_array_uint8(ctx, sizeof(postCode), postCode,
33 sizeof(postCode));
34 ASSERT_EQ(rc, PLDM_SUCCESS);
Delphine CC Chiu22fad392023-10-27 11:05:01 +080035
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 Jeffery0a1be3c2024-08-11 08:34:10 +000043 rc = decode_oem_meta_file_io_req(request, sizeof(buf) - hdrSize,
44 &retfileHandle, &retFileDataCnt,
45 retDataField.data());
Delphine CC Chiu22fad392023-10-27 11:05:01 +080046
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +000047 ASSERT_EQ(rc, PLDM_SUCCESS);
Delphine CC Chiu22fad392023-10-27 11:05:01 +080048 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
56TEST(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
67TEST(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
82TEST(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 Jeffery32df16c2024-05-17 14:28:18 +093096}