blob: 9ad3afd3da9793d872b3ee57acc4824e28d4cfeb [file] [log] [blame]
Patrick Venturecd8dab42019-01-15 19:57:38 -08001#include "blob_mock.hpp"
2#include "manager.hpp"
Patrick Ventureef3aead2018-09-12 08:53:29 -07003
4#include <gtest/gtest.h>
5
6namespace blobs
7{
8
9using ::testing::_;
10using ::testing::Return;
11
12TEST(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
34TEST(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
51TEST(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
70TEST(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} // namespace blobs