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