blob: 6f37ae62bc55fe50b62033c65868c56d12e3beba [file] [log] [blame]
levin@chromium.org5c528682011-03-28 10:54:15 +09001// Copyright (c) 2011 The Chromium Authors. All rights reserved.
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// These tests are POSIX only.
6
7#include "ipc/ipc_channel_posix.h"
8
9#include <fcntl.h>
10#include <sys/socket.h>
11#include <sys/un.h>
12#include <unistd.h>
13
14#include "base/basictypes.h"
15#include "base/eintr_wrapper.h"
16#include "base/file_path.h"
17#include "base/file_util.h"
levin@chromium.org5c528682011-03-28 10:54:15 +090018#include "base/memory/scoped_ptr.h"
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +090019#include "base/message_loop.h"
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +090020#include "base/test/multiprocess_test.h"
21#include "base/test/test_timeouts.h"
22#include "testing/multiprocess_func_list.h"
23
24namespace {
25
pkasting@chromium.orgc4f2de22011-09-01 09:46:33 +090026static const uint32 kQuitMessage = 47;
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +090027
28class IPCChannelPosixTestListener : public IPC::Channel::Listener {
29 public:
30 enum STATUS {
31 DISCONNECTED,
32 MESSAGE_RECEIVED,
33 CHANNEL_ERROR,
34 CONNECTED,
35 DENIED,
36 LISTEN_ERROR
37 };
38
39 IPCChannelPosixTestListener(bool quit_only_on_message)
40 : status_(DISCONNECTED), quit_only_on_message_(quit_only_on_message) {}
41
42 virtual ~IPCChannelPosixTestListener() {}
43
evan@chromium.orgecc3f072011-08-17 06:09:25 +090044 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE {
pkasting@chromium.orgc4f2de22011-09-01 09:46:33 +090045 EXPECT_EQ(message.type(), kQuitMessage);
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +090046 status_ = MESSAGE_RECEIVED;
47 QuitRunLoop();
jam@chromium.org8a2c7842010-12-24 15:19:28 +090048 return true;
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +090049 }
50
evan@chromium.orgecc3f072011-08-17 06:09:25 +090051 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE {
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +090052 status_ = CONNECTED;
53 if (!quit_only_on_message_) {
54 QuitRunLoop();
55 }
56 }
57
evan@chromium.orgecc3f072011-08-17 06:09:25 +090058 virtual void OnChannelError() OVERRIDE {
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +090059 status_ = CHANNEL_ERROR;
60 if (!quit_only_on_message_) {
61 QuitRunLoop();
62 }
63 }
64
evan@chromium.orgecc3f072011-08-17 06:09:25 +090065 virtual void OnChannelDenied() OVERRIDE {
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +090066 status_ = DENIED;
67 if (!quit_only_on_message_) {
68 QuitRunLoop();
69 }
70 }
71
evan@chromium.orgecc3f072011-08-17 06:09:25 +090072 virtual void OnChannelListenError() OVERRIDE {
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +090073 status_ = LISTEN_ERROR;
74 if (!quit_only_on_message_) {
75 QuitRunLoop();
76 }
77 }
78
79 STATUS status() { return status_; }
80
81 void QuitRunLoop() {
82 MessageLoopForIO::current()->QuitNow();
83 }
84
85 private:
86 // The current status of the listener.
87 STATUS status_;
88 // If |quit_only_on_message_| then the listener will only break out of
pkasting@chromium.orgc4f2de22011-09-01 09:46:33 +090089 // the run loop when kQuitMessage is received.
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +090090 bool quit_only_on_message_;
91};
92
93} // namespace
94
95class IPCChannelPosixTest : public base::MultiProcessTest {
96 public:
97 static const char kConnectionSocketTestName[];
98 static void SetUpSocket(IPC::ChannelHandle *handle,
99 IPC::Channel::Mode mode);
100 static void SpinRunLoop(int milliseconds);
101
102 protected:
103 virtual void SetUp();
104 virtual void TearDown();
105
106private:
107 scoped_ptr<MessageLoopForIO> message_loop_;
108};
109
110const char IPCChannelPosixTest::kConnectionSocketTestName[] =
111 "/var/tmp/chrome_IPCChannelPosixTest__ConnectionSocket";
112
113void IPCChannelPosixTest::SetUp() {
114 MultiProcessTest::SetUp();
115 // Construct a fresh IO Message loop for the duration of each test.
116 message_loop_.reset(new MessageLoopForIO());
117}
118
119void IPCChannelPosixTest::TearDown() {
120 message_loop_.reset(NULL);
121 MultiProcessTest::TearDown();
122}
123
124// Create up a socket and bind and listen to it, or connect it
125// depending on the |mode|.
126void IPCChannelPosixTest::SetUpSocket(IPC::ChannelHandle *handle,
127 IPC::Channel::Mode mode) {
128 const std::string& name = handle->name;
129
130 int socket_fd = socket(PF_UNIX, SOCK_STREAM, 0);
131 ASSERT_GE(socket_fd, 0) << name;
132 ASSERT_GE(fcntl(socket_fd, F_SETFL, O_NONBLOCK), 0);
133 struct sockaddr_un server_address = { 0 };
134 memset(&server_address, 0, sizeof(server_address));
135 server_address.sun_family = AF_UNIX;
136 int path_len = snprintf(server_address.sun_path, IPC::kMaxPipeNameLength,
137 "%s", name.c_str());
138 DCHECK_EQ(static_cast<int>(name.length()), path_len);
139 size_t server_address_len = offsetof(struct sockaddr_un,
140 sun_path) + path_len + 1;
141
142 if (mode == IPC::Channel::MODE_NAMED_SERVER) {
143 // Only one server at a time. Cleanup garbage if it exists.
144 unlink(name.c_str());
pkasting@chromium.orgc4f2de22011-09-01 09:46:33 +0900145 // Make sure the path we need exists.
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900146 FilePath path(name);
147 FilePath dir_path = path.DirName();
148 ASSERT_TRUE(file_util::CreateDirectory(dir_path));
149 ASSERT_GE(bind(socket_fd,
150 reinterpret_cast<struct sockaddr *>(&server_address),
151 server_address_len), 0) << server_address.sun_path
152 << ": " << strerror(errno)
153 << "(" << errno << ")";
154 ASSERT_GE(listen(socket_fd, SOMAXCONN), 0) << server_address.sun_path
155 << ": " << strerror(errno)
156 << "(" << errno << ")";
157 } else if (mode == IPC::Channel::MODE_NAMED_CLIENT) {
158 ASSERT_GE(connect(socket_fd,
159 reinterpret_cast<struct sockaddr *>(&server_address),
160 server_address_len), 0) << server_address.sun_path
161 << ": " << strerror(errno)
162 << "(" << errno << ")";
163 } else {
164 FAIL() << "Unknown mode " << mode;
165 }
166 handle->socket.fd = socket_fd;
167}
168
169void IPCChannelPosixTest::SpinRunLoop(int milliseconds) {
170 MessageLoopForIO *loop = MessageLoopForIO::current();
171 // Post a quit task so that this loop eventually ends and we don't hang
172 // in the case of a bad test. Usually, the run loop will quit sooner than
173 // that because all tests use a IPCChannelPosixTestListener which quits the
174 // current run loop on any channel activity.
175 loop->PostDelayedTask(FROM_HERE, new MessageLoop::QuitTask(), milliseconds);
176 loop->Run();
177}
178
179TEST_F(IPCChannelPosixTest, BasicListen) {
180 // Test creating a socket that is listening.
181 IPC::ChannelHandle handle("/var/tmp/IPCChannelPosixTest_BasicListen");
182 SetUpSocket(&handle, IPC::Channel::MODE_NAMED_SERVER);
183 unlink(handle.name.c_str());
184 IPC::Channel channel(handle, IPC::Channel::MODE_NAMED_SERVER, NULL);
185 ASSERT_TRUE(channel.Connect());
186 ASSERT_TRUE(channel.AcceptsConnections());
187 ASSERT_FALSE(channel.HasAcceptedConnection());
188 channel.ResetToAcceptingConnectionState();
189 ASSERT_FALSE(channel.HasAcceptedConnection());
190}
191
192TEST_F(IPCChannelPosixTest, BasicConnected) {
193 // Test creating a socket that is connected.
194 int pipe_fds[2];
195 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, pipe_fds));
196 std::string socket_name("/var/tmp/IPCChannelPosixTest_BasicConnected");
197 ASSERT_GE(fcntl(pipe_fds[0], F_SETFL, O_NONBLOCK), 0);
198
199 base::FileDescriptor fd(pipe_fds[0], false);
200 IPC::ChannelHandle handle(socket_name, fd);
201 IPC::Channel channel(handle, IPC::Channel::MODE_SERVER, NULL);
202 ASSERT_TRUE(channel.Connect());
203 ASSERT_FALSE(channel.AcceptsConnections());
204 channel.Close();
205 ASSERT_TRUE(HANDLE_EINTR(close(pipe_fds[1])) == 0);
206
207 // Make sure that we can use the socket that is created for us by
208 // a standard channel.
209 IPC::Channel channel2(socket_name, IPC::Channel::MODE_SERVER, NULL);
210 ASSERT_TRUE(channel2.Connect());
211 ASSERT_FALSE(channel2.AcceptsConnections());
212}
213
214TEST_F(IPCChannelPosixTest, AdvancedConnected) {
215 // Test creating a connection to an external process.
216 IPCChannelPosixTestListener listener(false);
217 IPC::ChannelHandle chan_handle(kConnectionSocketTestName);
218 SetUpSocket(&chan_handle, IPC::Channel::MODE_NAMED_SERVER);
219 IPC::Channel channel(chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener);
220 ASSERT_TRUE(channel.Connect());
221 ASSERT_TRUE(channel.AcceptsConnections());
222 ASSERT_FALSE(channel.HasAcceptedConnection());
223
224 base::ProcessHandle handle = SpawnChild("IPCChannelPosixTestConnectionProc",
225 false);
226 ASSERT_TRUE(handle);
227 SpinRunLoop(TestTimeouts::action_max_timeout_ms());
228 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status());
229 ASSERT_TRUE(channel.HasAcceptedConnection());
pkasting@chromium.orgc4f2de22011-09-01 09:46:33 +0900230 IPC::Message* message = new IPC::Message(0, // routing_id
231 kQuitMessage, // message type
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900232 IPC::Message::PRIORITY_NORMAL);
233 channel.Send(message);
234 SpinRunLoop(TestTimeouts::action_timeout_ms());
235 int exit_code = 0;
236 EXPECT_TRUE(base::WaitForExitCode(handle, &exit_code));
237 EXPECT_EQ(0, exit_code);
238 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status());
239 ASSERT_FALSE(channel.HasAcceptedConnection());
240}
241
242TEST_F(IPCChannelPosixTest, ResetState) {
243 // Test creating a connection to an external process. Close the connection,
244 // but continue to listen and make sure another external process can connect
245 // to us.
246 IPCChannelPosixTestListener listener(false);
247 IPC::ChannelHandle chan_handle(kConnectionSocketTestName);
248 SetUpSocket(&chan_handle, IPC::Channel::MODE_NAMED_SERVER);
249 IPC::Channel channel(chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener);
250 ASSERT_TRUE(channel.Connect());
251 ASSERT_TRUE(channel.AcceptsConnections());
252 ASSERT_FALSE(channel.HasAcceptedConnection());
253
254 base::ProcessHandle handle = SpawnChild("IPCChannelPosixTestConnectionProc",
255 false);
256 ASSERT_TRUE(handle);
257 SpinRunLoop(TestTimeouts::action_max_timeout_ms());
258 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status());
259 ASSERT_TRUE(channel.HasAcceptedConnection());
260 channel.ResetToAcceptingConnectionState();
261 ASSERT_FALSE(channel.HasAcceptedConnection());
262
263 base::ProcessHandle handle2 = SpawnChild("IPCChannelPosixTestConnectionProc",
264 false);
265 ASSERT_TRUE(handle2);
266 SpinRunLoop(TestTimeouts::action_max_timeout_ms());
267 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status());
268 ASSERT_TRUE(channel.HasAcceptedConnection());
pkasting@chromium.orgc4f2de22011-09-01 09:46:33 +0900269 IPC::Message* message = new IPC::Message(0, // routing_id
270 kQuitMessage, // message type
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900271 IPC::Message::PRIORITY_NORMAL);
272 channel.Send(message);
273 SpinRunLoop(TestTimeouts::action_timeout_ms());
274 EXPECT_TRUE(base::KillProcess(handle, 0, false));
275 int exit_code = 0;
276 EXPECT_TRUE(base::WaitForExitCode(handle2, &exit_code));
277 EXPECT_EQ(0, exit_code);
278 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status());
279 ASSERT_FALSE(channel.HasAcceptedConnection());
280}
281
dmaclach@chromium.org344a3d52011-08-25 23:14:05 +0900282TEST_F(IPCChannelPosixTest, BadChannelName) {
283 // Test empty name
284 IPC::ChannelHandle handle("");
285 IPC::Channel channel(handle, IPC::Channel::MODE_NAMED_SERVER, NULL);
286 ASSERT_FALSE(channel.Connect());
287
288 // Test name that is too long.
289 const char *kTooLongName = "This_is_a_very_long_name_to_proactively_implement"
290 "client-centered_synergy_through_top-line"
291 "platforms_Phosfluorescently_disintermediate_"
292 "clicks-and-mortar_best_practices_without_"
293 "future-proof_growth_strategies_Continually"
294 "pontificate_proactive_potentialities_before"
295 "leading-edge_processes";
296 EXPECT_GE(strlen(kTooLongName), IPC::kMaxPipeNameLength);
297 IPC::ChannelHandle handle2(kTooLongName);
298 IPC::Channel channel2(handle2, IPC::Channel::MODE_NAMED_SERVER, NULL);
299 EXPECT_FALSE(channel2.Connect());
300}
301
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900302TEST_F(IPCChannelPosixTest, MultiConnection) {
303 // Test setting up a connection to an external process, and then have
304 // another external process attempt to connect to us.
305 IPCChannelPosixTestListener listener(false);
306 IPC::ChannelHandle chan_handle(kConnectionSocketTestName);
307 SetUpSocket(&chan_handle, IPC::Channel::MODE_NAMED_SERVER);
308 IPC::Channel channel(chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener);
309 ASSERT_TRUE(channel.Connect());
310 ASSERT_TRUE(channel.AcceptsConnections());
311 ASSERT_FALSE(channel.HasAcceptedConnection());
312
313 base::ProcessHandle handle = SpawnChild("IPCChannelPosixTestConnectionProc",
314 false);
315 ASSERT_TRUE(handle);
316 SpinRunLoop(TestTimeouts::action_max_timeout_ms());
317 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status());
318 ASSERT_TRUE(channel.HasAcceptedConnection());
319 base::ProcessHandle handle2 = SpawnChild("IPCChannelPosixFailConnectionProc",
320 false);
321 ASSERT_TRUE(handle2);
322 SpinRunLoop(TestTimeouts::action_max_timeout_ms());
323 int exit_code = 0;
324 EXPECT_TRUE(base::WaitForExitCode(handle2, &exit_code));
325 EXPECT_EQ(exit_code, 0);
326 ASSERT_EQ(IPCChannelPosixTestListener::DENIED, listener.status());
327 ASSERT_TRUE(channel.HasAcceptedConnection());
pkasting@chromium.orgc4f2de22011-09-01 09:46:33 +0900328 IPC::Message* message = new IPC::Message(0, // routing_id
329 kQuitMessage, // message type
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900330 IPC::Message::PRIORITY_NORMAL);
331 channel.Send(message);
332 SpinRunLoop(TestTimeouts::action_timeout_ms());
333 EXPECT_TRUE(base::WaitForExitCode(handle, &exit_code));
334 EXPECT_EQ(exit_code, 0);
335 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status());
336 ASSERT_FALSE(channel.HasAcceptedConnection());
337}
338
dmaclach@chromium.org2812db62011-03-03 07:27:14 +0900339TEST_F(IPCChannelPosixTest, DoubleServer) {
340 // Test setting up two servers with the same name.
341 IPCChannelPosixTestListener listener(false);
342 IPCChannelPosixTestListener listener2(false);
343 IPC::ChannelHandle chan_handle(kConnectionSocketTestName);
344 IPC::Channel channel(chan_handle, IPC::Channel::MODE_SERVER, &listener);
345 IPC::Channel channel2(chan_handle, IPC::Channel::MODE_SERVER, &listener2);
346 ASSERT_TRUE(channel.Connect());
347 ASSERT_FALSE(channel2.Connect());
348}
349
350TEST_F(IPCChannelPosixTest, BadMode) {
351 // Test setting up two servers with a bad mode.
352 IPCChannelPosixTestListener listener(false);
353 IPC::ChannelHandle chan_handle(kConnectionSocketTestName);
354 IPC::Channel channel(chan_handle, IPC::Channel::MODE_NONE, &listener);
355 ASSERT_FALSE(channel.Connect());
356}
357
kkania@chromium.orgf37b4e52011-08-09 15:46:06 +0900358TEST_F(IPCChannelPosixTest, IsNamedServerInitialized) {
359 IPCChannelPosixTestListener listener(false);
360 IPC::ChannelHandle chan_handle(kConnectionSocketTestName);
361 ASSERT_TRUE(file_util::Delete(FilePath(kConnectionSocketTestName), false));
362 ASSERT_FALSE(IPC::Channel::IsNamedServerInitialized(
363 kConnectionSocketTestName));
364 IPC::Channel channel(chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener);
365 ASSERT_TRUE(IPC::Channel::IsNamedServerInitialized(
366 kConnectionSocketTestName));
367 channel.Close();
368 ASSERT_FALSE(IPC::Channel::IsNamedServerInitialized(
369 kConnectionSocketTestName));
370}
371
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900372// A long running process that connects to us
373MULTIPROCESS_TEST_MAIN(IPCChannelPosixTestConnectionProc) {
374 MessageLoopForIO message_loop;
375 IPCChannelPosixTestListener listener(true);
376 IPC::ChannelHandle handle(IPCChannelPosixTest::kConnectionSocketTestName);
377 IPCChannelPosixTest::SetUpSocket(&handle, IPC::Channel::MODE_NAMED_CLIENT);
378 IPC::Channel channel(handle, IPC::Channel::MODE_NAMED_CLIENT, &listener);
379 EXPECT_TRUE(channel.Connect());
380 IPCChannelPosixTest::SpinRunLoop(TestTimeouts::action_max_timeout_ms());
381 EXPECT_EQ(IPCChannelPosixTestListener::MESSAGE_RECEIVED, listener.status());
382 return 0;
383}
384
385// Simple external process that shouldn't be able to connect to us.
386MULTIPROCESS_TEST_MAIN(IPCChannelPosixFailConnectionProc) {
387 MessageLoopForIO message_loop;
388 IPCChannelPosixTestListener listener(false);
389 IPC::ChannelHandle handle(IPCChannelPosixTest::kConnectionSocketTestName);
390 IPCChannelPosixTest::SetUpSocket(&handle, IPC::Channel::MODE_NAMED_CLIENT);
391 IPC::Channel channel(handle, IPC::Channel::MODE_NAMED_CLIENT, &listener);
392
393 // In this case connect may succeed or fail depending on if the packet
394 // actually gets sent at sendmsg. Since we never delay on send, we may not
395 // see the error. However even if connect succeeds, eventually we will get an
396 // error back since the channel will be closed when we attempt to read from
397 // it.
398 bool connected = channel.Connect();
399 if (connected) {
400 IPCChannelPosixTest::SpinRunLoop(TestTimeouts::action_max_timeout_ms());
401 EXPECT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status());
402 } else {
403 EXPECT_EQ(IPCChannelPosixTestListener::DISCONNECTED, listener.status());
404 }
405 return 0;
406}
407