1 #include "libpldm/entity.h" 2 3 #include "common/utils.hpp" 4 #include "libpldmresponder/event_parser.hpp" 5 #include "libpldmresponder/pdr.hpp" 6 #include "libpldmresponder/pdr_utils.hpp" 7 #include "libpldmresponder/platform.hpp" 8 #include "oem/ibm/libpldmresponder/inband_code_update.hpp" 9 #include "oem/ibm/libpldmresponder/oem_ibm_handler.hpp" 10 #include "test/mocked_utils.hpp" 11 12 #include <iostream> 13 14 using namespace pldm::utils; 15 using namespace pldm::responder; 16 using namespace pldm::responder::pdr; 17 using namespace pldm::responder::pdr_utils; 18 using namespace pldm::responder::oem_ibm_platform; 19 20 class MockCodeUpdate : public CodeUpdate 21 { 22 public: 23 MockCodeUpdate(const pldm::utils::DBusHandler* dBusIntf) : 24 CodeUpdate(dBusIntf) 25 {} 26 27 MOCK_METHOD(void, setVersions, (), (override)); 28 }; 29 30 TEST(oemSetStateEffecterStatesHandler, testGoodRequest) 31 { 32 uint16_t entityID_ = PLDM_OEM_IBM_ENTITY_FIRMWARE_UPDATE; 33 uint16_t stateSetId_ = PLDM_OEM_IBM_BOOT_STATE; 34 uint16_t entityInstance_ = 0; 35 uint8_t compSensorCnt_ = 1; 36 sdbusplus::bus::bus bus(sdbusplus::bus::new_default()); 37 Requester requester(bus, "/abc/def"); 38 39 std::vector<get_sensor_state_field> stateField; 40 41 auto mockDbusHandler = std::make_unique<MockdBusHandler>(); 42 std::unique_ptr<CodeUpdate> mockCodeUpdate = 43 std::make_unique<MockCodeUpdate>(mockDbusHandler.get()); 44 std::unique_ptr<oem_platform::Handler> oemPlatformHandler{}; 45 46 oemPlatformHandler = std::make_unique<oem_ibm_platform::Handler>( 47 mockDbusHandler.get(), mockCodeUpdate.get(), 0x1, 0x9, requester); 48 49 auto rc = oemPlatformHandler->getOemStateSensorReadingsHandler( 50 entityID_, entityInstance_, stateSetId_, compSensorCnt_, stateField); 51 52 ASSERT_EQ(rc, PLDM_SUCCESS); 53 ASSERT_EQ(stateField.size(), 1); 54 ASSERT_EQ(stateField[0].event_state, tSideNum); 55 ASSERT_EQ(stateField[0].sensor_op_state, PLDM_SENSOR_ENABLED); 56 ASSERT_EQ(stateField[0].present_state, PLDM_SENSOR_UNKNOWN); 57 ASSERT_EQ(stateField[0].previous_state, PLDM_SENSOR_UNKNOWN); 58 59 entityInstance_ = 1; 60 61 std::vector<get_sensor_state_field> stateField1; 62 rc = oemPlatformHandler->getOemStateSensorReadingsHandler( 63 entityID_, entityInstance_, stateSetId_, compSensorCnt_, stateField1); 64 ASSERT_EQ(stateField1.size(), 1); 65 ASSERT_EQ(stateField1[0].event_state, tSideNum); 66 67 entityInstance_ = 2; 68 rc = oemPlatformHandler->getOemStateSensorReadingsHandler( 69 entityID_, entityInstance_, stateSetId_, compSensorCnt_, stateField1); 70 ASSERT_EQ(stateField1[0].event_state, PLDM_SENSOR_UNKNOWN); 71 72 entityID_ = 40; 73 stateSetId_ = 50; 74 rc = oemPlatformHandler->getOemStateSensorReadingsHandler( 75 entityID_, entityInstance_, stateSetId_, compSensorCnt_, stateField1); 76 ASSERT_EQ(rc, PLDM_PLATFORM_INVALID_STATE_VALUE); 77 78 entityID_ = PLDM_OEM_IBM_ENTITY_FIRMWARE_UPDATE; 79 entityInstance_ = 0; 80 stateSetId_ = PLDM_OEM_IBM_BOOT_STATE; 81 compSensorCnt_ = 1; 82 83 std::vector<set_effecter_state_field> setEffecterStateField; 84 setEffecterStateField.push_back({PLDM_REQUEST_SET, pSideNum}); 85 86 rc = oemPlatformHandler->oemSetStateEffecterStatesHandler( 87 entityID_, entityInstance_, stateSetId_, compSensorCnt_, 88 setEffecterStateField); 89 ASSERT_EQ(rc, PLDM_SUCCESS); 90 91 entityInstance_ = 2; 92 rc = oemPlatformHandler->oemSetStateEffecterStatesHandler( 93 entityID_, entityInstance_, stateSetId_, compSensorCnt_, 94 setEffecterStateField); 95 96 ASSERT_EQ(rc, PLDM_PLATFORM_INVALID_STATE_VALUE); 97 98 entityID_ = 34; 99 stateSetId_ = 99; 100 entityInstance_ = 0; 101 rc = oemPlatformHandler->oemSetStateEffecterStatesHandler( 102 entityID_, entityInstance_, stateSetId_, compSensorCnt_, 103 setEffecterStateField); 104 ASSERT_EQ(rc, PLDM_PLATFORM_SET_EFFECTER_UNSUPPORTED_SENSORSTATE); 105 } 106 107 TEST(EncodeCodeUpdateEvent, testGoodRequest) 108 { 109 size_t sensorEventSize = PLDM_SENSOR_EVENT_DATA_MIN_LENGTH + 1; 110 std::vector<uint8_t> sensorEventDataVec{}; 111 sensorEventDataVec.resize(sensorEventSize); 112 113 auto eventData = reinterpret_cast<struct pldm_sensor_event_data*>( 114 sensorEventDataVec.data()); 115 eventData->sensor_id = 0xA; 116 eventData->sensor_event_class_type = PLDM_SENSOR_OP_STATE; 117 118 auto opStateSensorEventData = 119 reinterpret_cast<struct pldm_sensor_event_sensor_op_state*>( 120 sensorEventDataVec.data()); 121 opStateSensorEventData->present_op_state = uint8_t(CodeUpdateState::START); 122 opStateSensorEventData->previous_op_state = uint8_t(CodeUpdateState::END); 123 124 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) + 125 PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES + 126 sensorEventDataVec.size()); 127 128 auto rc = 129 encodeEventMsg(PLDM_SENSOR_EVENT, sensorEventDataVec, requestMsg, 0x1); 130 131 EXPECT_EQ(rc, PLDM_SUCCESS); 132 } 133 134 TEST(EncodeCodeUpdate, testBadRequest) 135 { 136 std::vector<uint8_t> requestMsg; 137 std::vector<uint8_t> sensorEventDataVec{}; 138 139 auto rc = 140 encodeEventMsg(PLDM_SENSOR_EVENT, sensorEventDataVec, requestMsg, 0x1); 141 142 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 143 } 144 145 TEST(clearDirPath, testClearDirPath) 146 { 147 char dirPath[] = "/tmp/testClearDir/"; 148 fs::path dir(dirPath); 149 fs::create_directories(dir); 150 struct stat buffer; 151 ASSERT_EQ(stat(dirPath, &buffer), 0); 152 char filePath[] = "/tmp/testClearDir/file.txt"; 153 std::ofstream file(filePath); 154 ASSERT_EQ(stat(filePath, &buffer), 0); 155 156 auto mockDbusHandler = std::make_unique<MockdBusHandler>(); 157 std::unique_ptr<CodeUpdate> mockCodeUpdate = 158 std::make_unique<MockCodeUpdate>(mockDbusHandler.get()); 159 160 mockCodeUpdate->clearDirPath(dirPath); 161 ASSERT_EQ(stat(filePath, &buffer), -1); 162 ASSERT_EQ(stat(dirPath, &buffer), 0); 163 } 164