blob: cfe52088f54f3b30ed59af6748b1a61888f9d109 [file] [log] [blame]
Patrick Venture01123b22019-06-20 13:49:06 -07001#include "helper.hpp"
2#include "status.hpp"
William A. Kennington IIIf88bcf32021-10-14 02:15:10 -07003#include "tool_errors.hpp"
Patrick Venture01123b22019-06-20 13:49:06 -07004
Jie Yang328f5202021-03-16 00:52:07 -07005#include <blobs-ipmid/blobs.hpp>
Patrick Venture01123b22019-06-20 13:49:06 -07006#include <ipmiblob/test/blob_interface_mock.hpp>
7
Patrick Venture9b37b092020-05-28 20:58:57 -07008#include <cstdint>
9
Patrick Venture01123b22019-06-20 13:49:06 -070010#include <gtest/gtest.h>
11
12namespace host_tool
13{
14using ::testing::Return;
15using ::testing::TypedEq;
16
Vivekanand Veeracholanc7fa2c22021-02-18 18:05:41 -080017class HelperTest : public ::testing::Test
Patrick Venture01123b22019-06-20 13:49:06 -070018{
19 protected:
20 ipmiblob::BlobInterfaceMock blobMock;
21 std::uint16_t session = 0xbeef;
22};
23
Vivekanand Veeracholanc7fa2c22021-02-18 18:05:41 -080024TEST_F(HelperTest, PollStatusReturnsAfterSuccess)
Patrick Venture01123b22019-06-20 13:49:06 -070025{
26 ipmiblob::StatResponse verificationResponse = {};
27 /* the other details of the response are ignored, and should be. */
28 verificationResponse.metadata.push_back(
29 static_cast<std::uint8_t>(ipmi_flash::ActionStatus::success));
30
31 EXPECT_CALL(blobMock, getStat(TypedEq<std::uint16_t>(session)))
32 .WillOnce(Return(verificationResponse));
33
William A. Kennington IIIf88bcf32021-10-14 02:15:10 -070034 EXPECT_NO_THROW(pollStatus(session, &blobMock));
Patrick Venture01123b22019-06-20 13:49:06 -070035}
36
Vivekanand Veeracholanc7fa2c22021-02-18 18:05:41 -080037TEST_F(HelperTest, PollStatusReturnsAfterFailure)
Patrick Venture01123b22019-06-20 13:49:06 -070038{
39 ipmiblob::StatResponse verificationResponse = {};
40 /* the other details of the response are ignored, and should be. */
41 verificationResponse.metadata.push_back(
42 static_cast<std::uint8_t>(ipmi_flash::ActionStatus::failed));
43
44 EXPECT_CALL(blobMock, getStat(TypedEq<std::uint16_t>(session)))
45 .WillOnce(Return(verificationResponse));
46
William A. Kennington IIIf88bcf32021-10-14 02:15:10 -070047 EXPECT_THROW(pollStatus(session, &blobMock), ToolException);
Patrick Venture01123b22019-06-20 13:49:06 -070048}
49
Jie Yang328f5202021-03-16 00:52:07 -070050TEST_F(HelperTest, PollReadReadyReturnsAfterSuccess)
51{
52 ipmiblob::StatResponse blobResponse = {};
53 /* the other details of the response are ignored, and should be. */
Patrick Williams42a44c22024-08-16 15:21:32 -040054 blobResponse.blob_state =
55 blobs::StateFlags::open_read | blobs::StateFlags::committed;
Jie Yang328f5202021-03-16 00:52:07 -070056
57 EXPECT_CALL(blobMock, getStat(TypedEq<std::uint16_t>(session)))
58 .WillOnce(Return(blobResponse));
59
William A. Kennington IIIf88bcf32021-10-14 02:15:10 -070060 EXPECT_NO_THROW(pollReadReady(session, &blobMock));
Jie Yang328f5202021-03-16 00:52:07 -070061}
62
63TEST_F(HelperTest, PollReadReadyReturnsAfterFailure)
64{
65 ipmiblob::StatResponse blobResponse = {};
66 /* the other details of the response are ignored, and should be. */
67 blobResponse.blob_state = blobs::StateFlags::commit_error;
68
69 EXPECT_CALL(blobMock, getStat(TypedEq<std::uint16_t>(session)))
70 .WillOnce(Return(blobResponse));
71
William A. Kennington IIIf88bcf32021-10-14 02:15:10 -070072 EXPECT_THROW(pollReadReady(session, &blobMock), ToolException);
Jie Yang328f5202021-03-16 00:52:07 -070073}
74
75TEST_F(HelperTest, PollReadReadyReturnsAfterRetrySuccess)
76{
77 ipmiblob::StatResponse blobResponseRunning = {};
78 /* the other details of the response are ignored, and should be. */
79 blobResponseRunning.blob_state = blobs::StateFlags::committing;
80
81 ipmiblob::StatResponse blobResponseReadReady = {};
82 /* the other details of the response are ignored, and should be. */
83 blobResponseReadReady.blob_state = blobs::StateFlags::open_read;
84
85 EXPECT_CALL(blobMock, getStat(TypedEq<std::uint16_t>(session)))
86 .WillOnce(Return(blobResponseRunning))
87 .WillOnce(Return(blobResponseReadReady));
88
William A. Kennington IIIf88bcf32021-10-14 02:15:10 -070089 EXPECT_NO_THROW(pollReadReady(session, &blobMock));
Jie Yang328f5202021-03-16 00:52:07 -070090}
91
92TEST_F(HelperTest, PollReadReadyReturnsAfterRetryFailure)
93{
94 ipmiblob::StatResponse blobResponseRunning = {};
95 /* the other details of the response are ignored, and should be. */
96 blobResponseRunning.blob_state = blobs::StateFlags::committing;
97
98 ipmiblob::StatResponse blobResponseError = {};
99 /* the other details of the response are ignored, and should be. */
100 blobResponseError.blob_state = blobs::StateFlags::commit_error;
101
102 EXPECT_CALL(blobMock, getStat(TypedEq<std::uint16_t>(session)))
103 .WillOnce(Return(blobResponseRunning))
104 .WillOnce(Return(blobResponseError));
105
William A. Kennington IIIf88bcf32021-10-14 02:15:10 -0700106 EXPECT_THROW(pollReadReady(session, &blobMock), ToolException);
Jie Yang328f5202021-03-16 00:52:07 -0700107}
108
Vivekanand Veeracholanc7fa2c22021-02-18 18:05:41 -0800109TEST_F(HelperTest, MemcpyAlignedOneByte)
110{
111 const char source = 'a';
112 char destination;
113
114 EXPECT_EQ(&destination,
115 memcpyAligned(&destination, &source, sizeof(source)));
116 EXPECT_EQ(destination, source);
117}
118
119TEST_F(HelperTest, MemcpyAlignedMultiByte)
120{
121 const char source[14] = "abcdefghijklm";
122 char destination[14] = "xxxxxxxxxxxxx";
123
124 EXPECT_EQ(&destination,
125 memcpyAligned(&destination, &source, sizeof(source)));
126 EXPECT_EQ(0, memcmp(&destination, &source, sizeof(source)));
127}
128
Patrick Venture01123b22019-06-20 13:49:06 -0700129} // namespace host_tool