| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/file_util.h" | 5 #include "base/file_util.h" |
| 6 #include "base/files/file.h" | 6 #include "base/files/file.h" |
| 7 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
| 8 #include "base/time/time.h" | 8 #include "base/time/time.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 10 |
| 11 using base::File; | 11 using base::File; |
| 12 using base::FilePath; | 12 using base::FilePath; |
| 13 | 13 |
| 14 TEST(File, Create) { | 14 TEST(FileTest, Create) { |
| 15 base::ScopedTempDir temp_dir; | 15 base::ScopedTempDir temp_dir; |
| 16 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 16 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 17 FilePath file_path = temp_dir.path().AppendASCII("create_file_1"); | 17 FilePath file_path = temp_dir.path().AppendASCII("create_file_1"); |
| 18 | 18 |
| 19 { | 19 { |
| 20 // Open a file that doesn't exist. | 20 // Open a file that doesn't exist. |
| 21 File file(file_path, base::File::FLAG_OPEN | base::File::FLAG_READ); | 21 File file(file_path, base::File::FLAG_OPEN | base::File::FLAG_READ); |
| 22 EXPECT_FALSE(file.IsValid()); | 22 EXPECT_FALSE(file.IsValid()); |
| 23 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND, file.error_details()); | 23 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND, file.error_details()); |
| 24 } | 24 } |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_READ | | 80 base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_READ | |
| 81 base::File::FLAG_DELETE_ON_CLOSE); | 81 base::File::FLAG_DELETE_ON_CLOSE); |
| 82 EXPECT_TRUE(file.IsValid()); | 82 EXPECT_TRUE(file.IsValid()); |
| 83 EXPECT_TRUE(file.created()); | 83 EXPECT_TRUE(file.created()); |
| 84 EXPECT_EQ(base::File::FILE_OK, file.error_details()); | 84 EXPECT_EQ(base::File::FILE_OK, file.error_details()); |
| 85 } | 85 } |
| 86 | 86 |
| 87 EXPECT_FALSE(base::PathExists(file_path)); | 87 EXPECT_FALSE(base::PathExists(file_path)); |
| 88 } | 88 } |
| 89 | 89 |
| 90 TEST(File, DeleteOpenFile) { | 90 TEST(FileTest, Async) { |
| 91 base::ScopedTempDir temp_dir; |
| 92 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 93 FilePath file_path = temp_dir.path().AppendASCII("create_file"); |
| 94 |
| 95 { |
| 96 File file(file_path, base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_ASYNC); |
| 97 EXPECT_TRUE(file.IsValid()); |
| 98 EXPECT_TRUE(file.async()); |
| 99 } |
| 100 |
| 101 { |
| 102 File file(file_path, base::File::FLAG_OPEN_ALWAYS); |
| 103 EXPECT_TRUE(file.IsValid()); |
| 104 EXPECT_FALSE(file.async()); |
| 105 } |
| 106 } |
| 107 |
| 108 TEST(FileTest, DeleteOpenFile) { |
| 91 base::ScopedTempDir temp_dir; | 109 base::ScopedTempDir temp_dir; |
| 92 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 110 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 93 FilePath file_path = temp_dir.path().AppendASCII("create_file_1"); | 111 FilePath file_path = temp_dir.path().AppendASCII("create_file_1"); |
| 94 | 112 |
| 95 // Create a file. | 113 // Create a file. |
| 96 File file(file_path, | 114 File file(file_path, |
| 97 base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_READ | | 115 base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_READ | |
| 98 base::File::FLAG_SHARE_DELETE); | 116 base::File::FLAG_SHARE_DELETE); |
| 99 EXPECT_TRUE(file.IsValid()); | 117 EXPECT_TRUE(file.IsValid()); |
| 100 EXPECT_TRUE(file.created()); | 118 EXPECT_TRUE(file.created()); |
| 101 EXPECT_EQ(base::File::FILE_OK, file.error_details()); | 119 EXPECT_EQ(base::File::FILE_OK, file.error_details()); |
| 102 | 120 |
| 103 // Open an existing file and mark it as delete on close. | 121 // Open an existing file and mark it as delete on close. |
| 104 File same_file(file_path, | 122 File same_file(file_path, |
| 105 base::File::FLAG_OPEN | base::File::FLAG_DELETE_ON_CLOSE | | 123 base::File::FLAG_OPEN | base::File::FLAG_DELETE_ON_CLOSE | |
| 106 base::File::FLAG_READ); | 124 base::File::FLAG_READ); |
| 107 EXPECT_TRUE(file.IsValid()); | 125 EXPECT_TRUE(file.IsValid()); |
| 108 EXPECT_FALSE(same_file.created()); | 126 EXPECT_FALSE(same_file.created()); |
| 109 EXPECT_EQ(base::File::FILE_OK, same_file.error_details()); | 127 EXPECT_EQ(base::File::FILE_OK, same_file.error_details()); |
| 110 | 128 |
| 111 // Close both handles and check that the file is gone. | 129 // Close both handles and check that the file is gone. |
| 112 file.Close(); | 130 file.Close(); |
| 113 same_file.Close(); | 131 same_file.Close(); |
| 114 EXPECT_FALSE(base::PathExists(file_path)); | 132 EXPECT_FALSE(base::PathExists(file_path)); |
| 115 } | 133 } |
| 116 | 134 |
| 117 TEST(File, ReadWrite) { | 135 TEST(FileTest, ReadWrite) { |
| 118 base::ScopedTempDir temp_dir; | 136 base::ScopedTempDir temp_dir; |
| 119 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 137 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 120 FilePath file_path = temp_dir.path().AppendASCII("read_write_file"); | 138 FilePath file_path = temp_dir.path().AppendASCII("read_write_file"); |
| 121 File file(file_path, | 139 File file(file_path, |
| 122 base::File::FLAG_CREATE | base::File::FLAG_READ | | 140 base::File::FLAG_CREATE | base::File::FLAG_READ | |
| 123 base::File::FLAG_WRITE); | 141 base::File::FLAG_WRITE); |
| 124 ASSERT_TRUE(file.IsValid()); | 142 ASSERT_TRUE(file.IsValid()); |
| 125 | 143 |
| 126 char data_to_write[] = "test"; | 144 char data_to_write[] = "test"; |
| 127 const int kTestDataSize = 4; | 145 const int kTestDataSize = 4; |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 bytes_read = file.Read(0, data_read_2, static_cast<int>(file_size)); | 197 bytes_read = file.Read(0, data_read_2, static_cast<int>(file_size)); |
| 180 EXPECT_EQ(file_size, bytes_read); | 198 EXPECT_EQ(file_size, bytes_read); |
| 181 for (int i = 0; i < kTestDataSize; i++) | 199 for (int i = 0; i < kTestDataSize; i++) |
| 182 EXPECT_EQ(data_to_write[i], data_read_2[i]); | 200 EXPECT_EQ(data_to_write[i], data_read_2[i]); |
| 183 for (int i = kTestDataSize; i < kOffsetBeyondEndOfFile; i++) | 201 for (int i = kTestDataSize; i < kOffsetBeyondEndOfFile; i++) |
| 184 EXPECT_EQ(0, data_read_2[i]); | 202 EXPECT_EQ(0, data_read_2[i]); |
| 185 for (int i = kOffsetBeyondEndOfFile; i < file_size; i++) | 203 for (int i = kOffsetBeyondEndOfFile; i < file_size; i++) |
| 186 EXPECT_EQ(data_to_write[i - kOffsetBeyondEndOfFile], data_read_2[i]); | 204 EXPECT_EQ(data_to_write[i - kOffsetBeyondEndOfFile], data_read_2[i]); |
| 187 } | 205 } |
| 188 | 206 |
| 189 TEST(File, Append) { | 207 TEST(FileTest, Append) { |
| 190 base::ScopedTempDir temp_dir; | 208 base::ScopedTempDir temp_dir; |
| 191 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 209 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 192 FilePath file_path = temp_dir.path().AppendASCII("append_file"); | 210 FilePath file_path = temp_dir.path().AppendASCII("append_file"); |
| 193 File file(file_path, base::File::FLAG_CREATE | base::File::FLAG_APPEND); | 211 File file(file_path, base::File::FLAG_CREATE | base::File::FLAG_APPEND); |
| 194 ASSERT_TRUE(file.IsValid()); | 212 ASSERT_TRUE(file.IsValid()); |
| 195 | 213 |
| 196 char data_to_write[] = "test"; | 214 char data_to_write[] = "test"; |
| 197 const int kTestDataSize = 4; | 215 const int kTestDataSize = 4; |
| 198 | 216 |
| 199 // Write 0 bytes to the file. | 217 // Write 0 bytes to the file. |
| (...skipping 27 matching lines...) Expand all Loading... |
| 227 int bytes_read = file.Read(0, data_read_1, | 245 int bytes_read = file.Read(0, data_read_1, |
| 228 kTestDataSize + kAppendDataSize); | 246 kTestDataSize + kAppendDataSize); |
| 229 EXPECT_EQ(kTestDataSize + kAppendDataSize, bytes_read); | 247 EXPECT_EQ(kTestDataSize + kAppendDataSize, bytes_read); |
| 230 for (int i = 0; i < kTestDataSize; i++) | 248 for (int i = 0; i < kTestDataSize; i++) |
| 231 EXPECT_EQ(data_to_write[i], data_read_1[i]); | 249 EXPECT_EQ(data_to_write[i], data_read_1[i]); |
| 232 for (int i = 0; i < kAppendDataSize; i++) | 250 for (int i = 0; i < kAppendDataSize; i++) |
| 233 EXPECT_EQ(append_data_to_write[i], data_read_1[kTestDataSize + i]); | 251 EXPECT_EQ(append_data_to_write[i], data_read_1[kTestDataSize + i]); |
| 234 } | 252 } |
| 235 | 253 |
| 236 | 254 |
| 237 TEST(File, Length) { | 255 TEST(FileTest, Length) { |
| 238 base::ScopedTempDir temp_dir; | 256 base::ScopedTempDir temp_dir; |
| 239 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 257 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 240 FilePath file_path = temp_dir.path().AppendASCII("truncate_file"); | 258 FilePath file_path = temp_dir.path().AppendASCII("truncate_file"); |
| 241 File file(file_path, | 259 File file(file_path, |
| 242 base::File::FLAG_CREATE | base::File::FLAG_READ | | 260 base::File::FLAG_CREATE | base::File::FLAG_READ | |
| 243 base::File::FLAG_WRITE); | 261 base::File::FLAG_WRITE); |
| 244 ASSERT_TRUE(file.IsValid()); | 262 ASSERT_TRUE(file.IsValid()); |
| 245 EXPECT_EQ(0, file.GetLength()); | 263 EXPECT_EQ(0, file.GetLength()); |
| 246 | 264 |
| 247 // Write "test" to the file. | 265 // Write "test" to the file. |
| (...skipping 28 matching lines...) Expand all Loading... |
| 276 | 294 |
| 277 // Make sure the file was truncated. | 295 // Make sure the file was truncated. |
| 278 bytes_read = file.Read(0, data_read, kTestDataSize); | 296 bytes_read = file.Read(0, data_read, kTestDataSize); |
| 279 EXPECT_EQ(file_size, bytes_read); | 297 EXPECT_EQ(file_size, bytes_read); |
| 280 for (int i = 0; i < file_size; i++) | 298 for (int i = 0; i < file_size; i++) |
| 281 EXPECT_EQ(data_to_write[i], data_read[i]); | 299 EXPECT_EQ(data_to_write[i], data_read[i]); |
| 282 } | 300 } |
| 283 | 301 |
| 284 // Flakily fails: http://crbug.com/86494 | 302 // Flakily fails: http://crbug.com/86494 |
| 285 #if defined(OS_ANDROID) | 303 #if defined(OS_ANDROID) |
| 286 TEST(File, TouchGetInfo) { | 304 TEST(FileTest, TouchGetInfo) { |
| 287 #else | 305 #else |
| 288 TEST(File, DISABLED_TouchGetInfo) { | 306 TEST(FileTest, DISABLED_TouchGetInfo) { |
| 289 #endif | 307 #endif |
| 290 base::ScopedTempDir temp_dir; | 308 base::ScopedTempDir temp_dir; |
| 291 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 309 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 292 File file(temp_dir.path().AppendASCII("touch_get_info_file"), | 310 File file(temp_dir.path().AppendASCII("touch_get_info_file"), |
| 293 base::File::FLAG_CREATE | base::File::FLAG_WRITE | | 311 base::File::FLAG_CREATE | base::File::FLAG_WRITE | |
| 294 base::File::FLAG_WRITE_ATTRIBUTES); | 312 base::File::FLAG_WRITE_ATTRIBUTES); |
| 295 ASSERT_TRUE(file.IsValid()); | 313 ASSERT_TRUE(file.IsValid()); |
| 296 | 314 |
| 297 // Get info for a newly created file. | 315 // Get info for a newly created file. |
| 298 base::File::Info info; | 316 base::File::Info info; |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 342 EXPECT_EQ(info.last_accessed.ToInternalValue(), | 360 EXPECT_EQ(info.last_accessed.ToInternalValue(), |
| 343 new_last_accessed.ToInternalValue()); | 361 new_last_accessed.ToInternalValue()); |
| 344 EXPECT_EQ(info.last_modified.ToInternalValue(), | 362 EXPECT_EQ(info.last_modified.ToInternalValue(), |
| 345 new_last_modified.ToInternalValue()); | 363 new_last_modified.ToInternalValue()); |
| 346 #endif | 364 #endif |
| 347 | 365 |
| 348 EXPECT_EQ(info.creation_time.ToInternalValue(), | 366 EXPECT_EQ(info.creation_time.ToInternalValue(), |
| 349 creation_time.ToInternalValue()); | 367 creation_time.ToInternalValue()); |
| 350 } | 368 } |
| 351 | 369 |
| 352 TEST(File, ReadAtCurrentPosition) { | 370 TEST(FileTest, ReadAtCurrentPosition) { |
| 353 base::ScopedTempDir temp_dir; | 371 base::ScopedTempDir temp_dir; |
| 354 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 372 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 355 FilePath file_path = temp_dir.path().AppendASCII("read_at_current_position"); | 373 FilePath file_path = temp_dir.path().AppendASCII("read_at_current_position"); |
| 356 File file(file_path, | 374 File file(file_path, |
| 357 base::File::FLAG_CREATE | base::File::FLAG_READ | | 375 base::File::FLAG_CREATE | base::File::FLAG_READ | |
| 358 base::File::FLAG_WRITE); | 376 base::File::FLAG_WRITE); |
| 359 EXPECT_TRUE(file.IsValid()); | 377 EXPECT_TRUE(file.IsValid()); |
| 360 | 378 |
| 361 const char kData[] = "test"; | 379 const char kData[] = "test"; |
| 362 const int kDataSize = sizeof(kData) - 1; | 380 const int kDataSize = sizeof(kData) - 1; |
| 363 EXPECT_EQ(kDataSize, file.Write(0, kData, kDataSize)); | 381 EXPECT_EQ(kDataSize, file.Write(0, kData, kDataSize)); |
| 364 | 382 |
| 365 EXPECT_EQ(0, file.Seek(base::File::FROM_BEGIN, 0)); | 383 EXPECT_EQ(0, file.Seek(base::File::FROM_BEGIN, 0)); |
| 366 | 384 |
| 367 char buffer[kDataSize]; | 385 char buffer[kDataSize]; |
| 368 int first_chunk_size = kDataSize / 2; | 386 int first_chunk_size = kDataSize / 2; |
| 369 EXPECT_EQ(first_chunk_size, file.ReadAtCurrentPos(buffer, first_chunk_size)); | 387 EXPECT_EQ(first_chunk_size, file.ReadAtCurrentPos(buffer, first_chunk_size)); |
| 370 EXPECT_EQ(kDataSize - first_chunk_size, | 388 EXPECT_EQ(kDataSize - first_chunk_size, |
| 371 file.ReadAtCurrentPos(buffer + first_chunk_size, | 389 file.ReadAtCurrentPos(buffer + first_chunk_size, |
| 372 kDataSize - first_chunk_size)); | 390 kDataSize - first_chunk_size)); |
| 373 EXPECT_EQ(std::string(buffer, buffer + kDataSize), std::string(kData)); | 391 EXPECT_EQ(std::string(buffer, buffer + kDataSize), std::string(kData)); |
| 374 } | 392 } |
| 375 | 393 |
| 376 TEST(File, WriteAtCurrentPosition) { | 394 TEST(FileTest, WriteAtCurrentPosition) { |
| 377 base::ScopedTempDir temp_dir; | 395 base::ScopedTempDir temp_dir; |
| 378 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 396 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 379 FilePath file_path = temp_dir.path().AppendASCII("write_at_current_position"); | 397 FilePath file_path = temp_dir.path().AppendASCII("write_at_current_position"); |
| 380 File file(file_path, | 398 File file(file_path, |
| 381 base::File::FLAG_CREATE | base::File::FLAG_READ | | 399 base::File::FLAG_CREATE | base::File::FLAG_READ | |
| 382 base::File::FLAG_WRITE); | 400 base::File::FLAG_WRITE); |
| 383 EXPECT_TRUE(file.IsValid()); | 401 EXPECT_TRUE(file.IsValid()); |
| 384 | 402 |
| 385 const char kData[] = "test"; | 403 const char kData[] = "test"; |
| 386 const int kDataSize = sizeof(kData) - 1; | 404 const int kDataSize = sizeof(kData) - 1; |
| 387 | 405 |
| 388 int first_chunk_size = kDataSize / 2; | 406 int first_chunk_size = kDataSize / 2; |
| 389 EXPECT_EQ(first_chunk_size, file.WriteAtCurrentPos(kData, first_chunk_size)); | 407 EXPECT_EQ(first_chunk_size, file.WriteAtCurrentPos(kData, first_chunk_size)); |
| 390 EXPECT_EQ(kDataSize - first_chunk_size, | 408 EXPECT_EQ(kDataSize - first_chunk_size, |
| 391 file.WriteAtCurrentPos(kData + first_chunk_size, | 409 file.WriteAtCurrentPos(kData + first_chunk_size, |
| 392 kDataSize - first_chunk_size)); | 410 kDataSize - first_chunk_size)); |
| 393 | 411 |
| 394 char buffer[kDataSize]; | 412 char buffer[kDataSize]; |
| 395 EXPECT_EQ(kDataSize, file.Read(0, buffer, kDataSize)); | 413 EXPECT_EQ(kDataSize, file.Read(0, buffer, kDataSize)); |
| 396 EXPECT_EQ(std::string(buffer, buffer + kDataSize), std::string(kData)); | 414 EXPECT_EQ(std::string(buffer, buffer + kDataSize), std::string(kData)); |
| 397 } | 415 } |
| 398 | 416 |
| 399 #if defined(OS_WIN) | 417 #if defined(OS_WIN) |
| 400 TEST(File, GetInfoForDirectory) { | 418 TEST(FileTest, GetInfoForDirectory) { |
| 401 base::ScopedTempDir temp_dir; | 419 base::ScopedTempDir temp_dir; |
| 402 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 420 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 403 FilePath empty_dir = temp_dir.path().Append(FILE_PATH_LITERAL("gpfi_test")); | 421 FilePath empty_dir = temp_dir.path().Append(FILE_PATH_LITERAL("gpfi_test")); |
| 404 ASSERT_TRUE(CreateDirectory(empty_dir)); | 422 ASSERT_TRUE(CreateDirectory(empty_dir)); |
| 405 | 423 |
| 406 base::File dir( | 424 base::File dir( |
| 407 ::CreateFile(empty_dir.value().c_str(), | 425 ::CreateFile(empty_dir.value().c_str(), |
| 408 FILE_ALL_ACCESS, | 426 FILE_ALL_ACCESS, |
| 409 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, | 427 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, |
| 410 NULL, | 428 NULL, |
| 411 OPEN_EXISTING, | 429 OPEN_EXISTING, |
| 412 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory. | 430 FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory. |
| 413 NULL)); | 431 NULL)); |
| 414 ASSERT_TRUE(dir.IsValid()); | 432 ASSERT_TRUE(dir.IsValid()); |
| 415 | 433 |
| 416 base::File::Info info; | 434 base::File::Info info; |
| 417 EXPECT_TRUE(dir.GetInfo(&info)); | 435 EXPECT_TRUE(dir.GetInfo(&info)); |
| 418 EXPECT_TRUE(info.is_directory); | 436 EXPECT_TRUE(info.is_directory); |
| 419 EXPECT_FALSE(info.is_symbolic_link); | 437 EXPECT_FALSE(info.is_symbolic_link); |
| 420 EXPECT_EQ(0, info.size); | 438 EXPECT_EQ(0, info.size); |
| 421 } | 439 } |
| 422 #endif // defined(OS_WIN) | 440 #endif // defined(OS_WIN) |
| OLD | NEW |