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 |
(...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
296 #define MAYBE_MultiprocessBaseSharedMemory DISABLED_MultiprocessBaseSharedMemory | 296 #define MAYBE_MultiprocessBaseSharedMemory DISABLED_MultiprocessBaseSharedMemory |
297 #else | 297 #else |
298 #define MAYBE_MultiprocessBaseSharedMemory MultiprocessBaseSharedMemory | 298 #define MAYBE_MultiprocessBaseSharedMemory MultiprocessBaseSharedMemory |
299 #endif // defined(OS_ANDROID) | 299 #endif // defined(OS_ANDROID) |
300 TEST_F(EmbedderTest, MAYBE_MultiprocessBaseSharedMemory) { | 300 TEST_F(EmbedderTest, MAYBE_MultiprocessBaseSharedMemory) { |
301 RUN_CHILD_ON_PIPE(MultiprocessSharedMemoryClient, server_mp) | 301 RUN_CHILD_ON_PIPE(MultiprocessSharedMemoryClient, server_mp) |
302 // 1. Create a base::SharedMemory object and create a mojo shared buffer | 302 // 1. Create a base::SharedMemory object and create a mojo shared buffer |
303 // from it. | 303 // from it. |
304 base::SharedMemoryCreateOptions options; | 304 base::SharedMemoryCreateOptions options; |
305 options.size = 123; | 305 options.size = 123; |
306 #if defined(OS_MACOSX) && !defined(OS_IOS) | |
307 options.type = base::SharedMemoryHandle::POSIX; | |
308 #endif | |
309 base::SharedMemory shared_memory; | 306 base::SharedMemory shared_memory; |
310 ASSERT_TRUE(shared_memory.Create(options)); | 307 ASSERT_TRUE(shared_memory.Create(options)); |
311 base::SharedMemoryHandle shm_handle = base::SharedMemory::DuplicateHandle( | 308 base::SharedMemoryHandle shm_handle = base::SharedMemory::DuplicateHandle( |
312 shared_memory.handle()); | 309 shared_memory.handle()); |
313 MojoHandle sb1; | 310 MojoHandle sb1; |
314 ASSERT_EQ(MOJO_RESULT_OK, | 311 ASSERT_EQ(MOJO_RESULT_OK, |
315 CreateSharedBufferWrapper(shm_handle, 123, false, &sb1)); | 312 CreateSharedBufferWrapper(shm_handle, 123, false, &sb1)); |
316 | 313 |
317 // 2. Map |sb1| and write something into it. | 314 // 2. Map |sb1| and write something into it. |
318 char* buffer = nullptr; | 315 char* buffer = nullptr; |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
417 // expected value. | 414 // expected value. |
418 ASSERT_TRUE(shared_memory.Map(123)); | 415 ASSERT_TRUE(shared_memory.Map(123)); |
419 EXPECT_EQ(kByeWorld, | 416 EXPECT_EQ(kByeWorld, |
420 std::string(static_cast<char*>(shared_memory.memory()))); | 417 std::string(static_cast<char*>(shared_memory.memory()))); |
421 | 418 |
422 ASSERT_EQ(MOJO_RESULT_OK, MojoClose(sb1)); | 419 ASSERT_EQ(MOJO_RESULT_OK, MojoClose(sb1)); |
423 END_CHILD() | 420 END_CHILD() |
424 } | 421 } |
425 | 422 |
426 enum class HandleType { | 423 enum class HandleType { |
427 POSIX, | |
Anand Mistry (off Chromium)
2016/04/16 01:27:54
Please restore this. I changed the test so it no l
erikchen
2016/04/18 17:14:39
Done.
| |
428 MACH, | 424 MACH, |
429 MACH_NULL, | 425 MACH_NULL, |
430 }; | 426 }; |
431 | 427 |
432 const HandleType kTestHandleTypes[] = { | 428 const HandleType kTestHandleTypes[] = { |
433 HandleType::MACH, | 429 HandleType::MACH, |
434 HandleType::MACH_NULL, | 430 HandleType::MACH_NULL, |
435 HandleType::POSIX, | |
436 HandleType::POSIX, | |
437 HandleType::MACH, | 431 HandleType::MACH, |
438 }; | 432 }; |
439 | 433 |
440 // Test that we can mix file descriptors and mach port handles. | 434 // Test that we can mix file descriptors and mach port handles. |
441 TEST_F(EmbedderTest, MultiprocessMixMachAndFds) { | 435 TEST_F(EmbedderTest, MultiprocessMixMachAndFds) { |
442 const size_t kShmSize = 1234; | 436 const size_t kShmSize = 1234; |
443 RUN_CHILD_ON_PIPE(MultiprocessMixMachAndFdsClient, server_mp) | 437 RUN_CHILD_ON_PIPE(MultiprocessMixMachAndFdsClient, server_mp) |
444 // 1. Create fds or Mach objects and mojo handles from them. | 438 // 1. Create fds or Mach objects and mojo handles from them. |
445 MojoHandle platform_handles[arraysize(kTestHandleTypes)]; | 439 MojoHandle platform_handles[arraysize(kTestHandleTypes)]; |
446 for (size_t i = 0; i < arraysize(kTestHandleTypes); i++) { | 440 for (size_t i = 0; i < arraysize(kTestHandleTypes); i++) { |
447 const auto type = kTestHandleTypes[i]; | 441 const auto type = kTestHandleTypes[i]; |
448 ScopedPlatformHandle scoped_handle; | 442 ScopedPlatformHandle scoped_handle; |
449 if (type == HandleType::POSIX) { | 443 if (type == HandleType::MACH_NULL) { |
450 // The easiest source of fds is opening /dev/null. | |
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( | 444 scoped_handle.reset(PlatformHandle( |
458 static_cast<mach_port_t>(MACH_PORT_NULL))); | 445 static_cast<mach_port_t>(MACH_PORT_NULL))); |
459 EXPECT_EQ(PlatformHandle::Type::MACH, scoped_handle.get().type); | 446 EXPECT_EQ(PlatformHandle::Type::MACH, scoped_handle.get().type); |
460 } else { | 447 } else { |
461 base::SharedMemoryCreateOptions options; | 448 base::SharedMemoryCreateOptions options; |
462 options.size = kShmSize; | 449 options.size = kShmSize; |
463 options.type = base::SharedMemoryHandle::MACH; | 450 options.type = base::SharedMemoryHandle::MACH; |
464 base::SharedMemory shared_memory; | 451 base::SharedMemory shared_memory; |
465 ASSERT_TRUE(shared_memory.Create(options)); | 452 ASSERT_TRUE(shared_memory.Create(options)); |
466 base::SharedMemoryHandle shm_handle = | 453 base::SharedMemoryHandle shm_handle = |
(...skipping 23 matching lines...) Expand all Loading... | |
490 // |kNumHandles| handles. | 477 // |kNumHandles| handles. |
491 EXPECT_EQ("hello", | 478 EXPECT_EQ("hello", |
492 ReadMessageWithHandles(client_mp, platform_handles, kNumHandles)); | 479 ReadMessageWithHandles(client_mp, platform_handles, kNumHandles)); |
493 | 480 |
494 // 2. Extract each handle, and verify the type. | 481 // 2. Extract each handle, and verify the type. |
495 for (int i = 0; i < kNumHandles; i++) { | 482 for (int i = 0; i < kNumHandles; i++) { |
496 const auto type = kTestHandleTypes[i]; | 483 const auto type = kTestHandleTypes[i]; |
497 ScopedPlatformHandle scoped_handle; | 484 ScopedPlatformHandle scoped_handle; |
498 ASSERT_EQ(MOJO_RESULT_OK, | 485 ASSERT_EQ(MOJO_RESULT_OK, |
499 PassWrappedPlatformHandle(platform_handles[i], &scoped_handle)); | 486 PassWrappedPlatformHandle(platform_handles[i], &scoped_handle)); |
500 if (type == HandleType::POSIX) { | 487 if (type == HandleType::MACH_NULL) { |
501 EXPECT_NE(0, scoped_handle.get().handle); | |
502 EXPECT_EQ(PlatformHandle::Type::POSIX, scoped_handle.get().type); | |
503 } else if (type == HandleType::MACH_NULL) { | |
504 EXPECT_EQ(static_cast<mach_port_t>(MACH_PORT_NULL), | 488 EXPECT_EQ(static_cast<mach_port_t>(MACH_PORT_NULL), |
505 scoped_handle.get().port); | 489 scoped_handle.get().port); |
506 EXPECT_EQ(PlatformHandle::Type::MACH, scoped_handle.get().type); | 490 EXPECT_EQ(PlatformHandle::Type::MACH, scoped_handle.get().type); |
507 } else { | 491 } else { |
508 EXPECT_NE(static_cast<mach_port_t>(MACH_PORT_NULL), | 492 EXPECT_NE(static_cast<mach_port_t>(MACH_PORT_NULL), |
509 scoped_handle.get().port); | 493 scoped_handle.get().port); |
510 EXPECT_EQ(PlatformHandle::Type::MACH, scoped_handle.get().type); | 494 EXPECT_EQ(PlatformHandle::Type::MACH, scoped_handle.get().type); |
511 } | 495 } |
512 } | 496 } |
513 | 497 |
514 // 3. Say bye! | 498 // 3. Say bye! |
515 WriteMessage(client_mp, "bye"); | 499 WriteMessage(client_mp, "bye"); |
516 } | 500 } |
517 | 501 |
518 #endif // defined(OS_MACOSX) && !defined(OS_IOS) | 502 #endif // defined(OS_MACOSX) && !defined(OS_IOS) |
519 | 503 |
520 // TODO(vtl): Test immediate write & close. | 504 // TODO(vtl): Test immediate write & close. |
521 // TODO(vtl): Test broken-connection cases. | 505 // TODO(vtl): Test broken-connection cases. |
522 | 506 |
523 #endif // !defined(OS_IOS) | 507 #endif // !defined(OS_IOS) |
524 | 508 |
525 } // namespace | 509 } // namespace |
526 } // namespace edk | 510 } // namespace edk |
527 } // namespace mojo | 511 } // namespace mojo |
OLD | NEW |