blob: 825292037288493ea42a21ae9e3be64962503384 [file] [log] [blame]
Sampa Misraa2fa0702019-05-31 01:28:55 -05001#include "libpldmresponder/effecters.hpp"
Deepak Kodihalli557dfb02019-05-12 13:11:17 +05302#include "libpldmresponder/pdr.hpp"
George Liue53193f2020-02-24 09:23:26 +08003#include "libpldmresponder/pdr_utils.hpp"
Deepak Kodihalli557dfb02019-05-12 13:11:17 +05304#include "libpldmresponder/platform.hpp"
5
6#include <iostream>
7
Sampa Misraa2fa0702019-05-31 01:28:55 -05008#include <gmock/gmock-matchers.h>
9#include <gmock/gmock.h>
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053010#include <gtest/gtest.h>
11
12using namespace pldm::responder;
13
14TEST(getPDR, testGoodPath)
15{
16 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
17 requestPayload{};
George Liue53193f2020-02-24 09:23:26 +080018 auto req = reinterpret_cast<pldm_msg*>(requestPayload.data());
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053019 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
20
George Liue53193f2020-02-24 09:23:26 +080021 struct pldm_get_pdr_req* request =
22 reinterpret_cast<struct pldm_get_pdr_req*>(req->payload);
23 request->request_count = 100;
24
25 pdr_utils::RepoInterface& pdrRepo =
26 pdr::getRepo("./pdr_jsons/state_effecter/good");
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053027 ASSERT_EQ(pdrRepo.empty(), false);
Deepak Kodihallibc669f12019-11-28 08:52:07 -060028 platform::Handler handler;
George Liue53193f2020-02-24 09:23:26 +080029 auto response = handler.getPDR(req, requestPayloadLength);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053030 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
31
George Liue53193f2020-02-24 09:23:26 +080032 struct pldm_get_pdr_resp* resp =
33 reinterpret_cast<struct pldm_get_pdr_resp*>(responsePtr->payload);
34 ASSERT_EQ(PLDM_SUCCESS, resp->completion_code);
35 ASSERT_EQ(2, resp->next_record_handle);
36 ASSERT_EQ(true, resp->response_count != 0);
37
38 pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(resp->record_data);
39 ASSERT_EQ(hdr->record_handle, 1);
40 ASSERT_EQ(hdr->version, 1);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053041}
42
43TEST(getPDR, testShortRead)
44{
45 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
46 requestPayload{};
George Liue53193f2020-02-24 09:23:26 +080047 auto req = reinterpret_cast<pldm_msg*>(requestPayload.data());
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053048 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
49
George Liue53193f2020-02-24 09:23:26 +080050 struct pldm_get_pdr_req* request =
51 reinterpret_cast<struct pldm_get_pdr_req*>(req->payload);
52 request->request_count = 1;
53
54 pdr_utils::RepoInterface& pdrRepo =
55 pdr::getRepo("./pdr_jsons/state_effecter/good");
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053056 ASSERT_EQ(pdrRepo.empty(), false);
Deepak Kodihallibc669f12019-11-28 08:52:07 -060057 platform::Handler handler;
George Liue53193f2020-02-24 09:23:26 +080058 auto response = handler.getPDR(req, requestPayloadLength);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053059 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
George Liue53193f2020-02-24 09:23:26 +080060 struct pldm_get_pdr_resp* resp =
61 reinterpret_cast<struct pldm_get_pdr_resp*>(responsePtr->payload);
62 ASSERT_EQ(PLDM_SUCCESS, resp->completion_code);
63 ASSERT_EQ(1, resp->response_count);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053064}
65
66TEST(getPDR, testBadRecordHandle)
67{
68 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
69 requestPayload{};
George Liue53193f2020-02-24 09:23:26 +080070 auto req = reinterpret_cast<pldm_msg*>(requestPayload.data());
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053071 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
72
George Liue53193f2020-02-24 09:23:26 +080073 struct pldm_get_pdr_req* request =
74 reinterpret_cast<struct pldm_get_pdr_req*>(req->payload);
75 request->record_handle = 100000;
76 request->request_count = 1;
77
78 pdr_utils::RepoInterface& pdrRepo =
79 pdr::getRepo("./pdr_jsons/state_effecter/good");
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053080 ASSERT_EQ(pdrRepo.empty(), false);
Deepak Kodihallibc669f12019-11-28 08:52:07 -060081 platform::Handler handler;
George Liue53193f2020-02-24 09:23:26 +080082 auto response = handler.getPDR(req, requestPayloadLength);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053083 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
84
85 ASSERT_EQ(responsePtr->payload[0], PLDM_PLATFORM_INVALID_RECORD_HANDLE);
86}
87
88TEST(getPDR, testNoNextRecord)
89{
90 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
91 requestPayload{};
George Liue53193f2020-02-24 09:23:26 +080092 auto req = reinterpret_cast<pldm_msg*>(requestPayload.data());
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053093 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
94
George Liue53193f2020-02-24 09:23:26 +080095 struct pldm_get_pdr_req* request =
96 reinterpret_cast<struct pldm_get_pdr_req*>(req->payload);
97 request->record_handle = 1;
98
99 pdr_utils::RepoInterface& pdrRepo =
100 pdr::getRepo("./pdr_jsons/state_effecter/good");
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530101 ASSERT_EQ(pdrRepo.empty(), false);
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600102 platform::Handler handler;
George Liue53193f2020-02-24 09:23:26 +0800103 auto response = handler.getPDR(req, requestPayloadLength);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530104 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
George Liue53193f2020-02-24 09:23:26 +0800105 struct pldm_get_pdr_resp* resp =
106 reinterpret_cast<struct pldm_get_pdr_resp*>(responsePtr->payload);
107 ASSERT_EQ(PLDM_SUCCESS, resp->completion_code);
108 ASSERT_EQ(2, resp->next_record_handle);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530109}
110
111TEST(getPDR, testFindPDR)
112{
113 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
114 requestPayload{};
George Liue53193f2020-02-24 09:23:26 +0800115 auto req = reinterpret_cast<pldm_msg*>(requestPayload.data());
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530116 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
117
George Liue53193f2020-02-24 09:23:26 +0800118 struct pldm_get_pdr_req* request =
119 reinterpret_cast<struct pldm_get_pdr_req*>(req->payload);
120 request->request_count = 100;
121
122 pdr_utils::RepoInterface& pdrRepo =
123 pdr::getRepo("./pdr_jsons/state_effecter/good");
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530124 ASSERT_EQ(pdrRepo.empty(), false);
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600125 platform::Handler handler;
George Liue53193f2020-02-24 09:23:26 +0800126 auto response = handler.getPDR(req, requestPayloadLength);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530127
128 // Let's try to find a PDR of type stateEffecter (= 11) and entity type =
129 // 100
130 bool found = false;
131 uint32_t handle = 0; // start asking for PDRs from recordHandle 0
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530132 while (!found)
133 {
George Liue53193f2020-02-24 09:23:26 +0800134 request->record_handle = handle;
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600135 platform::Handler handler;
George Liue53193f2020-02-24 09:23:26 +0800136 auto response = handler.getPDR(req, requestPayloadLength);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530137 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
George Liue53193f2020-02-24 09:23:26 +0800138 struct pldm_get_pdr_resp* resp =
139 reinterpret_cast<struct pldm_get_pdr_resp*>(responsePtr->payload);
140 ASSERT_EQ(PLDM_SUCCESS, resp->completion_code);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530141
George Liue53193f2020-02-24 09:23:26 +0800142 handle = resp->next_record_handle; // point to the next pdr in case
143 // current is not what we want
144
145 pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(resp->record_data);
Sampa Misraaa8ae722019-12-12 03:20:40 -0600146 std::cerr << "PDR next record handle " << handle << "\n";
George Liue53193f2020-02-24 09:23:26 +0800147 std::cerr << "PDR type " << hdr->type << "\n";
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530148 if (hdr->type == PLDM_STATE_EFFECTER_PDR)
149 {
150 pldm_state_effecter_pdr* pdr =
George Liue53193f2020-02-24 09:23:26 +0800151 reinterpret_cast<pldm_state_effecter_pdr*>(resp->record_data);
Sampa Misraaa8ae722019-12-12 03:20:40 -0600152 std::cerr << "PDR entity type " << pdr->entity_type << "\n";
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530153 if (pdr->entity_type == 100)
154 {
155 found = true;
156 // Rest of the PDR can be accessed as need be
157 break;
158 }
159 }
George Liue53193f2020-02-24 09:23:26 +0800160 if (!resp->next_record_handle) // no more records
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530161 {
162 break;
163 }
164 }
165 ASSERT_EQ(found, true);
166}
Sampa Misraa2fa0702019-05-31 01:28:55 -0500167
168namespace pldm
169{
170
171namespace responder
172{
173
174class MockdBusHandler
175{
176 public:
177 MOCK_CONST_METHOD4(setDbusProperty,
178 int(const std::string&, const std::string&,
179 const std::string&,
180 const std::variant<std::string>&));
181};
182} // namespace responder
183} // namespace pldm
184
185using ::testing::_;
186using ::testing::Return;
187
188TEST(setStateEffecterStatesHandler, testGoodRequest)
189{
George Liue53193f2020-02-24 09:23:26 +0800190 pdr_utils::Repo pdrRepo = pdr::getRepoByType(
191 "./pdr_jsons/state_effecter/good", PLDM_STATE_EFFECTER_PDR);
192 pdr_utils::PdrEntry e;
193 auto record1 = pdr::getRecordByHandle(pdrRepo, 1, e);
194 ASSERT_NE(record1, nullptr);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500195 pldm_state_effecter_pdr* pdr =
George Liue53193f2020-02-24 09:23:26 +0800196 reinterpret_cast<pldm_state_effecter_pdr*>(e.data);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500197 EXPECT_EQ(pdr->hdr.type, PLDM_STATE_EFFECTER_PDR);
198
199 std::vector<set_effecter_state_field> stateField;
200 stateField.push_back({PLDM_REQUEST_SET, 1});
201 stateField.push_back({PLDM_REQUEST_SET, 1});
202
203 auto bootProgressInf = "xyz.openbmc_project.State.OperatingSystem.Status";
204 auto bootProgressProp = "OperatingSystemState";
205 std::string objPath = "/foo/bar";
206 std::variant<std::string> value{"xyz.openbmc_project.State.OperatingSystem."
207 "Status.OSStatus.Standby"};
208
209 MockdBusHandler handlerObj;
210 EXPECT_CALL(handlerObj, setDbusProperty(objPath, bootProgressProp,
211 bootProgressInf, value))
212 .Times(2);
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600213 platform::Handler handler;
214 auto rc = handler.setStateEffecterStatesHandler<MockdBusHandler>(
215 handlerObj, 0x1, stateField);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500216 ASSERT_EQ(rc, 0);
217}
218
219TEST(setStateEffecterStatesHandler, testBadRequest)
220{
George Liue53193f2020-02-24 09:23:26 +0800221 pdr_utils::Repo pdrRepo = pdr::getRepoByType(
222 "./pdr_jsons/state_effecter/good", PLDM_STATE_EFFECTER_PDR);
223 pdr_utils::PdrEntry e;
224 auto record1 = pdr::getRecordByHandle(pdrRepo, 1, e);
225 ASSERT_NE(record1, nullptr);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500226 pldm_state_effecter_pdr* pdr =
George Liue53193f2020-02-24 09:23:26 +0800227 reinterpret_cast<pldm_state_effecter_pdr*>(e.data);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500228 EXPECT_EQ(pdr->hdr.type, PLDM_STATE_EFFECTER_PDR);
229
230 std::vector<set_effecter_state_field> stateField;
231 stateField.push_back({PLDM_REQUEST_SET, 3});
232 stateField.push_back({PLDM_REQUEST_SET, 4});
233
234 MockdBusHandler handlerObj;
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600235 platform::Handler handler;
236 auto rc = handler.setStateEffecterStatesHandler<MockdBusHandler>(
237 handlerObj, 0x1, stateField);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500238 ASSERT_EQ(rc, PLDM_PLATFORM_SET_EFFECTER_UNSUPPORTED_SENSORSTATE);
239
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600240 rc = handler.setStateEffecterStatesHandler<MockdBusHandler>(handlerObj, 0x9,
241 stateField);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500242 ASSERT_EQ(rc, PLDM_PLATFORM_INVALID_EFFECTER_ID);
243
244 stateField.push_back({PLDM_REQUEST_SET, 4});
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600245 rc = handler.setStateEffecterStatesHandler<MockdBusHandler>(handlerObj, 0x1,
246 stateField);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500247 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
248
249 std::vector<set_effecter_state_field> newStateField;
250 newStateField.push_back({PLDM_REQUEST_SET, 1});
251
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600252 rc = handler.setStateEffecterStatesHandler<MockdBusHandler>(handlerObj, 0x2,
253 newStateField);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500254 ASSERT_EQ(rc, PLDM_PLATFORM_INVALID_STATE_VALUE);
255}