| #include "libpldm/base.h" |
| |
| #include "tests/pldm_interface_mock.hpp" |
| #include "util/state_machine/discovery/base/base_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 BaseDiscoveryTest : public ::testing::Test |
| { |
| protected: |
| std::shared_ptr<MockPldmInterface> mockInterface = |
| std::make_shared<MockPldmInterface>(); |
| }; |
| |
| TEST_F(BaseDiscoveryTest, StateMachineRunSuccess) |
| { |
| std::unique_ptr<BaseDiscoveryStateMachine> stateMachineBase = |
| std::make_unique<BaseDiscoveryStateMachine>(testFd, testDeviceId, |
| testNetId, mockInterface); |
| |
| EXPECT_CALL(*mockInterface, pldmSendAtNetwork(_, _, _, _, _)) |
| .WillOnce(Return(PLDM_REQUESTER_SUCCESS)) // send for getTid |
| .WillOnce(Return(PLDM_REQUESTER_SUCCESS)) // send for getTypes |
| .WillOnce(Return(PLDM_REQUESTER_SUCCESS)) // send for getVersions |
| .WillOnce(Return(PLDM_REQUESTER_SUCCESS)); // send for getCommands |
| |
| EXPECT_CALL(*mockInterface, pldmRecvAtNetwork(_, _, _, _, _, _)) |
| .WillOnce(Return(PLDM_REQUESTER_SUCCESS)) // receive for getTid |
| .WillOnce(Return(PLDM_REQUESTER_SUCCESS)) // receive for getTypes |
| .WillOnce(Return(PLDM_REQUESTER_SUCCESS)) // receive for getVersions |
| .WillOnce(Return(PLDM_REQUESTER_SUCCESS)); // receive for getCommands |
| |
| OperationStatus status = stateMachineBase.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(BaseDiscoveryTest, StateMachineRunReceiveFails) |
| { |
| // Fails at the second receive and hence discovery should fail and no more |
| // calls to be made to the interface |
| std::unique_ptr<BaseDiscoveryStateMachine> stateMachineBase = |
| std::make_unique<BaseDiscoveryStateMachine>(testFd, testDeviceId, |
| testNetId, mockInterface); |
| |
| EXPECT_CALL(*mockInterface, pldmSendAtNetwork(_, _, _, _, _)) |
| .WillOnce(Return(PLDM_REQUESTER_SUCCESS)) // send for getTid |
| .WillOnce(Return(PLDM_REQUESTER_SUCCESS)); // send for getTypes |
| |
| EXPECT_CALL(*mockInterface, pldmRecvAtNetwork(_, _, _, _, _, _)) |
| .WillOnce(Return(PLDM_REQUESTER_SUCCESS)) // receive for getTid |
| .WillOnce(Return( |
| PLDM_REQUESTER_RESP_MSG_TOO_SMALL)); // receive fails for getTypes |
| |
| OperationStatus status = stateMachineBase.get()->run(); |
| stdplus::println(stderr, "Status: {}", static_cast<int>(status)); |
| |
| // Receive failure - hence discovery fails |
| EXPECT_EQ(status, OperationStatus::PldmRecvFailure); |
| } |
| |
| TEST_F(BaseDiscoveryTest, StateMachineRunSendFails) |
| { |
| // Fails at the first send and hence discovery should fail and no more |
| // calls to be made to the interface |
| std::unique_ptr<BaseDiscoveryStateMachine> stateMachineBase = |
| std::make_unique<BaseDiscoveryStateMachine>(testFd, testDeviceId, |
| testNetId, mockInterface); |
| |
| EXPECT_CALL(*mockInterface, pldmSendAtNetwork(_, _, _, _, _)) |
| .WillOnce(Return(PLDM_REQUESTER_SEND_FAIL)); // send for getTid fails |
| |
| // No receiveFrom calls are expected as it failed on first send |
| |
| OperationStatus status = stateMachineBase.get()->run(); |
| stdplus::println(stderr, "Status: {}", static_cast<int>(status)); |
| |
| // Send failure - hence discovery fails |
| EXPECT_EQ(status, OperationStatus::PldmSendFailure); |
| } |