Trust the renderer's same-document navigation flag if it is a same-origin nav.
[chromium-blink-merge.git] / content / common / fileapi / file_system_util_unittest.cc
blob61dac02933350fbfb491d1e2956a06e8830270ee
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.
5 #include "webkit/common/fileapi/file_system_util.h"
7 #include "base/files/file_path.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "url/gurl.h"
11 using fileapi::CrackIsolatedFileSystemName;
12 using fileapi::FileSystemType;
13 using fileapi::GetIsolatedFileSystemName;
14 using fileapi::ValidateIsolatedFileSystemId;
15 using fileapi::VirtualPath;
17 namespace content {
18 namespace {
20 class FileSystemUtilTest : public testing::Test {};
22 TEST_F(FileSystemUtilTest, ParseFileSystemSchemeURL) {
23 GURL uri("filesystem:http://chromium.org/temporary/foo/bar");
24 GURL origin_url;
25 FileSystemType type;
26 base::FilePath virtual_path;
27 ParseFileSystemSchemeURL(uri, &origin_url, &type, &virtual_path);
28 EXPECT_EQ(GURL("http://chromium.org"), origin_url);
29 EXPECT_EQ(fileapi::kFileSystemTypeTemporary, type);
30 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
31 base::FilePath expected_path(FILE_PATH_LITERAL("foo\\bar"));
32 #else
33 base::FilePath expected_path(FILE_PATH_LITERAL("foo/bar"));
34 #endif
35 EXPECT_EQ(expected_path, virtual_path);
38 TEST_F(FileSystemUtilTest, GetTempFileSystemRootURI) {
39 GURL origin_url("http://chromium.org");
40 fileapi::FileSystemType type = fileapi::kFileSystemTypeTemporary;
41 GURL uri = GURL("filesystem:http://chromium.org/temporary/");
42 EXPECT_EQ(uri, GetFileSystemRootURI(origin_url, type));
45 TEST_F(FileSystemUtilTest, GetPersistentFileSystemRootURI) {
46 GURL origin_url("http://chromium.org");
47 fileapi::FileSystemType type = fileapi::kFileSystemTypePersistent;
48 GURL uri = GURL("filesystem:http://chromium.org/persistent/");
49 EXPECT_EQ(uri, GetFileSystemRootURI(origin_url, type));
52 TEST_F(FileSystemUtilTest, VirtualPathBaseName) {
53 struct test_data {
54 const base::FilePath::StringType path;
55 const base::FilePath::StringType base_name;
56 } test_cases[] = {
57 { FILE_PATH_LITERAL("foo/bar"), FILE_PATH_LITERAL("bar") },
58 { FILE_PATH_LITERAL("foo/b:bar"), FILE_PATH_LITERAL("b:bar") },
59 { FILE_PATH_LITERAL(""), FILE_PATH_LITERAL("") },
60 { FILE_PATH_LITERAL("/"), FILE_PATH_LITERAL("/") },
61 { FILE_PATH_LITERAL("foo//////bar"), FILE_PATH_LITERAL("bar") },
62 { FILE_PATH_LITERAL("foo/bar/"), FILE_PATH_LITERAL("bar") },
63 { FILE_PATH_LITERAL("foo/bar/////"), FILE_PATH_LITERAL("bar") },
64 { FILE_PATH_LITERAL("/bar/////"), FILE_PATH_LITERAL("bar") },
65 { FILE_PATH_LITERAL("bar/////"), FILE_PATH_LITERAL("bar") },
66 { FILE_PATH_LITERAL("bar/"), FILE_PATH_LITERAL("bar") },
67 { FILE_PATH_LITERAL("/bar"), FILE_PATH_LITERAL("bar") },
68 { FILE_PATH_LITERAL("////bar"), FILE_PATH_LITERAL("bar") },
69 { FILE_PATH_LITERAL("bar"), FILE_PATH_LITERAL("bar") }
71 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) {
72 base::FilePath input = base::FilePath(test_cases[i].path);
73 base::FilePath base_name = VirtualPath::BaseName(input);
74 EXPECT_EQ(test_cases[i].base_name, base_name.value());
78 TEST_F(FileSystemUtilTest, VirtualPathDirName) {
79 struct test_data {
80 const base::FilePath::StringType path;
81 const base::FilePath::StringType dir_name;
82 } test_cases[] = {
83 { FILE_PATH_LITERAL("foo/bar"), FILE_PATH_LITERAL("foo") },
84 { FILE_PATH_LITERAL("foo/b:bar"), FILE_PATH_LITERAL("foo") },
85 { FILE_PATH_LITERAL(""), FILE_PATH_LITERAL(".") },
86 { FILE_PATH_LITERAL("/"), FILE_PATH_LITERAL("/") },
87 { FILE_PATH_LITERAL("foo//////bar"), FILE_PATH_LITERAL("foo") },
88 { FILE_PATH_LITERAL("foo/bar/"), FILE_PATH_LITERAL("foo") },
89 { FILE_PATH_LITERAL("foo/bar/////"), FILE_PATH_LITERAL("foo") },
90 { FILE_PATH_LITERAL("/bar/////"), FILE_PATH_LITERAL("/") },
91 { FILE_PATH_LITERAL("bar/////"), FILE_PATH_LITERAL(".") },
92 { FILE_PATH_LITERAL("bar/"), FILE_PATH_LITERAL(".") },
93 { FILE_PATH_LITERAL("/bar"), FILE_PATH_LITERAL("/") },
94 { FILE_PATH_LITERAL("////bar"), FILE_PATH_LITERAL("/") },
95 { FILE_PATH_LITERAL("bar"), FILE_PATH_LITERAL(".") },
96 { FILE_PATH_LITERAL("c:bar"), FILE_PATH_LITERAL(".") },
97 #ifdef FILE_PATH_USES_WIN_SEPARATORS
98 { FILE_PATH_LITERAL("foo\\bar"), FILE_PATH_LITERAL("foo") },
99 { FILE_PATH_LITERAL("foo\\b:bar"), FILE_PATH_LITERAL("foo") },
100 { FILE_PATH_LITERAL("\\"), FILE_PATH_LITERAL("\\") },
101 { FILE_PATH_LITERAL("foo\\\\\\\\\\\\bar"), FILE_PATH_LITERAL("foo") },
102 { FILE_PATH_LITERAL("foo\\bar\\"), FILE_PATH_LITERAL("foo") },
103 { FILE_PATH_LITERAL("foo\\bar\\\\\\\\\\"), FILE_PATH_LITERAL("foo") },
104 { FILE_PATH_LITERAL("\\bar\\\\\\\\\\"), FILE_PATH_LITERAL("\\") },
105 { FILE_PATH_LITERAL("bar\\\\\\\\\\"), FILE_PATH_LITERAL(".") },
106 { FILE_PATH_LITERAL("bar\\"), FILE_PATH_LITERAL(".") },
107 { FILE_PATH_LITERAL("\\bar"), FILE_PATH_LITERAL("\\") },
108 { FILE_PATH_LITERAL("\\\\\\\\bar"), FILE_PATH_LITERAL("\\") },
109 #endif
111 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) {
112 base::FilePath input = base::FilePath(test_cases[i].path);
113 base::FilePath dir_name = VirtualPath::DirName(input);
114 EXPECT_EQ(test_cases[i].dir_name, dir_name.value());
118 TEST_F(FileSystemUtilTest, GetNormalizedFilePath) {
119 struct test_data {
120 const base::FilePath::StringType path;
121 const base::FilePath::StringType normalized_path;
122 } test_cases[] = {
123 { FILE_PATH_LITERAL(""), FILE_PATH_LITERAL("/") },
124 { FILE_PATH_LITERAL("/"), FILE_PATH_LITERAL("/") },
125 { FILE_PATH_LITERAL("foo/bar"), FILE_PATH_LITERAL("/foo/bar") },
126 { FILE_PATH_LITERAL("/foo/bar"), FILE_PATH_LITERAL("/foo/bar") },
127 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
128 { FILE_PATH_LITERAL("\\foo"), FILE_PATH_LITERAL("/foo") },
129 #endif
131 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) {
132 base::FilePath input = base::FilePath(test_cases[i].path);
133 base::FilePath::StringType normalized_path_string =
134 VirtualPath::GetNormalizedFilePath(input);
135 EXPECT_EQ(test_cases[i].normalized_path, normalized_path_string);
139 TEST_F(FileSystemUtilTest, IsAbsolutePath) {
140 EXPECT_TRUE(VirtualPath::IsAbsolute(FILE_PATH_LITERAL("/")));
141 EXPECT_TRUE(VirtualPath::IsAbsolute(FILE_PATH_LITERAL("/foo/bar")));
142 EXPECT_FALSE(VirtualPath::IsAbsolute(base::FilePath::StringType()));
143 EXPECT_FALSE(VirtualPath::IsAbsolute(FILE_PATH_LITERAL("foo/bar")));
146 TEST_F(FileSystemUtilTest, IsRootPath) {
147 EXPECT_TRUE(VirtualPath::IsRootPath(base::FilePath(FILE_PATH_LITERAL(""))));
148 EXPECT_TRUE(VirtualPath::IsRootPath(base::FilePath()));
149 EXPECT_TRUE(VirtualPath::IsRootPath(base::FilePath(FILE_PATH_LITERAL("/"))));
150 EXPECT_TRUE(VirtualPath::IsRootPath(base::FilePath(FILE_PATH_LITERAL("//"))));
151 EXPECT_FALSE(VirtualPath::IsRootPath(
152 base::FilePath(FILE_PATH_LITERAL("c:/"))));
153 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
154 EXPECT_TRUE(VirtualPath::IsRootPath(base::FilePath(FILE_PATH_LITERAL("\\"))));
155 EXPECT_FALSE(VirtualPath::IsRootPath(
156 base::FilePath(FILE_PATH_LITERAL("c:\\"))));
157 #endif
160 TEST_F(FileSystemUtilTest, VirtualPathGetComponents) {
161 struct test_data {
162 const base::FilePath::StringType path;
163 size_t count;
164 const base::FilePath::StringType components[2];
165 } test_cases[] = {
166 { FILE_PATH_LITERAL("foo/bar"),
168 { FILE_PATH_LITERAL("foo"), FILE_PATH_LITERAL("bar") } },
169 { FILE_PATH_LITERAL("foo"),
171 { FILE_PATH_LITERAL("foo"), FILE_PATH_LITERAL("") } },
172 { FILE_PATH_LITERAL("foo////bar"),
174 { FILE_PATH_LITERAL("foo"), FILE_PATH_LITERAL("bar") } },
175 { FILE_PATH_LITERAL("foo/c:bar"),
177 { FILE_PATH_LITERAL("foo"), FILE_PATH_LITERAL("c:bar") } },
178 { FILE_PATH_LITERAL("c:foo/bar"),
180 { FILE_PATH_LITERAL("c:foo"), FILE_PATH_LITERAL("bar") } },
181 { FILE_PATH_LITERAL("foo/bar"),
183 { FILE_PATH_LITERAL("foo"), FILE_PATH_LITERAL("bar") } },
184 { FILE_PATH_LITERAL("/foo/bar"),
186 { FILE_PATH_LITERAL("foo"), FILE_PATH_LITERAL("bar") } },
187 { FILE_PATH_LITERAL("c:/bar"),
189 { FILE_PATH_LITERAL("c:"), FILE_PATH_LITERAL("bar") } },
190 #ifdef FILE_PATH_USES_WIN_SEPARATORS
191 { FILE_PATH_LITERAL("c:\\bar"),
193 { FILE_PATH_LITERAL("c:"), FILE_PATH_LITERAL("bar") } },
194 #endif
196 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) {
197 base::FilePath input = base::FilePath(test_cases[i].path);
198 std::vector<base::FilePath::StringType> components;
199 VirtualPath::GetComponents(input, &components);
200 EXPECT_EQ(test_cases[i].count, components.size());
201 for (size_t j = 0; j < components.size(); ++j)
202 EXPECT_EQ(test_cases[i].components[j], components[j]);
204 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) {
205 base::FilePath input = base::FilePath(test_cases[i].path);
206 std::vector<std::string> components;
207 VirtualPath::GetComponentsUTF8Unsafe(input, &components);
208 EXPECT_EQ(test_cases[i].count, components.size());
209 for (size_t j = 0; j < components.size(); ++j) {
210 EXPECT_EQ(base::FilePath(test_cases[i].components[j]).AsUTF8Unsafe(),
211 components[j]);
216 TEST_F(FileSystemUtilTest, GetIsolatedFileSystemName) {
217 GURL origin_url("http://foo");
218 std::string fsname1 = GetIsolatedFileSystemName(origin_url, "bar");
219 EXPECT_EQ("http_foo_0:Isolated_bar", fsname1);
222 TEST_F(FileSystemUtilTest, CrackIsolatedFileSystemName) {
223 std::string fsid;
224 EXPECT_TRUE(CrackIsolatedFileSystemName("foo:Isolated_bar", &fsid));
225 EXPECT_EQ("bar", fsid);
226 EXPECT_TRUE(CrackIsolatedFileSystemName("foo:isolated_bar", &fsid));
227 EXPECT_EQ("bar", fsid);
228 EXPECT_TRUE(CrackIsolatedFileSystemName("foo:Isolated__bar", &fsid));
229 EXPECT_EQ("_bar", fsid);
230 EXPECT_TRUE(CrackIsolatedFileSystemName("foo::Isolated_bar", &fsid));
231 EXPECT_EQ("bar", fsid);
234 TEST_F(FileSystemUtilTest, RejectBadIsolatedFileSystemName) {
235 std::string fsid;
236 EXPECT_FALSE(CrackIsolatedFileSystemName("foobar", &fsid));
237 EXPECT_FALSE(CrackIsolatedFileSystemName("foo:_bar", &fsid));
238 EXPECT_FALSE(CrackIsolatedFileSystemName("foo:Isolatedbar", &fsid));
239 EXPECT_FALSE(CrackIsolatedFileSystemName("fooIsolatedbar", &fsid));
240 EXPECT_FALSE(CrackIsolatedFileSystemName("foo:Persistent", &fsid));
241 EXPECT_FALSE(CrackIsolatedFileSystemName("foo:Temporary", &fsid));
242 EXPECT_FALSE(CrackIsolatedFileSystemName("foo:External", &fsid));
243 EXPECT_FALSE(CrackIsolatedFileSystemName(":Isolated_bar", &fsid));
244 EXPECT_FALSE(CrackIsolatedFileSystemName("foo:Isolated_", &fsid));
247 TEST_F(FileSystemUtilTest, ValidateIsolatedFileSystemId) {
248 EXPECT_TRUE(ValidateIsolatedFileSystemId("ABCDEF0123456789ABCDEF0123456789"));
249 EXPECT_TRUE(ValidateIsolatedFileSystemId("ABCDEFABCDEFABCDEFABCDEFABCDEFAB"));
250 EXPECT_TRUE(ValidateIsolatedFileSystemId("01234567890123456789012345678901"));
252 const size_t kExpectedFileSystemIdSize = 32;
254 // Should not contain lowercase characters.
255 const std::string kLowercaseId = "abcdef0123456789abcdef0123456789";
256 EXPECT_EQ(kExpectedFileSystemIdSize, kLowercaseId.size());
257 EXPECT_FALSE(ValidateIsolatedFileSystemId(kLowercaseId));
259 // Should not be shorter/longer than expected.
260 EXPECT_FALSE(ValidateIsolatedFileSystemId(std::string()));
262 const std::string kShorterId = "ABCDEF0123456789ABCDEF";
263 EXPECT_GT(kExpectedFileSystemIdSize, kShorterId.size());
264 EXPECT_FALSE(ValidateIsolatedFileSystemId(kShorterId));
266 const std::string kLongerId = "ABCDEF0123456789ABCDEF0123456789ABCDEF";
267 EXPECT_LT(kExpectedFileSystemIdSize, kLongerId.size());
268 EXPECT_FALSE(ValidateIsolatedFileSystemId(kLongerId));
270 // Should not contain not alphabetical nor numerical characters.
271 const std::string kSlashId = "ABCD/EFGH/IJKL/MNOP/QRST/UVWX/YZ";
272 EXPECT_EQ(kExpectedFileSystemIdSize, kSlashId.size());
273 EXPECT_FALSE(ValidateIsolatedFileSystemId(kSlashId));
275 const std::string kBackslashId = "ABCD\\EFGH\\IJKL\\MNOP\\QRST\\UVWX\\YZ";
276 EXPECT_EQ(kExpectedFileSystemIdSize, kBackslashId.size());
277 EXPECT_FALSE(ValidateIsolatedFileSystemId(kBackslashId));
279 const std::string kSpaceId = "ABCD EFGH IJKL MNOP QRST UVWX YZ";
280 EXPECT_EQ(kExpectedFileSystemIdSize, kSpaceId.size());
281 EXPECT_FALSE(ValidateIsolatedFileSystemId(kSpaceId));
284 } // namespace
285 } // namespace content