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/eintr_wrapper.h"
16 #include "base/file_path.h"
17 #include "base/file_util.h"
18 #include "base/memory/scoped_ptr.h"
19 #include "base/message_loop.h"
20 #include "base/path_service.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_
;
97 class IPCChannelPosixTest
: public base::MultiProcessTest
{
99 static void SetUpSocket(IPC::ChannelHandle
*handle
,
100 IPC::Channel::Mode mode
);
101 static void SpinRunLoop(int milliseconds
);
102 static const std::string
GetConnectionSocketName();
103 static const std::string
GetChannelDirName();
106 virtual void SetUp();
107 virtual void TearDown();
110 scoped_ptr
<MessageLoopForIO
> message_loop_
;
113 const std::string
IPCChannelPosixTest::GetChannelDirName() {
114 #if defined(OS_ANDROID)
116 PathService::Get(base::DIR_CACHE
, &tmp_dir
);
117 return tmp_dir
.value();
123 const std::string
IPCChannelPosixTest::GetConnectionSocketName() {
124 return GetChannelDirName() + "/chrome_IPCChannelPosixTest__ConnectionSocket";
127 void IPCChannelPosixTest::SetUp() {
128 MultiProcessTest::SetUp();
129 // Construct a fresh IO Message loop for the duration of each test.
130 message_loop_
.reset(new MessageLoopForIO());
133 void IPCChannelPosixTest::TearDown() {
134 message_loop_
.reset(NULL
);
135 MultiProcessTest::TearDown();
138 // Create up a socket and bind and listen to it, or connect it
139 // depending on the |mode|.
140 void IPCChannelPosixTest::SetUpSocket(IPC::ChannelHandle
*handle
,
141 IPC::Channel::Mode mode
) {
142 const std::string
& name
= handle
->name
;
144 int socket_fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
145 ASSERT_GE(socket_fd
, 0) << name
;
146 ASSERT_GE(fcntl(socket_fd
, F_SETFL
, O_NONBLOCK
), 0);
147 struct sockaddr_un server_address
= { 0 };
148 memset(&server_address
, 0, sizeof(server_address
));
149 server_address
.sun_family
= AF_UNIX
;
150 int path_len
= snprintf(server_address
.sun_path
, IPC::kMaxPipeNameLength
,
152 DCHECK_EQ(static_cast<int>(name
.length()), path_len
);
153 size_t server_address_len
= offsetof(struct sockaddr_un
,
154 sun_path
) + path_len
+ 1;
156 if (mode
== IPC::Channel::MODE_NAMED_SERVER
) {
157 // Only one server at a time. Cleanup garbage if it exists.
158 unlink(name
.c_str());
159 // Make sure the path we need exists.
161 FilePath dir_path
= path
.DirName();
162 ASSERT_TRUE(file_util::CreateDirectory(dir_path
));
163 ASSERT_GE(bind(socket_fd
,
164 reinterpret_cast<struct sockaddr
*>(&server_address
),
165 server_address_len
), 0) << server_address
.sun_path
166 << ": " << strerror(errno
)
167 << "(" << errno
<< ")";
168 ASSERT_GE(listen(socket_fd
, SOMAXCONN
), 0) << server_address
.sun_path
169 << ": " << strerror(errno
)
170 << "(" << errno
<< ")";
171 } else if (mode
== IPC::Channel::MODE_NAMED_CLIENT
) {
172 ASSERT_GE(connect(socket_fd
,
173 reinterpret_cast<struct sockaddr
*>(&server_address
),
174 server_address_len
), 0) << server_address
.sun_path
175 << ": " << strerror(errno
)
176 << "(" << errno
<< ")";
178 FAIL() << "Unknown mode " << mode
;
180 handle
->socket
.fd
= socket_fd
;
183 void IPCChannelPosixTest::SpinRunLoop(int milliseconds
) {
184 MessageLoopForIO
*loop
= MessageLoopForIO::current();
185 // Post a quit task so that this loop eventually ends and we don't hang
186 // in the case of a bad test. Usually, the run loop will quit sooner than
187 // that because all tests use a IPCChannelPosixTestListener which quits the
188 // current run loop on any channel activity.
189 loop
->PostDelayedTask(
191 MessageLoop::QuitClosure(),
192 base::TimeDelta::FromMilliseconds(milliseconds
));
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 IPC::Channel
channel(handle
, IPC::Channel::MODE_NAMED_SERVER
, NULL
);
205 ASSERT_TRUE(channel
.Connect());
206 ASSERT_TRUE(channel
.AcceptsConnections());
207 ASSERT_FALSE(channel
.HasAcceptedConnection());
208 channel
.ResetToAcceptingConnectionState();
209 ASSERT_FALSE(channel
.HasAcceptedConnection());
212 TEST_F(IPCChannelPosixTest
, BasicConnected
) {
213 // Test creating a socket that is connected.
215 ASSERT_EQ(0, socketpair(AF_UNIX
, SOCK_STREAM
, 0, pipe_fds
));
216 std::string
socket_name("/var/tmp/IPCChannelPosixTest_BasicConnected");
217 ASSERT_GE(fcntl(pipe_fds
[0], F_SETFL
, O_NONBLOCK
), 0);
219 base::FileDescriptor
fd(pipe_fds
[0], false);
220 IPC::ChannelHandle
handle(socket_name
, fd
);
221 IPC::Channel
channel(handle
, IPC::Channel::MODE_SERVER
, NULL
);
222 ASSERT_TRUE(channel
.Connect());
223 ASSERT_FALSE(channel
.AcceptsConnections());
225 ASSERT_TRUE(HANDLE_EINTR(close(pipe_fds
[1])) == 0);
227 // Make sure that we can use the socket that is created for us by
228 // a standard channel.
229 IPC::Channel
channel2(socket_name
, IPC::Channel::MODE_SERVER
, NULL
);
230 ASSERT_TRUE(channel2
.Connect());
231 ASSERT_FALSE(channel2
.AcceptsConnections());
234 TEST_F(IPCChannelPosixTest
, AdvancedConnected
) {
235 // Test creating a connection to an external process.
236 IPCChannelPosixTestListener
listener(false);
237 IPC::ChannelHandle
chan_handle(GetConnectionSocketName());
238 SetUpSocket(&chan_handle
, IPC::Channel::MODE_NAMED_SERVER
);
239 IPC::Channel
channel(chan_handle
, IPC::Channel::MODE_NAMED_SERVER
, &listener
);
240 ASSERT_TRUE(channel
.Connect());
241 ASSERT_TRUE(channel
.AcceptsConnections());
242 ASSERT_FALSE(channel
.HasAcceptedConnection());
244 base::ProcessHandle handle
= SpawnChild("IPCChannelPosixTestConnectionProc",
247 SpinRunLoop(TestTimeouts::action_max_timeout_ms());
248 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED
, listener
.status());
249 ASSERT_TRUE(channel
.HasAcceptedConnection());
250 IPC::Message
* message
= new IPC::Message(0, // routing_id
251 kQuitMessage
, // message type
252 IPC::Message::PRIORITY_NORMAL
);
253 channel
.Send(message
);
254 SpinRunLoop(TestTimeouts::action_timeout_ms());
256 EXPECT_TRUE(base::WaitForExitCode(handle
, &exit_code
));
257 EXPECT_EQ(0, exit_code
);
258 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR
, listener
.status());
259 ASSERT_FALSE(channel
.HasAcceptedConnection());
262 TEST_F(IPCChannelPosixTest
, ResetState
) {
263 // Test creating a connection to an external process. Close the connection,
264 // but continue to listen and make sure another external process can connect
266 IPCChannelPosixTestListener
listener(false);
267 IPC::ChannelHandle
chan_handle(GetConnectionSocketName());
268 SetUpSocket(&chan_handle
, IPC::Channel::MODE_NAMED_SERVER
);
269 IPC::Channel
channel(chan_handle
, IPC::Channel::MODE_NAMED_SERVER
, &listener
);
270 ASSERT_TRUE(channel
.Connect());
271 ASSERT_TRUE(channel
.AcceptsConnections());
272 ASSERT_FALSE(channel
.HasAcceptedConnection());
274 base::ProcessHandle handle
= SpawnChild("IPCChannelPosixTestConnectionProc",
277 SpinRunLoop(TestTimeouts::action_max_timeout_ms());
278 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED
, listener
.status());
279 ASSERT_TRUE(channel
.HasAcceptedConnection());
280 channel
.ResetToAcceptingConnectionState();
281 ASSERT_FALSE(channel
.HasAcceptedConnection());
283 base::ProcessHandle handle2
= SpawnChild("IPCChannelPosixTestConnectionProc",
285 ASSERT_TRUE(handle2
);
286 SpinRunLoop(TestTimeouts::action_max_timeout_ms());
287 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED
, listener
.status());
288 ASSERT_TRUE(channel
.HasAcceptedConnection());
289 IPC::Message
* message
= new IPC::Message(0, // routing_id
290 kQuitMessage
, // message type
291 IPC::Message::PRIORITY_NORMAL
);
292 channel
.Send(message
);
293 SpinRunLoop(TestTimeouts::action_timeout_ms());
294 EXPECT_TRUE(base::KillProcess(handle
, 0, false));
296 EXPECT_TRUE(base::WaitForExitCode(handle2
, &exit_code
));
297 EXPECT_EQ(0, exit_code
);
298 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR
, listener
.status());
299 ASSERT_FALSE(channel
.HasAcceptedConnection());
302 TEST_F(IPCChannelPosixTest
, BadChannelName
) {
304 IPC::ChannelHandle
handle("");
305 IPC::Channel
channel(handle
, IPC::Channel::MODE_NAMED_SERVER
, NULL
);
306 ASSERT_FALSE(channel
.Connect());
308 // Test name that is too long.
309 const char *kTooLongName
= "This_is_a_very_long_name_to_proactively_implement"
310 "client-centered_synergy_through_top-line"
311 "platforms_Phosfluorescently_disintermediate_"
312 "clicks-and-mortar_best_practices_without_"
313 "future-proof_growth_strategies_Continually"
314 "pontificate_proactive_potentialities_before"
315 "leading-edge_processes";
316 EXPECT_GE(strlen(kTooLongName
), IPC::kMaxPipeNameLength
);
317 IPC::ChannelHandle
handle2(kTooLongName
);
318 IPC::Channel
channel2(handle2
, IPC::Channel::MODE_NAMED_SERVER
, NULL
);
319 EXPECT_FALSE(channel2
.Connect());
322 TEST_F(IPCChannelPosixTest
, MultiConnection
) {
323 // Test setting up a connection to an external process, and then have
324 // another external process attempt to connect to us.
325 IPCChannelPosixTestListener
listener(false);
326 IPC::ChannelHandle
chan_handle(GetConnectionSocketName());
327 SetUpSocket(&chan_handle
, IPC::Channel::MODE_NAMED_SERVER
);
328 IPC::Channel
channel(chan_handle
, IPC::Channel::MODE_NAMED_SERVER
, &listener
);
329 ASSERT_TRUE(channel
.Connect());
330 ASSERT_TRUE(channel
.AcceptsConnections());
331 ASSERT_FALSE(channel
.HasAcceptedConnection());
333 base::ProcessHandle handle
= SpawnChild("IPCChannelPosixTestConnectionProc",
336 SpinRunLoop(TestTimeouts::action_max_timeout_ms());
337 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED
, listener
.status());
338 ASSERT_TRUE(channel
.HasAcceptedConnection());
339 base::ProcessHandle handle2
= SpawnChild("IPCChannelPosixFailConnectionProc",
341 ASSERT_TRUE(handle2
);
342 SpinRunLoop(TestTimeouts::action_max_timeout_ms());
344 EXPECT_TRUE(base::WaitForExitCode(handle2
, &exit_code
));
345 EXPECT_EQ(exit_code
, 0);
346 ASSERT_EQ(IPCChannelPosixTestListener::DENIED
, 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_ms());
353 EXPECT_TRUE(base::WaitForExitCode(handle
, &exit_code
));
354 EXPECT_EQ(exit_code
, 0);
355 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR
, listener
.status());
356 ASSERT_FALSE(channel
.HasAcceptedConnection());
359 TEST_F(IPCChannelPosixTest
, DoubleServer
) {
360 // Test setting up two servers with the same name.
361 IPCChannelPosixTestListener
listener(false);
362 IPCChannelPosixTestListener
listener2(false);
363 IPC::ChannelHandle
chan_handle(GetConnectionSocketName());
364 IPC::Channel
channel(chan_handle
, IPC::Channel::MODE_SERVER
, &listener
);
365 IPC::Channel
channel2(chan_handle
, IPC::Channel::MODE_SERVER
, &listener2
);
366 ASSERT_TRUE(channel
.Connect());
367 ASSERT_FALSE(channel2
.Connect());
370 TEST_F(IPCChannelPosixTest
, BadMode
) {
371 // Test setting up two servers with a bad mode.
372 IPCChannelPosixTestListener
listener(false);
373 IPC::ChannelHandle
chan_handle(GetConnectionSocketName());
374 IPC::Channel
channel(chan_handle
, IPC::Channel::MODE_NONE
, &listener
);
375 ASSERT_FALSE(channel
.Connect());
378 TEST_F(IPCChannelPosixTest
, IsNamedServerInitialized
) {
379 const std::string
& connection_socket_name
= GetConnectionSocketName();
380 IPCChannelPosixTestListener
listener(false);
381 IPC::ChannelHandle
chan_handle(connection_socket_name
);
382 ASSERT_TRUE(file_util::Delete(FilePath(connection_socket_name
), false));
383 ASSERT_FALSE(IPC::Channel::IsNamedServerInitialized(
384 connection_socket_name
));
385 IPC::Channel
channel(chan_handle
, IPC::Channel::MODE_NAMED_SERVER
, &listener
);
386 ASSERT_TRUE(IPC::Channel::IsNamedServerInitialized(
387 connection_socket_name
));
389 ASSERT_FALSE(IPC::Channel::IsNamedServerInitialized(
390 connection_socket_name
));
393 // A long running process that connects to us
394 MULTIPROCESS_TEST_MAIN(IPCChannelPosixTestConnectionProc
) {
395 MessageLoopForIO message_loop
;
396 IPCChannelPosixTestListener
listener(true);
397 IPC::ChannelHandle
handle(IPCChannelPosixTest::GetConnectionSocketName());
398 IPCChannelPosixTest::SetUpSocket(&handle
, IPC::Channel::MODE_NAMED_CLIENT
);
399 IPC::Channel
channel(handle
, IPC::Channel::MODE_NAMED_CLIENT
, &listener
);
400 EXPECT_TRUE(channel
.Connect());
401 IPCChannelPosixTest::SpinRunLoop(TestTimeouts::action_max_timeout_ms());
402 EXPECT_EQ(IPCChannelPosixTestListener::MESSAGE_RECEIVED
, listener
.status());
406 // Simple external process that shouldn't be able to connect to us.
407 MULTIPROCESS_TEST_MAIN(IPCChannelPosixFailConnectionProc
) {
408 MessageLoopForIO message_loop
;
409 IPCChannelPosixTestListener
listener(false);
410 IPC::ChannelHandle
handle(IPCChannelPosixTest::GetConnectionSocketName());
411 IPCChannelPosixTest::SetUpSocket(&handle
, IPC::Channel::MODE_NAMED_CLIENT
);
412 IPC::Channel
channel(handle
, IPC::Channel::MODE_NAMED_CLIENT
, &listener
);
414 // In this case connect may succeed or fail depending on if the packet
415 // actually gets sent at sendmsg. Since we never delay on send, we may not
416 // see the error. However even if connect succeeds, eventually we will get an
417 // error back since the channel will be closed when we attempt to read from
419 bool connected
= channel
.Connect();
421 IPCChannelPosixTest::SpinRunLoop(TestTimeouts::action_max_timeout_ms());
422 EXPECT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR
, listener
.status());
424 EXPECT_EQ(IPCChannelPosixTestListener::DISCONNECTED
, listener
.status());