| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/files/file.h" | 8 #include "base/files/file.h" |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
| 11 #include "base/message_loop/message_loop_proxy.h" | 11 #include "base/message_loop/message_loop_proxy.h" |
| 12 #include "base/platform_file.h" | |
| 13 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 14 #include "base/strings/sys_string_conversions.h" | 13 #include "base/strings/sys_string_conversions.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
| 16 #include "content/public/test/async_file_test_helper.h" | 15 #include "content/public/test/async_file_test_helper.h" |
| 17 #include "content/public/test/test_file_system_context.h" | 16 #include "content/public/test/test_file_system_context.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 19 #include "webkit/browser/fileapi/async_file_util_adapter.h" | 18 #include "webkit/browser/fileapi/async_file_util_adapter.h" |
| 20 #include "webkit/browser/fileapi/file_system_context.h" | 19 #include "webkit/browser/fileapi/file_system_context.h" |
| 21 #include "webkit/browser/fileapi/file_system_file_util.h" | 20 #include "webkit/browser/fileapi/file_system_file_util.h" |
| 22 #include "webkit/browser/fileapi/file_system_operation_context.h" | 21 #include "webkit/browser/fileapi/file_system_operation_context.h" |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 bool DirectoryExists(const char *file_name) { | 90 bool DirectoryExists(const char *file_name) { |
| 92 return base::DirectoryExists(LocalPath(file_name)); | 91 return base::DirectoryExists(LocalPath(file_name)); |
| 93 } | 92 } |
| 94 | 93 |
| 95 int64 GetSize(const char *file_name) { | 94 int64 GetSize(const char *file_name) { |
| 96 base::File::Info info; | 95 base::File::Info info; |
| 97 base::GetFileInfo(LocalPath(file_name), &info); | 96 base::GetFileInfo(LocalPath(file_name), &info); |
| 98 return info.size; | 97 return info.size; |
| 99 } | 98 } |
| 100 | 99 |
| 101 base::File::Error CreateFile(const char* file_name, | 100 base::File CreateFile(const char* file_name) { |
| 102 base::PlatformFile* file_handle, | 101 int file_flags = base::File::FLAG_CREATE | |
| 103 bool* created) { | 102 base::File::FLAG_WRITE | base::File::FLAG_ASYNC; |
| 104 int file_flags = base::PLATFORM_FILE_CREATE | | |
| 105 base::PLATFORM_FILE_WRITE | base::PLATFORM_FILE_ASYNC; | |
| 106 | 103 |
| 107 scoped_ptr<FileSystemOperationContext> context(NewContext()); | 104 scoped_ptr<FileSystemOperationContext> context(NewContext()); |
| 108 return file_util()->CreateOrOpen( | 105 return file_util()->CreateOrOpen(context.get(), CreateURL(file_name), |
| 109 context.get(), | 106 file_flags); |
| 110 CreateURL(file_name), | |
| 111 file_flags, file_handle, created); | |
| 112 } | 107 } |
| 113 | 108 |
| 114 base::File::Error EnsureFileExists(const char* file_name, | 109 base::File::Error EnsureFileExists(const char* file_name, |
| 115 bool* created) { | 110 bool* created) { |
| 116 scoped_ptr<FileSystemOperationContext> context(NewContext()); | 111 scoped_ptr<FileSystemOperationContext> context(NewContext()); |
| 117 return file_util()->EnsureFileExists( | 112 return file_util()->EnsureFileExists(context.get(), |
| 118 context.get(), | 113 CreateURL(file_name), created); |
| 119 CreateURL(file_name), created); | |
| 120 } | 114 } |
| 121 | 115 |
| 122 FileSystemContext* file_system_context() { | 116 FileSystemContext* file_system_context() { |
| 123 return file_system_context_.get(); | 117 return file_system_context_.get(); |
| 124 } | 118 } |
| 125 | 119 |
| 126 private: | 120 private: |
| 127 base::MessageLoop message_loop_; | 121 base::MessageLoop message_loop_; |
| 128 scoped_refptr<FileSystemContext> file_system_context_; | 122 scoped_refptr<FileSystemContext> file_system_context_; |
| 129 base::ScopedTempDir data_dir_; | 123 base::ScopedTempDir data_dir_; |
| 130 | 124 |
| 131 DISALLOW_COPY_AND_ASSIGN(LocalFileUtilTest); | 125 DISALLOW_COPY_AND_ASSIGN(LocalFileUtilTest); |
| 132 }; | 126 }; |
| 133 | 127 |
| 134 TEST_F(LocalFileUtilTest, CreateAndClose) { | 128 TEST_F(LocalFileUtilTest, CreateAndClose) { |
| 135 const char *file_name = "test_file"; | 129 const char *file_name = "test_file"; |
| 136 base::PlatformFile file_handle; | 130 base::File file = CreateFile(file_name); |
| 137 bool created; | 131 ASSERT_TRUE(file.IsValid()); |
| 138 ASSERT_EQ(base::File::FILE_OK, | 132 ASSERT_TRUE(file.created()); |
| 139 CreateFile(file_name, &file_handle, &created)); | |
| 140 ASSERT_TRUE(created); | |
| 141 | 133 |
| 142 EXPECT_TRUE(FileExists(file_name)); | 134 EXPECT_TRUE(FileExists(file_name)); |
| 143 EXPECT_EQ(0, GetSize(file_name)); | 135 EXPECT_EQ(0, GetSize(file_name)); |
| 144 | 136 |
| 145 scoped_ptr<FileSystemOperationContext> context(NewContext()); | 137 scoped_ptr<FileSystemOperationContext> context(NewContext()); |
| 146 EXPECT_EQ(base::File::FILE_OK, | |
| 147 file_util()->Close(context.get(), file_handle)); | |
| 148 } | 138 } |
| 149 | 139 |
| 150 // base::CreateSymbolicLink is only supported on POSIX. | 140 // base::CreateSymbolicLink is only supported on POSIX. |
| 151 #if defined(OS_POSIX) | 141 #if defined(OS_POSIX) |
| 152 TEST_F(LocalFileUtilTest, CreateFailForSymlink) { | 142 TEST_F(LocalFileUtilTest, CreateFailForSymlink) { |
| 153 // Create symlink target file. | 143 // Create symlink target file. |
| 154 const char *target_name = "symlink_target"; | 144 const char *target_name = "symlink_target"; |
| 155 base::PlatformFile target_handle; | 145 base::File target_file = CreateFile(target_name); |
| 156 bool symlink_target_created = false; | 146 ASSERT_TRUE(target_file.IsValid()); |
| 157 ASSERT_EQ(base::File::FILE_OK, | 147 ASSERT_TRUE(target_file.created()); |
| 158 CreateFile(target_name, &target_handle, &symlink_target_created)); | |
| 159 ASSERT_TRUE(symlink_target_created); | |
| 160 base::FilePath target_path = LocalPath(target_name); | 148 base::FilePath target_path = LocalPath(target_name); |
| 161 | 149 |
| 162 // Create symlink where target must be real file. | 150 // Create symlink where target must be real file. |
| 163 const char *symlink_name = "symlink_file"; | 151 const char *symlink_name = "symlink_file"; |
| 164 base::FilePath symlink_path = LocalPath(symlink_name); | 152 base::FilePath symlink_path = LocalPath(symlink_name); |
| 165 ASSERT_TRUE(base::CreateSymbolicLink(target_path, symlink_path)); | 153 ASSERT_TRUE(base::CreateSymbolicLink(target_path, symlink_path)); |
| 166 ASSERT_TRUE(FileExists(symlink_name)); | 154 ASSERT_TRUE(FileExists(symlink_name)); |
| 167 | 155 |
| 168 // Try to open the symlink file which should fail. | 156 // Try to open the symlink file which should fail. |
| 169 scoped_ptr<FileSystemOperationContext> context(NewContext()); | 157 scoped_ptr<FileSystemOperationContext> context(NewContext()); |
| 170 FileSystemURL url = CreateURL(symlink_name); | 158 FileSystemURL url = CreateURL(symlink_name); |
| 171 int file_flags = base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ; | 159 int file_flags = base::File::FLAG_OPEN | base::File::FLAG_READ; |
| 172 base::PlatformFile file_handle; | 160 base::File file = file_util()->CreateOrOpen(context.get(), url, file_flags); |
| 173 bool created = false; | 161 ASSERT_FALSE(file.IsValid()); |
| 174 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND, | 162 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND, file.error_details()); |
| 175 file_util()->CreateOrOpen(context.get(), url, file_flags, | |
| 176 &file_handle, &created)); | |
| 177 EXPECT_FALSE(created); | |
| 178 } | 163 } |
| 179 #endif | 164 #endif |
| 180 | 165 |
| 181 TEST_F(LocalFileUtilTest, EnsureFileExists) { | 166 TEST_F(LocalFileUtilTest, EnsureFileExists) { |
| 182 const char *file_name = "foobar"; | 167 const char *file_name = "foobar"; |
| 183 bool created; | 168 bool created; |
| 184 ASSERT_EQ(base::File::FILE_OK, EnsureFileExists(file_name, &created)); | 169 ASSERT_EQ(base::File::FILE_OK, EnsureFileExists(file_name, &created)); |
| 185 ASSERT_TRUE(created); | 170 ASSERT_TRUE(created); |
| 186 | 171 |
| 187 EXPECT_TRUE(FileExists(file_name)); | 172 EXPECT_TRUE(FileExists(file_name)); |
| 188 EXPECT_EQ(0, GetSize(file_name)); | 173 EXPECT_EQ(0, GetSize(file_name)); |
| 189 | 174 |
| 190 ASSERT_EQ(base::File::FILE_OK, EnsureFileExists(file_name, &created)); | 175 ASSERT_EQ(base::File::FILE_OK, EnsureFileExists(file_name, &created)); |
| 191 EXPECT_FALSE(created); | 176 EXPECT_FALSE(created); |
| 192 } | 177 } |
| 193 | 178 |
| 194 TEST_F(LocalFileUtilTest, TouchFile) { | 179 TEST_F(LocalFileUtilTest, TouchFile) { |
| 195 const char *file_name = "test_file"; | 180 const char *file_name = "test_file"; |
| 196 base::PlatformFile file_handle; | 181 base::File file = CreateFile(file_name); |
| 197 bool created; | 182 ASSERT_TRUE(file.IsValid()); |
| 198 ASSERT_EQ(base::File::FILE_OK, | 183 ASSERT_TRUE(file.created()); |
| 199 CreateFile(file_name, &file_handle, &created)); | |
| 200 ASSERT_TRUE(created); | |
| 201 | 184 |
| 202 scoped_ptr<FileSystemOperationContext> context(NewContext()); | 185 scoped_ptr<FileSystemOperationContext> context(NewContext()); |
| 203 | 186 |
| 204 base::File::Info info; | 187 base::File::Info info; |
| 205 ASSERT_TRUE(base::GetFileInfo(LocalPath(file_name), &info)); | 188 ASSERT_TRUE(base::GetFileInfo(LocalPath(file_name), &info)); |
| 206 const base::Time new_accessed = | 189 const base::Time new_accessed = |
| 207 info.last_accessed + base::TimeDelta::FromHours(10); | 190 info.last_accessed + base::TimeDelta::FromHours(10); |
| 208 const base::Time new_modified = | 191 const base::Time new_modified = |
| 209 info.last_modified + base::TimeDelta::FromHours(5); | 192 info.last_modified + base::TimeDelta::FromHours(5); |
| 210 | 193 |
| 211 EXPECT_EQ(base::File::FILE_OK, | 194 EXPECT_EQ(base::File::FILE_OK, |
| 212 file_util()->Touch(context.get(), CreateURL(file_name), | 195 file_util()->Touch(context.get(), CreateURL(file_name), |
| 213 new_accessed, new_modified)); | 196 new_accessed, new_modified)); |
| 214 | 197 |
| 215 ASSERT_TRUE(base::GetFileInfo(LocalPath(file_name), &info)); | 198 ASSERT_TRUE(base::GetFileInfo(LocalPath(file_name), &info)); |
| 216 EXPECT_EQ(new_accessed, info.last_accessed); | 199 EXPECT_EQ(new_accessed, info.last_accessed); |
| 217 EXPECT_EQ(new_modified, info.last_modified); | 200 EXPECT_EQ(new_modified, info.last_modified); |
| 218 | |
| 219 EXPECT_EQ(base::File::FILE_OK, | |
| 220 file_util()->Close(context.get(), file_handle)); | |
| 221 } | 201 } |
| 222 | 202 |
| 223 TEST_F(LocalFileUtilTest, TouchDirectory) { | 203 TEST_F(LocalFileUtilTest, TouchDirectory) { |
| 224 const char *dir_name = "test_dir"; | 204 const char *dir_name = "test_dir"; |
| 225 scoped_ptr<FileSystemOperationContext> context(NewContext()); | 205 scoped_ptr<FileSystemOperationContext> context(NewContext()); |
| 226 ASSERT_EQ(base::File::FILE_OK, | 206 ASSERT_EQ(base::File::FILE_OK, |
| 227 file_util()->CreateDirectory(context.get(), | 207 file_util()->CreateDirectory(context.get(), |
| 228 CreateURL(dir_name), | 208 CreateURL(dir_name), |
| 229 false /* exclusive */, | 209 false /* exclusive */, |
| 230 false /* recursive */)); | 210 false /* recursive */)); |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 389 CreateURL(from_dir), | 369 CreateURL(from_dir), |
| 390 CreateURL(to_dir))); | 370 CreateURL(to_dir))); |
| 391 | 371 |
| 392 EXPECT_FALSE(DirectoryExists(from_dir)); | 372 EXPECT_FALSE(DirectoryExists(from_dir)); |
| 393 EXPECT_TRUE(DirectoryExists(to_dir)); | 373 EXPECT_TRUE(DirectoryExists(to_dir)); |
| 394 EXPECT_TRUE(FileExists(to_file)); | 374 EXPECT_TRUE(FileExists(to_file)); |
| 395 EXPECT_EQ(1020, GetSize(to_file)); | 375 EXPECT_EQ(1020, GetSize(to_file)); |
| 396 } | 376 } |
| 397 | 377 |
| 398 } // namespace content | 378 } // namespace content |
| OLD | NEW |