| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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 <string> | |
| 6 | |
| 7 #include "base/basictypes.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "testing/gtest/include/gtest/gtest.h" | |
| 10 #include "webkit/fileapi/file_system_url.h" | |
| 11 #include "webkit/fileapi/isolated_context.h" | |
| 12 | |
| 13 #define FPL(x) FILE_PATH_LITERAL(x) | |
| 14 | |
| 15 #if defined(FILE_PATH_USES_DRIVE_LETTERS) | |
| 16 #define DRIVE FPL("C:") | |
| 17 #else | |
| 18 #define DRIVE | |
| 19 #endif | |
| 20 | |
| 21 namespace fileapi { | |
| 22 | |
| 23 typedef IsolatedContext::MountPointInfo FileInfo; | |
| 24 | |
| 25 namespace { | |
| 26 | |
| 27 const base::FilePath kTestPaths[] = { | |
| 28 base::FilePath(DRIVE FPL("/a/b.txt")), | |
| 29 base::FilePath(DRIVE FPL("/c/d/e")), | |
| 30 base::FilePath(DRIVE FPL("/h/")), | |
| 31 base::FilePath(DRIVE FPL("/")), | |
| 32 #if defined(FILE_PATH_USES_WIN_SEPARATORS) | |
| 33 base::FilePath(DRIVE FPL("\\foo\\bar")), | |
| 34 base::FilePath(DRIVE FPL("\\")), | |
| 35 #endif | |
| 36 // For duplicated base name test. | |
| 37 base::FilePath(DRIVE FPL("/")), | |
| 38 base::FilePath(DRIVE FPL("/f/e")), | |
| 39 base::FilePath(DRIVE FPL("/f/b.txt")), | |
| 40 }; | |
| 41 | |
| 42 } // namespace | |
| 43 | |
| 44 class IsolatedContextTest : public testing::Test { | |
| 45 public: | |
| 46 IsolatedContextTest() { | |
| 47 for (size_t i = 0; i < arraysize(kTestPaths); ++i) | |
| 48 fileset_.insert(kTestPaths[i].NormalizePathSeparators()); | |
| 49 } | |
| 50 | |
| 51 virtual void SetUp() { | |
| 52 IsolatedContext::FileInfoSet files; | |
| 53 for (size_t i = 0; i < arraysize(kTestPaths); ++i) { | |
| 54 std::string name; | |
| 55 ASSERT_TRUE( | |
| 56 files.AddPath(kTestPaths[i].NormalizePathSeparators(), &name)); | |
| 57 names_.push_back(name); | |
| 58 } | |
| 59 id_ = IsolatedContext::GetInstance()->RegisterDraggedFileSystem(files); | |
| 60 IsolatedContext::GetInstance()->AddReference(id_); | |
| 61 ASSERT_FALSE(id_.empty()); | |
| 62 } | |
| 63 | |
| 64 virtual void TearDown() { | |
| 65 IsolatedContext::GetInstance()->RemoveReference(id_); | |
| 66 } | |
| 67 | |
| 68 IsolatedContext* isolated_context() const { | |
| 69 return IsolatedContext::GetInstance(); | |
| 70 } | |
| 71 | |
| 72 protected: | |
| 73 std::string id_; | |
| 74 std::multiset<base::FilePath> fileset_; | |
| 75 std::vector<std::string> names_; | |
| 76 | |
| 77 private: | |
| 78 DISALLOW_COPY_AND_ASSIGN(IsolatedContextTest); | |
| 79 }; | |
| 80 | |
| 81 TEST_F(IsolatedContextTest, RegisterAndRevokeTest) { | |
| 82 // See if the returned top-level entries match with what we registered. | |
| 83 std::vector<FileInfo> toplevels; | |
| 84 ASSERT_TRUE(isolated_context()->GetDraggedFileInfo(id_, &toplevels)); | |
| 85 ASSERT_EQ(fileset_.size(), toplevels.size()); | |
| 86 for (size_t i = 0; i < toplevels.size(); ++i) { | |
| 87 ASSERT_TRUE(fileset_.find(toplevels[i].path) != fileset_.end()); | |
| 88 } | |
| 89 | |
| 90 // See if the name of each registered kTestPaths (that is what we | |
| 91 // register in SetUp() by RegisterDraggedFileSystem) is properly cracked as | |
| 92 // a valid virtual path in the isolated filesystem. | |
| 93 for (size_t i = 0; i < arraysize(kTestPaths); ++i) { | |
| 94 base::FilePath virtual_path = isolated_context()->CreateVirtualRootPath(id_) | |
| 95 .AppendASCII(names_[i]); | |
| 96 std::string cracked_id; | |
| 97 base::FilePath cracked_path; | |
| 98 FileSystemType cracked_type; | |
| 99 ASSERT_TRUE(isolated_context()->CrackVirtualPath( | |
| 100 virtual_path, &cracked_id, &cracked_type, &cracked_path)); | |
| 101 ASSERT_EQ(kTestPaths[i].NormalizePathSeparators().value(), | |
| 102 cracked_path.value()); | |
| 103 ASSERT_EQ(id_, cracked_id); | |
| 104 ASSERT_EQ(kFileSystemTypeDragged, cracked_type); | |
| 105 } | |
| 106 | |
| 107 // Make sure GetRegisteredPath returns false for id_ since it is | |
| 108 // registered for dragged files. | |
| 109 base::FilePath path; | |
| 110 ASSERT_FALSE(isolated_context()->GetRegisteredPath(id_, &path)); | |
| 111 | |
| 112 // Deref the current one and registering a new one. | |
| 113 isolated_context()->RemoveReference(id_); | |
| 114 | |
| 115 std::string id2 = isolated_context()->RegisterFileSystemForPath( | |
| 116 kFileSystemTypeNativeLocal, base::FilePath(DRIVE FPL("/foo")), NULL); | |
| 117 | |
| 118 // Make sure the GetDraggedFileInfo returns false for both ones. | |
| 119 ASSERT_FALSE(isolated_context()->GetDraggedFileInfo(id2, &toplevels)); | |
| 120 ASSERT_FALSE(isolated_context()->GetDraggedFileInfo(id_, &toplevels)); | |
| 121 | |
| 122 // Make sure the GetRegisteredPath returns true only for the new one. | |
| 123 ASSERT_FALSE(isolated_context()->GetRegisteredPath(id_, &path)); | |
| 124 ASSERT_TRUE(isolated_context()->GetRegisteredPath(id2, &path)); | |
| 125 | |
| 126 // Try registering three more file systems for the same path as id2. | |
| 127 std::string id3 = isolated_context()->RegisterFileSystemForPath( | |
| 128 kFileSystemTypeNativeLocal, path, NULL); | |
| 129 std::string id4 = isolated_context()->RegisterFileSystemForPath( | |
| 130 kFileSystemTypeNativeLocal, path, NULL); | |
| 131 std::string id5 = isolated_context()->RegisterFileSystemForPath( | |
| 132 kFileSystemTypeNativeLocal, path, NULL); | |
| 133 | |
| 134 // Remove file system for id4. | |
| 135 isolated_context()->AddReference(id4); | |
| 136 isolated_context()->RemoveReference(id4); | |
| 137 | |
| 138 // Only id4 should become invalid now. | |
| 139 ASSERT_TRUE(isolated_context()->GetRegisteredPath(id2, &path)); | |
| 140 ASSERT_TRUE(isolated_context()->GetRegisteredPath(id3, &path)); | |
| 141 ASSERT_FALSE(isolated_context()->GetRegisteredPath(id4, &path)); | |
| 142 ASSERT_TRUE(isolated_context()->GetRegisteredPath(id5, &path)); | |
| 143 | |
| 144 // Revoke file system id5, after adding multiple references. | |
| 145 isolated_context()->AddReference(id5); | |
| 146 isolated_context()->AddReference(id5); | |
| 147 isolated_context()->AddReference(id5); | |
| 148 isolated_context()->RevokeFileSystem(id5); | |
| 149 | |
| 150 // No matter how many references we add id5 must be invalid now. | |
| 151 ASSERT_TRUE(isolated_context()->GetRegisteredPath(id2, &path)); | |
| 152 ASSERT_TRUE(isolated_context()->GetRegisteredPath(id3, &path)); | |
| 153 ASSERT_FALSE(isolated_context()->GetRegisteredPath(id4, &path)); | |
| 154 ASSERT_FALSE(isolated_context()->GetRegisteredPath(id5, &path)); | |
| 155 | |
| 156 // Revoke the file systems by path. | |
| 157 isolated_context()->RevokeFileSystemByPath(path); | |
| 158 | |
| 159 // Now all the file systems associated to the path must be invalid. | |
| 160 ASSERT_FALSE(isolated_context()->GetRegisteredPath(id2, &path)); | |
| 161 ASSERT_FALSE(isolated_context()->GetRegisteredPath(id3, &path)); | |
| 162 ASSERT_FALSE(isolated_context()->GetRegisteredPath(id4, &path)); | |
| 163 ASSERT_FALSE(isolated_context()->GetRegisteredPath(id5, &path)); | |
| 164 } | |
| 165 | |
| 166 TEST_F(IsolatedContextTest, CrackWithRelativePaths) { | |
| 167 const struct { | |
| 168 base::FilePath::StringType path; | |
| 169 bool valid; | |
| 170 } relatives[] = { | |
| 171 { FPL("foo"), true }, | |
| 172 { FPL("foo/bar"), true }, | |
| 173 { FPL(".."), false }, | |
| 174 { FPL("foo/.."), false }, | |
| 175 { FPL("foo/../bar"), false }, | |
| 176 #if defined(FILE_PATH_USES_WIN_SEPARATORS) | |
| 177 # define SHOULD_FAIL_WITH_WIN_SEPARATORS false | |
| 178 #else | |
| 179 # define SHOULD_FAIL_WITH_WIN_SEPARATORS true | |
| 180 #endif | |
| 181 { FPL("foo\\..\\baz"), SHOULD_FAIL_WITH_WIN_SEPARATORS }, | |
| 182 { FPL("foo/..\\baz"), SHOULD_FAIL_WITH_WIN_SEPARATORS }, | |
| 183 }; | |
| 184 | |
| 185 for (size_t i = 0; i < arraysize(kTestPaths); ++i) { | |
| 186 for (size_t j = 0; j < ARRAYSIZE_UNSAFE(relatives); ++j) { | |
| 187 SCOPED_TRACE(testing::Message() << "Testing " | |
| 188 << kTestPaths[i].value() << " " << relatives[j].path); | |
| 189 base::FilePath virtual_path = isolated_context()->CreateVirtualRootPath(id
_) | |
| 190 .AppendASCII(names_[i]).Append(relatives[j].path); | |
| 191 std::string cracked_id; | |
| 192 base::FilePath cracked_path; | |
| 193 FileSystemType cracked_type; | |
| 194 if (!relatives[j].valid) { | |
| 195 ASSERT_FALSE(isolated_context()->CrackVirtualPath( | |
| 196 virtual_path, &cracked_id, &cracked_type, &cracked_path)); | |
| 197 continue; | |
| 198 } | |
| 199 ASSERT_TRUE(isolated_context()->CrackVirtualPath( | |
| 200 virtual_path, &cracked_id, &cracked_type, &cracked_path)); | |
| 201 ASSERT_EQ(kTestPaths[i].Append(relatives[j].path) | |
| 202 .NormalizePathSeparators().value(), | |
| 203 cracked_path.value()); | |
| 204 ASSERT_EQ(id_, cracked_id); | |
| 205 ASSERT_EQ(kFileSystemTypeDragged, cracked_type); | |
| 206 } | |
| 207 } | |
| 208 } | |
| 209 | |
| 210 TEST_F(IsolatedContextTest, CrackURLWithRelativePaths) { | |
| 211 const struct { | |
| 212 base::FilePath::StringType path; | |
| 213 bool valid; | |
| 214 } relatives[] = { | |
| 215 { FPL("foo"), true }, | |
| 216 { FPL("foo/bar"), true }, | |
| 217 { FPL(".."), false }, | |
| 218 { FPL("foo/.."), false }, | |
| 219 { FPL("foo/../bar"), false }, | |
| 220 #if defined(FILE_PATH_USES_WIN_SEPARATORS) | |
| 221 # define SHOULD_FAIL_WITH_WIN_SEPARATORS false | |
| 222 #else | |
| 223 # define SHOULD_FAIL_WITH_WIN_SEPARATORS true | |
| 224 #endif | |
| 225 { FPL("foo\\..\\baz"), SHOULD_FAIL_WITH_WIN_SEPARATORS }, | |
| 226 { FPL("foo/..\\baz"), SHOULD_FAIL_WITH_WIN_SEPARATORS }, | |
| 227 }; | |
| 228 | |
| 229 for (size_t i = 0; i < arraysize(kTestPaths); ++i) { | |
| 230 for (size_t j = 0; j < ARRAYSIZE_UNSAFE(relatives); ++j) { | |
| 231 SCOPED_TRACE(testing::Message() << "Testing " | |
| 232 << kTestPaths[i].value() << " " << relatives[j].path); | |
| 233 base::FilePath virtual_path = isolated_context()->CreateVirtualRootPath(id
_) | |
| 234 .AppendASCII(names_[i]).Append(relatives[j].path); | |
| 235 | |
| 236 FileSystemURL cracked = isolated_context()->CreateCrackedFileSystemURL( | |
| 237 GURL("http://chromium.org"), kFileSystemTypeIsolated, virtual_path); | |
| 238 | |
| 239 ASSERT_EQ(relatives[j].valid, cracked.is_valid()); | |
| 240 | |
| 241 if (!relatives[j].valid) | |
| 242 continue; | |
| 243 ASSERT_EQ(GURL("http://chromium.org"), cracked.origin()); | |
| 244 ASSERT_EQ(kTestPaths[i].Append(relatives[j].path) | |
| 245 .NormalizePathSeparators().value(), | |
| 246 cracked.path().value()); | |
| 247 ASSERT_EQ(virtual_path.NormalizePathSeparators(), cracked.virtual_path()); | |
| 248 ASSERT_EQ(id_, cracked.filesystem_id()); | |
| 249 ASSERT_EQ(kFileSystemTypeDragged, cracked.type()); | |
| 250 ASSERT_EQ(kFileSystemTypeIsolated, cracked.mount_type()); | |
| 251 } | |
| 252 } | |
| 253 } | |
| 254 | |
| 255 TEST_F(IsolatedContextTest, TestWithVirtualRoot) { | |
| 256 std::string cracked_id; | |
| 257 base::FilePath cracked_path; | |
| 258 | |
| 259 // Trying to crack virtual root "/" returns true but with empty cracked path | |
| 260 // as "/" of the isolated filesystem is a pure virtual directory | |
| 261 // that has no corresponding platform directory. | |
| 262 base::FilePath virtual_path = isolated_context()->CreateVirtualRootPath(id_); | |
| 263 ASSERT_TRUE(isolated_context()->CrackVirtualPath( | |
| 264 virtual_path, &cracked_id, NULL, &cracked_path)); | |
| 265 ASSERT_EQ(FPL(""), cracked_path.value()); | |
| 266 ASSERT_EQ(id_, cracked_id); | |
| 267 | |
| 268 // Trying to crack "/foo" should fail (because "foo" is not the one | |
| 269 // included in the kTestPaths). | |
| 270 virtual_path = isolated_context()->CreateVirtualRootPath( | |
| 271 id_).AppendASCII("foo"); | |
| 272 ASSERT_FALSE(isolated_context()->CrackVirtualPath( | |
| 273 virtual_path, &cracked_id, NULL, &cracked_path)); | |
| 274 } | |
| 275 | |
| 276 TEST_F(IsolatedContextTest, CanHandleURL) { | |
| 277 const GURL test_origin("http://chromium.org"); | |
| 278 const base::FilePath test_path(FPL("/mount")); | |
| 279 | |
| 280 // Should handle isolated file system. | |
| 281 EXPECT_TRUE(isolated_context()->HandlesFileSystemMountType( | |
| 282 fileapi::kFileSystemTypeIsolated)); | |
| 283 | |
| 284 // Shouldn't handle the rest. | |
| 285 EXPECT_FALSE(isolated_context()->HandlesFileSystemMountType( | |
| 286 fileapi::kFileSystemTypeExternal)); | |
| 287 EXPECT_FALSE(isolated_context()->HandlesFileSystemMountType( | |
| 288 fileapi::kFileSystemTypeTemporary)); | |
| 289 EXPECT_FALSE(isolated_context()->HandlesFileSystemMountType( | |
| 290 fileapi::kFileSystemTypePersistent)); | |
| 291 EXPECT_FALSE(isolated_context()->HandlesFileSystemMountType( | |
| 292 fileapi::kFileSystemTypeTest)); | |
| 293 // Not even if it's isolated subtype. | |
| 294 EXPECT_FALSE(isolated_context()->HandlesFileSystemMountType( | |
| 295 fileapi::kFileSystemTypeNativeLocal)); | |
| 296 EXPECT_FALSE(isolated_context()->HandlesFileSystemMountType( | |
| 297 fileapi::kFileSystemTypeDragged)); | |
| 298 EXPECT_FALSE(isolated_context()->HandlesFileSystemMountType( | |
| 299 fileapi::kFileSystemTypeNativeMedia)); | |
| 300 EXPECT_FALSE(isolated_context()->HandlesFileSystemMountType( | |
| 301 fileapi::kFileSystemTypeDeviceMedia)); | |
| 302 } | |
| 303 | |
| 304 TEST_F(IsolatedContextTest, VirtualFileSystemTests) { | |
| 305 // Should be able to register empty and non-absolute paths | |
| 306 std::string empty_fsid = isolated_context()->RegisterFileSystemForVirtualPath( | |
| 307 fileapi::kFileSystemTypeIsolated, "_", base::FilePath()); | |
| 308 std::string relative_fsid = | |
| 309 isolated_context()->RegisterFileSystemForVirtualPath( | |
| 310 fileapi::kFileSystemTypeIsolated, "_", | |
| 311 base::FilePath(FPL("relpath"))); | |
| 312 ASSERT_FALSE(empty_fsid.empty()); | |
| 313 ASSERT_FALSE(relative_fsid.empty()); | |
| 314 | |
| 315 // Make sure that filesystem root is not prepended to cracked virtual paths. | |
| 316 base::FilePath database_root = base::FilePath(DRIVE FPL("/database_path")); | |
| 317 std::string database_fsid = | |
| 318 isolated_context()->RegisterFileSystemForVirtualPath( | |
| 319 fileapi::kFileSystemTypeIsolated, "_", database_root); | |
| 320 | |
| 321 base::FilePath test_virtual_path = | |
| 322 base::FilePath().AppendASCII("virtualdir").AppendASCII("virtualfile.txt"); | |
| 323 | |
| 324 base::FilePath whole_virtual_path = | |
| 325 isolated_context()->CreateVirtualRootPath(database_fsid) | |
| 326 .AppendASCII("_").Append(test_virtual_path); | |
| 327 | |
| 328 std::string cracked_id; | |
| 329 base::FilePath cracked_path; | |
| 330 ASSERT_TRUE(isolated_context()->CrackVirtualPath( | |
| 331 whole_virtual_path, &cracked_id, NULL, &cracked_path)); | |
| 332 ASSERT_EQ(database_fsid, cracked_id); | |
| 333 ASSERT_EQ(test_virtual_path, cracked_path); | |
| 334 } | |
| 335 | |
| 336 } // namespace fileapi | |
| OLD | NEW |