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