| 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 "webkit/fileapi/file_system_url.h" | |
| 6 | |
| 7 #include "base/files/file_path.h" | |
| 8 #include "googleurl/src/gurl.h" | |
| 9 #include "testing/gtest/include/gtest/gtest.h" | |
| 10 #include "webkit/fileapi/external_mount_points.h" | |
| 11 #include "webkit/fileapi/file_system_types.h" | |
| 12 #include "webkit/fileapi/file_system_util.h" | |
| 13 #include "webkit/fileapi/isolated_context.h" | |
| 14 #include "webkit/fileapi/syncable/syncable_file_system_util.h" | |
| 15 | |
| 16 #define FPL FILE_PATH_LITERAL | |
| 17 | |
| 18 #if defined(FILE_PATH_USES_DRIVE_LETTERS) | |
| 19 #define DRIVE FPL("C:") | |
| 20 #else | |
| 21 #define DRIVE FPL("/a/") | |
| 22 #endif | |
| 23 | |
| 24 namespace fileapi { | |
| 25 | |
| 26 namespace { | |
| 27 | |
| 28 FileSystemURL CreateFileSystemURL(const std::string& url_string) { | |
| 29 FileSystemURL url = FileSystemURL::CreateForTest(GURL(url_string)); | |
| 30 switch (url.type()) { | |
| 31 case kFileSystemTypeExternal: | |
| 32 return ExternalMountPoints::GetSystemInstance()-> | |
| 33 CreateCrackedFileSystemURL(url.origin(), url.type(), url.path()); | |
| 34 case kFileSystemTypeIsolated: | |
| 35 return IsolatedContext::GetInstance()->CreateCrackedFileSystemURL( | |
| 36 url.origin(), url.type(), url.path()); | |
| 37 default: | |
| 38 return url; | |
| 39 } | |
| 40 } | |
| 41 | |
| 42 FileSystemURL CreateExternalFileSystemURL(const GURL& origin, | |
| 43 FileSystemType type, | |
| 44 const base::FilePath& path) { | |
| 45 return ExternalMountPoints::GetSystemInstance()->CreateCrackedFileSystemURL( | |
| 46 origin, type, path); | |
| 47 } | |
| 48 | |
| 49 std::string NormalizedUTF8Path(const base::FilePath& path) { | |
| 50 return path.NormalizePathSeparators().AsUTF8Unsafe(); | |
| 51 } | |
| 52 | |
| 53 } // namespace | |
| 54 | |
| 55 TEST(FileSystemURLTest, ParsePersistent) { | |
| 56 FileSystemURL url = CreateFileSystemURL( | |
| 57 "filesystem:http://chromium.org/persistent/directory/file"); | |
| 58 ASSERT_TRUE(url.is_valid()); | |
| 59 EXPECT_EQ("http://chromium.org/", url.origin().spec()); | |
| 60 EXPECT_EQ(kFileSystemTypePersistent, url.type()); | |
| 61 EXPECT_EQ(FPL("file"), VirtualPath::BaseName(url.path()).value()); | |
| 62 EXPECT_EQ(FPL("directory"), url.path().DirName().value()); | |
| 63 } | |
| 64 | |
| 65 TEST(FileSystemURLTest, ParseTemporary) { | |
| 66 FileSystemURL url = CreateFileSystemURL( | |
| 67 "filesystem:http://chromium.org/temporary/directory/file"); | |
| 68 ASSERT_TRUE(url.is_valid()); | |
| 69 EXPECT_EQ("http://chromium.org/", url.origin().spec()); | |
| 70 EXPECT_EQ(kFileSystemTypeTemporary, url.type()); | |
| 71 EXPECT_EQ(FPL("file"), VirtualPath::BaseName(url.path()).value()); | |
| 72 EXPECT_EQ(FPL("directory"), url.path().DirName().value()); | |
| 73 } | |
| 74 | |
| 75 TEST(FileSystemURLTest, EnsureFilePathIsRelative) { | |
| 76 FileSystemURL url = CreateFileSystemURL( | |
| 77 "filesystem:http://chromium.org/temporary/////directory/file"); | |
| 78 ASSERT_TRUE(url.is_valid()); | |
| 79 EXPECT_EQ("http://chromium.org/", url.origin().spec()); | |
| 80 EXPECT_EQ(kFileSystemTypeTemporary, url.type()); | |
| 81 EXPECT_EQ(FPL("file"), VirtualPath::BaseName(url.path()).value()); | |
| 82 EXPECT_EQ(FPL("directory"), url.path().DirName().value()); | |
| 83 EXPECT_FALSE(url.path().IsAbsolute()); | |
| 84 } | |
| 85 | |
| 86 TEST(FileSystemURLTest, RejectBadSchemes) { | |
| 87 EXPECT_FALSE(CreateFileSystemURL("http://chromium.org/").is_valid()); | |
| 88 EXPECT_FALSE(CreateFileSystemURL("https://chromium.org/").is_valid()); | |
| 89 EXPECT_FALSE(CreateFileSystemURL("file:///foo/bar").is_valid()); | |
| 90 EXPECT_FALSE(CreateFileSystemURL("foobar:///foo/bar").is_valid()); | |
| 91 } | |
| 92 | |
| 93 TEST(FileSystemURLTest, UnescapePath) { | |
| 94 FileSystemURL url = CreateFileSystemURL( | |
| 95 "filesystem:http://chromium.org/persistent/%7Echromium/space%20bar"); | |
| 96 ASSERT_TRUE(url.is_valid()); | |
| 97 EXPECT_EQ(FPL("space bar"), VirtualPath::BaseName(url.path()).value()); | |
| 98 EXPECT_EQ(FPL("~chromium"), url.path().DirName().value()); | |
| 99 } | |
| 100 | |
| 101 TEST(FileSystemURLTest, RejectBadType) { | |
| 102 EXPECT_FALSE(CreateFileSystemURL( | |
| 103 "filesystem:http://c.org/foobar/file").is_valid()); | |
| 104 } | |
| 105 | |
| 106 TEST(FileSystemURLTest, RejectMalformedURL) { | |
| 107 EXPECT_FALSE(CreateFileSystemURL("filesystem:///foobar/file").is_valid()); | |
| 108 EXPECT_FALSE(CreateFileSystemURL("filesystem:foobar/file").is_valid()); | |
| 109 } | |
| 110 | |
| 111 TEST(FileSystemURLTest, CompareURLs) { | |
| 112 const GURL urls[] = { | |
| 113 GURL("filesystem:http://chromium.org/temporary/dir a/file a"), | |
| 114 GURL("filesystem:http://chromium.org/temporary/dir a/file a"), | |
| 115 GURL("filesystem:http://chromium.org/temporary/dir a/file b"), | |
| 116 GURL("filesystem:http://chromium.org/temporary/dir a/file aa"), | |
| 117 GURL("filesystem:http://chromium.org/temporary/dir b/file a"), | |
| 118 GURL("filesystem:http://chromium.org/temporary/dir aa/file b"), | |
| 119 GURL("filesystem:http://chromium.com/temporary/dir a/file a"), | |
| 120 GURL("filesystem:https://chromium.org/temporary/dir a/file a") | |
| 121 }; | |
| 122 | |
| 123 FileSystemURL::Comparator compare; | |
| 124 for (size_t i = 0; i < arraysize(urls); ++i) { | |
| 125 for (size_t j = 0; j < arraysize(urls); ++j) { | |
| 126 SCOPED_TRACE(testing::Message() << i << " < " << j); | |
| 127 EXPECT_EQ(urls[i] < urls[j], | |
| 128 compare(FileSystemURL::CreateForTest(urls[i]), | |
| 129 FileSystemURL::CreateForTest(urls[j]))); | |
| 130 } | |
| 131 } | |
| 132 | |
| 133 const FileSystemURL a = CreateFileSystemURL( | |
| 134 "filesystem:http://chromium.org/temporary/dir a/file a"); | |
| 135 const FileSystemURL b = CreateFileSystemURL( | |
| 136 "filesystem:http://chromium.org/persistent/dir a/file a"); | |
| 137 EXPECT_EQ(a.type() < b.type(), compare(a, b)); | |
| 138 EXPECT_EQ(b.type() < a.type(), compare(b, a)); | |
| 139 } | |
| 140 | |
| 141 TEST(FileSystemURLTest, IsParent) { | |
| 142 ScopedExternalFileSystem scoped1("foo", kFileSystemTypeSyncable, | |
| 143 base::FilePath()); | |
| 144 ScopedExternalFileSystem scoped2("bar", kFileSystemTypeSyncable, | |
| 145 base::FilePath()); | |
| 146 | |
| 147 const std::string root1 = GetFileSystemRootURI( | |
| 148 GURL("http://example.com"), kFileSystemTypeTemporary).spec(); | |
| 149 const std::string root2 = sync_file_system::GetSyncableFileSystemRootURI( | |
| 150 GURL("http://example.com"), "foo").spec(); | |
| 151 const std::string root3 = sync_file_system::GetSyncableFileSystemRootURI( | |
| 152 GURL("http://example.com"), "bar").spec(); | |
| 153 const std::string root4 = GetFileSystemRootURI( | |
| 154 GURL("http://chromium.org"), kFileSystemTypeTemporary).spec(); | |
| 155 | |
| 156 const std::string parent("dir"); | |
| 157 const std::string child("dir/child"); | |
| 158 const std::string other("other"); | |
| 159 | |
| 160 // True cases. | |
| 161 EXPECT_TRUE(CreateFileSystemURL(root1 + parent).IsParent( | |
| 162 CreateFileSystemURL(root1 + child))); | |
| 163 EXPECT_TRUE(CreateFileSystemURL(root2 + parent).IsParent( | |
| 164 CreateFileSystemURL(root2 + child))); | |
| 165 | |
| 166 // False cases: the path is not a child. | |
| 167 EXPECT_FALSE(CreateFileSystemURL(root1 + parent).IsParent( | |
| 168 CreateFileSystemURL(root1 + other))); | |
| 169 EXPECT_FALSE(CreateFileSystemURL(root1 + parent).IsParent( | |
| 170 CreateFileSystemURL(root1 + parent))); | |
| 171 EXPECT_FALSE(CreateFileSystemURL(root1 + child).IsParent( | |
| 172 CreateFileSystemURL(root1 + parent))); | |
| 173 | |
| 174 // False case: different types. | |
| 175 EXPECT_FALSE(CreateFileSystemURL(root1 + parent).IsParent( | |
| 176 CreateFileSystemURL(root2 + child))); | |
| 177 | |
| 178 // False case: different filesystem ID. | |
| 179 EXPECT_FALSE(CreateFileSystemURL(root1 + parent).IsParent( | |
| 180 CreateFileSystemURL(root3 + child))); | |
| 181 | |
| 182 // False case: different origins. | |
| 183 EXPECT_FALSE(CreateFileSystemURL(root1 + parent).IsParent( | |
| 184 CreateFileSystemURL(root4 + child))); | |
| 185 } | |
| 186 | |
| 187 TEST(FileSystemURLTest, DebugString) { | |
| 188 const GURL kOrigin("http://example.com"); | |
| 189 const base::FilePath kPath(FPL("dir/file")); | |
| 190 | |
| 191 const FileSystemURL kURL1 = FileSystemURL::CreateForTest( | |
| 192 kOrigin, kFileSystemTypeTemporary, kPath); | |
| 193 EXPECT_EQ("filesystem:http://example.com/temporary/" + | |
| 194 NormalizedUTF8Path(kPath), | |
| 195 kURL1.DebugString()); | |
| 196 | |
| 197 const base::FilePath kRoot(DRIVE FPL("/root")); | |
| 198 ScopedExternalFileSystem scoped_fs("foo", | |
| 199 kFileSystemTypeNativeLocal, | |
| 200 kRoot.NormalizePathSeparators()); | |
| 201 const FileSystemURL kURL2(CreateExternalFileSystemURL( | |
| 202 kOrigin, | |
| 203 kFileSystemTypeExternal, | |
| 204 scoped_fs.GetVirtualRootPath().Append(kPath))); | |
| 205 EXPECT_EQ("filesystem:http://example.com/external/" + | |
| 206 NormalizedUTF8Path(scoped_fs.GetVirtualRootPath().Append(kPath)) + | |
| 207 " (NativeLocal@foo:" + | |
| 208 NormalizedUTF8Path(kRoot.Append(kPath)) + ")", | |
| 209 kURL2.DebugString()); | |
| 210 } | |
| 211 | |
| 212 } // namespace fileapi | |
| OLD | NEW |