blob: e1c46180083d10c6102ce0c3b551c6bb62634cb6 [file] [log] [blame]
Jason Lingc78bfc82020-11-05 18:58:16 -08001#include "version_handler.hpp"
William A. Kennington IIIabf17352020-12-22 21:07:11 -08002#include "version_mock.hpp"
Jason Lingc78bfc82020-11-05 18:58:16 -08003
William A. Kennington IIIabf17352020-12-22 21:07:11 -08004#include <memory>
Jason Lingc78bfc82020-11-05 18:58:16 -08005#include <string>
6#include <vector>
7
8#include <gtest/gtest.h>
9using ::testing::_;
10using ::testing::IsEmpty;
11using ::testing::Return;
12namespace ipmi_flash
13{
14
15class VersionReadBlobTest : public ::testing::Test
16{
17 protected:
18 void SetUp() override
19 {
William A. Kennington IIIabf17352020-12-22 21:07:11 -080020 h = std::make_unique<VersionBlobHandler>(
21 createMockVersionConfigs(blobNames, &im, &tm));
Jason Lingc78bfc82020-11-05 18:58:16 -080022 }
23 std::unique_ptr<blobs::GenericBlobInterface> h;
24 std::vector<std::string> blobNames{"blob0", "blob1", "blob2", "blob3"};
25 std::unordered_map<std::string, TriggerMock*> tm;
26 std::unordered_map<std::string, ImageHandlerMock*> im;
27 const std::uint16_t defaultSessionNumber{200};
28 std::vector<uint8_t> vector1{0xDE, 0xAD, 0xBE, 0xEF,
29 0xBA, 0xDF, 0xEE, 0x0D};
30};
31
32TEST_F(VersionReadBlobTest, VerifyValidRead)
33{
William A. Kennington IIIabf17352020-12-22 21:07:11 -080034 EXPECT_CALL(*tm.at("blob0"), trigger()).WillOnce(Return(true));
Jason Lingc78bfc82020-11-05 18:58:16 -080035 EXPECT_CALL(*tm.at("blob0"), status())
36 .Times(2)
37 .WillRepeatedly(Return(ActionStatus::success));
38 EXPECT_TRUE(h->open(defaultSessionNumber, blobs::read, "blob0"));
39 /* file path gets bound to file_handler on creation so path parameter
40 * doesn't actually matter
41 */
42 EXPECT_CALL(*im.at("blob0"), open(_, std::ios::in))
43 .Times(2)
44 .WillRepeatedly(Return(true));
45 EXPECT_CALL(*im.at("blob0"), read(0, 10)).WillOnce(Return(vector1));
46 EXPECT_CALL(*im.at("blob0"), read(2, 10)).WillOnce(Return(vector1));
47 EXPECT_CALL(*im.at("blob0"), close()).Times(2);
48
49 EXPECT_EQ(h->read(defaultSessionNumber, 0, 10), vector1);
50 EXPECT_EQ(h->read(defaultSessionNumber, 2, 10), vector1);
51}
52
53TEST_F(VersionReadBlobTest, VerifyUnopenedReadFails)
54{
Jason Lingc78bfc82020-11-05 18:58:16 -080055 EXPECT_THAT(h->read(defaultSessionNumber, 0, 10), IsEmpty());
56}
57
58TEST_F(VersionReadBlobTest, VerifyTriggerFailureReadFails)
59{
William A. Kennington IIIabf17352020-12-22 21:07:11 -080060 EXPECT_CALL(*tm.at("blob0"), trigger()).WillOnce(Return(true));
Jason Lingc78bfc82020-11-05 18:58:16 -080061 EXPECT_CALL(*tm.at("blob0"), status())
Jason Lingc78bfc82020-11-05 18:58:16 -080062 .WillOnce(Return(ActionStatus::failed));
Jason Lingc78bfc82020-11-05 18:58:16 -080063 EXPECT_TRUE(h->open(defaultSessionNumber, blobs::read, "blob0"));
64 EXPECT_THAT(h->read(defaultSessionNumber, 0, 10), IsEmpty());
65}
66
67TEST_F(VersionReadBlobTest, VerifyReadFailsOnFileReadFailure)
68{
William A. Kennington IIIabf17352020-12-22 21:07:11 -080069 EXPECT_CALL(*tm.at("blob0"), trigger()).WillOnce(Return(true));
Jason Lingc78bfc82020-11-05 18:58:16 -080070 EXPECT_CALL(*tm.at("blob0"), status())
Jason Lingc78bfc82020-11-05 18:58:16 -080071 .WillOnce(Return(ActionStatus::success));
72 /* file path gets bound to file_handler on creation so path parameter
73 * doesn't actually matter
74 */
William A. Kennington IIIabf17352020-12-22 21:07:11 -080075 EXPECT_CALL(*im.at("blob0"), open(_, std::ios::in)).WillOnce(Return(true));
76 EXPECT_CALL(*im.at("blob0"), read(_, _)).WillOnce(Return(std::nullopt));
Jason Lingc78bfc82020-11-05 18:58:16 -080077 EXPECT_CALL(*im.at("blob0"), close()).Times(1);
78
79 EXPECT_TRUE(h->open(defaultSessionNumber, blobs::read, "blob0"));
80 EXPECT_THAT(h->read(defaultSessionNumber, 0, 10), IsEmpty());
81}
82
83TEST_F(VersionReadBlobTest, VerifyReadFailsOnFileOpenFailure)
84{
William A. Kennington IIIabf17352020-12-22 21:07:11 -080085 EXPECT_CALL(*tm.at("blob0"), trigger()).WillOnce(Return(true));
Jason Lingc78bfc82020-11-05 18:58:16 -080086 /* first call to trigger status fails, second succeeds */
87 EXPECT_CALL(*tm.at("blob0"), status())
Jason Lingc78bfc82020-11-05 18:58:16 -080088 .WillOnce(Return(ActionStatus::success));
89 /* file path gets bound to file_handler on creation so path parameter
90 * doesn't actually matter
91 */
William A. Kennington IIIabf17352020-12-22 21:07:11 -080092 EXPECT_CALL(*im.at("blob0"), open(_, std::ios::in)).WillOnce(Return(false));
Jason Lingc78bfc82020-11-05 18:58:16 -080093
94 EXPECT_TRUE(h->open(defaultSessionNumber, blobs::read, "blob0"));
95 EXPECT_THAT(h->read(defaultSessionNumber, 0, 10), IsEmpty());
96}
97
98} // namespace ipmi_flash