OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 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/external_mount_points.h" | 5 #include "webkit/fileapi/external_mount_points.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/file_path.h" | 9 #include "base/file_path.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 #include "webkit/fileapi/file_system_url.h" |
11 | 12 |
12 #define FPL FILE_PATH_LITERAL | 13 #define FPL FILE_PATH_LITERAL |
13 | 14 |
14 #if defined(FILE_PATH_USES_DRIVE_LETTERS) | 15 #if defined(FILE_PATH_USES_DRIVE_LETTERS) |
15 #define DRIVE FPL("C:") | 16 #define DRIVE FPL("C:") |
16 #else | 17 #else |
17 #define DRIVE | 18 #define DRIVE |
18 #endif | 19 #endif |
19 | 20 |
| 21 using fileapi::FileSystemURL; |
| 22 |
20 namespace { | 23 namespace { |
21 | 24 |
22 TEST(ExternalMountPointsTest, AddMountPoint) { | 25 TEST(ExternalMountPointsTest, AddMountPoint) { |
23 scoped_refptr<fileapi::ExternalMountPoints> mount_points( | 26 scoped_refptr<fileapi::ExternalMountPoints> mount_points( |
24 fileapi::ExternalMountPoints::CreateRefCounted()); | 27 fileapi::ExternalMountPoints::CreateRefCounted()); |
25 | 28 |
26 struct TestCase { | 29 struct TestCase { |
27 // The mount point's name. | 30 // The mount point's name. |
28 const char* const name; | 31 const char* const name; |
29 // The mount point's path. | 32 // The mount point's path. |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 // fails. | 219 // fails. |
217 if (!kTestCases[i].success) | 220 if (!kTestCases[i].success) |
218 continue; | 221 continue; |
219 | 222 |
220 FilePath expected_virtual_path(kTestCases[i].virtual_path); | 223 FilePath expected_virtual_path(kTestCases[i].virtual_path); |
221 EXPECT_EQ(expected_virtual_path.NormalizePathSeparators(), virtual_path) | 224 EXPECT_EQ(expected_virtual_path.NormalizePathSeparators(), virtual_path) |
222 << "Resolving " << kTestCases[i].local_path; | 225 << "Resolving " << kTestCases[i].local_path; |
223 } | 226 } |
224 } | 227 } |
225 | 228 |
| 229 TEST(ExternalMountPointsTest, HandlesFileSystemMountType) { |
| 230 scoped_refptr<fileapi::ExternalMountPoints> mount_points( |
| 231 fileapi::ExternalMountPoints::CreateRefCounted()); |
| 232 |
| 233 const GURL test_origin("http://chromium.org"); |
| 234 const FilePath test_path(FPL("/mount")); |
| 235 |
| 236 // Should handle External File System. |
| 237 EXPECT_TRUE(mount_points->HandlesFileSystemMountType( |
| 238 fileapi::kFileSystemTypeExternal)); |
| 239 |
| 240 // Shouldn't handle the rest. |
| 241 EXPECT_FALSE(mount_points->HandlesFileSystemMountType( |
| 242 fileapi::kFileSystemTypeIsolated)); |
| 243 EXPECT_FALSE(mount_points->HandlesFileSystemMountType( |
| 244 fileapi::kFileSystemTypeTemporary)); |
| 245 EXPECT_FALSE(mount_points->HandlesFileSystemMountType( |
| 246 fileapi::kFileSystemTypePersistent)); |
| 247 EXPECT_FALSE(mount_points->HandlesFileSystemMountType( |
| 248 fileapi::kFileSystemTypeTest)); |
| 249 // Not even if it's external subtype. |
| 250 EXPECT_FALSE(mount_points->HandlesFileSystemMountType( |
| 251 fileapi::kFileSystemTypeNativeLocal)); |
| 252 EXPECT_FALSE(mount_points->HandlesFileSystemMountType( |
| 253 fileapi::kFileSystemTypeRestrictedNativeLocal)); |
| 254 EXPECT_FALSE(mount_points->HandlesFileSystemMountType( |
| 255 fileapi::kFileSystemTypeDrive)); |
| 256 EXPECT_FALSE(mount_points->HandlesFileSystemMountType( |
| 257 fileapi::kFileSystemTypeSyncable)); |
| 258 } |
| 259 |
| 260 TEST(ExternalMountPointsTest, CreateCrackedFileSystemURL) { |
| 261 scoped_refptr<fileapi::ExternalMountPoints> mount_points( |
| 262 fileapi::ExternalMountPoints::CreateRefCounted()); |
| 263 |
| 264 const GURL kTestOrigin("http://chromium.org"); |
| 265 |
| 266 mount_points->RegisterFileSystem("c", |
| 267 fileapi::kFileSystemTypeNativeLocal, |
| 268 FilePath(DRIVE FPL("/a/b/c"))); |
| 269 mount_points->RegisterFileSystem("c(1)", |
| 270 fileapi::kFileSystemTypeDrive, |
| 271 FilePath(DRIVE FPL("/a/b/c(1)"))); |
| 272 mount_points->RegisterFileSystem("empty_path", |
| 273 fileapi::kFileSystemTypeSyncable, |
| 274 FilePath(FPL(""))); |
| 275 mount_points->RegisterFileSystem("mount", |
| 276 fileapi::kFileSystemTypeDrive, |
| 277 FilePath(DRIVE FPL("/root"))); |
| 278 |
| 279 // Try cracking invalid GURL. |
| 280 FileSystemURL invalid = mount_points->CrackURL(GURL("http://chromium.og")); |
| 281 EXPECT_FALSE(invalid.is_valid()); |
| 282 |
| 283 // Try cracking isolated path. |
| 284 FileSystemURL isolated = mount_points->CreateCrackedFileSystemURL( |
| 285 kTestOrigin, fileapi::kFileSystemTypeIsolated, FilePath(FPL("c"))); |
| 286 EXPECT_FALSE(isolated.is_valid()); |
| 287 |
| 288 // Try native local which is not cracked. |
| 289 FileSystemURL native_local = mount_points->CreateCrackedFileSystemURL( |
| 290 kTestOrigin, fileapi::kFileSystemTypeNativeLocal, FilePath(FPL("c"))); |
| 291 EXPECT_FALSE(native_local.is_valid()); |
| 292 |
| 293 struct TestCase { |
| 294 const FilePath::CharType* const path; |
| 295 bool expect_valid; |
| 296 fileapi::FileSystemType expect_type; |
| 297 const FilePath::CharType* const expect_path; |
| 298 const char* const expect_fs_id; |
| 299 }; |
| 300 |
| 301 const TestCase kTestCases[] = { |
| 302 { FPL("c/d/e"), |
| 303 true, fileapi::kFileSystemTypeNativeLocal, DRIVE FPL("/a/b/c/d/e"), "c" }, |
| 304 { FPL("c(1)/d/e"), |
| 305 true, fileapi::kFileSystemTypeDrive, DRIVE FPL("/a/b/c(1)/d/e"), "c(1)" }, |
| 306 { FPL("c(1)"), |
| 307 true, fileapi::kFileSystemTypeDrive, DRIVE FPL("/a/b/c(1)"), "c(1)" }, |
| 308 { FPL("empty_path/a"), |
| 309 true, fileapi::kFileSystemTypeSyncable, FPL("a"), "empty_path" }, |
| 310 { FPL("empty_path"), |
| 311 true, fileapi::kFileSystemTypeSyncable, FPL(""), "empty_path" }, |
| 312 { FPL("mount/a/b"), |
| 313 true, fileapi::kFileSystemTypeDrive, DRIVE FPL("/root/a/b"), "mount" }, |
| 314 { FPL("mount"), |
| 315 true, fileapi::kFileSystemTypeDrive, DRIVE FPL("/root"), "mount" }, |
| 316 { FPL("cc"), |
| 317 false, fileapi::kFileSystemTypeUnknown, FPL(""), "" }, |
| 318 { FPL(""), |
| 319 false, fileapi::kFileSystemTypeUnknown, FPL(""), "" }, |
| 320 { FPL(".."), |
| 321 false, fileapi::kFileSystemTypeUnknown, FPL(""), "" }, |
| 322 // Absolte paths. |
| 323 { FPL("/c/d/e"), |
| 324 false, fileapi::kFileSystemTypeUnknown, FPL(""), "" }, |
| 325 { FPL("/c(1)/d/e"), |
| 326 false, fileapi::kFileSystemTypeUnknown, FPL(""), "" }, |
| 327 { FPL("/empty_path"), |
| 328 false, fileapi::kFileSystemTypeUnknown, FPL(""), "" }, |
| 329 // PAth references parent. |
| 330 { FPL("c/d/../e"), |
| 331 false, fileapi::kFileSystemTypeUnknown, FPL(""), "" }, |
| 332 { FPL("/empty_path/a/../b"), |
| 333 false, fileapi::kFileSystemTypeUnknown, FPL(""), "" }, |
| 334 #if defined(FILE_PATH_USES_WIN_SEPARATORS) |
| 335 { FPL("c/d\\e"), |
| 336 true, fileapi::kFileSystemTypeNativeLocal, DRIVE FPL("/a/b/c/d/e"), "c" }, |
| 337 { FPL("mount\\a\\b"), |
| 338 true, fileapi::kFileSystemTypeDrive, DRIVE FPL("/root/a/b"), "mount" }, |
| 339 #endif |
| 340 }; |
| 341 |
| 342 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) { |
| 343 FileSystemURL cracked = mount_points->CreateCrackedFileSystemURL( |
| 344 kTestOrigin, |
| 345 fileapi::kFileSystemTypeExternal, |
| 346 FilePath(kTestCases[i].path)); |
| 347 |
| 348 EXPECT_EQ(kTestCases[i].expect_valid, cracked.is_valid()) |
| 349 << "Test case index: " << i; |
| 350 |
| 351 if (!kTestCases[i].expect_valid) |
| 352 continue; |
| 353 |
| 354 EXPECT_EQ(kTestOrigin, cracked.origin()) |
| 355 << "Test case index: " << i; |
| 356 EXPECT_EQ(kTestCases[i].expect_type, cracked.type()) |
| 357 << "Test case index: " << i; |
| 358 EXPECT_EQ(FilePath(kTestCases[i].expect_path).NormalizePathSeparators(), |
| 359 cracked.path()) |
| 360 << "Test case index: " << i; |
| 361 EXPECT_EQ(FilePath(kTestCases[i].path).NormalizePathSeparators(), |
| 362 cracked.virtual_path()) |
| 363 << "Test case index: " << i; |
| 364 EXPECT_EQ(kTestCases[i].expect_fs_id, cracked.filesystem_id()) |
| 365 << "Test case index: " << i; |
| 366 EXPECT_EQ(fileapi::kFileSystemTypeExternal, cracked.mount_type()) |
| 367 << "Test case index: " << i; |
| 368 } |
| 369 } |
| 370 |
| 371 TEST(ExternalMountPointsTest, CrackVirtualPath) { |
| 372 scoped_refptr<fileapi::ExternalMountPoints> mount_points( |
| 373 fileapi::ExternalMountPoints::CreateRefCounted()); |
| 374 |
| 375 const GURL kTestOrigin("http://chromium.org"); |
| 376 |
| 377 mount_points->RegisterFileSystem("c", |
| 378 fileapi::kFileSystemTypeNativeLocal, |
| 379 FilePath(DRIVE FPL("/a/b/c"))); |
| 380 mount_points->RegisterFileSystem("c(1)", |
| 381 fileapi::kFileSystemTypeDrive, |
| 382 FilePath(DRIVE FPL("/a/b/c(1)"))); |
| 383 mount_points->RegisterFileSystem("empty_path", |
| 384 fileapi::kFileSystemTypeSyncable, |
| 385 FilePath(FPL(""))); |
| 386 mount_points->RegisterFileSystem("mount", |
| 387 fileapi::kFileSystemTypeDrive, |
| 388 FilePath(DRIVE FPL("/root"))); |
| 389 |
| 390 struct TestCase { |
| 391 const FilePath::CharType* const path; |
| 392 bool expect_valid; |
| 393 fileapi::FileSystemType expect_type; |
| 394 const FilePath::CharType* const expect_path; |
| 395 const char* const expect_name; |
| 396 }; |
| 397 |
| 398 const TestCase kTestCases[] = { |
| 399 { FPL("c/d/e"), |
| 400 true, fileapi::kFileSystemTypeNativeLocal, DRIVE FPL("/a/b/c/d/e"), "c" }, |
| 401 { FPL("c(1)/d/e"), |
| 402 true, fileapi::kFileSystemTypeDrive, DRIVE FPL("/a/b/c(1)/d/e"), "c(1)" }, |
| 403 { FPL("c(1)"), |
| 404 true, fileapi::kFileSystemTypeDrive, DRIVE FPL("/a/b/c(1)"), "c(1)" }, |
| 405 { FPL("empty_path/a"), |
| 406 true, fileapi::kFileSystemTypeSyncable, FPL("a"), "empty_path" }, |
| 407 { FPL("empty_path"), |
| 408 true, fileapi::kFileSystemTypeSyncable, FPL(""), "empty_path" }, |
| 409 { FPL("mount/a/b"), |
| 410 true, fileapi::kFileSystemTypeDrive, DRIVE FPL("/root/a/b"), "mount" }, |
| 411 { FPL("mount"), |
| 412 true, fileapi::kFileSystemTypeDrive, DRIVE FPL("/root"), "mount" }, |
| 413 { FPL("cc"), |
| 414 false, fileapi::kFileSystemTypeUnknown, FPL(""), "" }, |
| 415 { FPL(""), |
| 416 false, fileapi::kFileSystemTypeUnknown, FPL(""), "" }, |
| 417 { FPL(".."), |
| 418 false, fileapi::kFileSystemTypeUnknown, FPL(""), "" }, |
| 419 // Absolte paths. |
| 420 { FPL("/c/d/e"), |
| 421 false, fileapi::kFileSystemTypeUnknown, FPL(""), "" }, |
| 422 { FPL("/c(1)/d/e"), |
| 423 false, fileapi::kFileSystemTypeUnknown, FPL(""), "" }, |
| 424 { FPL("/empty_path"), |
| 425 false, fileapi::kFileSystemTypeUnknown, FPL(""), "" }, |
| 426 // PAth references parent. |
| 427 { FPL("c/d/../e"), |
| 428 false, fileapi::kFileSystemTypeUnknown, FPL(""), "" }, |
| 429 { FPL("/empty_path/a/../b"), |
| 430 false, fileapi::kFileSystemTypeUnknown, FPL(""), "" }, |
| 431 #if defined(FILE_PATH_USES_WIN_SEPARATORS) |
| 432 { FPL("c/d\\e"), |
| 433 true, fileapi::kFileSystemTypeNativeLocal, DRIVE FPL("/a/b/c/d/e"), "c" }, |
| 434 { FPL("mount\\a\\b"), |
| 435 true, fileapi::kFileSystemTypeDrive, DRIVE FPL("/root/a/b"), "mount" }, |
| 436 #endif |
| 437 }; |
| 438 |
| 439 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) { |
| 440 std::string cracked_name; |
| 441 fileapi::FileSystemType cracked_type; |
| 442 FilePath cracked_path; |
| 443 EXPECT_EQ(kTestCases[i].expect_valid, |
| 444 mount_points->CrackVirtualPath(FilePath(kTestCases[i].path), |
| 445 &cracked_name, &cracked_type, &cracked_path)) |
| 446 << "Test case index: " << i; |
| 447 |
| 448 if (!kTestCases[i].expect_valid) |
| 449 continue; |
| 450 |
| 451 EXPECT_EQ(kTestCases[i].expect_type, cracked_type) |
| 452 << "Test case index: " << i; |
| 453 EXPECT_EQ(FilePath(kTestCases[i].expect_path).NormalizePathSeparators(), |
| 454 cracked_path) |
| 455 << "Test case index: " << i; |
| 456 EXPECT_EQ(kTestCases[i].expect_name, cracked_name) |
| 457 << "Test case index: " << i; |
| 458 } |
| 459 } |
| 460 |
226 } // namespace | 461 } // namespace |
227 | 462 |
OLD | NEW |