1 #include "version_handler.hpp"
2 #include "version_mock.hpp"
3 
4 #include <memory>
5 #include <string>
6 #include <vector>
7 
8 #include <gtest/gtest.h>
9 using ::testing::_;
10 using ::testing::IsEmpty;
11 using ::testing::Return;
12 namespace ipmi_flash
13 {
14 
15 class VersionReadBlobTest : public ::testing::Test
16 {
17   protected:
18     void SetUp() override
19     {
20         h = std::make_unique<VersionBlobHandler>(
21             createMockVersionConfigs(blobNames, &im, &tm));
22     }
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 
32 TEST_F(VersionReadBlobTest, VerifyValidRead)
33 {
34     EXPECT_CALL(*tm.at("blob0"), trigger()).WillOnce(Return(true));
35     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 
53 TEST_F(VersionReadBlobTest, VerifyUnopenedReadFails)
54 {
55     EXPECT_THAT(h->read(defaultSessionNumber, 0, 10), IsEmpty());
56 }
57 
58 TEST_F(VersionReadBlobTest, VerifyTriggerFailureReadFails)
59 {
60     EXPECT_CALL(*tm.at("blob0"), trigger()).WillOnce(Return(true));
61     EXPECT_CALL(*tm.at("blob0"), status())
62         .WillOnce(Return(ActionStatus::failed));
63     EXPECT_TRUE(h->open(defaultSessionNumber, blobs::read, "blob0"));
64     EXPECT_THAT(h->read(defaultSessionNumber, 0, 10), IsEmpty());
65 }
66 
67 TEST_F(VersionReadBlobTest, VerifyReadFailsOnFileReadFailure)
68 {
69     EXPECT_CALL(*tm.at("blob0"), trigger()).WillOnce(Return(true));
70     EXPECT_CALL(*tm.at("blob0"), status())
71         .WillOnce(Return(ActionStatus::success));
72     /* file path gets bound to file_handler on creation so path parameter
73      * doesn't actually matter
74      */
75     EXPECT_CALL(*im.at("blob0"), open(_, std::ios::in)).WillOnce(Return(true));
76     EXPECT_CALL(*im.at("blob0"), read(_, _)).WillOnce(Return(std::nullopt));
77     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 
83 TEST_F(VersionReadBlobTest, VerifyReadFailsOnFileOpenFailure)
84 {
85     EXPECT_CALL(*tm.at("blob0"), trigger()).WillOnce(Return(true));
86     /* first call to trigger status fails, second succeeds */
87     EXPECT_CALL(*tm.at("blob0"), status())
88         .WillOnce(Return(ActionStatus::success));
89     /* file path gets bound to file_handler on creation so path parameter
90      * doesn't actually matter
91      */
92     EXPECT_CALL(*im.at("blob0"), open(_, std::ios::in)).WillOnce(Return(false));
93 
94     EXPECT_TRUE(h->open(defaultSessionNumber, blobs::read, "blob0"));
95     EXPECT_THAT(h->read(defaultSessionNumber, 0, 10), IsEmpty());
96 }
97 
98 } // namespace ipmi_flash
99