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"
10 #include <sys/socket.h>
14 #include "base/basictypes.h"
15 #include "base/file_util.h"
16 #include "base/files/file_path.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/message_loop.h"
19 #include "base/path_service.h"
20 #include "base/posix/eintr_wrapper.h"
21 #include "base/test/multiprocess_test.h"
22 #include "base/test/test_timeouts.h"
23 #include "ipc/ipc_listener.h"
24 #include "testing/multiprocess_func_list.h"
28 static const uint32 kQuitMessage
= 47;
30 class IPCChannelPosixTestListener
: public IPC::Listener
{
41 IPCChannelPosixTestListener(bool quit_only_on_message
)
42 : status_(DISCONNECTED
), quit_only_on_message_(quit_only_on_message
) {}
44 virtual ~IPCChannelPosixTestListener() {}
46 virtual bool OnMessageReceived(const IPC::Message
& message
) OVERRIDE
{
47 EXPECT_EQ(message
.type(), kQuitMessage
);
48 status_
= MESSAGE_RECEIVED
;
53 virtual void OnChannelConnected(int32 peer_pid
) OVERRIDE
{
55 if (!quit_only_on_message_
) {
60 virtual void OnChannelError() OVERRIDE
{
61 status_
= CHANNEL_ERROR
;
62 if (!quit_only_on_message_
) {
67 virtual void OnChannelDenied() OVERRIDE
{
69 if (!quit_only_on_message_
) {
74 virtual void OnChannelListenError() OVERRIDE
{
75 status_
= LISTEN_ERROR
;
76 if (!quit_only_on_message_
) {
81 STATUS
status() { return status_
; }
84 MessageLoopForIO::current()->QuitNow();
88 // The current status of the listener.
90 // If |quit_only_on_message_| then the listener will only break out of
91 // the run loop when kQuitMessage is received.
92 bool quit_only_on_message_
;
95 class IPCChannelPosixTest
: public base::MultiProcessTest
{
97 static void SetUpSocket(IPC::ChannelHandle
*handle
,
98 IPC::Channel::Mode mode
);
99 static void SpinRunLoop(base::TimeDelta delay
);
100 static const std::string
GetConnectionSocketName();
101 static const std::string
GetChannelDirName();
104 virtual void SetUp();
105 virtual void TearDown();
108 scoped_ptr
<MessageLoopForIO
> message_loop_
;
111 const std::string
IPCChannelPosixTest::GetChannelDirName() {
112 #if defined(OS_ANDROID)
113 base::FilePath tmp_dir
;
114 PathService::Get(base::DIR_CACHE
, &tmp_dir
);
115 return tmp_dir
.value();
121 const std::string
IPCChannelPosixTest::GetConnectionSocketName() {
122 return GetChannelDirName() + "/chrome_IPCChannelPosixTest__ConnectionSocket";
125 void IPCChannelPosixTest::SetUp() {
126 MultiProcessTest::SetUp();
127 // Construct a fresh IO Message loop for the duration of each test.
128 message_loop_
.reset(new MessageLoopForIO());
131 void IPCChannelPosixTest::TearDown() {
132 message_loop_
.reset(NULL
);
133 MultiProcessTest::TearDown();
136 // Create up a socket and bind and listen to it, or connect it
137 // depending on the |mode|.
138 void IPCChannelPosixTest::SetUpSocket(IPC::ChannelHandle
*handle
,
139 IPC::Channel::Mode mode
) {
140 const std::string
& name
= handle
->name
;
142 int socket_fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
143 ASSERT_GE(socket_fd
, 0) << name
;
144 ASSERT_GE(fcntl(socket_fd
, F_SETFL
, O_NONBLOCK
), 0);
145 struct sockaddr_un server_address
= { 0 };
146 memset(&server_address
, 0, sizeof(server_address
));
147 server_address
.sun_family
= AF_UNIX
;
148 int path_len
= snprintf(server_address
.sun_path
, IPC::kMaxPipeNameLength
,
150 DCHECK_EQ(static_cast<int>(name
.length()), path_len
);
151 size_t server_address_len
= offsetof(struct sockaddr_un
,
152 sun_path
) + path_len
+ 1;
154 if (mode
== IPC::Channel::MODE_NAMED_SERVER
) {
155 // Only one server at a time. Cleanup garbage if it exists.
156 unlink(name
.c_str());
157 // Make sure the path we need exists.
158 base::FilePath
path(name
);
159 base::FilePath dir_path
= path
.DirName();
160 ASSERT_TRUE(file_util::CreateDirectory(dir_path
));
161 ASSERT_GE(bind(socket_fd
,
162 reinterpret_cast<struct sockaddr
*>(&server_address
),
163 server_address_len
), 0) << server_address
.sun_path
164 << ": " << strerror(errno
)
165 << "(" << errno
<< ")";
166 ASSERT_GE(listen(socket_fd
, SOMAXCONN
), 0) << server_address
.sun_path
167 << ": " << strerror(errno
)
168 << "(" << errno
<< ")";
169 } else if (mode
== IPC::Channel::MODE_NAMED_CLIENT
) {
170 ASSERT_GE(connect(socket_fd
,
171 reinterpret_cast<struct sockaddr
*>(&server_address
),
172 server_address_len
), 0) << server_address
.sun_path
173 << ": " << strerror(errno
)
174 << "(" << errno
<< ")";
176 FAIL() << "Unknown mode " << mode
;
178 handle
->socket
.fd
= socket_fd
;
181 void IPCChannelPosixTest::SpinRunLoop(base::TimeDelta delay
) {
182 MessageLoopForIO
*loop
= MessageLoopForIO::current();
183 // Post a quit task so that this loop eventually ends and we don't hang
184 // in the case of a bad test. Usually, the run loop will quit sooner than
185 // that because all tests use a IPCChannelPosixTestListener which quits the
186 // current run loop on any channel activity.
187 loop
->PostDelayedTask(
189 MessageLoop::QuitClosure(),
194 TEST_F(IPCChannelPosixTest
, BasicListen
) {
195 const std::string kChannelName
=
196 GetChannelDirName() + "/IPCChannelPosixTest_BasicListen";
198 // Test creating a socket that is listening.
199 IPC::ChannelHandle
handle(kChannelName
);
200 SetUpSocket(&handle
, IPC::Channel::MODE_NAMED_SERVER
);
201 unlink(handle
.name
.c_str());
202 IPC::Channel
channel(handle
, IPC::Channel::MODE_NAMED_SERVER
, NULL
);
203 ASSERT_TRUE(channel
.Connect());
204 ASSERT_TRUE(channel
.AcceptsConnections());
205 ASSERT_FALSE(channel
.HasAcceptedConnection());
206 channel
.ResetToAcceptingConnectionState();
207 ASSERT_FALSE(channel
.HasAcceptedConnection());
210 TEST_F(IPCChannelPosixTest
, BasicConnected
) {
211 // Test creating a socket that is connected.
213 ASSERT_EQ(0, socketpair(AF_UNIX
, SOCK_STREAM
, 0, pipe_fds
));
214 std::string
socket_name("/var/tmp/IPCChannelPosixTest_BasicConnected");
215 ASSERT_GE(fcntl(pipe_fds
[0], F_SETFL
, O_NONBLOCK
), 0);
217 base::FileDescriptor
fd(pipe_fds
[0], false);
218 IPC::ChannelHandle
handle(socket_name
, fd
);
219 IPC::Channel
channel(handle
, IPC::Channel::MODE_SERVER
, NULL
);
220 ASSERT_TRUE(channel
.Connect());
221 ASSERT_FALSE(channel
.AcceptsConnections());
223 ASSERT_TRUE(HANDLE_EINTR(close(pipe_fds
[1])) == 0);
225 // Make sure that we can use the socket that is created for us by
226 // a standard channel.
227 IPC::Channel
channel2(socket_name
, IPC::Channel::MODE_SERVER
, NULL
);
228 ASSERT_TRUE(channel2
.Connect());
229 ASSERT_FALSE(channel2
.AcceptsConnections());
232 TEST_F(IPCChannelPosixTest
, AdvancedConnected
) {
233 // Test creating a connection to an external process.
234 IPCChannelPosixTestListener
listener(false);
235 IPC::ChannelHandle
chan_handle(GetConnectionSocketName());
236 SetUpSocket(&chan_handle
, IPC::Channel::MODE_NAMED_SERVER
);
237 IPC::Channel
channel(chan_handle
, IPC::Channel::MODE_NAMED_SERVER
, &listener
);
238 ASSERT_TRUE(channel
.Connect());
239 ASSERT_TRUE(channel
.AcceptsConnections());
240 ASSERT_FALSE(channel
.HasAcceptedConnection());
242 base::ProcessHandle handle
= SpawnChild("IPCChannelPosixTestConnectionProc",
245 SpinRunLoop(TestTimeouts::action_max_timeout());
246 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED
, listener
.status());
247 ASSERT_TRUE(channel
.HasAcceptedConnection());
248 IPC::Message
* message
= new IPC::Message(0, // routing_id
249 kQuitMessage
, // message type
250 IPC::Message::PRIORITY_NORMAL
);
251 channel
.Send(message
);
252 SpinRunLoop(TestTimeouts::action_timeout());
254 EXPECT_TRUE(base::WaitForExitCode(handle
, &exit_code
));
255 EXPECT_EQ(0, exit_code
);
256 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR
, listener
.status());
257 ASSERT_FALSE(channel
.HasAcceptedConnection());
260 TEST_F(IPCChannelPosixTest
, ResetState
) {
261 // Test creating a connection to an external process. Close the connection,
262 // but continue to listen and make sure another external process can connect
264 IPCChannelPosixTestListener
listener(false);
265 IPC::ChannelHandle
chan_handle(GetConnectionSocketName());
266 SetUpSocket(&chan_handle
, IPC::Channel::MODE_NAMED_SERVER
);
267 IPC::Channel
channel(chan_handle
, IPC::Channel::MODE_NAMED_SERVER
, &listener
);
268 ASSERT_TRUE(channel
.Connect());
269 ASSERT_TRUE(channel
.AcceptsConnections());
270 ASSERT_FALSE(channel
.HasAcceptedConnection());
272 base::ProcessHandle handle
= SpawnChild("IPCChannelPosixTestConnectionProc",
275 SpinRunLoop(TestTimeouts::action_max_timeout());
276 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED
, listener
.status());
277 ASSERT_TRUE(channel
.HasAcceptedConnection());
278 channel
.ResetToAcceptingConnectionState();
279 ASSERT_FALSE(channel
.HasAcceptedConnection());
281 base::ProcessHandle handle2
= SpawnChild("IPCChannelPosixTestConnectionProc",
283 ASSERT_TRUE(handle2
);
284 SpinRunLoop(TestTimeouts::action_max_timeout());
285 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED
, listener
.status());
286 ASSERT_TRUE(channel
.HasAcceptedConnection());
287 IPC::Message
* message
= new IPC::Message(0, // routing_id
288 kQuitMessage
, // message type
289 IPC::Message::PRIORITY_NORMAL
);
290 channel
.Send(message
);
291 SpinRunLoop(TestTimeouts::action_timeout());
292 EXPECT_TRUE(base::KillProcess(handle
, 0, false));
294 EXPECT_TRUE(base::WaitForExitCode(handle2
, &exit_code
));
295 EXPECT_EQ(0, exit_code
);
296 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR
, listener
.status());
297 ASSERT_FALSE(channel
.HasAcceptedConnection());
300 TEST_F(IPCChannelPosixTest
, BadChannelName
) {
302 IPC::ChannelHandle
handle("");
303 IPC::Channel
channel(handle
, IPC::Channel::MODE_NAMED_SERVER
, NULL
);
304 ASSERT_FALSE(channel
.Connect());
306 // Test name that is too long.
307 const char *kTooLongName
= "This_is_a_very_long_name_to_proactively_implement"
308 "client-centered_synergy_through_top-line"
309 "platforms_Phosfluorescently_disintermediate_"
310 "clicks-and-mortar_best_practices_without_"
311 "future-proof_growth_strategies_Continually"
312 "pontificate_proactive_potentialities_before"
313 "leading-edge_processes";
314 EXPECT_GE(strlen(kTooLongName
), IPC::kMaxPipeNameLength
);
315 IPC::ChannelHandle
handle2(kTooLongName
);
316 IPC::Channel
channel2(handle2
, IPC::Channel::MODE_NAMED_SERVER
, NULL
);
317 EXPECT_FALSE(channel2
.Connect());
320 TEST_F(IPCChannelPosixTest
, MultiConnection
) {
321 // Test setting up a connection to an external process, and then have
322 // another external process attempt to connect to us.
323 IPCChannelPosixTestListener
listener(false);
324 IPC::ChannelHandle
chan_handle(GetConnectionSocketName());
325 SetUpSocket(&chan_handle
, IPC::Channel::MODE_NAMED_SERVER
);
326 IPC::Channel
channel(chan_handle
, IPC::Channel::MODE_NAMED_SERVER
, &listener
);
327 ASSERT_TRUE(channel
.Connect());
328 ASSERT_TRUE(channel
.AcceptsConnections());
329 ASSERT_FALSE(channel
.HasAcceptedConnection());
331 base::ProcessHandle handle
= SpawnChild("IPCChannelPosixTestConnectionProc",
334 SpinRunLoop(TestTimeouts::action_max_timeout());
335 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED
, listener
.status());
336 ASSERT_TRUE(channel
.HasAcceptedConnection());
337 base::ProcessHandle handle2
= SpawnChild("IPCChannelPosixFailConnectionProc",
339 ASSERT_TRUE(handle2
);
340 SpinRunLoop(TestTimeouts::action_max_timeout());
342 EXPECT_TRUE(base::WaitForExitCode(handle2
, &exit_code
));
343 EXPECT_EQ(exit_code
, 0);
344 ASSERT_EQ(IPCChannelPosixTestListener::DENIED
, listener
.status());
345 ASSERT_TRUE(channel
.HasAcceptedConnection());
346 IPC::Message
* message
= new IPC::Message(0, // routing_id
347 kQuitMessage
, // message type
348 IPC::Message::PRIORITY_NORMAL
);
349 channel
.Send(message
);
350 SpinRunLoop(TestTimeouts::action_timeout());
351 EXPECT_TRUE(base::WaitForExitCode(handle
, &exit_code
));
352 EXPECT_EQ(exit_code
, 0);
353 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR
, listener
.status());
354 ASSERT_FALSE(channel
.HasAcceptedConnection());
357 TEST_F(IPCChannelPosixTest
, DoubleServer
) {
358 // Test setting up two servers with the same name.
359 IPCChannelPosixTestListener
listener(false);
360 IPCChannelPosixTestListener
listener2(false);
361 IPC::ChannelHandle
chan_handle(GetConnectionSocketName());
362 IPC::Channel
channel(chan_handle
, IPC::Channel::MODE_SERVER
, &listener
);
363 IPC::Channel
channel2(chan_handle
, IPC::Channel::MODE_SERVER
, &listener2
);
364 ASSERT_TRUE(channel
.Connect());
365 ASSERT_FALSE(channel2
.Connect());
368 TEST_F(IPCChannelPosixTest
, BadMode
) {
369 // Test setting up two servers with a bad mode.
370 IPCChannelPosixTestListener
listener(false);
371 IPC::ChannelHandle
chan_handle(GetConnectionSocketName());
372 IPC::Channel
channel(chan_handle
, IPC::Channel::MODE_NONE
, &listener
);
373 ASSERT_FALSE(channel
.Connect());
376 TEST_F(IPCChannelPosixTest
, IsNamedServerInitialized
) {
377 const std::string
& connection_socket_name
= GetConnectionSocketName();
378 IPCChannelPosixTestListener
listener(false);
379 IPC::ChannelHandle
chan_handle(connection_socket_name
);
380 ASSERT_TRUE(file_util::Delete(base::FilePath(connection_socket_name
), false));
381 ASSERT_FALSE(IPC::Channel::IsNamedServerInitialized(
382 connection_socket_name
));
383 IPC::Channel
channel(chan_handle
, IPC::Channel::MODE_NAMED_SERVER
, &listener
);
384 ASSERT_TRUE(IPC::Channel::IsNamedServerInitialized(
385 connection_socket_name
));
387 ASSERT_FALSE(IPC::Channel::IsNamedServerInitialized(
388 connection_socket_name
));
391 // A long running process that connects to us
392 MULTIPROCESS_TEST_MAIN(IPCChannelPosixTestConnectionProc
) {
393 MessageLoopForIO message_loop
;
394 IPCChannelPosixTestListener
listener(true);
395 IPC::ChannelHandle
handle(IPCChannelPosixTest::GetConnectionSocketName());
396 IPCChannelPosixTest::SetUpSocket(&handle
, IPC::Channel::MODE_NAMED_CLIENT
);
397 IPC::Channel
channel(handle
, IPC::Channel::MODE_NAMED_CLIENT
, &listener
);
398 EXPECT_TRUE(channel
.Connect());
399 IPCChannelPosixTest::SpinRunLoop(TestTimeouts::action_max_timeout());
400 EXPECT_EQ(IPCChannelPosixTestListener::MESSAGE_RECEIVED
, listener
.status());
404 // Simple external process that shouldn't be able to connect to us.
405 MULTIPROCESS_TEST_MAIN(IPCChannelPosixFailConnectionProc
) {
406 MessageLoopForIO message_loop
;
407 IPCChannelPosixTestListener
listener(false);
408 IPC::ChannelHandle
handle(IPCChannelPosixTest::GetConnectionSocketName());
409 IPCChannelPosixTest::SetUpSocket(&handle
, IPC::Channel::MODE_NAMED_CLIENT
);
410 IPC::Channel
channel(handle
, IPC::Channel::MODE_NAMED_CLIENT
, &listener
);
412 // In this case connect may succeed or fail depending on if the packet
413 // actually gets sent at sendmsg. Since we never delay on send, we may not
414 // see the error. However even if connect succeeds, eventually we will get an
415 // error back since the channel will be closed when we attempt to read from
417 bool connected
= channel
.Connect();
419 IPCChannelPosixTest::SpinRunLoop(TestTimeouts::action_max_timeout());
420 EXPECT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR
, listener
.status());
422 EXPECT_EQ(IPCChannelPosixTestListener::DISCONNECTED
, listener
.status());