blob: 16c06dcc31cbe6ffbf24b41ae65502dcc826998e [file] [log] [blame]
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -05001#include "common/utils.hpp"
Deepak Kodihalli1b24f972019-02-01 04:09:13 -06002#include "libpldmresponder/base.hpp"
3
George Liuc453e162022-12-21 17:16:23 +08004#include <libpldm/base.h>
Deepak Kodihalli1b24f972019-02-01 04:09:13 -06005#include <string.h>
6
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -05007#include <sdeventplus/event.hpp>
8
Deepak Kodihalli1b24f972019-02-01 04:09:13 -06009#include <array>
10
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060011#include <gtest/gtest.h>
12
13using namespace pldm::responder;
14
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -050015class TestBaseCommands : public testing::Test
16{
17 protected:
18 TestBaseCommands() :
19 requester(pldm::utils::DBusHandler::getBus(), "/abc/def"),
20 event(sdeventplus::Event::get_default())
21 {}
22
23 uint8_t mctpEid = 0;
24 Requester requester;
25 sdeventplus::Event event;
26};
27
28TEST_F(TestBaseCommands, testPLDMTypesGoodRequest)
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060029{
vkaverapa6575b82019-04-03 05:33:52 -050030 std::array<uint8_t, sizeof(pldm_msg_hdr)> requestPayload{};
31 auto request = reinterpret_cast<pldm_msg*>(requestPayload.data());
32 // payload length will be 0 in this case
33 size_t requestPayloadLength = 0;
Sagar Srinivas79669c92021-04-28 15:43:30 -050034 base::Handler handler(mctpEid, requester, event, nullptr, nullptr);
Deepak Kodihallibc669f12019-11-28 08:52:07 -060035 auto response = handler.getPLDMTypes(request, requestPayloadLength);
Sridevi Rameshf58d9a52020-02-14 00:30:58 -060036 // Need to support OEM type.
vkaverapa6575b82019-04-03 05:33:52 -050037 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
38 uint8_t* payload_ptr = responsePtr->payload;
39 ASSERT_EQ(payload_ptr[0], 0);
Sridevi Rameshf58d9a52020-02-14 00:30:58 -060040 ASSERT_EQ(payload_ptr[1], 29); // 0b11101 see DSP0240 table11
vkaverapa6575b82019-04-03 05:33:52 -050041 ASSERT_EQ(payload_ptr[2], 0);
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060042}
43
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -050044TEST_F(TestBaseCommands, testGetPLDMCommandsGoodRequest)
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060045{
Sridevi Rameshf58d9a52020-02-14 00:30:58 -060046 // Need to support OEM type commands.
vkaverapa6575b82019-04-03 05:33:52 -050047 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_COMMANDS_REQ_BYTES>
48 requestPayload{};
49 auto request = reinterpret_cast<pldm_msg*>(requestPayload.data());
50 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
Sagar Srinivas79669c92021-04-28 15:43:30 -050051 base::Handler handler(mctpEid, requester, event, nullptr, nullptr);
Deepak Kodihallibc669f12019-11-28 08:52:07 -060052 auto response = handler.getPLDMCommands(request, requestPayloadLength);
vkaverapa6575b82019-04-03 05:33:52 -050053 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
54 uint8_t* payload_ptr = responsePtr->payload;
55 ASSERT_EQ(payload_ptr[0], 0);
John Wang5c4f80d2019-07-29 11:12:18 +080056 ASSERT_EQ(payload_ptr[1], 60); // 60 = 0b111100
vkaverapa6575b82019-04-03 05:33:52 -050057 ASSERT_EQ(payload_ptr[2], 0);
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060058}
59
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -050060TEST_F(TestBaseCommands, testGetPLDMCommandsBadRequest)
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060061{
vkaverapa6575b82019-04-03 05:33:52 -050062 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_COMMANDS_REQ_BYTES>
63 requestPayload{};
64 auto request = reinterpret_cast<pldm_msg*>(requestPayload.data());
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060065
vkaverapa6575b82019-04-03 05:33:52 -050066 request->payload[0] = 0xFF;
67 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
Sagar Srinivas79669c92021-04-28 15:43:30 -050068 base::Handler handler(mctpEid, requester, event, nullptr, nullptr);
Deepak Kodihallibc669f12019-11-28 08:52:07 -060069 auto response = handler.getPLDMCommands(request, requestPayloadLength);
vkaverapa6575b82019-04-03 05:33:52 -050070 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
71 uint8_t* payload_ptr = responsePtr->payload;
72 ASSERT_EQ(payload_ptr[0], PLDM_ERROR_INVALID_PLDM_TYPE);
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060073}
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -050074
75TEST_F(TestBaseCommands, testGetPLDMVersionGoodRequest)
Sampa Misra432e1872019-02-13 03:49:43 -060076{
vkaverapa6575b82019-04-03 05:33:52 -050077 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_VERSION_REQ_BYTES>
78 requestPayload{};
79 auto request = reinterpret_cast<pldm_msg*>(requestPayload.data());
80 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
Sampa Misra432e1872019-02-13 03:49:43 -060081
82 uint8_t pldmType = PLDM_BASE;
83 uint32_t transferHandle = 0x0;
84 uint8_t flag = PLDM_GET_FIRSTPART;
85 uint8_t retFlag = PLDM_START_AND_END;
Manojkiran Edacb454672022-06-29 11:05:42 +053086 ver32_t version = {0x00, 0xF0, 0xF0, 0xF1};
Sampa Misra432e1872019-02-13 03:49:43 -060087
88 auto rc =
vkaverapa6575b82019-04-03 05:33:52 -050089 encode_get_version_req(0, transferHandle, flag, pldmType, request);
Sampa Misra432e1872019-02-13 03:49:43 -060090
91 ASSERT_EQ(0, rc);
92
Sagar Srinivas79669c92021-04-28 15:43:30 -050093 base::Handler handler(mctpEid, requester, event, nullptr, nullptr);
Deepak Kodihallibc669f12019-11-28 08:52:07 -060094 auto response = handler.getPLDMVersion(request, requestPayloadLength);
vkaverapa6575b82019-04-03 05:33:52 -050095 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
Sampa Misra432e1872019-02-13 03:49:43 -060096
vkaverapa6575b82019-04-03 05:33:52 -050097 ASSERT_EQ(responsePtr->payload[0], 0);
98 ASSERT_EQ(0, memcmp(responsePtr->payload + sizeof(responsePtr->payload[0]),
99 &transferHandle, sizeof(transferHandle)));
100 ASSERT_EQ(0, memcmp(responsePtr->payload + sizeof(responsePtr->payload[0]) +
101 sizeof(transferHandle),
102 &retFlag, sizeof(flag)));
103 ASSERT_EQ(0, memcmp(responsePtr->payload + sizeof(responsePtr->payload[0]) +
104 sizeof(transferHandle) + sizeof(flag),
105 &version, sizeof(version)));
Sampa Misra432e1872019-02-13 03:49:43 -0600106}
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500107
108TEST_F(TestBaseCommands, testGetPLDMVersionBadRequest)
Sampa Misra94d704b2019-02-22 03:36:02 -0600109{
vkaverapa6575b82019-04-03 05:33:52 -0500110 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_VERSION_REQ_BYTES>
111 requestPayload{};
112 auto request = reinterpret_cast<pldm_msg*>(requestPayload.data());
113 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
Sampa Misra94d704b2019-02-22 03:36:02 -0600114
115 uint8_t pldmType = 7;
116 uint32_t transferHandle = 0x0;
117 uint8_t flag = PLDM_GET_FIRSTPART;
118
119 auto rc =
vkaverapa6575b82019-04-03 05:33:52 -0500120 encode_get_version_req(0, transferHandle, flag, pldmType, request);
Sampa Misra94d704b2019-02-22 03:36:02 -0600121
122 ASSERT_EQ(0, rc);
123
Sagar Srinivas79669c92021-04-28 15:43:30 -0500124 base::Handler handler(mctpEid, requester, event, nullptr, nullptr);
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600125 auto response = handler.getPLDMVersion(request, requestPayloadLength - 1);
vkaverapa6575b82019-04-03 05:33:52 -0500126 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
Sampa Misra94d704b2019-02-22 03:36:02 -0600127
vkaverapa6575b82019-04-03 05:33:52 -0500128 ASSERT_EQ(responsePtr->payload[0], PLDM_ERROR_INVALID_LENGTH);
Sampa Misra94d704b2019-02-22 03:36:02 -0600129
vkaverapa6575b82019-04-03 05:33:52 -0500130 request = reinterpret_cast<pldm_msg*>(requestPayload.data());
131 requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
Sampa Misra94d704b2019-02-22 03:36:02 -0600132
vkaverapa6575b82019-04-03 05:33:52 -0500133 rc = encode_get_version_req(0, transferHandle, flag, pldmType, request);
Sampa Misra94d704b2019-02-22 03:36:02 -0600134
135 ASSERT_EQ(0, rc);
136
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600137 response = handler.getPLDMVersion(request, requestPayloadLength);
vkaverapa6575b82019-04-03 05:33:52 -0500138 responsePtr = reinterpret_cast<pldm_msg*>(response.data());
Sampa Misra94d704b2019-02-22 03:36:02 -0600139
vkaverapa6575b82019-04-03 05:33:52 -0500140 ASSERT_EQ(responsePtr->payload[0], PLDM_ERROR_INVALID_PLDM_TYPE);
Sampa Misra94d704b2019-02-22 03:36:02 -0600141}
John Wang5c4f80d2019-07-29 11:12:18 +0800142
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -0500143TEST_F(TestBaseCommands, testGetTIDGoodRequest)
John Wang5c4f80d2019-07-29 11:12:18 +0800144{
145 std::array<uint8_t, sizeof(pldm_msg_hdr)> requestPayload{};
146 auto request = reinterpret_cast<pldm_msg*>(requestPayload.data());
147 size_t requestPayloadLength = 0;
148
Sagar Srinivas79669c92021-04-28 15:43:30 -0500149 base::Handler handler(mctpEid, requester, event, nullptr, nullptr);
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600150 auto response = handler.getTID(request, requestPayloadLength);
John Wang5c4f80d2019-07-29 11:12:18 +0800151
152 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
153 uint8_t* payload = responsePtr->payload;
154
155 ASSERT_EQ(payload[0], 0);
156 ASSERT_EQ(payload[1], 1);
157}