| 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/external_mount_points.h" |
| 6 |
| 7 #include <string> |
| 8 #include <vector> |
| 9 |
| 10 #include "base/file_path.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 |
| 13 #define FPL FILE_PATH_LITERAL |
| 14 |
| 15 #if defined(FILE_PATH_USES_DRIVE_LETTERS) |
| 16 #define DRIVE FPL("C:") |
| 17 #else |
| 18 #define DRIVE |
| 19 #endif |
| 20 |
| 21 namespace { |
| 22 |
| 23 TEST(ExternalMountPointsTest, AddMountPoint) { |
| 24 scoped_refptr<fileapi::ExternalMountPoints> mount_points( |
| 25 fileapi::ExternalMountPoints::CreateRefCounted()); |
| 26 |
| 27 struct TestCase { |
| 28 // The mount point's name. |
| 29 const char* const name; |
| 30 // The mount point's path. |
| 31 const FilePath::CharType* const path; |
| 32 // Whether the mount point registration should succeed. |
| 33 bool success; |
| 34 // Path returned by GetRegisteredPath. NULL if the method is expected to |
| 35 // fail. |
| 36 const FilePath::CharType* const registered_path; |
| 37 }; |
| 38 |
| 39 const TestCase kTestCases[] = { |
| 40 // Valid mount point. |
| 41 { "test", DRIVE FPL("/foo/test"), true, DRIVE FPL("/foo/test") }, |
| 42 // Valid mount point with only one path component. |
| 43 { "bbb", DRIVE FPL("/bbb"), true, DRIVE FPL("/bbb") }, |
| 44 // Existing mount point path is substring of the mount points path. |
| 45 { "test11", DRIVE FPL("/foo/test11"), true, DRIVE FPL("/foo/test11") }, |
| 46 // Path substring of an existing path. |
| 47 { "test1", DRIVE FPL("/foo/test1"), true, DRIVE FPL("/foo/test1") }, |
| 48 // Empty mount point name and path. |
| 49 { "", DRIVE FPL(""), false, NULL }, |
| 50 // Empty mount point name. |
| 51 { "", DRIVE FPL("/ddd"), false, NULL }, |
| 52 // Empty mount point path. |
| 53 { "empty_path", FPL(""), true, FPL("") }, |
| 54 // Name different from path's base name. |
| 55 { "not_base_name", DRIVE FPL("/x/y/z"), true, DRIVE FPL("/x/y/z") }, |
| 56 // References parent. |
| 57 { "invalid", DRIVE FPL("../foo/invalid"), false, NULL }, |
| 58 // Relative path. |
| 59 { "relative", DRIVE FPL("foo/relative"), false, NULL }, |
| 60 // Existing mount point path. |
| 61 { "path_exists", DRIVE FPL("/foo/test"), false, NULL }, |
| 62 // Mount point with the same name exists. |
| 63 { "test", DRIVE FPL("/foo/a/test_name_exists"), false, |
| 64 DRIVE FPL("/foo/test") }, |
| 65 // Child of an existing mount point. |
| 66 { "a1", DRIVE FPL("/foo/test/a"), false, NULL }, |
| 67 // Parent of an existing mount point. |
| 68 { "foo1", DRIVE FPL("/foo"), false, NULL }, |
| 69 // Bit bigger depth. |
| 70 { "g", DRIVE FPL("/foo/a/b/c/d/e/f/g"), true, |
| 71 DRIVE FPL("/foo/a/b/c/d/e/f/g") }, |
| 72 // Sibling mount point (with similar name) exists. |
| 73 { "ff", DRIVE FPL("/foo/a/b/c/d/e/ff"), true, |
| 74 DRIVE FPL("/foo/a/b/c/d/e/ff") }, |
| 75 // Lexicographically last among existing mount points. |
| 76 { "yyy", DRIVE FPL("/zzz/yyy"), true, DRIVE FPL("/zzz/yyy") }, |
| 77 // Parent of the lexicographically last mount point. |
| 78 { "zzz1", DRIVE FPL("/zzz"), false, NULL }, |
| 79 // Child of the lexicographically last mount point. |
| 80 { "xxx1", DRIVE FPL("/zzz/yyy/xxx"), false, NULL }, |
| 81 // Lexicographically first among existing mount points. |
| 82 { "b", DRIVE FPL("/a/b"), true, DRIVE FPL("/a/b") }, |
| 83 // Parent of lexicographically first mount point. |
| 84 { "a1", DRIVE FPL("/a"), false, NULL }, |
| 85 // Child of lexicographically last mount point. |
| 86 { "c1", DRIVE FPL("/a/b/c"), false, NULL }, |
| 87 // Path contains .. component. |
| 88 { "funky", DRIVE FPL("/tt/fun/../funky"), false, NULL }, |
| 89 // Windows separators. |
| 90 #if defined(FILE_PATH_USES_WIN_SEPARATORS) |
| 91 { "win", DRIVE FPL("\\try\\separators\\win"), true, |
| 92 DRIVE FPL("\\try\\separators\\win") }, |
| 93 { "win1", DRIVE FPL("\\try/separators\\win1"), true, |
| 94 DRIVE FPL("\\try/separators\\win1") }, |
| 95 #else |
| 96 { "win", DRIVE FPL("\\separators\\win"), false, NULL }, |
| 97 { "win1", DRIVE FPL("\\try/separators\\win1"), false, NULL }, |
| 98 #endif |
| 99 // Win separators, but relative path. |
| 100 { "win2", DRIVE FPL("try\\separators\\win2"), false, NULL }, |
| 101 }; |
| 102 |
| 103 // Test adding mount points. |
| 104 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) { |
| 105 EXPECT_EQ(kTestCases[i].success, |
| 106 mount_points->RegisterFileSystem( |
| 107 kTestCases[i].name, |
| 108 fileapi::kFileSystemTypeNativeLocal, |
| 109 FilePath(kTestCases[i].path))) |
| 110 << "Adding mount point: " << kTestCases[i].name << " with path " |
| 111 << kTestCases[i].path; |
| 112 } |
| 113 |
| 114 // Test that final mount point presence state is as expected. |
| 115 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) { |
| 116 FilePath found_path; |
| 117 EXPECT_EQ(kTestCases[i].registered_path != NULL, |
| 118 mount_points->GetRegisteredPath(kTestCases[i].name, &found_path)) |
| 119 << "Test case: " << i; |
| 120 if (kTestCases[i].registered_path) |
| 121 EXPECT_EQ(FilePath(kTestCases[i].registered_path), found_path); |
| 122 } |
| 123 } |
| 124 |
| 125 TEST(ExternalMountPointsTest, GetVirtualPath) { |
| 126 scoped_refptr<fileapi::ExternalMountPoints> mount_points( |
| 127 fileapi::ExternalMountPoints::CreateRefCounted()); |
| 128 |
| 129 mount_points->RegisterFileSystem("c", |
| 130 fileapi::kFileSystemTypeNativeLocal, |
| 131 FilePath(DRIVE FPL("/a/b/c"))); |
| 132 mount_points->RegisterFileSystem("x", |
| 133 fileapi::kFileSystemTypeNativeLocal, |
| 134 FilePath(DRIVE FPL("/z/y/x"))); |
| 135 mount_points->RegisterFileSystem("o", |
| 136 fileapi::kFileSystemTypeNativeLocal, |
| 137 FilePath(DRIVE FPL("/m/n/o"))); |
| 138 // A mount point whose name does not match its path base name. |
| 139 mount_points->RegisterFileSystem("mount", |
| 140 fileapi::kFileSystemTypeNativeLocal, |
| 141 FilePath(DRIVE FPL("/root/foo"))); |
| 142 // A mount point with an empty path. |
| 143 mount_points->RegisterFileSystem("empty_path", |
| 144 fileapi::kFileSystemTypeNativeLocal, |
| 145 FilePath(FPL(""))); |
| 146 |
| 147 struct TestCase { |
| 148 const FilePath::CharType* const local_path; |
| 149 bool success; |
| 150 const FilePath::CharType* const virtual_path; |
| 151 }; |
| 152 |
| 153 const TestCase kTestCases[] = { |
| 154 // Empty path. |
| 155 { FPL(""), false, FPL("") }, |
| 156 // No registered mount point (but is parent to a mount point). |
| 157 { DRIVE FPL("/a/b"), false, FPL("") }, |
| 158 // No registered mount point (but is parent to a mount point). |
| 159 { DRIVE FPL("/z/y"), false, FPL("") }, |
| 160 // No registered mount point (but is parent to a mount point). |
| 161 { DRIVE FPL("/m/n"), false, FPL("") }, |
| 162 // No registered mount point. |
| 163 { DRIVE FPL("/foo/mount"), false, FPL("") }, |
| 164 // An existing mount point path is substring. |
| 165 { DRIVE FPL("/a/b/c1"), false, FPL("") }, |
| 166 // No leading /. |
| 167 { DRIVE FPL("a/b/c"), false, FPL("") }, |
| 168 // Sibling to a root path. |
| 169 { DRIVE FPL("/a/b/d/e"), false, FPL("") }, |
| 170 // Sibling to a root path. |
| 171 { DRIVE FPL("/z/y/v/u"), false, FPL("") }, |
| 172 // Sibling to a root path. |
| 173 { DRIVE FPL("/m/n/p/q"), false, FPL("") }, |
| 174 // Mount point root path. |
| 175 { DRIVE FPL("/a/b/c"), true, FPL("c") }, |
| 176 // Mount point root path. |
| 177 { DRIVE FPL("/z/y/x"), true, FPL("x") }, |
| 178 // Mount point root path. |
| 179 { DRIVE FPL("/m/n/o"), true, FPL("o") }, |
| 180 // Mount point child path. |
| 181 { DRIVE FPL("/a/b/c/d/e"), true, FPL("c/d/e") }, |
| 182 // Mount point child path. |
| 183 { DRIVE FPL("/z/y/x/v/u"), true, FPL("x/v/u") }, |
| 184 // Mount point child path. |
| 185 { DRIVE FPL("/m/n/o/p/q"), true, FPL("o/p/q") }, |
| 186 // Name doesn't match mount point path base name. |
| 187 { DRIVE FPL("/root/foo/a/b/c"), true, FPL("mount/a/b/c") }, |
| 188 { DRIVE FPL("/root/foo"), true, FPL("mount") }, |
| 189 }; |
| 190 |
| 191 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) { |
| 192 // Initialize virtual path with a value. |
| 193 FilePath virtual_path(DRIVE FPL("/mount")); |
| 194 FilePath local_path(kTestCases[i].local_path); |
| 195 EXPECT_EQ(kTestCases[i].success, |
| 196 mount_points->GetVirtualPath(local_path, &virtual_path)) |
| 197 << "Resolving " << kTestCases[i].local_path; |
| 198 |
| 199 // There are no guarantees for |virtual_path| value if |GetVirtualPath| |
| 200 // fails. |
| 201 if (!kTestCases[i].success) |
| 202 continue; |
| 203 |
| 204 FilePath expected_virtual_path(kTestCases[i].virtual_path); |
| 205 EXPECT_EQ(expected_virtual_path, virtual_path) |
| 206 << "Resolving " << kTestCases[i].local_path; |
| 207 } |
| 208 } |
| 209 |
| 210 } // namespace |
| 211 |
| OLD | NEW |