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, VerifyDuplicateSessionNumberFails)
69 {
70     EXPECT_CALL(*tm.at("blob0"), trigger()).Times(1).WillOnce(Return(true));
71     EXPECT_CALL(*tm.at("blob1"), trigger()).Times(1).WillOnce(Return(true));
72     EXPECT_TRUE(h->open(defaultSessionNumber, blobs::read, "blob1"));
73     /* the duplicate session number of 0
74      *  should cause a failure for the open of a different blob
75      */
76     EXPECT_FALSE(h->open(defaultSessionNumber, blobs::read, "blob0"));
77     /* open after fail due to seq number works */
78     EXPECT_TRUE(h->open(defaultSessionNumber + 1, blobs::read, "blob0"));
79 }
80 
81 TEST_F(VersionOpenBlobTest, VerifyDoubleOpenFails)
82 {
83     EXPECT_CALL(*tm.at("blob1"), trigger())
84         .Times(1)
85         .WillRepeatedly(Return(true));
86     EXPECT_TRUE(h->open(0, blobs::read, "blob1"));
87     EXPECT_FALSE(h->open(2, blobs::read, "blob1"));
88 }
89 
90 TEST_F(VersionOpenBlobTest, VerifyFailedTriggerFails)
91 {
92     EXPECT_CALL(*tm.at("blob1"), trigger())
93         .Times(2)
94         .WillOnce(Return(false))
95         .WillOnce(Return(true));
96     EXPECT_FALSE(h->open(0, blobs::read, "blob1"));
97     EXPECT_TRUE(h->open(0, blobs::read, "blob1"));
98 }
99 
100 TEST_F(VersionOpenBlobTest, VerifyUnsupportedOpenFlagsFails)
101 {
102     EXPECT_CALL(*tm.at("blob1"), trigger()).Times(1).WillOnce(Return(true));
103     EXPECT_FALSE(h->open(0, blobs::write, "blob1"));
104     EXPECT_TRUE(h->open(0, blobs::read, "blob1"));
105 }
106 
107 } // namespace ipmi_flash
108