Index: webkit/fileapi/file_system_context_unittest.cc |
diff --git a/webkit/fileapi/file_system_context_unittest.cc b/webkit/fileapi/file_system_context_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..85c1d4bc6d6962ef31e4eda6494092bc4ce3d55e |
--- /dev/null |
+++ b/webkit/fileapi/file_system_context_unittest.cc |
@@ -0,0 +1,183 @@ |
+// Copyright (c) 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 "webkit/fileapi/file_system_context.h" |
+ |
+#include "base/files/scoped_temp_dir.h" |
+#include "base/message_loop.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+#include "webkit/fileapi/external_mount_points.h" |
+#include "webkit/fileapi/file_system_task_runners.h" |
+#include "webkit/fileapi/isolated_context.h" |
+#include "webkit/fileapi/mock_file_system_options.h" |
+#include "webkit/quota/mock_quota_manager.h" |
+#include "webkit/quota/mock_special_storage_policy.h" |
+ |
+#define FPL(x) FILE_PATH_LITERAL(x) |
+ |
+#if defined(FILE_PATH_USES_DRIVE_LETTERS) |
+#define DRIVE FPL("C:") |
+#else |
+#define DRIVE |
+#endif |
+ |
+namespace fileapi { |
+ |
+namespace { |
+ |
+GURL CreateRawFileSystemURL(const std::string& origin_str, |
+ const std::string& type_str, |
+ const std::string& fs_id, |
+ const FilePath& path) { |
+ return GURL(std::string("filesystem:") + origin_str + type_str + |
+ std::string("/") + fs_id + std::string("/") + |
+ path.AsUTF8Unsafe()); |
+} |
+ |
+class FileSystemContextTest : public testing::Test { |
+ public: |
+ FileSystemContextTest() {} |
+ |
+ void SetUp() { |
+ ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); |
+ |
+ scoped_refptr<quota::SpecialStoragePolicy> storage_policy( |
+ new quota::MockSpecialStoragePolicy()); |
+ |
+ mock_quota_manager_ = new quota::MockQuotaManager( |
+ false /* is_incognito */, |
+ data_dir_.path(), |
+ base::MessageLoopProxy::current(), |
+ base::MessageLoopProxy::current(), |
+ storage_policy); |
+ |
+ file_system_context_ = new FileSystemContext( |
+ FileSystemTaskRunners::CreateMockTaskRunners(), |
+ storage_policy, |
+ mock_quota_manager_->proxy(), |
+ data_dir_.path(), |
+ CreateAllowFileAccessOptions()); |
+ } |
+ |
+ protected: |
+ FileSystemContext* file_system_context() { |
+ return file_system_context_.get(); |
+ } |
+ |
+ private: |
+ base::ScopedTempDir data_dir_; |
+ MessageLoop message_loop_; |
+ scoped_refptr<quota::MockQuotaManager> mock_quota_manager_; |
+ scoped_refptr<FileSystemContext> file_system_context_; |
+}; |
+ |
+TEST_F(FileSystemContextTest, CrackFileSystemURL) { |
+ // Register Isolated mount points. |
+ std::string isolated_file_system_name = "root"; |
+ const std::string kIsolatedFileSystemID = |
+ IsolatedContext::GetInstance()->RegisterFileSystemForPath( |
+ kFileSystemTypeNativeLocal, |
+ FilePath(DRIVE FPL("/test/isolated/root")), |
+ &isolated_file_system_name); |
+ // Register system external mount point. |
+ ASSERT_TRUE(ExternalMountPoints::GetSystemInstance()->RegisterFileSystem( |
+ "system", |
+ kFileSystemTypeDrive, |
+ FilePath(DRIVE FPL("/test/sys/")))); |
+ // Register system external mount point that overrides registered isolated |
+ // mount point. |
+ ASSERT_TRUE(ExternalMountPoints::GetSystemInstance()->RegisterFileSystem( |
+ kIsolatedFileSystemID, |
+ kFileSystemTypeRestrictedNativeLocal, |
+ FilePath(DRIVE FPL("/test/system/isolated")))); |
+ |
+ struct TestCase { |
+ // Test case values. |
+ std::string root; |
+ FileSystemType type; |
+ std::string type_str; |
+ |
+ // Expected test results. |
+ bool expect_is_valid; |
+ bool expect_is_cracked; |
+ FileSystemType expect_type; |
+ const FilePath::CharType* expect_path; |
+ }; |
+ |
+ const GURL kTestOrigin = GURL("http://chromium.org/"); |
+ const FilePath kVirtualPathNoRoot = FilePath(FPL("root/file")); |
+ |
+ const TestCase kTestCases[] = { |
+ // Following should not be handled by url crackers: |
+ { "pers_mount", kFileSystemTypePersistent, "persistent", |
+ true, false, kFileSystemTypePersistent, FPL("pers_mount/root/file") }, |
+ { "temp_mount", kFileSystemTypeTemporary, "temporary", |
+ true, false, kFileSystemTypeTemporary, FPL("temp_mount/root/file") }, |
+ // Should be cracked by isolated mount points: |
+ { kIsolatedFileSystemID, kFileSystemTypeIsolated, "isolated", |
+ true, true, kFileSystemTypeNativeLocal, |
+ DRIVE FPL("/test/isolated/root/file") }, |
+ // Should be cracked by system mount points: |
+ { "system", kFileSystemTypeExternal,"external", |
+ true, true, kFileSystemTypeDrive, |
+ DRIVE FPL("/test/sys/root/file") }, |
+ { kIsolatedFileSystemID, kFileSystemTypeExternal, "external", |
+ true, true, kFileSystemTypeRestrictedNativeLocal, |
+ DRIVE FPL("/test/system/isolated/root/file") }, |
+ // Test for already cracked FielSystemURLs: |
+ { "system", kFileSystemTypeExternal, "external", |
+ true, true, kFileSystemTypeDrive, |
+ DRIVE FPL("/test/sys/root/file") }, |
+ { kIsolatedFileSystemID, kFileSystemTypeIsolated, "isolated", |
+ true, true, kFileSystemTypeNativeLocal, |
+ DRIVE FPL("/test/isolated/root/file") }, |
+ { "invalid", kFileSystemTypeUnknown, "external", |
+ false, false, kFileSystemTypeUnknown, FPL("") }, |
+ { "invalid", kFileSystemTypeUnknown, "isolated", |
+ false, false, kFileSystemTypeUnknown, FPL("") }, |
+ }; |
+ |
+ for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) { |
+ const FilePath virtual_path = |
+ FilePath::FromUTF8Unsafe(kTestCases[i].root).Append(kVirtualPathNoRoot); |
+ SCOPED_TRACE( |
+ testing::Message() << "Testing test case " << i << ": " |
+ << "virtual_path: " << virtual_path.value() |
+ << " file system type: " << kTestCases[i].type_str); |
+ |
+ FileSystemURL cracked = file_system_context()->CrackURL( |
+ CreateRawFileSystemURL(kTestOrigin.spec(), kTestCases[i].type_str, |
+ kTestCases[i].root, kVirtualPathNoRoot)); |
+ |
+ EXPECT_EQ(kTestCases[i].expect_is_valid, cracked.is_valid()); |
+ EXPECT_EQ(kTestCases[i].expect_is_cracked, cracked.is_cracked()); |
+ |
+ if (!kTestCases[i].expect_is_valid) |
+ continue; |
+ EXPECT_EQ(kTestOrigin, cracked.origin()); |
+ EXPECT_EQ(kTestCases[i].expect_type, cracked.type()); |
+ EXPECT_EQ(kTestCases[i].type, cracked.mount_type()); |
+ EXPECT_EQ(FilePath(kTestCases[i].expect_path).NormalizePathSeparators(), |
+ cracked.path()); |
+ // Following properties should not be set for non-cracked urls. |
+ bool is_cracked = kTestCases[i].expect_is_cracked; |
+ FilePath expect_virtual_path = |
+ is_cracked ? virtual_path.NormalizePathSeparators() : FilePath(); |
+ EXPECT_EQ(expect_virtual_path, cracked.virtual_path()); |
+ std::string expect_filesystem_id = |
+ is_cracked ? kTestCases[i].root : std::string(); |
+ EXPECT_EQ(expect_filesystem_id, cracked.filesystem_id()); |
+ } |
+ |
+ IsolatedContext::GetInstance()->RevokeFileSystemByPath( |
+ FilePath(DRIVE FPL("/test/isolated/root"))); |
+ ExternalMountPoints::GetSystemInstance()->RevokeFileSystem("system"); |
+ ExternalMountPoints::GetSystemInstance()->RevokeFileSystem( |
+ kIsolatedFileSystemID); |
+} |
+ |
+} // namespace |
+ |
+} // namespace fileapi |
+ |