OLD | NEW |
| (Empty) |
1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | |
2 // 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/message_loop.h" | |
17 #include "base/scoped_ptr.h" | |
18 #include "base/test/multiprocess_test.h" | |
19 #include "base/test/test_timeouts.h" | |
20 #include "testing/multiprocess_func_list.h" | |
21 | |
22 namespace { | |
23 | |
24 enum { | |
25 QUIT_MESSAGE = 47 | |
26 }; | |
27 | |
28 class 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 | |
44 virtual void OnMessageReceived(const IPC::Message& message) { | |
45 EXPECT_EQ(message.type(), QUIT_MESSAGE); | |
46 status_ = MESSAGE_RECEIVED; | |
47 QuitRunLoop(); | |
48 } | |
49 | |
50 virtual void OnChannelConnected(int32 peer_pid) { | |
51 status_ = CONNECTED; | |
52 if (!quit_only_on_message_) { | |
53 QuitRunLoop(); | |
54 } | |
55 } | |
56 | |
57 virtual void OnChannelError() { | |
58 status_ = CHANNEL_ERROR; | |
59 if (!quit_only_on_message_) { | |
60 QuitRunLoop(); | |
61 } | |
62 } | |
63 | |
64 virtual void OnChannelDenied() { | |
65 status_ = DENIED; | |
66 if (!quit_only_on_message_) { | |
67 QuitRunLoop(); | |
68 } | |
69 } | |
70 | |
71 virtual void OnChannelListenError() { | |
72 status_ = LISTEN_ERROR; | |
73 if (!quit_only_on_message_) { | |
74 QuitRunLoop(); | |
75 } | |
76 } | |
77 | |
78 STATUS status() { return status_; } | |
79 | |
80 void QuitRunLoop() { | |
81 MessageLoopForIO::current()->QuitNow(); | |
82 } | |
83 | |
84 private: | |
85 // The current status of the listener. | |
86 STATUS status_; | |
87 // If |quit_only_on_message_| then the listener will only break out of | |
88 // the run loop when the QUIT_MESSAGE is received. | |
89 bool quit_only_on_message_; | |
90 }; | |
91 | |
92 } // namespace | |
93 | |
94 class IPCChannelPosixTest : public base::MultiProcessTest { | |
95 public: | |
96 static const char kConnectionSocketTestName[]; | |
97 static void SetUpSocket(IPC::ChannelHandle *handle, | |
98 IPC::Channel::Mode mode); | |
99 static void SpinRunLoop(int milliseconds); | |
100 | |
101 protected: | |
102 virtual void SetUp(); | |
103 virtual void TearDown(); | |
104 | |
105 private: | |
106 scoped_ptr<MessageLoopForIO> message_loop_; | |
107 }; | |
108 | |
109 const char IPCChannelPosixTest::kConnectionSocketTestName[] = | |
110 "/var/tmp/chrome_IPCChannelPosixTest__ConnectionSocket"; | |
111 | |
112 void IPCChannelPosixTest::SetUp() { | |
113 MultiProcessTest::SetUp(); | |
114 // Construct a fresh IO Message loop for the duration of each test. | |
115 message_loop_.reset(new MessageLoopForIO()); | |
116 } | |
117 | |
118 void IPCChannelPosixTest::TearDown() { | |
119 message_loop_.reset(NULL); | |
120 MultiProcessTest::TearDown(); | |
121 } | |
122 | |
123 // Create up a socket and bind and listen to it, or connect it | |
124 // depending on the |mode|. | |
125 void IPCChannelPosixTest::SetUpSocket(IPC::ChannelHandle *handle, | |
126 IPC::Channel::Mode mode) { | |
127 const std::string& name = handle->name; | |
128 int socket_fd = socket(PF_UNIX, SOCK_STREAM, 0); | |
129 ASSERT_GE(socket_fd, 0) << name; | |
130 ASSERT_GE(fcntl(socket_fd, F_SETFL, O_NONBLOCK), 0); | |
131 struct sockaddr_un server_address = { 0 }; | |
132 memset(&server_address, 0, sizeof(server_address)); | |
133 server_address.sun_family = AF_UNIX; | |
134 DCHECK_EQ(static_cast<int>(name.length()), | |
135 snprintf(server_address.sun_path, IPC::kMaxPipeNameLength, "%s", | |
136 name.c_str())); | |
137 size_t server_address_len = offsetof(struct sockaddr_un, sun_path) + | |
138 strlen(server_address.sun_path) + 1; | |
139 | |
140 if (mode == IPC::Channel::MODE_NAMED_SERVER) { | |
141 // Only one server at a time. Cleanup garbage if it exists. | |
142 unlink(name.c_str()); | |
143 ASSERT_GE(bind(socket_fd, | |
144 reinterpret_cast<struct sockaddr *>(&server_address), | |
145 server_address_len), 0) << name << ": " << strerror(errno); | |
146 ASSERT_GE(listen(socket_fd, SOMAXCONN), 0) << name << ": " | |
147 << strerror(errno); | |
148 } else if (mode == IPC::Channel::MODE_NAMED_CLIENT) { | |
149 ASSERT_GE(connect(socket_fd, | |
150 reinterpret_cast<struct sockaddr *>(&server_address), | |
151 server_address_len), 0) << name | |
152 << ": " << strerror(errno); | |
153 } else { | |
154 FAIL() << "Unknown mode " << mode; | |
155 } | |
156 handle->socket.fd = socket_fd; | |
157 } | |
158 | |
159 void IPCChannelPosixTest::SpinRunLoop(int milliseconds) { | |
160 MessageLoopForIO *loop = MessageLoopForIO::current(); | |
161 // Post a quit task so that this loop eventually ends and we don't hang | |
162 // in the case of a bad test. Usually, the run loop will quit sooner than | |
163 // that because all tests use a IPCChannelPosixTestListener which quits the | |
164 // current run loop on any channel activity. | |
165 loop->PostDelayedTask(FROM_HERE, new MessageLoop::QuitTask(), milliseconds); | |
166 loop->Run(); | |
167 } | |
168 | |
169 TEST_F(IPCChannelPosixTest, BasicListen) { | |
170 // Test creating a socket that is listening. | |
171 IPC::ChannelHandle handle("/var/tmp/IPCChannelPosixTest::BasicListen"); | |
172 SetUpSocket(&handle, IPC::Channel::MODE_NAMED_SERVER); | |
173 unlink(handle.name.c_str()); | |
174 IPC::Channel channel(handle, IPC::Channel::MODE_NAMED_SERVER, NULL); | |
175 ASSERT_TRUE(channel.Connect()); | |
176 ASSERT_TRUE(channel.AcceptsConnections()); | |
177 ASSERT_FALSE(channel.HasAcceptedConnection()); | |
178 channel.ResetToAcceptingConnectionState(); | |
179 ASSERT_FALSE(channel.HasAcceptedConnection()); | |
180 } | |
181 | |
182 TEST_F(IPCChannelPosixTest, BasicConnected) { | |
183 // Test creating a socket that is connected. | |
184 int pipe_fds[2]; | |
185 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, pipe_fds)); | |
186 std::string socket_name("/var/tmp/IPCChannelPosixTest::BasicConnected"); | |
187 base::FileDescriptor fd(pipe_fds[0], false); | |
188 IPC::ChannelHandle handle(socket_name, fd); | |
189 IPC::Channel channel(handle, IPC::Channel::MODE_SERVER, NULL); | |
190 ASSERT_TRUE(channel.Connect()); | |
191 ASSERT_FALSE(channel.AcceptsConnections()); | |
192 channel.Close(); | |
193 ASSERT_TRUE(HANDLE_EINTR(close(pipe_fds[1])) == 0); | |
194 | |
195 // Make sure that we can use the socket that is created for us by | |
196 // a standard channel. | |
197 IPC::Channel channel2(socket_name, IPC::Channel::MODE_SERVER, NULL); | |
198 ASSERT_TRUE(channel2.Connect()); | |
199 ASSERT_FALSE(channel2.AcceptsConnections()); | |
200 } | |
201 | |
202 TEST_F(IPCChannelPosixTest, AdvancedConnected) { | |
203 // Test creating a connection to an external process. | |
204 IPCChannelPosixTestListener listener(false); | |
205 IPC::ChannelHandle chan_handle(kConnectionSocketTestName); | |
206 SetUpSocket(&chan_handle, IPC::Channel::MODE_NAMED_SERVER); | |
207 IPC::Channel channel(chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener); | |
208 ASSERT_TRUE(channel.Connect()); | |
209 ASSERT_TRUE(channel.AcceptsConnections()); | |
210 ASSERT_FALSE(channel.HasAcceptedConnection()); | |
211 | |
212 base::ProcessHandle handle = SpawnChild("IPCChannelPosixTestConnectionProc", | |
213 false); | |
214 ASSERT_TRUE(handle); | |
215 SpinRunLoop(TestTimeouts::action_max_timeout_ms()); | |
216 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status()); | |
217 ASSERT_TRUE(channel.HasAcceptedConnection()); | |
218 IPC::Message* message = new IPC::Message(0, // routing_id | |
219 QUIT_MESSAGE, // message type | |
220 IPC::Message::PRIORITY_NORMAL); | |
221 channel.Send(message); | |
222 SpinRunLoop(TestTimeouts::action_timeout_ms()); | |
223 int exit_code = 0; | |
224 EXPECT_TRUE(base::WaitForExitCode(handle, &exit_code)); | |
225 EXPECT_EQ(0, exit_code); | |
226 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); | |
227 ASSERT_FALSE(channel.HasAcceptedConnection()); | |
228 } | |
229 | |
230 TEST_F(IPCChannelPosixTest, ResetState) { | |
231 // Test creating a connection to an external process. Close the connection, | |
232 // but continue to listen and make sure another external process can connect | |
233 // to us. | |
234 IPCChannelPosixTestListener listener(false); | |
235 IPC::ChannelHandle chan_handle(kConnectionSocketTestName); | |
236 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); | |
245 SpinRunLoop(TestTimeouts::action_max_timeout_ms()); | |
246 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status()); | |
247 ASSERT_TRUE(channel.HasAcceptedConnection()); | |
248 channel.ResetToAcceptingConnectionState(); | |
249 ASSERT_FALSE(channel.HasAcceptedConnection()); | |
250 | |
251 base::ProcessHandle handle2 = SpawnChild("IPCChannelPosixTestConnectionProc", | |
252 false); | |
253 ASSERT_TRUE(handle2); | |
254 SpinRunLoop(TestTimeouts::action_max_timeout_ms()); | |
255 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status()); | |
256 ASSERT_TRUE(channel.HasAcceptedConnection()); | |
257 IPC::Message* message = new IPC::Message(0, // routing_id | |
258 QUIT_MESSAGE, // message type | |
259 IPC::Message::PRIORITY_NORMAL); | |
260 channel.Send(message); | |
261 SpinRunLoop(TestTimeouts::action_timeout_ms()); | |
262 EXPECT_TRUE(base::KillProcess(handle, 0, false)); | |
263 int exit_code = 0; | |
264 EXPECT_TRUE(base::WaitForExitCode(handle2, &exit_code)); | |
265 EXPECT_EQ(0, exit_code); | |
266 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); | |
267 ASSERT_FALSE(channel.HasAcceptedConnection()); | |
268 } | |
269 | |
270 TEST_F(IPCChannelPosixTest, MultiConnection) { | |
271 // Test setting up a connection to an external process, and then have | |
272 // another external process attempt to connect to us. | |
273 IPCChannelPosixTestListener listener(false); | |
274 IPC::ChannelHandle chan_handle(kConnectionSocketTestName); | |
275 SetUpSocket(&chan_handle, IPC::Channel::MODE_NAMED_SERVER); | |
276 IPC::Channel channel(chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener); | |
277 ASSERT_TRUE(channel.Connect()); | |
278 ASSERT_TRUE(channel.AcceptsConnections()); | |
279 ASSERT_FALSE(channel.HasAcceptedConnection()); | |
280 | |
281 base::ProcessHandle handle = SpawnChild("IPCChannelPosixTestConnectionProc", | |
282 false); | |
283 ASSERT_TRUE(handle); | |
284 SpinRunLoop(TestTimeouts::action_max_timeout_ms()); | |
285 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status()); | |
286 ASSERT_TRUE(channel.HasAcceptedConnection()); | |
287 base::ProcessHandle handle2 = SpawnChild("IPCChannelPosixFailConnectionProc", | |
288 false); | |
289 ASSERT_TRUE(handle2); | |
290 SpinRunLoop(TestTimeouts::action_max_timeout_ms()); | |
291 int exit_code = 0; | |
292 EXPECT_TRUE(base::WaitForExitCode(handle2, &exit_code)); | |
293 EXPECT_EQ(exit_code, 0); | |
294 ASSERT_EQ(IPCChannelPosixTestListener::DENIED, listener.status()); | |
295 ASSERT_TRUE(channel.HasAcceptedConnection()); | |
296 IPC::Message* message = new IPC::Message(0, // routing_id | |
297 QUIT_MESSAGE, // message type | |
298 IPC::Message::PRIORITY_NORMAL); | |
299 channel.Send(message); | |
300 SpinRunLoop(TestTimeouts::action_timeout_ms()); | |
301 EXPECT_TRUE(base::WaitForExitCode(handle, &exit_code)); | |
302 EXPECT_EQ(exit_code, 0); | |
303 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); | |
304 ASSERT_FALSE(channel.HasAcceptedConnection()); | |
305 } | |
306 | |
307 // A long running process that connects to us | |
308 MULTIPROCESS_TEST_MAIN(IPCChannelPosixTestConnectionProc) { | |
309 MessageLoopForIO message_loop; | |
310 IPCChannelPosixTestListener listener(true); | |
311 IPC::ChannelHandle handle(IPCChannelPosixTest::kConnectionSocketTestName); | |
312 IPCChannelPosixTest::SetUpSocket(&handle, IPC::Channel::MODE_NAMED_CLIENT); | |
313 IPC::Channel channel(handle, IPC::Channel::MODE_NAMED_CLIENT, &listener); | |
314 EXPECT_TRUE(channel.Connect()); | |
315 IPCChannelPosixTest::SpinRunLoop(TestTimeouts::action_max_timeout_ms()); | |
316 EXPECT_EQ(IPCChannelPosixTestListener::MESSAGE_RECEIVED, listener.status()); | |
317 return 0; | |
318 } | |
319 | |
320 // Simple external process that shouldn't be able to connect to us. | |
321 MULTIPROCESS_TEST_MAIN(IPCChannelPosixFailConnectionProc) { | |
322 MessageLoopForIO message_loop; | |
323 IPCChannelPosixTestListener listener(false); | |
324 IPC::ChannelHandle handle(IPCChannelPosixTest::kConnectionSocketTestName); | |
325 IPCChannelPosixTest::SetUpSocket(&handle, IPC::Channel::MODE_NAMED_CLIENT); | |
326 IPC::Channel channel(handle, IPC::Channel::MODE_NAMED_CLIENT, &listener); | |
327 | |
328 // In this case connect may succeed or fail depending on if the packet | |
329 // actually gets sent at sendmsg. Since we never delay on send, we may not | |
330 // see the error. However even if connect succeeds, eventually we will get an | |
331 // error back since the channel will be closed when we attempt to read from | |
332 // it. | |
333 bool connected = channel.Connect(); | |
334 if (connected) { | |
335 IPCChannelPosixTest::SpinRunLoop(TestTimeouts::action_max_timeout_ms()); | |
336 EXPECT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); | |
337 } else { | |
338 EXPECT_EQ(IPCChannelPosixTestListener::DISCONNECTED, listener.status()); | |
339 } | |
340 return 0; | |
341 } | |
342 | |
OLD | NEW |