| 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" |
| 26 #include "storage/browser/blob/blob_data_snapshot.h" |
| 25 #include "storage/browser/blob/blob_url_request_job.h" | 27 #include "storage/browser/blob/blob_url_request_job.h" |
| 26 #include "storage/browser/fileapi/file_system_context.h" | 28 #include "storage/browser/fileapi/file_system_context.h" |
| 27 #include "storage/browser/fileapi/file_system_operation_context.h" | 29 #include "storage/browser/fileapi/file_system_operation_context.h" |
| 28 #include "storage/browser/fileapi/file_system_url.h" | 30 #include "storage/browser/fileapi/file_system_url.h" |
| 29 #include "storage/common/blob/blob_data.h" | |
| 30 #include "testing/gtest/include/gtest/gtest.h" | 31 #include "testing/gtest/include/gtest/gtest.h" |
| 31 | 32 |
| 32 using storage::BlobData; | 33 using storage::BlobDataSnapshot; |
| 34 using storage::BlobDataBuilder; |
| 33 using storage::BlobURLRequestJob; | 35 using storage::BlobURLRequestJob; |
| 34 | 36 |
| 35 namespace content { | 37 namespace content { |
| 36 | 38 |
| 37 namespace { | 39 namespace { |
| 38 | 40 |
| 39 const int kBufferSize = 1024; | 41 const int kBufferSize = 1024; |
| 40 const char kTestData1[] = "Hello"; | 42 const char kTestData1[] = "Hello"; |
| 41 const char kTestData2[] = "Here it is data."; | 43 const char kTestData2[] = "Here it is data."; |
| 42 const char kTestFileData1[] = "0123456789"; | 44 const char kTestFileData1[] = "0123456789"; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 57 // A simple ProtocolHandler implementation to create BlobURLRequestJob. | 59 // A simple ProtocolHandler implementation to create BlobURLRequestJob. |
| 58 class MockProtocolHandler : | 60 class MockProtocolHandler : |
| 59 public net::URLRequestJobFactory::ProtocolHandler { | 61 public net::URLRequestJobFactory::ProtocolHandler { |
| 60 public: | 62 public: |
| 61 MockProtocolHandler(BlobURLRequestJobTest* test) : test_(test) {} | 63 MockProtocolHandler(BlobURLRequestJobTest* test) : test_(test) {} |
| 62 | 64 |
| 63 // net::URLRequestJobFactory::ProtocolHandler override. | 65 // net::URLRequestJobFactory::ProtocolHandler override. |
| 64 net::URLRequestJob* MaybeCreateJob( | 66 net::URLRequestJob* MaybeCreateJob( |
| 65 net::URLRequest* request, | 67 net::URLRequest* request, |
| 66 net::NetworkDelegate* network_delegate) const override { | 68 net::NetworkDelegate* network_delegate) const override { |
| 67 return new BlobURLRequestJob(request, | 69 return new BlobURLRequestJob(request, network_delegate, |
| 68 network_delegate, | 70 test_->blob_data_->BuildSnapshot().Pass(), |
| 69 test_->blob_data_.get(), | |
| 70 test_->file_system_context_.get(), | 71 test_->file_system_context_.get(), |
| 71 base::MessageLoopProxy::current().get()); | 72 base::MessageLoopProxy::current().get()); |
| 72 } | 73 } |
| 73 | 74 |
| 74 private: | 75 private: |
| 75 BlobURLRequestJobTest* test_; | 76 BlobURLRequestJobTest* test_; |
| 76 }; | 77 }; |
| 77 | 78 |
| 78 BlobURLRequestJobTest() | 79 BlobURLRequestJobTest() |
| 79 : blob_data_(new BlobData()), | 80 : blob_data_(new BlobDataBuilder("uuid")), expected_status_code_(0) {} |
| 80 expected_status_code_(0) {} | |
| 81 | 81 |
| 82 void SetUp() override { | 82 void SetUp() override { |
| 83 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 83 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 84 | 84 |
| 85 temp_file1_ = temp_dir_.path().AppendASCII("BlobFile1.dat"); | 85 temp_file1_ = temp_dir_.path().AppendASCII("BlobFile1.dat"); |
| 86 ASSERT_EQ(static_cast<int>(arraysize(kTestFileData1) - 1), | 86 ASSERT_EQ(static_cast<int>(arraysize(kTestFileData1) - 1), |
| 87 base::WriteFile(temp_file1_, kTestFileData1, | 87 base::WriteFile(temp_file1_, kTestFileData1, |
| 88 arraysize(kTestFileData1) - 1)); | 88 arraysize(kTestFileData1) - 1)); |
| 89 base::File::Info file_info1; | 89 base::File::Info file_info1; |
| 90 base::GetFileInfo(temp_file1_, &file_info1); | 90 base::GetFileInfo(temp_file1_, &file_info1); |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 *expected_result += std::string(kTestFileSystemFileData1 + 3, 4); | 212 *expected_result += std::string(kTestFileSystemFileData1 + 3, 4); |
| 213 *expected_result += std::string(kTestData2 + 4, 5); | 213 *expected_result += std::string(kTestData2 + 4, 5); |
| 214 *expected_result += std::string(kTestFileData2 + 5, 6); | 214 *expected_result += std::string(kTestFileData2 + 5, 6); |
| 215 *expected_result += std::string(kTestFileSystemFileData2 + 6, 7); | 215 *expected_result += std::string(kTestFileSystemFileData2 + 6, 7); |
| 216 } | 216 } |
| 217 | 217 |
| 218 // This only works if all the Blob items have a definite pre-computed length. | 218 // This only works if all the Blob items have a definite pre-computed length. |
| 219 // Otherwise, this will fail a CHECK. | 219 // Otherwise, this will fail a CHECK. |
| 220 int64 GetTotalBlobLength() const { | 220 int64 GetTotalBlobLength() const { |
| 221 int64 total = 0; | 221 int64 total = 0; |
| 222 const std::vector<BlobData::Item>& items = blob_data_->items(); | 222 scoped_ptr<BlobDataSnapshot> data = blob_data_->BuildSnapshot(); |
| 223 for (std::vector<BlobData::Item>::const_iterator it = items.begin(); | 223 const auto& items = data->items(); |
| 224 it != items.end(); ++it) { | 224 for (const auto& item : items) { |
| 225 int64 length = base::checked_cast<int64>(it->length()); | 225 int64 length = base::checked_cast<int64>(item->length()); |
| 226 CHECK(length <= kint64max - total); | 226 CHECK(length <= kint64max - total); |
| 227 total += length; | 227 total += length; |
| 228 } | 228 } |
| 229 return total; | 229 return total; |
| 230 } | 230 } |
| 231 | 231 |
| 232 protected: | 232 protected: |
| 233 base::ScopedTempDir temp_dir_; | 233 base::ScopedTempDir temp_dir_; |
| 234 base::FilePath temp_file1_; | 234 base::FilePath temp_file1_; |
| 235 base::FilePath temp_file2_; | 235 base::FilePath temp_file2_; |
| 236 base::Time temp_file_modification_time1_; | 236 base::Time temp_file_modification_time1_; |
| 237 base::Time temp_file_modification_time2_; | 237 base::Time temp_file_modification_time2_; |
| 238 GURL file_system_root_url_; | 238 GURL file_system_root_url_; |
| 239 GURL temp_file_system_file1_; | 239 GURL temp_file_system_file1_; |
| 240 GURL temp_file_system_file2_; | 240 GURL temp_file_system_file2_; |
| 241 base::Time temp_file_system_file_modification_time1_; | 241 base::Time temp_file_system_file_modification_time1_; |
| 242 base::Time temp_file_system_file_modification_time2_; | 242 base::Time temp_file_system_file_modification_time2_; |
| 243 | 243 |
| 244 base::MessageLoopForIO message_loop_; | 244 base::MessageLoopForIO message_loop_; |
| 245 scoped_refptr<storage::FileSystemContext> file_system_context_; | 245 scoped_refptr<storage::FileSystemContext> file_system_context_; |
| 246 scoped_refptr<BlobData> blob_data_; | 246 scoped_ptr<BlobDataBuilder> blob_data_; |
| 247 scoped_ptr<BlobDataSnapshot> blob_data_snapshot_; |
| 247 net::URLRequestJobFactoryImpl url_request_job_factory_; | 248 net::URLRequestJobFactoryImpl url_request_job_factory_; |
| 248 net::URLRequestContext url_request_context_; | 249 net::URLRequestContext url_request_context_; |
| 249 MockURLRequestDelegate url_request_delegate_; | 250 MockURLRequestDelegate url_request_delegate_; |
| 250 scoped_ptr<net::URLRequest> request_; | 251 scoped_ptr<net::URLRequest> request_; |
| 251 | 252 |
| 252 int expected_status_code_; | 253 int expected_status_code_; |
| 253 std::string expected_response_; | 254 std::string expected_response_; |
| 254 }; | 255 }; |
| 255 | 256 |
| 256 TEST_F(BlobURLRequestJobTest, TestGetSimpleDataRequest) { | 257 TEST_F(BlobURLRequestJobTest, TestGetSimpleDataRequest) { |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 407 EXPECT_TRUE(request_->response_headers()->GetMimeType(&content_type)); | 408 EXPECT_TRUE(request_->response_headers()->GetMimeType(&content_type)); |
| 408 EXPECT_EQ(kTestContentType, content_type); | 409 EXPECT_EQ(kTestContentType, content_type); |
| 409 void* iter = NULL; | 410 void* iter = NULL; |
| 410 std::string content_disposition; | 411 std::string content_disposition; |
| 411 EXPECT_TRUE(request_->response_headers()->EnumerateHeader( | 412 EXPECT_TRUE(request_->response_headers()->EnumerateHeader( |
| 412 &iter, "Content-Disposition", &content_disposition)); | 413 &iter, "Content-Disposition", &content_disposition)); |
| 413 EXPECT_EQ(kTestContentDisposition, content_disposition); | 414 EXPECT_EQ(kTestContentDisposition, content_disposition); |
| 414 } | 415 } |
| 415 | 416 |
| 416 } // namespace content | 417 } // namespace content |
| OLD | NEW |