| Kun Yi | 91beea6 | 2018-11-26 15:23:14 -0800 | [diff] [blame] | 1 | #include "handler_unittest.hpp" | 
|  | 2 |  | 
| Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 3 | #include <algorithm> | 
|  | 4 | #include <memory> | 
|  | 5 | #include <string> | 
|  | 6 | #include <vector> | 
|  | 7 |  | 
| Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 8 | using ::testing::_; | 
| Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 9 | using ::testing::AtLeast; | 
|  | 10 | using ::testing::ElementsAreArray; | 
|  | 11 | using ::testing::IsEmpty; | 
| Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 12 | using ::testing::Return; | 
|  | 13 | using ::testing::StrEq; | 
|  | 14 | using ::testing::StrNe; | 
| Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 15 | using ::testing::UnorderedElementsAreArray; | 
|  | 16 |  | 
| Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 17 | using namespace std::string_literals; | 
|  | 18 | using namespace binstore; | 
|  | 19 |  | 
| Kun Yi | 91beea6 | 2018-11-26 15:23:14 -0800 | [diff] [blame] | 20 | namespace blobs | 
|  | 21 | { | 
|  | 22 |  | 
| Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 23 | class BinaryStoreBlobHandlerBasicTest : public BinaryStoreBlobHandlerTest | 
|  | 24 | { | 
|  | 25 | protected: | 
|  | 26 | static inline std::string basicTestBaseId = "/test/"s; | 
|  | 27 | static inline std::string basicTestBlobId = "/test/blob0"s; | 
| Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 28 |  | 
|  | 29 | static const std::vector<std::string> basicTestBaseIdList; | 
| Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 30 | static inline std::string basicTestInvalidBlobId = "/invalid/blob0"s; | 
| Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 31 |  | 
|  | 32 | void addAllBaseIds() | 
|  | 33 | { | 
|  | 34 | for (size_t i = 0; i < basicTestBaseIdList.size(); ++i) | 
|  | 35 | { | 
|  | 36 | auto store = defaultMockStore(basicTestBaseIdList[i]); | 
|  | 37 |  | 
|  | 38 | EXPECT_CALL(*store, getBaseBlobId()).Times(AtLeast(1)); | 
|  | 39 |  | 
|  | 40 | handler.addNewBinaryStore(std::move(store)); | 
|  | 41 | } | 
|  | 42 | } | 
| Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 43 | }; | 
|  | 44 |  | 
| Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 45 | const std::vector<std::string> | 
|  | 46 | BinaryStoreBlobHandlerBasicTest::basicTestBaseIdList = { | 
|  | 47 | BinaryStoreBlobHandlerBasicTest::basicTestBaseId, "/another/"s, | 
|  | 48 | "/null\0/"s}; | 
|  | 49 |  | 
| Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 50 | TEST_F(BinaryStoreBlobHandlerBasicTest, CanHandleBlobZeroStoreFail) | 
|  | 51 | { | 
|  | 52 | // Cannot handle since there is no store. Shouldn't crash. | 
|  | 53 | EXPECT_FALSE(handler.canHandleBlob(basicTestInvalidBlobId)); | 
|  | 54 | } | 
|  | 55 |  | 
| Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 56 | TEST_F(BinaryStoreBlobHandlerBasicTest, CanHandleBlobChecksName) | 
| Kun Yi | 91beea6 | 2018-11-26 15:23:14 -0800 | [diff] [blame] | 57 | { | 
| Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 58 | auto store = defaultMockStore(basicTestBaseId); | 
| Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 59 |  | 
| Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 60 | EXPECT_CALL(*store, getBaseBlobId()).Times(AtLeast(1)); | 
|  | 61 |  | 
|  | 62 | handler.addNewBinaryStore(std::move(store)); | 
| Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 63 |  | 
| Kun Yi | 91beea6 | 2018-11-26 15:23:14 -0800 | [diff] [blame] | 64 | // Verify canHandleBlob checks and returns false on an invalid name. | 
| Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 65 | EXPECT_FALSE(handler.canHandleBlob(basicTestInvalidBlobId)); | 
| Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 66 | // Verify canHandleBlob return true for a blob id that it can handle | 
| Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 67 | EXPECT_TRUE(handler.canHandleBlob(basicTestBlobId)); | 
|  | 68 | } | 
|  | 69 |  | 
| Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 70 | TEST_F(BinaryStoreBlobHandlerBasicTest, GetBlobIdEqualsConcatenationsOfBaseIds) | 
| Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 71 | { | 
| Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 72 | addAllBaseIds(); | 
| Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 73 |  | 
| Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 74 | // When there is no other blob id, ids are just base ids (might be | 
|  | 75 | // re-ordered). | 
|  | 76 | EXPECT_THAT(handler.getBlobIds(), | 
|  | 77 | UnorderedElementsAreArray(basicTestBaseIdList)); | 
| Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 78 | } | 
|  | 79 |  | 
| Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 80 | TEST_F(BinaryStoreBlobHandlerBasicTest, CanHandleBlobInvalidNames) | 
| Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 81 | { | 
| Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 82 | addAllBaseIds(); | 
| Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 83 |  | 
| Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 84 | const std::vector<std::string> invalidNames = { | 
|  | 85 | basicTestInvalidBlobId, | 
|  | 86 | "/"s, | 
|  | 87 | "//"s, | 
|  | 88 | "/test"s, // Cannot handle the base path | 
|  | 89 | "/test/"s, | 
|  | 90 | "/test/this/blob"s, // Cannot handle nested path | 
|  | 91 | }; | 
| Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 92 |  | 
| Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 93 | // Unary helper for algorithm | 
|  | 94 | auto canHandle = [this](const std::string& blobId) { | 
|  | 95 | return handler.canHandleBlob(blobId); | 
|  | 96 | }; | 
| Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 97 |  | 
| Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 98 | EXPECT_TRUE( | 
|  | 99 | std::none_of(invalidNames.cbegin(), invalidNames.cend(), canHandle)); | 
|  | 100 | } | 
|  | 101 |  | 
|  | 102 | TEST_F(BinaryStoreBlobHandlerBasicTest, CanHandleBlobValidNames) | 
|  | 103 | { | 
|  | 104 | addAllBaseIds(); | 
|  | 105 |  | 
|  | 106 | const std::vector<std::string> validNames = { | 
|  | 107 | basicTestBlobId,  "/test/test"s,          "/test/xyz.abc"s, | 
|  | 108 | "/another/blob"s, "/null\0/\0\0zer\0\0"s, | 
|  | 109 | }; | 
|  | 110 |  | 
|  | 111 | // Unary helper for algorithm | 
|  | 112 | auto canHandle = [this](const std::string& blobId) { | 
|  | 113 | return handler.canHandleBlob(blobId); | 
|  | 114 | }; | 
|  | 115 |  | 
|  | 116 | // Verify canHandleBlob can handle a valid blob under the path. | 
|  | 117 | EXPECT_TRUE(std::all_of(validNames.cbegin(), validNames.cend(), canHandle)); | 
| Kun Yi | 91beea6 | 2018-11-26 15:23:14 -0800 | [diff] [blame] | 118 | } | 
|  | 119 |  | 
| Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 120 | TEST_F(BinaryStoreBlobHandlerBasicTest, DeleteReturnsWhatStoreReturns) | 
|  | 121 | { | 
| Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 122 | auto store = defaultMockStore(basicTestBaseId); | 
| Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 123 |  | 
| Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 124 | EXPECT_CALL(*store, getBaseBlobId()).Times(AtLeast(1)); | 
|  | 125 | EXPECT_CALL(*store, deleteBlob(StrEq(basicTestBlobId))) | 
| Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 126 | .WillOnce(Return(false)) | 
|  | 127 | .WillOnce(Return(true)); | 
| Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 128 | handler.addNewBinaryStore(std::move(store)); | 
|  | 129 |  | 
|  | 130 | // Unary helper for algorithm | 
|  | 131 | auto canHandle = [this](const std::string& blobId) { | 
|  | 132 | return handler.canHandleBlob(blobId); | 
|  | 133 | }; | 
| Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 134 |  | 
|  | 135 | // Verify canHandleBlob return true for a blob id that it can handle | 
| Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 136 | EXPECT_FALSE(canHandle(basicTestInvalidBlobId)); | 
|  | 137 | EXPECT_TRUE(canHandle(basicTestBlobId)); | 
| Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 138 | EXPECT_FALSE(handler.deleteBlob(basicTestInvalidBlobId)); | 
|  | 139 | EXPECT_FALSE(handler.deleteBlob(basicTestBlobId)); | 
|  | 140 | EXPECT_TRUE(handler.deleteBlob(basicTestBlobId)); | 
|  | 141 | } | 
|  | 142 |  | 
| Kun Yi | 91beea6 | 2018-11-26 15:23:14 -0800 | [diff] [blame] | 143 | } // namespace blobs |