summaryrefslogtreecommitdiffstats
path: root/webkit/fileapi/file_system_path_manager_unittest.cc
diff options
context:
space:
mode:
authorkinuko@chromium.org <kinuko@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-10-15 03:20:03 +0000
committerkinuko@chromium.org <kinuko@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-10-15 03:20:03 +0000
commitf698a7f22b36d056bbbfa74aaf910d8ac3b97c23 (patch)
treec21fbd8499daa68203d23e2e25a7ff16e595bd67 /webkit/fileapi/file_system_path_manager_unittest.cc
parent95249b6e92c3ac6742139f948028d7f3ec5b9dc6 (diff)
downloadchromium_src-f698a7f22b36d056bbbfa74aaf910d8ac3b97c23.zip
chromium_src-f698a7f22b36d056bbbfa74aaf910d8ac3b97c23.tar.gz
chromium_src-f698a7f22b36d056bbbfa74aaf910d8ac3b97c23.tar.bz2
Hide the FileSystem directory under 'unpredictable' location (part 2)
BUG=58361 TEST=FileSystemPathManager.* Review URL: http://codereview.chromium.org/3724001 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@62698 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'webkit/fileapi/file_system_path_manager_unittest.cc')
-rw-r--r--webkit/fileapi/file_system_path_manager_unittest.cc318
1 files changed, 268 insertions, 50 deletions
diff --git a/webkit/fileapi/file_system_path_manager_unittest.cc b/webkit/fileapi/file_system_path_manager_unittest.cc
index 1e67c3a..1bb39f3 100644
--- a/webkit/fileapi/file_system_path_manager_unittest.cc
+++ b/webkit/fileapi/file_system_path_manager_unittest.cc
@@ -5,13 +5,16 @@
#include "webkit/fileapi/file_system_path_manager.h"
#include "base/basictypes.h"
-#include "base/file_path.h"
+#include "base/file_util.h"
+#include "base/message_loop.h"
+#include "base/ref_counted.h"
+#include "base/scoped_callback_factory.h"
#include "base/scoped_ptr.h"
#include "base/scoped_temp_dir.h"
#include "googleurl/src/gurl.h"
#include "testing/gtest/include/gtest/gtest.h"
-using fileapi::FileSystemPathManager;
+using namespace fileapi;
namespace {
@@ -22,27 +25,44 @@ namespace {
#define PS "/"
#endif
-const FilePath::CharType kTestDataPath[] = FILE_PATH_LITERAL(
- "//tmp/TestingProfilePath");
+struct RootPathTestCase {
+ fileapi::FileSystemType type;
+ const char* origin_url;
+ const char* expected_path;
+};
const struct RootPathTest {
fileapi::FileSystemType type;
- bool off_the_record;
const char* origin_url;
- bool expect_root_path;
const char* expected_path;
} kRootPathTestCases[] = {
- { fileapi::kFileSystemTypeTemporary, false, "http://host:1/",
- true, "FileSystem" PS "http_host_1" PS "Temporary" },
- { fileapi::kFileSystemTypePersistent, false, "http://host:2/",
- true, "FileSystem" PS "http_host_2" PS "Persistent" },
- { fileapi::kFileSystemTypeTemporary, true, "http://host:3/",
- false, "" },
- { fileapi::kFileSystemTypePersistent, true, "http://host:4/",
- false, "" },
- // We disallow file:// URIs to access filesystem.
- { fileapi::kFileSystemTypeTemporary, false, "file:///some/path", false, "" },
- { fileapi::kFileSystemTypePersistent, false, "file:///some/path", false, "" },
+ { fileapi::kFileSystemTypeTemporary, "http://foo:1/",
+ "http_foo_1" PS "Temporary" },
+ { fileapi::kFileSystemTypePersistent, "http://foo:1/",
+ "http_foo_1" PS "Persistent" },
+ { fileapi::kFileSystemTypeTemporary, "http://bar.com/",
+ "http_bar.com_0" PS "Temporary" },
+ { fileapi::kFileSystemTypePersistent, "http://bar.com/",
+ "http_bar.com_0" PS "Persistent" },
+ { fileapi::kFileSystemTypeTemporary, "https://foo:2/",
+ "https_foo_2" PS "Temporary" },
+ { fileapi::kFileSystemTypePersistent, "https://foo:2/",
+ "https_foo_2" PS "Persistent" },
+ { fileapi::kFileSystemTypeTemporary, "https://bar.com/",
+ "https_bar.com_0" PS "Temporary" },
+ { fileapi::kFileSystemTypePersistent, "https://bar.com/",
+ "https_bar.com_0" PS "Persistent" },
+};
+
+const struct RootPathFileURITest {
+ fileapi::FileSystemType type;
+ const char* origin_url;
+ const char* expected_path;
+} kRootPathFileURITestCases[] = {
+ { fileapi::kFileSystemTypeTemporary, "file:///",
+ "file__0" PS "Temporary" },
+ { fileapi::kFileSystemTypePersistent, "file:///",
+ "file__0" PS "Persistent" },
};
const struct CheckValidPathTest {
@@ -59,6 +79,13 @@ const struct CheckValidPathTest {
{ FILE_PATH_LITERAL("a/b/../c/.."), false, },
};
+const char* const kPathToVirtualPathTestCases[] = {
+ "",
+ "a",
+ "a" PS "b",
+ "a" PS "b" PS "c",
+};
+
const struct IsRestrictedNameTest {
FilePath::StringType name;
bool expected_dangerous;
@@ -132,58 +159,249 @@ const struct IsRestrictedNameTest {
class FileSystemPathManagerTest : public testing::Test {
public:
FileSystemPathManagerTest()
- : data_path_(kTestDataPath) {
+ : callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
+ }
+
+ void SetUp() {
+ data_dir_.reset(new ScopedTempDir);
+ data_dir_->CreateUniqueTempDir();
+ ASSERT_TRUE(data_dir_->IsValid());
+ root_path_callback_status_ = false;
+ root_path_.clear();
+ file_system_name_.clear();
+ }
+
+ protected:
+ FileSystemPathManager* NewPathManager(
+ bool incognito,
+ bool allow_file_access) {
+ return new FileSystemPathManager(
+ base::MessageLoopProxy::CreateForCurrentThread(),
+ data_dir_->path(), incognito, allow_file_access);
+ }
+
+ void OnGetRootPath(bool success,
+ const FilePath& root_path,
+ const std::string& name) {
+ root_path_callback_status_ = success;
+ root_path_ = root_path;
+ file_system_name_ = name;
}
- FileSystemPathManager* GetNewPathManager(bool incognito) {
- path_manager_.reset(new FileSystemPathManager(
- data_path_, incognito, false /* allow_file_access */));
- return path_manager_.get();
+ bool GetRootPath(FileSystemPathManager* manager,
+ const GURL& origin_url,
+ fileapi::FileSystemType type,
+ bool create,
+ FilePath* root_path) {
+ manager->GetFileSystemRootPath(origin_url, type, create,
+ callback_factory_.NewCallback(
+ &FileSystemPathManagerTest::OnGetRootPath));
+ MessageLoop::current()->RunAllPending();
+ if (root_path)
+ *root_path = root_path_;
+ return root_path_callback_status_;
+ }
+
+ bool CheckValidFileSystemPath(FileSystemPathManager* manager,
+ const FilePath& path) {
+ return manager->CrackFileSystemPath(path, NULL, NULL, NULL);
+ }
+
+ FilePath data_path() { return data_dir_->path(); }
+ FilePath file_system_path() {
+ return data_dir_->path().Append(
+ FileSystemPathManager::kFileSystemDirectory);
}
private:
- FilePath data_path_;
- scoped_ptr<FileSystemPathManager> path_manager_;
+ scoped_ptr<ScopedTempDir> data_dir_;
+ base::ScopedCallbackFactory<FileSystemPathManagerTest> callback_factory_;
+
+ bool root_path_callback_status_;
+ FilePath root_path_;
+ std::string file_system_name_;
+
+ DISALLOW_COPY_AND_ASSIGN(FileSystemPathManagerTest);
};
-TEST_F(FileSystemPathManagerTest, GetRootPath) {
+TEST_F(FileSystemPathManagerTest, GetRootPathCreateAndExamine) {
+ std::vector<FilePath> returned_root_path(
+ ARRAYSIZE_UNSAFE(kRootPathTestCases));
+ scoped_ptr<FileSystemPathManager> manager(NewPathManager(false, false));
+
+ // Create a new root directory.
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathTestCases); ++i) {
- SCOPED_TRACE(testing::Message() << "RootPath #" << i << " "
+ SCOPED_TRACE(testing::Message() << "RootPath (create) #" << i << " "
<< kRootPathTestCases[i].expected_path);
- FileSystemPathManager* manager = GetNewPathManager(
- kRootPathTestCases[i].off_the_record);
+ FilePath root_path;
+ EXPECT_TRUE(GetRootPath(manager.get(),
+ GURL(kRootPathTestCases[i].origin_url),
+ kRootPathTestCases[i].type,
+ true /* create */, &root_path));
+
+ FilePath expected = file_system_path().AppendASCII(
+ kRootPathTestCases[i].expected_path);
+ EXPECT_EQ(expected.value(), root_path.DirName().value());
+ EXPECT_TRUE(file_util::DirectoryExists(root_path));
+ ASSERT_TRUE(returned_root_path.size() > i);
+ returned_root_path[i] = root_path;
+ }
+
+ // Get the root directory with create=false and see if we get the
+ // same directory.
+ for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathTestCases); ++i) {
+ SCOPED_TRACE(testing::Message() << "RootPath (get) #" << i << " "
+ << kRootPathTestCases[i].expected_path);
FilePath root_path;
- bool result = manager->GetFileSystemRootPath(
- GURL(kRootPathTestCases[i].origin_url),
- kRootPathTestCases[i].type,
- &root_path, NULL);
- EXPECT_EQ(kRootPathTestCases[i].expect_root_path, result);
- if (result) {
- FilePath expected = FilePath(kTestDataPath).AppendASCII(
- kRootPathTestCases[i].expected_path);
- EXPECT_EQ(expected.value(), root_path.value());
- }
+ EXPECT_TRUE(GetRootPath(manager.get(),
+ GURL(kRootPathTestCases[i].origin_url),
+ kRootPathTestCases[i].type,
+ false /* create */, &root_path));
+ ASSERT_TRUE(returned_root_path.size() > i);
+ EXPECT_EQ(returned_root_path[i].value(), root_path.value());
}
}
+TEST_F(FileSystemPathManagerTest, GetRootPathCreateAndExamineWithNewManager) {
+ std::vector<FilePath> returned_root_path(
+ ARRAYSIZE_UNSAFE(kRootPathTestCases));
+ scoped_ptr<FileSystemPathManager> manager1(NewPathManager(false, false));
+ scoped_ptr<FileSystemPathManager> manager2(NewPathManager(false, false));
+
+ GURL origin_url("http://foo.com:1/");
+
+ FilePath root_path1;
+ EXPECT_TRUE(GetRootPath(manager1.get(), origin_url,
+ kFileSystemTypeTemporary, true, &root_path1));
+ FilePath root_path2;
+ EXPECT_TRUE(GetRootPath(manager2.get(), origin_url,
+ kFileSystemTypeTemporary, false, &root_path2));
+
+ EXPECT_EQ(root_path1.value(), root_path2.value());
+}
+
+TEST_F(FileSystemPathManagerTest, GetRootPathGetWithoutCreate) {
+ scoped_ptr<FileSystemPathManager> manager(NewPathManager(false, false));
+
+ // Try to get a root directory without creating.
+ for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathTestCases); ++i) {
+ SCOPED_TRACE(testing::Message() << "RootPath (create=false) #" << i << " "
+ << kRootPathTestCases[i].expected_path);
+ EXPECT_FALSE(GetRootPath(manager.get(),
+ GURL(kRootPathTestCases[i].origin_url),
+ kRootPathTestCases[i].type,
+ false /* create */, NULL));
+ }
+}
+
+TEST_F(FileSystemPathManagerTest, GetRootPathInIncognito) {
+ scoped_ptr<FileSystemPathManager> manager(NewPathManager(
+ true /* incognito */, false));
+
+ // Try to get a root directory.
+ for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathTestCases); ++i) {
+ SCOPED_TRACE(testing::Message() << "RootPath (incognito) #" << i << " "
+ << kRootPathTestCases[i].expected_path);
+ EXPECT_FALSE(GetRootPath(manager.get(),
+ GURL(kRootPathTestCases[i].origin_url),
+ kRootPathTestCases[i].type,
+ true /* create */, NULL));
+ }
+}
+
+TEST_F(FileSystemPathManagerTest, GetRootPathFileURI) {
+ scoped_ptr<FileSystemPathManager> manager(NewPathManager(false, false));
+ for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathFileURITestCases); ++i) {
+ SCOPED_TRACE(testing::Message() << "RootPathFileURI (disallow) #"
+ << i << " " << kRootPathFileURITestCases[i].expected_path);
+ EXPECT_FALSE(GetRootPath(manager.get(),
+ GURL(kRootPathFileURITestCases[i].origin_url),
+ kRootPathFileURITestCases[i].type,
+ true /* create */, NULL));
+ }
+}
+
+TEST_F(FileSystemPathManagerTest, GetRootPathFileURIWithAllowFlag) {
+ scoped_ptr<FileSystemPathManager> manager(NewPathManager(
+ false, true /* allow_file_access_from_files */));
+ for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathFileURITestCases); ++i) {
+ SCOPED_TRACE(testing::Message() << "RootPathFileURI (allow) #"
+ << i << " " << kRootPathFileURITestCases[i].expected_path);
+ FilePath root_path;
+ EXPECT_TRUE(GetRootPath(manager.get(),
+ GURL(kRootPathFileURITestCases[i].origin_url),
+ kRootPathFileURITestCases[i].type,
+ true /* create */, &root_path));
+ FilePath expected = file_system_path().AppendASCII(
+ kRootPathFileURITestCases[i].expected_path);
+ EXPECT_EQ(expected.value(), root_path.DirName().value());
+ EXPECT_TRUE(file_util::DirectoryExists(root_path));
+ }
+}
+
+TEST_F(FileSystemPathManagerTest, VirtualPathFromFileSystemPathTest) {
+ scoped_ptr<FileSystemPathManager> manager(NewPathManager(false, false));
+ FilePath root_path;
+ EXPECT_TRUE(GetRootPath(manager.get(), GURL("http://foo.com/"),
+ fileapi::kFileSystemTypeTemporary,
+ true /* create */, &root_path));
+
+ for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kPathToVirtualPathTestCases); ++i) {
+ SCOPED_TRACE(testing::Message() << "PathToVirtualPath #"
+ << i << " " << kPathToVirtualPathTestCases[i]);
+ FilePath absolute_path = root_path.AppendASCII(
+ kPathToVirtualPathTestCases[i]);
+ FilePath virtual_path;
+ EXPECT_TRUE(manager->CrackFileSystemPath(absolute_path, NULL, NULL,
+ &virtual_path));
+
+ FilePath test_case_path;
+ test_case_path = test_case_path.AppendASCII(
+ kPathToVirtualPathTestCases[i]);
+ EXPECT_EQ(test_case_path.value(), virtual_path.value());
+ }
+}
+
+TEST_F(FileSystemPathManagerTest, TypeFromFileSystemPathTest) {
+ scoped_ptr<FileSystemPathManager> manager(NewPathManager(false, false));
+
+ FilePath root_path;
+ fileapi::FileSystemType type;
+
+ EXPECT_TRUE(GetRootPath(manager.get(), GURL("http://foo.com/"),
+ fileapi::kFileSystemTypeTemporary,
+ true /* create */, &root_path));
+ FilePath path = root_path.AppendASCII("test");
+ EXPECT_TRUE(manager->CrackFileSystemPath(path, NULL, &type, NULL));
+ EXPECT_EQ(fileapi::kFileSystemTypeTemporary, type);
+
+ EXPECT_TRUE(GetRootPath(manager.get(), GURL("http://foo.com/"),
+ fileapi::kFileSystemTypePersistent,
+ true /* create */, &root_path));
+ path = root_path.AppendASCII("test");
+ EXPECT_TRUE(manager->CrackFileSystemPath(path, NULL, &type, NULL));
+ EXPECT_EQ(fileapi::kFileSystemTypePersistent, type);
+}
+
TEST_F(FileSystemPathManagerTest, CheckValidPath) {
- FileSystemPathManager* manager = GetNewPathManager(false);
+ scoped_ptr<FileSystemPathManager> manager(NewPathManager(false, false));
FilePath root_path;
- EXPECT_TRUE(manager->GetFileSystemRootPath(
- GURL("http://foo.com/"), fileapi::kFileSystemTypePersistent,
- &root_path, NULL));
+ EXPECT_TRUE(GetRootPath(manager.get(), GURL("http://foo.com/"),
+ kFileSystemTypePersistent, true, &root_path));
// The root path must be valid, but upper directories or directories
// that are not in our temporary or persistent directory must be
// evaluated invalid.
- EXPECT_TRUE(manager->CheckValidFileSystemPath(root_path));
- EXPECT_FALSE(manager->CheckValidFileSystemPath(root_path.DirName()));
- EXPECT_FALSE(manager->CheckValidFileSystemPath(
- root_path.DirName().DirName()));
- EXPECT_FALSE(manager->CheckValidFileSystemPath(
- root_path.DirName().AppendASCII("ArbitraryName")));
+ EXPECT_TRUE(CheckValidFileSystemPath(manager.get(), root_path));
+ EXPECT_FALSE(CheckValidFileSystemPath(manager.get(), root_path.DirName()));
+ EXPECT_FALSE(CheckValidFileSystemPath(manager.get(),
+ root_path.DirName().DirName()));
+ EXPECT_FALSE(CheckValidFileSystemPath(manager.get(),
+ root_path.DirName().DirName()
+ .AppendASCII("ArbitraryName")
+ .AppendASCII("chrome-dummy")));
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kCheckValidPathTestCases); ++i) {
SCOPED_TRACE(testing::Message() << "CheckValidPath #" << i << " "
@@ -192,12 +410,12 @@ TEST_F(FileSystemPathManagerTest, CheckValidPath) {
if (!path.IsAbsolute())
path = root_path.Append(path);
EXPECT_EQ(kCheckValidPathTestCases[i].expected_valid,
- manager->CheckValidFileSystemPath(path));
+ CheckValidFileSystemPath(manager.get(), path));
}
}
TEST_F(FileSystemPathManagerTest, IsRestrictedName) {
- FileSystemPathManager* manager = GetNewPathManager(false);
+ scoped_ptr<FileSystemPathManager> manager(NewPathManager(false, false));
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kIsRestrictedNameTestCases); ++i) {
SCOPED_TRACE(testing::Message() << "IsRestrictedName #" << i << " "
<< kIsRestrictedNameTestCases[i].name);