| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // These tests are POSIX only. | 5 // These tests are POSIX only. |
| 6 | 6 |
| 7 #include "ipc/ipc_channel_posix.h" | 7 #include "ipc/ipc_channel_posix.h" |
| 8 | 8 |
| 9 #include <fcntl.h> | 9 #include <fcntl.h> |
| 10 #include <sys/socket.h> | 10 #include <sys/socket.h> |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 | 110 |
| 111 protected: | 111 protected: |
| 112 void SetUp() override; | 112 void SetUp() override; |
| 113 void TearDown() override; | 113 void TearDown() override; |
| 114 | 114 |
| 115 private: | 115 private: |
| 116 scoped_ptr<base::MessageLoopForIO> message_loop_; | 116 scoped_ptr<base::MessageLoopForIO> message_loop_; |
| 117 }; | 117 }; |
| 118 | 118 |
| 119 const std::string IPCChannelPosixTest::GetChannelDirName() { | 119 const std::string IPCChannelPosixTest::GetChannelDirName() { |
| 120 #if defined(OS_ANDROID) | |
| 121 base::FilePath tmp_dir; | 120 base::FilePath tmp_dir; |
| 122 PathService::Get(base::DIR_CACHE, &tmp_dir); | 121 PathService::Get(base::DIR_TEMP, &tmp_dir); |
| 123 return tmp_dir.value(); | 122 return tmp_dir.value(); |
| 124 #else | |
| 125 return "/var/tmp"; | |
| 126 #endif | |
| 127 } | 123 } |
| 128 | 124 |
| 129 const std::string IPCChannelPosixTest::GetConnectionSocketName() { | 125 const std::string IPCChannelPosixTest::GetConnectionSocketName() { |
| 130 return GetChannelDirName() + "/chrome_IPCChannelPosixTest__ConnectionSocket"; | 126 return GetChannelDirName() + "/chrome_IPCChannelPosixTest__ConnectionSocket"; |
| 131 } | 127 } |
| 132 | 128 |
| 133 void IPCChannelPosixTest::SetUp() { | 129 void IPCChannelPosixTest::SetUp() { |
| 134 MultiProcessTest::SetUp(); | 130 MultiProcessTest::SetUp(); |
| 135 // Construct a fresh IO Message loop for the duration of each test. | 131 // Construct a fresh IO Message loop for the duration of each test. |
| 136 message_loop_.reset(new base::MessageLoopForIO()); | 132 message_loop_.reset(new base::MessageLoopForIO()); |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 IPC::ChannelHandle handle(kChannelName); | 200 IPC::ChannelHandle handle(kChannelName); |
| 205 SetUpSocket(&handle, IPC::Channel::MODE_NAMED_SERVER); | 201 SetUpSocket(&handle, IPC::Channel::MODE_NAMED_SERVER); |
| 206 unlink(handle.name.c_str()); | 202 unlink(handle.name.c_str()); |
| 207 scoped_ptr<IPC::ChannelPosix> channel( | 203 scoped_ptr<IPC::ChannelPosix> channel( |
| 208 new IPC::ChannelPosix(handle, IPC::Channel::MODE_NAMED_SERVER, NULL)); | 204 new IPC::ChannelPosix(handle, IPC::Channel::MODE_NAMED_SERVER, NULL)); |
| 209 ASSERT_TRUE(channel->Connect()); | 205 ASSERT_TRUE(channel->Connect()); |
| 210 ASSERT_TRUE(channel->AcceptsConnections()); | 206 ASSERT_TRUE(channel->AcceptsConnections()); |
| 211 ASSERT_FALSE(channel->HasAcceptedConnection()); | 207 ASSERT_FALSE(channel->HasAcceptedConnection()); |
| 212 channel->ResetToAcceptingConnectionState(); | 208 channel->ResetToAcceptingConnectionState(); |
| 213 ASSERT_FALSE(channel->HasAcceptedConnection()); | 209 ASSERT_FALSE(channel->HasAcceptedConnection()); |
| 210 unlink(handle.name.c_str()); |
| 214 } | 211 } |
| 215 | 212 |
| 216 TEST_F(IPCChannelPosixTest, BasicConnected) { | 213 TEST_F(IPCChannelPosixTest, BasicConnected) { |
| 217 // Test creating a socket that is connected. | 214 // Test creating a socket that is connected. |
| 218 int pipe_fds[2]; | 215 int pipe_fds[2]; |
| 219 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, pipe_fds)); | 216 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, pipe_fds)); |
| 220 std::string socket_name("/var/tmp/IPCChannelPosixTest_BasicConnected"); | 217 std::string socket_name("/var/tmp/IPCChannelPosixTest_BasicConnected"); |
| 221 ASSERT_GE(fcntl(pipe_fds[0], F_SETFL, O_NONBLOCK), 0); | 218 ASSERT_GE(fcntl(pipe_fds[0], F_SETFL, O_NONBLOCK), 0); |
| 222 | 219 |
| 223 base::FileDescriptor fd(pipe_fds[0], false); | 220 base::FileDescriptor fd(pipe_fds[0], false); |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 301 IPC::Message* message = new IPC::Message(0, // routing_id | 298 IPC::Message* message = new IPC::Message(0, // routing_id |
| 302 kQuitMessage, // message type | 299 kQuitMessage, // message type |
| 303 IPC::Message::PRIORITY_NORMAL); | 300 IPC::Message::PRIORITY_NORMAL); |
| 304 channel->Send(message); | 301 channel->Send(message); |
| 305 SpinRunLoop(TestTimeouts::action_timeout()); | 302 SpinRunLoop(TestTimeouts::action_timeout()); |
| 306 int exit_code = 0; | 303 int exit_code = 0; |
| 307 EXPECT_TRUE(process.WaitForExit(&exit_code)); | 304 EXPECT_TRUE(process.WaitForExit(&exit_code)); |
| 308 EXPECT_EQ(0, exit_code); | 305 EXPECT_EQ(0, exit_code); |
| 309 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); | 306 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); |
| 310 ASSERT_FALSE(channel->HasAcceptedConnection()); | 307 ASSERT_FALSE(channel->HasAcceptedConnection()); |
| 308 unlink(chan_handle.name.c_str()); |
| 311 } | 309 } |
| 312 | 310 |
| 313 TEST_F(IPCChannelPosixTest, ResetState) { | 311 TEST_F(IPCChannelPosixTest, ResetState) { |
| 314 // Test creating a connection to an external process. Close the connection, | 312 // Test creating a connection to an external process. Close the connection, |
| 315 // but continue to listen and make sure another external process can connect | 313 // but continue to listen and make sure another external process can connect |
| 316 // to us. | 314 // to us. |
| 317 IPCChannelPosixTestListener listener(false); | 315 IPCChannelPosixTestListener listener(false); |
| 318 IPC::ChannelHandle chan_handle(GetConnectionSocketName()); | 316 IPC::ChannelHandle chan_handle(GetConnectionSocketName()); |
| 319 SetUpSocket(&chan_handle, IPC::Channel::MODE_NAMED_SERVER); | 317 SetUpSocket(&chan_handle, IPC::Channel::MODE_NAMED_SERVER); |
| 320 scoped_ptr<IPC::ChannelPosix> channel(new IPC::ChannelPosix( | 318 scoped_ptr<IPC::ChannelPosix> channel(new IPC::ChannelPosix( |
| (...skipping 19 matching lines...) Expand all Loading... |
| 340 kQuitMessage, // message type | 338 kQuitMessage, // message type |
| 341 IPC::Message::PRIORITY_NORMAL); | 339 IPC::Message::PRIORITY_NORMAL); |
| 342 channel->Send(message); | 340 channel->Send(message); |
| 343 SpinRunLoop(TestTimeouts::action_timeout()); | 341 SpinRunLoop(TestTimeouts::action_timeout()); |
| 344 EXPECT_TRUE(base::KillProcess(process.Handle(), 0, false)); | 342 EXPECT_TRUE(base::KillProcess(process.Handle(), 0, false)); |
| 345 int exit_code = 0; | 343 int exit_code = 0; |
| 346 EXPECT_TRUE(process2.WaitForExit(&exit_code)); | 344 EXPECT_TRUE(process2.WaitForExit(&exit_code)); |
| 347 EXPECT_EQ(0, exit_code); | 345 EXPECT_EQ(0, exit_code); |
| 348 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); | 346 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); |
| 349 ASSERT_FALSE(channel->HasAcceptedConnection()); | 347 ASSERT_FALSE(channel->HasAcceptedConnection()); |
| 348 unlink(chan_handle.name.c_str()); |
| 350 } | 349 } |
| 351 | 350 |
| 352 TEST_F(IPCChannelPosixTest, BadChannelName) { | 351 TEST_F(IPCChannelPosixTest, BadChannelName) { |
| 353 // Test empty name | 352 // Test empty name |
| 354 IPC::ChannelHandle handle(""); | 353 IPC::ChannelHandle handle(""); |
| 355 scoped_ptr<IPC::ChannelPosix> channel(new IPC::ChannelPosix( | 354 scoped_ptr<IPC::ChannelPosix> channel(new IPC::ChannelPosix( |
| 356 handle, IPC::Channel::MODE_NAMED_SERVER, NULL)); | 355 handle, IPC::Channel::MODE_NAMED_SERVER, NULL)); |
| 357 ASSERT_FALSE(channel->Connect()); | 356 ASSERT_FALSE(channel->Connect()); |
| 358 | 357 |
| 359 // Test name that is too long. | 358 // Test name that is too long. |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 398 ASSERT_TRUE(channel->HasAcceptedConnection()); | 397 ASSERT_TRUE(channel->HasAcceptedConnection()); |
| 399 IPC::Message* message = new IPC::Message(0, // routing_id | 398 IPC::Message* message = new IPC::Message(0, // routing_id |
| 400 kQuitMessage, // message type | 399 kQuitMessage, // message type |
| 401 IPC::Message::PRIORITY_NORMAL); | 400 IPC::Message::PRIORITY_NORMAL); |
| 402 channel->Send(message); | 401 channel->Send(message); |
| 403 SpinRunLoop(TestTimeouts::action_timeout()); | 402 SpinRunLoop(TestTimeouts::action_timeout()); |
| 404 EXPECT_TRUE(process.WaitForExit(&exit_code)); | 403 EXPECT_TRUE(process.WaitForExit(&exit_code)); |
| 405 EXPECT_EQ(exit_code, 0); | 404 EXPECT_EQ(exit_code, 0); |
| 406 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); | 405 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); |
| 407 ASSERT_FALSE(channel->HasAcceptedConnection()); | 406 ASSERT_FALSE(channel->HasAcceptedConnection()); |
| 407 unlink(chan_handle.name.c_str()); |
| 408 } | 408 } |
| 409 | 409 |
| 410 TEST_F(IPCChannelPosixTest, DoubleServer) { | 410 TEST_F(IPCChannelPosixTest, DoubleServer) { |
| 411 // Test setting up two servers with the same name. | 411 // Test setting up two servers with the same name. |
| 412 IPCChannelPosixTestListener listener(false); | 412 IPCChannelPosixTestListener listener(false); |
| 413 IPCChannelPosixTestListener listener2(false); | 413 IPCChannelPosixTestListener listener2(false); |
| 414 IPC::ChannelHandle chan_handle(GetConnectionSocketName()); | 414 IPC::ChannelHandle chan_handle(GetConnectionSocketName()); |
| 415 scoped_ptr<IPC::ChannelPosix> channel(new IPC::ChannelPosix( | 415 scoped_ptr<IPC::ChannelPosix> channel(new IPC::ChannelPosix( |
| 416 chan_handle, IPC::Channel::MODE_SERVER, &listener)); | 416 chan_handle, IPC::Channel::MODE_SERVER, &listener)); |
| 417 scoped_ptr<IPC::ChannelPosix> channel2(new IPC::ChannelPosix( | 417 scoped_ptr<IPC::ChannelPosix> channel2(new IPC::ChannelPosix( |
| (...skipping 18 matching lines...) Expand all Loading... |
| 436 ASSERT_TRUE(base::DeleteFile(base::FilePath(connection_socket_name), false)); | 436 ASSERT_TRUE(base::DeleteFile(base::FilePath(connection_socket_name), false)); |
| 437 ASSERT_FALSE(IPC::Channel::IsNamedServerInitialized( | 437 ASSERT_FALSE(IPC::Channel::IsNamedServerInitialized( |
| 438 connection_socket_name)); | 438 connection_socket_name)); |
| 439 scoped_ptr<IPC::ChannelPosix> channel(new IPC::ChannelPosix( | 439 scoped_ptr<IPC::ChannelPosix> channel(new IPC::ChannelPosix( |
| 440 chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener)); | 440 chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener)); |
| 441 ASSERT_TRUE(IPC::Channel::IsNamedServerInitialized( | 441 ASSERT_TRUE(IPC::Channel::IsNamedServerInitialized( |
| 442 connection_socket_name)); | 442 connection_socket_name)); |
| 443 channel->Close(); | 443 channel->Close(); |
| 444 ASSERT_FALSE(IPC::Channel::IsNamedServerInitialized( | 444 ASSERT_FALSE(IPC::Channel::IsNamedServerInitialized( |
| 445 connection_socket_name)); | 445 connection_socket_name)); |
| 446 unlink(chan_handle.name.c_str()); |
| 446 } | 447 } |
| 447 | 448 |
| 448 // A long running process that connects to us | 449 // A long running process that connects to us |
| 449 MULTIPROCESS_TEST_MAIN(IPCChannelPosixTestConnectionProc) { | 450 MULTIPROCESS_TEST_MAIN(IPCChannelPosixTestConnectionProc) { |
| 450 base::MessageLoopForIO message_loop; | 451 base::MessageLoopForIO message_loop; |
| 451 IPCChannelPosixTestListener listener(true); | 452 IPCChannelPosixTestListener listener(true); |
| 452 IPC::ChannelHandle handle(IPCChannelPosixTest::GetConnectionSocketName()); | 453 IPC::ChannelHandle handle(IPCChannelPosixTest::GetConnectionSocketName()); |
| 453 IPCChannelPosixTest::SetUpSocket(&handle, IPC::Channel::MODE_NAMED_CLIENT); | 454 IPCChannelPosixTest::SetUpSocket(&handle, IPC::Channel::MODE_NAMED_CLIENT); |
| 454 scoped_ptr<IPC::ChannelPosix> channel(new IPC::ChannelPosix( | 455 scoped_ptr<IPC::ChannelPosix> channel(new IPC::ChannelPosix( |
| 455 handle, IPC::Channel::MODE_NAMED_CLIENT, &listener)); | 456 handle, IPC::Channel::MODE_NAMED_CLIENT, &listener)); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 477 if (connected) { | 478 if (connected) { |
| 478 IPCChannelPosixTest::SpinRunLoop(TestTimeouts::action_max_timeout()); | 479 IPCChannelPosixTest::SpinRunLoop(TestTimeouts::action_max_timeout()); |
| 479 EXPECT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); | 480 EXPECT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); |
| 480 } else { | 481 } else { |
| 481 EXPECT_EQ(IPCChannelPosixTestListener::DISCONNECTED, listener.status()); | 482 EXPECT_EQ(IPCChannelPosixTestListener::DISCONNECTED, listener.status()); |
| 482 } | 483 } |
| 483 return 0; | 484 return 0; |
| 484 } | 485 } |
| 485 | 486 |
| 486 } // namespace | 487 } // namespace |
| OLD | NEW |