blob: c49ebadb7ef8a7a786e83a0a06feff70462a8ae0 [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
Brad Bishop5079ac42021-08-19 18:35:06 -040014using namespace pldm::pdr;
George Liu1e44c732020-02-28 20:20:06 +080015using namespace pldm::utils;
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053016using namespace pldm::responder;
Deepak Kodihallic682fe22020-03-04 00:42:54 -060017using namespace pldm::responder::platform;
18using namespace pldm::responder::pdr;
19using namespace pldm::responder::pdr_utils;
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053020
George Liu36e81352020-07-01 14:40:30 +080021using ::testing::_;
22using ::testing::Return;
23using ::testing::StrEq;
24
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053025TEST(getPDR, testGoodPath)
26{
27 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
28 requestPayload{};
George Liue53193f2020-02-24 09:23:26 +080029 auto req = reinterpret_cast<pldm_msg*>(requestPayload.data());
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053030 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
31
George Liue53193f2020-02-24 09:23:26 +080032 struct pldm_get_pdr_req* request =
33 reinterpret_cast<struct pldm_get_pdr_req*>(req->payload);
34 request->request_count = 100;
35
George Liu36e81352020-07-01 14:40:30 +080036 MockdBusHandler mockedUtils;
37 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
38 .Times(5)
39 .WillRepeatedly(Return("foo.bar"));
40
Deepak Kodihallic682fe22020-03-04 00:42:54 -060041 auto pdrRepo = pldm_pdr_init();
Sampa Misra5fb37d52021-03-06 07:26:00 -060042 auto event = sdeventplus::Event::get_default();
Sagar Srinivas90314a32023-10-17 10:38:03 -050043 Handler handler(&mockedUtils, 0, nullptr, "./pdr_jsons/state_effecter/good",
44 pdrRepo, nullptr, nullptr, nullptr, nullptr, nullptr,
Kamalkumar Patel3c50c822024-01-30 07:14:40 -060045 nullptr, event);
Deepak Kodihallic682fe22020-03-04 00:42:54 -060046 Repo repo(pdrRepo);
47 ASSERT_EQ(repo.empty(), false);
George Liue53193f2020-02-24 09:23:26 +080048 auto response = handler.getPDR(req, requestPayloadLength);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053049 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
50
George Liue53193f2020-02-24 09:23:26 +080051 struct pldm_get_pdr_resp* resp =
52 reinterpret_cast<struct pldm_get_pdr_resp*>(responsePtr->payload);
53 ASSERT_EQ(PLDM_SUCCESS, resp->completion_code);
54 ASSERT_EQ(2, resp->next_record_handle);
55 ASSERT_EQ(true, resp->response_count != 0);
56
57 pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(resp->record_data);
58 ASSERT_EQ(hdr->record_handle, 1);
59 ASSERT_EQ(hdr->version, 1);
Deepak Kodihallic682fe22020-03-04 00:42:54 -060060
61 pldm_pdr_destroy(pdrRepo);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053062}
63
64TEST(getPDR, testShortRead)
65{
66 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
67 requestPayload{};
George Liue53193f2020-02-24 09:23:26 +080068 auto req = reinterpret_cast<pldm_msg*>(requestPayload.data());
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053069 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
70
George Liue53193f2020-02-24 09:23:26 +080071 struct pldm_get_pdr_req* request =
72 reinterpret_cast<struct pldm_get_pdr_req*>(req->payload);
73 request->request_count = 1;
74
George Liu36e81352020-07-01 14:40:30 +080075 MockdBusHandler mockedUtils;
76 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
77 .Times(5)
78 .WillRepeatedly(Return("foo.bar"));
79
Deepak Kodihallic682fe22020-03-04 00:42:54 -060080 auto pdrRepo = pldm_pdr_init();
Sampa Misra5fb37d52021-03-06 07:26:00 -060081 auto event = sdeventplus::Event::get_default();
Sagar Srinivas90314a32023-10-17 10:38:03 -050082 Handler handler(&mockedUtils, 0, nullptr, "./pdr_jsons/state_effecter/good",
83 pdrRepo, nullptr, nullptr, nullptr, nullptr, nullptr,
Kamalkumar Patel3c50c822024-01-30 07:14:40 -060084 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();
Sagar Srinivas90314a32023-10-17 10:38:03 -0500115 Handler handler(&mockedUtils, 0, nullptr, "./pdr_jsons/state_effecter/good",
116 pdrRepo, nullptr, nullptr, nullptr, nullptr, nullptr,
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600117 nullptr, event);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600118 Repo repo(pdrRepo);
119 ASSERT_EQ(repo.empty(), false);
George Liue53193f2020-02-24 09:23:26 +0800120 auto response = handler.getPDR(req, requestPayloadLength);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530121 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
122
123 ASSERT_EQ(responsePtr->payload[0], PLDM_PLATFORM_INVALID_RECORD_HANDLE);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600124
125 pldm_pdr_destroy(pdrRepo);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530126}
127
128TEST(getPDR, testNoNextRecord)
129{
130 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
131 requestPayload{};
George Liue53193f2020-02-24 09:23:26 +0800132 auto req = reinterpret_cast<pldm_msg*>(requestPayload.data());
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530133 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
134
George Liue53193f2020-02-24 09:23:26 +0800135 struct pldm_get_pdr_req* request =
136 reinterpret_cast<struct pldm_get_pdr_req*>(req->payload);
137 request->record_handle = 1;
138
George Liu36e81352020-07-01 14:40:30 +0800139 MockdBusHandler mockedUtils;
140 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
141 .Times(5)
142 .WillRepeatedly(Return("foo.bar"));
143
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600144 auto pdrRepo = pldm_pdr_init();
Sampa Misra5fb37d52021-03-06 07:26:00 -0600145 auto event = sdeventplus::Event::get_default();
Sagar Srinivas90314a32023-10-17 10:38:03 -0500146 Handler handler(&mockedUtils, 0, nullptr, "./pdr_jsons/state_effecter/good",
147 pdrRepo, nullptr, nullptr, nullptr, nullptr, nullptr,
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600148 nullptr, event);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600149 Repo repo(pdrRepo);
150 ASSERT_EQ(repo.empty(), false);
George Liue53193f2020-02-24 09:23:26 +0800151 auto response = handler.getPDR(req, requestPayloadLength);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530152 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
George Liue53193f2020-02-24 09:23:26 +0800153 struct pldm_get_pdr_resp* resp =
154 reinterpret_cast<struct pldm_get_pdr_resp*>(responsePtr->payload);
155 ASSERT_EQ(PLDM_SUCCESS, resp->completion_code);
156 ASSERT_EQ(2, resp->next_record_handle);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600157
158 pldm_pdr_destroy(pdrRepo);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530159}
160
161TEST(getPDR, testFindPDR)
162{
163 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
164 requestPayload{};
George Liue53193f2020-02-24 09:23:26 +0800165 auto req = reinterpret_cast<pldm_msg*>(requestPayload.data());
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530166 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
167
George Liue53193f2020-02-24 09:23:26 +0800168 struct pldm_get_pdr_req* request =
169 reinterpret_cast<struct pldm_get_pdr_req*>(req->payload);
170 request->request_count = 100;
171
George Liu36e81352020-07-01 14:40:30 +0800172 MockdBusHandler mockedUtils;
173 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
174 .Times(5)
175 .WillRepeatedly(Return("foo.bar"));
176
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600177 auto pdrRepo = pldm_pdr_init();
Sampa Misra5fb37d52021-03-06 07:26:00 -0600178 auto event = sdeventplus::Event::get_default();
Sagar Srinivas90314a32023-10-17 10:38:03 -0500179 Handler handler(&mockedUtils, 0, nullptr, "./pdr_jsons/state_effecter/good",
180 pdrRepo, nullptr, nullptr, nullptr, nullptr, nullptr,
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600181 nullptr, event);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600182 Repo repo(pdrRepo);
183 ASSERT_EQ(repo.empty(), false);
George Liue53193f2020-02-24 09:23:26 +0800184 auto response = handler.getPDR(req, requestPayloadLength);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530185
186 // Let's try to find a PDR of type stateEffecter (= 11) and entity type =
187 // 100
188 bool found = false;
189 uint32_t handle = 0; // start asking for PDRs from recordHandle 0
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530190 while (!found)
191 {
George Liue53193f2020-02-24 09:23:26 +0800192 request->record_handle = handle;
George Liue53193f2020-02-24 09:23:26 +0800193 auto response = handler.getPDR(req, requestPayloadLength);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530194 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
George Liue53193f2020-02-24 09:23:26 +0800195 struct pldm_get_pdr_resp* resp =
196 reinterpret_cast<struct pldm_get_pdr_resp*>(responsePtr->payload);
197 ASSERT_EQ(PLDM_SUCCESS, resp->completion_code);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530198
George Liue53193f2020-02-24 09:23:26 +0800199 handle = resp->next_record_handle; // point to the next pdr in case
200 // current is not what we want
201
202 pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(resp->record_data);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530203 if (hdr->type == PLDM_STATE_EFFECTER_PDR)
204 {
205 pldm_state_effecter_pdr* pdr =
George Liue53193f2020-02-24 09:23:26 +0800206 reinterpret_cast<pldm_state_effecter_pdr*>(resp->record_data);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530207 if (pdr->entity_type == 100)
208 {
209 found = true;
210 // Rest of the PDR can be accessed as need be
211 break;
212 }
213 }
George Liue53193f2020-02-24 09:23:26 +0800214 if (!resp->next_record_handle) // no more records
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530215 {
216 break;
217 }
218 }
219 ASSERT_EQ(found, true);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600220
221 pldm_pdr_destroy(pdrRepo);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530222}
Sampa Misraa2fa0702019-05-31 01:28:55 -0500223
Sampa Misraa2fa0702019-05-31 01:28:55 -0500224TEST(setStateEffecterStatesHandler, testGoodRequest)
225{
George Liua9170122020-05-14 11:59:13 +0800226 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
227 requestPayload{};
228 auto req = reinterpret_cast<pldm_msg*>(requestPayload.data());
229 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
230
George Liu36e81352020-07-01 14:40:30 +0800231 MockdBusHandler mockedUtils;
232 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
233 .Times(5)
234 .WillRepeatedly(Return("foo.bar"));
235
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600236 auto inPDRRepo = pldm_pdr_init();
237 auto outPDRRepo = pldm_pdr_init();
238 Repo outRepo(outPDRRepo);
Sampa Misra5fb37d52021-03-06 07:26:00 -0600239 auto event = sdeventplus::Event::get_default();
Sagar Srinivas90314a32023-10-17 10:38:03 -0500240 Handler handler(&mockedUtils, 0, nullptr, "./pdr_jsons/state_effecter/good",
241 inPDRRepo, nullptr, nullptr, nullptr, nullptr, nullptr,
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600242 nullptr, event);
George Liua9170122020-05-14 11:59:13 +0800243 handler.getPDR(req, requestPayloadLength);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600244 Repo inRepo(inPDRRepo);
245 getRepoByType(inRepo, outRepo, PLDM_STATE_EFFECTER_PDR);
George Liue53193f2020-02-24 09:23:26 +0800246 pdr_utils::PdrEntry e;
Sampa Misra12afe112020-05-25 11:40:44 -0500247 auto record1 = pdr::getRecordByHandle(outRepo, 2, e);
George Liue53193f2020-02-24 09:23:26 +0800248 ASSERT_NE(record1, nullptr);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500249 pldm_state_effecter_pdr* pdr =
George Liue53193f2020-02-24 09:23:26 +0800250 reinterpret_cast<pldm_state_effecter_pdr*>(e.data);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500251 EXPECT_EQ(pdr->hdr.type, PLDM_STATE_EFFECTER_PDR);
252
253 std::vector<set_effecter_state_field> stateField;
254 stateField.push_back({PLDM_REQUEST_SET, 1});
255 stateField.push_back({PLDM_REQUEST_SET, 1});
George Liu1ec85d42020-02-12 16:05:32 +0800256 std::string value = "xyz.openbmc_project.Foo.Bar.V1";
George Liu1e44c732020-02-28 20:20:06 +0800257 PropertyValue propertyValue = value;
Sampa Misraa2fa0702019-05-31 01:28:55 -0500258
George Liu1ec85d42020-02-12 16:05:32 +0800259 DBusMapping dbusMapping{"/foo/bar", "xyz.openbmc_project.Foo.Bar",
260 "propertyName", "string"};
George Liu1e44c732020-02-28 20:20:06 +0800261
George Liu36e81352020-07-01 14:40:30 +0800262 EXPECT_CALL(mockedUtils, setDbusProperty(dbusMapping, propertyValue))
Sampa Misraa2fa0702019-05-31 01:28:55 -0500263 .Times(2);
George Liu0d7aca82020-03-30 15:01:36 +0800264 auto rc = platform_state_effecter::setStateEffecterStatesHandler<
George Liu36e81352020-07-01 14:40:30 +0800265 MockdBusHandler, Handler>(mockedUtils, handler, 0x1, stateField);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500266 ASSERT_EQ(rc, 0);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600267
268 pldm_pdr_destroy(inPDRRepo);
269 pldm_pdr_destroy(outPDRRepo);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500270}
271
272TEST(setStateEffecterStatesHandler, testBadRequest)
273{
George Liua9170122020-05-14 11:59:13 +0800274 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
275 requestPayload{};
276 auto req = reinterpret_cast<pldm_msg*>(requestPayload.data());
277 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
278
George Liu36e81352020-07-01 14:40:30 +0800279 MockdBusHandler mockedUtils;
280 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
281 .Times(5)
282 .WillRepeatedly(Return("foo.bar"));
283
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600284 auto inPDRRepo = pldm_pdr_init();
285 auto outPDRRepo = pldm_pdr_init();
286 Repo outRepo(outPDRRepo);
Sampa Misra5fb37d52021-03-06 07:26:00 -0600287 auto event = sdeventplus::Event::get_default();
Sagar Srinivas90314a32023-10-17 10:38:03 -0500288 Handler handler(&mockedUtils, 0, nullptr, "./pdr_jsons/state_effecter/good",
289 inPDRRepo, nullptr, nullptr, nullptr, nullptr, nullptr,
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600290 nullptr, event);
George Liua9170122020-05-14 11:59:13 +0800291 handler.getPDR(req, requestPayloadLength);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600292 Repo inRepo(inPDRRepo);
293 getRepoByType(inRepo, outRepo, PLDM_STATE_EFFECTER_PDR);
George Liue53193f2020-02-24 09:23:26 +0800294 pdr_utils::PdrEntry e;
Sampa Misra12afe112020-05-25 11:40:44 -0500295 auto record1 = pdr::getRecordByHandle(outRepo, 2, e);
George Liue53193f2020-02-24 09:23:26 +0800296 ASSERT_NE(record1, nullptr);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500297 pldm_state_effecter_pdr* pdr =
George Liue53193f2020-02-24 09:23:26 +0800298 reinterpret_cast<pldm_state_effecter_pdr*>(e.data);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500299 EXPECT_EQ(pdr->hdr.type, PLDM_STATE_EFFECTER_PDR);
300
301 std::vector<set_effecter_state_field> stateField;
302 stateField.push_back({PLDM_REQUEST_SET, 3});
303 stateField.push_back({PLDM_REQUEST_SET, 4});
304
George Liu0d7aca82020-03-30 15:01:36 +0800305 auto rc = platform_state_effecter::setStateEffecterStatesHandler<
George Liu36e81352020-07-01 14:40:30 +0800306 MockdBusHandler, Handler>(mockedUtils, handler, 0x1, stateField);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500307 ASSERT_EQ(rc, PLDM_PLATFORM_SET_EFFECTER_UNSUPPORTED_SENSORSTATE);
308
George Liu0d7aca82020-03-30 15:01:36 +0800309 rc = platform_state_effecter::setStateEffecterStatesHandler<MockdBusHandler,
310 Handler>(
George Liu36e81352020-07-01 14:40:30 +0800311 mockedUtils, handler, 0x9, stateField);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500312 ASSERT_EQ(rc, PLDM_PLATFORM_INVALID_EFFECTER_ID);
313
314 stateField.push_back({PLDM_REQUEST_SET, 4});
George Liu0d7aca82020-03-30 15:01:36 +0800315 rc = platform_state_effecter::setStateEffecterStatesHandler<MockdBusHandler,
316 Handler>(
George Liu36e81352020-07-01 14:40:30 +0800317 mockedUtils, handler, 0x1, stateField);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500318 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
319
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600320 pldm_pdr_destroy(inPDRRepo);
321 pldm_pdr_destroy(outPDRRepo);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500322}
George Liu64d26be2020-01-15 10:58:10 +0800323
324TEST(setNumericEffecterValueHandler, testGoodRequest)
325{
George Liu36e81352020-07-01 14:40:30 +0800326 MockdBusHandler mockedUtils;
327 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
328 .Times(5)
329 .WillRepeatedly(Return("foo.bar"));
330
George Liu64d26be2020-01-15 10:58:10 +0800331 auto inPDRRepo = pldm_pdr_init();
332 auto numericEffecterPdrRepo = pldm_pdr_init();
333 Repo numericEffecterPDRs(numericEffecterPdrRepo);
Sampa Misra5fb37d52021-03-06 07:26:00 -0600334 auto event = sdeventplus::Event::get_default();
Sagar Srinivas90314a32023-10-17 10:38:03 -0500335 Handler handler(&mockedUtils, 0, nullptr, "./pdr_jsons/state_effecter/good",
336 inPDRRepo, nullptr, nullptr, nullptr, nullptr, nullptr,
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600337 nullptr, event);
George Liu64d26be2020-01-15 10:58:10 +0800338 Repo inRepo(inPDRRepo);
339 getRepoByType(inRepo, numericEffecterPDRs, PLDM_NUMERIC_EFFECTER_PDR);
340
341 pdr_utils::PdrEntry e;
Sampa Misra12afe112020-05-25 11:40:44 -0500342 auto record4 = pdr::getRecordByHandle(numericEffecterPDRs, 4, e);
343 ASSERT_NE(record4, nullptr);
George Liu64d26be2020-01-15 10:58:10 +0800344
345 pldm_numeric_effecter_value_pdr* pdr =
346 reinterpret_cast<pldm_numeric_effecter_value_pdr*>(e.data);
347 EXPECT_EQ(pdr->hdr.type, PLDM_NUMERIC_EFFECTER_PDR);
348
349 uint16_t effecterId = 3;
350 uint32_t effecterValue = 2100000000; // 2036-07-18 21:20:00
Pavithra Barithaya45cd16b2021-07-01 08:19:59 -0500351 PropertyValue propertyValue = static_cast<uint64_t>(effecterValue);
George Liu64d26be2020-01-15 10:58:10 +0800352
George Liu64d26be2020-01-15 10:58:10 +0800353 DBusMapping dbusMapping{"/foo/bar", "xyz.openbmc_project.Foo.Bar",
354 "propertyName", "uint64_t"};
George Liu36e81352020-07-01 14:40:30 +0800355 EXPECT_CALL(mockedUtils, setDbusProperty(dbusMapping, propertyValue))
George Liu64d26be2020-01-15 10:58:10 +0800356 .Times(1);
357
358 auto rc = platform_numeric_effecter::setNumericEffecterValueHandler<
359 MockdBusHandler, Handler>(
George Liu36e81352020-07-01 14:40:30 +0800360 mockedUtils, handler, effecterId, PLDM_EFFECTER_DATA_SIZE_UINT32,
George Liu64d26be2020-01-15 10:58:10 +0800361 reinterpret_cast<uint8_t*>(&effecterValue), 4);
362 ASSERT_EQ(rc, 0);
363
364 pldm_pdr_destroy(inPDRRepo);
365 pldm_pdr_destroy(numericEffecterPdrRepo);
366}
367
368TEST(setNumericEffecterValueHandler, testBadRequest)
369{
George Liu36e81352020-07-01 14:40:30 +0800370 MockdBusHandler mockedUtils;
371 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
372 .Times(5)
373 .WillRepeatedly(Return("foo.bar"));
374
George Liu64d26be2020-01-15 10:58:10 +0800375 auto inPDRRepo = pldm_pdr_init();
376 auto numericEffecterPdrRepo = pldm_pdr_init();
377 Repo numericEffecterPDRs(numericEffecterPdrRepo);
Sampa Misra5fb37d52021-03-06 07:26:00 -0600378 auto event = sdeventplus::Event::get_default();
Sagar Srinivas90314a32023-10-17 10:38:03 -0500379 Handler handler(&mockedUtils, 0, nullptr, "./pdr_jsons/state_effecter/good",
380 inPDRRepo, nullptr, nullptr, nullptr, nullptr, nullptr,
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600381 nullptr, event);
George Liu64d26be2020-01-15 10:58:10 +0800382 Repo inRepo(inPDRRepo);
383 getRepoByType(inRepo, numericEffecterPDRs, PLDM_NUMERIC_EFFECTER_PDR);
384
385 pdr_utils::PdrEntry e;
Sampa Misra12afe112020-05-25 11:40:44 -0500386 auto record4 = pdr::getRecordByHandle(numericEffecterPDRs, 4, e);
387 ASSERT_NE(record4, nullptr);
George Liu64d26be2020-01-15 10:58:10 +0800388
389 pldm_numeric_effecter_value_pdr* pdr =
390 reinterpret_cast<pldm_numeric_effecter_value_pdr*>(e.data);
391 EXPECT_EQ(pdr->hdr.type, PLDM_NUMERIC_EFFECTER_PDR);
392
393 uint16_t effecterId = 3;
394 uint64_t effecterValue = 9876543210;
George Liu64d26be2020-01-15 10:58:10 +0800395 auto rc = platform_numeric_effecter::setNumericEffecterValueHandler<
396 MockdBusHandler, Handler>(
George Liu36e81352020-07-01 14:40:30 +0800397 mockedUtils, handler, effecterId, PLDM_EFFECTER_DATA_SIZE_SINT32,
George Liu64d26be2020-01-15 10:58:10 +0800398 reinterpret_cast<uint8_t*>(&effecterValue), 3);
399 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
400
401 pldm_pdr_destroy(inPDRRepo);
402 pldm_pdr_destroy(numericEffecterPdrRepo);
403}
Tom Josephb4268602020-04-17 17:20:45 +0530404
Archana Kakani6ece21fb2023-10-10 08:21:52 -0500405TEST(getNumericEffecterValueHandler, testGoodRequest)
406{
407 MockdBusHandler mockedUtils;
408 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
409 .Times(5)
410 .WillRepeatedly(Return("foo.bar"));
411
412 auto inPDRRepo = pldm_pdr_init();
413 auto numericEffecterPdrRepo = pldm_pdr_init();
414 Repo numericEffecterPDRs(numericEffecterPdrRepo);
415 auto event = sdeventplus::Event::get_default();
Sagar Srinivas90314a32023-10-17 10:38:03 -0500416 Handler handler(&mockedUtils, 0, nullptr, "./pdr_jsons/state_effecter/good",
417 inPDRRepo, nullptr, nullptr, nullptr, nullptr, nullptr,
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600418 nullptr, event);
Archana Kakani6ece21fb2023-10-10 08:21:52 -0500419 Repo inRepo(inPDRRepo);
420 getRepoByType(inRepo, numericEffecterPDRs, PLDM_NUMERIC_EFFECTER_PDR);
421
422 pdr_utils::PdrEntry e;
423 auto record4 = pdr::getRecordByHandle(numericEffecterPDRs, 4, e);
424 ASSERT_NE(record4, nullptr);
425
426 pldm_numeric_effecter_value_pdr* pdr =
427 reinterpret_cast<pldm_numeric_effecter_value_pdr*>(e.data);
428 EXPECT_EQ(pdr->hdr.type, PLDM_NUMERIC_EFFECTER_PDR);
429
430 uint16_t effecterId = 3;
431
432 uint8_t effecterDataSize{};
433 pldm::utils::PropertyValue dbusValue;
434 std::string propertyType;
435
436 // effecterValue return the present numeric setting
437 uint32_t effecterValue = 2100000000;
438 using effecterOperationalState = uint8_t;
439 using completionCode = uint8_t;
440
441 EXPECT_CALL(mockedUtils,
442 getDbusPropertyVariant(StrEq("/foo/bar"), StrEq("propertyName"),
443 StrEq("xyz.openbmc_project.Foo.Bar")))
444 .WillOnce(Return(PropertyValue(static_cast<uint64_t>(effecterValue))));
445
446 auto rc = platform_numeric_effecter::getNumericEffecterData<MockdBusHandler,
447 Handler>(
448 mockedUtils, handler, effecterId, effecterDataSize, propertyType,
449 dbusValue);
450
451 ASSERT_EQ(rc, 0);
452
453 size_t responsePayloadLength = sizeof(completionCode) +
454 sizeof(effecterDataSize) +
455 sizeof(effecterOperationalState) +
456 getEffecterDataSize(effecterDataSize) +
457 getEffecterDataSize(effecterDataSize);
458
459 Response response(responsePayloadLength + sizeof(pldm_msg_hdr));
460 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
461
462 rc = platform_numeric_effecter::getNumericEffecterValueHandler(
463 propertyType, dbusValue, effecterDataSize, responsePtr,
464 responsePayloadLength, 1);
465
466 ASSERT_EQ(rc, 0);
467
468 struct pldm_get_numeric_effecter_value_resp* resp =
469 reinterpret_cast<struct pldm_get_numeric_effecter_value_resp*>(
470 responsePtr->payload);
471 ASSERT_EQ(PLDM_SUCCESS, resp->completion_code);
472 uint32_t valPresent;
473 memcpy(&valPresent, &resp->pending_and_present_values[4],
474 sizeof(valPresent));
475
476 ASSERT_EQ(effecterValue, valPresent);
477
478 pldm_pdr_destroy(inPDRRepo);
479 pldm_pdr_destroy(numericEffecterPdrRepo);
480}
481
482TEST(getNumericEffecterValueHandler, testBadRequest)
483{
484 MockdBusHandler mockedUtils;
485 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
486 .Times(5)
487 .WillRepeatedly(Return("foo.bar"));
488
489 auto inPDRRepo = pldm_pdr_init();
490 auto numericEffecterPdrRepo = pldm_pdr_init();
491 Repo numericEffecterPDRs(numericEffecterPdrRepo);
492 auto event = sdeventplus::Event::get_default();
Sagar Srinivas90314a32023-10-17 10:38:03 -0500493 Handler handler(&mockedUtils, 0, nullptr, "./pdr_jsons/state_effecter/good",
494 inPDRRepo, nullptr, nullptr, nullptr, nullptr, nullptr,
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600495 nullptr, event);
Archana Kakani6ece21fb2023-10-10 08:21:52 -0500496 Repo inRepo(inPDRRepo);
497 getRepoByType(inRepo, numericEffecterPDRs, PLDM_NUMERIC_EFFECTER_PDR);
498
499 pdr_utils::PdrEntry e;
500 auto record4 = pdr::getRecordByHandle(numericEffecterPDRs, 4, e);
501 ASSERT_NE(record4, nullptr);
502
503 pldm_numeric_effecter_value_pdr* pdr =
504 reinterpret_cast<pldm_numeric_effecter_value_pdr*>(e.data);
505 EXPECT_EQ(pdr->hdr.type, PLDM_NUMERIC_EFFECTER_PDR);
506
507 uint16_t effecterId = 4;
508
509 uint8_t effecterDataSize{};
510 pldm::utils::PropertyValue dbusValue;
511 std::string propertyType;
512
513 auto rc = platform_numeric_effecter::getNumericEffecterData<MockdBusHandler,
514 Handler>(
515 mockedUtils, handler, effecterId, effecterDataSize, propertyType,
516 dbusValue);
517
518 ASSERT_EQ(rc, 128);
519
520 pldm_pdr_destroy(inPDRRepo);
521 pldm_pdr_destroy(numericEffecterPdrRepo);
522}
523
Tom Josephb4268602020-04-17 17:20:45 +0530524TEST(parseStateSensor, allScenarios)
525{
526 // Sample state sensor with SensorID - 1, EntityType - Processor Module(67)
527 // State Set ID - Operational Running Status(11), Supported States - 3,4
528 std::vector<uint8_t> sample1PDR{0x00, 0x00, 0x00, 0x00, 0x01, 0x04, 0x00,
529 0x00, 0x17, 0x00, 0x00, 0x00, 0x01, 0x00,
530 0x43, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
531 0x00, 0x01, 0x0b, 0x00, 0x01, 0x18};
532
Patrick Williams6da4f912023-05-10 07:50:53 -0500533 const auto& [terminusHandle1, sensorID1,
534 sensorInfo1] = parseStateSensorPDR(sample1PDR);
535 const auto& [containerID1, entityType1,
536 entityInstance1] = std::get<0>(sensorInfo1);
Tom Josephb4268602020-04-17 17:20:45 +0530537 const auto& states1 = std::get<1>(sensorInfo1);
538 CompositeSensorStates statesCmp1{{3u, 4u}};
539
540 ASSERT_EQ(le16toh(terminusHandle1), 0u);
541 ASSERT_EQ(le16toh(sensorID1), 1u);
542 ASSERT_EQ(le16toh(containerID1), 0u);
543 ASSERT_EQ(le16toh(entityType1), 67u);
544 ASSERT_EQ(le16toh(entityInstance1), 1u);
545 ASSERT_EQ(states1, statesCmp1);
546
547 // Sample state sensor with SensorID - 2, EntityType - System Firmware(31)
548 // State Set ID - Availability(2), Supported States - 3,4,9,10,11,13
549 std::vector<uint8_t> sample2PDR{0x00, 0x00, 0x00, 0x00, 0x01, 0x04, 0x00,
550 0x00, 0x17, 0x00, 0x00, 0x00, 0x02, 0x00,
551 0x1F, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
552 0x00, 0x01, 0x02, 0x00, 0x02, 0x18, 0x2E};
553
Patrick Williams6da4f912023-05-10 07:50:53 -0500554 const auto& [terminusHandle2, sensorID2,
555 sensorInfo2] = parseStateSensorPDR(sample2PDR);
556 const auto& [containerID2, entityType2,
557 entityInstance2] = std::get<0>(sensorInfo2);
Tom Josephb4268602020-04-17 17:20:45 +0530558 const auto& states2 = std::get<1>(sensorInfo2);
559 CompositeSensorStates statesCmp2{{3u, 4u, 9u, 10u, 11u, 13u}};
560
561 ASSERT_EQ(le16toh(terminusHandle2), 0u);
562 ASSERT_EQ(le16toh(sensorID2), 2u);
563 ASSERT_EQ(le16toh(containerID2), 0u);
564 ASSERT_EQ(le16toh(entityType2), 31u);
565 ASSERT_EQ(le16toh(entityInstance2), 1u);
566 ASSERT_EQ(states2, statesCmp2);
567
568 // Sample state sensor with SensorID - 3, EntityType - Virtual Machine
569 // Manager(33), Composite State Sensor -2 , State Set ID - Link State(33),
570 // Supported States - 1,2, State Set ID - Configuration State(15),
571 // Supported States - 1,2,3,4
572 std::vector<uint8_t> sample3PDR{
573 0x00, 0x00, 0x00, 0x00, 0x01, 0x04, 0x00, 0x00, 0x17, 0x00, 0x00,
574 0x00, 0x03, 0x00, 0x21, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x00,
575 0x02, 0x21, 0x00, 0x01, 0x06, 0x0F, 0x00, 0x01, 0x1E};
576
Patrick Williams6da4f912023-05-10 07:50:53 -0500577 const auto& [terminusHandle3, sensorID3,
578 sensorInfo3] = parseStateSensorPDR(sample3PDR);
579 const auto& [containerID3, entityType3,
580 entityInstance3] = std::get<0>(sensorInfo3);
Tom Josephb4268602020-04-17 17:20:45 +0530581 const auto& states3 = std::get<1>(sensorInfo3);
582 CompositeSensorStates statesCmp3{{1u, 2u}, {1u, 2u, 3u, 4u}};
583
584 ASSERT_EQ(le16toh(terminusHandle3), 0u);
585 ASSERT_EQ(le16toh(sensorID3), 3u);
586 ASSERT_EQ(le16toh(containerID3), 1u);
587 ASSERT_EQ(le16toh(entityType3), 33u);
588 ASSERT_EQ(le16toh(entityInstance3), 2u);
589 ASSERT_EQ(states3, statesCmp3);
590}
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530591
592TEST(StateSensorHandler, allScenarios)
593{
594 using namespace pldm::responder::events;
595
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530596 StateSensorHandler handler{"./event_jsons/good"};
597 constexpr uint8_t eventState0 = 0;
598 constexpr uint8_t eventState1 = 1;
599 constexpr uint8_t eventState2 = 2;
600 constexpr uint8_t eventState3 = 3;
601
602 // Event Entry 1
603 {
Sagar Srinivase3607a32024-02-16 03:50:53 -0600604 StateSensorEntry entry{1, 64, 1, 0, 1};
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530605 const auto& [dbusMapping, eventStateMap] = handler.getEventInfo(entry);
606 DBusMapping mapping{"/xyz/abc/def",
607 "xyz.openbmc_project.example1.value", "value1",
608 "string"};
609 ASSERT_EQ(mapping == dbusMapping, true);
610
611 const auto& propValue0 = eventStateMap.at(eventState0);
612 const auto& propValue1 = eventStateMap.at(eventState1);
613 const auto& propValue2 = eventStateMap.at(eventState2);
614 PropertyValue value0{std::in_place_type<std::string>,
615 "xyz.openbmc_project.State.Normal"};
616 PropertyValue value1{std::in_place_type<std::string>,
617 "xyz.openbmc_project.State.Critical"};
618 PropertyValue value2{std::in_place_type<std::string>,
619 "xyz.openbmc_project.State.Fatal"};
620 ASSERT_EQ(value0 == propValue0, true);
621 ASSERT_EQ(value1 == propValue1, true);
622 ASSERT_EQ(value2 == propValue2, true);
623 }
624
625 // Event Entry 2
626 {
Sagar Srinivase3607a32024-02-16 03:50:53 -0600627 StateSensorEntry entry{1, 64, 1, 1, 1};
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530628 const auto& [dbusMapping, eventStateMap] = handler.getEventInfo(entry);
629 DBusMapping mapping{"/xyz/abc/def",
630 "xyz.openbmc_project.example2.value", "value2",
631 "uint8_t"};
632 ASSERT_EQ(mapping == dbusMapping, true);
633
634 const auto& propValue0 = eventStateMap.at(eventState2);
635 const auto& propValue1 = eventStateMap.at(eventState3);
636 PropertyValue value0{std::in_place_type<uint8_t>, 9};
637 PropertyValue value1{std::in_place_type<uint8_t>, 10};
638 ASSERT_EQ(value0 == propValue0, true);
639 ASSERT_EQ(value1 == propValue1, true);
640 }
641
642 // Event Entry 3
643 {
Sagar Srinivase3607a32024-02-16 03:50:53 -0600644 StateSensorEntry entry{2, 67, 2, 0, 1};
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530645 const auto& [dbusMapping, eventStateMap] = handler.getEventInfo(entry);
646 DBusMapping mapping{"/xyz/abc/ghi",
647 "xyz.openbmc_project.example3.value", "value3",
648 "bool"};
649 ASSERT_EQ(mapping == dbusMapping, true);
650
651 const auto& propValue0 = eventStateMap.at(eventState0);
652 const auto& propValue1 = eventStateMap.at(eventState1);
653 PropertyValue value0{std::in_place_type<bool>, false};
654 PropertyValue value1{std::in_place_type<bool>, true};
655 ASSERT_EQ(value0 == propValue0, true);
656 ASSERT_EQ(value1 == propValue1, true);
657 }
658
Sagar Srinivase3607a32024-02-16 03:50:53 -0600659 // Event Entry 4
660 {
661 StateSensorEntry entry{2, 67, 2, 0, 2};
662 const auto& [dbusMapping, eventStateMap] = handler.getEventInfo(entry);
663 DBusMapping mapping{"/xyz/abc/jkl",
664 "xyz.openbmc_project.example4.value", "value4",
665 "string"};
666 ASSERT_EQ(mapping == dbusMapping, true);
667
668 const auto& propValue0 = eventStateMap.at(eventState0);
669 const auto& propValue1 = eventStateMap.at(eventState1);
670 const auto& propValue2 = eventStateMap.at(eventState2);
671 PropertyValue value0{std::in_place_type<std::string>, "Enabled"};
672 PropertyValue value1{std::in_place_type<std::string>, "Disabled"};
673 PropertyValue value2{std::in_place_type<std::string>, "Auto"};
674 ASSERT_EQ(value0 == propValue0, true);
675 ASSERT_EQ(value1 == propValue1, true);
676 ASSERT_EQ(value2 == propValue2, true);
677 }
678
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530679 // Invalid Entry
680 {
Sagar Srinivase3607a32024-02-16 03:50:53 -0600681 StateSensorEntry entry{0, 0, 0, 0, 1};
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530682 ASSERT_THROW(handler.getEventInfo(entry), std::out_of_range);
683 }
684}
Sampa Misra12afe112020-05-25 11:40:44 -0500685
686TEST(TerminusLocatorPDR, BMCTerminusLocatorPDR)
687{
688 auto inPDRRepo = pldm_pdr_init();
689 auto outPDRRepo = pldm_pdr_init();
690 Repo outRepo(outPDRRepo);
691 MockdBusHandler mockedUtils;
Sampa Misra5fb37d52021-03-06 07:26:00 -0600692 auto event = sdeventplus::Event::get_default();
Sagar Srinivas90314a32023-10-17 10:38:03 -0500693 Handler handler(&mockedUtils, 0, nullptr, "", inPDRRepo, nullptr, nullptr,
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600694 nullptr, nullptr, nullptr, nullptr, event);
Sampa Misra12afe112020-05-25 11:40:44 -0500695 Repo inRepo(inPDRRepo);
696 getRepoByType(inRepo, outRepo, PLDM_TERMINUS_LOCATOR_PDR);
697
698 // 1 BMC terminus locator PDR in the PDR repository
699 ASSERT_EQ(outRepo.getRecordCount(), 1);
700
701 pdr_utils::PdrEntry entry;
702 auto record = pdr::getRecordByHandle(outRepo, 1, entry);
703 ASSERT_NE(record, nullptr);
704
705 auto pdr = reinterpret_cast<const pldm_terminus_locator_pdr*>(entry.data);
706 EXPECT_EQ(pdr->hdr.record_handle, 1);
707 EXPECT_EQ(pdr->hdr.version, 1);
708 EXPECT_EQ(pdr->hdr.type, PLDM_TERMINUS_LOCATOR_PDR);
709 EXPECT_EQ(pdr->hdr.record_change_num, 0);
710 EXPECT_EQ(pdr->hdr.length,
711 sizeof(pldm_terminus_locator_pdr) - sizeof(pldm_pdr_hdr));
Manojkiran Edacc5f1582021-09-29 17:03:06 +0530712 EXPECT_EQ(pdr->terminus_handle, TERMINUS_HANDLE);
Sampa Misra12afe112020-05-25 11:40:44 -0500713 EXPECT_EQ(pdr->validity, PLDM_TL_PDR_VALID);
Manojkiran Edacc5f1582021-09-29 17:03:06 +0530714 EXPECT_EQ(pdr->tid, TERMINUS_ID);
Sampa Misra12afe112020-05-25 11:40:44 -0500715 EXPECT_EQ(pdr->container_id, 0);
716 EXPECT_EQ(pdr->terminus_locator_type, PLDM_TERMINUS_LOCATOR_TYPE_MCTP_EID);
717 EXPECT_EQ(pdr->terminus_locator_value_size,
718 sizeof(pldm_terminus_locator_type_mctp_eid));
719 auto locatorValue =
720 reinterpret_cast<const pldm_terminus_locator_type_mctp_eid*>(
721 pdr->terminus_locator_value);
722 EXPECT_EQ(locatorValue->eid, BmcMctpEid);
George Liua9170122020-05-14 11:59:13 +0800723 pldm_pdr_destroy(inPDRRepo);
724 pldm_pdr_destroy(outPDRRepo);
725}
726
727TEST(getStateSensorReadingsHandler, testGoodRequest)
728{
729 MockdBusHandler mockedUtils;
730 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
731 .Times(1)
732 .WillRepeatedly(Return("foo.bar"));
733
734 auto inPDRRepo = pldm_pdr_init();
735 auto outPDRRepo = pldm_pdr_init();
736 Repo outRepo(outPDRRepo);
Sampa Misra5fb37d52021-03-06 07:26:00 -0600737 auto event = sdeventplus::Event::get_default();
Sagar Srinivas90314a32023-10-17 10:38:03 -0500738 Handler handler(&mockedUtils, 0, nullptr, "./pdr_jsons/state_sensor/good",
739 inPDRRepo, nullptr, nullptr, nullptr, nullptr, nullptr,
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600740 nullptr, event);
George Liua9170122020-05-14 11:59:13 +0800741 Repo inRepo(inPDRRepo);
742 getRepoByType(inRepo, outRepo, PLDM_STATE_SENSOR_PDR);
743 pdr_utils::PdrEntry e;
744 auto record = pdr::getRecordByHandle(outRepo, 2, e);
745 ASSERT_NE(record, nullptr);
746 pldm_state_sensor_pdr* pdr =
747 reinterpret_cast<pldm_state_sensor_pdr*>(e.data);
748 EXPECT_EQ(pdr->hdr.type, PLDM_STATE_SENSOR_PDR);
749
750 std::vector<get_sensor_state_field> stateField;
751 uint8_t compSensorCnt{};
752 uint8_t sensorRearmCnt = 1;
753
754 MockdBusHandler handlerObj;
755 EXPECT_CALL(handlerObj,
756 getDbusPropertyVariant(StrEq("/foo/bar"), StrEq("propertyName"),
757 StrEq("xyz.openbmc_project.Foo.Bar")))
758 .WillOnce(Return(
759 PropertyValue(std::string("xyz.openbmc_project.Foo.Bar.V0"))));
760
761 auto rc = platform_state_sensor::getStateSensorReadingsHandler<
762 MockdBusHandler, Handler>(handlerObj, handler, 0x1, sensorRearmCnt,
763 compSensorCnt, stateField);
764 ASSERT_EQ(rc, 0);
765 ASSERT_EQ(compSensorCnt, 1);
George Liu916808c2021-01-19 17:56:42 +0800766 ASSERT_EQ(stateField[0].sensor_op_state, PLDM_SENSOR_UNAVAILABLE);
767 ASSERT_EQ(stateField[0].present_state, PLDM_SENSOR_NORMAL);
George Liua9170122020-05-14 11:59:13 +0800768 ASSERT_EQ(stateField[0].previous_state, PLDM_SENSOR_UNKNOWN);
George Liu916808c2021-01-19 17:56:42 +0800769 ASSERT_EQ(stateField[0].event_state, PLDM_SENSOR_UNKNOWN);
George Liua9170122020-05-14 11:59:13 +0800770
771 pldm_pdr_destroy(inPDRRepo);
772 pldm_pdr_destroy(outPDRRepo);
773}
774
775TEST(getStateSensorReadingsHandler, testBadRequest)
776{
777 MockdBusHandler mockedUtils;
778 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
779 .Times(1)
780 .WillRepeatedly(Return("foo.bar"));
781
782 auto inPDRRepo = pldm_pdr_init();
783 auto outPDRRepo = pldm_pdr_init();
784 Repo outRepo(outPDRRepo);
Sampa Misra5fb37d52021-03-06 07:26:00 -0600785 auto event = sdeventplus::Event::get_default();
Sagar Srinivas90314a32023-10-17 10:38:03 -0500786 Handler handler(&mockedUtils, 0, nullptr, "./pdr_jsons/state_sensor/good",
787 inPDRRepo, nullptr, nullptr, nullptr, nullptr, nullptr,
Kamalkumar Patel3c50c822024-01-30 07:14:40 -0600788 nullptr, event);
George Liua9170122020-05-14 11:59:13 +0800789 Repo inRepo(inPDRRepo);
790 getRepoByType(inRepo, outRepo, PLDM_STATE_SENSOR_PDR);
791 pdr_utils::PdrEntry e;
792 auto record = pdr::getRecordByHandle(outRepo, 2, e);
793 ASSERT_NE(record, nullptr);
794 pldm_state_sensor_pdr* pdr =
795 reinterpret_cast<pldm_state_sensor_pdr*>(e.data);
796 EXPECT_EQ(pdr->hdr.type, PLDM_STATE_SENSOR_PDR);
797
798 std::vector<get_sensor_state_field> stateField;
799 uint8_t compSensorCnt{};
800 uint8_t sensorRearmCnt = 3;
801
802 MockdBusHandler handlerObj;
803 auto rc = platform_state_sensor::getStateSensorReadingsHandler<
804 MockdBusHandler, Handler>(handlerObj, handler, 0x1, sensorRearmCnt,
805 compSensorCnt, stateField);
806 ASSERT_EQ(rc, PLDM_PLATFORM_REARM_UNAVAILABLE_IN_PRESENT_STATE);
Sampa Misra12afe112020-05-25 11:40:44 -0500807
808 pldm_pdr_destroy(inPDRRepo);
809 pldm_pdr_destroy(outPDRRepo);
810}