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 "webkit/browser/fileapi/sandbox_mount_point_provider.h" | 5 #include "webkit/browser/fileapi/sandbox_mount_point_provider.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 { fileapi::kFileSystemTypePersistent, "file:///", | 64 { fileapi::kFileSystemTypePersistent, "file:///", |
65 "000" PS "p", NULL }, | 65 "000" PS "p", NULL }, |
66 }; | 66 }; |
67 | 67 |
68 FileSystemURL CreateFileSystemURL(const char* path) { | 68 FileSystemURL CreateFileSystemURL(const char* path) { |
69 const GURL kOrigin("http://foo/"); | 69 const GURL kOrigin("http://foo/"); |
70 return FileSystemURL::CreateForTest( | 70 return FileSystemURL::CreateForTest( |
71 kOrigin, kFileSystemTypeTemporary, base::FilePath::FromUTF8Unsafe(path)); | 71 kOrigin, kFileSystemTypeTemporary, base::FilePath::FromUTF8Unsafe(path)); |
72 } | 72 } |
73 | 73 |
74 void DidValidateFileSystemRoot(base::PlatformFileError* error_out, | 74 void DidOpenFileSystem(base::PlatformFileError* error_out, |
75 base::PlatformFileError error) { | 75 base::PlatformFileError error) { |
76 *error_out = error; | 76 *error_out = error; |
77 } | 77 } |
78 | 78 |
79 } // namespace | 79 } // namespace |
80 | 80 |
81 class SandboxMountPointProviderTest : public testing::Test { | 81 class SandboxMountPointProviderTest : public testing::Test { |
82 protected: | 82 protected: |
83 virtual void SetUp() { | 83 virtual void SetUp() { |
84 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); | 84 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); |
85 } | 85 } |
(...skipping 15 matching lines...) Expand all Loading... |
101 void CreateOriginTypeDirectory(const GURL& origin, | 101 void CreateOriginTypeDirectory(const GURL& origin, |
102 fileapi::FileSystemType type) { | 102 fileapi::FileSystemType type) { |
103 base::FilePath target = provider_-> | 103 base::FilePath target = provider_-> |
104 GetBaseDirectoryForOriginAndType(origin, type, true); | 104 GetBaseDirectoryForOriginAndType(origin, type, true); |
105 ASSERT_TRUE(!target.empty()); | 105 ASSERT_TRUE(!target.empty()); |
106 ASSERT_TRUE(file_util::DirectoryExists(target)); | 106 ASSERT_TRUE(file_util::DirectoryExists(target)); |
107 } | 107 } |
108 | 108 |
109 bool GetRootPath(const GURL& origin_url, | 109 bool GetRootPath(const GURL& origin_url, |
110 fileapi::FileSystemType type, | 110 fileapi::FileSystemType type, |
111 bool create, | 111 OpenFileSystemMode mode, |
112 base::FilePath* root_path) { | 112 base::FilePath* root_path) { |
113 base::PlatformFileError* error = new base::PlatformFileError( | 113 base::PlatformFileError* error = new base::PlatformFileError( |
114 base::PLATFORM_FILE_OK); | 114 base::PLATFORM_FILE_OK); |
115 provider_->ValidateFileSystemRoot( | 115 provider_->OpenFileSystem( |
116 origin_url, type, create, | 116 origin_url, type, mode, |
117 base::Bind(&DidValidateFileSystemRoot, error)); | 117 base::Bind(&DidOpenFileSystem, error)); |
118 base::MessageLoop::current()->RunUntilIdle(); | 118 base::MessageLoop::current()->RunUntilIdle(); |
119 if (*error != base::PLATFORM_FILE_OK) | 119 if (*error != base::PLATFORM_FILE_OK) |
120 return false; | 120 return false; |
121 base::FilePath returned_root_path = | 121 base::FilePath returned_root_path = |
122 provider_->GetBaseDirectoryForOriginAndType( | 122 provider_->GetBaseDirectoryForOriginAndType( |
123 origin_url, type, false /* create */); | 123 origin_url, type, false /* create */); |
124 if (root_path) | 124 if (root_path) |
125 *root_path = returned_root_path; | 125 *root_path = returned_root_path; |
126 return !returned_root_path.empty(); | 126 return !returned_root_path.empty(); |
127 } | 127 } |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
278 SetUpNewProvider(CreateAllowFileAccessOptions()); | 278 SetUpNewProvider(CreateAllowFileAccessOptions()); |
279 | 279 |
280 // Create a new root directory. | 280 // Create a new root directory. |
281 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathTestCases); ++i) { | 281 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathTestCases); ++i) { |
282 SCOPED_TRACE(testing::Message() << "RootPath (create) #" << i << " " | 282 SCOPED_TRACE(testing::Message() << "RootPath (create) #" << i << " " |
283 << kRootPathTestCases[i].expected_path); | 283 << kRootPathTestCases[i].expected_path); |
284 | 284 |
285 base::FilePath root_path; | 285 base::FilePath root_path; |
286 EXPECT_TRUE(GetRootPath(GURL(kRootPathTestCases[i].origin_url), | 286 EXPECT_TRUE(GetRootPath(GURL(kRootPathTestCases[i].origin_url), |
287 kRootPathTestCases[i].type, | 287 kRootPathTestCases[i].type, |
288 true /* create */, &root_path)); | 288 OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, |
| 289 &root_path)); |
289 | 290 |
290 base::FilePath expected = file_system_path().AppendASCII( | 291 base::FilePath expected = file_system_path().AppendASCII( |
291 kRootPathTestCases[i].expected_path); | 292 kRootPathTestCases[i].expected_path); |
292 EXPECT_EQ(expected.value(), root_path.value()); | 293 EXPECT_EQ(expected.value(), root_path.value()); |
293 EXPECT_TRUE(file_util::DirectoryExists(root_path)); | 294 EXPECT_TRUE(file_util::DirectoryExists(root_path)); |
294 ASSERT_TRUE(returned_root_path.size() > i); | 295 ASSERT_TRUE(returned_root_path.size() > i); |
295 returned_root_path[i] = root_path; | 296 returned_root_path[i] = root_path; |
296 } | 297 } |
297 | 298 |
298 // Get the root directory with create=false and see if we get the | 299 // Get the root directory with create=false and see if we get the |
299 // same directory. | 300 // same directory. |
300 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathTestCases); ++i) { | 301 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathTestCases); ++i) { |
301 SCOPED_TRACE(testing::Message() << "RootPath (get) #" << i << " " | 302 SCOPED_TRACE(testing::Message() << "RootPath (get) #" << i << " " |
302 << kRootPathTestCases[i].expected_path); | 303 << kRootPathTestCases[i].expected_path); |
303 | 304 |
304 base::FilePath root_path; | 305 base::FilePath root_path; |
305 EXPECT_TRUE(GetRootPath(GURL(kRootPathTestCases[i].origin_url), | 306 EXPECT_TRUE(GetRootPath(GURL(kRootPathTestCases[i].origin_url), |
306 kRootPathTestCases[i].type, | 307 kRootPathTestCases[i].type, |
307 false /* create */, &root_path)); | 308 OPEN_FILE_SYSTEM_FAIL_IF_NONEXISTENT, |
| 309 &root_path)); |
308 ASSERT_TRUE(returned_root_path.size() > i); | 310 ASSERT_TRUE(returned_root_path.size() > i); |
309 EXPECT_EQ(returned_root_path[i].value(), root_path.value()); | 311 EXPECT_EQ(returned_root_path[i].value(), root_path.value()); |
310 } | 312 } |
311 } | 313 } |
312 | 314 |
313 TEST_F(SandboxMountPointProviderTest, | 315 TEST_F(SandboxMountPointProviderTest, |
314 GetRootPathCreateAndExamineWithNewProvider) { | 316 GetRootPathCreateAndExamineWithNewProvider) { |
315 std::vector<base::FilePath> returned_root_path( | 317 std::vector<base::FilePath> returned_root_path( |
316 ARRAYSIZE_UNSAFE(kRootPathTestCases)); | 318 ARRAYSIZE_UNSAFE(kRootPathTestCases)); |
317 SetUpNewProvider(CreateAllowFileAccessOptions()); | 319 SetUpNewProvider(CreateAllowFileAccessOptions()); |
318 | 320 |
319 GURL origin_url("http://foo.com:1/"); | 321 GURL origin_url("http://foo.com:1/"); |
320 | 322 |
321 base::FilePath root_path1; | 323 base::FilePath root_path1; |
322 EXPECT_TRUE(GetRootPath(origin_url, | 324 EXPECT_TRUE(GetRootPath(origin_url, kFileSystemTypeTemporary, |
323 kFileSystemTypeTemporary, true, &root_path1)); | 325 OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, |
| 326 &root_path1)); |
324 | 327 |
325 SetUpNewProvider(CreateDisallowFileAccessOptions()); | 328 SetUpNewProvider(CreateDisallowFileAccessOptions()); |
326 base::FilePath root_path2; | 329 base::FilePath root_path2; |
327 EXPECT_TRUE(GetRootPath(origin_url, | 330 EXPECT_TRUE(GetRootPath(origin_url, kFileSystemTypeTemporary, |
328 kFileSystemTypeTemporary, false, &root_path2)); | 331 OPEN_FILE_SYSTEM_FAIL_IF_NONEXISTENT, |
| 332 &root_path2)); |
329 | 333 |
330 EXPECT_EQ(root_path1.value(), root_path2.value()); | 334 EXPECT_EQ(root_path1.value(), root_path2.value()); |
331 } | 335 } |
332 | 336 |
333 TEST_F(SandboxMountPointProviderTest, GetRootPathGetWithoutCreate) { | 337 TEST_F(SandboxMountPointProviderTest, GetRootPathGetWithoutCreate) { |
334 SetUpNewProvider(CreateDisallowFileAccessOptions()); | 338 SetUpNewProvider(CreateDisallowFileAccessOptions()); |
335 | 339 |
336 // Try to get a root directory without creating. | 340 // Try to get a root directory without creating. |
337 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathTestCases); ++i) { | 341 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathTestCases); ++i) { |
338 SCOPED_TRACE(testing::Message() << "RootPath (create=false) #" << i << " " | 342 SCOPED_TRACE(testing::Message() << "RootPath (create=false) #" << i << " " |
339 << kRootPathTestCases[i].expected_path); | 343 << kRootPathTestCases[i].expected_path); |
340 EXPECT_FALSE(GetRootPath(GURL(kRootPathTestCases[i].origin_url), | 344 EXPECT_FALSE(GetRootPath(GURL(kRootPathTestCases[i].origin_url), |
341 kRootPathTestCases[i].type, | 345 kRootPathTestCases[i].type, |
342 false /* create */, NULL)); | 346 OPEN_FILE_SYSTEM_FAIL_IF_NONEXISTENT, |
| 347 NULL)); |
343 } | 348 } |
344 } | 349 } |
345 | 350 |
346 TEST_F(SandboxMountPointProviderTest, GetRootPathInIncognito) { | 351 TEST_F(SandboxMountPointProviderTest, GetRootPathInIncognito) { |
347 SetUpNewProvider(CreateIncognitoFileSystemOptions()); | 352 SetUpNewProvider(CreateIncognitoFileSystemOptions()); |
348 | 353 |
349 // Try to get a root directory. | 354 // Try to get a root directory. |
350 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathTestCases); ++i) { | 355 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathTestCases); ++i) { |
351 SCOPED_TRACE(testing::Message() << "RootPath (incognito) #" << i << " " | 356 SCOPED_TRACE(testing::Message() << "RootPath (incognito) #" << i << " " |
352 << kRootPathTestCases[i].expected_path); | 357 << kRootPathTestCases[i].expected_path); |
353 EXPECT_FALSE(GetRootPath(GURL(kRootPathTestCases[i].origin_url), | 358 EXPECT_FALSE( |
354 kRootPathTestCases[i].type, | 359 GetRootPath(GURL(kRootPathTestCases[i].origin_url), |
355 true /* create */, NULL)); | 360 kRootPathTestCases[i].type, |
| 361 OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, |
| 362 NULL)); |
356 } | 363 } |
357 } | 364 } |
358 | 365 |
359 TEST_F(SandboxMountPointProviderTest, GetRootPathFileURI) { | 366 TEST_F(SandboxMountPointProviderTest, GetRootPathFileURI) { |
360 SetUpNewProvider(CreateDisallowFileAccessOptions()); | 367 SetUpNewProvider(CreateDisallowFileAccessOptions()); |
361 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathFileURITestCases); ++i) { | 368 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathFileURITestCases); ++i) { |
362 SCOPED_TRACE(testing::Message() << "RootPathFileURI (disallow) #" | 369 SCOPED_TRACE(testing::Message() << "RootPathFileURI (disallow) #" |
363 << i << " " << kRootPathFileURITestCases[i].expected_path); | 370 << i << " " << kRootPathFileURITestCases[i].expected_path); |
364 EXPECT_FALSE(GetRootPath(GURL(kRootPathFileURITestCases[i].origin_url), | 371 EXPECT_FALSE( |
365 kRootPathFileURITestCases[i].type, | 372 GetRootPath(GURL(kRootPathFileURITestCases[i].origin_url), |
366 true /* create */, NULL)); | 373 kRootPathFileURITestCases[i].type, |
| 374 OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, |
| 375 NULL)); |
367 } | 376 } |
368 } | 377 } |
369 | 378 |
370 TEST_F(SandboxMountPointProviderTest, GetRootPathFileURIWithAllowFlag) { | 379 TEST_F(SandboxMountPointProviderTest, GetRootPathFileURIWithAllowFlag) { |
371 SetUpNewProvider(CreateAllowFileAccessOptions()); | 380 SetUpNewProvider(CreateAllowFileAccessOptions()); |
372 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathFileURITestCases); ++i) { | 381 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathFileURITestCases); ++i) { |
373 SCOPED_TRACE(testing::Message() << "RootPathFileURI (allow) #" | 382 SCOPED_TRACE(testing::Message() << "RootPathFileURI (allow) #" |
374 << i << " " << kRootPathFileURITestCases[i].expected_path); | 383 << i << " " << kRootPathFileURITestCases[i].expected_path); |
375 base::FilePath root_path; | 384 base::FilePath root_path; |
376 EXPECT_TRUE(GetRootPath(GURL(kRootPathFileURITestCases[i].origin_url), | 385 EXPECT_TRUE(GetRootPath(GURL(kRootPathFileURITestCases[i].origin_url), |
377 kRootPathFileURITestCases[i].type, | 386 kRootPathFileURITestCases[i].type, |
378 true /* create */, &root_path)); | 387 OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, |
| 388 &root_path)); |
379 base::FilePath expected = file_system_path().AppendASCII( | 389 base::FilePath expected = file_system_path().AppendASCII( |
380 kRootPathFileURITestCases[i].expected_path); | 390 kRootPathFileURITestCases[i].expected_path); |
381 EXPECT_EQ(expected.value(), root_path.value()); | 391 EXPECT_EQ(expected.value(), root_path.value()); |
382 EXPECT_TRUE(file_util::DirectoryExists(root_path)); | 392 EXPECT_TRUE(file_util::DirectoryExists(root_path)); |
383 } | 393 } |
384 } | 394 } |
385 | 395 |
386 } // namespace fileapi | 396 } // namespace fileapi |
OLD | NEW |