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_dir_url_request_job.h" | 5 #include "webkit/browser/fileapi/file_system_dir_url_request_job.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/file_util.h" | |
| 9 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 10 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
| 11 #include "base/format_macros.h" | 12 #include "base/format_macros.h" |
| 12 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 13 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
| 14 #include "base/platform_file.h" | 15 #include "base/platform_file.h" |
| 15 #include "base/run_loop.h" | 16 #include "base/run_loop.h" |
| 16 #include "base/strings/string_piece.h" | 17 #include "base/strings/string_piece.h" |
| 17 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
| 18 #include "content/public/test/test_file_system_context.h" | 19 #include "content/public/test/test_file_system_context.h" |
| 19 #include "net/base/net_errors.h" | 20 #include "net/base/net_errors.h" |
| 20 #include "net/base/net_util.h" | 21 #include "net/base/net_util.h" |
| 21 #include "net/base/request_priority.h" | 22 #include "net/base/request_priority.h" |
| 22 #include "net/http/http_request_headers.h" | 23 #include "net/http/http_request_headers.h" |
| 23 #include "net/url_request/url_request.h" | 24 #include "net/url_request/url_request.h" |
| 24 #include "net/url_request/url_request_context.h" | 25 #include "net/url_request/url_request_context.h" |
| 25 #include "net/url_request/url_request_test_util.h" | 26 #include "net/url_request/url_request_test_util.h" |
| 26 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
| 27 #include "third_party/icu/source/i18n/unicode/regex.h" | 28 #include "third_party/icu/source/i18n/unicode/regex.h" |
| 29 #include "webkit/browser/fileapi/external_mount_points.h" | |
| 28 #include "webkit/browser/fileapi/file_system_context.h" | 30 #include "webkit/browser/fileapi/file_system_context.h" |
| 29 #include "webkit/browser/fileapi/file_system_file_util.h" | 31 #include "webkit/browser/fileapi/file_system_file_util.h" |
| 30 #include "webkit/browser/fileapi/file_system_operation_context.h" | 32 #include "webkit/browser/fileapi/file_system_operation_context.h" |
| 31 #include "webkit/browser/fileapi/file_system_url.h" | 33 #include "webkit/browser/fileapi/file_system_url.h" |
| 32 #include "webkit/browser/quota/mock_special_storage_policy.h" | 34 #include "webkit/browser/quota/mock_special_storage_policy.h" |
| 33 | 35 |
| 34 using fileapi::FileSystemContext; | 36 using fileapi::FileSystemContext; |
| 35 using fileapi::FileSystemOperationContext; | 37 using fileapi::FileSystemOperationContext; |
| 36 using fileapi::FileSystemURL; | 38 using fileapi::FileSystemURL; |
| 37 | 39 |
| 38 namespace content { | 40 namespace content { |
| 39 namespace { | 41 namespace { |
| 40 | 42 |
| 41 // We always use the TEMPORARY FileSystem in this test. | 43 // We always use the TEMPORARY FileSystem in this test. |
| 42 static const char kFileSystemURLPrefix[] = | 44 static const char kFileSystemURLPrefix[] = |
| 43 "filesystem:http://remote/temporary/"; | 45 "filesystem:http://remote/temporary/"; |
| 44 | 46 |
| 47 static const char kValidExternalMountPoint[] = "mnt_name"; | |
|
kinuko
2014/03/13 04:19:20
nit: no need of static inside anon namespace? (And
vandebo (ex-Chrome)
2014/03/13 20:19:32
Done.
| |
| 48 | |
| 49 // An auto mounter that will try to mount anything for |storage_domain| = | |
| 50 // "automount", but will only succeed for the mount point "mnt_name". | |
| 51 bool TestAutoMountForURLRequest( | |
| 52 const base::FilePath& path, | |
| 53 const net::URLRequest* /*url_request*/, | |
| 54 const fileapi::FileSystemURL& filesystem_url, | |
| 55 const std::string& storage_domain, | |
| 56 const base::Callback<void(base::File::Error result)>& callback) { | |
| 57 if (storage_domain != "automount") | |
| 58 return false; | |
| 59 | |
| 60 std::vector<base::FilePath::StringType> components; | |
| 61 filesystem_url.path().GetComponents(&components); | |
| 62 #if defined(OS_POSIX) | |
| 63 std::string mount_point = components[0]; | |
| 64 #elif defined(OS_WIN) | |
| 65 std::string mount_point = base::WideToUTF8(components[0]); | |
|
kinuko
2014/03/13 04:19:20
FilePath(components[0]).AsUTF8Unsafe() would work
vandebo (ex-Chrome)
2014/03/13 20:19:32
Yea, but then you make an unnecessary object... I
| |
| 66 #endif // OS_WIN | |
| 67 | |
| 68 if (mount_point == kValidExternalMountPoint) { | |
| 69 fileapi::ExternalMountPoints::GetSystemInstance()->RegisterFileSystem( | |
| 70 kValidExternalMountPoint, fileapi::kFileSystemTypeNativeLocal, | |
| 71 fileapi::FileSystemMountOption(), path); | |
| 72 callback.Run(base::File::FILE_OK); | |
| 73 } else { | |
| 74 callback.Run(base::File::FILE_ERROR_NOT_FOUND); | |
| 75 } | |
| 76 return true; | |
| 77 } | |
| 78 | |
| 45 } // namespace | 79 } // namespace |
| 46 | 80 |
| 47 class FileSystemDirURLRequestJobTest : public testing::Test { | 81 class FileSystemDirURLRequestJobTest : public testing::Test { |
| 48 protected: | 82 protected: |
| 49 FileSystemDirURLRequestJobTest() | 83 FileSystemDirURLRequestJobTest() |
| 50 : weak_factory_(this) { | 84 : weak_factory_(this) { |
| 51 } | 85 } |
| 52 | 86 |
| 53 virtual void SetUp() OVERRIDE { | 87 virtual void SetUp() OVERRIDE { |
| 54 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 88 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 83 ASSERT_EQ(base::File::FILE_OK, result); | 117 ASSERT_EQ(base::File::FILE_OK, result); |
| 84 } | 118 } |
| 85 | 119 |
| 86 void TestRequestHelper(const GURL& url, bool run_to_completion, | 120 void TestRequestHelper(const GURL& url, bool run_to_completion, |
| 87 FileSystemContext* file_system_context) { | 121 FileSystemContext* file_system_context) { |
| 88 delegate_.reset(new net::TestDelegate()); | 122 delegate_.reset(new net::TestDelegate()); |
| 89 delegate_->set_quit_on_redirect(true); | 123 delegate_->set_quit_on_redirect(true); |
| 90 request_ = empty_context_.CreateRequest( | 124 request_ = empty_context_.CreateRequest( |
| 91 url, net::DEFAULT_PRIORITY, delegate_.get()); | 125 url, net::DEFAULT_PRIORITY, delegate_.get()); |
| 92 job_ = new fileapi::FileSystemDirURLRequestJob( | 126 job_ = new fileapi::FileSystemDirURLRequestJob( |
| 93 request_.get(), NULL, file_system_context); | 127 request_.get(), NULL, url.GetOrigin().host(), file_system_context); |
| 94 | 128 |
| 95 request_->Start(); | 129 request_->Start(); |
| 96 ASSERT_TRUE(request_->is_pending()); // verify that we're starting async | 130 ASSERT_TRUE(request_->is_pending()); // verify that we're starting async |
| 97 if (run_to_completion) | 131 if (run_to_completion) |
| 98 base::MessageLoop::current()->Run(); | 132 base::MessageLoop::current()->Run(); |
| 99 } | 133 } |
| 100 | 134 |
| 101 void TestRequest(const GURL& url) { | 135 void TestRequest(const GURL& url) { |
| 102 TestRequestHelper(url, true, file_system_context_.get()); | 136 TestRequestHelper(url, true, file_system_context_.get()); |
| 103 } | 137 } |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 258 EXPECT_EQ("<script>start(\"foo\\\\bar\");</script>", line); | 292 EXPECT_EQ("<script>start(\"foo\\\\bar\");</script>", line); |
| 259 #elif defined(OS_POSIX) | 293 #elif defined(OS_POSIX) |
| 260 EXPECT_EQ("<script>start(\"/foo/bar\");</script>", line); | 294 EXPECT_EQ("<script>start(\"/foo/bar\");</script>", line); |
| 261 #endif | 295 #endif |
| 262 | 296 |
| 263 EXPECT_TRUE(!!std::getline(in, line)); | 297 EXPECT_TRUE(!!std::getline(in, line)); |
| 264 VerifyListingEntry(line, "hoge", "hoge", false, 10); | 298 VerifyListingEntry(line, "hoge", "hoge", false, 10); |
| 265 | 299 |
| 266 EXPECT_TRUE(!!std::getline(in, line)); | 300 EXPECT_TRUE(!!std::getline(in, line)); |
| 267 VerifyListingEntry(line, "baz", "baz", true, 0); | 301 VerifyListingEntry(line, "baz", "baz", true, 0); |
| 302 EXPECT_FALSE(!!std::getline(in, line)); | |
| 268 } | 303 } |
| 269 | 304 |
| 270 TEST_F(FileSystemDirURLRequestJobTest, InvalidURL) { | 305 TEST_F(FileSystemDirURLRequestJobTest, InvalidURL) { |
| 271 TestRequest(GURL("filesystem:/foo/bar/baz")); | 306 TestRequest(GURL("filesystem:/foo/bar/baz")); |
| 272 ASSERT_FALSE(request_->is_pending()); | 307 ASSERT_FALSE(request_->is_pending()); |
| 273 EXPECT_TRUE(delegate_->request_failed()); | 308 EXPECT_TRUE(delegate_->request_failed()); |
| 274 ASSERT_FALSE(request_->status().is_success()); | 309 ASSERT_FALSE(request_->status().is_success()); |
| 275 EXPECT_EQ(net::ERR_INVALID_URL, request_->status().error()); | 310 EXPECT_EQ(net::ERR_INVALID_URL, request_->status().error()); |
| 276 } | 311 } |
| 277 | 312 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 314 EXPECT_TRUE(!!std::getline(in, line)); | 349 EXPECT_TRUE(!!std::getline(in, line)); |
| 315 EXPECT_FALSE(!!std::getline(in, line)); | 350 EXPECT_FALSE(!!std::getline(in, line)); |
| 316 | 351 |
| 317 TestRequestWithContext(CreateFileSystemURL("foo"), | 352 TestRequestWithContext(CreateFileSystemURL("foo"), |
| 318 file_system_context.get()); | 353 file_system_context.get()); |
| 319 ASSERT_FALSE(request_->is_pending()); | 354 ASSERT_FALSE(request_->is_pending()); |
| 320 ASSERT_FALSE(request_->status().is_success()); | 355 ASSERT_FALSE(request_->status().is_success()); |
| 321 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request_->status().error()); | 356 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request_->status().error()); |
| 322 } | 357 } |
| 323 | 358 |
| 359 TEST_F(FileSystemDirURLRequestJobTest, AutoMountDirectoryListing) { | |
| 360 base::FilePath mnt_point = temp_dir_.path().AppendASCII("auto_mount_dir"); | |
| 361 ASSERT_TRUE(base::CreateDirectory(mnt_point)); | |
| 362 ASSERT_TRUE(base::CreateDirectory(mnt_point.AppendASCII("foo"))); | |
| 363 ASSERT_EQ(10, | |
| 364 base::WriteFile(mnt_point.AppendASCII("bar"), "1234567890", 10)); | |
| 365 | |
| 366 std::vector<fileapi::URLRequestAutoMountHandler> handlers; | |
| 367 handlers.push_back(base::Bind(&TestAutoMountForURLRequest, mnt_point)); | |
| 368 scoped_refptr<FileSystemContext> file_system_context = | |
| 369 CreateFileSystemContextWithAutoMountersForTesting(NULL, handlers, | |
| 370 temp_dir_.path()); | |
| 371 | |
| 372 TestRequestWithContext(GURL("filesystem:http://automount/external/mnt_name"), | |
| 373 file_system_context); | |
| 374 | |
| 375 ASSERT_FALSE(request_->is_pending()); | |
| 376 EXPECT_EQ(1, delegate_->response_started_count()); | |
| 377 EXPECT_FALSE(delegate_->received_data_before_response()); | |
| 378 EXPECT_GT(delegate_->bytes_received(), 0); | |
| 379 | |
| 380 std::istringstream in(delegate_->data_received()); | |
| 381 std::string line; | |
| 382 EXPECT_TRUE(!!std::getline(in, line)); // |line| contains the temp dir path. | |
| 383 | |
| 384 EXPECT_TRUE(!!std::getline(in, line)); | |
| 385 VerifyListingEntry(line, "bar", "bar", false, 10); | |
| 386 | |
| 387 EXPECT_TRUE(!!std::getline(in, line)); | |
| 388 VerifyListingEntry(line, "foo", "foo", true, 4096); | |
| 389 EXPECT_FALSE(!!std::getline(in, line)); | |
| 390 | |
| 391 ASSERT_TRUE( | |
| 392 fileapi::ExternalMountPoints::GetSystemInstance()->RevokeFileSystem( | |
| 393 kValidExternalMountPoint)); | |
| 394 } | |
| 395 | |
| 396 TEST_F(FileSystemDirURLRequestJobTest, AutoMountInvalidRoot) { | |
| 397 base::FilePath mnt_point = temp_dir_.path().AppendASCII("auto_mount_dir"); | |
| 398 ASSERT_TRUE(base::CreateDirectory(mnt_point)); | |
| 399 | |
| 400 std::vector<fileapi::URLRequestAutoMountHandler> handlers; | |
| 401 handlers.push_back(base::Bind(&TestAutoMountForURLRequest, mnt_point)); | |
| 402 scoped_refptr<FileSystemContext> file_system_context = | |
| 403 CreateFileSystemContextWithAutoMountersForTesting(NULL, handlers, | |
| 404 temp_dir_.path()); | |
| 405 | |
| 406 TestRequestWithContext(GURL("filesystem:http://automount/external/invalid"), | |
| 407 file_system_context); | |
| 408 ASSERT_FALSE(request_->is_pending()); | |
| 409 ASSERT_FALSE(request_->status().is_success()); | |
| 410 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request_->status().error()); | |
| 411 | |
| 412 ASSERT_FALSE( | |
| 413 fileapi::ExternalMountPoints::GetSystemInstance()->RevokeFileSystem( | |
| 414 "invalid")); | |
| 415 } | |
| 416 | |
| 417 TEST_F(FileSystemDirURLRequestJobTest, AutoMountNoHandler) { | |
| 418 base::FilePath mnt_point = temp_dir_.path().AppendASCII("auto_mount_dir"); | |
| 419 ASSERT_TRUE(base::CreateDirectory(mnt_point)); | |
| 420 | |
| 421 std::vector<fileapi::URLRequestAutoMountHandler> handlers; | |
| 422 handlers.push_back(base::Bind(&TestAutoMountForURLRequest, mnt_point)); | |
| 423 scoped_refptr<FileSystemContext> file_system_context = | |
| 424 CreateFileSystemContextWithAutoMountersForTesting(NULL, handlers, | |
| 425 temp_dir_.path()); | |
| 426 | |
| 427 TestRequestWithContext(GURL("filesystem:http://noauto/external/mnt_name"), | |
| 428 file_system_context); | |
| 429 ASSERT_FALSE(request_->is_pending()); | |
| 430 ASSERT_FALSE(request_->status().is_success()); | |
| 431 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request_->status().error()); | |
| 432 | |
| 433 ASSERT_FALSE( | |
| 434 fileapi::ExternalMountPoints::GetSystemInstance()->RevokeFileSystem( | |
| 435 kValidExternalMountPoint)); | |
| 436 } | |
| 437 | |
| 324 } // namespace (anonymous) | 438 } // namespace (anonymous) |
| 325 } // namespace content | 439 } // namespace content |
| OLD | NEW |