Chromium Code Reviews| Index: webkit/fileapi/file_system_quota_unittest.cc |
| diff --git a/webkit/fileapi/file_system_quota_unittest.cc b/webkit/fileapi/file_system_quota_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..90d066208a34c4ab8907d97c44324d9759ee391c |
| --- /dev/null |
| +++ b/webkit/fileapi/file_system_quota_unittest.cc |
| @@ -0,0 +1,463 @@ |
| +// Copyright (c) 2011 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 "webkit/fileapi/file_system_operation.h" |
| + |
| +#include "base/file_util.h" |
| +#include "base/logging.h" |
| +#include "base/memory/scoped_callback_factory.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/memory/scoped_temp_dir.h" |
| +#include "base/message_loop.h" |
| +#include "base/platform_file.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "webkit/fileapi/file_system_callback_dispatcher.h" |
| +#include "webkit/fileapi/file_system_context.h" |
| +#include "webkit/fileapi/file_system_file_util.h" |
| +#include "webkit/fileapi/file_system_mount_point_provider.h" |
| +#include "webkit/fileapi/file_system_operation.h" |
| +#include "webkit/fileapi/file_system_path_manager.h" |
| +#include "webkit/fileapi/file_system_usage_cache.h" |
| +#include "webkit/fileapi/file_system_util.h" |
| +#include "webkit/fileapi/local_file_system_file_util.h" |
| +#include "webkit/fileapi/quota_file_util.h" |
| +#include "webkit/fileapi/sandbox_mount_point_provider.h" |
| +#include "webkit/quota/quota_manager.h" |
| + |
| +namespace fileapi { |
| + |
| +const int kFileOperationStatusNotSet = 1; |
| + |
| +class FileSystemQuotaTest : public testing::Test { |
| + public: |
| + FileSystemQuotaTest() |
| + : status_(kFileOperationStatusNotSet), |
| + quota_status_(quota::kQuotaStatusUnknown), |
| + usage_(-1), |
| + quota_(-1), |
| + callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {} |
| + |
| + FileSystemOperation* operation(); |
| + |
| + void set_local_path(const FilePath& path) { local_path_ = path; } |
| + const FilePath& local_path() const { return local_path_; } |
| + void set_status(int status) { status_ = status; } |
| + int status() const { return status_; } |
| + void set_info(const base::PlatformFileInfo& info) { info_ = info; } |
| + const base::PlatformFileInfo& info() const { return info_; } |
| + void set_path(const FilePath& path) { path_ = path; } |
| + const FilePath& path() const { return path_; } |
| + void set_entries(const std::vector<base::FileUtilProxy::Entry>& entries) { |
| + entries_ = entries; |
| + } |
| + const std::vector<base::FileUtilProxy::Entry>& entries() const { |
| + return entries_; |
| + } |
| + |
| + virtual void SetUp(); |
| + virtual void TearDown(); |
| + |
| + void OnGetUsageAndQuota( |
| + quota::QuotaStatusCode status, int64 usage, int64 quota); |
| + |
| + protected: |
| + base::PlatformFile OpenFile(const FilePath& virtual_path); |
| + void PrepareFileSet(const FilePath& virtual_path); |
| + |
| + GURL URLForPath(const FilePath& path) const { |
| + // Only the path will actually get used. |
| + return GURL(GetFileSystemRootURI(GURL("http://www.example.com/"), |
| + kFileSystemTypeTemporary).spec() + path.MaybeAsASCII()); |
| + } |
| + |
| + FilePath PlatformPath(FilePath virtual_path) { |
| + return root_dir_.Append(virtual_path); |
| + } |
| + |
| + bool VirtualFileExists(FilePath virtual_path) { |
| + return file_util::PathExists(PlatformPath(virtual_path)) && |
| + !file_util::DirectoryExists(PlatformPath(virtual_path)); |
| + } |
| + |
| + bool VirtualDirectoryExists(FilePath virtual_path) { |
| + return file_util::DirectoryExists(PlatformPath(virtual_path)); |
| + } |
| + |
| + FilePath CreateVirtualDirectory(const char* virtual_path_string) { |
| + FilePath virtual_path(virtual_path_string); |
| + file_util::CreateDirectory(PlatformPath(virtual_path)); |
| + return virtual_path; |
| + } |
| + |
| + FilePath CreateVirtualDirectoryInDir(const char* virtual_path_string, |
| + const FilePath& virtual_dir_path) { |
| + FilePath virtual_path(virtual_dir_path.AppendASCII(virtual_path_string)); |
| + file_util::CreateDirectory(PlatformPath(virtual_path)); |
| + return virtual_path; |
| + } |
| + |
| + FilePath CreateVirtualTemporaryFileInDir(const FilePath& virtual_dir_path) { |
| + FilePath absolute_dir_path(PlatformPath(virtual_dir_path)); |
| + FilePath absolute_file_path; |
| + if (file_util::CreateTemporaryFileInDir(absolute_dir_path, |
| + &absolute_file_path)) |
| + return virtual_dir_path.Append(absolute_file_path.BaseName()); |
| + else |
| + return FilePath(); |
| + } |
| + |
| + FilePath CreateVirtualTemporaryDirInDir(const FilePath& virtual_dir_path, |
| + const FilePath::StringType& prefix) { |
| + FilePath absolute_parent_dir_path(PlatformPath(virtual_dir_path)); |
| + FilePath absolute_child_dir_path; |
| + if (file_util::CreateTemporaryDirInDir(absolute_parent_dir_path, prefix, |
| + &absolute_child_dir_path)) |
| + return virtual_dir_path.Append(absolute_child_dir_path.BaseName()); |
| + else |
| + return FilePath(); |
| + } |
| + |
| + FilePath CreateVirtualTemporaryDir(const FilePath::StringType& prefix) { |
| + return CreateVirtualTemporaryDirInDir(FilePath(), prefix); |
| + } |
| + |
| + ScopedTempDir base_dir_; |
| + scoped_refptr<FileSystemContext> file_system_context_; |
| + scoped_refptr<quota::QuotaManager> quota_manager_; |
| + scoped_ptr<FileSystemPathManager> path_manager_; |
| + |
| + FilePath child_dir_path_; |
| + FilePath child_file1_path_; |
| + FilePath child_file2_path_; |
| + FilePath grandchild_file1_path_; |
| + FilePath grandchild_file2_path_; |
| + base::PlatformFile child_file1_; |
| + base::PlatformFile child_file2_; |
| + base::PlatformFile grandchild_file1_; |
| + base::PlatformFile grandchild_file2_; |
| + |
| + // For post-operation status. |
| + int status_; |
| + quota::QuotaStatusCode quota_status_; |
| + int64 usage_; |
| + int64 quota_; |
| + base::PlatformFileInfo info_; |
| + FilePath path_; |
| + FilePath local_path_; |
| + FilePath filesystem_dir_; |
| + FilePath root_dir_; |
| + std::vector<base::FileUtilProxy::Entry> entries_; |
| + |
| + base::ScopedCallbackFactory<FileSystemQuotaTest> callback_factory_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(FileSystemQuotaTest); |
| +}; |
| + |
| +namespace { |
| + |
| +class MockDispatcher : public FileSystemCallbackDispatcher { |
| + public: |
| + MockDispatcher(FileSystemQuotaTest* test) : test_(test) { } |
| + |
| + virtual void DidFail(base::PlatformFileError status) { |
| + test_->set_status(status); |
| + } |
| + |
| + virtual void DidSucceed() { |
| + test_->set_status(base::PLATFORM_FILE_OK); |
| + } |
| + |
| + virtual void DidGetLocalPath(const FilePath& local_path) { |
| + test_->set_local_path(local_path); |
| + test_->set_status(base::PLATFORM_FILE_OK); |
| + } |
| + |
| + virtual void DidReadMetadata( |
| + const base::PlatformFileInfo& info, |
| + const FilePath& platform_path) { |
| + test_->set_info(info); |
| + test_->set_path(platform_path); |
| + test_->set_status(base::PLATFORM_FILE_OK); |
| + } |
| + |
| + virtual void DidReadDirectory( |
| + const std::vector<base::FileUtilProxy::Entry>& entries, |
| + bool /* has_more */) { |
| + test_->set_entries(entries); |
| + } |
| + |
| + virtual void DidOpenFileSystem(const std::string&, const GURL&) { |
| + NOTREACHED(); |
|
Paweł Hajdan Jr.
2011/04/29 09:21:51
nit: Did you mean FAIL/ADD_FAILURE here and below?
Dai Mikurube (NOT FULLTIME)
2011/05/10 08:58:22
Modified to ADD_FAILURE().
|
| + } |
| + |
| + virtual void DidWrite(int64 bytes, bool complete) { |
| + NOTREACHED(); |
| + } |
| + |
| + private: |
| + FileSystemQuotaTest* test_; |
| +}; |
| + |
| +} // namespace (anonymous) |
| + |
| +void FileSystemQuotaTest::SetUp() { |
| + ASSERT_TRUE(base_dir_.CreateUniqueTempDir()); |
| + filesystem_dir_ = base_dir_.path().AppendASCII("filesystem"); |
| + file_util::CreateDirectory(filesystem_dir_); |
| + |
| + path_manager_.reset(new FileSystemPathManager( |
| + base::MessageLoopProxy::CreateForCurrentThread(), |
| + filesystem_dir_, NULL, false, true)); |
| + |
| + quota_manager_ = new quota::QuotaManager( |
| + false /* is_incognito */, |
| + filesystem_dir_, |
| + base::MessageLoopProxy::CreateForCurrentThread(), |
| + base::MessageLoopProxy::CreateForCurrentThread()); |
| + |
| + file_system_context_ = new FileSystemContext( |
| + base::MessageLoopProxy::CreateForCurrentThread(), |
| + base::MessageLoopProxy::CreateForCurrentThread(), |
| + NULL, quota_manager_, filesystem_dir_, |
| + false /* is_incognito */, true, false, |
| + path_manager_.get()); |
| + |
| + // Creates directory including "chrome-*". |
| + root_dir_ = path_manager_->sandbox_provider()-> |
| + ValidateFileSystemRootAndGetPathOnFileThread( |
| + GURL("http://www.example.com"), kFileSystemTypeTemporary, |
| + FilePath(), true); |
| +} |
| + |
| +void FileSystemQuotaTest::TearDown() { |
| + file_system_context_ = NULL; |
| + quota_manager_ = NULL; |
| + path_manager_.reset(NULL); |
| +} |
| + |
| +FileSystemOperation* FileSystemQuotaTest::operation() { |
| + FileSystemOperation* operation = new FileSystemOperation( |
| + new MockDispatcher(this), |
| + base::MessageLoopProxy::CreateForCurrentThread(), |
| + file_system_context_, |
| + LocalFileSystemFileUtil::GetInstance()); |
| + operation->file_system_operation_context()->set_src_type( |
| + kFileSystemTypeTemporary); |
| + operation->file_system_operation_context()->set_dest_type( |
| + kFileSystemTypeTemporary); |
| + GURL origin_url("http://www.example.com/"); |
| + operation->file_system_operation_context()->set_src_origin_url(origin_url); |
| + operation->file_system_operation_context()->set_dest_origin_url(origin_url); |
| + |
| + return operation; |
| +} |
| + |
| +base::PlatformFile FileSystemQuotaTest::OpenFile(const FilePath& virtual_path) { |
| + base::PlatformFile file; |
| + bool created; |
| + base::PlatformFileError error_code; |
| + file = base::CreatePlatformFile( |
| + PlatformPath(virtual_path), |
| + base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE | |
| + base::PLATFORM_FILE_ASYNC, &created, &error_code); |
| + //ASSERT_EQ(base::PLATFORM_FILE_OK, error_code); |
| + return file; |
| +} |
| + |
| +void FileSystemQuotaTest::OnGetUsageAndQuota( |
| + quota::QuotaStatusCode status, int64 usage, int64 quota) { |
| + quota_status_ = status; |
| + usage_ = usage; |
| + quota_ = quota; |
| +} |
| + |
| +void FileSystemQuotaTest::PrepareFileSet(const FilePath& virtual_path) { |
| + child_dir_path_ = CreateVirtualTemporaryDirInDir(virtual_path, "prefix"); |
| + child_file1_path_ = CreateVirtualTemporaryFileInDir(virtual_path); |
| + child_file1_ = OpenFile(child_file1_path_); |
| + child_file2_path_ = CreateVirtualTemporaryFileInDir(virtual_path); |
| + child_file2_ = OpenFile(child_file2_path_); |
| + grandchild_file1_path_ = CreateVirtualTemporaryFileInDir(child_dir_path_); |
| + grandchild_file1_ = OpenFile(grandchild_file1_path_); |
| + grandchild_file2_path_ = CreateVirtualTemporaryFileInDir(child_dir_path_); |
| + grandchild_file2_ = OpenFile(grandchild_file2_path_); |
| +} |
| + |
| +TEST_F(FileSystemQuotaTest, TestMoveSuccessSrcDirRecursive) { |
| + FilePath src_dir_path(CreateVirtualTemporaryDir("")); |
| + PrepareFileSet(src_dir_path); |
| + FilePath dest_dir_path(CreateVirtualTemporaryDir("")); |
| + |
| + ASSERT_TRUE(base::TruncatePlatformFile(child_file1_, 1013)); |
| + ASSERT_TRUE(base::TruncatePlatformFile(child_file2_, 129)); |
| + ASSERT_TRUE(base::TruncatePlatformFile(grandchild_file1_, 94)); |
| + ASSERT_TRUE(base::TruncatePlatformFile(grandchild_file2_, 517)); |
| + |
| + EXPECT_EQ(1013+129+94+517, |
| + file_util::ComputeDirectorySize(PlatformPath(src_dir_path))); |
| + |
| + quota_manager_->GetUsageAndQuota( |
| + GURL("http://www.example.com"), quota::kStorageTypeTemporary, |
| + callback_factory_.NewCallback(&FileSystemQuotaTest::OnGetUsageAndQuota)); |
| + MessageLoop::current()->RunAllPending(); |
| + EXPECT_EQ(1013+129+94+517 + FileSystemUsageCache::kUsageFileSize, usage_); |
| + |
| + operation()->Move(URLForPath(src_dir_path), URLForPath(dest_dir_path)); |
| + MessageLoop::current()->RunAllPending(); |
| + |
| + EXPECT_EQ(base::PLATFORM_FILE_OK, status()); |
| + EXPECT_TRUE(VirtualDirectoryExists(dest_dir_path.Append( |
| + child_dir_path_.BaseName()))); |
| + EXPECT_TRUE(VirtualFileExists(dest_dir_path.Append( |
| + child_dir_path_.BaseName()).Append( |
| + grandchild_file1_path_.BaseName()))); |
| + |
| + quota_manager_->GetUsageAndQuota( |
| + GURL("http://www.example.com"), quota::kStorageTypeTemporary, |
| + callback_factory_.NewCallback(&FileSystemQuotaTest::OnGetUsageAndQuota)); |
| + MessageLoop::current()->RunAllPending(); |
| + EXPECT_EQ(1013+129+94+517 + FileSystemUsageCache::kUsageFileSize, usage_); |
| +} |
| + |
| +TEST_F(FileSystemQuotaTest, TestCopySuccessSrcDirRecursive) { |
| + FilePath src_dir_path(CreateVirtualTemporaryDir("")); |
| + PrepareFileSet(src_dir_path); |
| + FilePath dest_dir_path(CreateVirtualTemporaryDir("")); |
| + |
| + ASSERT_TRUE(base::TruncatePlatformFile(child_file1_, 12)); |
| + ASSERT_TRUE(base::TruncatePlatformFile(child_file2_, 23)); |
| + ASSERT_TRUE(base::TruncatePlatformFile(grandchild_file1_, 34)); |
| + ASSERT_TRUE(base::TruncatePlatformFile(grandchild_file2_, 45)); |
| + |
| + EXPECT_EQ(12+23+34+45, |
| + file_util::ComputeDirectorySize(PlatformPath(src_dir_path))); |
| + |
| + quota_manager_->GetUsageAndQuota( |
| + GURL("http://www.example.com"), quota::kStorageTypeTemporary, |
| + callback_factory_.NewCallback(&FileSystemQuotaTest::OnGetUsageAndQuota)); |
| + MessageLoop::current()->RunAllPending(); |
| + EXPECT_EQ(12+23+34+45 + FileSystemUsageCache::kUsageFileSize, usage_); |
| + |
| + operation()->Copy(URLForPath(src_dir_path), URLForPath(dest_dir_path)); |
| + MessageLoop::current()->RunAllPending(); |
| + |
| + EXPECT_EQ(base::PLATFORM_FILE_OK, status()); |
| + EXPECT_TRUE(VirtualDirectoryExists(src_dir_path.Append( |
| + child_dir_path_.BaseName()))); |
| + EXPECT_TRUE(VirtualFileExists(src_dir_path.Append( |
| + child_dir_path_.BaseName()).Append( |
| + grandchild_file1_path_.BaseName()))); |
| + EXPECT_EQ(base::PLATFORM_FILE_OK, status()); |
| + EXPECT_TRUE(VirtualDirectoryExists(dest_dir_path.Append( |
| + child_dir_path_.BaseName()))); |
| + EXPECT_TRUE(VirtualFileExists(dest_dir_path.Append( |
| + child_dir_path_.BaseName()).Append( |
| + grandchild_file1_path_.BaseName()))); |
| + |
| + // TODO(dmikurube): These checks still fail... Notifying is required. |
| + // we have to check : |
| + // 1. The actual size of files on disk |
| + // 2. The described size in .usage |
| + // 3. The result of QuotaManager::GetUsageAndQuota |
| + // are the same number. |
| + EXPECT_EQ(2*(12+23+34+45) + FileSystemUsageCache::kUsageFileSize, |
| + file_util::ComputeDirectorySize(PlatformPath(FilePath()))); |
| + |
| + quota_manager_->GetUsageAndQuota( |
| + GURL("http://www.example.com"), quota::kStorageTypeTemporary, |
| + callback_factory_.NewCallback(&FileSystemQuotaTest::OnGetUsageAndQuota)); |
| + MessageLoop::current()->RunAllPending(); |
| + EXPECT_EQ(2*(12+23+34+45) + FileSystemUsageCache::kUsageFileSize, usage_); |
| + |
| + operation()->Copy(URLForPath(child_dir_path_), URLForPath(dest_dir_path)); |
| + MessageLoop::current()->RunAllPending(); |
| + |
| + quota_manager_->GetUsageAndQuota( |
| + GURL("http://www.example.com"), quota::kStorageTypeTemporary, |
| + callback_factory_.NewCallback(&FileSystemQuotaTest::OnGetUsageAndQuota)); |
| + MessageLoop::current()->RunAllPending(); |
| + EXPECT_EQ(2*(12+23)+3*(34+45) + FileSystemUsageCache::kUsageFileSize, usage_); |
| +} |
| + |
| +#if 0 |
|
Paweł Hajdan Jr.
2011/04/29 09:21:51
A general tip (I assume you will update it before
Dai Mikurube (NOT FULLTIME)
2011/05/10 08:58:22
Yes, I uploaded this patch just to share the curre
|
| +TEST_F(FileSystemQuotaTest, TestRemoveSuccess) { |
| + FilePath empty_dir_path(CreateVirtualTemporaryDir("")); |
| + EXPECT_TRUE(VirtualDirectoryExists(empty_dir_path)); |
| + |
| + operation()->Remove(URLForPath(empty_dir_path), false /* recursive */); |
| + MessageLoop::current()->RunAllPending(); |
| + EXPECT_EQ(base::PLATFORM_FILE_OK, status()); |
| + EXPECT_FALSE(VirtualDirectoryExists(empty_dir_path)); |
| + |
| + // Removing a non-empty directory with recursive flag == true should be ok. |
| + // parent_dir |
| + // | | |
| + // child_dir child_file |
| + // Verify deleting parent_dir. |
| + FilePath parent_dir_path(CreateVirtualTemporaryDir("")); |
| + FilePath child_file_path(CreateVirtualTemporaryFileInDir(parent_dir_path)); |
| + FilePath child_dir_path(CreateVirtualTemporaryDirInDir(parent_dir_path, |
| + "child_dir")); |
| + ASSERT_FALSE(child_dir_path.empty()); |
| + |
| + operation()->Remove(URLForPath(parent_dir_path), true /* recursive */); |
| + MessageLoop::current()->RunAllPending(); |
| + EXPECT_EQ(base::PLATFORM_FILE_OK, status()); |
| + EXPECT_FALSE(VirtualDirectoryExists(parent_dir_path)); |
| +} |
| + |
| +TEST_F(FileSystemQuotaTest, TestTruncate) { |
| + FilePath dir_path(CreateVirtualTemporaryDir("")); |
| + FilePath file_path(CreateVirtualTemporaryFileInDir(dir_path)); |
| + |
| + char test_data[] = "test data"; |
| + int data_size = static_cast<int>(sizeof(test_data)); |
| + EXPECT_EQ(data_size, |
| + file_util::WriteFile(PlatformPath(file_path), |
| + test_data, data_size)); |
| + |
| + // Check that its length is the size of the data written. |
| + operation()->GetMetadata(URLForPath(file_path)); |
| + MessageLoop::current()->RunAllPending(); |
| + EXPECT_EQ(base::PLATFORM_FILE_OK, status()); |
| + EXPECT_FALSE(info().is_directory); |
| + EXPECT_EQ(data_size, info().size); |
| + |
| + // Extend the file by truncating it. |
| + int length = 17; |
| + operation()->Truncate(URLForPath(file_path), length); |
| + MessageLoop::current()->RunAllPending(); |
| + EXPECT_EQ(base::PLATFORM_FILE_OK, status()); |
| + |
| + // Check that its length is now 17 and that it's all zeroes after the test |
| + // data. |
| + base::PlatformFileInfo info; |
| + |
| + EXPECT_TRUE(file_util::GetFileInfo(PlatformPath(file_path), &info)); |
| + EXPECT_EQ(length, info.size); |
| + char data[100]; |
| + EXPECT_EQ(length, file_util::ReadFile(PlatformPath(file_path), data, length)); |
| + for (int i = 0; i < length; ++i) { |
| + if (i < static_cast<int>(sizeof(test_data))) |
| + EXPECT_EQ(test_data[i], data[i]); |
| + else |
| + EXPECT_EQ(0, data[i]); |
| + } |
| + |
| + // Shorten the file by truncating it. |
| + length = 3; |
| + operation()->Truncate(URLForPath(file_path), length); |
| + MessageLoop::current()->RunAllPending(); |
| + EXPECT_EQ(base::PLATFORM_FILE_OK, status()); |
| + |
| + // Check that its length is now 3 and that it contains only bits of test data. |
| + EXPECT_TRUE(file_util::GetFileInfo(PlatformPath(file_path), &info)); |
| + EXPECT_EQ(length, info.size); |
| + EXPECT_EQ(length, file_util::ReadFile(PlatformPath(file_path), data, length)); |
| + for (int i = 0; i < length; ++i) |
| + EXPECT_EQ(test_data[i], data[i]); |
| +} |
| +#endif |
| + |
| +} // namespace fileapi |