Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame^] | 1 | #include "handler_unittest.hpp" |
| 2 | |
| 3 | using ::testing::_; |
| 4 | using ::testing::Return; |
| 5 | using ::testing::StartsWith; |
| 6 | |
| 7 | using namespace std::string_literals; |
| 8 | using namespace binstore; |
| 9 | |
| 10 | namespace blobs |
| 11 | { |
| 12 | |
| 13 | class BinaryStoreBlobHandlerReadWriteTest : public BinaryStoreBlobHandlerTest |
| 14 | { |
| 15 | protected: |
| 16 | static inline std::string rwTestBaseId = "/test/"s; |
| 17 | static inline std::string rwTestBlobId = "/test/blob0"s; |
| 18 | static inline std::vector<uint8_t> rwTestData = {0, 1, 2, 3, 4, |
| 19 | 5, 6, 7, 8, 9}; |
| 20 | static inline uint16_t rwTestSessionId = 0; |
| 21 | static inline uint32_t rwTestOffset = 0; |
| 22 | }; |
| 23 | |
| 24 | TEST_F(BinaryStoreBlobHandlerReadWriteTest, ReadWriteReturnsWhatStoreReturns) |
| 25 | { |
| 26 | auto testBaseId = "/test/"s; |
| 27 | auto testBlobId = "/test/blob0"s; |
| 28 | uint16_t flags = OpenFlags::read; |
| 29 | const std::vector<uint8_t> emptyData; |
| 30 | auto bstore = std::make_unique<MockBinaryStore>(); |
| 31 | |
| 32 | EXPECT_CALL(*bstore, getBaseBlobId()).WillRepeatedly(Return(testBaseId)); |
| 33 | EXPECT_CALL(*bstore, canHandleBlob(StartsWith(testBaseId))) |
| 34 | .WillRepeatedly(Return(true)); |
| 35 | EXPECT_CALL(*bstore, openOrCreateBlob(_, flags)).WillOnce(Return(true)); |
| 36 | EXPECT_CALL(*bstore, read(rwTestOffset, _)) |
| 37 | .WillOnce(Return(emptyData)) |
| 38 | .WillOnce(Return(rwTestData)); |
| 39 | |
| 40 | EXPECT_CALL(*bstore, write(rwTestOffset, emptyData)) |
| 41 | .WillOnce(Return(false)); |
| 42 | EXPECT_CALL(*bstore, write(rwTestOffset, rwTestData)) |
| 43 | .WillOnce(Return(true)); |
| 44 | |
| 45 | handler.addNewBinaryStore(std::move(bstore)); |
| 46 | |
| 47 | EXPECT_TRUE(handler.open(rwTestSessionId, flags, testBlobId)); |
| 48 | EXPECT_EQ(emptyData, handler.read(rwTestSessionId, rwTestOffset, 1)); |
| 49 | EXPECT_EQ(rwTestData, handler.read(rwTestSessionId, rwTestOffset, 1)); |
| 50 | EXPECT_FALSE(handler.write(rwTestSessionId, rwTestOffset, emptyData)); |
| 51 | EXPECT_TRUE(handler.write(rwTestSessionId, rwTestOffset, rwTestData)); |
| 52 | } |
| 53 | |
| 54 | } // namespace blobs |