OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 <mach/mach.h> | 5 #include <mach/mach.h> |
6 #include <mach/mach_vm.h> | 6 #include <mach/mach_vm.h> |
7 #include <servers/bootstrap.h> | 7 #include <servers/bootstrap.h> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/mac/mac_util.h" | 10 #include "base/mac/mac_util.h" |
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
224 | 224 |
225 // Tests that content written to shared memory in the server process can be read | 225 // Tests that content written to shared memory in the server process can be read |
226 // by the child process. | 226 // by the child process. |
227 TEST_F(SharedMemoryMacMultiProcessTest, MachBasedSharedMemory) { | 227 TEST_F(SharedMemoryMacMultiProcessTest, MachBasedSharedMemory) { |
228 // Mach-based SharedMemory isn't support on OSX 10.6. | 228 // Mach-based SharedMemory isn't support on OSX 10.6. |
229 if (mac::IsOSSnowLeopard()) | 229 if (mac::IsOSSnowLeopard()) |
230 return; | 230 return; |
231 | 231 |
232 SetUpChild("MachBasedSharedMemoryClient"); | 232 SetUpChild("MachBasedSharedMemoryClient"); |
233 | 233 |
234 scoped_ptr<SharedMemory> shared_memory( | 234 scoped_ptr<SharedMemory> shared_memory(CreateSharedMemory(s_memory_size)); |
235 CreateSharedMemory(s_memory_size).Pass()); | |
236 | 235 |
237 // Send the underlying memory object to the client process. | 236 // Send the underlying memory object to the client process. |
238 SendMachPort(client_port_.get(), shared_memory->handle().GetMemoryObject(), | 237 SendMachPort(client_port_.get(), shared_memory->handle().GetMemoryObject(), |
239 MACH_MSG_TYPE_COPY_SEND); | 238 MACH_MSG_TYPE_COPY_SEND); |
240 int rv = -1; | 239 int rv = -1; |
241 ASSERT_TRUE(child_process_.WaitForExitWithTimeout( | 240 ASSERT_TRUE(child_process_.WaitForExitWithTimeout( |
242 TestTimeouts::action_timeout(), &rv)); | 241 TestTimeouts::action_timeout(), &rv)); |
243 EXPECT_EQ(0, rv); | 242 EXPECT_EQ(0, rv); |
244 } | 243 } |
245 | 244 |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
376 shared_memory.reset(); | 375 shared_memory.reset(); |
377 EXPECT_EQ(active_name_count, GetActiveNameCount()); | 376 EXPECT_EQ(active_name_count, GetActiveNameCount()); |
378 } | 377 } |
379 | 378 |
380 // Tests that the read-only flag works. | 379 // Tests that the read-only flag works. |
381 TEST_F(SharedMemoryMacMultiProcessTest, MachReadOnly) { | 380 TEST_F(SharedMemoryMacMultiProcessTest, MachReadOnly) { |
382 // Mach-based SharedMemory isn't support on OSX 10.6. | 381 // Mach-based SharedMemory isn't support on OSX 10.6. |
383 if (mac::IsOSSnowLeopard()) | 382 if (mac::IsOSSnowLeopard()) |
384 return; | 383 return; |
385 | 384 |
386 scoped_ptr<SharedMemory> shared_memory( | 385 scoped_ptr<SharedMemory> shared_memory(CreateSharedMemory(s_memory_size)); |
387 CreateSharedMemory(s_memory_size).Pass()); | |
388 | 386 |
389 SharedMemoryHandle shm2 = shared_memory->handle().Duplicate(); | 387 SharedMemoryHandle shm2 = shared_memory->handle().Duplicate(); |
390 ASSERT_TRUE(shm2.IsValid()); | 388 ASSERT_TRUE(shm2.IsValid()); |
391 SharedMemory shared_memory2(shm2, true); | 389 SharedMemory shared_memory2(shm2, true); |
392 shared_memory2.Map(s_memory_size); | 390 shared_memory2.Map(s_memory_size); |
393 ASSERT_DEATH(memset(shared_memory2.memory(), 'b', s_memory_size), ""); | 391 ASSERT_DEATH(memset(shared_memory2.memory(), 'b', s_memory_size), ""); |
394 } | 392 } |
395 | 393 |
396 // Tests that the method ShareToProcess() works. | 394 // Tests that the method ShareToProcess() works. |
397 TEST_F(SharedMemoryMacMultiProcessTest, MachShareToProcess) { | 395 TEST_F(SharedMemoryMacMultiProcessTest, MachShareToProcess) { |
398 // Mach-based SharedMemory isn't support on OSX 10.6. | 396 // Mach-based SharedMemory isn't support on OSX 10.6. |
399 if (mac::IsOSSnowLeopard()) | 397 if (mac::IsOSSnowLeopard()) |
400 return; | 398 return; |
401 | 399 |
402 mach_msg_type_number_t active_name_count = GetActiveNameCount(); | 400 mach_msg_type_number_t active_name_count = GetActiveNameCount(); |
403 | 401 |
404 { | 402 { |
405 scoped_ptr<SharedMemory> shared_memory( | 403 scoped_ptr<SharedMemory> shared_memory(CreateSharedMemory(s_memory_size)); |
406 CreateSharedMemory(s_memory_size).Pass()); | |
407 | 404 |
408 SharedMemoryHandle shm2; | 405 SharedMemoryHandle shm2; |
409 ASSERT_TRUE(shared_memory->ShareToProcess(GetCurrentProcId(), &shm2)); | 406 ASSERT_TRUE(shared_memory->ShareToProcess(GetCurrentProcId(), &shm2)); |
410 ASSERT_TRUE(shm2.IsValid()); | 407 ASSERT_TRUE(shm2.IsValid()); |
411 SharedMemory shared_memory2(shm2, true); | 408 SharedMemory shared_memory2(shm2, true); |
412 shared_memory2.Map(s_memory_size); | 409 shared_memory2.Map(s_memory_size); |
413 | 410 |
414 ASSERT_EQ(0, memcmp(shared_memory->memory(), shared_memory2.memory(), | 411 ASSERT_EQ(0, memcmp(shared_memory->memory(), shared_memory2.memory(), |
415 s_memory_size)); | 412 s_memory_size)); |
416 } | 413 } |
417 | 414 |
418 EXPECT_EQ(active_name_count, GetActiveNameCount()); | 415 EXPECT_EQ(active_name_count, GetActiveNameCount()); |
419 } | 416 } |
420 | 417 |
421 // Tests that the method ShareReadOnlyToProcess() creates a memory object that | 418 // Tests that the method ShareReadOnlyToProcess() creates a memory object that |
422 // is read only. | 419 // is read only. |
423 TEST_F(SharedMemoryMacMultiProcessTest, MachShareToProcessReadonly) { | 420 TEST_F(SharedMemoryMacMultiProcessTest, MachShareToProcessReadonly) { |
424 // Mach-based SharedMemory isn't support on OSX 10.6. | 421 // Mach-based SharedMemory isn't support on OSX 10.6. |
425 if (mac::IsOSSnowLeopard()) | 422 if (mac::IsOSSnowLeopard()) |
426 return; | 423 return; |
427 | 424 |
428 scoped_ptr<SharedMemory> shared_memory( | 425 scoped_ptr<SharedMemory> shared_memory(CreateSharedMemory(s_memory_size)); |
429 CreateSharedMemory(s_memory_size).Pass()); | |
430 | 426 |
431 // Check the protection levels. | 427 // Check the protection levels. |
432 int current_prot, max_prot; | 428 int current_prot, max_prot; |
433 ASSERT_TRUE(GetProtections(shared_memory->memory(), | 429 ASSERT_TRUE(GetProtections(shared_memory->memory(), |
434 shared_memory->mapped_size(), ¤t_prot, | 430 shared_memory->mapped_size(), ¤t_prot, |
435 &max_prot)); | 431 &max_prot)); |
436 ASSERT_EQ(VM_PROT_READ | VM_PROT_WRITE, current_prot); | 432 ASSERT_EQ(VM_PROT_READ | VM_PROT_WRITE, current_prot); |
437 ASSERT_EQ(VM_PROT_READ | VM_PROT_WRITE, max_prot); | 433 ASSERT_EQ(VM_PROT_READ | VM_PROT_WRITE, max_prot); |
438 | 434 |
439 // Make a new memory object. | 435 // Make a new memory object. |
(...skipping 25 matching lines...) Expand all Loading... |
465 | 461 |
466 // Tests that the method ShareReadOnlyToProcess() doesn't leak. | 462 // Tests that the method ShareReadOnlyToProcess() doesn't leak. |
467 TEST_F(SharedMemoryMacMultiProcessTest, MachShareToProcessReadonlyLeak) { | 463 TEST_F(SharedMemoryMacMultiProcessTest, MachShareToProcessReadonlyLeak) { |
468 // Mach-based SharedMemory isn't support on OSX 10.6. | 464 // Mach-based SharedMemory isn't support on OSX 10.6. |
469 if (mac::IsOSSnowLeopard()) | 465 if (mac::IsOSSnowLeopard()) |
470 return; | 466 return; |
471 | 467 |
472 mach_msg_type_number_t active_name_count = GetActiveNameCount(); | 468 mach_msg_type_number_t active_name_count = GetActiveNameCount(); |
473 | 469 |
474 { | 470 { |
475 scoped_ptr<SharedMemory> shared_memory( | 471 scoped_ptr<SharedMemory> shared_memory(CreateSharedMemory(s_memory_size)); |
476 CreateSharedMemory(s_memory_size).Pass()); | |
477 | 472 |
478 SharedMemoryHandle shm2; | 473 SharedMemoryHandle shm2; |
479 ASSERT_TRUE( | 474 ASSERT_TRUE( |
480 shared_memory->ShareReadOnlyToProcess(GetCurrentProcId(), &shm2)); | 475 shared_memory->ShareReadOnlyToProcess(GetCurrentProcId(), &shm2)); |
481 ASSERT_TRUE(shm2.IsValid()); | 476 ASSERT_TRUE(shm2.IsValid()); |
482 | 477 |
483 // Intentionally map with |readonly| set to |false|. | 478 // Intentionally map with |readonly| set to |false|. |
484 SharedMemory shared_memory2(shm2, false); | 479 SharedMemory shared_memory2(shm2, false); |
485 shared_memory2.Map(s_memory_size); | 480 shared_memory2.Map(s_memory_size); |
486 } | 481 } |
487 | 482 |
488 EXPECT_EQ(active_name_count, GetActiveNameCount()); | 483 EXPECT_EQ(active_name_count, GetActiveNameCount()); |
489 } | 484 } |
490 | 485 |
491 } // namespace base | 486 } // namespace base |
OLD | NEW |