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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
|
// Copyright (c) 2011 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 "webkit/fileapi/file_system_path_manager.h"
#include <set>
#include <string>
#include "base/basictypes.h"
#include "base/file_util.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_callback_factory.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/scoped_temp_dir.h"
#include "base/message_loop.h"
#include "base/message_loop_proxy.h"
#include "base/sys_string_conversions.h"
#include "base/utf_string_conversions.h"
#include "googleurl/src/gurl.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "webkit/fileapi/file_system_util.h"
#include "webkit/fileapi/sandbox_mount_point_provider.h"
#include "webkit/quota/special_storage_policy.h"
using namespace fileapi;
namespace {
// PS stands for path separator.
#if defined(FILE_PATH_USES_WIN_SEPARATORS)
#define PS "\\"
#else
#define PS "/"
#endif
struct RootPathTestCase {
fileapi::FileSystemType type;
const char* origin_url;
const char* expected_path;
};
const struct RootPathTest {
fileapi::FileSystemType type;
const char* origin_url;
const char* expected_path;
} kRootPathTestCases[] = {
{ fileapi::kFileSystemTypeTemporary, "http://foo:1/",
"http_foo_1" PS "Temporary" },
{ fileapi::kFileSystemTypePersistent, "http://foo:1/",
"http_foo_1" PS "Persistent" },
{ fileapi::kFileSystemTypeTemporary, "http://bar.com/",
"http_bar.com_0" PS "Temporary" },
{ fileapi::kFileSystemTypePersistent, "http://bar.com/",
"http_bar.com_0" PS "Persistent" },
{ fileapi::kFileSystemTypeTemporary, "https://foo:2/",
"https_foo_2" PS "Temporary" },
{ fileapi::kFileSystemTypePersistent, "https://foo:2/",
"https_foo_2" PS "Persistent" },
{ fileapi::kFileSystemTypeTemporary, "https://bar.com/",
"https_bar.com_0" PS "Temporary" },
{ fileapi::kFileSystemTypePersistent, "https://bar.com/",
"https_bar.com_0" PS "Persistent" },
#if defined(OS_CHROMEOS)
{ fileapi::kFileSystemTypeExternal, "chrome-extension://foo/",
"chrome-extension__0" PS "External" },
#endif
};
const struct RootPathFileURITest {
fileapi::FileSystemType type;
const char* origin_url;
const char* expected_path;
const char* virtual_path;
} kRootPathFileURITestCases[] = {
{ fileapi::kFileSystemTypeTemporary, "file:///",
"file__0" PS "Temporary", NULL },
{ fileapi::kFileSystemTypePersistent, "file:///",
"file__0" PS "Persistent", NULL },
#if defined(OS_CHROMEOS)
{ fileapi::kFileSystemTypeExternal, "chrome-extension://foo/",
"chrome-extension__0" PS "External", "testing" },
#endif
};
const struct CheckValidPathTest {
FilePath::StringType path;
bool expected_valid;
} kCheckValidPathTestCases[] = {
{ FILE_PATH_LITERAL("//tmp/foo.txt"), false, },
{ FILE_PATH_LITERAL("//etc/hosts"), false, },
{ FILE_PATH_LITERAL("foo.txt"), true, },
{ FILE_PATH_LITERAL("a/b/c"), true, },
// Any paths that includes parent references are considered invalid.
{ FILE_PATH_LITERAL(".."), false, },
{ FILE_PATH_LITERAL("tmp/.."), false, },
{ FILE_PATH_LITERAL("a/b/../c/.."), false, },
};
const char* const kPathToVirtualPathTestCases[] = {
"",
"a",
"a" PS "b",
"a" PS "b" PS "c",
};
const struct IsRestrictedNameTest {
FilePath::StringType name;
bool expected_dangerous;
} kIsRestrictedNameTestCases[] = {
// Name that has restricted names in it.
{ FILE_PATH_LITERAL("con"), true, },
{ FILE_PATH_LITERAL("Con.txt"), true, },
{ FILE_PATH_LITERAL("Prn.png"), true, },
{ FILE_PATH_LITERAL("AUX"), true, },
{ FILE_PATH_LITERAL("nUl."), true, },
{ FILE_PATH_LITERAL("coM1"), true, },
{ FILE_PATH_LITERAL("COM3.com"), true, },
{ FILE_PATH_LITERAL("cOM7"), true, },
{ FILE_PATH_LITERAL("com9"), true, },
{ FILE_PATH_LITERAL("lpT1"), true, },
{ FILE_PATH_LITERAL("LPT4.com"), true, },
{ FILE_PATH_LITERAL("lPT8"), true, },
{ FILE_PATH_LITERAL("lPT9"), true, },
// Similar but safe cases.
{ FILE_PATH_LITERAL("con3"), false, },
{ FILE_PATH_LITERAL("PrnImage.png"), false, },
{ FILE_PATH_LITERAL("AUXX"), false, },
{ FILE_PATH_LITERAL("NULL"), false, },
{ FILE_PATH_LITERAL("coM0"), false, },
{ FILE_PATH_LITERAL("COM.com"), false, },
{ FILE_PATH_LITERAL("lpT0"), false, },
{ FILE_PATH_LITERAL("LPT.com"), false, },
// Ends with period or whitespace.
{ FILE_PATH_LITERAL("b "), true, },
{ FILE_PATH_LITERAL("b\t"), true, },
{ FILE_PATH_LITERAL("b\n"), true, },
{ FILE_PATH_LITERAL("b\r\n"), true, },
{ FILE_PATH_LITERAL("b."), true, },
{ FILE_PATH_LITERAL("b.."), true, },
// Similar but safe cases.
{ FILE_PATH_LITERAL("b c"), false, },
{ FILE_PATH_LITERAL("b\tc"), false, },
{ FILE_PATH_LITERAL("b\nc"), false, },
{ FILE_PATH_LITERAL("b\r\nc"), false, },
{ FILE_PATH_LITERAL("b c d e f"), false, },
{ FILE_PATH_LITERAL("b.c"), false, },
{ FILE_PATH_LITERAL("b..c"), false, },
// Name that has restricted chars in it.
{ FILE_PATH_LITERAL("a\\b"), true, },
{ FILE_PATH_LITERAL("a/b"), true, },
{ FILE_PATH_LITERAL("a<b"), true, },
{ FILE_PATH_LITERAL("a>b"), true, },
{ FILE_PATH_LITERAL("a:b"), true, },
{ FILE_PATH_LITERAL("a?b"), true, },
{ FILE_PATH_LITERAL("a|b"), true, },
{ FILE_PATH_LITERAL("ab\\"), true, },
{ FILE_PATH_LITERAL("ab/.txt"), true, },
{ FILE_PATH_LITERAL("ab<.txt"), true, },
{ FILE_PATH_LITERAL("ab>.txt"), true, },
{ FILE_PATH_LITERAL("ab:.txt"), true, },
{ FILE_PATH_LITERAL("ab?.txt"), true, },
{ FILE_PATH_LITERAL("ab|.txt"), true, },
{ FILE_PATH_LITERAL("\\ab"), true, },
{ FILE_PATH_LITERAL("/ab"), true, },
{ FILE_PATH_LITERAL("<ab"), true, },
{ FILE_PATH_LITERAL(">ab"), true, },
{ FILE_PATH_LITERAL(":ab"), true, },
{ FILE_PATH_LITERAL("?ab"), true, },
{ FILE_PATH_LITERAL("|ab"), true, },
};
FilePath UTF8ToFilePath(const std::string& str) {
FilePath::StringType result;
#if defined(OS_POSIX)
result = base::SysWideToNativeMB(UTF8ToWide(str));
#elif defined(OS_WIN)
result = UTF8ToUTF16(str);
#endif
return FilePath(result);
}
class TestSpecialStoragePolicy : public quota::SpecialStoragePolicy {
public:
virtual bool IsStorageProtected(const GURL& origin) {
return false;
}
virtual bool IsStorageUnlimited(const GURL& origin) {
return true;
}
virtual bool IsFileHandler(const std::string& extension_id) {
return true;
}
};
} // namespace
class FileSystemPathManagerTest : public testing::Test {
public:
FileSystemPathManagerTest()
: callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
}
void SetUp() {
ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
root_path_callback_status_ = false;
root_path_.clear();
file_system_name_.clear();
}
protected:
FileSystemPathManager* NewPathManager(
bool incognito,
bool allow_file_access) {
FileSystemPathManager* manager = new FileSystemPathManager(
base::MessageLoopProxy::CreateForCurrentThread(),
data_dir_.path(),
scoped_refptr<quota::SpecialStoragePolicy>(
new TestSpecialStoragePolicy()),
incognito,
allow_file_access);
#if defined(OS_CHROMEOS)
fileapi::ExternalFileSystemMountPointProvider* ext_provider =
manager->external_provider();
ext_provider->AddMountPoint(FilePath("/tmp/testing"));
#endif
return manager;
}
void OnGetRootPath(bool success,
const FilePath& root_path,
const std::string& name) {
root_path_callback_status_ = success;
root_path_ = root_path;
file_system_name_ = name;
}
bool GetRootPath(FileSystemPathManager* manager,
const GURL& origin_url,
fileapi::FileSystemType type,
bool create,
FilePath* root_path) {
manager->ValidateFileSystemRootAndGetURL(origin_url, type, create,
callback_factory_.NewCallback(
&FileSystemPathManagerTest::OnGetRootPath));
MessageLoop::current()->RunAllPending();
if (root_path)
*root_path = root_path_;
return root_path_callback_status_;
}
FilePath data_path() { return data_dir_.path(); }
FilePath file_system_path() {
return data_dir_.path().Append(
SandboxMountPointProvider::kFileSystemDirectory);
}
FilePath external_file_system_path() {
return UTF8ToFilePath(std::string(fileapi::kExternalDir));
}
FilePath external_file_path_root() {
return UTF8ToFilePath(std::string("/tmp"));
}
private:
ScopedTempDir data_dir_;
base::ScopedCallbackFactory<FileSystemPathManagerTest> callback_factory_;
bool root_path_callback_status_;
FilePath root_path_;
std::string file_system_name_;
DISALLOW_COPY_AND_ASSIGN(FileSystemPathManagerTest);
};
TEST_F(FileSystemPathManagerTest, GetRootPathCreateAndExamine) {
std::vector<FilePath> returned_root_path(
ARRAYSIZE_UNSAFE(kRootPathTestCases));
scoped_ptr<FileSystemPathManager> manager(NewPathManager(false, false));
// Create a new root directory.
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathTestCases); ++i) {
SCOPED_TRACE(testing::Message() << "RootPath (create) #" << i << " "
<< kRootPathTestCases[i].expected_path);
FilePath root_path;
EXPECT_TRUE(GetRootPath(manager.get(),
GURL(kRootPathTestCases[i].origin_url),
kRootPathTestCases[i].type,
true /* create */, &root_path));
if (kRootPathTestCases[i].type != fileapi::kFileSystemTypeExternal) {
FilePath expected = file_system_path().AppendASCII(
kRootPathTestCases[i].expected_path);
EXPECT_EQ(expected.value(), root_path.DirName().value());
EXPECT_TRUE(file_util::DirectoryExists(root_path));
} else {
// External file system root path is virtual one and does not match
// anything from the actual file system.
EXPECT_EQ(external_file_system_path().value(),
root_path.value());
}
ASSERT_TRUE(returned_root_path.size() > i);
returned_root_path[i] = root_path;
}
// Get the root directory with create=false and see if we get the
// same directory.
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathTestCases); ++i) {
SCOPED_TRACE(testing::Message() << "RootPath (get) #" << i << " "
<< kRootPathTestCases[i].expected_path);
FilePath root_path;
EXPECT_TRUE(GetRootPath(manager.get(),
GURL(kRootPathTestCases[i].origin_url),
kRootPathTestCases[i].type,
false /* create */, &root_path));
ASSERT_TRUE(returned_root_path.size() > i);
EXPECT_EQ(returned_root_path[i].value(), root_path.value());
}
}
TEST_F(FileSystemPathManagerTest, GetRootPathCreateAndExamineWithNewManager) {
std::vector<FilePath> returned_root_path(
ARRAYSIZE_UNSAFE(kRootPathTestCases));
scoped_ptr<FileSystemPathManager> manager1(NewPathManager(false, false));
scoped_ptr<FileSystemPathManager> manager2(NewPathManager(false, false));
GURL origin_url("http://foo.com:1/");
FilePath root_path1;
EXPECT_TRUE(GetRootPath(manager1.get(), origin_url,
kFileSystemTypeTemporary, true, &root_path1));
FilePath root_path2;
EXPECT_TRUE(GetRootPath(manager2.get(), origin_url,
kFileSystemTypeTemporary, false, &root_path2));
EXPECT_EQ(root_path1.value(), root_path2.value());
}
TEST_F(FileSystemPathManagerTest, GetRootPathGetWithoutCreate) {
scoped_ptr<FileSystemPathManager> manager(NewPathManager(false, false));
// Try to get a root directory without creating.
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathTestCases); ++i) {
SCOPED_TRACE(testing::Message() << "RootPath (create=false) #" << i << " "
<< kRootPathTestCases[i].expected_path);
EXPECT_FALSE(GetRootPath(manager.get(),
GURL(kRootPathTestCases[i].origin_url),
kRootPathTestCases[i].type,
false /* create */, NULL));
}
}
TEST_F(FileSystemPathManagerTest, GetRootPathInIncognito) {
scoped_ptr<FileSystemPathManager> manager(NewPathManager(
true /* incognito */, false));
// Try to get a root directory.
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathTestCases); ++i) {
SCOPED_TRACE(testing::Message() << "RootPath (incognito) #" << i << " "
<< kRootPathTestCases[i].expected_path);
EXPECT_FALSE(GetRootPath(manager.get(),
GURL(kRootPathTestCases[i].origin_url),
kRootPathTestCases[i].type,
true /* create */, NULL));
}
}
TEST_F(FileSystemPathManagerTest, GetRootPathFileURI) {
scoped_ptr<FileSystemPathManager> manager(NewPathManager(false, false));
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathFileURITestCases); ++i) {
SCOPED_TRACE(testing::Message() << "RootPathFileURI (disallow) #"
<< i << " " << kRootPathFileURITestCases[i].expected_path);
EXPECT_FALSE(GetRootPath(manager.get(),
GURL(kRootPathFileURITestCases[i].origin_url),
kRootPathFileURITestCases[i].type,
true /* create */, NULL));
}
}
TEST_F(FileSystemPathManagerTest, GetRootPathFileURIWithAllowFlag) {
scoped_ptr<FileSystemPathManager> manager(NewPathManager(
false, true /* allow_file_access_from_files */));
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathFileURITestCases); ++i) {
SCOPED_TRACE(testing::Message() << "RootPathFileURI (allow) #"
<< i << " " << kRootPathFileURITestCases[i].expected_path);
FilePath root_path;
EXPECT_TRUE(GetRootPath(manager.get(),
GURL(kRootPathFileURITestCases[i].origin_url),
kRootPathFileURITestCases[i].type,
true /* create */, &root_path));
if (kRootPathFileURITestCases[i].type != fileapi::kFileSystemTypeExternal) {
FilePath expected = file_system_path().AppendASCII(
kRootPathFileURITestCases[i].expected_path);
EXPECT_EQ(expected.value(), root_path.DirName().value());
EXPECT_TRUE(file_util::DirectoryExists(root_path));
} else {
EXPECT_EQ(external_file_path_root().value(), root_path.value());
}
}
}
TEST_F(FileSystemPathManagerTest, IsRestrictedName) {
scoped_ptr<FileSystemPathManager> manager(NewPathManager(false, false));
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kIsRestrictedNameTestCases); ++i) {
SCOPED_TRACE(testing::Message() << "IsRestrictedName #" << i << " "
<< kIsRestrictedNameTestCases[i].name);
FilePath name(kIsRestrictedNameTestCases[i].name);
EXPECT_EQ(kIsRestrictedNameTestCases[i].expected_dangerous,
manager->IsRestrictedFileName(kFileSystemTypeTemporary, name));
}
}
|