| 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/memory/scoped_ptr.h" |
| 9 #include "base/time/time.h" | 9 #include "base/time/time.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 458 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 469 #endif // defined(OS_WIN) | 469 #endif // defined(OS_WIN) |
| 470 | 470 |
| 471 #if defined(OS_POSIX) && defined(GTEST_HAS_DEATH_TEST) | 471 #if defined(OS_POSIX) && defined(GTEST_HAS_DEATH_TEST) |
| 472 TEST(FileTest, MemoryCorruption) { | 472 TEST(FileTest, MemoryCorruption) { |
| 473 { | 473 { |
| 474 // Test that changing the checksum value is detected. | 474 // Test that changing the checksum value is detected. |
| 475 base::File file; | 475 base::File file; |
| 476 EXPECT_NE(file.file_.file_memory_checksum_, | 476 EXPECT_NE(file.file_.file_memory_checksum_, |
| 477 implicit_cast<unsigned int>(file.GetPlatformFile())); | 477 implicit_cast<unsigned int>(file.GetPlatformFile())); |
| 478 file.file_.file_memory_checksum_ = file.GetPlatformFile(); | 478 file.file_.file_memory_checksum_ = file.GetPlatformFile(); |
| 479 EXPECT_DEATH(file.IsValid(), "corrupted fd memory"); | 479 EXPECT_DEATH(file.IsValid(), ""); |
| 480 | 480 |
| 481 file.file_.UpdateChecksum(); // Do not crash on File::~File(). | 481 file.file_.UpdateChecksum(); // Do not crash on File::~File(). |
| 482 } | 482 } |
| 483 | 483 |
| 484 { | 484 { |
| 485 // Test that changing the file descriptor value is detected. | 485 // Test that changing the file descriptor value is detected. |
| 486 base::File file; | 486 base::File file; |
| 487 file.file_.file_.reset(17); | 487 file.file_.file_.reset(17); |
| 488 EXPECT_DEATH(file.IsValid(), "corrupted fd memory"); | 488 EXPECT_DEATH(file.IsValid(), ""); |
| 489 | 489 |
| 490 // Do not crash on File::~File(). | 490 // Do not crash on File::~File(). |
| 491 ignore_result(file.file_.file_.release()); | 491 ignore_result(file.file_.file_.release()); |
| 492 file.file_.UpdateChecksum(); | 492 file.file_.UpdateChecksum(); |
| 493 } | 493 } |
| 494 | 494 |
| 495 { | 495 { |
| 496 // Test that GetPlatformFile() checks for corruption. | 496 // Test that GetPlatformFile() checks for corruption. |
| 497 base::File file; | 497 base::File file; |
| 498 file.file_.file_memory_checksum_ = file.GetPlatformFile(); | 498 file.file_.file_memory_checksum_ = file.GetPlatformFile(); |
| 499 EXPECT_DEATH(file.GetPlatformFile(), "corrupted fd memory"); | 499 EXPECT_DEATH(file.GetPlatformFile(), ""); |
| 500 | 500 |
| 501 file.file_.UpdateChecksum(); // Do not crash on File::~File(). | 501 file.file_.UpdateChecksum(); // Do not crash on File::~File(). |
| 502 } | 502 } |
| 503 | 503 |
| 504 { | 504 { |
| 505 // Test that the base::File destructor checks for corruption. | 505 // Test that the base::File destructor checks for corruption. |
| 506 scoped_ptr<base::File> file(new File()); | 506 scoped_ptr<base::File> file(new File()); |
| 507 file->file_.file_memory_checksum_ = file->GetPlatformFile(); | 507 file->file_.file_memory_checksum_ = file->GetPlatformFile(); |
| 508 EXPECT_DEATH(file.reset(), "corrupted fd memory"); | 508 EXPECT_DEATH(file.reset(), ""); |
| 509 | 509 |
| 510 // Do not crash on this thread's destructor call. | 510 // Do not crash on this thread's destructor call. |
| 511 file->file_.UpdateChecksum(); | 511 file->file_.UpdateChecksum(); |
| 512 } | 512 } |
| 513 | 513 |
| 514 { | 514 { |
| 515 // Test that the base::File constructor checks for corruption. | 515 // Test that the base::File constructor checks for corruption. |
| 516 base::File file; | 516 base::File file; |
| 517 file.file_.file_memory_checksum_ = file.GetPlatformFile(); | 517 file.file_.file_memory_checksum_ = file.GetPlatformFile(); |
| 518 EXPECT_DEATH(File f(file.Pass()), "corrupted fd memory"); | 518 EXPECT_DEATH(File f(file.Pass()), ""); |
| 519 | 519 |
| 520 file.file_.UpdateChecksum(); // Do not crash on File::~File(). | 520 file.file_.UpdateChecksum(); // Do not crash on File::~File(). |
| 521 } | 521 } |
| 522 | 522 |
| 523 { | 523 { |
| 524 // Test that doing IO checks for corruption. | 524 // Test that doing IO checks for corruption. |
| 525 base::File file; | 525 base::File file; |
| 526 file.file_.file_.reset(17); // A fake open FD value. | 526 file.file_.file_.reset(17); // A fake open FD value. |
| 527 | 527 |
| 528 EXPECT_DEATH(file.Seek(File::FROM_BEGIN, 0), "corrupted fd memory"); | 528 EXPECT_DEATH(file.Seek(File::FROM_BEGIN, 0), ""); |
| 529 EXPECT_DEATH(file.Read(0, NULL, 0), "corrupted fd memory"); | 529 EXPECT_DEATH(file.Read(0, NULL, 0), ""); |
| 530 EXPECT_DEATH(file.ReadAtCurrentPos(NULL, 0), "corrupted fd memory"); | 530 EXPECT_DEATH(file.ReadAtCurrentPos(NULL, 0), ""); |
| 531 EXPECT_DEATH(file.Write(0, NULL, 0), "corrupted fd memory"); | 531 EXPECT_DEATH(file.Write(0, NULL, 0), ""); |
| 532 | 532 |
| 533 ignore_result(file.file_.file_.release()); | 533 ignore_result(file.file_.file_.release()); |
| 534 file.file_.UpdateChecksum(); | 534 file.file_.UpdateChecksum(); |
| 535 } | 535 } |
| 536 } | 536 } |
| 537 #endif // defined(OS_POSIX) | 537 #endif // defined(OS_POSIX) |
| OLD | NEW |