1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/fileapi/external_file_url_request_job.h"
#include "base/bind.h"
#include "base/files/file_util.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/run_loop.h"
#include "base/threading/thread.h"
#include "chrome/browser/chromeos/drive/drive_file_stream_reader.h"
#include "chrome/browser/chromeos/drive/drive_integration_service.h"
#include "chrome/browser/chromeos/drive/fake_file_system.h"
#include "chrome/browser/chromeos/drive/file_system_util.h"
#include "chrome/browser/chromeos/drive/test_util.h"
#include "chrome/browser/drive/fake_drive_service.h"
#include "chrome/browser/drive/test_util.h"
#include "chrome/browser/prefs/browser_prefs.h"
#include "chrome/browser/prefs/pref_service_syncable.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/pref_registry/testing_pref_service_syncable.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/common/url_constants.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "content/public/test/test_file_system_options.h"
#include "google_apis/drive/test_util.h"
#include "net/base/request_priority.h"
#include "net/base/test_completion_callback.h"
#include "net/http/http_byte_range.h"
#include "net/url_request/redirect_info.h"
#include "net/url_request/url_request.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_test_util.h"
#include "storage/browser/fileapi/external_mount_points.h"
#include "storage/browser/fileapi/file_system_context.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
namespace chromeos {
namespace {
// A simple URLRequestJobFactory implementation to create
// ExternalFileURLRequestJob.
class TestURLRequestJobFactory : public net::URLRequestJobFactory {
public:
explicit TestURLRequestJobFactory(void* profile_id)
: profile_id_(profile_id) {}
virtual ~TestURLRequestJobFactory() {}
// net::URLRequestJobFactory override:
virtual net::URLRequestJob* MaybeCreateJobWithProtocolHandler(
const std::string& scheme,
net::URLRequest* request,
net::NetworkDelegate* network_delegate) const override {
return new ExternalFileURLRequestJob(
profile_id_, request, network_delegate);
}
net::URLRequestJob* MaybeInterceptRedirect(
net::URLRequest* request,
net::NetworkDelegate* network_delegate,
const GURL& location) const override {
return nullptr;
}
net::URLRequestJob* MaybeInterceptResponse(
net::URLRequest* request,
net::NetworkDelegate* network_delegate) const override {
return nullptr;
}
virtual bool IsHandledProtocol(const std::string& scheme) const override {
return scheme == content::kExternalFileScheme;
}
virtual bool IsHandledURL(const GURL& url) const override {
return url.is_valid() && IsHandledProtocol(url.scheme());
}
virtual bool IsSafeRedirectTarget(const GURL& location) const override {
return true;
}
private:
void* const profile_id_;
DISALLOW_COPY_AND_ASSIGN(TestURLRequestJobFactory);
};
class TestDelegate : public net::TestDelegate {
public:
TestDelegate() {}
const GURL& redirect_url() const { return redirect_url_; }
// net::TestDelegate override.
virtual void OnReceivedRedirect(net::URLRequest* request,
const net::RedirectInfo& redirect_info,
bool* defer_redirect) override {
redirect_url_ = redirect_info.new_url;
net::TestDelegate::OnReceivedRedirect(
request, redirect_info, defer_redirect);
}
private:
GURL redirect_url_;
DISALLOW_COPY_AND_ASSIGN(TestDelegate);
};
} // namespace
class ExternalFileURLRequestJobTest : public testing::Test {
protected:
ExternalFileURLRequestJobTest()
: thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
integration_service_factory_callback_(base::Bind(
&ExternalFileURLRequestJobTest::CreateDriveIntegrationService,
base::Unretained(this))),
fake_file_system_(NULL) {}
virtual ~ExternalFileURLRequestJobTest() {}
virtual void SetUp() override {
// Create a testing profile.
profile_manager_.reset(
new TestingProfileManager(TestingBrowserProcess::GetGlobal()));
ASSERT_TRUE(profile_manager_->SetUp());
Profile* const profile =
profile_manager_->CreateTestingProfile("test-user");
// Create the drive integration service for the profile.
integration_service_factory_scope_.reset(
new drive::DriveIntegrationServiceFactory::ScopedFactoryForTest(
&integration_service_factory_callback_));
drive::DriveIntegrationServiceFactory::GetForProfile(profile);
// Create the URL request job factory.
test_network_delegate_.reset(new net::TestNetworkDelegate);
test_url_request_job_factory_.reset(new TestURLRequestJobFactory(profile));
url_request_context_.reset(new net::URLRequestContext());
url_request_context_->set_job_factory(test_url_request_job_factory_.get());
url_request_context_->set_network_delegate(test_network_delegate_.get());
test_delegate_.reset(new TestDelegate);
}
virtual void TearDown() { profile_manager_.reset(); }
bool ReadDriveFileSync(const base::FilePath& file_path,
std::string* out_content) {
scoped_ptr<base::Thread> worker_thread(
new base::Thread("ReadDriveFileSync"));
if (!worker_thread->Start())
return false;
scoped_ptr<drive::DriveFileStreamReader> reader(
new drive::DriveFileStreamReader(
base::Bind(&ExternalFileURLRequestJobTest::GetFileSystem,
base::Unretained(this)),
worker_thread->message_loop_proxy().get()));
int error = net::ERR_FAILED;
scoped_ptr<drive::ResourceEntry> entry;
{
base::RunLoop run_loop;
reader->Initialize(file_path,
net::HttpByteRange(),
google_apis::test_util::CreateQuitCallback(
&run_loop,
google_apis::test_util::CreateCopyResultCallback(
&error, &entry)));
run_loop.Run();
}
if (error != net::OK || !entry)
return false;
// Read data from the reader.
std::string content;
if (drive::test_util::ReadAllData(reader.get(), &content) != net::OK)
return false;
if (static_cast<size_t>(entry->file_info().size()) != content.size())
return false;
*out_content = content;
return true;
}
scoped_ptr<net::URLRequestContext> url_request_context_;
scoped_ptr<TestDelegate> test_delegate_;
private:
// Create the drive integration service for the |profile|
drive::DriveIntegrationService* CreateDriveIntegrationService(
Profile* profile) {
drive::FakeDriveService* const drive_service = new drive::FakeDriveService;
if (!drive::test_util::SetUpTestEntries(drive_service))
return NULL;
const std::string& drive_mount_name =
drive::util::GetDriveMountPointPath(profile).BaseName().AsUTF8Unsafe();
storage::ExternalMountPoints::GetSystemInstance()->RegisterFileSystem(
drive_mount_name,
storage::kFileSystemTypeDrive,
storage::FileSystemMountOption(),
drive::util::GetDriveMountPointPath(profile));
DCHECK(!fake_file_system_);
fake_file_system_ = new drive::test_util::FakeFileSystem(drive_service);
if (!drive_cache_dir_.CreateUniqueTempDir())
return NULL;
return new drive::DriveIntegrationService(profile,
NULL,
drive_service,
drive_mount_name,
drive_cache_dir_.path(),
fake_file_system_);
}
drive::FileSystemInterface* GetFileSystem() { return fake_file_system_; }
content::TestBrowserThreadBundle thread_bundle_;
drive::DriveIntegrationServiceFactory::FactoryCallback
integration_service_factory_callback_;
scoped_ptr<drive::DriveIntegrationServiceFactory::ScopedFactoryForTest>
integration_service_factory_scope_;
scoped_ptr<drive::DriveIntegrationService> integration_service_;
drive::test_util::FakeFileSystem* fake_file_system_;
scoped_ptr<net::TestNetworkDelegate> test_network_delegate_;
scoped_ptr<TestURLRequestJobFactory> test_url_request_job_factory_;
scoped_ptr<TestingProfileManager> profile_manager_;
base::ScopedTempDir drive_cache_dir_;
scoped_refptr<storage::FileSystemContext> file_system_context_;
};
TEST_F(ExternalFileURLRequestJobTest, NonGetMethod) {
scoped_ptr<net::URLRequest> request(url_request_context_->CreateRequest(
GURL("externalfile:drive-test-user-hash/root/File 1.txt"),
net::DEFAULT_PRIORITY,
test_delegate_.get(),
NULL));
request->set_method("POST"); // Set non "GET" method.
request->Start();
base::RunLoop().Run();
EXPECT_EQ(net::URLRequestStatus::FAILED, request->status().status());
EXPECT_EQ(net::ERR_METHOD_NOT_SUPPORTED, request->status().error());
}
TEST_F(ExternalFileURLRequestJobTest, RegularFile) {
const GURL kTestUrl("externalfile:drive-test-user-hash/root/File 1.txt");
const base::FilePath kTestFilePath("drive/root/File 1.txt");
// For the first time, the file should be fetched from the server.
{
scoped_ptr<net::URLRequest> request(url_request_context_->CreateRequest(
kTestUrl, net::DEFAULT_PRIORITY, test_delegate_.get(), NULL));
request->Start();
base::RunLoop().Run();
EXPECT_EQ(net::URLRequestStatus::SUCCESS, request->status().status());
// It looks weird, but the mime type for the "File 1.txt" is "audio/mpeg"
// on the server.
std::string mime_type;
request->GetMimeType(&mime_type);
EXPECT_EQ("audio/mpeg", mime_type);
// Reading file must be done after |request| runs, otherwise
// it'll create a local cache file, and we cannot test correctly.
std::string expected_data;
ASSERT_TRUE(ReadDriveFileSync(kTestFilePath, &expected_data));
EXPECT_EQ(expected_data, test_delegate_->data_received());
}
// For the second time, the locally cached file should be used.
// The caching emulation is done by FakeFileSystem.
{
test_delegate_.reset(new TestDelegate);
scoped_ptr<net::URLRequest> request(url_request_context_->CreateRequest(
GURL("externalfile:drive-test-user-hash/root/File 1.txt"),
net::DEFAULT_PRIORITY,
test_delegate_.get(),
NULL));
request->Start();
base::RunLoop().Run();
EXPECT_EQ(net::URLRequestStatus::SUCCESS, request->status().status());
std::string mime_type;
request->GetMimeType(&mime_type);
EXPECT_EQ("audio/mpeg", mime_type);
std::string expected_data;
ASSERT_TRUE(ReadDriveFileSync(kTestFilePath, &expected_data));
EXPECT_EQ(expected_data, test_delegate_->data_received());
}
}
TEST_F(ExternalFileURLRequestJobTest, HostedDocument) {
// Open a gdoc file.
test_delegate_->set_quit_on_redirect(true);
scoped_ptr<net::URLRequest> request(url_request_context_->CreateRequest(
GURL(
"externalfile:drive-test-user-hash/root/Document 1 "
"excludeDir-test.gdoc"),
net::DEFAULT_PRIORITY,
test_delegate_.get(),
NULL));
request->Start();
base::RunLoop().Run();
EXPECT_EQ(net::URLRequestStatus::SUCCESS, request->status().status());
// Make sure that a hosted document triggers redirection.
EXPECT_TRUE(request->is_redirecting());
EXPECT_TRUE(test_delegate_->redirect_url().is_valid());
}
TEST_F(ExternalFileURLRequestJobTest, RootDirectory) {
scoped_ptr<net::URLRequest> request(url_request_context_->CreateRequest(
GURL("externalfile:drive-test-user-hash/root"),
net::DEFAULT_PRIORITY,
test_delegate_.get(),
NULL));
request->Start();
base::RunLoop().Run();
EXPECT_EQ(net::URLRequestStatus::FAILED, request->status().status());
EXPECT_EQ(net::ERR_FAILED, request->status().error());
}
TEST_F(ExternalFileURLRequestJobTest, Directory) {
scoped_ptr<net::URLRequest> request(url_request_context_->CreateRequest(
GURL("externalfile:drive-test-user-hash/root/Directory 1"),
net::DEFAULT_PRIORITY,
test_delegate_.get(),
NULL));
request->Start();
base::RunLoop().Run();
EXPECT_EQ(net::URLRequestStatus::FAILED, request->status().status());
EXPECT_EQ(net::ERR_FAILED, request->status().error());
}
TEST_F(ExternalFileURLRequestJobTest, NonExistingFile) {
scoped_ptr<net::URLRequest> request(url_request_context_->CreateRequest(
GURL("externalfile:drive-test-user-hash/root/non-existing-file.txt"),
net::DEFAULT_PRIORITY,
test_delegate_.get(),
NULL));
request->Start();
base::RunLoop().Run();
EXPECT_EQ(net::URLRequestStatus::FAILED, request->status().status());
EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request->status().error());
}
TEST_F(ExternalFileURLRequestJobTest, WrongFormat) {
scoped_ptr<net::URLRequest> request(
url_request_context_->CreateRequest(GURL("externalfile:"),
net::DEFAULT_PRIORITY,
test_delegate_.get(),
NULL));
request->Start();
base::RunLoop().Run();
EXPECT_EQ(net::URLRequestStatus::FAILED, request->status().status());
EXPECT_EQ(net::ERR_INVALID_URL, request->status().error());
}
TEST_F(ExternalFileURLRequestJobTest, Cancel) {
scoped_ptr<net::URLRequest> request(url_request_context_->CreateRequest(
GURL("externalfile:drive-test-user-hash/root/File 1.txt"),
net::DEFAULT_PRIORITY,
test_delegate_.get(),
NULL));
// Start the request, and cancel it immediately after it.
request->Start();
request->Cancel();
base::RunLoop().Run();
EXPECT_EQ(net::URLRequestStatus::CANCELED, request->status().status());
}
TEST_F(ExternalFileURLRequestJobTest, RangeHeader) {
const GURL kTestUrl("externalfile:drive-test-user-hash/root/File 1.txt");
const base::FilePath kTestFilePath("drive/root/File 1.txt");
scoped_ptr<net::URLRequest> request(url_request_context_->CreateRequest(
kTestUrl, net::DEFAULT_PRIORITY, test_delegate_.get(), NULL));
// Set range header.
request->SetExtraRequestHeaderByName(
"Range", "bytes=3-5", false /* overwrite */);
request->Start();
base::RunLoop().Run();
EXPECT_EQ(net::URLRequestStatus::SUCCESS, request->status().status());
// Reading file must be done after |request| runs, otherwise
// it'll create a local cache file, and we cannot test correctly.
std::string expected_data;
ASSERT_TRUE(ReadDriveFileSync(kTestFilePath, &expected_data));
EXPECT_EQ(expected_data.substr(3, 3), test_delegate_->data_received());
}
TEST_F(ExternalFileURLRequestJobTest, WrongRangeHeader) {
const GURL kTestUrl("externalfile:drive-test-user-hash/root/File 1.txt");
scoped_ptr<net::URLRequest> request(url_request_context_->CreateRequest(
kTestUrl, net::DEFAULT_PRIORITY, test_delegate_.get(), NULL));
// Set range header.
request->SetExtraRequestHeaderByName(
"Range", "Wrong Range Header Value", false /* overwrite */);
request->Start();
base::RunLoop().Run();
EXPECT_EQ(net::URLRequestStatus::FAILED, request->status().status());
EXPECT_EQ(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE, request->status().error());
}
} // namespace chromeos
|