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