xref: /openbmc/phosphor-ipmi-blobs/test/manager_close_unittest.cpp (revision aceb4baa1b3b58d425c4c706069ec51fb594305f)
1 #include "blob_mock.hpp"
2 
3 #include <blobs-ipmid/manager.hpp>
4 
5 #include <gtest/gtest.h>
6 
7 namespace blobs
8 {
9 
10 using ::testing::_;
11 using ::testing::Return;
12 
13 TEST(ManagerCloseTest, CloseNoSessionReturnsFalse)
14 {
15     // Calling Close on a session that doesn't exist should return false.
16 
17     BlobManager mgr;
18     uint16_t sess = 1;
19 
20     EXPECT_FALSE(mgr.close(sess));
21 }
22 
23 TEST(ManagerCloseTest, CloseSessionFoundButHandlerReturnsFalse)
24 {
25     // The handler was found but it returned failure.
26 
27     BlobManager mgr;
28     std::unique_ptr<BlobMock> m1 = std::make_unique<BlobMock>();
29     auto m1ptr = m1.get();
30     EXPECT_TRUE(mgr.registerHandler(std::move(m1)));
31 
32     uint16_t flags = OpenFlags::read, sess;
33     std::string path = "/asdf/asdf";
34 
35     EXPECT_CALL(*m1ptr, canHandleBlob(path)).WillOnce(Return(true));
36     EXPECT_CALL(*m1ptr, open(_, flags, path)).WillOnce(Return(true));
37     EXPECT_TRUE(mgr.open(flags, path, &sess));
38 
39     EXPECT_CALL(*m1ptr, close(sess)).WillOnce(Return(false));
40 
41     EXPECT_FALSE(mgr.close(sess));
42 
43     // TODO(venture): The session wasn't closed, need to verify.  Could call
44     // public GetHandler method.
45 }
46 
47 TEST(ManagerCloseTest, CloseSessionFoundAndHandlerReturnsSuccess)
48 {
49     // The handler was found and returned success.
50 
51     BlobManager mgr;
52     std::unique_ptr<BlobMock> m1 = std::make_unique<BlobMock>();
53     auto m1ptr = m1.get();
54     EXPECT_TRUE(mgr.registerHandler(std::move(m1)));
55 
56     uint16_t flags = OpenFlags::read, sess;
57     std::string path = "/asdf/asdf";
58 
59     EXPECT_CALL(*m1ptr, canHandleBlob(path)).WillOnce(Return(true));
60     EXPECT_CALL(*m1ptr, open(_, flags, path)).WillOnce(Return(true));
61     EXPECT_TRUE(mgr.open(flags, path, &sess));
62 
63     EXPECT_CALL(*m1ptr, close(sess)).WillOnce(Return(true));
64 
65     EXPECT_TRUE(mgr.close(sess));
66 }
67 } // namespace blobs
68