blob: 565eabfe8802d00aa31701443763d79703e3942c [file] [log] [blame]
Patrick Venturecd8dab42019-01-15 19:57:38 -08001#include "blob_mock.hpp"
2#include "manager.hpp"
3
Patrick Ventureef3aead2018-09-12 08:53:29 -07004#include <vector>
5
6#include <gtest/gtest.h>
7
8namespace blobs
9{
10
11using ::testing::_;
12using ::testing::Return;
13
14TEST(ManagerReadTest, ReadNoSessionReturnsFalse)
15{
16 // Calling Read on a session that doesn't exist should return false.
17
18 BlobManager mgr;
19 uint16_t sess = 1;
20 uint32_t ofs = 0x54;
21 uint32_t requested = 0x100;
22
23 std::vector<uint8_t> result = mgr.read(sess, ofs, requested);
24 EXPECT_EQ(0, result.size());
25}
26
27TEST(ManagerReadTest, ReadFromWriteOnlyFails)
28{
29 // The session manager will not route a Read call to a blob if the session
30 // was opened as write-only.
31
32 BlobManager mgr;
33 std::unique_ptr<BlobMock> m1 = std::make_unique<BlobMock>();
34 auto m1ptr = m1.get();
35 EXPECT_TRUE(mgr.registerHandler(std::move(m1)));
36
37 uint16_t sess = 1;
38 uint32_t ofs = 0x54;
39 uint32_t requested = 0x100;
40 uint16_t flags = OpenFlags::write;
41 std::string path = "/asdf/asdf";
42
43 EXPECT_CALL(*m1ptr, canHandleBlob(path)).WillOnce(Return(true));
44 EXPECT_CALL(*m1ptr, open(_, flags, path)).WillOnce(Return(true));
45 EXPECT_TRUE(mgr.open(flags, path, &sess));
46
47 std::vector<uint8_t> result = mgr.read(sess, ofs, requested);
48 EXPECT_EQ(0, result.size());
49}
50
51TEST(ManagerReadTest, ReadFromHandlerReturnsData)
52{
53 // There is no logic in this as it's just as a pass-thru command, however
54 // we want to verify this behavior doesn't change.
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 uint16_t sess = 1;
62 uint32_t ofs = 0x54;
Patrick Venture86c87f52019-02-01 14:44:15 -080063 uint32_t requested = 0x10;
Patrick Ventureef3aead2018-09-12 08:53:29 -070064 uint16_t flags = OpenFlags::read;
65 std::string path = "/asdf/asdf";
66 std::vector<uint8_t> data = {0x12, 0x14, 0x15, 0x16};
67
68 EXPECT_CALL(*m1ptr, canHandleBlob(path)).WillOnce(Return(true));
69 EXPECT_CALL(*m1ptr, open(_, flags, path)).WillOnce(Return(true));
70 EXPECT_TRUE(mgr.open(flags, path, &sess));
71
72 EXPECT_CALL(*m1ptr, read(sess, ofs, requested)).WillOnce(Return(data));
73
74 std::vector<uint8_t> result = mgr.read(sess, ofs, requested);
75 EXPECT_EQ(data.size(), result.size());
76 EXPECT_EQ(result, data);
77}
Patrick Venture86c87f52019-02-01 14:44:15 -080078
Patrick Ventureef3aead2018-09-12 08:53:29 -070079} // namespace blobs