blob: a646f9d4ae7e7e1a279e06c94a6e360eef37ed32 [file] [log] [blame]
Tom Joseph53279882021-04-28 06:29:13 -07001#include "common/test/mocked_utils.hpp"
Deepak Kodihallid130e1a2020-06-17 05:55:32 -05002#include "common/utils.hpp"
TOM JOSEPHd4d97a52020-03-23 14:36:34 +05303#include "libpldmresponder/event_parser.hpp"
Deepak Kodihalli557dfb02019-05-12 13:11:17 +05304#include "libpldmresponder/pdr.hpp"
George Liue53193f2020-02-24 09:23:26 +08005#include "libpldmresponder/pdr_utils.hpp"
Deepak Kodihalli557dfb02019-05-12 13:11:17 +05306#include "libpldmresponder/platform.hpp"
George Liu64d26be2020-01-15 10:58:10 +08007#include "libpldmresponder/platform_numeric_effecter.hpp"
George Liu0d7aca82020-03-30 15:01:36 +08008#include "libpldmresponder/platform_state_effecter.hpp"
George Liua9170122020-05-14 11:59:13 +08009#include "libpldmresponder/platform_state_sensor.hpp"
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053010
George Liu36e81352020-07-01 14:40:30 +080011#include <sdbusplus/test/sdbus_mock.hpp>
Sampa Misra5fb37d52021-03-06 07:26:00 -060012#include <sdeventplus/event.hpp>
George Liu36e81352020-07-01 14:40:30 +080013
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053014#include <iostream>
15
Brad Bishop5079ac42021-08-19 18:35:06 -040016using namespace pldm::pdr;
George Liu1e44c732020-02-28 20:20:06 +080017using namespace pldm::utils;
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053018using namespace pldm::responder;
Deepak Kodihallic682fe22020-03-04 00:42:54 -060019using namespace pldm::responder::platform;
20using namespace pldm::responder::pdr;
21using namespace pldm::responder::pdr_utils;
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053022
George Liu36e81352020-07-01 14:40:30 +080023using ::testing::_;
24using ::testing::Return;
25using ::testing::StrEq;
26
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053027TEST(getPDR, testGoodPath)
28{
29 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
30 requestPayload{};
George Liue53193f2020-02-24 09:23:26 +080031 auto req = reinterpret_cast<pldm_msg*>(requestPayload.data());
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053032 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
33
George Liue53193f2020-02-24 09:23:26 +080034 struct pldm_get_pdr_req* request =
35 reinterpret_cast<struct pldm_get_pdr_req*>(req->payload);
36 request->request_count = 100;
37
George Liu36e81352020-07-01 14:40:30 +080038 MockdBusHandler mockedUtils;
39 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
40 .Times(5)
41 .WillRepeatedly(Return("foo.bar"));
42
Deepak Kodihallic682fe22020-03-04 00:42:54 -060043 auto pdrRepo = pldm_pdr_init();
Sampa Misra5fb37d52021-03-06 07:26:00 -060044 auto event = sdeventplus::Event::get_default();
Pavithra Barithaya3aec9972020-12-14 01:55:44 -060045 Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good", pdrRepo,
Sampa Misra5fb37d52021-03-06 07:26:00 -060046 nullptr, nullptr, nullptr, nullptr, event);
Deepak Kodihallic682fe22020-03-04 00:42:54 -060047 Repo repo(pdrRepo);
48 ASSERT_EQ(repo.empty(), false);
George Liue53193f2020-02-24 09:23:26 +080049 auto response = handler.getPDR(req, requestPayloadLength);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053050 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
51
George Liue53193f2020-02-24 09:23:26 +080052 struct pldm_get_pdr_resp* resp =
53 reinterpret_cast<struct pldm_get_pdr_resp*>(responsePtr->payload);
54 ASSERT_EQ(PLDM_SUCCESS, resp->completion_code);
55 ASSERT_EQ(2, resp->next_record_handle);
56 ASSERT_EQ(true, resp->response_count != 0);
57
58 pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(resp->record_data);
59 ASSERT_EQ(hdr->record_handle, 1);
60 ASSERT_EQ(hdr->version, 1);
Deepak Kodihallic682fe22020-03-04 00:42:54 -060061
62 pldm_pdr_destroy(pdrRepo);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053063}
64
65TEST(getPDR, testShortRead)
66{
67 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
68 requestPayload{};
George Liue53193f2020-02-24 09:23:26 +080069 auto req = reinterpret_cast<pldm_msg*>(requestPayload.data());
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053070 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
71
George Liue53193f2020-02-24 09:23:26 +080072 struct pldm_get_pdr_req* request =
73 reinterpret_cast<struct pldm_get_pdr_req*>(req->payload);
74 request->request_count = 1;
75
George Liu36e81352020-07-01 14:40:30 +080076 MockdBusHandler mockedUtils;
77 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
78 .Times(5)
79 .WillRepeatedly(Return("foo.bar"));
80
Deepak Kodihallic682fe22020-03-04 00:42:54 -060081 auto pdrRepo = pldm_pdr_init();
Sampa Misra5fb37d52021-03-06 07:26:00 -060082 auto event = sdeventplus::Event::get_default();
Pavithra Barithaya3aec9972020-12-14 01:55:44 -060083 Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good", pdrRepo,
Sampa Misra5fb37d52021-03-06 07:26:00 -060084 nullptr, nullptr, nullptr, nullptr, event);
Deepak Kodihallic682fe22020-03-04 00:42:54 -060085 Repo repo(pdrRepo);
86 ASSERT_EQ(repo.empty(), false);
George Liue53193f2020-02-24 09:23:26 +080087 auto response = handler.getPDR(req, requestPayloadLength);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053088 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
George Liue53193f2020-02-24 09:23:26 +080089 struct pldm_get_pdr_resp* resp =
90 reinterpret_cast<struct pldm_get_pdr_resp*>(responsePtr->payload);
91 ASSERT_EQ(PLDM_SUCCESS, resp->completion_code);
92 ASSERT_EQ(1, resp->response_count);
Deepak Kodihallic682fe22020-03-04 00:42:54 -060093 pldm_pdr_destroy(pdrRepo);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053094}
95
96TEST(getPDR, testBadRecordHandle)
97{
98 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
99 requestPayload{};
George Liue53193f2020-02-24 09:23:26 +0800100 auto req = reinterpret_cast<pldm_msg*>(requestPayload.data());
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530101 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
102
George Liue53193f2020-02-24 09:23:26 +0800103 struct pldm_get_pdr_req* request =
104 reinterpret_cast<struct pldm_get_pdr_req*>(req->payload);
105 request->record_handle = 100000;
106 request->request_count = 1;
107
George Liu36e81352020-07-01 14:40:30 +0800108 MockdBusHandler mockedUtils;
109 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
110 .Times(5)
111 .WillRepeatedly(Return("foo.bar"));
112
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600113 auto pdrRepo = pldm_pdr_init();
Sampa Misra5fb37d52021-03-06 07:26:00 -0600114 auto event = sdeventplus::Event::get_default();
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600115 Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good", pdrRepo,
Sampa Misra5fb37d52021-03-06 07:26:00 -0600116 nullptr, nullptr, nullptr, nullptr, event);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600117 Repo repo(pdrRepo);
118 ASSERT_EQ(repo.empty(), false);
George Liue53193f2020-02-24 09:23:26 +0800119 auto response = handler.getPDR(req, requestPayloadLength);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530120 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
121
122 ASSERT_EQ(responsePtr->payload[0], PLDM_PLATFORM_INVALID_RECORD_HANDLE);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600123
124 pldm_pdr_destroy(pdrRepo);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530125}
126
127TEST(getPDR, testNoNextRecord)
128{
129 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
130 requestPayload{};
George Liue53193f2020-02-24 09:23:26 +0800131 auto req = reinterpret_cast<pldm_msg*>(requestPayload.data());
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530132 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
133
George Liue53193f2020-02-24 09:23:26 +0800134 struct pldm_get_pdr_req* request =
135 reinterpret_cast<struct pldm_get_pdr_req*>(req->payload);
136 request->record_handle = 1;
137
George Liu36e81352020-07-01 14:40:30 +0800138 MockdBusHandler mockedUtils;
139 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
140 .Times(5)
141 .WillRepeatedly(Return("foo.bar"));
142
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600143 auto pdrRepo = pldm_pdr_init();
Sampa Misra5fb37d52021-03-06 07:26:00 -0600144 auto event = sdeventplus::Event::get_default();
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600145 Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good", pdrRepo,
Sampa Misra5fb37d52021-03-06 07:26:00 -0600146 nullptr, nullptr, nullptr, nullptr, event);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600147 Repo repo(pdrRepo);
148 ASSERT_EQ(repo.empty(), false);
George Liue53193f2020-02-24 09:23:26 +0800149 auto response = handler.getPDR(req, requestPayloadLength);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530150 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
George Liue53193f2020-02-24 09:23:26 +0800151 struct pldm_get_pdr_resp* resp =
152 reinterpret_cast<struct pldm_get_pdr_resp*>(responsePtr->payload);
153 ASSERT_EQ(PLDM_SUCCESS, resp->completion_code);
154 ASSERT_EQ(2, resp->next_record_handle);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600155
156 pldm_pdr_destroy(pdrRepo);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530157}
158
159TEST(getPDR, testFindPDR)
160{
161 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
162 requestPayload{};
George Liue53193f2020-02-24 09:23:26 +0800163 auto req = reinterpret_cast<pldm_msg*>(requestPayload.data());
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530164 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
165
George Liue53193f2020-02-24 09:23:26 +0800166 struct pldm_get_pdr_req* request =
167 reinterpret_cast<struct pldm_get_pdr_req*>(req->payload);
168 request->request_count = 100;
169
George Liu36e81352020-07-01 14:40:30 +0800170 MockdBusHandler mockedUtils;
171 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
172 .Times(5)
173 .WillRepeatedly(Return("foo.bar"));
174
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600175 auto pdrRepo = pldm_pdr_init();
Sampa Misra5fb37d52021-03-06 07:26:00 -0600176 auto event = sdeventplus::Event::get_default();
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600177 Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good", pdrRepo,
Sampa Misra5fb37d52021-03-06 07:26:00 -0600178 nullptr, nullptr, nullptr, nullptr, event);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600179 Repo repo(pdrRepo);
180 ASSERT_EQ(repo.empty(), false);
George Liue53193f2020-02-24 09:23:26 +0800181 auto response = handler.getPDR(req, requestPayloadLength);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530182
183 // Let's try to find a PDR of type stateEffecter (= 11) and entity type =
184 // 100
185 bool found = false;
186 uint32_t handle = 0; // start asking for PDRs from recordHandle 0
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530187 while (!found)
188 {
George Liue53193f2020-02-24 09:23:26 +0800189 request->record_handle = handle;
George Liue53193f2020-02-24 09:23:26 +0800190 auto response = handler.getPDR(req, requestPayloadLength);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530191 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
George Liue53193f2020-02-24 09:23:26 +0800192 struct pldm_get_pdr_resp* resp =
193 reinterpret_cast<struct pldm_get_pdr_resp*>(responsePtr->payload);
194 ASSERT_EQ(PLDM_SUCCESS, resp->completion_code);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530195
George Liue53193f2020-02-24 09:23:26 +0800196 handle = resp->next_record_handle; // point to the next pdr in case
197 // current is not what we want
198
199 pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(resp->record_data);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530200 if (hdr->type == PLDM_STATE_EFFECTER_PDR)
201 {
202 pldm_state_effecter_pdr* pdr =
George Liue53193f2020-02-24 09:23:26 +0800203 reinterpret_cast<pldm_state_effecter_pdr*>(resp->record_data);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530204 if (pdr->entity_type == 100)
205 {
206 found = true;
207 // Rest of the PDR can be accessed as need be
208 break;
209 }
210 }
George Liue53193f2020-02-24 09:23:26 +0800211 if (!resp->next_record_handle) // no more records
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530212 {
213 break;
214 }
215 }
216 ASSERT_EQ(found, true);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600217
218 pldm_pdr_destroy(pdrRepo);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530219}
Sampa Misraa2fa0702019-05-31 01:28:55 -0500220
Sampa Misraa2fa0702019-05-31 01:28:55 -0500221TEST(setStateEffecterStatesHandler, testGoodRequest)
222{
George Liua9170122020-05-14 11:59:13 +0800223 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
224 requestPayload{};
225 auto req = reinterpret_cast<pldm_msg*>(requestPayload.data());
226 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
227
George Liu36e81352020-07-01 14:40:30 +0800228 MockdBusHandler mockedUtils;
229 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
230 .Times(5)
231 .WillRepeatedly(Return("foo.bar"));
232
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600233 auto inPDRRepo = pldm_pdr_init();
234 auto outPDRRepo = pldm_pdr_init();
235 Repo outRepo(outPDRRepo);
Sampa Misra5fb37d52021-03-06 07:26:00 -0600236 auto event = sdeventplus::Event::get_default();
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600237 Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good", inPDRRepo,
Sampa Misra5fb37d52021-03-06 07:26:00 -0600238 nullptr, nullptr, nullptr, nullptr, event);
George Liua9170122020-05-14 11:59:13 +0800239 handler.getPDR(req, requestPayloadLength);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600240 Repo inRepo(inPDRRepo);
241 getRepoByType(inRepo, outRepo, PLDM_STATE_EFFECTER_PDR);
George Liue53193f2020-02-24 09:23:26 +0800242 pdr_utils::PdrEntry e;
Sampa Misra12afe112020-05-25 11:40:44 -0500243 auto record1 = pdr::getRecordByHandle(outRepo, 2, e);
George Liue53193f2020-02-24 09:23:26 +0800244 ASSERT_NE(record1, nullptr);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500245 pldm_state_effecter_pdr* pdr =
George Liue53193f2020-02-24 09:23:26 +0800246 reinterpret_cast<pldm_state_effecter_pdr*>(e.data);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500247 EXPECT_EQ(pdr->hdr.type, PLDM_STATE_EFFECTER_PDR);
248
249 std::vector<set_effecter_state_field> stateField;
250 stateField.push_back({PLDM_REQUEST_SET, 1});
251 stateField.push_back({PLDM_REQUEST_SET, 1});
George Liu1ec85d42020-02-12 16:05:32 +0800252 std::string value = "xyz.openbmc_project.Foo.Bar.V1";
George Liu1e44c732020-02-28 20:20:06 +0800253 PropertyValue propertyValue = value;
Sampa Misraa2fa0702019-05-31 01:28:55 -0500254
George Liu1ec85d42020-02-12 16:05:32 +0800255 DBusMapping dbusMapping{"/foo/bar", "xyz.openbmc_project.Foo.Bar",
256 "propertyName", "string"};
George Liu1e44c732020-02-28 20:20:06 +0800257
George Liu36e81352020-07-01 14:40:30 +0800258 EXPECT_CALL(mockedUtils, setDbusProperty(dbusMapping, propertyValue))
Sampa Misraa2fa0702019-05-31 01:28:55 -0500259 .Times(2);
George Liu0d7aca82020-03-30 15:01:36 +0800260 auto rc = platform_state_effecter::setStateEffecterStatesHandler<
George Liu36e81352020-07-01 14:40:30 +0800261 MockdBusHandler, Handler>(mockedUtils, handler, 0x1, stateField);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500262 ASSERT_EQ(rc, 0);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600263
264 pldm_pdr_destroy(inPDRRepo);
265 pldm_pdr_destroy(outPDRRepo);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500266}
267
268TEST(setStateEffecterStatesHandler, testBadRequest)
269{
George Liua9170122020-05-14 11:59:13 +0800270 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
271 requestPayload{};
272 auto req = reinterpret_cast<pldm_msg*>(requestPayload.data());
273 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
274
George Liu36e81352020-07-01 14:40:30 +0800275 MockdBusHandler mockedUtils;
276 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
277 .Times(5)
278 .WillRepeatedly(Return("foo.bar"));
279
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600280 auto inPDRRepo = pldm_pdr_init();
281 auto outPDRRepo = pldm_pdr_init();
282 Repo outRepo(outPDRRepo);
Sampa Misra5fb37d52021-03-06 07:26:00 -0600283 auto event = sdeventplus::Event::get_default();
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600284 Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good", inPDRRepo,
Sampa Misra5fb37d52021-03-06 07:26:00 -0600285 nullptr, nullptr, nullptr, nullptr, event);
George Liua9170122020-05-14 11:59:13 +0800286 handler.getPDR(req, requestPayloadLength);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600287 Repo inRepo(inPDRRepo);
288 getRepoByType(inRepo, outRepo, PLDM_STATE_EFFECTER_PDR);
George Liue53193f2020-02-24 09:23:26 +0800289 pdr_utils::PdrEntry e;
Sampa Misra12afe112020-05-25 11:40:44 -0500290 auto record1 = pdr::getRecordByHandle(outRepo, 2, e);
George Liue53193f2020-02-24 09:23:26 +0800291 ASSERT_NE(record1, nullptr);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500292 pldm_state_effecter_pdr* pdr =
George Liue53193f2020-02-24 09:23:26 +0800293 reinterpret_cast<pldm_state_effecter_pdr*>(e.data);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500294 EXPECT_EQ(pdr->hdr.type, PLDM_STATE_EFFECTER_PDR);
295
296 std::vector<set_effecter_state_field> stateField;
297 stateField.push_back({PLDM_REQUEST_SET, 3});
298 stateField.push_back({PLDM_REQUEST_SET, 4});
299
George Liu0d7aca82020-03-30 15:01:36 +0800300 auto rc = platform_state_effecter::setStateEffecterStatesHandler<
George Liu36e81352020-07-01 14:40:30 +0800301 MockdBusHandler, Handler>(mockedUtils, handler, 0x1, stateField);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500302 ASSERT_EQ(rc, PLDM_PLATFORM_SET_EFFECTER_UNSUPPORTED_SENSORSTATE);
303
George Liu0d7aca82020-03-30 15:01:36 +0800304 rc = platform_state_effecter::setStateEffecterStatesHandler<MockdBusHandler,
305 Handler>(
George Liu36e81352020-07-01 14:40:30 +0800306 mockedUtils, handler, 0x9, stateField);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500307 ASSERT_EQ(rc, PLDM_PLATFORM_INVALID_EFFECTER_ID);
308
309 stateField.push_back({PLDM_REQUEST_SET, 4});
George Liu0d7aca82020-03-30 15:01:36 +0800310 rc = platform_state_effecter::setStateEffecterStatesHandler<MockdBusHandler,
311 Handler>(
George Liu36e81352020-07-01 14:40:30 +0800312 mockedUtils, handler, 0x1, stateField);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500313 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
314
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600315 pldm_pdr_destroy(inPDRRepo);
316 pldm_pdr_destroy(outPDRRepo);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500317}
George Liu64d26be2020-01-15 10:58:10 +0800318
319TEST(setNumericEffecterValueHandler, testGoodRequest)
320{
George Liu36e81352020-07-01 14:40:30 +0800321 MockdBusHandler mockedUtils;
322 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
323 .Times(5)
324 .WillRepeatedly(Return("foo.bar"));
325
George Liu64d26be2020-01-15 10:58:10 +0800326 auto inPDRRepo = pldm_pdr_init();
327 auto numericEffecterPdrRepo = pldm_pdr_init();
328 Repo numericEffecterPDRs(numericEffecterPdrRepo);
Sampa Misra5fb37d52021-03-06 07:26:00 -0600329 auto event = sdeventplus::Event::get_default();
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600330 Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good", inPDRRepo,
Sampa Misra5fb37d52021-03-06 07:26:00 -0600331 nullptr, nullptr, nullptr, nullptr, event);
George Liu64d26be2020-01-15 10:58:10 +0800332 Repo inRepo(inPDRRepo);
333 getRepoByType(inRepo, numericEffecterPDRs, PLDM_NUMERIC_EFFECTER_PDR);
334
335 pdr_utils::PdrEntry e;
Sampa Misra12afe112020-05-25 11:40:44 -0500336 auto record4 = pdr::getRecordByHandle(numericEffecterPDRs, 4, e);
337 ASSERT_NE(record4, nullptr);
George Liu64d26be2020-01-15 10:58:10 +0800338
339 pldm_numeric_effecter_value_pdr* pdr =
340 reinterpret_cast<pldm_numeric_effecter_value_pdr*>(e.data);
341 EXPECT_EQ(pdr->hdr.type, PLDM_NUMERIC_EFFECTER_PDR);
342
343 uint16_t effecterId = 3;
344 uint32_t effecterValue = 2100000000; // 2036-07-18 21:20:00
Pavithra Barithaya45cd16b2021-07-01 08:19:59 -0500345 PropertyValue propertyValue = static_cast<uint64_t>(effecterValue);
George Liu64d26be2020-01-15 10:58:10 +0800346
George Liu64d26be2020-01-15 10:58:10 +0800347 DBusMapping dbusMapping{"/foo/bar", "xyz.openbmc_project.Foo.Bar",
348 "propertyName", "uint64_t"};
George Liu36e81352020-07-01 14:40:30 +0800349 EXPECT_CALL(mockedUtils, setDbusProperty(dbusMapping, propertyValue))
George Liu64d26be2020-01-15 10:58:10 +0800350 .Times(1);
351
352 auto rc = platform_numeric_effecter::setNumericEffecterValueHandler<
353 MockdBusHandler, Handler>(
George Liu36e81352020-07-01 14:40:30 +0800354 mockedUtils, handler, effecterId, PLDM_EFFECTER_DATA_SIZE_UINT32,
George Liu64d26be2020-01-15 10:58:10 +0800355 reinterpret_cast<uint8_t*>(&effecterValue), 4);
356 ASSERT_EQ(rc, 0);
357
358 pldm_pdr_destroy(inPDRRepo);
359 pldm_pdr_destroy(numericEffecterPdrRepo);
360}
361
362TEST(setNumericEffecterValueHandler, testBadRequest)
363{
George Liu36e81352020-07-01 14:40:30 +0800364 MockdBusHandler mockedUtils;
365 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
366 .Times(5)
367 .WillRepeatedly(Return("foo.bar"));
368
George Liu64d26be2020-01-15 10:58:10 +0800369 auto inPDRRepo = pldm_pdr_init();
370 auto numericEffecterPdrRepo = pldm_pdr_init();
371 Repo numericEffecterPDRs(numericEffecterPdrRepo);
Sampa Misra5fb37d52021-03-06 07:26:00 -0600372 auto event = sdeventplus::Event::get_default();
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600373 Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good", inPDRRepo,
Sampa Misra5fb37d52021-03-06 07:26:00 -0600374 nullptr, nullptr, nullptr, nullptr, event);
George Liu64d26be2020-01-15 10:58:10 +0800375 Repo inRepo(inPDRRepo);
376 getRepoByType(inRepo, numericEffecterPDRs, PLDM_NUMERIC_EFFECTER_PDR);
377
378 pdr_utils::PdrEntry e;
Sampa Misra12afe112020-05-25 11:40:44 -0500379 auto record4 = pdr::getRecordByHandle(numericEffecterPDRs, 4, e);
380 ASSERT_NE(record4, nullptr);
George Liu64d26be2020-01-15 10:58:10 +0800381
382 pldm_numeric_effecter_value_pdr* pdr =
383 reinterpret_cast<pldm_numeric_effecter_value_pdr*>(e.data);
384 EXPECT_EQ(pdr->hdr.type, PLDM_NUMERIC_EFFECTER_PDR);
385
386 uint16_t effecterId = 3;
387 uint64_t effecterValue = 9876543210;
George Liu64d26be2020-01-15 10:58:10 +0800388 auto rc = platform_numeric_effecter::setNumericEffecterValueHandler<
389 MockdBusHandler, Handler>(
George Liu36e81352020-07-01 14:40:30 +0800390 mockedUtils, handler, effecterId, PLDM_EFFECTER_DATA_SIZE_SINT32,
George Liu64d26be2020-01-15 10:58:10 +0800391 reinterpret_cast<uint8_t*>(&effecterValue), 3);
392 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
393
394 pldm_pdr_destroy(inPDRRepo);
395 pldm_pdr_destroy(numericEffecterPdrRepo);
396}
Tom Josephb4268602020-04-17 17:20:45 +0530397
398TEST(parseStateSensor, allScenarios)
399{
400 // Sample state sensor with SensorID - 1, EntityType - Processor Module(67)
401 // State Set ID - Operational Running Status(11), Supported States - 3,4
402 std::vector<uint8_t> sample1PDR{0x00, 0x00, 0x00, 0x00, 0x01, 0x04, 0x00,
403 0x00, 0x17, 0x00, 0x00, 0x00, 0x01, 0x00,
404 0x43, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
405 0x00, 0x01, 0x0b, 0x00, 0x01, 0x18};
406
Patrick Williams6da4f912023-05-10 07:50:53 -0500407 const auto& [terminusHandle1, sensorID1,
408 sensorInfo1] = parseStateSensorPDR(sample1PDR);
409 const auto& [containerID1, entityType1,
410 entityInstance1] = std::get<0>(sensorInfo1);
Tom Josephb4268602020-04-17 17:20:45 +0530411 const auto& states1 = std::get<1>(sensorInfo1);
412 CompositeSensorStates statesCmp1{{3u, 4u}};
413
414 ASSERT_EQ(le16toh(terminusHandle1), 0u);
415 ASSERT_EQ(le16toh(sensorID1), 1u);
416 ASSERT_EQ(le16toh(containerID1), 0u);
417 ASSERT_EQ(le16toh(entityType1), 67u);
418 ASSERT_EQ(le16toh(entityInstance1), 1u);
419 ASSERT_EQ(states1, statesCmp1);
420
421 // Sample state sensor with SensorID - 2, EntityType - System Firmware(31)
422 // State Set ID - Availability(2), Supported States - 3,4,9,10,11,13
423 std::vector<uint8_t> sample2PDR{0x00, 0x00, 0x00, 0x00, 0x01, 0x04, 0x00,
424 0x00, 0x17, 0x00, 0x00, 0x00, 0x02, 0x00,
425 0x1F, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
426 0x00, 0x01, 0x02, 0x00, 0x02, 0x18, 0x2E};
427
Patrick Williams6da4f912023-05-10 07:50:53 -0500428 const auto& [terminusHandle2, sensorID2,
429 sensorInfo2] = parseStateSensorPDR(sample2PDR);
430 const auto& [containerID2, entityType2,
431 entityInstance2] = std::get<0>(sensorInfo2);
Tom Josephb4268602020-04-17 17:20:45 +0530432 const auto& states2 = std::get<1>(sensorInfo2);
433 CompositeSensorStates statesCmp2{{3u, 4u, 9u, 10u, 11u, 13u}};
434
435 ASSERT_EQ(le16toh(terminusHandle2), 0u);
436 ASSERT_EQ(le16toh(sensorID2), 2u);
437 ASSERT_EQ(le16toh(containerID2), 0u);
438 ASSERT_EQ(le16toh(entityType2), 31u);
439 ASSERT_EQ(le16toh(entityInstance2), 1u);
440 ASSERT_EQ(states2, statesCmp2);
441
442 // Sample state sensor with SensorID - 3, EntityType - Virtual Machine
443 // Manager(33), Composite State Sensor -2 , State Set ID - Link State(33),
444 // Supported States - 1,2, State Set ID - Configuration State(15),
445 // Supported States - 1,2,3,4
446 std::vector<uint8_t> sample3PDR{
447 0x00, 0x00, 0x00, 0x00, 0x01, 0x04, 0x00, 0x00, 0x17, 0x00, 0x00,
448 0x00, 0x03, 0x00, 0x21, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x00,
449 0x02, 0x21, 0x00, 0x01, 0x06, 0x0F, 0x00, 0x01, 0x1E};
450
Patrick Williams6da4f912023-05-10 07:50:53 -0500451 const auto& [terminusHandle3, sensorID3,
452 sensorInfo3] = parseStateSensorPDR(sample3PDR);
453 const auto& [containerID3, entityType3,
454 entityInstance3] = std::get<0>(sensorInfo3);
Tom Josephb4268602020-04-17 17:20:45 +0530455 const auto& states3 = std::get<1>(sensorInfo3);
456 CompositeSensorStates statesCmp3{{1u, 2u}, {1u, 2u, 3u, 4u}};
457
458 ASSERT_EQ(le16toh(terminusHandle3), 0u);
459 ASSERT_EQ(le16toh(sensorID3), 3u);
460 ASSERT_EQ(le16toh(containerID3), 1u);
461 ASSERT_EQ(le16toh(entityType3), 33u);
462 ASSERT_EQ(le16toh(entityInstance3), 2u);
463 ASSERT_EQ(states3, statesCmp3);
464}
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530465
466TEST(StateSensorHandler, allScenarios)
467{
468 using namespace pldm::responder::events;
469
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530470 StateSensorHandler handler{"./event_jsons/good"};
471 constexpr uint8_t eventState0 = 0;
472 constexpr uint8_t eventState1 = 1;
473 constexpr uint8_t eventState2 = 2;
474 constexpr uint8_t eventState3 = 3;
475
476 // Event Entry 1
477 {
478 StateSensorEntry entry{1, 64, 1, 0};
479 const auto& [dbusMapping, eventStateMap] = handler.getEventInfo(entry);
480 DBusMapping mapping{"/xyz/abc/def",
481 "xyz.openbmc_project.example1.value", "value1",
482 "string"};
483 ASSERT_EQ(mapping == dbusMapping, true);
484
485 const auto& propValue0 = eventStateMap.at(eventState0);
486 const auto& propValue1 = eventStateMap.at(eventState1);
487 const auto& propValue2 = eventStateMap.at(eventState2);
488 PropertyValue value0{std::in_place_type<std::string>,
489 "xyz.openbmc_project.State.Normal"};
490 PropertyValue value1{std::in_place_type<std::string>,
491 "xyz.openbmc_project.State.Critical"};
492 PropertyValue value2{std::in_place_type<std::string>,
493 "xyz.openbmc_project.State.Fatal"};
494 ASSERT_EQ(value0 == propValue0, true);
495 ASSERT_EQ(value1 == propValue1, true);
496 ASSERT_EQ(value2 == propValue2, true);
497 }
498
499 // Event Entry 2
500 {
501 StateSensorEntry entry{1, 64, 1, 1};
502 const auto& [dbusMapping, eventStateMap] = handler.getEventInfo(entry);
503 DBusMapping mapping{"/xyz/abc/def",
504 "xyz.openbmc_project.example2.value", "value2",
505 "uint8_t"};
506 ASSERT_EQ(mapping == dbusMapping, true);
507
508 const auto& propValue0 = eventStateMap.at(eventState2);
509 const auto& propValue1 = eventStateMap.at(eventState3);
510 PropertyValue value0{std::in_place_type<uint8_t>, 9};
511 PropertyValue value1{std::in_place_type<uint8_t>, 10};
512 ASSERT_EQ(value0 == propValue0, true);
513 ASSERT_EQ(value1 == propValue1, true);
514 }
515
516 // Event Entry 3
517 {
518 StateSensorEntry entry{2, 67, 2, 0};
519 const auto& [dbusMapping, eventStateMap] = handler.getEventInfo(entry);
520 DBusMapping mapping{"/xyz/abc/ghi",
521 "xyz.openbmc_project.example3.value", "value3",
522 "bool"};
523 ASSERT_EQ(mapping == dbusMapping, true);
524
525 const auto& propValue0 = eventStateMap.at(eventState0);
526 const auto& propValue1 = eventStateMap.at(eventState1);
527 PropertyValue value0{std::in_place_type<bool>, false};
528 PropertyValue value1{std::in_place_type<bool>, true};
529 ASSERT_EQ(value0 == propValue0, true);
530 ASSERT_EQ(value1 == propValue1, true);
531 }
532
533 // Invalid Entry
534 {
535 StateSensorEntry entry{0, 0, 0, 0};
536 ASSERT_THROW(handler.getEventInfo(entry), std::out_of_range);
537 }
538}
Sampa Misra12afe112020-05-25 11:40:44 -0500539
540TEST(TerminusLocatorPDR, BMCTerminusLocatorPDR)
541{
542 auto inPDRRepo = pldm_pdr_init();
543 auto outPDRRepo = pldm_pdr_init();
544 Repo outRepo(outPDRRepo);
545 MockdBusHandler mockedUtils;
Sampa Misra5fb37d52021-03-06 07:26:00 -0600546 auto event = sdeventplus::Event::get_default();
Sampa Misraaea5dde2020-08-31 08:33:47 -0500547 Handler handler(&mockedUtils, "", inPDRRepo, nullptr, nullptr, nullptr,
Sampa Misra5fb37d52021-03-06 07:26:00 -0600548 nullptr, event);
Sampa Misra12afe112020-05-25 11:40:44 -0500549 Repo inRepo(inPDRRepo);
550 getRepoByType(inRepo, outRepo, PLDM_TERMINUS_LOCATOR_PDR);
551
552 // 1 BMC terminus locator PDR in the PDR repository
553 ASSERT_EQ(outRepo.getRecordCount(), 1);
554
555 pdr_utils::PdrEntry entry;
556 auto record = pdr::getRecordByHandle(outRepo, 1, entry);
557 ASSERT_NE(record, nullptr);
558
559 auto pdr = reinterpret_cast<const pldm_terminus_locator_pdr*>(entry.data);
560 EXPECT_EQ(pdr->hdr.record_handle, 1);
561 EXPECT_EQ(pdr->hdr.version, 1);
562 EXPECT_EQ(pdr->hdr.type, PLDM_TERMINUS_LOCATOR_PDR);
563 EXPECT_EQ(pdr->hdr.record_change_num, 0);
564 EXPECT_EQ(pdr->hdr.length,
565 sizeof(pldm_terminus_locator_pdr) - sizeof(pldm_pdr_hdr));
Manojkiran Edacc5f1582021-09-29 17:03:06 +0530566 EXPECT_EQ(pdr->terminus_handle, TERMINUS_HANDLE);
Sampa Misra12afe112020-05-25 11:40:44 -0500567 EXPECT_EQ(pdr->validity, PLDM_TL_PDR_VALID);
Manojkiran Edacc5f1582021-09-29 17:03:06 +0530568 EXPECT_EQ(pdr->tid, TERMINUS_ID);
Sampa Misra12afe112020-05-25 11:40:44 -0500569 EXPECT_EQ(pdr->container_id, 0);
570 EXPECT_EQ(pdr->terminus_locator_type, PLDM_TERMINUS_LOCATOR_TYPE_MCTP_EID);
571 EXPECT_EQ(pdr->terminus_locator_value_size,
572 sizeof(pldm_terminus_locator_type_mctp_eid));
573 auto locatorValue =
574 reinterpret_cast<const pldm_terminus_locator_type_mctp_eid*>(
575 pdr->terminus_locator_value);
576 EXPECT_EQ(locatorValue->eid, BmcMctpEid);
George Liua9170122020-05-14 11:59:13 +0800577 pldm_pdr_destroy(inPDRRepo);
578 pldm_pdr_destroy(outPDRRepo);
579}
580
581TEST(getStateSensorReadingsHandler, testGoodRequest)
582{
583 MockdBusHandler mockedUtils;
584 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
585 .Times(1)
586 .WillRepeatedly(Return("foo.bar"));
587
588 auto inPDRRepo = pldm_pdr_init();
589 auto outPDRRepo = pldm_pdr_init();
590 Repo outRepo(outPDRRepo);
Sampa Misra5fb37d52021-03-06 07:26:00 -0600591 auto event = sdeventplus::Event::get_default();
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600592 Handler handler(&mockedUtils, "./pdr_jsons/state_sensor/good", inPDRRepo,
Sampa Misra5fb37d52021-03-06 07:26:00 -0600593 nullptr, nullptr, nullptr, nullptr, event);
George Liua9170122020-05-14 11:59:13 +0800594 Repo inRepo(inPDRRepo);
595 getRepoByType(inRepo, outRepo, PLDM_STATE_SENSOR_PDR);
596 pdr_utils::PdrEntry e;
597 auto record = pdr::getRecordByHandle(outRepo, 2, e);
598 ASSERT_NE(record, nullptr);
599 pldm_state_sensor_pdr* pdr =
600 reinterpret_cast<pldm_state_sensor_pdr*>(e.data);
601 EXPECT_EQ(pdr->hdr.type, PLDM_STATE_SENSOR_PDR);
602
603 std::vector<get_sensor_state_field> stateField;
604 uint8_t compSensorCnt{};
605 uint8_t sensorRearmCnt = 1;
606
607 MockdBusHandler handlerObj;
608 EXPECT_CALL(handlerObj,
609 getDbusPropertyVariant(StrEq("/foo/bar"), StrEq("propertyName"),
610 StrEq("xyz.openbmc_project.Foo.Bar")))
611 .WillOnce(Return(
612 PropertyValue(std::string("xyz.openbmc_project.Foo.Bar.V0"))));
613
614 auto rc = platform_state_sensor::getStateSensorReadingsHandler<
615 MockdBusHandler, Handler>(handlerObj, handler, 0x1, sensorRearmCnt,
616 compSensorCnt, stateField);
617 ASSERT_EQ(rc, 0);
618 ASSERT_EQ(compSensorCnt, 1);
George Liu916808c2021-01-19 17:56:42 +0800619 ASSERT_EQ(stateField[0].sensor_op_state, PLDM_SENSOR_UNAVAILABLE);
620 ASSERT_EQ(stateField[0].present_state, PLDM_SENSOR_NORMAL);
George Liua9170122020-05-14 11:59:13 +0800621 ASSERT_EQ(stateField[0].previous_state, PLDM_SENSOR_UNKNOWN);
George Liu916808c2021-01-19 17:56:42 +0800622 ASSERT_EQ(stateField[0].event_state, PLDM_SENSOR_UNKNOWN);
George Liua9170122020-05-14 11:59:13 +0800623
624 pldm_pdr_destroy(inPDRRepo);
625 pldm_pdr_destroy(outPDRRepo);
626}
627
628TEST(getStateSensorReadingsHandler, testBadRequest)
629{
630 MockdBusHandler mockedUtils;
631 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
632 .Times(1)
633 .WillRepeatedly(Return("foo.bar"));
634
635 auto inPDRRepo = pldm_pdr_init();
636 auto outPDRRepo = pldm_pdr_init();
637 Repo outRepo(outPDRRepo);
Sampa Misra5fb37d52021-03-06 07:26:00 -0600638 auto event = sdeventplus::Event::get_default();
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600639 Handler handler(&mockedUtils, "./pdr_jsons/state_sensor/good", inPDRRepo,
Sampa Misra5fb37d52021-03-06 07:26:00 -0600640 nullptr, nullptr, nullptr, nullptr, event);
George Liua9170122020-05-14 11:59:13 +0800641 Repo inRepo(inPDRRepo);
642 getRepoByType(inRepo, outRepo, PLDM_STATE_SENSOR_PDR);
643 pdr_utils::PdrEntry e;
644 auto record = pdr::getRecordByHandle(outRepo, 2, e);
645 ASSERT_NE(record, nullptr);
646 pldm_state_sensor_pdr* pdr =
647 reinterpret_cast<pldm_state_sensor_pdr*>(e.data);
648 EXPECT_EQ(pdr->hdr.type, PLDM_STATE_SENSOR_PDR);
649
650 std::vector<get_sensor_state_field> stateField;
651 uint8_t compSensorCnt{};
652 uint8_t sensorRearmCnt = 3;
653
654 MockdBusHandler handlerObj;
655 auto rc = platform_state_sensor::getStateSensorReadingsHandler<
656 MockdBusHandler, Handler>(handlerObj, handler, 0x1, sensorRearmCnt,
657 compSensorCnt, stateField);
658 ASSERT_EQ(rc, PLDM_PLATFORM_REARM_UNAVAILABLE_IN_PRESENT_STATE);
Sampa Misra12afe112020-05-25 11:40:44 -0500659
660 pldm_pdr_destroy(inPDRRepo);
661 pldm_pdr_destroy(outPDRRepo);
662}