blob: 68cb37710a133975b41c65cb6ded778691dfba2f [file] [log] [blame]
[email protected]5896fa042012-03-07 04:41:401// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]22b42c52010-12-20 06:59:232// 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"
[email protected]22b42c52010-12-20 06:59:2315#include "base/file_util.h"
[email protected]57999812013-02-24 05:40:5216#include "base/files/file_path.h"
[email protected]3b63f8f42011-03-28 01:54:1517#include "base/memory/scoped_ptr.h"
[email protected]22b42c52010-12-20 06:59:2318#include "base/message_loop.h"
[email protected]5f6259d2012-05-25 02:56:2519#include "base/path_service.h"
[email protected]2025d002012-11-14 20:54:3520#include "base/posix/eintr_wrapper.h"
[email protected]22b42c52010-12-20 06:59:2321#include "base/test/multiprocess_test.h"
22#include "base/test/test_timeouts.h"
[email protected]57319ce2012-06-11 22:35:2623#include "ipc/ipc_listener.h"
[email protected]bdf9bdc2013-03-13 04:23:1024#include "ipc/unix_domain_socket_util.h"
[email protected]22b42c52010-12-20 06:59:2325#include "testing/multiprocess_func_list.h"
26
27namespace {
28
[email protected]d6431722011-09-01 00:46:3329static const uint32 kQuitMessage = 47;
[email protected]22b42c52010-12-20 06:59:2330
[email protected]57319ce2012-06-11 22:35:2631class IPCChannelPosixTestListener : public IPC::Listener {
[email protected]22b42c52010-12-20 06:59:2332 public:
33 enum STATUS {
34 DISCONNECTED,
35 MESSAGE_RECEIVED,
36 CHANNEL_ERROR,
37 CONNECTED,
38 DENIED,
39 LISTEN_ERROR
40 };
41
42 IPCChannelPosixTestListener(bool quit_only_on_message)
43 : status_(DISCONNECTED), quit_only_on_message_(quit_only_on_message) {}
44
45 virtual ~IPCChannelPosixTestListener() {}
46
[email protected]da1bd182011-08-16 21:09:2547 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE {
[email protected]d6431722011-09-01 00:46:3348 EXPECT_EQ(message.type(), kQuitMessage);
[email protected]22b42c52010-12-20 06:59:2349 status_ = MESSAGE_RECEIVED;
50 QuitRunLoop();
[email protected]a95986a82010-12-24 06:19:2851 return true;
[email protected]22b42c52010-12-20 06:59:2352 }
53
[email protected]da1bd182011-08-16 21:09:2554 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE {
[email protected]22b42c52010-12-20 06:59:2355 status_ = CONNECTED;
56 if (!quit_only_on_message_) {
57 QuitRunLoop();
58 }
59 }
60
[email protected]da1bd182011-08-16 21:09:2561 virtual void OnChannelError() OVERRIDE {
[email protected]22b42c52010-12-20 06:59:2362 status_ = CHANNEL_ERROR;
63 if (!quit_only_on_message_) {
64 QuitRunLoop();
65 }
66 }
67
[email protected]da1bd182011-08-16 21:09:2568 virtual void OnChannelDenied() OVERRIDE {
[email protected]22b42c52010-12-20 06:59:2369 status_ = DENIED;
70 if (!quit_only_on_message_) {
71 QuitRunLoop();
72 }
73 }
74
[email protected]da1bd182011-08-16 21:09:2575 virtual void OnChannelListenError() OVERRIDE {
[email protected]22b42c52010-12-20 06:59:2376 status_ = LISTEN_ERROR;
77 if (!quit_only_on_message_) {
78 QuitRunLoop();
79 }
80 }
81
82 STATUS status() { return status_; }
83
84 void QuitRunLoop() {
[email protected]fd0a773a2013-04-30 20:55:0385 base::MessageLoopForIO::current()->QuitNow();
[email protected]22b42c52010-12-20 06:59:2386 }
87
88 private:
89 // The current status of the listener.
90 STATUS status_;
91 // If |quit_only_on_message_| then the listener will only break out of
[email protected]d6431722011-09-01 00:46:3392 // the run loop when kQuitMessage is received.
[email protected]22b42c52010-12-20 06:59:2393 bool quit_only_on_message_;
94};
95
[email protected]22b42c52010-12-20 06:59:2396class IPCChannelPosixTest : public base::MultiProcessTest {
97 public:
[email protected]22b42c52010-12-20 06:59:2398 static void SetUpSocket(IPC::ChannelHandle *handle,
99 IPC::Channel::Mode mode);
[email protected]0f2e45ec2012-07-11 15:41:43100 static void SpinRunLoop(base::TimeDelta delay);
[email protected]5f6259d2012-05-25 02:56:25101 static const std::string GetConnectionSocketName();
102 static const std::string GetChannelDirName();
[email protected]22b42c52010-12-20 06:59:23103
104 protected:
105 virtual void SetUp();
106 virtual void TearDown();
107
[email protected]fd0a773a2013-04-30 20:55:03108 private:
109 scoped_ptr<base::MessageLoopForIO> message_loop_;
[email protected]22b42c52010-12-20 06:59:23110};
111
[email protected]5f6259d2012-05-25 02:56:25112const std::string IPCChannelPosixTest::GetChannelDirName() {
[email protected]6fa21d02011-11-04 00:14:16113#if defined(OS_ANDROID)
[email protected]6d4b67a2013-02-10 04:49:30114 base::FilePath tmp_dir;
[email protected]5f6259d2012-05-25 02:56:25115 PathService::Get(base::DIR_CACHE, &tmp_dir);
116 return tmp_dir.value();
[email protected]6fa21d02011-11-04 00:14:16117#else
[email protected]5f6259d2012-05-25 02:56:25118 return "/var/tmp";
[email protected]6fa21d02011-11-04 00:14:16119#endif
[email protected]5f6259d2012-05-25 02:56:25120}
121
122const std::string IPCChannelPosixTest::GetConnectionSocketName() {
123 return GetChannelDirName() + "/chrome_IPCChannelPosixTest__ConnectionSocket";
124}
[email protected]22b42c52010-12-20 06:59:23125
126void IPCChannelPosixTest::SetUp() {
127 MultiProcessTest::SetUp();
128 // Construct a fresh IO Message loop for the duration of each test.
[email protected]fd0a773a2013-04-30 20:55:03129 message_loop_.reset(new base::MessageLoopForIO());
[email protected]22b42c52010-12-20 06:59:23130}
131
132void IPCChannelPosixTest::TearDown() {
133 message_loop_.reset(NULL);
134 MultiProcessTest::TearDown();
135}
136
137// Create up a socket and bind and listen to it, or connect it
138// depending on the |mode|.
139void IPCChannelPosixTest::SetUpSocket(IPC::ChannelHandle *handle,
140 IPC::Channel::Mode mode) {
141 const std::string& name = handle->name;
142
143 int socket_fd = socket(PF_UNIX, SOCK_STREAM, 0);
144 ASSERT_GE(socket_fd, 0) << name;
145 ASSERT_GE(fcntl(socket_fd, F_SETFL, O_NONBLOCK), 0);
146 struct sockaddr_un server_address = { 0 };
147 memset(&server_address, 0, sizeof(server_address));
148 server_address.sun_family = AF_UNIX;
[email protected]bdf9bdc2013-03-13 04:23:10149 int path_len = snprintf(server_address.sun_path, IPC::kMaxSocketNameLength,
[email protected]22b42c52010-12-20 06:59:23150 "%s", name.c_str());
151 DCHECK_EQ(static_cast<int>(name.length()), path_len);
152 size_t server_address_len = offsetof(struct sockaddr_un,
153 sun_path) + path_len + 1;
154
155 if (mode == IPC::Channel::MODE_NAMED_SERVER) {
156 // Only one server at a time. Cleanup garbage if it exists.
157 unlink(name.c_str());
[email protected]d6431722011-09-01 00:46:33158 // Make sure the path we need exists.
[email protected]6d4b67a2013-02-10 04:49:30159 base::FilePath path(name);
160 base::FilePath dir_path = path.DirName();
[email protected]22b42c52010-12-20 06:59:23161 ASSERT_TRUE(file_util::CreateDirectory(dir_path));
162 ASSERT_GE(bind(socket_fd,
163 reinterpret_cast<struct sockaddr *>(&server_address),
164 server_address_len), 0) << server_address.sun_path
165 << ": " << strerror(errno)
166 << "(" << errno << ")";
167 ASSERT_GE(listen(socket_fd, SOMAXCONN), 0) << server_address.sun_path
168 << ": " << strerror(errno)
169 << "(" << errno << ")";
170 } else if (mode == IPC::Channel::MODE_NAMED_CLIENT) {
171 ASSERT_GE(connect(socket_fd,
172 reinterpret_cast<struct sockaddr *>(&server_address),
173 server_address_len), 0) << server_address.sun_path
174 << ": " << strerror(errno)
175 << "(" << errno << ")";
176 } else {
177 FAIL() << "Unknown mode " << mode;
178 }
179 handle->socket.fd = socket_fd;
180}
181
[email protected]0f2e45ec2012-07-11 15:41:43182void IPCChannelPosixTest::SpinRunLoop(base::TimeDelta delay) {
[email protected]fd0a773a2013-04-30 20:55:03183 base::MessageLoopForIO* loop = base::MessageLoopForIO::current();
[email protected]22b42c52010-12-20 06:59:23184 // Post a quit task so that this loop eventually ends and we don't hang
185 // in the case of a bad test. Usually, the run loop will quit sooner than
186 // that because all tests use a IPCChannelPosixTestListener which quits the
187 // current run loop on any channel activity.
[email protected]fd0a773a2013-04-30 20:55:03188 loop->PostDelayedTask(FROM_HERE, base::MessageLoop::QuitClosure(), delay);
[email protected]22b42c52010-12-20 06:59:23189 loop->Run();
190}
191
192TEST_F(IPCChannelPosixTest, BasicListen) {
[email protected]5f6259d2012-05-25 02:56:25193 const std::string kChannelName =
194 GetChannelDirName() + "/IPCChannelPosixTest_BasicListen";
[email protected]6fa21d02011-11-04 00:14:16195
[email protected]22b42c52010-12-20 06:59:23196 // Test creating a socket that is listening.
[email protected]6fa21d02011-11-04 00:14:16197 IPC::ChannelHandle handle(kChannelName);
[email protected]22b42c52010-12-20 06:59:23198 SetUpSocket(&handle, IPC::Channel::MODE_NAMED_SERVER);
199 unlink(handle.name.c_str());
200 IPC::Channel channel(handle, IPC::Channel::MODE_NAMED_SERVER, NULL);
201 ASSERT_TRUE(channel.Connect());
202 ASSERT_TRUE(channel.AcceptsConnections());
203 ASSERT_FALSE(channel.HasAcceptedConnection());
204 channel.ResetToAcceptingConnectionState();
205 ASSERT_FALSE(channel.HasAcceptedConnection());
206}
207
208TEST_F(IPCChannelPosixTest, BasicConnected) {
209 // Test creating a socket that is connected.
210 int pipe_fds[2];
211 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, pipe_fds));
212 std::string socket_name("/var/tmp/IPCChannelPosixTest_BasicConnected");
213 ASSERT_GE(fcntl(pipe_fds[0], F_SETFL, O_NONBLOCK), 0);
214
215 base::FileDescriptor fd(pipe_fds[0], false);
216 IPC::ChannelHandle handle(socket_name, fd);
217 IPC::Channel channel(handle, IPC::Channel::MODE_SERVER, NULL);
218 ASSERT_TRUE(channel.Connect());
219 ASSERT_FALSE(channel.AcceptsConnections());
220 channel.Close();
221 ASSERT_TRUE(HANDLE_EINTR(close(pipe_fds[1])) == 0);
222
223 // Make sure that we can use the socket that is created for us by
224 // a standard channel.
225 IPC::Channel channel2(socket_name, IPC::Channel::MODE_SERVER, NULL);
226 ASSERT_TRUE(channel2.Connect());
227 ASSERT_FALSE(channel2.AcceptsConnections());
228}
229
230TEST_F(IPCChannelPosixTest, AdvancedConnected) {
231 // Test creating a connection to an external process.
232 IPCChannelPosixTestListener listener(false);
[email protected]5f6259d2012-05-25 02:56:25233 IPC::ChannelHandle chan_handle(GetConnectionSocketName());
[email protected]22b42c52010-12-20 06:59:23234 SetUpSocket(&chan_handle, IPC::Channel::MODE_NAMED_SERVER);
235 IPC::Channel channel(chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener);
236 ASSERT_TRUE(channel.Connect());
237 ASSERT_TRUE(channel.AcceptsConnections());
238 ASSERT_FALSE(channel.HasAcceptedConnection());
239
240 base::ProcessHandle handle = SpawnChild("IPCChannelPosixTestConnectionProc",
241 false);
242 ASSERT_TRUE(handle);
[email protected]0f2e45ec2012-07-11 15:41:43243 SpinRunLoop(TestTimeouts::action_max_timeout());
[email protected]22b42c52010-12-20 06:59:23244 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status());
245 ASSERT_TRUE(channel.HasAcceptedConnection());
[email protected]d6431722011-09-01 00:46:33246 IPC::Message* message = new IPC::Message(0, // routing_id
247 kQuitMessage, // message type
[email protected]22b42c52010-12-20 06:59:23248 IPC::Message::PRIORITY_NORMAL);
249 channel.Send(message);
[email protected]0f2e45ec2012-07-11 15:41:43250 SpinRunLoop(TestTimeouts::action_timeout());
[email protected]22b42c52010-12-20 06:59:23251 int exit_code = 0;
252 EXPECT_TRUE(base::WaitForExitCode(handle, &exit_code));
253 EXPECT_EQ(0, exit_code);
254 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status());
255 ASSERT_FALSE(channel.HasAcceptedConnection());
256}
257
258TEST_F(IPCChannelPosixTest, ResetState) {
259 // Test creating a connection to an external process. Close the connection,
260 // but continue to listen and make sure another external process can connect
261 // to us.
262 IPCChannelPosixTestListener listener(false);
[email protected]5f6259d2012-05-25 02:56:25263 IPC::ChannelHandle chan_handle(GetConnectionSocketName());
[email protected]22b42c52010-12-20 06:59:23264 SetUpSocket(&chan_handle, IPC::Channel::MODE_NAMED_SERVER);
265 IPC::Channel channel(chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener);
266 ASSERT_TRUE(channel.Connect());
267 ASSERT_TRUE(channel.AcceptsConnections());
268 ASSERT_FALSE(channel.HasAcceptedConnection());
269
270 base::ProcessHandle handle = SpawnChild("IPCChannelPosixTestConnectionProc",
271 false);
272 ASSERT_TRUE(handle);
[email protected]0f2e45ec2012-07-11 15:41:43273 SpinRunLoop(TestTimeouts::action_max_timeout());
[email protected]22b42c52010-12-20 06:59:23274 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status());
275 ASSERT_TRUE(channel.HasAcceptedConnection());
276 channel.ResetToAcceptingConnectionState();
277 ASSERT_FALSE(channel.HasAcceptedConnection());
278
279 base::ProcessHandle handle2 = SpawnChild("IPCChannelPosixTestConnectionProc",
280 false);
281 ASSERT_TRUE(handle2);
[email protected]0f2e45ec2012-07-11 15:41:43282 SpinRunLoop(TestTimeouts::action_max_timeout());
[email protected]22b42c52010-12-20 06:59:23283 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status());
284 ASSERT_TRUE(channel.HasAcceptedConnection());
[email protected]d6431722011-09-01 00:46:33285 IPC::Message* message = new IPC::Message(0, // routing_id
286 kQuitMessage, // message type
[email protected]22b42c52010-12-20 06:59:23287 IPC::Message::PRIORITY_NORMAL);
288 channel.Send(message);
[email protected]0f2e45ec2012-07-11 15:41:43289 SpinRunLoop(TestTimeouts::action_timeout());
[email protected]22b42c52010-12-20 06:59:23290 EXPECT_TRUE(base::KillProcess(handle, 0, false));
291 int exit_code = 0;
292 EXPECT_TRUE(base::WaitForExitCode(handle2, &exit_code));
293 EXPECT_EQ(0, exit_code);
294 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status());
295 ASSERT_FALSE(channel.HasAcceptedConnection());
296}
297
[email protected]3c524252011-08-25 14:14:05298TEST_F(IPCChannelPosixTest, BadChannelName) {
299 // Test empty name
300 IPC::ChannelHandle handle("");
301 IPC::Channel channel(handle, IPC::Channel::MODE_NAMED_SERVER, NULL);
302 ASSERT_FALSE(channel.Connect());
303
304 // Test name that is too long.
305 const char *kTooLongName = "This_is_a_very_long_name_to_proactively_implement"
306 "client-centered_synergy_through_top-line"
307 "platforms_Phosfluorescently_disintermediate_"
308 "clicks-and-mortar_best_practices_without_"
309 "future-proof_growth_strategies_Continually"
310 "pontificate_proactive_potentialities_before"
311 "leading-edge_processes";
[email protected]bdf9bdc2013-03-13 04:23:10312 EXPECT_GE(strlen(kTooLongName), IPC::kMaxSocketNameLength);
[email protected]3c524252011-08-25 14:14:05313 IPC::ChannelHandle handle2(kTooLongName);
314 IPC::Channel channel2(handle2, IPC::Channel::MODE_NAMED_SERVER, NULL);
315 EXPECT_FALSE(channel2.Connect());
316}
317
[email protected]22b42c52010-12-20 06:59:23318TEST_F(IPCChannelPosixTest, MultiConnection) {
319 // Test setting up a connection to an external process, and then have
320 // another external process attempt to connect to us.
321 IPCChannelPosixTestListener listener(false);
[email protected]5f6259d2012-05-25 02:56:25322 IPC::ChannelHandle chan_handle(GetConnectionSocketName());
[email protected]22b42c52010-12-20 06:59:23323 SetUpSocket(&chan_handle, IPC::Channel::MODE_NAMED_SERVER);
324 IPC::Channel channel(chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener);
325 ASSERT_TRUE(channel.Connect());
326 ASSERT_TRUE(channel.AcceptsConnections());
327 ASSERT_FALSE(channel.HasAcceptedConnection());
328
329 base::ProcessHandle handle = SpawnChild("IPCChannelPosixTestConnectionProc",
330 false);
331 ASSERT_TRUE(handle);
[email protected]0f2e45ec2012-07-11 15:41:43332 SpinRunLoop(TestTimeouts::action_max_timeout());
[email protected]22b42c52010-12-20 06:59:23333 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status());
334 ASSERT_TRUE(channel.HasAcceptedConnection());
335 base::ProcessHandle handle2 = SpawnChild("IPCChannelPosixFailConnectionProc",
336 false);
337 ASSERT_TRUE(handle2);
[email protected]0f2e45ec2012-07-11 15:41:43338 SpinRunLoop(TestTimeouts::action_max_timeout());
[email protected]22b42c52010-12-20 06:59:23339 int exit_code = 0;
340 EXPECT_TRUE(base::WaitForExitCode(handle2, &exit_code));
341 EXPECT_EQ(exit_code, 0);
342 ASSERT_EQ(IPCChannelPosixTestListener::DENIED, listener.status());
343 ASSERT_TRUE(channel.HasAcceptedConnection());
[email protected]d6431722011-09-01 00:46:33344 IPC::Message* message = new IPC::Message(0, // routing_id
345 kQuitMessage, // message type
[email protected]22b42c52010-12-20 06:59:23346 IPC::Message::PRIORITY_NORMAL);
347 channel.Send(message);
[email protected]0f2e45ec2012-07-11 15:41:43348 SpinRunLoop(TestTimeouts::action_timeout());
[email protected]22b42c52010-12-20 06:59:23349 EXPECT_TRUE(base::WaitForExitCode(handle, &exit_code));
350 EXPECT_EQ(exit_code, 0);
351 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status());
352 ASSERT_FALSE(channel.HasAcceptedConnection());
353}
354
[email protected]6aad23f92011-03-02 22:27:14355TEST_F(IPCChannelPosixTest, DoubleServer) {
356 // Test setting up two servers with the same name.
357 IPCChannelPosixTestListener listener(false);
358 IPCChannelPosixTestListener listener2(false);
[email protected]5f6259d2012-05-25 02:56:25359 IPC::ChannelHandle chan_handle(GetConnectionSocketName());
[email protected]6aad23f92011-03-02 22:27:14360 IPC::Channel channel(chan_handle, IPC::Channel::MODE_SERVER, &listener);
361 IPC::Channel channel2(chan_handle, IPC::Channel::MODE_SERVER, &listener2);
362 ASSERT_TRUE(channel.Connect());
363 ASSERT_FALSE(channel2.Connect());
364}
365
366TEST_F(IPCChannelPosixTest, BadMode) {
367 // Test setting up two servers with a bad mode.
368 IPCChannelPosixTestListener listener(false);
[email protected]5f6259d2012-05-25 02:56:25369 IPC::ChannelHandle chan_handle(GetConnectionSocketName());
[email protected]6aad23f92011-03-02 22:27:14370 IPC::Channel channel(chan_handle, IPC::Channel::MODE_NONE, &listener);
371 ASSERT_FALSE(channel.Connect());
372}
373
[email protected]313c00e52011-08-09 06:46:06374TEST_F(IPCChannelPosixTest, IsNamedServerInitialized) {
[email protected]5f6259d2012-05-25 02:56:25375 const std::string& connection_socket_name = GetConnectionSocketName();
[email protected]313c00e52011-08-09 06:46:06376 IPCChannelPosixTestListener listener(false);
[email protected]5f6259d2012-05-25 02:56:25377 IPC::ChannelHandle chan_handle(connection_socket_name);
[email protected]6d4b67a2013-02-10 04:49:30378 ASSERT_TRUE(file_util::Delete(base::FilePath(connection_socket_name), false));
[email protected]313c00e52011-08-09 06:46:06379 ASSERT_FALSE(IPC::Channel::IsNamedServerInitialized(
[email protected]5f6259d2012-05-25 02:56:25380 connection_socket_name));
[email protected]313c00e52011-08-09 06:46:06381 IPC::Channel channel(chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener);
382 ASSERT_TRUE(IPC::Channel::IsNamedServerInitialized(
[email protected]5f6259d2012-05-25 02:56:25383 connection_socket_name));
[email protected]313c00e52011-08-09 06:46:06384 channel.Close();
385 ASSERT_FALSE(IPC::Channel::IsNamedServerInitialized(
[email protected]5f6259d2012-05-25 02:56:25386 connection_socket_name));
[email protected]313c00e52011-08-09 06:46:06387}
388
[email protected]22b42c52010-12-20 06:59:23389// A long running process that connects to us
390MULTIPROCESS_TEST_MAIN(IPCChannelPosixTestConnectionProc) {
[email protected]fd0a773a2013-04-30 20:55:03391 base::MessageLoopForIO message_loop;
[email protected]22b42c52010-12-20 06:59:23392 IPCChannelPosixTestListener listener(true);
[email protected]5f6259d2012-05-25 02:56:25393 IPC::ChannelHandle handle(IPCChannelPosixTest::GetConnectionSocketName());
[email protected]22b42c52010-12-20 06:59:23394 IPCChannelPosixTest::SetUpSocket(&handle, IPC::Channel::MODE_NAMED_CLIENT);
395 IPC::Channel channel(handle, IPC::Channel::MODE_NAMED_CLIENT, &listener);
396 EXPECT_TRUE(channel.Connect());
[email protected]0f2e45ec2012-07-11 15:41:43397 IPCChannelPosixTest::SpinRunLoop(TestTimeouts::action_max_timeout());
[email protected]22b42c52010-12-20 06:59:23398 EXPECT_EQ(IPCChannelPosixTestListener::MESSAGE_RECEIVED, listener.status());
399 return 0;
400}
401
402// Simple external process that shouldn't be able to connect to us.
403MULTIPROCESS_TEST_MAIN(IPCChannelPosixFailConnectionProc) {
[email protected]fd0a773a2013-04-30 20:55:03404 base::MessageLoopForIO message_loop;
[email protected]22b42c52010-12-20 06:59:23405 IPCChannelPosixTestListener listener(false);
[email protected]5f6259d2012-05-25 02:56:25406 IPC::ChannelHandle handle(IPCChannelPosixTest::GetConnectionSocketName());
[email protected]22b42c52010-12-20 06:59:23407 IPCChannelPosixTest::SetUpSocket(&handle, IPC::Channel::MODE_NAMED_CLIENT);
408 IPC::Channel channel(handle, IPC::Channel::MODE_NAMED_CLIENT, &listener);
409
410 // In this case connect may succeed or fail depending on if the packet
411 // actually gets sent at sendmsg. Since we never delay on send, we may not
412 // see the error. However even if connect succeeds, eventually we will get an
413 // error back since the channel will be closed when we attempt to read from
414 // it.
415 bool connected = channel.Connect();
416 if (connected) {
[email protected]0f2e45ec2012-07-11 15:41:43417 IPCChannelPosixTest::SpinRunLoop(TestTimeouts::action_max_timeout());
[email protected]22b42c52010-12-20 06:59:23418 EXPECT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status());
419 } else {
420 EXPECT_EQ(IPCChannelPosixTestListener::DISCONNECTED, listener.status());
421 }
422 return 0;
423}
[email protected]2a3aa7b52013-01-11 20:56:22424
425} // namespace