blob: 3ef8006b48b081b341d768d54395878cfc6f36d3 [file] [log] [blame]
George Liu6492f522020-06-16 10:34:05 +08001#include "libpldm/base.h"
2
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -05003#include "common/utils.hpp"
Deepak Kodihalli1b24f972019-02-01 04:09:13 -06004#include "libpldmresponder/base.hpp"
5
6#include <string.h>
7
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -05008#include <sdeventplus/event.hpp>
9
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060010#include <array>
11
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060012#include <gtest/gtest.h>
13
14using namespace pldm::responder;
15
Sagar Srinivasa6a8ccd2021-04-01 07:58:33 -050016class TestBaseCommands : public testing::Test
17{
18 protected:
19 TestBaseCommands() :
20 requester(pldm::utils::DBusHandler::getBus(), "/abc/def"),
21 event(sdeventplus::Event::get_default())
22 {}
23
24 uint8_t mctpEid = 0;
25 Requester requester;
26 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;
Sagar Srinivas79669c92021-04-28 15:43:30 -050035 base::Handler handler(mctpEid, requester, 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);
Sagar Srinivas79669c92021-04-28 15:43:30 -050052 base::Handler handler(mctpEid, requester, 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);
Sagar Srinivas79669c92021-04-28 15:43:30 -050069 base::Handler handler(mctpEid, requester, 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
89 auto rc =
vkaverapa6575b82019-04-03 05:33:52 -050090 encode_get_version_req(0, transferHandle, flag, pldmType, request);
Sampa Misra432e1872019-02-13 03:49:43 -060091
92 ASSERT_EQ(0, rc);
93
Sagar Srinivas79669c92021-04-28 15:43:30 -050094 base::Handler handler(mctpEid, requester, 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
120 auto rc =
vkaverapa6575b82019-04-03 05:33:52 -0500121 encode_get_version_req(0, transferHandle, flag, pldmType, request);
Sampa Misra94d704b2019-02-22 03:36:02 -0600122
123 ASSERT_EQ(0, rc);
124
Sagar Srinivas79669c92021-04-28 15:43:30 -0500125 base::Handler handler(mctpEid, requester, 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
Sagar Srinivas79669c92021-04-28 15:43:30 -0500150 base::Handler handler(mctpEid, requester, 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}