Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(35)

Side by Side Diff: webkit/fileapi/obfuscated_file_system_file_util_unittest.cc

Issue 7057032: Integrated obfuscation with quota; all unit tests now pass (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Tiny comment fixes. Created 9 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <algorithm>
5 #include <set> 6 #include <set>
6 #include <string> 7 #include <string>
7 8
8 #include "base/file_path.h" 9 #include "base/file_path.h"
9 #include "base/file_util.h" 10 #include "base/file_util.h"
10 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_ptr.h"
11 #include "base/platform_file.h" 12 #include "base/platform_file.h"
12 #include "base/scoped_temp_dir.h" 13 #include "base/scoped_temp_dir.h"
13 #include "base/sys_string_conversions.h" 14 #include "base/sys_string_conversions.h"
14 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
15 #include "webkit/fileapi/file_system_context.h" 16 #include "webkit/fileapi/file_system_context.h"
16 #include "webkit/fileapi/file_system_operation_context.h" 17 #include "webkit/fileapi/file_system_operation_context.h"
18 #include "webkit/fileapi/file_system_test_helper.h"
17 #include "webkit/fileapi/obfuscated_file_system_file_util.h" 19 #include "webkit/fileapi/obfuscated_file_system_file_util.h"
18 20
19 using namespace fileapi; 21 using namespace fileapi;
20 22
21 namespace { 23 namespace {
22 24
23 FilePath UTF8ToFilePath(const std::string& str) { 25 FilePath UTF8ToFilePath(const std::string& str) {
24 FilePath::StringType result; 26 FilePath::StringType result;
25 #if defined(OS_POSIX) 27 #if defined(OS_POSIX)
26 result = str; 28 result = str;
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 {false, FILE_PATH_LITERAL("file 0"), 38}, 88 {false, FILE_PATH_LITERAL("file 0"), 38},
87 {false, FILE_PATH_LITERAL("file 2"), 60}, 89 {false, FILE_PATH_LITERAL("file 2"), 60},
88 {false, FILE_PATH_LITERAL("file 3"), 0}, 90 {false, FILE_PATH_LITERAL("file 3"), 0},
89 {false, FILE_PATH_LITERAL("dir a/file 0"), 39}, 91 {false, FILE_PATH_LITERAL("dir a/file 0"), 39},
90 {false, FILE_PATH_LITERAL("dir a/dir d/dir e/dir g/file 0"), 40}, 92 {false, FILE_PATH_LITERAL("dir a/dir d/dir e/dir g/file 0"), 40},
91 {false, FILE_PATH_LITERAL("dir a/dir d/dir e/dir g/file 1"), 41}, 93 {false, FILE_PATH_LITERAL("dir a/dir d/dir e/dir g/file 1"), 41},
92 {false, FILE_PATH_LITERAL("dir a/dir d/dir e/dir g/file 2"), 42}, 94 {false, FILE_PATH_LITERAL("dir a/dir d/dir e/dir g/file 2"), 42},
93 {false, FILE_PATH_LITERAL("dir a/dir d/dir e/dir g/file 3"), 50}, 95 {false, FILE_PATH_LITERAL("dir a/dir d/dir e/dir g/file 3"), 50},
94 }; 96 };
95 97
98 struct OriginEnumerationTestRecord {
99 std::string origin_url;
100 bool has_temporary;
101 bool has_persistent;
102 };
103
104 const OriginEnumerationTestRecord kOriginEnumerationTestRecords[] = {
105 {"http://example.com", false, true},
106 {"http://example1.com", true, false},
107 {"https://example1.com", true, true},
108 {"file://", false, true},
109 {"http://example.com:8000", false, true},
110 };
111
96 } // namespace (anonymous) 112 } // namespace (anonymous)
97 113
98 // TODO(ericu): The vast majority of this and the other FSFU subclass tests 114 // TODO(ericu): The vast majority of this and the other FSFU subclass tests
99 // could theoretically be shared. It would basically be a FSFU interface 115 // could theoretically be shared. It would basically be a FSFU interface
100 // compliance test, and only the subclass-specific bits that look into the 116 // compliance test, and only the subclass-specific bits that look into the
101 // implementation would need to be written per-subclass. 117 // implementation would need to be written per-subclass.
102 class ObfuscatedFileSystemFileUtilTest : public testing::Test { 118 class ObfuscatedFileSystemFileUtilTest : public testing::Test {
103 public: 119 public:
104 ObfuscatedFileSystemFileUtilTest() { 120 ObfuscatedFileSystemFileUtilTest()
121 : origin_(GURL("http://www.example.com")),
122 type_(kFileSystemTypeTemporary),
123 test_helper_(origin_, type_) {
105 } 124 }
106 125
107 void SetUp() { 126 void SetUp() {
108 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); 127 ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
109 128
110 obfuscated_file_system_file_util_ = 129 obfuscated_file_system_file_util_ =
111 new ObfuscatedFileSystemFileUtil(data_dir_.path()); 130 new ObfuscatedFileSystemFileUtil(data_dir_.path());
131 test_helper_.SetUp(data_dir_.path(),
132 false, // incognito
133 false, // unlimited quota
134 NULL, // quota::QuotaManagerProxy
135 obfuscated_file_system_file_util_.get());
112 } 136 }
113 137
114 FileSystemOperationContext* NewContext() { 138 FileSystemOperationContext* NewContext() {
115 FileSystemOperationContext *context = 139 FileSystemOperationContext* context = test_helper_.NewOperationContext();
116 new FileSystemOperationContext(NULL, NULL);
117 context->set_src_origin_url(GURL("http://example.com"));
118 context->set_dest_origin_url(GURL("http://example.com"));
119 context->set_src_type(kFileSystemTypeTemporary);
120 context->set_dest_type(kFileSystemTypeTemporary);
121 context->set_allowed_bytes_growth(1024 * 1024); 140 context->set_allowed_bytes_growth(1024 * 1024);
122
123 return context; 141 return context;
124 } 142 }
125 143
126 ObfuscatedFileSystemFileUtil* ofsfu() { 144 ObfuscatedFileSystemFileUtil* ofsfu() {
127 return obfuscated_file_system_file_util_.get(); 145 return obfuscated_file_system_file_util_.get();
128 } 146 }
129 147
130 const FilePath& test_directory() const { 148 const FilePath& test_directory() const {
131 return data_dir_.path(); 149 return data_dir_.path();
132 } 150 }
133 151
152 const GURL& origin_url() const {
153 return origin_;
154 }
155
156 FileSystemType type() const {
157 return type_;
158 }
159
134 int64 GetSize(const FilePath& path) { 160 int64 GetSize(const FilePath& path) {
135 int64 size; 161 int64 size;
136 EXPECT_TRUE(file_util::GetFileSize(path, &size)); 162 EXPECT_TRUE(file_util::GetFileSize(path, &size));
137 return size; 163 return size;
138 } 164 }
139 165
140 void CheckFileAndCloseHandle( 166 void CheckFileAndCloseHandle(
141 const FilePath& virtual_path, PlatformFile file_handle) { 167 const FilePath& virtual_path, PlatformFile file_handle) {
142 scoped_ptr<FileSystemOperationContext> context(NewContext()); 168 scoped_ptr<FileSystemOperationContext> context(NewContext());
143 FilePath local_path; 169 FilePath local_path;
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after
310 context.get(), path, last_access_time, last_modified_time)); 336 context.get(), path, last_access_time, last_modified_time));
311 context.reset(NewContext()); 337 context.reset(NewContext());
312 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetFileInfo( 338 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetFileInfo(
313 context.get(), path, &file_info, &local_path)); 339 context.get(), path, &file_info, &local_path));
314 EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT()); 340 EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT());
315 } 341 }
316 342
317 private: 343 private:
318 ScopedTempDir data_dir_; 344 ScopedTempDir data_dir_;
319 scoped_refptr<ObfuscatedFileSystemFileUtil> obfuscated_file_system_file_util_; 345 scoped_refptr<ObfuscatedFileSystemFileUtil> obfuscated_file_system_file_util_;
346 GURL origin_;
347 FileSystemType type_;
348 FileSystemTestOriginHelper test_helper_;
320 349
321 DISALLOW_COPY_AND_ASSIGN(ObfuscatedFileSystemFileUtilTest); 350 DISALLOW_COPY_AND_ASSIGN(ObfuscatedFileSystemFileUtilTest);
322 }; 351 };
323 352
324 TEST_F(ObfuscatedFileSystemFileUtilTest, TestCreateAndDeleteFile) { 353 TEST_F(ObfuscatedFileSystemFileUtilTest, TestCreateAndDeleteFile) {
325 base::PlatformFile file_handle = base::kInvalidPlatformFileValue; 354 base::PlatformFile file_handle = base::kInvalidPlatformFileValue;
326 bool created; 355 bool created;
327 FilePath path = UTF8ToFilePath("fake/file"); 356 FilePath path = UTF8ToFilePath("fake/file");
328 scoped_ptr<FileSystemOperationContext> context(NewContext()); 357 scoped_ptr<FileSystemOperationContext> context(NewContext());
329 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE; 358 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE;
(...skipping 476 matching lines...) Expand 10 before | Expand all | Expand 10 after
806 base::CreatePlatformFile( 835 base::CreatePlatformFile(
807 local_src_path, file_flags, &created, &error_code); 836 local_src_path, file_flags, &created, &error_code);
808 EXPECT_TRUE(created); 837 EXPECT_TRUE(created);
809 ASSERT_EQ(base::PLATFORM_FILE_OK, error_code); 838 ASSERT_EQ(base::PLATFORM_FILE_OK, error_code);
810 ASSERT_TRUE( 839 ASSERT_TRUE(
811 base::TruncatePlatformFile(file_handle, test_case.data_file_size)); 840 base::TruncatePlatformFile(file_handle, test_case.data_file_size));
812 EXPECT_TRUE(base::ClosePlatformFile(file_handle)); 841 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
813 } 842 }
814 } 843 }
815 844
816 const GURL origin_url("http://example.com"); 845 EXPECT_TRUE(ofsfu()->MigrateFromOldSandbox(origin_url(), type(), root_path));
817 fileapi::FileSystemType type = kFileSystemTypeTemporary;
818 EXPECT_TRUE(ofsfu()->MigrateFromOldSandbox(origin_url, type, root_path));
819 846
820 FilePath new_root = 847 FilePath new_root =
821 test_directory().AppendASCII("000").Append( 848 test_directory().AppendASCII("000").Append(
822 ofsfu()->GetDirectoryNameForType(type)).AppendASCII("Legacy"); 849 ofsfu()->GetDirectoryNameForType(type())).AppendASCII("Legacy");
823 for (size_t i = 0; i < arraysize(kMigrationTestCases); ++i) { 850 for (size_t i = 0; i < arraysize(kMigrationTestCases); ++i) {
824 SCOPED_TRACE(testing::Message() << "Validating kMigrationTestPath " << i); 851 SCOPED_TRACE(testing::Message() << "Validating kMigrationTestPath " << i);
825 const MigrationTestCaseRecord& test_case = kMigrationTestCases[i]; 852 const MigrationTestCaseRecord& test_case = kMigrationTestCases[i];
826 FilePath local_data_path = new_root.Append(test_case.path); 853 FilePath local_data_path = new_root.Append(test_case.path);
827 #if defined(OS_WIN) 854 #if defined(OS_WIN)
828 local_data_path = local_data_path.NormalizeWindowsPathSeparators(); 855 local_data_path = local_data_path.NormalizeWindowsPathSeparators();
829 #endif 856 #endif
830 scoped_ptr<FileSystemOperationContext> context(NewContext()); 857 scoped_ptr<FileSystemOperationContext> context(NewContext());
831 base::PlatformFileInfo ofsfu_file_info; 858 base::PlatformFileInfo ofsfu_file_info;
832 FilePath data_path; 859 FilePath data_path;
(...skipping 11 matching lines...) Expand all
844 ASSERT_TRUE(file_util::GetFileInfo(local_data_path, &platform_file_info)); 871 ASSERT_TRUE(file_util::GetFileInfo(local_data_path, &platform_file_info));
845 EXPECT_EQ(test_case.data_file_size, platform_file_info.size); 872 EXPECT_EQ(test_case.data_file_size, platform_file_info.size);
846 EXPECT_FALSE(platform_file_info.is_directory); 873 EXPECT_FALSE(platform_file_info.is_directory);
847 scoped_ptr<FileSystemOperationContext> context(NewContext()); 874 scoped_ptr<FileSystemOperationContext> context(NewContext());
848 EXPECT_EQ(local_data_path, data_path); 875 EXPECT_EQ(local_data_path, data_path);
849 EXPECT_EQ(platform_file_info.size, ofsfu_file_info.size); 876 EXPECT_EQ(platform_file_info.size, ofsfu_file_info.size);
850 EXPECT_FALSE(ofsfu_file_info.is_directory); 877 EXPECT_FALSE(ofsfu_file_info.is_directory);
851 } 878 }
852 } 879 }
853 } 880 }
881
882 TEST_F(ObfuscatedFileSystemFileUtilTest, TestOriginEnumerator) {
883 scoped_ptr<ObfuscatedFileSystemFileUtil::AbstractOriginEnumerator>
884 enumerator(ofsfu()->CreateOriginEnumerator());
885 EXPECT_TRUE(enumerator.get());
886 EXPECT_EQ(GURL(), enumerator->Next());
887 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypeTemporary));
888 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
889
890 std::set<GURL> origins_expected;
891
892 for (size_t i = 0; i < arraysize(kOriginEnumerationTestRecords); ++i) {
893 SCOPED_TRACE(testing::Message() <<
894 "Validating kOriginEnumerationTestRecords " << i);
895 const OriginEnumerationTestRecord& record =
896 kOriginEnumerationTestRecords[i];
897 GURL origin_url(record.origin_url);
898 origins_expected.insert(origin_url);
899 if (record.has_temporary) {
900 scoped_ptr<FileSystemOperationContext> context(NewContext());
901 context->set_src_origin_url(origin_url);
902 context->set_src_type(kFileSystemTypeTemporary);
903 bool created = false;
904 ASSERT_EQ(base::PLATFORM_FILE_OK,
905 ofsfu()->EnsureFileExists(context.get(),
906 FilePath().AppendASCII("file"), &created));
907 EXPECT_TRUE(created);
908 }
909 if (record.has_persistent) {
910 scoped_ptr<FileSystemOperationContext> context(NewContext());
911 context->set_src_origin_url(origin_url);
912 context->set_src_type(kFileSystemTypePersistent);
913 bool created = false;
914 ASSERT_EQ(base::PLATFORM_FILE_OK,
915 ofsfu()->EnsureFileExists(context.get(),
916 FilePath().AppendASCII("file"), &created));
917 EXPECT_TRUE(created);
918 }
919 }
920 enumerator.reset(ofsfu()->CreateOriginEnumerator());
921 EXPECT_TRUE(enumerator.get());
922 std::set<GURL> origins_found;
923 GURL origin;
924 while (!(origin = enumerator->Next()).is_empty()) {
925 origins_found.insert(origin);
926 SCOPED_TRACE(testing::Message() << "Handling " << origin.spec());
927 bool found = false;
928 for (size_t i = 0; !found && i < arraysize(kOriginEnumerationTestRecords);
929 ++i) {
930 const OriginEnumerationTestRecord& record =
931 kOriginEnumerationTestRecords[i];
932 if (GURL(record.origin_url) != origin)
933 continue;
934 found = true;
935 EXPECT_EQ(record.has_temporary,
936 enumerator->HasFileSystemType(kFileSystemTypeTemporary));
937 EXPECT_EQ(record.has_persistent,
938 enumerator->HasFileSystemType(kFileSystemTypePersistent));
939 }
940 EXPECT_TRUE(found);
941 }
942
943 std::set<GURL> diff;
944 std::set_symmetric_difference(origins_expected.begin(),
945 origins_expected.end(), origins_found.begin(), origins_found.end(),
946 inserter(diff, diff.begin()));
947 EXPECT_TRUE(diff.empty());
948 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698