blob: 66ddeb2b49676444245ceab4e185a0dd84e926b8 [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"
avi@chromium.orga29af562013-07-18 08:00:30 +090018#include "base/message_loop/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"
rsesek@chromium.org19319712013-07-24 14:15:24 +090021#include "base/process/kill.h"
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +090022#include "base/test/multiprocess_test.h"
23#include "base/test/test_timeouts.h"
brettw@chromium.orgf947ed02012-06-12 07:35:26 +090024#include "ipc/ipc_listener.h"
jeremya@chromium.orgf14bfab2013-03-13 13:23:10 +090025#include "ipc/unix_domain_socket_util.h"
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +090026#include "testing/multiprocess_func_list.h"
27
28namespace {
29
pkasting@chromium.orgc4f2de22011-09-01 09:46:33 +090030static const uint32 kQuitMessage = 47;
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +090031
brettw@chromium.orgf947ed02012-06-12 07:35:26 +090032class IPCChannelPosixTestListener : public IPC::Listener {
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +090033 public:
34 enum STATUS {
35 DISCONNECTED,
36 MESSAGE_RECEIVED,
37 CHANNEL_ERROR,
38 CONNECTED,
39 DENIED,
40 LISTEN_ERROR
41 };
42
43 IPCChannelPosixTestListener(bool quit_only_on_message)
44 : status_(DISCONNECTED), quit_only_on_message_(quit_only_on_message) {}
45
46 virtual ~IPCChannelPosixTestListener() {}
47
evan@chromium.orgecc3f072011-08-17 06:09:25 +090048 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE {
pkasting@chromium.orgc4f2de22011-09-01 09:46:33 +090049 EXPECT_EQ(message.type(), kQuitMessage);
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +090050 status_ = MESSAGE_RECEIVED;
51 QuitRunLoop();
jam@chromium.org8a2c7842010-12-24 15:19:28 +090052 return true;
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +090053 }
54
evan@chromium.orgecc3f072011-08-17 06:09:25 +090055 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE {
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +090056 status_ = CONNECTED;
57 if (!quit_only_on_message_) {
58 QuitRunLoop();
59 }
60 }
61
evan@chromium.orgecc3f072011-08-17 06:09:25 +090062 virtual void OnChannelError() OVERRIDE {
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +090063 status_ = CHANNEL_ERROR;
64 if (!quit_only_on_message_) {
65 QuitRunLoop();
66 }
67 }
68
evan@chromium.orgecc3f072011-08-17 06:09:25 +090069 virtual void OnChannelDenied() OVERRIDE {
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +090070 status_ = DENIED;
71 if (!quit_only_on_message_) {
72 QuitRunLoop();
73 }
74 }
75
evan@chromium.orgecc3f072011-08-17 06:09:25 +090076 virtual void OnChannelListenError() OVERRIDE {
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +090077 status_ = LISTEN_ERROR;
78 if (!quit_only_on_message_) {
79 QuitRunLoop();
80 }
81 }
82
83 STATUS status() { return status_; }
84
85 void QuitRunLoop() {
xhwang@chromium.org0b2c2a52013-05-01 05:55:03 +090086 base::MessageLoopForIO::current()->QuitNow();
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +090087 }
88
89 private:
90 // The current status of the listener.
91 STATUS status_;
92 // If |quit_only_on_message_| then the listener will only break out of
pkasting@chromium.orgc4f2de22011-09-01 09:46:33 +090093 // the run loop when kQuitMessage is received.
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +090094 bool quit_only_on_message_;
95};
96
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +090097class 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);
tedvessenes@gmail.com5966b642012-07-12 00:41:43 +0900101 static void SpinRunLoop(base::TimeDelta delay);
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
xhwang@chromium.org0b2c2a52013-05-01 05:55:03 +0900109 private:
110 scoped_ptr<base::MessageLoopForIO> message_loop_;
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900111};
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)
brettw@chromium.org22b3fda2013-02-10 13:49:30 +0900115 base::FilePath tmp_dir;
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900116 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.
xhwang@chromium.org0b2c2a52013-05-01 05:55:03 +0900130 message_loop_.reset(new base::MessageLoopForIO());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900131}
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;
jeremya@chromium.orgf14bfab2013-03-13 13:23:10 +0900150 int path_len = snprintf(server_address.sun_path, IPC::kMaxSocketNameLength,
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900151 "%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.
brettw@chromium.org22b3fda2013-02-10 13:49:30 +0900160 base::FilePath path(name);
161 base::FilePath dir_path = path.DirName();
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900162 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
tedvessenes@gmail.com5966b642012-07-12 00:41:43 +0900183void IPCChannelPosixTest::SpinRunLoop(base::TimeDelta delay) {
xhwang@chromium.org0b2c2a52013-05-01 05:55:03 +0900184 base::MessageLoopForIO* loop = base::MessageLoopForIO::current();
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900185 // 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.
xhwang@chromium.org0b2c2a52013-05-01 05:55:03 +0900189 loop->PostDelayedTask(FROM_HERE, base::MessageLoop::QuitClosure(), delay);
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900190 loop->Run();
191}
192
193TEST_F(IPCChannelPosixTest, BasicListen) {
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900194 const std::string kChannelName =
195 GetChannelDirName() + "/IPCChannelPosixTest_BasicListen";
jrg@chromium.org2eb192e2011-11-04 09:14:16 +0900196
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900197 // Test creating a socket that is listening.
jrg@chromium.org2eb192e2011-11-04 09:14:16 +0900198 IPC::ChannelHandle handle(kChannelName);
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900199 SetUpSocket(&handle, IPC::Channel::MODE_NAMED_SERVER);
200 unlink(handle.name.c_str());
201 IPC::Channel channel(handle, IPC::Channel::MODE_NAMED_SERVER, NULL);
202 ASSERT_TRUE(channel.Connect());
203 ASSERT_TRUE(channel.AcceptsConnections());
204 ASSERT_FALSE(channel.HasAcceptedConnection());
205 channel.ResetToAcceptingConnectionState();
206 ASSERT_FALSE(channel.HasAcceptedConnection());
207}
208
209TEST_F(IPCChannelPosixTest, BasicConnected) {
210 // Test creating a socket that is connected.
211 int pipe_fds[2];
212 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, pipe_fds));
213 std::string socket_name("/var/tmp/IPCChannelPosixTest_BasicConnected");
214 ASSERT_GE(fcntl(pipe_fds[0], F_SETFL, O_NONBLOCK), 0);
215
216 base::FileDescriptor fd(pipe_fds[0], false);
217 IPC::ChannelHandle handle(socket_name, fd);
218 IPC::Channel channel(handle, IPC::Channel::MODE_SERVER, NULL);
219 ASSERT_TRUE(channel.Connect());
220 ASSERT_FALSE(channel.AcceptsConnections());
221 channel.Close();
222 ASSERT_TRUE(HANDLE_EINTR(close(pipe_fds[1])) == 0);
223
224 // Make sure that we can use the socket that is created for us by
225 // a standard channel.
226 IPC::Channel channel2(socket_name, IPC::Channel::MODE_SERVER, NULL);
227 ASSERT_TRUE(channel2.Connect());
228 ASSERT_FALSE(channel2.AcceptsConnections());
229}
230
231TEST_F(IPCChannelPosixTest, AdvancedConnected) {
232 // Test creating a connection to an external process.
233 IPCChannelPosixTestListener listener(false);
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900234 IPC::ChannelHandle chan_handle(GetConnectionSocketName());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900235 SetUpSocket(&chan_handle, IPC::Channel::MODE_NAMED_SERVER);
236 IPC::Channel channel(chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener);
237 ASSERT_TRUE(channel.Connect());
238 ASSERT_TRUE(channel.AcceptsConnections());
239 ASSERT_FALSE(channel.HasAcceptedConnection());
240
241 base::ProcessHandle handle = SpawnChild("IPCChannelPosixTestConnectionProc",
242 false);
243 ASSERT_TRUE(handle);
tedvessenes@gmail.com5966b642012-07-12 00:41:43 +0900244 SpinRunLoop(TestTimeouts::action_max_timeout());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900245 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status());
246 ASSERT_TRUE(channel.HasAcceptedConnection());
bbudge@chromium.orgab4c6bc2013-11-05 07:28:12 +0900247 IPC::Message* message = new IPC::Message(0, // routing_id
248 kQuitMessage, // message type
249 IPC::Message::PRIORITY_NORMAL);
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900250 channel.Send(message);
tedvessenes@gmail.com5966b642012-07-12 00:41:43 +0900251 SpinRunLoop(TestTimeouts::action_timeout());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900252 int exit_code = 0;
253 EXPECT_TRUE(base::WaitForExitCode(handle, &exit_code));
254 EXPECT_EQ(0, exit_code);
255 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status());
256 ASSERT_FALSE(channel.HasAcceptedConnection());
257}
258
259TEST_F(IPCChannelPosixTest, ResetState) {
260 // Test creating a connection to an external process. Close the connection,
261 // but continue to listen and make sure another external process can connect
262 // to us.
263 IPCChannelPosixTestListener listener(false);
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900264 IPC::ChannelHandle chan_handle(GetConnectionSocketName());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900265 SetUpSocket(&chan_handle, IPC::Channel::MODE_NAMED_SERVER);
266 IPC::Channel channel(chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener);
267 ASSERT_TRUE(channel.Connect());
268 ASSERT_TRUE(channel.AcceptsConnections());
269 ASSERT_FALSE(channel.HasAcceptedConnection());
270
271 base::ProcessHandle handle = SpawnChild("IPCChannelPosixTestConnectionProc",
272 false);
273 ASSERT_TRUE(handle);
tedvessenes@gmail.com5966b642012-07-12 00:41:43 +0900274 SpinRunLoop(TestTimeouts::action_max_timeout());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900275 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status());
276 ASSERT_TRUE(channel.HasAcceptedConnection());
277 channel.ResetToAcceptingConnectionState();
278 ASSERT_FALSE(channel.HasAcceptedConnection());
279
280 base::ProcessHandle handle2 = SpawnChild("IPCChannelPosixTestConnectionProc",
281 false);
282 ASSERT_TRUE(handle2);
tedvessenes@gmail.com5966b642012-07-12 00:41:43 +0900283 SpinRunLoop(TestTimeouts::action_max_timeout());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900284 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status());
285 ASSERT_TRUE(channel.HasAcceptedConnection());
bbudge@chromium.orgab4c6bc2013-11-05 07:28:12 +0900286 IPC::Message* message = new IPC::Message(0, // routing_id
287 kQuitMessage, // message type
288 IPC::Message::PRIORITY_NORMAL);
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900289 channel.Send(message);
tedvessenes@gmail.com5966b642012-07-12 00:41:43 +0900290 SpinRunLoop(TestTimeouts::action_timeout());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900291 EXPECT_TRUE(base::KillProcess(handle, 0, false));
292 int exit_code = 0;
293 EXPECT_TRUE(base::WaitForExitCode(handle2, &exit_code));
294 EXPECT_EQ(0, exit_code);
295 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status());
296 ASSERT_FALSE(channel.HasAcceptedConnection());
297}
298
dmaclach@chromium.org344a3d52011-08-25 23:14:05 +0900299TEST_F(IPCChannelPosixTest, BadChannelName) {
300 // Test empty name
301 IPC::ChannelHandle handle("");
302 IPC::Channel channel(handle, IPC::Channel::MODE_NAMED_SERVER, NULL);
303 ASSERT_FALSE(channel.Connect());
304
305 // Test name that is too long.
306 const char *kTooLongName = "This_is_a_very_long_name_to_proactively_implement"
307 "client-centered_synergy_through_top-line"
308 "platforms_Phosfluorescently_disintermediate_"
309 "clicks-and-mortar_best_practices_without_"
310 "future-proof_growth_strategies_Continually"
311 "pontificate_proactive_potentialities_before"
312 "leading-edge_processes";
jeremya@chromium.orgf14bfab2013-03-13 13:23:10 +0900313 EXPECT_GE(strlen(kTooLongName), IPC::kMaxSocketNameLength);
dmaclach@chromium.org344a3d52011-08-25 23:14:05 +0900314 IPC::ChannelHandle handle2(kTooLongName);
315 IPC::Channel channel2(handle2, IPC::Channel::MODE_NAMED_SERVER, NULL);
316 EXPECT_FALSE(channel2.Connect());
317}
318
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900319TEST_F(IPCChannelPosixTest, MultiConnection) {
320 // Test setting up a connection to an external process, and then have
321 // another external process attempt to connect to us.
322 IPCChannelPosixTestListener listener(false);
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900323 IPC::ChannelHandle chan_handle(GetConnectionSocketName());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900324 SetUpSocket(&chan_handle, IPC::Channel::MODE_NAMED_SERVER);
325 IPC::Channel channel(chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener);
326 ASSERT_TRUE(channel.Connect());
327 ASSERT_TRUE(channel.AcceptsConnections());
328 ASSERT_FALSE(channel.HasAcceptedConnection());
329
330 base::ProcessHandle handle = SpawnChild("IPCChannelPosixTestConnectionProc",
331 false);
332 ASSERT_TRUE(handle);
tedvessenes@gmail.com5966b642012-07-12 00:41:43 +0900333 SpinRunLoop(TestTimeouts::action_max_timeout());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900334 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status());
335 ASSERT_TRUE(channel.HasAcceptedConnection());
336 base::ProcessHandle handle2 = SpawnChild("IPCChannelPosixFailConnectionProc",
337 false);
338 ASSERT_TRUE(handle2);
tedvessenes@gmail.com5966b642012-07-12 00:41:43 +0900339 SpinRunLoop(TestTimeouts::action_max_timeout());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900340 int exit_code = 0;
341 EXPECT_TRUE(base::WaitForExitCode(handle2, &exit_code));
342 EXPECT_EQ(exit_code, 0);
343 ASSERT_EQ(IPCChannelPosixTestListener::DENIED, listener.status());
344 ASSERT_TRUE(channel.HasAcceptedConnection());
bbudge@chromium.orgab4c6bc2013-11-05 07:28:12 +0900345 IPC::Message* message = new IPC::Message(0, // routing_id
346 kQuitMessage, // message type
347 IPC::Message::PRIORITY_NORMAL);
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900348 channel.Send(message);
tedvessenes@gmail.com5966b642012-07-12 00:41:43 +0900349 SpinRunLoop(TestTimeouts::action_timeout());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900350 EXPECT_TRUE(base::WaitForExitCode(handle, &exit_code));
351 EXPECT_EQ(exit_code, 0);
352 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status());
353 ASSERT_FALSE(channel.HasAcceptedConnection());
354}
355
dmaclach@chromium.org2812db62011-03-03 07:27:14 +0900356TEST_F(IPCChannelPosixTest, DoubleServer) {
357 // Test setting up two servers with the same name.
358 IPCChannelPosixTestListener listener(false);
359 IPCChannelPosixTestListener listener2(false);
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900360 IPC::ChannelHandle chan_handle(GetConnectionSocketName());
dmaclach@chromium.org2812db62011-03-03 07:27:14 +0900361 IPC::Channel channel(chan_handle, IPC::Channel::MODE_SERVER, &listener);
362 IPC::Channel channel2(chan_handle, IPC::Channel::MODE_SERVER, &listener2);
363 ASSERT_TRUE(channel.Connect());
364 ASSERT_FALSE(channel2.Connect());
365}
366
367TEST_F(IPCChannelPosixTest, BadMode) {
368 // Test setting up two servers with a bad mode.
369 IPCChannelPosixTestListener listener(false);
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900370 IPC::ChannelHandle chan_handle(GetConnectionSocketName());
dmaclach@chromium.org2812db62011-03-03 07:27:14 +0900371 IPC::Channel channel(chan_handle, IPC::Channel::MODE_NONE, &listener);
372 ASSERT_FALSE(channel.Connect());
373}
374
kkania@chromium.orgf37b4e52011-08-09 15:46:06 +0900375TEST_F(IPCChannelPosixTest, IsNamedServerInitialized) {
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900376 const std::string& connection_socket_name = GetConnectionSocketName();
kkania@chromium.orgf37b4e52011-08-09 15:46:06 +0900377 IPCChannelPosixTestListener listener(false);
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900378 IPC::ChannelHandle chan_handle(connection_socket_name);
brettw@chromium.org220b8de2013-07-17 04:10:23 +0900379 ASSERT_TRUE(base::DeleteFile(base::FilePath(connection_socket_name), false));
kkania@chromium.orgf37b4e52011-08-09 15:46:06 +0900380 ASSERT_FALSE(IPC::Channel::IsNamedServerInitialized(
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900381 connection_socket_name));
kkania@chromium.orgf37b4e52011-08-09 15:46:06 +0900382 IPC::Channel channel(chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener);
383 ASSERT_TRUE(IPC::Channel::IsNamedServerInitialized(
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900384 connection_socket_name));
kkania@chromium.orgf37b4e52011-08-09 15:46:06 +0900385 channel.Close();
386 ASSERT_FALSE(IPC::Channel::IsNamedServerInitialized(
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900387 connection_socket_name));
kkania@chromium.orgf37b4e52011-08-09 15:46:06 +0900388}
389
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900390// A long running process that connects to us
391MULTIPROCESS_TEST_MAIN(IPCChannelPosixTestConnectionProc) {
xhwang@chromium.org0b2c2a52013-05-01 05:55:03 +0900392 base::MessageLoopForIO message_loop;
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900393 IPCChannelPosixTestListener listener(true);
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900394 IPC::ChannelHandle handle(IPCChannelPosixTest::GetConnectionSocketName());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900395 IPCChannelPosixTest::SetUpSocket(&handle, IPC::Channel::MODE_NAMED_CLIENT);
396 IPC::Channel channel(handle, IPC::Channel::MODE_NAMED_CLIENT, &listener);
397 EXPECT_TRUE(channel.Connect());
tedvessenes@gmail.com5966b642012-07-12 00:41:43 +0900398 IPCChannelPosixTest::SpinRunLoop(TestTimeouts::action_max_timeout());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900399 EXPECT_EQ(IPCChannelPosixTestListener::MESSAGE_RECEIVED, listener.status());
400 return 0;
401}
402
403// Simple external process that shouldn't be able to connect to us.
404MULTIPROCESS_TEST_MAIN(IPCChannelPosixFailConnectionProc) {
xhwang@chromium.org0b2c2a52013-05-01 05:55:03 +0900405 base::MessageLoopForIO message_loop;
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900406 IPCChannelPosixTestListener listener(false);
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900407 IPC::ChannelHandle handle(IPCChannelPosixTest::GetConnectionSocketName());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900408 IPCChannelPosixTest::SetUpSocket(&handle, IPC::Channel::MODE_NAMED_CLIENT);
409 IPC::Channel channel(handle, IPC::Channel::MODE_NAMED_CLIENT, &listener);
410
411 // In this case connect may succeed or fail depending on if the packet
412 // actually gets sent at sendmsg. Since we never delay on send, we may not
413 // see the error. However even if connect succeeds, eventually we will get an
414 // error back since the channel will be closed when we attempt to read from
415 // it.
416 bool connected = channel.Connect();
417 if (connected) {
tedvessenes@gmail.com5966b642012-07-12 00:41:43 +0900418 IPCChannelPosixTest::SpinRunLoop(TestTimeouts::action_max_timeout());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900419 EXPECT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status());
420 } else {
421 EXPECT_EQ(IPCChannelPosixTestListener::DISCONNECTED, listener.status());
422 }
423 return 0;
424}
viettrungluu@chromium.org7ca19132013-01-12 05:56:22 +0900425
426} // namespace