| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "storage/browser/fileapi/file_system_context.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 | |
| 9 #include "base/files/scoped_temp_dir.h" | |
| 10 #include "base/macros.h" | |
| 11 #include "base/strings/stringprintf.h" | |
| 12 #include "base/threading/thread_task_runner_handle.h" | |
| 13 #include "build/build_config.h" | |
| 14 #include "content/browser/quota/mock_quota_manager.h" | |
| 15 #include "content/public/test/mock_special_storage_policy.h" | |
| 16 #include "content/public/test/test_file_system_options.h" | |
| 17 #include "storage/browser/fileapi/external_mount_points.h" | |
| 18 #include "storage/browser/fileapi/file_system_backend.h" | |
| 19 #include "storage/browser/fileapi/isolated_context.h" | |
| 20 #include "testing/gtest/include/gtest/gtest.h" | |
| 21 | |
| 22 #define FPL(x) FILE_PATH_LITERAL(x) | |
| 23 | |
| 24 #if defined(FILE_PATH_USES_DRIVE_LETTERS) | |
| 25 #define DRIVE FPL("C:") | |
| 26 #else | |
| 27 #define DRIVE | |
| 28 #endif | |
| 29 | |
| 30 using storage::ExternalMountPoints; | |
| 31 using storage::FileSystemBackend; | |
| 32 using storage::FileSystemContext; | |
| 33 using storage::FileSystemMountOption; | |
| 34 using storage::FileSystemURL; | |
| 35 using storage::IsolatedContext; | |
| 36 | |
| 37 namespace content { | |
| 38 | |
| 39 namespace { | |
| 40 | |
| 41 const char kTestOrigin[] = "http://chromium.org/"; | |
| 42 | |
| 43 GURL CreateRawFileSystemURL(const std::string& type_str, | |
| 44 const std::string& fs_id) { | |
| 45 std::string url_str = base::StringPrintf( | |
| 46 "filesystem:http://chromium.org/%s/%s/root/file", | |
| 47 type_str.c_str(), | |
| 48 fs_id.c_str()); | |
| 49 return GURL(url_str); | |
| 50 } | |
| 51 | |
| 52 class FileSystemContextTest : public testing::Test { | |
| 53 public: | |
| 54 FileSystemContextTest() {} | |
| 55 | |
| 56 void SetUp() override { | |
| 57 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); | |
| 58 | |
| 59 storage_policy_ = new MockSpecialStoragePolicy(); | |
| 60 | |
| 61 mock_quota_manager_ = new MockQuotaManager( | |
| 62 false /* is_incognito */, data_dir_.GetPath(), | |
| 63 base::ThreadTaskRunnerHandle::Get().get(), | |
| 64 base::ThreadTaskRunnerHandle::Get().get(), storage_policy_.get()); | |
| 65 } | |
| 66 | |
| 67 protected: | |
| 68 FileSystemContext* CreateFileSystemContextForTest( | |
| 69 storage::ExternalMountPoints* external_mount_points) { | |
| 70 return new FileSystemContext( | |
| 71 base::ThreadTaskRunnerHandle::Get().get(), | |
| 72 base::ThreadTaskRunnerHandle::Get().get(), external_mount_points, | |
| 73 storage_policy_.get(), mock_quota_manager_->proxy(), | |
| 74 ScopedVector<FileSystemBackend>(), | |
| 75 std::vector<storage::URLRequestAutoMountHandler>(), data_dir_.GetPath(), | |
| 76 CreateAllowFileAccessOptions()); | |
| 77 } | |
| 78 | |
| 79 // Verifies a *valid* filesystem url has expected values. | |
| 80 void ExpectFileSystemURLMatches(const FileSystemURL& url, | |
| 81 const GURL& expect_origin, | |
| 82 storage::FileSystemType expect_mount_type, | |
| 83 storage::FileSystemType expect_type, | |
| 84 const base::FilePath& expect_path, | |
| 85 const base::FilePath& expect_virtual_path, | |
| 86 const std::string& expect_filesystem_id) { | |
| 87 EXPECT_TRUE(url.is_valid()); | |
| 88 | |
| 89 EXPECT_EQ(expect_origin, url.origin()); | |
| 90 EXPECT_EQ(expect_mount_type, url.mount_type()); | |
| 91 EXPECT_EQ(expect_type, url.type()); | |
| 92 EXPECT_EQ(expect_path, url.path()); | |
| 93 EXPECT_EQ(expect_virtual_path, url.virtual_path()); | |
| 94 EXPECT_EQ(expect_filesystem_id, url.filesystem_id()); | |
| 95 } | |
| 96 | |
| 97 private: | |
| 98 base::ScopedTempDir data_dir_; | |
| 99 base::MessageLoop message_loop_; | |
| 100 scoped_refptr<storage::SpecialStoragePolicy> storage_policy_; | |
| 101 scoped_refptr<MockQuotaManager> mock_quota_manager_; | |
| 102 }; | |
| 103 | |
| 104 // It is not valid to pass NULL ExternalMountPoints to FileSystemContext on | |
| 105 // ChromeOS. | |
| 106 #if !defined(OS_CHROMEOS) | |
| 107 TEST_F(FileSystemContextTest, NullExternalMountPoints) { | |
| 108 scoped_refptr<FileSystemContext> file_system_context( | |
| 109 CreateFileSystemContextForTest(NULL)); | |
| 110 | |
| 111 // Cracking system external mount and isolated mount points should work. | |
| 112 std::string isolated_name = "root"; | |
| 113 std::string isolated_id = | |
| 114 IsolatedContext::GetInstance()->RegisterFileSystemForPath( | |
| 115 storage::kFileSystemTypeNativeLocal, | |
| 116 std::string(), | |
| 117 base::FilePath(DRIVE FPL("/test/isolated/root")), | |
| 118 &isolated_name); | |
| 119 // Register system external mount point. | |
| 120 ASSERT_TRUE(ExternalMountPoints::GetSystemInstance()->RegisterFileSystem( | |
| 121 "system", | |
| 122 storage::kFileSystemTypeNativeLocal, | |
| 123 FileSystemMountOption(), | |
| 124 base::FilePath(DRIVE FPL("/test/sys/")))); | |
| 125 | |
| 126 FileSystemURL cracked_isolated = file_system_context->CrackURL( | |
| 127 CreateRawFileSystemURL("isolated", isolated_id)); | |
| 128 | |
| 129 ExpectFileSystemURLMatches( | |
| 130 cracked_isolated, | |
| 131 GURL(kTestOrigin), | |
| 132 storage::kFileSystemTypeIsolated, | |
| 133 storage::kFileSystemTypeNativeLocal, | |
| 134 base::FilePath(DRIVE FPL("/test/isolated/root/file")) | |
| 135 .NormalizePathSeparators(), | |
| 136 base::FilePath::FromUTF8Unsafe(isolated_id) | |
| 137 .Append(FPL("root/file")) | |
| 138 .NormalizePathSeparators(), | |
| 139 isolated_id); | |
| 140 | |
| 141 FileSystemURL cracked_external = file_system_context->CrackURL( | |
| 142 CreateRawFileSystemURL("external", "system")); | |
| 143 | |
| 144 ExpectFileSystemURLMatches( | |
| 145 cracked_external, | |
| 146 GURL(kTestOrigin), | |
| 147 storage::kFileSystemTypeExternal, | |
| 148 storage::kFileSystemTypeNativeLocal, | |
| 149 base::FilePath(DRIVE FPL("/test/sys/root/file")) | |
| 150 .NormalizePathSeparators(), | |
| 151 base::FilePath(FPL("system/root/file")).NormalizePathSeparators(), | |
| 152 "system"); | |
| 153 | |
| 154 IsolatedContext::GetInstance()->RevokeFileSystem(isolated_id); | |
| 155 ExternalMountPoints::GetSystemInstance()->RevokeFileSystem("system"); | |
| 156 } | |
| 157 #endif // !defiend(OS_CHROMEOS) | |
| 158 | |
| 159 TEST_F(FileSystemContextTest, FileSystemContextKeepsMountPointsAlive) { | |
| 160 scoped_refptr<ExternalMountPoints> mount_points = | |
| 161 ExternalMountPoints::CreateRefCounted(); | |
| 162 | |
| 163 // Register system external mount point. | |
| 164 ASSERT_TRUE(mount_points->RegisterFileSystem( | |
| 165 "system", | |
| 166 storage::kFileSystemTypeNativeLocal, | |
| 167 FileSystemMountOption(), | |
| 168 base::FilePath(DRIVE FPL("/test/sys/")))); | |
| 169 | |
| 170 scoped_refptr<FileSystemContext> file_system_context( | |
| 171 CreateFileSystemContextForTest(mount_points.get())); | |
| 172 | |
| 173 // Release a MountPoints reference created in the test. | |
| 174 mount_points = NULL; | |
| 175 | |
| 176 // FileSystemContext should keep a reference to the |mount_points|, so it | |
| 177 // should be able to resolve the URL. | |
| 178 FileSystemURL cracked_external = file_system_context->CrackURL( | |
| 179 CreateRawFileSystemURL("external", "system")); | |
| 180 | |
| 181 ExpectFileSystemURLMatches( | |
| 182 cracked_external, | |
| 183 GURL(kTestOrigin), | |
| 184 storage::kFileSystemTypeExternal, | |
| 185 storage::kFileSystemTypeNativeLocal, | |
| 186 base::FilePath(DRIVE FPL("/test/sys/root/file")) | |
| 187 .NormalizePathSeparators(), | |
| 188 base::FilePath(FPL("system/root/file")).NormalizePathSeparators(), | |
| 189 "system"); | |
| 190 | |
| 191 // No need to revoke the registered filesystem since |mount_points| lifetime | |
| 192 // is bound to this test. | |
| 193 } | |
| 194 | |
| 195 TEST_F(FileSystemContextTest, CrackFileSystemURL) { | |
| 196 scoped_refptr<ExternalMountPoints> external_mount_points( | |
| 197 ExternalMountPoints::CreateRefCounted()); | |
| 198 scoped_refptr<FileSystemContext> file_system_context( | |
| 199 CreateFileSystemContextForTest(external_mount_points.get())); | |
| 200 | |
| 201 // Register an isolated mount point. | |
| 202 std::string isolated_file_system_name = "root"; | |
| 203 const std::string kIsolatedFileSystemID = | |
| 204 IsolatedContext::GetInstance()->RegisterFileSystemForPath( | |
| 205 storage::kFileSystemTypeNativeLocal, | |
| 206 std::string(), | |
| 207 base::FilePath(DRIVE FPL("/test/isolated/root")), | |
| 208 &isolated_file_system_name); | |
| 209 // Register system external mount point. | |
| 210 ASSERT_TRUE(ExternalMountPoints::GetSystemInstance()->RegisterFileSystem( | |
| 211 "system", | |
| 212 storage::kFileSystemTypeDrive, | |
| 213 FileSystemMountOption(), | |
| 214 base::FilePath(DRIVE FPL("/test/sys/")))); | |
| 215 ASSERT_TRUE(ExternalMountPoints::GetSystemInstance()->RegisterFileSystem( | |
| 216 "ext", | |
| 217 storage::kFileSystemTypeNativeLocal, | |
| 218 FileSystemMountOption(), | |
| 219 base::FilePath(DRIVE FPL("/test/ext")))); | |
| 220 // Register a system external mount point with the same name/id as the | |
| 221 // registered isolated mount point. | |
| 222 ASSERT_TRUE(ExternalMountPoints::GetSystemInstance()->RegisterFileSystem( | |
| 223 kIsolatedFileSystemID, | |
| 224 storage::kFileSystemTypeRestrictedNativeLocal, | |
| 225 FileSystemMountOption(), | |
| 226 base::FilePath(DRIVE FPL("/test/system/isolated")))); | |
| 227 // Add a mount points with the same name as a system mount point to | |
| 228 // FileSystemContext's external mount points. | |
| 229 ASSERT_TRUE(external_mount_points->RegisterFileSystem( | |
| 230 "ext", | |
| 231 storage::kFileSystemTypeNativeLocal, | |
| 232 FileSystemMountOption(), | |
| 233 base::FilePath(DRIVE FPL("/test/local/ext/")))); | |
| 234 | |
| 235 const GURL kTestOrigin = GURL("http://chromium.org/"); | |
| 236 const base::FilePath kVirtualPathNoRoot = base::FilePath(FPL("root/file")); | |
| 237 | |
| 238 struct TestCase { | |
| 239 // Test case values. | |
| 240 std::string root; | |
| 241 std::string type_str; | |
| 242 | |
| 243 // Expected test results. | |
| 244 bool expect_is_valid; | |
| 245 storage::FileSystemType expect_mount_type; | |
| 246 storage::FileSystemType expect_type; | |
| 247 const base::FilePath::CharType* expect_path; | |
| 248 std::string expect_filesystem_id; | |
| 249 }; | |
| 250 | |
| 251 const TestCase kTestCases[] = { | |
| 252 // Following should not be handled by the url crackers: | |
| 253 { | |
| 254 "pers_mount", "persistent", true /* is_valid */, | |
| 255 storage::kFileSystemTypePersistent, storage::kFileSystemTypePersistent, | |
| 256 FPL("pers_mount/root/file"), std::string() /* filesystem id */ | |
| 257 }, | |
| 258 { | |
| 259 "temp_mount", "temporary", true /* is_valid */, | |
| 260 storage::kFileSystemTypeTemporary, storage::kFileSystemTypeTemporary, | |
| 261 FPL("temp_mount/root/file"), std::string() /* filesystem id */ | |
| 262 }, | |
| 263 // Should be cracked by isolated mount points: | |
| 264 {kIsolatedFileSystemID, "isolated", true /* is_valid */, | |
| 265 storage::kFileSystemTypeIsolated, storage::kFileSystemTypeNativeLocal, | |
| 266 DRIVE FPL("/test/isolated/root/file"), kIsolatedFileSystemID}, | |
| 267 // Should be cracked by system mount points: | |
| 268 {"system", "external", true /* is_valid */, | |
| 269 storage::kFileSystemTypeExternal, storage::kFileSystemTypeDrive, | |
| 270 DRIVE FPL("/test/sys/root/file"), "system"}, | |
| 271 {kIsolatedFileSystemID, "external", true /* is_valid */, | |
| 272 storage::kFileSystemTypeExternal, | |
| 273 storage::kFileSystemTypeRestrictedNativeLocal, | |
| 274 DRIVE FPL("/test/system/isolated/root/file"), kIsolatedFileSystemID}, | |
| 275 // Should be cracked by FileSystemContext's ExternalMountPoints. | |
| 276 {"ext", "external", true /* is_valid */, storage::kFileSystemTypeExternal, | |
| 277 storage::kFileSystemTypeNativeLocal, | |
| 278 DRIVE FPL("/test/local/ext/root/file"), "ext"}, | |
| 279 // Test for invalid filesystem url (made invalid by adding invalid | |
| 280 // filesystem type). | |
| 281 {"sytem", "external", false /* is_valid */, | |
| 282 // The rest of values will be ignored. | |
| 283 storage::kFileSystemTypeUnknown, storage::kFileSystemTypeUnknown, | |
| 284 FPL(""), std::string()}, | |
| 285 // Test for URL with non-existing filesystem id. | |
| 286 {"invalid", "external", false /* is_valid */, | |
| 287 // The rest of values will be ignored. | |
| 288 storage::kFileSystemTypeUnknown, storage::kFileSystemTypeUnknown, | |
| 289 FPL(""), std::string()}, | |
| 290 }; | |
| 291 | |
| 292 for (size_t i = 0; i < arraysize(kTestCases); ++i) { | |
| 293 const base::FilePath virtual_path = | |
| 294 base::FilePath::FromUTF8Unsafe( | |
| 295 kTestCases[i].root).Append(kVirtualPathNoRoot); | |
| 296 | |
| 297 GURL raw_url = | |
| 298 CreateRawFileSystemURL(kTestCases[i].type_str, kTestCases[i].root); | |
| 299 FileSystemURL cracked_url = file_system_context->CrackURL(raw_url); | |
| 300 | |
| 301 SCOPED_TRACE(testing::Message() << "Test case " << i << ": " | |
| 302 << "Cracking URL: " << raw_url); | |
| 303 | |
| 304 EXPECT_EQ(kTestCases[i].expect_is_valid, cracked_url.is_valid()); | |
| 305 if (!kTestCases[i].expect_is_valid) | |
| 306 continue; | |
| 307 | |
| 308 ExpectFileSystemURLMatches( | |
| 309 cracked_url, | |
| 310 GURL(kTestOrigin), | |
| 311 kTestCases[i].expect_mount_type, | |
| 312 kTestCases[i].expect_type, | |
| 313 base::FilePath(kTestCases[i].expect_path).NormalizePathSeparators(), | |
| 314 virtual_path.NormalizePathSeparators(), | |
| 315 kTestCases[i].expect_filesystem_id); | |
| 316 } | |
| 317 | |
| 318 IsolatedContext::GetInstance()->RevokeFileSystemByPath( | |
| 319 base::FilePath(DRIVE FPL("/test/isolated/root"))); | |
| 320 ExternalMountPoints::GetSystemInstance()->RevokeFileSystem("system"); | |
| 321 ExternalMountPoints::GetSystemInstance()->RevokeFileSystem("ext"); | |
| 322 ExternalMountPoints::GetSystemInstance()->RevokeFileSystem( | |
| 323 kIsolatedFileSystemID); | |
| 324 } | |
| 325 | |
| 326 TEST_F(FileSystemContextTest, CanServeURLRequest) { | |
| 327 scoped_refptr<ExternalMountPoints> external_mount_points( | |
| 328 ExternalMountPoints::CreateRefCounted()); | |
| 329 scoped_refptr<FileSystemContext> context( | |
| 330 CreateFileSystemContextForTest(external_mount_points.get())); | |
| 331 | |
| 332 // A request for a sandbox mount point should be served. | |
| 333 FileSystemURL cracked_url = | |
| 334 context->CrackURL(CreateRawFileSystemURL("persistent", "pers_mount")); | |
| 335 EXPECT_EQ(storage::kFileSystemTypePersistent, cracked_url.mount_type()); | |
| 336 EXPECT_TRUE(context->CanServeURLRequest(cracked_url)); | |
| 337 | |
| 338 // A request for an isolated mount point should NOT be served. | |
| 339 std::string isolated_fs_name = "root"; | |
| 340 std::string isolated_fs_id = | |
| 341 IsolatedContext::GetInstance()->RegisterFileSystemForPath( | |
| 342 storage::kFileSystemTypeNativeLocal, | |
| 343 std::string(), | |
| 344 base::FilePath(DRIVE FPL("/test/isolated/root")), | |
| 345 &isolated_fs_name); | |
| 346 cracked_url = context->CrackURL( | |
| 347 CreateRawFileSystemURL("isolated", isolated_fs_id)); | |
| 348 EXPECT_EQ(storage::kFileSystemTypeIsolated, cracked_url.mount_type()); | |
| 349 EXPECT_FALSE(context->CanServeURLRequest(cracked_url)); | |
| 350 | |
| 351 // A request for an external mount point should be served. | |
| 352 const std::string kExternalMountName = "ext_mount"; | |
| 353 ASSERT_TRUE(ExternalMountPoints::GetSystemInstance()->RegisterFileSystem( | |
| 354 kExternalMountName, | |
| 355 storage::kFileSystemTypeDrive, | |
| 356 FileSystemMountOption(), | |
| 357 base::FilePath())); | |
| 358 cracked_url = context->CrackURL( | |
| 359 CreateRawFileSystemURL("external", kExternalMountName)); | |
| 360 EXPECT_EQ(storage::kFileSystemTypeExternal, cracked_url.mount_type()); | |
| 361 EXPECT_TRUE(context->CanServeURLRequest(cracked_url)); | |
| 362 | |
| 363 ExternalMountPoints::GetSystemInstance()->RevokeFileSystem( | |
| 364 kExternalMountName); | |
| 365 IsolatedContext::GetInstance()->RevokeFileSystem(isolated_fs_id); | |
| 366 } | |
| 367 | |
| 368 // Ensures that a backend exists for each common isolated file system type. | |
| 369 // See http://crbug.com/447027 | |
| 370 TEST_F(FileSystemContextTest, IsolatedFileSystemsTypesHandled) { | |
| 371 // This does not provide any "additional" file system handlers. In particular, | |
| 372 // on Chrome OS it does not provide chromeos::FileSystemBackend. | |
| 373 scoped_refptr<FileSystemContext> file_system_context( | |
| 374 CreateFileSystemContextForTest(nullptr)); | |
| 375 | |
| 376 // Isolated file system types are handled. | |
| 377 EXPECT_TRUE(file_system_context->GetFileSystemBackend( | |
| 378 storage::kFileSystemTypeIsolated)); | |
| 379 EXPECT_TRUE(file_system_context->GetFileSystemBackend( | |
| 380 storage::kFileSystemTypeDragged)); | |
| 381 EXPECT_TRUE(file_system_context->GetFileSystemBackend( | |
| 382 storage::kFileSystemTypeForTransientFile)); | |
| 383 EXPECT_TRUE(file_system_context->GetFileSystemBackend( | |
| 384 storage::kFileSystemTypeNativeLocal)); | |
| 385 EXPECT_TRUE(file_system_context->GetFileSystemBackend( | |
| 386 storage::kFileSystemTypeNativeForPlatformApp)); | |
| 387 } | |
| 388 | |
| 389 } // namespace | |
| 390 | |
| 391 } // namespace content | |
| OLD | NEW |