xref: /openbmc/phosphor-ipmi-blobs/test/manager_delete_unittest.cpp (revision cd833aa11075ac1041531270060e1fb3ba8aa5ac)
1cd8dab49SPatrick Venture #include "blob_mock.hpp"
2cd8dab49SPatrick Venture #include "manager.hpp"
3ef3aeadcSPatrick Venture 
4ef3aeadcSPatrick Venture #include <gtest/gtest.h>
5ef3aeadcSPatrick Venture 
6ef3aeadcSPatrick Venture namespace blobs
7ef3aeadcSPatrick Venture {
8ef3aeadcSPatrick Venture 
9ef3aeadcSPatrick Venture using ::testing::_;
10ef3aeadcSPatrick Venture using ::testing::Return;
11ef3aeadcSPatrick Venture 
TEST(ManagerDeleteTest,FileIsOpenReturnsFailure)12ef3aeadcSPatrick Venture TEST(ManagerDeleteTest, FileIsOpenReturnsFailure)
13ef3aeadcSPatrick Venture {
14ef3aeadcSPatrick Venture     // The blob manager maintains a naive list of open files and will
15ef3aeadcSPatrick Venture     // return failure if you try to delete an open file.
16ef3aeadcSPatrick Venture 
17ef3aeadcSPatrick Venture     // Open the file.
18ef3aeadcSPatrick Venture     BlobManager mgr;
19ef3aeadcSPatrick Venture     std::unique_ptr<BlobMock> m1 = std::make_unique<BlobMock>();
20ef3aeadcSPatrick Venture     auto m1ptr = m1.get();
21ef3aeadcSPatrick Venture     EXPECT_TRUE(mgr.registerHandler(std::move(m1)));
22ef3aeadcSPatrick Venture 
23ef3aeadcSPatrick Venture     uint16_t flags = OpenFlags::read, sess;
24ef3aeadcSPatrick Venture     std::string path = "/asdf/asdf";
25ef3aeadcSPatrick Venture 
26ef3aeadcSPatrick Venture     EXPECT_CALL(*m1ptr, canHandleBlob(path)).WillRepeatedly(Return(true));
27ef3aeadcSPatrick Venture     EXPECT_CALL(*m1ptr, open(_, flags, path)).WillOnce(Return(true));
28ef3aeadcSPatrick Venture     EXPECT_TRUE(mgr.open(flags, path, &sess));
29ef3aeadcSPatrick Venture 
30ef3aeadcSPatrick Venture     // Try to delete the file.
31ef3aeadcSPatrick Venture     EXPECT_FALSE(mgr.deleteBlob(path));
32ef3aeadcSPatrick Venture }
33ef3aeadcSPatrick Venture 
TEST(ManagerDeleteTest,FileHasNoHandler)34ef3aeadcSPatrick Venture TEST(ManagerDeleteTest, FileHasNoHandler)
35ef3aeadcSPatrick Venture {
36ef3aeadcSPatrick Venture     // The blob manager cannot find any handler.
37ef3aeadcSPatrick Venture 
38ef3aeadcSPatrick Venture     BlobManager mgr;
39ef3aeadcSPatrick Venture     std::unique_ptr<BlobMock> m1 = std::make_unique<BlobMock>();
40ef3aeadcSPatrick Venture     auto m1ptr = m1.get();
41ef3aeadcSPatrick Venture     EXPECT_TRUE(mgr.registerHandler(std::move(m1)));
42ef3aeadcSPatrick Venture 
43ef3aeadcSPatrick Venture     std::string path = "/asdf/asdf";
44ef3aeadcSPatrick Venture 
45ef3aeadcSPatrick Venture     EXPECT_CALL(*m1ptr, canHandleBlob(path)).WillOnce(Return(false));
46ef3aeadcSPatrick Venture 
47ef3aeadcSPatrick Venture     // Try to delete the file.
48ef3aeadcSPatrick Venture     EXPECT_FALSE(mgr.deleteBlob(path));
49ef3aeadcSPatrick Venture }
50ef3aeadcSPatrick Venture 
TEST(ManagerDeleteTest,FileIsNotOpenButHandlerDeleteFails)51ef3aeadcSPatrick Venture TEST(ManagerDeleteTest, FileIsNotOpenButHandlerDeleteFails)
52ef3aeadcSPatrick Venture {
53ef3aeadcSPatrick Venture     // The Blob manager finds the handler but the handler returns failure
54ef3aeadcSPatrick Venture     // on delete.
55ef3aeadcSPatrick Venture 
56ef3aeadcSPatrick Venture     BlobManager mgr;
57ef3aeadcSPatrick Venture     std::unique_ptr<BlobMock> m1 = std::make_unique<BlobMock>();
58ef3aeadcSPatrick Venture     auto m1ptr = m1.get();
59ef3aeadcSPatrick Venture     EXPECT_TRUE(mgr.registerHandler(std::move(m1)));
60ef3aeadcSPatrick Venture 
61ef3aeadcSPatrick Venture     std::string path = "/asdf/asdf";
62ef3aeadcSPatrick Venture 
63ef3aeadcSPatrick Venture     EXPECT_CALL(*m1ptr, canHandleBlob(path)).WillOnce(Return(true));
64ef3aeadcSPatrick Venture     EXPECT_CALL(*m1ptr, deleteBlob(path)).WillOnce(Return(false));
65ef3aeadcSPatrick Venture 
66ef3aeadcSPatrick Venture     // Try to delete the file.
67ef3aeadcSPatrick Venture     EXPECT_FALSE(mgr.deleteBlob(path));
68ef3aeadcSPatrick Venture }
69ef3aeadcSPatrick Venture 
TEST(ManagerDeleteTest,FileIsNotOpenAndHandlerSucceeds)70ef3aeadcSPatrick Venture TEST(ManagerDeleteTest, FileIsNotOpenAndHandlerSucceeds)
71ef3aeadcSPatrick Venture {
72ef3aeadcSPatrick Venture     // The Blob manager finds the handler and the handler returns success.
73ef3aeadcSPatrick Venture 
74ef3aeadcSPatrick Venture     BlobManager mgr;
75ef3aeadcSPatrick Venture     std::unique_ptr<BlobMock> m1 = std::make_unique<BlobMock>();
76ef3aeadcSPatrick Venture     auto m1ptr = m1.get();
77ef3aeadcSPatrick Venture     EXPECT_TRUE(mgr.registerHandler(std::move(m1)));
78ef3aeadcSPatrick Venture 
79ef3aeadcSPatrick Venture     std::string path = "/asdf/asdf";
80ef3aeadcSPatrick Venture 
81ef3aeadcSPatrick Venture     EXPECT_CALL(*m1ptr, canHandleBlob(path)).WillOnce(Return(true));
82ef3aeadcSPatrick Venture     EXPECT_CALL(*m1ptr, deleteBlob(path)).WillOnce(Return(true));
83ef3aeadcSPatrick Venture 
84ef3aeadcSPatrick Venture     // Try to delete the file.
85ef3aeadcSPatrick Venture     EXPECT_TRUE(mgr.deleteBlob(path));
86ef3aeadcSPatrick Venture }
87*cd833aa1SKun Yi 
TEST(ManagerDeleteTest,DeleteWorksAfterOpenClose)88*cd833aa1SKun Yi TEST(ManagerDeleteTest, DeleteWorksAfterOpenClose)
89*cd833aa1SKun Yi {
90*cd833aa1SKun Yi     // The Blob manager is able to decrement the ref count and delete.
91*cd833aa1SKun Yi 
92*cd833aa1SKun Yi     BlobManager mgr;
93*cd833aa1SKun Yi     std::unique_ptr<BlobMock> m1 = std::make_unique<BlobMock>();
94*cd833aa1SKun Yi     auto m1ptr = m1.get();
95*cd833aa1SKun Yi     EXPECT_TRUE(mgr.registerHandler(std::move(m1)));
96*cd833aa1SKun Yi 
97*cd833aa1SKun Yi     uint16_t flags = OpenFlags::read, sess;
98*cd833aa1SKun Yi     std::string path = "/asdf/asdf";
99*cd833aa1SKun Yi 
100*cd833aa1SKun Yi     EXPECT_CALL(*m1ptr, canHandleBlob(path)).WillRepeatedly(Return(true));
101*cd833aa1SKun Yi     EXPECT_CALL(*m1ptr, open(_, flags, path)).WillOnce(Return(true));
102*cd833aa1SKun Yi     EXPECT_CALL(*m1ptr, close(_)).WillOnce(Return(true));
103*cd833aa1SKun Yi     EXPECT_CALL(*m1ptr, deleteBlob(path)).WillOnce(Return(true));
104*cd833aa1SKun Yi 
105*cd833aa1SKun Yi     EXPECT_TRUE(mgr.open(flags, path, &sess));
106*cd833aa1SKun Yi     EXPECT_TRUE(mgr.close(sess));
107*cd833aa1SKun Yi 
108*cd833aa1SKun Yi     // Try to delete the file.
109*cd833aa1SKun Yi     EXPECT_TRUE(mgr.deleteBlob(path));
110*cd833aa1SKun Yi }
111ef3aeadcSPatrick Venture } // namespace blobs
112