| 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[] = | |
| 106 FILE_PATH_LITERAL("/tmp/testing"); | |
| 107 const base::FilePath::CharType kRootPath[] = FILE_PATH_LITERAL("/tmp"); | |
| 108 const base::FilePath::CharType kVirtualPath[] = FILE_PATH_LITERAL("testing"); | |
| 109 | |
| 110 } // namespace | |
| 111 | |
| 112 class FileSystemMountPointProviderTest : public testing::Test { | |
| 113 public: | |
| 114 FileSystemMountPointProviderTest() | |
| 115 : weak_factory_(this) { | |
| 116 } | |
| 117 | |
| 118 virtual void SetUp() { | |
| 119 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); | |
| 120 special_storage_policy_ = new quota::MockSpecialStoragePolicy; | |
| 121 } | |
| 122 | |
| 123 protected: | |
| 124 void SetupNewContext(const FileSystemOptions& options) { | |
| 125 scoped_refptr<ExternalMountPoints> external_mount_points( | |
| 126 ExternalMountPoints::CreateRefCounted()); | |
| 127 file_system_context_ = new FileSystemContext( | |
| 128 FileSystemTaskRunners::CreateMockTaskRunners(), | |
| 129 external_mount_points.get(), | |
| 130 special_storage_policy_, | |
| 131 NULL, | |
| 132 ScopedVector<FileSystemMountPointProvider>(), | |
| 133 data_dir_.path(), | |
| 134 options); | |
| 135 #if defined(OS_CHROMEOS) | |
| 136 base::FilePath mount_point_path = base::FilePath(kMountPoint); | |
| 137 external_mount_points->RegisterFileSystem( | |
| 138 mount_point_path.BaseName().AsUTF8Unsafe(), | |
| 139 kFileSystemTypeNativeLocal, | |
| 140 mount_point_path); | |
| 141 #endif | |
| 142 } | |
| 143 | |
| 144 FileSystemMountPointProvider* provider(FileSystemType type) { | |
| 145 DCHECK(file_system_context_); | |
| 146 return file_system_context_->GetMountPointProvider(type); | |
| 147 } | |
| 148 | |
| 149 bool GetRootPath(const GURL& origin_url, | |
| 150 fileapi::FileSystemType type, | |
| 151 bool create, | |
| 152 base::FilePath* root_path) { | |
| 153 base::FilePath virtual_path = base::FilePath(); | |
| 154 if (type == kFileSystemTypeExternal) | |
| 155 virtual_path = base::FilePath(kVirtualPath); | |
| 156 FileSystemURL url = file_system_context_->CreateCrackedFileSystemURL( | |
| 157 origin_url, type, virtual_path); | |
| 158 base::FilePath returned_root_path = | |
| 159 provider(type)->GetFileSystemRootPathOnFileThread(url, create); | |
| 160 if (root_path) | |
| 161 *root_path = returned_root_path; | |
| 162 return !returned_root_path.empty(); | |
| 163 } | |
| 164 | |
| 165 base::FilePath data_path() const { return data_dir_.path(); } | |
| 166 base::FilePath file_system_path() const { | |
| 167 return data_dir_.path().Append( | |
| 168 SandboxMountPointProvider::kFileSystemDirectory); | |
| 169 } | |
| 170 FileSystemContext* file_system_context() const { | |
| 171 return file_system_context_.get(); | |
| 172 } | |
| 173 | |
| 174 private: | |
| 175 base::ScopedTempDir data_dir_; | |
| 176 base::MessageLoop message_loop_; | |
| 177 base::WeakPtrFactory<FileSystemMountPointProviderTest> weak_factory_; | |
| 178 | |
| 179 scoped_refptr<quota::SpecialStoragePolicy> special_storage_policy_; | |
| 180 scoped_refptr<FileSystemContext> file_system_context_; | |
| 181 | |
| 182 DISALLOW_COPY_AND_ASSIGN(FileSystemMountPointProviderTest); | |
| 183 }; | |
| 184 | |
| 185 TEST_F(FileSystemMountPointProviderTest, GetRootPathCreateAndExamine) { | |
| 186 std::vector<base::FilePath> returned_root_path( | |
| 187 ARRAYSIZE_UNSAFE(kRootPathTestCases)); | |
| 188 SetupNewContext(CreateAllowFileAccessOptions()); | |
| 189 | |
| 190 // Create a new root directory. | |
| 191 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathTestCases); ++i) { | |
| 192 SCOPED_TRACE(testing::Message() << "RootPath (create) #" << i << " " | |
| 193 << kRootPathTestCases[i].expected_path); | |
| 194 | |
| 195 base::FilePath root_path; | |
| 196 EXPECT_TRUE(GetRootPath(GURL(kRootPathTestCases[i].origin_url), | |
| 197 kRootPathTestCases[i].type, | |
| 198 true /* create */, &root_path)); | |
| 199 | |
| 200 if (kRootPathTestCases[i].type != kFileSystemTypeExternal) { | |
| 201 base::FilePath expected = file_system_path().AppendASCII( | |
| 202 kRootPathTestCases[i].expected_path); | |
| 203 EXPECT_EQ(expected.value(), root_path.value()); | |
| 204 EXPECT_TRUE(file_util::DirectoryExists(root_path)); | |
| 205 } else { | |
| 206 // External file system root path is virtual one and does not match | |
| 207 // anything from the actual file system. | |
| 208 EXPECT_EQ(kRootPath, root_path.value()); | |
| 209 } | |
| 210 ASSERT_TRUE(returned_root_path.size() > i); | |
| 211 returned_root_path[i] = root_path; | |
| 212 } | |
| 213 | |
| 214 // Get the root directory with create=false and see if we get the | |
| 215 // same directory. | |
| 216 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathTestCases); ++i) { | |
| 217 SCOPED_TRACE(testing::Message() << "RootPath (get) #" << i << " " | |
| 218 << kRootPathTestCases[i].expected_path); | |
| 219 | |
| 220 base::FilePath root_path; | |
| 221 EXPECT_TRUE(GetRootPath(GURL(kRootPathTestCases[i].origin_url), | |
| 222 kRootPathTestCases[i].type, | |
| 223 false /* create */, &root_path)); | |
| 224 ASSERT_TRUE(returned_root_path.size() > i); | |
| 225 EXPECT_EQ(returned_root_path[i].value(), root_path.value()); | |
| 226 } | |
| 227 } | |
| 228 | |
| 229 TEST_F(FileSystemMountPointProviderTest, | |
| 230 GetRootPathCreateAndExamineWithNewProvider) { | |
| 231 std::vector<base::FilePath> returned_root_path( | |
| 232 ARRAYSIZE_UNSAFE(kRootPathTestCases)); | |
| 233 SetupNewContext(CreateAllowFileAccessOptions()); | |
| 234 | |
| 235 GURL origin_url("http://foo.com:1/"); | |
| 236 | |
| 237 base::FilePath root_path1; | |
| 238 EXPECT_TRUE(GetRootPath(origin_url, | |
| 239 kFileSystemTypeTemporary, true, &root_path1)); | |
| 240 | |
| 241 SetupNewContext(CreateDisallowFileAccessOptions()); | |
| 242 base::FilePath root_path2; | |
| 243 EXPECT_TRUE(GetRootPath(origin_url, | |
| 244 kFileSystemTypeTemporary, false, &root_path2)); | |
| 245 | |
| 246 EXPECT_EQ(root_path1.value(), root_path2.value()); | |
| 247 } | |
| 248 | |
| 249 TEST_F(FileSystemMountPointProviderTest, GetRootPathGetWithoutCreate) { | |
| 250 SetupNewContext(CreateDisallowFileAccessOptions()); | |
| 251 | |
| 252 // Try to get a root directory without creating. | |
| 253 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathTestCases); ++i) { | |
| 254 SCOPED_TRACE(testing::Message() << "RootPath (create=false) #" << i << " " | |
| 255 << kRootPathTestCases[i].expected_path); | |
| 256 // External type does not check the directory existence. | |
| 257 if (kRootPathTestCases[i].type != kFileSystemTypeExternal) { | |
| 258 EXPECT_FALSE(GetRootPath(GURL(kRootPathTestCases[i].origin_url), | |
| 259 kRootPathTestCases[i].type, | |
| 260 false /* create */, NULL)); | |
| 261 } | |
| 262 } | |
| 263 } | |
| 264 | |
| 265 TEST_F(FileSystemMountPointProviderTest, GetRootPathInIncognito) { | |
| 266 SetupNewContext(CreateIncognitoFileSystemOptions()); | |
| 267 | |
| 268 // Try to get a root directory. | |
| 269 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathTestCases); ++i) { | |
| 270 SCOPED_TRACE(testing::Message() << "RootPath (incognito) #" << i << " " | |
| 271 << kRootPathTestCases[i].expected_path); | |
| 272 // External type does not change the behavior in incognito mode. | |
| 273 if (kRootPathTestCases[i].type != kFileSystemTypeExternal) { | |
| 274 EXPECT_FALSE(GetRootPath(GURL(kRootPathTestCases[i].origin_url), | |
| 275 kRootPathTestCases[i].type, | |
| 276 true /* create */, NULL)); | |
| 277 } | |
| 278 } | |
| 279 } | |
| 280 | |
| 281 TEST_F(FileSystemMountPointProviderTest, GetRootPathFileURI) { | |
| 282 SetupNewContext(CreateDisallowFileAccessOptions()); | |
| 283 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathFileURITestCases); ++i) { | |
| 284 SCOPED_TRACE(testing::Message() << "RootPathFileURI (disallow) #" | |
| 285 << i << " " << kRootPathFileURITestCases[i].expected_path); | |
| 286 EXPECT_FALSE(GetRootPath(GURL(kRootPathFileURITestCases[i].origin_url), | |
| 287 kRootPathFileURITestCases[i].type, | |
| 288 true /* create */, NULL)); | |
| 289 } | |
| 290 } | |
| 291 | |
| 292 TEST_F(FileSystemMountPointProviderTest, GetRootPathFileURIWithAllowFlag) { | |
| 293 SetupNewContext(CreateAllowFileAccessOptions()); | |
| 294 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathFileURITestCases); ++i) { | |
| 295 SCOPED_TRACE(testing::Message() << "RootPathFileURI (allow) #" | |
| 296 << i << " " << kRootPathFileURITestCases[i].expected_path); | |
| 297 base::FilePath root_path; | |
| 298 EXPECT_TRUE(GetRootPath(GURL(kRootPathFileURITestCases[i].origin_url), | |
| 299 kRootPathFileURITestCases[i].type, | |
| 300 true /* create */, &root_path)); | |
| 301 base::FilePath expected = file_system_path().AppendASCII( | |
| 302 kRootPathFileURITestCases[i].expected_path); | |
| 303 EXPECT_EQ(expected.value(), root_path.value()); | |
| 304 EXPECT_TRUE(file_util::DirectoryExists(root_path)); | |
| 305 } | |
| 306 } | |
| 307 | |
| 308 } // namespace fileapi | |
| OLD | NEW |