blob: 88a6eb4bbc2307198180c918cb6b3debef4dddae [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"
3#include "libpldmresponder/platform.hpp"
4
5#include <iostream>
6
Sampa Misraa2fa0702019-05-31 01:28:55 -05007#include <gmock/gmock-matchers.h>
8#include <gmock/gmock.h>
Deepak Kodihalli557dfb02019-05-12 13:11:17 +05309#include <gtest/gtest.h>
10
11using namespace pldm::responder;
Sampa Misraa2fa0702019-05-31 01:28:55 -050012using namespace pldm::responder::pdr;
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053013
14TEST(getPDR, testGoodPath)
15{
16 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
17 requestPayload{};
18 auto request = reinterpret_cast<pldm_msg*>(requestPayload.data());
19 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
20
21 uint8_t* start = request->payload;
22 start += sizeof(uint32_t) + sizeof(uint32_t) + sizeof(uint8_t);
23 uint16_t* reqCount = reinterpret_cast<uint16_t*>(start);
24 *reqCount = 100;
25 using namespace pdr;
26 Repo& pdrRepo = get("./pdr_jsons/state_effecter/good");
27 ASSERT_EQ(pdrRepo.empty(), false);
Deepak Kodihallibc669f12019-11-28 08:52:07 -060028 platform::Handler handler;
29 auto response = handler.getPDR(request, requestPayloadLength);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053030 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
31
32 ASSERT_EQ(responsePtr->payload[0], PLDM_SUCCESS);
33 start = responsePtr->payload;
34 start += sizeof(uint8_t);
35 uint32_t* nextRecordHandle = reinterpret_cast<uint32_t*>(start);
36 ASSERT_EQ(*nextRecordHandle, 2);
37 start += sizeof(uint32_t) + sizeof(uint32_t) + sizeof(uint8_t);
38 uint16_t* recordCount = reinterpret_cast<uint16_t*>(start);
39 ASSERT_EQ(*recordCount != 0, true);
40 start += sizeof(uint16_t);
41 // Check a bit of the PDR common header
42 pldm_state_effecter_pdr* pdr =
43 reinterpret_cast<pldm_state_effecter_pdr*>(start);
44 ASSERT_EQ(pdr->hdr.record_handle, 1);
45 ASSERT_EQ(pdr->hdr.version, 1);
46}
47
48TEST(getPDR, testShortRead)
49{
50 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
51 requestPayload{};
52 auto request = reinterpret_cast<pldm_msg*>(requestPayload.data());
53 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
54
55 uint8_t* start = request->payload;
56 start += sizeof(uint32_t) + sizeof(uint32_t) + sizeof(uint8_t);
57 uint16_t* reqCount = reinterpret_cast<uint16_t*>(start);
58 // Read 1 byte of PDR
59 *reqCount = 1;
60 using namespace pdr;
61 Repo& pdrRepo = get("./pdr_jsons/state_effecter/good");
62 ASSERT_EQ(pdrRepo.empty(), false);
Deepak Kodihallibc669f12019-11-28 08:52:07 -060063 platform::Handler handler;
64 auto response = handler.getPDR(request, requestPayloadLength);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053065 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
66
67 ASSERT_EQ(responsePtr->payload[0], PLDM_SUCCESS);
68 start = responsePtr->payload;
69 start +=
70 sizeof(uint8_t) + sizeof(uint32_t) + sizeof(uint32_t) + sizeof(uint8_t);
71 uint16_t* recordCount = reinterpret_cast<uint16_t*>(start);
72 ASSERT_EQ(*recordCount, 1);
73}
74
75TEST(getPDR, testBadRecordHandle)
76{
77 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
78 requestPayload{};
79 auto request = reinterpret_cast<pldm_msg*>(requestPayload.data());
80 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
81
82 uint8_t* start = request->payload;
83 uint32_t* recordHandle = reinterpret_cast<uint32_t*>(start);
84 *recordHandle = 100000;
85 start += sizeof(uint32_t) + sizeof(uint32_t) + sizeof(uint8_t);
86 uint16_t* reqCount = reinterpret_cast<uint16_t*>(start);
87 *reqCount = 1;
88 using namespace pdr;
89 Repo& pdrRepo = get("./pdr_jsons/state_effecter/good");
90 ASSERT_EQ(pdrRepo.empty(), false);
Deepak Kodihallibc669f12019-11-28 08:52:07 -060091 platform::Handler handler;
92 auto response = handler.getPDR(request, requestPayloadLength);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053093 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
94
95 ASSERT_EQ(responsePtr->payload[0], PLDM_PLATFORM_INVALID_RECORD_HANDLE);
96}
97
98TEST(getPDR, testNoNextRecord)
99{
100 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
101 requestPayload{};
102 auto request = reinterpret_cast<pldm_msg*>(requestPayload.data());
103 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
104
105 uint8_t* start = request->payload;
106 uint32_t* recordHandle = reinterpret_cast<uint32_t*>(start);
107 *recordHandle = 3;
108 using namespace pdr;
109 Repo& pdrRepo = get("./pdr_jsons/state_effecter/good");
110 ASSERT_EQ(pdrRepo.empty(), false);
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600111 platform::Handler handler;
112 auto response = handler.getPDR(request, requestPayloadLength);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530113 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
114
115 ASSERT_EQ(responsePtr->payload[0], PLDM_SUCCESS);
116 start = responsePtr->payload;
117 start += sizeof(uint8_t);
118 uint32_t* nextRecordHandle = reinterpret_cast<uint32_t*>(start);
119 ASSERT_EQ(*nextRecordHandle, 0);
120}
121
122TEST(getPDR, testFindPDR)
123{
124 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
125 requestPayload{};
126 auto request = reinterpret_cast<pldm_msg*>(requestPayload.data());
127 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
128
129 uint8_t* start = request->payload;
130 start += sizeof(uint32_t) + sizeof(uint32_t) + sizeof(uint8_t);
131 uint16_t* reqCount = reinterpret_cast<uint16_t*>(start);
132 *reqCount = 100;
133 using namespace pdr;
134 Repo& pdrRepo = get("./pdr_jsons/state_effecter/good");
135 ASSERT_EQ(pdrRepo.empty(), false);
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600136 platform::Handler handler;
137 auto response = handler.getPDR(request, requestPayloadLength);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530138
139 // Let's try to find a PDR of type stateEffecter (= 11) and entity type =
140 // 100
141 bool found = false;
142 uint32_t handle = 0; // start asking for PDRs from recordHandle 0
143 uint32_t* recordHandle = nullptr;
144 while (!found)
145 {
146 start = request->payload;
147 recordHandle = reinterpret_cast<uint32_t*>(start);
148 *recordHandle = handle;
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600149 platform::Handler handler;
150 auto response = handler.getPDR(request, requestPayloadLength);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530151 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
152
153 ASSERT_EQ(responsePtr->payload[0], PLDM_SUCCESS);
154 start = responsePtr->payload;
155 start += sizeof(uint8_t);
156 uint32_t* nextRecordHandle = reinterpret_cast<uint32_t*>(start);
157 handle = *nextRecordHandle; // point to the next pdr in case current is
158 // not what we want
159 start += sizeof(uint32_t) + sizeof(uint32_t) + sizeof(uint8_t) +
160 sizeof(uint16_t);
161 pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(start);
162 uint32_t intType = hdr->type;
163 std::cerr << "PDR next record handle " << handle << std::endl;
164 std::cerr << "PDR type " << intType << std::endl;
165 if (hdr->type == PLDM_STATE_EFFECTER_PDR)
166 {
167 pldm_state_effecter_pdr* pdr =
168 reinterpret_cast<pldm_state_effecter_pdr*>(start);
169 std::cerr << "PDR entity type " << pdr->entity_type << std::endl;
170 if (pdr->entity_type == 100)
171 {
172 found = true;
173 // Rest of the PDR can be accessed as need be
174 break;
175 }
176 }
177 if (!*nextRecordHandle) // no more records
178 {
179 break;
180 }
181 }
182 ASSERT_EQ(found, true);
183}
Sampa Misraa2fa0702019-05-31 01:28:55 -0500184
185namespace pldm
186{
187
188namespace responder
189{
190
191class MockdBusHandler
192{
193 public:
194 MOCK_CONST_METHOD4(setDbusProperty,
195 int(const std::string&, const std::string&,
196 const std::string&,
197 const std::variant<std::string>&));
198};
199} // namespace responder
200} // namespace pldm
201
202using ::testing::_;
203using ::testing::Return;
204
205TEST(setStateEffecterStatesHandler, testGoodRequest)
206{
207 Repo& pdrRepo = get("./pdr_jsons/state_effecter/good");
208 pdr::Entry e = pdrRepo.at(1);
209 pldm_state_effecter_pdr* pdr =
210 reinterpret_cast<pldm_state_effecter_pdr*>(e.data());
211 EXPECT_EQ(pdr->hdr.type, PLDM_STATE_EFFECTER_PDR);
212
213 std::vector<set_effecter_state_field> stateField;
214 stateField.push_back({PLDM_REQUEST_SET, 1});
215 stateField.push_back({PLDM_REQUEST_SET, 1});
216
217 auto bootProgressInf = "xyz.openbmc_project.State.OperatingSystem.Status";
218 auto bootProgressProp = "OperatingSystemState";
219 std::string objPath = "/foo/bar";
220 std::variant<std::string> value{"xyz.openbmc_project.State.OperatingSystem."
221 "Status.OSStatus.Standby"};
222
223 MockdBusHandler handlerObj;
224 EXPECT_CALL(handlerObj, setDbusProperty(objPath, bootProgressProp,
225 bootProgressInf, value))
226 .Times(2);
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600227 platform::Handler handler;
228 auto rc = handler.setStateEffecterStatesHandler<MockdBusHandler>(
229 handlerObj, 0x1, stateField);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500230 ASSERT_EQ(rc, 0);
231}
232
233TEST(setStateEffecterStatesHandler, testBadRequest)
234{
235 Repo& pdrRepo = get("./pdr_jsons/state_effecter/good");
236 pdr::Entry e = pdrRepo.at(1);
237 pldm_state_effecter_pdr* pdr =
238 reinterpret_cast<pldm_state_effecter_pdr*>(e.data());
239 EXPECT_EQ(pdr->hdr.type, PLDM_STATE_EFFECTER_PDR);
240
241 std::vector<set_effecter_state_field> stateField;
242 stateField.push_back({PLDM_REQUEST_SET, 3});
243 stateField.push_back({PLDM_REQUEST_SET, 4});
244
245 MockdBusHandler handlerObj;
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600246 platform::Handler handler;
247 auto rc = handler.setStateEffecterStatesHandler<MockdBusHandler>(
248 handlerObj, 0x1, stateField);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500249 ASSERT_EQ(rc, PLDM_PLATFORM_SET_EFFECTER_UNSUPPORTED_SENSORSTATE);
250
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600251 rc = handler.setStateEffecterStatesHandler<MockdBusHandler>(handlerObj, 0x9,
252 stateField);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500253 ASSERT_EQ(rc, PLDM_PLATFORM_INVALID_EFFECTER_ID);
254
255 stateField.push_back({PLDM_REQUEST_SET, 4});
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600256 rc = handler.setStateEffecterStatesHandler<MockdBusHandler>(handlerObj, 0x1,
257 stateField);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500258 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
259
260 std::vector<set_effecter_state_field> newStateField;
261 newStateField.push_back({PLDM_REQUEST_SET, 1});
262
Deepak Kodihallibc669f12019-11-28 08:52:07 -0600263 rc = handler.setStateEffecterStatesHandler<MockdBusHandler>(handlerObj, 0x2,
264 newStateField);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500265 ASSERT_EQ(rc, PLDM_PLATFORM_INVALID_STATE_VALUE);
266}