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 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 // the run loop when kQuitMessage is received. | 91 // the run loop when kQuitMessage is received. |
92 bool quit_only_on_message_; | 92 bool quit_only_on_message_; |
93 }; | 93 }; |
94 | 94 |
95 } // namespace | 95 } // namespace |
96 | 96 |
97 class IPCChannelPosixTest : public base::MultiProcessTest { | 97 class IPCChannelPosixTest : public base::MultiProcessTest { |
98 public: | 98 public: |
99 static void SetUpSocket(IPC::ChannelHandle *handle, | 99 static void SetUpSocket(IPC::ChannelHandle *handle, |
100 IPC::Channel::Mode mode); | 100 IPC::Channel::Mode mode); |
101 static void SpinRunLoop(int milliseconds); | 101 static void SpinRunLoop(base::TimeDelta delay); |
102 static const std::string GetConnectionSocketName(); | 102 static const std::string GetConnectionSocketName(); |
103 static const std::string GetChannelDirName(); | 103 static const std::string GetChannelDirName(); |
104 | 104 |
105 protected: | 105 protected: |
106 virtual void SetUp(); | 106 virtual void SetUp(); |
107 virtual void TearDown(); | 107 virtual void TearDown(); |
108 | 108 |
109 private: | 109 private: |
110 scoped_ptr<MessageLoopForIO> message_loop_; | 110 scoped_ptr<MessageLoopForIO> message_loop_; |
111 }; | 111 }; |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 reinterpret_cast<struct sockaddr *>(&server_address), | 173 reinterpret_cast<struct sockaddr *>(&server_address), |
174 server_address_len), 0) << server_address.sun_path | 174 server_address_len), 0) << server_address.sun_path |
175 << ": " << strerror(errno) | 175 << ": " << strerror(errno) |
176 << "(" << errno << ")"; | 176 << "(" << errno << ")"; |
177 } else { | 177 } else { |
178 FAIL() << "Unknown mode " << mode; | 178 FAIL() << "Unknown mode " << mode; |
179 } | 179 } |
180 handle->socket.fd = socket_fd; | 180 handle->socket.fd = socket_fd; |
181 } | 181 } |
182 | 182 |
183 void IPCChannelPosixTest::SpinRunLoop(int milliseconds) { | 183 void IPCChannelPosixTest::SpinRunLoop(base::TimeDelta delay) { |
184 MessageLoopForIO *loop = MessageLoopForIO::current(); | 184 MessageLoopForIO *loop = MessageLoopForIO::current(); |
185 // Post a quit task so that this loop eventually ends and we don't hang | 185 // Post a quit task so that this loop eventually ends and we don't hang |
186 // in the case of a bad test. Usually, the run loop will quit sooner than | 186 // in the case of a bad test. Usually, the run loop will quit sooner than |
187 // that because all tests use a IPCChannelPosixTestListener which quits the | 187 // that because all tests use a IPCChannelPosixTestListener which quits the |
188 // current run loop on any channel activity. | 188 // current run loop on any channel activity. |
189 loop->PostDelayedTask( | 189 loop->PostDelayedTask( |
190 FROM_HERE, | 190 FROM_HERE, |
191 MessageLoop::QuitClosure(), | 191 MessageLoop::QuitClosure(), |
192 base::TimeDelta::FromMilliseconds(milliseconds)); | 192 delay); |
193 loop->Run(); | 193 loop->Run(); |
194 } | 194 } |
195 | 195 |
196 TEST_F(IPCChannelPosixTest, BasicListen) { | 196 TEST_F(IPCChannelPosixTest, BasicListen) { |
197 const std::string kChannelName = | 197 const std::string kChannelName = |
198 GetChannelDirName() + "/IPCChannelPosixTest_BasicListen"; | 198 GetChannelDirName() + "/IPCChannelPosixTest_BasicListen"; |
199 | 199 |
200 // Test creating a socket that is listening. | 200 // Test creating a socket that is listening. |
201 IPC::ChannelHandle handle(kChannelName); | 201 IPC::ChannelHandle handle(kChannelName); |
202 SetUpSocket(&handle, IPC::Channel::MODE_NAMED_SERVER); | 202 SetUpSocket(&handle, IPC::Channel::MODE_NAMED_SERVER); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 IPC::ChannelHandle chan_handle(GetConnectionSocketName()); | 237 IPC::ChannelHandle chan_handle(GetConnectionSocketName()); |
238 SetUpSocket(&chan_handle, IPC::Channel::MODE_NAMED_SERVER); | 238 SetUpSocket(&chan_handle, IPC::Channel::MODE_NAMED_SERVER); |
239 IPC::Channel channel(chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener); | 239 IPC::Channel channel(chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener); |
240 ASSERT_TRUE(channel.Connect()); | 240 ASSERT_TRUE(channel.Connect()); |
241 ASSERT_TRUE(channel.AcceptsConnections()); | 241 ASSERT_TRUE(channel.AcceptsConnections()); |
242 ASSERT_FALSE(channel.HasAcceptedConnection()); | 242 ASSERT_FALSE(channel.HasAcceptedConnection()); |
243 | 243 |
244 base::ProcessHandle handle = SpawnChild("IPCChannelPosixTestConnectionProc", | 244 base::ProcessHandle handle = SpawnChild("IPCChannelPosixTestConnectionProc", |
245 false); | 245 false); |
246 ASSERT_TRUE(handle); | 246 ASSERT_TRUE(handle); |
247 SpinRunLoop(TestTimeouts::action_max_timeout_ms()); | 247 SpinRunLoop(TestTimeouts::action_max_timeout()); |
248 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status()); | 248 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status()); |
249 ASSERT_TRUE(channel.HasAcceptedConnection()); | 249 ASSERT_TRUE(channel.HasAcceptedConnection()); |
250 IPC::Message* message = new IPC::Message(0, // routing_id | 250 IPC::Message* message = new IPC::Message(0, // routing_id |
251 kQuitMessage, // message type | 251 kQuitMessage, // message type |
252 IPC::Message::PRIORITY_NORMAL); | 252 IPC::Message::PRIORITY_NORMAL); |
253 channel.Send(message); | 253 channel.Send(message); |
254 SpinRunLoop(TestTimeouts::action_timeout_ms()); | 254 SpinRunLoop(TestTimeouts::action_timeout()); |
255 int exit_code = 0; | 255 int exit_code = 0; |
256 EXPECT_TRUE(base::WaitForExitCode(handle, &exit_code)); | 256 EXPECT_TRUE(base::WaitForExitCode(handle, &exit_code)); |
257 EXPECT_EQ(0, exit_code); | 257 EXPECT_EQ(0, exit_code); |
258 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); | 258 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); |
259 ASSERT_FALSE(channel.HasAcceptedConnection()); | 259 ASSERT_FALSE(channel.HasAcceptedConnection()); |
260 } | 260 } |
261 | 261 |
262 TEST_F(IPCChannelPosixTest, ResetState) { | 262 TEST_F(IPCChannelPosixTest, ResetState) { |
263 // Test creating a connection to an external process. Close the connection, | 263 // Test creating a connection to an external process. Close the connection, |
264 // but continue to listen and make sure another external process can connect | 264 // but continue to listen and make sure another external process can connect |
265 // to us. | 265 // to us. |
266 IPCChannelPosixTestListener listener(false); | 266 IPCChannelPosixTestListener listener(false); |
267 IPC::ChannelHandle chan_handle(GetConnectionSocketName()); | 267 IPC::ChannelHandle chan_handle(GetConnectionSocketName()); |
268 SetUpSocket(&chan_handle, IPC::Channel::MODE_NAMED_SERVER); | 268 SetUpSocket(&chan_handle, IPC::Channel::MODE_NAMED_SERVER); |
269 IPC::Channel channel(chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener); | 269 IPC::Channel channel(chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener); |
270 ASSERT_TRUE(channel.Connect()); | 270 ASSERT_TRUE(channel.Connect()); |
271 ASSERT_TRUE(channel.AcceptsConnections()); | 271 ASSERT_TRUE(channel.AcceptsConnections()); |
272 ASSERT_FALSE(channel.HasAcceptedConnection()); | 272 ASSERT_FALSE(channel.HasAcceptedConnection()); |
273 | 273 |
274 base::ProcessHandle handle = SpawnChild("IPCChannelPosixTestConnectionProc", | 274 base::ProcessHandle handle = SpawnChild("IPCChannelPosixTestConnectionProc", |
275 false); | 275 false); |
276 ASSERT_TRUE(handle); | 276 ASSERT_TRUE(handle); |
277 SpinRunLoop(TestTimeouts::action_max_timeout_ms()); | 277 SpinRunLoop(TestTimeouts::action_max_timeout()); |
278 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status()); | 278 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status()); |
279 ASSERT_TRUE(channel.HasAcceptedConnection()); | 279 ASSERT_TRUE(channel.HasAcceptedConnection()); |
280 channel.ResetToAcceptingConnectionState(); | 280 channel.ResetToAcceptingConnectionState(); |
281 ASSERT_FALSE(channel.HasAcceptedConnection()); | 281 ASSERT_FALSE(channel.HasAcceptedConnection()); |
282 | 282 |
283 base::ProcessHandle handle2 = SpawnChild("IPCChannelPosixTestConnectionProc", | 283 base::ProcessHandle handle2 = SpawnChild("IPCChannelPosixTestConnectionProc", |
284 false); | 284 false); |
285 ASSERT_TRUE(handle2); | 285 ASSERT_TRUE(handle2); |
286 SpinRunLoop(TestTimeouts::action_max_timeout_ms()); | 286 SpinRunLoop(TestTimeouts::action_max_timeout()); |
287 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status()); | 287 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status()); |
288 ASSERT_TRUE(channel.HasAcceptedConnection()); | 288 ASSERT_TRUE(channel.HasAcceptedConnection()); |
289 IPC::Message* message = new IPC::Message(0, // routing_id | 289 IPC::Message* message = new IPC::Message(0, // routing_id |
290 kQuitMessage, // message type | 290 kQuitMessage, // message type |
291 IPC::Message::PRIORITY_NORMAL); | 291 IPC::Message::PRIORITY_NORMAL); |
292 channel.Send(message); | 292 channel.Send(message); |
293 SpinRunLoop(TestTimeouts::action_timeout_ms()); | 293 SpinRunLoop(TestTimeouts::action_timeout()); |
294 EXPECT_TRUE(base::KillProcess(handle, 0, false)); | 294 EXPECT_TRUE(base::KillProcess(handle, 0, false)); |
295 int exit_code = 0; | 295 int exit_code = 0; |
296 EXPECT_TRUE(base::WaitForExitCode(handle2, &exit_code)); | 296 EXPECT_TRUE(base::WaitForExitCode(handle2, &exit_code)); |
297 EXPECT_EQ(0, exit_code); | 297 EXPECT_EQ(0, exit_code); |
298 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); | 298 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); |
299 ASSERT_FALSE(channel.HasAcceptedConnection()); | 299 ASSERT_FALSE(channel.HasAcceptedConnection()); |
300 } | 300 } |
301 | 301 |
302 TEST_F(IPCChannelPosixTest, BadChannelName) { | 302 TEST_F(IPCChannelPosixTest, BadChannelName) { |
303 // Test empty name | 303 // Test empty name |
(...skipping 22 matching lines...) Expand all Loading... |
326 IPC::ChannelHandle chan_handle(GetConnectionSocketName()); | 326 IPC::ChannelHandle chan_handle(GetConnectionSocketName()); |
327 SetUpSocket(&chan_handle, IPC::Channel::MODE_NAMED_SERVER); | 327 SetUpSocket(&chan_handle, IPC::Channel::MODE_NAMED_SERVER); |
328 IPC::Channel channel(chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener); | 328 IPC::Channel channel(chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener); |
329 ASSERT_TRUE(channel.Connect()); | 329 ASSERT_TRUE(channel.Connect()); |
330 ASSERT_TRUE(channel.AcceptsConnections()); | 330 ASSERT_TRUE(channel.AcceptsConnections()); |
331 ASSERT_FALSE(channel.HasAcceptedConnection()); | 331 ASSERT_FALSE(channel.HasAcceptedConnection()); |
332 | 332 |
333 base::ProcessHandle handle = SpawnChild("IPCChannelPosixTestConnectionProc", | 333 base::ProcessHandle handle = SpawnChild("IPCChannelPosixTestConnectionProc", |
334 false); | 334 false); |
335 ASSERT_TRUE(handle); | 335 ASSERT_TRUE(handle); |
336 SpinRunLoop(TestTimeouts::action_max_timeout_ms()); | 336 SpinRunLoop(TestTimeouts::action_max_timeout()); |
337 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status()); | 337 ASSERT_EQ(IPCChannelPosixTestListener::CONNECTED, listener.status()); |
338 ASSERT_TRUE(channel.HasAcceptedConnection()); | 338 ASSERT_TRUE(channel.HasAcceptedConnection()); |
339 base::ProcessHandle handle2 = SpawnChild("IPCChannelPosixFailConnectionProc", | 339 base::ProcessHandle handle2 = SpawnChild("IPCChannelPosixFailConnectionProc", |
340 false); | 340 false); |
341 ASSERT_TRUE(handle2); | 341 ASSERT_TRUE(handle2); |
342 SpinRunLoop(TestTimeouts::action_max_timeout_ms()); | 342 SpinRunLoop(TestTimeouts::action_max_timeout()); |
343 int exit_code = 0; | 343 int exit_code = 0; |
344 EXPECT_TRUE(base::WaitForExitCode(handle2, &exit_code)); | 344 EXPECT_TRUE(base::WaitForExitCode(handle2, &exit_code)); |
345 EXPECT_EQ(exit_code, 0); | 345 EXPECT_EQ(exit_code, 0); |
346 ASSERT_EQ(IPCChannelPosixTestListener::DENIED, listener.status()); | 346 ASSERT_EQ(IPCChannelPosixTestListener::DENIED, listener.status()); |
347 ASSERT_TRUE(channel.HasAcceptedConnection()); | 347 ASSERT_TRUE(channel.HasAcceptedConnection()); |
348 IPC::Message* message = new IPC::Message(0, // routing_id | 348 IPC::Message* message = new IPC::Message(0, // routing_id |
349 kQuitMessage, // message type | 349 kQuitMessage, // message type |
350 IPC::Message::PRIORITY_NORMAL); | 350 IPC::Message::PRIORITY_NORMAL); |
351 channel.Send(message); | 351 channel.Send(message); |
352 SpinRunLoop(TestTimeouts::action_timeout_ms()); | 352 SpinRunLoop(TestTimeouts::action_timeout()); |
353 EXPECT_TRUE(base::WaitForExitCode(handle, &exit_code)); | 353 EXPECT_TRUE(base::WaitForExitCode(handle, &exit_code)); |
354 EXPECT_EQ(exit_code, 0); | 354 EXPECT_EQ(exit_code, 0); |
355 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); | 355 ASSERT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); |
356 ASSERT_FALSE(channel.HasAcceptedConnection()); | 356 ASSERT_FALSE(channel.HasAcceptedConnection()); |
357 } | 357 } |
358 | 358 |
359 TEST_F(IPCChannelPosixTest, DoubleServer) { | 359 TEST_F(IPCChannelPosixTest, DoubleServer) { |
360 // Test setting up two servers with the same name. | 360 // Test setting up two servers with the same name. |
361 IPCChannelPosixTestListener listener(false); | 361 IPCChannelPosixTestListener listener(false); |
362 IPCChannelPosixTestListener listener2(false); | 362 IPCChannelPosixTestListener listener2(false); |
(...skipping 28 matching lines...) Expand all Loading... |
391 } | 391 } |
392 | 392 |
393 // A long running process that connects to us | 393 // A long running process that connects to us |
394 MULTIPROCESS_TEST_MAIN(IPCChannelPosixTestConnectionProc) { | 394 MULTIPROCESS_TEST_MAIN(IPCChannelPosixTestConnectionProc) { |
395 MessageLoopForIO message_loop; | 395 MessageLoopForIO message_loop; |
396 IPCChannelPosixTestListener listener(true); | 396 IPCChannelPosixTestListener listener(true); |
397 IPC::ChannelHandle handle(IPCChannelPosixTest::GetConnectionSocketName()); | 397 IPC::ChannelHandle handle(IPCChannelPosixTest::GetConnectionSocketName()); |
398 IPCChannelPosixTest::SetUpSocket(&handle, IPC::Channel::MODE_NAMED_CLIENT); | 398 IPCChannelPosixTest::SetUpSocket(&handle, IPC::Channel::MODE_NAMED_CLIENT); |
399 IPC::Channel channel(handle, IPC::Channel::MODE_NAMED_CLIENT, &listener); | 399 IPC::Channel channel(handle, IPC::Channel::MODE_NAMED_CLIENT, &listener); |
400 EXPECT_TRUE(channel.Connect()); | 400 EXPECT_TRUE(channel.Connect()); |
401 IPCChannelPosixTest::SpinRunLoop(TestTimeouts::action_max_timeout_ms()); | 401 IPCChannelPosixTest::SpinRunLoop(TestTimeouts::action_max_timeout()); |
402 EXPECT_EQ(IPCChannelPosixTestListener::MESSAGE_RECEIVED, listener.status()); | 402 EXPECT_EQ(IPCChannelPosixTestListener::MESSAGE_RECEIVED, listener.status()); |
403 return 0; | 403 return 0; |
404 } | 404 } |
405 | 405 |
406 // Simple external process that shouldn't be able to connect to us. | 406 // Simple external process that shouldn't be able to connect to us. |
407 MULTIPROCESS_TEST_MAIN(IPCChannelPosixFailConnectionProc) { | 407 MULTIPROCESS_TEST_MAIN(IPCChannelPosixFailConnectionProc) { |
408 MessageLoopForIO message_loop; | 408 MessageLoopForIO message_loop; |
409 IPCChannelPosixTestListener listener(false); | 409 IPCChannelPosixTestListener listener(false); |
410 IPC::ChannelHandle handle(IPCChannelPosixTest::GetConnectionSocketName()); | 410 IPC::ChannelHandle handle(IPCChannelPosixTest::GetConnectionSocketName()); |
411 IPCChannelPosixTest::SetUpSocket(&handle, IPC::Channel::MODE_NAMED_CLIENT); | 411 IPCChannelPosixTest::SetUpSocket(&handle, IPC::Channel::MODE_NAMED_CLIENT); |
412 IPC::Channel channel(handle, IPC::Channel::MODE_NAMED_CLIENT, &listener); | 412 IPC::Channel channel(handle, IPC::Channel::MODE_NAMED_CLIENT, &listener); |
413 | 413 |
414 // In this case connect may succeed or fail depending on if the packet | 414 // In this case connect may succeed or fail depending on if the packet |
415 // actually gets sent at sendmsg. Since we never delay on send, we may not | 415 // actually gets sent at sendmsg. Since we never delay on send, we may not |
416 // see the error. However even if connect succeeds, eventually we will get an | 416 // see the error. However even if connect succeeds, eventually we will get an |
417 // error back since the channel will be closed when we attempt to read from | 417 // error back since the channel will be closed when we attempt to read from |
418 // it. | 418 // it. |
419 bool connected = channel.Connect(); | 419 bool connected = channel.Connect(); |
420 if (connected) { | 420 if (connected) { |
421 IPCChannelPosixTest::SpinRunLoop(TestTimeouts::action_max_timeout_ms()); | 421 IPCChannelPosixTest::SpinRunLoop(TestTimeouts::action_max_timeout()); |
422 EXPECT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); | 422 EXPECT_EQ(IPCChannelPosixTestListener::CHANNEL_ERROR, listener.status()); |
423 } else { | 423 } else { |
424 EXPECT_EQ(IPCChannelPosixTestListener::DISCONNECTED, listener.status()); | 424 EXPECT_EQ(IPCChannelPosixTestListener::DISCONNECTED, listener.status()); |
425 } | 425 } |
426 return 0; | 426 return 0; |
427 } | 427 } |
OLD | NEW |