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 <stdint.h> | 10 #include <stdint.h> |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 } | 194 } |
195 | 195 |
196 TEST_F(IPCChannelPosixTest, BasicListen) { | 196 TEST_F(IPCChannelPosixTest, BasicListen) { |
197 const std::string kChannelName = | 197 const std::string kChannelName = |
198 GetChannelDirName() + "/IPCChannelPosixTest_BasicListen"; | 198 GetChannelDirName() + "/IPCChannelPosixTest_BasicListen"; |
199 | 199 |
200 // Test creating a socket that is listening. | 200 // Test creating a socket that is listening. |
201 IPC::ChannelHandle handle(kChannelName); | 201 IPC::ChannelHandle handle(kChannelName); |
202 SetUpSocket(&handle, IPC::Channel::MODE_NAMED_SERVER); | 202 SetUpSocket(&handle, IPC::Channel::MODE_NAMED_SERVER); |
203 unlink(handle.name.c_str()); | 203 unlink(handle.name.c_str()); |
204 scoped_ptr<IPC::ChannelPosix> channel(new IPC::ChannelPosix( | 204 scoped_ptr<IPC::ChannelPosix> channel( |
205 handle, IPC::Channel::MODE_NAMED_SERVER, NULL)); | 205 new IPC::ChannelPosix(handle, IPC::Channel::MODE_NAMED_SERVER, NULL)); |
206 ASSERT_TRUE(channel->Connect()); | 206 ASSERT_TRUE(channel->Connect()); |
207 ASSERT_TRUE(channel->AcceptsConnections()); | 207 ASSERT_TRUE(channel->AcceptsConnections()); |
208 ASSERT_FALSE(channel->HasAcceptedConnection()); | 208 ASSERT_FALSE(channel->HasAcceptedConnection()); |
209 channel->ResetToAcceptingConnectionState(); | 209 channel->ResetToAcceptingConnectionState(); |
210 ASSERT_FALSE(channel->HasAcceptedConnection()); | 210 ASSERT_FALSE(channel->HasAcceptedConnection()); |
211 unlink(handle.name.c_str()); | 211 unlink(handle.name.c_str()); |
212 } | 212 } |
213 | 213 |
214 TEST_F(IPCChannelPosixTest, BasicConnected) { | 214 TEST_F(IPCChannelPosixTest, BasicConnected) { |
215 // Test creating a socket that is connected. | 215 // Test creating a socket that is connected. |
216 int pipe_fds[2]; | 216 int pipe_fds[2]; |
217 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, pipe_fds)); | 217 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, pipe_fds)); |
218 std::string socket_name("/var/tmp/IPCChannelPosixTest_BasicConnected"); | 218 std::string socket_name("/var/tmp/IPCChannelPosixTest_BasicConnected"); |
219 ASSERT_GE(fcntl(pipe_fds[0], F_SETFL, O_NONBLOCK), 0); | 219 ASSERT_GE(fcntl(pipe_fds[0], F_SETFL, O_NONBLOCK), 0); |
220 | 220 |
221 base::FileDescriptor fd(pipe_fds[0], false); | 221 base::FileDescriptor fd(pipe_fds[0], false); |
222 IPC::ChannelHandle handle(socket_name, fd); | 222 IPC::ChannelHandle handle(socket_name, fd); |
223 scoped_ptr<IPC::ChannelPosix> channel( | 223 scoped_ptr<IPC::ChannelPosix> channel( |
224 new IPC::ChannelPosix(handle, IPC::Channel::MODE_SERVER, NULL)); | 224 new IPC::ChannelPosix(handle, IPC::Channel::MODE_SERVER, NULL)); |
225 ASSERT_TRUE(channel->Connect()); | 225 ASSERT_TRUE(channel->Connect()); |
226 ASSERT_FALSE(channel->AcceptsConnections()); | 226 ASSERT_FALSE(channel->AcceptsConnections()); |
227 channel->Close(); | 227 channel->Close(); |
228 ASSERT_TRUE(IGNORE_EINTR(close(pipe_fds[1])) == 0); | 228 ASSERT_TRUE(IGNORE_EINTR(close(pipe_fds[1])) == 0); |
229 | 229 |
230 // Make sure that we can use the socket that is created for us by | 230 // Make sure that we can use the socket that is created for us by |
231 // a standard channel. | 231 // a standard channel. |
232 scoped_ptr<IPC::ChannelPosix> channel2(new IPC::ChannelPosix( | 232 scoped_ptr<IPC::ChannelPosix> channel2( |
233 socket_name, IPC::Channel::MODE_SERVER, NULL)); | 233 new IPC::ChannelPosix(socket_name, IPC::Channel::MODE_SERVER, NULL)); |
234 ASSERT_TRUE(channel2->Connect()); | 234 ASSERT_TRUE(channel2->Connect()); |
235 ASSERT_FALSE(channel2->AcceptsConnections()); | 235 ASSERT_FALSE(channel2->AcceptsConnections()); |
236 } | 236 } |
237 | 237 |
238 // If a connection closes right before a Send() call, we may end up closing | 238 // If a connection closes right before a Send() call, we may end up closing |
239 // the connection without notifying the listener, which can cause hangs in | 239 // the connection without notifying the listener, which can cause hangs in |
240 // sync_message_filter and others. Make sure the listener is notified. | 240 // sync_message_filter and others. Make sure the listener is notified. |
241 TEST_F(IPCChannelPosixTest, SendHangTest) { | 241 TEST_F(IPCChannelPosixTest, SendHangTest) { |
242 IPCChannelPosixTestListener out_listener(true); | 242 IPCChannelPosixTestListener out_listener(true); |
243 IPCChannelPosixTestListener in_listener(true); | 243 IPCChannelPosixTestListener in_listener(true); |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
355 EXPECT_TRUE(process2.WaitForExit(&exit_code)); | 355 EXPECT_TRUE(process2.WaitForExit(&exit_code)); |
356 EXPECT_EQ(0, exit_code); | 356 EXPECT_EQ(0, exit_code); |
357 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); | 357 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); |
358 ASSERT_FALSE(channel->HasAcceptedConnection()); | 358 ASSERT_FALSE(channel->HasAcceptedConnection()); |
359 unlink(chan_handle.name.c_str()); | 359 unlink(chan_handle.name.c_str()); |
360 } | 360 } |
361 | 361 |
362 TEST_F(IPCChannelPosixTest, BadChannelName) { | 362 TEST_F(IPCChannelPosixTest, BadChannelName) { |
363 // Test empty name | 363 // Test empty name |
364 IPC::ChannelHandle handle(""); | 364 IPC::ChannelHandle handle(""); |
365 scoped_ptr<IPC::ChannelPosix> channel(new IPC::ChannelPosix( | 365 scoped_ptr<IPC::ChannelPosix> channel( |
366 handle, IPC::Channel::MODE_NAMED_SERVER, NULL)); | 366 new IPC::ChannelPosix(handle, IPC::Channel::MODE_NAMED_SERVER, NULL)); |
367 ASSERT_FALSE(channel->Connect()); | 367 ASSERT_FALSE(channel->Connect()); |
368 | 368 |
369 // Test name that is too long. | 369 // Test name that is too long. |
370 const char *kTooLongName = "This_is_a_very_long_name_to_proactively_implement" | 370 const char *kTooLongName = "This_is_a_very_long_name_to_proactively_implement" |
371 "client-centered_synergy_through_top-line" | 371 "client-centered_synergy_through_top-line" |
372 "platforms_Phosfluorescently_disintermediate_" | 372 "platforms_Phosfluorescently_disintermediate_" |
373 "clicks-and-mortar_best_practices_without_" | 373 "clicks-and-mortar_best_practices_without_" |
374 "future-proof_growth_strategies_Continually" | 374 "future-proof_growth_strategies_Continually" |
375 "pontificate_proactive_potentialities_before" | 375 "pontificate_proactive_potentialities_before" |
376 "leading-edge_processes"; | 376 "leading-edge_processes"; |
377 EXPECT_GE(strlen(kTooLongName), IPC::kMaxSocketNameLength); | 377 EXPECT_GE(strlen(kTooLongName), IPC::kMaxSocketNameLength); |
378 IPC::ChannelHandle handle2(kTooLongName); | 378 IPC::ChannelHandle handle2(kTooLongName); |
379 scoped_ptr<IPC::ChannelPosix> channel2(new IPC::ChannelPosix( | 379 scoped_ptr<IPC::ChannelPosix> channel2( |
380 handle2, IPC::Channel::MODE_NAMED_SERVER, NULL)); | 380 new IPC::ChannelPosix(handle2, IPC::Channel::MODE_NAMED_SERVER, NULL)); |
381 EXPECT_FALSE(channel2->Connect()); | 381 EXPECT_FALSE(channel2->Connect()); |
382 } | 382 } |
383 | 383 |
384 #if defined(OS_ANDROID) | 384 #if defined(OS_ANDROID) |
385 #define MAYBE_MultiConnection DISABLED_MultiConnection | 385 #define MAYBE_MultiConnection DISABLED_MultiConnection |
386 #else | 386 #else |
387 #define MAYBE_MultiConnection MultiConnection | 387 #define MAYBE_MultiConnection MultiConnection |
388 #endif | 388 #endif |
389 TEST_F(IPCChannelPosixTest, MAYBE_MultiConnection) { | 389 TEST_F(IPCChannelPosixTest, MAYBE_MultiConnection) { |
390 // Test setting up a connection to an external process, and then have | 390 // Test setting up a connection to an external process, and then have |
(...skipping 30 matching lines...) Expand all Loading... |
421 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); | 421 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); |
422 ASSERT_FALSE(channel->HasAcceptedConnection()); | 422 ASSERT_FALSE(channel->HasAcceptedConnection()); |
423 unlink(chan_handle.name.c_str()); | 423 unlink(chan_handle.name.c_str()); |
424 } | 424 } |
425 | 425 |
426 TEST_F(IPCChannelPosixTest, DoubleServer) { | 426 TEST_F(IPCChannelPosixTest, DoubleServer) { |
427 // Test setting up two servers with the same name. | 427 // Test setting up two servers with the same name. |
428 IPCChannelPosixTestListener listener(false); | 428 IPCChannelPosixTestListener listener(false); |
429 IPCChannelPosixTestListener listener2(false); | 429 IPCChannelPosixTestListener listener2(false); |
430 IPC::ChannelHandle chan_handle(GetConnectionSocketName()); | 430 IPC::ChannelHandle chan_handle(GetConnectionSocketName()); |
431 scoped_ptr<IPC::ChannelPosix> channel(new IPC::ChannelPosix( | 431 scoped_ptr<IPC::ChannelPosix> channel( |
432 chan_handle, IPC::Channel::MODE_SERVER, &listener)); | 432 new IPC::ChannelPosix(chan_handle, IPC::Channel::MODE_SERVER, &listener)); |
433 scoped_ptr<IPC::ChannelPosix> channel2(new IPC::ChannelPosix( | 433 scoped_ptr<IPC::ChannelPosix> channel2(new IPC::ChannelPosix( |
434 chan_handle, IPC::Channel::MODE_SERVER, &listener2)); | 434 chan_handle, IPC::Channel::MODE_SERVER, &listener2)); |
435 ASSERT_TRUE(channel->Connect()); | 435 ASSERT_TRUE(channel->Connect()); |
436 ASSERT_FALSE(channel2->Connect()); | 436 ASSERT_FALSE(channel2->Connect()); |
437 } | 437 } |
438 | 438 |
439 TEST_F(IPCChannelPosixTest, BadMode) { | 439 TEST_F(IPCChannelPosixTest, BadMode) { |
440 // Test setting up two servers with a bad mode. | 440 // Test setting up two servers with a bad mode. |
441 IPCChannelPosixTestListener listener(false); | 441 IPCChannelPosixTestListener listener(false); |
442 IPC::ChannelHandle chan_handle(GetConnectionSocketName()); | 442 IPC::ChannelHandle chan_handle(GetConnectionSocketName()); |
443 scoped_ptr<IPC::ChannelPosix> channel(new IPC::ChannelPosix( | 443 scoped_ptr<IPC::ChannelPosix> channel( |
444 chan_handle, IPC::Channel::MODE_NONE, &listener)); | 444 new IPC::ChannelPosix(chan_handle, IPC::Channel::MODE_NONE, &listener)); |
445 ASSERT_FALSE(channel->Connect()); | 445 ASSERT_FALSE(channel->Connect()); |
446 } | 446 } |
447 | 447 |
448 TEST_F(IPCChannelPosixTest, IsNamedServerInitialized) { | 448 TEST_F(IPCChannelPosixTest, IsNamedServerInitialized) { |
449 const std::string& connection_socket_name = GetConnectionSocketName(); | 449 const std::string& connection_socket_name = GetConnectionSocketName(); |
450 IPCChannelPosixTestListener listener(false); | 450 IPCChannelPosixTestListener listener(false); |
451 IPC::ChannelHandle chan_handle(connection_socket_name); | 451 IPC::ChannelHandle chan_handle(connection_socket_name); |
452 ASSERT_TRUE(base::DeleteFile(base::FilePath(connection_socket_name), false)); | 452 ASSERT_TRUE(base::DeleteFile(base::FilePath(connection_socket_name), false)); |
453 ASSERT_FALSE(IPC::Channel::IsNamedServerInitialized( | 453 ASSERT_FALSE(IPC::Channel::IsNamedServerInitialized( |
454 connection_socket_name)); | 454 connection_socket_name)); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
494 if (connected) { | 494 if (connected) { |
495 IPCChannelPosixTest::SpinRunLoop(TestTimeouts::action_max_timeout()); | 495 IPCChannelPosixTest::SpinRunLoop(TestTimeouts::action_max_timeout()); |
496 EXPECT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); | 496 EXPECT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); |
497 } else { | 497 } else { |
498 EXPECT_EQ(IPCChannelPosixTestListener::DISCONNECTED, listener.status()); | 498 EXPECT_EQ(IPCChannelPosixTestListener::DISCONNECTED, listener.status()); |
499 } | 499 } |
500 return 0; | 500 return 0; |
501 } | 501 } |
502 | 502 |
503 } // namespace | 503 } // namespace |
OLD | NEW |