blob: 62a2e4cc360ffd445589794179c189cda5cb6f55 [file] [log] [blame]
Patrick Ventureef3aead2018-09-12 08:53:29 -07001#include "blob_mock.hpp"
Patrick Ventureaceb4ba2018-09-27 14:50:37 -07002
3#include <blobs-ipmid/manager.hpp>
Patrick Ventureef3aead2018-09-12 08:53:29 -07004
5#include <gtest/gtest.h>
6
7namespace blobs
8{
9
10using ::testing::_;
11using ::testing::Return;
12
13TEST(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
35TEST(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
52TEST(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
71TEST(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