Remove PlatformFile from profile_browsertest
[chromium-blink-merge.git] / content / browser / fileapi / file_system_url_request_job_unittest.cc
blobdf444facd2f6a2dfac0c6b940bba3505f7c08a34
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
3 // found in the LICENSE file.
5 #include "webkit/browser/fileapi/file_system_url_request_job.h"
7 #include <string>
9 #include "base/bind.h"
10 #include "base/file_util.h"
11 #include "base/files/file_path.h"
12 #include "base/files/scoped_temp_dir.h"
13 #include "base/format_macros.h"
14 #include "base/memory/scoped_vector.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/message_loop/message_loop.h"
17 #include "base/message_loop/message_loop_proxy.h"
18 #include "base/platform_file.h"
19 #include "base/rand_util.h"
20 #include "base/run_loop.h"
21 #include "base/strings/string_piece.h"
22 #include "base/strings/stringprintf.h"
23 #include "base/strings/utf_string_conversions.h"
24 #include "content/public/test/async_file_test_helper.h"
25 #include "content/public/test/test_file_system_backend.h"
26 #include "content/public/test/test_file_system_context.h"
27 #include "net/base/load_flags.h"
28 #include "net/base/mime_util.h"
29 #include "net/base/net_errors.h"
30 #include "net/base/net_util.h"
31 #include "net/base/request_priority.h"
32 #include "net/http/http_byte_range.h"
33 #include "net/http/http_request_headers.h"
34 #include "net/url_request/url_request.h"
35 #include "net/url_request/url_request_context.h"
36 #include "net/url_request/url_request_test_util.h"
37 #include "testing/gtest/include/gtest/gtest.h"
38 #include "webkit/browser/fileapi/external_mount_points.h"
39 #include "webkit/browser/fileapi/file_system_context.h"
40 #include "webkit/browser/fileapi/file_system_file_util.h"
42 using content::AsyncFileTestHelper;
43 using fileapi::FileSystemContext;
44 using fileapi::FileSystemURL;
45 using fileapi::FileSystemURLRequestJob;
47 namespace content {
48 namespace {
50 // We always use the TEMPORARY FileSystem in this test.
51 const char kFileSystemURLPrefix[] = "filesystem:http://remote/temporary/";
52 const char kTestFileData[] = "0123456789";
54 void FillBuffer(char* buffer, size_t len) {
55 base::RandBytes(buffer, len);
58 const char kValidExternalMountPoint[] = "mnt_name";
60 // An auto mounter that will try to mount anything for |storage_domain| =
61 // "automount", but will only succeed for the mount point "mnt_name".
62 bool TestAutoMountForURLRequest(
63 const net::URLRequest* /*url_request*/,
64 const fileapi::FileSystemURL& filesystem_url,
65 const std::string& storage_domain,
66 const base::Callback<void(base::File::Error result)>& callback) {
67 if (storage_domain != "automount")
68 return false;
69 std::vector<base::FilePath::StringType> components;
70 filesystem_url.path().GetComponents(&components);
71 std::string mount_point = base::FilePath(components[0]).AsUTF8Unsafe();
73 if (mount_point == kValidExternalMountPoint) {
74 fileapi::ExternalMountPoints::GetSystemInstance()->RegisterFileSystem(
75 kValidExternalMountPoint, fileapi::kFileSystemTypeTest,
76 fileapi::FileSystemMountOption(), base::FilePath());
77 callback.Run(base::File::FILE_OK);
78 } else {
79 callback.Run(base::File::FILE_ERROR_NOT_FOUND);
81 return true;
84 } // namespace
86 class FileSystemURLRequestJobTest : public testing::Test {
87 protected:
88 FileSystemURLRequestJobTest() : weak_factory_(this) {
91 virtual void SetUp() OVERRIDE {
92 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
94 // We use the main thread so that we can get the root path synchronously.
95 // TODO(adamk): Run this on the FILE thread we've created as well.
96 file_system_context_ =
97 CreateFileSystemContextForTesting(NULL, temp_dir_.path());
99 file_system_context_->OpenFileSystem(
100 GURL("http://remote/"), fileapi::kFileSystemTypeTemporary,
101 fileapi::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT,
102 base::Bind(&FileSystemURLRequestJobTest::OnOpenFileSystem,
103 weak_factory_.GetWeakPtr()));
104 base::RunLoop().RunUntilIdle();
106 net::URLRequest::Deprecated::RegisterProtocolFactory(
107 "filesystem", &FileSystemURLRequestJobFactory);
110 virtual void TearDown() OVERRIDE {
111 net::URLRequest::Deprecated::RegisterProtocolFactory("filesystem", NULL);
112 ClearUnusedJob();
113 if (pending_job_.get()) {
114 pending_job_->Kill();
115 pending_job_ = NULL;
117 // FileReader posts a task to close the file in destructor.
118 base::RunLoop().RunUntilIdle();
121 void SetUpAutoMountContext() {
122 base::FilePath mnt_point = temp_dir_.path().AppendASCII("auto_mount_dir");
123 ASSERT_TRUE(base::CreateDirectory(mnt_point));
125 ScopedVector<fileapi::FileSystemBackend> additional_providers;
126 additional_providers.push_back(new TestFileSystemBackend(
127 base::MessageLoopProxy::current().get(), mnt_point));
129 std::vector<fileapi::URLRequestAutoMountHandler> handlers;
130 handlers.push_back(base::Bind(&TestAutoMountForURLRequest));
132 file_system_context_ = CreateFileSystemContextWithAutoMountersForTesting(
133 NULL, additional_providers.Pass(), handlers, temp_dir_.path());
135 ASSERT_EQ(static_cast<int>(sizeof(kTestFileData)) - 1,
136 base::WriteFile(mnt_point.AppendASCII("foo"), kTestFileData,
137 sizeof(kTestFileData) - 1));
140 void OnOpenFileSystem(const GURL& root_url,
141 const std::string& name,
142 base::File::Error result) {
143 ASSERT_EQ(base::File::FILE_OK, result);
146 void TestRequestHelper(const GURL& url,
147 const net::HttpRequestHeaders* headers,
148 bool run_to_completion,
149 FileSystemContext* file_system_context) {
150 delegate_.reset(new net::TestDelegate());
151 // Make delegate_ exit the MessageLoop when the request is done.
152 delegate_->set_quit_on_complete(true);
153 delegate_->set_quit_on_redirect(true);
154 request_ = empty_context_.CreateRequest(
155 url, net::DEFAULT_PRIORITY, delegate_.get(), NULL);
156 if (headers)
157 request_->SetExtraRequestHeaders(*headers);
158 ASSERT_TRUE(!job_);
159 job_ = new FileSystemURLRequestJob(
160 request_.get(), NULL, url.GetOrigin().host(), file_system_context);
161 pending_job_ = job_;
163 request_->Start();
164 ASSERT_TRUE(request_->is_pending()); // verify that we're starting async
165 if (run_to_completion)
166 base::MessageLoop::current()->Run();
169 void TestRequest(const GURL& url) {
170 TestRequestHelper(url, NULL, true, file_system_context_.get());
173 void TestRequestWithContext(const GURL& url,
174 FileSystemContext* file_system_context) {
175 TestRequestHelper(url, NULL, true, file_system_context);
178 void TestRequestWithHeaders(const GURL& url,
179 const net::HttpRequestHeaders* headers) {
180 TestRequestHelper(url, headers, true, file_system_context_.get());
183 void TestRequestNoRun(const GURL& url) {
184 TestRequestHelper(url, NULL, false, file_system_context_.get());
187 void CreateDirectory(const base::StringPiece& dir_name) {
188 FileSystemURL url = file_system_context_->CreateCrackedFileSystemURL(
189 GURL("http://remote"),
190 fileapi::kFileSystemTypeTemporary,
191 base::FilePath().AppendASCII(dir_name));
192 ASSERT_EQ(base::File::FILE_OK, AsyncFileTestHelper::CreateDirectory(
193 file_system_context_, url));
196 void WriteFile(const base::StringPiece& file_name,
197 const char* buf, int buf_size) {
198 FileSystemURL url = file_system_context_->CreateCrackedFileSystemURL(
199 GURL("http://remote"),
200 fileapi::kFileSystemTypeTemporary,
201 base::FilePath().AppendASCII(file_name));
202 ASSERT_EQ(base::File::FILE_OK,
203 AsyncFileTestHelper::CreateFileWithData(
204 file_system_context_, url, buf, buf_size));
207 GURL CreateFileSystemURL(const std::string& path) {
208 return GURL(kFileSystemURLPrefix + path);
211 static net::URLRequestJob* FileSystemURLRequestJobFactory(
212 net::URLRequest* request,
213 net::NetworkDelegate* network_delegate,
214 const std::string& scheme) {
215 DCHECK(job_);
216 net::URLRequestJob* temp = job_;
217 job_ = NULL;
218 return temp;
221 static void ClearUnusedJob() {
222 if (job_) {
223 scoped_refptr<net::URLRequestJob> deleter = job_;
224 job_ = NULL;
228 // Put the message loop at the top, so that it's the last thing deleted.
229 base::MessageLoopForIO message_loop_;
231 base::ScopedTempDir temp_dir_;
232 scoped_refptr<fileapi::FileSystemContext> file_system_context_;
233 base::WeakPtrFactory<FileSystemURLRequestJobTest> weak_factory_;
235 net::URLRequestContext empty_context_;
237 // NOTE: order matters, request must die before delegate
238 scoped_ptr<net::TestDelegate> delegate_;
239 scoped_ptr<net::URLRequest> request_;
241 scoped_refptr<net::URLRequestJob> pending_job_;
242 static net::URLRequestJob* job_;
245 // static
246 net::URLRequestJob* FileSystemURLRequestJobTest::job_ = NULL;
248 namespace {
250 TEST_F(FileSystemURLRequestJobTest, FileTest) {
251 WriteFile("file1.dat", kTestFileData, arraysize(kTestFileData) - 1);
252 TestRequest(CreateFileSystemURL("file1.dat"));
254 ASSERT_FALSE(request_->is_pending());
255 EXPECT_EQ(1, delegate_->response_started_count());
256 EXPECT_FALSE(delegate_->received_data_before_response());
257 EXPECT_EQ(kTestFileData, delegate_->data_received());
258 EXPECT_EQ(200, request_->GetResponseCode());
259 std::string cache_control;
260 request_->GetResponseHeaderByName("cache-control", &cache_control);
261 EXPECT_EQ("no-cache", cache_control);
264 TEST_F(FileSystemURLRequestJobTest, FileTestFullSpecifiedRange) {
265 const size_t buffer_size = 4000;
266 scoped_ptr<char[]> buffer(new char[buffer_size]);
267 FillBuffer(buffer.get(), buffer_size);
268 WriteFile("bigfile", buffer.get(), buffer_size);
270 const size_t first_byte_position = 500;
271 const size_t last_byte_position = buffer_size - first_byte_position;
272 std::string partial_buffer_string(buffer.get() + first_byte_position,
273 buffer.get() + last_byte_position + 1);
275 net::HttpRequestHeaders headers;
276 headers.SetHeader(
277 net::HttpRequestHeaders::kRange,
278 net::HttpByteRange::Bounded(
279 first_byte_position, last_byte_position).GetHeaderValue());
280 TestRequestWithHeaders(CreateFileSystemURL("bigfile"), &headers);
282 ASSERT_FALSE(request_->is_pending());
283 EXPECT_EQ(1, delegate_->response_started_count());
284 EXPECT_FALSE(delegate_->received_data_before_response());
285 EXPECT_TRUE(partial_buffer_string == delegate_->data_received());
288 TEST_F(FileSystemURLRequestJobTest, FileTestHalfSpecifiedRange) {
289 const size_t buffer_size = 4000;
290 scoped_ptr<char[]> buffer(new char[buffer_size]);
291 FillBuffer(buffer.get(), buffer_size);
292 WriteFile("bigfile", buffer.get(), buffer_size);
294 const size_t first_byte_position = 500;
295 std::string partial_buffer_string(buffer.get() + first_byte_position,
296 buffer.get() + buffer_size);
298 net::HttpRequestHeaders headers;
299 headers.SetHeader(
300 net::HttpRequestHeaders::kRange,
301 net::HttpByteRange::RightUnbounded(first_byte_position).GetHeaderValue());
302 TestRequestWithHeaders(CreateFileSystemURL("bigfile"), &headers);
303 ASSERT_FALSE(request_->is_pending());
304 EXPECT_EQ(1, delegate_->response_started_count());
305 EXPECT_FALSE(delegate_->received_data_before_response());
306 // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed.
307 EXPECT_TRUE(partial_buffer_string == delegate_->data_received());
310 TEST_F(FileSystemURLRequestJobTest, FileTestMultipleRangesNotSupported) {
311 WriteFile("file1.dat", kTestFileData, arraysize(kTestFileData) - 1);
312 net::HttpRequestHeaders headers;
313 headers.SetHeader(net::HttpRequestHeaders::kRange,
314 "bytes=0-5,10-200,200-300");
315 TestRequestWithHeaders(CreateFileSystemURL("file1.dat"), &headers);
316 EXPECT_TRUE(delegate_->request_failed());
317 EXPECT_EQ(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE,
318 request_->status().error());
321 TEST_F(FileSystemURLRequestJobTest, RangeOutOfBounds) {
322 WriteFile("file1.dat", kTestFileData, arraysize(kTestFileData) - 1);
323 net::HttpRequestHeaders headers;
324 headers.SetHeader(
325 net::HttpRequestHeaders::kRange,
326 net::HttpByteRange::Bounded(500, 1000).GetHeaderValue());
327 TestRequestWithHeaders(CreateFileSystemURL("file1.dat"), &headers);
329 ASSERT_FALSE(request_->is_pending());
330 EXPECT_TRUE(delegate_->request_failed());
331 EXPECT_EQ(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE,
332 request_->status().error());
335 TEST_F(FileSystemURLRequestJobTest, FileDirRedirect) {
336 CreateDirectory("dir");
337 TestRequest(CreateFileSystemURL("dir"));
339 EXPECT_EQ(1, delegate_->received_redirect_count());
340 EXPECT_TRUE(request_->status().is_success());
341 EXPECT_FALSE(delegate_->request_failed());
343 // We've deferred the redirect; now cancel the request to avoid following it.
344 request_->Cancel();
345 base::MessageLoop::current()->Run();
348 TEST_F(FileSystemURLRequestJobTest, InvalidURL) {
349 TestRequest(GURL("filesystem:/foo/bar/baz"));
350 ASSERT_FALSE(request_->is_pending());
351 EXPECT_TRUE(delegate_->request_failed());
352 EXPECT_EQ(net::ERR_INVALID_URL, request_->status().error());
355 TEST_F(FileSystemURLRequestJobTest, NoSuchRoot) {
356 TestRequest(GURL("filesystem:http://remote/persistent/somefile"));
357 ASSERT_FALSE(request_->is_pending());
358 EXPECT_TRUE(delegate_->request_failed());
359 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request_->status().error());
362 TEST_F(FileSystemURLRequestJobTest, NoSuchFile) {
363 TestRequest(CreateFileSystemURL("somefile"));
364 ASSERT_FALSE(request_->is_pending());
365 EXPECT_TRUE(delegate_->request_failed());
366 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request_->status().error());
369 TEST_F(FileSystemURLRequestJobTest, Cancel) {
370 WriteFile("file1.dat", kTestFileData, arraysize(kTestFileData) - 1);
371 TestRequestNoRun(CreateFileSystemURL("file1.dat"));
373 // Run StartAsync() and only StartAsync().
374 base::MessageLoop::current()->DeleteSoon(FROM_HERE, request_.release());
375 base::RunLoop().RunUntilIdle();
376 // If we get here, success! we didn't crash!
379 TEST_F(FileSystemURLRequestJobTest, GetMimeType) {
380 const char kFilename[] = "hoge.html";
382 std::string mime_type_direct;
383 base::FilePath::StringType extension =
384 base::FilePath().AppendASCII(kFilename).Extension();
385 if (!extension.empty())
386 extension = extension.substr(1);
387 EXPECT_TRUE(net::GetWellKnownMimeTypeFromExtension(
388 extension, &mime_type_direct));
390 TestRequest(CreateFileSystemURL(kFilename));
392 std::string mime_type_from_job;
393 request_->GetMimeType(&mime_type_from_job);
394 EXPECT_EQ(mime_type_direct, mime_type_from_job);
397 TEST_F(FileSystemURLRequestJobTest, Incognito) {
398 WriteFile("file", kTestFileData, arraysize(kTestFileData) - 1);
400 // Creates a new filesystem context for incognito mode.
401 scoped_refptr<FileSystemContext> file_system_context =
402 CreateIncognitoFileSystemContextForTesting(NULL, temp_dir_.path());
404 // The request should return NOT_FOUND error if it's in incognito mode.
405 TestRequestWithContext(CreateFileSystemURL("file"),
406 file_system_context.get());
407 ASSERT_FALSE(request_->is_pending());
408 EXPECT_TRUE(delegate_->request_failed());
409 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request_->status().error());
411 // Make sure it returns success with regular (non-incognito) context.
412 TestRequest(CreateFileSystemURL("file"));
413 ASSERT_FALSE(request_->is_pending());
414 EXPECT_EQ(kTestFileData, delegate_->data_received());
415 EXPECT_EQ(200, request_->GetResponseCode());
418 TEST_F(FileSystemURLRequestJobTest, AutoMountFileTest) {
419 SetUpAutoMountContext();
420 TestRequest(GURL("filesystem:http://automount/external/mnt_name/foo"));
422 ASSERT_FALSE(request_->is_pending());
423 EXPECT_EQ(1, delegate_->response_started_count());
424 EXPECT_FALSE(delegate_->received_data_before_response());
425 EXPECT_EQ(kTestFileData, delegate_->data_received());
426 EXPECT_EQ(200, request_->GetResponseCode());
427 std::string cache_control;
428 request_->GetResponseHeaderByName("cache-control", &cache_control);
429 EXPECT_EQ("no-cache", cache_control);
431 ASSERT_TRUE(
432 fileapi::ExternalMountPoints::GetSystemInstance()->RevokeFileSystem(
433 kValidExternalMountPoint));
436 TEST_F(FileSystemURLRequestJobTest, AutoMountInvalidRoot) {
437 SetUpAutoMountContext();
438 TestRequest(GURL("filesystem:http://automount/external/invalid/foo"));
440 ASSERT_FALSE(request_->is_pending());
441 EXPECT_TRUE(delegate_->request_failed());
442 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request_->status().error());
444 ASSERT_FALSE(
445 fileapi::ExternalMountPoints::GetSystemInstance()->RevokeFileSystem(
446 "invalid"));
449 TEST_F(FileSystemURLRequestJobTest, AutoMountNoHandler) {
450 SetUpAutoMountContext();
451 TestRequest(GURL("filesystem:http://noauto/external/mnt_name/foo"));
453 ASSERT_FALSE(request_->is_pending());
454 EXPECT_TRUE(delegate_->request_failed());
455 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request_->status().error());
457 ASSERT_FALSE(
458 fileapi::ExternalMountPoints::GetSystemInstance()->RevokeFileSystem(
459 kValidExternalMountPoint));
462 } // namespace
463 } // namespace content