xref: /openbmc/phosphor-ipmi-flash/tools/test/tools_helper_unittest.cpp (revision 328f520f44a4e986d58b204468660e7ddeedc189)
1 #include "helper.hpp"
2 #include "status.hpp"
3 
4 #include <blobs-ipmid/blobs.hpp>
5 #include <ipmiblob/test/blob_interface_mock.hpp>
6 
7 #include <cstdint>
8 
9 #include <gtest/gtest.h>
10 
11 namespace host_tool
12 {
13 using ::testing::Return;
14 using ::testing::TypedEq;
15 
16 class HelperTest : public ::testing::Test
17 {
18   protected:
19     ipmiblob::BlobInterfaceMock blobMock;
20     std::uint16_t session = 0xbeef;
21 };
22 
23 TEST_F(HelperTest, PollStatusReturnsAfterSuccess)
24 {
25     ipmiblob::StatResponse verificationResponse = {};
26     /* the other details of the response are ignored, and should be. */
27     verificationResponse.metadata.push_back(
28         static_cast<std::uint8_t>(ipmi_flash::ActionStatus::success));
29 
30     EXPECT_CALL(blobMock, getStat(TypedEq<std::uint16_t>(session)))
31         .WillOnce(Return(verificationResponse));
32 
33     EXPECT_TRUE(pollStatus(session, &blobMock));
34 }
35 
36 TEST_F(HelperTest, PollStatusReturnsAfterFailure)
37 {
38     ipmiblob::StatResponse verificationResponse = {};
39     /* the other details of the response are ignored, and should be. */
40     verificationResponse.metadata.push_back(
41         static_cast<std::uint8_t>(ipmi_flash::ActionStatus::failed));
42 
43     EXPECT_CALL(blobMock, getStat(TypedEq<std::uint16_t>(session)))
44         .WillOnce(Return(verificationResponse));
45 
46     EXPECT_FALSE(pollStatus(session, &blobMock));
47 }
48 
49 TEST_F(HelperTest, PollReadReadyReturnsAfterSuccess)
50 {
51     ipmiblob::StatResponse blobResponse = {};
52     /* the other details of the response are ignored, and should be. */
53     blobResponse.blob_state =
54         blobs::StateFlags::open_read | blobs::StateFlags::committed;
55 
56     EXPECT_CALL(blobMock, getStat(TypedEq<std::uint16_t>(session)))
57         .WillOnce(Return(blobResponse));
58 
59     EXPECT_TRUE(pollReadReady(session, &blobMock).first);
60 }
61 
62 TEST_F(HelperTest, PollReadReadyReturnsAfterFailure)
63 {
64     ipmiblob::StatResponse blobResponse = {};
65     /* the other details of the response are ignored, and should be. */
66     blobResponse.blob_state = blobs::StateFlags::commit_error;
67 
68     EXPECT_CALL(blobMock, getStat(TypedEq<std::uint16_t>(session)))
69         .WillOnce(Return(blobResponse));
70 
71     EXPECT_FALSE(pollReadReady(session, &blobMock).first);
72 }
73 
74 TEST_F(HelperTest, PollReadReadyReturnsAfterRetrySuccess)
75 {
76     ipmiblob::StatResponse blobResponseRunning = {};
77     /* the other details of the response are ignored, and should be. */
78     blobResponseRunning.blob_state = blobs::StateFlags::committing;
79 
80     ipmiblob::StatResponse blobResponseReadReady = {};
81     /* the other details of the response are ignored, and should be. */
82     blobResponseReadReady.blob_state = blobs::StateFlags::open_read;
83 
84     EXPECT_CALL(blobMock, getStat(TypedEq<std::uint16_t>(session)))
85         .WillOnce(Return(blobResponseRunning))
86         .WillOnce(Return(blobResponseReadReady));
87 
88     EXPECT_TRUE(pollReadReady(session, &blobMock).first);
89 }
90 
91 TEST_F(HelperTest, PollReadReadyReturnsAfterRetryFailure)
92 {
93     ipmiblob::StatResponse blobResponseRunning = {};
94     /* the other details of the response are ignored, and should be. */
95     blobResponseRunning.blob_state = blobs::StateFlags::committing;
96 
97     ipmiblob::StatResponse blobResponseError = {};
98     /* the other details of the response are ignored, and should be. */
99     blobResponseError.blob_state = blobs::StateFlags::commit_error;
100 
101     EXPECT_CALL(blobMock, getStat(TypedEq<std::uint16_t>(session)))
102         .WillOnce(Return(blobResponseRunning))
103         .WillOnce(Return(blobResponseError));
104 
105     EXPECT_FALSE(pollReadReady(session, &blobMock).first);
106 }
107 
108 TEST_F(HelperTest, MemcpyAlignedOneByte)
109 {
110     const char source = 'a';
111     char destination;
112 
113     EXPECT_EQ(&destination,
114               memcpyAligned(&destination, &source, sizeof(source)));
115     EXPECT_EQ(destination, source);
116 }
117 
118 TEST_F(HelperTest, MemcpyAlignedMultiByte)
119 {
120     const char source[14] = "abcdefghijklm";
121     char destination[14] = "xxxxxxxxxxxxx";
122 
123     EXPECT_EQ(&destination,
124               memcpyAligned(&destination, &source, sizeof(source)));
125     EXPECT_EQ(0, memcmp(&destination, &source, sizeof(source)));
126 }
127 
128 } // namespace host_tool
129