| 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 <errno.h> | 9 #include <errno.h> |
| 10 #include <fcntl.h> | 10 #include <fcntl.h> |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 }; | 107 }; |
| 108 | 108 |
| 109 class IPCChannelPosixTest : public base::MultiProcessTest { | 109 class IPCChannelPosixTest : public base::MultiProcessTest { |
| 110 public: | 110 public: |
| 111 static void SetUpSocket(IPC::ChannelHandle *handle, | 111 static void SetUpSocket(IPC::ChannelHandle *handle, |
| 112 IPC::Channel::Mode mode); | 112 IPC::Channel::Mode mode); |
| 113 static void SpinRunLoop(base::TimeDelta delay); | 113 static void SpinRunLoop(base::TimeDelta delay); |
| 114 static const std::string GetConnectionSocketName(); | 114 static const std::string GetConnectionSocketName(); |
| 115 static const std::string GetChannelDirName(); | 115 static const std::string GetChannelDirName(); |
| 116 | 116 |
| 117 bool WaitForExit(base::Process& process, int* exit_code) { |
| 118 #if defined(OS_ANDROID) |
| 119 return AndroidWaitForChildExitWithTimeout( |
| 120 process, base::TimeDelta::Max(), exit_code); |
| 121 #else |
| 122 return process.WaitForExit(exit_code); |
| 123 #endif // defined(OS_ANDROID) |
| 124 } |
| 125 |
| 117 protected: | 126 protected: |
| 118 void SetUp() override; | 127 void SetUp() override; |
| 119 void TearDown() override; | 128 void TearDown() override; |
| 120 | 129 |
| 121 private: | 130 private: |
| 122 std::unique_ptr<base::MessageLoopForIO> message_loop_; | 131 std::unique_ptr<base::MessageLoopForIO> message_loop_; |
| 123 }; | 132 }; |
| 124 | 133 |
| 125 const std::string IPCChannelPosixTest::GetChannelDirName() { | 134 const std::string IPCChannelPosixTest::GetChannelDirName() { |
| 126 base::FilePath tmp_dir; | 135 base::FilePath tmp_dir; |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 "OUT", base::FileDescriptor(in_chan->TakeClientFileDescriptor())); | 260 "OUT", base::FileDescriptor(in_chan->TakeClientFileDescriptor())); |
| 252 std::unique_ptr<IPC::ChannelPosix> out_chan(new IPC::ChannelPosix( | 261 std::unique_ptr<IPC::ChannelPosix> out_chan(new IPC::ChannelPosix( |
| 253 out_handle, IPC::Channel::MODE_CLIENT, &out_listener)); | 262 out_handle, IPC::Channel::MODE_CLIENT, &out_listener)); |
| 254 ASSERT_TRUE(in_chan->Connect()); | 263 ASSERT_TRUE(in_chan->Connect()); |
| 255 in_chan->Close(); // simulate remote process dying at an unfortunate time. | 264 in_chan->Close(); // simulate remote process dying at an unfortunate time. |
| 256 ASSERT_FALSE(out_chan->Connect()); | 265 ASSERT_FALSE(out_chan->Connect()); |
| 257 SpinRunLoop(TestTimeouts::action_max_timeout()); | 266 SpinRunLoop(TestTimeouts::action_max_timeout()); |
| 258 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, out_listener.status()); | 267 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, out_listener.status()); |
| 259 } | 268 } |
| 260 | 269 |
| 261 #if defined(OS_ANDROID) | 270 TEST_F(IPCChannelPosixTest, AdvancedConnected) { |
| 262 #define MAYBE_AdvancedConnected DISABLED_AdvancedConnected | |
| 263 #else | |
| 264 #define MAYBE_AdvancedConnected AdvancedConnected | |
| 265 #endif | |
| 266 TEST_F(IPCChannelPosixTest, MAYBE_AdvancedConnected) { | |
| 267 // Test creating a connection to an external process. | 271 // Test creating a connection to an external process. |
| 268 IPCChannelPosixTestListener listener(false); | 272 IPCChannelPosixTestListener listener(false); |
| 269 IPC::ChannelHandle chan_handle(GetConnectionSocketName()); | 273 IPC::ChannelHandle chan_handle(GetConnectionSocketName()); |
| 270 SetUpSocket(&chan_handle, IPC::Channel::MODE_NAMED_SERVER); | 274 SetUpSocket(&chan_handle, IPC::Channel::MODE_NAMED_SERVER); |
| 271 std::unique_ptr<IPC::ChannelPosix> channel(new IPC::ChannelPosix( | 275 std::unique_ptr<IPC::ChannelPosix> channel(new IPC::ChannelPosix( |
| 272 chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener)); | 276 chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener)); |
| 273 ASSERT_TRUE(channel->Connect()); | 277 ASSERT_TRUE(channel->Connect()); |
| 274 ASSERT_TRUE(channel->AcceptsConnections()); | 278 ASSERT_TRUE(channel->AcceptsConnections()); |
| 275 ASSERT_FALSE(channel->HasAcceptedConnection()); | 279 ASSERT_FALSE(channel->HasAcceptedConnection()); |
| 276 | 280 |
| 277 base::Process process = SpawnChild("IPCChannelPosixTestConnectionProc"); | 281 base::Process process = SpawnChild("IPCChannelPosixTestConnectionProc"); |
| 278 ASSERT_TRUE(process.IsValid()); | 282 ASSERT_TRUE(process.IsValid()); |
| 279 SpinRunLoop(TestTimeouts::action_max_timeout()); | 283 SpinRunLoop(TestTimeouts::action_max_timeout()); |
| 280 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status()); | 284 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status()); |
| 281 ASSERT_TRUE(channel->HasAcceptedConnection()); | 285 ASSERT_TRUE(channel->HasAcceptedConnection()); |
| 282 IPC::Message* message = new IPC::Message(0, // routing_id | 286 IPC::Message* message = new IPC::Message(0, // routing_id |
| 283 kQuitMessage, // message type | 287 kQuitMessage, // message type |
| 284 IPC::Message::PRIORITY_NORMAL); | 288 IPC::Message::PRIORITY_NORMAL); |
| 285 channel->Send(message); | 289 channel->Send(message); |
| 286 SpinRunLoop(TestTimeouts::action_timeout()); | 290 SpinRunLoop(TestTimeouts::action_timeout()); |
| 287 int exit_code = 0; | 291 int exit_code = 0; |
| 288 EXPECT_TRUE(process.WaitForExit(&exit_code)); | 292 EXPECT_TRUE(WaitForExit(process, &exit_code)); |
| 289 EXPECT_EQ(0, exit_code); | 293 EXPECT_EQ(0, exit_code); |
| 290 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); | 294 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); |
| 291 ASSERT_FALSE(channel->HasAcceptedConnection()); | 295 ASSERT_FALSE(channel->HasAcceptedConnection()); |
| 292 unlink(chan_handle.name.c_str()); | 296 unlink(chan_handle.name.c_str()); |
| 293 } | 297 } |
| 294 | 298 |
| 295 #if defined(OS_ANDROID) | 299 TEST_F(IPCChannelPosixTest, ResetState) { |
| 296 #define MAYBE_ResetState DISABLED_ResetState | |
| 297 #else | |
| 298 #define MAYBE_ResetState ResetState | |
| 299 #endif | |
| 300 TEST_F(IPCChannelPosixTest, MAYBE_ResetState) { | |
| 301 // Test creating a connection to an external process. Close the connection, | 300 // Test creating a connection to an external process. Close the connection, |
| 302 // but continue to listen and make sure another external process can connect | 301 // but continue to listen and make sure another external process can connect |
| 303 // to us. | 302 // to us. |
| 304 IPCChannelPosixTestListener listener(false); | 303 IPCChannelPosixTestListener listener(false); |
| 305 IPC::ChannelHandle chan_handle(GetConnectionSocketName()); | 304 IPC::ChannelHandle chan_handle(GetConnectionSocketName()); |
| 306 SetUpSocket(&chan_handle, IPC::Channel::MODE_NAMED_SERVER); | 305 SetUpSocket(&chan_handle, IPC::Channel::MODE_NAMED_SERVER); |
| 307 std::unique_ptr<IPC::ChannelPosix> channel(new IPC::ChannelPosix( | 306 std::unique_ptr<IPC::ChannelPosix> channel(new IPC::ChannelPosix( |
| 308 chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener)); | 307 chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener)); |
| 309 ASSERT_TRUE(channel->Connect()); | 308 ASSERT_TRUE(channel->Connect()); |
| 310 ASSERT_TRUE(channel->AcceptsConnections()); | 309 ASSERT_TRUE(channel->AcceptsConnections()); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 323 SpinRunLoop(TestTimeouts::action_max_timeout()); | 322 SpinRunLoop(TestTimeouts::action_max_timeout()); |
| 324 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status()); | 323 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status()); |
| 325 ASSERT_TRUE(channel->HasAcceptedConnection()); | 324 ASSERT_TRUE(channel->HasAcceptedConnection()); |
| 326 IPC::Message* message = new IPC::Message(0, // routing_id | 325 IPC::Message* message = new IPC::Message(0, // routing_id |
| 327 kQuitMessage, // message type | 326 kQuitMessage, // message type |
| 328 IPC::Message::PRIORITY_NORMAL); | 327 IPC::Message::PRIORITY_NORMAL); |
| 329 channel->Send(message); | 328 channel->Send(message); |
| 330 SpinRunLoop(TestTimeouts::action_timeout()); | 329 SpinRunLoop(TestTimeouts::action_timeout()); |
| 331 EXPECT_TRUE(process.Terminate(0, false)); | 330 EXPECT_TRUE(process.Terminate(0, false)); |
| 332 int exit_code = 0; | 331 int exit_code = 0; |
| 333 EXPECT_TRUE(process2.WaitForExit(&exit_code)); | 332 EXPECT_TRUE(WaitForExit(process2, &exit_code)); |
| 334 EXPECT_EQ(0, exit_code); | 333 EXPECT_EQ(0, exit_code); |
| 335 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); | 334 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); |
| 336 ASSERT_FALSE(channel->HasAcceptedConnection()); | 335 ASSERT_FALSE(channel->HasAcceptedConnection()); |
| 337 unlink(chan_handle.name.c_str()); | 336 unlink(chan_handle.name.c_str()); |
| 338 } | 337 } |
| 339 | 338 |
| 340 TEST_F(IPCChannelPosixTest, BadChannelName) { | 339 TEST_F(IPCChannelPosixTest, BadChannelName) { |
| 341 // Test empty name | 340 // Test empty name |
| 342 IPC::ChannelHandle handle(""); | 341 IPC::ChannelHandle handle(""); |
| 343 std::unique_ptr<IPC::ChannelPosix> channel( | 342 std::unique_ptr<IPC::ChannelPosix> channel( |
| 344 new IPC::ChannelPosix(handle, IPC::Channel::MODE_NAMED_SERVER, NULL)); | 343 new IPC::ChannelPosix(handle, IPC::Channel::MODE_NAMED_SERVER, NULL)); |
| 345 ASSERT_FALSE(channel->Connect()); | 344 ASSERT_FALSE(channel->Connect()); |
| 346 | 345 |
| 347 // Test name that is too long. | 346 // Test name that is too long. |
| 348 const char *kTooLongName = "This_is_a_very_long_name_to_proactively_implement" | 347 const char *kTooLongName = "This_is_a_very_long_name_to_proactively_implement" |
| 349 "client-centered_synergy_through_top-line" | 348 "client-centered_synergy_through_top-line" |
| 350 "platforms_Phosfluorescently_disintermediate_" | 349 "platforms_Phosfluorescently_disintermediate_" |
| 351 "clicks-and-mortar_best_practices_without_" | 350 "clicks-and-mortar_best_practices_without_" |
| 352 "future-proof_growth_strategies_Continually" | 351 "future-proof_growth_strategies_Continually" |
| 353 "pontificate_proactive_potentialities_before" | 352 "pontificate_proactive_potentialities_before" |
| 354 "leading-edge_processes"; | 353 "leading-edge_processes"; |
| 355 EXPECT_GE(strlen(kTooLongName), IPC::kMaxSocketNameLength); | 354 EXPECT_GE(strlen(kTooLongName), IPC::kMaxSocketNameLength); |
| 356 IPC::ChannelHandle handle2(kTooLongName); | 355 IPC::ChannelHandle handle2(kTooLongName); |
| 357 std::unique_ptr<IPC::ChannelPosix> channel2( | 356 std::unique_ptr<IPC::ChannelPosix> channel2( |
| 358 new IPC::ChannelPosix(handle2, IPC::Channel::MODE_NAMED_SERVER, NULL)); | 357 new IPC::ChannelPosix(handle2, IPC::Channel::MODE_NAMED_SERVER, NULL)); |
| 359 EXPECT_FALSE(channel2->Connect()); | 358 EXPECT_FALSE(channel2->Connect()); |
| 360 } | 359 } |
| 361 | 360 |
| 362 #if defined(OS_ANDROID) | 361 TEST_F(IPCChannelPosixTest, MultiConnection) { |
| 363 #define MAYBE_MultiConnection DISABLED_MultiConnection | |
| 364 #else | |
| 365 #define MAYBE_MultiConnection MultiConnection | |
| 366 #endif | |
| 367 TEST_F(IPCChannelPosixTest, MAYBE_MultiConnection) { | |
| 368 // Test setting up a connection to an external process, and then have | 362 // Test setting up a connection to an external process, and then have |
| 369 // another external process attempt to connect to us. | 363 // another external process attempt to connect to us. |
| 370 IPCChannelPosixTestListener listener(false); | 364 IPCChannelPosixTestListener listener(false); |
| 371 IPC::ChannelHandle chan_handle(GetConnectionSocketName()); | 365 IPC::ChannelHandle chan_handle(GetConnectionSocketName()); |
| 372 SetUpSocket(&chan_handle, IPC::Channel::MODE_NAMED_SERVER); | 366 SetUpSocket(&chan_handle, IPC::Channel::MODE_NAMED_SERVER); |
| 373 std::unique_ptr<IPC::ChannelPosix> channel(new IPC::ChannelPosix( | 367 std::unique_ptr<IPC::ChannelPosix> channel(new IPC::ChannelPosix( |
| 374 chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener)); | 368 chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener)); |
| 375 ASSERT_TRUE(channel->Connect()); | 369 ASSERT_TRUE(channel->Connect()); |
| 376 ASSERT_TRUE(channel->AcceptsConnections()); | 370 ASSERT_TRUE(channel->AcceptsConnections()); |
| 377 ASSERT_FALSE(channel->HasAcceptedConnection()); | 371 ASSERT_FALSE(channel->HasAcceptedConnection()); |
| 378 | 372 |
| 379 base::Process process = SpawnChild("IPCChannelPosixTestConnectionProc"); | 373 base::Process process = SpawnChild("IPCChannelPosixTestConnectionProc"); |
| 380 ASSERT_TRUE(process.IsValid()); | 374 ASSERT_TRUE(process.IsValid()); |
| 381 SpinRunLoop(TestTimeouts::action_max_timeout()); | 375 SpinRunLoop(TestTimeouts::action_max_timeout()); |
| 382 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status()); | 376 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status()); |
| 383 ASSERT_TRUE(channel->HasAcceptedConnection()); | 377 ASSERT_TRUE(channel->HasAcceptedConnection()); |
| 384 base::Process process2 = SpawnChild("IPCChannelPosixFailConnectionProc"); | 378 base::Process process2 = SpawnChild("IPCChannelPosixFailConnectionProc"); |
| 385 ASSERT_TRUE(process2.IsValid()); | 379 ASSERT_TRUE(process2.IsValid()); |
| 386 SpinRunLoop(TestTimeouts::action_max_timeout()); | 380 SpinRunLoop(TestTimeouts::action_max_timeout()); |
| 387 int exit_code = 0; | 381 int exit_code = 0; |
| 388 EXPECT_TRUE(process2.WaitForExit(&exit_code)); | 382 EXPECT_TRUE(WaitForExit(process2, &exit_code)); |
| 389 EXPECT_EQ(exit_code, 0); | 383 EXPECT_EQ(exit_code, 0); |
| 390 ASSERT_EQ(IPCChannelPosixTestListener::DENIED, listener.status()); | 384 ASSERT_EQ(IPCChannelPosixTestListener::DENIED, listener.status()); |
| 391 ASSERT_TRUE(channel->HasAcceptedConnection()); | 385 ASSERT_TRUE(channel->HasAcceptedConnection()); |
| 392 IPC::Message* message = new IPC::Message(0, // routing_id | 386 IPC::Message* message = new IPC::Message(0, // routing_id |
| 393 kQuitMessage, // message type | 387 kQuitMessage, // message type |
| 394 IPC::Message::PRIORITY_NORMAL); | 388 IPC::Message::PRIORITY_NORMAL); |
| 395 channel->Send(message); | 389 channel->Send(message); |
| 396 SpinRunLoop(TestTimeouts::action_timeout()); | 390 SpinRunLoop(TestTimeouts::action_timeout()); |
| 397 EXPECT_TRUE(process.WaitForExit(&exit_code)); | 391 EXPECT_TRUE(WaitForExit(process, &exit_code)); |
| 398 EXPECT_EQ(exit_code, 0); | 392 EXPECT_EQ(exit_code, 0); |
| 399 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); | 393 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); |
| 400 ASSERT_FALSE(channel->HasAcceptedConnection()); | 394 ASSERT_FALSE(channel->HasAcceptedConnection()); |
| 401 unlink(chan_handle.name.c_str()); | 395 unlink(chan_handle.name.c_str()); |
| 402 } | 396 } |
| 403 | 397 |
| 404 TEST_F(IPCChannelPosixTest, DoubleServer) { | 398 TEST_F(IPCChannelPosixTest, DoubleServer) { |
| 405 // Test setting up two servers with the same name. | 399 // Test setting up two servers with the same name. |
| 406 IPCChannelPosixTestListener listener(false); | 400 IPCChannelPosixTestListener listener(false); |
| 407 IPCChannelPosixTestListener listener2(false); | 401 IPCChannelPosixTestListener listener2(false); |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 472 if (connected) { | 466 if (connected) { |
| 473 IPCChannelPosixTest::SpinRunLoop(TestTimeouts::action_max_timeout()); | 467 IPCChannelPosixTest::SpinRunLoop(TestTimeouts::action_max_timeout()); |
| 474 EXPECT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); | 468 EXPECT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); |
| 475 } else { | 469 } else { |
| 476 EXPECT_EQ(IPCChannelPosixTestListener::DISCONNECTED, listener.status()); | 470 EXPECT_EQ(IPCChannelPosixTestListener::DISCONNECTED, listener.status()); |
| 477 } | 471 } |
| 478 return 0; | 472 return 0; |
| 479 } | 473 } |
| 480 | 474 |
| 481 } // namespace | 475 } // namespace |
| OLD | NEW |