blob: a4b4f8dbb9535ae510f226a4b7727f3b82af6604 [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)
hubbe@chromium.orga53c97c2014-02-21 09:18:29 +090044 : status_(DISCONNECTED),
45 quit_only_on_message_(quit_only_on_message) {
46 }
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +090047
48 virtual ~IPCChannelPosixTestListener() {}
49
evan@chromium.orgecc3f072011-08-17 06:09:25 +090050 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE {
pkasting@chromium.orgc4f2de22011-09-01 09:46:33 +090051 EXPECT_EQ(message.type(), kQuitMessage);
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +090052 status_ = MESSAGE_RECEIVED;
53 QuitRunLoop();
jam@chromium.org8a2c7842010-12-24 15:19:28 +090054 return true;
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +090055 }
56
evan@chromium.orgecc3f072011-08-17 06:09:25 +090057 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE {
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +090058 status_ = CONNECTED;
59 if (!quit_only_on_message_) {
60 QuitRunLoop();
61 }
62 }
63
evan@chromium.orgecc3f072011-08-17 06:09:25 +090064 virtual void OnChannelError() OVERRIDE {
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +090065 status_ = CHANNEL_ERROR;
hubbe@chromium.orga53c97c2014-02-21 09:18:29 +090066 QuitRunLoop();
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +090067 }
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() {
hubbe@chromium.orga53c97c2014-02-21 09:18:29 +090086 base::MessageLoopForIO* loop = base::MessageLoopForIO::current();
87 if (loop->is_running()) {
88 loop->QuitNow();
89 } else {
90 // Die as soon as Run is called.
91 loop->PostTask(FROM_HERE, loop->QuitClosure());
92 }
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +090093 }
94
95 private:
96 // The current status of the listener.
97 STATUS status_;
98 // If |quit_only_on_message_| then the listener will only break out of
pkasting@chromium.orgc4f2de22011-09-01 09:46:33 +090099 // the run loop when kQuitMessage is received.
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900100 bool quit_only_on_message_;
101};
102
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900103class IPCChannelPosixTest : public base::MultiProcessTest {
104 public:
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900105 static void SetUpSocket(IPC::ChannelHandle *handle,
106 IPC::Channel::Mode mode);
tedvessenes@gmail.com5966b642012-07-12 00:41:43 +0900107 static void SpinRunLoop(base::TimeDelta delay);
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900108 static const std::string GetConnectionSocketName();
109 static const std::string GetChannelDirName();
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900110
111 protected:
112 virtual void SetUp();
113 virtual void TearDown();
114
xhwang@chromium.org0b2c2a52013-05-01 05:55:03 +0900115 private:
116 scoped_ptr<base::MessageLoopForIO> message_loop_;
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900117};
118
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900119const std::string IPCChannelPosixTest::GetChannelDirName() {
jrg@chromium.org2eb192e2011-11-04 09:14:16 +0900120#if defined(OS_ANDROID)
brettw@chromium.org22b3fda2013-02-10 13:49:30 +0900121 base::FilePath tmp_dir;
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900122 PathService::Get(base::DIR_CACHE, &tmp_dir);
123 return tmp_dir.value();
jrg@chromium.org2eb192e2011-11-04 09:14:16 +0900124#else
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900125 return "/var/tmp";
jrg@chromium.org2eb192e2011-11-04 09:14:16 +0900126#endif
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900127}
128
129const std::string IPCChannelPosixTest::GetConnectionSocketName() {
130 return GetChannelDirName() + "/chrome_IPCChannelPosixTest__ConnectionSocket";
131}
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900132
133void IPCChannelPosixTest::SetUp() {
134 MultiProcessTest::SetUp();
135 // Construct a fresh IO Message loop for the duration of each test.
xhwang@chromium.org0b2c2a52013-05-01 05:55:03 +0900136 message_loop_.reset(new base::MessageLoopForIO());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900137}
138
139void IPCChannelPosixTest::TearDown() {
140 message_loop_.reset(NULL);
141 MultiProcessTest::TearDown();
142}
143
144// Create up a socket and bind and listen to it, or connect it
145// depending on the |mode|.
146void IPCChannelPosixTest::SetUpSocket(IPC::ChannelHandle *handle,
147 IPC::Channel::Mode mode) {
148 const std::string& name = handle->name;
149
150 int socket_fd = socket(PF_UNIX, SOCK_STREAM, 0);
151 ASSERT_GE(socket_fd, 0) << name;
152 ASSERT_GE(fcntl(socket_fd, F_SETFL, O_NONBLOCK), 0);
153 struct sockaddr_un server_address = { 0 };
154 memset(&server_address, 0, sizeof(server_address));
155 server_address.sun_family = AF_UNIX;
jeremya@chromium.orgf14bfab2013-03-13 13:23:10 +0900156 int path_len = snprintf(server_address.sun_path, IPC::kMaxSocketNameLength,
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900157 "%s", name.c_str());
158 DCHECK_EQ(static_cast<int>(name.length()), path_len);
159 size_t server_address_len = offsetof(struct sockaddr_un,
160 sun_path) + path_len + 1;
161
162 if (mode == IPC::Channel::MODE_NAMED_SERVER) {
163 // Only one server at a time. Cleanup garbage if it exists.
164 unlink(name.c_str());
pkasting@chromium.orgc4f2de22011-09-01 09:46:33 +0900165 // Make sure the path we need exists.
brettw@chromium.org22b3fda2013-02-10 13:49:30 +0900166 base::FilePath path(name);
167 base::FilePath dir_path = path.DirName();
brettw@chromium.org738669a2013-12-04 05:08:54 +0900168 ASSERT_TRUE(base::CreateDirectory(dir_path));
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900169 ASSERT_GE(bind(socket_fd,
170 reinterpret_cast<struct sockaddr *>(&server_address),
171 server_address_len), 0) << server_address.sun_path
172 << ": " << strerror(errno)
173 << "(" << errno << ")";
174 ASSERT_GE(listen(socket_fd, SOMAXCONN), 0) << server_address.sun_path
175 << ": " << strerror(errno)
176 << "(" << errno << ")";
177 } else if (mode == IPC::Channel::MODE_NAMED_CLIENT) {
178 ASSERT_GE(connect(socket_fd,
179 reinterpret_cast<struct sockaddr *>(&server_address),
180 server_address_len), 0) << server_address.sun_path
181 << ": " << strerror(errno)
182 << "(" << errno << ")";
183 } else {
184 FAIL() << "Unknown mode " << mode;
185 }
186 handle->socket.fd = socket_fd;
187}
188
tedvessenes@gmail.com5966b642012-07-12 00:41:43 +0900189void IPCChannelPosixTest::SpinRunLoop(base::TimeDelta delay) {
xhwang@chromium.org0b2c2a52013-05-01 05:55:03 +0900190 base::MessageLoopForIO* loop = base::MessageLoopForIO::current();
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900191 // Post a quit task so that this loop eventually ends and we don't hang
192 // in the case of a bad test. Usually, the run loop will quit sooner than
193 // that because all tests use a IPCChannelPosixTestListener which quits the
194 // current run loop on any channel activity.
hubbe@chromium.orga53c97c2014-02-21 09:18:29 +0900195 loop->PostDelayedTask(FROM_HERE, loop->QuitClosure(), delay);
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900196 loop->Run();
197}
198
199TEST_F(IPCChannelPosixTest, BasicListen) {
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900200 const std::string kChannelName =
201 GetChannelDirName() + "/IPCChannelPosixTest_BasicListen";
jrg@chromium.org2eb192e2011-11-04 09:14:16 +0900202
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900203 // Test creating a socket that is listening.
jrg@chromium.org2eb192e2011-11-04 09:14:16 +0900204 IPC::ChannelHandle handle(kChannelName);
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900205 SetUpSocket(&handle, IPC::Channel::MODE_NAMED_SERVER);
206 unlink(handle.name.c_str());
morrita@chromium.org844f1c32014-06-07 15:15:53 +0900207 scoped_ptr<IPC::ChannelPosix> channel(
208 new IPC::ChannelPosix(handle, IPC::Channel::MODE_NAMED_SERVER, NULL));
morrita@chromium.org2ced0042014-05-30 12:58:59 +0900209 ASSERT_TRUE(channel->Connect());
210 ASSERT_TRUE(channel->AcceptsConnections());
211 ASSERT_FALSE(channel->HasAcceptedConnection());
212 channel->ResetToAcceptingConnectionState();
213 ASSERT_FALSE(channel->HasAcceptedConnection());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900214}
215
216TEST_F(IPCChannelPosixTest, BasicConnected) {
217 // Test creating a socket that is connected.
218 int pipe_fds[2];
219 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, pipe_fds));
220 std::string socket_name("/var/tmp/IPCChannelPosixTest_BasicConnected");
221 ASSERT_GE(fcntl(pipe_fds[0], F_SETFL, O_NONBLOCK), 0);
222
223 base::FileDescriptor fd(pipe_fds[0], false);
224 IPC::ChannelHandle handle(socket_name, fd);
morrita@chromium.org844f1c32014-06-07 15:15:53 +0900225 scoped_ptr<IPC::ChannelPosix> channel(new IPC::ChannelPosix(
226 handle, IPC::Channel::MODE_SERVER, NULL));
morrita@chromium.org2ced0042014-05-30 12:58:59 +0900227 ASSERT_TRUE(channel->Connect());
228 ASSERT_FALSE(channel->AcceptsConnections());
229 channel->Close();
mark@chromium.orgfa5a0f92013-12-03 23:10:59 +0900230 ASSERT_TRUE(IGNORE_EINTR(close(pipe_fds[1])) == 0);
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900231
232 // Make sure that we can use the socket that is created for us by
233 // a standard channel.
morrita@chromium.org844f1c32014-06-07 15:15:53 +0900234 scoped_ptr<IPC::ChannelPosix> channel2(new IPC::ChannelPosix(
235 socket_name, IPC::Channel::MODE_SERVER, NULL));
morrita@chromium.org2ced0042014-05-30 12:58:59 +0900236 ASSERT_TRUE(channel2->Connect());
237 ASSERT_FALSE(channel2->AcceptsConnections());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900238}
239
hubbe@chromium.orga53c97c2014-02-21 09:18:29 +0900240// If a connection closes right before a Send() call, we may end up closing
241// the connection without notifying the listener, which can cause hangs in
242// sync_message_filter and others. Make sure the listener is notified.
243TEST_F(IPCChannelPosixTest, SendHangTest) {
244 IPCChannelPosixTestListener out_listener(true);
245 IPCChannelPosixTestListener in_listener(true);
246 IPC::ChannelHandle in_handle("IN");
morrita@chromium.org844f1c32014-06-07 15:15:53 +0900247 scoped_ptr<IPC::ChannelPosix> in_chan(new IPC::ChannelPosix(
248 in_handle, IPC::Channel::MODE_SERVER, &in_listener));
morrita@chromium.orgfde2b6b2014-06-07 05:13:51 +0900249 base::FileDescriptor out_fd(
250 in_chan->TakeClientFileDescriptor(), false);
hubbe@chromium.orga53c97c2014-02-21 09:18:29 +0900251 IPC::ChannelHandle out_handle("OUT", out_fd);
morrita@chromium.org844f1c32014-06-07 15:15:53 +0900252 scoped_ptr<IPC::ChannelPosix> out_chan(new IPC::ChannelPosix(
253 out_handle, IPC::Channel::MODE_CLIENT, &out_listener));
morrita@chromium.org2ced0042014-05-30 12:58:59 +0900254 ASSERT_TRUE(in_chan->Connect());
255 ASSERT_TRUE(out_chan->Connect());
256 in_chan->Close(); // simulate remote process dying at an unfortunate time.
hubbe@chromium.orga53c97c2014-02-21 09:18:29 +0900257 // Send will fail, because it cannot write the message.
morrita@chromium.org2ced0042014-05-30 12:58:59 +0900258 ASSERT_FALSE(out_chan->Send(new IPC::Message(
hubbe@chromium.orga53c97c2014-02-21 09:18:29 +0900259 0, // routing_id
260 kQuitMessage, // message type
261 IPC::Message::PRIORITY_NORMAL)));
262 SpinRunLoop(TestTimeouts::action_max_timeout());
263 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, out_listener.status());
264}
265
266// If a connection closes right before a Connect() call, we may end up closing
267// the connection without notifying the listener, which can cause hangs in
268// sync_message_filter and others. Make sure the listener is notified.
269TEST_F(IPCChannelPosixTest, AcceptHangTest) {
270 IPCChannelPosixTestListener out_listener(true);
271 IPCChannelPosixTestListener in_listener(true);
272 IPC::ChannelHandle in_handle("IN");
morrita@chromium.org844f1c32014-06-07 15:15:53 +0900273 scoped_ptr<IPC::ChannelPosix> in_chan(new IPC::ChannelPosix(
274 in_handle, IPC::Channel::MODE_SERVER, &in_listener));
morrita@chromium.orgfde2b6b2014-06-07 05:13:51 +0900275 base::FileDescriptor out_fd(
276 in_chan->TakeClientFileDescriptor(), false);
hubbe@chromium.orga53c97c2014-02-21 09:18:29 +0900277 IPC::ChannelHandle out_handle("OUT", out_fd);
morrita@chromium.org844f1c32014-06-07 15:15:53 +0900278 scoped_ptr<IPC::ChannelPosix> out_chan(new IPC::ChannelPosix(
279 out_handle, IPC::Channel::MODE_CLIENT, &out_listener));
morrita@chromium.org2ced0042014-05-30 12:58:59 +0900280 ASSERT_TRUE(in_chan->Connect());
281 in_chan->Close(); // simulate remote process dying at an unfortunate time.
282 ASSERT_FALSE(out_chan->Connect());
hubbe@chromium.orga53c97c2014-02-21 09:18:29 +0900283 SpinRunLoop(TestTimeouts::action_max_timeout());
284 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, out_listener.status());
285}
286
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900287TEST_F(IPCChannelPosixTest, AdvancedConnected) {
288 // Test creating a connection to an external process.
289 IPCChannelPosixTestListener listener(false);
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900290 IPC::ChannelHandle chan_handle(GetConnectionSocketName());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900291 SetUpSocket(&chan_handle, IPC::Channel::MODE_NAMED_SERVER);
morrita@chromium.org844f1c32014-06-07 15:15:53 +0900292 scoped_ptr<IPC::ChannelPosix> channel(new IPC::ChannelPosix(
293 chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener));
morrita@chromium.org2ced0042014-05-30 12:58:59 +0900294 ASSERT_TRUE(channel->Connect());
295 ASSERT_TRUE(channel->AcceptsConnections());
296 ASSERT_FALSE(channel->HasAcceptedConnection());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900297
viettrungluu@chromium.orgceed6052014-03-09 09:59:31 +0900298 base::ProcessHandle handle = SpawnChild("IPCChannelPosixTestConnectionProc");
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900299 ASSERT_TRUE(handle);
tedvessenes@gmail.com5966b642012-07-12 00:41:43 +0900300 SpinRunLoop(TestTimeouts::action_max_timeout());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900301 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status());
morrita@chromium.org2ced0042014-05-30 12:58:59 +0900302 ASSERT_TRUE(channel->HasAcceptedConnection());
bbudge@chromium.orgab4c6bc2013-11-05 07:28:12 +0900303 IPC::Message* message = new IPC::Message(0, // routing_id
304 kQuitMessage, // message type
305 IPC::Message::PRIORITY_NORMAL);
morrita@chromium.org2ced0042014-05-30 12:58:59 +0900306 channel->Send(message);
tedvessenes@gmail.com5966b642012-07-12 00:41:43 +0900307 SpinRunLoop(TestTimeouts::action_timeout());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900308 int exit_code = 0;
309 EXPECT_TRUE(base::WaitForExitCode(handle, &exit_code));
310 EXPECT_EQ(0, exit_code);
311 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status());
morrita@chromium.org2ced0042014-05-30 12:58:59 +0900312 ASSERT_FALSE(channel->HasAcceptedConnection());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900313}
314
315TEST_F(IPCChannelPosixTest, ResetState) {
316 // Test creating a connection to an external process. Close the connection,
317 // but continue to listen and make sure another external process can connect
318 // to us.
319 IPCChannelPosixTestListener listener(false);
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900320 IPC::ChannelHandle chan_handle(GetConnectionSocketName());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900321 SetUpSocket(&chan_handle, IPC::Channel::MODE_NAMED_SERVER);
morrita@chromium.org844f1c32014-06-07 15:15:53 +0900322 scoped_ptr<IPC::ChannelPosix> channel(new IPC::ChannelPosix(
323 chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener));
morrita@chromium.org2ced0042014-05-30 12:58:59 +0900324 ASSERT_TRUE(channel->Connect());
325 ASSERT_TRUE(channel->AcceptsConnections());
326 ASSERT_FALSE(channel->HasAcceptedConnection());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900327
viettrungluu@chromium.orgceed6052014-03-09 09:59:31 +0900328 base::ProcessHandle handle = SpawnChild("IPCChannelPosixTestConnectionProc");
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900329 ASSERT_TRUE(handle);
tedvessenes@gmail.com5966b642012-07-12 00:41:43 +0900330 SpinRunLoop(TestTimeouts::action_max_timeout());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900331 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status());
morrita@chromium.org2ced0042014-05-30 12:58:59 +0900332 ASSERT_TRUE(channel->HasAcceptedConnection());
333 channel->ResetToAcceptingConnectionState();
334 ASSERT_FALSE(channel->HasAcceptedConnection());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900335
viettrungluu@chromium.orgceed6052014-03-09 09:59:31 +0900336 base::ProcessHandle handle2 = SpawnChild("IPCChannelPosixTestConnectionProc");
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900337 ASSERT_TRUE(handle2);
tedvessenes@gmail.com5966b642012-07-12 00:41:43 +0900338 SpinRunLoop(TestTimeouts::action_max_timeout());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900339 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status());
morrita@chromium.org2ced0042014-05-30 12:58:59 +0900340 ASSERT_TRUE(channel->HasAcceptedConnection());
bbudge@chromium.orgab4c6bc2013-11-05 07:28:12 +0900341 IPC::Message* message = new IPC::Message(0, // routing_id
342 kQuitMessage, // message type
343 IPC::Message::PRIORITY_NORMAL);
morrita@chromium.org2ced0042014-05-30 12:58:59 +0900344 channel->Send(message);
tedvessenes@gmail.com5966b642012-07-12 00:41:43 +0900345 SpinRunLoop(TestTimeouts::action_timeout());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900346 EXPECT_TRUE(base::KillProcess(handle, 0, false));
347 int exit_code = 0;
348 EXPECT_TRUE(base::WaitForExitCode(handle2, &exit_code));
349 EXPECT_EQ(0, exit_code);
350 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status());
morrita@chromium.org2ced0042014-05-30 12:58:59 +0900351 ASSERT_FALSE(channel->HasAcceptedConnection());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900352}
353
dmaclach@chromium.org344a3d52011-08-25 23:14:05 +0900354TEST_F(IPCChannelPosixTest, BadChannelName) {
355 // Test empty name
356 IPC::ChannelHandle handle("");
morrita@chromium.org844f1c32014-06-07 15:15:53 +0900357 scoped_ptr<IPC::ChannelPosix> channel(new IPC::ChannelPosix(
358 handle, IPC::Channel::MODE_NAMED_SERVER, NULL));
morrita@chromium.org2ced0042014-05-30 12:58:59 +0900359 ASSERT_FALSE(channel->Connect());
dmaclach@chromium.org344a3d52011-08-25 23:14:05 +0900360
361 // Test name that is too long.
362 const char *kTooLongName = "This_is_a_very_long_name_to_proactively_implement"
363 "client-centered_synergy_through_top-line"
364 "platforms_Phosfluorescently_disintermediate_"
365 "clicks-and-mortar_best_practices_without_"
366 "future-proof_growth_strategies_Continually"
367 "pontificate_proactive_potentialities_before"
368 "leading-edge_processes";
jeremya@chromium.orgf14bfab2013-03-13 13:23:10 +0900369 EXPECT_GE(strlen(kTooLongName), IPC::kMaxSocketNameLength);
dmaclach@chromium.org344a3d52011-08-25 23:14:05 +0900370 IPC::ChannelHandle handle2(kTooLongName);
morrita@chromium.org844f1c32014-06-07 15:15:53 +0900371 scoped_ptr<IPC::ChannelPosix> channel2(new IPC::ChannelPosix(
372 handle2, IPC::Channel::MODE_NAMED_SERVER, NULL));
morrita@chromium.org2ced0042014-05-30 12:58:59 +0900373 EXPECT_FALSE(channel2->Connect());
dmaclach@chromium.org344a3d52011-08-25 23:14:05 +0900374}
375
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900376TEST_F(IPCChannelPosixTest, MultiConnection) {
377 // Test setting up a connection to an external process, and then have
378 // another external process attempt to connect to us.
379 IPCChannelPosixTestListener listener(false);
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900380 IPC::ChannelHandle chan_handle(GetConnectionSocketName());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900381 SetUpSocket(&chan_handle, IPC::Channel::MODE_NAMED_SERVER);
morrita@chromium.org844f1c32014-06-07 15:15:53 +0900382 scoped_ptr<IPC::ChannelPosix> channel(new IPC::ChannelPosix(
383 chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener));
morrita@chromium.org2ced0042014-05-30 12:58:59 +0900384 ASSERT_TRUE(channel->Connect());
385 ASSERT_TRUE(channel->AcceptsConnections());
386 ASSERT_FALSE(channel->HasAcceptedConnection());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900387
viettrungluu@chromium.orgceed6052014-03-09 09:59:31 +0900388 base::ProcessHandle handle = SpawnChild("IPCChannelPosixTestConnectionProc");
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900389 ASSERT_TRUE(handle);
tedvessenes@gmail.com5966b642012-07-12 00:41:43 +0900390 SpinRunLoop(TestTimeouts::action_max_timeout());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900391 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status());
morrita@chromium.org2ced0042014-05-30 12:58:59 +0900392 ASSERT_TRUE(channel->HasAcceptedConnection());
viettrungluu@chromium.orgceed6052014-03-09 09:59:31 +0900393 base::ProcessHandle handle2 = SpawnChild("IPCChannelPosixFailConnectionProc");
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900394 ASSERT_TRUE(handle2);
tedvessenes@gmail.com5966b642012-07-12 00:41:43 +0900395 SpinRunLoop(TestTimeouts::action_max_timeout());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900396 int exit_code = 0;
397 EXPECT_TRUE(base::WaitForExitCode(handle2, &exit_code));
398 EXPECT_EQ(exit_code, 0);
399 ASSERT_EQ(IPCChannelPosixTestListener::DENIED, listener.status());
morrita@chromium.org2ced0042014-05-30 12:58:59 +0900400 ASSERT_TRUE(channel->HasAcceptedConnection());
bbudge@chromium.orgab4c6bc2013-11-05 07:28:12 +0900401 IPC::Message* message = new IPC::Message(0, // routing_id
402 kQuitMessage, // message type
403 IPC::Message::PRIORITY_NORMAL);
morrita@chromium.org2ced0042014-05-30 12:58:59 +0900404 channel->Send(message);
tedvessenes@gmail.com5966b642012-07-12 00:41:43 +0900405 SpinRunLoop(TestTimeouts::action_timeout());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900406 EXPECT_TRUE(base::WaitForExitCode(handle, &exit_code));
407 EXPECT_EQ(exit_code, 0);
408 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status());
morrita@chromium.org2ced0042014-05-30 12:58:59 +0900409 ASSERT_FALSE(channel->HasAcceptedConnection());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900410}
411
dmaclach@chromium.org2812db62011-03-03 07:27:14 +0900412TEST_F(IPCChannelPosixTest, DoubleServer) {
413 // Test setting up two servers with the same name.
414 IPCChannelPosixTestListener listener(false);
415 IPCChannelPosixTestListener listener2(false);
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900416 IPC::ChannelHandle chan_handle(GetConnectionSocketName());
morrita@chromium.org844f1c32014-06-07 15:15:53 +0900417 scoped_ptr<IPC::ChannelPosix> channel(new IPC::ChannelPosix(
418 chan_handle, IPC::Channel::MODE_SERVER, &listener));
419 scoped_ptr<IPC::ChannelPosix> channel2(new IPC::ChannelPosix(
420 chan_handle, IPC::Channel::MODE_SERVER, &listener2));
morrita@chromium.org2ced0042014-05-30 12:58:59 +0900421 ASSERT_TRUE(channel->Connect());
422 ASSERT_FALSE(channel2->Connect());
dmaclach@chromium.org2812db62011-03-03 07:27:14 +0900423}
424
425TEST_F(IPCChannelPosixTest, BadMode) {
426 // Test setting up two servers with a bad mode.
427 IPCChannelPosixTestListener listener(false);
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900428 IPC::ChannelHandle chan_handle(GetConnectionSocketName());
morrita@chromium.org844f1c32014-06-07 15:15:53 +0900429 scoped_ptr<IPC::ChannelPosix> channel(new IPC::ChannelPosix(
morrita@chromium.org2ced0042014-05-30 12:58:59 +0900430 chan_handle, IPC::Channel::MODE_NONE, &listener));
431 ASSERT_FALSE(channel->Connect());
dmaclach@chromium.org2812db62011-03-03 07:27:14 +0900432}
433
kkania@chromium.orgf37b4e52011-08-09 15:46:06 +0900434TEST_F(IPCChannelPosixTest, IsNamedServerInitialized) {
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900435 const std::string& connection_socket_name = GetConnectionSocketName();
kkania@chromium.orgf37b4e52011-08-09 15:46:06 +0900436 IPCChannelPosixTestListener listener(false);
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900437 IPC::ChannelHandle chan_handle(connection_socket_name);
brettw@chromium.org220b8de2013-07-17 04:10:23 +0900438 ASSERT_TRUE(base::DeleteFile(base::FilePath(connection_socket_name), false));
kkania@chromium.orgf37b4e52011-08-09 15:46:06 +0900439 ASSERT_FALSE(IPC::Channel::IsNamedServerInitialized(
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900440 connection_socket_name));
morrita@chromium.org844f1c32014-06-07 15:15:53 +0900441 scoped_ptr<IPC::ChannelPosix> channel(new IPC::ChannelPosix(
442 chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener));
kkania@chromium.orgf37b4e52011-08-09 15:46:06 +0900443 ASSERT_TRUE(IPC::Channel::IsNamedServerInitialized(
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900444 connection_socket_name));
morrita@chromium.org2ced0042014-05-30 12:58:59 +0900445 channel->Close();
kkania@chromium.orgf37b4e52011-08-09 15:46:06 +0900446 ASSERT_FALSE(IPC::Channel::IsNamedServerInitialized(
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900447 connection_socket_name));
kkania@chromium.orgf37b4e52011-08-09 15:46:06 +0900448}
449
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900450// A long running process that connects to us
451MULTIPROCESS_TEST_MAIN(IPCChannelPosixTestConnectionProc) {
xhwang@chromium.org0b2c2a52013-05-01 05:55:03 +0900452 base::MessageLoopForIO message_loop;
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900453 IPCChannelPosixTestListener listener(true);
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900454 IPC::ChannelHandle handle(IPCChannelPosixTest::GetConnectionSocketName());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900455 IPCChannelPosixTest::SetUpSocket(&handle, IPC::Channel::MODE_NAMED_CLIENT);
morrita@chromium.org844f1c32014-06-07 15:15:53 +0900456 scoped_ptr<IPC::ChannelPosix> channel(new IPC::ChannelPosix(
457 handle, IPC::Channel::MODE_NAMED_CLIENT, &listener));
morrita@chromium.org2ced0042014-05-30 12:58:59 +0900458 EXPECT_TRUE(channel->Connect());
tedvessenes@gmail.com5966b642012-07-12 00:41:43 +0900459 IPCChannelPosixTest::SpinRunLoop(TestTimeouts::action_max_timeout());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900460 EXPECT_EQ(IPCChannelPosixTestListener::MESSAGE_RECEIVED, listener.status());
461 return 0;
462}
463
464// Simple external process that shouldn't be able to connect to us.
465MULTIPROCESS_TEST_MAIN(IPCChannelPosixFailConnectionProc) {
xhwang@chromium.org0b2c2a52013-05-01 05:55:03 +0900466 base::MessageLoopForIO message_loop;
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900467 IPCChannelPosixTestListener listener(false);
nileshagrawal@chromium.org42a2e0d2012-05-25 11:56:25 +0900468 IPC::ChannelHandle handle(IPCChannelPosixTest::GetConnectionSocketName());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900469 IPCChannelPosixTest::SetUpSocket(&handle, IPC::Channel::MODE_NAMED_CLIENT);
morrita@chromium.org844f1c32014-06-07 15:15:53 +0900470 scoped_ptr<IPC::ChannelPosix> channel(new IPC::ChannelPosix(
471 handle, IPC::Channel::MODE_NAMED_CLIENT, &listener));
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900472
473 // In this case connect may succeed or fail depending on if the packet
474 // actually gets sent at sendmsg. Since we never delay on send, we may not
475 // see the error. However even if connect succeeds, eventually we will get an
476 // error back since the channel will be closed when we attempt to read from
477 // it.
morrita@chromium.org2ced0042014-05-30 12:58:59 +0900478 bool connected = channel->Connect();
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900479 if (connected) {
tedvessenes@gmail.com5966b642012-07-12 00:41:43 +0900480 IPCChannelPosixTest::SpinRunLoop(TestTimeouts::action_max_timeout());
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900481 EXPECT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status());
482 } else {
483 EXPECT_EQ(IPCChannelPosixTestListener::DISCONNECTED, listener.status());
484 }
485 return 0;
486}
viettrungluu@chromium.org7ca19132013-01-12 05:56:22 +0900487
488} // namespace