1 #include "common/test/mocked_utils.hpp"
2 #include "libpldmresponder/pdr_utils.hpp"
3 #include "libpldmresponder/platform.hpp"
4 
5 #include <libpldm/platform.h>
6 
7 #include <sdbusplus/test/sdbus_mock.hpp>
8 #include <sdeventplus/event.hpp>
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);
36     auto event = sdeventplus::Event::get_default();
37     Handler handler(&mockedUtils, "./pdr_jsons/state_sensor/good", inPDRRepo,
38                     nullptr, nullptr, nullptr, nullptr, event);
39     handler.getPDR(req, requestPayloadLength);
40     Repo inRepo(inPDRRepo);
41     getRepoByType(inRepo, outRepo, PLDM_STATE_SENSOR_PDR);
42 
43     // 1 entries
44     ASSERT_EQ(outRepo.getRecordCount(), 1);
45 
46     // Check first PDR
47     pdr_utils::PdrEntry e;
48     auto record = pdr::getRecordByHandle(outRepo, 2, e);
49     ASSERT_NE(record, nullptr);
50 
51     pldm_state_sensor_pdr* pdr =
52         reinterpret_cast<pldm_state_sensor_pdr*>(e.data);
53     EXPECT_EQ(pdr->hdr.record_handle, 2);
54     EXPECT_EQ(pdr->hdr.version, 1);
55     EXPECT_EQ(pdr->hdr.type, PLDM_STATE_SENSOR_PDR);
56     EXPECT_EQ(pdr->hdr.record_change_num, 0);
57     EXPECT_EQ(pdr->hdr.length, 17);
58 
59     EXPECT_EQ(pdr->sensor_id, 1);
60 
61     const auto& [dbusMappings, dbusValMaps] =
62         handler.getDbusObjMaps(pdr->sensor_id, TypeId::PLDM_SENSOR_ID);
63     ASSERT_EQ(dbusMappings[0].objectPath, "/foo/bar");
64     ASSERT_EQ(dbusMappings[0].interface, "xyz.openbmc_project.Foo.Bar");
65     ASSERT_EQ(dbusMappings[0].propertyName, "propertyName");
66     ASSERT_EQ(dbusMappings[0].propertyType, "string");
67 
68     pldm_pdr_destroy(inPDRRepo);
69     pldm_pdr_destroy(outPDRRepo);
70 }
71 
72 TEST(GeneratePDR, testMalformedJson)
73 {
74     std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
75         requestPayload{};
76     auto req = reinterpret_cast<pldm_msg*>(requestPayload.data());
77     size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
78 
79     MockdBusHandler mockedUtils;
80     EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
81         .Times(1)
82         .WillRepeatedly(Return("foo.bar"));
83 
84     auto inPDRRepo = pldm_pdr_init();
85     auto outPDRRepo = pldm_pdr_init();
86     Repo outRepo(outPDRRepo);
87     auto event = sdeventplus::Event::get_default();
88     Handler handler(&mockedUtils, "./pdr_jsons/state_sensor/good", inPDRRepo,
89                     nullptr, nullptr, nullptr, nullptr, event);
90     handler.getPDR(req, requestPayloadLength);
91     Repo inRepo(inPDRRepo);
92     getRepoByType(inRepo, outRepo, PLDM_STATE_SENSOR_PDR);
93 
94     ASSERT_EQ(outRepo.getRecordCount(), 1);
95     ASSERT_THROW(pdr_utils::readJson("./pdr_jsons/state_sensor/malformed"),
96                  std::exception);
97 
98     pldm_pdr_destroy(inPDRRepo);
99     pldm_pdr_destroy(outPDRRepo);
100 }
101