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