diff options
author | pilgrim@chromium.org <pilgrim@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-04-30 00:38:33 +0000 |
---|---|---|
committer | pilgrim@chromium.org <pilgrim@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-04-30 00:38:33 +0000 |
commit | e14d0a738b877e637639bc0fa1a983e76e10f198 (patch) | |
tree | b0986d13d1f50cd21c9abe9bcdc72194338b5bda /webkit | |
parent | 01cd469fdafbf1b53235202368c93629f817ec88 (diff) | |
download | chromium_src-e14d0a738b877e637639bc0fa1a983e76e10f198.zip chromium_src-e14d0a738b877e637639bc0fa1a983e76e10f198.tar.gz chromium_src-e14d0a738b877e637639bc0fa1a983e76e10f198.tar.bz2 |
Move all remaining fileapi and quota unit tests from webkit/ to content/
BUG=338338
TBR=darin@chromium.org
Review URL: https://codereview.chromium.org/253753007
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@267007 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'webkit')
18 files changed, 22 insertions, 3751 deletions
diff --git a/webkit/browser/fileapi/external_mount_points_unittest.cc b/webkit/browser/fileapi/external_mount_points_unittest.cc deleted file mode 100644 index fa5d8d1..0000000 --- a/webkit/browser/fileapi/external_mount_points_unittest.cc +++ /dev/null @@ -1,507 +0,0 @@ -// Copyright (c) 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 "webkit/browser/fileapi/external_mount_points.h" - -#include <string> - -#include "base/files/file_path.h" -#include "testing/gtest/include/gtest/gtest.h" -#include "webkit/browser/fileapi/file_system_url.h" - -#define FPL FILE_PATH_LITERAL - -#if defined(FILE_PATH_USES_DRIVE_LETTERS) -#define DRIVE FPL("C:") -#else -#define DRIVE -#endif - -using fileapi::FileSystemURL; - -namespace { - -TEST(ExternalMountPointsTest, AddMountPoint) { - scoped_refptr<fileapi::ExternalMountPoints> mount_points( - fileapi::ExternalMountPoints::CreateRefCounted()); - - struct TestCase { - // The mount point's name. - const char* const name; - // The mount point's path. - const base::FilePath::CharType* const path; - // Whether the mount point registration should succeed. - bool success; - // Path returned by GetRegisteredPath. NULL if the method is expected to - // fail. - const base::FilePath::CharType* const registered_path; - }; - - const TestCase kTestCases[] = { - // Valid mount point. - { "test", DRIVE FPL("/foo/test"), true, DRIVE FPL("/foo/test") }, - // Valid mount point with only one path component. - { "bbb", DRIVE FPL("/bbb"), true, DRIVE FPL("/bbb") }, - // Existing mount point path is substring of the mount points path. - { "test11", DRIVE FPL("/foo/test11"), true, DRIVE FPL("/foo/test11") }, - // Path substring of an existing path. - { "test1", DRIVE FPL("/foo/test1"), true, DRIVE FPL("/foo/test1") }, - // Empty mount point name and path. - { "", DRIVE FPL(""), false, NULL }, - // Empty mount point name. - { "", DRIVE FPL("/ddd"), false, NULL }, - // Empty mount point path. - { "empty_path", FPL(""), true, FPL("") }, - // Name different from path's base name. - { "not_base_name", DRIVE FPL("/x/y/z"), true, DRIVE FPL("/x/y/z") }, - // References parent. - { "invalid", DRIVE FPL("../foo/invalid"), false, NULL }, - // Relative path. - { "relative", DRIVE FPL("foo/relative"), false, NULL }, - // Existing mount point path. - { "path_exists", DRIVE FPL("/foo/test"), false, NULL }, - // Mount point with the same name exists. - { "test", DRIVE FPL("/foo/a/test_name_exists"), false, - DRIVE FPL("/foo/test") }, - // Child of an existing mount point. - { "a1", DRIVE FPL("/foo/test/a"), false, NULL }, - // Parent of an existing mount point. - { "foo1", DRIVE FPL("/foo"), false, NULL }, - // Bit bigger depth. - { "g", DRIVE FPL("/foo/a/b/c/d/e/f/g"), true, - DRIVE FPL("/foo/a/b/c/d/e/f/g") }, - // Sibling mount point (with similar name) exists. - { "ff", DRIVE FPL("/foo/a/b/c/d/e/ff"), true, - DRIVE FPL("/foo/a/b/c/d/e/ff") }, - // Lexicographically last among existing mount points. - { "yyy", DRIVE FPL("/zzz/yyy"), true, DRIVE FPL("/zzz/yyy") }, - // Parent of the lexicographically last mount point. - { "zzz1", DRIVE FPL("/zzz"), false, NULL }, - // Child of the lexicographically last mount point. - { "xxx1", DRIVE FPL("/zzz/yyy/xxx"), false, NULL }, - // Lexicographically first among existing mount points. - { "b", DRIVE FPL("/a/b"), true, DRIVE FPL("/a/b") }, - // Parent of lexicographically first mount point. - { "a2", DRIVE FPL("/a"), false, NULL }, - // Child of lexicographically last mount point. - { "c1", DRIVE FPL("/a/b/c"), false, NULL }, - // Parent to all of the mount points. - { "root", DRIVE FPL("/"), false, NULL }, - // Path contains .. component. - { "funky", DRIVE FPL("/tt/fun/../funky"), false, NULL }, - // Windows separators. -#if defined(FILE_PATH_USES_WIN_SEPARATORS) - { "win", DRIVE FPL("\\try\\separators\\win"), true, - DRIVE FPL("\\try\\separators\\win") }, - { "win1", DRIVE FPL("\\try/separators\\win1"), true, - DRIVE FPL("\\try/separators\\win1") }, - { "win2", DRIVE FPL("\\try/separators\\win"), false, NULL }, -#else - { "win", DRIVE FPL("\\separators\\win"), false, NULL }, - { "win1", DRIVE FPL("\\try/separators\\win1"), false, NULL }, -#endif - // Win separators, but relative path. - { "win2", DRIVE FPL("try\\separators\\win2"), false, NULL }, - }; - - // Test adding mount points. - for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) { - EXPECT_EQ(kTestCases[i].success, - mount_points->RegisterFileSystem( - kTestCases[i].name, - fileapi::kFileSystemTypeNativeLocal, - fileapi::FileSystemMountOption(), - base::FilePath(kTestCases[i].path))) - << "Adding mount point: " << kTestCases[i].name << " with path " - << kTestCases[i].path; - } - - // Test that final mount point presence state is as expected. - for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) { - base::FilePath found_path; - EXPECT_EQ(kTestCases[i].registered_path != NULL, - mount_points->GetRegisteredPath(kTestCases[i].name, &found_path)) - << "Test case: " << i; - - if (kTestCases[i].registered_path) { - base::FilePath expected_path(kTestCases[i].registered_path); - EXPECT_EQ(expected_path.NormalizePathSeparators(), found_path); - } - } -} - -TEST(ExternalMountPointsTest, GetVirtualPath) { - scoped_refptr<fileapi::ExternalMountPoints> mount_points( - fileapi::ExternalMountPoints::CreateRefCounted()); - - mount_points->RegisterFileSystem("c", - fileapi::kFileSystemTypeNativeLocal, - fileapi::FileSystemMountOption(), - base::FilePath(DRIVE FPL("/a/b/c"))); - // Note that "/a/b/c" < "/a/b/c(1)" < "/a/b/c/". - mount_points->RegisterFileSystem("c(1)", - fileapi::kFileSystemTypeNativeLocal, - fileapi::FileSystemMountOption(), - base::FilePath(DRIVE FPL("/a/b/c(1)"))); - mount_points->RegisterFileSystem("x", - fileapi::kFileSystemTypeNativeLocal, - fileapi::FileSystemMountOption(), - base::FilePath(DRIVE FPL("/z/y/x"))); - mount_points->RegisterFileSystem("o", - fileapi::kFileSystemTypeNativeLocal, - fileapi::FileSystemMountOption(), - base::FilePath(DRIVE FPL("/m/n/o"))); - // A mount point whose name does not match its path base name. - mount_points->RegisterFileSystem("mount", - fileapi::kFileSystemTypeNativeLocal, - fileapi::FileSystemMountOption(), - base::FilePath(DRIVE FPL("/root/foo"))); - // A mount point with an empty path. - mount_points->RegisterFileSystem("empty_path", - fileapi::kFileSystemTypeNativeLocal, - fileapi::FileSystemMountOption(), - base::FilePath()); - - struct TestCase { - const base::FilePath::CharType* const local_path; - bool success; - const base::FilePath::CharType* const virtual_path; - }; - - const TestCase kTestCases[] = { - // Empty path. - { FPL(""), false, FPL("") }, - // No registered mount point (but is parent to a mount point). - { DRIVE FPL("/a/b"), false, FPL("") }, - // No registered mount point (but is parent to a mount point). - { DRIVE FPL("/z/y"), false, FPL("") }, - // No registered mount point (but is parent to a mount point). - { DRIVE FPL("/m/n"), false, FPL("") }, - // No registered mount point. - { DRIVE FPL("/foo/mount"), false, FPL("") }, - // An existing mount point path is substring. - { DRIVE FPL("/a/b/c1"), false, FPL("") }, - // No leading /. - { DRIVE FPL("a/b/c"), false, FPL("") }, - // Sibling to a root path. - { DRIVE FPL("/a/b/d/e"), false, FPL("") }, - // Sibling to a root path. - { DRIVE FPL("/z/y/v/u"), false, FPL("") }, - // Sibling to a root path. - { DRIVE FPL("/m/n/p/q"), false, FPL("") }, - // Mount point root path. - { DRIVE FPL("/a/b/c"), true, FPL("c") }, - // Mount point root path. - { DRIVE FPL("/z/y/x"), true, FPL("x") }, - // Mount point root path. - { DRIVE FPL("/m/n/o"), true, FPL("o") }, - // Mount point child path. - { DRIVE FPL("/a/b/c/d/e"), true, FPL("c/d/e") }, - // Mount point child path. - { DRIVE FPL("/z/y/x/v/u"), true, FPL("x/v/u") }, - // Mount point child path. - { DRIVE FPL("/m/n/o/p/q"), true, FPL("o/p/q") }, - // Name doesn't match mount point path base name. - { DRIVE FPL("/root/foo/a/b/c"), true, FPL("mount/a/b/c") }, - { DRIVE FPL("/root/foo"), true, FPL("mount") }, - // Mount point contains character whose ASCII code is smaller than file path - // separator's. - { DRIVE FPL("/a/b/c(1)/d/e"), true, FPL("c(1)/d/e") }, -#if defined(FILE_PATH_USES_WIN_SEPARATORS) - // Path with win separators mixed in. - { DRIVE FPL("/a\\b\\c/d"), true, FPL("c/d") }, -#endif - }; - - for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) { - // Initialize virtual path with a value. - base::FilePath virtual_path(DRIVE FPL("/mount")); - base::FilePath local_path(kTestCases[i].local_path); - EXPECT_EQ(kTestCases[i].success, - mount_points->GetVirtualPath(local_path, &virtual_path)) - << "Resolving " << kTestCases[i].local_path; - - // There are no guarantees for |virtual_path| value if |GetVirtualPath| - // fails. - if (!kTestCases[i].success) - continue; - - base::FilePath expected_virtual_path(kTestCases[i].virtual_path); - EXPECT_EQ(expected_virtual_path.NormalizePathSeparators(), virtual_path) - << "Resolving " << kTestCases[i].local_path; - } -} - -TEST(ExternalMountPointsTest, HandlesFileSystemMountType) { - scoped_refptr<fileapi::ExternalMountPoints> mount_points( - fileapi::ExternalMountPoints::CreateRefCounted()); - - const GURL test_origin("http://chromium.org"); - const base::FilePath test_path(FPL("/mount")); - - // Should handle External File System. - EXPECT_TRUE(mount_points->HandlesFileSystemMountType( - fileapi::kFileSystemTypeExternal)); - - // Shouldn't handle the rest. - EXPECT_FALSE(mount_points->HandlesFileSystemMountType( - fileapi::kFileSystemTypeIsolated)); - EXPECT_FALSE(mount_points->HandlesFileSystemMountType( - fileapi::kFileSystemTypeTemporary)); - EXPECT_FALSE(mount_points->HandlesFileSystemMountType( - fileapi::kFileSystemTypePersistent)); - EXPECT_FALSE(mount_points->HandlesFileSystemMountType( - fileapi::kFileSystemTypeTest)); - // Not even if it's external subtype. - EXPECT_FALSE(mount_points->HandlesFileSystemMountType( - fileapi::kFileSystemTypeNativeLocal)); - EXPECT_FALSE(mount_points->HandlesFileSystemMountType( - fileapi::kFileSystemTypeRestrictedNativeLocal)); - EXPECT_FALSE(mount_points->HandlesFileSystemMountType( - fileapi::kFileSystemTypeDrive)); - EXPECT_FALSE(mount_points->HandlesFileSystemMountType( - fileapi::kFileSystemTypeSyncable)); -} - -TEST(ExternalMountPointsTest, CreateCrackedFileSystemURL) { - scoped_refptr<fileapi::ExternalMountPoints> mount_points( - fileapi::ExternalMountPoints::CreateRefCounted()); - - const GURL kTestOrigin("http://chromium.org"); - - mount_points->RegisterFileSystem("c", - fileapi::kFileSystemTypeNativeLocal, - fileapi::FileSystemMountOption(), - base::FilePath(DRIVE FPL("/a/b/c"))); - mount_points->RegisterFileSystem("c(1)", - fileapi::kFileSystemTypeDrive, - fileapi::FileSystemMountOption(), - base::FilePath(DRIVE FPL("/a/b/c(1)"))); - mount_points->RegisterFileSystem("empty_path", - fileapi::kFileSystemTypeSyncable, - fileapi::FileSystemMountOption(), - base::FilePath()); - mount_points->RegisterFileSystem("mount", - fileapi::kFileSystemTypeDrive, - fileapi::FileSystemMountOption(), - base::FilePath(DRIVE FPL("/root"))); - - // Try cracking invalid GURL. - FileSystemURL invalid = mount_points->CrackURL(GURL("http://chromium.og")); - EXPECT_FALSE(invalid.is_valid()); - - // Try cracking isolated path. - FileSystemURL isolated = mount_points->CreateCrackedFileSystemURL( - kTestOrigin, fileapi::kFileSystemTypeIsolated, base::FilePath(FPL("c"))); - EXPECT_FALSE(isolated.is_valid()); - - // Try native local which is not cracked. - FileSystemURL native_local = mount_points->CreateCrackedFileSystemURL( - kTestOrigin, - fileapi::kFileSystemTypeNativeLocal, - base::FilePath(FPL("c"))); - EXPECT_FALSE(native_local.is_valid()); - - struct TestCase { - const base::FilePath::CharType* const path; - bool expect_valid; - fileapi::FileSystemType expect_type; - const base::FilePath::CharType* const expect_path; - const char* const expect_fs_id; - }; - - const TestCase kTestCases[] = { - { FPL("c/d/e"), - true, fileapi::kFileSystemTypeNativeLocal, DRIVE FPL("/a/b/c/d/e"), "c" }, - { FPL("c(1)/d/e"), - true, fileapi::kFileSystemTypeDrive, DRIVE FPL("/a/b/c(1)/d/e"), "c(1)" }, - { FPL("c(1)"), - true, fileapi::kFileSystemTypeDrive, DRIVE FPL("/a/b/c(1)"), "c(1)" }, - { FPL("empty_path/a"), - true, fileapi::kFileSystemTypeSyncable, FPL("a"), "empty_path" }, - { FPL("empty_path"), - true, fileapi::kFileSystemTypeSyncable, FPL(""), "empty_path" }, - { FPL("mount/a/b"), - true, fileapi::kFileSystemTypeDrive, DRIVE FPL("/root/a/b"), "mount" }, - { FPL("mount"), - true, fileapi::kFileSystemTypeDrive, DRIVE FPL("/root"), "mount" }, - { FPL("cc"), - false, fileapi::kFileSystemTypeUnknown, FPL(""), "" }, - { FPL(""), - false, fileapi::kFileSystemTypeUnknown, FPL(""), "" }, - { FPL(".."), - false, fileapi::kFileSystemTypeUnknown, FPL(""), "" }, - // Absolte paths. - { FPL("/c/d/e"), - false, fileapi::kFileSystemTypeUnknown, FPL(""), "" }, - { FPL("/c(1)/d/e"), - false, fileapi::kFileSystemTypeUnknown, FPL(""), "" }, - { FPL("/empty_path"), - false, fileapi::kFileSystemTypeUnknown, FPL(""), "" }, - // PAth references parent. - { FPL("c/d/../e"), - false, fileapi::kFileSystemTypeUnknown, FPL(""), "" }, - { FPL("/empty_path/a/../b"), - false, fileapi::kFileSystemTypeUnknown, FPL(""), "" }, -#if defined(FILE_PATH_USES_WIN_SEPARATORS) - { FPL("c/d\\e"), - true, fileapi::kFileSystemTypeNativeLocal, DRIVE FPL("/a/b/c/d/e"), "c" }, - { FPL("mount\\a\\b"), - true, fileapi::kFileSystemTypeDrive, DRIVE FPL("/root/a/b"), "mount" }, -#endif - }; - - for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) { - FileSystemURL cracked = mount_points->CreateCrackedFileSystemURL( - kTestOrigin, - fileapi::kFileSystemTypeExternal, - base::FilePath(kTestCases[i].path)); - - EXPECT_EQ(kTestCases[i].expect_valid, cracked.is_valid()) - << "Test case index: " << i; - - if (!kTestCases[i].expect_valid) - continue; - - EXPECT_EQ(kTestOrigin, cracked.origin()) - << "Test case index: " << i; - EXPECT_EQ(kTestCases[i].expect_type, cracked.type()) - << "Test case index: " << i; - EXPECT_EQ(base::FilePath( - kTestCases[i].expect_path).NormalizePathSeparators(), cracked.path()) - << "Test case index: " << i; - EXPECT_EQ(base::FilePath(kTestCases[i].path).NormalizePathSeparators(), - cracked.virtual_path()) - << "Test case index: " << i; - EXPECT_EQ(kTestCases[i].expect_fs_id, cracked.filesystem_id()) - << "Test case index: " << i; - EXPECT_EQ(fileapi::kFileSystemTypeExternal, cracked.mount_type()) - << "Test case index: " << i; - } -} - -TEST(ExternalMountPointsTest, CrackVirtualPath) { - scoped_refptr<fileapi::ExternalMountPoints> mount_points( - fileapi::ExternalMountPoints::CreateRefCounted()); - - const GURL kTestOrigin("http://chromium.org"); - - mount_points->RegisterFileSystem("c", - fileapi::kFileSystemTypeNativeLocal, - fileapi::FileSystemMountOption(), - base::FilePath(DRIVE FPL("/a/b/c"))); - mount_points->RegisterFileSystem("c(1)", - fileapi::kFileSystemTypeDrive, - fileapi::FileSystemMountOption(), - base::FilePath(DRIVE FPL("/a/b/c(1)"))); - mount_points->RegisterFileSystem("empty_path", - fileapi::kFileSystemTypeSyncable, - fileapi::FileSystemMountOption(), - base::FilePath()); - mount_points->RegisterFileSystem("mount", - fileapi::kFileSystemTypeDrive, - fileapi::FileSystemMountOption(), - base::FilePath(DRIVE FPL("/root"))); - - struct TestCase { - const base::FilePath::CharType* const path; - bool expect_valid; - fileapi::FileSystemType expect_type; - const base::FilePath::CharType* const expect_path; - const char* const expect_name; - }; - - const TestCase kTestCases[] = { - { FPL("c/d/e"), - true, fileapi::kFileSystemTypeNativeLocal, DRIVE FPL("/a/b/c/d/e"), "c" }, - { FPL("c(1)/d/e"), - true, fileapi::kFileSystemTypeDrive, DRIVE FPL("/a/b/c(1)/d/e"), "c(1)" }, - { FPL("c(1)"), - true, fileapi::kFileSystemTypeDrive, DRIVE FPL("/a/b/c(1)"), "c(1)" }, - { FPL("empty_path/a"), - true, fileapi::kFileSystemTypeSyncable, FPL("a"), "empty_path" }, - { FPL("empty_path"), - true, fileapi::kFileSystemTypeSyncable, FPL(""), "empty_path" }, - { FPL("mount/a/b"), - true, fileapi::kFileSystemTypeDrive, DRIVE FPL("/root/a/b"), "mount" }, - { FPL("mount"), - true, fileapi::kFileSystemTypeDrive, DRIVE FPL("/root"), "mount" }, - { FPL("cc"), - false, fileapi::kFileSystemTypeUnknown, FPL(""), "" }, - { FPL(""), - false, fileapi::kFileSystemTypeUnknown, FPL(""), "" }, - { FPL(".."), - false, fileapi::kFileSystemTypeUnknown, FPL(""), "" }, - // Absolte paths. - { FPL("/c/d/e"), - false, fileapi::kFileSystemTypeUnknown, FPL(""), "" }, - { FPL("/c(1)/d/e"), - false, fileapi::kFileSystemTypeUnknown, FPL(""), "" }, - { FPL("/empty_path"), - false, fileapi::kFileSystemTypeUnknown, FPL(""), "" }, - // PAth references parent. - { FPL("c/d/../e"), - false, fileapi::kFileSystemTypeUnknown, FPL(""), "" }, - { FPL("/empty_path/a/../b"), - false, fileapi::kFileSystemTypeUnknown, FPL(""), "" }, -#if defined(FILE_PATH_USES_WIN_SEPARATORS) - { FPL("c/d\\e"), - true, fileapi::kFileSystemTypeNativeLocal, DRIVE FPL("/a/b/c/d/e"), "c" }, - { FPL("mount\\a\\b"), - true, fileapi::kFileSystemTypeDrive, DRIVE FPL("/root/a/b"), "mount" }, -#endif - }; - - for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) { - std::string cracked_name; - fileapi::FileSystemType cracked_type; - base::FilePath cracked_path; - fileapi::FileSystemMountOption cracked_option; - EXPECT_EQ(kTestCases[i].expect_valid, - mount_points->CrackVirtualPath(base::FilePath(kTestCases[i].path), - &cracked_name, &cracked_type, &cracked_path, &cracked_option)) - << "Test case index: " << i; - - if (!kTestCases[i].expect_valid) - continue; - - EXPECT_EQ(kTestCases[i].expect_type, cracked_type) - << "Test case index: " << i; - EXPECT_EQ(base::FilePath( - kTestCases[i].expect_path).NormalizePathSeparators(), cracked_path) - << "Test case index: " << i; - EXPECT_EQ(kTestCases[i].expect_name, cracked_name) - << "Test case index: " << i; - } -} - -TEST(ExternalMountPointsTest, MountOption) { - scoped_refptr<fileapi::ExternalMountPoints> mount_points( - fileapi::ExternalMountPoints::CreateRefCounted()); - - mount_points->RegisterFileSystem( - "nosync", - fileapi::kFileSystemTypeNativeLocal, - fileapi::FileSystemMountOption(fileapi::COPY_SYNC_OPTION_NO_SYNC), - base::FilePath(DRIVE FPL("/nosync"))); - mount_points->RegisterFileSystem( - "sync", - fileapi::kFileSystemTypeNativeLocal, - fileapi::FileSystemMountOption(fileapi::COPY_SYNC_OPTION_SYNC), - base::FilePath(DRIVE FPL("/sync"))); - - std::string name; - fileapi::FileSystemType type; - fileapi::FileSystemMountOption option; - base::FilePath path; - EXPECT_TRUE(mount_points->CrackVirtualPath( - base::FilePath(FPL("nosync/file")), &name, &type, &path, &option)); - EXPECT_EQ(fileapi::COPY_SYNC_OPTION_NO_SYNC, option.copy_sync_option()); - EXPECT_TRUE(mount_points->CrackVirtualPath( - base::FilePath(FPL("sync/file")), &name, &type, &path, &option)); - EXPECT_EQ(fileapi::COPY_SYNC_OPTION_SYNC, option.copy_sync_option()); -} - -} // namespace - diff --git a/webkit/browser/fileapi/file_system_url_unittest.cc b/webkit/browser/fileapi/file_system_url_unittest.cc deleted file mode 100644 index c5a632c..0000000 --- a/webkit/browser/fileapi/file_system_url_unittest.cc +++ /dev/null @@ -1,213 +0,0 @@ -// Copyright (c) 2012 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/browser/fileapi/file_system_url.h" - -#include "base/files/file_path.h" -#include "testing/gtest/include/gtest/gtest.h" -#include "url/gurl.h" -#include "webkit/common/fileapi/file_system_types.h" -#include "webkit/common/fileapi/file_system_util.h" - -#define FPL FILE_PATH_LITERAL - -#if defined(FILE_PATH_USES_DRIVE_LETTERS) -#define DRIVE FPL("C:") -#else -#define DRIVE FPL("/a/") -#endif - -namespace fileapi { - -namespace { - -FileSystemURL CreateFileSystemURL(const std::string& url_string) { - FileSystemURL url = FileSystemURL::CreateForTest(GURL(url_string)); - EXPECT_TRUE(url.type() != kFileSystemTypeExternal && - url.type() != kFileSystemTypeIsolated); - return url; -} - -std::string NormalizedUTF8Path(const base::FilePath& path) { - return path.NormalizePathSeparators().AsUTF8Unsafe(); -} - -} // namespace - -TEST(FileSystemURLTest, ParsePersistent) { - FileSystemURL url = CreateFileSystemURL( - "filesystem:http://chromium.org/persistent/directory/file"); - ASSERT_TRUE(url.is_valid()); - EXPECT_EQ("http://chromium.org/", url.origin().spec()); - EXPECT_EQ(kFileSystemTypePersistent, url.type()); - EXPECT_EQ(FPL("file"), VirtualPath::BaseName(url.path()).value()); - EXPECT_EQ(FPL("directory"), url.path().DirName().value()); -} - -TEST(FileSystemURLTest, ParseTemporary) { - FileSystemURL url = CreateFileSystemURL( - "filesystem:http://chromium.org/temporary/directory/file"); - ASSERT_TRUE(url.is_valid()); - EXPECT_EQ("http://chromium.org/", url.origin().spec()); - EXPECT_EQ(kFileSystemTypeTemporary, url.type()); - EXPECT_EQ(FPL("file"), VirtualPath::BaseName(url.path()).value()); - EXPECT_EQ(FPL("directory"), url.path().DirName().value()); -} - -TEST(FileSystemURLTest, EnsureFilePathIsRelative) { - FileSystemURL url = CreateFileSystemURL( - "filesystem:http://chromium.org/temporary/////directory/file"); - ASSERT_TRUE(url.is_valid()); - EXPECT_EQ("http://chromium.org/", url.origin().spec()); - EXPECT_EQ(kFileSystemTypeTemporary, url.type()); - EXPECT_EQ(FPL("file"), VirtualPath::BaseName(url.path()).value()); - EXPECT_EQ(FPL("directory"), url.path().DirName().value()); - EXPECT_FALSE(url.path().IsAbsolute()); -} - -TEST(FileSystemURLTest, RejectBadSchemes) { - EXPECT_FALSE(CreateFileSystemURL("http://chromium.org/").is_valid()); - EXPECT_FALSE(CreateFileSystemURL("https://chromium.org/").is_valid()); - EXPECT_FALSE(CreateFileSystemURL("file:///foo/bar").is_valid()); - EXPECT_FALSE(CreateFileSystemURL("foobar:///foo/bar").is_valid()); -} - -TEST(FileSystemURLTest, UnescapePath) { - FileSystemURL url = CreateFileSystemURL( - "filesystem:http://chromium.org/persistent/%7Echromium/space%20bar"); - ASSERT_TRUE(url.is_valid()); - EXPECT_EQ(FPL("space bar"), VirtualPath::BaseName(url.path()).value()); - EXPECT_EQ(FPL("~chromium"), url.path().DirName().value()); -} - -TEST(FileSystemURLTest, RejectBadType) { - EXPECT_FALSE(CreateFileSystemURL( - "filesystem:http://c.org/foobar/file").is_valid()); - EXPECT_FALSE(CreateFileSystemURL( - "filesystem:http://c.org/temporaryfoo/file").is_valid()); -} - -TEST(FileSystemURLTest, RejectMalformedURL) { - EXPECT_FALSE(CreateFileSystemURL("filesystem:///foobar/file").is_valid()); - EXPECT_FALSE(CreateFileSystemURL("filesystem:foobar/file").is_valid()); -} - -TEST(FileSystemURLTest, CompareURLs) { - const GURL urls[] = { - GURL("filesystem:http://chromium.org/temporary/dir a/file a"), - GURL("filesystem:http://chromium.org/temporary/dir a/file a"), - GURL("filesystem:http://chromium.org/temporary/dir a/file b"), - GURL("filesystem:http://chromium.org/temporary/dir a/file aa"), - GURL("filesystem:http://chromium.org/temporary/dir b/file a"), - GURL("filesystem:http://chromium.org/temporary/dir aa/file b"), - GURL("filesystem:http://chromium.com/temporary/dir a/file a"), - GURL("filesystem:https://chromium.org/temporary/dir a/file a") - }; - - FileSystemURL::Comparator compare; - for (size_t i = 0; i < arraysize(urls); ++i) { - for (size_t j = 0; j < arraysize(urls); ++j) { - SCOPED_TRACE(testing::Message() << i << " < " << j); - EXPECT_EQ(urls[i] < urls[j], - compare(FileSystemURL::CreateForTest(urls[i]), - FileSystemURL::CreateForTest(urls[j]))); - } - } - - const FileSystemURL a = CreateFileSystemURL( - "filesystem:http://chromium.org/temporary/dir a/file a"); - const FileSystemURL b = CreateFileSystemURL( - "filesystem:http://chromium.org/persistent/dir a/file a"); - EXPECT_EQ(a.type() < b.type(), compare(a, b)); - EXPECT_EQ(b.type() < a.type(), compare(b, a)); -} - -TEST(FileSystemURLTest, IsParent) { - const std::string root1 = GetFileSystemRootURI( - GURL("http://example.com"), kFileSystemTypeTemporary).spec(); - const std::string root2 = GetFileSystemRootURI( - GURL("http://example.com"), kFileSystemTypePersistent).spec(); - const std::string root3 = GetFileSystemRootURI( - GURL("http://chromium.org"), kFileSystemTypeTemporary).spec(); - - const std::string parent("dir"); - const std::string child("dir/child"); - const std::string other("other"); - - // True cases. - EXPECT_TRUE(CreateFileSystemURL(root1 + parent).IsParent( - CreateFileSystemURL(root1 + child))); - EXPECT_TRUE(CreateFileSystemURL(root2 + parent).IsParent( - CreateFileSystemURL(root2 + child))); - - // False cases: the path is not a child. - EXPECT_FALSE(CreateFileSystemURL(root1 + parent).IsParent( - CreateFileSystemURL(root1 + other))); - EXPECT_FALSE(CreateFileSystemURL(root1 + parent).IsParent( - CreateFileSystemURL(root1 + parent))); - EXPECT_FALSE(CreateFileSystemURL(root1 + child).IsParent( - CreateFileSystemURL(root1 + parent))); - - // False case: different types. - EXPECT_FALSE(CreateFileSystemURL(root1 + parent).IsParent( - CreateFileSystemURL(root2 + child))); - - // False case: different origins. - EXPECT_FALSE(CreateFileSystemURL(root1 + parent).IsParent( - CreateFileSystemURL(root3 + child))); -} - -TEST(FileSystemURLTest, ToGURL) { - EXPECT_TRUE(FileSystemURL().ToGURL().is_empty()); - const char* kTestURL[] = { - "filesystem:http://chromium.org/persistent/directory/file0", - "filesystem:http://chromium.org/temporary/directory/file1", - "filesystem:http://chromium.org/isolated/directory/file2", - "filesystem:http://chromium.org/external/directory/file2", - "filesystem:http://chromium.org/test/directory/file3", - }; - - for (size_t i = 0; i < arraysize(kTestURL); ++i) { - EXPECT_EQ( - kTestURL[i], - FileSystemURL::CreateForTest(GURL(kTestURL[i])).ToGURL().spec()); - } -} - -TEST(FileSystemURLTest, DebugString) { - const GURL kOrigin("http://example.com"); - const base::FilePath kPath(FPL("dir/file")); - - const FileSystemURL kURL1 = FileSystemURL::CreateForTest( - kOrigin, kFileSystemTypeTemporary, kPath); - EXPECT_EQ("filesystem:http://example.com/temporary/" + - NormalizedUTF8Path(kPath), - kURL1.DebugString()); -} - -TEST(FileSystemURLTest, IsInSameFileSystem) { - FileSystemURL url_foo_temp_a = FileSystemURL::CreateForTest( - GURL("http://foo"), kFileSystemTypeTemporary, - base::FilePath::FromUTF8Unsafe("a")); - FileSystemURL url_foo_temp_b = FileSystemURL::CreateForTest( - GURL("http://foo"), kFileSystemTypeTemporary, - base::FilePath::FromUTF8Unsafe("b")); - FileSystemURL url_foo_perm_a = FileSystemURL::CreateForTest( - GURL("http://foo"), kFileSystemTypePersistent, - base::FilePath::FromUTF8Unsafe("a")); - FileSystemURL url_bar_temp_a = FileSystemURL::CreateForTest( - GURL("http://bar"), kFileSystemTypeTemporary, - base::FilePath::FromUTF8Unsafe("a")); - FileSystemURL url_bar_perm_a = FileSystemURL::CreateForTest( - GURL("http://bar"), kFileSystemTypePersistent, - base::FilePath::FromUTF8Unsafe("a")); - - EXPECT_TRUE(url_foo_temp_a.IsInSameFileSystem(url_foo_temp_a)); - EXPECT_TRUE(url_foo_temp_a.IsInSameFileSystem(url_foo_temp_b)); - EXPECT_FALSE(url_foo_temp_a.IsInSameFileSystem(url_foo_perm_a)); - EXPECT_FALSE(url_foo_temp_a.IsInSameFileSystem(url_bar_temp_a)); - EXPECT_FALSE(url_foo_temp_a.IsInSameFileSystem(url_bar_perm_a)); -} - -} // namespace fileapi diff --git a/webkit/browser/fileapi/file_system_usage_cache_unittest.cc b/webkit/browser/fileapi/file_system_usage_cache_unittest.cc deleted file mode 100644 index 1b99d9c..0000000 --- a/webkit/browser/fileapi/file_system_usage_cache_unittest.cc +++ /dev/null @@ -1,158 +0,0 @@ -// Copyright (c) 2012 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/browser/fileapi/file_system_usage_cache.h" - -#include "base/basictypes.h" -#include "base/file_util.h" -#include "base/files/scoped_temp_dir.h" -#include "base/message_loop/message_loop.h" -#include "testing/gtest/include/gtest/gtest.h" - -namespace fileapi { - -class FileSystemUsageCacheTest : public testing::Test { - public: - FileSystemUsageCacheTest() - : usage_cache_(base::MessageLoopProxy::current().get()) {} - - virtual void SetUp() { - ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); - } - - protected: - base::FilePath GetUsageFilePath() { - return data_dir_.path().Append(FileSystemUsageCache::kUsageFileName); - } - - FileSystemUsageCache* usage_cache() { - return &usage_cache_; - } - - private: - base::MessageLoop message_loop_; - base::ScopedTempDir data_dir_; - FileSystemUsageCache usage_cache_; - - DISALLOW_COPY_AND_ASSIGN(FileSystemUsageCacheTest); -}; - -TEST_F(FileSystemUsageCacheTest, CreateTest) { - base::FilePath usage_file_path = GetUsageFilePath(); - EXPECT_TRUE(usage_cache()->UpdateUsage(usage_file_path, 0)); -} - -TEST_F(FileSystemUsageCacheTest, SetSizeTest) { - static const int64 size = 240122; - base::FilePath usage_file_path = GetUsageFilePath(); - int64 usage = 0; - ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path, size)); - EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path, &usage)); - EXPECT_EQ(size, usage); -} - -TEST_F(FileSystemUsageCacheTest, SetLargeSizeTest) { - static const int64 size = kint64max; - base::FilePath usage_file_path = GetUsageFilePath(); - int64 usage = 0; - ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path, size)); - EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path, &usage)); - EXPECT_EQ(size, usage); -} - -TEST_F(FileSystemUsageCacheTest, IncAndGetSizeTest) { - base::FilePath usage_file_path = GetUsageFilePath(); - uint32 dirty = 0; - int64 usage = 0; - ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path, 98214)); - ASSERT_TRUE(usage_cache()->IncrementDirty(usage_file_path)); - EXPECT_TRUE(usage_cache()->GetDirty(usage_file_path, &dirty)); - EXPECT_EQ(1u, dirty); - EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path, &usage)); - EXPECT_EQ(98214, usage); -} - -TEST_F(FileSystemUsageCacheTest, DecAndGetSizeTest) { - static const int64 size = 71839; - base::FilePath usage_file_path = GetUsageFilePath(); - int64 usage = 0; - ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path, size)); - // DecrementDirty for dirty = 0 is invalid. It returns false. - ASSERT_FALSE(usage_cache()->DecrementDirty(usage_file_path)); - EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path, &usage)); - EXPECT_EQ(size, usage); -} - -TEST_F(FileSystemUsageCacheTest, IncDecAndGetSizeTest) { - static const int64 size = 198491; - base::FilePath usage_file_path = GetUsageFilePath(); - int64 usage = 0; - ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path, size)); - ASSERT_TRUE(usage_cache()->IncrementDirty(usage_file_path)); - ASSERT_TRUE(usage_cache()->DecrementDirty(usage_file_path)); - EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path, &usage)); - EXPECT_EQ(size, usage); -} - -TEST_F(FileSystemUsageCacheTest, DecIncAndGetSizeTest) { - base::FilePath usage_file_path = GetUsageFilePath(); - uint32 dirty = 0; - int64 usage = 0; - ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path, 854238)); - // DecrementDirty for dirty = 0 is invalid. It returns false. - ASSERT_FALSE(usage_cache()->DecrementDirty(usage_file_path)); - ASSERT_TRUE(usage_cache()->IncrementDirty(usage_file_path)); - // It tests DecrementDirty (which returns false) has no effect, i.e - // does not make dirty = -1 after DecrementDirty. - EXPECT_TRUE(usage_cache()->GetDirty(usage_file_path, &dirty)); - EXPECT_EQ(1u, dirty); - EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path, &usage)); - EXPECT_EQ(854238, usage); -} - -TEST_F(FileSystemUsageCacheTest, ManyIncsSameDecsAndGetSizeTest) { - static const int64 size = 82412; - base::FilePath usage_file_path = GetUsageFilePath(); - int64 usage = 0; - ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path, size)); - for (int i = 0; i < 20; i++) - ASSERT_TRUE(usage_cache()->IncrementDirty(usage_file_path)); - for (int i = 0; i < 20; i++) - ASSERT_TRUE(usage_cache()->DecrementDirty(usage_file_path)); - EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path, &usage)); - EXPECT_EQ(size, usage); -} - -TEST_F(FileSystemUsageCacheTest, ManyIncsLessDecsAndGetSizeTest) { - uint32 dirty = 0; - int64 usage = 0; - base::FilePath usage_file_path = GetUsageFilePath(); - ASSERT_TRUE(usage_cache()->UpdateUsage(usage_file_path, 19319)); - for (int i = 0; i < 20; i++) - ASSERT_TRUE(usage_cache()->IncrementDirty(usage_file_path)); - for (int i = 0; i < 19; i++) - ASSERT_TRUE(usage_cache()->DecrementDirty(usage_file_path)); - EXPECT_TRUE(usage_cache()->GetDirty(usage_file_path, &dirty)); - EXPECT_EQ(1u, dirty); - EXPECT_TRUE(usage_cache()->GetUsage(usage_file_path, &usage)); - EXPECT_EQ(19319, usage); -} - -TEST_F(FileSystemUsageCacheTest, GetSizeWithoutCacheFileTest) { - int64 usage = 0; - base::FilePath usage_file_path = GetUsageFilePath(); - EXPECT_FALSE(usage_cache()->GetUsage(usage_file_path, &usage)); -} - -TEST_F(FileSystemUsageCacheTest, IncrementDirtyWithoutCacheFileTest) { - base::FilePath usage_file_path = GetUsageFilePath(); - EXPECT_FALSE(usage_cache()->IncrementDirty(usage_file_path)); -} - -TEST_F(FileSystemUsageCacheTest, DecrementDirtyWithoutCacheFileTest) { - base::FilePath usage_file_path = GetUsageFilePath(); - EXPECT_FALSE(usage_cache()->IncrementDirty(usage_file_path)); -} - -} // namespace fileapi diff --git a/webkit/browser/fileapi/isolated_context_unittest.cc b/webkit/browser/fileapi/isolated_context_unittest.cc deleted file mode 100644 index 156181d..0000000 --- a/webkit/browser/fileapi/isolated_context_unittest.cc +++ /dev/null @@ -1,345 +0,0 @@ -// Copyright (c) 2012 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 <string> - -#include "base/basictypes.h" -#include "base/logging.h" -#include "testing/gtest/include/gtest/gtest.h" -#include "webkit/browser/fileapi/file_system_url.h" -#include "webkit/browser/fileapi/isolated_context.h" - -#define FPL(x) FILE_PATH_LITERAL(x) - -#if defined(FILE_PATH_USES_DRIVE_LETTERS) -#define DRIVE FPL("C:") -#else -#define DRIVE -#endif - -namespace fileapi { - -typedef IsolatedContext::MountPointInfo FileInfo; - -namespace { - -const base::FilePath kTestPaths[] = { - base::FilePath(DRIVE FPL("/a/b.txt")), - base::FilePath(DRIVE FPL("/c/d/e")), - base::FilePath(DRIVE FPL("/h/")), - base::FilePath(DRIVE FPL("/")), -#if defined(FILE_PATH_USES_WIN_SEPARATORS) - base::FilePath(DRIVE FPL("\\foo\\bar")), - base::FilePath(DRIVE FPL("\\")), -#endif - // For duplicated base name test. - base::FilePath(DRIVE FPL("/")), - base::FilePath(DRIVE FPL("/f/e")), - base::FilePath(DRIVE FPL("/f/b.txt")), -}; - -} // namespace - -class IsolatedContextTest : public testing::Test { - public: - IsolatedContextTest() { - for (size_t i = 0; i < arraysize(kTestPaths); ++i) - fileset_.insert(kTestPaths[i].NormalizePathSeparators()); - } - - virtual void SetUp() { - IsolatedContext::FileInfoSet files; - for (size_t i = 0; i < arraysize(kTestPaths); ++i) { - std::string name; - ASSERT_TRUE( - files.AddPath(kTestPaths[i].NormalizePathSeparators(), &name)); - names_.push_back(name); - } - id_ = IsolatedContext::GetInstance()->RegisterDraggedFileSystem(files); - IsolatedContext::GetInstance()->AddReference(id_); - ASSERT_FALSE(id_.empty()); - } - - virtual void TearDown() { - IsolatedContext::GetInstance()->RemoveReference(id_); - } - - IsolatedContext* isolated_context() const { - return IsolatedContext::GetInstance(); - } - - protected: - std::string id_; - std::multiset<base::FilePath> fileset_; - std::vector<std::string> names_; - - private: - DISALLOW_COPY_AND_ASSIGN(IsolatedContextTest); -}; - -TEST_F(IsolatedContextTest, RegisterAndRevokeTest) { - // See if the returned top-level entries match with what we registered. - std::vector<FileInfo> toplevels; - ASSERT_TRUE(isolated_context()->GetDraggedFileInfo(id_, &toplevels)); - ASSERT_EQ(fileset_.size(), toplevels.size()); - for (size_t i = 0; i < toplevels.size(); ++i) { - ASSERT_TRUE(fileset_.find(toplevels[i].path) != fileset_.end()); - } - - // See if the name of each registered kTestPaths (that is what we - // register in SetUp() by RegisterDraggedFileSystem) is properly cracked as - // a valid virtual path in the isolated filesystem. - for (size_t i = 0; i < arraysize(kTestPaths); ++i) { - base::FilePath virtual_path = isolated_context()->CreateVirtualRootPath(id_) - .AppendASCII(names_[i]); - std::string cracked_id; - base::FilePath cracked_path; - FileSystemType cracked_type; - FileSystemMountOption cracked_option; - ASSERT_TRUE(isolated_context()->CrackVirtualPath( - virtual_path, &cracked_id, &cracked_type, &cracked_path, - &cracked_option)); - ASSERT_EQ(kTestPaths[i].NormalizePathSeparators().value(), - cracked_path.value()); - ASSERT_EQ(id_, cracked_id); - ASSERT_EQ(kFileSystemTypeDragged, cracked_type); - } - - // Make sure GetRegisteredPath returns false for id_ since it is - // registered for dragged files. - base::FilePath path; - ASSERT_FALSE(isolated_context()->GetRegisteredPath(id_, &path)); - - // Deref the current one and registering a new one. - isolated_context()->RemoveReference(id_); - - std::string id2 = isolated_context()->RegisterFileSystemForPath( - kFileSystemTypeNativeLocal, base::FilePath(DRIVE FPL("/foo")), NULL); - - // Make sure the GetDraggedFileInfo returns false for both ones. - ASSERT_FALSE(isolated_context()->GetDraggedFileInfo(id2, &toplevels)); - ASSERT_FALSE(isolated_context()->GetDraggedFileInfo(id_, &toplevels)); - - // Make sure the GetRegisteredPath returns true only for the new one. - ASSERT_FALSE(isolated_context()->GetRegisteredPath(id_, &path)); - ASSERT_TRUE(isolated_context()->GetRegisteredPath(id2, &path)); - - // Try registering three more file systems for the same path as id2. - std::string id3 = isolated_context()->RegisterFileSystemForPath( - kFileSystemTypeNativeLocal, path, NULL); - std::string id4 = isolated_context()->RegisterFileSystemForPath( - kFileSystemTypeNativeLocal, path, NULL); - std::string id5 = isolated_context()->RegisterFileSystemForPath( - kFileSystemTypeNativeLocal, path, NULL); - - // Remove file system for id4. - isolated_context()->AddReference(id4); - isolated_context()->RemoveReference(id4); - - // Only id4 should become invalid now. - ASSERT_TRUE(isolated_context()->GetRegisteredPath(id2, &path)); - ASSERT_TRUE(isolated_context()->GetRegisteredPath(id3, &path)); - ASSERT_FALSE(isolated_context()->GetRegisteredPath(id4, &path)); - ASSERT_TRUE(isolated_context()->GetRegisteredPath(id5, &path)); - - // Revoke file system id5, after adding multiple references. - isolated_context()->AddReference(id5); - isolated_context()->AddReference(id5); - isolated_context()->AddReference(id5); - isolated_context()->RevokeFileSystem(id5); - - // No matter how many references we add id5 must be invalid now. - ASSERT_TRUE(isolated_context()->GetRegisteredPath(id2, &path)); - ASSERT_TRUE(isolated_context()->GetRegisteredPath(id3, &path)); - ASSERT_FALSE(isolated_context()->GetRegisteredPath(id4, &path)); - ASSERT_FALSE(isolated_context()->GetRegisteredPath(id5, &path)); - - // Revoke the file systems by path. - isolated_context()->RevokeFileSystemByPath(path); - - // Now all the file systems associated to the path must be invalid. - ASSERT_FALSE(isolated_context()->GetRegisteredPath(id2, &path)); - ASSERT_FALSE(isolated_context()->GetRegisteredPath(id3, &path)); - ASSERT_FALSE(isolated_context()->GetRegisteredPath(id4, &path)); - ASSERT_FALSE(isolated_context()->GetRegisteredPath(id5, &path)); -} - -TEST_F(IsolatedContextTest, CrackWithRelativePaths) { - const struct { - base::FilePath::StringType path; - bool valid; - } relatives[] = { - { FPL("foo"), true }, - { FPL("foo/bar"), true }, - { FPL(".."), false }, - { FPL("foo/.."), false }, - { FPL("foo/../bar"), false }, -#if defined(FILE_PATH_USES_WIN_SEPARATORS) -# define SHOULD_FAIL_WITH_WIN_SEPARATORS false -#else -# define SHOULD_FAIL_WITH_WIN_SEPARATORS true -#endif - { FPL("foo\\..\\baz"), SHOULD_FAIL_WITH_WIN_SEPARATORS }, - { FPL("foo/..\\baz"), SHOULD_FAIL_WITH_WIN_SEPARATORS }, - }; - - for (size_t i = 0; i < arraysize(kTestPaths); ++i) { - for (size_t j = 0; j < ARRAYSIZE_UNSAFE(relatives); ++j) { - SCOPED_TRACE(testing::Message() << "Testing " - << kTestPaths[i].value() << " " << relatives[j].path); - base::FilePath virtual_path = - isolated_context()->CreateVirtualRootPath(id_).AppendASCII( - names_[i]).Append(relatives[j].path); - std::string cracked_id; - base::FilePath cracked_path; - FileSystemType cracked_type; - FileSystemMountOption cracked_option; - if (!relatives[j].valid) { - ASSERT_FALSE(isolated_context()->CrackVirtualPath( - virtual_path, &cracked_id, &cracked_type, &cracked_path, - &cracked_option)); - continue; - } - ASSERT_TRUE(isolated_context()->CrackVirtualPath( - virtual_path, &cracked_id, &cracked_type, &cracked_path, - &cracked_option)); - ASSERT_EQ(kTestPaths[i].Append(relatives[j].path) - .NormalizePathSeparators().value(), - cracked_path.value()); - ASSERT_EQ(id_, cracked_id); - ASSERT_EQ(kFileSystemTypeDragged, cracked_type); - } - } -} - -TEST_F(IsolatedContextTest, CrackURLWithRelativePaths) { - const struct { - base::FilePath::StringType path; - bool valid; - } relatives[] = { - { FPL("foo"), true }, - { FPL("foo/bar"), true }, - { FPL(".."), false }, - { FPL("foo/.."), false }, - { FPL("foo/../bar"), false }, -#if defined(FILE_PATH_USES_WIN_SEPARATORS) -# define SHOULD_FAIL_WITH_WIN_SEPARATORS false -#else -# define SHOULD_FAIL_WITH_WIN_SEPARATORS true -#endif - { FPL("foo\\..\\baz"), SHOULD_FAIL_WITH_WIN_SEPARATORS }, - { FPL("foo/..\\baz"), SHOULD_FAIL_WITH_WIN_SEPARATORS }, - }; - - for (size_t i = 0; i < arraysize(kTestPaths); ++i) { - for (size_t j = 0; j < ARRAYSIZE_UNSAFE(relatives); ++j) { - SCOPED_TRACE(testing::Message() << "Testing " - << kTestPaths[i].value() << " " << relatives[j].path); - base::FilePath virtual_path = - isolated_context()->CreateVirtualRootPath(id_).AppendASCII( - names_[i]).Append(relatives[j].path); - - FileSystemURL cracked = isolated_context()->CreateCrackedFileSystemURL( - GURL("http://chromium.org"), kFileSystemTypeIsolated, virtual_path); - - ASSERT_EQ(relatives[j].valid, cracked.is_valid()); - - if (!relatives[j].valid) - continue; - ASSERT_EQ(GURL("http://chromium.org"), cracked.origin()); - ASSERT_EQ(kTestPaths[i].Append(relatives[j].path) - .NormalizePathSeparators().value(), - cracked.path().value()); - ASSERT_EQ(virtual_path.NormalizePathSeparators(), cracked.virtual_path()); - ASSERT_EQ(id_, cracked.filesystem_id()); - ASSERT_EQ(kFileSystemTypeDragged, cracked.type()); - ASSERT_EQ(kFileSystemTypeIsolated, cracked.mount_type()); - } - } -} - -TEST_F(IsolatedContextTest, TestWithVirtualRoot) { - std::string cracked_id; - base::FilePath cracked_path; - FileSystemMountOption cracked_option; - - // Trying to crack virtual root "/" returns true but with empty cracked path - // as "/" of the isolated filesystem is a pure virtual directory - // that has no corresponding platform directory. - base::FilePath virtual_path = isolated_context()->CreateVirtualRootPath(id_); - ASSERT_TRUE(isolated_context()->CrackVirtualPath( - virtual_path, &cracked_id, NULL, &cracked_path, &cracked_option)); - ASSERT_EQ(FPL(""), cracked_path.value()); - ASSERT_EQ(id_, cracked_id); - - // Trying to crack "/foo" should fail (because "foo" is not the one - // included in the kTestPaths). - virtual_path = isolated_context()->CreateVirtualRootPath( - id_).AppendASCII("foo"); - ASSERT_FALSE(isolated_context()->CrackVirtualPath( - virtual_path, &cracked_id, NULL, &cracked_path, &cracked_option)); -} - -TEST_F(IsolatedContextTest, CanHandleURL) { - const GURL test_origin("http://chromium.org"); - const base::FilePath test_path(FPL("/mount")); - - // Should handle isolated file system. - EXPECT_TRUE(isolated_context()->HandlesFileSystemMountType( - fileapi::kFileSystemTypeIsolated)); - - // Shouldn't handle the rest. - EXPECT_FALSE(isolated_context()->HandlesFileSystemMountType( - fileapi::kFileSystemTypeExternal)); - EXPECT_FALSE(isolated_context()->HandlesFileSystemMountType( - fileapi::kFileSystemTypeTemporary)); - EXPECT_FALSE(isolated_context()->HandlesFileSystemMountType( - fileapi::kFileSystemTypePersistent)); - EXPECT_FALSE(isolated_context()->HandlesFileSystemMountType( - fileapi::kFileSystemTypeTest)); - // Not even if it's isolated subtype. - EXPECT_FALSE(isolated_context()->HandlesFileSystemMountType( - fileapi::kFileSystemTypeNativeLocal)); - EXPECT_FALSE(isolated_context()->HandlesFileSystemMountType( - fileapi::kFileSystemTypeDragged)); - EXPECT_FALSE(isolated_context()->HandlesFileSystemMountType( - fileapi::kFileSystemTypeNativeMedia)); - EXPECT_FALSE(isolated_context()->HandlesFileSystemMountType( - fileapi::kFileSystemTypeDeviceMedia)); -} - -TEST_F(IsolatedContextTest, VirtualFileSystemTests) { - // Should be able to register empty and non-absolute paths - std::string empty_fsid = isolated_context()->RegisterFileSystemForVirtualPath( - fileapi::kFileSystemTypeIsolated, "_", base::FilePath()); - std::string relative_fsid = - isolated_context()->RegisterFileSystemForVirtualPath( - fileapi::kFileSystemTypeIsolated, "_", - base::FilePath(FPL("relpath"))); - ASSERT_FALSE(empty_fsid.empty()); - ASSERT_FALSE(relative_fsid.empty()); - - // Make sure that filesystem root is not prepended to cracked virtual paths. - base::FilePath database_root = base::FilePath(DRIVE FPL("/database_path")); - std::string database_fsid = - isolated_context()->RegisterFileSystemForVirtualPath( - fileapi::kFileSystemTypeIsolated, "_", database_root); - - base::FilePath test_virtual_path = - base::FilePath().AppendASCII("virtualdir").AppendASCII("virtualfile.txt"); - - base::FilePath whole_virtual_path = - isolated_context()->CreateVirtualRootPath(database_fsid) - .AppendASCII("_").Append(test_virtual_path); - - std::string cracked_id; - base::FilePath cracked_path; - FileSystemMountOption cracked_option; - ASSERT_TRUE(isolated_context()->CrackVirtualPath( - whole_virtual_path, &cracked_id, NULL, &cracked_path, &cracked_option)); - ASSERT_EQ(database_fsid, cracked_id); - ASSERT_EQ(test_virtual_path, cracked_path); -} - -} // namespace fileapi diff --git a/webkit/browser/fileapi/local_file_stream_writer.h b/webkit/browser/fileapi/local_file_stream_writer.h index 65ca9ac..062bd33 100644 --- a/webkit/browser/fileapi/local_file_stream_writer.h +++ b/webkit/browser/fileapi/local_file_stream_writer.h @@ -18,6 +18,10 @@ #include "webkit/browser/fileapi/file_stream_writer.h" #include "webkit/browser/webkit_storage_browser_export.h" +namespace content { +class LocalFileStreamWriterTest; +} + namespace net { class FileStream; } @@ -37,8 +41,8 @@ class WEBKIT_STORAGE_BROWSER_EXPORT LocalFileStreamWriter virtual int Flush(const net::CompletionCallback& callback) OVERRIDE; private: + friend class content::LocalFileStreamWriterTest; friend class FileStreamWriter; - friend class LocalFileStreamWriterTest; LocalFileStreamWriter(base::TaskRunner* task_runner, const base::FilePath& file_path, int64 initial_offset, diff --git a/webkit/browser/fileapi/local_file_stream_writer_unittest.cc b/webkit/browser/fileapi/local_file_stream_writer_unittest.cc deleted file mode 100644 index a414044..0000000 --- a/webkit/browser/fileapi/local_file_stream_writer_unittest.cc +++ /dev/null @@ -1,177 +0,0 @@ -// Copyright (c) 2012 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/browser/fileapi/local_file_stream_writer.h" - -#include <string> - -#include "base/callback.h" -#include "base/file_util.h" -#include "base/files/scoped_temp_dir.h" -#include "base/logging.h" -#include "base/memory/scoped_ptr.h" -#include "base/message_loop/message_loop.h" -#include "base/run_loop.h" -#include "base/threading/thread.h" -#include "net/base/io_buffer.h" -#include "net/base/test_completion_callback.h" -#include "testing/gtest/include/gtest/gtest.h" - -namespace fileapi { - -class LocalFileStreamWriterTest : public testing::Test { - public: - LocalFileStreamWriterTest() - : file_thread_("FileUtilProxyTestFileThread") {} - - virtual void SetUp() OVERRIDE { - ASSERT_TRUE(file_thread_.Start()); - ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); - } - - virtual void TearDown() OVERRIDE { - // Give another chance for deleted streams to perform Close. - base::RunLoop().RunUntilIdle(); - file_thread_.Stop(); - base::RunLoop().RunUntilIdle(); - } - - protected: - base::FilePath Path(const std::string& name) { - return temp_dir_.path().AppendASCII(name); - } - - int WriteStringToWriter(LocalFileStreamWriter* writer, - const std::string& data) { - scoped_refptr<net::StringIOBuffer> buffer(new net::StringIOBuffer(data)); - scoped_refptr<net::DrainableIOBuffer> drainable( - new net::DrainableIOBuffer(buffer.get(), buffer->size())); - - while (drainable->BytesRemaining() > 0) { - net::TestCompletionCallback callback; - int result = writer->Write( - drainable.get(), drainable->BytesRemaining(), callback.callback()); - if (result == net::ERR_IO_PENDING) - result = callback.WaitForResult(); - if (result <= 0) - return result; - drainable->DidConsume(result); - } - return net::OK; - } - - std::string GetFileContent(const base::FilePath& path) { - std::string content; - base::ReadFileToString(path, &content); - return content; - } - - base::FilePath CreateFileWithContent(const std::string& name, - const std::string& data) { - base::FilePath path = Path(name); - base::WriteFile(path, data.c_str(), data.size()); - return path; - } - - base::MessageLoopProxy* file_task_runner() const { - return file_thread_.message_loop_proxy().get(); - } - - LocalFileStreamWriter* CreateWriter(const base::FilePath& path, - int64 offset) { - return new LocalFileStreamWriter(file_task_runner(), path, offset, - FileStreamWriter::OPEN_EXISTING_FILE); - } - - private: - base::MessageLoopForIO message_loop_; - base::Thread file_thread_; - base::ScopedTempDir temp_dir_; -}; - -void NeverCalled(int unused) { - ADD_FAILURE(); -} - -TEST_F(LocalFileStreamWriterTest, Write) { - base::FilePath path = CreateFileWithContent("file_a", std::string()); - scoped_ptr<LocalFileStreamWriter> writer(CreateWriter(path, 0)); - EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "foo")); - EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "bar")); - writer.reset(); - base::RunLoop().RunUntilIdle(); - EXPECT_TRUE(base::PathExists(path)); - EXPECT_EQ("foobar", GetFileContent(path)); -} - -TEST_F(LocalFileStreamWriterTest, WriteMiddle) { - base::FilePath path = CreateFileWithContent("file_a", "foobar"); - scoped_ptr<LocalFileStreamWriter> writer(CreateWriter(path, 2)); - EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "xxx")); - writer.reset(); - base::RunLoop().RunUntilIdle(); - EXPECT_TRUE(base::PathExists(path)); - EXPECT_EQ("foxxxr", GetFileContent(path)); -} - -TEST_F(LocalFileStreamWriterTest, WriteEnd) { - base::FilePath path = CreateFileWithContent("file_a", "foobar"); - scoped_ptr<LocalFileStreamWriter> writer(CreateWriter(path, 6)); - EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "xxx")); - writer.reset(); - base::RunLoop().RunUntilIdle(); - EXPECT_TRUE(base::PathExists(path)); - EXPECT_EQ("foobarxxx", GetFileContent(path)); -} - -TEST_F(LocalFileStreamWriterTest, WriteFailForNonexistingFile) { - base::FilePath path = Path("file_a"); - ASSERT_FALSE(base::PathExists(path)); - scoped_ptr<LocalFileStreamWriter> writer(CreateWriter(path, 0)); - EXPECT_EQ(net::ERR_FILE_NOT_FOUND, WriteStringToWriter(writer.get(), "foo")); - writer.reset(); - base::RunLoop().RunUntilIdle(); - EXPECT_FALSE(base::PathExists(path)); -} - -TEST_F(LocalFileStreamWriterTest, CancelBeforeOperation) { - base::FilePath path = Path("file_a"); - scoped_ptr<LocalFileStreamWriter> writer(CreateWriter(path, 0)); - // Cancel immediately fails when there's no in-flight operation. - int cancel_result = writer->Cancel(base::Bind(&NeverCalled)); - EXPECT_EQ(net::ERR_UNEXPECTED, cancel_result); -} - -TEST_F(LocalFileStreamWriterTest, CancelAfterFinishedOperation) { - base::FilePath path = CreateFileWithContent("file_a", std::string()); - scoped_ptr<LocalFileStreamWriter> writer(CreateWriter(path, 0)); - EXPECT_EQ(net::OK, WriteStringToWriter(writer.get(), "foo")); - - // Cancel immediately fails when there's no in-flight operation. - int cancel_result = writer->Cancel(base::Bind(&NeverCalled)); - EXPECT_EQ(net::ERR_UNEXPECTED, cancel_result); - - writer.reset(); - base::RunLoop().RunUntilIdle(); - // Write operation is already completed. - EXPECT_TRUE(base::PathExists(path)); - EXPECT_EQ("foo", GetFileContent(path)); -} - -TEST_F(LocalFileStreamWriterTest, CancelWrite) { - base::FilePath path = CreateFileWithContent("file_a", "foobar"); - scoped_ptr<LocalFileStreamWriter> writer(CreateWriter(path, 0)); - - scoped_refptr<net::StringIOBuffer> buffer(new net::StringIOBuffer("xxx")); - int result = - writer->Write(buffer.get(), buffer->size(), base::Bind(&NeverCalled)); - ASSERT_EQ(net::ERR_IO_PENDING, result); - - net::TestCompletionCallback callback; - writer->Cancel(callback.callback()); - int cancel_result = callback.WaitForResult(); - EXPECT_EQ(net::OK, cancel_result); -} - -} // namespace fileapi diff --git a/webkit/browser/fileapi/native_file_util_unittest.cc b/webkit/browser/fileapi/native_file_util_unittest.cc deleted file mode 100644 index 6850921..0000000 --- a/webkit/browser/fileapi/native_file_util_unittest.cc +++ /dev/null @@ -1,405 +0,0 @@ -// Copyright (c) 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 <set> - -#include "base/file_util.h" -#include "base/files/file.h" -#include "base/files/file_path.h" -#include "base/files/scoped_temp_dir.h" -#include "testing/gtest/include/gtest/gtest.h" -#include "webkit/browser/fileapi/native_file_util.h" - -namespace fileapi { - -class NativeFileUtilTest : public testing::Test { - public: - NativeFileUtilTest() {} - - virtual void SetUp() { - ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); - } - - protected: - base::FilePath Path() { - return data_dir_.path(); - } - - base::FilePath Path(const char* file_name) { - return data_dir_.path().AppendASCII(file_name); - } - - bool FileExists(const base::FilePath& path) { - return base::PathExists(path) && - !base::DirectoryExists(path); - } - - int64 GetSize(const base::FilePath& path) { - base::File::Info info; - base::GetFileInfo(path, &info); - return info.size; - } - - private: - base::ScopedTempDir data_dir_; - - DISALLOW_COPY_AND_ASSIGN(NativeFileUtilTest); -}; - -TEST_F(NativeFileUtilTest, CreateCloseAndDeleteFile) { - base::FilePath file_name = Path("test_file"); - int flags = base::File::FLAG_WRITE | base::File::FLAG_ASYNC; - base::File file = - NativeFileUtil::CreateOrOpen(file_name, base::File::FLAG_CREATE | flags); - ASSERT_TRUE(file.IsValid()); - ASSERT_TRUE(file.created()); - - EXPECT_TRUE(base::PathExists(file_name)); - EXPECT_TRUE(NativeFileUtil::PathExists(file_name)); - EXPECT_EQ(0, GetSize(file_name)); - file.Close(); - - file = NativeFileUtil::CreateOrOpen(file_name, base::File::FLAG_OPEN | flags); - ASSERT_TRUE(file.IsValid()); - ASSERT_FALSE(file.created()); - file.Close(); - - ASSERT_EQ(base::File::FILE_OK, - NativeFileUtil::DeleteFile(file_name)); - EXPECT_FALSE(base::PathExists(file_name)); - EXPECT_FALSE(NativeFileUtil::PathExists(file_name)); -} - -TEST_F(NativeFileUtilTest, EnsureFileExists) { - base::FilePath file_name = Path("foobar"); - bool created = false; - ASSERT_EQ(base::File::FILE_OK, - NativeFileUtil::EnsureFileExists(file_name, &created)); - ASSERT_TRUE(created); - - EXPECT_TRUE(FileExists(file_name)); - EXPECT_EQ(0, GetSize(file_name)); - - ASSERT_EQ(base::File::FILE_OK, - NativeFileUtil::EnsureFileExists(file_name, &created)); - EXPECT_FALSE(created); -} - -TEST_F(NativeFileUtilTest, CreateAndDeleteDirectory) { - base::FilePath dir_name = Path("test_dir"); - ASSERT_EQ(base::File::FILE_OK, - NativeFileUtil::CreateDirectory(dir_name, - false /* exclusive */, - false /* recursive */)); - - EXPECT_TRUE(NativeFileUtil::DirectoryExists(dir_name)); - EXPECT_TRUE(base::DirectoryExists(dir_name)); - - ASSERT_EQ(base::File::FILE_ERROR_EXISTS, - NativeFileUtil::CreateDirectory(dir_name, - true /* exclusive */, - false /* recursive */)); - - ASSERT_EQ(base::File::FILE_OK, - NativeFileUtil::DeleteDirectory(dir_name)); - EXPECT_FALSE(base::DirectoryExists(dir_name)); - EXPECT_FALSE(NativeFileUtil::DirectoryExists(dir_name)); -} - -TEST_F(NativeFileUtilTest, TouchFileAndGetFileInfo) { - base::FilePath file_name = Path("test_file"); - base::File::Info native_info; - ASSERT_EQ(base::File::FILE_ERROR_NOT_FOUND, - NativeFileUtil::GetFileInfo(file_name, &native_info)); - - bool created = false; - ASSERT_EQ(base::File::FILE_OK, - NativeFileUtil::EnsureFileExists(file_name, &created)); - ASSERT_TRUE(created); - - base::File::Info info; - ASSERT_TRUE(base::GetFileInfo(file_name, &info)); - ASSERT_EQ(base::File::FILE_OK, - NativeFileUtil::GetFileInfo(file_name, &native_info)); - ASSERT_EQ(info.size, native_info.size); - ASSERT_EQ(info.is_directory, native_info.is_directory); - ASSERT_EQ(info.is_symbolic_link, native_info.is_symbolic_link); - ASSERT_EQ(info.last_modified, native_info.last_modified); - ASSERT_EQ(info.last_accessed, native_info.last_accessed); - ASSERT_EQ(info.creation_time, native_info.creation_time); - - const base::Time new_accessed = - info.last_accessed + base::TimeDelta::FromHours(10); - const base::Time new_modified = - info.last_modified + base::TimeDelta::FromHours(5); - - EXPECT_EQ(base::File::FILE_OK, - NativeFileUtil::Touch(file_name, - new_accessed, new_modified)); - - ASSERT_TRUE(base::GetFileInfo(file_name, &info)); - EXPECT_EQ(new_accessed, info.last_accessed); - EXPECT_EQ(new_modified, info.last_modified); -} - -TEST_F(NativeFileUtilTest, CreateFileEnumerator) { - base::FilePath path_1 = Path("dir1"); - base::FilePath path_2 = Path("file1"); - base::FilePath path_11 = Path("dir1").AppendASCII("file11"); - base::FilePath path_12 = Path("dir1").AppendASCII("dir12"); - base::FilePath path_121 = - Path("dir1").AppendASCII("dir12").AppendASCII("file121"); - ASSERT_EQ(base::File::FILE_OK, - NativeFileUtil::CreateDirectory(path_1, false, false)); - bool created = false; - ASSERT_EQ(base::File::FILE_OK, - NativeFileUtil::EnsureFileExists(path_2, &created)); - ASSERT_EQ(base::File::FILE_OK, - NativeFileUtil::EnsureFileExists(path_11, &created)); - ASSERT_EQ(base::File::FILE_OK, - NativeFileUtil::CreateDirectory(path_12, false, false)); - ASSERT_EQ(base::File::FILE_OK, - NativeFileUtil::EnsureFileExists(path_121, &created)); - - { - scoped_ptr<FileSystemFileUtil::AbstractFileEnumerator> enumerator = - NativeFileUtil::CreateFileEnumerator(Path(), false); - std::set<base::FilePath> set; - set.insert(path_1); - set.insert(path_2); - for (base::FilePath path = enumerator->Next(); !path.empty(); - path = enumerator->Next()) - EXPECT_EQ(1U, set.erase(path)); - EXPECT_TRUE(set.empty()); - } - - { - scoped_ptr<FileSystemFileUtil::AbstractFileEnumerator> enumerator = - NativeFileUtil::CreateFileEnumerator(Path(), true); - std::set<base::FilePath> set; - set.insert(path_1); - set.insert(path_2); - set.insert(path_11); - set.insert(path_12); - set.insert(path_121); - for (base::FilePath path = enumerator->Next(); !path.empty(); - path = enumerator->Next()) - EXPECT_EQ(1U, set.erase(path)); - EXPECT_TRUE(set.empty()); - } -} - -TEST_F(NativeFileUtilTest, Truncate) { - base::FilePath file_name = Path("truncated"); - bool created = false; - ASSERT_EQ(base::File::FILE_OK, - NativeFileUtil::EnsureFileExists(file_name, &created)); - ASSERT_TRUE(created); - - ASSERT_EQ(base::File::FILE_OK, - NativeFileUtil::Truncate(file_name, 1020)); - - EXPECT_TRUE(FileExists(file_name)); - EXPECT_EQ(1020, GetSize(file_name)); -} - -TEST_F(NativeFileUtilTest, CopyFile) { - base::FilePath from_file = Path("fromfile"); - base::FilePath to_file1 = Path("tofile1"); - base::FilePath to_file2 = Path("tofile2"); - const NativeFileUtil::CopyOrMoveMode nosync = NativeFileUtil::COPY_NOSYNC; - const NativeFileUtil::CopyOrMoveMode sync = NativeFileUtil::COPY_SYNC; - bool created = false; - ASSERT_EQ(base::File::FILE_OK, - NativeFileUtil::EnsureFileExists(from_file, &created)); - ASSERT_TRUE(created); - - ASSERT_EQ(base::File::FILE_OK, - NativeFileUtil::Truncate(from_file, 1020)); - - EXPECT_TRUE(FileExists(from_file)); - EXPECT_EQ(1020, GetSize(from_file)); - - ASSERT_EQ(base::File::FILE_OK, - NativeFileUtil::CopyOrMoveFile( - from_file, to_file1, FileSystemOperation::OPTION_NONE, nosync)); - - ASSERT_EQ(base::File::FILE_OK, - NativeFileUtil::CopyOrMoveFile( - from_file, to_file2, FileSystemOperation::OPTION_NONE, sync)); - - EXPECT_TRUE(FileExists(from_file)); - EXPECT_EQ(1020, GetSize(from_file)); - EXPECT_TRUE(FileExists(to_file1)); - EXPECT_EQ(1020, GetSize(to_file1)); - EXPECT_TRUE(FileExists(to_file2)); - EXPECT_EQ(1020, GetSize(to_file2)); - - base::FilePath dir = Path("dir"); - ASSERT_EQ(base::File::FILE_OK, - NativeFileUtil::CreateDirectory(dir, false, false)); - ASSERT_TRUE(base::DirectoryExists(dir)); - base::FilePath to_dir_file = dir.AppendASCII("file"); - ASSERT_EQ(base::File::FILE_OK, - NativeFileUtil::CopyOrMoveFile( - from_file, to_dir_file, - FileSystemOperation::OPTION_NONE, nosync)); - EXPECT_TRUE(FileExists(to_dir_file)); - EXPECT_EQ(1020, GetSize(to_dir_file)); - - // Following tests are error checking. - // Source doesn't exist. - EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND, - NativeFileUtil::CopyOrMoveFile( - Path("nonexists"), Path("file"), - FileSystemOperation::OPTION_NONE, nosync)); - - // Source is not a file. - EXPECT_EQ(base::File::FILE_ERROR_NOT_A_FILE, - NativeFileUtil::CopyOrMoveFile( - dir, Path("file"), FileSystemOperation::OPTION_NONE, nosync)); - // Destination is not a file. - EXPECT_EQ(base::File::FILE_ERROR_INVALID_OPERATION, - NativeFileUtil::CopyOrMoveFile( - from_file, dir, FileSystemOperation::OPTION_NONE, nosync)); - // Destination's parent doesn't exist. - EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND, - NativeFileUtil::CopyOrMoveFile( - from_file, Path("nodir").AppendASCII("file"), - FileSystemOperation::OPTION_NONE, nosync)); - // Destination's parent is a file. - EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND, - NativeFileUtil::CopyOrMoveFile( - from_file, Path("tofile1").AppendASCII("file"), - FileSystemOperation::OPTION_NONE, nosync)); -} - -TEST_F(NativeFileUtilTest, MoveFile) { - base::FilePath from_file = Path("fromfile"); - base::FilePath to_file = Path("tofile"); - const NativeFileUtil::CopyOrMoveMode move = NativeFileUtil::MOVE; - bool created = false; - ASSERT_EQ(base::File::FILE_OK, - NativeFileUtil::EnsureFileExists(from_file, &created)); - ASSERT_TRUE(created); - - ASSERT_EQ(base::File::FILE_OK, NativeFileUtil::Truncate(from_file, 1020)); - - EXPECT_TRUE(FileExists(from_file)); - EXPECT_EQ(1020, GetSize(from_file)); - - ASSERT_EQ(base::File::FILE_OK, - NativeFileUtil::CopyOrMoveFile( - from_file, to_file, FileSystemOperation::OPTION_NONE, move)); - - EXPECT_FALSE(FileExists(from_file)); - EXPECT_TRUE(FileExists(to_file)); - EXPECT_EQ(1020, GetSize(to_file)); - - ASSERT_EQ(base::File::FILE_OK, - NativeFileUtil::EnsureFileExists(from_file, &created)); - ASSERT_TRUE(FileExists(from_file)); - ASSERT_EQ(base::File::FILE_OK, NativeFileUtil::Truncate(from_file, 1020)); - - base::FilePath dir = Path("dir"); - ASSERT_EQ(base::File::FILE_OK, - NativeFileUtil::CreateDirectory(dir, false, false)); - ASSERT_TRUE(base::DirectoryExists(dir)); - base::FilePath to_dir_file = dir.AppendASCII("file"); - ASSERT_EQ(base::File::FILE_OK, - NativeFileUtil::CopyOrMoveFile( - from_file, to_dir_file, - FileSystemOperation::OPTION_NONE, move)); - EXPECT_FALSE(FileExists(from_file)); - EXPECT_TRUE(FileExists(to_dir_file)); - EXPECT_EQ(1020, GetSize(to_dir_file)); - - // Following is error checking. - // Source doesn't exist. - EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND, - NativeFileUtil::CopyOrMoveFile( - Path("nonexists"), Path("file"), - FileSystemOperation::OPTION_NONE, move)); - - // Source is not a file. - EXPECT_EQ(base::File::FILE_ERROR_NOT_A_FILE, - NativeFileUtil::CopyOrMoveFile( - dir, Path("file"), FileSystemOperation::OPTION_NONE, move)); - ASSERT_EQ(base::File::FILE_OK, - NativeFileUtil::EnsureFileExists(from_file, &created)); - ASSERT_TRUE(FileExists(from_file)); - // Destination is not a file. - EXPECT_EQ(base::File::FILE_ERROR_INVALID_OPERATION, - NativeFileUtil::CopyOrMoveFile( - from_file, dir, FileSystemOperation::OPTION_NONE, move)); - - ASSERT_EQ(base::File::FILE_OK, - NativeFileUtil::EnsureFileExists(from_file, &created)); - ASSERT_TRUE(FileExists(from_file)); - // Destination's parent doesn't exist. - EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND, - NativeFileUtil::CopyOrMoveFile( - from_file, Path("nodir").AppendASCII("file"), - FileSystemOperation::OPTION_NONE, move)); - // Destination's parent is a file. - EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND, - NativeFileUtil::CopyOrMoveFile( - from_file, Path("tofile1").AppendASCII("file"), - FileSystemOperation::OPTION_NONE, move)); -} - -TEST_F(NativeFileUtilTest, PreserveLastModified) { - base::FilePath from_file = Path("fromfile"); - base::FilePath to_file1 = Path("tofile1"); - base::FilePath to_file2 = Path("tofile2"); - base::FilePath to_file3 = Path("tofile3"); - bool created = false; - ASSERT_EQ(base::File::FILE_OK, - NativeFileUtil::EnsureFileExists(from_file, &created)); - ASSERT_TRUE(created); - EXPECT_TRUE(FileExists(from_file)); - - base::File::Info file_info1; - ASSERT_EQ(base::File::FILE_OK, - NativeFileUtil::GetFileInfo(from_file, &file_info1)); - - // Test for copy (nosync). - ASSERT_EQ(base::File::FILE_OK, - NativeFileUtil::CopyOrMoveFile( - from_file, to_file1, - FileSystemOperation::OPTION_PRESERVE_LAST_MODIFIED, - NativeFileUtil::COPY_NOSYNC)); - - base::File::Info file_info2; - ASSERT_TRUE(FileExists(to_file1)); - ASSERT_EQ(base::File::FILE_OK, - NativeFileUtil::GetFileInfo(to_file1, &file_info2)); - EXPECT_EQ(file_info1.last_modified, file_info2.last_modified); - - // Test for copy (sync). - ASSERT_EQ(base::File::FILE_OK, - NativeFileUtil::CopyOrMoveFile( - from_file, to_file2, - FileSystemOperation::OPTION_PRESERVE_LAST_MODIFIED, - NativeFileUtil::COPY_SYNC)); - - ASSERT_TRUE(FileExists(to_file2)); - ASSERT_EQ(base::File::FILE_OK, - NativeFileUtil::GetFileInfo(to_file1, &file_info2)); - EXPECT_EQ(file_info1.last_modified, file_info2.last_modified); - - // Test for move. - ASSERT_EQ(base::File::FILE_OK, - NativeFileUtil::CopyOrMoveFile( - from_file, to_file3, - FileSystemOperation::OPTION_PRESERVE_LAST_MODIFIED, - NativeFileUtil::MOVE)); - - ASSERT_TRUE(FileExists(to_file3)); - ASSERT_EQ(base::File::FILE_OK, - NativeFileUtil::GetFileInfo(to_file2, &file_info2)); - EXPECT_EQ(file_info1.last_modified, file_info2.last_modified); -} - -} // namespace fileapi diff --git a/webkit/browser/fileapi/obfuscated_file_util.h b/webkit/browser/fileapi/obfuscated_file_util.h index 61568d1..fbd460e 100644 --- a/webkit/browser/fileapi/obfuscated_file_util.h +++ b/webkit/browser/fileapi/obfuscated_file_util.h @@ -32,6 +32,7 @@ class TimeTicks; namespace content { class ObfuscatedFileUtilTest; +class QuotaBackendImplTest; } namespace quota { @@ -235,8 +236,8 @@ class WEBKIT_STORAGE_BROWSER_EXPORT_PRIVATE ObfuscatedFileUtil typedef SandboxDirectoryDatabase::FileInfo FileInfo; friend class ObfuscatedFileEnumerator; - friend class QuotaBackendImplTest; friend class content::ObfuscatedFileUtilTest; + friend class content::QuotaBackendImplTest; // Helper method to create an obfuscated file util for regular // (temporary, persistent) file systems. Used only for testing. diff --git a/webkit/browser/fileapi/quota/quota_backend_impl.h b/webkit/browser/fileapi/quota/quota_backend_impl.h index 787f5389..ec4c45a 100644 --- a/webkit/browser/fileapi/quota/quota_backend_impl.h +++ b/webkit/browser/fileapi/quota/quota_backend_impl.h @@ -16,6 +16,10 @@ namespace base { class SequencedTaskRunner; } +namespace content { +class QuotaBackendImplTest; +} + namespace quota { class QuotaManagerProxy; } @@ -60,7 +64,7 @@ class WEBKIT_STORAGE_BROWSER_EXPORT QuotaBackendImpl FileSystemType type) OVERRIDE; private: - friend class QuotaBackendImplTest; + friend class content::QuotaBackendImplTest; struct QuotaReservationInfo { QuotaReservationInfo(const GURL& origin, FileSystemType type, int64 delta); diff --git a/webkit/browser/fileapi/quota/quota_backend_impl_unittest.cc b/webkit/browser/fileapi/quota/quota_backend_impl_unittest.cc deleted file mode 100644 index 9f665e4..0000000 --- a/webkit/browser/fileapi/quota/quota_backend_impl_unittest.cc +++ /dev/null @@ -1,266 +0,0 @@ -// 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 "webkit/browser/fileapi/quota/quota_backend_impl.h" - -#include <string> - -#include "base/files/scoped_temp_dir.h" -#include "base/message_loop/message_loop.h" -#include "testing/gtest/include/gtest/gtest.h" -#include "third_party/leveldatabase/src/helpers/memenv/memenv.h" -#include "third_party/leveldatabase/src/include/leveldb/env.h" -#include "webkit/browser/fileapi/file_system_usage_cache.h" -#include "webkit/browser/fileapi/obfuscated_file_util.h" -#include "webkit/browser/quota/quota_manager_proxy.h" - -namespace fileapi { - -namespace { - -const char kOrigin[] = "http://example.com"; - -bool DidReserveQuota(bool accepted, - base::File::Error* error_out, - int64* delta_out, - base::File::Error error, - int64 delta) { - DCHECK(error_out); - DCHECK(delta_out); - *error_out = error; - *delta_out = delta; - return accepted; -} - -class MockQuotaManagerProxy : public quota::QuotaManagerProxy { - public: - MockQuotaManagerProxy() - : QuotaManagerProxy(NULL, NULL), - storage_modified_count_(0), - usage_(0), quota_(0) {} - - // We don't mock them. - virtual void NotifyOriginInUse(const GURL& origin) OVERRIDE {} - virtual void NotifyOriginNoLongerInUse(const GURL& origin) OVERRIDE {} - virtual void SetUsageCacheEnabled(quota::QuotaClient::ID client_id, - const GURL& origin, - quota::StorageType type, - bool enabled) OVERRIDE {} - - virtual void NotifyStorageModified( - quota::QuotaClient::ID client_id, - const GURL& origin, - quota::StorageType type, - int64 delta) OVERRIDE { - ++storage_modified_count_; - usage_ += delta; - ASSERT_LE(usage_, quota_); - } - - virtual void GetUsageAndQuota( - base::SequencedTaskRunner* original_task_runner, - const GURL& origin, - quota::StorageType type, - const GetUsageAndQuotaCallback& callback) OVERRIDE { - callback.Run(quota::kQuotaStatusOk, usage_, quota_); - } - - int storage_modified_count() { return storage_modified_count_; } - int64 usage() { return usage_; } - void set_usage(int64 usage) { usage_ = usage; } - void set_quota(int64 quota) { quota_ = quota; } - - protected: - virtual ~MockQuotaManagerProxy() {} - - private: - int storage_modified_count_; - int64 usage_; - int64 quota_; - - DISALLOW_COPY_AND_ASSIGN(MockQuotaManagerProxy); -}; - -} // namespace - -class QuotaBackendImplTest : public testing::Test { - public: - QuotaBackendImplTest() - : file_system_usage_cache_(file_task_runner()), - quota_manager_proxy_(new MockQuotaManagerProxy) {} - - virtual void SetUp() OVERRIDE { - ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); - in_memory_env_.reset(leveldb::NewMemEnv(leveldb::Env::Default())); - file_util_.reset(ObfuscatedFileUtil::CreateForTesting( - NULL, data_dir_.path(), in_memory_env_.get(), file_task_runner())); - backend_.reset(new QuotaBackendImpl(file_task_runner(), - file_util_.get(), - &file_system_usage_cache_, - quota_manager_proxy_.get())); - } - - virtual void TearDown() OVERRIDE { - backend_.reset(); - quota_manager_proxy_ = NULL; - file_util_.reset(); - message_loop_.RunUntilIdle(); - } - - protected: - void InitializeForOriginAndType(const GURL& origin, FileSystemType type) { - ASSERT_TRUE(file_util_->InitOriginDatabase(origin, true /* create */)); - ASSERT_TRUE(file_util_->origin_database_ != NULL); - - std::string type_string = - SandboxFileSystemBackendDelegate::GetTypeString(type); - base::File::Error error = base::File::FILE_ERROR_FAILED; - base::FilePath path = file_util_->GetDirectoryForOriginAndType( - origin, type_string, true /* create */, &error); - ASSERT_EQ(base::File::FILE_OK, error); - - ASSERT_TRUE(file_system_usage_cache_.UpdateUsage( - GetUsageCachePath(origin, type), 0)); - } - - base::SequencedTaskRunner* file_task_runner() { - return base::MessageLoopProxy::current().get(); - } - - base::FilePath GetUsageCachePath(const GURL& origin, FileSystemType type) { - base::FilePath path; - base::File::Error error = - backend_->GetUsageCachePath(origin, type, &path); - EXPECT_EQ(base::File::FILE_OK, error); - EXPECT_FALSE(path.empty()); - return path; - } - - base::MessageLoop message_loop_; - base::ScopedTempDir data_dir_; - scoped_ptr<leveldb::Env> in_memory_env_; - scoped_ptr<ObfuscatedFileUtil> file_util_; - FileSystemUsageCache file_system_usage_cache_; - scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_; - scoped_ptr<QuotaBackendImpl> backend_; - - private: - DISALLOW_COPY_AND_ASSIGN(QuotaBackendImplTest); -}; - -TEST_F(QuotaBackendImplTest, ReserveQuota_Basic) { - FileSystemType type = fileapi::kFileSystemTypeTemporary; - InitializeForOriginAndType(GURL(kOrigin), type); - quota_manager_proxy_->set_quota(10000); - - int64 delta = 0; - - const int64 kDelta1 = 1000; - base::File::Error error = base::File::FILE_ERROR_FAILED; - backend_->ReserveQuota(GURL(kOrigin), type, kDelta1, - base::Bind(&DidReserveQuota, true, &error, &delta)); - EXPECT_EQ(base::File::FILE_OK, error); - EXPECT_EQ(kDelta1, delta); - EXPECT_EQ(kDelta1, quota_manager_proxy_->usage()); - - const int64 kDelta2 = -300; - error = base::File::FILE_ERROR_FAILED; - backend_->ReserveQuota(GURL(kOrigin), type, kDelta2, - base::Bind(&DidReserveQuota, true, &error, &delta)); - EXPECT_EQ(base::File::FILE_OK, error); - EXPECT_EQ(kDelta2, delta); - EXPECT_EQ(kDelta1 + kDelta2, quota_manager_proxy_->usage()); - - EXPECT_EQ(2, quota_manager_proxy_->storage_modified_count()); -} - -TEST_F(QuotaBackendImplTest, ReserveQuota_NoSpace) { - FileSystemType type = fileapi::kFileSystemTypeTemporary; - InitializeForOriginAndType(GURL(kOrigin), type); - quota_manager_proxy_->set_quota(100); - - int64 delta = 0; - - const int64 kDelta = 1000; - base::File::Error error = base::File::FILE_ERROR_FAILED; - backend_->ReserveQuota(GURL(kOrigin), type, kDelta, - base::Bind(&DidReserveQuota, true, &error, &delta)); - EXPECT_EQ(base::File::FILE_OK, error); - EXPECT_EQ(100, delta); - EXPECT_EQ(100, quota_manager_proxy_->usage()); - - EXPECT_EQ(1, quota_manager_proxy_->storage_modified_count()); -} - -TEST_F(QuotaBackendImplTest, ReserveQuota_Revert) { - FileSystemType type = fileapi::kFileSystemTypeTemporary; - InitializeForOriginAndType(GURL(kOrigin), type); - quota_manager_proxy_->set_quota(10000); - - int64 delta = 0; - - const int64 kDelta = 1000; - base::File::Error error = base::File::FILE_ERROR_FAILED; - backend_->ReserveQuota(GURL(kOrigin), type, kDelta, - base::Bind(&DidReserveQuota, false, &error, &delta)); - EXPECT_EQ(base::File::FILE_OK, error); - EXPECT_EQ(kDelta, delta); - EXPECT_EQ(0, quota_manager_proxy_->usage()); - - EXPECT_EQ(2, quota_manager_proxy_->storage_modified_count()); -} - -TEST_F(QuotaBackendImplTest, ReleaseReservedQuota) { - FileSystemType type = fileapi::kFileSystemTypeTemporary; - InitializeForOriginAndType(GURL(kOrigin), type); - const int64 kInitialUsage = 2000; - quota_manager_proxy_->set_usage(kInitialUsage); - quota_manager_proxy_->set_quota(10000); - - const int64 kSize = 1000; - backend_->ReleaseReservedQuota(GURL(kOrigin), type, kSize); - EXPECT_EQ(kInitialUsage - kSize, quota_manager_proxy_->usage()); - - EXPECT_EQ(1, quota_manager_proxy_->storage_modified_count()); -} - -TEST_F(QuotaBackendImplTest, CommitQuotaUsage) { - FileSystemType type = fileapi::kFileSystemTypeTemporary; - InitializeForOriginAndType(GURL(kOrigin), type); - quota_manager_proxy_->set_quota(10000); - base::FilePath path = GetUsageCachePath(GURL(kOrigin), type); - - const int64 kDelta1 = 1000; - backend_->CommitQuotaUsage(GURL(kOrigin), type, kDelta1); - EXPECT_EQ(kDelta1, quota_manager_proxy_->usage()); - int64 usage = 0; - EXPECT_TRUE(file_system_usage_cache_.GetUsage(path, &usage)); - EXPECT_EQ(kDelta1, usage); - - const int64 kDelta2 = -300; - backend_->CommitQuotaUsage(GURL(kOrigin), type, kDelta2); - EXPECT_EQ(kDelta1 + kDelta2, quota_manager_proxy_->usage()); - usage = 0; - EXPECT_TRUE(file_system_usage_cache_.GetUsage(path, &usage)); - EXPECT_EQ(kDelta1 + kDelta2, usage); - - EXPECT_EQ(2, quota_manager_proxy_->storage_modified_count()); -} - -TEST_F(QuotaBackendImplTest, DirtyCount) { - FileSystemType type = fileapi::kFileSystemTypeTemporary; - InitializeForOriginAndType(GURL(kOrigin), type); - base::FilePath path = GetUsageCachePath(GURL(kOrigin), type); - - backend_->IncrementDirtyCount(GURL(kOrigin), type); - uint32 dirty = 0; - ASSERT_TRUE(file_system_usage_cache_.GetDirty(path, &dirty)); - EXPECT_EQ(1u, dirty); - - backend_->DecrementDirtyCount(GURL(kOrigin), type); - ASSERT_TRUE(file_system_usage_cache_.GetDirty(path, &dirty)); - EXPECT_EQ(0u, dirty); -} - -} // namespace fileapi diff --git a/webkit/browser/fileapi/quota/quota_reservation_manager.h b/webkit/browser/fileapi/quota/quota_reservation_manager.h index 2fea930..6dc66ec 100644 --- a/webkit/browser/fileapi/quota/quota_reservation_manager.h +++ b/webkit/browser/fileapi/quota/quota_reservation_manager.h @@ -17,6 +17,10 @@ #include "webkit/browser/webkit_storage_browser_export.h" #include "webkit/common/fileapi/file_system_types.h" +namespace content { +class QuotaReservationManagerTest; +} + namespace fileapi { class QuotaReservation; @@ -83,7 +87,7 @@ class WEBKIT_STORAGE_BROWSER_EXPORT QuotaReservationManager { friend class QuotaReservation; friend class QuotaReservationBuffer; - friend class QuotaReservationManagerTest; + friend class content::QuotaReservationManagerTest; void ReserveQuota(const GURL& origin, FileSystemType type, diff --git a/webkit/browser/fileapi/quota/quota_reservation_manager_unittest.cc b/webkit/browser/fileapi/quota/quota_reservation_manager_unittest.cc deleted file mode 100644 index 7b2b738..0000000 --- a/webkit/browser/fileapi/quota/quota_reservation_manager_unittest.cc +++ /dev/null @@ -1,362 +0,0 @@ -// 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 "webkit/browser/fileapi/quota/quota_reservation_manager.h" - -#include "base/bind.h" -#include "base/bind_helpers.h" -#include "base/file_util.h" -#include "base/files/file.h" -#include "base/files/scoped_temp_dir.h" -#include "base/message_loop/message_loop.h" -#include "base/run_loop.h" -#include "testing/gtest/include/gtest/gtest.h" -#include "webkit/browser/fileapi/quota/open_file_handle.h" -#include "webkit/browser/fileapi/quota/quota_reservation.h" - -namespace fileapi { - -namespace { - -const char kOrigin[] = "http://example.com"; -const FileSystemType kType = kFileSystemTypeTemporary; -const int64 kInitialFileSize = 1; - -typedef QuotaReservationManager::ReserveQuotaCallback ReserveQuotaCallback; - -int64 GetFileSize(const base::FilePath& path) { - int64 size = 0; - base::GetFileSize(path, &size); - return size; -} - -void SetFileSize(const base::FilePath& path, int64 size) { - base::File file(path, base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_WRITE); - ASSERT_TRUE(file.IsValid()); - ASSERT_TRUE(file.SetLength(size)); -} - -class FakeBackend : public QuotaReservationManager::QuotaBackend { - public: - FakeBackend() - : on_memory_usage_(kInitialFileSize), - on_disk_usage_(kInitialFileSize) {} - virtual ~FakeBackend() {} - - virtual void ReserveQuota(const GURL& origin, - FileSystemType type, - int64 delta, - const ReserveQuotaCallback& callback) OVERRIDE { - EXPECT_EQ(GURL(kOrigin), origin); - EXPECT_EQ(kType, type); - on_memory_usage_ += delta; - base::MessageLoopProxy::current()->PostTask( - FROM_HERE, - base::Bind(base::IgnoreResult(callback), base::File::FILE_OK, delta)); - } - - virtual void ReleaseReservedQuota(const GURL& origin, - FileSystemType type, - int64 size) OVERRIDE { - EXPECT_LE(0, size); - EXPECT_EQ(GURL(kOrigin), origin); - EXPECT_EQ(kType, type); - on_memory_usage_ -= size; - } - - virtual void CommitQuotaUsage(const GURL& origin, - FileSystemType type, - int64 delta) OVERRIDE { - EXPECT_EQ(GURL(kOrigin), origin); - EXPECT_EQ(kType, type); - on_disk_usage_ += delta; - on_memory_usage_ += delta; - } - - virtual void IncrementDirtyCount(const GURL& origin, - FileSystemType type) OVERRIDE {} - virtual void DecrementDirtyCount(const GURL& origin, - FileSystemType type) OVERRIDE {} - - int64 on_memory_usage() { return on_memory_usage_; } - int64 on_disk_usage() { return on_disk_usage_; } - - private: - int64 on_memory_usage_; - int64 on_disk_usage_; - - DISALLOW_COPY_AND_ASSIGN(FakeBackend); -}; - -class FakeWriter { - public: - explicit FakeWriter(scoped_ptr<OpenFileHandle> handle) - : handle_(handle.Pass()), - path_(handle_->platform_path()), - max_written_offset_(handle_->GetEstimatedFileSize()), - append_mode_write_amount_(0), - dirty_(false) { - } - - ~FakeWriter() { - if (handle_) - EXPECT_FALSE(dirty_); - } - - int64 Truncate(int64 length) { - int64 consumed = 0; - - if (max_written_offset_ < length) { - consumed = length - max_written_offset_; - max_written_offset_ = length; - } - SetFileSize(path_, length); - return consumed; - } - - int64 Write(int64 max_offset) { - dirty_ = true; - - int64 consumed = 0; - if (max_written_offset_ < max_offset) { - consumed = max_offset - max_written_offset_; - max_written_offset_ = max_offset; - } - if (GetFileSize(path_) < max_offset) - SetFileSize(path_, max_offset); - return consumed; - } - - int64 Append(int64 amount) { - dirty_ = true; - append_mode_write_amount_ += amount; - SetFileSize(path_, GetFileSize(path_) + amount); - return amount; - } - - void ReportUsage() { - handle_->UpdateMaxWrittenOffset(max_written_offset_); - handle_->AddAppendModeWriteAmount(append_mode_write_amount_); - max_written_offset_ = handle_->GetEstimatedFileSize(); - append_mode_write_amount_ = 0; - dirty_ = false; - } - - void ClearWithoutUsageReport() { - handle_.reset(); - } - - private: - scoped_ptr<OpenFileHandle> handle_; - base::FilePath path_; - int64 max_written_offset_; - int64 append_mode_write_amount_; - bool dirty_; -}; - -void ExpectSuccess(bool* done, base::File::Error error) { - EXPECT_FALSE(*done); - *done = true; - EXPECT_EQ(base::File::FILE_OK, error); -} - -void RefreshReservation(QuotaReservation* reservation, int64 size) { - DCHECK(reservation); - - bool done = false; - reservation->RefreshReservation(size, base::Bind(&ExpectSuccess, &done)); - base::RunLoop().RunUntilIdle(); - EXPECT_TRUE(done); -} - -} // namespace - -class QuotaReservationManagerTest : public testing::Test { - public: - QuotaReservationManagerTest() {} - virtual ~QuotaReservationManagerTest() {} - - virtual void SetUp() OVERRIDE { - ASSERT_TRUE(work_dir_.CreateUniqueTempDir()); - file_path_ = work_dir_.path().Append(FILE_PATH_LITERAL("hoge")); - SetFileSize(file_path_, kInitialFileSize); - - scoped_ptr<QuotaReservationManager::QuotaBackend> backend(new FakeBackend); - reservation_manager_.reset(new QuotaReservationManager(backend.Pass())); - } - - virtual void TearDown() OVERRIDE { - reservation_manager_.reset(); - } - - FakeBackend* fake_backend() { - return static_cast<FakeBackend*>(reservation_manager_->backend_.get()); - } - - QuotaReservationManager* reservation_manager() { - return reservation_manager_.get(); - } - - const base::FilePath& file_path() const { - return file_path_; - } - - private: - base::MessageLoop message_loop_; - base::ScopedTempDir work_dir_; - base::FilePath file_path_; - scoped_ptr<QuotaReservationManager> reservation_manager_; - - DISALLOW_COPY_AND_ASSIGN(QuotaReservationManagerTest); -}; - -TEST_F(QuotaReservationManagerTest, BasicTest) { - scoped_refptr<QuotaReservation> reservation = - reservation_manager()->CreateReservation(GURL(kOrigin), kType); - - { - RefreshReservation(reservation.get(), 10 + 20 + 3); - int64 cached_reserved_quota = reservation->remaining_quota(); - FakeWriter writer(reservation->GetOpenFileHandle(file_path())); - - cached_reserved_quota -= writer.Write(kInitialFileSize + 10); - EXPECT_LE(0, cached_reserved_quota); - cached_reserved_quota -= writer.Append(20); - EXPECT_LE(0, cached_reserved_quota); - - writer.ReportUsage(); - } - - EXPECT_EQ(3, reservation->remaining_quota()); - EXPECT_EQ(kInitialFileSize + 10 + 20, GetFileSize(file_path())); - EXPECT_EQ(kInitialFileSize + 10 + 20, fake_backend()->on_disk_usage()); - EXPECT_EQ(kInitialFileSize + 10 + 20 + 3, fake_backend()->on_memory_usage()); - - { - RefreshReservation(reservation.get(), 5); - FakeWriter writer(reservation->GetOpenFileHandle(file_path())); - - EXPECT_EQ(0, writer.Truncate(3)); - - writer.ReportUsage(); - } - - EXPECT_EQ(5, reservation->remaining_quota()); - EXPECT_EQ(3, GetFileSize(file_path())); - EXPECT_EQ(3, fake_backend()->on_disk_usage()); - EXPECT_EQ(3 + 5, fake_backend()->on_memory_usage()); - - reservation = NULL; - - EXPECT_EQ(3, fake_backend()->on_memory_usage()); -} - -TEST_F(QuotaReservationManagerTest, MultipleWriter) { - scoped_refptr<QuotaReservation> reservation = - reservation_manager()->CreateReservation(GURL(kOrigin), kType); - - { - RefreshReservation(reservation.get(), 10 + 20 + 30 + 40 + 5); - int64 cached_reserved_quota = reservation->remaining_quota(); - FakeWriter writer1(reservation->GetOpenFileHandle(file_path())); - FakeWriter writer2(reservation->GetOpenFileHandle(file_path())); - FakeWriter writer3(reservation->GetOpenFileHandle(file_path())); - - cached_reserved_quota -= writer1.Write(kInitialFileSize + 10); - EXPECT_LE(0, cached_reserved_quota); - cached_reserved_quota -= writer2.Write(kInitialFileSize + 20); - cached_reserved_quota -= writer3.Append(30); - EXPECT_LE(0, cached_reserved_quota); - cached_reserved_quota -= writer3.Append(40); - EXPECT_LE(0, cached_reserved_quota); - - writer1.ReportUsage(); - writer2.ReportUsage(); - writer3.ReportUsage(); - } - - EXPECT_EQ(kInitialFileSize + 20 + 30 + 40, GetFileSize(file_path())); - EXPECT_EQ(kInitialFileSize + 10 + 20 + 30 + 40 + 5, - fake_backend()->on_memory_usage()); - EXPECT_EQ(kInitialFileSize + 20 + 30 + 40, fake_backend()->on_disk_usage()); - - reservation = NULL; - - EXPECT_EQ(kInitialFileSize + 20 + 30 + 40, fake_backend()->on_disk_usage()); -} - -TEST_F(QuotaReservationManagerTest, MultipleClient) { - scoped_refptr<QuotaReservation> reservation1 = - reservation_manager()->CreateReservation(GURL(kOrigin), kType); - RefreshReservation(reservation1, 10); - int64 cached_reserved_quota1 = reservation1->remaining_quota(); - - scoped_refptr<QuotaReservation> reservation2 = - reservation_manager()->CreateReservation(GURL(kOrigin), kType); - RefreshReservation(reservation2, 20); - int64 cached_reserved_quota2 = reservation2->remaining_quota(); - - scoped_ptr<FakeWriter> writer1( - new FakeWriter(reservation1->GetOpenFileHandle(file_path()))); - - scoped_ptr<FakeWriter> writer2( - new FakeWriter(reservation2->GetOpenFileHandle(file_path()))); - - cached_reserved_quota1 -= writer1->Write(kInitialFileSize + 10); - EXPECT_LE(0, cached_reserved_quota1); - - cached_reserved_quota2 -= writer2->Append(20); - EXPECT_LE(0, cached_reserved_quota2); - - writer1->ReportUsage(); - RefreshReservation(reservation1.get(), 2); - cached_reserved_quota1 = reservation1->remaining_quota(); - - writer2->ReportUsage(); - RefreshReservation(reservation2.get(), 3); - cached_reserved_quota2 = reservation2->remaining_quota(); - - writer1.reset(); - writer2.reset(); - - EXPECT_EQ(kInitialFileSize + 10 + 20, GetFileSize(file_path())); - EXPECT_EQ(kInitialFileSize + 10 + 20 + 2 + 3, - fake_backend()->on_memory_usage()); - EXPECT_EQ(kInitialFileSize + 10 + 20, fake_backend()->on_disk_usage()); - - reservation1 = NULL; - EXPECT_EQ(kInitialFileSize + 10 + 20 + 3, fake_backend()->on_memory_usage()); - - reservation2 = NULL; - EXPECT_EQ(kInitialFileSize + 10 + 20, fake_backend()->on_memory_usage()); -} - -TEST_F(QuotaReservationManagerTest, ClientCrash) { - scoped_refptr<QuotaReservation> reservation1 = - reservation_manager()->CreateReservation(GURL(kOrigin), kType); - RefreshReservation(reservation1.get(), 15); - - scoped_refptr<QuotaReservation> reservation2 = - reservation_manager()->CreateReservation(GURL(kOrigin), kType); - RefreshReservation(reservation2.get(), 20); - - { - FakeWriter writer(reservation1->GetOpenFileHandle(file_path())); - - writer.Write(kInitialFileSize + 10); - - reservation1->OnClientCrash(); - writer.ClearWithoutUsageReport(); - } - reservation1 = NULL; - - EXPECT_EQ(kInitialFileSize + 10, GetFileSize(file_path())); - EXPECT_EQ(kInitialFileSize + 15 + 20, fake_backend()->on_memory_usage()); - EXPECT_EQ(kInitialFileSize + 10, fake_backend()->on_disk_usage()); - - reservation2 = NULL; - EXPECT_EQ(kInitialFileSize + 10, fake_backend()->on_memory_usage()); -} - -} // namespace fileapi diff --git a/webkit/browser/fileapi/sandbox_directory_database.h b/webkit/browser/fileapi/sandbox_directory_database.h index e8a1231..114af1b 100644 --- a/webkit/browser/fileapi/sandbox_directory_database.h +++ b/webkit/browser/fileapi/sandbox_directory_database.h @@ -14,6 +14,10 @@ #include "base/time/time.h" #include "webkit/browser/webkit_storage_browser_export.h" +namespace content { +class SandboxDirectoryDatabaseTest; +} + namespace tracked_objects { class Location; } @@ -105,8 +109,8 @@ class WEBKIT_STORAGE_BROWSER_EXPORT_PRIVATE SandboxDirectoryDatabase { FAIL_ON_CORRUPTION, }; + friend class content::SandboxDirectoryDatabaseTest; friend class ObfuscatedFileUtil; - friend class SandboxDirectoryDatabaseTest; bool Init(RecoveryOption recovery_option); bool RepairDatabase(const std::string& db_path); diff --git a/webkit/browser/fileapi/sandbox_directory_database_unittest.cc b/webkit/browser/fileapi/sandbox_directory_database_unittest.cc deleted file mode 100644 index 0201987..0000000 --- a/webkit/browser/fileapi/sandbox_directory_database_unittest.cc +++ /dev/null @@ -1,672 +0,0 @@ -// Copyright (c) 2012 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/browser/fileapi/sandbox_directory_database.h" - -#include <math.h> -#include <limits> - -#include "base/file_util.h" -#include "base/files/file.h" -#include "base/files/scoped_temp_dir.h" -#include "base/memory/scoped_ptr.h" -#include "base/strings/string_number_conversions.h" -#include "base/strings/string_util.h" -#include "testing/gtest/include/gtest/gtest.h" -#include "third_party/leveldatabase/src/include/leveldb/db.h" -#include "webkit/browser/fileapi/sandbox_database_test_helper.h" -#include "webkit/common/fileapi/file_system_util.h" - -#define FPL(x) FILE_PATH_LITERAL(x) - -namespace fileapi { - -namespace { -const base::FilePath::CharType kDirectoryDatabaseName[] = FPL("Paths"); -} - -class SandboxDirectoryDatabaseTest : public testing::Test { - public: - typedef SandboxDirectoryDatabase::FileId FileId; - typedef SandboxDirectoryDatabase::FileInfo FileInfo; - - SandboxDirectoryDatabaseTest() { - EXPECT_TRUE(base_.CreateUniqueTempDir()); - InitDatabase(); - } - - SandboxDirectoryDatabase* db() { - return db_.get(); - } - - void InitDatabase() { - // Call CloseDatabase() to avoid having multiple database instances for - // single directory at once. - CloseDatabase(); - db_.reset(new SandboxDirectoryDatabase(path(), NULL)); - } - - void CloseDatabase() { - db_.reset(); - } - - base::File::Error AddFileInfo( - FileId parent_id, const base::FilePath::StringType& name) { - FileId file_id; - FileInfo info; - info.parent_id = parent_id; - info.name = name; - return db_->AddFileInfo(info, &file_id); - } - - void CreateDirectory(FileId parent_id, - const base::FilePath::StringType& name, - FileId* file_id_out) { - FileInfo info; - info.parent_id = parent_id; - info.name = name; - ASSERT_EQ(base::File::FILE_OK, db_->AddFileInfo(info, file_id_out)); - } - - void CreateFile(FileId parent_id, - const base::FilePath::StringType& name, - const base::FilePath::StringType& data_path, - FileId* file_id_out) { - FileId file_id; - - FileInfo info; - info.parent_id = parent_id; - info.name = name; - info.data_path = base::FilePath(data_path).NormalizePathSeparators(); - ASSERT_EQ(base::File::FILE_OK, db_->AddFileInfo(info, &file_id)); - - base::FilePath local_path = path().Append(data_path); - if (!base::DirectoryExists(local_path.DirName())) - ASSERT_TRUE(base::CreateDirectory(local_path.DirName())); - - base::File file(local_path, - base::File::FLAG_CREATE | base::File::FLAG_WRITE); - ASSERT_TRUE(file.IsValid()); - ASSERT_TRUE(file.created()); - - if (file_id_out) - *file_id_out = file_id; - } - - void ClearDatabaseAndDirectory() { - db_.reset(); - ASSERT_TRUE(base::DeleteFile(path(), true /* recursive */)); - ASSERT_TRUE(base::CreateDirectory(path())); - db_.reset(new SandboxDirectoryDatabase(path(), NULL)); - } - - bool RepairDatabase() { - return db()->RepairDatabase( - FilePathToString(path().Append(kDirectoryDatabaseName))); - } - - const base::FilePath& path() { - return base_.path(); - } - - // Makes link from |parent_id| to |child_id| with |name|. - void MakeHierarchyLink(FileId parent_id, - FileId child_id, - const base::FilePath::StringType& name) { - ASSERT_TRUE(db()->db_->Put( - leveldb::WriteOptions(), - "CHILD_OF:" + base::Int64ToString(parent_id) + ":" + - FilePathToString(base::FilePath(name)), - base::Int64ToString(child_id)).ok()); - } - - // Deletes link from parent of |file_id| to |file_id|. - void DeleteHierarchyLink(FileId file_id) { - FileInfo file_info; - ASSERT_TRUE(db()->GetFileInfo(file_id, &file_info)); - ASSERT_TRUE(db()->db_->Delete( - leveldb::WriteOptions(), - "CHILD_OF:" + base::Int64ToString(file_info.parent_id) + ":" + - FilePathToString(base::FilePath(file_info.name))).ok()); - } - - protected: - // Common temp base for nondestructive uses. - base::ScopedTempDir base_; - scoped_ptr<SandboxDirectoryDatabase> db_; - - private: - DISALLOW_COPY_AND_ASSIGN(SandboxDirectoryDatabaseTest); -}; - -TEST_F(SandboxDirectoryDatabaseTest, TestMissingFileGetInfo) { - FileId file_id = 888; - FileInfo info; - EXPECT_FALSE(db()->GetFileInfo(file_id, &info)); -} - -TEST_F(SandboxDirectoryDatabaseTest, TestGetRootFileInfoBeforeCreate) { - FileId file_id = 0; - FileInfo info; - EXPECT_TRUE(db()->GetFileInfo(file_id, &info)); - EXPECT_EQ(0, info.parent_id); - EXPECT_TRUE(info.name.empty()); - EXPECT_TRUE(info.data_path.empty()); -} - -TEST_F(SandboxDirectoryDatabaseTest, TestMissingParentAddFileInfo) { - FileId parent_id = 7; - EXPECT_EQ(base::File::FILE_ERROR_NOT_A_DIRECTORY, - AddFileInfo(parent_id, FILE_PATH_LITERAL("foo"))); -} - -TEST_F(SandboxDirectoryDatabaseTest, TestAddNameClash) { - FileInfo info; - FileId file_id; - info.parent_id = 0; - info.name = FILE_PATH_LITERAL("dir 0"); - EXPECT_EQ(base::File::FILE_OK, db()->AddFileInfo(info, &file_id)); - - // Check for name clash in the root directory. - base::FilePath::StringType name = info.name; - EXPECT_EQ(base::File::FILE_ERROR_EXISTS, AddFileInfo(0, name)); - name = FILE_PATH_LITERAL("dir 1"); - EXPECT_EQ(base::File::FILE_OK, AddFileInfo(0, name)); - - name = FILE_PATH_LITERAL("subdir 0"); - EXPECT_EQ(base::File::FILE_OK, AddFileInfo(file_id, name)); - - // Check for name clash in a subdirectory. - EXPECT_EQ(base::File::FILE_ERROR_EXISTS, AddFileInfo(file_id, name)); - name = FILE_PATH_LITERAL("subdir 1"); - EXPECT_EQ(base::File::FILE_OK, AddFileInfo(file_id, name)); -} - -TEST_F(SandboxDirectoryDatabaseTest, TestRenameNoMoveNameClash) { - FileInfo info; - FileId file_id0; - base::FilePath::StringType name0 = FILE_PATH_LITERAL("foo"); - base::FilePath::StringType name1 = FILE_PATH_LITERAL("bar"); - base::FilePath::StringType name2 = FILE_PATH_LITERAL("bas"); - info.parent_id = 0; - info.name = name0; - EXPECT_EQ(base::File::FILE_OK, db()->AddFileInfo(info, &file_id0)); - EXPECT_EQ(base::File::FILE_OK, AddFileInfo(0, name1)); - info.name = name1; - EXPECT_FALSE(db()->UpdateFileInfo(file_id0, info)); - info.name = name2; - EXPECT_TRUE(db()->UpdateFileInfo(file_id0, info)); -} - -TEST_F(SandboxDirectoryDatabaseTest, TestMoveSameNameNameClash) { - FileInfo info; - FileId file_id0; - FileId file_id1; - base::FilePath::StringType name0 = FILE_PATH_LITERAL("foo"); - base::FilePath::StringType name1 = FILE_PATH_LITERAL("bar"); - info.parent_id = 0; - info.name = name0; - EXPECT_EQ(base::File::FILE_OK, db()->AddFileInfo(info, &file_id0)); - info.parent_id = file_id0; - EXPECT_EQ(base::File::FILE_OK, db()->AddFileInfo(info, &file_id1)); - info.parent_id = 0; - EXPECT_FALSE(db()->UpdateFileInfo(file_id1, info)); - info.name = name1; - EXPECT_TRUE(db()->UpdateFileInfo(file_id1, info)); -} - -TEST_F(SandboxDirectoryDatabaseTest, TestMoveRenameNameClash) { - FileInfo info; - FileId file_id0; - FileId file_id1; - base::FilePath::StringType name0 = FILE_PATH_LITERAL("foo"); - base::FilePath::StringType name1 = FILE_PATH_LITERAL("bar"); - base::FilePath::StringType name2 = FILE_PATH_LITERAL("bas"); - info.parent_id = 0; - info.name = name0; - EXPECT_EQ(base::File::FILE_OK, db()->AddFileInfo(info, &file_id0)); - info.parent_id = file_id0; - info.name = name1; - EXPECT_EQ(base::File::FILE_OK, db()->AddFileInfo(info, &file_id1)); - info.parent_id = 0; - info.name = name0; - EXPECT_FALSE(db()->UpdateFileInfo(file_id1, info)); - info.name = name1; - EXPECT_TRUE(db()->UpdateFileInfo(file_id1, info)); - // Also test a successful move+rename. - info.parent_id = file_id0; - info.name = name2; - EXPECT_TRUE(db()->UpdateFileInfo(file_id1, info)); -} - -TEST_F(SandboxDirectoryDatabaseTest, TestRemoveWithChildren) { - FileInfo info; - FileId file_id0; - FileId file_id1; - info.parent_id = 0; - info.name = FILE_PATH_LITERAL("foo"); - EXPECT_EQ(base::File::FILE_OK, db()->AddFileInfo(info, &file_id0)); - info.parent_id = file_id0; - EXPECT_EQ(base::File::FILE_OK, db()->AddFileInfo(info, &file_id1)); - EXPECT_FALSE(db()->RemoveFileInfo(file_id0)); - EXPECT_TRUE(db()->RemoveFileInfo(file_id1)); - EXPECT_TRUE(db()->RemoveFileInfo(file_id0)); -} - -TEST_F(SandboxDirectoryDatabaseTest, TestGetChildWithName) { - FileInfo info; - FileId file_id0; - FileId file_id1; - base::FilePath::StringType name0 = FILE_PATH_LITERAL("foo"); - base::FilePath::StringType name1 = FILE_PATH_LITERAL("bar"); - info.parent_id = 0; - info.name = name0; - EXPECT_EQ(base::File::FILE_OK, db()->AddFileInfo(info, &file_id0)); - info.parent_id = file_id0; - info.name = name1; - EXPECT_EQ(base::File::FILE_OK, db()->AddFileInfo(info, &file_id1)); - EXPECT_NE(file_id0, file_id1); - - FileId check_file_id; - EXPECT_FALSE(db()->GetChildWithName(0, name1, &check_file_id)); - EXPECT_TRUE(db()->GetChildWithName(0, name0, &check_file_id)); - EXPECT_EQ(file_id0, check_file_id); - EXPECT_FALSE(db()->GetChildWithName(file_id0, name0, &check_file_id)); - EXPECT_TRUE(db()->GetChildWithName(file_id0, name1, &check_file_id)); - EXPECT_EQ(file_id1, check_file_id); -} - -TEST_F(SandboxDirectoryDatabaseTest, TestGetFileWithPath) { - FileInfo info; - FileId file_id0; - FileId file_id1; - FileId file_id2; - base::FilePath::StringType name0 = FILE_PATH_LITERAL("foo"); - base::FilePath::StringType name1 = FILE_PATH_LITERAL("bar"); - base::FilePath::StringType name2 = FILE_PATH_LITERAL("dog"); - - info.parent_id = 0; - info.name = name0; - EXPECT_EQ(base::File::FILE_OK, db()->AddFileInfo(info, &file_id0)); - info.parent_id = file_id0; - info.name = name1; - EXPECT_EQ(base::File::FILE_OK, db()->AddFileInfo(info, &file_id1)); - EXPECT_NE(file_id0, file_id1); - info.parent_id = file_id1; - info.name = name2; - EXPECT_EQ(base::File::FILE_OK, db()->AddFileInfo(info, &file_id2)); - EXPECT_NE(file_id0, file_id2); - EXPECT_NE(file_id1, file_id2); - - FileId check_file_id; - base::FilePath path = base::FilePath(name0); - EXPECT_TRUE(db()->GetFileWithPath(path, &check_file_id)); - EXPECT_EQ(file_id0, check_file_id); - - path = path.Append(name1); - EXPECT_TRUE(db()->GetFileWithPath(path, &check_file_id)); - EXPECT_EQ(file_id1, check_file_id); - - path = path.Append(name2); - EXPECT_TRUE(db()->GetFileWithPath(path, &check_file_id)); - EXPECT_EQ(file_id2, check_file_id); -} - -TEST_F(SandboxDirectoryDatabaseTest, TestListChildren) { - // No children in the root. - std::vector<FileId> children; - EXPECT_TRUE(db()->ListChildren(0, &children)); - EXPECT_TRUE(children.empty()); - - // One child in the root. - FileId file_id0; - FileInfo info; - info.parent_id = 0; - info.name = FILE_PATH_LITERAL("foo"); - EXPECT_EQ(base::File::FILE_OK, db()->AddFileInfo(info, &file_id0)); - EXPECT_TRUE(db()->ListChildren(0, &children)); - EXPECT_EQ(children.size(), 1UL); - EXPECT_EQ(children[0], file_id0); - - // Two children in the root. - FileId file_id1; - info.name = FILE_PATH_LITERAL("bar"); - EXPECT_EQ(base::File::FILE_OK, db()->AddFileInfo(info, &file_id1)); - EXPECT_TRUE(db()->ListChildren(0, &children)); - EXPECT_EQ(2UL, children.size()); - if (children[0] == file_id0) { - EXPECT_EQ(children[1], file_id1); - } else { - EXPECT_EQ(children[1], file_id0); - EXPECT_EQ(children[0], file_id1); - } - - // No children in a subdirectory. - EXPECT_TRUE(db()->ListChildren(file_id0, &children)); - EXPECT_TRUE(children.empty()); - - // One child in a subdirectory. - info.parent_id = file_id0; - info.name = FILE_PATH_LITERAL("foo"); - FileId file_id2; - FileId file_id3; - EXPECT_EQ(base::File::FILE_OK, db()->AddFileInfo(info, &file_id2)); - EXPECT_TRUE(db()->ListChildren(file_id0, &children)); - EXPECT_EQ(1UL, children.size()); - EXPECT_EQ(children[0], file_id2); - - // Two children in a subdirectory. - info.name = FILE_PATH_LITERAL("bar"); - EXPECT_EQ(base::File::FILE_OK, db()->AddFileInfo(info, &file_id3)); - EXPECT_TRUE(db()->ListChildren(file_id0, &children)); - EXPECT_EQ(2UL, children.size()); - if (children[0] == file_id2) { - EXPECT_EQ(children[1], file_id3); - } else { - EXPECT_EQ(children[1], file_id2); - EXPECT_EQ(children[0], file_id3); - } -} - -TEST_F(SandboxDirectoryDatabaseTest, TestUpdateModificationTime) { - FileInfo info0; - FileId file_id; - info0.parent_id = 0; - info0.name = FILE_PATH_LITERAL("name"); - info0.data_path = base::FilePath(FILE_PATH_LITERAL("fake path")); - info0.modification_time = base::Time::Now(); - EXPECT_EQ(base::File::FILE_OK, db()->AddFileInfo(info0, &file_id)); - FileInfo info1; - EXPECT_TRUE(db()->GetFileInfo(file_id, &info1)); - EXPECT_EQ(info0.name, info1.name); - EXPECT_EQ(info0.parent_id, info1.parent_id); - EXPECT_EQ(info0.data_path, info1.data_path); - EXPECT_EQ( - floor(info0.modification_time.ToDoubleT()), - info1.modification_time.ToDoubleT()); - - EXPECT_TRUE(db()->UpdateModificationTime(file_id, base::Time::UnixEpoch())); - EXPECT_TRUE(db()->GetFileInfo(file_id, &info1)); - EXPECT_EQ(info0.name, info1.name); - EXPECT_EQ(info0.parent_id, info1.parent_id); - EXPECT_EQ(info0.data_path, info1.data_path); - EXPECT_NE(info0.modification_time, info1.modification_time); - EXPECT_EQ( - info1.modification_time.ToDoubleT(), - floor(base::Time::UnixEpoch().ToDoubleT())); - - EXPECT_FALSE(db()->UpdateModificationTime(999, base::Time::UnixEpoch())); -} - -TEST_F(SandboxDirectoryDatabaseTest, TestSimpleFileOperations) { - FileId file_id = 888; - FileInfo info0; - EXPECT_FALSE(db()->GetFileInfo(file_id, &info0)); - info0.parent_id = 0; - info0.data_path = base::FilePath(FILE_PATH_LITERAL("foo")); - info0.name = FILE_PATH_LITERAL("file name"); - info0.modification_time = base::Time::Now(); - EXPECT_EQ(base::File::FILE_OK, db()->AddFileInfo(info0, &file_id)); - FileInfo info1; - EXPECT_TRUE(db()->GetFileInfo(file_id, &info1)); - EXPECT_EQ(info0.parent_id, info1.parent_id); - EXPECT_EQ(info0.data_path, info1.data_path); - EXPECT_EQ(info0.name, info1.name); - EXPECT_EQ( - floor(info0.modification_time.ToDoubleT()), - info1.modification_time.ToDoubleT()); -} - -TEST_F(SandboxDirectoryDatabaseTest, TestOverwritingMoveFileSrcDirectory) { - FileId directory_id; - FileInfo info0; - info0.parent_id = 0; - info0.name = FILE_PATH_LITERAL("directory"); - info0.modification_time = base::Time::Now(); - EXPECT_EQ(base::File::FILE_OK, db()->AddFileInfo(info0, &directory_id)); - - FileId file_id; - FileInfo info1; - info1.parent_id = 0; - info1.data_path = base::FilePath(FILE_PATH_LITERAL("bar")); - info1.name = FILE_PATH_LITERAL("file"); - info1.modification_time = base::Time::UnixEpoch(); - EXPECT_EQ(base::File::FILE_OK, db()->AddFileInfo(info1, &file_id)); - - EXPECT_FALSE(db()->OverwritingMoveFile(directory_id, file_id)); -} - -TEST_F(SandboxDirectoryDatabaseTest, TestOverwritingMoveFileDestDirectory) { - FileId file_id; - FileInfo info0; - info0.parent_id = 0; - info0.name = FILE_PATH_LITERAL("file"); - info0.data_path = base::FilePath(FILE_PATH_LITERAL("bar")); - info0.modification_time = base::Time::Now(); - EXPECT_EQ(base::File::FILE_OK, db()->AddFileInfo(info0, &file_id)); - - FileId directory_id; - FileInfo info1; - info1.parent_id = 0; - info1.name = FILE_PATH_LITERAL("directory"); - info1.modification_time = base::Time::UnixEpoch(); - EXPECT_EQ(base::File::FILE_OK, db()->AddFileInfo(info1, &directory_id)); - - EXPECT_FALSE(db()->OverwritingMoveFile(file_id, directory_id)); -} - -TEST_F(SandboxDirectoryDatabaseTest, TestOverwritingMoveFileSuccess) { - FileId file_id0; - FileInfo info0; - info0.parent_id = 0; - info0.data_path = base::FilePath(FILE_PATH_LITERAL("foo")); - info0.name = FILE_PATH_LITERAL("file name 0"); - info0.modification_time = base::Time::Now(); - EXPECT_EQ(base::File::FILE_OK, db()->AddFileInfo(info0, &file_id0)); - - FileInfo dir_info; - FileId dir_id; - dir_info.parent_id = 0; - dir_info.name = FILE_PATH_LITERAL("directory name"); - EXPECT_EQ(base::File::FILE_OK, db()->AddFileInfo(dir_info, &dir_id)); - - FileId file_id1; - FileInfo info1; - info1.parent_id = dir_id; - info1.data_path = base::FilePath(FILE_PATH_LITERAL("bar")); - info1.name = FILE_PATH_LITERAL("file name 1"); - info1.modification_time = base::Time::UnixEpoch(); - EXPECT_EQ(base::File::FILE_OK, db()->AddFileInfo(info1, &file_id1)); - - EXPECT_TRUE(db()->OverwritingMoveFile(file_id0, file_id1)); - - FileInfo check_info; - FileId check_id; - - EXPECT_FALSE(db()->GetFileWithPath(base::FilePath(info0.name), &check_id)); - EXPECT_TRUE(db()->GetFileWithPath( - base::FilePath(dir_info.name).Append(info1.name), &check_id)); - EXPECT_TRUE(db()->GetFileInfo(check_id, &check_info)); - - EXPECT_EQ(info0.data_path, check_info.data_path); -} - -TEST_F(SandboxDirectoryDatabaseTest, TestGetNextInteger) { - int64 next = -1; - EXPECT_TRUE(db()->GetNextInteger(&next)); - EXPECT_EQ(0, next); - EXPECT_TRUE(db()->GetNextInteger(&next)); - EXPECT_EQ(1, next); - InitDatabase(); - EXPECT_TRUE(db()->GetNextInteger(&next)); - EXPECT_EQ(2, next); - EXPECT_TRUE(db()->GetNextInteger(&next)); - EXPECT_EQ(3, next); - InitDatabase(); - EXPECT_TRUE(db()->GetNextInteger(&next)); - EXPECT_EQ(4, next); -} - -TEST_F(SandboxDirectoryDatabaseTest, TestConsistencyCheck_Empty) { - EXPECT_TRUE(db()->IsFileSystemConsistent()); - - int64 next = -1; - EXPECT_TRUE(db()->GetNextInteger(&next)); - EXPECT_EQ(0, next); - EXPECT_TRUE(db()->IsFileSystemConsistent()); -} - -TEST_F(SandboxDirectoryDatabaseTest, TestConsistencyCheck_Consistent) { - FileId dir_id; - CreateFile(0, FPL("foo"), FPL("hoge"), NULL); - CreateDirectory(0, FPL("bar"), &dir_id); - CreateFile(dir_id, FPL("baz"), FPL("fuga"), NULL); - CreateFile(dir_id, FPL("fizz"), FPL("buzz"), NULL); - - EXPECT_TRUE(db()->IsFileSystemConsistent()); -} - -TEST_F(SandboxDirectoryDatabaseTest, - TestConsistencyCheck_BackingMultiEntry) { - const base::FilePath::CharType kBackingFileName[] = FPL("the celeb"); - CreateFile(0, FPL("foo"), kBackingFileName, NULL); - - EXPECT_TRUE(db()->IsFileSystemConsistent()); - ASSERT_TRUE(base::DeleteFile(path().Append(kBackingFileName), false)); - CreateFile(0, FPL("bar"), kBackingFileName, NULL); - EXPECT_FALSE(db()->IsFileSystemConsistent()); -} - -TEST_F(SandboxDirectoryDatabaseTest, TestConsistencyCheck_FileLost) { - const base::FilePath::CharType kBackingFileName[] = FPL("hoge"); - CreateFile(0, FPL("foo"), kBackingFileName, NULL); - - EXPECT_TRUE(db()->IsFileSystemConsistent()); - ASSERT_TRUE(base::DeleteFile(path().Append(kBackingFileName), false)); - EXPECT_TRUE(db()->IsFileSystemConsistent()); -} - -TEST_F(SandboxDirectoryDatabaseTest, TestConsistencyCheck_OrphanFile) { - CreateFile(0, FPL("foo"), FPL("hoge"), NULL); - - EXPECT_TRUE(db()->IsFileSystemConsistent()); - - base::File file(path().Append(FPL("Orphan File")), - base::File::FLAG_CREATE | base::File::FLAG_WRITE); - ASSERT_TRUE(file.IsValid()); - ASSERT_TRUE(file.created()); - file.Close(); - - EXPECT_TRUE(db()->IsFileSystemConsistent()); -} - -TEST_F(SandboxDirectoryDatabaseTest, TestConsistencyCheck_RootLoop) { - EXPECT_TRUE(db()->IsFileSystemConsistent()); - MakeHierarchyLink(0, 0, base::FilePath::StringType()); - EXPECT_FALSE(db()->IsFileSystemConsistent()); -} - -TEST_F(SandboxDirectoryDatabaseTest, TestConsistencyCheck_DirectoryLoop) { - FileId dir1_id; - FileId dir2_id; - base::FilePath::StringType dir1_name = FPL("foo"); - CreateDirectory(0, dir1_name, &dir1_id); - CreateDirectory(dir1_id, FPL("bar"), &dir2_id); - - EXPECT_TRUE(db()->IsFileSystemConsistent()); - MakeHierarchyLink(dir2_id, dir1_id, dir1_name); - EXPECT_FALSE(db()->IsFileSystemConsistent()); -} - -TEST_F(SandboxDirectoryDatabaseTest, TestConsistencyCheck_NameMismatch) { - FileId dir_id; - FileId file_id; - CreateDirectory(0, FPL("foo"), &dir_id); - CreateFile(dir_id, FPL("bar"), FPL("hoge/fuga/piyo"), &file_id); - - EXPECT_TRUE(db()->IsFileSystemConsistent()); - DeleteHierarchyLink(file_id); - MakeHierarchyLink(dir_id, file_id, FPL("baz")); - EXPECT_FALSE(db()->IsFileSystemConsistent()); -} - -TEST_F(SandboxDirectoryDatabaseTest, TestConsistencyCheck_WreckedEntries) { - FileId dir1_id; - FileId dir2_id; - CreateDirectory(0, FPL("foo"), &dir1_id); - CreateDirectory(dir1_id, FPL("bar"), &dir2_id); - CreateFile(dir2_id, FPL("baz"), FPL("fizz/buzz"), NULL); - - EXPECT_TRUE(db()->IsFileSystemConsistent()); - DeleteHierarchyLink(dir2_id); // Delete link from |dir1_id| to |dir2_id|. - EXPECT_FALSE(db()->IsFileSystemConsistent()); -} - -TEST_F(SandboxDirectoryDatabaseTest, TestRepairDatabase_Success) { - base::FilePath::StringType kFileName = FPL("bar"); - - FileId file_id_prev; - CreateFile(0, FPL("foo"), FPL("hoge"), NULL); - CreateFile(0, kFileName, FPL("fuga"), &file_id_prev); - - const base::FilePath kDatabaseDirectory = - path().Append(kDirectoryDatabaseName); - CloseDatabase(); - CorruptDatabase(kDatabaseDirectory, leveldb::kDescriptorFile, - 0, std::numeric_limits<size_t>::max()); - InitDatabase(); - EXPECT_FALSE(db()->IsFileSystemConsistent()); - - FileId file_id; - EXPECT_TRUE(db()->GetChildWithName(0, kFileName, &file_id)); - EXPECT_EQ(file_id_prev, file_id); - - EXPECT_TRUE(db()->IsFileSystemConsistent()); -} - -TEST_F(SandboxDirectoryDatabaseTest, TestRepairDatabase_Failure) { - base::FilePath::StringType kFileName = FPL("bar"); - - CreateFile(0, FPL("foo"), FPL("hoge"), NULL); - CreateFile(0, kFileName, FPL("fuga"), NULL); - - const base::FilePath kDatabaseDirectory = - path().Append(kDirectoryDatabaseName); - CloseDatabase(); - CorruptDatabase(kDatabaseDirectory, leveldb::kDescriptorFile, - 0, std::numeric_limits<size_t>::max()); - CorruptDatabase(kDatabaseDirectory, leveldb::kLogFile, - -1, 1); - InitDatabase(); - EXPECT_FALSE(db()->IsFileSystemConsistent()); - - FileId file_id; - EXPECT_FALSE(db()->GetChildWithName(0, kFileName, &file_id)); - EXPECT_TRUE(db()->IsFileSystemConsistent()); -} - -TEST_F(SandboxDirectoryDatabaseTest, TestRepairDatabase_MissingManifest) { - base::FilePath::StringType kFileName = FPL("bar"); - - FileId file_id_prev; - CreateFile(0, FPL("foo"), FPL("hoge"), NULL); - CreateFile(0, kFileName, FPL("fuga"), &file_id_prev); - - const base::FilePath kDatabaseDirectory = - path().Append(kDirectoryDatabaseName); - CloseDatabase(); - - DeleteDatabaseFile(kDatabaseDirectory, leveldb::kDescriptorFile); - - InitDatabase(); - EXPECT_FALSE(db()->IsFileSystemConsistent()); - - FileId file_id; - EXPECT_TRUE(db()->GetChildWithName(0, kFileName, &file_id)); - EXPECT_EQ(file_id_prev, file_id); - - EXPECT_TRUE(db()->IsFileSystemConsistent()); -} - -} // namespace fileapi diff --git a/webkit/browser/fileapi/sandbox_isolated_origin_database_unittest.cc b/webkit/browser/fileapi/sandbox_isolated_origin_database_unittest.cc deleted file mode 100644 index 047a961..0000000 --- a/webkit/browser/fileapi/sandbox_isolated_origin_database_unittest.cc +++ /dev/null @@ -1,41 +0,0 @@ -// 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 "base/basictypes.h" -#include "base/file_util.h" -#include "base/files/scoped_temp_dir.h" -#include "testing/gtest/include/gtest/gtest.h" -#include "webkit/browser/fileapi/sandbox_isolated_origin_database.h" -#include "webkit/browser/fileapi/sandbox_origin_database.h" - -namespace fileapi { - -namespace { -const base::FilePath::CharType kOriginDirectory[] = FILE_PATH_LITERAL("iso"); -} // namespace - -TEST(SandboxIsolatedOriginDatabaseTest, BasicTest) { - base::ScopedTempDir dir; - ASSERT_TRUE(dir.CreateUniqueTempDir()); - - std::string kOrigin("origin"); - SandboxIsolatedOriginDatabase database(kOrigin, dir.path(), - base::FilePath(kOriginDirectory)); - - EXPECT_TRUE(database.HasOriginPath(kOrigin)); - - base::FilePath path1, path2; - - EXPECT_FALSE(database.GetPathForOrigin(std::string(), &path1)); - EXPECT_FALSE(database.GetPathForOrigin("foo", &path1)); - - EXPECT_TRUE(database.HasOriginPath(kOrigin)); - EXPECT_TRUE(database.GetPathForOrigin(kOrigin, &path1)); - EXPECT_TRUE(database.GetPathForOrigin(kOrigin, &path2)); - EXPECT_FALSE(path1.empty()); - EXPECT_FALSE(path2.empty()); - EXPECT_EQ(path1, path2); -} - -} // namespace fileapi diff --git a/webkit/browser/fileapi/sandbox_origin_database_unittest.cc b/webkit/browser/fileapi/sandbox_origin_database_unittest.cc deleted file mode 100644 index dd870eb..0000000 --- a/webkit/browser/fileapi/sandbox_origin_database_unittest.cc +++ /dev/null @@ -1,303 +0,0 @@ -// Copyright (c) 2012 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 <algorithm> -#include <functional> -#include <limits> -#include <string> -#include <vector> - -#include "base/file_util.h" -#include "base/files/file.h" -#include "base/files/file_path.h" -#include "base/files/scoped_temp_dir.h" -#include "base/stl_util.h" -#include "testing/gtest/include/gtest/gtest.h" -#include "third_party/leveldatabase/src/db/filename.h" -#include "third_party/leveldatabase/src/include/leveldb/db.h" -#include "webkit/browser/fileapi/sandbox_database_test_helper.h" -#include "webkit/browser/fileapi/sandbox_origin_database.h" -#include "webkit/common/fileapi/file_system_util.h" - -namespace fileapi { - -namespace { -const base::FilePath::CharType kFileSystemDirName[] = - FILE_PATH_LITERAL("File System"); -const base::FilePath::CharType kOriginDatabaseName[] = - FILE_PATH_LITERAL("Origins"); -} // namespace - -TEST(SandboxOriginDatabaseTest, BasicTest) { - base::ScopedTempDir dir; - ASSERT_TRUE(dir.CreateUniqueTempDir()); - const base::FilePath kFSDir = dir.path().Append(kFileSystemDirName); - EXPECT_FALSE(base::PathExists(kFSDir)); - EXPECT_TRUE(base::CreateDirectory(kFSDir)); - - SandboxOriginDatabase database(kFSDir, NULL); - std::string origin("origin"); - - EXPECT_FALSE(database.HasOriginPath(origin)); - // Double-check to make sure that had no side effects. - EXPECT_FALSE(database.HasOriginPath(origin)); - - base::FilePath path0; - base::FilePath path1; - - // Empty strings aren't valid origins. - EXPECT_FALSE(database.GetPathForOrigin(std::string(), &path0)); - - EXPECT_TRUE(database.GetPathForOrigin(origin, &path0)); - EXPECT_TRUE(database.HasOriginPath(origin)); - EXPECT_TRUE(database.GetPathForOrigin(origin, &path1)); - EXPECT_FALSE(path0.empty()); - EXPECT_FALSE(path1.empty()); - EXPECT_EQ(path0, path1); - - EXPECT_TRUE(base::PathExists(kFSDir.Append(kOriginDatabaseName))); -} - -TEST(SandboxOriginDatabaseTest, TwoPathTest) { - base::ScopedTempDir dir; - ASSERT_TRUE(dir.CreateUniqueTempDir()); - const base::FilePath kFSDir = dir.path().Append(kFileSystemDirName); - EXPECT_FALSE(base::PathExists(kFSDir)); - EXPECT_TRUE(base::CreateDirectory(kFSDir)); - - SandboxOriginDatabase database(kFSDir, NULL); - std::string origin0("origin0"); - std::string origin1("origin1"); - - EXPECT_FALSE(database.HasOriginPath(origin0)); - EXPECT_FALSE(database.HasOriginPath(origin1)); - - base::FilePath path0; - base::FilePath path1; - EXPECT_TRUE(database.GetPathForOrigin(origin0, &path0)); - EXPECT_TRUE(database.HasOriginPath(origin0)); - EXPECT_FALSE(database.HasOriginPath(origin1)); - EXPECT_TRUE(database.GetPathForOrigin(origin1, &path1)); - EXPECT_TRUE(database.HasOriginPath(origin1)); - EXPECT_FALSE(path0.empty()); - EXPECT_FALSE(path1.empty()); - EXPECT_NE(path0, path1); - - EXPECT_TRUE(base::PathExists(kFSDir.Append(kOriginDatabaseName))); -} - -TEST(SandboxOriginDatabaseTest, DropDatabaseTest) { - base::ScopedTempDir dir; - ASSERT_TRUE(dir.CreateUniqueTempDir()); - const base::FilePath kFSDir = dir.path().Append(kFileSystemDirName); - EXPECT_FALSE(base::PathExists(kFSDir)); - EXPECT_TRUE(base::CreateDirectory(kFSDir)); - - SandboxOriginDatabase database(kFSDir, NULL); - std::string origin("origin"); - - EXPECT_FALSE(database.HasOriginPath(origin)); - - base::FilePath path0; - EXPECT_TRUE(database.GetPathForOrigin(origin, &path0)); - EXPECT_TRUE(database.HasOriginPath(origin)); - EXPECT_FALSE(path0.empty()); - - EXPECT_TRUE(base::PathExists(kFSDir.Append(kOriginDatabaseName))); - - database.DropDatabase(); - - base::FilePath path1; - EXPECT_TRUE(database.HasOriginPath(origin)); - EXPECT_TRUE(database.GetPathForOrigin(origin, &path1)); - EXPECT_FALSE(path1.empty()); - EXPECT_EQ(path0, path1); -} - -TEST(SandboxOriginDatabaseTest, DeleteOriginTest) { - base::ScopedTempDir dir; - ASSERT_TRUE(dir.CreateUniqueTempDir()); - const base::FilePath kFSDir = dir.path().Append(kFileSystemDirName); - EXPECT_FALSE(base::PathExists(kFSDir)); - EXPECT_TRUE(base::CreateDirectory(kFSDir)); - - SandboxOriginDatabase database(kFSDir, NULL); - std::string origin("origin"); - - EXPECT_FALSE(database.HasOriginPath(origin)); - EXPECT_TRUE(database.RemovePathForOrigin(origin)); - - base::FilePath path0; - EXPECT_TRUE(database.GetPathForOrigin(origin, &path0)); - EXPECT_TRUE(database.HasOriginPath(origin)); - EXPECT_FALSE(path0.empty()); - - EXPECT_TRUE(database.RemovePathForOrigin(origin)); - EXPECT_FALSE(database.HasOriginPath(origin)); - - base::FilePath path1; - EXPECT_TRUE(database.GetPathForOrigin(origin, &path1)); - EXPECT_FALSE(path1.empty()); - EXPECT_NE(path0, path1); -} - -TEST(SandboxOriginDatabaseTest, ListOriginsTest) { - base::ScopedTempDir dir; - ASSERT_TRUE(dir.CreateUniqueTempDir()); - const base::FilePath kFSDir = dir.path().Append(kFileSystemDirName); - EXPECT_FALSE(base::PathExists(kFSDir)); - EXPECT_TRUE(base::CreateDirectory(kFSDir)); - - std::vector<SandboxOriginDatabase::OriginRecord> origins; - - SandboxOriginDatabase database(kFSDir, NULL); - EXPECT_TRUE(database.ListAllOrigins(&origins)); - EXPECT_TRUE(origins.empty()); - origins.clear(); - - std::string origin0("origin0"); - std::string origin1("origin1"); - - EXPECT_FALSE(database.HasOriginPath(origin0)); - EXPECT_FALSE(database.HasOriginPath(origin1)); - - base::FilePath path0; - base::FilePath path1; - EXPECT_TRUE(database.GetPathForOrigin(origin0, &path0)); - EXPECT_TRUE(database.ListAllOrigins(&origins)); - EXPECT_EQ(origins.size(), 1UL); - EXPECT_EQ(origins[0].origin, origin0); - EXPECT_EQ(origins[0].path, path0); - origins.clear(); - EXPECT_TRUE(database.GetPathForOrigin(origin1, &path1)); - EXPECT_TRUE(database.ListAllOrigins(&origins)); - EXPECT_EQ(origins.size(), 2UL); - if (origins[0].origin == origin0) { - EXPECT_EQ(origins[0].path, path0); - EXPECT_EQ(origins[1].origin, origin1); - EXPECT_EQ(origins[1].path, path1); - } else { - EXPECT_EQ(origins[0].origin, origin1); - EXPECT_EQ(origins[0].path, path1); - EXPECT_EQ(origins[1].origin, origin0); - EXPECT_EQ(origins[1].path, path0); - } -} - -TEST(SandboxOriginDatabaseTest, DatabaseRecoveryTest) { - // Checks if SandboxOriginDatabase properly handles database corruption. - // In this test, we'll register some origins to the origin database, then - // corrupt database and its log file. - // After repairing, the origin database should be consistent even when some - // entries lost. - - base::ScopedTempDir dir; - ASSERT_TRUE(dir.CreateUniqueTempDir()); - const base::FilePath kFSDir = dir.path().Append(kFileSystemDirName); - const base::FilePath kDBDir = kFSDir.Append(kOriginDatabaseName); - EXPECT_FALSE(base::PathExists(kFSDir)); - EXPECT_TRUE(base::CreateDirectory(kFSDir)); - - const std::string kOrigins[] = { - "foo.example.com", - "bar.example.com", - "baz.example.com", - "hoge.example.com", - "fuga.example.com", - }; - - scoped_ptr<SandboxOriginDatabase> database( - new SandboxOriginDatabase(kFSDir, NULL)); - for (size_t i = 0; i < arraysize(kOrigins); ++i) { - base::FilePath path; - EXPECT_FALSE(database->HasOriginPath(kOrigins[i])); - EXPECT_TRUE(database->GetPathForOrigin(kOrigins[i], &path)); - EXPECT_FALSE(path.empty()); - EXPECT_TRUE(database->GetPathForOrigin(kOrigins[i], &path)); - - if (i != 1) - EXPECT_TRUE(base::CreateDirectory(kFSDir.Append(path))); - } - database.reset(); - - const base::FilePath kGarbageDir = kFSDir.AppendASCII("foo"); - const base::FilePath kGarbageFile = kGarbageDir.AppendASCII("bar"); - EXPECT_TRUE(base::CreateDirectory(kGarbageDir)); - base::File file(kGarbageFile, - base::File::FLAG_CREATE | base::File::FLAG_WRITE); - EXPECT_TRUE(file.IsValid()); - file.Close(); - - // Corrupt database itself and last log entry to drop last 1 database - // operation. The database should detect the corruption and should recover - // its consistency after recovery. - CorruptDatabase(kDBDir, leveldb::kDescriptorFile, - 0, std::numeric_limits<size_t>::max()); - CorruptDatabase(kDBDir, leveldb::kLogFile, -1, 1); - - base::FilePath path; - database.reset(new SandboxOriginDatabase(kFSDir, NULL)); - std::vector<SandboxOriginDatabase::OriginRecord> origins_in_db; - EXPECT_TRUE(database->ListAllOrigins(&origins_in_db)); - - // Expect all but last added origin will be repaired back, and kOrigins[1] - // should be dropped due to absence of backing directory. - EXPECT_EQ(arraysize(kOrigins) - 2, origins_in_db.size()); - - const std::string kOrigin("piyo.example.org"); - EXPECT_FALSE(database->HasOriginPath(kOrigin)); - EXPECT_TRUE(database->GetPathForOrigin(kOrigin, &path)); - EXPECT_FALSE(path.empty()); - EXPECT_TRUE(database->HasOriginPath(kOrigin)); - - EXPECT_FALSE(base::PathExists(kGarbageFile)); - EXPECT_FALSE(base::PathExists(kGarbageDir)); -} - -TEST(SandboxOriginDatabaseTest, DatabaseRecoveryForMissingDBFileTest) { - const leveldb::FileType kLevelDBFileTypes[] = { - leveldb::kLogFile, - leveldb::kDBLockFile, - leveldb::kTableFile, - leveldb::kDescriptorFile, - leveldb::kCurrentFile, - leveldb::kTempFile, - leveldb::kInfoLogFile, - }; - - for (size_t i = 0; i < arraysize(kLevelDBFileTypes); ++i) { - base::ScopedTempDir dir; - ASSERT_TRUE(dir.CreateUniqueTempDir()); - const base::FilePath kFSDir = dir.path().Append(kFileSystemDirName); - const base::FilePath kDBDir = kFSDir.Append(kOriginDatabaseName); - EXPECT_FALSE(base::PathExists(kFSDir)); - EXPECT_TRUE(base::CreateDirectory(kFSDir)); - - const std::string kOrigin = "foo.example.com"; - base::FilePath path; - - scoped_ptr<SandboxOriginDatabase> database( - new SandboxOriginDatabase(kFSDir, NULL)); - EXPECT_FALSE(database->HasOriginPath(kOrigin)); - EXPECT_TRUE(database->GetPathForOrigin(kOrigin, &path)); - EXPECT_FALSE(path.empty()); - EXPECT_TRUE(database->GetPathForOrigin(kOrigin, &path)); - EXPECT_TRUE(base::CreateDirectory(kFSDir.Append(path))); - database.reset(); - - DeleteDatabaseFile(kDBDir, kLevelDBFileTypes[i]); - - database.reset(new SandboxOriginDatabase(kFSDir, NULL)); - std::vector<SandboxOriginDatabase::OriginRecord> origins_in_db; - EXPECT_TRUE(database->ListAllOrigins(&origins_in_db)); - - const std::string kOrigin2("piyo.example.org"); - EXPECT_FALSE(database->HasOriginPath(kOrigin2)); - EXPECT_TRUE(database->GetPathForOrigin(kOrigin2, &path)); - EXPECT_FALSE(path.empty()); - EXPECT_TRUE(database->HasOriginPath(kOrigin2)); - } -} - -} // namespace fileapi diff --git a/webkit/browser/fileapi/sandbox_prioritized_origin_database_unittest.cc b/webkit/browser/fileapi/sandbox_prioritized_origin_database_unittest.cc deleted file mode 100644 index c93b839..0000000 --- a/webkit/browser/fileapi/sandbox_prioritized_origin_database_unittest.cc +++ /dev/null @@ -1,214 +0,0 @@ -// 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 "base/basictypes.h" -#include "base/file_util.h" -#include "base/files/scoped_temp_dir.h" -#include "base/platform_file.h" -#include "testing/gtest/include/gtest/gtest.h" -#include "webkit/browser/fileapi/sandbox_origin_database.h" -#include "webkit/browser/fileapi/sandbox_prioritized_origin_database.h" - -namespace fileapi { - -TEST(SandboxPrioritizedOriginDatabaseTest, BasicTest) { - base::ScopedTempDir dir; - base::FilePath path; - ASSERT_TRUE(dir.CreateUniqueTempDir()); - - const std::string kOrigin1("origin1"); - const std::string kOrigin2("origin2"); - - SandboxPrioritizedOriginDatabase database(dir.path(), NULL); - - // Set the kOrigin1 as a parimary origin. - EXPECT_TRUE(database.InitializePrimaryOrigin(kOrigin1)); - - // Add two origins. - EXPECT_TRUE(database.GetPathForOrigin(kOrigin1, &path)); - EXPECT_TRUE(database.GetPathForOrigin(kOrigin2, &path)); - - // Verify them. - EXPECT_TRUE(database.HasOriginPath(kOrigin1)); - EXPECT_TRUE(database.GetPathForOrigin(kOrigin1, &path)); - EXPECT_FALSE(path.empty()); - EXPECT_TRUE(database.HasOriginPath(kOrigin2)); - EXPECT_TRUE(database.GetPathForOrigin(kOrigin2, &path)); - EXPECT_FALSE(path.empty()); - - std::vector<SandboxOriginDatabaseInterface::OriginRecord> origins; - database.ListAllOrigins(&origins); - ASSERT_EQ(2U, origins.size()); - EXPECT_TRUE(origins[0].origin == kOrigin1 || - origins[1].origin == kOrigin1); - EXPECT_TRUE(origins[0].origin == kOrigin2 || - origins[1].origin == kOrigin2); - EXPECT_NE(origins[0].path, origins[1].path); - - // Try remove path for kOrigin1. - database.RemovePathForOrigin(kOrigin1); - - // Verify the removal. - EXPECT_FALSE(database.HasOriginPath(kOrigin1)); - EXPECT_TRUE(database.HasOriginPath(kOrigin2)); - database.ListAllOrigins(&origins); - ASSERT_EQ(1U, origins.size()); - EXPECT_EQ(kOrigin2, origins[0].origin); - - // Try remove path for kOrigin2. - database.RemovePathForOrigin(kOrigin2); - - // Verify the removal. - EXPECT_FALSE(database.HasOriginPath(kOrigin1)); - EXPECT_FALSE(database.HasOriginPath(kOrigin2)); - database.ListAllOrigins(&origins); - EXPECT_TRUE(origins.empty()); -} - -TEST(SandboxPrioritizedOriginDatabaseTest, SetPrimaryLaterTest) { - base::ScopedTempDir dir; - base::FilePath path; - ASSERT_TRUE(dir.CreateUniqueTempDir()); - - const std::string kOrigin1("origin1"); - const std::string kOrigin2("origin2"); - - SandboxPrioritizedOriginDatabase database(dir.path(), NULL); - - EXPECT_TRUE(database.GetPrimaryOrigin().empty()); - - EXPECT_TRUE(database.GetPathForOrigin(kOrigin1, &path)); - EXPECT_TRUE(database.GetPathForOrigin(kOrigin2, &path)); - - // Set the kOrigin1 as a parimary origin. - EXPECT_TRUE(database.InitializePrimaryOrigin(kOrigin1)); - EXPECT_EQ(kOrigin1, database.GetPrimaryOrigin()); - - // Regardless of whether it is initialized as primary or not - // they should just work. - EXPECT_TRUE(database.HasOriginPath(kOrigin1)); - EXPECT_TRUE(database.GetPathForOrigin(kOrigin1, &path)); - EXPECT_FALSE(path.empty()); - EXPECT_TRUE(database.HasOriginPath(kOrigin2)); - EXPECT_TRUE(database.GetPathForOrigin(kOrigin2, &path)); - EXPECT_FALSE(path.empty()); -} - -TEST(SandboxPrioritizedOriginDatabaseTest, LostPrimaryOriginFileTest) { - base::ScopedTempDir dir; - base::FilePath path; - ASSERT_TRUE(dir.CreateUniqueTempDir()); - - const std::string kOrigin1("origin1"); - const std::string kData("foo"); - - SandboxPrioritizedOriginDatabase database(dir.path(), NULL); - - EXPECT_TRUE(database.GetPrimaryOrigin().empty()); - - // Set the kOrigin1 as a parimary origin. - EXPECT_TRUE(database.InitializePrimaryOrigin(kOrigin1)); - EXPECT_EQ(kOrigin1, database.GetPrimaryOrigin()); - - // Make sure it works. - EXPECT_TRUE(database.HasOriginPath(kOrigin1)); - EXPECT_TRUE(database.GetPathForOrigin(kOrigin1, &path)); - - // Reset the database. - database.DropDatabase(); - - // kOrigin1 should still be marked as primary. - EXPECT_TRUE(database.HasOriginPath(kOrigin1)); - EXPECT_TRUE(database.GetPathForOrigin(kOrigin1, &path)); - - // Corrupt the primary origin file. - base::WriteFile(database.primary_origin_file(), kData.data(), kData.size()); - - // Reset the database. - database.DropDatabase(); - - // kOrigin1 is no longer marked as primary, and unfortunately we fail - // to find the data for the origin. - EXPECT_FALSE(database.HasOriginPath(kOrigin1)); -} - -TEST(SandboxPrioritizedOriginDatabaseTest, MigrationTest) { - base::ScopedTempDir dir; - ASSERT_TRUE(dir.CreateUniqueTempDir()); - - const std::string kOrigin1("origin1"); - const std::string kOrigin2("origin2"); - const std::string kFakeDirectoryData1("0123456789"); - const std::string kFakeDirectoryData2("abcde"); - base::FilePath old_dir_db_path1, old_dir_db_path2; - base::FilePath path1, path2; - - // Initialize the directory with two origins using the regular - // SandboxOriginDatabase. - { - SandboxOriginDatabase database_old(dir.path(), NULL); - base::FilePath old_db_path = database_old.GetDatabasePath(); - EXPECT_FALSE(base::PathExists(old_db_path)); - - // Initialize paths for kOrigin1 and kOrigin2. - EXPECT_TRUE(database_old.GetPathForOrigin(kOrigin1, &path1)); - EXPECT_FALSE(path1.empty()); - EXPECT_TRUE(database_old.GetPathForOrigin(kOrigin2, &path2)); - EXPECT_FALSE(path2.empty()); - - EXPECT_TRUE(base::DirectoryExists(old_db_path)); - - // Populate the origin directory with some fake data. - old_dir_db_path1 = dir.path().Append(path1); - ASSERT_TRUE(base::CreateDirectory(old_dir_db_path1)); - EXPECT_EQ(static_cast<int>(kFakeDirectoryData1.size()), - base::WriteFile(old_dir_db_path1.AppendASCII("dummy"), - kFakeDirectoryData1.data(), - kFakeDirectoryData1.size())); - old_dir_db_path2 = dir.path().Append(path2); - ASSERT_TRUE(base::CreateDirectory(old_dir_db_path2)); - EXPECT_EQ(static_cast<int>(kFakeDirectoryData2.size()), - base::WriteFile(old_dir_db_path2.AppendASCII("dummy"), - kFakeDirectoryData2.data(), - kFakeDirectoryData2.size())); - } - - // Re-open the directory using sandboxPrioritizedOriginDatabase. - SandboxPrioritizedOriginDatabase database(dir.path(), NULL); - - // Set the kOrigin1 as a parimary origin. - // (Trying to initialize another origin should fail). - EXPECT_TRUE(database.InitializePrimaryOrigin(kOrigin1)); - EXPECT_FALSE(database.InitializePrimaryOrigin(kOrigin2)); - - EXPECT_EQ(kOrigin1, database.GetPrimaryOrigin()); - - // Regardless of whether the origin is registered as primary or not - // it should just work. - EXPECT_TRUE(database.HasOriginPath(kOrigin1)); - EXPECT_TRUE(database.GetPathForOrigin(kOrigin1, &path1)); - EXPECT_TRUE(database.HasOriginPath(kOrigin2)); - EXPECT_TRUE(database.GetPathForOrigin(kOrigin2, &path2)); - - // The directory content must be kept (or migrated if necessary) as well. - std::string origin_db_data; - base::FilePath dir_db_path = dir.path().Append(path1); - EXPECT_TRUE(base::PathExists(dir_db_path.AppendASCII("dummy"))); - EXPECT_TRUE(base::ReadFileToString( - dir_db_path.AppendASCII("dummy"), &origin_db_data)); - EXPECT_EQ(kFakeDirectoryData1, origin_db_data); - - origin_db_data.clear(); - dir_db_path = dir.path().Append(path2); - EXPECT_TRUE(base::PathExists(dir_db_path.AppendASCII("dummy"))); - EXPECT_TRUE(base::ReadFileToString( - dir_db_path.AppendASCII("dummy"), &origin_db_data)); - EXPECT_EQ(kFakeDirectoryData2, origin_db_data); - - // After the migration the kOrigin1 directory database path must be gone. - EXPECT_FALSE(base::PathExists(old_dir_db_path1)); - EXPECT_TRUE(base::PathExists(old_dir_db_path2)); -} - -} // namespace fileapi diff --git a/webkit/browser/fileapi/timed_task_helper_unittest.cc b/webkit/browser/fileapi/timed_task_helper_unittest.cc deleted file mode 100644 index fa3e84d..0000000 --- a/webkit/browser/fileapi/timed_task_helper_unittest.cc +++ /dev/null @@ -1,83 +0,0 @@ -// 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 "base/basictypes.h" -#include "base/bind.h" -#include "base/location.h" -#include "base/memory/scoped_ptr.h" -#include "base/message_loop/message_loop_proxy.h" -#include "base/run_loop.h" -#include "base/time/time.h" -#include "testing/gtest/include/gtest/gtest.h" -#include "webkit/browser/fileapi/timed_task_helper.h" - -namespace fileapi { - -namespace { - -class Embedder { - public: - Embedder() - : timer_(base::MessageLoopProxy::current().get()), timer_fired_(false) {} - - void OnTimerFired() { - timer_fired_ = true; - } - - TimedTaskHelper* timer() { return &timer_; } - bool timer_fired() const { return timer_fired_; } - - private: - TimedTaskHelper timer_; - bool timer_fired_; -}; - -} // namespace - -TEST(TimedTaskHelper, FireTimerWhenAlive) { - base::MessageLoop message_loop; - Embedder embedder; - - ASSERT_FALSE(embedder.timer_fired()); - ASSERT_FALSE(embedder.timer()->IsRunning()); - - embedder.timer()->Start( - FROM_HERE, - base::TimeDelta::FromSeconds(0), - base::Bind(&Embedder::OnTimerFired, base::Unretained(&embedder))); - - ASSERT_TRUE(embedder.timer()->IsRunning()); - embedder.timer()->Reset(); - ASSERT_TRUE(embedder.timer()->IsRunning()); - - base::RunLoop().RunUntilIdle(); - - ASSERT_TRUE(embedder.timer_fired()); -} - -TEST(TimedTaskHelper, FireTimerWhenAlreadyDeleted) { - base::MessageLoop message_loop; - - // Run message loop after embedder is already deleted to make sure callback - // doesn't cause a crash for use after free. - { - Embedder embedder; - - ASSERT_FALSE(embedder.timer_fired()); - ASSERT_FALSE(embedder.timer()->IsRunning()); - - embedder.timer()->Start( - FROM_HERE, - base::TimeDelta::FromSeconds(0), - base::Bind(&Embedder::OnTimerFired, base::Unretained(&embedder))); - - ASSERT_TRUE(embedder.timer()->IsRunning()); - } - - // At this point the callback is still in the message queue but - // embedder is gone. - base::RunLoop().RunUntilIdle(); -} - -} // namespace fileapi |