OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 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. |
| 4 |
| 5 #include "webkit/fileapi/file_system_context.h" |
| 6 |
| 7 #include "base/files/scoped_temp_dir.h" |
| 8 #include "base/message_loop.h" |
| 9 #include "base/stringprintf.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 #include "webkit/fileapi/external_mount_points.h" |
| 12 #include "webkit/fileapi/file_system_task_runners.h" |
| 13 #include "webkit/fileapi/isolated_context.h" |
| 14 #include "webkit/fileapi/mock_file_system_options.h" |
| 15 #include "webkit/quota/mock_quota_manager.h" |
| 16 #include "webkit/quota/mock_special_storage_policy.h" |
| 17 |
| 18 #define FPL(x) FILE_PATH_LITERAL(x) |
| 19 |
| 20 #if defined(FILE_PATH_USES_DRIVE_LETTERS) |
| 21 #define DRIVE FPL("C:") |
| 22 #else |
| 23 #define DRIVE |
| 24 #endif |
| 25 |
| 26 namespace fileapi { |
| 27 |
| 28 namespace { |
| 29 |
| 30 const char kTestOrigin[] = "http://chromium.org/"; |
| 31 const FilePath::CharType kVirtualPathNoRoot[] = FPL("root/file"); |
| 32 |
| 33 GURL CreateRawFileSystemURL(const std::string& type_str, |
| 34 const std::string& fs_id) { |
| 35 std::string url_str = base::StringPrintf( |
| 36 "filesystem:http://chromium.org/%s/%s/root/file", |
| 37 type_str.c_str(), |
| 38 fs_id.c_str()); |
| 39 return GURL(url_str); |
| 40 } |
| 41 |
| 42 class FileSystemContextTest : public testing::Test { |
| 43 public: |
| 44 FileSystemContextTest() {} |
| 45 |
| 46 void SetUp() { |
| 47 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); |
| 48 |
| 49 scoped_refptr<quota::SpecialStoragePolicy> storage_policy( |
| 50 new quota::MockSpecialStoragePolicy()); |
| 51 |
| 52 mock_quota_manager_ = new quota::MockQuotaManager( |
| 53 false /* is_incognito */, |
| 54 data_dir_.path(), |
| 55 base::MessageLoopProxy::current(), |
| 56 base::MessageLoopProxy::current(), |
| 57 storage_policy); |
| 58 |
| 59 file_system_context_ = new FileSystemContext( |
| 60 FileSystemTaskRunners::CreateMockTaskRunners(), |
| 61 storage_policy, |
| 62 mock_quota_manager_->proxy(), |
| 63 data_dir_.path(), |
| 64 CreateAllowFileAccessOptions()); |
| 65 } |
| 66 |
| 67 protected: |
| 68 FileSystemContext* file_system_context() { |
| 69 return file_system_context_.get(); |
| 70 } |
| 71 |
| 72 // Verifies a *valid* filesystem url has expected values. |
| 73 void ExpectFileSystemURLMatches(const FileSystemURL& url, |
| 74 const GURL& expect_origin, |
| 75 FileSystemType expect_mount_type, |
| 76 FileSystemType expect_type, |
| 77 const FilePath& expect_path, |
| 78 const FilePath& expect_virtual_path, |
| 79 const std::string& expect_filesystem_id) { |
| 80 ASSERT_TRUE(url.is_valid()); |
| 81 |
| 82 EXPECT_EQ(expect_origin, url.origin()); |
| 83 EXPECT_EQ(expect_mount_type, url.mount_type()); |
| 84 EXPECT_EQ(expect_type, url.type()); |
| 85 EXPECT_EQ(expect_path, url.path()); |
| 86 EXPECT_EQ(expect_virtual_path, url.virtual_path()); |
| 87 EXPECT_EQ(expect_filesystem_id, url.filesystem_id()); |
| 88 } |
| 89 |
| 90 private: |
| 91 base::ScopedTempDir data_dir_; |
| 92 MessageLoop message_loop_; |
| 93 scoped_refptr<quota::MockQuotaManager> mock_quota_manager_; |
| 94 scoped_refptr<FileSystemContext> file_system_context_; |
| 95 }; |
| 96 |
| 97 TEST_F(FileSystemContextTest, CrackFileSystemURL) { |
| 98 // Register an isolated mount point. |
| 99 std::string isolated_file_system_name = "root"; |
| 100 const std::string kIsolatedFileSystemID = |
| 101 IsolatedContext::GetInstance()->RegisterFileSystemForPath( |
| 102 kFileSystemTypeNativeLocal, |
| 103 FilePath(DRIVE FPL("/test/isolated/root")), |
| 104 &isolated_file_system_name); |
| 105 // Register system external mount point. |
| 106 ASSERT_TRUE(ExternalMountPoints::GetSystemInstance()->RegisterFileSystem( |
| 107 "system", |
| 108 kFileSystemTypeDrive, |
| 109 FilePath(DRIVE FPL("/test/sys/")))); |
| 110 // Register a system external mount point with the same name/id as the |
| 111 // registered isolated mount point. |
| 112 ASSERT_TRUE(ExternalMountPoints::GetSystemInstance()->RegisterFileSystem( |
| 113 kIsolatedFileSystemID, |
| 114 kFileSystemTypeRestrictedNativeLocal, |
| 115 FilePath(DRIVE FPL("/test/system/isolated")))); |
| 116 |
| 117 struct TestCase { |
| 118 // Test case values. |
| 119 std::string root; |
| 120 std::string type_str; |
| 121 |
| 122 // Expected test results. |
| 123 bool expect_is_valid; |
| 124 FileSystemType expect_mount_type; |
| 125 FileSystemType expect_type; |
| 126 const FilePath::CharType* expect_path; |
| 127 bool expect_virtual_path_empty; |
| 128 std::string expect_filesystem_id; |
| 129 }; |
| 130 |
| 131 const TestCase kTestCases[] = { |
| 132 // Following should not be handled by the url crackers: |
| 133 { |
| 134 "pers_mount", "persistent", true /* is_valid */, |
| 135 kFileSystemTypePersistent, kFileSystemTypePersistent, |
| 136 FPL("pers_mount/root/file"), true /* virtual path empty */, |
| 137 std::string() /* filesystem id */ |
| 138 }, |
| 139 { |
| 140 "temp_mount", "temporary", true /* is_valid */, |
| 141 kFileSystemTypeTemporary, kFileSystemTypeTemporary, |
| 142 FPL("temp_mount/root/file"), true /* virtual path empty */, |
| 143 std::string() /* filesystem id */ |
| 144 }, |
| 145 // Should be cracked by isolated mount points: |
| 146 { |
| 147 kIsolatedFileSystemID, "isolated", true /* is_valid */, |
| 148 kFileSystemTypeIsolated, kFileSystemTypeNativeLocal, |
| 149 DRIVE FPL("/test/isolated/root/file"), false /* virtual path empty */, |
| 150 kIsolatedFileSystemID |
| 151 }, |
| 152 // Should be cracked by system mount points: |
| 153 { |
| 154 "system", "external", true /* is_valid */, |
| 155 kFileSystemTypeExternal, kFileSystemTypeDrive, |
| 156 DRIVE FPL("/test/sys/root/file"), false /* virtual path empty */, |
| 157 "system" |
| 158 }, |
| 159 { |
| 160 kIsolatedFileSystemID, "external", true /* is_valid */, |
| 161 kFileSystemTypeExternal, kFileSystemTypeRestrictedNativeLocal, |
| 162 DRIVE FPL("/test/system/isolated/root/file"), |
| 163 false /* virtual path empty */, |
| 164 kIsolatedFileSystemID |
| 165 }, |
| 166 // Test for invalid filesystem url (made invalid by adding invalid |
| 167 // filesystem type). |
| 168 { |
| 169 "sytem", "external", false /* is_valid */, |
| 170 // The rest of values will be ignored. |
| 171 kFileSystemTypeUnknown, kFileSystemTypeUnknown, FPL(""), true, |
| 172 std::string() |
| 173 }, |
| 174 // Test for URL with non-existing filesystem id. |
| 175 { |
| 176 "invalid", "external", false /* is_valid */, |
| 177 // The rest of values will be ignored. |
| 178 kFileSystemTypeUnknown, kFileSystemTypeUnknown, FPL(""), true, |
| 179 std::string() |
| 180 }, |
| 181 }; |
| 182 |
| 183 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) { |
| 184 const FilePath virtual_path = |
| 185 FilePath::FromUTF8Unsafe(kTestCases[i].root).Append(kVirtualPathNoRoot); |
| 186 |
| 187 GURL raw_url = |
| 188 CreateRawFileSystemURL(kTestCases[i].type_str, kTestCases[i].root); |
| 189 FileSystemURL cracked_url = file_system_context()->CrackURL(raw_url); |
| 190 |
| 191 SCOPED_TRACE(testing::Message() << "Test case " << i << ": " |
| 192 << "Cracking URL: " << raw_url); |
| 193 |
| 194 EXPECT_EQ(kTestCases[i].expect_is_valid, cracked_url.is_valid()); |
| 195 if (!kTestCases[i].expect_is_valid) |
| 196 continue; |
| 197 |
| 198 ExpectFileSystemURLMatches( |
| 199 cracked_url, |
| 200 GURL(kTestOrigin), |
| 201 kTestCases[i].expect_mount_type, |
| 202 kTestCases[i].expect_type, |
| 203 FilePath(kTestCases[i].expect_path).NormalizePathSeparators(), |
| 204 kTestCases[i].expect_virtual_path_empty ? |
| 205 FilePath() : virtual_path.NormalizePathSeparators(), |
| 206 kTestCases[i].expect_filesystem_id); |
| 207 } |
| 208 |
| 209 IsolatedContext::GetInstance()->RevokeFileSystemByPath( |
| 210 FilePath(DRIVE FPL("/test/isolated/root"))); |
| 211 ExternalMountPoints::GetSystemInstance()->RevokeFileSystem("system"); |
| 212 ExternalMountPoints::GetSystemInstance()->RevokeFileSystem( |
| 213 kIsolatedFileSystemID); |
| 214 } |
| 215 |
| 216 } // namespace |
| 217 |
| 218 } // namespace fileapi |
| 219 |
OLD | NEW |