[email protected] | e7e4673 | 2012-01-05 11:45:55 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 8eb140ed | 2011-04-28 08:01:54 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include <string> |
| 6 | |
| 7 | #include "base/file_path.h" |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 8 | #include "base/files/scoped_temp_dir.h" |
[email protected] | 3ed847a6 | 2012-06-07 01:20:01 | [diff] [blame] | 9 | #include "base/message_loop.h" |
[email protected] | e078590 | 2011-05-19 23:34:17 | [diff] [blame] | 10 | #include "base/message_loop_proxy.h" |
[email protected] | 8eb140ed | 2011-04-28 08:01:54 | [diff] [blame] | 11 | #include "base/platform_file.h" |
| 12 | #include "base/sys_string_conversions.h" |
| 13 | #include "base/utf_string_conversions.h" |
| 14 | #include "testing/gtest/include/gtest/gtest.h" |
| 15 | #include "webkit/fileapi/file_system_context.h" |
| 16 | #include "webkit/fileapi/file_system_file_util.h" |
[email protected] | 8eb140ed | 2011-04-28 08:01:54 | [diff] [blame] | 17 | #include "webkit/fileapi/file_system_operation_context.h" |
[email protected] | e078590 | 2011-05-19 23:34:17 | [diff] [blame] | 18 | #include "webkit/fileapi/file_system_types.h" |
[email protected] | 02a6054 | 2012-07-24 20:05:33 | [diff] [blame] | 19 | #include "webkit/fileapi/local_file_system_test_helper.h" |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 20 | #include "webkit/fileapi/local_file_util.h" |
| 21 | #include "webkit/fileapi/native_file_util.h" |
[email protected] | 8eb140ed | 2011-04-28 08:01:54 | [diff] [blame] | 22 | |
[email protected] | 4ce4e79d | 2011-05-13 16:27:15 | [diff] [blame] | 23 | namespace fileapi { |
[email protected] | 8eb140ed | 2011-04-28 08:01:54 | [diff] [blame] | 24 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 25 | // TODO(dmikurube): Cover all public methods in LocalFileUtil. |
| 26 | class LocalFileUtilTest : public testing::Test { |
[email protected] | 8eb140ed | 2011-04-28 08:01:54 | [diff] [blame] | 27 | public: |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 28 | LocalFileUtilTest() |
[email protected] | 4f056a87 | 2013-01-23 04:24:36 | [diff] [blame] | 29 | : test_helper_(GURL("http://foo/"), kFileSystemTypeTest) {} |
[email protected] | 8eb140ed | 2011-04-28 08:01:54 | [diff] [blame] | 30 | |
| 31 | void SetUp() { |
| 32 | ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); |
[email protected] | 4f056a87 | 2013-01-23 04:24:36 | [diff] [blame] | 33 | test_helper_.SetUp(data_dir_.path()); |
[email protected] | 77316bf | 2011-05-19 08:16:44 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | void TearDown() { |
| 37 | test_helper_.TearDown(); |
[email protected] | 8eb140ed | 2011-04-28 08:01:54 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | protected: |
| 41 | FileSystemOperationContext* NewContext() { |
[email protected] | 77316bf | 2011-05-19 08:16:44 | [diff] [blame] | 42 | FileSystemOperationContext* context = test_helper_.NewOperationContext(); |
[email protected] | b97b5d3 | 2011-05-10 11:20:56 | [diff] [blame] | 43 | return context; |
[email protected] | 8eb140ed | 2011-04-28 08:01:54 | [diff] [blame] | 44 | } |
| 45 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 46 | LocalFileUtil* FileUtil() { |
[email protected] | 4f056a87 | 2013-01-23 04:24:36 | [diff] [blame] | 47 | return static_cast<LocalFileUtil*>(test_helper_.file_util()); |
[email protected] | 8eb140ed | 2011-04-28 08:01:54 | [diff] [blame] | 48 | } |
| 49 | |
[email protected] | 949f25a | 2012-06-27 01:53:09 | [diff] [blame] | 50 | FileSystemURL Path(const std::string& file_name) { |
| 51 | return test_helper_.CreateURLFromUTF8(file_name); |
[email protected] | 77316bf | 2011-05-19 08:16:44 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | FilePath LocalPath(const char *file_name) { |
| 55 | return test_helper_.GetLocalPathFromASCII(file_name); |
[email protected] | 8eb140ed | 2011-04-28 08:01:54 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | bool FileExists(const char *file_name) { |
[email protected] | 77316bf | 2011-05-19 08:16:44 | [diff] [blame] | 59 | return file_util::PathExists(LocalPath(file_name)) && |
| 60 | !file_util::DirectoryExists(LocalPath(file_name)); |
[email protected] | 8eb140ed | 2011-04-28 08:01:54 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | bool DirectoryExists(const char *file_name) { |
[email protected] | 77316bf | 2011-05-19 08:16:44 | [diff] [blame] | 64 | return file_util::DirectoryExists(LocalPath(file_name)); |
[email protected] | 8eb140ed | 2011-04-28 08:01:54 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | int64 GetSize(const char *file_name) { |
| 68 | base::PlatformFileInfo info; |
[email protected] | 77316bf | 2011-05-19 08:16:44 | [diff] [blame] | 69 | file_util::GetFileInfo(LocalPath(file_name), &info); |
[email protected] | 8eb140ed | 2011-04-28 08:01:54 | [diff] [blame] | 70 | return info.size; |
| 71 | } |
| 72 | |
| 73 | base::PlatformFileError CreateFile(const char* file_name, |
| 74 | base::PlatformFile* file_handle, bool* created) { |
| 75 | int file_flags = base::PLATFORM_FILE_CREATE | |
| 76 | base::PLATFORM_FILE_WRITE | base::PLATFORM_FILE_ASYNC; |
| 77 | |
| 78 | scoped_ptr<FileSystemOperationContext> context(NewContext()); |
| 79 | return FileUtil()->CreateOrOpen( |
| 80 | context.get(), |
[email protected] | 77316bf | 2011-05-19 08:16:44 | [diff] [blame] | 81 | Path(file_name), |
[email protected] | 8eb140ed | 2011-04-28 08:01:54 | [diff] [blame] | 82 | file_flags, file_handle, created); |
| 83 | } |
| 84 | |
| 85 | base::PlatformFileError EnsureFileExists(const char* file_name, |
| 86 | bool* created) { |
| 87 | scoped_ptr<FileSystemOperationContext> context(NewContext()); |
| 88 | return FileUtil()->EnsureFileExists( |
| 89 | context.get(), |
[email protected] | 77316bf | 2011-05-19 08:16:44 | [diff] [blame] | 90 | Path(file_name), created); |
[email protected] | 8eb140ed | 2011-04-28 08:01:54 | [diff] [blame] | 91 | } |
| 92 | |
[email protected] | 02a6054 | 2012-07-24 20:05:33 | [diff] [blame] | 93 | const LocalFileSystemTestOriginHelper& test_helper() const { |
[email protected] | 45ea0fbbb | 2012-02-27 22:28:49 | [diff] [blame] | 94 | return test_helper_; |
| 95 | } |
| 96 | |
[email protected] | 8eb140ed | 2011-04-28 08:01:54 | [diff] [blame] | 97 | private: |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 98 | scoped_ptr<LocalFileUtil> local_file_util_; |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 99 | base::ScopedTempDir data_dir_; |
[email protected] | 3ed847a6 | 2012-06-07 01:20:01 | [diff] [blame] | 100 | MessageLoop message_loop_; |
[email protected] | 02a6054 | 2012-07-24 20:05:33 | [diff] [blame] | 101 | LocalFileSystemTestOriginHelper test_helper_; |
[email protected] | 8eb140ed | 2011-04-28 08:01:54 | [diff] [blame] | 102 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 103 | DISALLOW_COPY_AND_ASSIGN(LocalFileUtilTest); |
[email protected] | 8eb140ed | 2011-04-28 08:01:54 | [diff] [blame] | 104 | }; |
| 105 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 106 | TEST_F(LocalFileUtilTest, CreateAndClose) { |
[email protected] | 8eb140ed | 2011-04-28 08:01:54 | [diff] [blame] | 107 | const char *file_name = "test_file"; |
| 108 | base::PlatformFile file_handle; |
| 109 | bool created; |
| 110 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 111 | CreateFile(file_name, &file_handle, &created)); |
| 112 | ASSERT_TRUE(created); |
| 113 | |
| 114 | EXPECT_TRUE(FileExists(file_name)); |
| 115 | EXPECT_EQ(0, GetSize(file_name)); |
| 116 | |
| 117 | scoped_ptr<FileSystemOperationContext> context(NewContext()); |
| 118 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 119 | FileUtil()->Close(context.get(), file_handle)); |
| 120 | } |
| 121 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 122 | TEST_F(LocalFileUtilTest, EnsureFileExists) { |
[email protected] | 8eb140ed | 2011-04-28 08:01:54 | [diff] [blame] | 123 | const char *file_name = "foobar"; |
| 124 | bool created; |
| 125 | ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(file_name, &created)); |
| 126 | ASSERT_TRUE(created); |
| 127 | |
| 128 | EXPECT_TRUE(FileExists(file_name)); |
| 129 | EXPECT_EQ(0, GetSize(file_name)); |
| 130 | |
| 131 | ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(file_name, &created)); |
| 132 | EXPECT_FALSE(created); |
| 133 | } |
| 134 | |
[email protected] | 307a825a | 2012-11-01 11:48:52 | [diff] [blame] | 135 | TEST_F(LocalFileUtilTest, TouchFile) { |
| 136 | const char *file_name = "test_file"; |
| 137 | base::PlatformFile file_handle; |
| 138 | bool created; |
| 139 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 140 | CreateFile(file_name, &file_handle, &created)); |
| 141 | ASSERT_TRUE(created); |
| 142 | |
| 143 | scoped_ptr<FileSystemOperationContext> context(NewContext()); |
| 144 | |
| 145 | base::PlatformFileInfo info; |
| 146 | ASSERT_TRUE(file_util::GetFileInfo(LocalPath(file_name), &info)); |
| 147 | const base::Time new_accessed = |
| 148 | info.last_accessed + base::TimeDelta::FromHours(10); |
| 149 | const base::Time new_modified = |
| 150 | info.last_modified + base::TimeDelta::FromHours(5); |
| 151 | |
| 152 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 153 | FileUtil()->Touch(context.get(), Path(file_name), |
| 154 | new_accessed, new_modified)); |
| 155 | |
| 156 | ASSERT_TRUE(file_util::GetFileInfo(LocalPath(file_name), &info)); |
| 157 | EXPECT_EQ(new_accessed, info.last_accessed); |
| 158 | EXPECT_EQ(new_modified, info.last_modified); |
| 159 | |
| 160 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 161 | FileUtil()->Close(context.get(), file_handle)); |
| 162 | } |
| 163 | |
| 164 | TEST_F(LocalFileUtilTest, TouchDirectory) { |
| 165 | const char *dir_name = "test_dir"; |
| 166 | scoped_ptr<FileSystemOperationContext> context(NewContext()); |
| 167 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 168 | FileUtil()->CreateDirectory(context.get(), |
| 169 | Path(dir_name), |
| 170 | false /* exclusive */, |
| 171 | false /* recursive */)); |
| 172 | |
| 173 | base::PlatformFileInfo info; |
| 174 | ASSERT_TRUE(file_util::GetFileInfo(LocalPath(dir_name), &info)); |
| 175 | const base::Time new_accessed = |
| 176 | info.last_accessed + base::TimeDelta::FromHours(10); |
| 177 | const base::Time new_modified = |
| 178 | info.last_modified + base::TimeDelta::FromHours(5); |
| 179 | |
| 180 | EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 181 | FileUtil()->Touch(context.get(), Path(dir_name), |
| 182 | new_accessed, new_modified)); |
| 183 | |
| 184 | ASSERT_TRUE(file_util::GetFileInfo(LocalPath(dir_name), &info)); |
| 185 | EXPECT_EQ(new_accessed, info.last_accessed); |
| 186 | EXPECT_EQ(new_modified, info.last_modified); |
| 187 | } |
| 188 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 189 | TEST_F(LocalFileUtilTest, Truncate) { |
[email protected] | 8eb140ed | 2011-04-28 08:01:54 | [diff] [blame] | 190 | const char *file_name = "truncated"; |
| 191 | bool created; |
| 192 | ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(file_name, &created)); |
| 193 | ASSERT_TRUE(created); |
| 194 | |
| 195 | scoped_ptr<FileSystemOperationContext> context; |
| 196 | |
| 197 | context.reset(NewContext()); |
| 198 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 199 | FileUtil()->Truncate(context.get(), Path(file_name), 1020)); |
| 200 | |
| 201 | EXPECT_TRUE(FileExists(file_name)); |
| 202 | EXPECT_EQ(1020, GetSize(file_name)); |
| 203 | } |
| 204 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 205 | TEST_F(LocalFileUtilTest, CopyFile) { |
[email protected] | 8eb140ed | 2011-04-28 08:01:54 | [diff] [blame] | 206 | const char *from_file = "fromfile"; |
| 207 | const char *to_file1 = "tofile1"; |
| 208 | const char *to_file2 = "tofile2"; |
| 209 | bool created; |
| 210 | ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(from_file, &created)); |
| 211 | ASSERT_TRUE(created); |
[email protected] | 8eb140ed | 2011-04-28 08:01:54 | [diff] [blame] | 212 | |
[email protected] | 45ea0fbbb | 2012-02-27 22:28:49 | [diff] [blame] | 213 | scoped_ptr<FileSystemOperationContext> context; |
[email protected] | 8eb140ed | 2011-04-28 08:01:54 | [diff] [blame] | 214 | context.reset(NewContext()); |
| 215 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 216 | FileUtil()->Truncate(context.get(), Path(from_file), 1020)); |
| 217 | |
| 218 | EXPECT_TRUE(FileExists(from_file)); |
| 219 | EXPECT_EQ(1020, GetSize(from_file)); |
| 220 | |
| 221 | context.reset(NewContext()); |
| 222 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 45ea0fbbb | 2012-02-27 22:28:49 | [diff] [blame] | 223 | test_helper().SameFileUtilCopy(context.get(), |
| 224 | Path(from_file), Path(to_file1))); |
[email protected] | 8eb140ed | 2011-04-28 08:01:54 | [diff] [blame] | 225 | |
| 226 | context.reset(NewContext()); |
| 227 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 45ea0fbbb | 2012-02-27 22:28:49 | [diff] [blame] | 228 | test_helper().SameFileUtilCopy(context.get(), |
| 229 | Path(from_file), Path(to_file2))); |
[email protected] | 8eb140ed | 2011-04-28 08:01:54 | [diff] [blame] | 230 | |
| 231 | EXPECT_TRUE(FileExists(from_file)); |
| 232 | EXPECT_EQ(1020, GetSize(from_file)); |
| 233 | EXPECT_TRUE(FileExists(to_file1)); |
| 234 | EXPECT_EQ(1020, GetSize(to_file1)); |
| 235 | EXPECT_TRUE(FileExists(to_file2)); |
| 236 | EXPECT_EQ(1020, GetSize(to_file2)); |
| 237 | } |
| 238 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 239 | TEST_F(LocalFileUtilTest, CopyDirectory) { |
[email protected] | 8eb140ed | 2011-04-28 08:01:54 | [diff] [blame] | 240 | const char *from_dir = "fromdir"; |
| 241 | const char *from_file = "fromdir/fromfile"; |
| 242 | const char *to_dir = "todir"; |
| 243 | const char *to_file = "todir/fromfile"; |
| 244 | bool created; |
| 245 | scoped_ptr<FileSystemOperationContext> context; |
| 246 | |
| 247 | context.reset(NewContext()); |
| 248 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 249 | FileUtil()->CreateDirectory(context.get(), Path(from_dir), false, false)); |
| 250 | ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(from_file, &created)); |
| 251 | ASSERT_TRUE(created); |
| 252 | |
| 253 | context.reset(NewContext()); |
| 254 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 255 | FileUtil()->Truncate(context.get(), Path(from_file), 1020)); |
| 256 | |
| 257 | EXPECT_TRUE(DirectoryExists(from_dir)); |
| 258 | EXPECT_TRUE(FileExists(from_file)); |
| 259 | EXPECT_EQ(1020, GetSize(from_file)); |
| 260 | EXPECT_FALSE(DirectoryExists(to_dir)); |
| 261 | |
| 262 | context.reset(NewContext()); |
| 263 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 45ea0fbbb | 2012-02-27 22:28:49 | [diff] [blame] | 264 | test_helper().SameFileUtilCopy(context.get(), |
| 265 | Path(from_dir), Path(to_dir))); |
[email protected] | 8eb140ed | 2011-04-28 08:01:54 | [diff] [blame] | 266 | |
| 267 | EXPECT_TRUE(DirectoryExists(from_dir)); |
| 268 | EXPECT_TRUE(FileExists(from_file)); |
| 269 | EXPECT_EQ(1020, GetSize(from_file)); |
| 270 | EXPECT_TRUE(DirectoryExists(to_dir)); |
| 271 | EXPECT_TRUE(FileExists(to_file)); |
| 272 | EXPECT_EQ(1020, GetSize(to_file)); |
| 273 | } |
| 274 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 275 | TEST_F(LocalFileUtilTest, MoveFile) { |
[email protected] | 8eb140ed | 2011-04-28 08:01:54 | [diff] [blame] | 276 | const char *from_file = "fromfile"; |
| 277 | const char *to_file = "tofile"; |
| 278 | bool created; |
| 279 | ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(from_file, &created)); |
| 280 | ASSERT_TRUE(created); |
| 281 | scoped_ptr<FileSystemOperationContext> context; |
| 282 | |
| 283 | context.reset(NewContext()); |
| 284 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 285 | FileUtil()->Truncate(context.get(), Path(from_file), 1020)); |
| 286 | |
| 287 | EXPECT_TRUE(FileExists(from_file)); |
| 288 | EXPECT_EQ(1020, GetSize(from_file)); |
| 289 | |
| 290 | context.reset(NewContext()); |
| 291 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 45ea0fbbb | 2012-02-27 22:28:49 | [diff] [blame] | 292 | test_helper().SameFileUtilMove(context.get(), |
| 293 | Path(from_file), Path(to_file))); |
[email protected] | 8eb140ed | 2011-04-28 08:01:54 | [diff] [blame] | 294 | |
| 295 | EXPECT_FALSE(FileExists(from_file)); |
| 296 | EXPECT_TRUE(FileExists(to_file)); |
| 297 | EXPECT_EQ(1020, GetSize(to_file)); |
| 298 | } |
| 299 | |
[email protected] | 7878ece | 2011-09-05 11:41:49 | [diff] [blame] | 300 | TEST_F(LocalFileUtilTest, MoveDirectory) { |
[email protected] | 8eb140ed | 2011-04-28 08:01:54 | [diff] [blame] | 301 | const char *from_dir = "fromdir"; |
| 302 | const char *from_file = "fromdir/fromfile"; |
| 303 | const char *to_dir = "todir"; |
| 304 | const char *to_file = "todir/fromfile"; |
| 305 | bool created; |
| 306 | scoped_ptr<FileSystemOperationContext> context; |
| 307 | |
| 308 | context.reset(NewContext()); |
| 309 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 310 | FileUtil()->CreateDirectory(context.get(), Path(from_dir), false, false)); |
| 311 | ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(from_file, &created)); |
| 312 | ASSERT_TRUE(created); |
| 313 | |
| 314 | context.reset(NewContext()); |
| 315 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 316 | FileUtil()->Truncate(context.get(), Path(from_file), 1020)); |
| 317 | |
| 318 | EXPECT_TRUE(DirectoryExists(from_dir)); |
| 319 | EXPECT_TRUE(FileExists(from_file)); |
| 320 | EXPECT_EQ(1020, GetSize(from_file)); |
| 321 | EXPECT_FALSE(DirectoryExists(to_dir)); |
| 322 | |
| 323 | context.reset(NewContext()); |
| 324 | ASSERT_EQ(base::PLATFORM_FILE_OK, |
[email protected] | 45ea0fbbb | 2012-02-27 22:28:49 | [diff] [blame] | 325 | test_helper().SameFileUtilMove(context.get(), |
| 326 | Path(from_dir), Path(to_dir))); |
[email protected] | 8eb140ed | 2011-04-28 08:01:54 | [diff] [blame] | 327 | |
| 328 | EXPECT_FALSE(DirectoryExists(from_dir)); |
| 329 | EXPECT_TRUE(DirectoryExists(to_dir)); |
| 330 | EXPECT_TRUE(FileExists(to_file)); |
| 331 | EXPECT_EQ(1020, GetSize(to_file)); |
| 332 | } |
[email protected] | 4ce4e79d | 2011-05-13 16:27:15 | [diff] [blame] | 333 | |
| 334 | } // namespace fileapi |