1 #include "flags.hpp"
2 #include "image_mock.hpp"
3 #include "triggerable_mock.hpp"
4 #include "util.hpp"
5 #include "version_handler.hpp"
6 
7 #include <array>
8 #include <string>
9 #include <vector>
10 
11 #include <gtest/gtest.h>
12 using ::testing::_;
13 using ::testing::Return;
14 namespace ipmi_flash
15 {
16 
17 class VersionCloseExpireBlobTest : public ::testing::Test
18 {
19   protected:
20     void SetUp() override
21     {
22         VersionInfoMap vim;
23         for (const auto& blobName : blobNames)
24         {
25             auto t = CreateTriggerMock();
26             auto i = CreateImageMock();
27             tm[blobName] = reinterpret_cast<TriggerMock*>(t.get());
28             im[blobName] = reinterpret_cast<ImageHandlerMock*>(i.get());
29             vim.try_emplace(
30                 blobName,
31                 VersionInfoPack(
32                     blobName, std::make_unique<VersionActionPack>(std::move(t)),
33                     std::move(i)));
34         }
35         h = VersionBlobHandler::create(std::move(vim));
36         ASSERT_NE(h, nullptr);
37         for (const auto& [key, val] : tm)
38         {
39             ON_CALL(*val, trigger()).WillByDefault(Return(true));
40         }
41     }
42     std::unique_ptr<blobs::GenericBlobInterface> h;
43     std::vector<std::string> blobNames{"blob0", "blob1", "blob2", "blob3"};
44     std::unordered_map<std::string, TriggerMock*> tm;
45     std::unordered_map<std::string, ImageHandlerMock*> im;
46 };
47 
48 TEST_F(VersionCloseExpireBlobTest, VerifyOpenThenClose)
49 {
50     EXPECT_CALL(*tm.at("blob0"), status())
51         .Times(1)
52         .WillOnce(Return(ActionStatus::success));
53     EXPECT_CALL(*tm.at("blob0"), abort()).Times(0);
54     EXPECT_TRUE(h->open(0, blobs::read, "blob0"));
55     EXPECT_TRUE(h->close(0));
56 }
57 
58 TEST_F(VersionCloseExpireBlobTest, VerifyUnopenedBlobCloseFails)
59 {
60     EXPECT_CALL(*tm.at("blob0"), status()).Times(0);
61     EXPECT_CALL(*tm.at("blob0"), abort()).Times(0);
62     EXPECT_FALSE(h->close(0));
63 }
64 
65 TEST_F(VersionCloseExpireBlobTest, VerifyDoubleCloseFails)
66 {
67     EXPECT_CALL(*tm.at("blob0"), status())
68         .Times(1)
69         .WillOnce(Return(ActionStatus::success));
70     EXPECT_CALL(*tm.at("blob0"), abort()).Times(0);
71     EXPECT_TRUE(h->open(0, blobs::read, "blob0"));
72     EXPECT_TRUE(h->close(0));
73     EXPECT_FALSE(h->close(0));
74 }
75 
76 TEST_F(VersionCloseExpireBlobTest, VerifyBadSessionNumberCloseFails)
77 {
78     EXPECT_CALL(*tm.at("blob0"), status())
79         .Times(1)
80         .WillOnce(Return(ActionStatus::success));
81     EXPECT_CALL(*tm.at("blob0"), abort()).Times(0);
82     EXPECT_TRUE(h->open(0, blobs::read, "blob0"));
83     EXPECT_FALSE(h->close(1));
84     EXPECT_TRUE(h->close(0));
85 }
86 
87 TEST_F(VersionCloseExpireBlobTest, VerifyRunningActionIsAborted)
88 {
89     EXPECT_CALL(*tm.at("blob0"), status())
90         .Times(1)
91         .WillOnce(Return(ActionStatus::running));
92     EXPECT_CALL(*tm.at("blob0"), abort()).Times(1);
93     EXPECT_TRUE(h->open(0, blobs::read, "blob0"));
94     EXPECT_TRUE(h->close(0));
95 }
96 
97 } // namespace ipmi_flash
98