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