blob: 33c6d5a6a9dae331ceb868e6fcf66db5bf87afef [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
6using ::testing::_;
7using ::testing::Return;
8
9namespace blobs
10{
11
12TEST(ManagerWriteTest, WriteNoSessionReturnsFalse)
13{
14 // Calling Write 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.write(sess, ofs, data));
22}
23
24TEST(ManagerWriteTest, WriteSessionFoundButHandlerReturnsFalse)
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, write(sess, ofs, data)).WillOnce(Return(false));
43
44 EXPECT_FALSE(mgr.write(sess, ofs, data));
45}
46
47TEST(ManagerWriteTest, WriteFailsBecauseFileOpenedReadOnly)
48{
49 // The manager will not route a write 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_FALSE(mgr.write(sess, ofs, data));
66}
67
68TEST(ManagerWriteTest, WriteSessionFoundAndHandlerReturnsSuccess)
69{
70 // The handler was found and returned success.
71
72 BlobManager mgr;
73 std::unique_ptr<BlobMock> m1 = std::make_unique<BlobMock>();
74 auto m1ptr = m1.get();
75 EXPECT_TRUE(mgr.registerHandler(std::move(m1)));
76
77 uint16_t flags = OpenFlags::write, sess;
78 std::string path = "/asdf/asdf";
79 uint32_t ofs = 0x54;
80 std::vector<uint8_t> data = {0x11, 0x22};
81
82 EXPECT_CALL(*m1ptr, canHandleBlob(path)).WillOnce(Return(true));
83 EXPECT_CALL(*m1ptr, open(_, flags, path)).WillOnce(Return(true));
84 EXPECT_TRUE(mgr.open(flags, path, &sess));
85
86 EXPECT_CALL(*m1ptr, write(sess, ofs, data)).WillOnce(Return(true));
87
88 EXPECT_TRUE(mgr.write(sess, ofs, data));
89}
90} // namespace blobs