| 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 "webkit/browser/fileapi/file_system_url_request_job.h" | 5 #include "storage/browser/fileapi/file_system_url_request_job.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| 11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 12 #include "base/files/scoped_temp_dir.h" | 12 #include "base/files/scoped_temp_dir.h" |
| 13 #include "base/format_macros.h" | 13 #include "base/format_macros.h" |
| 14 #include "base/memory/scoped_vector.h" | 14 #include "base/memory/scoped_vector.h" |
| 15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 27 #include "net/base/mime_util.h" | 27 #include "net/base/mime_util.h" |
| 28 #include "net/base/net_errors.h" | 28 #include "net/base/net_errors.h" |
| 29 #include "net/base/net_util.h" | 29 #include "net/base/net_util.h" |
| 30 #include "net/base/request_priority.h" | 30 #include "net/base/request_priority.h" |
| 31 #include "net/http/http_byte_range.h" | 31 #include "net/http/http_byte_range.h" |
| 32 #include "net/http/http_request_headers.h" | 32 #include "net/http/http_request_headers.h" |
| 33 #include "net/url_request/url_request.h" | 33 #include "net/url_request/url_request.h" |
| 34 #include "net/url_request/url_request_context.h" | 34 #include "net/url_request/url_request_context.h" |
| 35 #include "net/url_request/url_request_test_util.h" | 35 #include "net/url_request/url_request_test_util.h" |
| 36 #include "testing/gtest/include/gtest/gtest.h" | 36 #include "testing/gtest/include/gtest/gtest.h" |
| 37 #include "webkit/browser/fileapi/external_mount_points.h" | 37 #include "storage/browser/fileapi/external_mount_points.h" |
| 38 #include "webkit/browser/fileapi/file_system_context.h" | 38 #include "storage/browser/fileapi/file_system_context.h" |
| 39 #include "webkit/browser/fileapi/file_system_file_util.h" | 39 #include "storage/browser/fileapi/file_system_file_util.h" |
| 40 | 40 |
| 41 using content::AsyncFileTestHelper; | 41 using content::AsyncFileTestHelper; |
| 42 using fileapi::FileSystemContext; | 42 using storage::FileSystemContext; |
| 43 using fileapi::FileSystemURL; | 43 using storage::FileSystemURL; |
| 44 using fileapi::FileSystemURLRequestJob; | 44 using storage::FileSystemURLRequestJob; |
| 45 | 45 |
| 46 namespace content { | 46 namespace content { |
| 47 namespace { | 47 namespace { |
| 48 | 48 |
| 49 // We always use the TEMPORARY FileSystem in this test. | 49 // We always use the TEMPORARY FileSystem in this test. |
| 50 const char kFileSystemURLPrefix[] = "filesystem:http://remote/temporary/"; | 50 const char kFileSystemURLPrefix[] = "filesystem:http://remote/temporary/"; |
| 51 const char kTestFileData[] = "0123456789"; | 51 const char kTestFileData[] = "0123456789"; |
| 52 | 52 |
| 53 void FillBuffer(char* buffer, size_t len) { | 53 void FillBuffer(char* buffer, size_t len) { |
| 54 base::RandBytes(buffer, len); | 54 base::RandBytes(buffer, len); |
| 55 } | 55 } |
| 56 | 56 |
| 57 const char kValidExternalMountPoint[] = "mnt_name"; | 57 const char kValidExternalMountPoint[] = "mnt_name"; |
| 58 | 58 |
| 59 // An auto mounter that will try to mount anything for |storage_domain| = | 59 // An auto mounter that will try to mount anything for |storage_domain| = |
| 60 // "automount", but will only succeed for the mount point "mnt_name". | 60 // "automount", but will only succeed for the mount point "mnt_name". |
| 61 bool TestAutoMountForURLRequest( | 61 bool TestAutoMountForURLRequest( |
| 62 const net::URLRequest* /*url_request*/, | 62 const net::URLRequest* /*url_request*/, |
| 63 const fileapi::FileSystemURL& filesystem_url, | 63 const storage::FileSystemURL& filesystem_url, |
| 64 const std::string& storage_domain, | 64 const std::string& storage_domain, |
| 65 const base::Callback<void(base::File::Error result)>& callback) { | 65 const base::Callback<void(base::File::Error result)>& callback) { |
| 66 if (storage_domain != "automount") | 66 if (storage_domain != "automount") |
| 67 return false; | 67 return false; |
| 68 std::vector<base::FilePath::StringType> components; | 68 std::vector<base::FilePath::StringType> components; |
| 69 filesystem_url.path().GetComponents(&components); | 69 filesystem_url.path().GetComponents(&components); |
| 70 std::string mount_point = base::FilePath(components[0]).AsUTF8Unsafe(); | 70 std::string mount_point = base::FilePath(components[0]).AsUTF8Unsafe(); |
| 71 | 71 |
| 72 if (mount_point == kValidExternalMountPoint) { | 72 if (mount_point == kValidExternalMountPoint) { |
| 73 fileapi::ExternalMountPoints::GetSystemInstance()->RegisterFileSystem( | 73 storage::ExternalMountPoints::GetSystemInstance()->RegisterFileSystem( |
| 74 kValidExternalMountPoint, fileapi::kFileSystemTypeTest, | 74 kValidExternalMountPoint, |
| 75 fileapi::FileSystemMountOption(), base::FilePath()); | 75 storage::kFileSystemTypeTest, |
| 76 storage::FileSystemMountOption(), |
| 77 base::FilePath()); |
| 76 callback.Run(base::File::FILE_OK); | 78 callback.Run(base::File::FILE_OK); |
| 77 } else { | 79 } else { |
| 78 callback.Run(base::File::FILE_ERROR_NOT_FOUND); | 80 callback.Run(base::File::FILE_ERROR_NOT_FOUND); |
| 79 } | 81 } |
| 80 return true; | 82 return true; |
| 81 } | 83 } |
| 82 | 84 |
| 83 class FileSystemURLRequestJobFactory : public net::URLRequestJobFactory { | 85 class FileSystemURLRequestJobFactory : public net::URLRequestJobFactory { |
| 84 public: | 86 public: |
| 85 FileSystemURLRequestJobFactory(const std::string& storage_domain, | 87 FileSystemURLRequestJobFactory(const std::string& storage_domain, |
| 86 FileSystemContext* context) | 88 FileSystemContext* context) |
| 87 : storage_domain_(storage_domain), file_system_context_(context) { | 89 : storage_domain_(storage_domain), file_system_context_(context) { |
| 88 } | 90 } |
| 89 | 91 |
| 90 virtual net::URLRequestJob* MaybeCreateJobWithProtocolHandler( | 92 virtual net::URLRequestJob* MaybeCreateJobWithProtocolHandler( |
| 91 const std::string& scheme, | 93 const std::string& scheme, |
| 92 net::URLRequest* request, | 94 net::URLRequest* request, |
| 93 net::NetworkDelegate* network_delegate) const OVERRIDE { | 95 net::NetworkDelegate* network_delegate) const OVERRIDE { |
| 94 return new fileapi::FileSystemURLRequestJob( | 96 return new storage::FileSystemURLRequestJob( |
| 95 request, network_delegate, storage_domain_, file_system_context_); | 97 request, network_delegate, storage_domain_, file_system_context_); |
| 96 } | 98 } |
| 97 | 99 |
| 98 virtual bool IsHandledProtocol(const std::string& scheme) const OVERRIDE { | 100 virtual bool IsHandledProtocol(const std::string& scheme) const OVERRIDE { |
| 99 return true; | 101 return true; |
| 100 } | 102 } |
| 101 | 103 |
| 102 virtual bool IsHandledURL(const GURL& url) const OVERRIDE { | 104 virtual bool IsHandledURL(const GURL& url) const OVERRIDE { |
| 103 return true; | 105 return true; |
| 104 } | 106 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 121 | 123 |
| 122 virtual void SetUp() OVERRIDE { | 124 virtual void SetUp() OVERRIDE { |
| 123 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 125 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 124 | 126 |
| 125 // We use the main thread so that we can get the root path synchronously. | 127 // We use the main thread so that we can get the root path synchronously. |
| 126 // TODO(adamk): Run this on the FILE thread we've created as well. | 128 // TODO(adamk): Run this on the FILE thread we've created as well. |
| 127 file_system_context_ = | 129 file_system_context_ = |
| 128 CreateFileSystemContextForTesting(NULL, temp_dir_.path()); | 130 CreateFileSystemContextForTesting(NULL, temp_dir_.path()); |
| 129 | 131 |
| 130 file_system_context_->OpenFileSystem( | 132 file_system_context_->OpenFileSystem( |
| 131 GURL("http://remote/"), fileapi::kFileSystemTypeTemporary, | 133 GURL("http://remote/"), |
| 132 fileapi::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, | 134 storage::kFileSystemTypeTemporary, |
| 135 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, |
| 133 base::Bind(&FileSystemURLRequestJobTest::OnOpenFileSystem, | 136 base::Bind(&FileSystemURLRequestJobTest::OnOpenFileSystem, |
| 134 weak_factory_.GetWeakPtr())); | 137 weak_factory_.GetWeakPtr())); |
| 135 base::RunLoop().RunUntilIdle(); | 138 base::RunLoop().RunUntilIdle(); |
| 136 } | 139 } |
| 137 | 140 |
| 138 virtual void TearDown() OVERRIDE { | 141 virtual void TearDown() OVERRIDE { |
| 139 // FileReader posts a task to close the file in destructor. | 142 // FileReader posts a task to close the file in destructor. |
| 140 base::RunLoop().RunUntilIdle(); | 143 base::RunLoop().RunUntilIdle(); |
| 141 } | 144 } |
| 142 | 145 |
| 143 void SetUpAutoMountContext() { | 146 void SetUpAutoMountContext() { |
| 144 base::FilePath mnt_point = temp_dir_.path().AppendASCII("auto_mount_dir"); | 147 base::FilePath mnt_point = temp_dir_.path().AppendASCII("auto_mount_dir"); |
| 145 ASSERT_TRUE(base::CreateDirectory(mnt_point)); | 148 ASSERT_TRUE(base::CreateDirectory(mnt_point)); |
| 146 | 149 |
| 147 ScopedVector<fileapi::FileSystemBackend> additional_providers; | 150 ScopedVector<storage::FileSystemBackend> additional_providers; |
| 148 additional_providers.push_back(new TestFileSystemBackend( | 151 additional_providers.push_back(new TestFileSystemBackend( |
| 149 base::MessageLoopProxy::current().get(), mnt_point)); | 152 base::MessageLoopProxy::current().get(), mnt_point)); |
| 150 | 153 |
| 151 std::vector<fileapi::URLRequestAutoMountHandler> handlers; | 154 std::vector<storage::URLRequestAutoMountHandler> handlers; |
| 152 handlers.push_back(base::Bind(&TestAutoMountForURLRequest)); | 155 handlers.push_back(base::Bind(&TestAutoMountForURLRequest)); |
| 153 | 156 |
| 154 file_system_context_ = CreateFileSystemContextWithAutoMountersForTesting( | 157 file_system_context_ = CreateFileSystemContextWithAutoMountersForTesting( |
| 155 NULL, additional_providers.Pass(), handlers, temp_dir_.path()); | 158 NULL, additional_providers.Pass(), handlers, temp_dir_.path()); |
| 156 | 159 |
| 157 ASSERT_EQ(static_cast<int>(sizeof(kTestFileData)) - 1, | 160 ASSERT_EQ(static_cast<int>(sizeof(kTestFileData)) - 1, |
| 158 base::WriteFile(mnt_point.AppendASCII("foo"), kTestFileData, | 161 base::WriteFile(mnt_point.AppendASCII("foo"), kTestFileData, |
| 159 sizeof(kTestFileData) - 1)); | 162 sizeof(kTestFileData) - 1)); |
| 160 } | 163 } |
| 161 | 164 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 203 TestRequestHelper(url, headers, true, file_system_context_.get()); | 206 TestRequestHelper(url, headers, true, file_system_context_.get()); |
| 204 } | 207 } |
| 205 | 208 |
| 206 void TestRequestNoRun(const GURL& url) { | 209 void TestRequestNoRun(const GURL& url) { |
| 207 TestRequestHelper(url, NULL, false, file_system_context_.get()); | 210 TestRequestHelper(url, NULL, false, file_system_context_.get()); |
| 208 } | 211 } |
| 209 | 212 |
| 210 void CreateDirectory(const base::StringPiece& dir_name) { | 213 void CreateDirectory(const base::StringPiece& dir_name) { |
| 211 FileSystemURL url = file_system_context_->CreateCrackedFileSystemURL( | 214 FileSystemURL url = file_system_context_->CreateCrackedFileSystemURL( |
| 212 GURL("http://remote"), | 215 GURL("http://remote"), |
| 213 fileapi::kFileSystemTypeTemporary, | 216 storage::kFileSystemTypeTemporary, |
| 214 base::FilePath().AppendASCII(dir_name)); | 217 base::FilePath().AppendASCII(dir_name)); |
| 215 ASSERT_EQ(base::File::FILE_OK, AsyncFileTestHelper::CreateDirectory( | 218 ASSERT_EQ(base::File::FILE_OK, AsyncFileTestHelper::CreateDirectory( |
| 216 file_system_context_, url)); | 219 file_system_context_, url)); |
| 217 } | 220 } |
| 218 | 221 |
| 219 void WriteFile(const base::StringPiece& file_name, | 222 void WriteFile(const base::StringPiece& file_name, |
| 220 const char* buf, int buf_size) { | 223 const char* buf, int buf_size) { |
| 221 FileSystemURL url = file_system_context_->CreateCrackedFileSystemURL( | 224 FileSystemURL url = file_system_context_->CreateCrackedFileSystemURL( |
| 222 GURL("http://remote"), | 225 GURL("http://remote"), |
| 223 fileapi::kFileSystemTypeTemporary, | 226 storage::kFileSystemTypeTemporary, |
| 224 base::FilePath().AppendASCII(file_name)); | 227 base::FilePath().AppendASCII(file_name)); |
| 225 ASSERT_EQ(base::File::FILE_OK, | 228 ASSERT_EQ(base::File::FILE_OK, |
| 226 AsyncFileTestHelper::CreateFileWithData( | 229 AsyncFileTestHelper::CreateFileWithData( |
| 227 file_system_context_, url, buf, buf_size)); | 230 file_system_context_, url, buf, buf_size)); |
| 228 } | 231 } |
| 229 | 232 |
| 230 GURL CreateFileSystemURL(const std::string& path) { | 233 GURL CreateFileSystemURL(const std::string& path) { |
| 231 return GURL(kFileSystemURLPrefix + path); | 234 return GURL(kFileSystemURLPrefix + path); |
| 232 } | 235 } |
| 233 | 236 |
| 234 // Put the message loop at the top, so that it's the last thing deleted. | 237 // Put the message loop at the top, so that it's the last thing deleted. |
| 235 base::MessageLoopForIO message_loop_; | 238 base::MessageLoopForIO message_loop_; |
| 236 | 239 |
| 237 base::ScopedTempDir temp_dir_; | 240 base::ScopedTempDir temp_dir_; |
| 238 scoped_refptr<fileapi::FileSystemContext> file_system_context_; | 241 scoped_refptr<storage::FileSystemContext> file_system_context_; |
| 239 base::WeakPtrFactory<FileSystemURLRequestJobTest> weak_factory_; | 242 base::WeakPtrFactory<FileSystemURLRequestJobTest> weak_factory_; |
| 240 | 243 |
| 241 net::URLRequestContext empty_context_; | 244 net::URLRequestContext empty_context_; |
| 242 scoped_ptr<FileSystemURLRequestJobFactory> job_factory_; | 245 scoped_ptr<FileSystemURLRequestJobFactory> job_factory_; |
| 243 | 246 |
| 244 // NOTE: order matters, request must die before delegate | 247 // NOTE: order matters, request must die before delegate |
| 245 scoped_ptr<net::TestDelegate> delegate_; | 248 scoped_ptr<net::TestDelegate> delegate_; |
| 246 scoped_ptr<net::URLRequest> request_; | 249 scoped_ptr<net::URLRequest> request_; |
| 247 }; | 250 }; |
| 248 | 251 |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 423 ASSERT_FALSE(request_->is_pending()); | 426 ASSERT_FALSE(request_->is_pending()); |
| 424 EXPECT_EQ(1, delegate_->response_started_count()); | 427 EXPECT_EQ(1, delegate_->response_started_count()); |
| 425 EXPECT_FALSE(delegate_->received_data_before_response()); | 428 EXPECT_FALSE(delegate_->received_data_before_response()); |
| 426 EXPECT_EQ(kTestFileData, delegate_->data_received()); | 429 EXPECT_EQ(kTestFileData, delegate_->data_received()); |
| 427 EXPECT_EQ(200, request_->GetResponseCode()); | 430 EXPECT_EQ(200, request_->GetResponseCode()); |
| 428 std::string cache_control; | 431 std::string cache_control; |
| 429 request_->GetResponseHeaderByName("cache-control", &cache_control); | 432 request_->GetResponseHeaderByName("cache-control", &cache_control); |
| 430 EXPECT_EQ("no-cache", cache_control); | 433 EXPECT_EQ("no-cache", cache_control); |
| 431 | 434 |
| 432 ASSERT_TRUE( | 435 ASSERT_TRUE( |
| 433 fileapi::ExternalMountPoints::GetSystemInstance()->RevokeFileSystem( | 436 storage::ExternalMountPoints::GetSystemInstance()->RevokeFileSystem( |
| 434 kValidExternalMountPoint)); | 437 kValidExternalMountPoint)); |
| 435 } | 438 } |
| 436 | 439 |
| 437 TEST_F(FileSystemURLRequestJobTest, AutoMountInvalidRoot) { | 440 TEST_F(FileSystemURLRequestJobTest, AutoMountInvalidRoot) { |
| 438 SetUpAutoMountContext(); | 441 SetUpAutoMountContext(); |
| 439 TestRequest(GURL("filesystem:http://automount/external/invalid/foo")); | 442 TestRequest(GURL("filesystem:http://automount/external/invalid/foo")); |
| 440 | 443 |
| 441 ASSERT_FALSE(request_->is_pending()); | 444 ASSERT_FALSE(request_->is_pending()); |
| 442 EXPECT_TRUE(delegate_->request_failed()); | 445 EXPECT_TRUE(delegate_->request_failed()); |
| 443 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request_->status().error()); | 446 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request_->status().error()); |
| 444 | 447 |
| 445 ASSERT_FALSE( | 448 ASSERT_FALSE( |
| 446 fileapi::ExternalMountPoints::GetSystemInstance()->RevokeFileSystem( | 449 storage::ExternalMountPoints::GetSystemInstance()->RevokeFileSystem( |
| 447 "invalid")); | 450 "invalid")); |
| 448 } | 451 } |
| 449 | 452 |
| 450 TEST_F(FileSystemURLRequestJobTest, AutoMountNoHandler) { | 453 TEST_F(FileSystemURLRequestJobTest, AutoMountNoHandler) { |
| 451 SetUpAutoMountContext(); | 454 SetUpAutoMountContext(); |
| 452 TestRequest(GURL("filesystem:http://noauto/external/mnt_name/foo")); | 455 TestRequest(GURL("filesystem:http://noauto/external/mnt_name/foo")); |
| 453 | 456 |
| 454 ASSERT_FALSE(request_->is_pending()); | 457 ASSERT_FALSE(request_->is_pending()); |
| 455 EXPECT_TRUE(delegate_->request_failed()); | 458 EXPECT_TRUE(delegate_->request_failed()); |
| 456 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request_->status().error()); | 459 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request_->status().error()); |
| 457 | 460 |
| 458 ASSERT_FALSE( | 461 ASSERT_FALSE( |
| 459 fileapi::ExternalMountPoints::GetSystemInstance()->RevokeFileSystem( | 462 storage::ExternalMountPoints::GetSystemInstance()->RevokeFileSystem( |
| 460 kValidExternalMountPoint)); | 463 kValidExternalMountPoint)); |
| 461 } | 464 } |
| 462 | 465 |
| 463 } // namespace | 466 } // namespace |
| 464 } // namespace content | 467 } // namespace content |
| OLD | NEW |