Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 1 | #include "handler_unittest.hpp" |
| 2 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame^] | 3 | #include <cstdint> |
| 4 | #include <memory> |
| 5 | #include <string> |
| 6 | #include <vector> |
| 7 | |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 8 | using ::testing::_; |
| 9 | using ::testing::Return; |
| 10 | using ::testing::StartsWith; |
| 11 | |
| 12 | using namespace std::string_literals; |
| 13 | using namespace binstore; |
| 14 | |
| 15 | namespace blobs |
| 16 | { |
| 17 | |
| 18 | class 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 | |
| 29 | TEST_F(BinaryStoreBlobHandlerReadWriteTest, ReadWriteReturnsWhatStoreReturns) |
| 30 | { |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 31 | uint16_t flags = OpenFlags::read; |
| 32 | const std::vector<uint8_t> emptyData; |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame^] | 33 | auto store = defaultMockStore(rwTestBaseId); |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 34 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame^] | 35 | EXPECT_CALL(*store, openOrCreateBlob(_, flags)).WillOnce(Return(true)); |
| 36 | EXPECT_CALL(*store, read(rwTestOffset, _)) |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 37 | .WillOnce(Return(emptyData)) |
| 38 | .WillOnce(Return(rwTestData)); |
| 39 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame^] | 40 | EXPECT_CALL(*store, write(rwTestOffset, emptyData)).WillOnce(Return(false)); |
| 41 | EXPECT_CALL(*store, write(rwTestOffset, rwTestData)).WillOnce(Return(true)); |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 42 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame^] | 43 | handler.addNewBinaryStore(std::move(store)); |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 44 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame^] | 45 | EXPECT_TRUE(handler.open(rwTestSessionId, flags, rwTestBlobId)); |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 46 | 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 |