OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 #if defined(OS_WIN) | 5 #if defined(OS_WIN) |
6 #include <windows.h> | 6 #include <windows.h> |
7 #endif | 7 #endif |
8 #include <stdio.h> | 8 #include <stdio.h> |
9 #include <iostream> | 9 #include <iostream> |
10 #include <string> | 10 #include <string> |
11 | 11 |
12 #include "chrome/common/ipc_tests.h" | 12 #include "chrome/common/ipc_tests.h" |
13 | 13 |
14 #include "base/at_exit.h" | 14 #include "base/at_exit.h" |
15 #include "base/base_switches.h" | 15 #include "base/base_switches.h" |
16 #include "base/command_line.h" | 16 #include "base/command_line.h" |
17 #include "base/debug_on_start.h" | 17 #include "base/debug_on_start.h" |
18 #include "base/perftimer.h" | 18 #include "base/perftimer.h" |
19 #include "base/perf_test_suite.h" | 19 #include "base/perf_test_suite.h" |
20 #include "base/test_suite.h" | 20 #include "base/test_suite.h" |
21 #include "base/thread.h" | 21 #include "base/thread.h" |
22 #include "chrome/common/chrome_switches.h" | 22 #include "chrome/common/chrome_switches.h" |
| 23 #if defined(OS_POSIX) |
| 24 #include "chrome/common/file_descriptor_posix.h" |
| 25 #endif |
23 #include "chrome/common/ipc_channel.h" | 26 #include "chrome/common/ipc_channel.h" |
24 #include "chrome/common/ipc_channel_proxy.h" | 27 #include "chrome/common/ipc_channel_proxy.h" |
25 #include "chrome/common/ipc_message_utils.h" | 28 #include "chrome/common/ipc_message_utils.h" |
26 #include "testing/multiprocess_func_list.h" | 29 #include "testing/multiprocess_func_list.h" |
27 | 30 |
28 // Define to enable IPC performance testing instead of the regular unit tests | 31 // Define to enable IPC performance testing instead of the regular unit tests |
29 // #define PERFORMANCE_TEST | 32 // #define PERFORMANCE_TEST |
30 | 33 |
31 const wchar_t kTestClientChannel[] = L"T1"; | 34 const wchar_t kTestClientChannel[] = L"T1"; |
32 const wchar_t kReflectorChannel[] = L"T2"; | 35 const wchar_t kReflectorChannel[] = L"T2"; |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 } | 89 } |
87 | 90 |
88 base::ProcessHandle ret = NULL; | 91 base::ProcessHandle ret = NULL; |
89 switch (child_type) { | 92 switch (child_type) { |
90 case TEST_CLIENT: | 93 case TEST_CLIENT: |
91 ret = MultiProcessTest::SpawnChild(L"RunTestClient", | 94 ret = MultiProcessTest::SpawnChild(L"RunTestClient", |
92 fds_to_map, | 95 fds_to_map, |
93 debug_on_start); | 96 debug_on_start); |
94 channel->OnClientConnected(); | 97 channel->OnClientConnected(); |
95 break; | 98 break; |
| 99 case TEST_DESCRIPTOR_CLIENT: |
| 100 ret = MultiProcessTest::SpawnChild(L"RunTestDescriptorClient", |
| 101 fds_to_map, |
| 102 debug_on_start); |
| 103 channel->OnClientConnected(); |
| 104 break; |
96 case TEST_REFLECTOR: | 105 case TEST_REFLECTOR: |
97 ret = MultiProcessTest::SpawnChild(L"RunReflector", | 106 ret = MultiProcessTest::SpawnChild(L"RunReflector", |
98 fds_to_map, | 107 fds_to_map, |
99 debug_on_start); | 108 debug_on_start); |
100 channel->OnClientConnected(); | 109 channel->OnClientConnected(); |
101 break; | 110 break; |
102 case FUZZER_SERVER: | 111 case FUZZER_SERVER: |
103 ret = MultiProcessTest::SpawnChild(L"RunFuzzServer", | 112 ret = MultiProcessTest::SpawnChild(L"RunFuzzServer", |
104 fds_to_map, | 113 fds_to_map, |
105 debug_on_start); | 114 debug_on_start); |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 // Run message loop. | 219 // Run message loop. |
211 MessageLoop::current()->Run(); | 220 MessageLoop::current()->Run(); |
212 | 221 |
213 // Close Channel so client gets its OnChannelError() callback fired. | 222 // Close Channel so client gets its OnChannelError() callback fired. |
214 chan.Close(); | 223 chan.Close(); |
215 | 224 |
216 // Cleanup child process. | 225 // Cleanup child process. |
217 EXPECT_TRUE(base::WaitForSingleProcess(process_handle, 5000)); | 226 EXPECT_TRUE(base::WaitForSingleProcess(process_handle, 5000)); |
218 } | 227 } |
219 | 228 |
| 229 #if defined(OS_POSIX) |
| 230 |
| 231 class MyChannelDescriptorListener : public IPC::Channel::Listener { |
| 232 public: |
| 233 virtual void OnMessageReceived(const IPC::Message& message) { |
| 234 void* iter = NULL; |
| 235 |
| 236 FileDescriptor descriptor; |
| 237 |
| 238 ASSERT_TRUE( |
| 239 IPC::ParamTraits<FileDescriptor>::Read(&message, &iter, &descriptor)); |
| 240 VerifyDescriptor(&descriptor); |
| 241 MessageLoop::current()->Quit(); |
| 242 } |
| 243 |
| 244 virtual void OnChannelError() { |
| 245 MessageLoop::current()->Quit(); |
| 246 } |
| 247 |
| 248 private: |
| 249 static void VerifyDescriptor(FileDescriptor* descriptor) { |
| 250 const int fd = open("/dev/null", O_RDONLY); |
| 251 struct stat st1, st2; |
| 252 fstat(fd, &st1); |
| 253 close(fd); |
| 254 fstat(descriptor->fd, &st2); |
| 255 close(descriptor->fd); |
| 256 ASSERT_EQ(st1.st_ino, st2.st_ino); |
| 257 } |
| 258 }; |
| 259 |
| 260 TEST_F(IPCChannelTest, DescriptorTest) { |
| 261 // Setup IPC channel. |
| 262 MyChannelDescriptorListener listener; |
| 263 |
| 264 IPC::Channel chan(kTestClientChannel, IPC::Channel::MODE_SERVER, |
| 265 &listener); |
| 266 chan.Connect(); |
| 267 |
| 268 base::ProcessHandle process_handle = SpawnChild(TEST_DESCRIPTOR_CLIENT, |
| 269 &chan); |
| 270 ASSERT_TRUE(process_handle); |
| 271 |
| 272 FileDescriptor descriptor; |
| 273 const int fd = open("/dev/null", O_RDONLY); |
| 274 ASSERT_GE(fd, 0); |
| 275 descriptor.auto_close = true; |
| 276 descriptor.fd = fd; |
| 277 |
| 278 IPC::Message* message = new IPC::Message(0, // routing_id |
| 279 3, // message type |
| 280 IPC::Message::PRIORITY_NORMAL); |
| 281 IPC::ParamTraits<FileDescriptor>::Write(message, descriptor); |
| 282 chan.Send(message); |
| 283 |
| 284 // Run message loop. |
| 285 MessageLoop::current()->Run(); |
| 286 |
| 287 // Close Channel so client gets its OnChannelError() callback fired. |
| 288 chan.Close(); |
| 289 |
| 290 // Cleanup child process. |
| 291 EXPECT_TRUE(base::WaitForSingleProcess(process_handle, 5000)); |
| 292 } |
| 293 |
| 294 MULTIPROCESS_TEST_MAIN(RunTestDescriptorClient) { |
| 295 MessageLoopForIO main_message_loop; |
| 296 MyChannelDescriptorListener listener; |
| 297 |
| 298 // setup IPC channel |
| 299 IPC::Channel chan(kTestClientChannel, IPC::Channel::MODE_CLIENT, |
| 300 &listener); |
| 301 chan.Connect(); |
| 302 |
| 303 // run message loop |
| 304 MessageLoop::current()->Run(); |
| 305 // return true; |
| 306 return NULL; |
| 307 } |
| 308 |
| 309 #endif // defined(OS_POSIX) |
| 310 |
220 TEST_F(IPCChannelTest, ChannelProxyTest) { | 311 TEST_F(IPCChannelTest, ChannelProxyTest) { |
221 // The thread needs to out-live the ChannelProxy. | 312 // The thread needs to out-live the ChannelProxy. |
222 base::Thread thread("ChannelProxyTestServer"); | 313 base::Thread thread("ChannelProxyTestServer"); |
223 base::Thread::Options options; | 314 base::Thread::Options options; |
224 options.message_loop_type = MessageLoop::TYPE_IO; | 315 options.message_loop_type = MessageLoop::TYPE_IO; |
225 thread.StartWithOptions(options); | 316 thread.StartWithOptions(options); |
226 { | 317 { |
227 // setup IPC channel proxy | 318 // setup IPC channel proxy |
228 IPC::ChannelProxy chan(kTestClientChannel, IPC::Channel::MODE_SERVER, | 319 IPC::ChannelProxy chan(kTestClientChannel, IPC::Channel::MODE_SERVER, |
229 &channel_listener, NULL, thread.message_loop()); | 320 &channel_listener, NULL, thread.message_loop()); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
270 IPC::Channel chan(kTestClientChannel, IPC::Channel::MODE_CLIENT, | 361 IPC::Channel chan(kTestClientChannel, IPC::Channel::MODE_CLIENT, |
271 &channel_listener); | 362 &channel_listener); |
272 chan.Connect(); | 363 chan.Connect(); |
273 channel_listener.Init(&chan); | 364 channel_listener.Init(&chan); |
274 Send(&chan, "hello from child"); | 365 Send(&chan, "hello from child"); |
275 // run message loop | 366 // run message loop |
276 MessageLoop::current()->Run(); | 367 MessageLoop::current()->Run(); |
277 // return true; | 368 // return true; |
278 return NULL; | 369 return NULL; |
279 } | 370 } |
| 371 |
280 #endif // !PERFORMANCE_TEST | 372 #endif // !PERFORMANCE_TEST |
281 | 373 |
282 #ifdef PERFORMANCE_TEST | 374 #ifdef PERFORMANCE_TEST |
283 | 375 |
284 //----------------------------------------------------------------------------- | 376 //----------------------------------------------------------------------------- |
285 // Manually performance test | 377 // Manually performance test |
286 // | 378 // |
287 // This test times the roundtrip IPC message cycle. It is enabled with a | 379 // This test times the roundtrip IPC message cycle. It is enabled with a |
288 // special preprocessor define to enable it instead of the standard IPC | 380 // special preprocessor define to enable it instead of the standard IPC |
289 // unit tests. This works around some funny termination conditions in the | 381 // unit tests. This works around some funny termination conditions in the |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
442 #endif // PERFORMANCE_TEST | 534 #endif // PERFORMANCE_TEST |
443 | 535 |
444 int main(int argc, char** argv) { | 536 int main(int argc, char** argv) { |
445 #ifdef PERFORMANCE_TEST | 537 #ifdef PERFORMANCE_TEST |
446 int retval = PerfTestSuite(argc, argv).Run(); | 538 int retval = PerfTestSuite(argc, argv).Run(); |
447 #else | 539 #else |
448 int retval = TestSuite(argc, argv).Run(); | 540 int retval = TestSuite(argc, argv).Run(); |
449 #endif | 541 #endif |
450 return retval; | 542 return retval; |
451 } | 543 } |
OLD | NEW |