Chromium Code Reviews| 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 "webkit/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" |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 46 namespace { | 46 namespace { |
| 47 | 47 |
| 48 // We always use the TEMPORARY FileSystem in this test. | 48 // We always use the TEMPORARY FileSystem in this test. |
| 49 const char kFileSystemURLPrefix[] = "filesystem:http://remote/temporary/"; | 49 const char kFileSystemURLPrefix[] = "filesystem:http://remote/temporary/"; |
| 50 const char kTestFileData[] = "0123456789"; | 50 const char kTestFileData[] = "0123456789"; |
| 51 | 51 |
| 52 void FillBuffer(char* buffer, size_t len) { | 52 void FillBuffer(char* buffer, size_t len) { |
| 53 base::RandBytes(buffer, len); | 53 base::RandBytes(buffer, len); |
| 54 } | 54 } |
| 55 | 55 |
| 56 static const char kValidExternalMountPoint[] = "mnt_name"; | |
| 57 | |
| 58 // An auto mounter that will try to mount anything for |storage_domain| = | |
| 59 // "automount", but will only succeed for the mount point "mnt_name". | |
| 60 bool TestAutoMountForURLRequest( | |
| 61 const base::FilePath& path, | |
| 62 const net::URLRequest* /*url_request*/, | |
| 63 const fileapi::FileSystemURL& filesystem_url, | |
| 64 const std::string& storage_domain, | |
| 65 const base::Callback<void(base::File::Error result)>& callback) { | |
| 66 if (storage_domain != "automount") | |
| 67 return false; | |
| 68 std::vector<base::FilePath::StringType> components; | |
| 69 filesystem_url.path().GetComponents(&components); | |
| 70 #if defined(OS_POSIX) | |
| 71 std::string mount_point = components[0]; | |
| 72 #elif defined(OS_WIN) | |
| 73 std::string mount_point = base::WideToUTF8(components[0]); | |
|
kinuko
2014/03/13 04:19:20
ditto, AsUTF8Unsafe() should work?
vandebo (ex-Chrome)
2014/03/13 20:19:32
Done.
| |
| 74 #endif // OS_WIN | |
| 75 | |
| 76 if (mount_point == kValidExternalMountPoint) { | |
| 77 fileapi::ExternalMountPoints::GetSystemInstance()->RegisterFileSystem( | |
| 78 kValidExternalMountPoint, fileapi::kFileSystemTypeNativeLocal, | |
| 79 fileapi::FileSystemMountOption(), path); | |
| 80 callback.Run(base::File::FILE_OK); | |
| 81 } else { | |
| 82 callback.Run(base::File::FILE_ERROR_NOT_FOUND); | |
| 83 } | |
| 84 return true; | |
| 85 } | |
| 86 | |
| 56 } // namespace | 87 } // namespace |
| 57 | 88 |
| 58 class FileSystemURLRequestJobTest : public testing::Test { | 89 class FileSystemURLRequestJobTest : public testing::Test { |
| 59 protected: | 90 protected: |
| 60 FileSystemURLRequestJobTest() : weak_factory_(this) { | 91 FileSystemURLRequestJobTest() : weak_factory_(this) { |
| 61 } | 92 } |
| 62 | 93 |
| 63 virtual void SetUp() OVERRIDE { | 94 virtual void SetUp() OVERRIDE { |
| 64 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 95 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 65 | 96 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 103 delegate_.reset(new net::TestDelegate()); | 134 delegate_.reset(new net::TestDelegate()); |
| 104 // Make delegate_ exit the MessageLoop when the request is done. | 135 // Make delegate_ exit the MessageLoop when the request is done. |
| 105 delegate_->set_quit_on_complete(true); | 136 delegate_->set_quit_on_complete(true); |
| 106 delegate_->set_quit_on_redirect(true); | 137 delegate_->set_quit_on_redirect(true); |
| 107 request_ = empty_context_.CreateRequest( | 138 request_ = empty_context_.CreateRequest( |
| 108 url, net::DEFAULT_PRIORITY, delegate_.get()); | 139 url, net::DEFAULT_PRIORITY, delegate_.get()); |
| 109 if (headers) | 140 if (headers) |
| 110 request_->SetExtraRequestHeaders(*headers); | 141 request_->SetExtraRequestHeaders(*headers); |
| 111 ASSERT_TRUE(!job_); | 142 ASSERT_TRUE(!job_); |
| 112 job_ = new FileSystemURLRequestJob( | 143 job_ = new FileSystemURLRequestJob( |
| 113 request_.get(), NULL, file_system_context); | 144 request_.get(), NULL, url.GetOrigin().host(), file_system_context); |
| 114 pending_job_ = job_; | 145 pending_job_ = job_; |
| 115 | 146 |
| 116 request_->Start(); | 147 request_->Start(); |
| 117 ASSERT_TRUE(request_->is_pending()); // verify that we're starting async | 148 ASSERT_TRUE(request_->is_pending()); // verify that we're starting async |
| 118 if (run_to_completion) | 149 if (run_to_completion) |
| 119 base::MessageLoop::current()->Run(); | 150 base::MessageLoop::current()->Run(); |
| 120 } | 151 } |
| 121 | 152 |
| 122 void TestRequest(const GURL& url) { | 153 void TestRequest(const GURL& url) { |
| 123 TestRequestHelper(url, NULL, true, file_system_context_.get()); | 154 TestRequestHelper(url, NULL, true, file_system_context_.get()); |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 253 net::HttpRequestHeaders::kRange, | 284 net::HttpRequestHeaders::kRange, |
| 254 net::HttpByteRange::RightUnbounded(first_byte_position).GetHeaderValue()); | 285 net::HttpByteRange::RightUnbounded(first_byte_position).GetHeaderValue()); |
| 255 TestRequestWithHeaders(CreateFileSystemURL("bigfile"), &headers); | 286 TestRequestWithHeaders(CreateFileSystemURL("bigfile"), &headers); |
| 256 ASSERT_FALSE(request_->is_pending()); | 287 ASSERT_FALSE(request_->is_pending()); |
| 257 EXPECT_EQ(1, delegate_->response_started_count()); | 288 EXPECT_EQ(1, delegate_->response_started_count()); |
| 258 EXPECT_FALSE(delegate_->received_data_before_response()); | 289 EXPECT_FALSE(delegate_->received_data_before_response()); |
| 259 // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed. | 290 // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed. |
| 260 EXPECT_TRUE(partial_buffer_string == delegate_->data_received()); | 291 EXPECT_TRUE(partial_buffer_string == delegate_->data_received()); |
| 261 } | 292 } |
| 262 | 293 |
| 263 | |
| 264 TEST_F(FileSystemURLRequestJobTest, FileTestMultipleRangesNotSupported) { | 294 TEST_F(FileSystemURLRequestJobTest, FileTestMultipleRangesNotSupported) { |
| 265 WriteFile("file1.dat", kTestFileData, arraysize(kTestFileData) - 1); | 295 WriteFile("file1.dat", kTestFileData, arraysize(kTestFileData) - 1); |
| 266 net::HttpRequestHeaders headers; | 296 net::HttpRequestHeaders headers; |
| 267 headers.SetHeader(net::HttpRequestHeaders::kRange, | 297 headers.SetHeader(net::HttpRequestHeaders::kRange, |
| 268 "bytes=0-5,10-200,200-300"); | 298 "bytes=0-5,10-200,200-300"); |
| 269 TestRequestWithHeaders(CreateFileSystemURL("file1.dat"), &headers); | 299 TestRequestWithHeaders(CreateFileSystemURL("file1.dat"), &headers); |
| 270 EXPECT_TRUE(delegate_->request_failed()); | 300 EXPECT_TRUE(delegate_->request_failed()); |
| 271 EXPECT_EQ(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE, | 301 EXPECT_EQ(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE, |
| 272 request_->status().error()); | 302 request_->status().error()); |
| 273 } | 303 } |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 362 EXPECT_TRUE(delegate_->request_failed()); | 392 EXPECT_TRUE(delegate_->request_failed()); |
| 363 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request_->status().error()); | 393 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request_->status().error()); |
| 364 | 394 |
| 365 // Make sure it returns success with regular (non-incognito) context. | 395 // Make sure it returns success with regular (non-incognito) context. |
| 366 TestRequest(CreateFileSystemURL("file")); | 396 TestRequest(CreateFileSystemURL("file")); |
| 367 ASSERT_FALSE(request_->is_pending()); | 397 ASSERT_FALSE(request_->is_pending()); |
| 368 EXPECT_EQ(kTestFileData, delegate_->data_received()); | 398 EXPECT_EQ(kTestFileData, delegate_->data_received()); |
| 369 EXPECT_EQ(200, request_->GetResponseCode()); | 399 EXPECT_EQ(200, request_->GetResponseCode()); |
| 370 } | 400 } |
| 371 | 401 |
| 402 TEST_F(FileSystemURLRequestJobTest, AutoMountFileTest) { | |
| 403 base::FilePath mnt_point = temp_dir_.path().AppendASCII("auto_mount_dir"); | |
| 404 ASSERT_TRUE(base::CreateDirectory(mnt_point)); | |
| 405 ASSERT_EQ(static_cast<int>(sizeof(kTestFileData)) - 1, | |
| 406 base::WriteFile(mnt_point.AppendASCII("foo"), kTestFileData, | |
| 407 sizeof(kTestFileData) - 1)); | |
| 408 | |
| 409 std::vector<fileapi::URLRequestAutoMountHandler> handlers; | |
| 410 handlers.push_back(base::Bind(&TestAutoMountForURLRequest, mnt_point)); | |
| 411 scoped_refptr<FileSystemContext> file_system_context = | |
| 412 CreateFileSystemContextWithAutoMountersForTesting(NULL, handlers, | |
| 413 temp_dir_.path()); | |
| 414 TestRequestWithContext( | |
| 415 GURL("filesystem:http://automount/external/mnt_name/foo"), | |
| 416 file_system_context); | |
| 417 | |
| 418 ASSERT_FALSE(request_->is_pending()); | |
| 419 EXPECT_EQ(1, delegate_->response_started_count()); | |
| 420 EXPECT_FALSE(delegate_->received_data_before_response()); | |
| 421 EXPECT_EQ(kTestFileData, delegate_->data_received()); | |
| 422 EXPECT_EQ(200, request_->GetResponseCode()); | |
| 423 std::string cache_control; | |
| 424 request_->GetResponseHeaderByName("cache-control", &cache_control); | |
| 425 EXPECT_EQ("no-cache", cache_control); | |
| 426 | |
| 427 ASSERT_TRUE( | |
| 428 fileapi::ExternalMountPoints::GetSystemInstance()->RevokeFileSystem( | |
| 429 kValidExternalMountPoint)); | |
| 430 } | |
| 431 | |
| 432 TEST_F(FileSystemURLRequestJobTest, AutoMountInvalidRoot) { | |
| 433 base::FilePath mnt_point = temp_dir_.path().AppendASCII("auto_mount_dir"); | |
| 434 ASSERT_TRUE(base::CreateDirectory(mnt_point)); | |
| 435 ASSERT_EQ(static_cast<int>(sizeof(kTestFileData)) - 1, | |
| 436 base::WriteFile(mnt_point.AppendASCII("foo"), kTestFileData, | |
| 437 sizeof(kTestFileData) - 1)); | |
| 438 | |
| 439 std::vector<fileapi::URLRequestAutoMountHandler> handlers; | |
| 440 handlers.push_back(base::Bind(&TestAutoMountForURLRequest, mnt_point)); | |
| 441 scoped_refptr<FileSystemContext> file_system_context = | |
| 442 CreateFileSystemContextWithAutoMountersForTesting(NULL, handlers, | |
| 443 temp_dir_.path()); | |
| 444 TestRequestWithContext( | |
| 445 GURL("filesystem:http://automount/external/invalid/foo"), | |
| 446 file_system_context); | |
| 447 | |
| 448 ASSERT_FALSE(request_->is_pending()); | |
| 449 EXPECT_TRUE(delegate_->request_failed()); | |
| 450 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request_->status().error()); | |
| 451 | |
| 452 ASSERT_FALSE( | |
| 453 fileapi::ExternalMountPoints::GetSystemInstance()->RevokeFileSystem( | |
| 454 "invalid")); | |
| 455 } | |
| 456 | |
| 457 TEST_F(FileSystemURLRequestJobTest, AutoMountNoHandler) { | |
| 458 base::FilePath mnt_point = temp_dir_.path().AppendASCII("auto_mount_dir"); | |
| 459 ASSERT_TRUE(base::CreateDirectory(mnt_point)); | |
| 460 ASSERT_EQ(static_cast<int>(sizeof(kTestFileData)) - 1, | |
| 461 base::WriteFile(mnt_point.AppendASCII("foo"), kTestFileData, | |
| 462 sizeof(kTestFileData) - 1)); | |
| 463 | |
| 464 std::vector<fileapi::URLRequestAutoMountHandler> handlers; | |
| 465 handlers.push_back(base::Bind(&TestAutoMountForURLRequest, mnt_point)); | |
| 466 scoped_refptr<FileSystemContext> file_system_context = | |
| 467 CreateFileSystemContextWithAutoMountersForTesting(NULL, handlers, | |
| 468 temp_dir_.path()); | |
| 469 TestRequestWithContext( | |
| 470 GURL("filesystem:http://noauto/external/mnt_name/foo"), | |
| 471 file_system_context); | |
| 472 | |
| 473 ASSERT_FALSE(request_->is_pending()); | |
| 474 EXPECT_TRUE(delegate_->request_failed()); | |
| 475 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request_->status().error()); | |
| 476 | |
| 477 ASSERT_FALSE( | |
| 478 fileapi::ExternalMountPoints::GetSystemInstance()->RevokeFileSystem( | |
| 479 kValidExternalMountPoint)); | |
| 480 } | |
| 481 | |
| 372 } // namespace | 482 } // namespace |
| 373 } // namespace content | 483 } // namespace content |
| OLD | NEW |