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