blob: 48c0778cf0b9877508963d4ab87e1bc89dc3d838 [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
Lora Lin84213eb2024-07-22 09:52:01 +08005#include <cstdlib>
6#include <new>
7
Delphine CC Chiu22fad392023-10-27 11:05:01 +08008#include "msgbuf.h"
9
10#include <gtest/gtest.h>
11
Lora Lin84213eb2024-07-22 09:52:01 +080012#ifdef LIBPLDM_API_TESTING
13TEST(DecodeOemMetaFileIoWriteReq, testGoodDecodeRequest)
Delphine CC Chiu22fad392023-10-27 11:05:01 +080014{
Lora Lin84213eb2024-07-22 09:52:01 +080015 constexpr const uint8_t postCode[4] = {0x93, 0xe0, 0x00, 0xea};
Delphine CC Chiu22fad392023-10-27 11:05:01 +080016 struct pldm_msgbuf _ctx;
17 struct pldm_msgbuf* ctx = &_ctx;
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +000018 int rc;
Delphine CC Chiu22fad392023-10-27 11:05:01 +080019
Lora Lin84213eb2024-07-22 09:52:01 +080020 constexpr size_t encodedPayloadLen =
21 PLDM_OEM_META_FILE_IO_WRITE_REQ_MIN_LENGTH + sizeof(postCode);
22 constexpr size_t encodedMsgLen = sizeof(pldm_msg_hdr) + encodedPayloadLen;
23 alignas(pldm_msg) unsigned char encodedMsgBuf[encodedMsgLen] = {};
24 auto* encodedMsg = new (encodedMsgBuf) pldm_msg;
Delphine CC Chiu22fad392023-10-27 11:05:01 +080025
Lora Lin84213eb2024-07-22 09:52:01 +080026 rc = pldm_msgbuf_init_errno(ctx, 0, encodedMsg->payload, encodedPayloadLen);
27 ASSERT_EQ(rc, 0);
Delphine CC Chiu22fad392023-10-27 11:05:01 +080028
Lora Lin84213eb2024-07-22 09:52:01 +080029 pldm_msgbuf_insert_uint8(ctx, 0);
30 pldm_msgbuf_insert_int32(ctx, sizeof(postCode));
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +000031 rc = pldm_msgbuf_insert_array_uint8(ctx, sizeof(postCode), postCode,
32 sizeof(postCode));
Lora Lin84213eb2024-07-22 09:52:01 +080033 ASSERT_EQ(rc, 0);
Delphine CC Chiu22fad392023-10-27 11:05:01 +080034
Lora Lin84213eb2024-07-22 09:52:01 +080035 rc = pldm_msgbuf_destroy_consumed(ctx);
36 ASSERT_EQ(rc, 0);
Delphine CC Chiu22fad392023-10-27 11:05:01 +080037
Lora Lin84213eb2024-07-22 09:52:01 +080038 constexpr size_t decodedReqLen =
39 sizeof(struct pldm_oem_meta_file_io_write_req) + sizeof(postCode);
40 alignas(pldm_oem_meta_file_io_write_req) unsigned char
41 decodedReqBuf[decodedReqLen];
42 auto* decodedReq = new (decodedReqBuf) pldm_oem_meta_file_io_write_req;
43 auto* decodedReqData =
44 static_cast<uint8_t*>(pldm_oem_meta_file_io_write_req_data(decodedReq));
Delphine CC Chiu22fad392023-10-27 11:05:01 +080045
Lora Lin84213eb2024-07-22 09:52:01 +080046 rc = decode_oem_meta_file_io_write_req(encodedMsg, encodedPayloadLen,
47 decodedReq, decodedReqLen);
48 ASSERT_EQ(rc, 0);
Delphine CC Chiu22fad392023-10-27 11:05:01 +080049
Lora Lin84213eb2024-07-22 09:52:01 +080050 EXPECT_EQ(decodedReq->handle, 0);
51 ASSERT_EQ(decodedReq->length, sizeof(postCode));
52 EXPECT_EQ(memcmp(decodedReqData, postCode, decodedReq->length), 0);
Delphine CC Chiu22fad392023-10-27 11:05:01 +080053}
Lora Lin84213eb2024-07-22 09:52:01 +080054#endif
Delphine CC Chiu22fad392023-10-27 11:05:01 +080055
Lora Lin84213eb2024-07-22 09:52:01 +080056#ifdef LIBPLDM_API_TESTING
57TEST(DecodeOemMetaFileIoWriteReq, testInvalidFieldsDecodeRequest)
Delphine CC Chiu22fad392023-10-27 11:05:01 +080058{
Lora Lin84213eb2024-07-22 09:52:01 +080059 struct pldm_msg msg = {};
Delphine CC Chiu22fad392023-10-27 11:05:01 +080060
Lora Lin84213eb2024-07-22 09:52:01 +080061 auto rc = decode_oem_meta_file_io_write_req(&msg, sizeof(msg), NULL, 0);
62 EXPECT_EQ(rc, -EINVAL);
Delphine CC Chiu22fad392023-10-27 11:05:01 +080063}
Lora Lin84213eb2024-07-22 09:52:01 +080064#endif
Delphine CC Chiu22fad392023-10-27 11:05:01 +080065
Lora Lin84213eb2024-07-22 09:52:01 +080066#ifdef LIBPLDM_API_TESTING
67TEST(DecodeOemMetaFileIoWriteReq, testInvalidLengthDecodeRequest)
Delphine CC Chiu22fad392023-10-27 11:05:01 +080068{
Lora Lin84213eb2024-07-22 09:52:01 +080069 struct pldm_oem_meta_file_io_write_req req = {};
70 struct pldm_msg msg = {};
Delphine CC Chiu22fad392023-10-27 11:05:01 +080071
Lora Lin84213eb2024-07-22 09:52:01 +080072 auto rc = decode_oem_meta_file_io_write_req(&msg, 0, &req, sizeof(req));
73 EXPECT_EQ(rc, -EOVERFLOW);
Delphine CC Chiu22fad392023-10-27 11:05:01 +080074}
Lora Lin84213eb2024-07-22 09:52:01 +080075#endif
Delphine CC Chiu22fad392023-10-27 11:05:01 +080076
Lora Lin84213eb2024-07-22 09:52:01 +080077#ifdef LIBPLDM_API_TESTING
78TEST(DecodeOemMetaFileIoWriteReq, testInvalidDataRequest)
Delphine CC Chiu22fad392023-10-27 11:05:01 +080079{
Lora Lin84213eb2024-07-22 09:52:01 +080080 struct pldm_oem_meta_file_io_write_req req = {};
81 struct pldm_msg msg = {};
82 int rc;
Delphine CC Chiu22fad392023-10-27 11:05:01 +080083
Lora Lin84213eb2024-07-22 09:52:01 +080084 rc = decode_oem_meta_file_io_write_req(
85 &msg, PLDM_OEM_META_FILE_IO_WRITE_REQ_MIN_LENGTH - 1, &req,
86 sizeof(req));
87 EXPECT_EQ(rc, -EOVERFLOW);
Andrew Jeffery32df16c2024-05-17 14:28:18 +093088}
Lora Lin84213eb2024-07-22 09:52:01 +080089#endif