Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(184)

Side by Side Diff: trunk/src/ipc/ipc_channel_posix_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698