Tom Joseph | 5327988 | 2021-04-28 06:29:13 -0700 | [diff] [blame] | 1 | #include "common/test/mocked_utils.hpp" |
George Liu | 4371f2b | 2020-05-09 19:55:18 +0800 | [diff] [blame] | 2 | #include "libpldmresponder/pdr_utils.hpp" |
| 3 | #include "libpldmresponder/platform.hpp" |
George Liu | 4371f2b | 2020-05-09 19:55:18 +0800 | [diff] [blame] | 4 | |
George Liu | c453e16 | 2022-12-21 17:16:23 +0800 | [diff] [blame] | 5 | #include <libpldm/platform.h> |
| 6 | |
George Liu | 4371f2b | 2020-05-09 19:55:18 +0800 | [diff] [blame] | 7 | #include <sdbusplus/test/sdbus_mock.hpp> |
Sampa Misra | 5fb37d5 | 2021-03-06 07:26:00 -0600 | [diff] [blame] | 8 | #include <sdeventplus/event.hpp> |
George Liu | 4371f2b | 2020-05-09 19:55:18 +0800 | [diff] [blame] | 9 | |
| 10 | #include <gtest/gtest.h> |
| 11 | |
| 12 | using namespace pldm::responder; |
| 13 | using namespace pldm::responder::platform; |
| 14 | using namespace pldm::responder::pdr; |
| 15 | using namespace pldm::responder::pdr_utils; |
| 16 | |
| 17 | using ::testing::_; |
| 18 | using ::testing::Return; |
| 19 | using ::testing::StrEq; |
| 20 | |
| 21 | TEST(GeneratePDRByStateSensor, testGoodJson) |
| 22 | { |
| 23 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES> |
| 24 | requestPayload{}; |
| 25 | auto req = reinterpret_cast<pldm_msg*>(requestPayload.data()); |
| 26 | size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr); |
| 27 | |
| 28 | MockdBusHandler mockedUtils; |
| 29 | EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _)) |
| 30 | .Times(1) |
| 31 | .WillRepeatedly(Return("foo.bar")); |
| 32 | |
| 33 | auto inPDRRepo = pldm_pdr_init(); |
| 34 | auto outPDRRepo = pldm_pdr_init(); |
| 35 | Repo outRepo(outPDRRepo); |
Sampa Misra | 5fb37d5 | 2021-03-06 07:26:00 -0600 | [diff] [blame] | 36 | auto event = sdeventplus::Event::get_default(); |
Sagar Srinivas | 90314a3 | 2023-10-17 10:38:03 -0500 | [diff] [blame] | 37 | Handler handler(&mockedUtils, 0, nullptr, "./pdr_jsons/state_sensor/good", |
| 38 | inPDRRepo, nullptr, nullptr, nullptr, nullptr, nullptr, |
George Liu | a881c17 | 2021-06-21 18:28:11 +0800 | [diff] [blame] | 39 | event); |
George Liu | 4371f2b | 2020-05-09 19:55:18 +0800 | [diff] [blame] | 40 | handler.getPDR(req, requestPayloadLength); |
| 41 | Repo inRepo(inPDRRepo); |
| 42 | getRepoByType(inRepo, outRepo, PLDM_STATE_SENSOR_PDR); |
| 43 | |
| 44 | // 1 entries |
| 45 | ASSERT_EQ(outRepo.getRecordCount(), 1); |
| 46 | |
| 47 | // Check first PDR |
| 48 | pdr_utils::PdrEntry e; |
| 49 | auto record = pdr::getRecordByHandle(outRepo, 2, e); |
| 50 | ASSERT_NE(record, nullptr); |
| 51 | |
| 52 | pldm_state_sensor_pdr* pdr = |
| 53 | reinterpret_cast<pldm_state_sensor_pdr*>(e.data); |
| 54 | EXPECT_EQ(pdr->hdr.record_handle, 2); |
| 55 | EXPECT_EQ(pdr->hdr.version, 1); |
| 56 | EXPECT_EQ(pdr->hdr.type, PLDM_STATE_SENSOR_PDR); |
| 57 | EXPECT_EQ(pdr->hdr.record_change_num, 0); |
| 58 | EXPECT_EQ(pdr->hdr.length, 17); |
| 59 | |
| 60 | EXPECT_EQ(pdr->sensor_id, 1); |
| 61 | |
| 62 | const auto& [dbusMappings, dbusValMaps] = |
| 63 | handler.getDbusObjMaps(pdr->sensor_id, TypeId::PLDM_SENSOR_ID); |
| 64 | ASSERT_EQ(dbusMappings[0].objectPath, "/foo/bar"); |
| 65 | ASSERT_EQ(dbusMappings[0].interface, "xyz.openbmc_project.Foo.Bar"); |
| 66 | ASSERT_EQ(dbusMappings[0].propertyName, "propertyName"); |
| 67 | ASSERT_EQ(dbusMappings[0].propertyType, "string"); |
| 68 | |
| 69 | pldm_pdr_destroy(inPDRRepo); |
| 70 | pldm_pdr_destroy(outPDRRepo); |
| 71 | } |
| 72 | |
| 73 | TEST(GeneratePDR, testMalformedJson) |
| 74 | { |
| 75 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES> |
| 76 | requestPayload{}; |
| 77 | auto req = reinterpret_cast<pldm_msg*>(requestPayload.data()); |
| 78 | size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr); |
| 79 | |
| 80 | MockdBusHandler mockedUtils; |
| 81 | EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _)) |
| 82 | .Times(1) |
| 83 | .WillRepeatedly(Return("foo.bar")); |
| 84 | |
| 85 | auto inPDRRepo = pldm_pdr_init(); |
| 86 | auto outPDRRepo = pldm_pdr_init(); |
| 87 | Repo outRepo(outPDRRepo); |
Sampa Misra | 5fb37d5 | 2021-03-06 07:26:00 -0600 | [diff] [blame] | 88 | auto event = sdeventplus::Event::get_default(); |
Sagar Srinivas | 90314a3 | 2023-10-17 10:38:03 -0500 | [diff] [blame] | 89 | Handler handler(&mockedUtils, 0, nullptr, "./pdr_jsons/state_sensor/good", |
| 90 | inPDRRepo, nullptr, nullptr, nullptr, nullptr, nullptr, |
George Liu | a881c17 | 2021-06-21 18:28:11 +0800 | [diff] [blame] | 91 | event); |
George Liu | 4371f2b | 2020-05-09 19:55:18 +0800 | [diff] [blame] | 92 | handler.getPDR(req, requestPayloadLength); |
| 93 | Repo inRepo(inPDRRepo); |
| 94 | getRepoByType(inRepo, outRepo, PLDM_STATE_SENSOR_PDR); |
| 95 | |
| 96 | ASSERT_EQ(outRepo.getRecordCount(), 1); |
| 97 | ASSERT_THROW(pdr_utils::readJson("./pdr_jsons/state_sensor/malformed"), |
| 98 | std::exception); |
| 99 | |
| 100 | pldm_pdr_destroy(inPDRRepo); |
| 101 | pldm_pdr_destroy(outPDRRepo); |
| 102 | } |