blob: accdb60f6416c22a5523e69c189fab4d82e7d74f [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
George Liu1e44c732020-02-28 20:20:06 +080016using namespace pldm::utils;
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053017using namespace pldm::responder;
Deepak Kodihallic682fe22020-03-04 00:42:54 -060018using namespace pldm::responder::platform;
19using namespace pldm::responder::pdr;
20using namespace pldm::responder::pdr_utils;
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053021
George Liu36e81352020-07-01 14:40:30 +080022using ::testing::_;
23using ::testing::Return;
24using ::testing::StrEq;
25
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053026TEST(getPDR, testGoodPath)
27{
28 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
29 requestPayload{};
George Liue53193f2020-02-24 09:23:26 +080030 auto req = reinterpret_cast<pldm_msg*>(requestPayload.data());
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053031 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
32
George Liue53193f2020-02-24 09:23:26 +080033 struct pldm_get_pdr_req* request =
34 reinterpret_cast<struct pldm_get_pdr_req*>(req->payload);
35 request->request_count = 100;
36
George Liu36e81352020-07-01 14:40:30 +080037 MockdBusHandler mockedUtils;
38 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
39 .Times(5)
40 .WillRepeatedly(Return("foo.bar"));
41
Deepak Kodihallic682fe22020-03-04 00:42:54 -060042 auto pdrRepo = pldm_pdr_init();
Sampa Misra5fb37d52021-03-06 07:26:00 -060043 auto event = sdeventplus::Event::get_default();
Pavithra Barithaya3aec9972020-12-14 01:55:44 -060044 Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good", pdrRepo,
Sampa Misra5fb37d52021-03-06 07:26:00 -060045 nullptr, nullptr, nullptr, nullptr, event);
Deepak Kodihallic682fe22020-03-04 00:42:54 -060046 Repo repo(pdrRepo);
47 ASSERT_EQ(repo.empty(), false);
George Liue53193f2020-02-24 09:23:26 +080048 auto response = handler.getPDR(req, requestPayloadLength);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053049 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
50
George Liue53193f2020-02-24 09:23:26 +080051 struct pldm_get_pdr_resp* resp =
52 reinterpret_cast<struct pldm_get_pdr_resp*>(responsePtr->payload);
53 ASSERT_EQ(PLDM_SUCCESS, resp->completion_code);
54 ASSERT_EQ(2, resp->next_record_handle);
55 ASSERT_EQ(true, resp->response_count != 0);
56
57 pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(resp->record_data);
58 ASSERT_EQ(hdr->record_handle, 1);
59 ASSERT_EQ(hdr->version, 1);
Deepak Kodihallic682fe22020-03-04 00:42:54 -060060
61 pldm_pdr_destroy(pdrRepo);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053062}
63
64TEST(getPDR, testShortRead)
65{
66 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
67 requestPayload{};
George Liue53193f2020-02-24 09:23:26 +080068 auto req = reinterpret_cast<pldm_msg*>(requestPayload.data());
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053069 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
70
George Liue53193f2020-02-24 09:23:26 +080071 struct pldm_get_pdr_req* request =
72 reinterpret_cast<struct pldm_get_pdr_req*>(req->payload);
73 request->request_count = 1;
74
George Liu36e81352020-07-01 14:40:30 +080075 MockdBusHandler mockedUtils;
76 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
77 .Times(5)
78 .WillRepeatedly(Return("foo.bar"));
79
Deepak Kodihallic682fe22020-03-04 00:42:54 -060080 auto pdrRepo = pldm_pdr_init();
Sampa Misra5fb37d52021-03-06 07:26:00 -060081 auto event = sdeventplus::Event::get_default();
Pavithra Barithaya3aec9972020-12-14 01:55:44 -060082 Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good", pdrRepo,
Sampa Misra5fb37d52021-03-06 07:26:00 -060083 nullptr, nullptr, nullptr, nullptr, event);
Deepak Kodihallic682fe22020-03-04 00:42:54 -060084 Repo repo(pdrRepo);
85 ASSERT_EQ(repo.empty(), false);
George Liue53193f2020-02-24 09:23:26 +080086 auto response = handler.getPDR(req, requestPayloadLength);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053087 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
George Liue53193f2020-02-24 09:23:26 +080088 struct pldm_get_pdr_resp* resp =
89 reinterpret_cast<struct pldm_get_pdr_resp*>(responsePtr->payload);
90 ASSERT_EQ(PLDM_SUCCESS, resp->completion_code);
91 ASSERT_EQ(1, resp->response_count);
Deepak Kodihallic682fe22020-03-04 00:42:54 -060092 pldm_pdr_destroy(pdrRepo);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053093}
94
95TEST(getPDR, testBadRecordHandle)
96{
97 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
98 requestPayload{};
George Liue53193f2020-02-24 09:23:26 +080099 auto req = reinterpret_cast<pldm_msg*>(requestPayload.data());
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530100 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
101
George Liue53193f2020-02-24 09:23:26 +0800102 struct pldm_get_pdr_req* request =
103 reinterpret_cast<struct pldm_get_pdr_req*>(req->payload);
104 request->record_handle = 100000;
105 request->request_count = 1;
106
George Liu36e81352020-07-01 14:40:30 +0800107 MockdBusHandler mockedUtils;
108 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
109 .Times(5)
110 .WillRepeatedly(Return("foo.bar"));
111
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600112 auto pdrRepo = pldm_pdr_init();
Sampa Misra5fb37d52021-03-06 07:26:00 -0600113 auto event = sdeventplus::Event::get_default();
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600114 Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good", pdrRepo,
Sampa Misra5fb37d52021-03-06 07:26:00 -0600115 nullptr, nullptr, nullptr, nullptr, event);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600116 Repo repo(pdrRepo);
117 ASSERT_EQ(repo.empty(), false);
George Liue53193f2020-02-24 09:23:26 +0800118 auto response = handler.getPDR(req, requestPayloadLength);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530119 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
120
121 ASSERT_EQ(responsePtr->payload[0], PLDM_PLATFORM_INVALID_RECORD_HANDLE);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600122
123 pldm_pdr_destroy(pdrRepo);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530124}
125
126TEST(getPDR, testNoNextRecord)
127{
128 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
129 requestPayload{};
George Liue53193f2020-02-24 09:23:26 +0800130 auto req = reinterpret_cast<pldm_msg*>(requestPayload.data());
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530131 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
132
George Liue53193f2020-02-24 09:23:26 +0800133 struct pldm_get_pdr_req* request =
134 reinterpret_cast<struct pldm_get_pdr_req*>(req->payload);
135 request->record_handle = 1;
136
George Liu36e81352020-07-01 14:40:30 +0800137 MockdBusHandler mockedUtils;
138 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
139 .Times(5)
140 .WillRepeatedly(Return("foo.bar"));
141
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600142 auto pdrRepo = pldm_pdr_init();
Sampa Misra5fb37d52021-03-06 07:26:00 -0600143 auto event = sdeventplus::Event::get_default();
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600144 Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good", pdrRepo,
Sampa Misra5fb37d52021-03-06 07:26:00 -0600145 nullptr, nullptr, nullptr, nullptr, event);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600146 Repo repo(pdrRepo);
147 ASSERT_EQ(repo.empty(), false);
George Liue53193f2020-02-24 09:23:26 +0800148 auto response = handler.getPDR(req, requestPayloadLength);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530149 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
George Liue53193f2020-02-24 09:23:26 +0800150 struct pldm_get_pdr_resp* resp =
151 reinterpret_cast<struct pldm_get_pdr_resp*>(responsePtr->payload);
152 ASSERT_EQ(PLDM_SUCCESS, resp->completion_code);
153 ASSERT_EQ(2, resp->next_record_handle);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600154
155 pldm_pdr_destroy(pdrRepo);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530156}
157
158TEST(getPDR, testFindPDR)
159{
160 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
161 requestPayload{};
George Liue53193f2020-02-24 09:23:26 +0800162 auto req = reinterpret_cast<pldm_msg*>(requestPayload.data());
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530163 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
164
George Liue53193f2020-02-24 09:23:26 +0800165 struct pldm_get_pdr_req* request =
166 reinterpret_cast<struct pldm_get_pdr_req*>(req->payload);
167 request->request_count = 100;
168
George Liu36e81352020-07-01 14:40:30 +0800169 MockdBusHandler mockedUtils;
170 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
171 .Times(5)
172 .WillRepeatedly(Return("foo.bar"));
173
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600174 auto pdrRepo = pldm_pdr_init();
Sampa Misra5fb37d52021-03-06 07:26:00 -0600175 auto event = sdeventplus::Event::get_default();
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600176 Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good", pdrRepo,
Sampa Misra5fb37d52021-03-06 07:26:00 -0600177 nullptr, nullptr, nullptr, nullptr, event);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600178 Repo repo(pdrRepo);
179 ASSERT_EQ(repo.empty(), false);
George Liue53193f2020-02-24 09:23:26 +0800180 auto response = handler.getPDR(req, requestPayloadLength);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530181
182 // Let's try to find a PDR of type stateEffecter (= 11) and entity type =
183 // 100
184 bool found = false;
185 uint32_t handle = 0; // start asking for PDRs from recordHandle 0
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530186 while (!found)
187 {
George Liue53193f2020-02-24 09:23:26 +0800188 request->record_handle = handle;
George Liue53193f2020-02-24 09:23:26 +0800189 auto response = handler.getPDR(req, requestPayloadLength);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530190 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
George Liue53193f2020-02-24 09:23:26 +0800191 struct pldm_get_pdr_resp* resp =
192 reinterpret_cast<struct pldm_get_pdr_resp*>(responsePtr->payload);
193 ASSERT_EQ(PLDM_SUCCESS, resp->completion_code);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530194
George Liue53193f2020-02-24 09:23:26 +0800195 handle = resp->next_record_handle; // point to the next pdr in case
196 // current is not what we want
197
198 pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(resp->record_data);
Sampa Misraaa8ae722019-12-12 03:20:40 -0600199 std::cerr << "PDR next record handle " << handle << "\n";
George Liue53193f2020-02-24 09:23:26 +0800200 std::cerr << "PDR type " << hdr->type << "\n";
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530201 if (hdr->type == PLDM_STATE_EFFECTER_PDR)
202 {
203 pldm_state_effecter_pdr* pdr =
George Liue53193f2020-02-24 09:23:26 +0800204 reinterpret_cast<pldm_state_effecter_pdr*>(resp->record_data);
Sampa Misraaa8ae722019-12-12 03:20:40 -0600205 std::cerr << "PDR entity type " << pdr->entity_type << "\n";
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530206 if (pdr->entity_type == 100)
207 {
208 found = true;
209 // Rest of the PDR can be accessed as need be
210 break;
211 }
212 }
George Liue53193f2020-02-24 09:23:26 +0800213 if (!resp->next_record_handle) // no more records
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530214 {
215 break;
216 }
217 }
218 ASSERT_EQ(found, true);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600219
220 pldm_pdr_destroy(pdrRepo);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530221}
Sampa Misraa2fa0702019-05-31 01:28:55 -0500222
Sampa Misraa2fa0702019-05-31 01:28:55 -0500223TEST(setStateEffecterStatesHandler, testGoodRequest)
224{
George Liua9170122020-05-14 11:59:13 +0800225 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
226 requestPayload{};
227 auto req = reinterpret_cast<pldm_msg*>(requestPayload.data());
228 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
229
George Liu36e81352020-07-01 14:40:30 +0800230 MockdBusHandler mockedUtils;
231 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
232 .Times(5)
233 .WillRepeatedly(Return("foo.bar"));
234
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600235 auto inPDRRepo = pldm_pdr_init();
236 auto outPDRRepo = pldm_pdr_init();
237 Repo outRepo(outPDRRepo);
Sampa Misra5fb37d52021-03-06 07:26:00 -0600238 auto event = sdeventplus::Event::get_default();
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600239 Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good", inPDRRepo,
Sampa Misra5fb37d52021-03-06 07:26:00 -0600240 nullptr, nullptr, nullptr, nullptr, event);
George Liua9170122020-05-14 11:59:13 +0800241 handler.getPDR(req, requestPayloadLength);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600242 Repo inRepo(inPDRRepo);
243 getRepoByType(inRepo, outRepo, PLDM_STATE_EFFECTER_PDR);
George Liue53193f2020-02-24 09:23:26 +0800244 pdr_utils::PdrEntry e;
Sampa Misra12afe112020-05-25 11:40:44 -0500245 auto record1 = pdr::getRecordByHandle(outRepo, 2, e);
George Liue53193f2020-02-24 09:23:26 +0800246 ASSERT_NE(record1, nullptr);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500247 pldm_state_effecter_pdr* pdr =
George Liue53193f2020-02-24 09:23:26 +0800248 reinterpret_cast<pldm_state_effecter_pdr*>(e.data);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500249 EXPECT_EQ(pdr->hdr.type, PLDM_STATE_EFFECTER_PDR);
250
251 std::vector<set_effecter_state_field> stateField;
252 stateField.push_back({PLDM_REQUEST_SET, 1});
253 stateField.push_back({PLDM_REQUEST_SET, 1});
George Liu1ec85d42020-02-12 16:05:32 +0800254 std::string value = "xyz.openbmc_project.Foo.Bar.V1";
George Liu1e44c732020-02-28 20:20:06 +0800255 PropertyValue propertyValue = value;
Sampa Misraa2fa0702019-05-31 01:28:55 -0500256
George Liu1ec85d42020-02-12 16:05:32 +0800257 DBusMapping dbusMapping{"/foo/bar", "xyz.openbmc_project.Foo.Bar",
258 "propertyName", "string"};
George Liu1e44c732020-02-28 20:20:06 +0800259
George Liu36e81352020-07-01 14:40:30 +0800260 EXPECT_CALL(mockedUtils, setDbusProperty(dbusMapping, propertyValue))
Sampa Misraa2fa0702019-05-31 01:28:55 -0500261 .Times(2);
George Liu0d7aca82020-03-30 15:01:36 +0800262 auto rc = platform_state_effecter::setStateEffecterStatesHandler<
George Liu36e81352020-07-01 14:40:30 +0800263 MockdBusHandler, Handler>(mockedUtils, handler, 0x1, stateField);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500264 ASSERT_EQ(rc, 0);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600265
266 pldm_pdr_destroy(inPDRRepo);
267 pldm_pdr_destroy(outPDRRepo);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500268}
269
270TEST(setStateEffecterStatesHandler, testBadRequest)
271{
George Liua9170122020-05-14 11:59:13 +0800272 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
273 requestPayload{};
274 auto req = reinterpret_cast<pldm_msg*>(requestPayload.data());
275 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
276
George Liu36e81352020-07-01 14:40:30 +0800277 MockdBusHandler mockedUtils;
278 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
279 .Times(5)
280 .WillRepeatedly(Return("foo.bar"));
281
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600282 auto inPDRRepo = pldm_pdr_init();
283 auto outPDRRepo = pldm_pdr_init();
284 Repo outRepo(outPDRRepo);
Sampa Misra5fb37d52021-03-06 07:26:00 -0600285 auto event = sdeventplus::Event::get_default();
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600286 Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good", inPDRRepo,
Sampa Misra5fb37d52021-03-06 07:26:00 -0600287 nullptr, nullptr, nullptr, nullptr, event);
George Liua9170122020-05-14 11:59:13 +0800288 handler.getPDR(req, requestPayloadLength);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600289 Repo inRepo(inPDRRepo);
290 getRepoByType(inRepo, outRepo, PLDM_STATE_EFFECTER_PDR);
George Liue53193f2020-02-24 09:23:26 +0800291 pdr_utils::PdrEntry e;
Sampa Misra12afe112020-05-25 11:40:44 -0500292 auto record1 = pdr::getRecordByHandle(outRepo, 2, e);
George Liue53193f2020-02-24 09:23:26 +0800293 ASSERT_NE(record1, nullptr);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500294 pldm_state_effecter_pdr* pdr =
George Liue53193f2020-02-24 09:23:26 +0800295 reinterpret_cast<pldm_state_effecter_pdr*>(e.data);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500296 EXPECT_EQ(pdr->hdr.type, PLDM_STATE_EFFECTER_PDR);
297
298 std::vector<set_effecter_state_field> stateField;
299 stateField.push_back({PLDM_REQUEST_SET, 3});
300 stateField.push_back({PLDM_REQUEST_SET, 4});
301
George Liu0d7aca82020-03-30 15:01:36 +0800302 auto rc = platform_state_effecter::setStateEffecterStatesHandler<
George Liu36e81352020-07-01 14:40:30 +0800303 MockdBusHandler, Handler>(mockedUtils, handler, 0x1, stateField);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500304 ASSERT_EQ(rc, PLDM_PLATFORM_SET_EFFECTER_UNSUPPORTED_SENSORSTATE);
305
George Liu0d7aca82020-03-30 15:01:36 +0800306 rc = platform_state_effecter::setStateEffecterStatesHandler<MockdBusHandler,
307 Handler>(
George Liu36e81352020-07-01 14:40:30 +0800308 mockedUtils, handler, 0x9, stateField);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500309 ASSERT_EQ(rc, PLDM_PLATFORM_INVALID_EFFECTER_ID);
310
311 stateField.push_back({PLDM_REQUEST_SET, 4});
George Liu0d7aca82020-03-30 15:01:36 +0800312 rc = platform_state_effecter::setStateEffecterStatesHandler<MockdBusHandler,
313 Handler>(
George Liu36e81352020-07-01 14:40:30 +0800314 mockedUtils, handler, 0x1, stateField);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500315 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
316
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600317 pldm_pdr_destroy(inPDRRepo);
318 pldm_pdr_destroy(outPDRRepo);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500319}
George Liu64d26be2020-01-15 10:58:10 +0800320
321TEST(setNumericEffecterValueHandler, testGoodRequest)
322{
George Liu36e81352020-07-01 14:40:30 +0800323 MockdBusHandler mockedUtils;
324 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
325 .Times(5)
326 .WillRepeatedly(Return("foo.bar"));
327
George Liu64d26be2020-01-15 10:58:10 +0800328 auto inPDRRepo = pldm_pdr_init();
329 auto numericEffecterPdrRepo = pldm_pdr_init();
330 Repo numericEffecterPDRs(numericEffecterPdrRepo);
Sampa Misra5fb37d52021-03-06 07:26:00 -0600331 auto event = sdeventplus::Event::get_default();
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600332 Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good", inPDRRepo,
Sampa Misra5fb37d52021-03-06 07:26:00 -0600333 nullptr, nullptr, nullptr, nullptr, event);
George Liu64d26be2020-01-15 10:58:10 +0800334 Repo inRepo(inPDRRepo);
335 getRepoByType(inRepo, numericEffecterPDRs, PLDM_NUMERIC_EFFECTER_PDR);
336
337 pdr_utils::PdrEntry e;
Sampa Misra12afe112020-05-25 11:40:44 -0500338 auto record4 = pdr::getRecordByHandle(numericEffecterPDRs, 4, e);
339 ASSERT_NE(record4, nullptr);
George Liu64d26be2020-01-15 10:58:10 +0800340
341 pldm_numeric_effecter_value_pdr* pdr =
342 reinterpret_cast<pldm_numeric_effecter_value_pdr*>(e.data);
343 EXPECT_EQ(pdr->hdr.type, PLDM_NUMERIC_EFFECTER_PDR);
344
345 uint16_t effecterId = 3;
346 uint32_t effecterValue = 2100000000; // 2036-07-18 21:20:00
Pavithra Barithaya45cd16b2021-07-01 08:19:59 -0500347 PropertyValue propertyValue = static_cast<uint64_t>(effecterValue);
George Liu64d26be2020-01-15 10:58:10 +0800348
George Liu64d26be2020-01-15 10:58:10 +0800349 DBusMapping dbusMapping{"/foo/bar", "xyz.openbmc_project.Foo.Bar",
350 "propertyName", "uint64_t"};
George Liu36e81352020-07-01 14:40:30 +0800351 EXPECT_CALL(mockedUtils, setDbusProperty(dbusMapping, propertyValue))
George Liu64d26be2020-01-15 10:58:10 +0800352 .Times(1);
353
354 auto rc = platform_numeric_effecter::setNumericEffecterValueHandler<
355 MockdBusHandler, Handler>(
George Liu36e81352020-07-01 14:40:30 +0800356 mockedUtils, handler, effecterId, PLDM_EFFECTER_DATA_SIZE_UINT32,
George Liu64d26be2020-01-15 10:58:10 +0800357 reinterpret_cast<uint8_t*>(&effecterValue), 4);
358 ASSERT_EQ(rc, 0);
359
360 pldm_pdr_destroy(inPDRRepo);
361 pldm_pdr_destroy(numericEffecterPdrRepo);
362}
363
364TEST(setNumericEffecterValueHandler, testBadRequest)
365{
George Liu36e81352020-07-01 14:40:30 +0800366 MockdBusHandler mockedUtils;
367 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
368 .Times(5)
369 .WillRepeatedly(Return("foo.bar"));
370
George Liu64d26be2020-01-15 10:58:10 +0800371 auto inPDRRepo = pldm_pdr_init();
372 auto numericEffecterPdrRepo = pldm_pdr_init();
373 Repo numericEffecterPDRs(numericEffecterPdrRepo);
Sampa Misra5fb37d52021-03-06 07:26:00 -0600374 auto event = sdeventplus::Event::get_default();
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600375 Handler handler(&mockedUtils, "./pdr_jsons/state_effecter/good", inPDRRepo,
Sampa Misra5fb37d52021-03-06 07:26:00 -0600376 nullptr, nullptr, nullptr, nullptr, event);
George Liu64d26be2020-01-15 10:58:10 +0800377 Repo inRepo(inPDRRepo);
378 getRepoByType(inRepo, numericEffecterPDRs, PLDM_NUMERIC_EFFECTER_PDR);
379
380 pdr_utils::PdrEntry e;
Sampa Misra12afe112020-05-25 11:40:44 -0500381 auto record4 = pdr::getRecordByHandle(numericEffecterPDRs, 4, e);
382 ASSERT_NE(record4, nullptr);
George Liu64d26be2020-01-15 10:58:10 +0800383
384 pldm_numeric_effecter_value_pdr* pdr =
385 reinterpret_cast<pldm_numeric_effecter_value_pdr*>(e.data);
386 EXPECT_EQ(pdr->hdr.type, PLDM_NUMERIC_EFFECTER_PDR);
387
388 uint16_t effecterId = 3;
389 uint64_t effecterValue = 9876543210;
George Liu64d26be2020-01-15 10:58:10 +0800390 auto rc = platform_numeric_effecter::setNumericEffecterValueHandler<
391 MockdBusHandler, Handler>(
George Liu36e81352020-07-01 14:40:30 +0800392 mockedUtils, handler, effecterId, PLDM_EFFECTER_DATA_SIZE_SINT32,
George Liu64d26be2020-01-15 10:58:10 +0800393 reinterpret_cast<uint8_t*>(&effecterValue), 3);
394 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
395
396 pldm_pdr_destroy(inPDRRepo);
397 pldm_pdr_destroy(numericEffecterPdrRepo);
398}
Tom Josephb4268602020-04-17 17:20:45 +0530399
400TEST(parseStateSensor, allScenarios)
401{
402 // Sample state sensor with SensorID - 1, EntityType - Processor Module(67)
403 // State Set ID - Operational Running Status(11), Supported States - 3,4
404 std::vector<uint8_t> sample1PDR{0x00, 0x00, 0x00, 0x00, 0x01, 0x04, 0x00,
405 0x00, 0x17, 0x00, 0x00, 0x00, 0x01, 0x00,
406 0x43, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
407 0x00, 0x01, 0x0b, 0x00, 0x01, 0x18};
408
409 const auto& [terminusHandle1, sensorID1, sensorInfo1] =
410 parseStateSensorPDR(sample1PDR);
411 const auto& [containerID1, entityType1, entityInstance1] =
412 std::get<0>(sensorInfo1);
413 const auto& states1 = std::get<1>(sensorInfo1);
414 CompositeSensorStates statesCmp1{{3u, 4u}};
415
416 ASSERT_EQ(le16toh(terminusHandle1), 0u);
417 ASSERT_EQ(le16toh(sensorID1), 1u);
418 ASSERT_EQ(le16toh(containerID1), 0u);
419 ASSERT_EQ(le16toh(entityType1), 67u);
420 ASSERT_EQ(le16toh(entityInstance1), 1u);
421 ASSERT_EQ(states1, statesCmp1);
422
423 // Sample state sensor with SensorID - 2, EntityType - System Firmware(31)
424 // State Set ID - Availability(2), Supported States - 3,4,9,10,11,13
425 std::vector<uint8_t> sample2PDR{0x00, 0x00, 0x00, 0x00, 0x01, 0x04, 0x00,
426 0x00, 0x17, 0x00, 0x00, 0x00, 0x02, 0x00,
427 0x1F, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
428 0x00, 0x01, 0x02, 0x00, 0x02, 0x18, 0x2E};
429
430 const auto& [terminusHandle2, sensorID2, sensorInfo2] =
431 parseStateSensorPDR(sample2PDR);
432 const auto& [containerID2, entityType2, entityInstance2] =
433 std::get<0>(sensorInfo2);
434 const auto& states2 = std::get<1>(sensorInfo2);
435 CompositeSensorStates statesCmp2{{3u, 4u, 9u, 10u, 11u, 13u}};
436
437 ASSERT_EQ(le16toh(terminusHandle2), 0u);
438 ASSERT_EQ(le16toh(sensorID2), 2u);
439 ASSERT_EQ(le16toh(containerID2), 0u);
440 ASSERT_EQ(le16toh(entityType2), 31u);
441 ASSERT_EQ(le16toh(entityInstance2), 1u);
442 ASSERT_EQ(states2, statesCmp2);
443
444 // Sample state sensor with SensorID - 3, EntityType - Virtual Machine
445 // Manager(33), Composite State Sensor -2 , State Set ID - Link State(33),
446 // Supported States - 1,2, State Set ID - Configuration State(15),
447 // Supported States - 1,2,3,4
448 std::vector<uint8_t> sample3PDR{
449 0x00, 0x00, 0x00, 0x00, 0x01, 0x04, 0x00, 0x00, 0x17, 0x00, 0x00,
450 0x00, 0x03, 0x00, 0x21, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x00,
451 0x02, 0x21, 0x00, 0x01, 0x06, 0x0F, 0x00, 0x01, 0x1E};
452
453 const auto& [terminusHandle3, sensorID3, sensorInfo3] =
454 parseStateSensorPDR(sample3PDR);
455 const auto& [containerID3, entityType3, entityInstance3] =
456 std::get<0>(sensorInfo3);
457 const auto& states3 = std::get<1>(sensorInfo3);
458 CompositeSensorStates statesCmp3{{1u, 2u}, {1u, 2u, 3u, 4u}};
459
460 ASSERT_EQ(le16toh(terminusHandle3), 0u);
461 ASSERT_EQ(le16toh(sensorID3), 3u);
462 ASSERT_EQ(le16toh(containerID3), 1u);
463 ASSERT_EQ(le16toh(entityType3), 33u);
464 ASSERT_EQ(le16toh(entityInstance3), 2u);
465 ASSERT_EQ(states3, statesCmp3);
466}
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530467
468TEST(StateSensorHandler, allScenarios)
469{
470 using namespace pldm::responder::events;
471
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530472 StateSensorHandler handler{"./event_jsons/good"};
473 constexpr uint8_t eventState0 = 0;
474 constexpr uint8_t eventState1 = 1;
475 constexpr uint8_t eventState2 = 2;
476 constexpr uint8_t eventState3 = 3;
477
478 // Event Entry 1
479 {
480 StateSensorEntry entry{1, 64, 1, 0};
481 const auto& [dbusMapping, eventStateMap] = handler.getEventInfo(entry);
482 DBusMapping mapping{"/xyz/abc/def",
483 "xyz.openbmc_project.example1.value", "value1",
484 "string"};
485 ASSERT_EQ(mapping == dbusMapping, true);
486
487 const auto& propValue0 = eventStateMap.at(eventState0);
488 const auto& propValue1 = eventStateMap.at(eventState1);
489 const auto& propValue2 = eventStateMap.at(eventState2);
490 PropertyValue value0{std::in_place_type<std::string>,
491 "xyz.openbmc_project.State.Normal"};
492 PropertyValue value1{std::in_place_type<std::string>,
493 "xyz.openbmc_project.State.Critical"};
494 PropertyValue value2{std::in_place_type<std::string>,
495 "xyz.openbmc_project.State.Fatal"};
496 ASSERT_EQ(value0 == propValue0, true);
497 ASSERT_EQ(value1 == propValue1, true);
498 ASSERT_EQ(value2 == propValue2, true);
499 }
500
501 // Event Entry 2
502 {
503 StateSensorEntry entry{1, 64, 1, 1};
504 const auto& [dbusMapping, eventStateMap] = handler.getEventInfo(entry);
505 DBusMapping mapping{"/xyz/abc/def",
506 "xyz.openbmc_project.example2.value", "value2",
507 "uint8_t"};
508 ASSERT_EQ(mapping == dbusMapping, true);
509
510 const auto& propValue0 = eventStateMap.at(eventState2);
511 const auto& propValue1 = eventStateMap.at(eventState3);
512 PropertyValue value0{std::in_place_type<uint8_t>, 9};
513 PropertyValue value1{std::in_place_type<uint8_t>, 10};
514 ASSERT_EQ(value0 == propValue0, true);
515 ASSERT_EQ(value1 == propValue1, true);
516 }
517
518 // Event Entry 3
519 {
520 StateSensorEntry entry{2, 67, 2, 0};
521 const auto& [dbusMapping, eventStateMap] = handler.getEventInfo(entry);
522 DBusMapping mapping{"/xyz/abc/ghi",
523 "xyz.openbmc_project.example3.value", "value3",
524 "bool"};
525 ASSERT_EQ(mapping == dbusMapping, true);
526
527 const auto& propValue0 = eventStateMap.at(eventState0);
528 const auto& propValue1 = eventStateMap.at(eventState1);
529 PropertyValue value0{std::in_place_type<bool>, false};
530 PropertyValue value1{std::in_place_type<bool>, true};
531 ASSERT_EQ(value0 == propValue0, true);
532 ASSERT_EQ(value1 == propValue1, true);
533 }
534
535 // Invalid Entry
536 {
537 StateSensorEntry entry{0, 0, 0, 0};
538 ASSERT_THROW(handler.getEventInfo(entry), std::out_of_range);
539 }
540}
Sampa Misra12afe112020-05-25 11:40:44 -0500541
542TEST(TerminusLocatorPDR, BMCTerminusLocatorPDR)
543{
544 auto inPDRRepo = pldm_pdr_init();
545 auto outPDRRepo = pldm_pdr_init();
546 Repo outRepo(outPDRRepo);
547 MockdBusHandler mockedUtils;
Sampa Misra5fb37d52021-03-06 07:26:00 -0600548 auto event = sdeventplus::Event::get_default();
Sampa Misraaea5dde2020-08-31 08:33:47 -0500549 Handler handler(&mockedUtils, "", inPDRRepo, nullptr, nullptr, nullptr,
Sampa Misra5fb37d52021-03-06 07:26:00 -0600550 nullptr, event);
Sampa Misra12afe112020-05-25 11:40:44 -0500551 Repo inRepo(inPDRRepo);
552 getRepoByType(inRepo, outRepo, PLDM_TERMINUS_LOCATOR_PDR);
553
554 // 1 BMC terminus locator PDR in the PDR repository
555 ASSERT_EQ(outRepo.getRecordCount(), 1);
556
557 pdr_utils::PdrEntry entry;
558 auto record = pdr::getRecordByHandle(outRepo, 1, entry);
559 ASSERT_NE(record, nullptr);
560
561 auto pdr = reinterpret_cast<const pldm_terminus_locator_pdr*>(entry.data);
562 EXPECT_EQ(pdr->hdr.record_handle, 1);
563 EXPECT_EQ(pdr->hdr.version, 1);
564 EXPECT_EQ(pdr->hdr.type, PLDM_TERMINUS_LOCATOR_PDR);
565 EXPECT_EQ(pdr->hdr.record_change_num, 0);
566 EXPECT_EQ(pdr->hdr.length,
567 sizeof(pldm_terminus_locator_pdr) - sizeof(pldm_pdr_hdr));
568 EXPECT_EQ(pdr->terminus_handle, BmcPldmTerminusHandle);
569 EXPECT_EQ(pdr->validity, PLDM_TL_PDR_VALID);
570 EXPECT_EQ(pdr->tid, BmcTerminusId);
571 EXPECT_EQ(pdr->container_id, 0);
572 EXPECT_EQ(pdr->terminus_locator_type, PLDM_TERMINUS_LOCATOR_TYPE_MCTP_EID);
573 EXPECT_EQ(pdr->terminus_locator_value_size,
574 sizeof(pldm_terminus_locator_type_mctp_eid));
575 auto locatorValue =
576 reinterpret_cast<const pldm_terminus_locator_type_mctp_eid*>(
577 pdr->terminus_locator_value);
578 EXPECT_EQ(locatorValue->eid, BmcMctpEid);
George Liua9170122020-05-14 11:59:13 +0800579 pldm_pdr_destroy(inPDRRepo);
580 pldm_pdr_destroy(outPDRRepo);
581}
582
583TEST(getStateSensorReadingsHandler, testGoodRequest)
584{
585 MockdBusHandler mockedUtils;
586 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
587 .Times(1)
588 .WillRepeatedly(Return("foo.bar"));
589
590 auto inPDRRepo = pldm_pdr_init();
591 auto outPDRRepo = pldm_pdr_init();
592 Repo outRepo(outPDRRepo);
Sampa Misra5fb37d52021-03-06 07:26:00 -0600593 auto event = sdeventplus::Event::get_default();
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600594 Handler handler(&mockedUtils, "./pdr_jsons/state_sensor/good", inPDRRepo,
Sampa Misra5fb37d52021-03-06 07:26:00 -0600595 nullptr, nullptr, nullptr, nullptr, event);
George Liua9170122020-05-14 11:59:13 +0800596 Repo inRepo(inPDRRepo);
597 getRepoByType(inRepo, outRepo, PLDM_STATE_SENSOR_PDR);
598 pdr_utils::PdrEntry e;
599 auto record = pdr::getRecordByHandle(outRepo, 2, e);
600 ASSERT_NE(record, nullptr);
601 pldm_state_sensor_pdr* pdr =
602 reinterpret_cast<pldm_state_sensor_pdr*>(e.data);
603 EXPECT_EQ(pdr->hdr.type, PLDM_STATE_SENSOR_PDR);
604
605 std::vector<get_sensor_state_field> stateField;
606 uint8_t compSensorCnt{};
607 uint8_t sensorRearmCnt = 1;
608
609 MockdBusHandler handlerObj;
610 EXPECT_CALL(handlerObj,
611 getDbusPropertyVariant(StrEq("/foo/bar"), StrEq("propertyName"),
612 StrEq("xyz.openbmc_project.Foo.Bar")))
613 .WillOnce(Return(
614 PropertyValue(std::string("xyz.openbmc_project.Foo.Bar.V0"))));
615
616 auto rc = platform_state_sensor::getStateSensorReadingsHandler<
617 MockdBusHandler, Handler>(handlerObj, handler, 0x1, sensorRearmCnt,
618 compSensorCnt, stateField);
619 ASSERT_EQ(rc, 0);
620 ASSERT_EQ(compSensorCnt, 1);
George Liu916808c2021-01-19 17:56:42 +0800621 ASSERT_EQ(stateField[0].sensor_op_state, PLDM_SENSOR_UNAVAILABLE);
622 ASSERT_EQ(stateField[0].present_state, PLDM_SENSOR_NORMAL);
George Liua9170122020-05-14 11:59:13 +0800623 ASSERT_EQ(stateField[0].previous_state, PLDM_SENSOR_UNKNOWN);
George Liu916808c2021-01-19 17:56:42 +0800624 ASSERT_EQ(stateField[0].event_state, PLDM_SENSOR_UNKNOWN);
George Liua9170122020-05-14 11:59:13 +0800625
626 pldm_pdr_destroy(inPDRRepo);
627 pldm_pdr_destroy(outPDRRepo);
628}
629
630TEST(getStateSensorReadingsHandler, testBadRequest)
631{
632 MockdBusHandler mockedUtils;
633 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
634 .Times(1)
635 .WillRepeatedly(Return("foo.bar"));
636
637 auto inPDRRepo = pldm_pdr_init();
638 auto outPDRRepo = pldm_pdr_init();
639 Repo outRepo(outPDRRepo);
Sampa Misra5fb37d52021-03-06 07:26:00 -0600640 auto event = sdeventplus::Event::get_default();
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600641 Handler handler(&mockedUtils, "./pdr_jsons/state_sensor/good", inPDRRepo,
Sampa Misra5fb37d52021-03-06 07:26:00 -0600642 nullptr, nullptr, nullptr, nullptr, event);
George Liua9170122020-05-14 11:59:13 +0800643 Repo inRepo(inPDRRepo);
644 getRepoByType(inRepo, outRepo, PLDM_STATE_SENSOR_PDR);
645 pdr_utils::PdrEntry e;
646 auto record = pdr::getRecordByHandle(outRepo, 2, e);
647 ASSERT_NE(record, nullptr);
648 pldm_state_sensor_pdr* pdr =
649 reinterpret_cast<pldm_state_sensor_pdr*>(e.data);
650 EXPECT_EQ(pdr->hdr.type, PLDM_STATE_SENSOR_PDR);
651
652 std::vector<get_sensor_state_field> stateField;
653 uint8_t compSensorCnt{};
654 uint8_t sensorRearmCnt = 3;
655
656 MockdBusHandler handlerObj;
657 auto rc = platform_state_sensor::getStateSensorReadingsHandler<
658 MockdBusHandler, Handler>(handlerObj, handler, 0x1, sensorRearmCnt,
659 compSensorCnt, stateField);
660 ASSERT_EQ(rc, PLDM_PLATFORM_REARM_UNAVAILABLE_IN_PRESENT_STATE);
Sampa Misra12afe112020-05-25 11:40:44 -0500661
662 pldm_pdr_destroy(inPDRRepo);
663 pldm_pdr_destroy(outPDRRepo);
664}