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(ManagerDeleteTest, FileIsOpenReturnsFailure)
14 {
15     // The blob manager maintains a naive list of open files and will
16     // return failure if you try to delete an open file.
17 
18     // Open the file.
19     BlobManager mgr;
20     std::unique_ptr<BlobMock> m1 = std::make_unique<BlobMock>();
21     auto m1ptr = m1.get();
22     EXPECT_TRUE(mgr.registerHandler(std::move(m1)));
23 
24     uint16_t flags = OpenFlags::read, sess;
25     std::string path = "/asdf/asdf";
26 
27     EXPECT_CALL(*m1ptr, canHandleBlob(path)).WillRepeatedly(Return(true));
28     EXPECT_CALL(*m1ptr, open(_, flags, path)).WillOnce(Return(true));
29     EXPECT_TRUE(mgr.open(flags, path, &sess));
30 
31     // Try to delete the file.
32     EXPECT_FALSE(mgr.deleteBlob(path));
33 }
34 
35 TEST(ManagerDeleteTest, FileHasNoHandler)
36 {
37     // The blob manager cannot find any handler.
38 
39     BlobManager mgr;
40     std::unique_ptr<BlobMock> m1 = std::make_unique<BlobMock>();
41     auto m1ptr = m1.get();
42     EXPECT_TRUE(mgr.registerHandler(std::move(m1)));
43 
44     std::string path = "/asdf/asdf";
45 
46     EXPECT_CALL(*m1ptr, canHandleBlob(path)).WillOnce(Return(false));
47 
48     // Try to delete the file.
49     EXPECT_FALSE(mgr.deleteBlob(path));
50 }
51 
52 TEST(ManagerDeleteTest, FileIsNotOpenButHandlerDeleteFails)
53 {
54     // The Blob manager finds the handler but the handler returns failure
55     // on delete.
56 
57     BlobManager mgr;
58     std::unique_ptr<BlobMock> m1 = std::make_unique<BlobMock>();
59     auto m1ptr = m1.get();
60     EXPECT_TRUE(mgr.registerHandler(std::move(m1)));
61 
62     std::string path = "/asdf/asdf";
63 
64     EXPECT_CALL(*m1ptr, canHandleBlob(path)).WillOnce(Return(true));
65     EXPECT_CALL(*m1ptr, deleteBlob(path)).WillOnce(Return(false));
66 
67     // Try to delete the file.
68     EXPECT_FALSE(mgr.deleteBlob(path));
69 }
70 
71 TEST(ManagerDeleteTest, FileIsNotOpenAndHandlerSucceeds)
72 {
73     // The Blob manager finds the handler and the handler returns success.
74 
75     BlobManager mgr;
76     std::unique_ptr<BlobMock> m1 = std::make_unique<BlobMock>();
77     auto m1ptr = m1.get();
78     EXPECT_TRUE(mgr.registerHandler(std::move(m1)));
79 
80     std::string path = "/asdf/asdf";
81 
82     EXPECT_CALL(*m1ptr, canHandleBlob(path)).WillOnce(Return(true));
83     EXPECT_CALL(*m1ptr, deleteBlob(path)).WillOnce(Return(true));
84 
85     // Try to delete the file.
86     EXPECT_TRUE(mgr.deleteBlob(path));
87 }
88 } // namespace blobs
89