1 #include "libpldm/base.h"
2 
3 #include "libpldmresponder/base.hpp"
4 
5 #include <string.h>
6 
7 #include <array>
8 
9 #include <gtest/gtest.h>
10 
11 using namespace pldm::responder;
12 
13 TEST(GetPLDMTypes, testGoodRequest)
14 {
15     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;
19     base::Handler handler;
20     auto response = handler.getPLDMTypes(request, requestPayloadLength);
21     // Need to support OEM type.
22     auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
23     uint8_t* payload_ptr = responsePtr->payload;
24     ASSERT_EQ(payload_ptr[0], 0);
25     ASSERT_EQ(payload_ptr[1], 29); // 0b11101 see DSP0240 table11
26     ASSERT_EQ(payload_ptr[2], 0);
27 }
28 
29 TEST(GetPLDMCommands, testGoodRequest)
30 {
31     // Need to support OEM type commands.
32     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);
36     base::Handler handler;
37     auto response = handler.getPLDMCommands(request, requestPayloadLength);
38     auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
39     uint8_t* payload_ptr = responsePtr->payload;
40     ASSERT_EQ(payload_ptr[0], 0);
41     ASSERT_EQ(payload_ptr[1], 60); // 60 = 0b111100
42     ASSERT_EQ(payload_ptr[2], 0);
43 }
44 
45 TEST(GetPLDMCommands, testBadRequest)
46 {
47     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 
51     request->payload[0] = 0xFF;
52     size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
53     base::Handler handler;
54     auto response = handler.getPLDMCommands(request, requestPayloadLength);
55     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);
58 }
59 TEST(GetPLDMVersion, testGoodRequest)
60 {
61     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);
65 
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;
70     ver32_t version = {0xF1, 0xF0, 0xF0, 0x00};
71 
72     auto rc =
73         encode_get_version_req(0, transferHandle, flag, pldmType, request);
74 
75     ASSERT_EQ(0, rc);
76 
77     base::Handler handler;
78     auto response = handler.getPLDMVersion(request, requestPayloadLength);
79     auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
80 
81     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)));
90 }
91 TEST(GetPLDMVersion, testBadRequest)
92 {
93     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);
97 
98     uint8_t pldmType = 7;
99     uint32_t transferHandle = 0x0;
100     uint8_t flag = PLDM_GET_FIRSTPART;
101 
102     auto rc =
103         encode_get_version_req(0, transferHandle, flag, pldmType, request);
104 
105     ASSERT_EQ(0, rc);
106 
107     base::Handler handler;
108     auto response = handler.getPLDMVersion(request, requestPayloadLength - 1);
109     auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
110 
111     ASSERT_EQ(responsePtr->payload[0], PLDM_ERROR_INVALID_LENGTH);
112 
113     request = reinterpret_cast<pldm_msg*>(requestPayload.data());
114     requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
115 
116     rc = encode_get_version_req(0, transferHandle, flag, pldmType, request);
117 
118     ASSERT_EQ(0, rc);
119 
120     response = handler.getPLDMVersion(request, requestPayloadLength);
121     responsePtr = reinterpret_cast<pldm_msg*>(response.data());
122 
123     ASSERT_EQ(responsePtr->payload[0], PLDM_ERROR_INVALID_PLDM_TYPE);
124 }
125 
126 TEST(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 
132     base::Handler handler;
133     auto response = handler.getTID(request, requestPayloadLength);
134 
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 }
141