OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "mojo/edk/embedder/embedder.h" | 5 #include "mojo/edk/embedder/embedder.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 #include <string.h> | 9 #include <string.h> |
10 | 10 |
11 #include <utility> | 11 #include <utility> |
12 | 12 |
13 #include "base/bind.h" | 13 #include "base/bind.h" |
14 #include "base/command_line.h" | 14 #include "base/command_line.h" |
15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "base/macros.h" |
16 #include "base/memory/shared_memory.h" | 17 #include "base/memory/shared_memory.h" |
17 #include "base/message_loop/message_loop.h" | 18 #include "base/message_loop/message_loop.h" |
18 #include "base/synchronization/waitable_event.h" | 19 #include "base/synchronization/waitable_event.h" |
19 #include "base/test/test_timeouts.h" | 20 #include "base/test/test_timeouts.h" |
20 #include "mojo/edk/embedder/platform_channel_pair.h" | 21 #include "mojo/edk/embedder/platform_channel_pair.h" |
21 #include "mojo/edk/embedder/test_embedder.h" | 22 #include "mojo/edk/embedder/test_embedder.h" |
22 #include "mojo/edk/system/test_utils.h" | 23 #include "mojo/edk/system/test_utils.h" |
23 #include "mojo/edk/test/mojo_test_base.h" | 24 #include "mojo/edk/test/mojo_test_base.h" |
24 #include "mojo/message_pump/message_pump_mojo.h" | 25 #include "mojo/message_pump/message_pump_mojo.h" |
25 #include "mojo/public/c/system/core.h" | 26 #include "mojo/public/c/system/core.h" |
(...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
369 base::SharedMemory shared_memory(shm_handle, false); | 370 base::SharedMemory shared_memory(shm_handle, false); |
370 ASSERT_TRUE(shared_memory.Map(123)); | 371 ASSERT_TRUE(shared_memory.Map(123)); |
371 EXPECT_NE(buffer, shared_memory.memory()); | 372 EXPECT_NE(buffer, shared_memory.memory()); |
372 EXPECT_EQ(kByeWorld, std::string(static_cast<char*>(shared_memory.memory()))); | 373 EXPECT_EQ(kByeWorld, std::string(static_cast<char*>(shared_memory.memory()))); |
373 | 374 |
374 // 6. Close |sb1|. Should fail because |PassSharedMemoryHandle()| should have | 375 // 6. Close |sb1|. Should fail because |PassSharedMemoryHandle()| should have |
375 // closed the handle. | 376 // closed the handle. |
376 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(sb1)); | 377 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(sb1)); |
377 } | 378 } |
378 | 379 |
| 380 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 381 TEST_F(EmbedderTest, MultiprocessMachSharedMemory) { |
| 382 RUN_CHILD_ON_PIPE(MultiprocessSharedMemoryClient, server_mp) |
| 383 // 1. Create a Mach base::SharedMemory object and create a mojo shared |
| 384 // buffer from it. |
| 385 base::SharedMemoryCreateOptions options; |
| 386 options.size = 123; |
| 387 options.type = base::SharedMemoryHandle::MACH; |
| 388 base::SharedMemory shared_memory; |
| 389 ASSERT_TRUE(shared_memory.Create(options)); |
| 390 base::SharedMemoryHandle shm_handle = base::SharedMemory::DuplicateHandle( |
| 391 shared_memory.handle()); |
| 392 MojoHandle sb1; |
| 393 ASSERT_EQ(MOJO_RESULT_OK, |
| 394 CreateSharedBufferWrapper(shm_handle, 123, false, &sb1)); |
| 395 |
| 396 // 2. Map |sb1| and write something into it. |
| 397 char* buffer = nullptr; |
| 398 ASSERT_EQ(MOJO_RESULT_OK, |
| 399 MojoMapBuffer(sb1, 0, 123, reinterpret_cast<void**>(&buffer), 0)); |
| 400 ASSERT_TRUE(buffer); |
| 401 memcpy(buffer, kHelloWorld, sizeof(kHelloWorld)); |
| 402 |
| 403 // 3. Duplicate |sb1| into |sb2| and pass to |server_mp|. |
| 404 MojoHandle sb2 = MOJO_HANDLE_INVALID; |
| 405 EXPECT_EQ(MOJO_RESULT_OK, MojoDuplicateBufferHandle(sb1, 0, &sb2)); |
| 406 EXPECT_NE(MOJO_HANDLE_INVALID, sb2); |
| 407 WriteMessageWithHandles(server_mp, "hello", &sb2, 1); |
| 408 |
| 409 // 4. Read a message from |server_mp|. |
| 410 EXPECT_EQ("bye", ReadMessage(server_mp)); |
| 411 |
| 412 // 5. Expect that the contents of the shared buffer have changed. |
| 413 EXPECT_EQ(kByeWorld, std::string(buffer)); |
| 414 |
| 415 // 6. Map the original base::SharedMemory and expect it contains the |
| 416 // expected value. |
| 417 ASSERT_TRUE(shared_memory.Map(123)); |
| 418 EXPECT_EQ(kByeWorld, |
| 419 std::string(static_cast<char*>(shared_memory.memory()))); |
| 420 |
| 421 ASSERT_EQ(MOJO_RESULT_OK, MojoClose(sb1)); |
| 422 END_CHILD() |
| 423 } |
| 424 |
| 425 const base::SharedMemoryHandle::Type kTestHandleTypes[] = { |
| 426 base::SharedMemoryHandle::MACH, |
| 427 base::SharedMemoryHandle::POSIX, |
| 428 base::SharedMemoryHandle::POSIX, |
| 429 base::SharedMemoryHandle::MACH, |
| 430 }; |
| 431 |
| 432 // Test that we can mix file descriptor and mach port handles. |
| 433 TEST_F(EmbedderTest, MultiprocessMixMachAndFds) { |
| 434 const size_t kShmSize = 1234; |
| 435 RUN_CHILD_ON_PIPE(MultiprocessMixMachAndFdsClient, server_mp) |
| 436 // 1. Create the base::SharedMemory objects and mojo handles from them. |
| 437 MojoHandle platform_handles[arraysize(kTestHandleTypes)]; |
| 438 for (size_t i = 0; i < arraysize(kTestHandleTypes); i++) { |
| 439 const auto type = kTestHandleTypes[i]; |
| 440 base::SharedMemoryCreateOptions options; |
| 441 options.size = kShmSize; |
| 442 options.type = type; |
| 443 base::SharedMemory shared_memory; |
| 444 ASSERT_TRUE(shared_memory.Create(options)); |
| 445 base::SharedMemoryHandle shm_handle = base::SharedMemory::DuplicateHandle( |
| 446 shared_memory.handle()); |
| 447 ScopedPlatformHandle scoped_handle; |
| 448 if (type == base::SharedMemoryHandle::POSIX) |
| 449 scoped_handle.reset(PlatformHandle(shm_handle.GetFileDescriptor().fd)); |
| 450 else |
| 451 scoped_handle.reset(PlatformHandle(shm_handle.GetMemoryObject())); |
| 452 ASSERT_EQ(MOJO_RESULT_OK, CreatePlatformHandleWrapper( |
| 453 std::move(scoped_handle), platform_handles + i)); |
| 454 |
| 455 // Map the shared memory object and write the type into it. 'P' for POSIX, |
| 456 // and 'M' for Mach. |
| 457 ASSERT_TRUE(shared_memory.Map(kShmSize)); |
| 458 static_cast<char*>(shared_memory.memory())[0] = |
| 459 type == base::SharedMemoryHandle::POSIX ? 'P' : 'M'; |
| 460 } |
| 461 |
| 462 // 2. Send all the handles to the child. |
| 463 WriteMessageWithHandles(server_mp, "hello", platform_handles, |
| 464 arraysize(kTestHandleTypes)); |
| 465 |
| 466 // 3. Read a message from |server_mp|. |
| 467 EXPECT_EQ("bye", ReadMessage(server_mp)); |
| 468 END_CHILD() |
| 469 } |
| 470 |
| 471 DEFINE_TEST_CLIENT_TEST_WITH_PIPE(MultiprocessMixMachAndFdsClient, EmbedderTest, |
| 472 client_mp) { |
| 473 const int kNumHandles = 4; |
| 474 const size_t kShmSize = 1234; |
| 475 MojoHandle platform_handles[kNumHandles]; |
| 476 |
| 477 // 1. Read from |client_mp|, which should have a message containing |
| 478 // |kNumHandles| handles. |
| 479 EXPECT_EQ("hello", |
| 480 ReadMessageWithHandles(client_mp, platform_handles, kNumHandles)); |
| 481 |
| 482 // 2. Extract each handle, map it, and verify the type. |
| 483 for (int i = 0; i < kNumHandles; i++) { |
| 484 ScopedPlatformHandle scoped_handle; |
| 485 ASSERT_EQ(MOJO_RESULT_OK, |
| 486 PassWrappedPlatformHandle(platform_handles[i], &scoped_handle)); |
| 487 base::SharedMemoryHandle shm_handle; |
| 488 char type = 0; |
| 489 if (scoped_handle.get().type == PlatformHandle::Type::POSIX) { |
| 490 shm_handle = base::SharedMemoryHandle(scoped_handle.release().handle, |
| 491 false); |
| 492 type = 'P'; |
| 493 } else { |
| 494 shm_handle = base::SharedMemoryHandle(scoped_handle.release().port, |
| 495 kShmSize, base::GetCurrentProcId()); |
| 496 type = 'M'; |
| 497 } |
| 498 |
| 499 // Verify the type order. |
| 500 EXPECT_EQ(kTestHandleTypes[i], shm_handle.GetType()); |
| 501 |
| 502 base::SharedMemory shared_memory(shm_handle, false); |
| 503 ASSERT_TRUE(shared_memory.Map(kShmSize)); |
| 504 EXPECT_EQ(type, static_cast<char*>(shared_memory.memory())[0]); |
| 505 } |
| 506 |
| 507 // 3. Say bye! |
| 508 WriteMessage(client_mp, "bye"); |
| 509 } |
| 510 |
| 511 #endif // defined(OS_MACOSX) && !defined(OS_IOS) |
| 512 |
379 // TODO(vtl): Test immediate write & close. | 513 // TODO(vtl): Test immediate write & close. |
380 // TODO(vtl): Test broken-connection cases. | 514 // TODO(vtl): Test broken-connection cases. |
381 | 515 |
382 #endif // !defined(OS_IOS) | 516 #endif // !defined(OS_IOS) |
383 | 517 |
384 } // namespace | 518 } // namespace |
385 } // namespace edk | 519 } // namespace edk |
386 } // namespace mojo | 520 } // namespace mojo |
OLD | NEW |