blob: 834f0ea74487640e7a47f916fb384c08546ab456 [file] [log] [blame]
tedvessenes@gmail.com56b33702012-03-07 13:41:40 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// These tests are POSIX only.
6
7#include "ipc/ipc_channel_posix.h"
8
9#include <fcntl.h>
10#include <sys/socket.h>
11#include <sys/un.h>
12#include <unistd.h>
13
14#include "base/basictypes.h"
15#include "base/eintr_wrapper.h"
16#include "base/file_path.h"
17#include "base/file_util.h"
levin@chromium.org5c528682011-03-28 10:54:15 +090018#include "base/memory/scoped_ptr.h"
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +090019#include "base/message_loop.h"
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +090020#include "base/path_service.h"
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +090021#include "base/test/multiprocess_test.h"
22#include "base/test/test_timeouts.h"
brettw@chromium.orgf947ed02012-06-12 07:35:26 +090023#include "ipc/ipc_listener.h"
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +090024#include "testing/multiprocess_func_list.h"
25
26namespace {
27
pkasting@chromium.orgc4f2de22011-09-01 09:46:33 +090028static const uint32 kQuitMessage = 47;
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +090029
brettw@chromium.orgf947ed02012-06-12 07:35:26 +090030class IPCChannelPosixTestListener : public IPC::Listener {
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +090031 public:
32 enum STATUS {
33 DISCONNECTED,
34 MESSAGE_RECEIVED,
35 CHANNEL_ERROR,
36 CONNECTED,
37 DENIED,
38 LISTEN_ERROR
39 };
40
41 IPCChannelPosixTestListener(bool quit_only_on_message)
42 : status_(DISCONNECTED), quit_only_on_message_(quit_only_on_message) {}
43
44 virtual ~IPCChannelPosixTestListener() {}
45
evan@chromium.orgecc3f072011-08-17 06:09:25 +090046 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE {
pkasting@chromium.orgc4f2de22011-09-01 09:46:33 +090047 EXPECT_EQ(message.type(), kQuitMessage);
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +090048 status_ = MESSAGE_RECEIVED;
49 QuitRunLoop();
jam@chromium.org8a2c7842010-12-24 15:19:28 +090050 return true;
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +090051 }
52
evan@chromium.orgecc3f072011-08-17 06:09:25 +090053 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE {
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +090054 status_ = CONNECTED;
55 if (!quit_only_on_message_) {
56 QuitRunLoop();
57 }
58 }
59
evan@chromium.orgecc3f072011-08-17 06:09:25 +090060 virtual void OnChannelError() OVERRIDE {
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +090061 status_ = CHANNEL_ERROR;
62 if (!quit_only_on_message_) {
63 QuitRunLoop();
64 }
65 }
66
evan@chromium.orgecc3f072011-08-17 06:09:25 +090067 virtual void OnChannelDenied() OVERRIDE {
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +090068 status_ = DENIED;
69 if (!quit_only_on_message_) {
70 QuitRunLoop();
71 }
72 }
73
evan@chromium.orgecc3f072011-08-17 06:09:25 +090074 virtual void OnChannelListenError() OVERRIDE {
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +090075 status_ = LISTEN_ERROR;
76 if (!quit_only_on_message_) {
77 QuitRunLoop();
78 }
79 }
80
81 STATUS status() { return status_; }
82
83 void QuitRunLoop() {
84 MessageLoopForIO::current()->QuitNow();
85 }
86
87 private:
88 // The current status of the listener.
89 STATUS status_;
90 // If |quit_only_on_message_| then the listener will only break out of
pkasting@chromium.orgc4f2de22011-09-01 09:46:33 +090091 // the run loop when kQuitMessage is received.
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +090092 bool quit_only_on_message_;
93};
94
95} // namespace
96
97class IPCChannelPosixTest : public base::MultiProcessTest {
98 public:
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +090099 static void SetUpSocket(IPC::ChannelHandle *handle,
100 IPC::Channel::Mode mode);
101 static void SpinRunLoop(int milliseconds);
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900102 static const std::string GetConnectionSocketName();
103 static const std::string GetChannelDirName();
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900104
105 protected:
106 virtual void SetUp();
107 virtual void TearDown();
108
109private:
110 scoped_ptr<MessageLoopForIO> message_loop_;
111};
112
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900113const std::string IPCChannelPosixTest::GetChannelDirName() {
jrg@chromium.org2eb192e2011-11-04 09:14:16 +0900114#if defined(OS_ANDROID)
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900115 FilePath tmp_dir;
116 PathService::Get(base::DIR_CACHE, &tmp_dir);
117 return tmp_dir.value();
jrg@chromium.org2eb192e2011-11-04 09:14:16 +0900118#else
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900119 return "/var/tmp";
jrg@chromium.org2eb192e2011-11-04 09:14:16 +0900120#endif
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900121}
122
123const std::string IPCChannelPosixTest::GetConnectionSocketName() {
124 return GetChannelDirName() + "/chrome_IPCChannelPosixTest__ConnectionSocket";
125}
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900126
127void IPCChannelPosixTest::SetUp() {
128 MultiProcessTest::SetUp();
129 // Construct a fresh IO Message loop for the duration of each test.
130 message_loop_.reset(new MessageLoopForIO());
131}
132
133void IPCChannelPosixTest::TearDown() {
134 message_loop_.reset(NULL);
135 MultiProcessTest::TearDown();
136}
137
138// Create up a socket and bind and listen to it, or connect it
139// depending on the |mode|.
140void IPCChannelPosixTest::SetUpSocket(IPC::ChannelHandle *handle,
141 IPC::Channel::Mode mode) {
142 const std::string& name = handle->name;
143
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,
151 "%s", name.c_str());
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;
155
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());
pkasting@chromium.orgc4f2de22011-09-01 09:46:33 +0900159 // Make sure the path we need exists.
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900160 FilePath path(name);
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 << ")";
177 } else {
178 FAIL() << "Unknown mode " << mode;
179 }
180 handle->socket.fd = socket_fd;
181}
182
183void 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.
tedvessenes@gmail.com56b33702012-03-07 13:41:40 +0900189 loop->PostDelayedTask(
190 FROM_HERE,
191 MessageLoop::QuitClosure(),
192 base::TimeDelta::FromMilliseconds(milliseconds));
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900193 loop->Run();
194}
195
196TEST_F(IPCChannelPosixTest, BasicListen) {
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900197 const std::string kChannelName =
198 GetChannelDirName() + "/IPCChannelPosixTest_BasicListen";
jrg@chromium.org2eb192e2011-11-04 09:14:16 +0900199
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900200 // Test creating a socket that is listening.
jrg@chromium.org2eb192e2011-11-04 09:14:16 +0900201 IPC::ChannelHandle handle(kChannelName);
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900202 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());
210}
211
212TEST_F(IPCChannelPosixTest, BasicConnected) {
213 // Test creating a socket that is connected.
214 int pipe_fds[2];
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);
218
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());
224 channel.Close();
225 ASSERT_TRUE(HANDLE_EINTR(close(pipe_fds[1])) == 0);
226
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());
232}
233
234TEST_F(IPCChannelPosixTest, AdvancedConnected) {
235 // Test creating a connection to an external process.
236 IPCChannelPosixTestListener listener(false);
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900237 IPC::ChannelHandle chan_handle(GetConnectionSocketName());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900238 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());
243
244 base::ProcessHandle handle = SpawnChild("IPCChannelPosixTestConnectionProc",
245 false);
246 ASSERT_TRUE(handle);
247 SpinRunLoop(TestTimeouts::action_max_timeout_ms());
248 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status());
249 ASSERT_TRUE(channel.HasAcceptedConnection());
pkasting@chromium.orgc4f2de22011-09-01 09:46:33 +0900250 IPC::Message* message = new IPC::Message(0, // routing_id
251 kQuitMessage, // message type
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900252 IPC::Message::PRIORITY_NORMAL);
253 channel.Send(message);
254 SpinRunLoop(TestTimeouts::action_timeout_ms());
255 int exit_code = 0;
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());
260}
261
262TEST_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
265 // to us.
266 IPCChannelPosixTestListener listener(false);
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900267 IPC::ChannelHandle chan_handle(GetConnectionSocketName());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900268 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());
273
274 base::ProcessHandle handle = SpawnChild("IPCChannelPosixTestConnectionProc",
275 false);
276 ASSERT_TRUE(handle);
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());
282
283 base::ProcessHandle handle2 = SpawnChild("IPCChannelPosixTestConnectionProc",
284 false);
285 ASSERT_TRUE(handle2);
286 SpinRunLoop(TestTimeouts::action_max_timeout_ms());
287 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status());
288 ASSERT_TRUE(channel.HasAcceptedConnection());
pkasting@chromium.orgc4f2de22011-09-01 09:46:33 +0900289 IPC::Message* message = new IPC::Message(0, // routing_id
290 kQuitMessage, // message type
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900291 IPC::Message::PRIORITY_NORMAL);
292 channel.Send(message);
293 SpinRunLoop(TestTimeouts::action_timeout_ms());
294 EXPECT_TRUE(base::KillProcess(handle, 0, false));
295 int exit_code = 0;
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());
300}
301
dmaclach@chromium.org344a3d52011-08-25 23:14:05 +0900302TEST_F(IPCChannelPosixTest, BadChannelName) {
303 // Test empty name
304 IPC::ChannelHandle handle("");
305 IPC::Channel channel(handle, IPC::Channel::MODE_NAMED_SERVER, NULL);
306 ASSERT_FALSE(channel.Connect());
307
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());
320}
321
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900322TEST_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);
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900326 IPC::ChannelHandle chan_handle(GetConnectionSocketName());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900327 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());
332
333 base::ProcessHandle handle = SpawnChild("IPCChannelPosixTestConnectionProc",
334 false);
335 ASSERT_TRUE(handle);
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",
340 false);
341 ASSERT_TRUE(handle2);
342 SpinRunLoop(TestTimeouts::action_max_timeout_ms());
343 int exit_code = 0;
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());
pkasting@chromium.orgc4f2de22011-09-01 09:46:33 +0900348 IPC::Message* message = new IPC::Message(0, // routing_id
349 kQuitMessage, // message type
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900350 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());
357}
358
dmaclach@chromium.org2812db62011-03-03 07:27:14 +0900359TEST_F(IPCChannelPosixTest, DoubleServer) {
360 // Test setting up two servers with the same name.
361 IPCChannelPosixTestListener listener(false);
362 IPCChannelPosixTestListener listener2(false);
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900363 IPC::ChannelHandle chan_handle(GetConnectionSocketName());
dmaclach@chromium.org2812db62011-03-03 07:27:14 +0900364 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());
368}
369
370TEST_F(IPCChannelPosixTest, BadMode) {
371 // Test setting up two servers with a bad mode.
372 IPCChannelPosixTestListener listener(false);
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900373 IPC::ChannelHandle chan_handle(GetConnectionSocketName());
dmaclach@chromium.org2812db62011-03-03 07:27:14 +0900374 IPC::Channel channel(chan_handle, IPC::Channel::MODE_NONE, &listener);
375 ASSERT_FALSE(channel.Connect());
376}
377
kkania@chromium.orgf37b4e52011-08-09 15:46:06 +0900378TEST_F(IPCChannelPosixTest, IsNamedServerInitialized) {
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900379 const std::string& connection_socket_name = GetConnectionSocketName();
kkania@chromium.orgf37b4e52011-08-09 15:46:06 +0900380 IPCChannelPosixTestListener listener(false);
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900381 IPC::ChannelHandle chan_handle(connection_socket_name);
382 ASSERT_TRUE(file_util::Delete(FilePath(connection_socket_name), false));
kkania@chromium.orgf37b4e52011-08-09 15:46:06 +0900383 ASSERT_FALSE(IPC::Channel::IsNamedServerInitialized(
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900384 connection_socket_name));
kkania@chromium.orgf37b4e52011-08-09 15:46:06 +0900385 IPC::Channel channel(chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener);
386 ASSERT_TRUE(IPC::Channel::IsNamedServerInitialized(
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900387 connection_socket_name));
kkania@chromium.orgf37b4e52011-08-09 15:46:06 +0900388 channel.Close();
389 ASSERT_FALSE(IPC::Channel::IsNamedServerInitialized(
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900390 connection_socket_name));
kkania@chromium.orgf37b4e52011-08-09 15:46:06 +0900391}
392
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900393// A long running process that connects to us
394MULTIPROCESS_TEST_MAIN(IPCChannelPosixTestConnectionProc) {
395 MessageLoopForIO message_loop;
396 IPCChannelPosixTestListener listener(true);
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900397 IPC::ChannelHandle handle(IPCChannelPosixTest::GetConnectionSocketName());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900398 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());
403 return 0;
404}
405
406// Simple external process that shouldn't be able to connect to us.
407MULTIPROCESS_TEST_MAIN(IPCChannelPosixFailConnectionProc) {
408 MessageLoopForIO message_loop;
409 IPCChannelPosixTestListener listener(false);
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900410 IPC::ChannelHandle handle(IPCChannelPosixTest::GetConnectionSocketName());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900411 IPCChannelPosixTest::SetUpSocket(&handle, IPC::Channel::MODE_NAMED_CLIENT);
412 IPC::Channel channel(handle, IPC::Channel::MODE_NAMED_CLIENT, &listener);
413
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
418 // it.
419 bool connected = channel.Connect();
420 if (connected) {
421 IPCChannelPosixTest::SpinRunLoop(TestTimeouts::action_max_timeout_ms());
422 EXPECT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status());
423 } else {
424 EXPECT_EQ(IPCChannelPosixTestListener::DISCONNECTED, listener.status());
425 }
426 return 0;
427}