| 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 "chrome/browser/chromeos/drive/drive_url_request_job.h" | 5 #include "chrome/browser/chromeos/drive/drive_url_request_job.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/files/file_util.h" |
| 8 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 9 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
| 11 #include "base/sequenced_task_runner.h" | |
| 12 #include "base/threading/sequenced_worker_pool.h" | |
| 13 #include "base/threading/thread.h" | 12 #include "base/threading/thread.h" |
| 14 #include "chrome/browser/chromeos/drive/drive_file_stream_reader.h" | 13 #include "chrome/browser/chromeos/drive/drive_file_stream_reader.h" |
| 14 #include "chrome/browser/chromeos/drive/drive_integration_service.h" |
| 15 #include "chrome/browser/chromeos/drive/fake_file_system.h" | 15 #include "chrome/browser/chromeos/drive/fake_file_system.h" |
| 16 #include "chrome/browser/chromeos/drive/file_system_util.h" | 16 #include "chrome/browser/chromeos/drive/file_system_util.h" |
| 17 #include "chrome/browser/chromeos/drive/test_util.h" | 17 #include "chrome/browser/chromeos/drive/test_util.h" |
| 18 #include "chrome/browser/drive/fake_drive_service.h" | 18 #include "chrome/browser/drive/fake_drive_service.h" |
| 19 #include "chrome/browser/drive/test_util.h" | 19 #include "chrome/browser/drive/test_util.h" |
| 20 #include "chrome/browser/prefs/browser_prefs.h" |
| 21 #include "chrome/browser/prefs/pref_service_syncable.h" |
| 22 #include "chrome/browser/profiles/profile_manager.h" |
| 20 #include "chrome/common/url_constants.h" | 23 #include "chrome/common/url_constants.h" |
| 24 #include "chrome/test/base/testing_browser_process.h" |
| 25 #include "chrome/test/base/testing_profile.h" |
| 26 #include "chrome/test/base/testing_profile_manager.h" |
| 27 #include "components/pref_registry/pref_registry_syncable.h" |
| 28 #include "components/pref_registry/testing_pref_service_syncable.h" |
| 21 #include "content/public/browser/browser_thread.h" | 29 #include "content/public/browser/browser_thread.h" |
| 22 #include "content/public/test/test_browser_thread_bundle.h" | 30 #include "content/public/test/test_browser_thread_bundle.h" |
| 31 #include "content/public/test/test_file_system_options.h" |
| 23 #include "google_apis/drive/test_util.h" | 32 #include "google_apis/drive/test_util.h" |
| 24 #include "net/base/request_priority.h" | 33 #include "net/base/request_priority.h" |
| 25 #include "net/base/test_completion_callback.h" | 34 #include "net/base/test_completion_callback.h" |
| 26 #include "net/http/http_byte_range.h" | 35 #include "net/http/http_byte_range.h" |
| 27 #include "net/url_request/redirect_info.h" | 36 #include "net/url_request/redirect_info.h" |
| 28 #include "net/url_request/url_request.h" | 37 #include "net/url_request/url_request.h" |
| 29 #include "net/url_request/url_request_context.h" | 38 #include "net/url_request/url_request_context.h" |
| 30 #include "net/url_request/url_request_test_util.h" | 39 #include "net/url_request/url_request_test_util.h" |
| 40 #include "storage/browser/fileapi/external_mount_points.h" |
| 41 #include "storage/browser/fileapi/file_system_context.h" |
| 31 #include "testing/gtest/include/gtest/gtest.h" | 42 #include "testing/gtest/include/gtest/gtest.h" |
| 32 #include "url/gurl.h" | 43 #include "url/gurl.h" |
| 33 | 44 |
| 34 namespace drive { | 45 namespace drive { |
| 35 namespace { | 46 namespace { |
| 36 | 47 |
| 37 // A simple URLRequestJobFactory implementation to create DriveURLRequestJob. | 48 // A simple URLRequestJobFactory implementation to create DriveURLRequestJob. |
| 38 class TestURLRequestJobFactory : public net::URLRequestJobFactory { | 49 class TestURLRequestJobFactory : public net::URLRequestJobFactory { |
| 39 public: | 50 public: |
| 40 TestURLRequestJobFactory( | 51 explicit TestURLRequestJobFactory(void* profile_id) |
| 41 const DriveURLRequestJob::FileSystemGetter& file_system_getter, | 52 : profile_id_(profile_id) {} |
| 42 base::SequencedTaskRunner* sequenced_task_runner) | |
| 43 : file_system_getter_(file_system_getter), | |
| 44 sequenced_task_runner_(sequenced_task_runner) { | |
| 45 } | |
| 46 | 53 |
| 47 virtual ~TestURLRequestJobFactory() {} | 54 virtual ~TestURLRequestJobFactory() {} |
| 48 | 55 |
| 49 // net::URLRequestJobFactory override: | 56 // net::URLRequestJobFactory override: |
| 50 virtual net::URLRequestJob* MaybeCreateJobWithProtocolHandler( | 57 virtual net::URLRequestJob* MaybeCreateJobWithProtocolHandler( |
| 51 const std::string& scheme, | 58 const std::string& scheme, |
| 52 net::URLRequest* request, | 59 net::URLRequest* request, |
| 53 net::NetworkDelegate* network_delegate) const OVERRIDE { | 60 net::NetworkDelegate* network_delegate) const OVERRIDE { |
| 54 return new DriveURLRequestJob(file_system_getter_, | 61 return new DriveURLRequestJob(profile_id_, request, network_delegate); |
| 55 sequenced_task_runner_.get(), | |
| 56 request, | |
| 57 network_delegate); | |
| 58 } | 62 } |
| 59 | 63 |
| 60 virtual bool IsHandledProtocol(const std::string& scheme) const OVERRIDE { | 64 virtual bool IsHandledProtocol(const std::string& scheme) const OVERRIDE { |
| 61 return scheme == chrome::kDriveScheme; | 65 return scheme == chrome::kDriveScheme; |
| 62 } | 66 } |
| 63 | 67 |
| 64 virtual bool IsHandledURL(const GURL& url) const OVERRIDE { | 68 virtual bool IsHandledURL(const GURL& url) const OVERRIDE { |
| 65 return url.is_valid() && IsHandledProtocol(url.scheme()); | 69 return url.is_valid() && IsHandledProtocol(url.scheme()); |
| 66 } | 70 } |
| 67 | 71 |
| 68 virtual bool IsSafeRedirectTarget(const GURL& location) const OVERRIDE { | 72 virtual bool IsSafeRedirectTarget(const GURL& location) const OVERRIDE { |
| 69 return true; | 73 return true; |
| 70 } | 74 } |
| 71 | 75 |
| 72 private: | 76 private: |
| 73 const DriveURLRequestJob::FileSystemGetter file_system_getter_; | 77 void* const profile_id_; |
| 74 scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner_; | |
| 75 | |
| 76 DISALLOW_COPY_AND_ASSIGN(TestURLRequestJobFactory); | 78 DISALLOW_COPY_AND_ASSIGN(TestURLRequestJobFactory); |
| 77 }; | 79 }; |
| 78 | 80 |
| 79 class TestDelegate : public net::TestDelegate { | 81 class TestDelegate : public net::TestDelegate { |
| 80 public: | 82 public: |
| 81 TestDelegate() {} | 83 TestDelegate() {} |
| 82 | 84 |
| 83 const GURL& redirect_url() const { return redirect_url_; } | 85 const GURL& redirect_url() const { return redirect_url_; } |
| 84 | 86 |
| 85 // net::TestDelegate override. | 87 // net::TestDelegate override. |
| 86 virtual void OnReceivedRedirect(net::URLRequest* request, | 88 virtual void OnReceivedRedirect(net::URLRequest* request, |
| 87 const net::RedirectInfo& redirect_info, | 89 const net::RedirectInfo& redirect_info, |
| 88 bool* defer_redirect) OVERRIDE{ | 90 bool* defer_redirect) OVERRIDE { |
| 89 redirect_url_ = redirect_info.new_url; | 91 redirect_url_ = redirect_info.new_url; |
| 90 net::TestDelegate::OnReceivedRedirect( | 92 net::TestDelegate::OnReceivedRedirect( |
| 91 request, redirect_info, defer_redirect); | 93 request, redirect_info, defer_redirect); |
| 92 } | 94 } |
| 93 | 95 |
| 94 private: | 96 private: |
| 95 GURL redirect_url_; | 97 GURL redirect_url_; |
| 96 | 98 |
| 97 DISALLOW_COPY_AND_ASSIGN(TestDelegate); | 99 DISALLOW_COPY_AND_ASSIGN(TestDelegate); |
| 98 }; | 100 }; |
| 99 | 101 |
| 100 } // namespace | 102 } // namespace |
| 101 | 103 |
| 102 class DriveURLRequestJobTest : public testing::Test { | 104 class DriveURLRequestJobTest : public testing::Test { |
| 103 protected: | 105 protected: |
| 104 DriveURLRequestJobTest() | 106 DriveURLRequestJobTest() |
| 105 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { | 107 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), |
| 106 } | 108 integration_service_factory_callback_( |
| 109 base::Bind(&DriveURLRequestJobTest::CreateDriveIntegrationService, |
| 110 base::Unretained(this))), |
| 111 fake_file_system_(NULL) {} |
| 107 | 112 |
| 108 virtual ~DriveURLRequestJobTest() { | 113 virtual ~DriveURLRequestJobTest() { |
| 109 } | 114 } |
| 110 | 115 |
| 111 virtual void SetUp() OVERRIDE { | 116 virtual void SetUp() OVERRIDE { |
| 112 // Initialize FakeDriveService. | 117 // Create a testing profile. |
| 113 fake_drive_service_.reset(new FakeDriveService); | 118 profile_manager_.reset( |
| 114 ASSERT_TRUE(test_util::SetUpTestEntries(fake_drive_service_.get())); | 119 new TestingProfileManager(TestingBrowserProcess::GetGlobal())); |
| 120 ASSERT_TRUE(profile_manager_->SetUp()); |
| 121 Profile* const profile = |
| 122 profile_manager_->CreateTestingProfile("test-user"); |
| 115 | 123 |
| 116 // Initialize FakeFileSystem. | 124 // Create the drive integration service for the profile. |
| 117 fake_file_system_.reset( | 125 integration_service_factory_scope_.reset( |
| 118 new test_util::FakeFileSystem(fake_drive_service_.get())); | 126 new DriveIntegrationServiceFactory::ScopedFactoryForTest( |
| 127 &integration_service_factory_callback_)); |
| 128 DriveIntegrationServiceFactory::GetForProfile(profile); |
| 119 | 129 |
| 120 scoped_refptr<base::SequencedWorkerPool> blocking_pool = | 130 // Create the URL request job factory. |
| 121 content::BrowserThread::GetBlockingPool(); | |
| 122 test_network_delegate_.reset(new net::TestNetworkDelegate); | 131 test_network_delegate_.reset(new net::TestNetworkDelegate); |
| 123 test_url_request_job_factory_.reset(new TestURLRequestJobFactory( | 132 test_url_request_job_factory_.reset(new TestURLRequestJobFactory(profile)); |
| 124 base::Bind(&DriveURLRequestJobTest::GetFileSystem, | |
| 125 base::Unretained(this)), | |
| 126 blocking_pool->GetSequencedTaskRunner( | |
| 127 blocking_pool->GetSequenceToken()).get())); | |
| 128 url_request_context_.reset(new net::URLRequestContext()); | 133 url_request_context_.reset(new net::URLRequestContext()); |
| 129 url_request_context_->set_job_factory(test_url_request_job_factory_.get()); | 134 url_request_context_->set_job_factory(test_url_request_job_factory_.get()); |
| 130 url_request_context_->set_network_delegate(test_network_delegate_.get()); | 135 url_request_context_->set_network_delegate(test_network_delegate_.get()); |
| 131 test_delegate_.reset(new TestDelegate); | 136 test_delegate_.reset(new TestDelegate); |
| 132 } | 137 } |
| 133 | 138 |
| 134 FileSystemInterface* GetFileSystem() { | 139 virtual void TearDown() { profile_manager_.reset(); } |
| 135 return fake_file_system_.get(); | |
| 136 } | |
| 137 | 140 |
| 138 bool ReadDriveFileSync( | 141 bool ReadDriveFileSync( |
| 139 const base::FilePath& file_path, std::string* out_content) { | 142 const base::FilePath& file_path, std::string* out_content) { |
| 140 scoped_ptr<base::Thread> worker_thread( | 143 scoped_ptr<base::Thread> worker_thread( |
| 141 new base::Thread("ReadDriveFileSync")); | 144 new base::Thread("ReadDriveFileSync")); |
| 142 if (!worker_thread->Start()) | 145 if (!worker_thread->Start()) |
| 143 return false; | 146 return false; |
| 144 | 147 |
| 145 scoped_ptr<DriveFileStreamReader> reader(new DriveFileStreamReader( | 148 scoped_ptr<DriveFileStreamReader> reader(new DriveFileStreamReader( |
| 146 base::Bind(&DriveURLRequestJobTest::GetFileSystem, | 149 base::Bind(&DriveURLRequestJobTest::GetFileSystem, |
| (...skipping 20 matching lines...) Expand all Loading... |
| 167 if (test_util::ReadAllData(reader.get(), &content) != net::OK) | 170 if (test_util::ReadAllData(reader.get(), &content) != net::OK) |
| 168 return false; | 171 return false; |
| 169 | 172 |
| 170 if (static_cast<size_t>(entry->file_info().size()) != content.size()) | 173 if (static_cast<size_t>(entry->file_info().size()) != content.size()) |
| 171 return false; | 174 return false; |
| 172 | 175 |
| 173 *out_content = content; | 176 *out_content = content; |
| 174 return true; | 177 return true; |
| 175 } | 178 } |
| 176 | 179 |
| 180 scoped_ptr<net::URLRequestContext> url_request_context_; |
| 181 scoped_ptr<TestDelegate> test_delegate_; |
| 182 |
| 183 private: |
| 184 // Create the drive integration service for the |profile| |
| 185 DriveIntegrationService* CreateDriveIntegrationService(Profile* profile) { |
| 186 FakeDriveService* const drive_service = new FakeDriveService; |
| 187 if (!test_util::SetUpTestEntries(drive_service)) |
| 188 return NULL; |
| 189 |
| 190 const std::string& drive_mount_name = |
| 191 util::GetDriveMountPointPath(profile).BaseName().AsUTF8Unsafe(); |
| 192 storage::ExternalMountPoints::GetSystemInstance()->RegisterFileSystem( |
| 193 drive_mount_name, |
| 194 storage::kFileSystemTypeDrive, |
| 195 storage::FileSystemMountOption(), |
| 196 util::GetDriveMountPointPath(profile)); |
| 197 DCHECK(!fake_file_system_); |
| 198 fake_file_system_ = new test_util::FakeFileSystem(drive_service); |
| 199 if (!drive_cache_dir_.CreateUniqueTempDir()) |
| 200 return NULL; |
| 201 return new drive::DriveIntegrationService(profile, |
| 202 NULL, |
| 203 drive_service, |
| 204 drive_mount_name, |
| 205 drive_cache_dir_.path(), |
| 206 fake_file_system_); |
| 207 } |
| 208 |
| 209 FileSystemInterface* GetFileSystem() { return fake_file_system_; } |
| 210 |
| 177 content::TestBrowserThreadBundle thread_bundle_; | 211 content::TestBrowserThreadBundle thread_bundle_; |
| 178 | 212 DriveIntegrationServiceFactory::FactoryCallback |
| 179 scoped_ptr<FakeDriveService> fake_drive_service_; | 213 integration_service_factory_callback_; |
| 180 scoped_ptr<test_util::FakeFileSystem> fake_file_system_; | 214 scoped_ptr<DriveIntegrationServiceFactory::ScopedFactoryForTest> |
| 215 integration_service_factory_scope_; |
| 216 scoped_ptr<DriveIntegrationService> integration_service_; |
| 217 test_util::FakeFileSystem* fake_file_system_; |
| 181 | 218 |
| 182 scoped_ptr<net::TestNetworkDelegate> test_network_delegate_; | 219 scoped_ptr<net::TestNetworkDelegate> test_network_delegate_; |
| 183 scoped_ptr<TestURLRequestJobFactory> test_url_request_job_factory_; | 220 scoped_ptr<TestURLRequestJobFactory> test_url_request_job_factory_; |
| 184 scoped_ptr<net::URLRequestContext> url_request_context_; | 221 |
| 185 scoped_ptr<TestDelegate> test_delegate_; | 222 scoped_ptr<TestingProfileManager> profile_manager_; |
| 223 base::ScopedTempDir drive_cache_dir_; |
| 224 scoped_refptr<storage::FileSystemContext> file_system_context_; |
| 186 }; | 225 }; |
| 187 | 226 |
| 188 TEST_F(DriveURLRequestJobTest, NonGetMethod) { | 227 TEST_F(DriveURLRequestJobTest, NonGetMethod) { |
| 189 scoped_ptr<net::URLRequest> request(url_request_context_->CreateRequest( | 228 scoped_ptr<net::URLRequest> request(url_request_context_->CreateRequest( |
| 190 GURL("drive:drive/root/File 1.txt"), | 229 GURL("drive:drive/root/File 1.txt"), |
| 191 net::DEFAULT_PRIORITY, | 230 net::DEFAULT_PRIORITY, |
| 192 test_delegate_.get(), | 231 test_delegate_.get(), |
| 193 NULL)); | 232 NULL)); |
| 194 request->set_method("POST"); // Set non "GET" method. | 233 request->set_method("POST"); // Set non "GET" method. |
| 195 request->Start(); | 234 request->Start(); |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 383 "Range", "Wrong Range Header Value", false /* overwrite */); | 422 "Range", "Wrong Range Header Value", false /* overwrite */); |
| 384 request->Start(); | 423 request->Start(); |
| 385 | 424 |
| 386 base::RunLoop().Run(); | 425 base::RunLoop().Run(); |
| 387 | 426 |
| 388 EXPECT_EQ(net::URLRequestStatus::FAILED, request->status().status()); | 427 EXPECT_EQ(net::URLRequestStatus::FAILED, request->status().status()); |
| 389 EXPECT_EQ(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE, request->status().error()); | 428 EXPECT_EQ(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE, request->status().error()); |
| 390 } | 429 } |
| 391 | 430 |
| 392 } // namespace drive | 431 } // namespace drive |
| OLD | NEW |