xref: /openbmc/phosphor-ipmi-blobs/test/manager_expire_unittest.cpp (revision aa17bdaf3e34ce3a876dfb828ace2ec2c635e5e5)
1*aa17bdafSKun Yi #include "blob_mock.hpp"
2*aa17bdafSKun Yi #include "manager.hpp"
3*aa17bdafSKun Yi 
4*aa17bdafSKun Yi #include <string>
5*aa17bdafSKun Yi 
6*aa17bdafSKun Yi #include <gtest/gtest.h>
7*aa17bdafSKun Yi 
8*aa17bdafSKun Yi namespace blobs
9*aa17bdafSKun Yi {
10*aa17bdafSKun Yi 
11*aa17bdafSKun Yi using namespace std::chrono_literals;
12*aa17bdafSKun Yi 
13*aa17bdafSKun Yi using ::testing::_;
14*aa17bdafSKun Yi using ::testing::Return;
15*aa17bdafSKun Yi 
TEST(ManagerExpireTest,OpenWithLongTimeoutSucceeds)16*aa17bdafSKun Yi TEST(ManagerExpireTest, OpenWithLongTimeoutSucceeds)
17*aa17bdafSKun Yi {
18*aa17bdafSKun Yi     // With a long timeout, open should succeed without calling expire.
19*aa17bdafSKun Yi     BlobManager mgr(2min);
20*aa17bdafSKun Yi     std::unique_ptr<BlobMock> m1 = std::make_unique<BlobMock>();
21*aa17bdafSKun Yi     auto m1ptr = m1.get();
22*aa17bdafSKun Yi     EXPECT_TRUE(mgr.registerHandler(std::move(m1)));
23*aa17bdafSKun Yi 
24*aa17bdafSKun Yi     uint16_t flags = OpenFlags::read, sess;
25*aa17bdafSKun Yi     std::string path = "/asdf/asdf";
26*aa17bdafSKun Yi 
27*aa17bdafSKun Yi     EXPECT_CALL(*m1ptr, canHandleBlob(path)).WillOnce(Return(true));
28*aa17bdafSKun Yi     EXPECT_CALL(*m1ptr, open(_, flags, path)).WillOnce(Return(true));
29*aa17bdafSKun Yi     EXPECT_TRUE(mgr.open(flags, path, &sess));
30*aa17bdafSKun Yi     // Do not expect the open session to expire
31*aa17bdafSKun Yi     EXPECT_CALL(*m1ptr, expire(sess)).Times(0);
32*aa17bdafSKun Yi }
33*aa17bdafSKun Yi 
TEST(ManagerExpireTest,ZeroTimeoutWillCauseExpiration)34*aa17bdafSKun Yi TEST(ManagerExpireTest, ZeroTimeoutWillCauseExpiration)
35*aa17bdafSKun Yi {
36*aa17bdafSKun Yi     // With timeout being zero, every open will cause all previous opened
37*aa17bdafSKun Yi     // sessions to expire.
38*aa17bdafSKun Yi     BlobManager mgr(0min);
39*aa17bdafSKun Yi     std::unique_ptr<BlobMock> m1 = std::make_unique<BlobMock>();
40*aa17bdafSKun Yi     auto m1ptr = m1.get();
41*aa17bdafSKun Yi     EXPECT_TRUE(mgr.registerHandler(std::move(m1)));
42*aa17bdafSKun Yi 
43*aa17bdafSKun Yi     uint16_t flags = OpenFlags::read, sess;
44*aa17bdafSKun Yi     std::string path = "/asdf/asdf";
45*aa17bdafSKun Yi     const int testIterations = 10;
46*aa17bdafSKun Yi 
47*aa17bdafSKun Yi     EXPECT_CALL(*m1ptr, canHandleBlob(path)).WillRepeatedly(Return(true));
48*aa17bdafSKun Yi     EXPECT_CALL(*m1ptr, open(_, flags, path)).WillRepeatedly(Return(true));
49*aa17bdafSKun Yi     for (int i = 0; i < testIterations; ++i)
50*aa17bdafSKun Yi     {
51*aa17bdafSKun Yi         if (i != 0)
52*aa17bdafSKun Yi         {
53*aa17bdafSKun Yi             // Here 'sess' is the session ID obtained in previous loop
54*aa17bdafSKun Yi             EXPECT_CALL(*m1ptr, expire(sess)).WillOnce(Return(true));
55*aa17bdafSKun Yi         }
56*aa17bdafSKun Yi         EXPECT_TRUE(mgr.open(flags, path, &sess));
57*aa17bdafSKun Yi     }
58*aa17bdafSKun Yi }
59*aa17bdafSKun Yi } // namespace blobs
60