Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(16)

Side by Side Diff: mojo/edk/embedder/embedder_unittest.cc

Issue 1890043002: [mojo-edk] Support transferring null Mach ports. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add header comments. Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | mojo/edk/system/channel.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/files/file.h"
15 #include "base/logging.h" 16 #include "base/logging.h"
16 #include "base/macros.h" 17 #include "base/macros.h"
17 #include "base/memory/shared_memory.h" 18 #include "base/memory/shared_memory.h"
18 #include "base/message_loop/message_loop.h" 19 #include "base/message_loop/message_loop.h"
19 #include "base/synchronization/waitable_event.h" 20 #include "base/synchronization/waitable_event.h"
20 #include "base/test/test_timeouts.h" 21 #include "base/test/test_timeouts.h"
21 #include "mojo/edk/embedder/platform_channel_pair.h" 22 #include "mojo/edk/embedder/platform_channel_pair.h"
22 #include "mojo/edk/embedder/test_embedder.h" 23 #include "mojo/edk/embedder/test_embedder.h"
23 #include "mojo/edk/system/test_utils.h" 24 #include "mojo/edk/system/test_utils.h"
24 #include "mojo/edk/test/mojo_test_base.h" 25 #include "mojo/edk/test/mojo_test_base.h"
(...skipping 390 matching lines...) Expand 10 before | Expand all | Expand 10 after
415 // 6. Map the original base::SharedMemory and expect it contains the 416 // 6. Map the original base::SharedMemory and expect it contains the
416 // expected value. 417 // expected value.
417 ASSERT_TRUE(shared_memory.Map(123)); 418 ASSERT_TRUE(shared_memory.Map(123));
418 EXPECT_EQ(kByeWorld, 419 EXPECT_EQ(kByeWorld,
419 std::string(static_cast<char*>(shared_memory.memory()))); 420 std::string(static_cast<char*>(shared_memory.memory())));
420 421
421 ASSERT_EQ(MOJO_RESULT_OK, MojoClose(sb1)); 422 ASSERT_EQ(MOJO_RESULT_OK, MojoClose(sb1));
422 END_CHILD() 423 END_CHILD()
423 } 424 }
424 425
425 const base::SharedMemoryHandle::Type kTestHandleTypes[] = { 426 enum class HandleType {
426 base::SharedMemoryHandle::MACH, 427 POSIX,
427 base::SharedMemoryHandle::POSIX, 428 MACH,
428 base::SharedMemoryHandle::POSIX, 429 MACH_NULL,
429 base::SharedMemoryHandle::MACH,
430 }; 430 };
431 431
432 // Test that we can mix file descriptor and mach port handles. 432 const HandleType kTestHandleTypes[] = {
433 HandleType::MACH,
434 HandleType::MACH_NULL,
435 HandleType::POSIX,
436 HandleType::POSIX,
437 HandleType::MACH,
438 };
439
440 // Test that we can mix file descriptors and mach port handles.
433 TEST_F(EmbedderTest, MultiprocessMixMachAndFds) { 441 TEST_F(EmbedderTest, MultiprocessMixMachAndFds) {
434 const size_t kShmSize = 1234; 442 const size_t kShmSize = 1234;
435 RUN_CHILD_ON_PIPE(MultiprocessMixMachAndFdsClient, server_mp) 443 RUN_CHILD_ON_PIPE(MultiprocessMixMachAndFdsClient, server_mp)
436 // 1. Create the base::SharedMemory objects and mojo handles from them. 444 // 1. Create fds or Mach objects and mojo handles from them.
437 MojoHandle platform_handles[arraysize(kTestHandleTypes)]; 445 MojoHandle platform_handles[arraysize(kTestHandleTypes)];
438 for (size_t i = 0; i < arraysize(kTestHandleTypes); i++) { 446 for (size_t i = 0; i < arraysize(kTestHandleTypes); i++) {
439 const auto type = kTestHandleTypes[i]; 447 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 ScopedPlatformHandle scoped_handle;
448 if (type == base::SharedMemoryHandle::POSIX) 449 if (type == HandleType::POSIX) {
449 scoped_handle.reset(PlatformHandle(shm_handle.GetFileDescriptor().fd)); 450 // The easiest source of fds is opening /dev/null.
450 else 451 base::File file(base::FilePath("/dev/null"),
452 base::File::FLAG_OPEN | base::File::FLAG_WRITE);
453 ASSERT_TRUE(file.IsValid());
454 scoped_handle.reset(PlatformHandle(file.TakePlatformFile()));
455 EXPECT_EQ(PlatformHandle::Type::POSIX, scoped_handle.get().type);
456 } else if (type == HandleType::MACH_NULL) {
457 scoped_handle.reset(PlatformHandle(
458 static_cast<mach_port_t>(MACH_PORT_NULL)));
459 EXPECT_EQ(PlatformHandle::Type::MACH, scoped_handle.get().type);
460 } else {
461 base::SharedMemoryCreateOptions options;
462 options.size = kShmSize;
463 options.type = base::SharedMemoryHandle::MACH;
464 base::SharedMemory shared_memory;
465 ASSERT_TRUE(shared_memory.Create(options));
466 base::SharedMemoryHandle shm_handle =
467 base::SharedMemory::DuplicateHandle(shared_memory.handle());
451 scoped_handle.reset(PlatformHandle(shm_handle.GetMemoryObject())); 468 scoped_handle.reset(PlatformHandle(shm_handle.GetMemoryObject()));
469 EXPECT_EQ(PlatformHandle::Type::MACH, scoped_handle.get().type);
470 }
452 ASSERT_EQ(MOJO_RESULT_OK, CreatePlatformHandleWrapper( 471 ASSERT_EQ(MOJO_RESULT_OK, CreatePlatformHandleWrapper(
453 std::move(scoped_handle), platform_handles + i)); 472 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 } 473 }
461 474
462 // 2. Send all the handles to the child. 475 // 2. Send all the handles to the child.
463 WriteMessageWithHandles(server_mp, "hello", platform_handles, 476 WriteMessageWithHandles(server_mp, "hello", platform_handles,
464 arraysize(kTestHandleTypes)); 477 arraysize(kTestHandleTypes));
465 478
466 // 3. Read a message from |server_mp|. 479 // 3. Read a message from |server_mp|.
467 EXPECT_EQ("bye", ReadMessage(server_mp)); 480 EXPECT_EQ("bye", ReadMessage(server_mp));
468 END_CHILD() 481 END_CHILD()
469 } 482 }
470 483
471 DEFINE_TEST_CLIENT_TEST_WITH_PIPE(MultiprocessMixMachAndFdsClient, EmbedderTest, 484 DEFINE_TEST_CLIENT_TEST_WITH_PIPE(MultiprocessMixMachAndFdsClient, EmbedderTest,
472 client_mp) { 485 client_mp) {
473 const int kNumHandles = 4; 486 const int kNumHandles = arraysize(kTestHandleTypes);
474 const size_t kShmSize = 1234;
475 MojoHandle platform_handles[kNumHandles]; 487 MojoHandle platform_handles[kNumHandles];
476 488
477 // 1. Read from |client_mp|, which should have a message containing 489 // 1. Read from |client_mp|, which should have a message containing
478 // |kNumHandles| handles. 490 // |kNumHandles| handles.
479 EXPECT_EQ("hello", 491 EXPECT_EQ("hello",
480 ReadMessageWithHandles(client_mp, platform_handles, kNumHandles)); 492 ReadMessageWithHandles(client_mp, platform_handles, kNumHandles));
481 493
482 // 2. Extract each handle, map it, and verify the type. 494 // 2. Extract each handle, and verify the type.
483 for (int i = 0; i < kNumHandles; i++) { 495 for (int i = 0; i < kNumHandles; i++) {
496 const auto type = kTestHandleTypes[i];
484 ScopedPlatformHandle scoped_handle; 497 ScopedPlatformHandle scoped_handle;
485 ASSERT_EQ(MOJO_RESULT_OK, 498 ASSERT_EQ(MOJO_RESULT_OK,
486 PassWrappedPlatformHandle(platform_handles[i], &scoped_handle)); 499 PassWrappedPlatformHandle(platform_handles[i], &scoped_handle));
487 base::SharedMemoryHandle shm_handle; 500 if (type == HandleType::POSIX) {
488 char type = 0; 501 EXPECT_NE(0, scoped_handle.get().handle);
489 if (scoped_handle.get().type == PlatformHandle::Type::POSIX) { 502 EXPECT_EQ(PlatformHandle::Type::POSIX, scoped_handle.get().type);
490 shm_handle = base::SharedMemoryHandle(scoped_handle.release().handle, 503 } else if (type == HandleType::MACH_NULL) {
491 false); 504 EXPECT_EQ(static_cast<mach_port_t>(MACH_PORT_NULL),
492 type = 'P'; 505 scoped_handle.get().port);
506 EXPECT_EQ(PlatformHandle::Type::MACH, scoped_handle.get().type);
493 } else { 507 } else {
494 shm_handle = base::SharedMemoryHandle(scoped_handle.release().port, 508 EXPECT_NE(static_cast<mach_port_t>(MACH_PORT_NULL),
495 kShmSize, base::GetCurrentProcId()); 509 scoped_handle.get().port);
496 type = 'M'; 510 EXPECT_EQ(PlatformHandle::Type::MACH, scoped_handle.get().type);
497 } 511 }
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 } 512 }
506 513
507 // 3. Say bye! 514 // 3. Say bye!
508 WriteMessage(client_mp, "bye"); 515 WriteMessage(client_mp, "bye");
509 } 516 }
510 517
511 #endif // defined(OS_MACOSX) && !defined(OS_IOS) 518 #endif // defined(OS_MACOSX) && !defined(OS_IOS)
512 519
513 // TODO(vtl): Test immediate write & close. 520 // TODO(vtl): Test immediate write & close.
514 // TODO(vtl): Test broken-connection cases. 521 // TODO(vtl): Test broken-connection cases.
515 522
516 #endif // !defined(OS_IOS) 523 #endif // !defined(OS_IOS)
517 524
518 } // namespace 525 } // namespace
519 } // namespace edk 526 } // namespace edk
520 } // namespace mojo 527 } // namespace mojo
OLDNEW
« no previous file with comments | « no previous file | mojo/edk/system/channel.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698