OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/fileapi/file_system_path_manager.h" | 5 #include "webkit/fileapi/file_system_path_manager.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
12 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
13 #include "base/message_loop_proxy.h" | 13 #include "base/message_loop_proxy.h" |
14 #include "base/ref_counted.h" | 14 #include "base/ref_counted.h" |
15 #include "base/scoped_callback_factory.h" | 15 #include "base/scoped_callback_factory.h" |
16 #include "base/scoped_ptr.h" | 16 #include "base/scoped_ptr.h" |
17 #include "base/scoped_temp_dir.h" | 17 #include "base/scoped_temp_dir.h" |
18 #include "googleurl/src/gurl.h" | 18 #include "googleurl/src/gurl.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 #include "webkit/fileapi/file_system_util.h" |
| 21 #include "webkit/fileapi/sandbox_mount_point_provider.h" |
20 | 22 |
21 using namespace fileapi; | 23 using namespace fileapi; |
22 | 24 |
23 namespace { | 25 namespace { |
24 | 26 |
25 // PS stands for path separator. | 27 // PS stands for path separator. |
26 #if defined(FILE_PATH_USES_WIN_SEPARATORS) | 28 #if defined(FILE_PATH_USES_WIN_SEPARATORS) |
27 #define PS "\\" | 29 #define PS "\\" |
28 #else | 30 #else |
29 #define PS "/" | 31 #define PS "/" |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 } | 207 } |
206 | 208 |
207 bool CheckValidFileSystemPath(FileSystemPathManager* manager, | 209 bool CheckValidFileSystemPath(FileSystemPathManager* manager, |
208 const FilePath& path) { | 210 const FilePath& path) { |
209 return manager->CrackFileSystemPath(path, NULL, NULL, NULL); | 211 return manager->CrackFileSystemPath(path, NULL, NULL, NULL); |
210 } | 212 } |
211 | 213 |
212 FilePath data_path() { return data_dir_.path(); } | 214 FilePath data_path() { return data_dir_.path(); } |
213 FilePath file_system_path() { | 215 FilePath file_system_path() { |
214 return data_dir_.path().Append( | 216 return data_dir_.path().Append( |
215 FileSystemPathManager::kFileSystemDirectory); | 217 SandboxMountPointProvider::kFileSystemDirectory); |
216 } | 218 } |
217 | 219 |
218 private: | 220 private: |
219 ScopedTempDir data_dir_; | 221 ScopedTempDir data_dir_; |
220 base::ScopedCallbackFactory<FileSystemPathManagerTest> callback_factory_; | 222 base::ScopedCallbackFactory<FileSystemPathManagerTest> callback_factory_; |
221 | 223 |
222 bool root_path_callback_status_; | 224 bool root_path_callback_status_; |
223 FilePath root_path_; | 225 FilePath root_path_; |
224 std::string file_system_name_; | 226 std::string file_system_name_; |
225 | 227 |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
338 true /* create */, &root_path)); | 340 true /* create */, &root_path)); |
339 FilePath expected = file_system_path().AppendASCII( | 341 FilePath expected = file_system_path().AppendASCII( |
340 kRootPathFileURITestCases[i].expected_path); | 342 kRootPathFileURITestCases[i].expected_path); |
341 EXPECT_EQ(expected.value(), root_path.DirName().value()); | 343 EXPECT_EQ(expected.value(), root_path.DirName().value()); |
342 EXPECT_TRUE(file_util::DirectoryExists(root_path)); | 344 EXPECT_TRUE(file_util::DirectoryExists(root_path)); |
343 } | 345 } |
344 } | 346 } |
345 | 347 |
346 TEST_F(FileSystemPathManagerTest, VirtualPathFromFileSystemPathTest) { | 348 TEST_F(FileSystemPathManagerTest, VirtualPathFromFileSystemPathTest) { |
347 scoped_ptr<FileSystemPathManager> manager(NewPathManager(false, false)); | 349 scoped_ptr<FileSystemPathManager> manager(NewPathManager(false, false)); |
348 FilePath root_path; | 350 GURL root_url = GetFileSystemRootURI( |
349 EXPECT_TRUE(GetRootPath(manager.get(), GURL("http://foo.com/"), | 351 GURL("http://foo.com/"), fileapi::kFileSystemTypeTemporary); |
350 fileapi::kFileSystemTypeTemporary, | 352 FilePath root_path = FilePath().AppendASCII(root_url.spec()); |
351 true /* create */, &root_path)); | |
352 | 353 |
353 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kPathToVirtualPathTestCases); ++i) { | 354 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kPathToVirtualPathTestCases); ++i) { |
354 SCOPED_TRACE(testing::Message() << "PathToVirtualPath #" | 355 SCOPED_TRACE(testing::Message() << "PathToVirtualPath #" |
355 << i << " " << kPathToVirtualPathTestCases[i]); | 356 << i << " " << kPathToVirtualPathTestCases[i]); |
356 FilePath absolute_path = root_path.AppendASCII( | 357 FilePath absolute_path; |
357 kPathToVirtualPathTestCases[i]); | 358 // TODO(ericu): Clean this up when we've got more sane path-handling. |
| 359 // This hack is necessary because root_path is actually a URL [ending with a |
| 360 // forward slash], and AppendASCII("") on Windows will delete the trailing |
| 361 // slash, making the path invalid as far as CrackFileSystemPath is |
| 362 // concerned. |
| 363 if (strlen(kPathToVirtualPathTestCases[i])) |
| 364 absolute_path = root_path.AppendASCII( |
| 365 kPathToVirtualPathTestCases[i]); |
| 366 else |
| 367 absolute_path = root_path; |
358 FilePath virtual_path; | 368 FilePath virtual_path; |
359 EXPECT_TRUE(manager->CrackFileSystemPath(absolute_path, NULL, NULL, | 369 EXPECT_TRUE(manager->CrackFileSystemPath(absolute_path, NULL, NULL, |
360 &virtual_path)); | 370 &virtual_path)); |
361 | 371 |
362 FilePath test_case_path; | 372 FilePath test_case_path; |
363 test_case_path = test_case_path.AppendASCII( | 373 test_case_path = test_case_path.AppendASCII( |
364 kPathToVirtualPathTestCases[i]); | 374 kPathToVirtualPathTestCases[i]); |
365 EXPECT_EQ(test_case_path.value(), virtual_path.value()); | 375 EXPECT_EQ(test_case_path.value(), virtual_path.value()); |
366 } | 376 } |
367 } | 377 } |
368 | 378 |
369 TEST_F(FileSystemPathManagerTest, TypeFromFileSystemPathTest) { | 379 TEST_F(FileSystemPathManagerTest, TypeFromFileSystemPathTest) { |
370 scoped_ptr<FileSystemPathManager> manager(NewPathManager(false, false)); | 380 scoped_ptr<FileSystemPathManager> manager(NewPathManager(false, false)); |
371 | 381 |
372 FilePath root_path; | |
373 fileapi::FileSystemType type; | 382 fileapi::FileSystemType type; |
374 | 383 |
375 EXPECT_TRUE(GetRootPath(manager.get(), GURL("http://foo.com/"), | 384 GURL root_url = GetFileSystemRootURI( |
376 fileapi::kFileSystemTypeTemporary, | 385 GURL("http://foo.com/"), fileapi::kFileSystemTypeTemporary); |
377 true /* create */, &root_path)); | 386 FilePath root_path = FilePath().AppendASCII(root_url.spec()); |
378 FilePath path = root_path.AppendASCII("test"); | 387 FilePath path = root_path.AppendASCII("test"); |
379 EXPECT_TRUE(manager->CrackFileSystemPath(path, NULL, &type, NULL)); | 388 EXPECT_TRUE(manager->CrackFileSystemPath(path, NULL, &type, NULL)); |
380 EXPECT_EQ(fileapi::kFileSystemTypeTemporary, type); | 389 EXPECT_EQ(fileapi::kFileSystemTypeTemporary, type); |
381 | 390 |
382 EXPECT_TRUE(GetRootPath(manager.get(), GURL("http://foo.com/"), | 391 root_url = GetFileSystemRootURI( |
383 fileapi::kFileSystemTypePersistent, | 392 GURL("http://foo.com/"), fileapi::kFileSystemTypePersistent); |
384 true /* create */, &root_path)); | 393 root_path = FilePath().AppendASCII(root_url.spec()); |
385 path = root_path.AppendASCII("test"); | 394 path = root_path.AppendASCII("test"); |
386 EXPECT_TRUE(manager->CrackFileSystemPath(path, NULL, &type, NULL)); | 395 EXPECT_TRUE(manager->CrackFileSystemPath(path, NULL, &type, NULL)); |
387 EXPECT_EQ(fileapi::kFileSystemTypePersistent, type); | 396 EXPECT_EQ(fileapi::kFileSystemTypePersistent, type); |
388 } | 397 } |
389 | 398 |
390 TEST_F(FileSystemPathManagerTest, CheckValidPath) { | 399 TEST_F(FileSystemPathManagerTest, CheckValidPath) { |
391 scoped_ptr<FileSystemPathManager> manager(NewPathManager(false, false)); | 400 scoped_ptr<FileSystemPathManager> manager(NewPathManager(false, false)); |
392 FilePath root_path; | 401 GURL root_url = GetFileSystemRootURI( |
393 EXPECT_TRUE(GetRootPath(manager.get(), GURL("http://foo.com/"), | 402 GURL("http://foo.com/"), fileapi::kFileSystemTypePersistent); |
394 kFileSystemTypePersistent, true, &root_path)); | 403 FilePath root_path = FilePath().AppendASCII(root_url.spec()); |
395 | 404 |
396 // The root path must be valid, but upper directories or directories | 405 // The root path must be valid, but upper directories or directories |
397 // that are not in our temporary or persistent directory must be | 406 // that are not in our temporary or persistent directory must be |
398 // evaluated invalid. | 407 // evaluated invalid. |
399 EXPECT_TRUE(CheckValidFileSystemPath(manager.get(), root_path)); | 408 EXPECT_TRUE(CheckValidFileSystemPath(manager.get(), root_path)); |
400 EXPECT_FALSE(CheckValidFileSystemPath(manager.get(), root_path.DirName())); | 409 EXPECT_FALSE(CheckValidFileSystemPath(manager.get(), root_path.DirName())); |
401 EXPECT_FALSE(CheckValidFileSystemPath(manager.get(), | 410 EXPECT_FALSE(CheckValidFileSystemPath(manager.get(), |
402 root_path.DirName().DirName())); | 411 root_path.DirName().DirName())); |
403 EXPECT_FALSE(CheckValidFileSystemPath(manager.get(), | 412 EXPECT_FALSE(CheckValidFileSystemPath(manager.get(), |
404 root_path.DirName().DirName() | 413 root_path.DirName().DirName() |
405 .AppendASCII("ArbitraryName") | 414 .AppendASCII("ArbitraryName") |
406 .AppendASCII("chrome-dummy"))); | 415 .AppendASCII("chrome-dummy"))); |
407 | 416 |
408 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kCheckValidPathTestCases); ++i) { | 417 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kCheckValidPathTestCases); ++i) { |
409 SCOPED_TRACE(testing::Message() << "CheckValidPath #" << i << " " | 418 SCOPED_TRACE(testing::Message() << "CheckValidPath #" << i << " " |
410 << kCheckValidPathTestCases[i].path); | 419 << kCheckValidPathTestCases[i].path); |
411 FilePath path(kCheckValidPathTestCases[i].path); | 420 FilePath path(kCheckValidPathTestCases[i].path); |
| 421 #ifdef FILE_PATH_USES_WIN_SEPARATORS |
| 422 path = path.NormalizeWindowsPathSeparators(); |
| 423 #endif |
412 if (!path.IsAbsolute()) | 424 if (!path.IsAbsolute()) |
413 path = root_path.Append(path); | 425 path = root_path.Append(path); |
414 EXPECT_EQ(kCheckValidPathTestCases[i].expected_valid, | 426 EXPECT_EQ(kCheckValidPathTestCases[i].expected_valid, |
415 CheckValidFileSystemPath(manager.get(), path)); | 427 CheckValidFileSystemPath(manager.get(), path)); |
416 } | 428 } |
417 } | 429 } |
418 | 430 |
419 TEST_F(FileSystemPathManagerTest, IsRestrictedName) { | 431 TEST_F(FileSystemPathManagerTest, IsRestrictedName) { |
| 432 scoped_ptr<FileSystemPathManager> manager(NewPathManager(false, false)); |
420 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kIsRestrictedNameTestCases); ++i) { | 433 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kIsRestrictedNameTestCases); ++i) { |
421 SCOPED_TRACE(testing::Message() << "IsRestrictedName #" << i << " " | 434 SCOPED_TRACE(testing::Message() << "IsRestrictedName #" << i << " " |
422 << kIsRestrictedNameTestCases[i].name); | 435 << kIsRestrictedNameTestCases[i].name); |
423 FilePath name(kIsRestrictedNameTestCases[i].name); | 436 FilePath name(kIsRestrictedNameTestCases[i].name); |
424 EXPECT_EQ(kIsRestrictedNameTestCases[i].expected_dangerous, | 437 EXPECT_EQ(kIsRestrictedNameTestCases[i].expected_dangerous, |
425 FileSystemPathManager::IsRestrictedFileName(name)); | 438 manager->IsRestrictedFileName(kFileSystemTypeTemporary, name)); |
426 } | 439 } |
427 } | 440 } |
428 | |
429 class FileSystemPathManagerOriginEnumeratorTest : public testing::Test { | |
430 public: | |
431 void SetUp() { | |
432 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); | |
433 enumerator_.reset(new FileSystemPathManager::OriginEnumerator( | |
434 data_dir_.path())); | |
435 } | |
436 | |
437 FileSystemPathManager::OriginEnumerator* enumerator() const { | |
438 return enumerator_.get(); | |
439 } | |
440 | |
441 protected: | |
442 void CreateOriginTypeDirectory(const std::string& origin_identifier, | |
443 fileapi::FileSystemType type) { | |
444 std::string type_string = | |
445 FileSystemPathManager::GetFileSystemTypeString(type); | |
446 ASSERT_TRUE(!type_string.empty()); | |
447 FilePath target = data_dir_.path().AppendASCII(origin_identifier) | |
448 .AppendASCII(type_string); | |
449 file_util::CreateDirectory(target); | |
450 ASSERT_TRUE(file_util::DirectoryExists(target)); | |
451 } | |
452 | |
453 ScopedTempDir data_dir_; | |
454 scoped_ptr<FileSystemPathManager::OriginEnumerator> enumerator_; | |
455 }; | |
456 | |
457 TEST_F(FileSystemPathManagerOriginEnumeratorTest, Empty) { | |
458 ASSERT_TRUE(enumerator()->Next().empty()); | |
459 } | |
460 | |
461 TEST_F(FileSystemPathManagerOriginEnumeratorTest, EnumerateOrigins) { | |
462 const char* temporary_origins[] = { | |
463 "http_www.bar.com_0", | |
464 "http_www.foo.com_0", | |
465 "http_www.foo.com_80", | |
466 "http_www.example.com_8080", | |
467 "http_www.google.com_80", | |
468 }; | |
469 const char* persistent_origins[] = { | |
470 "http_www.bar.com_0", | |
471 "http_www.foo.com_8080", | |
472 "http_www.foo.com_80", | |
473 }; | |
474 size_t temporary_size = ARRAYSIZE_UNSAFE(temporary_origins); | |
475 size_t persistent_size = ARRAYSIZE_UNSAFE(persistent_origins); | |
476 std::set<std::string> temporary_set, persistent_set; | |
477 for (size_t i = 0; i < temporary_size; ++i) { | |
478 CreateOriginTypeDirectory(temporary_origins[i], | |
479 fileapi::kFileSystemTypeTemporary); | |
480 temporary_set.insert(temporary_origins[i]); | |
481 } | |
482 for (size_t i = 0; i < persistent_size; ++i) { | |
483 CreateOriginTypeDirectory(persistent_origins[i], kFileSystemTypePersistent); | |
484 persistent_set.insert(persistent_origins[i]); | |
485 } | |
486 | |
487 size_t temporary_actual_size = 0; | |
488 size_t persistent_actual_size = 0; | |
489 std::string current; | |
490 while (!(current = enumerator()->Next()).empty()) { | |
491 SCOPED_TRACE(testing::Message() << "EnumerateOrigin " << current); | |
492 if (enumerator()->HasTemporary()) { | |
493 ASSERT_TRUE(temporary_set.find(current) != temporary_set.end()); | |
494 ++temporary_actual_size; | |
495 } | |
496 if (enumerator()->HasPersistent()) { | |
497 ASSERT_TRUE(persistent_set.find(current) != persistent_set.end()); | |
498 ++persistent_actual_size; | |
499 } | |
500 } | |
501 | |
502 ASSERT_EQ(temporary_size, temporary_actual_size); | |
503 ASSERT_EQ(persistent_size, persistent_actual_size); | |
504 } | |
OLD | NEW |