OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "base/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "base/bind.h" | 6 #include "base/bind.h" |
7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/numerics/safe_conversions.h" | 12 #include "base/numerics/safe_conversions.h" |
13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
15 #include "content/browser/fileapi/mock_url_request_delegate.h" | 15 #include "content/browser/fileapi/mock_url_request_delegate.h" |
16 #include "content/public/test/async_file_test_helper.h" | 16 #include "content/public/test/async_file_test_helper.h" |
17 #include "content/public/test/test_file_system_context.h" | 17 #include "content/public/test/test_file_system_context.h" |
18 #include "net/base/request_priority.h" | 18 #include "net/base/request_priority.h" |
19 #include "net/http/http_byte_range.h" | 19 #include "net/http/http_byte_range.h" |
20 #include "net/http/http_request_headers.h" | 20 #include "net/http/http_request_headers.h" |
21 #include "net/http/http_response_headers.h" | 21 #include "net/http/http_response_headers.h" |
22 #include "net/url_request/url_request.h" | 22 #include "net/url_request/url_request.h" |
23 #include "net/url_request/url_request_context.h" | 23 #include "net/url_request/url_request_context.h" |
24 #include "net/url_request/url_request_job_factory_impl.h" | 24 #include "net/url_request/url_request_job_factory_impl.h" |
25 #include "storage/browser/blob/blob_data_builder.h" | 25 #include "storage/browser/blob/blob_data_builder.h" |
| 26 #include "storage/browser/blob/blob_data_handle.h" |
26 #include "storage/browser/blob/blob_data_snapshot.h" | 27 #include "storage/browser/blob/blob_data_snapshot.h" |
| 28 #include "storage/browser/blob/blob_storage_context.h" |
27 #include "storage/browser/blob/blob_url_request_job.h" | 29 #include "storage/browser/blob/blob_url_request_job.h" |
28 #include "storage/browser/fileapi/file_system_context.h" | 30 #include "storage/browser/fileapi/file_system_context.h" |
29 #include "storage/browser/fileapi/file_system_operation_context.h" | 31 #include "storage/browser/fileapi/file_system_operation_context.h" |
30 #include "storage/browser/fileapi/file_system_url.h" | 32 #include "storage/browser/fileapi/file_system_url.h" |
31 #include "testing/gtest/include/gtest/gtest.h" | 33 #include "testing/gtest/include/gtest/gtest.h" |
32 | 34 |
33 using storage::BlobDataSnapshot; | 35 using storage::BlobDataSnapshot; |
34 using storage::BlobDataBuilder; | 36 using storage::BlobDataBuilder; |
35 using storage::BlobURLRequestJob; | 37 using storage::BlobURLRequestJob; |
36 | 38 |
(...skipping 23 matching lines...) Expand all Loading... |
60 class MockProtocolHandler : | 62 class MockProtocolHandler : |
61 public net::URLRequestJobFactory::ProtocolHandler { | 63 public net::URLRequestJobFactory::ProtocolHandler { |
62 public: | 64 public: |
63 MockProtocolHandler(BlobURLRequestJobTest* test) : test_(test) {} | 65 MockProtocolHandler(BlobURLRequestJobTest* test) : test_(test) {} |
64 | 66 |
65 // net::URLRequestJobFactory::ProtocolHandler override. | 67 // net::URLRequestJobFactory::ProtocolHandler override. |
66 net::URLRequestJob* MaybeCreateJob( | 68 net::URLRequestJob* MaybeCreateJob( |
67 net::URLRequest* request, | 69 net::URLRequest* request, |
68 net::NetworkDelegate* network_delegate) const override { | 70 net::NetworkDelegate* network_delegate) const override { |
69 return new BlobURLRequestJob(request, network_delegate, | 71 return new BlobURLRequestJob(request, network_delegate, |
70 test_->blob_data_->BuildSnapshot().Pass(), | 72 test_->GetSnapshotFromBuilder(), |
71 test_->file_system_context_.get(), | 73 test_->file_system_context_.get(), |
72 base::MessageLoopProxy::current().get()); | 74 base::MessageLoopProxy::current().get()); |
73 } | 75 } |
74 | 76 |
75 private: | 77 private: |
76 BlobURLRequestJobTest* test_; | 78 BlobURLRequestJobTest* test_; |
77 }; | 79 }; |
78 | 80 |
79 BlobURLRequestJobTest() | 81 BlobURLRequestJobTest() |
80 : blob_data_(new BlobDataBuilder("uuid")), expected_status_code_(0) {} | 82 : blob_data_(new BlobDataBuilder("uuid")), expected_status_code_(0) {} |
(...skipping 15 matching lines...) Expand all Loading... |
96 arraysize(kTestFileData2) - 1)); | 98 arraysize(kTestFileData2) - 1)); |
97 base::File::Info file_info2; | 99 base::File::Info file_info2; |
98 base::GetFileInfo(temp_file2_, &file_info2); | 100 base::GetFileInfo(temp_file2_, &file_info2); |
99 temp_file_modification_time2_ = file_info2.last_modified; | 101 temp_file_modification_time2_ = file_info2.last_modified; |
100 | 102 |
101 url_request_job_factory_.SetProtocolHandler("blob", | 103 url_request_job_factory_.SetProtocolHandler("blob", |
102 new MockProtocolHandler(this)); | 104 new MockProtocolHandler(this)); |
103 url_request_context_.set_job_factory(&url_request_job_factory_); | 105 url_request_context_.set_job_factory(&url_request_job_factory_); |
104 } | 106 } |
105 | 107 |
106 void TearDown() override {} | 108 void TearDown() override { |
| 109 blob_handle_.reset(); |
| 110 // Clean up for ASAN |
| 111 base::RunLoop run_loop; |
| 112 run_loop.RunUntilIdle(); |
| 113 } |
107 | 114 |
108 void SetUpFileSystem() { | 115 void SetUpFileSystem() { |
109 // Prepare file system. | 116 // Prepare file system. |
110 file_system_context_ = CreateFileSystemContextForTesting( | 117 file_system_context_ = CreateFileSystemContextForTesting( |
111 NULL, temp_dir_.path()); | 118 NULL, temp_dir_.path()); |
112 | 119 |
113 file_system_context_->OpenFileSystem( | 120 file_system_context_->OpenFileSystem( |
114 GURL(kFileSystemURLOrigin), | 121 GURL(kFileSystemURLOrigin), |
115 kFileSystemType, | 122 kFileSystemType, |
116 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, | 123 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 blob_data_->AppendFileSystemFile(temp_file_system_file2_, 6, 7, | 215 blob_data_->AppendFileSystemFile(temp_file_system_file2_, 6, 7, |
209 temp_file_system_file_modification_time2_); | 216 temp_file_system_file_modification_time2_); |
210 *expected_result = std::string(kTestData1 + 1, 2); | 217 *expected_result = std::string(kTestData1 + 1, 2); |
211 *expected_result += std::string(kTestFileData1 + 2, 3); | 218 *expected_result += std::string(kTestFileData1 + 2, 3); |
212 *expected_result += std::string(kTestFileSystemFileData1 + 3, 4); | 219 *expected_result += std::string(kTestFileSystemFileData1 + 3, 4); |
213 *expected_result += std::string(kTestData2 + 4, 5); | 220 *expected_result += std::string(kTestData2 + 4, 5); |
214 *expected_result += std::string(kTestFileData2 + 5, 6); | 221 *expected_result += std::string(kTestFileData2 + 5, 6); |
215 *expected_result += std::string(kTestFileSystemFileData2 + 6, 7); | 222 *expected_result += std::string(kTestFileSystemFileData2 + 6, 7); |
216 } | 223 } |
217 | 224 |
| 225 scoped_ptr<BlobDataSnapshot> GetSnapshotFromBuilder() { |
| 226 if (!blob_handle_) { |
| 227 blob_handle_ = blob_context_.AddFinishedBlob(blob_data_.get()).Pass(); |
| 228 } |
| 229 return blob_handle_->CreateSnapshot().Pass(); |
| 230 } |
| 231 |
218 // This only works if all the Blob items have a definite pre-computed length. | 232 // This only works if all the Blob items have a definite pre-computed length. |
219 // Otherwise, this will fail a CHECK. | 233 // Otherwise, this will fail a CHECK. |
220 int64 GetTotalBlobLength() const { | 234 int64 GetTotalBlobLength() { |
221 int64 total = 0; | 235 int64 total = 0; |
222 scoped_ptr<BlobDataSnapshot> data = blob_data_->BuildSnapshot(); | 236 scoped_ptr<BlobDataSnapshot> data = GetSnapshotFromBuilder(); |
223 const auto& items = data->items(); | 237 const auto& items = data->items(); |
224 for (const auto& item : items) { | 238 for (const auto& item : items) { |
225 int64 length = base::checked_cast<int64>(item->length()); | 239 int64 length = base::checked_cast<int64>(item->length()); |
226 CHECK(length <= kint64max - total); | 240 CHECK(length <= kint64max - total); |
227 total += length; | 241 total += length; |
228 } | 242 } |
229 return total; | 243 return total; |
230 } | 244 } |
231 | 245 |
232 protected: | 246 protected: |
233 base::ScopedTempDir temp_dir_; | 247 base::ScopedTempDir temp_dir_; |
234 base::FilePath temp_file1_; | 248 base::FilePath temp_file1_; |
235 base::FilePath temp_file2_; | 249 base::FilePath temp_file2_; |
236 base::Time temp_file_modification_time1_; | 250 base::Time temp_file_modification_time1_; |
237 base::Time temp_file_modification_time2_; | 251 base::Time temp_file_modification_time2_; |
238 GURL file_system_root_url_; | 252 GURL file_system_root_url_; |
239 GURL temp_file_system_file1_; | 253 GURL temp_file_system_file1_; |
240 GURL temp_file_system_file2_; | 254 GURL temp_file_system_file2_; |
241 base::Time temp_file_system_file_modification_time1_; | 255 base::Time temp_file_system_file_modification_time1_; |
242 base::Time temp_file_system_file_modification_time2_; | 256 base::Time temp_file_system_file_modification_time2_; |
243 | 257 |
244 base::MessageLoopForIO message_loop_; | 258 base::MessageLoopForIO message_loop_; |
245 scoped_refptr<storage::FileSystemContext> file_system_context_; | 259 scoped_refptr<storage::FileSystemContext> file_system_context_; |
| 260 |
| 261 storage::BlobStorageContext blob_context_; |
| 262 scoped_ptr<storage::BlobDataHandle> blob_handle_; |
246 scoped_ptr<BlobDataBuilder> blob_data_; | 263 scoped_ptr<BlobDataBuilder> blob_data_; |
247 scoped_ptr<BlobDataSnapshot> blob_data_snapshot_; | 264 scoped_ptr<BlobDataSnapshot> blob_data_snapshot_; |
248 net::URLRequestJobFactoryImpl url_request_job_factory_; | 265 net::URLRequestJobFactoryImpl url_request_job_factory_; |
249 net::URLRequestContext url_request_context_; | 266 net::URLRequestContext url_request_context_; |
250 MockURLRequestDelegate url_request_delegate_; | 267 MockURLRequestDelegate url_request_delegate_; |
251 scoped_ptr<net::URLRequest> request_; | 268 scoped_ptr<net::URLRequest> request_; |
252 | 269 |
253 int expected_status_code_; | 270 int expected_status_code_; |
254 std::string expected_response_; | 271 std::string expected_response_; |
255 }; | 272 }; |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
408 EXPECT_TRUE(request_->response_headers()->GetMimeType(&content_type)); | 425 EXPECT_TRUE(request_->response_headers()->GetMimeType(&content_type)); |
409 EXPECT_EQ(kTestContentType, content_type); | 426 EXPECT_EQ(kTestContentType, content_type); |
410 void* iter = NULL; | 427 void* iter = NULL; |
411 std::string content_disposition; | 428 std::string content_disposition; |
412 EXPECT_TRUE(request_->response_headers()->EnumerateHeader( | 429 EXPECT_TRUE(request_->response_headers()->EnumerateHeader( |
413 &iter, "Content-Disposition", &content_disposition)); | 430 &iter, "Content-Disposition", &content_disposition)); |
414 EXPECT_EQ(kTestContentDisposition, content_disposition); | 431 EXPECT_EQ(kTestContentDisposition, content_disposition); |
415 } | 432 } |
416 | 433 |
417 } // namespace content | 434 } // namespace content |
OLD | NEW |