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 | |
Lora Lin | 84213eb | 2024-07-22 09:52:01 +0800 | [diff] [blame^] | 5 | #include <cstdlib> |
| 6 | #include <new> |
| 7 | |
Delphine CC Chiu | 22fad39 | 2023-10-27 11:05:01 +0800 | [diff] [blame] | 8 | #include "msgbuf.h" |
| 9 | |
| 10 | #include <gtest/gtest.h> |
| 11 | |
Lora Lin | 84213eb | 2024-07-22 09:52:01 +0800 | [diff] [blame^] | 12 | #ifdef LIBPLDM_API_TESTING |
| 13 | TEST(DecodeOemMetaFileIoWriteReq, testGoodDecodeRequest) |
Delphine CC Chiu | 22fad39 | 2023-10-27 11:05:01 +0800 | [diff] [blame] | 14 | { |
Lora Lin | 84213eb | 2024-07-22 09:52:01 +0800 | [diff] [blame^] | 15 | constexpr const uint8_t postCode[4] = {0x93, 0xe0, 0x00, 0xea}; |
Delphine CC Chiu | 22fad39 | 2023-10-27 11:05:01 +0800 | [diff] [blame] | 16 | struct pldm_msgbuf _ctx; |
| 17 | struct pldm_msgbuf* ctx = &_ctx; |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 18 | int rc; |
Delphine CC Chiu | 22fad39 | 2023-10-27 11:05:01 +0800 | [diff] [blame] | 19 | |
Lora Lin | 84213eb | 2024-07-22 09:52:01 +0800 | [diff] [blame^] | 20 | 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 Chiu | 22fad39 | 2023-10-27 11:05:01 +0800 | [diff] [blame] | 25 | |
Lora Lin | 84213eb | 2024-07-22 09:52:01 +0800 | [diff] [blame^] | 26 | rc = pldm_msgbuf_init_errno(ctx, 0, encodedMsg->payload, encodedPayloadLen); |
| 27 | ASSERT_EQ(rc, 0); |
Delphine CC Chiu | 22fad39 | 2023-10-27 11:05:01 +0800 | [diff] [blame] | 28 | |
Lora Lin | 84213eb | 2024-07-22 09:52:01 +0800 | [diff] [blame^] | 29 | pldm_msgbuf_insert_uint8(ctx, 0); |
| 30 | pldm_msgbuf_insert_int32(ctx, sizeof(postCode)); |
Andrew Jeffery | 0a1be3c | 2024-08-11 08:34:10 +0000 | [diff] [blame] | 31 | rc = pldm_msgbuf_insert_array_uint8(ctx, sizeof(postCode), postCode, |
| 32 | sizeof(postCode)); |
Lora Lin | 84213eb | 2024-07-22 09:52:01 +0800 | [diff] [blame^] | 33 | ASSERT_EQ(rc, 0); |
Delphine CC Chiu | 22fad39 | 2023-10-27 11:05:01 +0800 | [diff] [blame] | 34 | |
Lora Lin | 84213eb | 2024-07-22 09:52:01 +0800 | [diff] [blame^] | 35 | rc = pldm_msgbuf_destroy_consumed(ctx); |
| 36 | ASSERT_EQ(rc, 0); |
Delphine CC Chiu | 22fad39 | 2023-10-27 11:05:01 +0800 | [diff] [blame] | 37 | |
Lora Lin | 84213eb | 2024-07-22 09:52:01 +0800 | [diff] [blame^] | 38 | 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 Chiu | 22fad39 | 2023-10-27 11:05:01 +0800 | [diff] [blame] | 45 | |
Lora Lin | 84213eb | 2024-07-22 09:52:01 +0800 | [diff] [blame^] | 46 | rc = decode_oem_meta_file_io_write_req(encodedMsg, encodedPayloadLen, |
| 47 | decodedReq, decodedReqLen); |
| 48 | ASSERT_EQ(rc, 0); |
Delphine CC Chiu | 22fad39 | 2023-10-27 11:05:01 +0800 | [diff] [blame] | 49 | |
Lora Lin | 84213eb | 2024-07-22 09:52:01 +0800 | [diff] [blame^] | 50 | EXPECT_EQ(decodedReq->handle, 0); |
| 51 | ASSERT_EQ(decodedReq->length, sizeof(postCode)); |
| 52 | EXPECT_EQ(memcmp(decodedReqData, postCode, decodedReq->length), 0); |
Delphine CC Chiu | 22fad39 | 2023-10-27 11:05:01 +0800 | [diff] [blame] | 53 | } |
Lora Lin | 84213eb | 2024-07-22 09:52:01 +0800 | [diff] [blame^] | 54 | #endif |
Delphine CC Chiu | 22fad39 | 2023-10-27 11:05:01 +0800 | [diff] [blame] | 55 | |
Lora Lin | 84213eb | 2024-07-22 09:52:01 +0800 | [diff] [blame^] | 56 | #ifdef LIBPLDM_API_TESTING |
| 57 | TEST(DecodeOemMetaFileIoWriteReq, testInvalidFieldsDecodeRequest) |
Delphine CC Chiu | 22fad39 | 2023-10-27 11:05:01 +0800 | [diff] [blame] | 58 | { |
Lora Lin | 84213eb | 2024-07-22 09:52:01 +0800 | [diff] [blame^] | 59 | struct pldm_msg msg = {}; |
Delphine CC Chiu | 22fad39 | 2023-10-27 11:05:01 +0800 | [diff] [blame] | 60 | |
Lora Lin | 84213eb | 2024-07-22 09:52:01 +0800 | [diff] [blame^] | 61 | auto rc = decode_oem_meta_file_io_write_req(&msg, sizeof(msg), NULL, 0); |
| 62 | EXPECT_EQ(rc, -EINVAL); |
Delphine CC Chiu | 22fad39 | 2023-10-27 11:05:01 +0800 | [diff] [blame] | 63 | } |
Lora Lin | 84213eb | 2024-07-22 09:52:01 +0800 | [diff] [blame^] | 64 | #endif |
Delphine CC Chiu | 22fad39 | 2023-10-27 11:05:01 +0800 | [diff] [blame] | 65 | |
Lora Lin | 84213eb | 2024-07-22 09:52:01 +0800 | [diff] [blame^] | 66 | #ifdef LIBPLDM_API_TESTING |
| 67 | TEST(DecodeOemMetaFileIoWriteReq, testInvalidLengthDecodeRequest) |
Delphine CC Chiu | 22fad39 | 2023-10-27 11:05:01 +0800 | [diff] [blame] | 68 | { |
Lora Lin | 84213eb | 2024-07-22 09:52:01 +0800 | [diff] [blame^] | 69 | struct pldm_oem_meta_file_io_write_req req = {}; |
| 70 | struct pldm_msg msg = {}; |
Delphine CC Chiu | 22fad39 | 2023-10-27 11:05:01 +0800 | [diff] [blame] | 71 | |
Lora Lin | 84213eb | 2024-07-22 09:52:01 +0800 | [diff] [blame^] | 72 | auto rc = decode_oem_meta_file_io_write_req(&msg, 0, &req, sizeof(req)); |
| 73 | EXPECT_EQ(rc, -EOVERFLOW); |
Delphine CC Chiu | 22fad39 | 2023-10-27 11:05:01 +0800 | [diff] [blame] | 74 | } |
Lora Lin | 84213eb | 2024-07-22 09:52:01 +0800 | [diff] [blame^] | 75 | #endif |
Delphine CC Chiu | 22fad39 | 2023-10-27 11:05:01 +0800 | [diff] [blame] | 76 | |
Lora Lin | 84213eb | 2024-07-22 09:52:01 +0800 | [diff] [blame^] | 77 | #ifdef LIBPLDM_API_TESTING |
| 78 | TEST(DecodeOemMetaFileIoWriteReq, testInvalidDataRequest) |
Delphine CC Chiu | 22fad39 | 2023-10-27 11:05:01 +0800 | [diff] [blame] | 79 | { |
Lora Lin | 84213eb | 2024-07-22 09:52:01 +0800 | [diff] [blame^] | 80 | struct pldm_oem_meta_file_io_write_req req = {}; |
| 81 | struct pldm_msg msg = {}; |
| 82 | int rc; |
Delphine CC Chiu | 22fad39 | 2023-10-27 11:05:01 +0800 | [diff] [blame] | 83 | |
Lora Lin | 84213eb | 2024-07-22 09:52:01 +0800 | [diff] [blame^] | 84 | 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 Jeffery | 32df16c | 2024-05-17 14:28:18 +0930 | [diff] [blame] | 88 | } |
Lora Lin | 84213eb | 2024-07-22 09:52:01 +0800 | [diff] [blame^] | 89 | #endif |