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, 0, nullptr, "./pdr_jsons/state_sensor/good",
38                     inPDRRepo, nullptr, nullptr, nullptr, nullptr, nullptr,
39                     event);
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);
88     auto event = sdeventplus::Event::get_default();
89     Handler handler(&mockedUtils, 0, nullptr, "./pdr_jsons/state_sensor/good",
90                     inPDRRepo, nullptr, nullptr, nullptr, nullptr, nullptr,
91                     event);
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 }
103