blob: 2bee66d2d0ccff90812207ae2b69ab0ecf3cbd2a [file] [log] [blame]
Patrick Venture01123b22019-06-20 13:49:06 -07001#include "helper.hpp"
2#include "status.hpp"
3
Jie Yang328f5202021-03-16 00:52:07 -07004#include <blobs-ipmid/blobs.hpp>
Patrick Venture01123b22019-06-20 13:49:06 -07005#include <ipmiblob/test/blob_interface_mock.hpp>
6
Patrick Venture9b37b092020-05-28 20:58:57 -07007#include <cstdint>
8
Patrick Venture01123b22019-06-20 13:49:06 -07009#include <gtest/gtest.h>
10
11namespace host_tool
12{
13using ::testing::Return;
14using ::testing::TypedEq;
15
Vivekanand Veeracholanc7fa2c22021-02-18 18:05:41 -080016class HelperTest : public ::testing::Test
Patrick Venture01123b22019-06-20 13:49:06 -070017{
18 protected:
19 ipmiblob::BlobInterfaceMock blobMock;
20 std::uint16_t session = 0xbeef;
21};
22
Vivekanand Veeracholanc7fa2c22021-02-18 18:05:41 -080023TEST_F(HelperTest, PollStatusReturnsAfterSuccess)
Patrick Venture01123b22019-06-20 13:49:06 -070024{
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
Vivekanand Veeracholanc7fa2c22021-02-18 18:05:41 -080036TEST_F(HelperTest, PollStatusReturnsAfterFailure)
Patrick Venture01123b22019-06-20 13:49:06 -070037{
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
Jie Yang328f5202021-03-16 00:52:07 -070049TEST_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
62TEST_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
74TEST_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
91TEST_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
Vivekanand Veeracholanc7fa2c22021-02-18 18:05:41 -0800108TEST_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
118TEST_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
Patrick Venture01123b22019-06-20 13:49:06 -0700128} // namespace host_tool