Kun Yi | 91beea6 | 2018-11-26 15:23:14 -0800 | [diff] [blame] | 1 | #include "handler_unittest.hpp" |
| 2 | |
Kun Yi | 8ca234e | 2019-03-04 10:14:45 -0800 | [diff] [blame^] | 3 | #include "fake_sys_file.hpp" |
| 4 | |
| 5 | #include <google/protobuf/message_lite.h> |
| 6 | #include <google/protobuf/text_format.h> |
| 7 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 8 | #include <algorithm> |
Kun Yi | 8ca234e | 2019-03-04 10:14:45 -0800 | [diff] [blame^] | 9 | #include <boost/endian/arithmetic.hpp> |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 10 | #include <memory> |
| 11 | #include <string> |
| 12 | #include <vector> |
| 13 | |
Kun Yi | 8ca234e | 2019-03-04 10:14:45 -0800 | [diff] [blame^] | 14 | #include "binaryblob.pb.h" |
| 15 | |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 16 | using ::testing::_; |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 17 | using ::testing::AtLeast; |
| 18 | using ::testing::ElementsAreArray; |
| 19 | using ::testing::IsEmpty; |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 20 | using ::testing::Return; |
| 21 | using ::testing::StrEq; |
| 22 | using ::testing::StrNe; |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 23 | using ::testing::UnorderedElementsAreArray; |
| 24 | |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 25 | using namespace std::string_literals; |
| 26 | using namespace binstore; |
| 27 | |
Kun Yi | 91beea6 | 2018-11-26 15:23:14 -0800 | [diff] [blame] | 28 | namespace blobs |
| 29 | { |
| 30 | |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 31 | class BinaryStoreBlobHandlerBasicTest : public BinaryStoreBlobHandlerTest |
| 32 | { |
| 33 | protected: |
| 34 | static inline std::string basicTestBaseId = "/test/"s; |
| 35 | static inline std::string basicTestBlobId = "/test/blob0"s; |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 36 | |
| 37 | static const std::vector<std::string> basicTestBaseIdList; |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 38 | static inline std::string basicTestInvalidBlobId = "/invalid/blob0"s; |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 39 | |
| 40 | void addAllBaseIds() |
| 41 | { |
| 42 | for (size_t i = 0; i < basicTestBaseIdList.size(); ++i) |
| 43 | { |
| 44 | auto store = defaultMockStore(basicTestBaseIdList[i]); |
| 45 | |
| 46 | EXPECT_CALL(*store, getBaseBlobId()).Times(AtLeast(1)); |
| 47 | |
| 48 | handler.addNewBinaryStore(std::move(store)); |
| 49 | } |
| 50 | } |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 51 | }; |
| 52 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 53 | const std::vector<std::string> |
| 54 | BinaryStoreBlobHandlerBasicTest::basicTestBaseIdList = { |
| 55 | BinaryStoreBlobHandlerBasicTest::basicTestBaseId, "/another/"s, |
| 56 | "/null\0/"s}; |
| 57 | |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 58 | TEST_F(BinaryStoreBlobHandlerBasicTest, CanHandleBlobZeroStoreFail) |
| 59 | { |
| 60 | // Cannot handle since there is no store. Shouldn't crash. |
| 61 | EXPECT_FALSE(handler.canHandleBlob(basicTestInvalidBlobId)); |
| 62 | } |
| 63 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 64 | TEST_F(BinaryStoreBlobHandlerBasicTest, CanHandleBlobChecksName) |
Kun Yi | 91beea6 | 2018-11-26 15:23:14 -0800 | [diff] [blame] | 65 | { |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 66 | auto store = defaultMockStore(basicTestBaseId); |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 67 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 68 | EXPECT_CALL(*store, getBaseBlobId()).Times(AtLeast(1)); |
| 69 | |
| 70 | handler.addNewBinaryStore(std::move(store)); |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 71 | |
Kun Yi | 91beea6 | 2018-11-26 15:23:14 -0800 | [diff] [blame] | 72 | // Verify canHandleBlob checks and returns false on an invalid name. |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 73 | EXPECT_FALSE(handler.canHandleBlob(basicTestInvalidBlobId)); |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 74 | // Verify canHandleBlob return true for a blob id that it can handle |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 75 | EXPECT_TRUE(handler.canHandleBlob(basicTestBlobId)); |
| 76 | } |
| 77 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 78 | TEST_F(BinaryStoreBlobHandlerBasicTest, GetBlobIdEqualsConcatenationsOfBaseIds) |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 79 | { |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 80 | addAllBaseIds(); |
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 | // When there is no other blob id, ids are just base ids (might be |
| 83 | // re-ordered). |
| 84 | EXPECT_THAT(handler.getBlobIds(), |
| 85 | UnorderedElementsAreArray(basicTestBaseIdList)); |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 88 | TEST_F(BinaryStoreBlobHandlerBasicTest, CanHandleBlobInvalidNames) |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 89 | { |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 90 | addAllBaseIds(); |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 91 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 92 | const std::vector<std::string> invalidNames = { |
| 93 | basicTestInvalidBlobId, |
| 94 | "/"s, |
| 95 | "//"s, |
| 96 | "/test"s, // Cannot handle the base path |
| 97 | "/test/"s, |
| 98 | "/test/this/blob"s, // Cannot handle nested path |
| 99 | }; |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 100 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 101 | // Unary helper for algorithm |
| 102 | auto canHandle = [this](const std::string& blobId) { |
| 103 | return handler.canHandleBlob(blobId); |
| 104 | }; |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 105 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 106 | EXPECT_TRUE( |
| 107 | std::none_of(invalidNames.cbegin(), invalidNames.cend(), canHandle)); |
| 108 | } |
| 109 | |
| 110 | TEST_F(BinaryStoreBlobHandlerBasicTest, CanHandleBlobValidNames) |
| 111 | { |
| 112 | addAllBaseIds(); |
| 113 | |
| 114 | const std::vector<std::string> validNames = { |
| 115 | basicTestBlobId, "/test/test"s, "/test/xyz.abc"s, |
| 116 | "/another/blob"s, "/null\0/\0\0zer\0\0"s, |
| 117 | }; |
| 118 | |
| 119 | // Unary helper for algorithm |
| 120 | auto canHandle = [this](const std::string& blobId) { |
| 121 | return handler.canHandleBlob(blobId); |
| 122 | }; |
| 123 | |
| 124 | // Verify canHandleBlob can handle a valid blob under the path. |
| 125 | EXPECT_TRUE(std::all_of(validNames.cbegin(), validNames.cend(), canHandle)); |
Kun Yi | 91beea6 | 2018-11-26 15:23:14 -0800 | [diff] [blame] | 126 | } |
| 127 | |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 128 | TEST_F(BinaryStoreBlobHandlerBasicTest, DeleteReturnsWhatStoreReturns) |
| 129 | { |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 130 | auto store = defaultMockStore(basicTestBaseId); |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 131 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 132 | EXPECT_CALL(*store, getBaseBlobId()).Times(AtLeast(1)); |
| 133 | EXPECT_CALL(*store, deleteBlob(StrEq(basicTestBlobId))) |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 134 | .WillOnce(Return(false)) |
| 135 | .WillOnce(Return(true)); |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 136 | handler.addNewBinaryStore(std::move(store)); |
| 137 | |
| 138 | // Unary helper for algorithm |
| 139 | auto canHandle = [this](const std::string& blobId) { |
| 140 | return handler.canHandleBlob(blobId); |
| 141 | }; |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 142 | |
| 143 | // Verify canHandleBlob return true for a blob id that it can handle |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 144 | EXPECT_FALSE(canHandle(basicTestInvalidBlobId)); |
| 145 | EXPECT_TRUE(canHandle(basicTestBlobId)); |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 146 | EXPECT_FALSE(handler.deleteBlob(basicTestInvalidBlobId)); |
| 147 | EXPECT_FALSE(handler.deleteBlob(basicTestBlobId)); |
| 148 | EXPECT_TRUE(handler.deleteBlob(basicTestBlobId)); |
| 149 | } |
| 150 | |
Kun Yi | 8ca234e | 2019-03-04 10:14:45 -0800 | [diff] [blame^] | 151 | TEST_F(BinaryStoreBlobHandlerBasicTest, StaleDataIsClearedDuringCreation) |
| 152 | { |
| 153 | using namespace google::protobuf; |
| 154 | |
| 155 | auto basicTestStaleBlobStr = |
| 156 | R"(blob_base_id: "/stale/" |
| 157 | blobs { |
| 158 | blob_id: "/stale/blob" |
| 159 | })"; |
| 160 | |
| 161 | // Create sysfile containing a valid but stale blob |
| 162 | const std::string staleBaseId = "/stale/"s; |
| 163 | const std::string staleBlobId = "/stale/blob"s; |
| 164 | binaryblobproto::BinaryBlobBase staleBlob; |
| 165 | EXPECT_TRUE(TextFormat::ParseFromString(basicTestStaleBlobStr, &staleBlob)); |
| 166 | |
| 167 | // Serialize to string stored in the fakeSysFile |
| 168 | auto staleBlobData = staleBlob.SerializeAsString(); |
| 169 | boost::endian::little_uint64_t sizeLE = staleBlobData.size(); |
| 170 | std::string commitData(sizeLE.data(), sizeof(sizeLE)); |
| 171 | commitData += staleBlobData; |
| 172 | |
| 173 | std::vector<std::string> expectedIdList = {basicTestBaseId}; |
| 174 | |
| 175 | handler.addNewBinaryStore(BinaryStore::createFromConfig( |
| 176 | basicTestBaseId, std::make_unique<FakeSysFile>(commitData), 0)); |
| 177 | EXPECT_FALSE(handler.canHandleBlob(staleBlobId)); |
| 178 | EXPECT_EQ(handler.getBlobIds(), expectedIdList); |
| 179 | } |
| 180 | |
Kun Yi | 91beea6 | 2018-11-26 15:23:14 -0800 | [diff] [blame] | 181 | } // namespace blobs |