Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(365)

Side by Side Diff: content/common/fileapi/file_system_util_unittest.cc

Issue 2827933003: Move storage tests from content/common next to the files they cover. (Closed)
Patch Set: Add missing dep. Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « content/common/database_identifier_unittest.cc ('k') | content/test/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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/common/fileapi/file_system_util.h"
6
7 #include <stddef.h>
8
9 #include "base/files/file_path.h"
10 #include "base/macros.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "url/gurl.h"
13
14 using storage::CrackIsolatedFileSystemName;
15 using storage::GetExternalFileSystemRootURIString;
16 using storage::GetIsolatedFileSystemName;
17 using storage::GetIsolatedFileSystemRootURIString;
18 using storage::ValidateIsolatedFileSystemId;
19 using storage::VirtualPath;
20
21 namespace content {
22 namespace {
23
24 class FileSystemUtilTest : public testing::Test {};
25
26 TEST_F(FileSystemUtilTest, ParseFileSystemSchemeURL) {
27 GURL uri("filesystem:http://chromium.org/temporary/foo/bar");
28 GURL origin_url;
29 storage::FileSystemType type;
30 base::FilePath virtual_path;
31 ParseFileSystemSchemeURL(uri, &origin_url, &type, &virtual_path);
32 EXPECT_EQ(GURL("http://chromium.org"), origin_url);
33 EXPECT_EQ(storage::kFileSystemTypeTemporary, type);
34 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
35 base::FilePath expected_path(FILE_PATH_LITERAL("foo\\bar"));
36 #else
37 base::FilePath expected_path(FILE_PATH_LITERAL("foo/bar"));
38 #endif
39 EXPECT_EQ(expected_path, virtual_path);
40 }
41
42 TEST_F(FileSystemUtilTest, GetTempFileSystemRootURI) {
43 GURL origin_url("http://chromium.org");
44 storage::FileSystemType type = storage::kFileSystemTypeTemporary;
45 GURL uri = GURL("filesystem:http://chromium.org/temporary/");
46 EXPECT_EQ(uri, GetFileSystemRootURI(origin_url, type));
47 }
48
49 TEST_F(FileSystemUtilTest, GetPersistentFileSystemRootURI) {
50 GURL origin_url("http://chromium.org");
51 storage::FileSystemType type = storage::kFileSystemTypePersistent;
52 GURL uri = GURL("filesystem:http://chromium.org/persistent/");
53 EXPECT_EQ(uri, GetFileSystemRootURI(origin_url, type));
54 }
55
56 TEST_F(FileSystemUtilTest, VirtualPathBaseName) {
57 struct test_data {
58 const base::FilePath::StringType path;
59 const base::FilePath::StringType base_name;
60 } test_cases[] = {
61 { FILE_PATH_LITERAL("foo/bar"), FILE_PATH_LITERAL("bar") },
62 { FILE_PATH_LITERAL("foo/b:bar"), FILE_PATH_LITERAL("b:bar") },
63 { FILE_PATH_LITERAL(""), FILE_PATH_LITERAL("") },
64 { FILE_PATH_LITERAL("/"), FILE_PATH_LITERAL("/") },
65 { FILE_PATH_LITERAL("foo//////bar"), FILE_PATH_LITERAL("bar") },
66 { FILE_PATH_LITERAL("foo/bar/"), FILE_PATH_LITERAL("bar") },
67 { FILE_PATH_LITERAL("foo/bar/////"), FILE_PATH_LITERAL("bar") },
68 { FILE_PATH_LITERAL("/bar/////"), FILE_PATH_LITERAL("bar") },
69 { FILE_PATH_LITERAL("bar/////"), FILE_PATH_LITERAL("bar") },
70 { FILE_PATH_LITERAL("bar/"), FILE_PATH_LITERAL("bar") },
71 { FILE_PATH_LITERAL("/bar"), FILE_PATH_LITERAL("bar") },
72 { FILE_PATH_LITERAL("////bar"), FILE_PATH_LITERAL("bar") },
73 { FILE_PATH_LITERAL("bar"), FILE_PATH_LITERAL("bar") }
74 };
75 for (size_t i = 0; i < arraysize(test_cases); ++i) {
76 base::FilePath input = base::FilePath(test_cases[i].path);
77 base::FilePath base_name = VirtualPath::BaseName(input);
78 EXPECT_EQ(test_cases[i].base_name, base_name.value());
79 }
80 }
81
82 TEST_F(FileSystemUtilTest, VirtualPathDirName) {
83 struct test_data {
84 const base::FilePath::StringType path;
85 const base::FilePath::StringType dir_name;
86 } test_cases[] = {
87 { FILE_PATH_LITERAL("foo/bar"), FILE_PATH_LITERAL("foo") },
88 { FILE_PATH_LITERAL("foo/b:bar"), FILE_PATH_LITERAL("foo") },
89 { FILE_PATH_LITERAL(""), FILE_PATH_LITERAL(".") },
90 { FILE_PATH_LITERAL("/"), FILE_PATH_LITERAL("/") },
91 { FILE_PATH_LITERAL("foo//////bar"), FILE_PATH_LITERAL("foo") },
92 { FILE_PATH_LITERAL("foo/bar/"), FILE_PATH_LITERAL("foo") },
93 { FILE_PATH_LITERAL("foo/bar/////"), FILE_PATH_LITERAL("foo") },
94 { FILE_PATH_LITERAL("/bar/////"), FILE_PATH_LITERAL("/") },
95 { FILE_PATH_LITERAL("bar/////"), FILE_PATH_LITERAL(".") },
96 { FILE_PATH_LITERAL("bar/"), FILE_PATH_LITERAL(".") },
97 { FILE_PATH_LITERAL("/bar"), FILE_PATH_LITERAL("/") },
98 { FILE_PATH_LITERAL("////bar"), FILE_PATH_LITERAL("/") },
99 { FILE_PATH_LITERAL("bar"), FILE_PATH_LITERAL(".") },
100 { FILE_PATH_LITERAL("c:bar"), FILE_PATH_LITERAL(".") },
101 #ifdef FILE_PATH_USES_WIN_SEPARATORS
102 { FILE_PATH_LITERAL("foo\\bar"), FILE_PATH_LITERAL("foo") },
103 { FILE_PATH_LITERAL("foo\\b:bar"), FILE_PATH_LITERAL("foo") },
104 { FILE_PATH_LITERAL("\\"), FILE_PATH_LITERAL("\\") },
105 { FILE_PATH_LITERAL("foo\\\\\\\\\\\\bar"), FILE_PATH_LITERAL("foo") },
106 { FILE_PATH_LITERAL("foo\\bar\\"), FILE_PATH_LITERAL("foo") },
107 { FILE_PATH_LITERAL("foo\\bar\\\\\\\\\\"), FILE_PATH_LITERAL("foo") },
108 { FILE_PATH_LITERAL("\\bar\\\\\\\\\\"), FILE_PATH_LITERAL("\\") },
109 { FILE_PATH_LITERAL("bar\\\\\\\\\\"), FILE_PATH_LITERAL(".") },
110 { FILE_PATH_LITERAL("bar\\"), FILE_PATH_LITERAL(".") },
111 { FILE_PATH_LITERAL("\\bar"), FILE_PATH_LITERAL("\\") },
112 { FILE_PATH_LITERAL("\\\\\\\\bar"), FILE_PATH_LITERAL("\\") },
113 #endif
114 };
115 for (size_t i = 0; i < arraysize(test_cases); ++i) {
116 base::FilePath input = base::FilePath(test_cases[i].path);
117 base::FilePath dir_name = VirtualPath::DirName(input);
118 EXPECT_EQ(test_cases[i].dir_name, dir_name.value());
119 }
120 }
121
122 TEST_F(FileSystemUtilTest, GetNormalizedFilePath) {
123 struct test_data {
124 const base::FilePath::StringType path;
125 const base::FilePath::StringType normalized_path;
126 } test_cases[] = {
127 { FILE_PATH_LITERAL(""), FILE_PATH_LITERAL("/") },
128 { FILE_PATH_LITERAL("/"), FILE_PATH_LITERAL("/") },
129 { FILE_PATH_LITERAL("foo/bar"), FILE_PATH_LITERAL("/foo/bar") },
130 { FILE_PATH_LITERAL("/foo/bar"), FILE_PATH_LITERAL("/foo/bar") },
131 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
132 { FILE_PATH_LITERAL("\\foo"), FILE_PATH_LITERAL("/foo") },
133 #endif
134 };
135 for (size_t i = 0; i < arraysize(test_cases); ++i) {
136 base::FilePath input = base::FilePath(test_cases[i].path);
137 base::FilePath::StringType normalized_path_string =
138 VirtualPath::GetNormalizedFilePath(input);
139 EXPECT_EQ(test_cases[i].normalized_path, normalized_path_string);
140 }
141 }
142
143 TEST_F(FileSystemUtilTest, IsAbsolutePath) {
144 EXPECT_TRUE(VirtualPath::IsAbsolute(FILE_PATH_LITERAL("/")));
145 EXPECT_TRUE(VirtualPath::IsAbsolute(FILE_PATH_LITERAL("/foo/bar")));
146 EXPECT_FALSE(VirtualPath::IsAbsolute(base::FilePath::StringType()));
147 EXPECT_FALSE(VirtualPath::IsAbsolute(FILE_PATH_LITERAL("foo/bar")));
148 }
149
150 TEST_F(FileSystemUtilTest, IsRootPath) {
151 EXPECT_TRUE(VirtualPath::IsRootPath(base::FilePath(FILE_PATH_LITERAL(""))));
152 EXPECT_TRUE(VirtualPath::IsRootPath(base::FilePath()));
153 EXPECT_TRUE(VirtualPath::IsRootPath(base::FilePath(FILE_PATH_LITERAL("/"))));
154 EXPECT_TRUE(VirtualPath::IsRootPath(base::FilePath(FILE_PATH_LITERAL("//"))));
155 EXPECT_FALSE(VirtualPath::IsRootPath(
156 base::FilePath(FILE_PATH_LITERAL("c:/"))));
157 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
158 EXPECT_TRUE(VirtualPath::IsRootPath(base::FilePath(FILE_PATH_LITERAL("\\"))));
159 EXPECT_FALSE(VirtualPath::IsRootPath(
160 base::FilePath(FILE_PATH_LITERAL("c:\\"))));
161 #endif
162 }
163
164 TEST_F(FileSystemUtilTest, VirtualPathGetComponents) {
165 struct test_data {
166 const base::FilePath::StringType path;
167 size_t count;
168 const base::FilePath::StringType components[2];
169 } test_cases[] = {
170 { FILE_PATH_LITERAL("foo/bar"),
171 2,
172 { FILE_PATH_LITERAL("foo"), FILE_PATH_LITERAL("bar") } },
173 { FILE_PATH_LITERAL("foo"),
174 1,
175 { FILE_PATH_LITERAL("foo"), FILE_PATH_LITERAL("") } },
176 { FILE_PATH_LITERAL("foo////bar"),
177 2,
178 { FILE_PATH_LITERAL("foo"), FILE_PATH_LITERAL("bar") } },
179 { FILE_PATH_LITERAL("foo/c:bar"),
180 2,
181 { FILE_PATH_LITERAL("foo"), FILE_PATH_LITERAL("c:bar") } },
182 { FILE_PATH_LITERAL("c:foo/bar"),
183 2,
184 { FILE_PATH_LITERAL("c:foo"), FILE_PATH_LITERAL("bar") } },
185 { FILE_PATH_LITERAL("foo/bar"),
186 2,
187 { FILE_PATH_LITERAL("foo"), FILE_PATH_LITERAL("bar") } },
188 { FILE_PATH_LITERAL("/foo/bar"),
189 2,
190 { FILE_PATH_LITERAL("foo"), FILE_PATH_LITERAL("bar") } },
191 { FILE_PATH_LITERAL("c:/bar"),
192 2,
193 { FILE_PATH_LITERAL("c:"), FILE_PATH_LITERAL("bar") } },
194 #ifdef FILE_PATH_USES_WIN_SEPARATORS
195 { FILE_PATH_LITERAL("c:\\bar"),
196 2,
197 { FILE_PATH_LITERAL("c:"), FILE_PATH_LITERAL("bar") } },
198 #endif
199 };
200 for (size_t i = 0; i < arraysize(test_cases); ++i) {
201 base::FilePath input = base::FilePath(test_cases[i].path);
202 std::vector<base::FilePath::StringType> components;
203 VirtualPath::GetComponents(input, &components);
204 EXPECT_EQ(test_cases[i].count, components.size());
205 for (size_t j = 0; j < components.size(); ++j)
206 EXPECT_EQ(test_cases[i].components[j], components[j]);
207 }
208 for (size_t i = 0; i < arraysize(test_cases); ++i) {
209 base::FilePath input = base::FilePath(test_cases[i].path);
210 std::vector<std::string> components;
211 VirtualPath::GetComponentsUTF8Unsafe(input, &components);
212 EXPECT_EQ(test_cases[i].count, components.size());
213 for (size_t j = 0; j < components.size(); ++j) {
214 EXPECT_EQ(base::FilePath(test_cases[i].components[j]).AsUTF8Unsafe(),
215 components[j]);
216 }
217 }
218 }
219
220 TEST_F(FileSystemUtilTest, GetIsolatedFileSystemName) {
221 GURL origin_url("http://foo");
222 std::string fsname1 = GetIsolatedFileSystemName(origin_url, "bar");
223 EXPECT_EQ("http_foo_0:Isolated_bar", fsname1);
224 }
225
226 TEST_F(FileSystemUtilTest, CrackIsolatedFileSystemName) {
227 std::string 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);
232 EXPECT_TRUE(CrackIsolatedFileSystemName("foo:Isolated__bar", &fsid));
233 EXPECT_EQ("_bar", fsid);
234 EXPECT_TRUE(CrackIsolatedFileSystemName("foo::Isolated_bar", &fsid));
235 EXPECT_EQ("bar", fsid);
236 }
237
238 TEST_F(FileSystemUtilTest, RejectBadIsolatedFileSystemName) {
239 std::string fsid;
240 EXPECT_FALSE(CrackIsolatedFileSystemName("foobar", &fsid));
241 EXPECT_FALSE(CrackIsolatedFileSystemName("foo:_bar", &fsid));
242 EXPECT_FALSE(CrackIsolatedFileSystemName("foo:Isolatedbar", &fsid));
243 EXPECT_FALSE(CrackIsolatedFileSystemName("fooIsolatedbar", &fsid));
244 EXPECT_FALSE(CrackIsolatedFileSystemName("foo:Persistent", &fsid));
245 EXPECT_FALSE(CrackIsolatedFileSystemName("foo:Temporary", &fsid));
246 EXPECT_FALSE(CrackIsolatedFileSystemName("foo:External", &fsid));
247 EXPECT_FALSE(CrackIsolatedFileSystemName(":Isolated_bar", &fsid));
248 EXPECT_FALSE(CrackIsolatedFileSystemName("foo:Isolated_", &fsid));
249 }
250
251 TEST_F(FileSystemUtilTest, ValidateIsolatedFileSystemId) {
252 EXPECT_TRUE(ValidateIsolatedFileSystemId("ABCDEF0123456789ABCDEF0123456789"));
253 EXPECT_TRUE(ValidateIsolatedFileSystemId("ABCDEFABCDEFABCDEFABCDEFABCDEFAB"));
254 EXPECT_TRUE(ValidateIsolatedFileSystemId("01234567890123456789012345678901"));
255
256 const size_t kExpectedFileSystemIdSize = 32;
257
258 // Should not contain lowercase characters.
259 const std::string kLowercaseId = "abcdef0123456789abcdef0123456789";
260 EXPECT_EQ(kExpectedFileSystemIdSize, kLowercaseId.size());
261 EXPECT_FALSE(ValidateIsolatedFileSystemId(kLowercaseId));
262
263 // Should not be shorter/longer than expected.
264 EXPECT_FALSE(ValidateIsolatedFileSystemId(std::string()));
265
266 const std::string kShorterId = "ABCDEF0123456789ABCDEF";
267 EXPECT_GT(kExpectedFileSystemIdSize, kShorterId.size());
268 EXPECT_FALSE(ValidateIsolatedFileSystemId(kShorterId));
269
270 const std::string kLongerId = "ABCDEF0123456789ABCDEF0123456789ABCDEF";
271 EXPECT_LT(kExpectedFileSystemIdSize, kLongerId.size());
272 EXPECT_FALSE(ValidateIsolatedFileSystemId(kLongerId));
273
274 // Should not contain not alphabetical nor numerical characters.
275 const std::string kSlashId = "ABCD/EFGH/IJKL/MNOP/QRST/UVWX/YZ";
276 EXPECT_EQ(kExpectedFileSystemIdSize, kSlashId.size());
277 EXPECT_FALSE(ValidateIsolatedFileSystemId(kSlashId));
278
279 const std::string kBackslashId = "ABCD\\EFGH\\IJKL\\MNOP\\QRST\\UVWX\\YZ";
280 EXPECT_EQ(kExpectedFileSystemIdSize, kBackslashId.size());
281 EXPECT_FALSE(ValidateIsolatedFileSystemId(kBackslashId));
282
283 const std::string kSpaceId = "ABCD EFGH IJKL MNOP QRST UVWX YZ";
284 EXPECT_EQ(kExpectedFileSystemIdSize, kSpaceId.size());
285 EXPECT_FALSE(ValidateIsolatedFileSystemId(kSpaceId));
286 }
287
288 TEST_F(FileSystemUtilTest, GetIsolatedFileSystemRootURIString) {
289 const GURL kOriginURL("http://foo");
290 // Percents must be escaped, otherwise they will be unintentionally unescaped.
291 const std::string kFileSystemId = "A%20B";
292 const std::string kRootName = "C%20D";
293
294 const std::string url_string =
295 GetIsolatedFileSystemRootURIString(kOriginURL, kFileSystemId, kRootName);
296 EXPECT_EQ("filesystem:http://foo/isolated/A%2520B/C%2520D/", url_string);
297 }
298
299 TEST_F(FileSystemUtilTest, GetExternalFileSystemRootURIString) {
300 const GURL kOriginURL("http://foo");
301 // Percents must be escaped, otherwise they will be unintentionally unescaped.
302 const std::string kMountName = "X%20Y";
303
304 const std::string url_string =
305 GetExternalFileSystemRootURIString(kOriginURL, kMountName);
306 EXPECT_EQ("filesystem:http://foo/external/X%2520Y/", url_string);
307 }
308
309 } // namespace
310 } // namespace content
OLDNEW
« no previous file with comments | « content/common/database_identifier_unittest.cc ('k') | content/test/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698