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     }
25 
26     std::unique_ptr<blobs::GenericBlobInterface> h;
27     std::vector<std::string> blobNames{"blob0", "blob1", "blob2", "blob3"};
28     std::unordered_map<std::string, TriggerMock*> tm;
29     std::unordered_map<std::string, ImageHandlerMock*> im;
30     const std::uint16_t defaultSessionNumber{0};
31 };
32 
33 TEST_F(VersionOpenBlobTest, VerifySingleBlobOpen)
34 {
35     EXPECT_CALL(*tm.at("blob0"), trigger()).Times(1).WillOnce(Return(true));
36     EXPECT_TRUE(h->open(defaultSessionNumber, blobs::read, "blob0"));
37 }
38 
39 TEST_F(VersionOpenBlobTest, VerifyMultipleBlobOpens)
40 {
41     for (const auto& [_, val] : tm)
42     {
43         /* set the expectation that every onOpen will be triggered */
44         EXPECT_CALL(*val, trigger()).WillOnce(Return(true));
45     }
46     int i{defaultSessionNumber};
47     for (const auto& blob : blobNames)
48     {
49         EXPECT_TRUE(h->open(i++, blobs::read, blob));
50     }
51 }
52 
53 TEST_F(VersionOpenBlobTest, VerifyOpenAfterClose)
54 {
55     EXPECT_CALL(*tm.at("blob0"), trigger()).WillOnce(Return(true));
56     EXPECT_TRUE(h->open(defaultSessionNumber, blobs::read, "blob0"));
57 
58     EXPECT_CALL(*tm.at("blob0"), abort()).Times(1);
59     EXPECT_TRUE(h->close(defaultSessionNumber));
60 
61     EXPECT_CALL(*tm.at("blob0"), trigger()).WillOnce(Return(true));
62     EXPECT_TRUE(h->open(defaultSessionNumber, blobs::read, "blob0"));
63 }
64 
65 TEST_F(VersionOpenBlobTest, VerifyDoubleOpenFails)
66 {
67     EXPECT_CALL(*tm.at("blob1"), trigger()).WillOnce(Return(true));
68     EXPECT_TRUE(h->open(0, blobs::read, "blob1"));
69     EXPECT_FALSE(h->open(2, blobs::read, "blob1"));
70     EXPECT_FALSE(h->open(2, blobs::read, "blob1"));
71 }
72 
73 TEST_F(VersionOpenBlobTest, VerifyFailedTriggerFails)
74 {
75     EXPECT_CALL(*tm.at("blob1"), trigger()).WillOnce(Return(false));
76     EXPECT_FALSE(h->open(0, blobs::read, "blob1"));
77     EXPECT_CALL(*tm.at("blob1"), trigger()).WillOnce(Return(true));
78     EXPECT_TRUE(h->open(0, blobs::read, "blob1"));
79 }
80 
81 TEST_F(VersionOpenBlobTest, VerifyUnsupportedOpenFlagsFails)
82 {
83     EXPECT_FALSE(h->open(0, blobs::write, "blob1"));
84     EXPECT_CALL(*tm.at("blob1"), trigger()).WillOnce(Return(true));
85     EXPECT_TRUE(h->open(0, blobs::read, "blob1"));
86 }
87 
88 } // namespace ipmi_flash
89