| 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/browser/fileapi/file_system_context.h" | |
| 6 | |
| 7 #include "base/files/scoped_temp_dir.h" | |
| 8 #include "base/message_loop/message_loop.h" | |
| 9 #include "base/strings/stringprintf.h" | |
| 10 #include "testing/gtest/include/gtest/gtest.h" | |
| 11 #include "webkit/browser/fileapi/external_mount_points.h" | |
| 12 #include "webkit/browser/fileapi/file_system_backend.h" | |
| 13 #include "webkit/browser/fileapi/isolated_context.h" | |
| 14 #include "webkit/browser/fileapi/mock_file_system_options.h" | |
| 15 #include "webkit/browser/quota/mock_quota_manager.h" | |
| 16 #include "webkit/browser/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 | |
| 32 GURL CreateRawFileSystemURL(const std::string& type_str, | |
| 33 const std::string& fs_id) { | |
| 34 std::string url_str = base::StringPrintf( | |
| 35 "filesystem:http://chromium.org/%s/%s/root/file", | |
| 36 type_str.c_str(), | |
| 37 fs_id.c_str()); | |
| 38 return GURL(url_str); | |
| 39 } | |
| 40 | |
| 41 class FileSystemContextTest : public testing::Test { | |
| 42 public: | |
| 43 FileSystemContextTest() {} | |
| 44 | |
| 45 virtual void SetUp() { | |
| 46 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); | |
| 47 | |
| 48 storage_policy_ = new quota::MockSpecialStoragePolicy(); | |
| 49 | |
| 50 mock_quota_manager_ = | |
| 51 new quota::MockQuotaManager(false /* is_incognito */, | |
| 52 data_dir_.path(), | |
| 53 base::MessageLoopProxy::current().get(), | |
| 54 base::MessageLoopProxy::current().get(), | |
| 55 storage_policy_.get()); | |
| 56 } | |
| 57 | |
| 58 protected: | |
| 59 FileSystemContext* CreateFileSystemContextForTest( | |
| 60 ExternalMountPoints* external_mount_points) { | |
| 61 return new FileSystemContext(base::MessageLoopProxy::current().get(), | |
| 62 base::MessageLoopProxy::current().get(), | |
| 63 external_mount_points, | |
| 64 storage_policy_.get(), | |
| 65 mock_quota_manager_->proxy(), | |
| 66 ScopedVector<FileSystemBackend>(), | |
| 67 data_dir_.path(), | |
| 68 CreateAllowFileAccessOptions()); | |
| 69 } | |
| 70 | |
| 71 // Verifies a *valid* filesystem url has expected values. | |
| 72 void ExpectFileSystemURLMatches(const FileSystemURL& url, | |
| 73 const GURL& expect_origin, | |
| 74 FileSystemType expect_mount_type, | |
| 75 FileSystemType expect_type, | |
| 76 const base::FilePath& expect_path, | |
| 77 const base::FilePath& expect_virtual_path, | |
| 78 const std::string& expect_filesystem_id) { | |
| 79 EXPECT_TRUE(url.is_valid()); | |
| 80 | |
| 81 EXPECT_EQ(expect_origin, url.origin()); | |
| 82 EXPECT_EQ(expect_mount_type, url.mount_type()); | |
| 83 EXPECT_EQ(expect_type, url.type()); | |
| 84 EXPECT_EQ(expect_path, url.path()); | |
| 85 EXPECT_EQ(expect_virtual_path, url.virtual_path()); | |
| 86 EXPECT_EQ(expect_filesystem_id, url.filesystem_id()); | |
| 87 } | |
| 88 | |
| 89 private: | |
| 90 base::ScopedTempDir data_dir_; | |
| 91 base::MessageLoop message_loop_; | |
| 92 scoped_refptr<quota::SpecialStoragePolicy> storage_policy_; | |
| 93 scoped_refptr<quota::MockQuotaManager> mock_quota_manager_; | |
| 94 }; | |
| 95 | |
| 96 // It is not valid to pass NULL ExternalMountPoints to FileSystemContext on | |
| 97 // ChromeOS. | |
| 98 #if !defined(OS_CHROMEOS) | |
| 99 TEST_F(FileSystemContextTest, NullExternalMountPoints) { | |
| 100 scoped_refptr<FileSystemContext> file_system_context( | |
| 101 CreateFileSystemContextForTest(NULL)); | |
| 102 | |
| 103 // Cracking system external mount and isolated mount points should work. | |
| 104 std::string isolated_name = "root"; | |
| 105 std::string isolated_id = | |
| 106 IsolatedContext::GetInstance()->RegisterFileSystemForPath( | |
| 107 kFileSystemTypeNativeLocal, | |
| 108 base::FilePath(DRIVE FPL("/test/isolated/root")), | |
| 109 &isolated_name); | |
| 110 // Register system external mount point. | |
| 111 ASSERT_TRUE(ExternalMountPoints::GetSystemInstance()->RegisterFileSystem( | |
| 112 "system", | |
| 113 kFileSystemTypeNativeLocal, | |
| 114 base::FilePath(DRIVE FPL("/test/sys/")))); | |
| 115 | |
| 116 FileSystemURL cracked_isolated = file_system_context->CrackURL( | |
| 117 CreateRawFileSystemURL("isolated", isolated_id)); | |
| 118 | |
| 119 ExpectFileSystemURLMatches( | |
| 120 cracked_isolated, | |
| 121 GURL(kTestOrigin), | |
| 122 kFileSystemTypeIsolated, | |
| 123 kFileSystemTypeNativeLocal, | |
| 124 base::FilePath(DRIVE FPL("/test/isolated/root/file")).NormalizePathSeparat
ors(), | |
| 125 base::FilePath::FromUTF8Unsafe(isolated_id).Append(FPL("root/file")). | |
| 126 NormalizePathSeparators(), | |
| 127 isolated_id); | |
| 128 | |
| 129 FileSystemURL cracked_external = file_system_context->CrackURL( | |
| 130 CreateRawFileSystemURL("external", "system")); | |
| 131 | |
| 132 ExpectFileSystemURLMatches( | |
| 133 cracked_external, | |
| 134 GURL(kTestOrigin), | |
| 135 kFileSystemTypeExternal, | |
| 136 kFileSystemTypeNativeLocal, | |
| 137 base::FilePath( | |
| 138 DRIVE FPL("/test/sys/root/file")).NormalizePathSeparators(), | |
| 139 base::FilePath(FPL("system/root/file")).NormalizePathSeparators(), | |
| 140 "system"); | |
| 141 | |
| 142 | |
| 143 IsolatedContext::GetInstance()->RevokeFileSystem(isolated_id); | |
| 144 ExternalMountPoints::GetSystemInstance()->RevokeFileSystem("system"); | |
| 145 } | |
| 146 #endif // !defiend(OS_CHROMEOS) | |
| 147 | |
| 148 TEST_F(FileSystemContextTest, FileSystemContextKeepsMountPointsAlive) { | |
| 149 scoped_refptr<ExternalMountPoints> mount_points = | |
| 150 ExternalMountPoints::CreateRefCounted(); | |
| 151 | |
| 152 // Register system external mount point. | |
| 153 ASSERT_TRUE(mount_points->RegisterFileSystem( | |
| 154 "system", | |
| 155 kFileSystemTypeNativeLocal, | |
| 156 base::FilePath(DRIVE FPL("/test/sys/")))); | |
| 157 | |
| 158 scoped_refptr<FileSystemContext> file_system_context( | |
| 159 CreateFileSystemContextForTest(mount_points.get())); | |
| 160 | |
| 161 // Release a MountPoints reference created in the test. | |
| 162 mount_points = NULL; | |
| 163 | |
| 164 // FileSystemContext should keep a reference to the |mount_points|, so it | |
| 165 // should be able to resolve the URL. | |
| 166 FileSystemURL cracked_external = file_system_context->CrackURL( | |
| 167 CreateRawFileSystemURL("external", "system")); | |
| 168 | |
| 169 ExpectFileSystemURLMatches( | |
| 170 cracked_external, | |
| 171 GURL(kTestOrigin), | |
| 172 kFileSystemTypeExternal, | |
| 173 kFileSystemTypeNativeLocal, | |
| 174 base::FilePath( | |
| 175 DRIVE FPL("/test/sys/root/file")).NormalizePathSeparators(), | |
| 176 base::FilePath(FPL("system/root/file")).NormalizePathSeparators(), | |
| 177 "system"); | |
| 178 | |
| 179 // No need to revoke the registered filesystem since |mount_points| lifetime | |
| 180 // is bound to this test. | |
| 181 } | |
| 182 | |
| 183 TEST_F(FileSystemContextTest, CrackFileSystemURL) { | |
| 184 scoped_refptr<ExternalMountPoints> external_mount_points( | |
| 185 ExternalMountPoints::CreateRefCounted()); | |
| 186 scoped_refptr<FileSystemContext> file_system_context( | |
| 187 CreateFileSystemContextForTest(external_mount_points.get())); | |
| 188 | |
| 189 // Register an isolated mount point. | |
| 190 std::string isolated_file_system_name = "root"; | |
| 191 const std::string kIsolatedFileSystemID = | |
| 192 IsolatedContext::GetInstance()->RegisterFileSystemForPath( | |
| 193 kFileSystemTypeNativeLocal, | |
| 194 base::FilePath(DRIVE FPL("/test/isolated/root")), | |
| 195 &isolated_file_system_name); | |
| 196 // Register system external mount point. | |
| 197 ASSERT_TRUE(ExternalMountPoints::GetSystemInstance()->RegisterFileSystem( | |
| 198 "system", | |
| 199 kFileSystemTypeDrive, | |
| 200 base::FilePath(DRIVE FPL("/test/sys/")))); | |
| 201 ASSERT_TRUE(ExternalMountPoints::GetSystemInstance()->RegisterFileSystem( | |
| 202 "ext", | |
| 203 kFileSystemTypeNativeLocal, | |
| 204 base::FilePath(DRIVE FPL("/test/ext")))); | |
| 205 // Register a system external mount point with the same name/id as the | |
| 206 // registered isolated mount point. | |
| 207 ASSERT_TRUE(ExternalMountPoints::GetSystemInstance()->RegisterFileSystem( | |
| 208 kIsolatedFileSystemID, | |
| 209 kFileSystemTypeRestrictedNativeLocal, | |
| 210 base::FilePath(DRIVE FPL("/test/system/isolated")))); | |
| 211 // Add a mount points with the same name as a system mount point to | |
| 212 // FileSystemContext's external mount points. | |
| 213 ASSERT_TRUE(external_mount_points->RegisterFileSystem( | |
| 214 "ext", | |
| 215 kFileSystemTypeNativeLocal, | |
| 216 base::FilePath(DRIVE FPL("/test/local/ext/")))); | |
| 217 | |
| 218 const GURL kTestOrigin = GURL("http://chromium.org/"); | |
| 219 const base::FilePath kVirtualPathNoRoot = base::FilePath(FPL("root/file")); | |
| 220 | |
| 221 struct TestCase { | |
| 222 // Test case values. | |
| 223 std::string root; | |
| 224 std::string type_str; | |
| 225 | |
| 226 // Expected test results. | |
| 227 bool expect_is_valid; | |
| 228 FileSystemType expect_mount_type; | |
| 229 FileSystemType expect_type; | |
| 230 const base::FilePath::CharType* expect_path; | |
| 231 std::string expect_filesystem_id; | |
| 232 }; | |
| 233 | |
| 234 const TestCase kTestCases[] = { | |
| 235 // Following should not be handled by the url crackers: | |
| 236 { | |
| 237 "pers_mount", "persistent", true /* is_valid */, | |
| 238 kFileSystemTypePersistent, kFileSystemTypePersistent, | |
| 239 FPL("pers_mount/root/file"), | |
| 240 std::string() /* filesystem id */ | |
| 241 }, | |
| 242 { | |
| 243 "temp_mount", "temporary", true /* is_valid */, | |
| 244 kFileSystemTypeTemporary, kFileSystemTypeTemporary, | |
| 245 FPL("temp_mount/root/file"), | |
| 246 std::string() /* filesystem id */ | |
| 247 }, | |
| 248 // Should be cracked by isolated mount points: | |
| 249 { | |
| 250 kIsolatedFileSystemID, "isolated", true /* is_valid */, | |
| 251 kFileSystemTypeIsolated, kFileSystemTypeNativeLocal, | |
| 252 DRIVE FPL("/test/isolated/root/file"), | |
| 253 kIsolatedFileSystemID | |
| 254 }, | |
| 255 // Should be cracked by system mount points: | |
| 256 { | |
| 257 "system", "external", true /* is_valid */, | |
| 258 kFileSystemTypeExternal, kFileSystemTypeDrive, | |
| 259 DRIVE FPL("/test/sys/root/file"), | |
| 260 "system" | |
| 261 }, | |
| 262 { | |
| 263 kIsolatedFileSystemID, "external", true /* is_valid */, | |
| 264 kFileSystemTypeExternal, kFileSystemTypeRestrictedNativeLocal, | |
| 265 DRIVE FPL("/test/system/isolated/root/file"), | |
| 266 kIsolatedFileSystemID | |
| 267 }, | |
| 268 // Should be cracked by FileSystemContext's ExternalMountPoints. | |
| 269 { | |
| 270 "ext", "external", true /* is_valid */, | |
| 271 kFileSystemTypeExternal, kFileSystemTypeNativeLocal, | |
| 272 DRIVE FPL("/test/local/ext/root/file"), | |
| 273 "ext" | |
| 274 }, | |
| 275 // Test for invalid filesystem url (made invalid by adding invalid | |
| 276 // filesystem type). | |
| 277 { | |
| 278 "sytem", "external", false /* is_valid */, | |
| 279 // The rest of values will be ignored. | |
| 280 kFileSystemTypeUnknown, kFileSystemTypeUnknown, FPL(""), | |
| 281 std::string() | |
| 282 }, | |
| 283 // Test for URL with non-existing filesystem id. | |
| 284 { | |
| 285 "invalid", "external", false /* is_valid */, | |
| 286 // The rest of values will be ignored. | |
| 287 kFileSystemTypeUnknown, kFileSystemTypeUnknown, FPL(""), | |
| 288 std::string() | |
| 289 }, | |
| 290 }; | |
| 291 | |
| 292 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) { | |
| 293 const base::FilePath virtual_path = | |
| 294 base::FilePath::FromUTF8Unsafe(kTestCases[i].root).Append(kVirtualPathNo
Root); | |
| 295 | |
| 296 GURL raw_url = | |
| 297 CreateRawFileSystemURL(kTestCases[i].type_str, kTestCases[i].root); | |
| 298 FileSystemURL cracked_url = file_system_context->CrackURL(raw_url); | |
| 299 | |
| 300 SCOPED_TRACE(testing::Message() << "Test case " << i << ": " | |
| 301 << "Cracking URL: " << raw_url); | |
| 302 | |
| 303 EXPECT_EQ(kTestCases[i].expect_is_valid, cracked_url.is_valid()); | |
| 304 if (!kTestCases[i].expect_is_valid) | |
| 305 continue; | |
| 306 | |
| 307 ExpectFileSystemURLMatches( | |
| 308 cracked_url, | |
| 309 GURL(kTestOrigin), | |
| 310 kTestCases[i].expect_mount_type, | |
| 311 kTestCases[i].expect_type, | |
| 312 base::FilePath(kTestCases[i].expect_path).NormalizePathSeparators(), | |
| 313 virtual_path.NormalizePathSeparators(), | |
| 314 kTestCases[i].expect_filesystem_id); | |
| 315 } | |
| 316 | |
| 317 IsolatedContext::GetInstance()->RevokeFileSystemByPath( | |
| 318 base::FilePath(DRIVE FPL("/test/isolated/root"))); | |
| 319 ExternalMountPoints::GetSystemInstance()->RevokeFileSystem("system"); | |
| 320 ExternalMountPoints::GetSystemInstance()->RevokeFileSystem("ext"); | |
| 321 ExternalMountPoints::GetSystemInstance()->RevokeFileSystem( | |
| 322 kIsolatedFileSystemID); | |
| 323 } | |
| 324 | |
| 325 } // namespace | |
| 326 | |
| 327 } // namespace fileapi | |
| OLD | NEW |