blob: 2d52987d0475ddf07c2dedd5e3cd7f2cac5bc8cd [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
Archana Kakani6ece21fb2023-10-10 08:21:52 -0500398TEST(getNumericEffecterValueHandler, testGoodRequest)
399{
400 MockdBusHandler mockedUtils;
401 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
402 .Times(5)
403 .WillRepeatedly(Return("foo.bar"));
404
405 auto inPDRRepo = pldm_pdr_init();
406 auto numericEffecterPdrRepo = pldm_pdr_init();
407 Repo numericEffecterPDRs(numericEffecterPdrRepo);
408 auto event = sdeventplus::Event::get_default();
409 Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good", inPDRRepo,
410 nullptr, nullptr, nullptr, nullptr, event);
411 Repo inRepo(inPDRRepo);
412 getRepoByType(inRepo, numericEffecterPDRs, PLDM_NUMERIC_EFFECTER_PDR);
413
414 pdr_utils::PdrEntry e;
415 auto record4 = pdr::getRecordByHandle(numericEffecterPDRs, 4, e);
416 ASSERT_NE(record4, nullptr);
417
418 pldm_numeric_effecter_value_pdr* pdr =
419 reinterpret_cast<pldm_numeric_effecter_value_pdr*>(e.data);
420 EXPECT_EQ(pdr->hdr.type, PLDM_NUMERIC_EFFECTER_PDR);
421
422 uint16_t effecterId = 3;
423
424 uint8_t effecterDataSize{};
425 pldm::utils::PropertyValue dbusValue;
426 std::string propertyType;
427
428 // effecterValue return the present numeric setting
429 uint32_t effecterValue = 2100000000;
430 using effecterOperationalState = uint8_t;
431 using completionCode = uint8_t;
432
433 EXPECT_CALL(mockedUtils,
434 getDbusPropertyVariant(StrEq("/foo/bar"), StrEq("propertyName"),
435 StrEq("xyz.openbmc_project.Foo.Bar")))
436 .WillOnce(Return(PropertyValue(static_cast<uint64_t>(effecterValue))));
437
438 auto rc = platform_numeric_effecter::getNumericEffecterData<MockdBusHandler,
439 Handler>(
440 mockedUtils, handler, effecterId, effecterDataSize, propertyType,
441 dbusValue);
442
443 ASSERT_EQ(rc, 0);
444
445 size_t responsePayloadLength = sizeof(completionCode) +
446 sizeof(effecterDataSize) +
447 sizeof(effecterOperationalState) +
448 getEffecterDataSize(effecterDataSize) +
449 getEffecterDataSize(effecterDataSize);
450
451 Response response(responsePayloadLength + sizeof(pldm_msg_hdr));
452 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
453
454 rc = platform_numeric_effecter::getNumericEffecterValueHandler(
455 propertyType, dbusValue, effecterDataSize, responsePtr,
456 responsePayloadLength, 1);
457
458 ASSERT_EQ(rc, 0);
459
460 struct pldm_get_numeric_effecter_value_resp* resp =
461 reinterpret_cast<struct pldm_get_numeric_effecter_value_resp*>(
462 responsePtr->payload);
463 ASSERT_EQ(PLDM_SUCCESS, resp->completion_code);
464 uint32_t valPresent;
465 memcpy(&valPresent, &resp->pending_and_present_values[4],
466 sizeof(valPresent));
467
468 ASSERT_EQ(effecterValue, valPresent);
469
470 pldm_pdr_destroy(inPDRRepo);
471 pldm_pdr_destroy(numericEffecterPdrRepo);
472}
473
474TEST(getNumericEffecterValueHandler, testBadRequest)
475{
476 MockdBusHandler mockedUtils;
477 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
478 .Times(5)
479 .WillRepeatedly(Return("foo.bar"));
480
481 auto inPDRRepo = pldm_pdr_init();
482 auto numericEffecterPdrRepo = pldm_pdr_init();
483 Repo numericEffecterPDRs(numericEffecterPdrRepo);
484 auto event = sdeventplus::Event::get_default();
485 Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good", inPDRRepo,
486 nullptr, nullptr, nullptr, nullptr, event);
487 Repo inRepo(inPDRRepo);
488 getRepoByType(inRepo, numericEffecterPDRs, PLDM_NUMERIC_EFFECTER_PDR);
489
490 pdr_utils::PdrEntry e;
491 auto record4 = pdr::getRecordByHandle(numericEffecterPDRs, 4, e);
492 ASSERT_NE(record4, nullptr);
493
494 pldm_numeric_effecter_value_pdr* pdr =
495 reinterpret_cast<pldm_numeric_effecter_value_pdr*>(e.data);
496 EXPECT_EQ(pdr->hdr.type, PLDM_NUMERIC_EFFECTER_PDR);
497
498 uint16_t effecterId = 4;
499
500 uint8_t effecterDataSize{};
501 pldm::utils::PropertyValue dbusValue;
502 std::string propertyType;
503
504 auto rc = platform_numeric_effecter::getNumericEffecterData<MockdBusHandler,
505 Handler>(
506 mockedUtils, handler, effecterId, effecterDataSize, propertyType,
507 dbusValue);
508
509 ASSERT_EQ(rc, 128);
510
511 pldm_pdr_destroy(inPDRRepo);
512 pldm_pdr_destroy(numericEffecterPdrRepo);
513}
514
Tom Josephb4268602020-04-17 17:20:45 +0530515TEST(parseStateSensor, allScenarios)
516{
517 // Sample state sensor with SensorID - 1, EntityType - Processor Module(67)
518 // State Set ID - Operational Running Status(11), Supported States - 3,4
519 std::vector<uint8_t> sample1PDR{0x00, 0x00, 0x00, 0x00, 0x01, 0x04, 0x00,
520 0x00, 0x17, 0x00, 0x00, 0x00, 0x01, 0x00,
521 0x43, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
522 0x00, 0x01, 0x0b, 0x00, 0x01, 0x18};
523
Patrick Williams6da4f912023-05-10 07:50:53 -0500524 const auto& [terminusHandle1, sensorID1,
525 sensorInfo1] = parseStateSensorPDR(sample1PDR);
526 const auto& [containerID1, entityType1,
527 entityInstance1] = std::get<0>(sensorInfo1);
Tom Josephb4268602020-04-17 17:20:45 +0530528 const auto& states1 = std::get<1>(sensorInfo1);
529 CompositeSensorStates statesCmp1{{3u, 4u}};
530
531 ASSERT_EQ(le16toh(terminusHandle1), 0u);
532 ASSERT_EQ(le16toh(sensorID1), 1u);
533 ASSERT_EQ(le16toh(containerID1), 0u);
534 ASSERT_EQ(le16toh(entityType1), 67u);
535 ASSERT_EQ(le16toh(entityInstance1), 1u);
536 ASSERT_EQ(states1, statesCmp1);
537
538 // Sample state sensor with SensorID - 2, EntityType - System Firmware(31)
539 // State Set ID - Availability(2), Supported States - 3,4,9,10,11,13
540 std::vector<uint8_t> sample2PDR{0x00, 0x00, 0x00, 0x00, 0x01, 0x04, 0x00,
541 0x00, 0x17, 0x00, 0x00, 0x00, 0x02, 0x00,
542 0x1F, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
543 0x00, 0x01, 0x02, 0x00, 0x02, 0x18, 0x2E};
544
Patrick Williams6da4f912023-05-10 07:50:53 -0500545 const auto& [terminusHandle2, sensorID2,
546 sensorInfo2] = parseStateSensorPDR(sample2PDR);
547 const auto& [containerID2, entityType2,
548 entityInstance2] = std::get<0>(sensorInfo2);
Tom Josephb4268602020-04-17 17:20:45 +0530549 const auto& states2 = std::get<1>(sensorInfo2);
550 CompositeSensorStates statesCmp2{{3u, 4u, 9u, 10u, 11u, 13u}};
551
552 ASSERT_EQ(le16toh(terminusHandle2), 0u);
553 ASSERT_EQ(le16toh(sensorID2), 2u);
554 ASSERT_EQ(le16toh(containerID2), 0u);
555 ASSERT_EQ(le16toh(entityType2), 31u);
556 ASSERT_EQ(le16toh(entityInstance2), 1u);
557 ASSERT_EQ(states2, statesCmp2);
558
559 // Sample state sensor with SensorID - 3, EntityType - Virtual Machine
560 // Manager(33), Composite State Sensor -2 , State Set ID - Link State(33),
561 // Supported States - 1,2, State Set ID - Configuration State(15),
562 // Supported States - 1,2,3,4
563 std::vector<uint8_t> sample3PDR{
564 0x00, 0x00, 0x00, 0x00, 0x01, 0x04, 0x00, 0x00, 0x17, 0x00, 0x00,
565 0x00, 0x03, 0x00, 0x21, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x00,
566 0x02, 0x21, 0x00, 0x01, 0x06, 0x0F, 0x00, 0x01, 0x1E};
567
Patrick Williams6da4f912023-05-10 07:50:53 -0500568 const auto& [terminusHandle3, sensorID3,
569 sensorInfo3] = parseStateSensorPDR(sample3PDR);
570 const auto& [containerID3, entityType3,
571 entityInstance3] = std::get<0>(sensorInfo3);
Tom Josephb4268602020-04-17 17:20:45 +0530572 const auto& states3 = std::get<1>(sensorInfo3);
573 CompositeSensorStates statesCmp3{{1u, 2u}, {1u, 2u, 3u, 4u}};
574
575 ASSERT_EQ(le16toh(terminusHandle3), 0u);
576 ASSERT_EQ(le16toh(sensorID3), 3u);
577 ASSERT_EQ(le16toh(containerID3), 1u);
578 ASSERT_EQ(le16toh(entityType3), 33u);
579 ASSERT_EQ(le16toh(entityInstance3), 2u);
580 ASSERT_EQ(states3, statesCmp3);
581}
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530582
583TEST(StateSensorHandler, allScenarios)
584{
585 using namespace pldm::responder::events;
586
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530587 StateSensorHandler handler{"./event_jsons/good"};
588 constexpr uint8_t eventState0 = 0;
589 constexpr uint8_t eventState1 = 1;
590 constexpr uint8_t eventState2 = 2;
591 constexpr uint8_t eventState3 = 3;
592
593 // Event Entry 1
594 {
595 StateSensorEntry entry{1, 64, 1, 0};
596 const auto& [dbusMapping, eventStateMap] = handler.getEventInfo(entry);
597 DBusMapping mapping{"/xyz/abc/def",
598 "xyz.openbmc_project.example1.value", "value1",
599 "string"};
600 ASSERT_EQ(mapping == dbusMapping, true);
601
602 const auto& propValue0 = eventStateMap.at(eventState0);
603 const auto& propValue1 = eventStateMap.at(eventState1);
604 const auto& propValue2 = eventStateMap.at(eventState2);
605 PropertyValue value0{std::in_place_type<std::string>,
606 "xyz.openbmc_project.State.Normal"};
607 PropertyValue value1{std::in_place_type<std::string>,
608 "xyz.openbmc_project.State.Critical"};
609 PropertyValue value2{std::in_place_type<std::string>,
610 "xyz.openbmc_project.State.Fatal"};
611 ASSERT_EQ(value0 == propValue0, true);
612 ASSERT_EQ(value1 == propValue1, true);
613 ASSERT_EQ(value2 == propValue2, true);
614 }
615
616 // Event Entry 2
617 {
618 StateSensorEntry entry{1, 64, 1, 1};
619 const auto& [dbusMapping, eventStateMap] = handler.getEventInfo(entry);
620 DBusMapping mapping{"/xyz/abc/def",
621 "xyz.openbmc_project.example2.value", "value2",
622 "uint8_t"};
623 ASSERT_EQ(mapping == dbusMapping, true);
624
625 const auto& propValue0 = eventStateMap.at(eventState2);
626 const auto& propValue1 = eventStateMap.at(eventState3);
627 PropertyValue value0{std::in_place_type<uint8_t>, 9};
628 PropertyValue value1{std::in_place_type<uint8_t>, 10};
629 ASSERT_EQ(value0 == propValue0, true);
630 ASSERT_EQ(value1 == propValue1, true);
631 }
632
633 // Event Entry 3
634 {
635 StateSensorEntry entry{2, 67, 2, 0};
636 const auto& [dbusMapping, eventStateMap] = handler.getEventInfo(entry);
637 DBusMapping mapping{"/xyz/abc/ghi",
638 "xyz.openbmc_project.example3.value", "value3",
639 "bool"};
640 ASSERT_EQ(mapping == dbusMapping, true);
641
642 const auto& propValue0 = eventStateMap.at(eventState0);
643 const auto& propValue1 = eventStateMap.at(eventState1);
644 PropertyValue value0{std::in_place_type<bool>, false};
645 PropertyValue value1{std::in_place_type<bool>, true};
646 ASSERT_EQ(value0 == propValue0, true);
647 ASSERT_EQ(value1 == propValue1, true);
648 }
649
650 // Invalid Entry
651 {
652 StateSensorEntry entry{0, 0, 0, 0};
653 ASSERT_THROW(handler.getEventInfo(entry), std::out_of_range);
654 }
655}
Sampa Misra12afe112020-05-25 11:40:44 -0500656
657TEST(TerminusLocatorPDR, BMCTerminusLocatorPDR)
658{
659 auto inPDRRepo = pldm_pdr_init();
660 auto outPDRRepo = pldm_pdr_init();
661 Repo outRepo(outPDRRepo);
662 MockdBusHandler mockedUtils;
Sampa Misra5fb37d52021-03-06 07:26:00 -0600663 auto event = sdeventplus::Event::get_default();
Sampa Misraaea5dde2020-08-31 08:33:47 -0500664 Handler handler(&mockedUtils, "", inPDRRepo, nullptr, nullptr, nullptr,
Sampa Misra5fb37d52021-03-06 07:26:00 -0600665 nullptr, event);
Sampa Misra12afe112020-05-25 11:40:44 -0500666 Repo inRepo(inPDRRepo);
667 getRepoByType(inRepo, outRepo, PLDM_TERMINUS_LOCATOR_PDR);
668
669 // 1 BMC terminus locator PDR in the PDR repository
670 ASSERT_EQ(outRepo.getRecordCount(), 1);
671
672 pdr_utils::PdrEntry entry;
673 auto record = pdr::getRecordByHandle(outRepo, 1, entry);
674 ASSERT_NE(record, nullptr);
675
676 auto pdr = reinterpret_cast<const pldm_terminus_locator_pdr*>(entry.data);
677 EXPECT_EQ(pdr->hdr.record_handle, 1);
678 EXPECT_EQ(pdr->hdr.version, 1);
679 EXPECT_EQ(pdr->hdr.type, PLDM_TERMINUS_LOCATOR_PDR);
680 EXPECT_EQ(pdr->hdr.record_change_num, 0);
681 EXPECT_EQ(pdr->hdr.length,
682 sizeof(pldm_terminus_locator_pdr) - sizeof(pldm_pdr_hdr));
Manojkiran Edacc5f1582021-09-29 17:03:06 +0530683 EXPECT_EQ(pdr->terminus_handle, TERMINUS_HANDLE);
Sampa Misra12afe112020-05-25 11:40:44 -0500684 EXPECT_EQ(pdr->validity, PLDM_TL_PDR_VALID);
Manojkiran Edacc5f1582021-09-29 17:03:06 +0530685 EXPECT_EQ(pdr->tid, TERMINUS_ID);
Sampa Misra12afe112020-05-25 11:40:44 -0500686 EXPECT_EQ(pdr->container_id, 0);
687 EXPECT_EQ(pdr->terminus_locator_type, PLDM_TERMINUS_LOCATOR_TYPE_MCTP_EID);
688 EXPECT_EQ(pdr->terminus_locator_value_size,
689 sizeof(pldm_terminus_locator_type_mctp_eid));
690 auto locatorValue =
691 reinterpret_cast<const pldm_terminus_locator_type_mctp_eid*>(
692 pdr->terminus_locator_value);
693 EXPECT_EQ(locatorValue->eid, BmcMctpEid);
George Liua9170122020-05-14 11:59:13 +0800694 pldm_pdr_destroy(inPDRRepo);
695 pldm_pdr_destroy(outPDRRepo);
696}
697
698TEST(getStateSensorReadingsHandler, testGoodRequest)
699{
700 MockdBusHandler mockedUtils;
701 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
702 .Times(1)
703 .WillRepeatedly(Return("foo.bar"));
704
705 auto inPDRRepo = pldm_pdr_init();
706 auto outPDRRepo = pldm_pdr_init();
707 Repo outRepo(outPDRRepo);
Sampa Misra5fb37d52021-03-06 07:26:00 -0600708 auto event = sdeventplus::Event::get_default();
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600709 Handler handler(&mockedUtils, "./pdr_jsons/state_sensor/good", inPDRRepo,
Sampa Misra5fb37d52021-03-06 07:26:00 -0600710 nullptr, nullptr, nullptr, nullptr, event);
George Liua9170122020-05-14 11:59:13 +0800711 Repo inRepo(inPDRRepo);
712 getRepoByType(inRepo, outRepo, PLDM_STATE_SENSOR_PDR);
713 pdr_utils::PdrEntry e;
714 auto record = pdr::getRecordByHandle(outRepo, 2, e);
715 ASSERT_NE(record, nullptr);
716 pldm_state_sensor_pdr* pdr =
717 reinterpret_cast<pldm_state_sensor_pdr*>(e.data);
718 EXPECT_EQ(pdr->hdr.type, PLDM_STATE_SENSOR_PDR);
719
720 std::vector<get_sensor_state_field> stateField;
721 uint8_t compSensorCnt{};
722 uint8_t sensorRearmCnt = 1;
723
724 MockdBusHandler handlerObj;
725 EXPECT_CALL(handlerObj,
726 getDbusPropertyVariant(StrEq("/foo/bar"), StrEq("propertyName"),
727 StrEq("xyz.openbmc_project.Foo.Bar")))
728 .WillOnce(Return(
729 PropertyValue(std::string("xyz.openbmc_project.Foo.Bar.V0"))));
730
731 auto rc = platform_state_sensor::getStateSensorReadingsHandler<
732 MockdBusHandler, Handler>(handlerObj, handler, 0x1, sensorRearmCnt,
733 compSensorCnt, stateField);
734 ASSERT_EQ(rc, 0);
735 ASSERT_EQ(compSensorCnt, 1);
George Liu916808c2021-01-19 17:56:42 +0800736 ASSERT_EQ(stateField[0].sensor_op_state, PLDM_SENSOR_UNAVAILABLE);
737 ASSERT_EQ(stateField[0].present_state, PLDM_SENSOR_NORMAL);
George Liua9170122020-05-14 11:59:13 +0800738 ASSERT_EQ(stateField[0].previous_state, PLDM_SENSOR_UNKNOWN);
George Liu916808c2021-01-19 17:56:42 +0800739 ASSERT_EQ(stateField[0].event_state, PLDM_SENSOR_UNKNOWN);
George Liua9170122020-05-14 11:59:13 +0800740
741 pldm_pdr_destroy(inPDRRepo);
742 pldm_pdr_destroy(outPDRRepo);
743}
744
745TEST(getStateSensorReadingsHandler, testBadRequest)
746{
747 MockdBusHandler mockedUtils;
748 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
749 .Times(1)
750 .WillRepeatedly(Return("foo.bar"));
751
752 auto inPDRRepo = pldm_pdr_init();
753 auto outPDRRepo = pldm_pdr_init();
754 Repo outRepo(outPDRRepo);
Sampa Misra5fb37d52021-03-06 07:26:00 -0600755 auto event = sdeventplus::Event::get_default();
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600756 Handler handler(&mockedUtils, "./pdr_jsons/state_sensor/good", inPDRRepo,
Sampa Misra5fb37d52021-03-06 07:26:00 -0600757 nullptr, nullptr, nullptr, nullptr, event);
George Liua9170122020-05-14 11:59:13 +0800758 Repo inRepo(inPDRRepo);
759 getRepoByType(inRepo, outRepo, PLDM_STATE_SENSOR_PDR);
760 pdr_utils::PdrEntry e;
761 auto record = pdr::getRecordByHandle(outRepo, 2, e);
762 ASSERT_NE(record, nullptr);
763 pldm_state_sensor_pdr* pdr =
764 reinterpret_cast<pldm_state_sensor_pdr*>(e.data);
765 EXPECT_EQ(pdr->hdr.type, PLDM_STATE_SENSOR_PDR);
766
767 std::vector<get_sensor_state_field> stateField;
768 uint8_t compSensorCnt{};
769 uint8_t sensorRearmCnt = 3;
770
771 MockdBusHandler handlerObj;
772 auto rc = platform_state_sensor::getStateSensorReadingsHandler<
773 MockdBusHandler, Handler>(handlerObj, handler, 0x1, sensorRearmCnt,
774 compSensorCnt, stateField);
775 ASSERT_EQ(rc, PLDM_PLATFORM_REARM_UNAVAILABLE_IN_PRESENT_STATE);
Sampa Misra12afe112020-05-25 11:40:44 -0500776
777 pldm_pdr_destroy(inPDRRepo);
778 pldm_pdr_destroy(outPDRRepo);
779}