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
|
// Copyright 2013 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 "storage/browser/fileapi/file_system_context.h"
#include "base/files/scoped_temp_dir.h"
#include "base/message_loop/message_loop.h"
#include "base/strings/stringprintf.h"
#include "content/browser/quota/mock_quota_manager.h"
#include "content/public/test/mock_special_storage_policy.h"
#include "content/public/test/test_file_system_options.h"
#include "storage/browser/fileapi/external_mount_points.h"
#include "storage/browser/fileapi/file_system_backend.h"
#include "storage/browser/fileapi/isolated_context.h"
#include "testing/gtest/include/gtest/gtest.h"
#define FPL(x) FILE_PATH_LITERAL(x)
#if defined(FILE_PATH_USES_DRIVE_LETTERS)
#define DRIVE FPL("C:")
#else
#define DRIVE
#endif
using storage::ExternalMountPoints;
using storage::FileSystemBackend;
using storage::FileSystemContext;
using storage::FileSystemMountOption;
using storage::FileSystemURL;
using storage::IsolatedContext;
namespace content {
namespace {
const char kTestOrigin[] = "http://chromium.org/";
GURL CreateRawFileSystemURL(const std::string& type_str,
const std::string& fs_id) {
std::string url_str = base::StringPrintf(
"filesystem:http://chromium.org/%s/%s/root/file",
type_str.c_str(),
fs_id.c_str());
return GURL(url_str);
}
class FileSystemContextTest : public testing::Test {
public:
FileSystemContextTest() {}
void SetUp() override {
ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
storage_policy_ = new MockSpecialStoragePolicy();
mock_quota_manager_ =
new MockQuotaManager(false /* is_incognito */,
data_dir_.path(),
base::MessageLoopProxy::current().get(),
base::MessageLoopProxy::current().get(),
storage_policy_.get());
}
protected:
FileSystemContext* CreateFileSystemContextForTest(
storage::ExternalMountPoints* external_mount_points) {
return new FileSystemContext(
base::MessageLoopProxy::current().get(),
base::MessageLoopProxy::current().get(),
external_mount_points,
storage_policy_.get(),
mock_quota_manager_->proxy(),
ScopedVector<FileSystemBackend>(),
std::vector<storage::URLRequestAutoMountHandler>(),
data_dir_.path(),
CreateAllowFileAccessOptions());
}
// Verifies a *valid* filesystem url has expected values.
void ExpectFileSystemURLMatches(const FileSystemURL& url,
const GURL& expect_origin,
storage::FileSystemType expect_mount_type,
storage::FileSystemType expect_type,
const base::FilePath& expect_path,
const base::FilePath& expect_virtual_path,
const std::string& expect_filesystem_id) {
EXPECT_TRUE(url.is_valid());
EXPECT_EQ(expect_origin, url.origin());
EXPECT_EQ(expect_mount_type, url.mount_type());
EXPECT_EQ(expect_type, url.type());
EXPECT_EQ(expect_path, url.path());
EXPECT_EQ(expect_virtual_path, url.virtual_path());
EXPECT_EQ(expect_filesystem_id, url.filesystem_id());
}
private:
base::ScopedTempDir data_dir_;
base::MessageLoop message_loop_;
scoped_refptr<storage::SpecialStoragePolicy> storage_policy_;
scoped_refptr<MockQuotaManager> mock_quota_manager_;
};
// It is not valid to pass NULL ExternalMountPoints to FileSystemContext on
// ChromeOS.
#if !defined(OS_CHROMEOS)
TEST_F(FileSystemContextTest, NullExternalMountPoints) {
scoped_refptr<FileSystemContext> file_system_context(
CreateFileSystemContextForTest(NULL));
// Cracking system external mount and isolated mount points should work.
std::string isolated_name = "root";
std::string isolated_id =
IsolatedContext::GetInstance()->RegisterFileSystemForPath(
storage::kFileSystemTypeNativeLocal,
std::string(),
base::FilePath(DRIVE FPL("/test/isolated/root")),
&isolated_name);
// Register system external mount point.
ASSERT_TRUE(ExternalMountPoints::GetSystemInstance()->RegisterFileSystem(
"system",
storage::kFileSystemTypeNativeLocal,
FileSystemMountOption(),
base::FilePath(DRIVE FPL("/test/sys/"))));
FileSystemURL cracked_isolated = file_system_context->CrackURL(
CreateRawFileSystemURL("isolated", isolated_id));
ExpectFileSystemURLMatches(
cracked_isolated,
GURL(kTestOrigin),
storage::kFileSystemTypeIsolated,
storage::kFileSystemTypeNativeLocal,
base::FilePath(DRIVE FPL("/test/isolated/root/file"))
.NormalizePathSeparators(),
base::FilePath::FromUTF8Unsafe(isolated_id)
.Append(FPL("root/file"))
.NormalizePathSeparators(),
isolated_id);
FileSystemURL cracked_external = file_system_context->CrackURL(
CreateRawFileSystemURL("external", "system"));
ExpectFileSystemURLMatches(
cracked_external,
GURL(kTestOrigin),
storage::kFileSystemTypeExternal,
storage::kFileSystemTypeNativeLocal,
base::FilePath(DRIVE FPL("/test/sys/root/file"))
.NormalizePathSeparators(),
base::FilePath(FPL("system/root/file")).NormalizePathSeparators(),
"system");
IsolatedContext::GetInstance()->RevokeFileSystem(isolated_id);
ExternalMountPoints::GetSystemInstance()->RevokeFileSystem("system");
}
#endif // !defiend(OS_CHROMEOS)
TEST_F(FileSystemContextTest, FileSystemContextKeepsMountPointsAlive) {
scoped_refptr<ExternalMountPoints> mount_points =
ExternalMountPoints::CreateRefCounted();
// Register system external mount point.
ASSERT_TRUE(mount_points->RegisterFileSystem(
"system",
storage::kFileSystemTypeNativeLocal,
FileSystemMountOption(),
base::FilePath(DRIVE FPL("/test/sys/"))));
scoped_refptr<FileSystemContext> file_system_context(
CreateFileSystemContextForTest(mount_points.get()));
// Release a MountPoints reference created in the test.
mount_points = NULL;
// FileSystemContext should keep a reference to the |mount_points|, so it
// should be able to resolve the URL.
FileSystemURL cracked_external = file_system_context->CrackURL(
CreateRawFileSystemURL("external", "system"));
ExpectFileSystemURLMatches(
cracked_external,
GURL(kTestOrigin),
storage::kFileSystemTypeExternal,
storage::kFileSystemTypeNativeLocal,
base::FilePath(DRIVE FPL("/test/sys/root/file"))
.NormalizePathSeparators(),
base::FilePath(FPL("system/root/file")).NormalizePathSeparators(),
"system");
// No need to revoke the registered filesystem since |mount_points| lifetime
// is bound to this test.
}
TEST_F(FileSystemContextTest, CrackFileSystemURL) {
scoped_refptr<ExternalMountPoints> external_mount_points(
ExternalMountPoints::CreateRefCounted());
scoped_refptr<FileSystemContext> file_system_context(
CreateFileSystemContextForTest(external_mount_points.get()));
// Register an isolated mount point.
std::string isolated_file_system_name = "root";
const std::string kIsolatedFileSystemID =
IsolatedContext::GetInstance()->RegisterFileSystemForPath(
storage::kFileSystemTypeNativeLocal,
std::string(),
base::FilePath(DRIVE FPL("/test/isolated/root")),
&isolated_file_system_name);
// Register system external mount point.
ASSERT_TRUE(ExternalMountPoints::GetSystemInstance()->RegisterFileSystem(
"system",
storage::kFileSystemTypeDrive,
FileSystemMountOption(),
base::FilePath(DRIVE FPL("/test/sys/"))));
ASSERT_TRUE(ExternalMountPoints::GetSystemInstance()->RegisterFileSystem(
"ext",
storage::kFileSystemTypeNativeLocal,
FileSystemMountOption(),
base::FilePath(DRIVE FPL("/test/ext"))));
// Register a system external mount point with the same name/id as the
// registered isolated mount point.
ASSERT_TRUE(ExternalMountPoints::GetSystemInstance()->RegisterFileSystem(
kIsolatedFileSystemID,
storage::kFileSystemTypeRestrictedNativeLocal,
FileSystemMountOption(),
base::FilePath(DRIVE FPL("/test/system/isolated"))));
// Add a mount points with the same name as a system mount point to
// FileSystemContext's external mount points.
ASSERT_TRUE(external_mount_points->RegisterFileSystem(
"ext",
storage::kFileSystemTypeNativeLocal,
FileSystemMountOption(),
base::FilePath(DRIVE FPL("/test/local/ext/"))));
const GURL kTestOrigin = GURL("http://chromium.org/");
const base::FilePath kVirtualPathNoRoot = base::FilePath(FPL("root/file"));
struct TestCase {
// Test case values.
std::string root;
std::string type_str;
// Expected test results.
bool expect_is_valid;
storage::FileSystemType expect_mount_type;
storage::FileSystemType expect_type;
const base::FilePath::CharType* expect_path;
std::string expect_filesystem_id;
};
const TestCase kTestCases[] = {
// Following should not be handled by the url crackers:
{
"pers_mount", "persistent", true /* is_valid */,
storage::kFileSystemTypePersistent, storage::kFileSystemTypePersistent,
FPL("pers_mount/root/file"), std::string() /* filesystem id */
},
{
"temp_mount", "temporary", true /* is_valid */,
storage::kFileSystemTypeTemporary, storage::kFileSystemTypeTemporary,
FPL("temp_mount/root/file"), std::string() /* filesystem id */
},
// Should be cracked by isolated mount points:
{kIsolatedFileSystemID, "isolated", true /* is_valid */,
storage::kFileSystemTypeIsolated, storage::kFileSystemTypeNativeLocal,
DRIVE FPL("/test/isolated/root/file"), kIsolatedFileSystemID},
// Should be cracked by system mount points:
{"system", "external", true /* is_valid */,
storage::kFileSystemTypeExternal, storage::kFileSystemTypeDrive,
DRIVE FPL("/test/sys/root/file"), "system"},
{kIsolatedFileSystemID, "external", true /* is_valid */,
storage::kFileSystemTypeExternal,
storage::kFileSystemTypeRestrictedNativeLocal,
DRIVE FPL("/test/system/isolated/root/file"), kIsolatedFileSystemID},
// Should be cracked by FileSystemContext's ExternalMountPoints.
{"ext", "external", true /* is_valid */, storage::kFileSystemTypeExternal,
storage::kFileSystemTypeNativeLocal,
DRIVE FPL("/test/local/ext/root/file"), "ext"},
// Test for invalid filesystem url (made invalid by adding invalid
// filesystem type).
{"sytem", "external", false /* is_valid */,
// The rest of values will be ignored.
storage::kFileSystemTypeUnknown, storage::kFileSystemTypeUnknown,
FPL(""), std::string()},
// Test for URL with non-existing filesystem id.
{"invalid", "external", false /* is_valid */,
// The rest of values will be ignored.
storage::kFileSystemTypeUnknown, storage::kFileSystemTypeUnknown,
FPL(""), std::string()},
};
for (size_t i = 0; i < arraysize(kTestCases); ++i) {
const base::FilePath virtual_path =
base::FilePath::FromUTF8Unsafe(
kTestCases[i].root).Append(kVirtualPathNoRoot);
GURL raw_url =
CreateRawFileSystemURL(kTestCases[i].type_str, kTestCases[i].root);
FileSystemURL cracked_url = file_system_context->CrackURL(raw_url);
SCOPED_TRACE(testing::Message() << "Test case " << i << ": "
<< "Cracking URL: " << raw_url);
EXPECT_EQ(kTestCases[i].expect_is_valid, cracked_url.is_valid());
if (!kTestCases[i].expect_is_valid)
continue;
ExpectFileSystemURLMatches(
cracked_url,
GURL(kTestOrigin),
kTestCases[i].expect_mount_type,
kTestCases[i].expect_type,
base::FilePath(kTestCases[i].expect_path).NormalizePathSeparators(),
virtual_path.NormalizePathSeparators(),
kTestCases[i].expect_filesystem_id);
}
IsolatedContext::GetInstance()->RevokeFileSystemByPath(
base::FilePath(DRIVE FPL("/test/isolated/root")));
ExternalMountPoints::GetSystemInstance()->RevokeFileSystem("system");
ExternalMountPoints::GetSystemInstance()->RevokeFileSystem("ext");
ExternalMountPoints::GetSystemInstance()->RevokeFileSystem(
kIsolatedFileSystemID);
}
TEST_F(FileSystemContextTest, CanServeURLRequest) {
scoped_refptr<ExternalMountPoints> external_mount_points(
ExternalMountPoints::CreateRefCounted());
scoped_refptr<FileSystemContext> context(
CreateFileSystemContextForTest(external_mount_points.get()));
// A request for a sandbox mount point should be served.
FileSystemURL cracked_url =
context->CrackURL(CreateRawFileSystemURL("persistent", "pers_mount"));
EXPECT_EQ(storage::kFileSystemTypePersistent, cracked_url.mount_type());
EXPECT_TRUE(context->CanServeURLRequest(cracked_url));
// A request for an isolated mount point should NOT be served.
std::string isolated_fs_name = "root";
std::string isolated_fs_id =
IsolatedContext::GetInstance()->RegisterFileSystemForPath(
storage::kFileSystemTypeNativeLocal,
std::string(),
base::FilePath(DRIVE FPL("/test/isolated/root")),
&isolated_fs_name);
cracked_url = context->CrackURL(
CreateRawFileSystemURL("isolated", isolated_fs_id));
EXPECT_EQ(storage::kFileSystemTypeIsolated, cracked_url.mount_type());
EXPECT_FALSE(context->CanServeURLRequest(cracked_url));
// A request for an external mount point should be served.
const std::string kExternalMountName = "ext_mount";
ASSERT_TRUE(ExternalMountPoints::GetSystemInstance()->RegisterFileSystem(
kExternalMountName,
storage::kFileSystemTypeDrive,
FileSystemMountOption(),
base::FilePath()));
cracked_url = context->CrackURL(
CreateRawFileSystemURL("external", kExternalMountName));
EXPECT_EQ(storage::kFileSystemTypeExternal, cracked_url.mount_type());
EXPECT_TRUE(context->CanServeURLRequest(cracked_url));
ExternalMountPoints::GetSystemInstance()->RevokeFileSystem(
kExternalMountName);
IsolatedContext::GetInstance()->RevokeFileSystem(isolated_fs_id);
}
} // namespace
} // namespace content
|