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::Return;
12 
13 namespace ipmi_flash
14 {
15 
16 class VersionCloseExpireBlobTest : public ::testing::Test
17 {
18   protected:
SetUp()19     void SetUp() override
20     {
21         h = std::make_unique<VersionBlobHandler>(
22             createMockVersionConfigs(blobNames, &im, &tm));
23     }
24 
25     std::unique_ptr<blobs::GenericBlobInterface> h;
26     std::vector<std::string> blobNames{"blob0", "blob1", "blob2", "blob3"};
27     std::unordered_map<std::string, TriggerMock*> tm;
28     std::unordered_map<std::string, ImageHandlerMock*> im;
29 };
30 
TEST_F(VersionCloseExpireBlobTest,VerifyOpenThenClose)31 TEST_F(VersionCloseExpireBlobTest, VerifyOpenThenClose)
32 {
33     EXPECT_CALL(*tm.at("blob0"), trigger()).WillOnce(Return(true));
34     EXPECT_TRUE(h->open(0, blobs::read, "blob0"));
35     EXPECT_CALL(*tm.at("blob0"), abort()).Times(1);
36     EXPECT_TRUE(h->close(0));
37 }
38 
TEST_F(VersionCloseExpireBlobTest,VerifySingleAbort)39 TEST_F(VersionCloseExpireBlobTest, VerifySingleAbort)
40 {
41     EXPECT_CALL(*tm.at("blob0"), trigger()).WillOnce(Return(true));
42     EXPECT_TRUE(h->open(0, blobs::read, "blob0"));
43     EXPECT_TRUE(h->open(1, blobs::read, "blob0"));
44     EXPECT_TRUE(h->close(0));
45     EXPECT_CALL(*tm.at("blob0"), abort()).Times(1);
46     EXPECT_TRUE(h->close(1));
47 }
48 
TEST_F(VersionCloseExpireBlobTest,VerifyUnopenedBlobCloseFails)49 TEST_F(VersionCloseExpireBlobTest, VerifyUnopenedBlobCloseFails)
50 {
51     EXPECT_FALSE(h->close(0));
52 }
53 
TEST_F(VersionCloseExpireBlobTest,VerifyDoubleCloseFails)54 TEST_F(VersionCloseExpireBlobTest, VerifyDoubleCloseFails)
55 {
56     EXPECT_CALL(*tm.at("blob0"), trigger()).WillOnce(Return(true));
57     EXPECT_TRUE(h->open(0, blobs::read, "blob0"));
58     EXPECT_CALL(*tm.at("blob0"), abort()).Times(1);
59     EXPECT_TRUE(h->close(0));
60     EXPECT_FALSE(h->close(0));
61 }
62 
TEST_F(VersionCloseExpireBlobTest,VerifyBadSessionNumberCloseFails)63 TEST_F(VersionCloseExpireBlobTest, VerifyBadSessionNumberCloseFails)
64 {
65     EXPECT_CALL(*tm.at("blob0"), trigger()).WillOnce(Return(true));
66     EXPECT_TRUE(h->open(0, blobs::read, "blob0"));
67     EXPECT_FALSE(h->close(1));
68     EXPECT_CALL(*tm.at("blob0"), abort()).Times(1);
69     EXPECT_TRUE(h->close(0));
70 }
71 
TEST_F(VersionCloseExpireBlobTest,VerifyRunningActionIsAborted)72 TEST_F(VersionCloseExpireBlobTest, VerifyRunningActionIsAborted)
73 {
74     EXPECT_CALL(*tm.at("blob0"), trigger()).WillOnce(Return(true));
75     EXPECT_TRUE(h->open(0, blobs::read, "blob0"));
76     EXPECT_CALL(*tm.at("blob0"), abort()).Times(1);
77     EXPECT_TRUE(h->close(0));
78 }
79 
80 } // namespace ipmi_flash
81