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/files/file.h" | 5 #include "base/files/file.h" |
6 #include "base/files/file_util.h" | 6 #include "base/files/file_util.h" |
7 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
| 8 #include "base/memory/scoped_ptr.h" |
8 #include "base/time/time.h" | 9 #include "base/time/time.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
10 | 11 |
11 using base::File; | 12 using base::File; |
12 using base::FilePath; | 13 using base::FilePath; |
13 | 14 |
14 TEST(FileTest, Create) { | 15 TEST(FileTest, Create) { |
15 base::ScopedTempDir temp_dir; | 16 base::ScopedTempDir temp_dir; |
16 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 17 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
17 FilePath file_path = temp_dir.path().AppendASCII("create_file_1"); | 18 FilePath file_path = temp_dir.path().AppendASCII("create_file_1"); |
(...skipping 441 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
459 NULL)); | 460 NULL)); |
460 ASSERT_TRUE(dir.IsValid()); | 461 ASSERT_TRUE(dir.IsValid()); |
461 | 462 |
462 base::File::Info info; | 463 base::File::Info info; |
463 EXPECT_TRUE(dir.GetInfo(&info)); | 464 EXPECT_TRUE(dir.GetInfo(&info)); |
464 EXPECT_TRUE(info.is_directory); | 465 EXPECT_TRUE(info.is_directory); |
465 EXPECT_FALSE(info.is_symbolic_link); | 466 EXPECT_FALSE(info.is_symbolic_link); |
466 EXPECT_EQ(0, info.size); | 467 EXPECT_EQ(0, info.size); |
467 } | 468 } |
468 #endif // defined(OS_WIN) | 469 #endif // defined(OS_WIN) |
| 470 |
| 471 #if defined(OS_POSIX) && defined(GTEST_HAS_DEATH_TEST) |
| 472 TEST(FileTest, MemoryCorruption) { |
| 473 { |
| 474 // Test that changing the checksum value is detected. |
| 475 base::File file; |
| 476 EXPECT_NE(file.file_.file_memory_checksum_, |
| 477 implicit_cast<unsigned int>(file.GetPlatformFile())); |
| 478 file.file_.file_memory_checksum_ = file.GetPlatformFile(); |
| 479 EXPECT_DEATH(file.IsValid(), ""); |
| 480 |
| 481 file.file_.UpdateChecksum(); // Do not crash on File::~File(). |
| 482 } |
| 483 |
| 484 { |
| 485 // Test that changing the file descriptor value is detected. |
| 486 base::File file; |
| 487 file.file_.file_.reset(17); |
| 488 EXPECT_DEATH(file.IsValid(), ""); |
| 489 |
| 490 // Do not crash on File::~File(). |
| 491 ignore_result(file.file_.file_.release()); |
| 492 file.file_.UpdateChecksum(); |
| 493 } |
| 494 |
| 495 { |
| 496 // Test that GetPlatformFile() checks for corruption. |
| 497 base::File file; |
| 498 file.file_.file_memory_checksum_ = file.GetPlatformFile(); |
| 499 EXPECT_DEATH(file.GetPlatformFile(), ""); |
| 500 |
| 501 file.file_.UpdateChecksum(); // Do not crash on File::~File(). |
| 502 } |
| 503 |
| 504 { |
| 505 // Test that the base::File destructor checks for corruption. |
| 506 scoped_ptr<base::File> file(new File()); |
| 507 file->file_.file_memory_checksum_ = file->GetPlatformFile(); |
| 508 EXPECT_DEATH(file.reset(), ""); |
| 509 |
| 510 // Do not crash on this thread's destructor call. |
| 511 file->file_.UpdateChecksum(); |
| 512 } |
| 513 |
| 514 { |
| 515 // Test that the base::File constructor checks for corruption. |
| 516 base::File file; |
| 517 file.file_.file_memory_checksum_ = file.GetPlatformFile(); |
| 518 EXPECT_DEATH(File f(file.Pass()), ""); |
| 519 |
| 520 file.file_.UpdateChecksum(); // Do not crash on File::~File(). |
| 521 } |
| 522 |
| 523 { |
| 524 // Test that doing IO checks for corruption. |
| 525 base::File file; |
| 526 file.file_.file_.reset(17); // A fake open FD value. |
| 527 |
| 528 EXPECT_DEATH(file.Seek(File::FROM_BEGIN, 0), ""); |
| 529 EXPECT_DEATH(file.Read(0, NULL, 0), ""); |
| 530 EXPECT_DEATH(file.ReadAtCurrentPos(NULL, 0), ""); |
| 531 EXPECT_DEATH(file.Write(0, NULL, 0), ""); |
| 532 |
| 533 ignore_result(file.file_.file_.release()); |
| 534 file.file_.UpdateChecksum(); |
| 535 } |
| 536 } |
| 537 #endif // defined(OS_POSIX) |
OLD | NEW |