1 // Copyright 2021 Google Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "log_handler.hpp"
16 #include "log_mock.hpp"
17 
18 #include <memory>
19 #include <string>
20 #include <unordered_map>
21 #include <vector>
22 
23 #include <gtest/gtest.h>
24 
25 using ::testing::Return;
26 
27 namespace ipmi_flash
28 {
29 
30 class LogCloseExpireBlobTest : public ::testing::Test
31 {
32   protected:
33     void SetUp() override
34     {
35         h = std::make_unique<LogBlobHandler>(
36             createMockLogConfigs(blobNames, &im, &tm));
37     }
38 
39     std::unique_ptr<blobs::GenericBlobInterface> h;
40     std::vector<std::string> blobNames{"blob0", "blob1", "blob2", "blob3"};
41     std::unordered_map<std::string, TriggerMock*> tm;
42     std::unordered_map<std::string, ImageHandlerMock*> im;
43 };
44 
45 TEST_F(LogCloseExpireBlobTest, VerifyOpenThenClose)
46 {
47     EXPECT_CALL(*tm.at("blob0"), trigger()).WillOnce(Return(true));
48     EXPECT_TRUE(h->open(0, blobs::read, "blob0"));
49     EXPECT_CALL(*tm.at("blob0"), abort()).Times(1);
50     EXPECT_TRUE(h->close(0));
51 }
52 
53 TEST_F(LogCloseExpireBlobTest, VerifySingleAbort)
54 {
55     EXPECT_CALL(*tm.at("blob0"), trigger()).WillOnce(Return(true));
56     EXPECT_TRUE(h->open(0, blobs::read, "blob0"));
57     EXPECT_TRUE(h->open(1, blobs::read, "blob0"));
58     EXPECT_TRUE(h->close(0));
59     EXPECT_CALL(*tm.at("blob0"), abort()).Times(1);
60     EXPECT_TRUE(h->close(1));
61 }
62 
63 TEST_F(LogCloseExpireBlobTest, VerifyUnopenedBlobCloseFails)
64 {
65     EXPECT_FALSE(h->close(0));
66 }
67 
68 TEST_F(LogCloseExpireBlobTest, VerifyDoubleCloseFails)
69 {
70     EXPECT_CALL(*tm.at("blob0"), trigger()).WillOnce(Return(true));
71     EXPECT_TRUE(h->open(0, blobs::read, "blob0"));
72     EXPECT_CALL(*tm.at("blob0"), abort()).Times(1);
73     EXPECT_TRUE(h->close(0));
74     EXPECT_FALSE(h->close(0));
75 }
76 
77 TEST_F(LogCloseExpireBlobTest, VerifyBadSessionNumberCloseFails)
78 {
79     EXPECT_CALL(*tm.at("blob0"), trigger()).WillOnce(Return(true));
80     EXPECT_TRUE(h->open(0, blobs::read, "blob0"));
81     EXPECT_FALSE(h->close(1));
82     EXPECT_CALL(*tm.at("blob0"), abort()).Times(1);
83     EXPECT_TRUE(h->close(0));
84 }
85 
86 TEST_F(LogCloseExpireBlobTest, VerifyRunningActionIsAborted)
87 {
88     EXPECT_CALL(*tm.at("blob0"), trigger()).WillOnce(Return(true));
89     EXPECT_TRUE(h->open(0, blobs::read, "blob0"));
90     EXPECT_CALL(*tm.at("blob0"), abort()).Times(1);
91     EXPECT_TRUE(h->close(0));
92 }
93 
94 } // namespace ipmi_flash
95