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 
10 using ::testing::_;
11 using ::testing::Return;
12 
13 namespace ipmi_flash
14 {
15 
16 class VersionCloseExpireBlobTest : public ::testing::Test
17 {
18   protected:
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 
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 
39 TEST_F(VersionCloseExpireBlobTest, VerifyUnopenedBlobCloseFails)
40 {
41     EXPECT_FALSE(h->close(0));
42 }
43 
44 TEST_F(VersionCloseExpireBlobTest, VerifyDoubleCloseFails)
45 {
46     EXPECT_CALL(*tm.at("blob0"), trigger()).WillOnce(Return(true));
47     EXPECT_TRUE(h->open(0, blobs::read, "blob0"));
48     EXPECT_CALL(*tm.at("blob0"), abort()).Times(1);
49     EXPECT_TRUE(h->close(0));
50     EXPECT_FALSE(h->close(0));
51 }
52 
53 TEST_F(VersionCloseExpireBlobTest, VerifyBadSessionNumberCloseFails)
54 {
55     EXPECT_CALL(*tm.at("blob0"), trigger()).WillOnce(Return(true));
56     EXPECT_TRUE(h->open(0, blobs::read, "blob0"));
57     EXPECT_FALSE(h->close(1));
58     EXPECT_CALL(*tm.at("blob0"), abort()).Times(1);
59     EXPECT_TRUE(h->close(0));
60 }
61 
62 TEST_F(VersionCloseExpireBlobTest, VerifyRunningActionIsAborted)
63 {
64     EXPECT_CALL(*tm.at("blob0"), trigger()).WillOnce(Return(true));
65     EXPECT_TRUE(h->open(0, blobs::read, "blob0"));
66     EXPECT_CALL(*tm.at("blob0"), abort()).Times(1);
67     EXPECT_TRUE(h->close(0));
68 }
69 
70 } // namespace ipmi_flash
71