| 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 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 // Pass the service name to the child process. | 197 // Pass the service name to the child process. |
| 198 command_line.AppendSwitchASCII(g_service_switch_name, service_name_); | 198 command_line.AppendSwitchASCII(g_service_switch_name, service_name_); |
| 199 return command_line; | 199 return command_line; |
| 200 } | 200 } |
| 201 | 201 |
| 202 void SetUpChild(const std::string& name) { | 202 void SetUpChild(const std::string& name) { |
| 203 // Make a random service name so that this test doesn't conflict with other | 203 // Make a random service name so that this test doesn't conflict with other |
| 204 // similar tests. | 204 // similar tests. |
| 205 service_name_ = CreateRandomServiceName(); | 205 service_name_ = CreateRandomServiceName(); |
| 206 server_port_.reset(BecomeMachServer(service_name_.c_str())); | 206 server_port_.reset(BecomeMachServer(service_name_.c_str())); |
| 207 child_process_ = SpawnChild(name); | 207 spawn_child_ = SpawnChild(name); |
| 208 client_port_.reset(ReceiveMachPort(server_port_.get())); | 208 client_port_.reset(ReceiveMachPort(server_port_.get())); |
| 209 } | 209 } |
| 210 | 210 |
| 211 static const int s_memory_size = 99999; | 211 static const int s_memory_size = 99999; |
| 212 | 212 |
| 213 protected: | 213 protected: |
| 214 std::string service_name_; | 214 std::string service_name_; |
| 215 | 215 |
| 216 // A port on which the main process listens for mach messages from the child | 216 // A port on which the main process listens for mach messages from the child |
| 217 // process. | 217 // process. |
| 218 mac::ScopedMachReceiveRight server_port_; | 218 mac::ScopedMachReceiveRight server_port_; |
| 219 | 219 |
| 220 // A port on which the child process listens for mach messages from the main | 220 // A port on which the child process listens for mach messages from the main |
| 221 // process. | 221 // process. |
| 222 mac::ScopedMachSendRight client_port_; | 222 mac::ScopedMachSendRight client_port_; |
| 223 | 223 |
| 224 base::Process child_process_; | 224 base::SpawnChildResult spawn_child_; |
| 225 DISALLOW_COPY_AND_ASSIGN(SharedMemoryMacMultiProcessTest); | 225 DISALLOW_COPY_AND_ASSIGN(SharedMemoryMacMultiProcessTest); |
| 226 }; | 226 }; |
| 227 | 227 |
| 228 // Tests that content written to shared memory in the server process can be read | 228 // Tests that content written to shared memory in the server process can be read |
| 229 // by the child process. | 229 // by the child process. |
| 230 TEST_F(SharedMemoryMacMultiProcessTest, MachBasedSharedMemory) { | 230 TEST_F(SharedMemoryMacMultiProcessTest, MachBasedSharedMemory) { |
| 231 SetUpChild("MachBasedSharedMemoryClient"); | 231 SetUpChild("MachBasedSharedMemoryClient"); |
| 232 | 232 |
| 233 std::unique_ptr<SharedMemory> shared_memory( | 233 std::unique_ptr<SharedMemory> shared_memory( |
| 234 CreateSharedMemory(s_memory_size)); | 234 CreateSharedMemory(s_memory_size)); |
| 235 | 235 |
| 236 // Send the underlying memory object to the client process. | 236 // Send the underlying memory object to the client process. |
| 237 SendMachPort(client_port_.get(), shared_memory->handle().GetMemoryObject(), | 237 SendMachPort(client_port_.get(), shared_memory->handle().GetMemoryObject(), |
| 238 MACH_MSG_TYPE_COPY_SEND); | 238 MACH_MSG_TYPE_COPY_SEND); |
| 239 int rv = -1; | 239 int rv = -1; |
| 240 ASSERT_TRUE(child_process_.WaitForExitWithTimeout( | 240 ASSERT_TRUE(spawn_child_.process.WaitForExitWithTimeout( |
| 241 TestTimeouts::action_timeout(), &rv)); | 241 TestTimeouts::action_timeout(), &rv)); |
| 242 EXPECT_EQ(0, rv); | 242 EXPECT_EQ(0, rv); |
| 243 } | 243 } |
| 244 | 244 |
| 245 MULTIPROCESS_TEST_MAIN(MachBasedSharedMemoryClient) { | 245 MULTIPROCESS_TEST_MAIN(MachBasedSharedMemoryClient) { |
| 246 mac::ScopedMachReceiveRight client_port(CommonChildProcessSetUp()); | 246 mac::ScopedMachReceiveRight client_port(CommonChildProcessSetUp()); |
| 247 // The next mach port should be for a memory object. | 247 // The next mach port should be for a memory object. |
| 248 mach_port_t memory_object = ReceiveMachPort(client_port.get()); | 248 mach_port_t memory_object = ReceiveMachPort(client_port.get()); |
| 249 SharedMemoryHandle shm(memory_object, | 249 SharedMemoryHandle shm(memory_object, |
| 250 SharedMemoryMacMultiProcessTest::s_memory_size, | 250 SharedMemoryMacMultiProcessTest::s_memory_size, |
| (...skipping 19 matching lines...) Expand all Loading... |
| 270 size_t page_size = SysInfo::VMAllocationGranularity(); | 270 size_t page_size = SysInfo::VMAllocationGranularity(); |
| 271 char* start = static_cast<char*>(shared_memory.memory()); | 271 char* start = static_cast<char*>(shared_memory.memory()); |
| 272 memset(start, 'a', page_size); | 272 memset(start, 'a', page_size); |
| 273 memset(start + page_size, 'b', page_size); | 273 memset(start + page_size, 'b', page_size); |
| 274 memset(start + 2 * page_size, 'c', page_size); | 274 memset(start + 2 * page_size, 'c', page_size); |
| 275 | 275 |
| 276 // Send the underlying memory object to the client process. | 276 // Send the underlying memory object to the client process. |
| 277 SendMachPort( | 277 SendMachPort( |
| 278 client_port_.get(), shm.GetMemoryObject(), MACH_MSG_TYPE_COPY_SEND); | 278 client_port_.get(), shm.GetMemoryObject(), MACH_MSG_TYPE_COPY_SEND); |
| 279 int rv = -1; | 279 int rv = -1; |
| 280 ASSERT_TRUE(child_process_.WaitForExitWithTimeout( | 280 ASSERT_TRUE(spawn_child_.process.WaitForExitWithTimeout( |
| 281 TestTimeouts::action_timeout(), &rv)); | 281 TestTimeouts::action_timeout(), &rv)); |
| 282 EXPECT_EQ(0, rv); | 282 EXPECT_EQ(0, rv); |
| 283 } | 283 } |
| 284 | 284 |
| 285 MULTIPROCESS_TEST_MAIN(MachBasedSharedMemoryWithOffsetClient) { | 285 MULTIPROCESS_TEST_MAIN(MachBasedSharedMemoryWithOffsetClient) { |
| 286 mac::ScopedMachReceiveRight client_port(CommonChildProcessSetUp()); | 286 mac::ScopedMachReceiveRight client_port(CommonChildProcessSetUp()); |
| 287 // The next mach port should be for a memory object. | 287 // The next mach port should be for a memory object. |
| 288 mach_port_t memory_object = ReceiveMachPort(client_port.get()); | 288 mach_port_t memory_object = ReceiveMachPort(client_port.get()); |
| 289 SharedMemoryHandle shm(memory_object, | 289 SharedMemoryHandle shm(memory_object, |
| 290 SharedMemoryMacMultiProcessTest::s_memory_size, | 290 SharedMemoryMacMultiProcessTest::s_memory_size, |
| (...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 450 | 450 |
| 451 // Intentionally map with |readonly| set to |false|. | 451 // Intentionally map with |readonly| set to |false|. |
| 452 SharedMemory shared_memory2(shm2, false); | 452 SharedMemory shared_memory2(shm2, false); |
| 453 shared_memory2.Map(s_memory_size); | 453 shared_memory2.Map(s_memory_size); |
| 454 } | 454 } |
| 455 | 455 |
| 456 EXPECT_EQ(active_name_count, GetActiveNameCount()); | 456 EXPECT_EQ(active_name_count, GetActiveNameCount()); |
| 457 } | 457 } |
| 458 | 458 |
| 459 } // namespace base | 459 } // namespace base |
| OLD | NEW |