blob: 448e6485be3b0a6a9b02a20764a8369a11d07d94 [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"
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +090015#include "base/file_util.h"
brettw@chromium.org59eef1f2013-02-24 14:40:52 +090016#include "base/files/file_path.h"
levin@chromium.org5c528682011-03-28 10:54:15 +090017#include "base/memory/scoped_ptr.h"
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +090018#include "base/message_loop.h"
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +090019#include "base/path_service.h"
brettw@chromium.orgb1788fb2012-11-15 05:54:35 +090020#include "base/posix/eintr_wrapper.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
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +090095class IPCChannelPosixTest : public base::MultiProcessTest {
96 public:
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +090097 static void SetUpSocket(IPC::ChannelHandle *handle,
98 IPC::Channel::Mode mode);
tedvessenes@gmail.com5966b642012-07-12 00:41:43 +090099 static void SpinRunLoop(base::TimeDelta delay);
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900100 static const std::string GetConnectionSocketName();
101 static const std::string GetChannelDirName();
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900102
103 protected:
104 virtual void SetUp();
105 virtual void TearDown();
106
107private:
108 scoped_ptr<MessageLoopForIO> message_loop_;
109};
110
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900111const std::string IPCChannelPosixTest::GetChannelDirName() {
jrg@chromium.org2eb192e2011-11-04 09:14:16 +0900112#if defined(OS_ANDROID)
brettw@chromium.org22b3fda2013-02-10 13:49:30 +0900113 base::FilePath tmp_dir;
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900114 PathService::Get(base::DIR_CACHE, &tmp_dir);
115 return tmp_dir.value();
jrg@chromium.org2eb192e2011-11-04 09:14:16 +0900116#else
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900117 return "/var/tmp";
jrg@chromium.org2eb192e2011-11-04 09:14:16 +0900118#endif
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900119}
120
121const std::string IPCChannelPosixTest::GetConnectionSocketName() {
122 return GetChannelDirName() + "/chrome_IPCChannelPosixTest__ConnectionSocket";
123}
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900124
125void IPCChannelPosixTest::SetUp() {
126 MultiProcessTest::SetUp();
127 // Construct a fresh IO Message loop for the duration of each test.
128 message_loop_.reset(new MessageLoopForIO());
129}
130
131void IPCChannelPosixTest::TearDown() {
132 message_loop_.reset(NULL);
133 MultiProcessTest::TearDown();
134}
135
136// Create up a socket and bind and listen to it, or connect it
137// depending on the |mode|.
138void IPCChannelPosixTest::SetUpSocket(IPC::ChannelHandle *handle,
139 IPC::Channel::Mode mode) {
140 const std::string& name = handle->name;
141
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,
149 "%s", name.c_str());
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;
153
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());
pkasting@chromium.orgc4f2de22011-09-01 09:46:33 +0900157 // Make sure the path we need exists.
brettw@chromium.org22b3fda2013-02-10 13:49:30 +0900158 base::FilePath path(name);
159 base::FilePath dir_path = path.DirName();
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900160 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 << ")";
175 } else {
176 FAIL() << "Unknown mode " << mode;
177 }
178 handle->socket.fd = socket_fd;
179}
180
tedvessenes@gmail.com5966b642012-07-12 00:41:43 +0900181void IPCChannelPosixTest::SpinRunLoop(base::TimeDelta delay) {
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900182 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.
tedvessenes@gmail.com56b33702012-03-07 13:41:40 +0900187 loop->PostDelayedTask(
188 FROM_HERE,
189 MessageLoop::QuitClosure(),
tedvessenes@gmail.com5966b642012-07-12 00:41:43 +0900190 delay);
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900191 loop->Run();
192}
193
194TEST_F(IPCChannelPosixTest, BasicListen) {
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900195 const std::string kChannelName =
196 GetChannelDirName() + "/IPCChannelPosixTest_BasicListen";
jrg@chromium.org2eb192e2011-11-04 09:14:16 +0900197
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900198 // Test creating a socket that is listening.
jrg@chromium.org2eb192e2011-11-04 09:14:16 +0900199 IPC::ChannelHandle handle(kChannelName);
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900200 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());
208}
209
210TEST_F(IPCChannelPosixTest, BasicConnected) {
211 // Test creating a socket that is connected.
212 int pipe_fds[2];
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);
216
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());
222 channel.Close();
223 ASSERT_TRUE(HANDLE_EINTR(close(pipe_fds[1])) == 0);
224
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());
230}
231
232TEST_F(IPCChannelPosixTest, AdvancedConnected) {
233 // Test creating a connection to an external process.
234 IPCChannelPosixTestListener listener(false);
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900235 IPC::ChannelHandle chan_handle(GetConnectionSocketName());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900236 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());
241
242 base::ProcessHandle handle = SpawnChild("IPCChannelPosixTestConnectionProc",
243 false);
244 ASSERT_TRUE(handle);
tedvessenes@gmail.com5966b642012-07-12 00:41:43 +0900245 SpinRunLoop(TestTimeouts::action_max_timeout());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900246 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status());
247 ASSERT_TRUE(channel.HasAcceptedConnection());
pkasting@chromium.orgc4f2de22011-09-01 09:46:33 +0900248 IPC::Message* message = new IPC::Message(0, // routing_id
249 kQuitMessage, // message type
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900250 IPC::Message::PRIORITY_NORMAL);
251 channel.Send(message);
tedvessenes@gmail.com5966b642012-07-12 00:41:43 +0900252 SpinRunLoop(TestTimeouts::action_timeout());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900253 int exit_code = 0;
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());
258}
259
260TEST_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
263 // to us.
264 IPCChannelPosixTestListener listener(false);
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900265 IPC::ChannelHandle chan_handle(GetConnectionSocketName());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900266 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());
271
272 base::ProcessHandle handle = SpawnChild("IPCChannelPosixTestConnectionProc",
273 false);
274 ASSERT_TRUE(handle);
tedvessenes@gmail.com5966b642012-07-12 00:41:43 +0900275 SpinRunLoop(TestTimeouts::action_max_timeout());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900276 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status());
277 ASSERT_TRUE(channel.HasAcceptedConnection());
278 channel.ResetToAcceptingConnectionState();
279 ASSERT_FALSE(channel.HasAcceptedConnection());
280
281 base::ProcessHandle handle2 = SpawnChild("IPCChannelPosixTestConnectionProc",
282 false);
283 ASSERT_TRUE(handle2);
tedvessenes@gmail.com5966b642012-07-12 00:41:43 +0900284 SpinRunLoop(TestTimeouts::action_max_timeout());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900285 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status());
286 ASSERT_TRUE(channel.HasAcceptedConnection());
pkasting@chromium.orgc4f2de22011-09-01 09:46:33 +0900287 IPC::Message* message = new IPC::Message(0, // routing_id
288 kQuitMessage, // message type
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900289 IPC::Message::PRIORITY_NORMAL);
290 channel.Send(message);
tedvessenes@gmail.com5966b642012-07-12 00:41:43 +0900291 SpinRunLoop(TestTimeouts::action_timeout());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900292 EXPECT_TRUE(base::KillProcess(handle, 0, false));
293 int exit_code = 0;
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());
298}
299
dmaclach@chromium.org344a3d52011-08-25 23:14:05 +0900300TEST_F(IPCChannelPosixTest, BadChannelName) {
301 // Test empty name
302 IPC::ChannelHandle handle("");
303 IPC::Channel channel(handle, IPC::Channel::MODE_NAMED_SERVER, NULL);
304 ASSERT_FALSE(channel.Connect());
305
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());
318}
319
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900320TEST_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);
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900324 IPC::ChannelHandle chan_handle(GetConnectionSocketName());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900325 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());
330
331 base::ProcessHandle handle = SpawnChild("IPCChannelPosixTestConnectionProc",
332 false);
333 ASSERT_TRUE(handle);
tedvessenes@gmail.com5966b642012-07-12 00:41:43 +0900334 SpinRunLoop(TestTimeouts::action_max_timeout());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900335 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status());
336 ASSERT_TRUE(channel.HasAcceptedConnection());
337 base::ProcessHandle handle2 = SpawnChild("IPCChannelPosixFailConnectionProc",
338 false);
339 ASSERT_TRUE(handle2);
tedvessenes@gmail.com5966b642012-07-12 00:41:43 +0900340 SpinRunLoop(TestTimeouts::action_max_timeout());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900341 int exit_code = 0;
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());
pkasting@chromium.orgc4f2de22011-09-01 09:46:33 +0900346 IPC::Message* message = new IPC::Message(0, // routing_id
347 kQuitMessage, // message type
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900348 IPC::Message::PRIORITY_NORMAL);
349 channel.Send(message);
tedvessenes@gmail.com5966b642012-07-12 00:41:43 +0900350 SpinRunLoop(TestTimeouts::action_timeout());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900351 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());
355}
356
dmaclach@chromium.org2812db62011-03-03 07:27:14 +0900357TEST_F(IPCChannelPosixTest, DoubleServer) {
358 // Test setting up two servers with the same name.
359 IPCChannelPosixTestListener listener(false);
360 IPCChannelPosixTestListener listener2(false);
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900361 IPC::ChannelHandle chan_handle(GetConnectionSocketName());
dmaclach@chromium.org2812db62011-03-03 07:27:14 +0900362 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());
366}
367
368TEST_F(IPCChannelPosixTest, BadMode) {
369 // Test setting up two servers with a bad mode.
370 IPCChannelPosixTestListener listener(false);
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900371 IPC::ChannelHandle chan_handle(GetConnectionSocketName());
dmaclach@chromium.org2812db62011-03-03 07:27:14 +0900372 IPC::Channel channel(chan_handle, IPC::Channel::MODE_NONE, &listener);
373 ASSERT_FALSE(channel.Connect());
374}
375
kkania@chromium.orgf37b4e52011-08-09 15:46:06 +0900376TEST_F(IPCChannelPosixTest, IsNamedServerInitialized) {
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900377 const std::string& connection_socket_name = GetConnectionSocketName();
kkania@chromium.orgf37b4e52011-08-09 15:46:06 +0900378 IPCChannelPosixTestListener listener(false);
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900379 IPC::ChannelHandle chan_handle(connection_socket_name);
brettw@chromium.org22b3fda2013-02-10 13:49:30 +0900380 ASSERT_TRUE(file_util::Delete(base::FilePath(connection_socket_name), false));
kkania@chromium.orgf37b4e52011-08-09 15:46:06 +0900381 ASSERT_FALSE(IPC::Channel::IsNamedServerInitialized(
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900382 connection_socket_name));
kkania@chromium.orgf37b4e52011-08-09 15:46:06 +0900383 IPC::Channel channel(chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener);
384 ASSERT_TRUE(IPC::Channel::IsNamedServerInitialized(
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900385 connection_socket_name));
kkania@chromium.orgf37b4e52011-08-09 15:46:06 +0900386 channel.Close();
387 ASSERT_FALSE(IPC::Channel::IsNamedServerInitialized(
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900388 connection_socket_name));
kkania@chromium.orgf37b4e52011-08-09 15:46:06 +0900389}
390
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900391// A long running process that connects to us
392MULTIPROCESS_TEST_MAIN(IPCChannelPosixTestConnectionProc) {
393 MessageLoopForIO message_loop;
394 IPCChannelPosixTestListener listener(true);
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900395 IPC::ChannelHandle handle(IPCChannelPosixTest::GetConnectionSocketName());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900396 IPCChannelPosixTest::SetUpSocket(&handle, IPC::Channel::MODE_NAMED_CLIENT);
397 IPC::Channel channel(handle, IPC::Channel::MODE_NAMED_CLIENT, &listener);
398 EXPECT_TRUE(channel.Connect());
tedvessenes@gmail.com5966b642012-07-12 00:41:43 +0900399 IPCChannelPosixTest::SpinRunLoop(TestTimeouts::action_max_timeout());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900400 EXPECT_EQ(IPCChannelPosixTestListener::MESSAGE_RECEIVED, listener.status());
401 return 0;
402}
403
404// Simple external process that shouldn't be able to connect to us.
405MULTIPROCESS_TEST_MAIN(IPCChannelPosixFailConnectionProc) {
406 MessageLoopForIO message_loop;
407 IPCChannelPosixTestListener listener(false);
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900408 IPC::ChannelHandle handle(IPCChannelPosixTest::GetConnectionSocketName());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900409 IPCChannelPosixTest::SetUpSocket(&handle, IPC::Channel::MODE_NAMED_CLIENT);
410 IPC::Channel channel(handle, IPC::Channel::MODE_NAMED_CLIENT, &listener);
411
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
416 // it.
417 bool connected = channel.Connect();
418 if (connected) {
tedvessenes@gmail.com5966b642012-07-12 00:41:43 +0900419 IPCChannelPosixTest::SpinRunLoop(TestTimeouts::action_max_timeout());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900420 EXPECT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status());
421 } else {
422 EXPECT_EQ(IPCChannelPosixTestListener::DISCONNECTED, listener.status());
423 }
424 return 0;
425}
viettrungluu@chromium.org7ca19132013-01-12 05:56:22 +0900426
427} // namespace