blob: 17e3cefdab312cd7e840d8f9c4ca7210f4918ba1 [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::_;
9using ::testing::Return;
10using ::testing::StartsWith;
11
12using namespace std::string_literals;
13using namespace binstore;
14
15namespace blobs
16{
17
18class BinaryStoreBlobHandlerReadWriteTest : public BinaryStoreBlobHandlerTest
19{
20 protected:
21 static inline std::string rwTestBaseId = "/test/"s;
22 static inline std::string rwTestBlobId = "/test/blob0"s;
23 static inline std::vector<uint8_t> rwTestData = {0, 1, 2, 3, 4,
24 5, 6, 7, 8, 9};
25 static inline uint16_t rwTestSessionId = 0;
26 static inline uint32_t rwTestOffset = 0;
27};
28
29TEST_F(BinaryStoreBlobHandlerReadWriteTest, ReadWriteReturnsWhatStoreReturns)
30{
Kun Yic0adbc32018-12-18 22:35:29 -080031 uint16_t flags = OpenFlags::read;
32 const std::vector<uint8_t> emptyData;
Kun Yi64dc05c2018-12-19 13:19:03 -080033 auto store = defaultMockStore(rwTestBaseId);
Kun Yic0adbc32018-12-18 22:35:29 -080034
Kun Yi64dc05c2018-12-19 13:19:03 -080035 EXPECT_CALL(*store, openOrCreateBlob(_, flags)).WillOnce(Return(true));
36 EXPECT_CALL(*store, read(rwTestOffset, _))
Kun Yic0adbc32018-12-18 22:35:29 -080037 .WillOnce(Return(emptyData))
38 .WillOnce(Return(rwTestData));
39
Kun Yi64dc05c2018-12-19 13:19:03 -080040 EXPECT_CALL(*store, write(rwTestOffset, emptyData)).WillOnce(Return(false));
41 EXPECT_CALL(*store, write(rwTestOffset, rwTestData)).WillOnce(Return(true));
Kun Yic0adbc32018-12-18 22:35:29 -080042
Kun Yi64dc05c2018-12-19 13:19:03 -080043 handler.addNewBinaryStore(std::move(store));
Kun Yic0adbc32018-12-18 22:35:29 -080044
Kun Yi64dc05c2018-12-19 13:19:03 -080045 EXPECT_TRUE(handler.open(rwTestSessionId, flags, rwTestBlobId));
Kun Yic0adbc32018-12-18 22:35:29 -080046 EXPECT_EQ(emptyData, handler.read(rwTestSessionId, rwTestOffset, 1));
47 EXPECT_EQ(rwTestData, handler.read(rwTestSessionId, rwTestOffset, 1));
48 EXPECT_FALSE(handler.write(rwTestSessionId, rwTestOffset, emptyData));
49 EXPECT_TRUE(handler.write(rwTestSessionId, rwTestOffset, rwTestData));
50}
51
52} // namespace blobs