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::_; |
Kun Yi | 6967b77 | 2019-02-22 13:48:12 -0800 | [diff] [blame^] | 9 | using ::testing::ElementsAre; |
| 10 | using ::testing::IsEmpty; |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 11 | using ::testing::Return; |
| 12 | using ::testing::StartsWith; |
| 13 | |
| 14 | using namespace std::string_literals; |
| 15 | using namespace binstore; |
| 16 | |
| 17 | namespace blobs |
| 18 | { |
| 19 | |
| 20 | class BinaryStoreBlobHandlerReadWriteTest : public BinaryStoreBlobHandlerTest |
| 21 | { |
| 22 | protected: |
Kun Yi | 6967b77 | 2019-02-22 13:48:12 -0800 | [diff] [blame^] | 23 | BinaryStoreBlobHandlerReadWriteTest() |
| 24 | { |
| 25 | addDefaultStore(rwTestBaseId); |
| 26 | } |
| 27 | |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 28 | 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 Yi | 6967b77 | 2019-02-22 13:48:12 -0800 | [diff] [blame^] | 32 | static inline uint16_t rwTestROFlags = OpenFlags::read; |
| 33 | static inline uint16_t rwTestRWFlags = OpenFlags::read | OpenFlags::write; |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 34 | static inline uint16_t rwTestSessionId = 0; |
| 35 | static inline uint32_t rwTestOffset = 0; |
Kun Yi | 6967b77 | 2019-02-22 13:48:12 -0800 | [diff] [blame^] | 36 | |
| 37 | void openAndWriteTestData() |
| 38 | { |
| 39 | EXPECT_TRUE(handler.open(rwTestSessionId, rwTestRWFlags, rwTestBlobId)); |
| 40 | EXPECT_TRUE(handler.write(rwTestSessionId, rwTestOffset, rwTestData)); |
| 41 | } |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | TEST_F(BinaryStoreBlobHandlerReadWriteTest, ReadWriteReturnsWhatStoreReturns) |
| 45 | { |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 46 | const std::vector<uint8_t> emptyData; |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 47 | auto store = defaultMockStore(rwTestBaseId); |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 48 | |
Kun Yi | 6967b77 | 2019-02-22 13:48:12 -0800 | [diff] [blame^] | 49 | EXPECT_CALL(*store, openOrCreateBlob(_, rwTestRWFlags)) |
| 50 | .WillOnce(Return(true)); |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 51 | EXPECT_CALL(*store, read(rwTestOffset, _)) |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 52 | .WillOnce(Return(emptyData)) |
| 53 | .WillOnce(Return(rwTestData)); |
| 54 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 55 | EXPECT_CALL(*store, write(rwTestOffset, emptyData)).WillOnce(Return(false)); |
| 56 | EXPECT_CALL(*store, write(rwTestOffset, rwTestData)).WillOnce(Return(true)); |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 57 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 58 | handler.addNewBinaryStore(std::move(store)); |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 59 | |
Kun Yi | 6967b77 | 2019-02-22 13:48:12 -0800 | [diff] [blame^] | 60 | EXPECT_TRUE(handler.open(rwTestSessionId, rwTestRWFlags, rwTestBlobId)); |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 61 | 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 Yi | 6967b77 | 2019-02-22 13:48:12 -0800 | [diff] [blame^] | 67 | TEST_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 | |
| 74 | TEST_F(BinaryStoreBlobHandlerReadWriteTest, WriteFailForWrongFlag) |
| 75 | { |
| 76 | EXPECT_TRUE(handler.open(rwTestSessionId, rwTestROFlags, rwTestBlobId)); |
| 77 | EXPECT_FALSE(handler.write(rwTestSessionId, rwTestOffset, rwTestData)); |
| 78 | } |
| 79 | |
| 80 | TEST_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 | |
| 88 | TEST_F(BinaryStoreBlobHandlerReadWriteTest, AbleToReadDataWritten) |
| 89 | { |
| 90 | openAndWriteTestData(); |
| 91 | EXPECT_EQ(rwTestData, |
| 92 | handler.read(rwTestSessionId, rwTestOffset, rwTestData.size())); |
| 93 | } |
| 94 | |
| 95 | TEST_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 | |
| 104 | TEST_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 | |
| 118 | TEST_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 | |
| 141 | TEST_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 Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 148 | } // namespace blobs |