OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <set> | 5 #include <set> |
6 #include <string> | 6 #include <string> |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 const char* expected_path; | 81 const char* expected_path; |
82 const char* virtual_path; | 82 const char* virtual_path; |
83 } kRootPathFileURITestCases[] = { | 83 } kRootPathFileURITestCases[] = { |
84 { fileapi::kFileSystemTypeTemporary, "file:///", | 84 { fileapi::kFileSystemTypeTemporary, "file:///", |
85 "000" PS "t", NULL }, | 85 "000" PS "t", NULL }, |
86 { fileapi::kFileSystemTypePersistent, "file:///", | 86 { fileapi::kFileSystemTypePersistent, "file:///", |
87 "000" PS "p", NULL }, | 87 "000" PS "p", NULL }, |
88 }; | 88 }; |
89 | 89 |
90 const struct CheckValidPathTest { | 90 const struct CheckValidPathTest { |
91 FilePath::StringType path; | 91 base::FilePath::StringType path; |
92 bool expected_valid; | 92 bool expected_valid; |
93 } kCheckValidPathTestCases[] = { | 93 } kCheckValidPathTestCases[] = { |
94 { FILE_PATH_LITERAL("//tmp/foo.txt"), false, }, | 94 { FILE_PATH_LITERAL("//tmp/foo.txt"), false, }, |
95 { FILE_PATH_LITERAL("//etc/hosts"), false, }, | 95 { FILE_PATH_LITERAL("//etc/hosts"), false, }, |
96 { FILE_PATH_LITERAL("foo.txt"), true, }, | 96 { FILE_PATH_LITERAL("foo.txt"), true, }, |
97 { FILE_PATH_LITERAL("a/b/c"), true, }, | 97 { FILE_PATH_LITERAL("a/b/c"), true, }, |
98 // Any paths that includes parent references are considered invalid. | 98 // Any paths that includes parent references are considered invalid. |
99 { FILE_PATH_LITERAL(".."), false, }, | 99 { FILE_PATH_LITERAL(".."), false, }, |
100 { FILE_PATH_LITERAL("tmp/.."), false, }, | 100 { FILE_PATH_LITERAL("tmp/.."), false, }, |
101 { FILE_PATH_LITERAL("a/b/../c/.."), false, }, | 101 { FILE_PATH_LITERAL("a/b/../c/.."), false, }, |
102 }; | 102 }; |
103 | 103 |
104 const struct IsRestrictedNameTest { | 104 const struct IsRestrictedNameTest { |
105 FilePath::StringType name; | 105 base::FilePath::StringType name; |
106 bool expected_dangerous; | 106 bool expected_dangerous; |
107 } kIsRestrictedNameTestCases[] = { | 107 } kIsRestrictedNameTestCases[] = { |
108 // Names that contain strings that used to be restricted, but are now allowed. | 108 // Names that contain strings that used to be restricted, but are now allowed. |
109 { FILE_PATH_LITERAL("con"), false, }, | 109 { FILE_PATH_LITERAL("con"), false, }, |
110 { FILE_PATH_LITERAL("Con.txt"), false, }, | 110 { FILE_PATH_LITERAL("Con.txt"), false, }, |
111 { FILE_PATH_LITERAL("Prn.png"), false, }, | 111 { FILE_PATH_LITERAL("Prn.png"), false, }, |
112 { FILE_PATH_LITERAL("AUX"), false, }, | 112 { FILE_PATH_LITERAL("AUX"), false, }, |
113 { FILE_PATH_LITERAL("nUl."), false, }, | 113 { FILE_PATH_LITERAL("nUl."), false, }, |
114 { FILE_PATH_LITERAL("coM1"), false, }, | 114 { FILE_PATH_LITERAL("coM1"), false, }, |
115 { FILE_PATH_LITERAL("COM3.com"), false, }, | 115 { FILE_PATH_LITERAL("COM3.com"), false, }, |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
186 { FILE_PATH_LITERAL(". "), false, }, | 186 { FILE_PATH_LITERAL(". "), false, }, |
187 { FILE_PATH_LITERAL(" . "), false, }, | 187 { FILE_PATH_LITERAL(" . "), false, }, |
188 { FILE_PATH_LITERAL(" .."), false, }, | 188 { FILE_PATH_LITERAL(" .."), false, }, |
189 { FILE_PATH_LITERAL(".. "), false, }, | 189 { FILE_PATH_LITERAL(".. "), false, }, |
190 { FILE_PATH_LITERAL(" .. "), false, }, | 190 { FILE_PATH_LITERAL(" .. "), false, }, |
191 { FILE_PATH_LITERAL("b."), false, }, | 191 { FILE_PATH_LITERAL("b."), false, }, |
192 { FILE_PATH_LITERAL(".b"), false, }, | 192 { FILE_PATH_LITERAL(".b"), false, }, |
193 }; | 193 }; |
194 | 194 |
195 // For External filesystem. | 195 // For External filesystem. |
196 const FilePath::CharType kMountPoint[] = FILE_PATH_LITERAL("/tmp/testing"); | 196 const base::FilePath::CharType kMountPoint[] = FILE_PATH_LITERAL("/tmp/testing")
; |
197 const FilePath::CharType kRootPath[] = FILE_PATH_LITERAL("/tmp"); | 197 const base::FilePath::CharType kRootPath[] = FILE_PATH_LITERAL("/tmp"); |
198 const FilePath::CharType kVirtualPath[] = FILE_PATH_LITERAL("testing"); | 198 const base::FilePath::CharType kVirtualPath[] = FILE_PATH_LITERAL("testing"); |
199 | 199 |
200 } // namespace | 200 } // namespace |
201 | 201 |
202 class FileSystemMountPointProviderTest : public testing::Test { | 202 class FileSystemMountPointProviderTest : public testing::Test { |
203 public: | 203 public: |
204 FileSystemMountPointProviderTest() | 204 FileSystemMountPointProviderTest() |
205 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | 205 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
206 } | 206 } |
207 | 207 |
208 void SetUp() { | 208 void SetUp() { |
209 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); | 209 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); |
210 special_storage_policy_ = new quota::MockSpecialStoragePolicy; | 210 special_storage_policy_ = new quota::MockSpecialStoragePolicy; |
211 } | 211 } |
212 | 212 |
213 protected: | 213 protected: |
214 void SetupNewContext(const FileSystemOptions& options) { | 214 void SetupNewContext(const FileSystemOptions& options) { |
215 scoped_refptr<ExternalMountPoints> external_mount_points( | 215 scoped_refptr<ExternalMountPoints> external_mount_points( |
216 ExternalMountPoints::CreateRefCounted()); | 216 ExternalMountPoints::CreateRefCounted()); |
217 file_system_context_ = new FileSystemContext( | 217 file_system_context_ = new FileSystemContext( |
218 FileSystemTaskRunners::CreateMockTaskRunners(), | 218 FileSystemTaskRunners::CreateMockTaskRunners(), |
219 external_mount_points.get(), | 219 external_mount_points.get(), |
220 special_storage_policy_, | 220 special_storage_policy_, |
221 NULL, | 221 NULL, |
222 data_dir_.path(), | 222 data_dir_.path(), |
223 options); | 223 options); |
224 #if defined(OS_CHROMEOS) | 224 #if defined(OS_CHROMEOS) |
225 FilePath mount_point_path = FilePath(kMountPoint); | 225 base::FilePath mount_point_path = base::FilePath(kMountPoint); |
226 external_mount_points->RegisterFileSystem( | 226 external_mount_points->RegisterFileSystem( |
227 mount_point_path.BaseName().AsUTF8Unsafe(), | 227 mount_point_path.BaseName().AsUTF8Unsafe(), |
228 kFileSystemTypeNativeLocal, | 228 kFileSystemTypeNativeLocal, |
229 mount_point_path); | 229 mount_point_path); |
230 #endif | 230 #endif |
231 } | 231 } |
232 | 232 |
233 FileSystemMountPointProvider* provider(FileSystemType type) { | 233 FileSystemMountPointProvider* provider(FileSystemType type) { |
234 DCHECK(file_system_context_); | 234 DCHECK(file_system_context_); |
235 return file_system_context_->GetMountPointProvider(type); | 235 return file_system_context_->GetMountPointProvider(type); |
236 } | 236 } |
237 | 237 |
238 bool GetRootPath(const GURL& origin_url, | 238 bool GetRootPath(const GURL& origin_url, |
239 fileapi::FileSystemType type, | 239 fileapi::FileSystemType type, |
240 bool create, | 240 bool create, |
241 FilePath* root_path) { | 241 base::FilePath* root_path) { |
242 FilePath virtual_path = FilePath(); | 242 base::FilePath virtual_path = base::FilePath(); |
243 if (type == kFileSystemTypeExternal) | 243 if (type == kFileSystemTypeExternal) |
244 virtual_path = FilePath(kVirtualPath); | 244 virtual_path = base::FilePath(kVirtualPath); |
245 FileSystemURL url = file_system_context_->CreateCrackedFileSystemURL( | 245 FileSystemURL url = file_system_context_->CreateCrackedFileSystemURL( |
246 origin_url, type, virtual_path); | 246 origin_url, type, virtual_path); |
247 FilePath returned_root_path = | 247 base::FilePath returned_root_path = |
248 provider(type)->GetFileSystemRootPathOnFileThread(url, create); | 248 provider(type)->GetFileSystemRootPathOnFileThread(url, create); |
249 if (root_path) | 249 if (root_path) |
250 *root_path = returned_root_path; | 250 *root_path = returned_root_path; |
251 return !returned_root_path.empty(); | 251 return !returned_root_path.empty(); |
252 } | 252 } |
253 | 253 |
254 FilePath data_path() const { return data_dir_.path(); } | 254 base::FilePath data_path() const { return data_dir_.path(); } |
255 FilePath file_system_path() const { | 255 base::FilePath file_system_path() const { |
256 return data_dir_.path().Append( | 256 return data_dir_.path().Append( |
257 SandboxMountPointProvider::kFileSystemDirectory); | 257 SandboxMountPointProvider::kFileSystemDirectory); |
258 } | 258 } |
259 FileSystemContext* file_system_context() const { | 259 FileSystemContext* file_system_context() const { |
260 return file_system_context_.get(); | 260 return file_system_context_.get(); |
261 } | 261 } |
262 | 262 |
263 private: | 263 private: |
264 base::ScopedTempDir data_dir_; | 264 base::ScopedTempDir data_dir_; |
265 MessageLoop message_loop_; | 265 MessageLoop message_loop_; |
266 base::WeakPtrFactory<FileSystemMountPointProviderTest> weak_factory_; | 266 base::WeakPtrFactory<FileSystemMountPointProviderTest> weak_factory_; |
267 | 267 |
268 scoped_refptr<quota::SpecialStoragePolicy> special_storage_policy_; | 268 scoped_refptr<quota::SpecialStoragePolicy> special_storage_policy_; |
269 scoped_refptr<FileSystemContext> file_system_context_; | 269 scoped_refptr<FileSystemContext> file_system_context_; |
270 | 270 |
271 DISALLOW_COPY_AND_ASSIGN(FileSystemMountPointProviderTest); | 271 DISALLOW_COPY_AND_ASSIGN(FileSystemMountPointProviderTest); |
272 }; | 272 }; |
273 | 273 |
274 TEST_F(FileSystemMountPointProviderTest, GetRootPathCreateAndExamine) { | 274 TEST_F(FileSystemMountPointProviderTest, GetRootPathCreateAndExamine) { |
275 std::vector<FilePath> returned_root_path( | 275 std::vector<base::FilePath> returned_root_path( |
276 ARRAYSIZE_UNSAFE(kRootPathTestCases)); | 276 ARRAYSIZE_UNSAFE(kRootPathTestCases)); |
277 SetupNewContext(CreateAllowFileAccessOptions()); | 277 SetupNewContext(CreateAllowFileAccessOptions()); |
278 | 278 |
279 // Create a new root directory. | 279 // Create a new root directory. |
280 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathTestCases); ++i) { | 280 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathTestCases); ++i) { |
281 SCOPED_TRACE(testing::Message() << "RootPath (create) #" << i << " " | 281 SCOPED_TRACE(testing::Message() << "RootPath (create) #" << i << " " |
282 << kRootPathTestCases[i].expected_path); | 282 << kRootPathTestCases[i].expected_path); |
283 | 283 |
284 FilePath root_path; | 284 base::FilePath root_path; |
285 EXPECT_TRUE(GetRootPath(GURL(kRootPathTestCases[i].origin_url), | 285 EXPECT_TRUE(GetRootPath(GURL(kRootPathTestCases[i].origin_url), |
286 kRootPathTestCases[i].type, | 286 kRootPathTestCases[i].type, |
287 true /* create */, &root_path)); | 287 true /* create */, &root_path)); |
288 | 288 |
289 if (kRootPathTestCases[i].type != kFileSystemTypeExternal) { | 289 if (kRootPathTestCases[i].type != kFileSystemTypeExternal) { |
290 FilePath expected = file_system_path().AppendASCII( | 290 base::FilePath expected = file_system_path().AppendASCII( |
291 kRootPathTestCases[i].expected_path); | 291 kRootPathTestCases[i].expected_path); |
292 EXPECT_EQ(expected.value(), root_path.value()); | 292 EXPECT_EQ(expected.value(), root_path.value()); |
293 EXPECT_TRUE(file_util::DirectoryExists(root_path)); | 293 EXPECT_TRUE(file_util::DirectoryExists(root_path)); |
294 } else { | 294 } else { |
295 // External file system root path is virtual one and does not match | 295 // External file system root path is virtual one and does not match |
296 // anything from the actual file system. | 296 // anything from the actual file system. |
297 EXPECT_EQ(kRootPath, root_path.value()); | 297 EXPECT_EQ(kRootPath, root_path.value()); |
298 } | 298 } |
299 ASSERT_TRUE(returned_root_path.size() > i); | 299 ASSERT_TRUE(returned_root_path.size() > i); |
300 returned_root_path[i] = root_path; | 300 returned_root_path[i] = root_path; |
301 } | 301 } |
302 | 302 |
303 // Get the root directory with create=false and see if we get the | 303 // Get the root directory with create=false and see if we get the |
304 // same directory. | 304 // same directory. |
305 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathTestCases); ++i) { | 305 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathTestCases); ++i) { |
306 SCOPED_TRACE(testing::Message() << "RootPath (get) #" << i << " " | 306 SCOPED_TRACE(testing::Message() << "RootPath (get) #" << i << " " |
307 << kRootPathTestCases[i].expected_path); | 307 << kRootPathTestCases[i].expected_path); |
308 | 308 |
309 FilePath root_path; | 309 base::FilePath root_path; |
310 EXPECT_TRUE(GetRootPath(GURL(kRootPathTestCases[i].origin_url), | 310 EXPECT_TRUE(GetRootPath(GURL(kRootPathTestCases[i].origin_url), |
311 kRootPathTestCases[i].type, | 311 kRootPathTestCases[i].type, |
312 false /* create */, &root_path)); | 312 false /* create */, &root_path)); |
313 ASSERT_TRUE(returned_root_path.size() > i); | 313 ASSERT_TRUE(returned_root_path.size() > i); |
314 EXPECT_EQ(returned_root_path[i].value(), root_path.value()); | 314 EXPECT_EQ(returned_root_path[i].value(), root_path.value()); |
315 } | 315 } |
316 } | 316 } |
317 | 317 |
318 TEST_F(FileSystemMountPointProviderTest, | 318 TEST_F(FileSystemMountPointProviderTest, |
319 GetRootPathCreateAndExamineWithNewProvider) { | 319 GetRootPathCreateAndExamineWithNewProvider) { |
320 std::vector<FilePath> returned_root_path( | 320 std::vector<base::FilePath> returned_root_path( |
321 ARRAYSIZE_UNSAFE(kRootPathTestCases)); | 321 ARRAYSIZE_UNSAFE(kRootPathTestCases)); |
322 SetupNewContext(CreateAllowFileAccessOptions()); | 322 SetupNewContext(CreateAllowFileAccessOptions()); |
323 | 323 |
324 GURL origin_url("http://foo.com:1/"); | 324 GURL origin_url("http://foo.com:1/"); |
325 | 325 |
326 FilePath root_path1; | 326 base::FilePath root_path1; |
327 EXPECT_TRUE(GetRootPath(origin_url, | 327 EXPECT_TRUE(GetRootPath(origin_url, |
328 kFileSystemTypeTemporary, true, &root_path1)); | 328 kFileSystemTypeTemporary, true, &root_path1)); |
329 | 329 |
330 SetupNewContext(CreateDisallowFileAccessOptions()); | 330 SetupNewContext(CreateDisallowFileAccessOptions()); |
331 FilePath root_path2; | 331 base::FilePath root_path2; |
332 EXPECT_TRUE(GetRootPath(origin_url, | 332 EXPECT_TRUE(GetRootPath(origin_url, |
333 kFileSystemTypeTemporary, false, &root_path2)); | 333 kFileSystemTypeTemporary, false, &root_path2)); |
334 | 334 |
335 EXPECT_EQ(root_path1.value(), root_path2.value()); | 335 EXPECT_EQ(root_path1.value(), root_path2.value()); |
336 } | 336 } |
337 | 337 |
338 TEST_F(FileSystemMountPointProviderTest, GetRootPathGetWithoutCreate) { | 338 TEST_F(FileSystemMountPointProviderTest, GetRootPathGetWithoutCreate) { |
339 SetupNewContext(CreateDisallowFileAccessOptions()); | 339 SetupNewContext(CreateDisallowFileAccessOptions()); |
340 | 340 |
341 // Try to get a root directory without creating. | 341 // Try to get a root directory without creating. |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
376 kRootPathFileURITestCases[i].type, | 376 kRootPathFileURITestCases[i].type, |
377 true /* create */, NULL)); | 377 true /* create */, NULL)); |
378 } | 378 } |
379 } | 379 } |
380 | 380 |
381 TEST_F(FileSystemMountPointProviderTest, GetRootPathFileURIWithAllowFlag) { | 381 TEST_F(FileSystemMountPointProviderTest, GetRootPathFileURIWithAllowFlag) { |
382 SetupNewContext(CreateAllowFileAccessOptions()); | 382 SetupNewContext(CreateAllowFileAccessOptions()); |
383 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathFileURITestCases); ++i) { | 383 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathFileURITestCases); ++i) { |
384 SCOPED_TRACE(testing::Message() << "RootPathFileURI (allow) #" | 384 SCOPED_TRACE(testing::Message() << "RootPathFileURI (allow) #" |
385 << i << " " << kRootPathFileURITestCases[i].expected_path); | 385 << i << " " << kRootPathFileURITestCases[i].expected_path); |
386 FilePath root_path; | 386 base::FilePath root_path; |
387 EXPECT_TRUE(GetRootPath(GURL(kRootPathFileURITestCases[i].origin_url), | 387 EXPECT_TRUE(GetRootPath(GURL(kRootPathFileURITestCases[i].origin_url), |
388 kRootPathFileURITestCases[i].type, | 388 kRootPathFileURITestCases[i].type, |
389 true /* create */, &root_path)); | 389 true /* create */, &root_path)); |
390 FilePath expected = file_system_path().AppendASCII( | 390 base::FilePath expected = file_system_path().AppendASCII( |
391 kRootPathFileURITestCases[i].expected_path); | 391 kRootPathFileURITestCases[i].expected_path); |
392 EXPECT_EQ(expected.value(), root_path.value()); | 392 EXPECT_EQ(expected.value(), root_path.value()); |
393 EXPECT_TRUE(file_util::DirectoryExists(root_path)); | 393 EXPECT_TRUE(file_util::DirectoryExists(root_path)); |
394 } | 394 } |
395 } | 395 } |
396 | 396 |
397 TEST_F(FileSystemMountPointProviderTest, IsRestrictedName) { | 397 TEST_F(FileSystemMountPointProviderTest, IsRestrictedName) { |
398 SetupNewContext(CreateDisallowFileAccessOptions()); | 398 SetupNewContext(CreateDisallowFileAccessOptions()); |
399 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kIsRestrictedNameTestCases); ++i) { | 399 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kIsRestrictedNameTestCases); ++i) { |
400 SCOPED_TRACE(testing::Message() << "IsRestrictedName #" << i << " " | 400 SCOPED_TRACE(testing::Message() << "IsRestrictedName #" << i << " " |
401 << kIsRestrictedNameTestCases[i].name); | 401 << kIsRestrictedNameTestCases[i].name); |
402 FilePath name(kIsRestrictedNameTestCases[i].name); | 402 base::FilePath name(kIsRestrictedNameTestCases[i].name); |
403 EXPECT_EQ(kIsRestrictedNameTestCases[i].expected_dangerous, | 403 EXPECT_EQ(kIsRestrictedNameTestCases[i].expected_dangerous, |
404 provider(kFileSystemTypeTemporary)->IsRestrictedFileName(name)); | 404 provider(kFileSystemTypeTemporary)->IsRestrictedFileName(name)); |
405 } | 405 } |
406 } | 406 } |
407 | 407 |
408 } // namespace fileapi | 408 } // namespace fileapi |
OLD | NEW |