blob: 919d259e92097647e15c63d2f44f1e8f1bbe4a00 [file] [log] [blame]
Patrick Venturecd8dab42019-01-15 19:57:38 -08001#include "blob_mock.hpp"
2#include "manager.hpp"
Patrick Venture5c4b17b2018-10-04 10:32:22 -07003
4#include <gtest/gtest.h>
5
6using ::testing::_;
7using ::testing::Return;
8
9namespace blobs
10{
11
12TEST(ManagerWriteMetaTest, WriteMetaNoSessionReturnsFalse)
13{
14 // Calling WriteMeta on a session that doesn't exist should return false.
15
16 BlobManager mgr;
17 uint16_t sess = 1;
18 uint32_t ofs = 0x54;
19 std::vector<uint8_t> data = {0x11, 0x22};
20
21 EXPECT_FALSE(mgr.writeMeta(sess, ofs, data));
22}
23
24TEST(ManagerWriteMetaTest, WriteMetaSessionFoundButHandlerReturnsFalse)
25{
26 // The handler was found but it returned failure.
27
28 BlobManager mgr;
29 std::unique_ptr<BlobMock> m1 = std::make_unique<BlobMock>();
30 auto m1ptr = m1.get();
31 EXPECT_TRUE(mgr.registerHandler(std::move(m1)));
32
33 uint16_t flags = OpenFlags::write, sess;
34 std::string path = "/asdf/asdf";
35 uint32_t ofs = 0x54;
36 std::vector<uint8_t> data = {0x11, 0x22};
37
38 EXPECT_CALL(*m1ptr, canHandleBlob(path)).WillOnce(Return(true));
39 EXPECT_CALL(*m1ptr, open(_, flags, path)).WillOnce(Return(true));
40 EXPECT_TRUE(mgr.open(flags, path, &sess));
41
42 EXPECT_CALL(*m1ptr, writeMeta(sess, ofs, data)).WillOnce(Return(false));
43
44 EXPECT_FALSE(mgr.writeMeta(sess, ofs, data));
45}
46
47TEST(ManagerWriteMetaTest, WriteMetaSucceedsEvenIfFileOpenedReadOnly)
48{
49 // The manager will not route a WriteMeta call to a file opened read-only.
50
51 BlobManager mgr;
52 std::unique_ptr<BlobMock> m1 = std::make_unique<BlobMock>();
53 auto m1ptr = m1.get();
54 EXPECT_TRUE(mgr.registerHandler(std::move(m1)));
55
56 uint16_t flags = OpenFlags::read, sess;
57 std::string path = "/asdf/asdf";
58 uint32_t ofs = 0x54;
59 std::vector<uint8_t> data = {0x11, 0x22};
60
61 EXPECT_CALL(*m1ptr, canHandleBlob(path)).WillOnce(Return(true));
62 EXPECT_CALL(*m1ptr, open(_, flags, path)).WillOnce(Return(true));
63 EXPECT_TRUE(mgr.open(flags, path, &sess));
64
65 EXPECT_CALL(*m1ptr, writeMeta(sess, ofs, data)).WillOnce(Return(true));
66
67 EXPECT_TRUE(mgr.writeMeta(sess, ofs, data));
68}
69
70TEST(ManagerWriteMetaTest, WriteMetaMetaSessionFoundAndHandlerReturnsSuccess)
71{
72 // The handler was found and returned 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 uint16_t flags = OpenFlags::write, sess;
80 std::string path = "/asdf/asdf";
81 uint32_t ofs = 0x54;
82 std::vector<uint8_t> data = {0x11, 0x22};
83
84 EXPECT_CALL(*m1ptr, canHandleBlob(path)).WillOnce(Return(true));
85 EXPECT_CALL(*m1ptr, open(_, flags, path)).WillOnce(Return(true));
86 EXPECT_TRUE(mgr.open(flags, path, &sess));
87
88 EXPECT_CALL(*m1ptr, writeMeta(sess, ofs, data)).WillOnce(Return(true));
89
90 EXPECT_TRUE(mgr.writeMeta(sess, ofs, data));
91}
92} // namespace blobs