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 <stddef.h> |
| 6 #include <stdint.h> |
| 7 #include <string.h> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/logging.h" |
| 11 #include "base/memory/shared_memory.h" |
| 12 #include "base/run_loop.h" |
| 13 #include "content/public/test/test_browser_thread_bundle.h" |
| 14 #include "storage/browser/blob/blob_data_builder.h" |
| 15 #include "storage/browser/blob/blob_data_handle.h" |
| 16 #include "storage/browser/blob/blob_storage_context.h" |
| 17 #include "storage/browser/blob/blob_transport_host.h" |
| 18 #include "storage/common/blob_storage/blob_storage_constants.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 |
| 21 namespace storage { |
| 22 namespace { |
| 23 const std::string kBlobUUID = "blobUUIDYAY"; |
| 24 const std::string kContentType = "content_type"; |
| 25 const std::string kContentDisposition = "content_disposition"; |
| 26 const std::string kCompletedBlobUUID = "completedBlob"; |
| 27 const std::string kCompletedBlobData = "completedBlobData"; |
| 28 |
| 29 const size_t kTestBlobStorageIPCThresholdBytes = 5; |
| 30 const size_t kTestBlobStorageMaxSharedMemoryBytes = 20; |
| 31 |
| 32 const size_t kTestBlobStorageMaxBlobMemorySize = 400; |
| 33 const uint64_t kTestBlobStorageMaxDiskSpace = 4000; |
| 34 const uint64_t kTestBlobStorageMinFileSizeBytes = 10; |
| 35 const uint64_t kTestBlobStorageMaxFileSizeBytes = 100; |
| 36 |
| 37 void PopulateBytes(char* bytes, size_t length) { |
| 38 for (size_t i = 0; i < length; i++) { |
| 39 bytes[i] = static_cast<char>(i); |
| 40 } |
| 41 } |
| 42 |
| 43 void AddMemoryItem(size_t length, std::vector<DataElement>* out) { |
| 44 DataElement bytes; |
| 45 bytes.SetToBytesDescription(length); |
| 46 out->push_back(bytes); |
| 47 } |
| 48 |
| 49 void AddShortcutMemoryItem(size_t length, std::vector<DataElement>* out) { |
| 50 DataElement bytes; |
| 51 bytes.SetToAllocatedBytes(length); |
| 52 PopulateBytes(bytes.mutable_bytes(), length); |
| 53 out->push_back(bytes); |
| 54 } |
| 55 |
| 56 void AddShortcutMemoryItem(size_t length, BlobDataBuilder* out) { |
| 57 DataElement bytes; |
| 58 bytes.SetToAllocatedBytes(length); |
| 59 PopulateBytes(bytes.mutable_bytes(), length); |
| 60 out->AppendData(bytes.bytes(), length); |
| 61 } |
| 62 |
| 63 void AddBlobItem(std::vector<DataElement>* out) { |
| 64 DataElement blob; |
| 65 blob.SetToBlob(kCompletedBlobUUID); |
| 66 out->push_back(blob); |
| 67 } |
| 68 } // namespace |
| 69 |
| 70 class BlobTransportHostTest : public testing::Test { |
| 71 public: |
| 72 BlobTransportHostTest() |
| 73 : status_code_(BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS), |
| 74 request_called_(false) {} |
| 75 ~BlobTransportHostTest() override {} |
| 76 |
| 77 void SetUp() override { |
| 78 status_code_ = BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS; |
| 79 request_called_ = false; |
| 80 requests_.clear(); |
| 81 memory_handles_.clear(); |
| 82 storage::BlobStorageLimits limits; |
| 83 limits.max_ipc_memory_size = kTestBlobStorageIPCThresholdBytes; |
| 84 limits.max_shared_memory_size = kTestBlobStorageMaxSharedMemoryBytes; |
| 85 limits.max_blob_in_memory_space = kTestBlobStorageMaxBlobMemorySize; |
| 86 limits.max_blob_disk_space = kTestBlobStorageMaxDiskSpace; |
| 87 limits.min_page_file_size = kTestBlobStorageMinFileSizeBytes; |
| 88 limits.max_file_size = kTestBlobStorageMaxFileSizeBytes; |
| 89 context_.mutable_memory_controller()->set_limits_for_testing(limits); |
| 90 BlobDataBuilder builder(kCompletedBlobUUID); |
| 91 builder.AppendData(kCompletedBlobData); |
| 92 completed_blob_handle_ = context_.AddFinishedBlob(builder); |
| 93 EXPECT_EQ(BlobStatus::DONE, completed_blob_handle_->GetBlobStatus()); |
| 94 } |
| 95 |
| 96 void StatusCallback(BlobStatus status) { |
| 97 status_called_ = true; |
| 98 status_code_ = status; |
| 99 } |
| 100 |
| 101 void RequestMemoryCallback( |
| 102 std::vector<storage::BlobItemBytesRequest> requests, |
| 103 std::vector<base::SharedMemoryHandle> shared_memory_handles, |
| 104 std::vector<base::File> files) { |
| 105 requests_ = std::move(requests); |
| 106 memory_handles_ = std::move(shared_memory_handles); |
| 107 request_called_ = true; |
| 108 } |
| 109 |
| 110 BlobStatus BuildBlobAsync(const std::string& uuid, |
| 111 const std::vector<DataElement>& descriptions, |
| 112 std::unique_ptr<BlobDataHandle>* storage) { |
| 113 EXPECT_NE(storage, nullptr); |
| 114 request_called_ = false; |
| 115 status_called_ = false; |
| 116 *storage = host_.StartBuildingBlob( |
| 117 uuid, kContentType, kContentDisposition, descriptions, &context_, |
| 118 base::Bind(&BlobTransportHostTest::RequestMemoryCallback, |
| 119 base::Unretained(this)), |
| 120 base::Bind(&BlobTransportHostTest::StatusCallback, |
| 121 base::Unretained(this))); |
| 122 if (status_called_) |
| 123 return status_code_; |
| 124 else |
| 125 return context_.GetBlobStatus(uuid); |
| 126 } |
| 127 |
| 128 BlobStatus GetBlobStatus(const std::string& uuid) const { |
| 129 return context_.GetBlobStatus(uuid); |
| 130 } |
| 131 |
| 132 bool IsBeingBuiltInContext(const std::string& uuid) const { |
| 133 return BlobStatusIsPending(context_.GetBlobStatus(uuid)); |
| 134 } |
| 135 |
| 136 content::TestBrowserThreadBundle browser_thread_bundle_; |
| 137 BlobStorageContext context_; |
| 138 BlobTransportHost host_; |
| 139 bool status_called_; |
| 140 BlobStatus status_code_; |
| 141 |
| 142 bool request_called_; |
| 143 std::vector<storage::BlobItemBytesRequest> requests_; |
| 144 std::vector<base::SharedMemoryHandle> memory_handles_; |
| 145 std::unique_ptr<BlobDataHandle> completed_blob_handle_; |
| 146 }; |
| 147 |
| 148 // The 'shortcut' method is when the data is included in the initial IPCs and |
| 149 // the browser uses that instead of requesting the memory. |
| 150 TEST_F(BlobTransportHostTest, TestShortcut) { |
| 151 std::vector<DataElement> descriptions; |
| 152 |
| 153 AddShortcutMemoryItem(10, &descriptions); |
| 154 AddBlobItem(&descriptions); |
| 155 AddShortcutMemoryItem(300, &descriptions); |
| 156 |
| 157 BlobDataBuilder expected(kBlobUUID); |
| 158 expected.set_content_type(kContentType); |
| 159 expected.set_content_disposition(kContentDisposition); |
| 160 AddShortcutMemoryItem(10, &expected); |
| 161 expected.AppendData(kCompletedBlobData); |
| 162 AddShortcutMemoryItem(300, &expected); |
| 163 |
| 164 std::unique_ptr<BlobDataHandle> handle; |
| 165 EXPECT_EQ(BlobStatus::DONE, BuildBlobAsync(kBlobUUID, descriptions, &handle)); |
| 166 |
| 167 EXPECT_FALSE(request_called_); |
| 168 EXPECT_EQ(0u, host_.blob_building_count()); |
| 169 EXPECT_FALSE(handle->IsBeingBuilt()); |
| 170 ASSERT_FALSE(handle->IsBroken()); |
| 171 std::unique_ptr<BlobDataSnapshot> data = handle->CreateSnapshot(); |
| 172 EXPECT_EQ(expected, *data); |
| 173 data.reset(); |
| 174 handle.reset(); |
| 175 base::RunLoop().RunUntilIdle(); |
| 176 }; |
| 177 |
| 178 TEST_F(BlobTransportHostTest, TestShortcutNoRoom) { |
| 179 std::vector<DataElement> descriptions; |
| 180 |
| 181 AddShortcutMemoryItem(10, &descriptions); |
| 182 AddBlobItem(&descriptions); |
| 183 AddShortcutMemoryItem(5000, &descriptions); |
| 184 |
| 185 std::unique_ptr<BlobDataHandle> handle; |
| 186 EXPECT_EQ(BlobStatus::ERR_OUT_OF_MEMORY, |
| 187 BuildBlobAsync(kBlobUUID, descriptions, &handle)); |
| 188 |
| 189 EXPECT_FALSE(request_called_); |
| 190 EXPECT_EQ(0u, host_.blob_building_count()); |
| 191 }; |
| 192 |
| 193 TEST_F(BlobTransportHostTest, TestSingleSharedMemRequest) { |
| 194 std::vector<DataElement> descriptions; |
| 195 const size_t kSize = kTestBlobStorageIPCThresholdBytes + 1; |
| 196 AddMemoryItem(kSize, &descriptions); |
| 197 |
| 198 std::unique_ptr<BlobDataHandle> handle; |
| 199 EXPECT_EQ(BlobStatus::PENDING_TRANSPORT, |
| 200 BuildBlobAsync(kBlobUUID, descriptions, &handle)); |
| 201 EXPECT_TRUE(handle); |
| 202 EXPECT_EQ(BlobStatus::PENDING_TRANSPORT, handle->GetBlobStatus()); |
| 203 |
| 204 EXPECT_TRUE(request_called_); |
| 205 EXPECT_EQ(1u, host_.blob_building_count()); |
| 206 ASSERT_EQ(1u, requests_.size()); |
| 207 request_called_ = false; |
| 208 |
| 209 EXPECT_EQ( |
| 210 BlobItemBytesRequest::CreateSharedMemoryRequest(0, 0, 0, kSize, 0, 0), |
| 211 requests_.at(0)); |
| 212 }; |
| 213 |
| 214 TEST_F(BlobTransportHostTest, TestMultipleSharedMemRequests) { |
| 215 std::vector<DataElement> descriptions; |
| 216 const size_t kSize = kTestBlobStorageMaxSharedMemoryBytes + 1; |
| 217 const char kFirstBlockByte = 7; |
| 218 const char kSecondBlockByte = 19; |
| 219 AddMemoryItem(kSize, &descriptions); |
| 220 |
| 221 BlobDataBuilder expected(kBlobUUID); |
| 222 expected.set_content_type(kContentType); |
| 223 expected.set_content_disposition(kContentDisposition); |
| 224 char data[kSize]; |
| 225 memset(data, kFirstBlockByte, kTestBlobStorageMaxSharedMemoryBytes); |
| 226 expected.AppendData(data, kTestBlobStorageMaxSharedMemoryBytes); |
| 227 expected.AppendData(&kSecondBlockByte, 1); |
| 228 |
| 229 std::unique_ptr<BlobDataHandle> handle; |
| 230 EXPECT_EQ(BlobStatus::PENDING_TRANSPORT, |
| 231 BuildBlobAsync(kBlobUUID, descriptions, &handle)); |
| 232 |
| 233 EXPECT_TRUE(request_called_); |
| 234 EXPECT_EQ(1u, host_.blob_building_count()); |
| 235 ASSERT_EQ(1u, requests_.size()); |
| 236 request_called_ = false; |
| 237 |
| 238 // We need to grab a duplicate handle so we can have two blocks open at the |
| 239 // same time. |
| 240 base::SharedMemoryHandle shared_mem_handle = |
| 241 base::SharedMemory::DuplicateHandle(memory_handles_.at(0)); |
| 242 EXPECT_TRUE(base::SharedMemory::IsHandleValid(shared_mem_handle)); |
| 243 base::SharedMemory shared_memory(shared_mem_handle, false); |
| 244 EXPECT_TRUE(shared_memory.Map(kTestBlobStorageMaxSharedMemoryBytes)); |
| 245 |
| 246 EXPECT_EQ(BlobItemBytesRequest::CreateSharedMemoryRequest( |
| 247 0, 0, 0, kTestBlobStorageMaxSharedMemoryBytes, 0, 0), |
| 248 requests_.at(0)); |
| 249 |
| 250 memset(shared_memory.memory(), kFirstBlockByte, |
| 251 kTestBlobStorageMaxSharedMemoryBytes); |
| 252 |
| 253 BlobItemBytesResponse response(0); |
| 254 std::vector<BlobItemBytesResponse> responses = {response}; |
| 255 host_.OnMemoryResponses(kBlobUUID, responses, &context_); |
| 256 EXPECT_EQ(BlobStatus::PENDING_TRANSPORT, GetBlobStatus(kBlobUUID)); |
| 257 ASSERT_TRUE(handle); |
| 258 EXPECT_EQ(BlobStatus::PENDING_TRANSPORT, handle->GetBlobStatus()); |
| 259 |
| 260 EXPECT_TRUE(request_called_); |
| 261 EXPECT_EQ(1u, host_.blob_building_count()); |
| 262 ASSERT_EQ(1u, requests_.size()); |
| 263 request_called_ = false; |
| 264 |
| 265 EXPECT_EQ(BlobItemBytesRequest::CreateSharedMemoryRequest( |
| 266 1, 0, kTestBlobStorageMaxSharedMemoryBytes, 1, 0, 0), |
| 267 requests_.at(0)); |
| 268 |
| 269 memset(shared_memory.memory(), kSecondBlockByte, 1); |
| 270 |
| 271 response.request_number = 1; |
| 272 responses[0] = response; |
| 273 host_.OnMemoryResponses(kBlobUUID, responses, &context_); |
| 274 EXPECT_TRUE(handle); |
| 275 EXPECT_EQ(BlobStatus::DONE, handle->GetBlobStatus()); |
| 276 EXPECT_FALSE(request_called_); |
| 277 EXPECT_EQ(0u, host_.blob_building_count()); |
| 278 std::unique_ptr<BlobDataHandle> blob_handle = |
| 279 context_.GetBlobDataFromUUID(kBlobUUID); |
| 280 EXPECT_FALSE(blob_handle->IsBeingBuilt()); |
| 281 EXPECT_FALSE(blob_handle->IsBroken()); |
| 282 std::unique_ptr<BlobDataSnapshot> blob_data = blob_handle->CreateSnapshot(); |
| 283 EXPECT_EQ(expected, *blob_data); |
| 284 }; |
| 285 |
| 286 TEST_F(BlobTransportHostTest, TestBasicIPCAndStopBuilding) { |
| 287 std::vector<DataElement> descriptions; |
| 288 |
| 289 AddMemoryItem(2, &descriptions); |
| 290 AddBlobItem(&descriptions); |
| 291 AddMemoryItem(2, &descriptions); |
| 292 |
| 293 BlobDataBuilder expected(kBlobUUID); |
| 294 expected.set_content_type(kContentType); |
| 295 expected.set_content_disposition(kContentDisposition); |
| 296 AddShortcutMemoryItem(2, &expected); |
| 297 expected.AppendData(kCompletedBlobData); |
| 298 AddShortcutMemoryItem(2, &expected); |
| 299 |
| 300 std::unique_ptr<BlobDataHandle> handle1; |
| 301 EXPECT_EQ(BlobStatus::PENDING_TRANSPORT, |
| 302 BuildBlobAsync(kBlobUUID, descriptions, &handle1)); |
| 303 EXPECT_TRUE(handle1); |
| 304 host_.CancelBuildingBlob(kBlobUUID, BlobStatus::ERR_OUT_OF_MEMORY, &context_); |
| 305 |
| 306 // Check that we're broken, and then remove the blob. |
| 307 EXPECT_FALSE(handle1->IsBeingBuilt()); |
| 308 EXPECT_TRUE(handle1->IsBroken()); |
| 309 handle1.reset(); |
| 310 base::RunLoop().RunUntilIdle(); |
| 311 handle1 = context_.GetBlobDataFromUUID(kBlobUUID); |
| 312 EXPECT_FALSE(handle1.get()); |
| 313 |
| 314 // This should succeed because we've removed all references to the blob. |
| 315 std::unique_ptr<BlobDataHandle> handle2; |
| 316 EXPECT_EQ(BlobStatus::PENDING_TRANSPORT, |
| 317 BuildBlobAsync(kBlobUUID, descriptions, &handle2)); |
| 318 |
| 319 EXPECT_TRUE(request_called_); |
| 320 EXPECT_EQ(1u, host_.blob_building_count()); |
| 321 request_called_ = false; |
| 322 |
| 323 BlobItemBytesResponse response1(0); |
| 324 PopulateBytes(response1.allocate_mutable_data(2), 2); |
| 325 BlobItemBytesResponse response2(1); |
| 326 PopulateBytes(response2.allocate_mutable_data(2), 2); |
| 327 std::vector<BlobItemBytesResponse> responses = {response1, response2}; |
| 328 |
| 329 host_.OnMemoryResponses(kBlobUUID, responses, &context_); |
| 330 EXPECT_EQ(BlobStatus::DONE, handle2->GetBlobStatus()); |
| 331 EXPECT_FALSE(request_called_); |
| 332 EXPECT_EQ(0u, host_.blob_building_count()); |
| 333 EXPECT_FALSE(handle2->IsBeingBuilt()); |
| 334 EXPECT_FALSE(handle2->IsBroken()); |
| 335 std::unique_ptr<BlobDataSnapshot> blob_data = handle2->CreateSnapshot(); |
| 336 EXPECT_EQ(expected, *blob_data); |
| 337 }; |
| 338 |
| 339 TEST_F(BlobTransportHostTest, TestBreakingAllBuilding) { |
| 340 const std::string& kBlob1 = "blob1"; |
| 341 const std::string& kBlob2 = "blob2"; |
| 342 const std::string& kBlob3 = "blob3"; |
| 343 |
| 344 std::vector<DataElement> descriptions; |
| 345 AddMemoryItem(2, &descriptions); |
| 346 |
| 347 // Register blobs. |
| 348 std::unique_ptr<BlobDataHandle> handle1; |
| 349 std::unique_ptr<BlobDataHandle> handle2; |
| 350 std::unique_ptr<BlobDataHandle> handle3; |
| 351 EXPECT_EQ(BlobStatus::PENDING_TRANSPORT, |
| 352 BuildBlobAsync(kBlob1, descriptions, &handle1)); |
| 353 EXPECT_EQ(BlobStatus::PENDING_TRANSPORT, |
| 354 BuildBlobAsync(kBlob2, descriptions, &handle2)); |
| 355 EXPECT_EQ(BlobStatus::PENDING_TRANSPORT, |
| 356 BuildBlobAsync(kBlob3, descriptions, &handle3)); |
| 357 |
| 358 EXPECT_TRUE(request_called_); |
| 359 EXPECT_TRUE(handle1->IsBeingBuilt() && handle2->IsBeingBuilt() && |
| 360 handle3->IsBeingBuilt()); |
| 361 EXPECT_FALSE(handle1->IsBroken() || handle2->IsBroken() || |
| 362 handle3->IsBroken()); |
| 363 |
| 364 EXPECT_TRUE(IsBeingBuiltInContext(kBlob1) && IsBeingBuiltInContext(kBlob2) && |
| 365 IsBeingBuiltInContext(kBlob3)); |
| 366 |
| 367 // This shouldn't call the transport complete callbacks, so our handles should |
| 368 // still be false. |
| 369 host_.CancelAll(&context_); |
| 370 |
| 371 EXPECT_FALSE(handle1->IsBeingBuilt() || handle2->IsBeingBuilt() || |
| 372 handle3->IsBeingBuilt()); |
| 373 EXPECT_TRUE(handle1->IsBroken() && handle2->IsBroken() && |
| 374 handle3->IsBroken()); |
| 375 |
| 376 base::RunLoop().RunUntilIdle(); |
| 377 }; |
| 378 |
| 379 TEST_F(BlobTransportHostTest, TestBadIPCs) { |
| 380 std::vector<DataElement> descriptions; |
| 381 |
| 382 // Test reusing same blob uuid. |
| 383 AddMemoryItem(10, &descriptions); |
| 384 AddBlobItem(&descriptions); |
| 385 AddMemoryItem(300, &descriptions); |
| 386 std::unique_ptr<BlobDataHandle> handle1; |
| 387 EXPECT_EQ(BlobStatus::PENDING_TRANSPORT, |
| 388 BuildBlobAsync(kBlobUUID, descriptions, &handle1)); |
| 389 EXPECT_TRUE(host_.IsBeingBuilt(kBlobUUID)); |
| 390 EXPECT_TRUE(request_called_); |
| 391 host_.CancelBuildingBlob(kBlobUUID, BlobStatus::ERR_REFERENCED_BLOB_BROKEN, |
| 392 &context_); |
| 393 handle1.reset(); |
| 394 EXPECT_FALSE(context_.GetBlobDataFromUUID(kBlobUUID).get()); |
| 395 |
| 396 // Test empty responses. |
| 397 EXPECT_EQ(BlobStatus::PENDING_TRANSPORT, |
| 398 BuildBlobAsync(kBlobUUID, descriptions, &handle1)); |
| 399 std::vector<BlobItemBytesResponse> responses; |
| 400 host_.OnMemoryResponses(kBlobUUID, responses, &context_); |
| 401 EXPECT_EQ(BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS, |
| 402 handle1->GetBlobStatus()); |
| 403 handle1.reset(); |
| 404 |
| 405 // Test response problems below here. |
| 406 descriptions.clear(); |
| 407 AddMemoryItem(2, &descriptions); |
| 408 AddBlobItem(&descriptions); |
| 409 AddMemoryItem(2, &descriptions); |
| 410 EXPECT_EQ(BlobStatus::PENDING_TRANSPORT, |
| 411 BuildBlobAsync(kBlobUUID, descriptions, &handle1)); |
| 412 |
| 413 // Invalid request number. |
| 414 BlobItemBytesResponse response1(3); |
| 415 PopulateBytes(response1.allocate_mutable_data(2), 2); |
| 416 responses = {response1}; |
| 417 host_.OnMemoryResponses(kBlobUUID, responses, &context_); |
| 418 EXPECT_EQ(BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS, |
| 419 handle1->GetBlobStatus()); |
| 420 EXPECT_TRUE(context_.GetBlobDataFromUUID(kBlobUUID)->IsBroken()); |
| 421 handle1.reset(); |
| 422 base::RunLoop().RunUntilIdle(); |
| 423 |
| 424 // Duplicate request number responses. |
| 425 EXPECT_EQ(BlobStatus::PENDING_TRANSPORT, |
| 426 BuildBlobAsync(kBlobUUID, descriptions, &handle1)); |
| 427 response1.request_number = 0; |
| 428 BlobItemBytesResponse response2(0); |
| 429 PopulateBytes(response2.allocate_mutable_data(2), 2); |
| 430 responses = {response1, response2}; |
| 431 host_.OnMemoryResponses(kBlobUUID, responses, &context_); |
| 432 EXPECT_EQ(BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS, |
| 433 handle1->GetBlobStatus()); |
| 434 EXPECT_TRUE(context_.GetBlobDataFromUUID(kBlobUUID)->IsBroken()); |
| 435 handle1.reset(); |
| 436 base::RunLoop().RunUntilIdle(); |
| 437 }; |
| 438 |
| 439 TEST_F(BlobTransportHostTest, WaitOnReferencedBlob) { |
| 440 const std::string& kBlob1 = "blob1"; |
| 441 const std::string& kBlob2 = "blob2"; |
| 442 const std::string& kBlob3 = "blob3"; |
| 443 |
| 444 std::vector<DataElement> descriptions; |
| 445 AddMemoryItem(2, &descriptions); |
| 446 |
| 447 // Register blobs. |
| 448 std::unique_ptr<BlobDataHandle> handle1; |
| 449 |
| 450 std::unique_ptr<BlobDataHandle> handle2; |
| 451 std::unique_ptr<BlobDataHandle> handle3; |
| 452 EXPECT_EQ(BlobStatus::PENDING_TRANSPORT, |
| 453 BuildBlobAsync(kBlob1, descriptions, &handle1)); |
| 454 EXPECT_EQ(BlobStatus::PENDING_TRANSPORT, |
| 455 BuildBlobAsync(kBlob2, descriptions, &handle2)); |
| 456 EXPECT_TRUE(request_called_); |
| 457 request_called_ = false; |
| 458 |
| 459 // Finish the third one, with a reference to the first and second blob. |
| 460 DataElement element; |
| 461 element.SetToBlob(kBlob1); |
| 462 descriptions.push_back(element); |
| 463 element.SetToBlob(kBlob2); |
| 464 descriptions.push_back(element); |
| 465 |
| 466 EXPECT_EQ(BlobStatus::PENDING_TRANSPORT, |
| 467 BuildBlobAsync(kBlob3, descriptions, &handle3)); |
| 468 EXPECT_TRUE(request_called_); |
| 469 request_called_ = false; |
| 470 |
| 471 // Finish the third, but we should still be 'building' it. |
| 472 BlobItemBytesResponse response1(0); |
| 473 PopulateBytes(response1.allocate_mutable_data(2), 2); |
| 474 std::vector<BlobItemBytesResponse> responses = {response1}; |
| 475 host_.OnMemoryResponses(kBlob3, responses, &context_); |
| 476 EXPECT_EQ(BlobStatus::PENDING_INTERNALS, handle3->GetBlobStatus()); |
| 477 EXPECT_FALSE(request_called_); |
| 478 EXPECT_FALSE(host_.IsBeingBuilt(kBlob3)); |
| 479 EXPECT_TRUE(IsBeingBuiltInContext(kBlob3)); |
| 480 |
| 481 // Finish the first. |
| 482 descriptions.clear(); |
| 483 AddShortcutMemoryItem(2, &descriptions); |
| 484 host_.OnMemoryResponses(kBlob1, responses, &context_); |
| 485 EXPECT_EQ(BlobStatus::DONE, handle1->GetBlobStatus()); |
| 486 EXPECT_FALSE(request_called_); |
| 487 EXPECT_FALSE(host_.IsBeingBuilt(kBlob1)); |
| 488 EXPECT_FALSE(IsBeingBuiltInContext(kBlob1)); |
| 489 EXPECT_TRUE(context_.GetBlobDataFromUUID(kBlob1)); |
| 490 |
| 491 // Run the message loop so we propogate the construction complete callbacks. |
| 492 base::RunLoop().RunUntilIdle(); |
| 493 // Verify we're not done. |
| 494 EXPECT_TRUE(IsBeingBuiltInContext(kBlob3)); |
| 495 |
| 496 // Finish the second. |
| 497 host_.OnMemoryResponses(kBlob2, responses, &context_); |
| 498 EXPECT_EQ(BlobStatus::DONE, handle2->GetBlobStatus()); |
| 499 EXPECT_FALSE(request_called_); |
| 500 EXPECT_FALSE(host_.IsBeingBuilt(kBlob2)); |
| 501 EXPECT_FALSE(IsBeingBuiltInContext(kBlob2)); |
| 502 EXPECT_TRUE(context_.GetBlobDataFromUUID(kBlob2)); |
| 503 |
| 504 // Run the message loop so we propogate the construction complete callbacks. |
| 505 base::RunLoop().RunUntilIdle(); |
| 506 // Finally, we should be finished with third blob. |
| 507 EXPECT_FALSE(host_.IsBeingBuilt(kBlob3)); |
| 508 EXPECT_FALSE(IsBeingBuiltInContext(kBlob3)); |
| 509 EXPECT_TRUE(context_.GetBlobDataFromUUID(kBlob3)); |
| 510 }; |
| 511 |
| 512 } // namespace storage |
OLD | NEW |