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