blob: 0c9b32410288483bf056db812b2b48af467fed3a [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);
Sampa Misraaa8ae722019-12-12 03:20:40 -0600200 std::cerr << "PDR next record handle " << handle << "\n";
George Liue53193f2020-02-24 09:23:26 +0800201 std::cerr << "PDR type " << hdr->type << "\n";
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530202 if (hdr->type == PLDM_STATE_EFFECTER_PDR)
203 {
204 pldm_state_effecter_pdr* pdr =
George Liue53193f2020-02-24 09:23:26 +0800205 reinterpret_cast<pldm_state_effecter_pdr*>(resp->record_data);
Sampa Misraaa8ae722019-12-12 03:20:40 -0600206 std::cerr << "PDR entity type " << pdr->entity_type << "\n";
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();
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600240 Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good", inPDRRepo,
Sampa Misra5fb37d52021-03-06 07:26:00 -0600241 nullptr, nullptr, nullptr, nullptr, event);
George Liua9170122020-05-14 11:59:13 +0800242 handler.getPDR(req, requestPayloadLength);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600243 Repo inRepo(inPDRRepo);
244 getRepoByType(inRepo, outRepo, PLDM_STATE_EFFECTER_PDR);
George Liue53193f2020-02-24 09:23:26 +0800245 pdr_utils::PdrEntry e;
Sampa Misra12afe112020-05-25 11:40:44 -0500246 auto record1 = pdr::getRecordByHandle(outRepo, 2, e);
George Liue53193f2020-02-24 09:23:26 +0800247 ASSERT_NE(record1, nullptr);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500248 pldm_state_effecter_pdr* pdr =
George Liue53193f2020-02-24 09:23:26 +0800249 reinterpret_cast<pldm_state_effecter_pdr*>(e.data);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500250 EXPECT_EQ(pdr->hdr.type, PLDM_STATE_EFFECTER_PDR);
251
252 std::vector<set_effecter_state_field> stateField;
253 stateField.push_back({PLDM_REQUEST_SET, 1});
254 stateField.push_back({PLDM_REQUEST_SET, 1});
George Liu1ec85d42020-02-12 16:05:32 +0800255 std::string value = "xyz.openbmc_project.Foo.Bar.V1";
George Liu1e44c732020-02-28 20:20:06 +0800256 PropertyValue propertyValue = value;
Sampa Misraa2fa0702019-05-31 01:28:55 -0500257
George Liu1ec85d42020-02-12 16:05:32 +0800258 DBusMapping dbusMapping{"/foo/bar", "xyz.openbmc_project.Foo.Bar",
259 "propertyName", "string"};
George Liu1e44c732020-02-28 20:20:06 +0800260
George Liu36e81352020-07-01 14:40:30 +0800261 EXPECT_CALL(mockedUtils, setDbusProperty(dbusMapping, propertyValue))
Sampa Misraa2fa0702019-05-31 01:28:55 -0500262 .Times(2);
George Liu0d7aca82020-03-30 15:01:36 +0800263 auto rc = platform_state_effecter::setStateEffecterStatesHandler<
George Liu36e81352020-07-01 14:40:30 +0800264 MockdBusHandler, Handler>(mockedUtils, handler, 0x1, stateField);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500265 ASSERT_EQ(rc, 0);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600266
267 pldm_pdr_destroy(inPDRRepo);
268 pldm_pdr_destroy(outPDRRepo);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500269}
270
271TEST(setStateEffecterStatesHandler, testBadRequest)
272{
George Liua9170122020-05-14 11:59:13 +0800273 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
274 requestPayload{};
275 auto req = reinterpret_cast<pldm_msg*>(requestPayload.data());
276 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
277
George Liu36e81352020-07-01 14:40:30 +0800278 MockdBusHandler mockedUtils;
279 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
280 .Times(5)
281 .WillRepeatedly(Return("foo.bar"));
282
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600283 auto inPDRRepo = pldm_pdr_init();
284 auto outPDRRepo = pldm_pdr_init();
285 Repo outRepo(outPDRRepo);
Sampa Misra5fb37d52021-03-06 07:26:00 -0600286 auto event = sdeventplus::Event::get_default();
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600287 Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good", inPDRRepo,
Sampa Misra5fb37d52021-03-06 07:26:00 -0600288 nullptr, nullptr, nullptr, nullptr, event);
George Liua9170122020-05-14 11:59:13 +0800289 handler.getPDR(req, requestPayloadLength);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600290 Repo inRepo(inPDRRepo);
291 getRepoByType(inRepo, outRepo, PLDM_STATE_EFFECTER_PDR);
George Liue53193f2020-02-24 09:23:26 +0800292 pdr_utils::PdrEntry e;
Sampa Misra12afe112020-05-25 11:40:44 -0500293 auto record1 = pdr::getRecordByHandle(outRepo, 2, e);
George Liue53193f2020-02-24 09:23:26 +0800294 ASSERT_NE(record1, nullptr);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500295 pldm_state_effecter_pdr* pdr =
George Liue53193f2020-02-24 09:23:26 +0800296 reinterpret_cast<pldm_state_effecter_pdr*>(e.data);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500297 EXPECT_EQ(pdr->hdr.type, PLDM_STATE_EFFECTER_PDR);
298
299 std::vector<set_effecter_state_field> stateField;
300 stateField.push_back({PLDM_REQUEST_SET, 3});
301 stateField.push_back({PLDM_REQUEST_SET, 4});
302
George Liu0d7aca82020-03-30 15:01:36 +0800303 auto rc = platform_state_effecter::setStateEffecterStatesHandler<
George Liu36e81352020-07-01 14:40:30 +0800304 MockdBusHandler, Handler>(mockedUtils, handler, 0x1, stateField);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500305 ASSERT_EQ(rc, PLDM_PLATFORM_SET_EFFECTER_UNSUPPORTED_SENSORSTATE);
306
George Liu0d7aca82020-03-30 15:01:36 +0800307 rc = platform_state_effecter::setStateEffecterStatesHandler<MockdBusHandler,
308 Handler>(
George Liu36e81352020-07-01 14:40:30 +0800309 mockedUtils, handler, 0x9, stateField);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500310 ASSERT_EQ(rc, PLDM_PLATFORM_INVALID_EFFECTER_ID);
311
312 stateField.push_back({PLDM_REQUEST_SET, 4});
George Liu0d7aca82020-03-30 15:01:36 +0800313 rc = platform_state_effecter::setStateEffecterStatesHandler<MockdBusHandler,
314 Handler>(
George Liu36e81352020-07-01 14:40:30 +0800315 mockedUtils, handler, 0x1, stateField);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500316 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
317
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600318 pldm_pdr_destroy(inPDRRepo);
319 pldm_pdr_destroy(outPDRRepo);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500320}
George Liu64d26be2020-01-15 10:58:10 +0800321
322TEST(setNumericEffecterValueHandler, testGoodRequest)
323{
George Liu36e81352020-07-01 14:40:30 +0800324 MockdBusHandler mockedUtils;
325 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
326 .Times(5)
327 .WillRepeatedly(Return("foo.bar"));
328
George Liu64d26be2020-01-15 10:58:10 +0800329 auto inPDRRepo = pldm_pdr_init();
330 auto numericEffecterPdrRepo = pldm_pdr_init();
331 Repo numericEffecterPDRs(numericEffecterPdrRepo);
Sampa Misra5fb37d52021-03-06 07:26:00 -0600332 auto event = sdeventplus::Event::get_default();
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600333 Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good", inPDRRepo,
Sampa Misra5fb37d52021-03-06 07:26:00 -0600334 nullptr, nullptr, nullptr, nullptr, event);
George Liu64d26be2020-01-15 10:58:10 +0800335 Repo inRepo(inPDRRepo);
336 getRepoByType(inRepo, numericEffecterPDRs, PLDM_NUMERIC_EFFECTER_PDR);
337
338 pdr_utils::PdrEntry e;
Sampa Misra12afe112020-05-25 11:40:44 -0500339 auto record4 = pdr::getRecordByHandle(numericEffecterPDRs, 4, e);
340 ASSERT_NE(record4, nullptr);
George Liu64d26be2020-01-15 10:58:10 +0800341
342 pldm_numeric_effecter_value_pdr* pdr =
343 reinterpret_cast<pldm_numeric_effecter_value_pdr*>(e.data);
344 EXPECT_EQ(pdr->hdr.type, PLDM_NUMERIC_EFFECTER_PDR);
345
346 uint16_t effecterId = 3;
347 uint32_t effecterValue = 2100000000; // 2036-07-18 21:20:00
Pavithra Barithaya45cd16b2021-07-01 08:19:59 -0500348 PropertyValue propertyValue = static_cast<uint64_t>(effecterValue);
George Liu64d26be2020-01-15 10:58:10 +0800349
George Liu64d26be2020-01-15 10:58:10 +0800350 DBusMapping dbusMapping{"/foo/bar", "xyz.openbmc_project.Foo.Bar",
351 "propertyName", "uint64_t"};
George Liu36e81352020-07-01 14:40:30 +0800352 EXPECT_CALL(mockedUtils, setDbusProperty(dbusMapping, propertyValue))
George Liu64d26be2020-01-15 10:58:10 +0800353 .Times(1);
354
355 auto rc = platform_numeric_effecter::setNumericEffecterValueHandler<
356 MockdBusHandler, Handler>(
George Liu36e81352020-07-01 14:40:30 +0800357 mockedUtils, handler, effecterId, PLDM_EFFECTER_DATA_SIZE_UINT32,
George Liu64d26be2020-01-15 10:58:10 +0800358 reinterpret_cast<uint8_t*>(&effecterValue), 4);
359 ASSERT_EQ(rc, 0);
360
361 pldm_pdr_destroy(inPDRRepo);
362 pldm_pdr_destroy(numericEffecterPdrRepo);
363}
364
365TEST(setNumericEffecterValueHandler, testBadRequest)
366{
George Liu36e81352020-07-01 14:40:30 +0800367 MockdBusHandler mockedUtils;
368 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
369 .Times(5)
370 .WillRepeatedly(Return("foo.bar"));
371
George Liu64d26be2020-01-15 10:58:10 +0800372 auto inPDRRepo = pldm_pdr_init();
373 auto numericEffecterPdrRepo = pldm_pdr_init();
374 Repo numericEffecterPDRs(numericEffecterPdrRepo);
Sampa Misra5fb37d52021-03-06 07:26:00 -0600375 auto event = sdeventplus::Event::get_default();
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600376 Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good", inPDRRepo,
Sampa Misra5fb37d52021-03-06 07:26:00 -0600377 nullptr, nullptr, nullptr, nullptr, event);
George Liu64d26be2020-01-15 10:58:10 +0800378 Repo inRepo(inPDRRepo);
379 getRepoByType(inRepo, numericEffecterPDRs, PLDM_NUMERIC_EFFECTER_PDR);
380
381 pdr_utils::PdrEntry e;
Sampa Misra12afe112020-05-25 11:40:44 -0500382 auto record4 = pdr::getRecordByHandle(numericEffecterPDRs, 4, e);
383 ASSERT_NE(record4, nullptr);
George Liu64d26be2020-01-15 10:58:10 +0800384
385 pldm_numeric_effecter_value_pdr* pdr =
386 reinterpret_cast<pldm_numeric_effecter_value_pdr*>(e.data);
387 EXPECT_EQ(pdr->hdr.type, PLDM_NUMERIC_EFFECTER_PDR);
388
389 uint16_t effecterId = 3;
390 uint64_t effecterValue = 9876543210;
George Liu64d26be2020-01-15 10:58:10 +0800391 auto rc = platform_numeric_effecter::setNumericEffecterValueHandler<
392 MockdBusHandler, Handler>(
George Liu36e81352020-07-01 14:40:30 +0800393 mockedUtils, handler, effecterId, PLDM_EFFECTER_DATA_SIZE_SINT32,
George Liu64d26be2020-01-15 10:58:10 +0800394 reinterpret_cast<uint8_t*>(&effecterValue), 3);
395 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
396
397 pldm_pdr_destroy(inPDRRepo);
398 pldm_pdr_destroy(numericEffecterPdrRepo);
399}
Tom Josephb4268602020-04-17 17:20:45 +0530400
401TEST(parseStateSensor, allScenarios)
402{
403 // Sample state sensor with SensorID - 1, EntityType - Processor Module(67)
404 // State Set ID - Operational Running Status(11), Supported States - 3,4
405 std::vector<uint8_t> sample1PDR{0x00, 0x00, 0x00, 0x00, 0x01, 0x04, 0x00,
406 0x00, 0x17, 0x00, 0x00, 0x00, 0x01, 0x00,
407 0x43, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
408 0x00, 0x01, 0x0b, 0x00, 0x01, 0x18};
409
Patrick Williams6da4f912023-05-10 07:50:53 -0500410 const auto& [terminusHandle1, sensorID1,
411 sensorInfo1] = parseStateSensorPDR(sample1PDR);
412 const auto& [containerID1, entityType1,
413 entityInstance1] = std::get<0>(sensorInfo1);
Tom Josephb4268602020-04-17 17:20:45 +0530414 const auto& states1 = std::get<1>(sensorInfo1);
415 CompositeSensorStates statesCmp1{{3u, 4u}};
416
417 ASSERT_EQ(le16toh(terminusHandle1), 0u);
418 ASSERT_EQ(le16toh(sensorID1), 1u);
419 ASSERT_EQ(le16toh(containerID1), 0u);
420 ASSERT_EQ(le16toh(entityType1), 67u);
421 ASSERT_EQ(le16toh(entityInstance1), 1u);
422 ASSERT_EQ(states1, statesCmp1);
423
424 // Sample state sensor with SensorID - 2, EntityType - System Firmware(31)
425 // State Set ID - Availability(2), Supported States - 3,4,9,10,11,13
426 std::vector<uint8_t> sample2PDR{0x00, 0x00, 0x00, 0x00, 0x01, 0x04, 0x00,
427 0x00, 0x17, 0x00, 0x00, 0x00, 0x02, 0x00,
428 0x1F, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
429 0x00, 0x01, 0x02, 0x00, 0x02, 0x18, 0x2E};
430
Patrick Williams6da4f912023-05-10 07:50:53 -0500431 const auto& [terminusHandle2, sensorID2,
432 sensorInfo2] = parseStateSensorPDR(sample2PDR);
433 const auto& [containerID2, entityType2,
434 entityInstance2] = std::get<0>(sensorInfo2);
Tom Josephb4268602020-04-17 17:20:45 +0530435 const auto& states2 = std::get<1>(sensorInfo2);
436 CompositeSensorStates statesCmp2{{3u, 4u, 9u, 10u, 11u, 13u}};
437
438 ASSERT_EQ(le16toh(terminusHandle2), 0u);
439 ASSERT_EQ(le16toh(sensorID2), 2u);
440 ASSERT_EQ(le16toh(containerID2), 0u);
441 ASSERT_EQ(le16toh(entityType2), 31u);
442 ASSERT_EQ(le16toh(entityInstance2), 1u);
443 ASSERT_EQ(states2, statesCmp2);
444
445 // Sample state sensor with SensorID - 3, EntityType - Virtual Machine
446 // Manager(33), Composite State Sensor -2 , State Set ID - Link State(33),
447 // Supported States - 1,2, State Set ID - Configuration State(15),
448 // Supported States - 1,2,3,4
449 std::vector<uint8_t> sample3PDR{
450 0x00, 0x00, 0x00, 0x00, 0x01, 0x04, 0x00, 0x00, 0x17, 0x00, 0x00,
451 0x00, 0x03, 0x00, 0x21, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x00,
452 0x02, 0x21, 0x00, 0x01, 0x06, 0x0F, 0x00, 0x01, 0x1E};
453
Patrick Williams6da4f912023-05-10 07:50:53 -0500454 const auto& [terminusHandle3, sensorID3,
455 sensorInfo3] = parseStateSensorPDR(sample3PDR);
456 const auto& [containerID3, entityType3,
457 entityInstance3] = std::get<0>(sensorInfo3);
Tom Josephb4268602020-04-17 17:20:45 +0530458 const auto& states3 = std::get<1>(sensorInfo3);
459 CompositeSensorStates statesCmp3{{1u, 2u}, {1u, 2u, 3u, 4u}};
460
461 ASSERT_EQ(le16toh(terminusHandle3), 0u);
462 ASSERT_EQ(le16toh(sensorID3), 3u);
463 ASSERT_EQ(le16toh(containerID3), 1u);
464 ASSERT_EQ(le16toh(entityType3), 33u);
465 ASSERT_EQ(le16toh(entityInstance3), 2u);
466 ASSERT_EQ(states3, statesCmp3);
467}
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530468
469TEST(StateSensorHandler, allScenarios)
470{
471 using namespace pldm::responder::events;
472
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530473 StateSensorHandler handler{"./event_jsons/good"};
474 constexpr uint8_t eventState0 = 0;
475 constexpr uint8_t eventState1 = 1;
476 constexpr uint8_t eventState2 = 2;
477 constexpr uint8_t eventState3 = 3;
478
479 // Event Entry 1
480 {
481 StateSensorEntry entry{1, 64, 1, 0};
482 const auto& [dbusMapping, eventStateMap] = handler.getEventInfo(entry);
483 DBusMapping mapping{"/xyz/abc/def",
484 "xyz.openbmc_project.example1.value", "value1",
485 "string"};
486 ASSERT_EQ(mapping == dbusMapping, true);
487
488 const auto& propValue0 = eventStateMap.at(eventState0);
489 const auto& propValue1 = eventStateMap.at(eventState1);
490 const auto& propValue2 = eventStateMap.at(eventState2);
491 PropertyValue value0{std::in_place_type<std::string>,
492 "xyz.openbmc_project.State.Normal"};
493 PropertyValue value1{std::in_place_type<std::string>,
494 "xyz.openbmc_project.State.Critical"};
495 PropertyValue value2{std::in_place_type<std::string>,
496 "xyz.openbmc_project.State.Fatal"};
497 ASSERT_EQ(value0 == propValue0, true);
498 ASSERT_EQ(value1 == propValue1, true);
499 ASSERT_EQ(value2 == propValue2, true);
500 }
501
502 // Event Entry 2
503 {
504 StateSensorEntry entry{1, 64, 1, 1};
505 const auto& [dbusMapping, eventStateMap] = handler.getEventInfo(entry);
506 DBusMapping mapping{"/xyz/abc/def",
507 "xyz.openbmc_project.example2.value", "value2",
508 "uint8_t"};
509 ASSERT_EQ(mapping == dbusMapping, true);
510
511 const auto& propValue0 = eventStateMap.at(eventState2);
512 const auto& propValue1 = eventStateMap.at(eventState3);
513 PropertyValue value0{std::in_place_type<uint8_t>, 9};
514 PropertyValue value1{std::in_place_type<uint8_t>, 10};
515 ASSERT_EQ(value0 == propValue0, true);
516 ASSERT_EQ(value1 == propValue1, true);
517 }
518
519 // Event Entry 3
520 {
521 StateSensorEntry entry{2, 67, 2, 0};
522 const auto& [dbusMapping, eventStateMap] = handler.getEventInfo(entry);
523 DBusMapping mapping{"/xyz/abc/ghi",
524 "xyz.openbmc_project.example3.value", "value3",
525 "bool"};
526 ASSERT_EQ(mapping == dbusMapping, true);
527
528 const auto& propValue0 = eventStateMap.at(eventState0);
529 const auto& propValue1 = eventStateMap.at(eventState1);
530 PropertyValue value0{std::in_place_type<bool>, false};
531 PropertyValue value1{std::in_place_type<bool>, true};
532 ASSERT_EQ(value0 == propValue0, true);
533 ASSERT_EQ(value1 == propValue1, true);
534 }
535
536 // Invalid Entry
537 {
538 StateSensorEntry entry{0, 0, 0, 0};
539 ASSERT_THROW(handler.getEventInfo(entry), std::out_of_range);
540 }
541}
Sampa Misra12afe112020-05-25 11:40:44 -0500542
543TEST(TerminusLocatorPDR, BMCTerminusLocatorPDR)
544{
545 auto inPDRRepo = pldm_pdr_init();
546 auto outPDRRepo = pldm_pdr_init();
547 Repo outRepo(outPDRRepo);
548 MockdBusHandler mockedUtils;
Sampa Misra5fb37d52021-03-06 07:26:00 -0600549 auto event = sdeventplus::Event::get_default();
Sampa Misraaea5dde2020-08-31 08:33:47 -0500550 Handler handler(&mockedUtils, "", inPDRRepo, nullptr, nullptr, nullptr,
Sampa Misra5fb37d52021-03-06 07:26:00 -0600551 nullptr, event);
Sampa Misra12afe112020-05-25 11:40:44 -0500552 Repo inRepo(inPDRRepo);
553 getRepoByType(inRepo, outRepo, PLDM_TERMINUS_LOCATOR_PDR);
554
555 // 1 BMC terminus locator PDR in the PDR repository
556 ASSERT_EQ(outRepo.getRecordCount(), 1);
557
558 pdr_utils::PdrEntry entry;
559 auto record = pdr::getRecordByHandle(outRepo, 1, entry);
560 ASSERT_NE(record, nullptr);
561
562 auto pdr = reinterpret_cast<const pldm_terminus_locator_pdr*>(entry.data);
563 EXPECT_EQ(pdr->hdr.record_handle, 1);
564 EXPECT_EQ(pdr->hdr.version, 1);
565 EXPECT_EQ(pdr->hdr.type, PLDM_TERMINUS_LOCATOR_PDR);
566 EXPECT_EQ(pdr->hdr.record_change_num, 0);
567 EXPECT_EQ(pdr->hdr.length,
568 sizeof(pldm_terminus_locator_pdr) - sizeof(pldm_pdr_hdr));
Manojkiran Edacc5f1582021-09-29 17:03:06 +0530569 EXPECT_EQ(pdr->terminus_handle, TERMINUS_HANDLE);
Sampa Misra12afe112020-05-25 11:40:44 -0500570 EXPECT_EQ(pdr->validity, PLDM_TL_PDR_VALID);
Manojkiran Edacc5f1582021-09-29 17:03:06 +0530571 EXPECT_EQ(pdr->tid, TERMINUS_ID);
Sampa Misra12afe112020-05-25 11:40:44 -0500572 EXPECT_EQ(pdr->container_id, 0);
573 EXPECT_EQ(pdr->terminus_locator_type, PLDM_TERMINUS_LOCATOR_TYPE_MCTP_EID);
574 EXPECT_EQ(pdr->terminus_locator_value_size,
575 sizeof(pldm_terminus_locator_type_mctp_eid));
576 auto locatorValue =
577 reinterpret_cast<const pldm_terminus_locator_type_mctp_eid*>(
578 pdr->terminus_locator_value);
579 EXPECT_EQ(locatorValue->eid, BmcMctpEid);
George Liua9170122020-05-14 11:59:13 +0800580 pldm_pdr_destroy(inPDRRepo);
581 pldm_pdr_destroy(outPDRRepo);
582}
583
584TEST(getStateSensorReadingsHandler, testGoodRequest)
585{
586 MockdBusHandler mockedUtils;
587 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
588 .Times(1)
589 .WillRepeatedly(Return("foo.bar"));
590
591 auto inPDRRepo = pldm_pdr_init();
592 auto outPDRRepo = pldm_pdr_init();
593 Repo outRepo(outPDRRepo);
Sampa Misra5fb37d52021-03-06 07:26:00 -0600594 auto event = sdeventplus::Event::get_default();
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600595 Handler handler(&mockedUtils, "./pdr_jsons/state_sensor/good", inPDRRepo,
Sampa Misra5fb37d52021-03-06 07:26:00 -0600596 nullptr, nullptr, nullptr, nullptr, event);
George Liua9170122020-05-14 11:59:13 +0800597 Repo inRepo(inPDRRepo);
598 getRepoByType(inRepo, outRepo, PLDM_STATE_SENSOR_PDR);
599 pdr_utils::PdrEntry e;
600 auto record = pdr::getRecordByHandle(outRepo, 2, e);
601 ASSERT_NE(record, nullptr);
602 pldm_state_sensor_pdr* pdr =
603 reinterpret_cast<pldm_state_sensor_pdr*>(e.data);
604 EXPECT_EQ(pdr->hdr.type, PLDM_STATE_SENSOR_PDR);
605
606 std::vector<get_sensor_state_field> stateField;
607 uint8_t compSensorCnt{};
608 uint8_t sensorRearmCnt = 1;
609
610 MockdBusHandler handlerObj;
611 EXPECT_CALL(handlerObj,
612 getDbusPropertyVariant(StrEq("/foo/bar"), StrEq("propertyName"),
613 StrEq("xyz.openbmc_project.Foo.Bar")))
614 .WillOnce(Return(
615 PropertyValue(std::string("xyz.openbmc_project.Foo.Bar.V0"))));
616
617 auto rc = platform_state_sensor::getStateSensorReadingsHandler<
618 MockdBusHandler, Handler>(handlerObj, handler, 0x1, sensorRearmCnt,
619 compSensorCnt, stateField);
620 ASSERT_EQ(rc, 0);
621 ASSERT_EQ(compSensorCnt, 1);
George Liu916808c2021-01-19 17:56:42 +0800622 ASSERT_EQ(stateField[0].sensor_op_state, PLDM_SENSOR_UNAVAILABLE);
623 ASSERT_EQ(stateField[0].present_state, PLDM_SENSOR_NORMAL);
George Liua9170122020-05-14 11:59:13 +0800624 ASSERT_EQ(stateField[0].previous_state, PLDM_SENSOR_UNKNOWN);
George Liu916808c2021-01-19 17:56:42 +0800625 ASSERT_EQ(stateField[0].event_state, PLDM_SENSOR_UNKNOWN);
George Liua9170122020-05-14 11:59:13 +0800626
627 pldm_pdr_destroy(inPDRRepo);
628 pldm_pdr_destroy(outPDRRepo);
629}
630
631TEST(getStateSensorReadingsHandler, testBadRequest)
632{
633 MockdBusHandler mockedUtils;
634 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
635 .Times(1)
636 .WillRepeatedly(Return("foo.bar"));
637
638 auto inPDRRepo = pldm_pdr_init();
639 auto outPDRRepo = pldm_pdr_init();
640 Repo outRepo(outPDRRepo);
Sampa Misra5fb37d52021-03-06 07:26:00 -0600641 auto event = sdeventplus::Event::get_default();
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600642 Handler handler(&mockedUtils, "./pdr_jsons/state_sensor/good", inPDRRepo,
Sampa Misra5fb37d52021-03-06 07:26:00 -0600643 nullptr, nullptr, nullptr, nullptr, event);
George Liua9170122020-05-14 11:59:13 +0800644 Repo inRepo(inPDRRepo);
645 getRepoByType(inRepo, outRepo, PLDM_STATE_SENSOR_PDR);
646 pdr_utils::PdrEntry e;
647 auto record = pdr::getRecordByHandle(outRepo, 2, e);
648 ASSERT_NE(record, nullptr);
649 pldm_state_sensor_pdr* pdr =
650 reinterpret_cast<pldm_state_sensor_pdr*>(e.data);
651 EXPECT_EQ(pdr->hdr.type, PLDM_STATE_SENSOR_PDR);
652
653 std::vector<get_sensor_state_field> stateField;
654 uint8_t compSensorCnt{};
655 uint8_t sensorRearmCnt = 3;
656
657 MockdBusHandler handlerObj;
658 auto rc = platform_state_sensor::getStateSensorReadingsHandler<
659 MockdBusHandler, Handler>(handlerObj, handler, 0x1, sensorRearmCnt,
660 compSensorCnt, stateField);
661 ASSERT_EQ(rc, PLDM_PLATFORM_REARM_UNAVAILABLE_IN_PRESENT_STATE);
Sampa Misra12afe112020-05-25 11:40:44 -0500662
663 pldm_pdr_destroy(inPDRRepo);
664 pldm_pdr_destroy(outPDRRepo);
665}