blob: ac6e681ef55fe980a1b8c64aa9541dd04c419d2b [file] [log] [blame]
George Liu6492f522020-06-16 10:34:05 +08001#include "libpldm/base.h"
2
Deepak Kodihalli1b24f972019-02-01 04:09:13 -06003#include "libpldmresponder/base.hpp"
4
5#include <string.h>
6
7#include <array>
8
Deepak Kodihalli1b24f972019-02-01 04:09:13 -06009#include <gtest/gtest.h>
10
11using namespace pldm::responder;
12
13TEST(GetPLDMTypes, testGoodRequest)
14{
vkaverapa6575b82019-04-03 05:33:52 -050015 std::array<uint8_t, sizeof(pldm_msg_hdr)> requestPayload{};
16 auto request = reinterpret_cast<pldm_msg*>(requestPayload.data());
17 // payload length will be 0 in this case
18 size_t requestPayloadLength = 0;
Deepak Kodihallibc669f12019-11-28 08:52:07 -060019 base::Handler handler;
20 auto response = handler.getPLDMTypes(request, requestPayloadLength);
Sridevi Rameshf58d9a52020-02-14 00:30:58 -060021 // Need to support OEM type.
vkaverapa6575b82019-04-03 05:33:52 -050022 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
23 uint8_t* payload_ptr = responsePtr->payload;
24 ASSERT_EQ(payload_ptr[0], 0);
Sridevi Rameshf58d9a52020-02-14 00:30:58 -060025 ASSERT_EQ(payload_ptr[1], 29); // 0b11101 see DSP0240 table11
vkaverapa6575b82019-04-03 05:33:52 -050026 ASSERT_EQ(payload_ptr[2], 0);
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060027}
28
29TEST(GetPLDMCommands, testGoodRequest)
30{
Sridevi Rameshf58d9a52020-02-14 00:30:58 -060031 // Need to support OEM type commands.
vkaverapa6575b82019-04-03 05:33:52 -050032 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_COMMANDS_REQ_BYTES>
33 requestPayload{};
34 auto request = reinterpret_cast<pldm_msg*>(requestPayload.data());
35 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
Deepak Kodihallibc669f12019-11-28 08:52:07 -060036 base::Handler handler;
37 auto response = handler.getPLDMCommands(request, requestPayloadLength);
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);
John Wang5c4f80d2019-07-29 11:12:18 +080041 ASSERT_EQ(payload_ptr[1], 60); // 60 = 0b111100
vkaverapa6575b82019-04-03 05:33:52 -050042 ASSERT_EQ(payload_ptr[2], 0);
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060043}
44
45TEST(GetPLDMCommands, testBadRequest)
46{
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());
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060050
vkaverapa6575b82019-04-03 05:33:52 -050051 request->payload[0] = 0xFF;
52 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
Deepak Kodihallibc669f12019-11-28 08:52:07 -060053 base::Handler handler;
54 auto response = handler.getPLDMCommands(request, requestPayloadLength);
vkaverapa6575b82019-04-03 05:33:52 -050055 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
56 uint8_t* payload_ptr = responsePtr->payload;
57 ASSERT_EQ(payload_ptr[0], PLDM_ERROR_INVALID_PLDM_TYPE);
Deepak Kodihalli1b24f972019-02-01 04:09:13 -060058}
Sampa Misra432e1872019-02-13 03:49:43 -060059TEST(GetPLDMVersion, testGoodRequest)
60{
vkaverapa6575b82019-04-03 05:33:52 -050061 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_VERSION_REQ_BYTES>
62 requestPayload{};
63 auto request = reinterpret_cast<pldm_msg*>(requestPayload.data());
64 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
Sampa Misra432e1872019-02-13 03:49:43 -060065
66 uint8_t pldmType = PLDM_BASE;
67 uint32_t transferHandle = 0x0;
68 uint8_t flag = PLDM_GET_FIRSTPART;
69 uint8_t retFlag = PLDM_START_AND_END;
Deepak Kodihalli97e0bd52019-02-21 03:54:22 -060070 ver32_t version = {0xF1, 0xF0, 0xF0, 0x00};
Sampa Misra432e1872019-02-13 03:49:43 -060071
72 auto rc =
vkaverapa6575b82019-04-03 05:33:52 -050073 encode_get_version_req(0, transferHandle, flag, pldmType, request);
Sampa Misra432e1872019-02-13 03:49:43 -060074
75 ASSERT_EQ(0, rc);
76
Deepak Kodihallibc669f12019-11-28 08:52:07 -060077 base::Handler handler;
78 auto response = handler.getPLDMVersion(request, requestPayloadLength);
vkaverapa6575b82019-04-03 05:33:52 -050079 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
Sampa Misra432e1872019-02-13 03:49:43 -060080
vkaverapa6575b82019-04-03 05:33:52 -050081 ASSERT_EQ(responsePtr->payload[0], 0);
82 ASSERT_EQ(0, memcmp(responsePtr->payload + sizeof(responsePtr->payload[0]),
83 &transferHandle, sizeof(transferHandle)));
84 ASSERT_EQ(0, memcmp(responsePtr->payload + sizeof(responsePtr->payload[0]) +
85 sizeof(transferHandle),
86 &retFlag, sizeof(flag)));
87 ASSERT_EQ(0, memcmp(responsePtr->payload + sizeof(responsePtr->payload[0]) +
88 sizeof(transferHandle) + sizeof(flag),
89 &version, sizeof(version)));
Sampa Misra432e1872019-02-13 03:49:43 -060090}
Sampa Misra94d704b2019-02-22 03:36:02 -060091TEST(GetPLDMVersion, testBadRequest)
92{
vkaverapa6575b82019-04-03 05:33:52 -050093 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_VERSION_REQ_BYTES>
94 requestPayload{};
95 auto request = reinterpret_cast<pldm_msg*>(requestPayload.data());
96 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
Sampa Misra94d704b2019-02-22 03:36:02 -060097
98 uint8_t pldmType = 7;
99 uint32_t transferHandle = 0x0;
100 uint8_t flag = PLDM_GET_FIRSTPART;
101
102 auto rc =
vkaverapa6575b82019-04-03 05:33:52 -0500103 encode_get_version_req(0, transferHandle, flag, pldmType, request);
Sampa Misra94d704b2019-02-22 03:36:02 -0600104
105 ASSERT_EQ(0, rc);
106
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600107 base::Handler handler;
108 auto response = handler.getPLDMVersion(request, requestPayloadLength - 1);
vkaverapa6575b82019-04-03 05:33:52 -0500109 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
Sampa Misra94d704b2019-02-22 03:36:02 -0600110
vkaverapa6575b82019-04-03 05:33:52 -0500111 ASSERT_EQ(responsePtr->payload[0], PLDM_ERROR_INVALID_LENGTH);
Sampa Misra94d704b2019-02-22 03:36:02 -0600112
vkaverapa6575b82019-04-03 05:33:52 -0500113 request = reinterpret_cast<pldm_msg*>(requestPayload.data());
114 requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
Sampa Misra94d704b2019-02-22 03:36:02 -0600115
vkaverapa6575b82019-04-03 05:33:52 -0500116 rc = encode_get_version_req(0, transferHandle, flag, pldmType, request);
Sampa Misra94d704b2019-02-22 03:36:02 -0600117
118 ASSERT_EQ(0, rc);
119
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600120 response = handler.getPLDMVersion(request, requestPayloadLength);
vkaverapa6575b82019-04-03 05:33:52 -0500121 responsePtr = reinterpret_cast<pldm_msg*>(response.data());
Sampa Misra94d704b2019-02-22 03:36:02 -0600122
vkaverapa6575b82019-04-03 05:33:52 -0500123 ASSERT_EQ(responsePtr->payload[0], PLDM_ERROR_INVALID_PLDM_TYPE);
Sampa Misra94d704b2019-02-22 03:36:02 -0600124}
John Wang5c4f80d2019-07-29 11:12:18 +0800125
126TEST(GetTID, testGoodRequest)
127{
128 std::array<uint8_t, sizeof(pldm_msg_hdr)> requestPayload{};
129 auto request = reinterpret_cast<pldm_msg*>(requestPayload.data());
130 size_t requestPayloadLength = 0;
131
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600132 base::Handler handler;
133 auto response = handler.getTID(request, requestPayloadLength);
John Wang5c4f80d2019-07-29 11:12:18 +0800134
135 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
136 uint8_t* payload = responsePtr->payload;
137
138 ASSERT_EQ(payload[0], 0);
139 ASSERT_EQ(payload[1], 1);
140}