OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "storage/browser/blob/blob_storage_context.h" | 5 #include "storage/browser/blob/blob_storage_context.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <limits> | 9 #include <limits> |
10 #include <memory> | 10 #include <memory> |
11 #include <string> | 11 #include <string> |
12 | 12 |
13 #include "base/files/file.h" | 13 #include "base/files/file.h" |
14 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
16 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
17 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
18 #include "base/time/time.h" | 18 #include "base/time/time.h" |
19 #include "content/browser/blob_storage/blob_dispatcher_host.h" | 19 #include "content/browser/blob_storage/blob_dispatcher_host.h" |
20 #include "content/browser/blob_storage/chrome_blob_storage_context.h" | 20 #include "content/browser/blob_storage/chrome_blob_storage_context.h" |
21 #include "content/public/test/test_browser_context.h" | 21 #include "content/public/test/test_browser_context.h" |
22 #include "net/base/io_buffer.h" | 22 #include "net/base/io_buffer.h" |
23 #include "net/base/test_completion_callback.h" | 23 #include "net/base/test_completion_callback.h" |
24 #include "net/disk_cache/disk_cache.h" | 24 #include "net/disk_cache/disk_cache.h" |
25 #include "storage/browser/blob/blob_async_builder_host.h" | 25 #include "storage/browser/blob/blob_async_builder_host.h" |
26 #include "storage/browser/blob/blob_data_builder.h" | 26 #include "storage/browser/blob/blob_data_builder.h" |
27 #include "storage/browser/blob/blob_data_handle.h" | 27 #include "storage/browser/blob/blob_data_handle.h" |
28 #include "storage/browser/blob/blob_data_item.h" | 28 #include "storage/browser/blob/blob_data_item.h" |
29 #include "storage/browser/blob/blob_data_snapshot.h" | 29 #include "storage/browser/blob/blob_data_snapshot.h" |
30 #include "storage/browser/blob/blob_transport_result.h" | |
31 #include "storage/common/blob_storage/blob_item_bytes_request.h" | 30 #include "storage/common/blob_storage/blob_item_bytes_request.h" |
32 #include "storage/common/blob_storage/blob_item_bytes_response.h" | 31 #include "storage/common/blob_storage/blob_item_bytes_response.h" |
33 #include "testing/gtest/include/gtest/gtest.h" | 32 #include "testing/gtest/include/gtest/gtest.h" |
34 | 33 |
35 using RequestMemoryCallback = | 34 using RequestMemoryCallback = |
36 storage::BlobAsyncBuilderHost::RequestMemoryCallback; | 35 storage::BlobAsyncBuilderHost::RequestMemoryCallback; |
37 | 36 |
38 namespace storage { | 37 namespace storage { |
39 namespace { | 38 namespace { |
40 | 39 |
41 const char kContentType[] = "text/plain"; | 40 // const char kContentType[] = "text/plain"; |
42 const char kContentDisposition[] = "content_disposition"; | 41 // const char kContentDisposition[] = "content_disposition"; |
43 const int kTestDiskCacheStreamIndex = 0; | 42 const int kTestDiskCacheStreamIndex = 0; |
44 | 43 |
45 // Our disk cache tests don't need a real data handle since the tests themselves | 44 // Our disk cache tests don't need a real data handle since the tests themselves |
46 // scope the disk cache and entries. | 45 // scope the disk cache and entries. |
47 class EmptyDataHandle : public storage::BlobDataBuilder::DataHandle { | 46 class EmptyDataHandle : public storage::BlobDataBuilder::DataHandle { |
48 private: | 47 private: |
49 ~EmptyDataHandle() override {} | 48 ~EmptyDataHandle() override {} |
50 }; | 49 }; |
51 | 50 |
52 std::unique_ptr<disk_cache::Backend> CreateInMemoryDiskCache() { | 51 std::unique_ptr<disk_cache::Backend> CreateInMemoryDiskCache() { |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 base::RunLoop().RunUntilIdle(); | 113 base::RunLoop().RunUntilIdle(); |
115 | 114 |
116 EXPECT_TRUE(context_.registry().HasEntry(kId)); | 115 EXPECT_TRUE(context_.registry().HasEntry(kId)); |
117 context_.DecrementBlobRefCount(kId); | 116 context_.DecrementBlobRefCount(kId); |
118 EXPECT_FALSE(context_.registry().HasEntry(kId)); | 117 EXPECT_FALSE(context_.registry().HasEntry(kId)); |
119 | 118 |
120 // Make sure it goes away in the end. | 119 // Make sure it goes away in the end. |
121 blob_data_handle = context_.GetBlobDataFromUUID(kId); | 120 blob_data_handle = context_.GetBlobDataFromUUID(kId); |
122 EXPECT_FALSE(blob_data_handle); | 121 EXPECT_FALSE(blob_data_handle); |
123 } | 122 } |
124 | 123 // |
125 TEST_F(BlobStorageContextTest, OnCancelBuildingBlob) { | 124 // TEST_F(BlobStorageContextTest, OnCancelBuildingBlob) { |
126 base::MessageLoop fake_io_message_loop; | 125 // base::MessageLoop fake_io_message_loop; |
127 | 126 // |
128 // Build up a basic blob. | 127 // // Build up a basic blob. |
129 const std::string kId("id"); | 128 // const std::string kId("id"); |
130 context_.CreatePendingBlob(kId, std::string(kContentType), | 129 // context_.CreatePendingBlob(kId, std::string(kContentType), |
131 std::string(kContentDisposition)); | 130 // std::string(kContentDisposition)); |
132 EXPECT_TRUE(context_.IsBeingBuilt(kId)); | 131 // EXPECT_TRUE(context_.IsBeingBuilt(kId)); |
133 context_.CancelPendingBlob(kId, IPCBlobCreationCancelCode::OUT_OF_MEMORY); | 132 // context_.CancelPendingBlob(kId, IPCBlobCreationCancelCode::OUT_OF_MEMORY); |
134 EXPECT_TRUE(context_.registry().HasEntry(kId)); | 133 // EXPECT_TRUE(context_.registry().HasEntry(kId)); |
135 EXPECT_FALSE(context_.IsBeingBuilt(kId)); | 134 // EXPECT_FALSE(context_.IsBeingBuilt(kId)); |
136 EXPECT_TRUE(context_.IsBroken(kId)); | 135 // EXPECT_TRUE(context_.IsBroken(kId)); |
137 } | 136 //} |
138 | 137 |
139 TEST_F(BlobStorageContextTest, BlobDataHandle) { | 138 TEST_F(BlobStorageContextTest, BlobDataHandle) { |
140 base::MessageLoop fake_io_message_loop; | 139 base::MessageLoop fake_io_message_loop; |
141 | 140 |
142 // Build up a basic blob. | 141 // Build up a basic blob. |
143 const std::string kId("id"); | 142 const std::string kId("id"); |
144 std::unique_ptr<BlobDataHandle> blob_data_handle = SetupBasicBlob(kId); | 143 std::unique_ptr<BlobDataHandle> blob_data_handle = SetupBasicBlob(kId); |
145 EXPECT_TRUE(blob_data_handle); | 144 EXPECT_TRUE(blob_data_handle); |
146 | 145 |
147 // Get another handle | 146 // Get another handle |
(...skipping 24 matching lines...) Expand all Loading... |
172 BlobDataBuilder builder2(kId2); | 171 BlobDataBuilder builder2(kId2); |
173 builder1.AppendData("Data1Data2"); | 172 builder1.AppendData("Data1Data2"); |
174 builder2.AppendBlob(kId1); | 173 builder2.AppendBlob(kId1); |
175 builder2.AppendBlob(kId1); | 174 builder2.AppendBlob(kId1); |
176 builder2.AppendBlob(kId1); | 175 builder2.AppendBlob(kId1); |
177 builder2.AppendBlob(kId1); | 176 builder2.AppendBlob(kId1); |
178 builder2.AppendBlob(kId1); | 177 builder2.AppendBlob(kId1); |
179 builder2.AppendBlob(kId1); | 178 builder2.AppendBlob(kId1); |
180 builder2.AppendBlob(kId1); | 179 builder2.AppendBlob(kId1); |
181 | 180 |
182 EXPECT_EQ(0lu, context_.memory_usage()); | 181 EXPECT_EQ(0lu, context_.memory_controller().memory_usage()); |
183 | 182 |
184 std::unique_ptr<BlobDataHandle> blob_data_handle = | 183 std::unique_ptr<BlobDataHandle> blob_data_handle = |
185 context_.AddFinishedBlob(&builder1); | 184 context_.AddFinishedBlob(&builder1); |
186 EXPECT_EQ(10lu, context_.memory_usage()); | 185 EXPECT_EQ(10lu, context_.memory_controller().memory_usage()); |
187 std::unique_ptr<BlobDataHandle> blob_data_handle2 = | 186 std::unique_ptr<BlobDataHandle> blob_data_handle2 = |
188 context_.AddFinishedBlob(&builder2); | 187 context_.AddFinishedBlob(&builder2); |
189 EXPECT_EQ(10lu, context_.memory_usage()); | 188 EXPECT_EQ(10lu, context_.memory_controller().memory_usage()); |
190 | 189 |
191 EXPECT_EQ(2u, context_.registry().blob_count()); | 190 EXPECT_EQ(2u, context_.registry().blob_count()); |
192 | 191 |
193 blob_data_handle.reset(); | 192 blob_data_handle.reset(); |
194 base::RunLoop().RunUntilIdle(); | 193 base::RunLoop().RunUntilIdle(); |
195 | 194 |
196 EXPECT_EQ(10lu, context_.memory_usage()); | 195 EXPECT_EQ(10lu, context_.memory_controller().memory_usage()); |
197 EXPECT_EQ(1u, context_.registry().blob_count()); | 196 EXPECT_EQ(1u, context_.registry().blob_count()); |
198 blob_data_handle2.reset(); | 197 blob_data_handle2.reset(); |
199 base::RunLoop().RunUntilIdle(); | 198 base::RunLoop().RunUntilIdle(); |
200 | 199 |
201 EXPECT_EQ(0lu, context_.memory_usage()); | 200 EXPECT_EQ(0lu, context_.memory_controller().memory_usage()); |
202 EXPECT_EQ(0u, context_.registry().blob_count()); | 201 EXPECT_EQ(0u, context_.registry().blob_count()); |
203 } | 202 } |
204 | 203 |
205 TEST_F(BlobStorageContextTest, AddFinishedBlob) { | 204 TEST_F(BlobStorageContextTest, AddFinishedBlob) { |
206 const std::string kId1("id1"); | 205 const std::string kId1("id1"); |
207 const std::string kId2("id12"); | 206 const std::string kId2("id12"); |
208 const std::string kId2Prime("id2.prime"); | 207 const std::string kId2Prime("id2.prime"); |
209 const std::string kId3("id3"); | 208 const std::string kId3("id3"); |
210 const std::string kId3Prime("id3.prime"); | 209 const std::string kId3Prime("id3.prime"); |
211 | 210 |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
268 blob_data_handle.reset(); | 267 blob_data_handle.reset(); |
269 blob_data_handle2.reset(); | 268 blob_data_handle2.reset(); |
270 blob_data_handle3.reset(); | 269 blob_data_handle3.reset(); |
271 base::RunLoop().RunUntilIdle(); | 270 base::RunLoop().RunUntilIdle(); |
272 } | 271 } |
273 | 272 |
274 TEST_F(BlobStorageContextTest, AddFinishedBlob_LargeOffset) { | 273 TEST_F(BlobStorageContextTest, AddFinishedBlob_LargeOffset) { |
275 // A value which does not fit in a 4-byte data type. Used to confirm that | 274 // A value which does not fit in a 4-byte data type. Used to confirm that |
276 // large values are supported on 32-bit Chromium builds. Regression test for: | 275 // large values are supported on 32-bit Chromium builds. Regression test for: |
277 // crbug.com/458122. | 276 // crbug.com/458122. |
278 const uint64_t kLargeSize = std::numeric_limits<uint64_t>::max(); | 277 const uint64_t kLargeSize = std::numeric_limits<uint64_t>::max() - 1; |
279 | 278 |
280 const uint64_t kBlobLength = 5; | 279 const uint64_t kBlobLength = 5; |
281 const std::string kId1("id1"); | 280 const std::string kId1("id1"); |
282 const std::string kId2("id2"); | 281 const std::string kId2("id2"); |
283 base::MessageLoop fake_io_message_loop; | 282 base::MessageLoop fake_io_message_loop; |
284 | 283 |
285 BlobDataBuilder builder1(kId1); | 284 BlobDataBuilder builder1(kId1); |
286 builder1.AppendFileSystemFile(GURL(), 0, kLargeSize, base::Time::Now()); | 285 builder1.AppendFileSystemFile(GURL(), 0, kLargeSize, base::Time::Now()); |
287 | 286 |
288 BlobDataBuilder builder2(kId2); | 287 BlobDataBuilder builder2(kId2); |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
430 std::unique_ptr<BlobDataSnapshot> data = blob_data_handle->CreateSnapshot(); | 429 std::unique_ptr<BlobDataSnapshot> data = blob_data_handle->CreateSnapshot(); |
431 blob_data_handle.reset(); | 430 blob_data_handle.reset(); |
432 first_handle.reset(); | 431 first_handle.reset(); |
433 base::RunLoop().RunUntilIdle(); | 432 base::RunLoop().RunUntilIdle(); |
434 | 433 |
435 // The url registration should keep the blob alive even after | 434 // The url registration should keep the blob alive even after |
436 // explicit references are dropped. | 435 // explicit references are dropped. |
437 blob_data_handle = context_.GetBlobDataFromPublicURL(kUrl); | 436 blob_data_handle = context_.GetBlobDataFromPublicURL(kUrl); |
438 EXPECT_TRUE(blob_data_handle); | 437 EXPECT_TRUE(blob_data_handle); |
439 blob_data_handle.reset(); | 438 blob_data_handle.reset(); |
| 439 |
440 base::RunLoop().RunUntilIdle(); | 440 base::RunLoop().RunUntilIdle(); |
441 | |
442 // Finally get rid of the url registration and the blob. | 441 // Finally get rid of the url registration and the blob. |
443 context_.RevokePublicBlobURL(kUrl); | 442 context_.RevokePublicBlobURL(kUrl); |
444 blob_data_handle = context_.GetBlobDataFromPublicURL(kUrl); | 443 blob_data_handle = context_.GetBlobDataFromPublicURL(kUrl); |
445 EXPECT_FALSE(blob_data_handle.get()); | 444 EXPECT_FALSE(blob_data_handle.get()); |
446 EXPECT_FALSE(context_.registry().HasEntry(kId)); | 445 EXPECT_FALSE(context_.registry().HasEntry(kId)); |
447 } | 446 } |
448 | 447 |
449 TEST_F(BlobStorageContextTest, TestUnknownBrokenAndBuildingBlobReference) { | 448 TEST_F(BlobStorageContextTest, TestUnknownBrokenAndBuildingBlobReference) { |
450 base::MessageLoop fake_io_message_loop; | 449 base::MessageLoop fake_io_message_loop; |
451 const std::string kBrokenId("broken_id"); | 450 const std::string kBrokenId("broken_id"); |
452 const std::string kBuildingId("building_id"); | 451 const std::string kBuildingId("building_id"); |
453 const std::string kReferencingId("referencing_id"); | 452 const std::string kReferencingId("referencing_id"); |
454 const std::string kUnknownId("unknown_id"); | 453 const std::string kUnknownId("unknown_id"); |
455 | 454 |
456 // Create a broken blob and a building blob. | 455 // Create a broken blob. |
457 context_.CreatePendingBlob(kBuildingId, "", ""); | 456 std::unique_ptr<BlobDataHandle> broken_handle = |
458 context_.CreatePendingBlob(kBrokenId, "", ""); | 457 context_.BuildBrokenBlob(kBrokenId, "", "", BlobStatus::OUT_OF_MEMORY); |
459 context_.CancelPendingBlob(kBrokenId, IPCBlobCreationCancelCode::UNKNOWN); | 458 EXPECT_TRUE(context_.GetBlobStatus(kBrokenId) == BlobStatus::OUT_OF_MEMORY); |
460 EXPECT_TRUE(context_.IsBroken(kBrokenId)); | |
461 EXPECT_TRUE(context_.registry().HasEntry(kBrokenId)); | 459 EXPECT_TRUE(context_.registry().HasEntry(kBrokenId)); |
462 | 460 |
463 // Try to create a blob with a reference to an unknown blob. | 461 // Try to create a blob with a reference to an unknown blob. |
464 BlobDataBuilder builder(kReferencingId); | 462 BlobDataBuilder builder(kReferencingId); |
465 builder.AppendData("data"); | 463 builder.AppendData("data"); |
466 builder.AppendBlob(kUnknownId); | 464 builder.AppendBlob(kUnknownId); |
467 std::unique_ptr<BlobDataHandle> handle = context_.AddFinishedBlob(builder); | 465 std::unique_ptr<BlobDataHandle> handle = context_.AddFinishedBlob(builder); |
468 EXPECT_TRUE(handle->IsBroken()); | 466 EXPECT_TRUE(handle->IsBroken()); |
469 EXPECT_TRUE(context_.registry().HasEntry(kReferencingId)); | 467 EXPECT_TRUE(context_.registry().HasEntry(kReferencingId)); |
470 handle.reset(); | 468 handle.reset(); |
(...skipping 19 matching lines...) Expand all Loading... |
490 EXPECT_TRUE(handle->IsBroken()); | 488 EXPECT_TRUE(handle->IsBroken()); |
491 EXPECT_TRUE(context_.registry().HasEntry(kReferencingId)); | 489 EXPECT_TRUE(context_.registry().HasEntry(kReferencingId)); |
492 handle.reset(); | 490 handle.reset(); |
493 base::RunLoop().RunUntilIdle(); | 491 base::RunLoop().RunUntilIdle(); |
494 EXPECT_FALSE(context_.registry().HasEntry(kReferencingId)); | 492 EXPECT_FALSE(context_.registry().HasEntry(kReferencingId)); |
495 } | 493 } |
496 | 494 |
497 // TODO(michaeln): tests for the depcrecated url stuff | 495 // TODO(michaeln): tests for the depcrecated url stuff |
498 | 496 |
499 } // namespace content | 497 } // namespace content |
OLD | NEW |