blob: f056ec85ccc50310345c8d4c1a492b6eda4e8ef4 [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:
20 TestBaseCommands() :
Andrew Jefferya330b2f2023-05-04 14:55:37 +093021 instanceIdDb(), event(sdeventplus::Event::get_default())
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -050022 {}
23
24 uint8_t mctpEid = 0;
Andrew Jeffery7c1dc7e2023-04-28 14:52:16 +093025 TestInstanceIdDb instanceIdDb;
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -050026 sdeventplus::Event event;
27};
28
29TEST_F(TestBaseCommands, testPLDMTypesGoodRequest)
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060030{
vkaverapa6575b82019-04-03 05:33:52 -050031 std::array<uint8_t, sizeof(pldm_msg_hdr)> requestPayload{};
32 auto request = reinterpret_cast<pldm_msg*>(requestPayload.data());
33 // payload length will be 0 in this case
34 size_t requestPayloadLength = 0;
Andrew Jefferya330b2f2023-05-04 14:55:37 +093035 base::Handler handler(mctpEid, instanceIdDb, event, nullptr, nullptr);
Deepak Kodihallibc669f12019-11-28 08:52:07 -060036 auto response = handler.getPLDMTypes(request, requestPayloadLength);
Sridevi Rameshf58d9a52020-02-14 00:30:58 -060037 // Need to support OEM type.
vkaverapa6575b82019-04-03 05:33:52 -050038 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
39 uint8_t* payload_ptr = responsePtr->payload;
40 ASSERT_EQ(payload_ptr[0], 0);
Sridevi Rameshf58d9a52020-02-14 00:30:58 -060041 ASSERT_EQ(payload_ptr[1], 29); // 0b11101 see DSP0240 table11
vkaverapa6575b82019-04-03 05:33:52 -050042 ASSERT_EQ(payload_ptr[2], 0);
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060043}
44
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -050045TEST_F(TestBaseCommands, testGetPLDMCommandsGoodRequest)
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060046{
Sridevi Rameshf58d9a52020-02-14 00:30:58 -060047 // Need to support OEM type commands.
vkaverapa6575b82019-04-03 05:33:52 -050048 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_COMMANDS_REQ_BYTES>
49 requestPayload{};
50 auto request = reinterpret_cast<pldm_msg*>(requestPayload.data());
51 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
Andrew Jefferya330b2f2023-05-04 14:55:37 +093052 base::Handler handler(mctpEid, instanceIdDb, event, nullptr, nullptr);
Deepak Kodihallibc669f12019-11-28 08:52:07 -060053 auto response = handler.getPLDMCommands(request, requestPayloadLength);
vkaverapa6575b82019-04-03 05:33:52 -050054 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
55 uint8_t* payload_ptr = responsePtr->payload;
56 ASSERT_EQ(payload_ptr[0], 0);
John Wang5c4f80d2019-07-29 11:12:18 +080057 ASSERT_EQ(payload_ptr[1], 60); // 60 = 0b111100
vkaverapa6575b82019-04-03 05:33:52 -050058 ASSERT_EQ(payload_ptr[2], 0);
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060059}
60
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -050061TEST_F(TestBaseCommands, testGetPLDMCommandsBadRequest)
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060062{
vkaverapa6575b82019-04-03 05:33:52 -050063 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_COMMANDS_REQ_BYTES>
64 requestPayload{};
65 auto request = reinterpret_cast<pldm_msg*>(requestPayload.data());
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060066
vkaverapa6575b82019-04-03 05:33:52 -050067 request->payload[0] = 0xFF;
68 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
Andrew Jefferya330b2f2023-05-04 14:55:37 +093069 base::Handler handler(mctpEid, instanceIdDb, event, nullptr, nullptr);
Deepak Kodihallibc669f12019-11-28 08:52:07 -060070 auto response = handler.getPLDMCommands(request, requestPayloadLength);
vkaverapa6575b82019-04-03 05:33:52 -050071 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
72 uint8_t* payload_ptr = responsePtr->payload;
73 ASSERT_EQ(payload_ptr[0], PLDM_ERROR_INVALID_PLDM_TYPE);
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060074}
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -050075
76TEST_F(TestBaseCommands, testGetPLDMVersionGoodRequest)
Sampa Misra432e1872019-02-13 03:49:43 -060077{
vkaverapa6575b82019-04-03 05:33:52 -050078 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_VERSION_REQ_BYTES>
79 requestPayload{};
80 auto request = reinterpret_cast<pldm_msg*>(requestPayload.data());
81 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
Sampa Misra432e1872019-02-13 03:49:43 -060082
83 uint8_t pldmType = PLDM_BASE;
84 uint32_t transferHandle = 0x0;
85 uint8_t flag = PLDM_GET_FIRSTPART;
86 uint8_t retFlag = PLDM_START_AND_END;
Manojkiran Edacb454672022-06-29 11:05:42 +053087 ver32_t version = {0x00, 0xF0, 0xF0, 0xF1};
Sampa Misra432e1872019-02-13 03:49:43 -060088
Patrick Williams6da4f912023-05-10 07:50:53 -050089 auto rc = encode_get_version_req(0, transferHandle, flag, pldmType,
90 request);
Sampa Misra432e1872019-02-13 03:49:43 -060091
92 ASSERT_EQ(0, rc);
93
Andrew Jefferya330b2f2023-05-04 14:55:37 +093094 base::Handler handler(mctpEid, instanceIdDb, event, nullptr, nullptr);
Deepak Kodihallibc669f12019-11-28 08:52:07 -060095 auto response = handler.getPLDMVersion(request, requestPayloadLength);
vkaverapa6575b82019-04-03 05:33:52 -050096 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
Sampa Misra432e1872019-02-13 03:49:43 -060097
vkaverapa6575b82019-04-03 05:33:52 -050098 ASSERT_EQ(responsePtr->payload[0], 0);
99 ASSERT_EQ(0, memcmp(responsePtr->payload + sizeof(responsePtr->payload[0]),
100 &transferHandle, sizeof(transferHandle)));
101 ASSERT_EQ(0, memcmp(responsePtr->payload + sizeof(responsePtr->payload[0]) +
102 sizeof(transferHandle),
103 &retFlag, sizeof(flag)));
104 ASSERT_EQ(0, memcmp(responsePtr->payload + sizeof(responsePtr->payload[0]) +
105 sizeof(transferHandle) + sizeof(flag),
106 &version, sizeof(version)));
Sampa Misra432e1872019-02-13 03:49:43 -0600107}
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500108
109TEST_F(TestBaseCommands, testGetPLDMVersionBadRequest)
Sampa Misra94d704b2019-02-22 03:36:02 -0600110{
vkaverapa6575b82019-04-03 05:33:52 -0500111 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_VERSION_REQ_BYTES>
112 requestPayload{};
113 auto request = reinterpret_cast<pldm_msg*>(requestPayload.data());
114 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
Sampa Misra94d704b2019-02-22 03:36:02 -0600115
116 uint8_t pldmType = 7;
117 uint32_t transferHandle = 0x0;
118 uint8_t flag = PLDM_GET_FIRSTPART;
119
Patrick Williams6da4f912023-05-10 07:50:53 -0500120 auto rc = encode_get_version_req(0, transferHandle, flag, pldmType,
121 request);
Sampa Misra94d704b2019-02-22 03:36:02 -0600122
123 ASSERT_EQ(0, rc);
124
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930125 base::Handler handler(mctpEid, instanceIdDb, event, nullptr, nullptr);
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600126 auto response = handler.getPLDMVersion(request, requestPayloadLength - 1);
vkaverapa6575b82019-04-03 05:33:52 -0500127 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
Sampa Misra94d704b2019-02-22 03:36:02 -0600128
vkaverapa6575b82019-04-03 05:33:52 -0500129 ASSERT_EQ(responsePtr->payload[0], PLDM_ERROR_INVALID_LENGTH);
Sampa Misra94d704b2019-02-22 03:36:02 -0600130
vkaverapa6575b82019-04-03 05:33:52 -0500131 request = reinterpret_cast<pldm_msg*>(requestPayload.data());
132 requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
Sampa Misra94d704b2019-02-22 03:36:02 -0600133
vkaverapa6575b82019-04-03 05:33:52 -0500134 rc = encode_get_version_req(0, transferHandle, flag, pldmType, request);
Sampa Misra94d704b2019-02-22 03:36:02 -0600135
136 ASSERT_EQ(0, rc);
137
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600138 response = handler.getPLDMVersion(request, requestPayloadLength);
vkaverapa6575b82019-04-03 05:33:52 -0500139 responsePtr = reinterpret_cast<pldm_msg*>(response.data());
Sampa Misra94d704b2019-02-22 03:36:02 -0600140
vkaverapa6575b82019-04-03 05:33:52 -0500141 ASSERT_EQ(responsePtr->payload[0], PLDM_ERROR_INVALID_PLDM_TYPE);
Sampa Misra94d704b2019-02-22 03:36:02 -0600142}
John Wang5c4f80d2019-07-29 11:12:18 +0800143
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500144TEST_F(TestBaseCommands, testGetTIDGoodRequest)
John Wang5c4f80d2019-07-29 11:12:18 +0800145{
146 std::array<uint8_t, sizeof(pldm_msg_hdr)> requestPayload{};
147 auto request = reinterpret_cast<pldm_msg*>(requestPayload.data());
148 size_t requestPayloadLength = 0;
149
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930150 base::Handler handler(mctpEid, instanceIdDb, event, nullptr, nullptr);
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600151 auto response = handler.getTID(request, requestPayloadLength);
John Wang5c4f80d2019-07-29 11:12:18 +0800152
153 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
154 uint8_t* payload = responsePtr->payload;
155
156 ASSERT_EQ(payload[0], 0);
157 ASSERT_EQ(payload[1], 1);
158}