| 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/basictypes.h" | 5 #include "base/basictypes.h" |
| 6 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
| 7 #include "base/memory/shared_memory.h" | 7 #include "base/memory/shared_memory.h" |
| 8 #include "base/process/kill.h" | 8 #include "base/process/kill.h" |
| 9 #include "base/rand_util.h" | 9 #include "base/rand_util.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| (...skipping 459 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 470 contents.size())); | 470 contents.size())); |
| 471 } | 471 } |
| 472 | 472 |
| 473 TEST(SharedMemoryTest, MapAt) { | 473 TEST(SharedMemoryTest, MapAt) { |
| 474 ASSERT_TRUE(SysInfo::VMAllocationGranularity() >= sizeof(uint32)); | 474 ASSERT_TRUE(SysInfo::VMAllocationGranularity() >= sizeof(uint32)); |
| 475 const size_t kCount = SysInfo::VMAllocationGranularity(); | 475 const size_t kCount = SysInfo::VMAllocationGranularity(); |
| 476 const size_t kDataSize = kCount * sizeof(uint32); | 476 const size_t kDataSize = kCount * sizeof(uint32); |
| 477 | 477 |
| 478 SharedMemory memory; | 478 SharedMemory memory; |
| 479 ASSERT_TRUE(memory.CreateAndMapAnonymous(kDataSize)); | 479 ASSERT_TRUE(memory.CreateAndMapAnonymous(kDataSize)); |
| 480 ASSERT_TRUE(memory.Map(kDataSize)); | |
| 481 uint32* ptr = static_cast<uint32*>(memory.memory()); | 480 uint32* ptr = static_cast<uint32*>(memory.memory()); |
| 482 ASSERT_NE(ptr, static_cast<void*>(NULL)); | 481 ASSERT_NE(ptr, static_cast<void*>(NULL)); |
| 483 | 482 |
| 484 for (size_t i = 0; i < kCount; ++i) { | 483 for (size_t i = 0; i < kCount; ++i) { |
| 485 ptr[i] = i; | 484 ptr[i] = i; |
| 486 } | 485 } |
| 487 | 486 |
| 488 memory.Unmap(); | 487 memory.Unmap(); |
| 489 | 488 |
| 490 off_t offset = SysInfo::VMAllocationGranularity(); | 489 off_t offset = SysInfo::VMAllocationGranularity(); |
| 491 ASSERT_TRUE(memory.MapAt(offset, kDataSize - offset)); | 490 ASSERT_TRUE(memory.MapAt(offset, kDataSize - offset)); |
| 492 offset /= sizeof(uint32); | 491 offset /= sizeof(uint32); |
| 493 ptr = static_cast<uint32*>(memory.memory()); | 492 ptr = static_cast<uint32*>(memory.memory()); |
| 494 ASSERT_NE(ptr, static_cast<void*>(NULL)); | 493 ASSERT_NE(ptr, static_cast<void*>(NULL)); |
| 495 for (size_t i = offset; i < kCount; ++i) { | 494 for (size_t i = offset; i < kCount; ++i) { |
| 496 EXPECT_EQ(ptr[i - offset], i); | 495 EXPECT_EQ(ptr[i - offset], i); |
| 497 } | 496 } |
| 498 } | 497 } |
| 499 | 498 |
| 499 TEST(SharedMemoryTest, MapTwice) { |
| 500 const uint32 kDataSize = 1024; |
| 501 SharedMemory memory; |
| 502 bool rv = memory.CreateAndMapAnonymous(kDataSize); |
| 503 EXPECT_TRUE(rv); |
| 504 |
| 505 void* old_address = memory.memory(); |
| 506 |
| 507 rv = memory.Map(kDataSize); |
| 508 EXPECT_FALSE(rv); |
| 509 EXPECT_EQ(old_address, memory.memory()); |
| 510 } |
| 511 |
| 500 #if defined(OS_POSIX) | 512 #if defined(OS_POSIX) |
| 501 // Create a shared memory object, mmap it, and mprotect it to PROT_EXEC. | 513 // Create a shared memory object, mmap it, and mprotect it to PROT_EXEC. |
| 502 TEST(SharedMemoryTest, AnonymousExecutable) { | 514 TEST(SharedMemoryTest, AnonymousExecutable) { |
| 503 const uint32 kTestSize = 1 << 16; | 515 const uint32 kTestSize = 1 << 16; |
| 504 | 516 |
| 505 SharedMemory shared_memory; | 517 SharedMemory shared_memory; |
| 506 SharedMemoryCreateOptions options; | 518 SharedMemoryCreateOptions options; |
| 507 options.size = kTestSize; | 519 options.size = kTestSize; |
| 508 options.executable = true; | 520 options.executable = true; |
| 509 | 521 |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 661 SharedMemoryProcessTest::CleanUp(); | 673 SharedMemoryProcessTest::CleanUp(); |
| 662 } | 674 } |
| 663 | 675 |
| 664 MULTIPROCESS_TEST_MAIN(SharedMemoryTestMain) { | 676 MULTIPROCESS_TEST_MAIN(SharedMemoryTestMain) { |
| 665 return SharedMemoryProcessTest::TaskTestMain(); | 677 return SharedMemoryProcessTest::TaskTestMain(); |
| 666 } | 678 } |
| 667 | 679 |
| 668 #endif // !OS_IOS | 680 #endif // !OS_IOS |
| 669 | 681 |
| 670 } // namespace base | 682 } // namespace base |
| OLD | NEW |