blob: 5579499a5f7cc73e2b91b7bb2494765b32f7f77f [file] [log] [blame]
Kun Yic0adbc32018-12-18 22:35:29 -08001#include "handler_unittest.hpp"
2
Kun Yi64dc05c2018-12-19 13:19:03 -08003#include <cstdint>
4#include <memory>
5#include <string>
6#include <vector>
7
Kun Yic0adbc32018-12-18 22:35:29 -08008using ::testing::_;
Kun Yi6967b772019-02-22 13:48:12 -08009using ::testing::ElementsAre;
10using ::testing::IsEmpty;
Kun Yic0adbc32018-12-18 22:35:29 -080011using ::testing::Return;
12using ::testing::StartsWith;
13
14using namespace std::string_literals;
15using namespace binstore;
16
17namespace blobs
18{
19
20class BinaryStoreBlobHandlerReadWriteTest : public BinaryStoreBlobHandlerTest
21{
22 protected:
Kun Yi6967b772019-02-22 13:48:12 -080023 BinaryStoreBlobHandlerReadWriteTest()
24 {
25 addDefaultStore(rwTestBaseId);
26 }
27
Kun Yic0adbc32018-12-18 22:35:29 -080028 static inline std::string rwTestBaseId = "/test/"s;
29 static inline std::string rwTestBlobId = "/test/blob0"s;
30 static inline std::vector<uint8_t> rwTestData = {0, 1, 2, 3, 4,
31 5, 6, 7, 8, 9};
Kun Yi6967b772019-02-22 13:48:12 -080032 static inline uint16_t rwTestROFlags = OpenFlags::read;
33 static inline uint16_t rwTestRWFlags = OpenFlags::read | OpenFlags::write;
Kun Yic0adbc32018-12-18 22:35:29 -080034 static inline uint16_t rwTestSessionId = 0;
35 static inline uint32_t rwTestOffset = 0;
Kun Yi6967b772019-02-22 13:48:12 -080036
37 void openAndWriteTestData()
38 {
39 EXPECT_TRUE(handler.open(rwTestSessionId, rwTestRWFlags, rwTestBlobId));
40 EXPECT_TRUE(handler.write(rwTestSessionId, rwTestOffset, rwTestData));
41 }
Kun Yic0adbc32018-12-18 22:35:29 -080042};
43
44TEST_F(BinaryStoreBlobHandlerReadWriteTest, ReadWriteReturnsWhatStoreReturns)
45{
Kun Yic0adbc32018-12-18 22:35:29 -080046 const std::vector<uint8_t> emptyData;
Kun Yi64dc05c2018-12-19 13:19:03 -080047 auto store = defaultMockStore(rwTestBaseId);
Kun Yic0adbc32018-12-18 22:35:29 -080048
Kun Yi6967b772019-02-22 13:48:12 -080049 EXPECT_CALL(*store, openOrCreateBlob(_, rwTestRWFlags))
50 .WillOnce(Return(true));
Kun Yi64dc05c2018-12-19 13:19:03 -080051 EXPECT_CALL(*store, read(rwTestOffset, _))
Kun Yic0adbc32018-12-18 22:35:29 -080052 .WillOnce(Return(emptyData))
53 .WillOnce(Return(rwTestData));
54
Kun Yi64dc05c2018-12-19 13:19:03 -080055 EXPECT_CALL(*store, write(rwTestOffset, emptyData)).WillOnce(Return(false));
56 EXPECT_CALL(*store, write(rwTestOffset, rwTestData)).WillOnce(Return(true));
Kun Yic0adbc32018-12-18 22:35:29 -080057
Kun Yi64dc05c2018-12-19 13:19:03 -080058 handler.addNewBinaryStore(std::move(store));
Kun Yic0adbc32018-12-18 22:35:29 -080059
Kun Yi6967b772019-02-22 13:48:12 -080060 EXPECT_TRUE(handler.open(rwTestSessionId, rwTestRWFlags, rwTestBlobId));
Kun Yic0adbc32018-12-18 22:35:29 -080061 EXPECT_EQ(emptyData, handler.read(rwTestSessionId, rwTestOffset, 1));
62 EXPECT_EQ(rwTestData, handler.read(rwTestSessionId, rwTestOffset, 1));
63 EXPECT_FALSE(handler.write(rwTestSessionId, rwTestOffset, emptyData));
64 EXPECT_TRUE(handler.write(rwTestSessionId, rwTestOffset, rwTestData));
65}
66
Kun Yi6967b772019-02-22 13:48:12 -080067TEST_F(BinaryStoreBlobHandlerReadWriteTest, WriteFailForWrongSession)
68{
69 uint16_t wrongSessionId = 1;
70 EXPECT_TRUE(handler.open(rwTestSessionId, rwTestRWFlags, rwTestBlobId));
71 EXPECT_FALSE(handler.write(wrongSessionId, rwTestOffset, rwTestData));
72}
73
74TEST_F(BinaryStoreBlobHandlerReadWriteTest, WriteFailForWrongFlag)
75{
76 EXPECT_TRUE(handler.open(rwTestSessionId, rwTestROFlags, rwTestBlobId));
77 EXPECT_FALSE(handler.write(rwTestSessionId, rwTestOffset, rwTestData));
78}
79
80TEST_F(BinaryStoreBlobHandlerReadWriteTest, ReadReturnEmptyForWrongSession)
81{
82 uint16_t wrongSessionId = 1;
83 EXPECT_TRUE(handler.open(rwTestSessionId, rwTestROFlags, rwTestBlobId));
84 EXPECT_THAT(handler.read(wrongSessionId, rwTestOffset, rwTestData.size()),
85 IsEmpty());
86}
87
88TEST_F(BinaryStoreBlobHandlerReadWriteTest, AbleToReadDataWritten)
89{
90 openAndWriteTestData();
91 EXPECT_EQ(rwTestData,
92 handler.read(rwTestSessionId, rwTestOffset, rwTestData.size()));
93}
94
95TEST_F(BinaryStoreBlobHandlerReadWriteTest, ReadBeyondEndReturnsEmpty)
96{
97 openAndWriteTestData();
98 const size_t offsetBeyondEnd = rwTestData.size();
99 EXPECT_THAT(
100 handler.read(rwTestSessionId, offsetBeyondEnd, rwTestData.size()),
101 IsEmpty());
102}
103
104TEST_F(BinaryStoreBlobHandlerReadWriteTest, ReadAtOffset)
105{
106 openAndWriteTestData();
107 EXPECT_EQ(rwTestData,
108 handler.read(rwTestSessionId, rwTestOffset, rwTestData.size()));
109
110 // Now read over the entire offset range for 1 byte
111 for (size_t i = 0; i < rwTestData.size(); ++i)
112 {
113 EXPECT_THAT(handler.read(rwTestSessionId, i, 1),
114 ElementsAre(rwTestData[i]));
115 }
116}
117
118TEST_F(BinaryStoreBlobHandlerReadWriteTest, PartialOverwriteDataWorks)
119{
120 // Construct a partially overwritten vector
121 const size_t overwritePos = 8;
122 std::vector<uint8_t> overwriteData = {8, 9, 10, 11, 12};
123 std::vector<uint8_t> expectedOverWrittenData(
124 rwTestData.begin(), rwTestData.begin() + overwritePos);
125 expectedOverWrittenData.insert(expectedOverWrittenData.end(),
126 overwriteData.begin(), overwriteData.end());
127
128 openAndWriteTestData();
129 EXPECT_EQ(rwTestData,
130 handler.read(rwTestSessionId, rwTestOffset, rwTestData.size()));
131 EXPECT_TRUE(handler.write(rwTestSessionId, overwritePos, overwriteData));
132
133 EXPECT_EQ(expectedOverWrittenData,
134 handler.read(rwTestSessionId, 0, expectedOverWrittenData.size()));
135
136 EXPECT_THAT(
137 handler.read(rwTestSessionId, expectedOverWrittenData.size(), 1),
138 IsEmpty());
139}
140
141TEST_F(BinaryStoreBlobHandlerReadWriteTest, WriteWithGapShouldFail)
142{
143 const auto gapOffset = 10;
144 EXPECT_TRUE(handler.open(rwTestSessionId, rwTestRWFlags, rwTestBlobId));
145 EXPECT_FALSE(handler.write(rwTestSessionId, gapOffset, rwTestData));
146}
147
Kun Yic0adbc32018-12-18 22:35:29 -0800148} // namespace blobs