| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/metrics/persistent_memory_allocator.h" | 5 #include "base/metrics/persistent_memory_allocator.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/files/file.h" | 9 #include "base/files/file.h" |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| (...skipping 595 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 606 EXPECT_EQ(42U, type); | 606 EXPECT_EQ(42U, type); |
| 607 } | 607 } |
| 608 | 608 |
| 609 | 609 |
| 610 #if !defined(OS_NACL) | 610 #if !defined(OS_NACL) |
| 611 //----- FilePersistentMemoryAllocator ------------------------------------------ | 611 //----- FilePersistentMemoryAllocator ------------------------------------------ |
| 612 | 612 |
| 613 TEST(FilePersistentMemoryAllocatorTest, CreationTest) { | 613 TEST(FilePersistentMemoryAllocatorTest, CreationTest) { |
| 614 ScopedTempDir temp_dir; | 614 ScopedTempDir temp_dir; |
| 615 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 615 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 616 FilePath file_path = temp_dir.path().AppendASCII("persistent_memory"); | 616 FilePath file_path = temp_dir.GetPath().AppendASCII("persistent_memory"); |
| 617 | 617 |
| 618 PersistentMemoryAllocator::MemoryInfo meminfo1; | 618 PersistentMemoryAllocator::MemoryInfo meminfo1; |
| 619 Reference r123, r456, r789; | 619 Reference r123, r456, r789; |
| 620 { | 620 { |
| 621 LocalPersistentMemoryAllocator local(TEST_MEMORY_SIZE, TEST_ID, ""); | 621 LocalPersistentMemoryAllocator local(TEST_MEMORY_SIZE, TEST_ID, ""); |
| 622 EXPECT_FALSE(local.IsReadonly()); | 622 EXPECT_FALSE(local.IsReadonly()); |
| 623 r123 = local.Allocate(123, 123); | 623 r123 = local.Allocate(123, 123); |
| 624 r456 = local.Allocate(456, 456); | 624 r456 = local.Allocate(456, 456); |
| 625 r789 = local.Allocate(789, 789); | 625 r789 = local.Allocate(789, 789); |
| 626 local.MakeIterable(r123); | 626 local.MakeIterable(r123); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 661 file.GetMemoryInfo(&meminfo2); | 661 file.GetMemoryInfo(&meminfo2); |
| 662 EXPECT_GE(meminfo1.total, meminfo2.total); | 662 EXPECT_GE(meminfo1.total, meminfo2.total); |
| 663 EXPECT_GE(meminfo1.free, meminfo2.free); | 663 EXPECT_GE(meminfo1.free, meminfo2.free); |
| 664 EXPECT_EQ(mmlength, meminfo2.total); | 664 EXPECT_EQ(mmlength, meminfo2.total); |
| 665 EXPECT_EQ(0U, meminfo2.free); | 665 EXPECT_EQ(0U, meminfo2.free); |
| 666 } | 666 } |
| 667 | 667 |
| 668 TEST(FilePersistentMemoryAllocatorTest, ExtendTest) { | 668 TEST(FilePersistentMemoryAllocatorTest, ExtendTest) { |
| 669 ScopedTempDir temp_dir; | 669 ScopedTempDir temp_dir; |
| 670 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 670 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 671 FilePath file_path = temp_dir.path().AppendASCII("extend_test"); | 671 FilePath file_path = temp_dir.GetPath().AppendASCII("extend_test"); |
| 672 MemoryMappedFile::Region region = {0, 16 << 10}; // 16KiB maximum size. | 672 MemoryMappedFile::Region region = {0, 16 << 10}; // 16KiB maximum size. |
| 673 | 673 |
| 674 // Start with a small but valid file of persistent data. | 674 // Start with a small but valid file of persistent data. |
| 675 ASSERT_FALSE(PathExists(file_path)); | 675 ASSERT_FALSE(PathExists(file_path)); |
| 676 { | 676 { |
| 677 LocalPersistentMemoryAllocator local(TEST_MEMORY_SIZE, TEST_ID, ""); | 677 LocalPersistentMemoryAllocator local(TEST_MEMORY_SIZE, TEST_ID, ""); |
| 678 local.Allocate(1, 1); | 678 local.Allocate(1, 1); |
| 679 local.Allocate(11, 11); | 679 local.Allocate(11, 11); |
| 680 | 680 |
| 681 File writer(file_path, File::FLAG_CREATE | File::FLAG_WRITE); | 681 File writer(file_path, File::FLAG_CREATE | File::FLAG_WRITE); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 727 local.MakeIterable(local.Allocate(1, 1)); | 727 local.MakeIterable(local.Allocate(1, 1)); |
| 728 local.MakeIterable(local.Allocate(11, 11)); | 728 local.MakeIterable(local.Allocate(11, 11)); |
| 729 const size_t minsize = local.used(); | 729 const size_t minsize = local.used(); |
| 730 std::unique_ptr<char[]> garbage(new char[minsize]); | 730 std::unique_ptr<char[]> garbage(new char[minsize]); |
| 731 RandBytes(garbage.get(), minsize); | 731 RandBytes(garbage.get(), minsize); |
| 732 | 732 |
| 733 std::unique_ptr<MemoryMappedFile> mmfile; | 733 std::unique_ptr<MemoryMappedFile> mmfile; |
| 734 char filename[100]; | 734 char filename[100]; |
| 735 for (size_t filesize = minsize; filesize > 0; --filesize) { | 735 for (size_t filesize = minsize; filesize > 0; --filesize) { |
| 736 strings::SafeSPrintf(filename, "memory_%d_A", filesize); | 736 strings::SafeSPrintf(filename, "memory_%d_A", filesize); |
| 737 FilePath file_path = temp_dir.path().AppendASCII(filename); | 737 FilePath file_path = temp_dir.GetPath().AppendASCII(filename); |
| 738 ASSERT_FALSE(PathExists(file_path)); | 738 ASSERT_FALSE(PathExists(file_path)); |
| 739 { | 739 { |
| 740 File writer(file_path, File::FLAG_CREATE | File::FLAG_WRITE); | 740 File writer(file_path, File::FLAG_CREATE | File::FLAG_WRITE); |
| 741 ASSERT_TRUE(writer.IsValid()); | 741 ASSERT_TRUE(writer.IsValid()); |
| 742 writer.Write(0, (const char*)local.data(), filesize); | 742 writer.Write(0, (const char*)local.data(), filesize); |
| 743 } | 743 } |
| 744 ASSERT_TRUE(PathExists(file_path)); | 744 ASSERT_TRUE(PathExists(file_path)); |
| 745 | 745 |
| 746 // Request read/write access for some sizes that are a multple of the | 746 // Request read/write access for some sizes that are a multple of the |
| 747 // allocator's alignment size. The allocator is strict about file size | 747 // allocator's alignment size. The allocator is strict about file size |
| (...skipping 29 matching lines...) Expand all Loading... |
| 777 // Ensure that short files are detected as corrupt and full files are not. | 777 // Ensure that short files are detected as corrupt and full files are not. |
| 778 EXPECT_EQ(filesize != minsize, allocator.IsCorrupt()); | 778 EXPECT_EQ(filesize != minsize, allocator.IsCorrupt()); |
| 779 } else { | 779 } else { |
| 780 // For filesize >= minsize, the file must be acceptable. This | 780 // For filesize >= minsize, the file must be acceptable. This |
| 781 // else clause (file-not-acceptable) should be reached only if | 781 // else clause (file-not-acceptable) should be reached only if |
| 782 // filesize < minsize. | 782 // filesize < minsize. |
| 783 EXPECT_LT(filesize, minsize); | 783 EXPECT_LT(filesize, minsize); |
| 784 } | 784 } |
| 785 | 785 |
| 786 strings::SafeSPrintf(filename, "memory_%d_B", filesize); | 786 strings::SafeSPrintf(filename, "memory_%d_B", filesize); |
| 787 file_path = temp_dir.path().AppendASCII(filename); | 787 file_path = temp_dir.GetPath().AppendASCII(filename); |
| 788 ASSERT_FALSE(PathExists(file_path)); | 788 ASSERT_FALSE(PathExists(file_path)); |
| 789 { | 789 { |
| 790 File writer(file_path, File::FLAG_CREATE | File::FLAG_WRITE); | 790 File writer(file_path, File::FLAG_CREATE | File::FLAG_WRITE); |
| 791 ASSERT_TRUE(writer.IsValid()); | 791 ASSERT_TRUE(writer.IsValid()); |
| 792 writer.Write(0, (const char*)garbage.get(), filesize); | 792 writer.Write(0, (const char*)garbage.get(), filesize); |
| 793 } | 793 } |
| 794 ASSERT_TRUE(PathExists(file_path)); | 794 ASSERT_TRUE(PathExists(file_path)); |
| 795 | 795 |
| 796 mmfile.reset(new MemoryMappedFile()); | 796 mmfile.reset(new MemoryMappedFile()); |
| 797 mmfile->Initialize(File(file_path, file_flags), map_access); | 797 mmfile->Initialize(File(file_path, file_flags), map_access); |
| 798 EXPECT_EQ(filesize, mmfile->length()); | 798 EXPECT_EQ(filesize, mmfile->length()); |
| 799 if (FilePersistentMemoryAllocator::IsFileAcceptable(*mmfile, read_only)) { | 799 if (FilePersistentMemoryAllocator::IsFileAcceptable(*mmfile, read_only)) { |
| 800 // Make sure construction doesn't crash. It will, however, cause | 800 // Make sure construction doesn't crash. It will, however, cause |
| 801 // error messages warning about about a corrupted memory segment. | 801 // error messages warning about about a corrupted memory segment. |
| 802 FilePersistentMemoryAllocator allocator(std::move(mmfile), 0, 0, "", | 802 FilePersistentMemoryAllocator allocator(std::move(mmfile), 0, 0, "", |
| 803 read_only); | 803 read_only); |
| 804 EXPECT_TRUE(allocator.IsCorrupt()); // Garbage data so it should be. | 804 EXPECT_TRUE(allocator.IsCorrupt()); // Garbage data so it should be. |
| 805 } else { | 805 } else { |
| 806 // For filesize >= minsize, the file must be acceptable. This | 806 // For filesize >= minsize, the file must be acceptable. This |
| 807 // else clause (file-not-acceptable) should be reached only if | 807 // else clause (file-not-acceptable) should be reached only if |
| 808 // filesize < minsize. | 808 // filesize < minsize. |
| 809 EXPECT_GT(minsize, filesize); | 809 EXPECT_GT(minsize, filesize); |
| 810 } | 810 } |
| 811 } | 811 } |
| 812 } | 812 } |
| 813 #endif // !defined(OS_NACL) | 813 #endif // !defined(OS_NACL) |
| 814 | 814 |
| 815 } // namespace base | 815 } // namespace base |
| OLD | NEW |