blob: 46568823717e8fc5717e90a202ffee4565c12462 [file] [log] [blame]
Andrew Jeffery2abbce72023-10-18 10:17:35 +10301#include "common/instance_id.hpp"
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -05002#include "common/utils.hpp"
Deepak Kodihalli1b24f972019-02-01 04:09:13 -06003#include "libpldmresponder/base.hpp"
Andrew Jeffery7c1dc7e2023-04-28 14:52:16 +09304#include "test/test_instance_id.hpp"
Deepak Kodihalli1b24f972019-02-01 04:09:13 -06005
George Liuc453e162022-12-21 17:16:23 +08006#include <libpldm/base.h>
Deepak Kodihalli1b24f972019-02-01 04:09:13 -06007#include <string.h>
8
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -05009#include <sdeventplus/event.hpp>
10
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060011#include <array>
12
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060013#include <gtest/gtest.h>
14
15using namespace pldm::responder;
16
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -050017class TestBaseCommands : public testing::Test
18{
19 protected:
Sagar Srinivas90314a32023-10-17 10:38:03 -050020 TestBaseCommands() : event(sdeventplus::Event::get_default()) {}
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -050021
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -050022 sdeventplus::Event event;
23};
24
25TEST_F(TestBaseCommands, testPLDMTypesGoodRequest)
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060026{
vkaverapa6575b82019-04-03 05:33:52 -050027 std::array<uint8_t, sizeof(pldm_msg_hdr)> requestPayload{};
28 auto request = reinterpret_cast<pldm_msg*>(requestPayload.data());
29 // payload length will be 0 in this case
30 size_t requestPayloadLength = 0;
George Liua881c172021-06-21 18:28:11 +080031 base::Handler handler(event);
Deepak Kodihallibc669f12019-11-28 08:52:07 -060032 auto response = handler.getPLDMTypes(request, requestPayloadLength);
Sridevi Rameshf58d9a52020-02-14 00:30:58 -060033 // Need to support OEM type.
vkaverapa6575b82019-04-03 05:33:52 -050034 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
35 uint8_t* payload_ptr = responsePtr->payload;
36 ASSERT_EQ(payload_ptr[0], 0);
Sridevi Rameshf58d9a52020-02-14 00:30:58 -060037 ASSERT_EQ(payload_ptr[1], 29); // 0b11101 see DSP0240 table11
vkaverapa6575b82019-04-03 05:33:52 -050038 ASSERT_EQ(payload_ptr[2], 0);
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060039}
40
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -050041TEST_F(TestBaseCommands, testGetPLDMCommandsGoodRequest)
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060042{
Sridevi Rameshf58d9a52020-02-14 00:30:58 -060043 // Need to support OEM type commands.
vkaverapa6575b82019-04-03 05:33:52 -050044 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_COMMANDS_REQ_BYTES>
45 requestPayload{};
46 auto request = reinterpret_cast<pldm_msg*>(requestPayload.data());
47 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
George Liua881c172021-06-21 18:28:11 +080048 base::Handler handler(event);
Deepak Kodihallibc669f12019-11-28 08:52:07 -060049 auto response = handler.getPLDMCommands(request, requestPayloadLength);
vkaverapa6575b82019-04-03 05:33:52 -050050 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
51 uint8_t* payload_ptr = responsePtr->payload;
52 ASSERT_EQ(payload_ptr[0], 0);
John Wang5c4f80d2019-07-29 11:12:18 +080053 ASSERT_EQ(payload_ptr[1], 60); // 60 = 0b111100
vkaverapa6575b82019-04-03 05:33:52 -050054 ASSERT_EQ(payload_ptr[2], 0);
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060055}
56
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -050057TEST_F(TestBaseCommands, testGetPLDMCommandsBadRequest)
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060058{
vkaverapa6575b82019-04-03 05:33:52 -050059 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_COMMANDS_REQ_BYTES>
60 requestPayload{};
61 auto request = reinterpret_cast<pldm_msg*>(requestPayload.data());
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060062
vkaverapa6575b82019-04-03 05:33:52 -050063 request->payload[0] = 0xFF;
64 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
George Liua881c172021-06-21 18:28:11 +080065 base::Handler handler(event);
Deepak Kodihallibc669f12019-11-28 08:52:07 -060066 auto response = handler.getPLDMCommands(request, requestPayloadLength);
vkaverapa6575b82019-04-03 05:33:52 -050067 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
68 uint8_t* payload_ptr = responsePtr->payload;
69 ASSERT_EQ(payload_ptr[0], PLDM_ERROR_INVALID_PLDM_TYPE);
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060070}
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -050071
72TEST_F(TestBaseCommands, testGetPLDMVersionGoodRequest)
Sampa Misra432e1872019-02-13 03:49:43 -060073{
vkaverapa6575b82019-04-03 05:33:52 -050074 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_VERSION_REQ_BYTES>
75 requestPayload{};
76 auto request = reinterpret_cast<pldm_msg*>(requestPayload.data());
77 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
Sampa Misra432e1872019-02-13 03:49:43 -060078
79 uint8_t pldmType = PLDM_BASE;
80 uint32_t transferHandle = 0x0;
81 uint8_t flag = PLDM_GET_FIRSTPART;
82 uint8_t retFlag = PLDM_START_AND_END;
Manojkiran Edacb454672022-06-29 11:05:42 +053083 ver32_t version = {0x00, 0xF0, 0xF0, 0xF1};
Sampa Misra432e1872019-02-13 03:49:43 -060084
Patrick Williams16c2a0a2024-08-16 15:20:59 -040085 auto rc =
86 encode_get_version_req(0, transferHandle, flag, pldmType, request);
Sampa Misra432e1872019-02-13 03:49:43 -060087
88 ASSERT_EQ(0, rc);
89
George Liua881c172021-06-21 18:28:11 +080090 base::Handler handler(event);
Deepak Kodihallibc669f12019-11-28 08:52:07 -060091 auto response = handler.getPLDMVersion(request, requestPayloadLength);
vkaverapa6575b82019-04-03 05:33:52 -050092 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
Sampa Misra432e1872019-02-13 03:49:43 -060093
vkaverapa6575b82019-04-03 05:33:52 -050094 ASSERT_EQ(responsePtr->payload[0], 0);
95 ASSERT_EQ(0, memcmp(responsePtr->payload + sizeof(responsePtr->payload[0]),
96 &transferHandle, sizeof(transferHandle)));
97 ASSERT_EQ(0, memcmp(responsePtr->payload + sizeof(responsePtr->payload[0]) +
98 sizeof(transferHandle),
99 &retFlag, sizeof(flag)));
100 ASSERT_EQ(0, memcmp(responsePtr->payload + sizeof(responsePtr->payload[0]) +
101 sizeof(transferHandle) + sizeof(flag),
102 &version, sizeof(version)));
Sampa Misra432e1872019-02-13 03:49:43 -0600103}
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500104
105TEST_F(TestBaseCommands, testGetPLDMVersionBadRequest)
Sampa Misra94d704b2019-02-22 03:36:02 -0600106{
vkaverapa6575b82019-04-03 05:33:52 -0500107 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_VERSION_REQ_BYTES>
108 requestPayload{};
109 auto request = reinterpret_cast<pldm_msg*>(requestPayload.data());
110 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
Sampa Misra94d704b2019-02-22 03:36:02 -0600111
112 uint8_t pldmType = 7;
113 uint32_t transferHandle = 0x0;
114 uint8_t flag = PLDM_GET_FIRSTPART;
115
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400116 auto rc =
117 encode_get_version_req(0, transferHandle, flag, pldmType, request);
Sampa Misra94d704b2019-02-22 03:36:02 -0600118
119 ASSERT_EQ(0, rc);
120
George Liua881c172021-06-21 18:28:11 +0800121 base::Handler handler(event);
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600122 auto response = handler.getPLDMVersion(request, requestPayloadLength - 1);
vkaverapa6575b82019-04-03 05:33:52 -0500123 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
Sampa Misra94d704b2019-02-22 03:36:02 -0600124
vkaverapa6575b82019-04-03 05:33:52 -0500125 ASSERT_EQ(responsePtr->payload[0], PLDM_ERROR_INVALID_LENGTH);
Sampa Misra94d704b2019-02-22 03:36:02 -0600126
vkaverapa6575b82019-04-03 05:33:52 -0500127 request = reinterpret_cast<pldm_msg*>(requestPayload.data());
128 requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
Sampa Misra94d704b2019-02-22 03:36:02 -0600129
vkaverapa6575b82019-04-03 05:33:52 -0500130 rc = encode_get_version_req(0, transferHandle, flag, pldmType, request);
Sampa Misra94d704b2019-02-22 03:36:02 -0600131
132 ASSERT_EQ(0, rc);
133
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600134 response = handler.getPLDMVersion(request, requestPayloadLength);
vkaverapa6575b82019-04-03 05:33:52 -0500135 responsePtr = reinterpret_cast<pldm_msg*>(response.data());
Sampa Misra94d704b2019-02-22 03:36:02 -0600136
vkaverapa6575b82019-04-03 05:33:52 -0500137 ASSERT_EQ(responsePtr->payload[0], PLDM_ERROR_INVALID_PLDM_TYPE);
Sampa Misra94d704b2019-02-22 03:36:02 -0600138}
John Wang5c4f80d2019-07-29 11:12:18 +0800139
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500140TEST_F(TestBaseCommands, testGetTIDGoodRequest)
John Wang5c4f80d2019-07-29 11:12:18 +0800141{
142 std::array<uint8_t, sizeof(pldm_msg_hdr)> requestPayload{};
143 auto request = reinterpret_cast<pldm_msg*>(requestPayload.data());
144 size_t requestPayloadLength = 0;
145
George Liua881c172021-06-21 18:28:11 +0800146 base::Handler handler(event);
147 handler.setOemPlatformHandler(nullptr);
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600148 auto response = handler.getTID(request, requestPayloadLength);
John Wang5c4f80d2019-07-29 11:12:18 +0800149
150 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
151 uint8_t* payload = responsePtr->payload;
152
153 ASSERT_EQ(payload[0], 0);
154 ASSERT_EQ(payload[1], 1);
155}