blob: 309d3f684fc236ae53e2cfb9ab3d1c9a320db204 [file] [log] [blame]
Patrick Ventureef3aead2018-09-12 08:53:29 -07001#include "blob_mock.hpp"
2#include "manager.hpp"
3
4#include <string>
5
6#include <gtest/gtest.h>
7
8namespace blobs
9{
10
11using ::testing::_;
12using ::testing::Return;
13
14TEST(ManagerOpenTest, OpenButNoHandler)
15{
16 // No handler claims to be able to open the file.
17
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)).WillOnce(Return(false));
27 EXPECT_FALSE(mgr.open(flags, path, &sess));
28}
29
30TEST(ManagerOpenTest, OpenButHandlerFailsOpen)
31{
32 // The handler is found but Open fails.
33
34 BlobManager mgr;
35 std::unique_ptr<BlobMock> m1 = std::make_unique<BlobMock>();
36 auto m1ptr = m1.get();
37 EXPECT_TRUE(mgr.registerHandler(std::move(m1)));
38
39 uint16_t flags = OpenFlags::read, sess;
40 std::string path = "/asdf/asdf";
41
42 EXPECT_CALL(*m1ptr, canHandleBlob(path)).WillOnce(Return(true));
43 EXPECT_CALL(*m1ptr, open(_, flags, path)).WillOnce(Return(false));
44 EXPECT_FALSE(mgr.open(flags, path, &sess));
45}
46
47TEST(ManagerOpenTest, OpenFailsMustSupplyAtLeastReadOrWriteFlag)
48{
49 // One must supply either read or write in the flags for the session to
50 // open.
51
52 BlobManager mgr;
53 std::unique_ptr<BlobMock> m1 = std::make_unique<BlobMock>();
54 auto m1ptr = m1.get();
55 EXPECT_TRUE(mgr.registerHandler(std::move(m1)));
56
57 uint16_t flags = 0, sess;
58 std::string path = "/asdf/asdf";
59
60 /* It checks if someone can handle the blob before it checks the flags. */
61 EXPECT_CALL(*m1ptr, canHandleBlob(path)).WillOnce(Return(true));
62
63 EXPECT_FALSE(mgr.open(flags, path, &sess));
64}
65
66TEST(ManagerOpenTest, OpenSucceeds)
67{
68 // The handler is found and Open succeeds.
69
70 BlobManager mgr;
71 std::unique_ptr<BlobMock> m1 = std::make_unique<BlobMock>();
72 auto m1ptr = m1.get();
73 EXPECT_TRUE(mgr.registerHandler(std::move(m1)));
74
75 uint16_t flags = OpenFlags::read, sess;
76 std::string path = "/asdf/asdf";
77
78 EXPECT_CALL(*m1ptr, canHandleBlob(path)).WillOnce(Return(true));
79 EXPECT_CALL(*m1ptr, open(_, flags, path)).WillOnce(Return(true));
80 EXPECT_TRUE(mgr.open(flags, path, &sess));
81
82 // TODO(venture): Need a way to verify the session is associated with it,
83 // maybe just call Read() or SessionStat()
84}
85} // namespace blobs