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 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
286 // Test creating a connection to an external process. | 286 // Test creating a connection to an external process. |
287 IPCChannelPosixTestListener listener(false); | 287 IPCChannelPosixTestListener listener(false); |
288 IPC::ChannelHandle chan_handle(GetConnectionSocketName()); | 288 IPC::ChannelHandle chan_handle(GetConnectionSocketName()); |
289 SetUpSocket(&chan_handle, IPC::Channel::MODE_NAMED_SERVER); | 289 SetUpSocket(&chan_handle, IPC::Channel::MODE_NAMED_SERVER); |
290 scoped_ptr<IPC::ChannelPosix> channel(new IPC::ChannelPosix( | 290 scoped_ptr<IPC::ChannelPosix> channel(new IPC::ChannelPosix( |
291 chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener)); | 291 chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener)); |
292 ASSERT_TRUE(channel->Connect()); | 292 ASSERT_TRUE(channel->Connect()); |
293 ASSERT_TRUE(channel->AcceptsConnections()); | 293 ASSERT_TRUE(channel->AcceptsConnections()); |
294 ASSERT_FALSE(channel->HasAcceptedConnection()); | 294 ASSERT_FALSE(channel->HasAcceptedConnection()); |
295 | 295 |
296 base::ProcessHandle handle = SpawnChild("IPCChannelPosixTestConnectionProc"); | 296 base::Process process = SpawnChild("IPCChannelPosixTestConnectionProc"); |
297 ASSERT_TRUE(handle); | 297 ASSERT_TRUE(process.IsValid()); |
298 SpinRunLoop(TestTimeouts::action_max_timeout()); | 298 SpinRunLoop(TestTimeouts::action_max_timeout()); |
299 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status()); | 299 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status()); |
300 ASSERT_TRUE(channel->HasAcceptedConnection()); | 300 ASSERT_TRUE(channel->HasAcceptedConnection()); |
301 IPC::Message* message = new IPC::Message(0, // routing_id | 301 IPC::Message* message = new IPC::Message(0, // routing_id |
302 kQuitMessage, // message type | 302 kQuitMessage, // message type |
303 IPC::Message::PRIORITY_NORMAL); | 303 IPC::Message::PRIORITY_NORMAL); |
304 channel->Send(message); | 304 channel->Send(message); |
305 SpinRunLoop(TestTimeouts::action_timeout()); | 305 SpinRunLoop(TestTimeouts::action_timeout()); |
306 int exit_code = 0; | 306 int exit_code = 0; |
307 EXPECT_TRUE(base::WaitForExitCode(handle, &exit_code)); | 307 EXPECT_TRUE(process.WaitForExit(&exit_code)); |
308 EXPECT_EQ(0, exit_code); | 308 EXPECT_EQ(0, exit_code); |
309 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); | 309 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); |
310 ASSERT_FALSE(channel->HasAcceptedConnection()); | 310 ASSERT_FALSE(channel->HasAcceptedConnection()); |
311 } | 311 } |
312 | 312 |
313 TEST_F(IPCChannelPosixTest, ResetState) { | 313 TEST_F(IPCChannelPosixTest, ResetState) { |
314 // Test creating a connection to an external process. Close the connection, | 314 // Test creating a connection to an external process. Close the connection, |
315 // but continue to listen and make sure another external process can connect | 315 // but continue to listen and make sure another external process can connect |
316 // to us. | 316 // to us. |
317 IPCChannelPosixTestListener listener(false); | 317 IPCChannelPosixTestListener listener(false); |
318 IPC::ChannelHandle chan_handle(GetConnectionSocketName()); | 318 IPC::ChannelHandle chan_handle(GetConnectionSocketName()); |
319 SetUpSocket(&chan_handle, IPC::Channel::MODE_NAMED_SERVER); | 319 SetUpSocket(&chan_handle, IPC::Channel::MODE_NAMED_SERVER); |
320 scoped_ptr<IPC::ChannelPosix> channel(new IPC::ChannelPosix( | 320 scoped_ptr<IPC::ChannelPosix> channel(new IPC::ChannelPosix( |
321 chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener)); | 321 chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener)); |
322 ASSERT_TRUE(channel->Connect()); | 322 ASSERT_TRUE(channel->Connect()); |
323 ASSERT_TRUE(channel->AcceptsConnections()); | 323 ASSERT_TRUE(channel->AcceptsConnections()); |
324 ASSERT_FALSE(channel->HasAcceptedConnection()); | 324 ASSERT_FALSE(channel->HasAcceptedConnection()); |
325 | 325 |
326 base::ProcessHandle handle = SpawnChild("IPCChannelPosixTestConnectionProc"); | 326 base::Process process = SpawnChild("IPCChannelPosixTestConnectionProc"); |
327 ASSERT_TRUE(handle); | 327 ASSERT_TRUE(process.IsValid()); |
328 SpinRunLoop(TestTimeouts::action_max_timeout()); | 328 SpinRunLoop(TestTimeouts::action_max_timeout()); |
329 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status()); | 329 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status()); |
330 ASSERT_TRUE(channel->HasAcceptedConnection()); | 330 ASSERT_TRUE(channel->HasAcceptedConnection()); |
331 channel->ResetToAcceptingConnectionState(); | 331 channel->ResetToAcceptingConnectionState(); |
332 ASSERT_FALSE(channel->HasAcceptedConnection()); | 332 ASSERT_FALSE(channel->HasAcceptedConnection()); |
333 | 333 |
334 base::ProcessHandle handle2 = SpawnChild("IPCChannelPosixTestConnectionProc"); | 334 base::Process process2 = SpawnChild("IPCChannelPosixTestConnectionProc"); |
335 ASSERT_TRUE(handle2); | 335 ASSERT_TRUE(process2.IsValid()); |
336 SpinRunLoop(TestTimeouts::action_max_timeout()); | 336 SpinRunLoop(TestTimeouts::action_max_timeout()); |
337 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status()); | 337 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status()); |
338 ASSERT_TRUE(channel->HasAcceptedConnection()); | 338 ASSERT_TRUE(channel->HasAcceptedConnection()); |
339 IPC::Message* message = new IPC::Message(0, // routing_id | 339 IPC::Message* message = new IPC::Message(0, // routing_id |
340 kQuitMessage, // message type | 340 kQuitMessage, // message type |
341 IPC::Message::PRIORITY_NORMAL); | 341 IPC::Message::PRIORITY_NORMAL); |
342 channel->Send(message); | 342 channel->Send(message); |
343 SpinRunLoop(TestTimeouts::action_timeout()); | 343 SpinRunLoop(TestTimeouts::action_timeout()); |
344 EXPECT_TRUE(base::KillProcess(handle, 0, false)); | 344 EXPECT_TRUE(base::KillProcess(process.Handle(), 0, false)); |
345 int exit_code = 0; | 345 int exit_code = 0; |
346 EXPECT_TRUE(base::WaitForExitCode(handle2, &exit_code)); | 346 EXPECT_TRUE(process2.WaitForExit(&exit_code)); |
347 EXPECT_EQ(0, exit_code); | 347 EXPECT_EQ(0, exit_code); |
348 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); | 348 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); |
349 ASSERT_FALSE(channel->HasAcceptedConnection()); | 349 ASSERT_FALSE(channel->HasAcceptedConnection()); |
350 } | 350 } |
351 | 351 |
352 TEST_F(IPCChannelPosixTest, BadChannelName) { | 352 TEST_F(IPCChannelPosixTest, BadChannelName) { |
353 // Test empty name | 353 // Test empty name |
354 IPC::ChannelHandle handle(""); | 354 IPC::ChannelHandle handle(""); |
355 scoped_ptr<IPC::ChannelPosix> channel(new IPC::ChannelPosix( | 355 scoped_ptr<IPC::ChannelPosix> channel(new IPC::ChannelPosix( |
356 handle, IPC::Channel::MODE_NAMED_SERVER, NULL)); | 356 handle, IPC::Channel::MODE_NAMED_SERVER, NULL)); |
(...skipping 19 matching lines...) Expand all Loading... |
376 // another external process attempt to connect to us. | 376 // another external process attempt to connect to us. |
377 IPCChannelPosixTestListener listener(false); | 377 IPCChannelPosixTestListener listener(false); |
378 IPC::ChannelHandle chan_handle(GetConnectionSocketName()); | 378 IPC::ChannelHandle chan_handle(GetConnectionSocketName()); |
379 SetUpSocket(&chan_handle, IPC::Channel::MODE_NAMED_SERVER); | 379 SetUpSocket(&chan_handle, IPC::Channel::MODE_NAMED_SERVER); |
380 scoped_ptr<IPC::ChannelPosix> channel(new IPC::ChannelPosix( | 380 scoped_ptr<IPC::ChannelPosix> channel(new IPC::ChannelPosix( |
381 chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener)); | 381 chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener)); |
382 ASSERT_TRUE(channel->Connect()); | 382 ASSERT_TRUE(channel->Connect()); |
383 ASSERT_TRUE(channel->AcceptsConnections()); | 383 ASSERT_TRUE(channel->AcceptsConnections()); |
384 ASSERT_FALSE(channel->HasAcceptedConnection()); | 384 ASSERT_FALSE(channel->HasAcceptedConnection()); |
385 | 385 |
386 base::ProcessHandle handle = SpawnChild("IPCChannelPosixTestConnectionProc"); | 386 base::Process process = SpawnChild("IPCChannelPosixTestConnectionProc"); |
387 ASSERT_TRUE(handle); | 387 ASSERT_TRUE(process.IsValid()); |
388 SpinRunLoop(TestTimeouts::action_max_timeout()); | 388 SpinRunLoop(TestTimeouts::action_max_timeout()); |
389 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status()); | 389 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status()); |
390 ASSERT_TRUE(channel->HasAcceptedConnection()); | 390 ASSERT_TRUE(channel->HasAcceptedConnection()); |
391 base::ProcessHandle handle2 = SpawnChild("IPCChannelPosixFailConnectionProc"); | 391 base::Process process2 = SpawnChild("IPCChannelPosixFailConnectionProc"); |
392 ASSERT_TRUE(handle2); | 392 ASSERT_TRUE(process2.IsValid()); |
393 SpinRunLoop(TestTimeouts::action_max_timeout()); | 393 SpinRunLoop(TestTimeouts::action_max_timeout()); |
394 int exit_code = 0; | 394 int exit_code = 0; |
395 EXPECT_TRUE(base::WaitForExitCode(handle2, &exit_code)); | 395 EXPECT_TRUE(process2.WaitForExit(&exit_code)); |
396 EXPECT_EQ(exit_code, 0); | 396 EXPECT_EQ(exit_code, 0); |
397 ASSERT_EQ(IPCChannelPosixTestListener::DENIED, listener.status()); | 397 ASSERT_EQ(IPCChannelPosixTestListener::DENIED, listener.status()); |
398 ASSERT_TRUE(channel->HasAcceptedConnection()); | 398 ASSERT_TRUE(channel->HasAcceptedConnection()); |
399 IPC::Message* message = new IPC::Message(0, // routing_id | 399 IPC::Message* message = new IPC::Message(0, // routing_id |
400 kQuitMessage, // message type | 400 kQuitMessage, // message type |
401 IPC::Message::PRIORITY_NORMAL); | 401 IPC::Message::PRIORITY_NORMAL); |
402 channel->Send(message); | 402 channel->Send(message); |
403 SpinRunLoop(TestTimeouts::action_timeout()); | 403 SpinRunLoop(TestTimeouts::action_timeout()); |
404 EXPECT_TRUE(base::WaitForExitCode(handle, &exit_code)); | 404 EXPECT_TRUE(process.WaitForExit(&exit_code)); |
405 EXPECT_EQ(exit_code, 0); | 405 EXPECT_EQ(exit_code, 0); |
406 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); | 406 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); |
407 ASSERT_FALSE(channel->HasAcceptedConnection()); | 407 ASSERT_FALSE(channel->HasAcceptedConnection()); |
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()); |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
477 if (connected) { | 477 if (connected) { |
478 IPCChannelPosixTest::SpinRunLoop(TestTimeouts::action_max_timeout()); | 478 IPCChannelPosixTest::SpinRunLoop(TestTimeouts::action_max_timeout()); |
479 EXPECT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); | 479 EXPECT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); |
480 } else { | 480 } else { |
481 EXPECT_EQ(IPCChannelPosixTestListener::DISCONNECTED, listener.status()); | 481 EXPECT_EQ(IPCChannelPosixTestListener::DISCONNECTED, listener.status()); |
482 } | 482 } |
483 return 0; | 483 return 0; |
484 } | 484 } |
485 | 485 |
486 } // namespace | 486 } // namespace |
OLD | NEW |