| 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 479 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 490 EXPECT_EQ(42U, allocator.Id()); | 490 EXPECT_EQ(42U, allocator.Id()); |
| 491 EXPECT_NE(0U, allocator.Allocate(24, 1)); | 491 EXPECT_NE(0U, allocator.Allocate(24, 1)); |
| 492 EXPECT_FALSE(allocator.IsFull()); | 492 EXPECT_FALSE(allocator.IsFull()); |
| 493 EXPECT_FALSE(allocator.IsCorrupt()); | 493 EXPECT_FALSE(allocator.IsCorrupt()); |
| 494 } | 494 } |
| 495 | 495 |
| 496 | 496 |
| 497 //----- SharedPersistentMemoryAllocator ---------------------------------------- | 497 //----- SharedPersistentMemoryAllocator ---------------------------------------- |
| 498 | 498 |
| 499 TEST(SharedPersistentMemoryAllocatorTest, CreationTest) { | 499 TEST(SharedPersistentMemoryAllocatorTest, CreationTest) { |
| 500 SharedMemoryHandle shared_handle; | 500 SharedMemoryHandle shared_handle_1; |
| 501 SharedMemoryHandle shared_handle_2; |
| 501 | 502 |
| 502 PersistentMemoryAllocator::MemoryInfo meminfo1; | 503 PersistentMemoryAllocator::MemoryInfo meminfo1; |
| 503 Reference r123, r456, r789; | 504 Reference r123, r456, r789; |
| 504 { | 505 { |
| 505 std::unique_ptr<SharedMemory> shmem1(new SharedMemory()); | 506 std::unique_ptr<SharedMemory> shmem1(new SharedMemory()); |
| 506 ASSERT_TRUE(shmem1->CreateAndMapAnonymous(TEST_MEMORY_SIZE)); | 507 ASSERT_TRUE(shmem1->CreateAndMapAnonymous(TEST_MEMORY_SIZE)); |
| 507 SharedPersistentMemoryAllocator local(std::move(shmem1), TEST_ID, "", | 508 SharedPersistentMemoryAllocator local(std::move(shmem1), TEST_ID, "", |
| 508 false); | 509 false); |
| 509 EXPECT_FALSE(local.IsReadonly()); | 510 EXPECT_FALSE(local.IsReadonly()); |
| 510 r123 = local.Allocate(123, 123); | 511 r123 = local.Allocate(123, 123); |
| 511 r456 = local.Allocate(456, 456); | 512 r456 = local.Allocate(456, 456); |
| 512 r789 = local.Allocate(789, 789); | 513 r789 = local.Allocate(789, 789); |
| 513 local.MakeIterable(r123); | 514 local.MakeIterable(r123); |
| 514 local.ChangeType(r456, 654, 456); | 515 local.ChangeType(r456, 654, 456); |
| 515 local.MakeIterable(r789); | 516 local.MakeIterable(r789); |
| 516 local.GetMemoryInfo(&meminfo1); | 517 local.GetMemoryInfo(&meminfo1); |
| 517 EXPECT_FALSE(local.IsFull()); | 518 EXPECT_FALSE(local.IsFull()); |
| 518 EXPECT_FALSE(local.IsCorrupt()); | 519 EXPECT_FALSE(local.IsCorrupt()); |
| 519 | 520 |
| 520 ASSERT_TRUE(local.shared_memory()->ShareToProcess( | 521 ASSERT_TRUE(local.shared_memory()->ShareToProcess(GetCurrentProcessHandle(), |
| 521 GetCurrentProcessHandle(), | 522 &shared_handle_1)); |
| 522 &shared_handle)); | 523 ASSERT_TRUE(local.shared_memory()->ShareToProcess(GetCurrentProcessHandle(), |
| 524 &shared_handle_2)); |
| 523 } | 525 } |
| 524 | 526 |
| 525 // Read-only test. | 527 // Read-only test. |
| 526 std::unique_ptr<SharedMemory> shmem2(new SharedMemory(shared_handle, | 528 std::unique_ptr<SharedMemory> shmem2(new SharedMemory(shared_handle_1, |
| 527 /*readonly=*/true)); | 529 /*readonly=*/true)); |
| 528 ASSERT_TRUE(shmem2->Map(TEST_MEMORY_SIZE)); | 530 ASSERT_TRUE(shmem2->Map(TEST_MEMORY_SIZE)); |
| 529 | 531 |
| 530 SharedPersistentMemoryAllocator shalloc2(std::move(shmem2), 0, "", true); | 532 SharedPersistentMemoryAllocator shalloc2(std::move(shmem2), 0, "", true); |
| 531 EXPECT_TRUE(shalloc2.IsReadonly()); | 533 EXPECT_TRUE(shalloc2.IsReadonly()); |
| 532 EXPECT_EQ(TEST_ID, shalloc2.Id()); | 534 EXPECT_EQ(TEST_ID, shalloc2.Id()); |
| 533 EXPECT_FALSE(shalloc2.IsFull()); | 535 EXPECT_FALSE(shalloc2.IsFull()); |
| 534 EXPECT_FALSE(shalloc2.IsCorrupt()); | 536 EXPECT_FALSE(shalloc2.IsCorrupt()); |
| 535 | 537 |
| 536 PersistentMemoryAllocator::Iterator iter2(&shalloc2); | 538 PersistentMemoryAllocator::Iterator iter2(&shalloc2); |
| 537 uint32_t type; | 539 uint32_t type; |
| 538 EXPECT_EQ(r123, iter2.GetNext(&type)); | 540 EXPECT_EQ(r123, iter2.GetNext(&type)); |
| 539 EXPECT_EQ(r789, iter2.GetNext(&type)); | 541 EXPECT_EQ(r789, iter2.GetNext(&type)); |
| 540 EXPECT_EQ(0U, iter2.GetNext(&type)); | 542 EXPECT_EQ(0U, iter2.GetNext(&type)); |
| 541 | 543 |
| 542 EXPECT_EQ(123U, shalloc2.GetType(r123)); | 544 EXPECT_EQ(123U, shalloc2.GetType(r123)); |
| 543 EXPECT_EQ(654U, shalloc2.GetType(r456)); | 545 EXPECT_EQ(654U, shalloc2.GetType(r456)); |
| 544 EXPECT_EQ(789U, shalloc2.GetType(r789)); | 546 EXPECT_EQ(789U, shalloc2.GetType(r789)); |
| 545 | 547 |
| 546 PersistentMemoryAllocator::MemoryInfo meminfo2; | 548 PersistentMemoryAllocator::MemoryInfo meminfo2; |
| 547 shalloc2.GetMemoryInfo(&meminfo2); | 549 shalloc2.GetMemoryInfo(&meminfo2); |
| 548 EXPECT_EQ(meminfo1.total, meminfo2.total); | 550 EXPECT_EQ(meminfo1.total, meminfo2.total); |
| 549 EXPECT_EQ(meminfo1.free, meminfo2.free); | 551 EXPECT_EQ(meminfo1.free, meminfo2.free); |
| 550 | 552 |
| 551 // Read/write test. | 553 // Read/write test. |
| 552 std::unique_ptr<SharedMemory> shmem3(new SharedMemory(shared_handle, | 554 std::unique_ptr<SharedMemory> shmem3(new SharedMemory(shared_handle_2, |
| 553 /*readonly=*/false)); | 555 /*readonly=*/false)); |
| 554 ASSERT_TRUE(shmem3->Map(TEST_MEMORY_SIZE)); | 556 ASSERT_TRUE(shmem3->Map(TEST_MEMORY_SIZE)); |
| 555 | 557 |
| 556 SharedPersistentMemoryAllocator shalloc3(std::move(shmem3), 0, "", false); | 558 SharedPersistentMemoryAllocator shalloc3(std::move(shmem3), 0, "", false); |
| 557 EXPECT_FALSE(shalloc3.IsReadonly()); | 559 EXPECT_FALSE(shalloc3.IsReadonly()); |
| 558 EXPECT_EQ(TEST_ID, shalloc3.Id()); | 560 EXPECT_EQ(TEST_ID, shalloc3.Id()); |
| 559 EXPECT_FALSE(shalloc3.IsFull()); | 561 EXPECT_FALSE(shalloc3.IsFull()); |
| 560 EXPECT_FALSE(shalloc3.IsCorrupt()); | 562 EXPECT_FALSE(shalloc3.IsCorrupt()); |
| 561 | 563 |
| 562 PersistentMemoryAllocator::Iterator iter3(&shalloc3); | 564 PersistentMemoryAllocator::Iterator iter3(&shalloc3); |
| (...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 781 // For filesize >= minsize, the file must be acceptable. This | 783 // For filesize >= minsize, the file must be acceptable. This |
| 782 // else clause (file-not-acceptable) should be reached only if | 784 // else clause (file-not-acceptable) should be reached only if |
| 783 // filesize < minsize. | 785 // filesize < minsize. |
| 784 EXPECT_GT(minsize, filesize); | 786 EXPECT_GT(minsize, filesize); |
| 785 } | 787 } |
| 786 } | 788 } |
| 787 } | 789 } |
| 788 #endif // !defined(OS_NACL) | 790 #endif // !defined(OS_NACL) |
| 789 | 791 |
| 790 } // namespace base | 792 } // namespace base |
| OLD | NEW |