Add test for the GeneratePDRByStateSensor method
Signed-off-by: George Liu <liuxiwei@inspur.com>
Change-Id: Ie53dfa61e01f8a20b785f4cd305032a965cbaac3
diff --git a/test/libpldmresponder_pdr_sensor_test.cpp b/test/libpldmresponder_pdr_sensor_test.cpp
new file mode 100644
index 0000000..bda9293
--- /dev/null
+++ b/test/libpldmresponder_pdr_sensor_test.cpp
@@ -0,0 +1,97 @@
+#include "libpldm/platform.h"
+
+#include "libpldmresponder/pdr_utils.hpp"
+#include "libpldmresponder/platform.hpp"
+#include "mocked_utils.hpp"
+
+#include <sdbusplus/test/sdbus_mock.hpp>
+
+#include <gtest/gtest.h>
+
+using namespace pldm::responder;
+using namespace pldm::responder::platform;
+using namespace pldm::responder::pdr;
+using namespace pldm::responder::pdr_utils;
+
+using ::testing::_;
+using ::testing::Return;
+using ::testing::StrEq;
+
+TEST(GeneratePDRByStateSensor, testGoodJson)
+{
+ std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
+ requestPayload{};
+ auto req = reinterpret_cast<pldm_msg*>(requestPayload.data());
+ size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
+
+ MockdBusHandler mockedUtils;
+ EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
+ .Times(1)
+ .WillRepeatedly(Return("foo.bar"));
+
+ auto inPDRRepo = pldm_pdr_init();
+ auto outPDRRepo = pldm_pdr_init();
+ Repo outRepo(outPDRRepo);
+ Handler handler(&mockedUtils, "./pdr_jsons/state_sensor/good", "",
+ inPDRRepo, nullptr, nullptr);
+ handler.getPDR(req, requestPayloadLength);
+ Repo inRepo(inPDRRepo);
+ getRepoByType(inRepo, outRepo, PLDM_STATE_SENSOR_PDR);
+
+ // 1 entries
+ ASSERT_EQ(outRepo.getRecordCount(), 1);
+
+ // Check first PDR
+ pdr_utils::PdrEntry e;
+ auto record = pdr::getRecordByHandle(outRepo, 2, e);
+ ASSERT_NE(record, nullptr);
+
+ pldm_state_sensor_pdr* pdr =
+ reinterpret_cast<pldm_state_sensor_pdr*>(e.data);
+ EXPECT_EQ(pdr->hdr.record_handle, 2);
+ EXPECT_EQ(pdr->hdr.version, 1);
+ EXPECT_EQ(pdr->hdr.type, PLDM_STATE_SENSOR_PDR);
+ EXPECT_EQ(pdr->hdr.record_change_num, 0);
+ EXPECT_EQ(pdr->hdr.length, 17);
+
+ EXPECT_EQ(pdr->sensor_id, 1);
+
+ const auto& [dbusMappings, dbusValMaps] =
+ handler.getDbusObjMaps(pdr->sensor_id, TypeId::PLDM_SENSOR_ID);
+ ASSERT_EQ(dbusMappings[0].objectPath, "/foo/bar");
+ ASSERT_EQ(dbusMappings[0].interface, "xyz.openbmc_project.Foo.Bar");
+ ASSERT_EQ(dbusMappings[0].propertyName, "propertyName");
+ ASSERT_EQ(dbusMappings[0].propertyType, "string");
+
+ pldm_pdr_destroy(inPDRRepo);
+ pldm_pdr_destroy(outPDRRepo);
+}
+
+TEST(GeneratePDR, testMalformedJson)
+{
+ std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
+ requestPayload{};
+ auto req = reinterpret_cast<pldm_msg*>(requestPayload.data());
+ size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
+
+ MockdBusHandler mockedUtils;
+ EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
+ .Times(1)
+ .WillRepeatedly(Return("foo.bar"));
+
+ auto inPDRRepo = pldm_pdr_init();
+ auto outPDRRepo = pldm_pdr_init();
+ Repo outRepo(outPDRRepo);
+ Handler handler(&mockedUtils, "./pdr_jsons/state_sensor/good", "",
+ inPDRRepo, nullptr, nullptr);
+ handler.getPDR(req, requestPayloadLength);
+ Repo inRepo(inPDRRepo);
+ getRepoByType(inRepo, outRepo, PLDM_STATE_SENSOR_PDR);
+
+ ASSERT_EQ(outRepo.getRecordCount(), 1);
+ ASSERT_THROW(pdr_utils::readJson("./pdr_jsons/state_sensor/malformed"),
+ std::exception);
+
+ pldm_pdr_destroy(inPDRRepo);
+ pldm_pdr_destroy(outPDRRepo);
+}
diff --git a/test/meson.build b/test/meson.build
index 52a431e..1972b02 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -30,6 +30,7 @@
'libpldmresponder_bios_integer_attribute_test',
'libpldmresponder_bios_enum_attribute_test',
'libpldmresponder_pdr_effecter_test',
+ 'libpldmresponder_pdr_sensor_test',
'libpldmresponder_bios_table_test',
'libpldmresponder_platform_test',
'pldmd_instanceid_test',