| #include "libpldm/base.h" |
| |
| #include "tests/pldm_interface_mock.hpp" |
| #include "util/state_machine/discovery/rde/rde_disc_state_machine.hpp" |
| |
| #include <stdplus/print.hpp> |
| |
| #include <memory> |
| |
| #include <gmock/gmock.h> |
| #include <gtest/gtest.h> |
| |
| using ::testing::_; |
| using ::testing::Return; |
| |
| constexpr int testFd = 3; |
| constexpr int testNetId = 6; |
| constexpr std::string testDeviceId = "1_1_3_1"; |
| |
| class RdeDiscoveryTest : public ::testing::Test |
| { |
| protected: |
| std::shared_ptr<MockPldmInterface> mockInterface = |
| std::make_shared<MockPldmInterface>(); |
| }; |
| |
| TEST_F(RdeDiscoveryTest, StateMachineRunSuccess) |
| { |
| std::unique_ptr<RdeDiscoveryStateMachine> stateMachineRde = |
| std::make_unique<RdeDiscoveryStateMachine>(testFd, testDeviceId, |
| testNetId, mockInterface); |
| |
| EXPECT_CALL(*mockInterface, pldmSendAtNetwork(_, _, _, _, _)) |
| .WillOnce( |
| Return(PLDM_REQUESTER_SUCCESS)) // send for Negotiate Redfish Params |
| .WillOnce(Return(PLDM_REQUESTER_SUCCESS)); // send for Medium Params |
| |
| EXPECT_CALL(*mockInterface, pldmRecvAtNetwork(_, _, _, _, _, _)) |
| .WillOnce(Return( |
| PLDM_REQUESTER_SUCCESS)) // receive for Negotiate Redfish Params |
| .WillOnce(Return(PLDM_REQUESTER_SUCCESS)); // receive for Medium Params |
| OperationStatus status = stateMachineRde.get()->run(); |
| stdplus::println(stderr, "Status: {}", static_cast<int>(status)); |
| // All invocations tested with success code (State machine runs fine) |
| EXPECT_EQ(status, OperationStatus::Success); |
| } |
| |
| TEST_F(RdeDiscoveryTest, StateMachineRunReceiveFails) |
| { |
| // Fails at the first receive and hence discovery should fail and no more |
| // calls to be made to the interface |
| std::unique_ptr<RdeDiscoveryStateMachine> stateMachineRde = |
| std::make_unique<RdeDiscoveryStateMachine>(testFd, testDeviceId, |
| testNetId, mockInterface); |
| |
| EXPECT_CALL(*mockInterface, pldmSendAtNetwork(_, _, _, _, _)) |
| .WillOnce(Return(PLDM_REQUESTER_SUCCESS)); // send for Negotiate Redfish |
| |
| EXPECT_CALL(*mockInterface, pldmRecvAtNetwork(_, _, _, _, _, _)) |
| .WillOnce(Return( |
| PLDM_REQUESTER_RESP_MSG_TOO_SMALL)); // receive fails for negotiate |
| // redfish params |
| |
| // Since negotiate redfish params fail hence next command not invoked |
| OperationStatus status = stateMachineRde.get()->run(); |
| stdplus::println(stderr, "Status: {}", static_cast<int>(status)); |
| |
| // Receive failure - hence discovery fails |
| EXPECT_EQ(status, OperationStatus::PldmRecvFailure); |
| } |
| |
| TEST_F(RdeDiscoveryTest, StateMachineRunSendFails) |
| { |
| // Fails at the first send and hence discovery should fail and no more |
| // calls to be made to the interface |
| std::unique_ptr<RdeDiscoveryStateMachine> stateMachineRde = |
| std::make_unique<RdeDiscoveryStateMachine>(testFd, testDeviceId, |
| testNetId, mockInterface); |
| |
| // send for Negotiate Redfish Params fails |
| EXPECT_CALL(*mockInterface, pldmSendAtNetwork(_, _, _, _, _)) |
| .WillOnce(Return(PLDM_REQUESTER_SEND_FAIL)); |
| |
| // No receiveFrom calls are expected as it failed on first send |
| OperationStatus status = stateMachineRde.get()->run(); |
| stdplus::println(stderr, "Status: {}", static_cast<int>(status)); |
| |
| // Send failure - hence discovery fails |
| EXPECT_EQ(status, OperationStatus::PldmSendFailure); |
| } |