OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // These tests are POSIX only. | 5 // These tests are POSIX only. |
6 | 6 |
7 #include "ipc/ipc_channel_posix.h" | 7 #include "ipc/ipc_channel_posix.h" |
8 | 8 |
9 #include <fcntl.h> | 9 #include <fcntl.h> |
10 #include <sys/socket.h> | 10 #include <sys/socket.h> |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 } | 197 } |
198 | 198 |
199 TEST_F(IPCChannelPosixTest, BasicListen) { | 199 TEST_F(IPCChannelPosixTest, BasicListen) { |
200 const std::string kChannelName = | 200 const std::string kChannelName = |
201 GetChannelDirName() + "/IPCChannelPosixTest_BasicListen"; | 201 GetChannelDirName() + "/IPCChannelPosixTest_BasicListen"; |
202 | 202 |
203 // Test creating a socket that is listening. | 203 // Test creating a socket that is listening. |
204 IPC::ChannelHandle handle(kChannelName); | 204 IPC::ChannelHandle handle(kChannelName); |
205 SetUpSocket(&handle, IPC::Channel::MODE_NAMED_SERVER); | 205 SetUpSocket(&handle, IPC::Channel::MODE_NAMED_SERVER); |
206 unlink(handle.name.c_str()); | 206 unlink(handle.name.c_str()); |
207 IPC::Channel channel(handle, IPC::Channel::MODE_NAMED_SERVER, NULL); | 207 scoped_ptr<IPC::Channel> channel( |
208 ASSERT_TRUE(channel.Connect()); | 208 IPC::Channel::CreateNamedServer(handle, NULL)); |
209 ASSERT_TRUE(channel.AcceptsConnections()); | 209 ASSERT_TRUE(channel->Connect()); |
210 ASSERT_FALSE(channel.HasAcceptedConnection()); | 210 ASSERT_TRUE(channel->AcceptsConnections()); |
211 channel.ResetToAcceptingConnectionState(); | 211 ASSERT_FALSE(channel->HasAcceptedConnection()); |
212 ASSERT_FALSE(channel.HasAcceptedConnection()); | 212 channel->ResetToAcceptingConnectionState(); |
| 213 ASSERT_FALSE(channel->HasAcceptedConnection()); |
213 } | 214 } |
214 | 215 |
215 TEST_F(IPCChannelPosixTest, BasicConnected) { | 216 TEST_F(IPCChannelPosixTest, BasicConnected) { |
216 // Test creating a socket that is connected. | 217 // Test creating a socket that is connected. |
217 int pipe_fds[2]; | 218 int pipe_fds[2]; |
218 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, pipe_fds)); | 219 ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, pipe_fds)); |
219 std::string socket_name("/var/tmp/IPCChannelPosixTest_BasicConnected"); | 220 std::string socket_name("/var/tmp/IPCChannelPosixTest_BasicConnected"); |
220 ASSERT_GE(fcntl(pipe_fds[0], F_SETFL, O_NONBLOCK), 0); | 221 ASSERT_GE(fcntl(pipe_fds[0], F_SETFL, O_NONBLOCK), 0); |
221 | 222 |
222 base::FileDescriptor fd(pipe_fds[0], false); | 223 base::FileDescriptor fd(pipe_fds[0], false); |
223 IPC::ChannelHandle handle(socket_name, fd); | 224 IPC::ChannelHandle handle(socket_name, fd); |
224 IPC::Channel channel(handle, IPC::Channel::MODE_SERVER, NULL); | 225 scoped_ptr<IPC::Channel> channel(IPC::Channel::CreateServer(handle, NULL)); |
225 ASSERT_TRUE(channel.Connect()); | 226 ASSERT_TRUE(channel->Connect()); |
226 ASSERT_FALSE(channel.AcceptsConnections()); | 227 ASSERT_FALSE(channel->AcceptsConnections()); |
227 channel.Close(); | 228 channel->Close(); |
228 ASSERT_TRUE(IGNORE_EINTR(close(pipe_fds[1])) == 0); | 229 ASSERT_TRUE(IGNORE_EINTR(close(pipe_fds[1])) == 0); |
229 | 230 |
230 // Make sure that we can use the socket that is created for us by | 231 // Make sure that we can use the socket that is created for us by |
231 // a standard channel. | 232 // a standard channel. |
232 IPC::Channel channel2(socket_name, IPC::Channel::MODE_SERVER, NULL); | 233 scoped_ptr<IPC::Channel> channel2( |
233 ASSERT_TRUE(channel2.Connect()); | 234 IPC::Channel::CreateServer(socket_name, NULL)); |
234 ASSERT_FALSE(channel2.AcceptsConnections()); | 235 ASSERT_TRUE(channel2->Connect()); |
| 236 ASSERT_FALSE(channel2->AcceptsConnections()); |
235 } | 237 } |
236 | 238 |
237 // If a connection closes right before a Send() call, we may end up closing | 239 // If a connection closes right before a Send() call, we may end up closing |
238 // the connection without notifying the listener, which can cause hangs in | 240 // the connection without notifying the listener, which can cause hangs in |
239 // sync_message_filter and others. Make sure the listener is notified. | 241 // sync_message_filter and others. Make sure the listener is notified. |
240 TEST_F(IPCChannelPosixTest, SendHangTest) { | 242 TEST_F(IPCChannelPosixTest, SendHangTest) { |
241 IPCChannelPosixTestListener out_listener(true); | 243 IPCChannelPosixTestListener out_listener(true); |
242 IPCChannelPosixTestListener in_listener(true); | 244 IPCChannelPosixTestListener in_listener(true); |
243 IPC::ChannelHandle in_handle("IN"); | 245 IPC::ChannelHandle in_handle("IN"); |
244 IPC::Channel in_chan(in_handle, IPC::Channel::MODE_SERVER, &in_listener); | 246 scoped_ptr<IPC::Channel> in_chan( |
245 base::FileDescriptor out_fd(in_chan.TakeClientFileDescriptor(), false); | 247 IPC::Channel::CreateServer(in_handle, &in_listener)); |
| 248 base::FileDescriptor out_fd(in_chan->TakeClientFileDescriptor(), false); |
246 IPC::ChannelHandle out_handle("OUT", out_fd); | 249 IPC::ChannelHandle out_handle("OUT", out_fd); |
247 IPC::Channel out_chan(out_handle, IPC::Channel::MODE_CLIENT, &out_listener); | 250 scoped_ptr<IPC::Channel> out_chan( |
248 ASSERT_TRUE(in_chan.Connect()); | 251 IPC::Channel::CreateClient(out_handle, &out_listener)); |
249 ASSERT_TRUE(out_chan.Connect()); | 252 ASSERT_TRUE(in_chan->Connect()); |
250 in_chan.Close(); // simulate remote process dying at an unfortunate time. | 253 ASSERT_TRUE(out_chan->Connect()); |
| 254 in_chan->Close(); // simulate remote process dying at an unfortunate time. |
251 // Send will fail, because it cannot write the message. | 255 // Send will fail, because it cannot write the message. |
252 ASSERT_FALSE(out_chan.Send(new IPC::Message( | 256 ASSERT_FALSE(out_chan->Send(new IPC::Message( |
253 0, // routing_id | 257 0, // routing_id |
254 kQuitMessage, // message type | 258 kQuitMessage, // message type |
255 IPC::Message::PRIORITY_NORMAL))); | 259 IPC::Message::PRIORITY_NORMAL))); |
256 SpinRunLoop(TestTimeouts::action_max_timeout()); | 260 SpinRunLoop(TestTimeouts::action_max_timeout()); |
257 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, out_listener.status()); | 261 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, out_listener.status()); |
258 } | 262 } |
259 | 263 |
260 // If a connection closes right before a Connect() call, we may end up closing | 264 // If a connection closes right before a Connect() call, we may end up closing |
261 // the connection without notifying the listener, which can cause hangs in | 265 // the connection without notifying the listener, which can cause hangs in |
262 // sync_message_filter and others. Make sure the listener is notified. | 266 // sync_message_filter and others. Make sure the listener is notified. |
263 TEST_F(IPCChannelPosixTest, AcceptHangTest) { | 267 TEST_F(IPCChannelPosixTest, AcceptHangTest) { |
264 IPCChannelPosixTestListener out_listener(true); | 268 IPCChannelPosixTestListener out_listener(true); |
265 IPCChannelPosixTestListener in_listener(true); | 269 IPCChannelPosixTestListener in_listener(true); |
266 IPC::ChannelHandle in_handle("IN"); | 270 IPC::ChannelHandle in_handle("IN"); |
267 IPC::Channel in_chan(in_handle, IPC::Channel::MODE_SERVER, &in_listener); | 271 scoped_ptr<IPC::Channel> in_chan( |
268 base::FileDescriptor out_fd(in_chan.TakeClientFileDescriptor(), false); | 272 IPC::Channel::CreateServer(in_handle, &in_listener)); |
| 273 base::FileDescriptor out_fd(in_chan->TakeClientFileDescriptor(), false); |
269 IPC::ChannelHandle out_handle("OUT", out_fd); | 274 IPC::ChannelHandle out_handle("OUT", out_fd); |
270 IPC::Channel out_chan(out_handle, IPC::Channel::MODE_CLIENT, &out_listener); | 275 scoped_ptr<IPC::Channel> out_chan( |
271 ASSERT_TRUE(in_chan.Connect()); | 276 IPC::Channel::CreateClient(out_handle, &out_listener)); |
272 in_chan.Close(); // simulate remote process dying at an unfortunate time. | 277 ASSERT_TRUE(in_chan->Connect()); |
273 ASSERT_FALSE(out_chan.Connect()); | 278 in_chan->Close(); // simulate remote process dying at an unfortunate time. |
| 279 ASSERT_FALSE(out_chan->Connect()); |
274 SpinRunLoop(TestTimeouts::action_max_timeout()); | 280 SpinRunLoop(TestTimeouts::action_max_timeout()); |
275 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, out_listener.status()); | 281 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, out_listener.status()); |
276 } | 282 } |
277 | 283 |
278 TEST_F(IPCChannelPosixTest, AdvancedConnected) { | 284 TEST_F(IPCChannelPosixTest, AdvancedConnected) { |
279 // Test creating a connection to an external process. | 285 // Test creating a connection to an external process. |
280 IPCChannelPosixTestListener listener(false); | 286 IPCChannelPosixTestListener listener(false); |
281 IPC::ChannelHandle chan_handle(GetConnectionSocketName()); | 287 IPC::ChannelHandle chan_handle(GetConnectionSocketName()); |
282 SetUpSocket(&chan_handle, IPC::Channel::MODE_NAMED_SERVER); | 288 SetUpSocket(&chan_handle, IPC::Channel::MODE_NAMED_SERVER); |
283 IPC::Channel channel(chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener); | 289 scoped_ptr<IPC::Channel> channel( |
284 ASSERT_TRUE(channel.Connect()); | 290 IPC::Channel::CreateNamedServer(chan_handle, &listener)); |
285 ASSERT_TRUE(channel.AcceptsConnections()); | 291 ASSERT_TRUE(channel->Connect()); |
286 ASSERT_FALSE(channel.HasAcceptedConnection()); | 292 ASSERT_TRUE(channel->AcceptsConnections()); |
| 293 ASSERT_FALSE(channel->HasAcceptedConnection()); |
287 | 294 |
288 base::ProcessHandle handle = SpawnChild("IPCChannelPosixTestConnectionProc"); | 295 base::ProcessHandle handle = SpawnChild("IPCChannelPosixTestConnectionProc"); |
289 ASSERT_TRUE(handle); | 296 ASSERT_TRUE(handle); |
290 SpinRunLoop(TestTimeouts::action_max_timeout()); | 297 SpinRunLoop(TestTimeouts::action_max_timeout()); |
291 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status()); | 298 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status()); |
292 ASSERT_TRUE(channel.HasAcceptedConnection()); | 299 ASSERT_TRUE(channel->HasAcceptedConnection()); |
293 IPC::Message* message = new IPC::Message(0, // routing_id | 300 IPC::Message* message = new IPC::Message(0, // routing_id |
294 kQuitMessage, // message type | 301 kQuitMessage, // message type |
295 IPC::Message::PRIORITY_NORMAL); | 302 IPC::Message::PRIORITY_NORMAL); |
296 channel.Send(message); | 303 channel->Send(message); |
297 SpinRunLoop(TestTimeouts::action_timeout()); | 304 SpinRunLoop(TestTimeouts::action_timeout()); |
298 int exit_code = 0; | 305 int exit_code = 0; |
299 EXPECT_TRUE(base::WaitForExitCode(handle, &exit_code)); | 306 EXPECT_TRUE(base::WaitForExitCode(handle, &exit_code)); |
300 EXPECT_EQ(0, exit_code); | 307 EXPECT_EQ(0, exit_code); |
301 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); | 308 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); |
302 ASSERT_FALSE(channel.HasAcceptedConnection()); | 309 ASSERT_FALSE(channel->HasAcceptedConnection()); |
303 } | 310 } |
304 | 311 |
305 TEST_F(IPCChannelPosixTest, ResetState) { | 312 TEST_F(IPCChannelPosixTest, ResetState) { |
306 // Test creating a connection to an external process. Close the connection, | 313 // Test creating a connection to an external process. Close the connection, |
307 // but continue to listen and make sure another external process can connect | 314 // but continue to listen and make sure another external process can connect |
308 // to us. | 315 // to us. |
309 IPCChannelPosixTestListener listener(false); | 316 IPCChannelPosixTestListener listener(false); |
310 IPC::ChannelHandle chan_handle(GetConnectionSocketName()); | 317 IPC::ChannelHandle chan_handle(GetConnectionSocketName()); |
311 SetUpSocket(&chan_handle, IPC::Channel::MODE_NAMED_SERVER); | 318 SetUpSocket(&chan_handle, IPC::Channel::MODE_NAMED_SERVER); |
312 IPC::Channel channel(chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener); | 319 scoped_ptr<IPC::Channel> channel( |
313 ASSERT_TRUE(channel.Connect()); | 320 IPC::Channel::CreateNamedServer(chan_handle, &listener)); |
314 ASSERT_TRUE(channel.AcceptsConnections()); | 321 ASSERT_TRUE(channel->Connect()); |
315 ASSERT_FALSE(channel.HasAcceptedConnection()); | 322 ASSERT_TRUE(channel->AcceptsConnections()); |
| 323 ASSERT_FALSE(channel->HasAcceptedConnection()); |
316 | 324 |
317 base::ProcessHandle handle = SpawnChild("IPCChannelPosixTestConnectionProc"); | 325 base::ProcessHandle handle = SpawnChild("IPCChannelPosixTestConnectionProc"); |
318 ASSERT_TRUE(handle); | 326 ASSERT_TRUE(handle); |
319 SpinRunLoop(TestTimeouts::action_max_timeout()); | 327 SpinRunLoop(TestTimeouts::action_max_timeout()); |
320 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status()); | 328 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status()); |
321 ASSERT_TRUE(channel.HasAcceptedConnection()); | 329 ASSERT_TRUE(channel->HasAcceptedConnection()); |
322 channel.ResetToAcceptingConnectionState(); | 330 channel->ResetToAcceptingConnectionState(); |
323 ASSERT_FALSE(channel.HasAcceptedConnection()); | 331 ASSERT_FALSE(channel->HasAcceptedConnection()); |
324 | 332 |
325 base::ProcessHandle handle2 = SpawnChild("IPCChannelPosixTestConnectionProc"); | 333 base::ProcessHandle handle2 = SpawnChild("IPCChannelPosixTestConnectionProc"); |
326 ASSERT_TRUE(handle2); | 334 ASSERT_TRUE(handle2); |
327 SpinRunLoop(TestTimeouts::action_max_timeout()); | 335 SpinRunLoop(TestTimeouts::action_max_timeout()); |
328 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status()); | 336 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status()); |
329 ASSERT_TRUE(channel.HasAcceptedConnection()); | 337 ASSERT_TRUE(channel->HasAcceptedConnection()); |
330 IPC::Message* message = new IPC::Message(0, // routing_id | 338 IPC::Message* message = new IPC::Message(0, // routing_id |
331 kQuitMessage, // message type | 339 kQuitMessage, // message type |
332 IPC::Message::PRIORITY_NORMAL); | 340 IPC::Message::PRIORITY_NORMAL); |
333 channel.Send(message); | 341 channel->Send(message); |
334 SpinRunLoop(TestTimeouts::action_timeout()); | 342 SpinRunLoop(TestTimeouts::action_timeout()); |
335 EXPECT_TRUE(base::KillProcess(handle, 0, false)); | 343 EXPECT_TRUE(base::KillProcess(handle, 0, false)); |
336 int exit_code = 0; | 344 int exit_code = 0; |
337 EXPECT_TRUE(base::WaitForExitCode(handle2, &exit_code)); | 345 EXPECT_TRUE(base::WaitForExitCode(handle2, &exit_code)); |
338 EXPECT_EQ(0, exit_code); | 346 EXPECT_EQ(0, exit_code); |
339 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); | 347 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); |
340 ASSERT_FALSE(channel.HasAcceptedConnection()); | 348 ASSERT_FALSE(channel->HasAcceptedConnection()); |
341 } | 349 } |
342 | 350 |
343 TEST_F(IPCChannelPosixTest, BadChannelName) { | 351 TEST_F(IPCChannelPosixTest, BadChannelName) { |
344 // Test empty name | 352 // Test empty name |
345 IPC::ChannelHandle handle(""); | 353 IPC::ChannelHandle handle(""); |
346 IPC::Channel channel(handle, IPC::Channel::MODE_NAMED_SERVER, NULL); | 354 scoped_ptr<IPC::Channel> channel( |
347 ASSERT_FALSE(channel.Connect()); | 355 IPC::Channel::CreateNamedServer(handle, NULL)); |
| 356 ASSERT_FALSE(channel->Connect()); |
348 | 357 |
349 // Test name that is too long. | 358 // Test name that is too long. |
350 const char *kTooLongName = "This_is_a_very_long_name_to_proactively_implement" | 359 const char *kTooLongName = "This_is_a_very_long_name_to_proactively_implement" |
351 "client-centered_synergy_through_top-line" | 360 "client-centered_synergy_through_top-line" |
352 "platforms_Phosfluorescently_disintermediate_" | 361 "platforms_Phosfluorescently_disintermediate_" |
353 "clicks-and-mortar_best_practices_without_" | 362 "clicks-and-mortar_best_practices_without_" |
354 "future-proof_growth_strategies_Continually" | 363 "future-proof_growth_strategies_Continually" |
355 "pontificate_proactive_potentialities_before" | 364 "pontificate_proactive_potentialities_before" |
356 "leading-edge_processes"; | 365 "leading-edge_processes"; |
357 EXPECT_GE(strlen(kTooLongName), IPC::kMaxSocketNameLength); | 366 EXPECT_GE(strlen(kTooLongName), IPC::kMaxSocketNameLength); |
358 IPC::ChannelHandle handle2(kTooLongName); | 367 IPC::ChannelHandle handle2(kTooLongName); |
359 IPC::Channel channel2(handle2, IPC::Channel::MODE_NAMED_SERVER, NULL); | 368 scoped_ptr<IPC::Channel> channel2( |
360 EXPECT_FALSE(channel2.Connect()); | 369 IPC::Channel::CreateNamedServer(handle2, NULL)); |
| 370 EXPECT_FALSE(channel2->Connect()); |
361 } | 371 } |
362 | 372 |
363 TEST_F(IPCChannelPosixTest, MultiConnection) { | 373 TEST_F(IPCChannelPosixTest, MultiConnection) { |
364 // Test setting up a connection to an external process, and then have | 374 // Test setting up a connection to an external process, and then have |
365 // another external process attempt to connect to us. | 375 // another external process attempt to connect to us. |
366 IPCChannelPosixTestListener listener(false); | 376 IPCChannelPosixTestListener listener(false); |
367 IPC::ChannelHandle chan_handle(GetConnectionSocketName()); | 377 IPC::ChannelHandle chan_handle(GetConnectionSocketName()); |
368 SetUpSocket(&chan_handle, IPC::Channel::MODE_NAMED_SERVER); | 378 SetUpSocket(&chan_handle, IPC::Channel::MODE_NAMED_SERVER); |
369 IPC::Channel channel(chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener); | 379 scoped_ptr<IPC::Channel> channel( |
370 ASSERT_TRUE(channel.Connect()); | 380 IPC::Channel::CreateNamedServer(chan_handle, &listener)); |
371 ASSERT_TRUE(channel.AcceptsConnections()); | 381 ASSERT_TRUE(channel->Connect()); |
372 ASSERT_FALSE(channel.HasAcceptedConnection()); | 382 ASSERT_TRUE(channel->AcceptsConnections()); |
| 383 ASSERT_FALSE(channel->HasAcceptedConnection()); |
373 | 384 |
374 base::ProcessHandle handle = SpawnChild("IPCChannelPosixTestConnectionProc"); | 385 base::ProcessHandle handle = SpawnChild("IPCChannelPosixTestConnectionProc"); |
375 ASSERT_TRUE(handle); | 386 ASSERT_TRUE(handle); |
376 SpinRunLoop(TestTimeouts::action_max_timeout()); | 387 SpinRunLoop(TestTimeouts::action_max_timeout()); |
377 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status()); | 388 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status()); |
378 ASSERT_TRUE(channel.HasAcceptedConnection()); | 389 ASSERT_TRUE(channel->HasAcceptedConnection()); |
379 base::ProcessHandle handle2 = SpawnChild("IPCChannelPosixFailConnectionProc"); | 390 base::ProcessHandle handle2 = SpawnChild("IPCChannelPosixFailConnectionProc"); |
380 ASSERT_TRUE(handle2); | 391 ASSERT_TRUE(handle2); |
381 SpinRunLoop(TestTimeouts::action_max_timeout()); | 392 SpinRunLoop(TestTimeouts::action_max_timeout()); |
382 int exit_code = 0; | 393 int exit_code = 0; |
383 EXPECT_TRUE(base::WaitForExitCode(handle2, &exit_code)); | 394 EXPECT_TRUE(base::WaitForExitCode(handle2, &exit_code)); |
384 EXPECT_EQ(exit_code, 0); | 395 EXPECT_EQ(exit_code, 0); |
385 ASSERT_EQ(IPCChannelPosixTestListener::DENIED, listener.status()); | 396 ASSERT_EQ(IPCChannelPosixTestListener::DENIED, listener.status()); |
386 ASSERT_TRUE(channel.HasAcceptedConnection()); | 397 ASSERT_TRUE(channel->HasAcceptedConnection()); |
387 IPC::Message* message = new IPC::Message(0, // routing_id | 398 IPC::Message* message = new IPC::Message(0, // routing_id |
388 kQuitMessage, // message type | 399 kQuitMessage, // message type |
389 IPC::Message::PRIORITY_NORMAL); | 400 IPC::Message::PRIORITY_NORMAL); |
390 channel.Send(message); | 401 channel->Send(message); |
391 SpinRunLoop(TestTimeouts::action_timeout()); | 402 SpinRunLoop(TestTimeouts::action_timeout()); |
392 EXPECT_TRUE(base::WaitForExitCode(handle, &exit_code)); | 403 EXPECT_TRUE(base::WaitForExitCode(handle, &exit_code)); |
393 EXPECT_EQ(exit_code, 0); | 404 EXPECT_EQ(exit_code, 0); |
394 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); | 405 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); |
395 ASSERT_FALSE(channel.HasAcceptedConnection()); | 406 ASSERT_FALSE(channel->HasAcceptedConnection()); |
396 } | 407 } |
397 | 408 |
398 TEST_F(IPCChannelPosixTest, DoubleServer) { | 409 TEST_F(IPCChannelPosixTest, DoubleServer) { |
399 // Test setting up two servers with the same name. | 410 // Test setting up two servers with the same name. |
400 IPCChannelPosixTestListener listener(false); | 411 IPCChannelPosixTestListener listener(false); |
401 IPCChannelPosixTestListener listener2(false); | 412 IPCChannelPosixTestListener listener2(false); |
402 IPC::ChannelHandle chan_handle(GetConnectionSocketName()); | 413 IPC::ChannelHandle chan_handle(GetConnectionSocketName()); |
403 IPC::Channel channel(chan_handle, IPC::Channel::MODE_SERVER, &listener); | 414 scoped_ptr<IPC::Channel> channel( |
404 IPC::Channel channel2(chan_handle, IPC::Channel::MODE_SERVER, &listener2); | 415 IPC::Channel::CreateServer(chan_handle, &listener)); |
405 ASSERT_TRUE(channel.Connect()); | 416 scoped_ptr<IPC::Channel> channel2( |
406 ASSERT_FALSE(channel2.Connect()); | 417 IPC::Channel::CreateServer(chan_handle, &listener2)); |
| 418 ASSERT_TRUE(channel->Connect()); |
| 419 ASSERT_FALSE(channel2->Connect()); |
407 } | 420 } |
408 | 421 |
409 TEST_F(IPCChannelPosixTest, BadMode) { | 422 TEST_F(IPCChannelPosixTest, BadMode) { |
410 // Test setting up two servers with a bad mode. | 423 // Test setting up two servers with a bad mode. |
411 IPCChannelPosixTestListener listener(false); | 424 IPCChannelPosixTestListener listener(false); |
412 IPC::ChannelHandle chan_handle(GetConnectionSocketName()); | 425 IPC::ChannelHandle chan_handle(GetConnectionSocketName()); |
413 IPC::Channel channel(chan_handle, IPC::Channel::MODE_NONE, &listener); | 426 scoped_ptr<IPC::Channel> channel(IPC::Channel::CreateByModeForProxy( |
414 ASSERT_FALSE(channel.Connect()); | 427 chan_handle, IPC::Channel::MODE_NONE, &listener)); |
| 428 ASSERT_FALSE(channel->Connect()); |
415 } | 429 } |
416 | 430 |
417 TEST_F(IPCChannelPosixTest, IsNamedServerInitialized) { | 431 TEST_F(IPCChannelPosixTest, IsNamedServerInitialized) { |
418 const std::string& connection_socket_name = GetConnectionSocketName(); | 432 const std::string& connection_socket_name = GetConnectionSocketName(); |
419 IPCChannelPosixTestListener listener(false); | 433 IPCChannelPosixTestListener listener(false); |
420 IPC::ChannelHandle chan_handle(connection_socket_name); | 434 IPC::ChannelHandle chan_handle(connection_socket_name); |
421 ASSERT_TRUE(base::DeleteFile(base::FilePath(connection_socket_name), false)); | 435 ASSERT_TRUE(base::DeleteFile(base::FilePath(connection_socket_name), false)); |
422 ASSERT_FALSE(IPC::Channel::IsNamedServerInitialized( | 436 ASSERT_FALSE(IPC::Channel::IsNamedServerInitialized( |
423 connection_socket_name)); | 437 connection_socket_name)); |
424 IPC::Channel channel(chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener); | 438 scoped_ptr<IPC::Channel> channel( |
| 439 IPC::Channel::CreateNamedServer(chan_handle, &listener)); |
425 ASSERT_TRUE(IPC::Channel::IsNamedServerInitialized( | 440 ASSERT_TRUE(IPC::Channel::IsNamedServerInitialized( |
426 connection_socket_name)); | 441 connection_socket_name)); |
427 channel.Close(); | 442 channel->Close(); |
428 ASSERT_FALSE(IPC::Channel::IsNamedServerInitialized( | 443 ASSERT_FALSE(IPC::Channel::IsNamedServerInitialized( |
429 connection_socket_name)); | 444 connection_socket_name)); |
430 } | 445 } |
431 | 446 |
432 // A long running process that connects to us | 447 // A long running process that connects to us |
433 MULTIPROCESS_TEST_MAIN(IPCChannelPosixTestConnectionProc) { | 448 MULTIPROCESS_TEST_MAIN(IPCChannelPosixTestConnectionProc) { |
434 base::MessageLoopForIO message_loop; | 449 base::MessageLoopForIO message_loop; |
435 IPCChannelPosixTestListener listener(true); | 450 IPCChannelPosixTestListener listener(true); |
436 IPC::ChannelHandle handle(IPCChannelPosixTest::GetConnectionSocketName()); | 451 IPC::ChannelHandle handle(IPCChannelPosixTest::GetConnectionSocketName()); |
437 IPCChannelPosixTest::SetUpSocket(&handle, IPC::Channel::MODE_NAMED_CLIENT); | 452 IPCChannelPosixTest::SetUpSocket(&handle, IPC::Channel::MODE_NAMED_CLIENT); |
438 IPC::Channel channel(handle, IPC::Channel::MODE_NAMED_CLIENT, &listener); | 453 scoped_ptr<IPC::Channel> channel( |
439 EXPECT_TRUE(channel.Connect()); | 454 IPC::Channel::CreateNamedClient(handle, &listener)); |
| 455 EXPECT_TRUE(channel->Connect()); |
440 IPCChannelPosixTest::SpinRunLoop(TestTimeouts::action_max_timeout()); | 456 IPCChannelPosixTest::SpinRunLoop(TestTimeouts::action_max_timeout()); |
441 EXPECT_EQ(IPCChannelPosixTestListener::MESSAGE_RECEIVED, listener.status()); | 457 EXPECT_EQ(IPCChannelPosixTestListener::MESSAGE_RECEIVED, listener.status()); |
442 return 0; | 458 return 0; |
443 } | 459 } |
444 | 460 |
445 // Simple external process that shouldn't be able to connect to us. | 461 // Simple external process that shouldn't be able to connect to us. |
446 MULTIPROCESS_TEST_MAIN(IPCChannelPosixFailConnectionProc) { | 462 MULTIPROCESS_TEST_MAIN(IPCChannelPosixFailConnectionProc) { |
447 base::MessageLoopForIO message_loop; | 463 base::MessageLoopForIO message_loop; |
448 IPCChannelPosixTestListener listener(false); | 464 IPCChannelPosixTestListener listener(false); |
449 IPC::ChannelHandle handle(IPCChannelPosixTest::GetConnectionSocketName()); | 465 IPC::ChannelHandle handle(IPCChannelPosixTest::GetConnectionSocketName()); |
450 IPCChannelPosixTest::SetUpSocket(&handle, IPC::Channel::MODE_NAMED_CLIENT); | 466 IPCChannelPosixTest::SetUpSocket(&handle, IPC::Channel::MODE_NAMED_CLIENT); |
451 IPC::Channel channel(handle, IPC::Channel::MODE_NAMED_CLIENT, &listener); | 467 scoped_ptr<IPC::Channel> channel( |
| 468 IPC::Channel::CreateNamedClient(handle, &listener)); |
452 | 469 |
453 // In this case connect may succeed or fail depending on if the packet | 470 // In this case connect may succeed or fail depending on if the packet |
454 // actually gets sent at sendmsg. Since we never delay on send, we may not | 471 // actually gets sent at sendmsg. Since we never delay on send, we may not |
455 // see the error. However even if connect succeeds, eventually we will get an | 472 // see the error. However even if connect succeeds, eventually we will get an |
456 // error back since the channel will be closed when we attempt to read from | 473 // error back since the channel will be closed when we attempt to read from |
457 // it. | 474 // it. |
458 bool connected = channel.Connect(); | 475 bool connected = channel->Connect(); |
459 if (connected) { | 476 if (connected) { |
460 IPCChannelPosixTest::SpinRunLoop(TestTimeouts::action_max_timeout()); | 477 IPCChannelPosixTest::SpinRunLoop(TestTimeouts::action_max_timeout()); |
461 EXPECT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); | 478 EXPECT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); |
462 } else { | 479 } else { |
463 EXPECT_EQ(IPCChannelPosixTestListener::DISCONNECTED, listener.status()); | 480 EXPECT_EQ(IPCChannelPosixTestListener::DISCONNECTED, listener.status()); |
464 } | 481 } |
465 return 0; | 482 return 0; |
466 } | 483 } |
467 | 484 |
468 } // namespace | 485 } // namespace |
OLD | NEW |