blob: 700af678755aa175b5301f8989e17001ab662442 [file] [log] [blame]
George Liu4371f2b2020-05-09 19:55:18 +08001#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>
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();
Pavithra Barithaya3aec9972020-12-14 01:55:44 -060037 Handler handler(&mockedUtils, "./pdr_jsons/state_sensor/good", inPDRRepo,
Sampa Misra5fb37d52021-03-06 07:26:00 -060038 nullptr, nullptr, nullptr, nullptr, event);
George Liu4371f2b2020-05-09 19:55:18 +080039 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
72TEST(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);
Sampa Misra5fb37d52021-03-06 07:26:00 -060087 auto event = sdeventplus::Event::get_default();
Pavithra Barithaya3aec9972020-12-14 01:55:44 -060088 Handler handler(&mockedUtils, "./pdr_jsons/state_sensor/good", inPDRRepo,
Sampa Misra5fb37d52021-03-06 07:26:00 -060089 nullptr, nullptr, nullptr, nullptr, event);
George Liu4371f2b2020-05-09 19:55:18 +080090 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}