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

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

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

Powered by Google App Engine
This is Rietveld 408576698