Only sync parent directory once after a leveldb file rename.
[chromium-blink-merge.git] / webkit / fileapi / file_system_util_unittest.cc
blobc370618b593e78425dc83956557dd48ad9b76244
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.
5 #include "webkit/fileapi/file_system_util.h"
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/file_system_url.h"
12 namespace fileapi {
13 namespace {
15 class FileSystemUtilTest : public testing::Test {};
17 TEST_F(FileSystemUtilTest, GetTempFileSystemRootURI) {
18 GURL origin_url("http://chromium.org");
19 fileapi::FileSystemType type = fileapi::kFileSystemTypeTemporary;
20 GURL uri = GURL("filesystem:http://chromium.org/temporary/");
21 EXPECT_EQ(uri, GetFileSystemRootURI(origin_url, type));
24 TEST_F(FileSystemUtilTest, GetPersistentFileSystemRootURI) {
25 GURL origin_url("http://chromium.org");
26 fileapi::FileSystemType type = fileapi::kFileSystemTypePersistent;
27 GURL uri = GURL("filesystem:http://chromium.org/persistent/");
28 EXPECT_EQ(uri, GetFileSystemRootURI(origin_url, type));
31 TEST_F(FileSystemUtilTest, VirtualPathBaseName) {
32 struct test_data {
33 const base::FilePath::StringType path;
34 const base::FilePath::StringType base_name;
35 } test_cases[] = {
36 { FILE_PATH_LITERAL("foo/bar"), FILE_PATH_LITERAL("bar") },
37 { FILE_PATH_LITERAL("foo/b:bar"), FILE_PATH_LITERAL("b:bar") },
38 { FILE_PATH_LITERAL(""), FILE_PATH_LITERAL("") },
39 { FILE_PATH_LITERAL("/"), FILE_PATH_LITERAL("/") },
40 { FILE_PATH_LITERAL("foo//////bar"), FILE_PATH_LITERAL("bar") },
41 { FILE_PATH_LITERAL("foo/bar/"), FILE_PATH_LITERAL("bar") },
42 { FILE_PATH_LITERAL("foo/bar/////"), FILE_PATH_LITERAL("bar") },
43 { FILE_PATH_LITERAL("/bar/////"), FILE_PATH_LITERAL("bar") },
44 { FILE_PATH_LITERAL("bar/////"), FILE_PATH_LITERAL("bar") },
45 { FILE_PATH_LITERAL("bar/"), FILE_PATH_LITERAL("bar") },
46 { FILE_PATH_LITERAL("/bar"), FILE_PATH_LITERAL("bar") },
47 { FILE_PATH_LITERAL("////bar"), FILE_PATH_LITERAL("bar") },
48 { FILE_PATH_LITERAL("bar"), FILE_PATH_LITERAL("bar") }
50 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) {
51 base::FilePath input = base::FilePath(test_cases[i].path);
52 base::FilePath base_name = VirtualPath::BaseName(input);
53 EXPECT_EQ(test_cases[i].base_name, base_name.value());
57 TEST_F(FileSystemUtilTest, VirtualPathDirName) {
58 struct test_data {
59 const base::FilePath::StringType path;
60 const base::FilePath::StringType dir_name;
61 } test_cases[] = {
62 { FILE_PATH_LITERAL("foo/bar"), FILE_PATH_LITERAL("foo") },
63 { FILE_PATH_LITERAL("foo/b:bar"), FILE_PATH_LITERAL("foo") },
64 { FILE_PATH_LITERAL(""), FILE_PATH_LITERAL(".") },
65 { FILE_PATH_LITERAL("/"), FILE_PATH_LITERAL("/") },
66 { FILE_PATH_LITERAL("foo//////bar"), FILE_PATH_LITERAL("foo") },
67 { FILE_PATH_LITERAL("foo/bar/"), FILE_PATH_LITERAL("foo") },
68 { FILE_PATH_LITERAL("foo/bar/////"), FILE_PATH_LITERAL("foo") },
69 { FILE_PATH_LITERAL("/bar/////"), FILE_PATH_LITERAL("/") },
70 { FILE_PATH_LITERAL("bar/////"), FILE_PATH_LITERAL(".") },
71 { FILE_PATH_LITERAL("bar/"), FILE_PATH_LITERAL(".") },
72 { FILE_PATH_LITERAL("/bar"), FILE_PATH_LITERAL("/") },
73 { FILE_PATH_LITERAL("////bar"), FILE_PATH_LITERAL("/") },
74 { FILE_PATH_LITERAL("bar"), FILE_PATH_LITERAL(".") },
75 { FILE_PATH_LITERAL("c:bar"), FILE_PATH_LITERAL(".") },
76 #ifdef FILE_PATH_USES_WIN_SEPARATORS
77 { FILE_PATH_LITERAL("foo\\bar"), FILE_PATH_LITERAL("foo") },
78 { FILE_PATH_LITERAL("foo\\b:bar"), FILE_PATH_LITERAL("foo") },
79 { FILE_PATH_LITERAL("\\"), FILE_PATH_LITERAL("\\") },
80 { FILE_PATH_LITERAL("foo\\\\\\\\\\\\bar"), FILE_PATH_LITERAL("foo") },
81 { FILE_PATH_LITERAL("foo\\bar\\"), FILE_PATH_LITERAL("foo") },
82 { FILE_PATH_LITERAL("foo\\bar\\\\\\\\\\"), FILE_PATH_LITERAL("foo") },
83 { FILE_PATH_LITERAL("\\bar\\\\\\\\\\"), FILE_PATH_LITERAL("\\") },
84 { FILE_PATH_LITERAL("bar\\\\\\\\\\"), FILE_PATH_LITERAL(".") },
85 { FILE_PATH_LITERAL("bar\\"), FILE_PATH_LITERAL(".") },
86 { FILE_PATH_LITERAL("\\bar"), FILE_PATH_LITERAL("\\") },
87 { FILE_PATH_LITERAL("\\\\\\\\bar"), FILE_PATH_LITERAL("\\") },
88 #endif
90 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) {
91 base::FilePath input = base::FilePath(test_cases[i].path);
92 base::FilePath dir_name = VirtualPath::DirName(input);
93 EXPECT_EQ(test_cases[i].dir_name, dir_name.value());
97 TEST_F(FileSystemUtilTest, GetNormalizedFilePath) {
98 struct test_data {
99 const base::FilePath::StringType path;
100 const base::FilePath::StringType normalized_path;
101 } test_cases[] = {
102 { FILE_PATH_LITERAL(""), FILE_PATH_LITERAL("/") },
103 { FILE_PATH_LITERAL("/"), FILE_PATH_LITERAL("/") },
104 { FILE_PATH_LITERAL("foo/bar"), FILE_PATH_LITERAL("/foo/bar") },
105 { FILE_PATH_LITERAL("/foo/bar"), FILE_PATH_LITERAL("/foo/bar") },
106 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
107 { FILE_PATH_LITERAL("\\foo"), FILE_PATH_LITERAL("/foo") },
108 #endif
110 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) {
111 base::FilePath input = base::FilePath(test_cases[i].path);
112 base::FilePath::StringType normalized_path_string =
113 VirtualPath::GetNormalizedFilePath(input);
114 EXPECT_EQ(test_cases[i].normalized_path, normalized_path_string);
118 TEST_F(FileSystemUtilTest, IsAbsolutePath) {
119 EXPECT_TRUE(VirtualPath::IsAbsolute(FILE_PATH_LITERAL("/")));
120 EXPECT_TRUE(VirtualPath::IsAbsolute(FILE_PATH_LITERAL("/foo/bar")));
121 EXPECT_FALSE(VirtualPath::IsAbsolute(base::FilePath::StringType()));
122 EXPECT_FALSE(VirtualPath::IsAbsolute(FILE_PATH_LITERAL("foo/bar")));
125 TEST_F(FileSystemUtilTest, VirtualPathGetComponents) {
126 struct test_data {
127 const base::FilePath::StringType path;
128 size_t count;
129 const base::FilePath::StringType components[3];
130 } test_cases[] = {
131 { FILE_PATH_LITERAL("foo/bar"),
133 { FILE_PATH_LITERAL("foo"), FILE_PATH_LITERAL("bar") } },
134 { FILE_PATH_LITERAL("foo"),
136 { FILE_PATH_LITERAL("foo") } },
137 { FILE_PATH_LITERAL("foo////bar"),
139 { FILE_PATH_LITERAL("foo"), FILE_PATH_LITERAL("bar") } },
140 { FILE_PATH_LITERAL("foo/c:bar"),
142 { FILE_PATH_LITERAL("foo"), FILE_PATH_LITERAL("c:bar") } },
143 { FILE_PATH_LITERAL("c:foo/bar"),
145 { FILE_PATH_LITERAL("c:foo"), FILE_PATH_LITERAL("bar") } },
146 { FILE_PATH_LITERAL("foo/bar"),
148 { FILE_PATH_LITERAL("foo"), FILE_PATH_LITERAL("bar") } },
149 { FILE_PATH_LITERAL("/foo/bar"),
151 { FILE_PATH_LITERAL("foo"), FILE_PATH_LITERAL("bar") } },
152 { FILE_PATH_LITERAL("c:/bar"),
154 { FILE_PATH_LITERAL("c:"), FILE_PATH_LITERAL("bar") } },
155 #ifdef FILE_PATH_USES_WIN_SEPARATORS
156 { FILE_PATH_LITERAL("c:\\bar"),
158 { FILE_PATH_LITERAL("c:"), FILE_PATH_LITERAL("bar") } },
159 #endif
161 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) {
162 base::FilePath input = base::FilePath(test_cases[i].path);
163 std::vector<base::FilePath::StringType> components;
164 VirtualPath::GetComponents(input, &components);
165 EXPECT_EQ(test_cases[i].count, components.size());
166 for (size_t j = 0; j < components.size(); ++j)
167 EXPECT_EQ(test_cases[i].components[j], components[j]);
171 TEST_F(FileSystemUtilTest, GetIsolatedFileSystemName) {
172 GURL origin_url("http://foo");
173 std::string fsname1 = GetIsolatedFileSystemName(origin_url, "bar");
174 EXPECT_EQ("http_foo_0:Isolated_bar", fsname1);
177 TEST_F(FileSystemUtilTest, CrackIsolatedFileSystemName) {
178 std::string fsid;
179 EXPECT_TRUE(CrackIsolatedFileSystemName("foo:Isolated_bar", &fsid));
180 EXPECT_EQ("bar", fsid);
181 EXPECT_TRUE(CrackIsolatedFileSystemName("foo:isolated_bar", &fsid));
182 EXPECT_EQ("bar", fsid);
183 EXPECT_TRUE(CrackIsolatedFileSystemName("foo:Isolated__bar", &fsid));
184 EXPECT_EQ("_bar", fsid);
185 EXPECT_TRUE(CrackIsolatedFileSystemName("foo::Isolated_bar", &fsid));
186 EXPECT_EQ("bar", fsid);
189 TEST_F(FileSystemUtilTest, RejectBadIsolatedFileSystemName) {
190 std::string fsid;
191 EXPECT_FALSE(CrackIsolatedFileSystemName("foobar", &fsid));
192 EXPECT_FALSE(CrackIsolatedFileSystemName("foo:_bar", &fsid));
193 EXPECT_FALSE(CrackIsolatedFileSystemName("foo:Isolatedbar", &fsid));
194 EXPECT_FALSE(CrackIsolatedFileSystemName("fooIsolatedbar", &fsid));
195 EXPECT_FALSE(CrackIsolatedFileSystemName("foo:Persistent", &fsid));
196 EXPECT_FALSE(CrackIsolatedFileSystemName("foo:Temporary", &fsid));
197 EXPECT_FALSE(CrackIsolatedFileSystemName("foo:External", &fsid));
198 EXPECT_FALSE(CrackIsolatedFileSystemName(":Isolated_bar", &fsid));
199 EXPECT_FALSE(CrackIsolatedFileSystemName("foo:Isolated_", &fsid));
202 TEST_F(FileSystemUtilTest, AreSameFileSystem) {
203 FileSystemURL url_foo_temp_a = FileSystemURL::CreateForTest(
204 GURL("http://foo"), kFileSystemTypeTemporary,
205 base::FilePath::FromUTF8Unsafe("a"));
206 FileSystemURL url_foo_temp_b = FileSystemURL::CreateForTest(
207 GURL("http://foo"), kFileSystemTypeTemporary,
208 base::FilePath::FromUTF8Unsafe("b"));
209 FileSystemURL url_foo_perm_a = FileSystemURL::CreateForTest(
210 GURL("http://foo"), kFileSystemTypePersistent,
211 base::FilePath::FromUTF8Unsafe("a"));
212 FileSystemURL url_bar_temp_a = FileSystemURL::CreateForTest(
213 GURL("http://bar"), kFileSystemTypeTemporary,
214 base::FilePath::FromUTF8Unsafe("a"));
215 FileSystemURL url_bar_perm_a = FileSystemURL::CreateForTest(
216 GURL("http://bar"), kFileSystemTypePersistent,
217 base::FilePath::FromUTF8Unsafe("a"));
219 EXPECT_TRUE(AreSameFileSystem(url_foo_temp_a, url_foo_temp_a));
220 EXPECT_TRUE(AreSameFileSystem(url_foo_temp_a, url_foo_temp_b));
221 EXPECT_FALSE(AreSameFileSystem(url_foo_temp_a, url_foo_perm_a));
222 EXPECT_FALSE(AreSameFileSystem(url_foo_temp_a, url_bar_temp_a));
223 EXPECT_FALSE(AreSameFileSystem(url_foo_temp_a, url_bar_perm_a));
226 } // namespace (anonymous)
227 } // namespace fileapi