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/memory/shared_memory.h" | 5 #include "base/memory/shared_memory.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
(...skipping 392 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
403 } | 403 } |
404 | 404 |
405 TEST(SharedMemoryTest, ShareToSelf) { | 405 TEST(SharedMemoryTest, ShareToSelf) { |
406 StringPiece contents = "Hello World"; | 406 StringPiece contents = "Hello World"; |
407 | 407 |
408 SharedMemory shmem; | 408 SharedMemory shmem; |
409 ASSERT_TRUE(shmem.CreateAndMapAnonymous(contents.size())); | 409 ASSERT_TRUE(shmem.CreateAndMapAnonymous(contents.size())); |
410 memcpy(shmem.memory(), contents.data(), contents.size()); | 410 memcpy(shmem.memory(), contents.data(), contents.size()); |
411 EXPECT_TRUE(shmem.Unmap()); | 411 EXPECT_TRUE(shmem.Unmap()); |
412 | 412 |
413 SharedMemoryHandle shared_handle; | 413 SharedMemoryHandle shared_handle = shmem.handle().Duplicate(); |
414 ASSERT_TRUE(shmem.ShareToProcess(GetCurrentProcessHandle(), &shared_handle)); | 414 ASSERT_TRUE(shared_handle.IsValid()); |
415 #if defined(OS_WIN) | 415 #if defined(OS_WIN) |
416 ASSERT_TRUE(shared_handle.OwnershipPassesToIPC()); | 416 ASSERT_TRUE(shared_handle.OwnershipPassesToIPC()); |
417 #endif | 417 #endif |
418 SharedMemory shared(shared_handle, /*readonly=*/false); | 418 SharedMemory shared(shared_handle, /*readonly=*/false); |
419 | 419 |
420 ASSERT_TRUE(shared.Map(contents.size())); | 420 ASSERT_TRUE(shared.Map(contents.size())); |
421 EXPECT_EQ( | 421 EXPECT_EQ( |
422 contents, | 422 contents, |
423 StringPiece(static_cast<const char*>(shared.memory()), contents.size())); | 423 StringPiece(static_cast<const char*>(shared.memory()), contents.size())); |
424 | 424 |
425 shared_handle = SharedMemoryHandle(); | 425 shared_handle = shmem.handle().Duplicate(); |
426 ASSERT_TRUE(shmem.ShareToProcess(GetCurrentProcessHandle(), &shared_handle)); | 426 ASSERT_TRUE(shared_handle.IsValid()); |
427 #if defined(OS_WIN) | 427 #if defined(OS_WIN) |
428 ASSERT_TRUE(shared_handle.OwnershipPassesToIPC()); | 428 ASSERT_TRUE(shared_handle.OwnershipPassesToIPC()); |
429 #endif | 429 #endif |
430 SharedMemory readonly(shared_handle, /*readonly=*/true); | 430 SharedMemory readonly(shared_handle, /*readonly=*/true); |
431 | 431 |
432 ASSERT_TRUE(readonly.Map(contents.size())); | 432 ASSERT_TRUE(readonly.Map(contents.size())); |
433 EXPECT_EQ(contents, | 433 EXPECT_EQ(contents, |
434 StringPiece(static_cast<const char*>(readonly.memory()), | 434 StringPiece(static_cast<const char*>(readonly.memory()), |
435 contents.size())); | 435 contents.size())); |
436 } | 436 } |
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
700 memory.Close(); | 700 memory.Close(); |
701 SharedMemoryProcessTest::CleanUp(); | 701 SharedMemoryProcessTest::CleanUp(); |
702 } | 702 } |
703 | 703 |
704 MULTIPROCESS_TEST_MAIN(SharedMemoryTestMain) { | 704 MULTIPROCESS_TEST_MAIN(SharedMemoryTestMain) { |
705 return SharedMemoryProcessTest::TaskTestMain(); | 705 return SharedMemoryProcessTest::TaskTestMain(); |
706 } | 706 } |
707 #endif // !defined(OS_IOS) && !defined(OS_ANDROID) && !defined(OS_MACOSX) | 707 #endif // !defined(OS_IOS) && !defined(OS_ANDROID) && !defined(OS_MACOSX) |
708 | 708 |
709 } // namespace base | 709 } // namespace base |
OLD | NEW |