| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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_url.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 | |
| 9 #include "base/files/file_path.h" | |
| 10 #include "base/macros.h" | |
| 11 #include "storage/common/fileapi/file_system_types.h" | |
| 12 #include "storage/common/fileapi/file_system_util.h" | |
| 13 #include "testing/gtest/include/gtest/gtest.h" | |
| 14 #include "url/gurl.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 using storage::FileSystemURL; | |
| 25 using storage::kFileSystemTypeExternal; | |
| 26 using storage::kFileSystemTypeIsolated; | |
| 27 using storage::kFileSystemTypePersistent; | |
| 28 using storage::kFileSystemTypeTemporary; | |
| 29 using storage::VirtualPath; | |
| 30 | |
| 31 namespace content { | |
| 32 | |
| 33 namespace { | |
| 34 | |
| 35 FileSystemURL CreateFileSystemURL(const std::string& url_string) { | |
| 36 FileSystemURL url = FileSystemURL::CreateForTest(GURL(url_string)); | |
| 37 EXPECT_TRUE(url.type() != kFileSystemTypeExternal && | |
| 38 url.type() != kFileSystemTypeIsolated); | |
| 39 return url; | |
| 40 } | |
| 41 | |
| 42 std::string NormalizedUTF8Path(const base::FilePath& path) { | |
| 43 return path.NormalizePathSeparators().AsUTF8Unsafe(); | |
| 44 } | |
| 45 | |
| 46 } // namespace | |
| 47 | |
| 48 TEST(FileSystemURLTest, ParsePersistent) { | |
| 49 FileSystemURL url = CreateFileSystemURL( | |
| 50 "filesystem:http://chromium.org/persistent/directory/file"); | |
| 51 ASSERT_TRUE(url.is_valid()); | |
| 52 EXPECT_EQ("http://chromium.org/", url.origin().spec()); | |
| 53 EXPECT_EQ(kFileSystemTypePersistent, url.type()); | |
| 54 EXPECT_EQ(FPL("file"), VirtualPath::BaseName(url.path()).value()); | |
| 55 EXPECT_EQ(FPL("directory"), url.path().DirName().value()); | |
| 56 } | |
| 57 | |
| 58 TEST(FileSystemURLTest, ParseTemporary) { | |
| 59 FileSystemURL url = CreateFileSystemURL( | |
| 60 "filesystem:http://chromium.org/temporary/directory/file"); | |
| 61 ASSERT_TRUE(url.is_valid()); | |
| 62 EXPECT_EQ("http://chromium.org/", url.origin().spec()); | |
| 63 EXPECT_EQ(kFileSystemTypeTemporary, url.type()); | |
| 64 EXPECT_EQ(FPL("file"), VirtualPath::BaseName(url.path()).value()); | |
| 65 EXPECT_EQ(FPL("directory"), url.path().DirName().value()); | |
| 66 } | |
| 67 | |
| 68 TEST(FileSystemURLTest, EnsureFilePathIsRelative) { | |
| 69 FileSystemURL url = CreateFileSystemURL( | |
| 70 "filesystem:http://chromium.org/temporary/////directory/file"); | |
| 71 ASSERT_TRUE(url.is_valid()); | |
| 72 EXPECT_EQ("http://chromium.org/", url.origin().spec()); | |
| 73 EXPECT_EQ(kFileSystemTypeTemporary, url.type()); | |
| 74 EXPECT_EQ(FPL("file"), VirtualPath::BaseName(url.path()).value()); | |
| 75 EXPECT_EQ(FPL("directory"), url.path().DirName().value()); | |
| 76 EXPECT_FALSE(url.path().IsAbsolute()); | |
| 77 } | |
| 78 | |
| 79 TEST(FileSystemURLTest, RejectBadSchemes) { | |
| 80 EXPECT_FALSE(CreateFileSystemURL("http://chromium.org/").is_valid()); | |
| 81 EXPECT_FALSE(CreateFileSystemURL("https://chromium.org/").is_valid()); | |
| 82 EXPECT_FALSE(CreateFileSystemURL("file:///foo/bar").is_valid()); | |
| 83 EXPECT_FALSE(CreateFileSystemURL("foobar:///foo/bar").is_valid()); | |
| 84 } | |
| 85 | |
| 86 TEST(FileSystemURLTest, UnescapePath) { | |
| 87 FileSystemURL url = CreateFileSystemURL( | |
| 88 "filesystem:http://chromium.org/persistent/%7Echromium/space%20bar"); | |
| 89 ASSERT_TRUE(url.is_valid()); | |
| 90 EXPECT_EQ(FPL("space bar"), VirtualPath::BaseName(url.path()).value()); | |
| 91 EXPECT_EQ(FPL("~chromium"), url.path().DirName().value()); | |
| 92 } | |
| 93 | |
| 94 TEST(FileSystemURLTest, RejectBadType) { | |
| 95 EXPECT_FALSE(CreateFileSystemURL( | |
| 96 "filesystem:http://c.org/foobar/file").is_valid()); | |
| 97 EXPECT_FALSE(CreateFileSystemURL( | |
| 98 "filesystem:http://c.org/temporaryfoo/file").is_valid()); | |
| 99 } | |
| 100 | |
| 101 TEST(FileSystemURLTest, RejectMalformedURL) { | |
| 102 EXPECT_FALSE(CreateFileSystemURL("filesystem:///foobar/file").is_valid()); | |
| 103 EXPECT_FALSE(CreateFileSystemURL("filesystem:foobar/file").is_valid()); | |
| 104 } | |
| 105 | |
| 106 TEST(FileSystemURLTest, CompareURLs) { | |
| 107 const GURL urls[] = { | |
| 108 GURL("filesystem:http://chromium.org/temporary/dir a/file a"), | |
| 109 GURL("filesystem:http://chromium.org/temporary/dir a/file a"), | |
| 110 GURL("filesystem:http://chromium.org/temporary/dir a/file b"), | |
| 111 GURL("filesystem:http://chromium.org/temporary/dir a/file aa"), | |
| 112 GURL("filesystem:http://chromium.org/temporary/dir b/file a"), | |
| 113 GURL("filesystem:http://chromium.org/temporary/dir aa/file b"), | |
| 114 GURL("filesystem:http://chromium.com/temporary/dir a/file a"), | |
| 115 GURL("filesystem:https://chromium.org/temporary/dir a/file a") | |
| 116 }; | |
| 117 | |
| 118 FileSystemURL::Comparator compare; | |
| 119 for (size_t i = 0; i < arraysize(urls); ++i) { | |
| 120 for (size_t j = 0; j < arraysize(urls); ++j) { | |
| 121 SCOPED_TRACE(testing::Message() << i << " < " << j); | |
| 122 EXPECT_EQ(urls[i] < urls[j], | |
| 123 compare(FileSystemURL::CreateForTest(urls[i]), | |
| 124 FileSystemURL::CreateForTest(urls[j]))); | |
| 125 } | |
| 126 } | |
| 127 | |
| 128 const FileSystemURL a = CreateFileSystemURL( | |
| 129 "filesystem:http://chromium.org/temporary/dir a/file a"); | |
| 130 const FileSystemURL b = CreateFileSystemURL( | |
| 131 "filesystem:http://chromium.org/persistent/dir a/file a"); | |
| 132 EXPECT_EQ(a.type() < b.type(), compare(a, b)); | |
| 133 EXPECT_EQ(b.type() < a.type(), compare(b, a)); | |
| 134 } | |
| 135 | |
| 136 TEST(FileSystemURLTest, IsParent) { | |
| 137 const std::string root1 = GetFileSystemRootURI( | |
| 138 GURL("http://example.com"), kFileSystemTypeTemporary).spec(); | |
| 139 const std::string root2 = GetFileSystemRootURI( | |
| 140 GURL("http://example.com"), kFileSystemTypePersistent).spec(); | |
| 141 const std::string root3 = GetFileSystemRootURI( | |
| 142 GURL("http://chromium.org"), kFileSystemTypeTemporary).spec(); | |
| 143 | |
| 144 const std::string parent("dir"); | |
| 145 const std::string child("dir/child"); | |
| 146 const std::string other("other"); | |
| 147 | |
| 148 // True cases. | |
| 149 EXPECT_TRUE(CreateFileSystemURL(root1 + parent).IsParent( | |
| 150 CreateFileSystemURL(root1 + child))); | |
| 151 EXPECT_TRUE(CreateFileSystemURL(root2 + parent).IsParent( | |
| 152 CreateFileSystemURL(root2 + child))); | |
| 153 | |
| 154 // False cases: the path is not a child. | |
| 155 EXPECT_FALSE(CreateFileSystemURL(root1 + parent).IsParent( | |
| 156 CreateFileSystemURL(root1 + other))); | |
| 157 EXPECT_FALSE(CreateFileSystemURL(root1 + parent).IsParent( | |
| 158 CreateFileSystemURL(root1 + parent))); | |
| 159 EXPECT_FALSE(CreateFileSystemURL(root1 + child).IsParent( | |
| 160 CreateFileSystemURL(root1 + parent))); | |
| 161 | |
| 162 // False case: different types. | |
| 163 EXPECT_FALSE(CreateFileSystemURL(root1 + parent).IsParent( | |
| 164 CreateFileSystemURL(root2 + child))); | |
| 165 | |
| 166 // False case: different origins. | |
| 167 EXPECT_FALSE(CreateFileSystemURL(root1 + parent).IsParent( | |
| 168 CreateFileSystemURL(root3 + child))); | |
| 169 } | |
| 170 | |
| 171 TEST(FileSystemURLTest, ToGURL) { | |
| 172 EXPECT_TRUE(FileSystemURL().ToGURL().is_empty()); | |
| 173 const char* kTestURL[] = { | |
| 174 "filesystem:http://chromium.org/persistent/directory/file0", | |
| 175 "filesystem:http://chromium.org/temporary/directory/file1", | |
| 176 "filesystem:http://chromium.org/isolated/directory/file2", | |
| 177 "filesystem:http://chromium.org/external/directory/file2", | |
| 178 "filesystem:http://chromium.org/test/directory/file3", | |
| 179 "filesystem:http://chromium.org/test/plus%2B/space%20/colon%3A", | |
| 180 }; | |
| 181 | |
| 182 for (size_t i = 0; i < arraysize(kTestURL); ++i) { | |
| 183 EXPECT_EQ( | |
| 184 kTestURL[i], | |
| 185 FileSystemURL::CreateForTest(GURL(kTestURL[i])).ToGURL().spec()); | |
| 186 } | |
| 187 } | |
| 188 | |
| 189 TEST(FileSystemURLTest, DebugString) { | |
| 190 const GURL kOrigin("http://example.com"); | |
| 191 const base::FilePath kPath(FPL("dir/file")); | |
| 192 | |
| 193 const FileSystemURL kURL1 = FileSystemURL::CreateForTest( | |
| 194 kOrigin, kFileSystemTypeTemporary, kPath); | |
| 195 EXPECT_EQ("filesystem:http://example.com/temporary/" + | |
| 196 NormalizedUTF8Path(kPath), | |
| 197 kURL1.DebugString()); | |
| 198 } | |
| 199 | |
| 200 TEST(FileSystemURLTest, IsInSameFileSystem) { | |
| 201 FileSystemURL url_foo_temp_a = FileSystemURL::CreateForTest( | |
| 202 GURL("http://foo"), kFileSystemTypeTemporary, | |
| 203 base::FilePath::FromUTF8Unsafe("a")); | |
| 204 FileSystemURL url_foo_temp_b = FileSystemURL::CreateForTest( | |
| 205 GURL("http://foo"), kFileSystemTypeTemporary, | |
| 206 base::FilePath::FromUTF8Unsafe("b")); | |
| 207 FileSystemURL url_foo_perm_a = FileSystemURL::CreateForTest( | |
| 208 GURL("http://foo"), kFileSystemTypePersistent, | |
| 209 base::FilePath::FromUTF8Unsafe("a")); | |
| 210 FileSystemURL url_bar_temp_a = FileSystemURL::CreateForTest( | |
| 211 GURL("http://bar"), kFileSystemTypeTemporary, | |
| 212 base::FilePath::FromUTF8Unsafe("a")); | |
| 213 FileSystemURL url_bar_perm_a = FileSystemURL::CreateForTest( | |
| 214 GURL("http://bar"), kFileSystemTypePersistent, | |
| 215 base::FilePath::FromUTF8Unsafe("a")); | |
| 216 | |
| 217 EXPECT_TRUE(url_foo_temp_a.IsInSameFileSystem(url_foo_temp_a)); | |
| 218 EXPECT_TRUE(url_foo_temp_a.IsInSameFileSystem(url_foo_temp_b)); | |
| 219 EXPECT_FALSE(url_foo_temp_a.IsInSameFileSystem(url_foo_perm_a)); | |
| 220 EXPECT_FALSE(url_foo_temp_a.IsInSameFileSystem(url_bar_temp_a)); | |
| 221 EXPECT_FALSE(url_foo_temp_a.IsInSameFileSystem(url_bar_perm_a)); | |
| 222 } | |
| 223 | |
| 224 } // namespace content | |
| OLD | NEW |