1 // Copyright (c) 2011 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/test/multiprocess_test.h"
21 #include "base/test/test_timeouts.h"
22 #include "testing/multiprocess_func_list.h"
26 static const uint32 kQuitMessage
= 47;
28 class IPCChannelPosixTestListener
: public IPC::Channel::Listener
{
39 IPCChannelPosixTestListener(bool quit_only_on_message
)
40 : status_(DISCONNECTED
), quit_only_on_message_(quit_only_on_message
) {}
42 virtual ~IPCChannelPosixTestListener() {}
44 virtual bool OnMessageReceived(const IPC::Message
& message
) OVERRIDE
{
45 EXPECT_EQ(message
.type(), kQuitMessage
);
46 status_
= MESSAGE_RECEIVED
;
51 virtual void OnChannelConnected(int32 peer_pid
) OVERRIDE
{
53 if (!quit_only_on_message_
) {
58 virtual void OnChannelError() OVERRIDE
{
59 status_
= CHANNEL_ERROR
;
60 if (!quit_only_on_message_
) {
65 virtual void OnChannelDenied() OVERRIDE
{
67 if (!quit_only_on_message_
) {
72 virtual void OnChannelListenError() OVERRIDE
{
73 status_
= LISTEN_ERROR
;
74 if (!quit_only_on_message_
) {
79 STATUS
status() { return status_
; }
82 MessageLoopForIO::current()->QuitNow();
86 // The current status of the listener.
88 // If |quit_only_on_message_| then the listener will only break out of
89 // the run loop when kQuitMessage is received.
90 bool quit_only_on_message_
;
95 class IPCChannelPosixTest
: public base::MultiProcessTest
{
97 static const char kConnectionSocketTestName
[];
98 static void SetUpSocket(IPC::ChannelHandle
*handle
,
99 IPC::Channel::Mode mode
);
100 static void SpinRunLoop(int milliseconds
);
103 virtual void SetUp();
104 virtual void TearDown();
107 scoped_ptr
<MessageLoopForIO
> message_loop_
;
110 const char IPCChannelPosixTest::kConnectionSocketTestName
[] =
111 "/var/tmp/chrome_IPCChannelPosixTest__ConnectionSocket";
113 void IPCChannelPosixTest::SetUp() {
114 MultiProcessTest::SetUp();
115 // Construct a fresh IO Message loop for the duration of each test.
116 message_loop_
.reset(new MessageLoopForIO());
119 void IPCChannelPosixTest::TearDown() {
120 message_loop_
.reset(NULL
);
121 MultiProcessTest::TearDown();
124 // Create up a socket and bind and listen to it, or connect it
125 // depending on the |mode|.
126 void IPCChannelPosixTest::SetUpSocket(IPC::ChannelHandle
*handle
,
127 IPC::Channel::Mode mode
) {
128 const std::string
& name
= handle
->name
;
130 int socket_fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
131 ASSERT_GE(socket_fd
, 0) << name
;
132 ASSERT_GE(fcntl(socket_fd
, F_SETFL
, O_NONBLOCK
), 0);
133 struct sockaddr_un server_address
= { 0 };
134 memset(&server_address
, 0, sizeof(server_address
));
135 server_address
.sun_family
= AF_UNIX
;
136 int path_len
= snprintf(server_address
.sun_path
, IPC::kMaxPipeNameLength
,
138 DCHECK_EQ(static_cast<int>(name
.length()), path_len
);
139 size_t server_address_len
= offsetof(struct sockaddr_un
,
140 sun_path
) + path_len
+ 1;
142 if (mode
== IPC::Channel::MODE_NAMED_SERVER
) {
143 // Only one server at a time. Cleanup garbage if it exists.
144 unlink(name
.c_str());
145 // Make sure the path we need exists.
147 FilePath dir_path
= path
.DirName();
148 ASSERT_TRUE(file_util::CreateDirectory(dir_path
));
149 ASSERT_GE(bind(socket_fd
,
150 reinterpret_cast<struct sockaddr
*>(&server_address
),
151 server_address_len
), 0) << server_address
.sun_path
152 << ": " << strerror(errno
)
153 << "(" << errno
<< ")";
154 ASSERT_GE(listen(socket_fd
, SOMAXCONN
), 0) << server_address
.sun_path
155 << ": " << strerror(errno
)
156 << "(" << errno
<< ")";
157 } else if (mode
== IPC::Channel::MODE_NAMED_CLIENT
) {
158 ASSERT_GE(connect(socket_fd
,
159 reinterpret_cast<struct sockaddr
*>(&server_address
),
160 server_address_len
), 0) << server_address
.sun_path
161 << ": " << strerror(errno
)
162 << "(" << errno
<< ")";
164 FAIL() << "Unknown mode " << mode
;
166 handle
->socket
.fd
= socket_fd
;
169 void IPCChannelPosixTest::SpinRunLoop(int milliseconds
) {
170 MessageLoopForIO
*loop
= MessageLoopForIO::current();
171 // Post a quit task so that this loop eventually ends and we don't hang
172 // in the case of a bad test. Usually, the run loop will quit sooner than
173 // that because all tests use a IPCChannelPosixTestListener which quits the
174 // current run loop on any channel activity.
175 loop
->PostDelayedTask(FROM_HERE
, new MessageLoop::QuitTask(), milliseconds
);
179 TEST_F(IPCChannelPosixTest
, BasicListen
) {
180 // Test creating a socket that is listening.
181 IPC::ChannelHandle
handle("/var/tmp/IPCChannelPosixTest_BasicListen");
182 SetUpSocket(&handle
, IPC::Channel::MODE_NAMED_SERVER
);
183 unlink(handle
.name
.c_str());
184 IPC::Channel
channel(handle
, IPC::Channel::MODE_NAMED_SERVER
, NULL
);
185 ASSERT_TRUE(channel
.Connect());
186 ASSERT_TRUE(channel
.AcceptsConnections());
187 ASSERT_FALSE(channel
.HasAcceptedConnection());
188 channel
.ResetToAcceptingConnectionState();
189 ASSERT_FALSE(channel
.HasAcceptedConnection());
192 TEST_F(IPCChannelPosixTest
, BasicConnected
) {
193 // Test creating a socket that is connected.
195 ASSERT_EQ(0, socketpair(AF_UNIX
, SOCK_STREAM
, 0, pipe_fds
));
196 std::string
socket_name("/var/tmp/IPCChannelPosixTest_BasicConnected");
197 ASSERT_GE(fcntl(pipe_fds
[0], F_SETFL
, O_NONBLOCK
), 0);
199 base::FileDescriptor
fd(pipe_fds
[0], false);
200 IPC::ChannelHandle
handle(socket_name
, fd
);
201 IPC::Channel
channel(handle
, IPC::Channel::MODE_SERVER
, NULL
);
202 ASSERT_TRUE(channel
.Connect());
203 ASSERT_FALSE(channel
.AcceptsConnections());
205 ASSERT_TRUE(HANDLE_EINTR(close(pipe_fds
[1])) == 0);
207 // Make sure that we can use the socket that is created for us by
208 // a standard channel.
209 IPC::Channel
channel2(socket_name
, IPC::Channel::MODE_SERVER
, NULL
);
210 ASSERT_TRUE(channel2
.Connect());
211 ASSERT_FALSE(channel2
.AcceptsConnections());
214 TEST_F(IPCChannelPosixTest
, AdvancedConnected
) {
215 // Test creating a connection to an external process.
216 IPCChannelPosixTestListener
listener(false);
217 IPC::ChannelHandle
chan_handle(kConnectionSocketTestName
);
218 SetUpSocket(&chan_handle
, IPC::Channel::MODE_NAMED_SERVER
);
219 IPC::Channel
channel(chan_handle
, IPC::Channel::MODE_NAMED_SERVER
, &listener
);
220 ASSERT_TRUE(channel
.Connect());
221 ASSERT_TRUE(channel
.AcceptsConnections());
222 ASSERT_FALSE(channel
.HasAcceptedConnection());
224 base::ProcessHandle handle
= SpawnChild("IPCChannelPosixTestConnectionProc",
227 SpinRunLoop(TestTimeouts::action_max_timeout_ms());
228 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED
, listener
.status());
229 ASSERT_TRUE(channel
.HasAcceptedConnection());
230 IPC::Message
* message
= new IPC::Message(0, // routing_id
231 kQuitMessage
, // message type
232 IPC::Message::PRIORITY_NORMAL
);
233 channel
.Send(message
);
234 SpinRunLoop(TestTimeouts::action_timeout_ms());
236 EXPECT_TRUE(base::WaitForExitCode(handle
, &exit_code
));
237 EXPECT_EQ(0, exit_code
);
238 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR
, listener
.status());
239 ASSERT_FALSE(channel
.HasAcceptedConnection());
242 TEST_F(IPCChannelPosixTest
, ResetState
) {
243 // Test creating a connection to an external process. Close the connection,
244 // but continue to listen and make sure another external process can connect
246 IPCChannelPosixTestListener
listener(false);
247 IPC::ChannelHandle
chan_handle(kConnectionSocketTestName
);
248 SetUpSocket(&chan_handle
, IPC::Channel::MODE_NAMED_SERVER
);
249 IPC::Channel
channel(chan_handle
, IPC::Channel::MODE_NAMED_SERVER
, &listener
);
250 ASSERT_TRUE(channel
.Connect());
251 ASSERT_TRUE(channel
.AcceptsConnections());
252 ASSERT_FALSE(channel
.HasAcceptedConnection());
254 base::ProcessHandle handle
= SpawnChild("IPCChannelPosixTestConnectionProc",
257 SpinRunLoop(TestTimeouts::action_max_timeout_ms());
258 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED
, listener
.status());
259 ASSERT_TRUE(channel
.HasAcceptedConnection());
260 channel
.ResetToAcceptingConnectionState();
261 ASSERT_FALSE(channel
.HasAcceptedConnection());
263 base::ProcessHandle handle2
= SpawnChild("IPCChannelPosixTestConnectionProc",
265 ASSERT_TRUE(handle2
);
266 SpinRunLoop(TestTimeouts::action_max_timeout_ms());
267 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED
, listener
.status());
268 ASSERT_TRUE(channel
.HasAcceptedConnection());
269 IPC::Message
* message
= new IPC::Message(0, // routing_id
270 kQuitMessage
, // message type
271 IPC::Message::PRIORITY_NORMAL
);
272 channel
.Send(message
);
273 SpinRunLoop(TestTimeouts::action_timeout_ms());
274 EXPECT_TRUE(base::KillProcess(handle
, 0, false));
276 EXPECT_TRUE(base::WaitForExitCode(handle2
, &exit_code
));
277 EXPECT_EQ(0, exit_code
);
278 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR
, listener
.status());
279 ASSERT_FALSE(channel
.HasAcceptedConnection());
282 TEST_F(IPCChannelPosixTest
, BadChannelName
) {
284 IPC::ChannelHandle
handle("");
285 IPC::Channel
channel(handle
, IPC::Channel::MODE_NAMED_SERVER
, NULL
);
286 ASSERT_FALSE(channel
.Connect());
288 // Test name that is too long.
289 const char *kTooLongName
= "This_is_a_very_long_name_to_proactively_implement"
290 "client-centered_synergy_through_top-line"
291 "platforms_Phosfluorescently_disintermediate_"
292 "clicks-and-mortar_best_practices_without_"
293 "future-proof_growth_strategies_Continually"
294 "pontificate_proactive_potentialities_before"
295 "leading-edge_processes";
296 EXPECT_GE(strlen(kTooLongName
), IPC::kMaxPipeNameLength
);
297 IPC::ChannelHandle
handle2(kTooLongName
);
298 IPC::Channel
channel2(handle2
, IPC::Channel::MODE_NAMED_SERVER
, NULL
);
299 EXPECT_FALSE(channel2
.Connect());
302 TEST_F(IPCChannelPosixTest
, MultiConnection
) {
303 // Test setting up a connection to an external process, and then have
304 // another external process attempt to connect to us.
305 IPCChannelPosixTestListener
listener(false);
306 IPC::ChannelHandle
chan_handle(kConnectionSocketTestName
);
307 SetUpSocket(&chan_handle
, IPC::Channel::MODE_NAMED_SERVER
);
308 IPC::Channel
channel(chan_handle
, IPC::Channel::MODE_NAMED_SERVER
, &listener
);
309 ASSERT_TRUE(channel
.Connect());
310 ASSERT_TRUE(channel
.AcceptsConnections());
311 ASSERT_FALSE(channel
.HasAcceptedConnection());
313 base::ProcessHandle handle
= SpawnChild("IPCChannelPosixTestConnectionProc",
316 SpinRunLoop(TestTimeouts::action_max_timeout_ms());
317 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED
, listener
.status());
318 ASSERT_TRUE(channel
.HasAcceptedConnection());
319 base::ProcessHandle handle2
= SpawnChild("IPCChannelPosixFailConnectionProc",
321 ASSERT_TRUE(handle2
);
322 SpinRunLoop(TestTimeouts::action_max_timeout_ms());
324 EXPECT_TRUE(base::WaitForExitCode(handle2
, &exit_code
));
325 EXPECT_EQ(exit_code
, 0);
326 ASSERT_EQ(IPCChannelPosixTestListener::DENIED
, listener
.status());
327 ASSERT_TRUE(channel
.HasAcceptedConnection());
328 IPC::Message
* message
= new IPC::Message(0, // routing_id
329 kQuitMessage
, // message type
330 IPC::Message::PRIORITY_NORMAL
);
331 channel
.Send(message
);
332 SpinRunLoop(TestTimeouts::action_timeout_ms());
333 EXPECT_TRUE(base::WaitForExitCode(handle
, &exit_code
));
334 EXPECT_EQ(exit_code
, 0);
335 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR
, listener
.status());
336 ASSERT_FALSE(channel
.HasAcceptedConnection());
339 TEST_F(IPCChannelPosixTest
, DoubleServer
) {
340 // Test setting up two servers with the same name.
341 IPCChannelPosixTestListener
listener(false);
342 IPCChannelPosixTestListener
listener2(false);
343 IPC::ChannelHandle
chan_handle(kConnectionSocketTestName
);
344 IPC::Channel
channel(chan_handle
, IPC::Channel::MODE_SERVER
, &listener
);
345 IPC::Channel
channel2(chan_handle
, IPC::Channel::MODE_SERVER
, &listener2
);
346 ASSERT_TRUE(channel
.Connect());
347 ASSERT_FALSE(channel2
.Connect());
350 TEST_F(IPCChannelPosixTest
, BadMode
) {
351 // Test setting up two servers with a bad mode.
352 IPCChannelPosixTestListener
listener(false);
353 IPC::ChannelHandle
chan_handle(kConnectionSocketTestName
);
354 IPC::Channel
channel(chan_handle
, IPC::Channel::MODE_NONE
, &listener
);
355 ASSERT_FALSE(channel
.Connect());
358 TEST_F(IPCChannelPosixTest
, IsNamedServerInitialized
) {
359 IPCChannelPosixTestListener
listener(false);
360 IPC::ChannelHandle
chan_handle(kConnectionSocketTestName
);
361 ASSERT_TRUE(file_util::Delete(FilePath(kConnectionSocketTestName
), false));
362 ASSERT_FALSE(IPC::Channel::IsNamedServerInitialized(
363 kConnectionSocketTestName
));
364 IPC::Channel
channel(chan_handle
, IPC::Channel::MODE_NAMED_SERVER
, &listener
);
365 ASSERT_TRUE(IPC::Channel::IsNamedServerInitialized(
366 kConnectionSocketTestName
));
368 ASSERT_FALSE(IPC::Channel::IsNamedServerInitialized(
369 kConnectionSocketTestName
));
372 // A long running process that connects to us
373 MULTIPROCESS_TEST_MAIN(IPCChannelPosixTestConnectionProc
) {
374 MessageLoopForIO message_loop
;
375 IPCChannelPosixTestListener
listener(true);
376 IPC::ChannelHandle
handle(IPCChannelPosixTest::kConnectionSocketTestName
);
377 IPCChannelPosixTest::SetUpSocket(&handle
, IPC::Channel::MODE_NAMED_CLIENT
);
378 IPC::Channel
channel(handle
, IPC::Channel::MODE_NAMED_CLIENT
, &listener
);
379 EXPECT_TRUE(channel
.Connect());
380 IPCChannelPosixTest::SpinRunLoop(TestTimeouts::action_max_timeout_ms());
381 EXPECT_EQ(IPCChannelPosixTestListener::MESSAGE_RECEIVED
, listener
.status());
385 // Simple external process that shouldn't be able to connect to us.
386 MULTIPROCESS_TEST_MAIN(IPCChannelPosixFailConnectionProc
) {
387 MessageLoopForIO message_loop
;
388 IPCChannelPosixTestListener
listener(false);
389 IPC::ChannelHandle
handle(IPCChannelPosixTest::kConnectionSocketTestName
);
390 IPCChannelPosixTest::SetUpSocket(&handle
, IPC::Channel::MODE_NAMED_CLIENT
);
391 IPC::Channel
channel(handle
, IPC::Channel::MODE_NAMED_CLIENT
, &listener
);
393 // In this case connect may succeed or fail depending on if the packet
394 // actually gets sent at sendmsg. Since we never delay on send, we may not
395 // see the error. However even if connect succeeds, eventually we will get an
396 // error back since the channel will be closed when we attempt to read from
398 bool connected
= channel
.Connect();
400 IPCChannelPosixTest::SpinRunLoop(TestTimeouts::action_max_timeout_ms());
401 EXPECT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR
, listener
.status());
403 EXPECT_EQ(IPCChannelPosixTestListener::DISCONNECTED
, listener
.status());