xref: /openbmc/phosphor-ipmi-flash/tools/test/tools_helper_unittest.cpp (revision 42a44c281cce08be0ca6251955f4fb73d30c8ced)
101123b2aSPatrick Venture #include "helper.hpp"
201123b2aSPatrick Venture #include "status.hpp"
3f88bcf3bSWilliam A. Kennington III #include "tool_errors.hpp"
401123b2aSPatrick Venture 
5328f520fSJie Yang #include <blobs-ipmid/blobs.hpp>
601123b2aSPatrick Venture #include <ipmiblob/test/blob_interface_mock.hpp>
701123b2aSPatrick Venture 
89b37b095SPatrick Venture #include <cstdint>
99b37b095SPatrick Venture 
1001123b2aSPatrick Venture #include <gtest/gtest.h>
1101123b2aSPatrick Venture 
1201123b2aSPatrick Venture namespace host_tool
1301123b2aSPatrick Venture {
1401123b2aSPatrick Venture using ::testing::Return;
1501123b2aSPatrick Venture using ::testing::TypedEq;
1601123b2aSPatrick Venture 
17c7fa2c28SVivekanand Veeracholan class HelperTest : public ::testing::Test
1801123b2aSPatrick Venture {
1901123b2aSPatrick Venture   protected:
2001123b2aSPatrick Venture     ipmiblob::BlobInterfaceMock blobMock;
2101123b2aSPatrick Venture     std::uint16_t session = 0xbeef;
2201123b2aSPatrick Venture };
2301123b2aSPatrick Venture 
TEST_F(HelperTest,PollStatusReturnsAfterSuccess)24c7fa2c28SVivekanand Veeracholan TEST_F(HelperTest, PollStatusReturnsAfterSuccess)
2501123b2aSPatrick Venture {
2601123b2aSPatrick Venture     ipmiblob::StatResponse verificationResponse = {};
2701123b2aSPatrick Venture     /* the other details of the response are ignored, and should be. */
2801123b2aSPatrick Venture     verificationResponse.metadata.push_back(
2901123b2aSPatrick Venture         static_cast<std::uint8_t>(ipmi_flash::ActionStatus::success));
3001123b2aSPatrick Venture 
3101123b2aSPatrick Venture     EXPECT_CALL(blobMock, getStat(TypedEq<std::uint16_t>(session)))
3201123b2aSPatrick Venture         .WillOnce(Return(verificationResponse));
3301123b2aSPatrick Venture 
34f88bcf3bSWilliam A. Kennington III     EXPECT_NO_THROW(pollStatus(session, &blobMock));
3501123b2aSPatrick Venture }
3601123b2aSPatrick Venture 
TEST_F(HelperTest,PollStatusReturnsAfterFailure)37c7fa2c28SVivekanand Veeracholan TEST_F(HelperTest, PollStatusReturnsAfterFailure)
3801123b2aSPatrick Venture {
3901123b2aSPatrick Venture     ipmiblob::StatResponse verificationResponse = {};
4001123b2aSPatrick Venture     /* the other details of the response are ignored, and should be. */
4101123b2aSPatrick Venture     verificationResponse.metadata.push_back(
4201123b2aSPatrick Venture         static_cast<std::uint8_t>(ipmi_flash::ActionStatus::failed));
4301123b2aSPatrick Venture 
4401123b2aSPatrick Venture     EXPECT_CALL(blobMock, getStat(TypedEq<std::uint16_t>(session)))
4501123b2aSPatrick Venture         .WillOnce(Return(verificationResponse));
4601123b2aSPatrick Venture 
47f88bcf3bSWilliam A. Kennington III     EXPECT_THROW(pollStatus(session, &blobMock), ToolException);
4801123b2aSPatrick Venture }
4901123b2aSPatrick Venture 
TEST_F(HelperTest,PollReadReadyReturnsAfterSuccess)50328f520fSJie Yang TEST_F(HelperTest, PollReadReadyReturnsAfterSuccess)
51328f520fSJie Yang {
52328f520fSJie Yang     ipmiblob::StatResponse blobResponse = {};
53328f520fSJie Yang     /* the other details of the response are ignored, and should be. */
54*42a44c28SPatrick Williams     blobResponse.blob_state =
55*42a44c28SPatrick Williams         blobs::StateFlags::open_read | blobs::StateFlags::committed;
56328f520fSJie Yang 
57328f520fSJie Yang     EXPECT_CALL(blobMock, getStat(TypedEq<std::uint16_t>(session)))
58328f520fSJie Yang         .WillOnce(Return(blobResponse));
59328f520fSJie Yang 
60f88bcf3bSWilliam A. Kennington III     EXPECT_NO_THROW(pollReadReady(session, &blobMock));
61328f520fSJie Yang }
62328f520fSJie Yang 
TEST_F(HelperTest,PollReadReadyReturnsAfterFailure)63328f520fSJie Yang TEST_F(HelperTest, PollReadReadyReturnsAfterFailure)
64328f520fSJie Yang {
65328f520fSJie Yang     ipmiblob::StatResponse blobResponse = {};
66328f520fSJie Yang     /* the other details of the response are ignored, and should be. */
67328f520fSJie Yang     blobResponse.blob_state = blobs::StateFlags::commit_error;
68328f520fSJie Yang 
69328f520fSJie Yang     EXPECT_CALL(blobMock, getStat(TypedEq<std::uint16_t>(session)))
70328f520fSJie Yang         .WillOnce(Return(blobResponse));
71328f520fSJie Yang 
72f88bcf3bSWilliam A. Kennington III     EXPECT_THROW(pollReadReady(session, &blobMock), ToolException);
73328f520fSJie Yang }
74328f520fSJie Yang 
TEST_F(HelperTest,PollReadReadyReturnsAfterRetrySuccess)75328f520fSJie Yang TEST_F(HelperTest, PollReadReadyReturnsAfterRetrySuccess)
76328f520fSJie Yang {
77328f520fSJie Yang     ipmiblob::StatResponse blobResponseRunning = {};
78328f520fSJie Yang     /* the other details of the response are ignored, and should be. */
79328f520fSJie Yang     blobResponseRunning.blob_state = blobs::StateFlags::committing;
80328f520fSJie Yang 
81328f520fSJie Yang     ipmiblob::StatResponse blobResponseReadReady = {};
82328f520fSJie Yang     /* the other details of the response are ignored, and should be. */
83328f520fSJie Yang     blobResponseReadReady.blob_state = blobs::StateFlags::open_read;
84328f520fSJie Yang 
85328f520fSJie Yang     EXPECT_CALL(blobMock, getStat(TypedEq<std::uint16_t>(session)))
86328f520fSJie Yang         .WillOnce(Return(blobResponseRunning))
87328f520fSJie Yang         .WillOnce(Return(blobResponseReadReady));
88328f520fSJie Yang 
89f88bcf3bSWilliam A. Kennington III     EXPECT_NO_THROW(pollReadReady(session, &blobMock));
90328f520fSJie Yang }
91328f520fSJie Yang 
TEST_F(HelperTest,PollReadReadyReturnsAfterRetryFailure)92328f520fSJie Yang TEST_F(HelperTest, PollReadReadyReturnsAfterRetryFailure)
93328f520fSJie Yang {
94328f520fSJie Yang     ipmiblob::StatResponse blobResponseRunning = {};
95328f520fSJie Yang     /* the other details of the response are ignored, and should be. */
96328f520fSJie Yang     blobResponseRunning.blob_state = blobs::StateFlags::committing;
97328f520fSJie Yang 
98328f520fSJie Yang     ipmiblob::StatResponse blobResponseError = {};
99328f520fSJie Yang     /* the other details of the response are ignored, and should be. */
100328f520fSJie Yang     blobResponseError.blob_state = blobs::StateFlags::commit_error;
101328f520fSJie Yang 
102328f520fSJie Yang     EXPECT_CALL(blobMock, getStat(TypedEq<std::uint16_t>(session)))
103328f520fSJie Yang         .WillOnce(Return(blobResponseRunning))
104328f520fSJie Yang         .WillOnce(Return(blobResponseError));
105328f520fSJie Yang 
106f88bcf3bSWilliam A. Kennington III     EXPECT_THROW(pollReadReady(session, &blobMock), ToolException);
107328f520fSJie Yang }
108328f520fSJie Yang 
TEST_F(HelperTest,MemcpyAlignedOneByte)109c7fa2c28SVivekanand Veeracholan TEST_F(HelperTest, MemcpyAlignedOneByte)
110c7fa2c28SVivekanand Veeracholan {
111c7fa2c28SVivekanand Veeracholan     const char source = 'a';
112c7fa2c28SVivekanand Veeracholan     char destination;
113c7fa2c28SVivekanand Veeracholan 
114c7fa2c28SVivekanand Veeracholan     EXPECT_EQ(&destination,
115c7fa2c28SVivekanand Veeracholan               memcpyAligned(&destination, &source, sizeof(source)));
116c7fa2c28SVivekanand Veeracholan     EXPECT_EQ(destination, source);
117c7fa2c28SVivekanand Veeracholan }
118c7fa2c28SVivekanand Veeracholan 
TEST_F(HelperTest,MemcpyAlignedMultiByte)119c7fa2c28SVivekanand Veeracholan TEST_F(HelperTest, MemcpyAlignedMultiByte)
120c7fa2c28SVivekanand Veeracholan {
121c7fa2c28SVivekanand Veeracholan     const char source[14] = "abcdefghijklm";
122c7fa2c28SVivekanand Veeracholan     char destination[14] = "xxxxxxxxxxxxx";
123c7fa2c28SVivekanand Veeracholan 
124c7fa2c28SVivekanand Veeracholan     EXPECT_EQ(&destination,
125c7fa2c28SVivekanand Veeracholan               memcpyAligned(&destination, &source, sizeof(source)));
126c7fa2c28SVivekanand Veeracholan     EXPECT_EQ(0, memcmp(&destination, &source, sizeof(source)));
127c7fa2c28SVivekanand Veeracholan }
128c7fa2c28SVivekanand Veeracholan 
12901123b2aSPatrick Venture } // namespace host_tool
130