Add ICU message format support
[chromium-blink-merge.git] / content / browser / fileapi / file_system_dir_url_request_job_unittest.cc
blob8d6f17b9d86e97a8ee394c7f26a897e28d3f920f
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 "storage/browser/fileapi/file_system_dir_url_request_job.h"
7 #include <string>
9 #include "base/files/file_path.h"
10 #include "base/files/file_util.h"
11 #include "base/files/scoped_temp_dir.h"
12 #include "base/format_macros.h"
13 #include "base/location.h"
14 #include "base/memory/scoped_vector.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/run_loop.h"
17 #include "base/strings/string_piece.h"
18 #include "base/strings/utf_string_conversions.h"
19 #include "base/thread_task_runner_handle.h"
20 #include "content/public/test/mock_special_storage_policy.h"
21 #include "content/public/test/test_file_system_backend.h"
22 #include "content/public/test/test_file_system_context.h"
23 #include "net/base/net_errors.h"
24 #include "net/base/net_util.h"
25 #include "net/base/request_priority.h"
26 #include "net/http/http_request_headers.h"
27 #include "net/url_request/url_request.h"
28 #include "net/url_request/url_request_context.h"
29 #include "net/url_request/url_request_test_util.h"
30 #include "storage/browser/fileapi/external_mount_points.h"
31 #include "storage/browser/fileapi/file_system_context.h"
32 #include "storage/browser/fileapi/file_system_file_util.h"
33 #include "storage/browser/fileapi/file_system_operation_context.h"
34 #include "storage/browser/fileapi/file_system_url.h"
35 #include "testing/gtest/include/gtest/gtest.h"
36 #include "third_party/icu/source/i18n/unicode/datefmt.h"
37 #include "third_party/icu/source/i18n/unicode/regex.h"
39 using storage::FileSystemContext;
40 using storage::FileSystemOperationContext;
41 using storage::FileSystemURL;
43 namespace content {
44 namespace {
46 // We always use the TEMPORARY FileSystem in this test.
47 const char kFileSystemURLPrefix[] = "filesystem:http://remote/temporary/";
49 const char kValidExternalMountPoint[] = "mnt_name";
51 // An auto mounter that will try to mount anything for |storage_domain| =
52 // "automount", but will only succeed for the mount point "mnt_name".
53 bool TestAutoMountForURLRequest(
54 const net::URLRequest* /*url_request*/,
55 const storage::FileSystemURL& filesystem_url,
56 const std::string& storage_domain,
57 const base::Callback<void(base::File::Error result)>& callback) {
58 if (storage_domain != "automount")
59 return false;
61 std::vector<base::FilePath::StringType> components;
62 filesystem_url.path().GetComponents(&components);
63 std::string mount_point = base::FilePath(components[0]).AsUTF8Unsafe();
65 if (mount_point == kValidExternalMountPoint) {
66 storage::ExternalMountPoints::GetSystemInstance()->RegisterFileSystem(
67 kValidExternalMountPoint,
68 storage::kFileSystemTypeTest,
69 storage::FileSystemMountOption(),
70 base::FilePath());
71 callback.Run(base::File::FILE_OK);
72 } else {
73 callback.Run(base::File::FILE_ERROR_NOT_FOUND);
75 return true;
78 class FileSystemDirURLRequestJobFactory : public net::URLRequestJobFactory {
79 public:
80 FileSystemDirURLRequestJobFactory(const std::string& storage_domain,
81 FileSystemContext* context)
82 : storage_domain_(storage_domain), file_system_context_(context) {
85 net::URLRequestJob* MaybeCreateJobWithProtocolHandler(
86 const std::string& scheme,
87 net::URLRequest* request,
88 net::NetworkDelegate* network_delegate) const override {
89 return new storage::FileSystemDirURLRequestJob(
90 request, network_delegate, storage_domain_, file_system_context_);
93 net::URLRequestJob* MaybeInterceptRedirect(
94 net::URLRequest* request,
95 net::NetworkDelegate* network_delegate,
96 const GURL& location) const override {
97 return nullptr;
100 net::URLRequestJob* MaybeInterceptResponse(
101 net::URLRequest* request,
102 net::NetworkDelegate* network_delegate) const override {
103 return nullptr;
106 bool IsHandledProtocol(const std::string& scheme) const override {
107 return true;
110 bool IsHandledURL(const GURL& url) const override { return true; }
112 bool IsSafeRedirectTarget(const GURL& location) const override {
113 return false;
116 private:
117 std::string storage_domain_;
118 FileSystemContext* file_system_context_;
122 } // namespace
124 class FileSystemDirURLRequestJobTest : public testing::Test {
125 protected:
126 FileSystemDirURLRequestJobTest()
127 : weak_factory_(this) {
130 void SetUp() override {
131 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
133 special_storage_policy_ = new MockSpecialStoragePolicy;
134 file_system_context_ = CreateFileSystemContextForTesting(
135 NULL, temp_dir_.path());
137 file_system_context_->OpenFileSystem(
138 GURL("http://remote/"),
139 storage::kFileSystemTypeTemporary,
140 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT,
141 base::Bind(&FileSystemDirURLRequestJobTest::OnOpenFileSystem,
142 weak_factory_.GetWeakPtr()));
143 base::RunLoop().RunUntilIdle();
146 void TearDown() override {
147 // NOTE: order matters, request must die before delegate
148 request_.reset(NULL);
149 delegate_.reset(NULL);
152 void SetUpAutoMountContext(base::FilePath* mnt_point) {
153 *mnt_point = temp_dir_.path().AppendASCII("auto_mount_dir");
154 ASSERT_TRUE(base::CreateDirectory(*mnt_point));
156 ScopedVector<storage::FileSystemBackend> additional_providers;
157 additional_providers.push_back(new TestFileSystemBackend(
158 base::ThreadTaskRunnerHandle::Get().get(), *mnt_point));
160 std::vector<storage::URLRequestAutoMountHandler> handlers;
161 handlers.push_back(base::Bind(&TestAutoMountForURLRequest));
163 file_system_context_ = CreateFileSystemContextWithAutoMountersForTesting(
164 NULL, additional_providers.Pass(), handlers, temp_dir_.path());
167 void OnOpenFileSystem(const GURL& root_url,
168 const std::string& name,
169 base::File::Error result) {
170 ASSERT_EQ(base::File::FILE_OK, result);
173 void TestRequestHelper(const GURL& url, bool run_to_completion,
174 FileSystemContext* file_system_context) {
175 delegate_.reset(new net::TestDelegate());
176 delegate_->set_quit_on_redirect(true);
177 job_factory_.reset(new FileSystemDirURLRequestJobFactory(
178 url.GetOrigin().host(), file_system_context));
179 empty_context_.set_job_factory(job_factory_.get());
181 request_ = empty_context_.CreateRequest(
182 url, net::DEFAULT_PRIORITY, delegate_.get());
183 request_->Start();
184 ASSERT_TRUE(request_->is_pending()); // verify that we're starting async
185 if (run_to_completion)
186 base::MessageLoop::current()->Run();
189 void TestRequest(const GURL& url) {
190 TestRequestHelper(url, true, file_system_context_.get());
193 void TestRequestWithContext(const GURL& url,
194 FileSystemContext* file_system_context) {
195 TestRequestHelper(url, true, file_system_context);
198 void TestRequestNoRun(const GURL& url) {
199 TestRequestHelper(url, false, file_system_context_.get());
202 FileSystemURL CreateURL(const base::FilePath& file_path) {
203 return file_system_context_->CreateCrackedFileSystemURL(
204 GURL("http://remote"), storage::kFileSystemTypeTemporary, file_path);
207 FileSystemOperationContext* NewOperationContext() {
208 FileSystemOperationContext* context(
209 new FileSystemOperationContext(file_system_context_.get()));
210 context->set_allowed_bytes_growth(1024);
211 return context;
214 void CreateDirectory(const base::StringPiece& dir_name) {
215 base::FilePath path = base::FilePath().AppendASCII(dir_name);
216 scoped_ptr<FileSystemOperationContext> context(NewOperationContext());
217 ASSERT_EQ(base::File::FILE_OK, file_util()->CreateDirectory(
218 context.get(),
219 CreateURL(path),
220 false /* exclusive */,
221 false /* recursive */));
224 void EnsureFileExists(const base::StringPiece file_name) {
225 base::FilePath path = base::FilePath().AppendASCII(file_name);
226 scoped_ptr<FileSystemOperationContext> context(NewOperationContext());
227 ASSERT_EQ(base::File::FILE_OK, file_util()->EnsureFileExists(
228 context.get(), CreateURL(path), NULL));
231 void TruncateFile(const base::StringPiece file_name, int64 length) {
232 base::FilePath path = base::FilePath().AppendASCII(file_name);
233 scoped_ptr<FileSystemOperationContext> context(NewOperationContext());
234 ASSERT_EQ(base::File::FILE_OK, file_util()->Truncate(
235 context.get(), CreateURL(path), length));
238 base::File::Error GetFileInfo(const base::FilePath& path,
239 base::File::Info* file_info,
240 base::FilePath* platform_file_path) {
241 scoped_ptr<FileSystemOperationContext> context(NewOperationContext());
242 return file_util()->GetFileInfo(context.get(),
243 CreateURL(path),
244 file_info, platform_file_path);
247 // If |size| is negative, the reported size is ignored.
248 void VerifyListingEntry(const std::string& entry_line,
249 const std::string& name,
250 const std::string& url,
251 bool is_directory,
252 int64 size) {
253 #define STR "([^\"]*)"
254 icu::UnicodeString pattern("^<script>addRow\\(\"" STR "\",\"" STR
255 "\",(0|1),\"" STR "\",\"" STR "\"\\);</script>");
256 #undef STR
257 icu::UnicodeString input(entry_line.c_str());
259 UErrorCode status = U_ZERO_ERROR;
260 icu::RegexMatcher match(pattern, input, 0, status);
262 EXPECT_TRUE(match.find());
263 EXPECT_EQ(5, match.groupCount());
264 EXPECT_EQ(icu::UnicodeString(name.c_str()), match.group(1, status));
265 EXPECT_EQ(icu::UnicodeString(url.c_str()), match.group(2, status));
266 EXPECT_EQ(icu::UnicodeString(is_directory ? "1" : "0"),
267 match.group(3, status));
268 if (size >= 0) {
269 icu::UnicodeString size_string(
270 base::FormatBytesUnlocalized(size).c_str());
271 EXPECT_EQ(size_string, match.group(4, status));
274 icu::UnicodeString date_ustr(match.group(5, status));
275 scoped_ptr<icu::DateFormat> formatter(
276 icu::DateFormat::createDateTimeInstance(icu::DateFormat::kShort));
277 UErrorCode parse_status = U_ZERO_ERROR;
278 UDate udate = formatter->parse(date_ustr, parse_status);
279 EXPECT_TRUE(U_SUCCESS(parse_status));
280 base::Time date = base::Time::FromJsTime(udate);
281 EXPECT_FALSE(date.is_null());
284 GURL CreateFileSystemURL(const std::string path) {
285 return GURL(kFileSystemURLPrefix + path);
288 storage::FileSystemFileUtil* file_util() {
289 return file_system_context_->sandbox_delegate()->sync_file_util();
292 // Put the message loop at the top, so that it's the last thing deleted.
293 // Delete all task runner objects before the MessageLoop, to help prevent
294 // leaks caused by tasks posted during shutdown.
295 base::MessageLoopForIO message_loop_;
297 base::ScopedTempDir temp_dir_;
298 net::URLRequestContext empty_context_;
299 scoped_ptr<net::TestDelegate> delegate_;
300 scoped_ptr<net::URLRequest> request_;
301 scoped_ptr<FileSystemDirURLRequestJobFactory> job_factory_;
302 scoped_refptr<MockSpecialStoragePolicy> special_storage_policy_;
303 scoped_refptr<FileSystemContext> file_system_context_;
304 base::WeakPtrFactory<FileSystemDirURLRequestJobTest> weak_factory_;
307 namespace {
309 TEST_F(FileSystemDirURLRequestJobTest, DirectoryListing) {
310 CreateDirectory("foo");
311 CreateDirectory("foo/bar");
312 CreateDirectory("foo/bar/baz");
314 EnsureFileExists("foo/bar/hoge");
315 TruncateFile("foo/bar/hoge", 10);
317 TestRequest(CreateFileSystemURL("foo/bar/"));
319 ASSERT_FALSE(request_->is_pending());
320 EXPECT_EQ(1, delegate_->response_started_count());
321 EXPECT_FALSE(delegate_->received_data_before_response());
322 EXPECT_GT(delegate_->bytes_received(), 0);
324 std::istringstream in(delegate_->data_received());
325 std::string line;
326 EXPECT_TRUE(std::getline(in, line));
328 #if defined(OS_WIN)
329 EXPECT_EQ("<script>start(\"foo\\\\bar\");</script>", line);
330 #elif defined(OS_POSIX)
331 EXPECT_EQ("<script>start(\"/foo/bar\");</script>", line);
332 #endif
334 EXPECT_TRUE(std::getline(in, line));
335 VerifyListingEntry(line, "hoge", "hoge", false, 10);
337 EXPECT_TRUE(std::getline(in, line));
338 VerifyListingEntry(line, "baz", "baz", true, 0);
339 EXPECT_FALSE(!!std::getline(in, line));
342 TEST_F(FileSystemDirURLRequestJobTest, InvalidURL) {
343 TestRequest(GURL("filesystem:/foo/bar/baz"));
344 ASSERT_FALSE(request_->is_pending());
345 EXPECT_TRUE(delegate_->request_failed());
346 ASSERT_FALSE(request_->status().is_success());
347 EXPECT_EQ(net::ERR_INVALID_URL, request_->status().error());
350 TEST_F(FileSystemDirURLRequestJobTest, NoSuchRoot) {
351 TestRequest(GURL("filesystem:http://remote/persistent/somedir/"));
352 ASSERT_FALSE(request_->is_pending());
353 ASSERT_FALSE(request_->status().is_success());
354 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request_->status().error());
357 TEST_F(FileSystemDirURLRequestJobTest, NoSuchDirectory) {
358 TestRequest(CreateFileSystemURL("somedir/"));
359 ASSERT_FALSE(request_->is_pending());
360 ASSERT_FALSE(request_->status().is_success());
361 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request_->status().error());
364 TEST_F(FileSystemDirURLRequestJobTest, Cancel) {
365 CreateDirectory("foo");
366 TestRequestNoRun(CreateFileSystemURL("foo/"));
367 // Run StartAsync() and only StartAsync().
368 base::MessageLoop::current()->DeleteSoon(FROM_HERE, request_.release());
369 base::RunLoop().RunUntilIdle();
370 // If we get here, success! we didn't crash!
373 TEST_F(FileSystemDirURLRequestJobTest, Incognito) {
374 CreateDirectory("foo");
376 scoped_refptr<FileSystemContext> file_system_context =
377 CreateIncognitoFileSystemContextForTesting(NULL, temp_dir_.path());
379 TestRequestWithContext(CreateFileSystemURL("/"),
380 file_system_context.get());
381 ASSERT_FALSE(request_->is_pending());
382 ASSERT_TRUE(request_->status().is_success());
384 std::istringstream in(delegate_->data_received());
385 std::string line;
386 EXPECT_TRUE(std::getline(in, line));
387 EXPECT_FALSE(!!std::getline(in, line));
389 TestRequestWithContext(CreateFileSystemURL("foo"),
390 file_system_context.get());
391 ASSERT_FALSE(request_->is_pending());
392 ASSERT_FALSE(request_->status().is_success());
393 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request_->status().error());
396 TEST_F(FileSystemDirURLRequestJobTest, AutoMountDirectoryListing) {
397 base::FilePath mnt_point;
398 SetUpAutoMountContext(&mnt_point);
399 ASSERT_TRUE(base::CreateDirectory(mnt_point));
400 ASSERT_TRUE(base::CreateDirectory(mnt_point.AppendASCII("foo")));
401 ASSERT_EQ(10,
402 base::WriteFile(mnt_point.AppendASCII("bar"), "1234567890", 10));
404 TestRequest(GURL("filesystem:http://automount/external/mnt_name"));
406 ASSERT_FALSE(request_->is_pending());
407 EXPECT_EQ(1, delegate_->response_started_count());
408 EXPECT_FALSE(delegate_->received_data_before_response());
409 EXPECT_GT(delegate_->bytes_received(), 0);
411 std::istringstream in(delegate_->data_received());
412 std::string line;
413 EXPECT_TRUE(std::getline(in, line)); // |line| contains the temp dir path.
415 // Result order is not guaranteed, so sort the results.
416 std::vector<std::string> listing_entries;
417 while (!!std::getline(in, line))
418 listing_entries.push_back(line);
420 ASSERT_EQ(2U, listing_entries.size());
421 std::sort(listing_entries.begin(), listing_entries.end());
422 VerifyListingEntry(listing_entries[0], "bar", "bar", false, 10);
423 VerifyListingEntry(listing_entries[1], "foo", "foo", true, -1);
425 ASSERT_TRUE(
426 storage::ExternalMountPoints::GetSystemInstance()->RevokeFileSystem(
427 kValidExternalMountPoint));
430 TEST_F(FileSystemDirURLRequestJobTest, AutoMountInvalidRoot) {
431 base::FilePath mnt_point;
432 SetUpAutoMountContext(&mnt_point);
433 TestRequest(GURL("filesystem:http://automount/external/invalid"));
435 ASSERT_FALSE(request_->is_pending());
436 ASSERT_FALSE(request_->status().is_success());
437 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request_->status().error());
439 ASSERT_FALSE(
440 storage::ExternalMountPoints::GetSystemInstance()->RevokeFileSystem(
441 "invalid"));
444 TEST_F(FileSystemDirURLRequestJobTest, AutoMountNoHandler) {
445 base::FilePath mnt_point;
446 SetUpAutoMountContext(&mnt_point);
447 TestRequest(GURL("filesystem:http://noauto/external/mnt_name"));
449 ASSERT_FALSE(request_->is_pending());
450 ASSERT_FALSE(request_->status().is_success());
451 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request_->status().error());
453 ASSERT_FALSE(
454 storage::ExternalMountPoints::GetSystemInstance()->RevokeFileSystem(
455 kValidExternalMountPoint));
458 } // namespace
459 } // namespace content