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
3 // found in the LICENSE file.
5 // These tests are POSIX only.
7 #include "ipc/ipc_channel_posix.h"
11 #include <sys/socket.h>
15 #include "base/files/file_path.h"
16 #include "base/files/file_util.h"
17 #include "base/location.h"
18 #include "base/memory/scoped_ptr.h"
19 #include "base/path_service.h"
20 #include "base/posix/eintr_wrapper.h"
21 #include "base/process/process.h"
22 #include "base/single_thread_task_runner.h"
23 #include "base/test/multiprocess_test.h"
24 #include "base/test/test_timeouts.h"
25 #include "ipc/ipc_listener.h"
26 #include "ipc/unix_domain_socket_util.h"
27 #include "testing/multiprocess_func_list.h"
31 static const uint32_t kQuitMessage
= 47;
33 class IPCChannelPosixTestListener
: public IPC::Listener
{
44 IPCChannelPosixTestListener(bool quit_only_on_message
)
45 : status_(DISCONNECTED
),
46 quit_only_on_message_(quit_only_on_message
) {
49 ~IPCChannelPosixTestListener() override
{}
51 bool OnMessageReceived(const IPC::Message
& message
) override
{
52 EXPECT_EQ(message
.type(), kQuitMessage
);
53 status_
= MESSAGE_RECEIVED
;
58 void OnChannelConnected(int32_t peer_pid
) override
{
60 if (!quit_only_on_message_
) {
65 void OnChannelError() override
{
66 status_
= CHANNEL_ERROR
;
70 void OnChannelDenied() override
{
72 if (!quit_only_on_message_
) {
77 void OnChannelListenError() override
{
78 status_
= LISTEN_ERROR
;
79 if (!quit_only_on_message_
) {
84 STATUS
status() { return status_
; }
87 base::MessageLoopForIO
* loop
= base::MessageLoopForIO::current();
88 if (loop
->is_running()) {
91 // Die as soon as Run is called.
92 loop
->task_runner()->PostTask(FROM_HERE
, loop
->QuitClosure());
97 // The current status of the listener.
99 // If |quit_only_on_message_| then the listener will only break out of
100 // the run loop when kQuitMessage is received.
101 bool quit_only_on_message_
;
104 class IPCChannelPosixTest
: public base::MultiProcessTest
{
106 static void SetUpSocket(IPC::ChannelHandle
*handle
,
107 IPC::Channel::Mode mode
);
108 static void SpinRunLoop(base::TimeDelta delay
);
109 static const std::string
GetConnectionSocketName();
110 static const std::string
GetChannelDirName();
113 void SetUp() override
;
114 void TearDown() override
;
117 scoped_ptr
<base::MessageLoopForIO
> message_loop_
;
120 const std::string
IPCChannelPosixTest::GetChannelDirName() {
121 base::FilePath tmp_dir
;
122 PathService::Get(base::DIR_TEMP
, &tmp_dir
);
123 return tmp_dir
.value();
126 const std::string
IPCChannelPosixTest::GetConnectionSocketName() {
127 return GetChannelDirName() + "/chrome_IPCChannelPosixTest__ConnectionSocket";
130 void IPCChannelPosixTest::SetUp() {
131 MultiProcessTest::SetUp();
132 // Construct a fresh IO Message loop for the duration of each test.
133 message_loop_
.reset(new base::MessageLoopForIO());
136 void IPCChannelPosixTest::TearDown() {
137 message_loop_
.reset(NULL
);
138 MultiProcessTest::TearDown();
141 // Create up a socket and bind and listen to it, or connect it
142 // depending on the |mode|.
143 void IPCChannelPosixTest::SetUpSocket(IPC::ChannelHandle
*handle
,
144 IPC::Channel::Mode mode
) {
145 const std::string
& name
= handle
->name
;
147 int socket_fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
148 ASSERT_GE(socket_fd
, 0) << name
;
149 ASSERT_GE(fcntl(socket_fd
, F_SETFL
, O_NONBLOCK
), 0);
150 struct sockaddr_un server_address
= { 0 };
151 memset(&server_address
, 0, sizeof(server_address
));
152 server_address
.sun_family
= AF_UNIX
;
153 int path_len
= snprintf(server_address
.sun_path
, IPC::kMaxSocketNameLength
,
155 DCHECK_EQ(static_cast<int>(name
.length()), path_len
);
156 size_t server_address_len
= offsetof(struct sockaddr_un
,
157 sun_path
) + path_len
+ 1;
159 if (mode
== IPC::Channel::MODE_NAMED_SERVER
) {
160 // Only one server at a time. Cleanup garbage if it exists.
161 unlink(name
.c_str());
162 // Make sure the path we need exists.
163 base::FilePath
path(name
);
164 base::FilePath dir_path
= path
.DirName();
165 ASSERT_TRUE(base::CreateDirectory(dir_path
));
166 ASSERT_GE(bind(socket_fd
,
167 reinterpret_cast<struct sockaddr
*>(&server_address
),
168 server_address_len
), 0) << server_address
.sun_path
169 << ": " << strerror(errno
)
170 << "(" << errno
<< ")";
171 ASSERT_GE(listen(socket_fd
, SOMAXCONN
), 0) << server_address
.sun_path
172 << ": " << strerror(errno
)
173 << "(" << errno
<< ")";
174 } else if (mode
== IPC::Channel::MODE_NAMED_CLIENT
) {
175 ASSERT_GE(connect(socket_fd
,
176 reinterpret_cast<struct sockaddr
*>(&server_address
),
177 server_address_len
), 0) << server_address
.sun_path
178 << ": " << strerror(errno
)
179 << "(" << errno
<< ")";
181 FAIL() << "Unknown mode " << mode
;
183 handle
->socket
.fd
= socket_fd
;
186 void IPCChannelPosixTest::SpinRunLoop(base::TimeDelta delay
) {
187 base::MessageLoopForIO
* loop
= base::MessageLoopForIO::current();
188 // Post a quit task so that this loop eventually ends and we don't hang
189 // in the case of a bad test. Usually, the run loop will quit sooner than
190 // that because all tests use a IPCChannelPosixTestListener which quits the
191 // current run loop on any channel activity.
192 loop
->task_runner()->PostDelayedTask(FROM_HERE
, loop
->QuitClosure(), delay
);
196 TEST_F(IPCChannelPosixTest
, BasicListen
) {
197 const std::string kChannelName
=
198 GetChannelDirName() + "/IPCChannelPosixTest_BasicListen";
200 // Test creating a socket that is listening.
201 IPC::ChannelHandle
handle(kChannelName
);
202 SetUpSocket(&handle
, IPC::Channel::MODE_NAMED_SERVER
);
203 unlink(handle
.name
.c_str());
204 scoped_ptr
<IPC::ChannelPosix
> channel(new IPC::ChannelPosix(
205 handle
, IPC::Channel::MODE_NAMED_SERVER
, NULL
));
206 ASSERT_TRUE(channel
->Connect());
207 ASSERT_TRUE(channel
->AcceptsConnections());
208 ASSERT_FALSE(channel
->HasAcceptedConnection());
209 channel
->ResetToAcceptingConnectionState();
210 ASSERT_FALSE(channel
->HasAcceptedConnection());
211 unlink(handle
.name
.c_str());
214 TEST_F(IPCChannelPosixTest
, BasicConnected
) {
215 // Test creating a socket that is connected.
217 ASSERT_EQ(0, socketpair(AF_UNIX
, SOCK_STREAM
, 0, pipe_fds
));
218 std::string
socket_name("/var/tmp/IPCChannelPosixTest_BasicConnected");
219 ASSERT_GE(fcntl(pipe_fds
[0], F_SETFL
, O_NONBLOCK
), 0);
221 base::FileDescriptor
fd(pipe_fds
[0], false);
222 IPC::ChannelHandle
handle(socket_name
, fd
);
223 scoped_ptr
<IPC::ChannelPosix
> channel(
224 new IPC::ChannelPosix(handle
, IPC::Channel::MODE_SERVER
, NULL
));
225 ASSERT_TRUE(channel
->Connect());
226 ASSERT_FALSE(channel
->AcceptsConnections());
228 ASSERT_TRUE(IGNORE_EINTR(close(pipe_fds
[1])) == 0);
230 // Make sure that we can use the socket that is created for us by
231 // a standard channel.
232 scoped_ptr
<IPC::ChannelPosix
> channel2(new IPC::ChannelPosix(
233 socket_name
, IPC::Channel::MODE_SERVER
, NULL
));
234 ASSERT_TRUE(channel2
->Connect());
235 ASSERT_FALSE(channel2
->AcceptsConnections());
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
240 // sync_message_filter and others. Make sure the listener is notified.
241 TEST_F(IPCChannelPosixTest
, SendHangTest
) {
242 IPCChannelPosixTestListener
out_listener(true);
243 IPCChannelPosixTestListener
in_listener(true);
244 IPC::ChannelHandle
in_handle("IN");
245 scoped_ptr
<IPC::ChannelPosix
> in_chan(new IPC::ChannelPosix(
246 in_handle
, IPC::Channel::MODE_SERVER
, &in_listener
));
247 IPC::ChannelHandle
out_handle(
248 "OUT", base::FileDescriptor(in_chan
->TakeClientFileDescriptor()));
249 scoped_ptr
<IPC::ChannelPosix
> out_chan(new IPC::ChannelPosix(
250 out_handle
, IPC::Channel::MODE_CLIENT
, &out_listener
));
251 ASSERT_TRUE(in_chan
->Connect());
252 ASSERT_TRUE(out_chan
->Connect());
253 in_chan
->Close(); // simulate remote process dying at an unfortunate time.
254 // Send will fail, because it cannot write the message.
255 ASSERT_FALSE(out_chan
->Send(new IPC::Message(
257 kQuitMessage
, // message type
258 IPC::Message::PRIORITY_NORMAL
)));
259 SpinRunLoop(TestTimeouts::action_max_timeout());
260 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR
, out_listener
.status());
263 // If a connection closes right before a Connect() call, we may end up closing
264 // the connection without notifying the listener, which can cause hangs in
265 // sync_message_filter and others. Make sure the listener is notified.
266 TEST_F(IPCChannelPosixTest
, AcceptHangTest
) {
267 IPCChannelPosixTestListener
out_listener(true);
268 IPCChannelPosixTestListener
in_listener(true);
269 IPC::ChannelHandle
in_handle("IN");
270 scoped_ptr
<IPC::ChannelPosix
> in_chan(new IPC::ChannelPosix(
271 in_handle
, IPC::Channel::MODE_SERVER
, &in_listener
));
272 IPC::ChannelHandle
out_handle(
273 "OUT", base::FileDescriptor(in_chan
->TakeClientFileDescriptor()));
274 scoped_ptr
<IPC::ChannelPosix
> out_chan(new IPC::ChannelPosix(
275 out_handle
, IPC::Channel::MODE_CLIENT
, &out_listener
));
276 ASSERT_TRUE(in_chan
->Connect());
277 in_chan
->Close(); // simulate remote process dying at an unfortunate time.
278 ASSERT_FALSE(out_chan
->Connect());
279 SpinRunLoop(TestTimeouts::action_max_timeout());
280 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR
, out_listener
.status());
283 #if defined(OS_ANDROID)
284 #define MAYBE_AdvancedConnected DISABLED_AdvancedConnected
286 #define MAYBE_AdvancedConnected AdvancedConnected
288 TEST_F(IPCChannelPosixTest
, MAYBE_AdvancedConnected
) {
289 // Test creating a connection to an external process.
290 IPCChannelPosixTestListener
listener(false);
291 IPC::ChannelHandle
chan_handle(GetConnectionSocketName());
292 SetUpSocket(&chan_handle
, IPC::Channel::MODE_NAMED_SERVER
);
293 scoped_ptr
<IPC::ChannelPosix
> channel(new IPC::ChannelPosix(
294 chan_handle
, IPC::Channel::MODE_NAMED_SERVER
, &listener
));
295 ASSERT_TRUE(channel
->Connect());
296 ASSERT_TRUE(channel
->AcceptsConnections());
297 ASSERT_FALSE(channel
->HasAcceptedConnection());
299 base::Process process
= SpawnChild("IPCChannelPosixTestConnectionProc");
300 ASSERT_TRUE(process
.IsValid());
301 SpinRunLoop(TestTimeouts::action_max_timeout());
302 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED
, listener
.status());
303 ASSERT_TRUE(channel
->HasAcceptedConnection());
304 IPC::Message
* message
= new IPC::Message(0, // routing_id
305 kQuitMessage
, // message type
306 IPC::Message::PRIORITY_NORMAL
);
307 channel
->Send(message
);
308 SpinRunLoop(TestTimeouts::action_timeout());
310 EXPECT_TRUE(process
.WaitForExit(&exit_code
));
311 EXPECT_EQ(0, exit_code
);
312 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR
, listener
.status());
313 ASSERT_FALSE(channel
->HasAcceptedConnection());
314 unlink(chan_handle
.name
.c_str());
317 #if defined(OS_ANDROID)
318 #define MAYBE_ResetState DISABLED_ResetState
320 #define MAYBE_ResetState ResetState
322 TEST_F(IPCChannelPosixTest
, MAYBE_ResetState
) {
323 // Test creating a connection to an external process. Close the connection,
324 // but continue to listen and make sure another external process can connect
326 IPCChannelPosixTestListener
listener(false);
327 IPC::ChannelHandle
chan_handle(GetConnectionSocketName());
328 SetUpSocket(&chan_handle
, IPC::Channel::MODE_NAMED_SERVER
);
329 scoped_ptr
<IPC::ChannelPosix
> channel(new IPC::ChannelPosix(
330 chan_handle
, IPC::Channel::MODE_NAMED_SERVER
, &listener
));
331 ASSERT_TRUE(channel
->Connect());
332 ASSERT_TRUE(channel
->AcceptsConnections());
333 ASSERT_FALSE(channel
->HasAcceptedConnection());
335 base::Process process
= SpawnChild("IPCChannelPosixTestConnectionProc");
336 ASSERT_TRUE(process
.IsValid());
337 SpinRunLoop(TestTimeouts::action_max_timeout());
338 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED
, listener
.status());
339 ASSERT_TRUE(channel
->HasAcceptedConnection());
340 channel
->ResetToAcceptingConnectionState();
341 ASSERT_FALSE(channel
->HasAcceptedConnection());
343 base::Process process2
= SpawnChild("IPCChannelPosixTestConnectionProc");
344 ASSERT_TRUE(process2
.IsValid());
345 SpinRunLoop(TestTimeouts::action_max_timeout());
346 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED
, listener
.status());
347 ASSERT_TRUE(channel
->HasAcceptedConnection());
348 IPC::Message
* message
= new IPC::Message(0, // routing_id
349 kQuitMessage
, // message type
350 IPC::Message::PRIORITY_NORMAL
);
351 channel
->Send(message
);
352 SpinRunLoop(TestTimeouts::action_timeout());
353 EXPECT_TRUE(process
.Terminate(0, false));
355 EXPECT_TRUE(process2
.WaitForExit(&exit_code
));
356 EXPECT_EQ(0, exit_code
);
357 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR
, listener
.status());
358 ASSERT_FALSE(channel
->HasAcceptedConnection());
359 unlink(chan_handle
.name
.c_str());
362 TEST_F(IPCChannelPosixTest
, BadChannelName
) {
364 IPC::ChannelHandle
handle("");
365 scoped_ptr
<IPC::ChannelPosix
> channel(new IPC::ChannelPosix(
366 handle
, IPC::Channel::MODE_NAMED_SERVER
, NULL
));
367 ASSERT_FALSE(channel
->Connect());
369 // Test name that is too long.
370 const char *kTooLongName
= "This_is_a_very_long_name_to_proactively_implement"
371 "client-centered_synergy_through_top-line"
372 "platforms_Phosfluorescently_disintermediate_"
373 "clicks-and-mortar_best_practices_without_"
374 "future-proof_growth_strategies_Continually"
375 "pontificate_proactive_potentialities_before"
376 "leading-edge_processes";
377 EXPECT_GE(strlen(kTooLongName
), IPC::kMaxSocketNameLength
);
378 IPC::ChannelHandle
handle2(kTooLongName
);
379 scoped_ptr
<IPC::ChannelPosix
> channel2(new IPC::ChannelPosix(
380 handle2
, IPC::Channel::MODE_NAMED_SERVER
, NULL
));
381 EXPECT_FALSE(channel2
->Connect());
384 #if defined(OS_ANDROID)
385 #define MAYBE_MultiConnection DISABLED_MultiConnection
387 #define MAYBE_MultiConnection MultiConnection
389 TEST_F(IPCChannelPosixTest
, MAYBE_MultiConnection
) {
390 // Test setting up a connection to an external process, and then have
391 // another external process attempt to connect to us.
392 IPCChannelPosixTestListener
listener(false);
393 IPC::ChannelHandle
chan_handle(GetConnectionSocketName());
394 SetUpSocket(&chan_handle
, IPC::Channel::MODE_NAMED_SERVER
);
395 scoped_ptr
<IPC::ChannelPosix
> channel(new IPC::ChannelPosix(
396 chan_handle
, IPC::Channel::MODE_NAMED_SERVER
, &listener
));
397 ASSERT_TRUE(channel
->Connect());
398 ASSERT_TRUE(channel
->AcceptsConnections());
399 ASSERT_FALSE(channel
->HasAcceptedConnection());
401 base::Process process
= SpawnChild("IPCChannelPosixTestConnectionProc");
402 ASSERT_TRUE(process
.IsValid());
403 SpinRunLoop(TestTimeouts::action_max_timeout());
404 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED
, listener
.status());
405 ASSERT_TRUE(channel
->HasAcceptedConnection());
406 base::Process process2
= SpawnChild("IPCChannelPosixFailConnectionProc");
407 ASSERT_TRUE(process2
.IsValid());
408 SpinRunLoop(TestTimeouts::action_max_timeout());
410 EXPECT_TRUE(process2
.WaitForExit(&exit_code
));
411 EXPECT_EQ(exit_code
, 0);
412 ASSERT_EQ(IPCChannelPosixTestListener::DENIED
, listener
.status());
413 ASSERT_TRUE(channel
->HasAcceptedConnection());
414 IPC::Message
* message
= new IPC::Message(0, // routing_id
415 kQuitMessage
, // message type
416 IPC::Message::PRIORITY_NORMAL
);
417 channel
->Send(message
);
418 SpinRunLoop(TestTimeouts::action_timeout());
419 EXPECT_TRUE(process
.WaitForExit(&exit_code
));
420 EXPECT_EQ(exit_code
, 0);
421 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR
, listener
.status());
422 ASSERT_FALSE(channel
->HasAcceptedConnection());
423 unlink(chan_handle
.name
.c_str());
426 TEST_F(IPCChannelPosixTest
, DoubleServer
) {
427 // Test setting up two servers with the same name.
428 IPCChannelPosixTestListener
listener(false);
429 IPCChannelPosixTestListener
listener2(false);
430 IPC::ChannelHandle
chan_handle(GetConnectionSocketName());
431 scoped_ptr
<IPC::ChannelPosix
> channel(new IPC::ChannelPosix(
432 chan_handle
, IPC::Channel::MODE_SERVER
, &listener
));
433 scoped_ptr
<IPC::ChannelPosix
> channel2(new IPC::ChannelPosix(
434 chan_handle
, IPC::Channel::MODE_SERVER
, &listener2
));
435 ASSERT_TRUE(channel
->Connect());
436 ASSERT_FALSE(channel2
->Connect());
439 TEST_F(IPCChannelPosixTest
, BadMode
) {
440 // Test setting up two servers with a bad mode.
441 IPCChannelPosixTestListener
listener(false);
442 IPC::ChannelHandle
chan_handle(GetConnectionSocketName());
443 scoped_ptr
<IPC::ChannelPosix
> channel(new IPC::ChannelPosix(
444 chan_handle
, IPC::Channel::MODE_NONE
, &listener
));
445 ASSERT_FALSE(channel
->Connect());
448 TEST_F(IPCChannelPosixTest
, IsNamedServerInitialized
) {
449 const std::string
& connection_socket_name
= GetConnectionSocketName();
450 IPCChannelPosixTestListener
listener(false);
451 IPC::ChannelHandle
chan_handle(connection_socket_name
);
452 ASSERT_TRUE(base::DeleteFile(base::FilePath(connection_socket_name
), false));
453 ASSERT_FALSE(IPC::Channel::IsNamedServerInitialized(
454 connection_socket_name
));
455 scoped_ptr
<IPC::ChannelPosix
> channel(new IPC::ChannelPosix(
456 chan_handle
, IPC::Channel::MODE_NAMED_SERVER
, &listener
));
457 ASSERT_TRUE(IPC::Channel::IsNamedServerInitialized(
458 connection_socket_name
));
460 ASSERT_FALSE(IPC::Channel::IsNamedServerInitialized(
461 connection_socket_name
));
462 unlink(chan_handle
.name
.c_str());
465 // A long running process that connects to us
466 MULTIPROCESS_TEST_MAIN(IPCChannelPosixTestConnectionProc
) {
467 base::MessageLoopForIO message_loop
;
468 IPCChannelPosixTestListener
listener(true);
469 IPC::ChannelHandle
handle(IPCChannelPosixTest::GetConnectionSocketName());
470 IPCChannelPosixTest::SetUpSocket(&handle
, IPC::Channel::MODE_NAMED_CLIENT
);
471 scoped_ptr
<IPC::ChannelPosix
> channel(new IPC::ChannelPosix(
472 handle
, IPC::Channel::MODE_NAMED_CLIENT
, &listener
));
473 EXPECT_TRUE(channel
->Connect());
474 IPCChannelPosixTest::SpinRunLoop(TestTimeouts::action_max_timeout());
475 EXPECT_EQ(IPCChannelPosixTestListener::MESSAGE_RECEIVED
, listener
.status());
479 // Simple external process that shouldn't be able to connect to us.
480 MULTIPROCESS_TEST_MAIN(IPCChannelPosixFailConnectionProc
) {
481 base::MessageLoopForIO message_loop
;
482 IPCChannelPosixTestListener
listener(false);
483 IPC::ChannelHandle
handle(IPCChannelPosixTest::GetConnectionSocketName());
484 IPCChannelPosixTest::SetUpSocket(&handle
, IPC::Channel::MODE_NAMED_CLIENT
);
485 scoped_ptr
<IPC::ChannelPosix
> channel(new IPC::ChannelPosix(
486 handle
, IPC::Channel::MODE_NAMED_CLIENT
, &listener
));
488 // In this case connect may succeed or fail depending on if the packet
489 // actually gets sent at sendmsg. Since we never delay on send, we may not
490 // see the error. However even if connect succeeds, eventually we will get an
491 // error back since the channel will be closed when we attempt to read from
493 bool connected
= channel
->Connect();
495 IPCChannelPosixTest::SpinRunLoop(TestTimeouts::action_max_timeout());
496 EXPECT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR
, listener
.status());
498 EXPECT_EQ(IPCChannelPosixTestListener::DISCONNECTED
, listener
.status());