blob: 1c561eadbcf79aac3dc6bda560ba576eec9a67aa [file] [log] [blame]
Deepak Kodihallid130e1a2020-06-17 05:55:32 -05001#include "common/utils.hpp"
TOM JOSEPHd4d97a52020-03-23 14:36:34 +05302#include "libpldmresponder/event_parser.hpp"
Deepak Kodihalli557dfb02019-05-12 13:11:17 +05303#include "libpldmresponder/pdr.hpp"
George Liue53193f2020-02-24 09:23:26 +08004#include "libpldmresponder/pdr_utils.hpp"
Deepak Kodihalli557dfb02019-05-12 13:11:17 +05305#include "libpldmresponder/platform.hpp"
George Liu64d26be2020-01-15 10:58:10 +08006#include "libpldmresponder/platform_numeric_effecter.hpp"
George Liu0d7aca82020-03-30 15:01:36 +08007#include "libpldmresponder/platform_state_effecter.hpp"
George Liua9170122020-05-14 11:59:13 +08008#include "libpldmresponder/platform_state_sensor.hpp"
George Liu1e44c732020-02-28 20:20:06 +08009#include "mocked_utils.hpp"
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053010
George Liu36e81352020-07-01 14:40:30 +080011#include <sdbusplus/test/sdbus_mock.hpp>
12
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053013#include <iostream>
14
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();
Deepak Kodihallib5c227e2020-07-13 06:58:34 -050042 Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good",
George Liucae18662020-05-15 09:32:57 +080043 "./event_jsons/good", pdrRepo, nullptr, nullptr, nullptr);
Deepak Kodihallic682fe22020-03-04 00:42:54 -060044 Repo repo(pdrRepo);
45 ASSERT_EQ(repo.empty(), false);
George Liue53193f2020-02-24 09:23:26 +080046 auto response = handler.getPDR(req, requestPayloadLength);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053047 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
48
George Liue53193f2020-02-24 09:23:26 +080049 struct pldm_get_pdr_resp* resp =
50 reinterpret_cast<struct pldm_get_pdr_resp*>(responsePtr->payload);
51 ASSERT_EQ(PLDM_SUCCESS, resp->completion_code);
52 ASSERT_EQ(2, resp->next_record_handle);
53 ASSERT_EQ(true, resp->response_count != 0);
54
55 pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(resp->record_data);
56 ASSERT_EQ(hdr->record_handle, 1);
57 ASSERT_EQ(hdr->version, 1);
Deepak Kodihallic682fe22020-03-04 00:42:54 -060058
59 pldm_pdr_destroy(pdrRepo);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053060}
61
62TEST(getPDR, testShortRead)
63{
64 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
65 requestPayload{};
George Liue53193f2020-02-24 09:23:26 +080066 auto req = reinterpret_cast<pldm_msg*>(requestPayload.data());
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053067 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
68
George Liue53193f2020-02-24 09:23:26 +080069 struct pldm_get_pdr_req* request =
70 reinterpret_cast<struct pldm_get_pdr_req*>(req->payload);
71 request->request_count = 1;
72
George Liu36e81352020-07-01 14:40:30 +080073 MockdBusHandler mockedUtils;
74 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
75 .Times(5)
76 .WillRepeatedly(Return("foo.bar"));
77
Deepak Kodihallic682fe22020-03-04 00:42:54 -060078 auto pdrRepo = pldm_pdr_init();
Deepak Kodihallib5c227e2020-07-13 06:58:34 -050079 Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good",
George Liucae18662020-05-15 09:32:57 +080080 "./event_jsons/good", pdrRepo, nullptr, nullptr, nullptr);
Deepak Kodihallic682fe22020-03-04 00:42:54 -060081 Repo repo(pdrRepo);
82 ASSERT_EQ(repo.empty(), false);
George Liue53193f2020-02-24 09:23:26 +080083 auto response = handler.getPDR(req, requestPayloadLength);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053084 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
George Liue53193f2020-02-24 09:23:26 +080085 struct pldm_get_pdr_resp* resp =
86 reinterpret_cast<struct pldm_get_pdr_resp*>(responsePtr->payload);
87 ASSERT_EQ(PLDM_SUCCESS, resp->completion_code);
88 ASSERT_EQ(1, resp->response_count);
Deepak Kodihallic682fe22020-03-04 00:42:54 -060089 pldm_pdr_destroy(pdrRepo);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053090}
91
92TEST(getPDR, testBadRecordHandle)
93{
94 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
95 requestPayload{};
George Liue53193f2020-02-24 09:23:26 +080096 auto req = reinterpret_cast<pldm_msg*>(requestPayload.data());
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053097 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
98
George Liue53193f2020-02-24 09:23:26 +080099 struct pldm_get_pdr_req* request =
100 reinterpret_cast<struct pldm_get_pdr_req*>(req->payload);
101 request->record_handle = 100000;
102 request->request_count = 1;
103
George Liu36e81352020-07-01 14:40:30 +0800104 MockdBusHandler mockedUtils;
105 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
106 .Times(5)
107 .WillRepeatedly(Return("foo.bar"));
108
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600109 auto pdrRepo = pldm_pdr_init();
Deepak Kodihallib5c227e2020-07-13 06:58:34 -0500110 Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good",
George Liucae18662020-05-15 09:32:57 +0800111 "./event_jsons/good", pdrRepo, nullptr, nullptr, nullptr);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600112 Repo repo(pdrRepo);
113 ASSERT_EQ(repo.empty(), false);
George Liue53193f2020-02-24 09:23:26 +0800114 auto response = handler.getPDR(req, requestPayloadLength);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530115 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
116
117 ASSERT_EQ(responsePtr->payload[0], PLDM_PLATFORM_INVALID_RECORD_HANDLE);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600118
119 pldm_pdr_destroy(pdrRepo);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530120}
121
122TEST(getPDR, testNoNextRecord)
123{
124 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
125 requestPayload{};
George Liue53193f2020-02-24 09:23:26 +0800126 auto req = reinterpret_cast<pldm_msg*>(requestPayload.data());
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530127 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
128
George Liue53193f2020-02-24 09:23:26 +0800129 struct pldm_get_pdr_req* request =
130 reinterpret_cast<struct pldm_get_pdr_req*>(req->payload);
131 request->record_handle = 1;
132
George Liu36e81352020-07-01 14:40:30 +0800133 MockdBusHandler mockedUtils;
134 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
135 .Times(5)
136 .WillRepeatedly(Return("foo.bar"));
137
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600138 auto pdrRepo = pldm_pdr_init();
Deepak Kodihallib5c227e2020-07-13 06:58:34 -0500139 Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good",
George Liucae18662020-05-15 09:32:57 +0800140 "./event_jsons/good", pdrRepo, nullptr, nullptr, nullptr);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600141 Repo repo(pdrRepo);
142 ASSERT_EQ(repo.empty(), false);
George Liue53193f2020-02-24 09:23:26 +0800143 auto response = handler.getPDR(req, requestPayloadLength);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530144 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
George Liue53193f2020-02-24 09:23:26 +0800145 struct pldm_get_pdr_resp* resp =
146 reinterpret_cast<struct pldm_get_pdr_resp*>(responsePtr->payload);
147 ASSERT_EQ(PLDM_SUCCESS, resp->completion_code);
148 ASSERT_EQ(2, resp->next_record_handle);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600149
150 pldm_pdr_destroy(pdrRepo);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530151}
152
153TEST(getPDR, testFindPDR)
154{
155 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
156 requestPayload{};
George Liue53193f2020-02-24 09:23:26 +0800157 auto req = reinterpret_cast<pldm_msg*>(requestPayload.data());
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530158 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
159
George Liue53193f2020-02-24 09:23:26 +0800160 struct pldm_get_pdr_req* request =
161 reinterpret_cast<struct pldm_get_pdr_req*>(req->payload);
162 request->request_count = 100;
163
George Liu36e81352020-07-01 14:40:30 +0800164 MockdBusHandler mockedUtils;
165 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
166 .Times(5)
167 .WillRepeatedly(Return("foo.bar"));
168
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600169 auto pdrRepo = pldm_pdr_init();
Deepak Kodihallib5c227e2020-07-13 06:58:34 -0500170 Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good",
George Liucae18662020-05-15 09:32:57 +0800171 "./event_jsons/good", pdrRepo, nullptr, nullptr, nullptr);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600172 Repo repo(pdrRepo);
173 ASSERT_EQ(repo.empty(), false);
George Liue53193f2020-02-24 09:23:26 +0800174 auto response = handler.getPDR(req, requestPayloadLength);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530175
176 // Let's try to find a PDR of type stateEffecter (= 11) and entity type =
177 // 100
178 bool found = false;
179 uint32_t handle = 0; // start asking for PDRs from recordHandle 0
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530180 while (!found)
181 {
George Liue53193f2020-02-24 09:23:26 +0800182 request->record_handle = handle;
George Liue53193f2020-02-24 09:23:26 +0800183 auto response = handler.getPDR(req, requestPayloadLength);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530184 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
George Liue53193f2020-02-24 09:23:26 +0800185 struct pldm_get_pdr_resp* resp =
186 reinterpret_cast<struct pldm_get_pdr_resp*>(responsePtr->payload);
187 ASSERT_EQ(PLDM_SUCCESS, resp->completion_code);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530188
George Liue53193f2020-02-24 09:23:26 +0800189 handle = resp->next_record_handle; // point to the next pdr in case
190 // current is not what we want
191
192 pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(resp->record_data);
Sampa Misraaa8ae722019-12-12 03:20:40 -0600193 std::cerr << "PDR next record handle " << handle << "\n";
George Liue53193f2020-02-24 09:23:26 +0800194 std::cerr << "PDR type " << hdr->type << "\n";
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530195 if (hdr->type == PLDM_STATE_EFFECTER_PDR)
196 {
197 pldm_state_effecter_pdr* pdr =
George Liue53193f2020-02-24 09:23:26 +0800198 reinterpret_cast<pldm_state_effecter_pdr*>(resp->record_data);
Sampa Misraaa8ae722019-12-12 03:20:40 -0600199 std::cerr << "PDR entity type " << pdr->entity_type << "\n";
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530200 if (pdr->entity_type == 100)
201 {
202 found = true;
203 // Rest of the PDR can be accessed as need be
204 break;
205 }
206 }
George Liue53193f2020-02-24 09:23:26 +0800207 if (!resp->next_record_handle) // no more records
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530208 {
209 break;
210 }
211 }
212 ASSERT_EQ(found, true);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600213
214 pldm_pdr_destroy(pdrRepo);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530215}
Sampa Misraa2fa0702019-05-31 01:28:55 -0500216
Sampa Misraa2fa0702019-05-31 01:28:55 -0500217TEST(setStateEffecterStatesHandler, testGoodRequest)
218{
George Liua9170122020-05-14 11:59:13 +0800219 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
220 requestPayload{};
221 auto req = reinterpret_cast<pldm_msg*>(requestPayload.data());
222 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
223
George Liu36e81352020-07-01 14:40:30 +0800224 MockdBusHandler mockedUtils;
225 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
226 .Times(5)
227 .WillRepeatedly(Return("foo.bar"));
228
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600229 auto inPDRRepo = pldm_pdr_init();
230 auto outPDRRepo = pldm_pdr_init();
231 Repo outRepo(outPDRRepo);
Deepak Kodihallib5c227e2020-07-13 06:58:34 -0500232 Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good",
George Liucae18662020-05-15 09:32:57 +0800233 "./event_jsons/good", inPDRRepo, nullptr, nullptr, nullptr);
George Liua9170122020-05-14 11:59:13 +0800234 handler.getPDR(req, requestPayloadLength);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600235 Repo inRepo(inPDRRepo);
236 getRepoByType(inRepo, outRepo, PLDM_STATE_EFFECTER_PDR);
George Liue53193f2020-02-24 09:23:26 +0800237 pdr_utils::PdrEntry e;
Sampa Misra12afe112020-05-25 11:40:44 -0500238 auto record1 = pdr::getRecordByHandle(outRepo, 2, e);
George Liue53193f2020-02-24 09:23:26 +0800239 ASSERT_NE(record1, nullptr);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500240 pldm_state_effecter_pdr* pdr =
George Liue53193f2020-02-24 09:23:26 +0800241 reinterpret_cast<pldm_state_effecter_pdr*>(e.data);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500242 EXPECT_EQ(pdr->hdr.type, PLDM_STATE_EFFECTER_PDR);
243
244 std::vector<set_effecter_state_field> stateField;
245 stateField.push_back({PLDM_REQUEST_SET, 1});
246 stateField.push_back({PLDM_REQUEST_SET, 1});
George Liu1ec85d42020-02-12 16:05:32 +0800247 std::string value = "xyz.openbmc_project.Foo.Bar.V1";
George Liu1e44c732020-02-28 20:20:06 +0800248 PropertyValue propertyValue = value;
Sampa Misraa2fa0702019-05-31 01:28:55 -0500249
George Liu1ec85d42020-02-12 16:05:32 +0800250 DBusMapping dbusMapping{"/foo/bar", "xyz.openbmc_project.Foo.Bar",
251 "propertyName", "string"};
George Liu1e44c732020-02-28 20:20:06 +0800252
George Liu36e81352020-07-01 14:40:30 +0800253 EXPECT_CALL(mockedUtils, setDbusProperty(dbusMapping, propertyValue))
Sampa Misraa2fa0702019-05-31 01:28:55 -0500254 .Times(2);
George Liu0d7aca82020-03-30 15:01:36 +0800255 auto rc = platform_state_effecter::setStateEffecterStatesHandler<
George Liu36e81352020-07-01 14:40:30 +0800256 MockdBusHandler, Handler>(mockedUtils, handler, 0x1, stateField);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500257 ASSERT_EQ(rc, 0);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600258
259 pldm_pdr_destroy(inPDRRepo);
260 pldm_pdr_destroy(outPDRRepo);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500261}
262
263TEST(setStateEffecterStatesHandler, testBadRequest)
264{
George Liua9170122020-05-14 11:59:13 +0800265 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
266 requestPayload{};
267 auto req = reinterpret_cast<pldm_msg*>(requestPayload.data());
268 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
269
George Liu36e81352020-07-01 14:40:30 +0800270 MockdBusHandler mockedUtils;
271 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
272 .Times(5)
273 .WillRepeatedly(Return("foo.bar"));
274
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600275 auto inPDRRepo = pldm_pdr_init();
276 auto outPDRRepo = pldm_pdr_init();
277 Repo outRepo(outPDRRepo);
Deepak Kodihallib5c227e2020-07-13 06:58:34 -0500278 Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good",
George Liucae18662020-05-15 09:32:57 +0800279 "./event_jsons/good", inPDRRepo, nullptr, nullptr, nullptr);
George Liua9170122020-05-14 11:59:13 +0800280 handler.getPDR(req, requestPayloadLength);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600281 Repo inRepo(inPDRRepo);
282 getRepoByType(inRepo, outRepo, PLDM_STATE_EFFECTER_PDR);
George Liue53193f2020-02-24 09:23:26 +0800283 pdr_utils::PdrEntry e;
Sampa Misra12afe112020-05-25 11:40:44 -0500284 auto record1 = pdr::getRecordByHandle(outRepo, 2, e);
George Liue53193f2020-02-24 09:23:26 +0800285 ASSERT_NE(record1, nullptr);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500286 pldm_state_effecter_pdr* pdr =
George Liue53193f2020-02-24 09:23:26 +0800287 reinterpret_cast<pldm_state_effecter_pdr*>(e.data);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500288 EXPECT_EQ(pdr->hdr.type, PLDM_STATE_EFFECTER_PDR);
289
290 std::vector<set_effecter_state_field> stateField;
291 stateField.push_back({PLDM_REQUEST_SET, 3});
292 stateField.push_back({PLDM_REQUEST_SET, 4});
293
George Liu0d7aca82020-03-30 15:01:36 +0800294 auto rc = platform_state_effecter::setStateEffecterStatesHandler<
George Liu36e81352020-07-01 14:40:30 +0800295 MockdBusHandler, Handler>(mockedUtils, handler, 0x1, stateField);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500296 ASSERT_EQ(rc, PLDM_PLATFORM_SET_EFFECTER_UNSUPPORTED_SENSORSTATE);
297
George Liu0d7aca82020-03-30 15:01:36 +0800298 rc = platform_state_effecter::setStateEffecterStatesHandler<MockdBusHandler,
299 Handler>(
George Liu36e81352020-07-01 14:40:30 +0800300 mockedUtils, handler, 0x9, stateField);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500301 ASSERT_EQ(rc, PLDM_PLATFORM_INVALID_EFFECTER_ID);
302
303 stateField.push_back({PLDM_REQUEST_SET, 4});
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, 0x1, stateField);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500307 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
308
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600309 pldm_pdr_destroy(inPDRRepo);
310 pldm_pdr_destroy(outPDRRepo);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500311}
George Liu64d26be2020-01-15 10:58:10 +0800312
313TEST(setNumericEffecterValueHandler, testGoodRequest)
314{
George Liu36e81352020-07-01 14:40:30 +0800315 MockdBusHandler mockedUtils;
316 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
317 .Times(5)
318 .WillRepeatedly(Return("foo.bar"));
319
George Liu64d26be2020-01-15 10:58:10 +0800320 auto inPDRRepo = pldm_pdr_init();
321 auto numericEffecterPdrRepo = pldm_pdr_init();
322 Repo numericEffecterPDRs(numericEffecterPdrRepo);
Deepak Kodihallib5c227e2020-07-13 06:58:34 -0500323 Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good", "",
George Liucae18662020-05-15 09:32:57 +0800324 inPDRRepo, nullptr, nullptr, nullptr);
George Liu64d26be2020-01-15 10:58:10 +0800325 Repo inRepo(inPDRRepo);
326 getRepoByType(inRepo, numericEffecterPDRs, PLDM_NUMERIC_EFFECTER_PDR);
327
328 pdr_utils::PdrEntry e;
Sampa Misra12afe112020-05-25 11:40:44 -0500329 auto record4 = pdr::getRecordByHandle(numericEffecterPDRs, 4, e);
330 ASSERT_NE(record4, nullptr);
George Liu64d26be2020-01-15 10:58:10 +0800331
332 pldm_numeric_effecter_value_pdr* pdr =
333 reinterpret_cast<pldm_numeric_effecter_value_pdr*>(e.data);
334 EXPECT_EQ(pdr->hdr.type, PLDM_NUMERIC_EFFECTER_PDR);
335
336 uint16_t effecterId = 3;
337 uint32_t effecterValue = 2100000000; // 2036-07-18 21:20:00
338 PropertyValue propertyValue = static_cast<uint32_t>(effecterValue);
339
George Liu64d26be2020-01-15 10:58:10 +0800340 DBusMapping dbusMapping{"/foo/bar", "xyz.openbmc_project.Foo.Bar",
341 "propertyName", "uint64_t"};
George Liu36e81352020-07-01 14:40:30 +0800342 EXPECT_CALL(mockedUtils, setDbusProperty(dbusMapping, propertyValue))
George Liu64d26be2020-01-15 10:58:10 +0800343 .Times(1);
344
345 auto rc = platform_numeric_effecter::setNumericEffecterValueHandler<
346 MockdBusHandler, Handler>(
George Liu36e81352020-07-01 14:40:30 +0800347 mockedUtils, handler, effecterId, PLDM_EFFECTER_DATA_SIZE_UINT32,
George Liu64d26be2020-01-15 10:58:10 +0800348 reinterpret_cast<uint8_t*>(&effecterValue), 4);
349 ASSERT_EQ(rc, 0);
350
351 pldm_pdr_destroy(inPDRRepo);
352 pldm_pdr_destroy(numericEffecterPdrRepo);
353}
354
355TEST(setNumericEffecterValueHandler, testBadRequest)
356{
George Liu36e81352020-07-01 14:40:30 +0800357 MockdBusHandler mockedUtils;
358 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
359 .Times(5)
360 .WillRepeatedly(Return("foo.bar"));
361
George Liu64d26be2020-01-15 10:58:10 +0800362 auto inPDRRepo = pldm_pdr_init();
363 auto numericEffecterPdrRepo = pldm_pdr_init();
364 Repo numericEffecterPDRs(numericEffecterPdrRepo);
Deepak Kodihallib5c227e2020-07-13 06:58:34 -0500365 Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good", "",
George Liucae18662020-05-15 09:32:57 +0800366 inPDRRepo, nullptr, nullptr, nullptr);
George Liu64d26be2020-01-15 10:58:10 +0800367 Repo inRepo(inPDRRepo);
368 getRepoByType(inRepo, numericEffecterPDRs, PLDM_NUMERIC_EFFECTER_PDR);
369
370 pdr_utils::PdrEntry e;
Sampa Misra12afe112020-05-25 11:40:44 -0500371 auto record4 = pdr::getRecordByHandle(numericEffecterPDRs, 4, e);
372 ASSERT_NE(record4, nullptr);
George Liu64d26be2020-01-15 10:58:10 +0800373
374 pldm_numeric_effecter_value_pdr* pdr =
375 reinterpret_cast<pldm_numeric_effecter_value_pdr*>(e.data);
376 EXPECT_EQ(pdr->hdr.type, PLDM_NUMERIC_EFFECTER_PDR);
377
378 uint16_t effecterId = 3;
379 uint64_t effecterValue = 9876543210;
George Liu64d26be2020-01-15 10:58:10 +0800380 auto rc = platform_numeric_effecter::setNumericEffecterValueHandler<
381 MockdBusHandler, Handler>(
George Liu36e81352020-07-01 14:40:30 +0800382 mockedUtils, handler, effecterId, PLDM_EFFECTER_DATA_SIZE_SINT32,
George Liu64d26be2020-01-15 10:58:10 +0800383 reinterpret_cast<uint8_t*>(&effecterValue), 3);
384 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
385
386 pldm_pdr_destroy(inPDRRepo);
387 pldm_pdr_destroy(numericEffecterPdrRepo);
388}
Tom Josephb4268602020-04-17 17:20:45 +0530389
390TEST(parseStateSensor, allScenarios)
391{
392 // Sample state sensor with SensorID - 1, EntityType - Processor Module(67)
393 // State Set ID - Operational Running Status(11), Supported States - 3,4
394 std::vector<uint8_t> sample1PDR{0x00, 0x00, 0x00, 0x00, 0x01, 0x04, 0x00,
395 0x00, 0x17, 0x00, 0x00, 0x00, 0x01, 0x00,
396 0x43, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
397 0x00, 0x01, 0x0b, 0x00, 0x01, 0x18};
398
399 const auto& [terminusHandle1, sensorID1, sensorInfo1] =
400 parseStateSensorPDR(sample1PDR);
401 const auto& [containerID1, entityType1, entityInstance1] =
402 std::get<0>(sensorInfo1);
403 const auto& states1 = std::get<1>(sensorInfo1);
404 CompositeSensorStates statesCmp1{{3u, 4u}};
405
406 ASSERT_EQ(le16toh(terminusHandle1), 0u);
407 ASSERT_EQ(le16toh(sensorID1), 1u);
408 ASSERT_EQ(le16toh(containerID1), 0u);
409 ASSERT_EQ(le16toh(entityType1), 67u);
410 ASSERT_EQ(le16toh(entityInstance1), 1u);
411 ASSERT_EQ(states1, statesCmp1);
412
413 // Sample state sensor with SensorID - 2, EntityType - System Firmware(31)
414 // State Set ID - Availability(2), Supported States - 3,4,9,10,11,13
415 std::vector<uint8_t> sample2PDR{0x00, 0x00, 0x00, 0x00, 0x01, 0x04, 0x00,
416 0x00, 0x17, 0x00, 0x00, 0x00, 0x02, 0x00,
417 0x1F, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
418 0x00, 0x01, 0x02, 0x00, 0x02, 0x18, 0x2E};
419
420 const auto& [terminusHandle2, sensorID2, sensorInfo2] =
421 parseStateSensorPDR(sample2PDR);
422 const auto& [containerID2, entityType2, entityInstance2] =
423 std::get<0>(sensorInfo2);
424 const auto& states2 = std::get<1>(sensorInfo2);
425 CompositeSensorStates statesCmp2{{3u, 4u, 9u, 10u, 11u, 13u}};
426
427 ASSERT_EQ(le16toh(terminusHandle2), 0u);
428 ASSERT_EQ(le16toh(sensorID2), 2u);
429 ASSERT_EQ(le16toh(containerID2), 0u);
430 ASSERT_EQ(le16toh(entityType2), 31u);
431 ASSERT_EQ(le16toh(entityInstance2), 1u);
432 ASSERT_EQ(states2, statesCmp2);
433
434 // Sample state sensor with SensorID - 3, EntityType - Virtual Machine
435 // Manager(33), Composite State Sensor -2 , State Set ID - Link State(33),
436 // Supported States - 1,2, State Set ID - Configuration State(15),
437 // Supported States - 1,2,3,4
438 std::vector<uint8_t> sample3PDR{
439 0x00, 0x00, 0x00, 0x00, 0x01, 0x04, 0x00, 0x00, 0x17, 0x00, 0x00,
440 0x00, 0x03, 0x00, 0x21, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x00,
441 0x02, 0x21, 0x00, 0x01, 0x06, 0x0F, 0x00, 0x01, 0x1E};
442
443 const auto& [terminusHandle3, sensorID3, sensorInfo3] =
444 parseStateSensorPDR(sample3PDR);
445 const auto& [containerID3, entityType3, entityInstance3] =
446 std::get<0>(sensorInfo3);
447 const auto& states3 = std::get<1>(sensorInfo3);
448 CompositeSensorStates statesCmp3{{1u, 2u}, {1u, 2u, 3u, 4u}};
449
450 ASSERT_EQ(le16toh(terminusHandle3), 0u);
451 ASSERT_EQ(le16toh(sensorID3), 3u);
452 ASSERT_EQ(le16toh(containerID3), 1u);
453 ASSERT_EQ(le16toh(entityType3), 33u);
454 ASSERT_EQ(le16toh(entityInstance3), 2u);
455 ASSERT_EQ(states3, statesCmp3);
456}
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530457
458TEST(StateSensorHandler, allScenarios)
459{
460 using namespace pldm::responder::events;
461
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530462 StateSensorHandler handler{"./event_jsons/good"};
463 constexpr uint8_t eventState0 = 0;
464 constexpr uint8_t eventState1 = 1;
465 constexpr uint8_t eventState2 = 2;
466 constexpr uint8_t eventState3 = 3;
467
468 // Event Entry 1
469 {
470 StateSensorEntry entry{1, 64, 1, 0};
471 const auto& [dbusMapping, eventStateMap] = handler.getEventInfo(entry);
472 DBusMapping mapping{"/xyz/abc/def",
473 "xyz.openbmc_project.example1.value", "value1",
474 "string"};
475 ASSERT_EQ(mapping == dbusMapping, true);
476
477 const auto& propValue0 = eventStateMap.at(eventState0);
478 const auto& propValue1 = eventStateMap.at(eventState1);
479 const auto& propValue2 = eventStateMap.at(eventState2);
480 PropertyValue value0{std::in_place_type<std::string>,
481 "xyz.openbmc_project.State.Normal"};
482 PropertyValue value1{std::in_place_type<std::string>,
483 "xyz.openbmc_project.State.Critical"};
484 PropertyValue value2{std::in_place_type<std::string>,
485 "xyz.openbmc_project.State.Fatal"};
486 ASSERT_EQ(value0 == propValue0, true);
487 ASSERT_EQ(value1 == propValue1, true);
488 ASSERT_EQ(value2 == propValue2, true);
489 }
490
491 // Event Entry 2
492 {
493 StateSensorEntry entry{1, 64, 1, 1};
494 const auto& [dbusMapping, eventStateMap] = handler.getEventInfo(entry);
495 DBusMapping mapping{"/xyz/abc/def",
496 "xyz.openbmc_project.example2.value", "value2",
497 "uint8_t"};
498 ASSERT_EQ(mapping == dbusMapping, true);
499
500 const auto& propValue0 = eventStateMap.at(eventState2);
501 const auto& propValue1 = eventStateMap.at(eventState3);
502 PropertyValue value0{std::in_place_type<uint8_t>, 9};
503 PropertyValue value1{std::in_place_type<uint8_t>, 10};
504 ASSERT_EQ(value0 == propValue0, true);
505 ASSERT_EQ(value1 == propValue1, true);
506 }
507
508 // Event Entry 3
509 {
510 StateSensorEntry entry{2, 67, 2, 0};
511 const auto& [dbusMapping, eventStateMap] = handler.getEventInfo(entry);
512 DBusMapping mapping{"/xyz/abc/ghi",
513 "xyz.openbmc_project.example3.value", "value3",
514 "bool"};
515 ASSERT_EQ(mapping == dbusMapping, true);
516
517 const auto& propValue0 = eventStateMap.at(eventState0);
518 const auto& propValue1 = eventStateMap.at(eventState1);
519 PropertyValue value0{std::in_place_type<bool>, false};
520 PropertyValue value1{std::in_place_type<bool>, true};
521 ASSERT_EQ(value0 == propValue0, true);
522 ASSERT_EQ(value1 == propValue1, true);
523 }
524
525 // Invalid Entry
526 {
527 StateSensorEntry entry{0, 0, 0, 0};
528 ASSERT_THROW(handler.getEventInfo(entry), std::out_of_range);
529 }
530}
Sampa Misra12afe112020-05-25 11:40:44 -0500531
532TEST(TerminusLocatorPDR, BMCTerminusLocatorPDR)
533{
534 auto inPDRRepo = pldm_pdr_init();
535 auto outPDRRepo = pldm_pdr_init();
536 Repo outRepo(outPDRRepo);
537 MockdBusHandler mockedUtils;
George Liucae18662020-05-15 09:32:57 +0800538 Handler handler(&mockedUtils, "", "", inPDRRepo, nullptr, nullptr, nullptr);
Sampa Misra12afe112020-05-25 11:40:44 -0500539 Repo inRepo(inPDRRepo);
540 getRepoByType(inRepo, outRepo, PLDM_TERMINUS_LOCATOR_PDR);
541
542 // 1 BMC terminus locator PDR in the PDR repository
543 ASSERT_EQ(outRepo.getRecordCount(), 1);
544
545 pdr_utils::PdrEntry entry;
546 auto record = pdr::getRecordByHandle(outRepo, 1, entry);
547 ASSERT_NE(record, nullptr);
548
549 auto pdr = reinterpret_cast<const pldm_terminus_locator_pdr*>(entry.data);
550 EXPECT_EQ(pdr->hdr.record_handle, 1);
551 EXPECT_EQ(pdr->hdr.version, 1);
552 EXPECT_EQ(pdr->hdr.type, PLDM_TERMINUS_LOCATOR_PDR);
553 EXPECT_EQ(pdr->hdr.record_change_num, 0);
554 EXPECT_EQ(pdr->hdr.length,
555 sizeof(pldm_terminus_locator_pdr) - sizeof(pldm_pdr_hdr));
556 EXPECT_EQ(pdr->terminus_handle, BmcPldmTerminusHandle);
557 EXPECT_EQ(pdr->validity, PLDM_TL_PDR_VALID);
558 EXPECT_EQ(pdr->tid, BmcTerminusId);
559 EXPECT_EQ(pdr->container_id, 0);
560 EXPECT_EQ(pdr->terminus_locator_type, PLDM_TERMINUS_LOCATOR_TYPE_MCTP_EID);
561 EXPECT_EQ(pdr->terminus_locator_value_size,
562 sizeof(pldm_terminus_locator_type_mctp_eid));
563 auto locatorValue =
564 reinterpret_cast<const pldm_terminus_locator_type_mctp_eid*>(
565 pdr->terminus_locator_value);
566 EXPECT_EQ(locatorValue->eid, BmcMctpEid);
George Liua9170122020-05-14 11:59:13 +0800567 pldm_pdr_destroy(inPDRRepo);
568 pldm_pdr_destroy(outPDRRepo);
569}
570
571TEST(getStateSensorReadingsHandler, testGoodRequest)
572{
573 MockdBusHandler mockedUtils;
574 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
575 .Times(1)
576 .WillRepeatedly(Return("foo.bar"));
577
578 auto inPDRRepo = pldm_pdr_init();
579 auto outPDRRepo = pldm_pdr_init();
580 Repo outRepo(outPDRRepo);
581 Handler handler(&mockedUtils, "./pdr_jsons/state_sensor/good", "",
George Liucae18662020-05-15 09:32:57 +0800582 inPDRRepo, nullptr, nullptr, nullptr);
George Liua9170122020-05-14 11:59:13 +0800583 Repo inRepo(inPDRRepo);
584 getRepoByType(inRepo, outRepo, PLDM_STATE_SENSOR_PDR);
585 pdr_utils::PdrEntry e;
586 auto record = pdr::getRecordByHandle(outRepo, 2, e);
587 ASSERT_NE(record, nullptr);
588 pldm_state_sensor_pdr* pdr =
589 reinterpret_cast<pldm_state_sensor_pdr*>(e.data);
590 EXPECT_EQ(pdr->hdr.type, PLDM_STATE_SENSOR_PDR);
591
592 std::vector<get_sensor_state_field> stateField;
593 uint8_t compSensorCnt{};
594 uint8_t sensorRearmCnt = 1;
595
596 MockdBusHandler handlerObj;
597 EXPECT_CALL(handlerObj,
598 getDbusPropertyVariant(StrEq("/foo/bar"), StrEq("propertyName"),
599 StrEq("xyz.openbmc_project.Foo.Bar")))
600 .WillOnce(Return(
601 PropertyValue(std::string("xyz.openbmc_project.Foo.Bar.V0"))));
602
603 auto rc = platform_state_sensor::getStateSensorReadingsHandler<
604 MockdBusHandler, Handler>(handlerObj, handler, 0x1, sensorRearmCnt,
605 compSensorCnt, stateField);
606 ASSERT_EQ(rc, 0);
607 ASSERT_EQ(compSensorCnt, 1);
608 ASSERT_EQ(stateField[0].sensor_op_state, PLDM_SENSOR_ENABLED);
609 ASSERT_EQ(stateField[0].present_state, PLDM_SENSOR_UNKNOWN);
610 ASSERT_EQ(stateField[0].previous_state, PLDM_SENSOR_UNKNOWN);
611 ASSERT_EQ(stateField[0].event_state, PLDM_SENSOR_ENABLED);
612
613 pldm_pdr_destroy(inPDRRepo);
614 pldm_pdr_destroy(outPDRRepo);
615}
616
617TEST(getStateSensorReadingsHandler, testBadRequest)
618{
619 MockdBusHandler mockedUtils;
620 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
621 .Times(1)
622 .WillRepeatedly(Return("foo.bar"));
623
624 auto inPDRRepo = pldm_pdr_init();
625 auto outPDRRepo = pldm_pdr_init();
626 Repo outRepo(outPDRRepo);
627 Handler handler(&mockedUtils, "./pdr_jsons/state_sensor/good", "",
George Liucae18662020-05-15 09:32:57 +0800628 inPDRRepo, nullptr, nullptr, nullptr);
George Liua9170122020-05-14 11:59:13 +0800629 Repo inRepo(inPDRRepo);
630 getRepoByType(inRepo, outRepo, PLDM_STATE_SENSOR_PDR);
631 pdr_utils::PdrEntry e;
632 auto record = pdr::getRecordByHandle(outRepo, 2, e);
633 ASSERT_NE(record, nullptr);
634 pldm_state_sensor_pdr* pdr =
635 reinterpret_cast<pldm_state_sensor_pdr*>(e.data);
636 EXPECT_EQ(pdr->hdr.type, PLDM_STATE_SENSOR_PDR);
637
638 std::vector<get_sensor_state_field> stateField;
639 uint8_t compSensorCnt{};
640 uint8_t sensorRearmCnt = 3;
641
642 MockdBusHandler handlerObj;
643 auto rc = platform_state_sensor::getStateSensorReadingsHandler<
644 MockdBusHandler, Handler>(handlerObj, handler, 0x1, sensorRearmCnt,
645 compSensorCnt, stateField);
646 ASSERT_EQ(rc, PLDM_PLATFORM_REARM_UNAVAILABLE_IN_PRESENT_STATE);
Sampa Misra12afe112020-05-25 11:40:44 -0500647
648 pldm_pdr_destroy(inPDRRepo);
649 pldm_pdr_destroy(outPDRRepo);
650}