1 #include "version_handler.hpp"
2 #include "version_mock.hpp"
3 
4 #include <memory>
5 #include <string>
6 #include <unordered_map>
7 #include <vector>
8 
9 #include <gtest/gtest.h>
10 
11 using ::testing::_;
12 using ::testing::Return;
13 
14 namespace ipmi_flash
15 {
16 
17 class VersionOpenBlobTest : public ::testing::Test
18 {
19   protected:
20     void SetUp() override
21     {
22         h = std::make_unique<VersionBlobHandler>(
23             createMockVersionConfigs(blobNames, &im, &tm));
24         for (const auto& blob : blobNames)
25         {
26             EXPECT_CALL(*tm.at(blob), status())
27                 .WillRepeatedly(Return(ActionStatus::unknown));
28         }
29     }
30 
31     std::unique_ptr<blobs::GenericBlobInterface> h;
32     std::vector<std::string> blobNames{"blob0", "blob1", "blob2", "blob3"};
33     std::unordered_map<std::string, TriggerMock*> tm;
34     std::unordered_map<std::string, ImageHandlerMock*> im;
35     const std::uint16_t defaultSessionNumber{0};
36 };
37 
38 TEST_F(VersionOpenBlobTest, VerifySingleBlobOpen)
39 {
40     EXPECT_CALL(*tm.at("blob0"), trigger()).Times(1).WillOnce(Return(true));
41     EXPECT_TRUE(h->open(defaultSessionNumber, blobs::read, "blob0"));
42 }
43 
44 TEST_F(VersionOpenBlobTest, VerifyMultipleBlobOpens)
45 {
46     for (const auto& [key, val] : tm)
47     {
48         /* set the expectation that every onOpen will be triggered */
49         EXPECT_CALL(*val, trigger()).Times(1).WillOnce(Return(true));
50     }
51     int i{defaultSessionNumber};
52     for (const auto& blob : blobNames)
53     {
54         EXPECT_TRUE(h->open(i++, blobs::read, blob));
55     }
56 }
57 
58 TEST_F(VersionOpenBlobTest, VerifyOpenAfterClose)
59 {
60     EXPECT_CALL(*tm.at("blob0"), trigger())
61         .Times(2)
62         .WillRepeatedly(Return(true));
63     EXPECT_TRUE(h->open(defaultSessionNumber, blobs::read, "blob0"));
64     EXPECT_TRUE(h->close(defaultSessionNumber));
65     EXPECT_TRUE(h->open(defaultSessionNumber, blobs::read, "blob0"));
66 }
67 
68 TEST_F(VersionOpenBlobTest, VerifyDoubleOpenFails)
69 {
70     EXPECT_CALL(*tm.at("blob1"), trigger())
71         .Times(1)
72         .WillRepeatedly(Return(true));
73     EXPECT_TRUE(h->open(0, blobs::read, "blob1"));
74     EXPECT_FALSE(h->open(2, blobs::read, "blob1"));
75 }
76 
77 TEST_F(VersionOpenBlobTest, VerifyFailedTriggerFails)
78 {
79     EXPECT_CALL(*tm.at("blob1"), trigger())
80         .Times(2)
81         .WillOnce(Return(false))
82         .WillOnce(Return(true));
83     EXPECT_FALSE(h->open(0, blobs::read, "blob1"));
84     EXPECT_TRUE(h->open(0, blobs::read, "blob1"));
85 }
86 
87 TEST_F(VersionOpenBlobTest, VerifyUnsupportedOpenFlagsFails)
88 {
89     EXPECT_CALL(*tm.at("blob1"), trigger()).Times(1).WillOnce(Return(true));
90     EXPECT_FALSE(h->open(0, blobs::write, "blob1"));
91     EXPECT_TRUE(h->open(0, blobs::read, "blob1"));
92 }
93 
94 } // namespace ipmi_flash
95