OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "storage/browser/blob/blob_async_builder_host.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/memory/shared_memory.h" |
| 10 #include "storage/common/blob_storage/blob_storage_constants.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 |
| 13 namespace storage { |
| 14 namespace { |
| 15 const std::string kBlobUUID = "blobUUIDYAY"; |
| 16 const std::string kFakeBlobUUID = "fakeBlob"; |
| 17 const std::string kBlobType = "blobtypeYAY"; |
| 18 |
| 19 const size_t kTestBlobStorageIPCThresholdBytes = 5; |
| 20 const size_t kTestBlobStorageMaxSharedMemoryBytes = 20; |
| 21 const uint64_t kTestBlobStorageMaxFileSizeBytes = 100; |
| 22 |
| 23 void PopulateBytes(char* bytes, size_t length) { |
| 24 for (size_t i = 0; i < length; i++) { |
| 25 bytes[i] = static_cast<char>(i); |
| 26 } |
| 27 } |
| 28 |
| 29 void AddMemoryItem(size_t length, std::vector<DataElement>* out) { |
| 30 DataElement bytes; |
| 31 bytes.SetToBytesDescription(length); |
| 32 out->push_back(bytes); |
| 33 } |
| 34 |
| 35 void AddShortcutMemoryItem(size_t length, std::vector<DataElement>* out) { |
| 36 DataElement bytes; |
| 37 bytes.SetToAllocatedBytes(length); |
| 38 PopulateBytes(bytes.mutable_bytes(), length); |
| 39 out->push_back(bytes); |
| 40 } |
| 41 |
| 42 void AddShortcutMemoryItem(size_t length, BlobDataBuilder* out) { |
| 43 DataElement bytes; |
| 44 bytes.SetToAllocatedBytes(length); |
| 45 PopulateBytes(bytes.mutable_bytes(), length); |
| 46 out->AppendData(bytes.bytes(), length); |
| 47 } |
| 48 |
| 49 void AddBlobItem(std::vector<DataElement>* out) { |
| 50 DataElement blob; |
| 51 blob.SetToBlob(kFakeBlobUUID); |
| 52 out->push_back(blob); |
| 53 } |
| 54 |
| 55 class BlobAsyncBuilderHostTest : public testing::Test { |
| 56 protected: |
| 57 BlobAsyncBuilderHostTest() |
| 58 : matching_builder_(nullptr), |
| 59 done_called_(false), |
| 60 cancel_called_(false), |
| 61 cancel_code_(IPCBlobCreationCancelCode::UNKNOWN), |
| 62 request_called_(false) {} |
| 63 ~BlobAsyncBuilderHostTest() override {} |
| 64 |
| 65 void SetUp() override { |
| 66 matching_builder_ = nullptr; |
| 67 done_called_ = false; |
| 68 cancel_called_ = false; |
| 69 cancel_code_ = IPCBlobCreationCancelCode::UNKNOWN; |
| 70 request_called_ = false; |
| 71 requests_.clear(); |
| 72 memory_handles_.clear(); |
| 73 file_handles_.clear(); |
| 74 host_.SetMemoryConstantsForTesting(kTestBlobStorageIPCThresholdBytes, |
| 75 kTestBlobStorageMaxSharedMemoryBytes, |
| 76 kTestBlobStorageMaxFileSizeBytes); |
| 77 } |
| 78 |
| 79 void SetMatchingBuilder(BlobDataBuilder* builder) { |
| 80 matching_builder_ = builder; |
| 81 } |
| 82 |
| 83 void CancelCallback(IPCBlobCreationCancelCode code) { |
| 84 cancel_called_ = true; |
| 85 cancel_code_ = code; |
| 86 } |
| 87 |
| 88 void DoneCallback(const BlobDataBuilder& builder) { |
| 89 // This does a deep comparison, including internal data items. |
| 90 if (matching_builder_) |
| 91 EXPECT_EQ(*matching_builder_, builder); |
| 92 done_called_ = true; |
| 93 } |
| 94 |
| 95 void RequestMemoryCallback( |
| 96 const std::vector<storage::BlobItemBytesRequest>& requests, |
| 97 const std::vector<base::SharedMemoryHandle>& shared_memory_handles, |
| 98 const std::vector<uint64_t>& file_sizes) { |
| 99 this->requests_ = requests; |
| 100 memory_handles_ = shared_memory_handles; |
| 101 file_handles_ = file_sizes; |
| 102 request_called_ = true; |
| 103 } |
| 104 |
| 105 bool BuildBlobAsync(const std::vector<DataElement>& descriptions, |
| 106 size_t memory_available) { |
| 107 done_called_ = false; |
| 108 cancel_called_ = false; |
| 109 request_called_ = false; |
| 110 return host_.StartBuildingBlob( |
| 111 kBlobUUID, kBlobType, descriptions, memory_available, |
| 112 base::Bind(&BlobAsyncBuilderHostTest::RequestMemoryCallback, |
| 113 base::Unretained(this)), |
| 114 base::Bind(&BlobAsyncBuilderHostTest::DoneCallback, |
| 115 base::Unretained(this)), |
| 116 base::Bind(&BlobAsyncBuilderHostTest::CancelCallback, |
| 117 base::Unretained(this))); |
| 118 } |
| 119 |
| 120 BlobDataBuilder* matching_builder_; |
| 121 BlobAsyncBuilderHost host_; |
| 122 bool done_called_; |
| 123 bool cancel_called_; |
| 124 IPCBlobCreationCancelCode cancel_code_; |
| 125 |
| 126 bool request_called_; |
| 127 std::vector<storage::BlobItemBytesRequest> requests_; |
| 128 std::vector<base::SharedMemoryHandle> memory_handles_; |
| 129 std::vector<uint64_t> file_handles_; |
| 130 }; |
| 131 |
| 132 TEST_F(BlobAsyncBuilderHostTest, TestShortcut) { |
| 133 std::vector<DataElement> descriptions; |
| 134 |
| 135 AddShortcutMemoryItem(10, &descriptions); |
| 136 AddBlobItem(&descriptions); |
| 137 AddShortcutMemoryItem(5000, &descriptions); |
| 138 |
| 139 BlobDataBuilder expected(kBlobUUID); |
| 140 expected.set_content_type(kBlobType); |
| 141 AddShortcutMemoryItem(10, &expected); |
| 142 expected.AppendBlob(kFakeBlobUUID); |
| 143 AddShortcutMemoryItem(5000, &expected); |
| 144 SetMatchingBuilder(&expected); |
| 145 |
| 146 EXPECT_TRUE(BuildBlobAsync(descriptions, 5010)); |
| 147 |
| 148 EXPECT_TRUE(done_called_); |
| 149 EXPECT_FALSE(cancel_called_); |
| 150 EXPECT_FALSE(request_called_); |
| 151 EXPECT_EQ(0u, host_.blob_building_count()); |
| 152 }; |
| 153 |
| 154 TEST_F(BlobAsyncBuilderHostTest, TestSingleSharedMemRequest) { |
| 155 std::vector<DataElement> descriptions; |
| 156 const size_t kSize = kTestBlobStorageIPCThresholdBytes + 1; |
| 157 AddMemoryItem(kSize, &descriptions); |
| 158 |
| 159 EXPECT_TRUE( |
| 160 BuildBlobAsync(descriptions, kTestBlobStorageIPCThresholdBytes + 1)); |
| 161 |
| 162 EXPECT_FALSE(done_called_); |
| 163 EXPECT_FALSE(cancel_called_); |
| 164 EXPECT_TRUE(request_called_); |
| 165 EXPECT_EQ(1u, host_.blob_building_count()); |
| 166 ASSERT_EQ(1u, requests_.size()); |
| 167 request_called_ = false; |
| 168 |
| 169 EXPECT_EQ( |
| 170 BlobItemBytesRequest::CreateSharedMemoryRequest(0, 0, 0, kSize, 0, 0), |
| 171 requests_.at(0)); |
| 172 }; |
| 173 |
| 174 TEST_F(BlobAsyncBuilderHostTest, TestMultipleSharedMemRequests) { |
| 175 std::vector<DataElement> descriptions; |
| 176 const size_t kSize = kTestBlobStorageMaxSharedMemoryBytes + 1; |
| 177 const char kFirstBlockByte = 7; |
| 178 const char kSecondBlockByte = 19; |
| 179 AddMemoryItem(kSize, &descriptions); |
| 180 |
| 181 BlobDataBuilder expected(kBlobUUID); |
| 182 expected.set_content_type(kBlobType); |
| 183 char data[kSize]; |
| 184 memset(data, kFirstBlockByte, kTestBlobStorageMaxSharedMemoryBytes); |
| 185 expected.AppendData(data, kTestBlobStorageMaxSharedMemoryBytes); |
| 186 expected.AppendData(&kSecondBlockByte, 1); |
| 187 SetMatchingBuilder(&expected); |
| 188 |
| 189 EXPECT_TRUE( |
| 190 BuildBlobAsync(descriptions, kTestBlobStorageMaxSharedMemoryBytes + 1)); |
| 191 |
| 192 EXPECT_FALSE(done_called_); |
| 193 EXPECT_FALSE(cancel_called_); |
| 194 EXPECT_TRUE(request_called_); |
| 195 EXPECT_EQ(1u, host_.blob_building_count()); |
| 196 ASSERT_EQ(1u, requests_.size()); |
| 197 request_called_ = false; |
| 198 |
| 199 // We need to grab a duplicate handle so we can have two blocks open at the |
| 200 // same time. |
| 201 base::SharedMemoryHandle handle = |
| 202 base::SharedMemory::DuplicateHandle(memory_handles_.at(0)); |
| 203 EXPECT_TRUE(base::SharedMemory::IsHandleValid(handle)); |
| 204 base::SharedMemory shared_memory(handle, false); |
| 205 EXPECT_TRUE(shared_memory.Map(kTestBlobStorageMaxSharedMemoryBytes)); |
| 206 |
| 207 EXPECT_EQ(BlobItemBytesRequest::CreateSharedMemoryRequest( |
| 208 0, 0, 0, kTestBlobStorageMaxSharedMemoryBytes, 0, 0), |
| 209 requests_.at(0)); |
| 210 |
| 211 memset(shared_memory.memory(), kFirstBlockByte, |
| 212 kTestBlobStorageMaxSharedMemoryBytes); |
| 213 |
| 214 BlobItemBytesResponse response(0); |
| 215 std::vector<BlobItemBytesResponse> responses = {response}; |
| 216 EXPECT_TRUE(host_.OnMemoryResponses(kBlobUUID, responses)); |
| 217 |
| 218 EXPECT_FALSE(done_called_); |
| 219 EXPECT_FALSE(cancel_called_); |
| 220 EXPECT_TRUE(request_called_); |
| 221 EXPECT_EQ(1u, host_.blob_building_count()); |
| 222 ASSERT_EQ(1u, requests_.size()); |
| 223 request_called_ = false; |
| 224 |
| 225 EXPECT_EQ(BlobItemBytesRequest::CreateSharedMemoryRequest( |
| 226 1, 0, kTestBlobStorageMaxSharedMemoryBytes, 1, 0, 0), |
| 227 requests_.at(0)); |
| 228 |
| 229 memset(shared_memory.memory(), kSecondBlockByte, 1); |
| 230 |
| 231 response.request_number = 1; |
| 232 responses[0] = response; |
| 233 EXPECT_TRUE(host_.OnMemoryResponses(kBlobUUID, responses)); |
| 234 EXPECT_TRUE(done_called_); |
| 235 EXPECT_FALSE(cancel_called_); |
| 236 EXPECT_FALSE(request_called_); |
| 237 EXPECT_EQ(0u, host_.blob_building_count()); |
| 238 }; |
| 239 |
| 240 TEST_F(BlobAsyncBuilderHostTest, TestBasicIPCAndStopBuilding) { |
| 241 std::vector<DataElement> descriptions; |
| 242 |
| 243 AddMemoryItem(2, &descriptions); |
| 244 AddBlobItem(&descriptions); |
| 245 AddMemoryItem(2, &descriptions); |
| 246 |
| 247 BlobDataBuilder expected(kBlobUUID); |
| 248 expected.set_content_type(kBlobType); |
| 249 AddShortcutMemoryItem(2, &expected); |
| 250 expected.AppendBlob(kFakeBlobUUID); |
| 251 AddShortcutMemoryItem(2, &expected); |
| 252 SetMatchingBuilder(&expected); |
| 253 |
| 254 EXPECT_TRUE(BuildBlobAsync(descriptions, 5010)); |
| 255 host_.StopBuildingBlob(kBlobUUID); |
| 256 EXPECT_TRUE(BuildBlobAsync(descriptions, 5010)); |
| 257 |
| 258 EXPECT_FALSE(done_called_); |
| 259 EXPECT_FALSE(cancel_called_); |
| 260 EXPECT_TRUE(request_called_); |
| 261 EXPECT_EQ(1u, host_.blob_building_count()); |
| 262 request_called_ = false; |
| 263 |
| 264 BlobItemBytesResponse response1(0); |
| 265 PopulateBytes(response1.allocate_mutable_data(2), 2); |
| 266 BlobItemBytesResponse response2(1); |
| 267 PopulateBytes(response2.allocate_mutable_data(2), 2); |
| 268 std::vector<BlobItemBytesResponse> responses = {response1, response2}; |
| 269 EXPECT_TRUE(host_.OnMemoryResponses(kBlobUUID, responses)); |
| 270 EXPECT_TRUE(done_called_); |
| 271 EXPECT_FALSE(cancel_called_); |
| 272 EXPECT_FALSE(request_called_); |
| 273 EXPECT_EQ(0u, host_.blob_building_count()); |
| 274 }; |
| 275 |
| 276 TEST_F(BlobAsyncBuilderHostTest, TestBadIPCs) { |
| 277 std::vector<DataElement> descriptions; |
| 278 |
| 279 // Test reusing same blob uuid. |
| 280 SetMatchingBuilder(nullptr); |
| 281 AddMemoryItem(10, &descriptions); |
| 282 AddBlobItem(&descriptions); |
| 283 AddMemoryItem(5000, &descriptions); |
| 284 EXPECT_TRUE(BuildBlobAsync(descriptions, 5010)); |
| 285 EXPECT_FALSE(BuildBlobAsync(descriptions, 5010)); |
| 286 EXPECT_FALSE(done_called_); |
| 287 EXPECT_FALSE(cancel_called_); |
| 288 EXPECT_FALSE(request_called_); |
| 289 host_.StopBuildingBlob(kBlobUUID); |
| 290 |
| 291 // Test we're _not_ an error if we get a bad uuid for responses. |
| 292 BlobItemBytesResponse response(0); |
| 293 std::vector<BlobItemBytesResponse> responses = {response}; |
| 294 EXPECT_TRUE(host_.OnMemoryResponses(kBlobUUID, responses)); |
| 295 |
| 296 // Test empty responses. |
| 297 responses.clear(); |
| 298 EXPECT_FALSE(host_.OnMemoryResponses(kBlobUUID, responses)); |
| 299 |
| 300 // Test response problems below here. |
| 301 descriptions.clear(); |
| 302 AddMemoryItem(2, &descriptions); |
| 303 AddBlobItem(&descriptions); |
| 304 AddMemoryItem(2, &descriptions); |
| 305 EXPECT_TRUE(BuildBlobAsync(descriptions, 5010)); |
| 306 |
| 307 // Invalid request number. |
| 308 BlobItemBytesResponse response1(3); |
| 309 PopulateBytes(response1.allocate_mutable_data(2), 2); |
| 310 responses = {response1}; |
| 311 EXPECT_FALSE(host_.OnMemoryResponses(kBlobUUID, responses)); |
| 312 |
| 313 // Duplicate request number responses. |
| 314 EXPECT_TRUE(BuildBlobAsync(descriptions, 5010)); |
| 315 response1.request_number = 0; |
| 316 BlobItemBytesResponse response2(0); |
| 317 PopulateBytes(response2.allocate_mutable_data(2), 2); |
| 318 responses = {response1, response2}; |
| 319 EXPECT_FALSE(host_.OnMemoryResponses(kBlobUUID, responses)); |
| 320 }; |
| 321 |
| 322 } // namespace |
| 323 } // namespace storage |
OLD | NEW |