| OLD | NEW | 
|---|
|  | (Empty) | 
| 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. |  | 
| 4 |  | 
| 5 #include <set> |  | 
| 6 #include <string> |  | 
| 7 |  | 
| 8 #include "base/basictypes.h" |  | 
| 9 #include "base/bind.h" |  | 
| 10 #include "base/file_util.h" |  | 
| 11 #include "base/files/scoped_temp_dir.h" |  | 
| 12 #include "base/memory/ref_counted.h" |  | 
| 13 #include "base/memory/scoped_ptr.h" |  | 
| 14 #include "base/memory/weak_ptr.h" |  | 
| 15 #include "base/message_loop.h" |  | 
| 16 #include "base/message_loop_proxy.h" |  | 
| 17 #include "base/strings/sys_string_conversions.h" |  | 
| 18 #include "base/utf_string_conversions.h" |  | 
| 19 #include "googleurl/src/gurl.h" |  | 
| 20 #include "testing/gtest/include/gtest/gtest.h" |  | 
| 21 #include "webkit/fileapi/external_mount_points.h" |  | 
| 22 #include "webkit/fileapi/file_system_context.h" |  | 
| 23 #include "webkit/fileapi/file_system_task_runners.h" |  | 
| 24 #include "webkit/fileapi/file_system_url.h" |  | 
| 25 #include "webkit/fileapi/file_system_util.h" |  | 
| 26 #include "webkit/fileapi/mock_file_system_options.h" |  | 
| 27 #include "webkit/fileapi/sandbox_mount_point_provider.h" |  | 
| 28 #include "webkit/quota/mock_special_storage_policy.h" |  | 
| 29 |  | 
| 30 #if defined(OS_CHROMEOS) |  | 
| 31 #include "webkit/chromeos/fileapi/cros_mount_point_provider.h" |  | 
| 32 #endif |  | 
| 33 |  | 
| 34 namespace fileapi { |  | 
| 35 |  | 
| 36 namespace { |  | 
| 37 |  | 
| 38 // PS stands for path separator. |  | 
| 39 #if defined(FILE_PATH_USES_WIN_SEPARATORS) |  | 
| 40 #define PS  "\\" |  | 
| 41 #else |  | 
| 42 #define PS  "/" |  | 
| 43 #endif |  | 
| 44 |  | 
| 45 struct RootPathTestCase { |  | 
| 46   fileapi::FileSystemType type; |  | 
| 47   const char* origin_url; |  | 
| 48   const char* expected_path; |  | 
| 49 }; |  | 
| 50 |  | 
| 51 const struct RootPathTest { |  | 
| 52   fileapi::FileSystemType type; |  | 
| 53   const char* origin_url; |  | 
| 54   const char* expected_path; |  | 
| 55 } kRootPathTestCases[] = { |  | 
| 56   { fileapi::kFileSystemTypeTemporary, "http://foo:1/", |  | 
| 57     "000" PS "t" }, |  | 
| 58   { fileapi::kFileSystemTypePersistent, "http://foo:1/", |  | 
| 59     "000" PS "p" }, |  | 
| 60   { fileapi::kFileSystemTypeTemporary, "http://bar.com/", |  | 
| 61     "001" PS "t" }, |  | 
| 62   { fileapi::kFileSystemTypePersistent, "http://bar.com/", |  | 
| 63     "001" PS "p" }, |  | 
| 64   { fileapi::kFileSystemTypeTemporary, "https://foo:2/", |  | 
| 65     "002" PS "t" }, |  | 
| 66   { fileapi::kFileSystemTypePersistent, "https://foo:2/", |  | 
| 67     "002" PS "p" }, |  | 
| 68   { fileapi::kFileSystemTypeTemporary, "https://bar.com/", |  | 
| 69     "003" PS "t" }, |  | 
| 70   { fileapi::kFileSystemTypePersistent, "https://bar.com/", |  | 
| 71     "003" PS "p" }, |  | 
| 72 #if defined(OS_CHROMEOS) |  | 
| 73   { fileapi::kFileSystemTypeExternal, "chrome-extension://foo/", |  | 
| 74     "chrome-extension__0" /* unused, only for logging */ }, |  | 
| 75 #endif |  | 
| 76 }; |  | 
| 77 |  | 
| 78 const struct RootPathFileURITest { |  | 
| 79   fileapi::FileSystemType type; |  | 
| 80   const char* origin_url; |  | 
| 81   const char* expected_path; |  | 
| 82   const char* virtual_path; |  | 
| 83 } kRootPathFileURITestCases[] = { |  | 
| 84   { fileapi::kFileSystemTypeTemporary, "file:///", |  | 
| 85     "000" PS "t", NULL }, |  | 
| 86   { fileapi::kFileSystemTypePersistent, "file:///", |  | 
| 87     "000" PS "p", NULL }, |  | 
| 88 }; |  | 
| 89 |  | 
| 90 const struct CheckValidPathTest { |  | 
| 91   base::FilePath::StringType path; |  | 
| 92   bool expected_valid; |  | 
| 93 } kCheckValidPathTestCases[] = { |  | 
| 94   { FILE_PATH_LITERAL("//tmp/foo.txt"), false, }, |  | 
| 95   { FILE_PATH_LITERAL("//etc/hosts"), false, }, |  | 
| 96   { FILE_PATH_LITERAL("foo.txt"), true, }, |  | 
| 97   { FILE_PATH_LITERAL("a/b/c"), true, }, |  | 
| 98   // Any paths that includes parent references are considered invalid. |  | 
| 99   { FILE_PATH_LITERAL(".."), false, }, |  | 
| 100   { FILE_PATH_LITERAL("tmp/.."), false, }, |  | 
| 101   { FILE_PATH_LITERAL("a/b/../c/.."), false, }, |  | 
| 102 }; |  | 
| 103 |  | 
| 104 // For External filesystem. |  | 
| 105 const base::FilePath::CharType kMountPoint[] = FILE_PATH_LITERAL("/tmp/testing")
     ; |  | 
| 106 const base::FilePath::CharType kRootPath[] = FILE_PATH_LITERAL("/tmp"); |  | 
| 107 const base::FilePath::CharType kVirtualPath[] = FILE_PATH_LITERAL("testing"); |  | 
| 108 |  | 
| 109 }  // namespace |  | 
| 110 |  | 
| 111 class FileSystemMountPointProviderTest : public testing::Test { |  | 
| 112  public: |  | 
| 113   FileSystemMountPointProviderTest() |  | 
| 114       : weak_factory_(this) { |  | 
| 115   } |  | 
| 116 |  | 
| 117   virtual void SetUp() { |  | 
| 118     ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); |  | 
| 119     special_storage_policy_ = new quota::MockSpecialStoragePolicy; |  | 
| 120   } |  | 
| 121 |  | 
| 122  protected: |  | 
| 123   void SetupNewContext(const FileSystemOptions& options) { |  | 
| 124     scoped_refptr<ExternalMountPoints> external_mount_points( |  | 
| 125         ExternalMountPoints::CreateRefCounted()); |  | 
| 126     file_system_context_ = new FileSystemContext( |  | 
| 127         FileSystemTaskRunners::CreateMockTaskRunners(), |  | 
| 128         external_mount_points.get(), |  | 
| 129         special_storage_policy_, |  | 
| 130         NULL, |  | 
| 131         ScopedVector<FileSystemMountPointProvider>(), |  | 
| 132         data_dir_.path(), |  | 
| 133         options); |  | 
| 134 #if defined(OS_CHROMEOS) |  | 
| 135     base::FilePath mount_point_path = base::FilePath(kMountPoint); |  | 
| 136     external_mount_points->RegisterFileSystem( |  | 
| 137         mount_point_path.BaseName().AsUTF8Unsafe(), |  | 
| 138         kFileSystemTypeNativeLocal, |  | 
| 139         mount_point_path); |  | 
| 140 #endif |  | 
| 141   } |  | 
| 142 |  | 
| 143   FileSystemMountPointProvider* provider(FileSystemType type) { |  | 
| 144     DCHECK(file_system_context_); |  | 
| 145     return file_system_context_->GetMountPointProvider(type); |  | 
| 146   } |  | 
| 147 |  | 
| 148   bool GetRootPath(const GURL& origin_url, |  | 
| 149                    fileapi::FileSystemType type, |  | 
| 150                    bool create, |  | 
| 151                    base::FilePath* root_path) { |  | 
| 152     base::FilePath virtual_path = base::FilePath(); |  | 
| 153     if (type == kFileSystemTypeExternal) |  | 
| 154       virtual_path = base::FilePath(kVirtualPath); |  | 
| 155     FileSystemURL url = file_system_context_->CreateCrackedFileSystemURL( |  | 
| 156         origin_url, type, virtual_path); |  | 
| 157     base::FilePath returned_root_path = |  | 
| 158         provider(type)->GetFileSystemRootPathOnFileThread(url, create); |  | 
| 159     if (root_path) |  | 
| 160       *root_path = returned_root_path; |  | 
| 161     return !returned_root_path.empty(); |  | 
| 162   } |  | 
| 163 |  | 
| 164   base::FilePath data_path() const { return data_dir_.path(); } |  | 
| 165   base::FilePath file_system_path() const { |  | 
| 166     return data_dir_.path().Append( |  | 
| 167         SandboxMountPointProvider::kFileSystemDirectory); |  | 
| 168   } |  | 
| 169   FileSystemContext* file_system_context() const { |  | 
| 170     return file_system_context_.get(); |  | 
| 171   } |  | 
| 172 |  | 
| 173  private: |  | 
| 174   base::ScopedTempDir data_dir_; |  | 
| 175   base::MessageLoop message_loop_; |  | 
| 176   base::WeakPtrFactory<FileSystemMountPointProviderTest> weak_factory_; |  | 
| 177 |  | 
| 178   scoped_refptr<quota::SpecialStoragePolicy> special_storage_policy_; |  | 
| 179   scoped_refptr<FileSystemContext> file_system_context_; |  | 
| 180 |  | 
| 181   DISALLOW_COPY_AND_ASSIGN(FileSystemMountPointProviderTest); |  | 
| 182 }; |  | 
| 183 |  | 
| 184 TEST_F(FileSystemMountPointProviderTest, GetRootPathCreateAndExamine) { |  | 
| 185   std::vector<base::FilePath> returned_root_path( |  | 
| 186       ARRAYSIZE_UNSAFE(kRootPathTestCases)); |  | 
| 187   SetupNewContext(CreateAllowFileAccessOptions()); |  | 
| 188 |  | 
| 189   // Create a new root directory. |  | 
| 190   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathTestCases); ++i) { |  | 
| 191     SCOPED_TRACE(testing::Message() << "RootPath (create) #" << i << " " |  | 
| 192                  << kRootPathTestCases[i].expected_path); |  | 
| 193 |  | 
| 194     base::FilePath root_path; |  | 
| 195     EXPECT_TRUE(GetRootPath(GURL(kRootPathTestCases[i].origin_url), |  | 
| 196                             kRootPathTestCases[i].type, |  | 
| 197                             true /* create */, &root_path)); |  | 
| 198 |  | 
| 199     if (kRootPathTestCases[i].type != kFileSystemTypeExternal) { |  | 
| 200       base::FilePath expected = file_system_path().AppendASCII( |  | 
| 201           kRootPathTestCases[i].expected_path); |  | 
| 202       EXPECT_EQ(expected.value(), root_path.value()); |  | 
| 203       EXPECT_TRUE(file_util::DirectoryExists(root_path)); |  | 
| 204     } else { |  | 
| 205       // External file system root path is virtual one and does not match |  | 
| 206       // anything from the actual file system. |  | 
| 207       EXPECT_EQ(kRootPath, root_path.value()); |  | 
| 208     } |  | 
| 209     ASSERT_TRUE(returned_root_path.size() > i); |  | 
| 210     returned_root_path[i] = root_path; |  | 
| 211   } |  | 
| 212 |  | 
| 213   // Get the root directory with create=false and see if we get the |  | 
| 214   // same directory. |  | 
| 215   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathTestCases); ++i) { |  | 
| 216     SCOPED_TRACE(testing::Message() << "RootPath (get) #" << i << " " |  | 
| 217                  << kRootPathTestCases[i].expected_path); |  | 
| 218 |  | 
| 219     base::FilePath root_path; |  | 
| 220     EXPECT_TRUE(GetRootPath(GURL(kRootPathTestCases[i].origin_url), |  | 
| 221                             kRootPathTestCases[i].type, |  | 
| 222                             false /* create */, &root_path)); |  | 
| 223     ASSERT_TRUE(returned_root_path.size() > i); |  | 
| 224     EXPECT_EQ(returned_root_path[i].value(), root_path.value()); |  | 
| 225   } |  | 
| 226 } |  | 
| 227 |  | 
| 228 TEST_F(FileSystemMountPointProviderTest, |  | 
| 229        GetRootPathCreateAndExamineWithNewProvider) { |  | 
| 230   std::vector<base::FilePath> returned_root_path( |  | 
| 231       ARRAYSIZE_UNSAFE(kRootPathTestCases)); |  | 
| 232   SetupNewContext(CreateAllowFileAccessOptions()); |  | 
| 233 |  | 
| 234   GURL origin_url("http://foo.com:1/"); |  | 
| 235 |  | 
| 236   base::FilePath root_path1; |  | 
| 237   EXPECT_TRUE(GetRootPath(origin_url, |  | 
| 238                           kFileSystemTypeTemporary, true, &root_path1)); |  | 
| 239 |  | 
| 240   SetupNewContext(CreateDisallowFileAccessOptions()); |  | 
| 241   base::FilePath root_path2; |  | 
| 242   EXPECT_TRUE(GetRootPath(origin_url, |  | 
| 243                           kFileSystemTypeTemporary, false, &root_path2)); |  | 
| 244 |  | 
| 245   EXPECT_EQ(root_path1.value(), root_path2.value()); |  | 
| 246 } |  | 
| 247 |  | 
| 248 TEST_F(FileSystemMountPointProviderTest, GetRootPathGetWithoutCreate) { |  | 
| 249   SetupNewContext(CreateDisallowFileAccessOptions()); |  | 
| 250 |  | 
| 251   // Try to get a root directory without creating. |  | 
| 252   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathTestCases); ++i) { |  | 
| 253     SCOPED_TRACE(testing::Message() << "RootPath (create=false) #" << i << " " |  | 
| 254                  << kRootPathTestCases[i].expected_path); |  | 
| 255     // External type does not check the directory existence. |  | 
| 256     if (kRootPathTestCases[i].type != kFileSystemTypeExternal) { |  | 
| 257       EXPECT_FALSE(GetRootPath(GURL(kRootPathTestCases[i].origin_url), |  | 
| 258                               kRootPathTestCases[i].type, |  | 
| 259                               false /* create */, NULL)); |  | 
| 260     } |  | 
| 261   } |  | 
| 262 } |  | 
| 263 |  | 
| 264 TEST_F(FileSystemMountPointProviderTest, GetRootPathInIncognito) { |  | 
| 265   SetupNewContext(CreateIncognitoFileSystemOptions()); |  | 
| 266 |  | 
| 267   // Try to get a root directory. |  | 
| 268   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathTestCases); ++i) { |  | 
| 269     SCOPED_TRACE(testing::Message() << "RootPath (incognito) #" << i << " " |  | 
| 270                  << kRootPathTestCases[i].expected_path); |  | 
| 271     // External type does not change the behavior in incognito mode. |  | 
| 272     if (kRootPathTestCases[i].type != kFileSystemTypeExternal) { |  | 
| 273       EXPECT_FALSE(GetRootPath(GURL(kRootPathTestCases[i].origin_url), |  | 
| 274                               kRootPathTestCases[i].type, |  | 
| 275                               true /* create */, NULL)); |  | 
| 276     } |  | 
| 277   } |  | 
| 278 } |  | 
| 279 |  | 
| 280 TEST_F(FileSystemMountPointProviderTest, GetRootPathFileURI) { |  | 
| 281   SetupNewContext(CreateDisallowFileAccessOptions()); |  | 
| 282   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathFileURITestCases); ++i) { |  | 
| 283     SCOPED_TRACE(testing::Message() << "RootPathFileURI (disallow) #" |  | 
| 284                  << i << " " << kRootPathFileURITestCases[i].expected_path); |  | 
| 285     EXPECT_FALSE(GetRootPath(GURL(kRootPathFileURITestCases[i].origin_url), |  | 
| 286                              kRootPathFileURITestCases[i].type, |  | 
| 287                              true /* create */, NULL)); |  | 
| 288   } |  | 
| 289 } |  | 
| 290 |  | 
| 291 TEST_F(FileSystemMountPointProviderTest, GetRootPathFileURIWithAllowFlag) { |  | 
| 292   SetupNewContext(CreateAllowFileAccessOptions()); |  | 
| 293   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathFileURITestCases); ++i) { |  | 
| 294     SCOPED_TRACE(testing::Message() << "RootPathFileURI (allow) #" |  | 
| 295                  << i << " " << kRootPathFileURITestCases[i].expected_path); |  | 
| 296     base::FilePath root_path; |  | 
| 297     EXPECT_TRUE(GetRootPath(GURL(kRootPathFileURITestCases[i].origin_url), |  | 
| 298                             kRootPathFileURITestCases[i].type, |  | 
| 299                             true /* create */, &root_path)); |  | 
| 300     base::FilePath expected = file_system_path().AppendASCII( |  | 
| 301         kRootPathFileURITestCases[i].expected_path); |  | 
| 302     EXPECT_EQ(expected.value(), root_path.value()); |  | 
| 303     EXPECT_TRUE(file_util::DirectoryExists(root_path)); |  | 
| 304   } |  | 
| 305 } |  | 
| 306 |  | 
| 307 }  // namespace fileapi |  | 
| OLD | NEW | 
|---|