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

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

Issue 1867733002: mac: Remove POSIX shared memory. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@temp50_base
Patch Set: Comments from amistry. 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 | « ipc/ipc_message_utils.cc ('k') | mojo/edk/embedder/platform_shared_buffer.cc » ('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
(...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after
295 #define MAYBE_MultiprocessBaseSharedMemory DISABLED_MultiprocessBaseSharedMemory 295 #define MAYBE_MultiprocessBaseSharedMemory DISABLED_MultiprocessBaseSharedMemory
296 #else 296 #else
297 #define MAYBE_MultiprocessBaseSharedMemory MultiprocessBaseSharedMemory 297 #define MAYBE_MultiprocessBaseSharedMemory MultiprocessBaseSharedMemory
298 #endif // defined(OS_ANDROID) 298 #endif // defined(OS_ANDROID)
299 TEST_F(EmbedderTest, MAYBE_MultiprocessBaseSharedMemory) { 299 TEST_F(EmbedderTest, MAYBE_MultiprocessBaseSharedMemory) {
300 RUN_CHILD_ON_PIPE(MultiprocessSharedMemoryClient, server_mp) 300 RUN_CHILD_ON_PIPE(MultiprocessSharedMemoryClient, server_mp)
301 // 1. Create a base::SharedMemory object and create a mojo shared buffer 301 // 1. Create a base::SharedMemory object and create a mojo shared buffer
302 // from it. 302 // from it.
303 base::SharedMemoryCreateOptions options; 303 base::SharedMemoryCreateOptions options;
304 options.size = 123; 304 options.size = 123;
305 #if defined(OS_MACOSX) && !defined(OS_IOS)
306 options.type = base::SharedMemoryHandle::POSIX;
Nico 2016/04/13 01:59:59 this isn't needed? why was this here?
Anand Mistry (off Chromium) 2016/04/13 12:10:48 This was here because we supported both posix and
307 #endif
308 base::SharedMemory shared_memory; 305 base::SharedMemory shared_memory;
309 ASSERT_TRUE(shared_memory.Create(options)); 306 ASSERT_TRUE(shared_memory.Create(options));
310 base::SharedMemoryHandle shm_handle = base::SharedMemory::DuplicateHandle( 307 base::SharedMemoryHandle shm_handle = base::SharedMemory::DuplicateHandle(
311 shared_memory.handle()); 308 shared_memory.handle());
312 MojoHandle sb1; 309 MojoHandle sb1;
313 ASSERT_EQ(MOJO_RESULT_OK, 310 ASSERT_EQ(MOJO_RESULT_OK,
314 CreateSharedBufferWrapper(shm_handle, 123, false, &sb1)); 311 CreateSharedBufferWrapper(shm_handle, 123, false, &sb1));
315 312
316 // 2. Map |sb1| and write something into it. 313 // 2. Map |sb1| and write something into it.
317 char* buffer = nullptr; 314 char* buffer = nullptr;
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
370 base::SharedMemory shared_memory(shm_handle, false); 367 base::SharedMemory shared_memory(shm_handle, false);
371 ASSERT_TRUE(shared_memory.Map(123)); 368 ASSERT_TRUE(shared_memory.Map(123));
372 EXPECT_NE(buffer, shared_memory.memory()); 369 EXPECT_NE(buffer, shared_memory.memory());
373 EXPECT_EQ(kByeWorld, std::string(static_cast<char*>(shared_memory.memory()))); 370 EXPECT_EQ(kByeWorld, std::string(static_cast<char*>(shared_memory.memory())));
374 371
375 // 6. Close |sb1|. Should fail because |PassSharedMemoryHandle()| should have 372 // 6. Close |sb1|. Should fail because |PassSharedMemoryHandle()| should have
376 // closed the handle. 373 // closed the handle.
377 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(sb1)); 374 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(sb1));
378 } 375 }
379 376
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
513 // TODO(vtl): Test immediate write & close. 377 // TODO(vtl): Test immediate write & close.
514 // TODO(vtl): Test broken-connection cases. 378 // TODO(vtl): Test broken-connection cases.
515 379
516 #endif // !defined(OS_IOS) 380 #endif // !defined(OS_IOS)
517 381
518 } // namespace 382 } // namespace
519 } // namespace edk 383 } // namespace edk
520 } // namespace mojo 384 } // namespace mojo
OLDNEW
« no previous file with comments | « ipc/ipc_message_utils.cc ('k') | mojo/edk/embedder/platform_shared_buffer.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698