| 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" | |
| 9 #include "base/time/time.h" | 8 #include "base/time/time.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 10 |
| 12 using base::File; | 11 using base::File; |
| 13 using base::FilePath; | 12 using base::FilePath; |
| 14 | 13 |
| 15 TEST(FileTest, Create) { | 14 TEST(FileTest, Create) { |
| 16 base::ScopedTempDir temp_dir; | 15 base::ScopedTempDir temp_dir; |
| 17 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 16 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 18 FilePath file_path = temp_dir.path().AppendASCII("create_file_1"); | 17 FilePath file_path = temp_dir.path().AppendASCII("create_file_1"); |
| (...skipping 484 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 503 NULL)); | 502 NULL)); |
| 504 ASSERT_TRUE(dir.IsValid()); | 503 ASSERT_TRUE(dir.IsValid()); |
| 505 | 504 |
| 506 base::File::Info info; | 505 base::File::Info info; |
| 507 EXPECT_TRUE(dir.GetInfo(&info)); | 506 EXPECT_TRUE(dir.GetInfo(&info)); |
| 508 EXPECT_TRUE(info.is_directory); | 507 EXPECT_TRUE(info.is_directory); |
| 509 EXPECT_FALSE(info.is_symbolic_link); | 508 EXPECT_FALSE(info.is_symbolic_link); |
| 510 EXPECT_EQ(0, info.size); | 509 EXPECT_EQ(0, info.size); |
| 511 } | 510 } |
| 512 #endif // defined(OS_WIN) | 511 #endif // defined(OS_WIN) |
| 513 | |
| 514 #if defined(OS_POSIX) && defined(GTEST_HAS_DEATH_TEST) | |
| 515 TEST(FileTest, MemoryCorruption) { | |
| 516 { | |
| 517 // Test that changing the checksum value is detected. | |
| 518 base::File file; | |
| 519 EXPECT_NE(file.file_.file_memory_checksum_, | |
| 520 static_cast<unsigned int>(file.GetPlatformFile())); | |
| 521 file.file_.file_memory_checksum_ = file.GetPlatformFile(); | |
| 522 EXPECT_DEATH(file.IsValid(), ""); | |
| 523 | |
| 524 file.file_.UpdateChecksum(); // Do not crash on File::~File(). | |
| 525 } | |
| 526 | |
| 527 { | |
| 528 // Test that changing the file descriptor value is detected. | |
| 529 base::File file; | |
| 530 file.file_.file_.reset(17); | |
| 531 EXPECT_DEATH(file.IsValid(), ""); | |
| 532 | |
| 533 // Do not crash on File::~File(). | |
| 534 ignore_result(file.file_.file_.release()); | |
| 535 file.file_.UpdateChecksum(); | |
| 536 } | |
| 537 | |
| 538 { | |
| 539 // Test that GetPlatformFile() checks for corruption. | |
| 540 base::File file; | |
| 541 file.file_.file_memory_checksum_ = file.GetPlatformFile(); | |
| 542 EXPECT_DEATH(file.GetPlatformFile(), ""); | |
| 543 | |
| 544 file.file_.UpdateChecksum(); // Do not crash on File::~File(). | |
| 545 } | |
| 546 | |
| 547 { | |
| 548 // Test that the base::File destructor checks for corruption. | |
| 549 scoped_ptr<base::File> file(new File()); | |
| 550 file->file_.file_memory_checksum_ = file->GetPlatformFile(); | |
| 551 EXPECT_DEATH(file.reset(), ""); | |
| 552 | |
| 553 // Do not crash on this thread's destructor call. | |
| 554 file->file_.UpdateChecksum(); | |
| 555 } | |
| 556 | |
| 557 { | |
| 558 // Test that the base::File constructor checks for corruption. | |
| 559 base::File file; | |
| 560 file.file_.file_memory_checksum_ = file.GetPlatformFile(); | |
| 561 EXPECT_DEATH(File f(file.Pass()), ""); | |
| 562 | |
| 563 file.file_.UpdateChecksum(); // Do not crash on File::~File(). | |
| 564 } | |
| 565 | |
| 566 { | |
| 567 // Test that doing IO checks for corruption. | |
| 568 base::File file; | |
| 569 file.file_.file_.reset(17); // A fake open FD value. | |
| 570 | |
| 571 EXPECT_DEATH(file.Seek(File::FROM_BEGIN, 0), ""); | |
| 572 EXPECT_DEATH(file.Read(0, NULL, 0), ""); | |
| 573 EXPECT_DEATH(file.ReadAtCurrentPos(NULL, 0), ""); | |
| 574 EXPECT_DEATH(file.Write(0, NULL, 0), ""); | |
| 575 | |
| 576 ignore_result(file.file_.file_.release()); | |
| 577 file.file_.UpdateChecksum(); | |
| 578 } | |
| 579 } | |
| 580 #endif // defined(OS_POSIX) | |
| OLD | NEW |