blob: 92da0662c703f461c8869d6f297d4606bf97d26a [file] [log] [blame]
Tom Joseph53279882021-04-28 06:29:13 -07001#include "common/test/mocked_utils.hpp"
George Liu4371f2b2020-05-09 19:55:18 +08002#include "libpldmresponder/pdr_utils.hpp"
3#include "libpldmresponder/platform.hpp"
George Liu4371f2b2020-05-09 19:55:18 +08004
George Liuc453e162022-12-21 17:16:23 +08005#include <libpldm/platform.h>
6
George Liu4371f2b2020-05-09 19:55:18 +08007#include <sdbusplus/test/sdbus_mock.hpp>
Sampa Misra5fb37d52021-03-06 07:26:00 -06008#include <sdeventplus/event.hpp>
George Liu4371f2b2020-05-09 19:55:18 +08009
10#include <gtest/gtest.h>
11
12using namespace pldm::responder;
13using namespace pldm::responder::platform;
14using namespace pldm::responder::pdr;
15using namespace pldm::responder::pdr_utils;
16
17using ::testing::_;
18using ::testing::Return;
19using ::testing::StrEq;
20
21TEST(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 Misra5fb37d52021-03-06 07:26:00 -060036 auto event = sdeventplus::Event::get_default();
Sagar Srinivas90314a32023-10-17 10:38:03 -050037 Handler handler(&mockedUtils, 0, nullptr, "./pdr_jsons/state_sensor/good",
38 inPDRRepo, nullptr, nullptr, nullptr, nullptr, nullptr,
Kamalkumar Patel3c50c822024-01-30 07:14:40 -060039 nullptr, event);
George Liu4371f2b2020-05-09 19:55:18 +080040 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
73TEST(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 Misra5fb37d52021-03-06 07:26:00 -060088 auto event = sdeventplus::Event::get_default();
Sagar Srinivas90314a32023-10-17 10:38:03 -050089 Handler handler(&mockedUtils, 0, nullptr, "./pdr_jsons/state_sensor/good",
90 inPDRRepo, nullptr, nullptr, nullptr, nullptr, nullptr,
Kamalkumar Patel3c50c822024-01-30 07:14:40 -060091 nullptr, event);
George Liu4371f2b2020-05-09 19:55:18 +080092 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}