xref: /openbmc/phosphor-ipmi-flash/bmc/version-handler/test/version_read_unittest.cpp (revision 4175b4ca8372fd4452245db4f9328afa773e4d93)
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, VerifyTriggerFailureReadFails)
54 {
55     EXPECT_CALL(*tm.at("blob0"), trigger()).WillOnce(Return(true));
56     EXPECT_CALL(*tm.at("blob0"), status())
57         .WillOnce(Return(ActionStatus::failed));
58     EXPECT_TRUE(h->open(defaultSessionNumber, blobs::read, "blob0"));
59     EXPECT_THAT(h->read(defaultSessionNumber, 0, 10), IsEmpty());
60 }
61 
62 TEST_F(VersionReadBlobTest, VerifyReadFailsOnFileReadFailure)
63 {
64     EXPECT_CALL(*tm.at("blob0"), trigger()).WillOnce(Return(true));
65     EXPECT_CALL(*tm.at("blob0"), status())
66         .WillOnce(Return(ActionStatus::success));
67     /* file path gets bound to file_handler on creation so path parameter
68      * doesn't actually matter
69      */
70     EXPECT_CALL(*im.at("blob0"), open(_, std::ios::in)).WillOnce(Return(true));
71     EXPECT_CALL(*im.at("blob0"), read(_, _)).WillOnce(Return(std::nullopt));
72     EXPECT_CALL(*im.at("blob0"), close()).Times(1);
73 
74     EXPECT_TRUE(h->open(defaultSessionNumber, blobs::read, "blob0"));
75     EXPECT_THAT(h->read(defaultSessionNumber, 0, 10), IsEmpty());
76 }
77 
78 TEST_F(VersionReadBlobTest, VerifyReadFailsOnFileOpenFailure)
79 {
80     EXPECT_CALL(*tm.at("blob0"), trigger()).WillOnce(Return(true));
81     /* first call to trigger status fails, second succeeds */
82     EXPECT_CALL(*tm.at("blob0"), status())
83         .WillOnce(Return(ActionStatus::success));
84     /* file path gets bound to file_handler on creation so path parameter
85      * doesn't actually matter
86      */
87     EXPECT_CALL(*im.at("blob0"), open(_, std::ios::in)).WillOnce(Return(false));
88 
89     EXPECT_TRUE(h->open(defaultSessionNumber, blobs::read, "blob0"));
90     EXPECT_THAT(h->read(defaultSessionNumber, 0, 10), IsEmpty());
91 }
92 
93 } // namespace ipmi_flash
94