Remove now-passing tests and rebaseline one after the Big Scary Roll.
[chromium-blink-merge.git] / ipc / ipc_tests.cc
bloba7546c392053c1745ce33252cf316c5cf91c7b34
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.
5 #include "build/build_config.h"
7 #if defined(OS_WIN)
8 #include <windows.h>
9 #elif defined(OS_POSIX)
10 #include <sys/types.h>
11 #include <unistd.h>
12 #endif
14 #include <stdio.h>
15 #include <iostream>
16 #include <string>
18 #include "ipc/ipc_tests.h"
20 #include "base/at_exit.h"
21 #include "base/base_switches.h"
22 #include "base/command_line.h"
23 #include "base/debug_on_start.h"
24 #if defined(OS_POSIX)
25 #include "base/at_exit.h"
26 #include "base/global_descriptors_posix.h"
27 #endif
28 #include "base/perftimer.h"
29 #include "base/test/perf_test_suite.h"
30 #include "base/test/test_suite.h"
31 #include "base/thread.h"
32 #include "ipc/ipc_descriptors.h"
33 #include "ipc/ipc_channel.h"
34 #include "ipc/ipc_channel_proxy.h"
35 #include "ipc/ipc_message_utils.h"
36 #include "ipc/ipc_switches.h"
37 #include "testing/multiprocess_func_list.h"
39 // Define to enable IPC performance testing instead of the regular unit tests
40 // #define PERFORMANCE_TEST
42 const char kTestClientChannel[] = "T1";
43 const char kReflectorChannel[] = "T2";
44 const char kFuzzerChannel[] = "F3";
45 const char kSyncSocketChannel[] = "S4";
47 const size_t kLongMessageStringNumBytes = 50000;
49 #ifndef PERFORMANCE_TEST
51 void IPCChannelTest::SetUp() {
52 MultiProcessTest::SetUp();
54 // Construct a fresh IO Message loop for the duration of each test.
55 message_loop_ = new MessageLoopForIO();
58 void IPCChannelTest::TearDown() {
59 delete message_loop_;
60 message_loop_ = NULL;
62 MultiProcessTest::TearDown();
65 #if defined(OS_WIN)
66 base::ProcessHandle IPCChannelTest::SpawnChild(ChildType child_type,
67 IPC::Channel *channel) {
68 // kDebugChildren support.
69 bool debug_on_start =
70 CommandLine::ForCurrentProcess()->HasSwitch(switches::kDebugChildren);
72 switch (child_type) {
73 case TEST_CLIENT:
74 return MultiProcessTest::SpawnChild(L"RunTestClient", debug_on_start);
75 break;
76 case TEST_REFLECTOR:
77 return MultiProcessTest::SpawnChild(L"RunReflector", debug_on_start);
78 break;
79 case FUZZER_SERVER:
80 return MultiProcessTest::SpawnChild(L"RunFuzzServer", debug_on_start);
81 break;
82 case SYNC_SOCKET_SERVER:
83 return MultiProcessTest::SpawnChild(L"RunSyncSocketServer", debug_on_start);
84 break;
85 default:
86 return NULL;
87 break;
90 #elif defined(OS_POSIX)
91 base::ProcessHandle IPCChannelTest::SpawnChild(ChildType child_type,
92 IPC::Channel *channel) {
93 // kDebugChildren support.
94 bool debug_on_start =
95 CommandLine::ForCurrentProcess()->HasSwitch(switches::kDebugChildren);
97 base::file_handle_mapping_vector fds_to_map;
98 const int ipcfd = channel->GetClientFileDescriptor();
99 if (ipcfd > -1) {
100 fds_to_map.push_back(std::pair<int,int>(ipcfd, kPrimaryIPCChannel + 3));
103 base::ProcessHandle ret = NULL;
104 switch (child_type) {
105 case TEST_CLIENT:
106 ret = MultiProcessTest::SpawnChild(L"RunTestClient",
107 fds_to_map,
108 debug_on_start);
109 break;
110 case TEST_DESCRIPTOR_CLIENT:
111 ret = MultiProcessTest::SpawnChild(L"RunTestDescriptorClient",
112 fds_to_map,
113 debug_on_start);
114 break;
115 case TEST_DESCRIPTOR_CLIENT_SANDBOXED:
116 ret = MultiProcessTest::SpawnChild(L"RunTestDescriptorClientSandboxed",
117 fds_to_map,
118 debug_on_start);
119 break;
120 case TEST_REFLECTOR:
121 ret = MultiProcessTest::SpawnChild(L"RunReflector",
122 fds_to_map,
123 debug_on_start);
124 break;
125 case FUZZER_SERVER:
126 ret = MultiProcessTest::SpawnChild(L"RunFuzzServer",
127 fds_to_map,
128 debug_on_start);
129 break;
130 case SYNC_SOCKET_SERVER:
131 ret = MultiProcessTest::SpawnChild(L"RunSyncSocketServer",
132 fds_to_map,
133 debug_on_start);
134 break;
135 default:
136 return NULL;
137 break;
139 return ret;
141 #endif // defined(OS_POSIX)
143 TEST_F(IPCChannelTest, BasicMessageTest) {
144 int v1 = 10;
145 std::string v2("foobar");
146 std::wstring v3(L"hello world");
148 IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL);
149 EXPECT_TRUE(m.WriteInt(v1));
150 EXPECT_TRUE(m.WriteString(v2));
151 EXPECT_TRUE(m.WriteWString(v3));
153 void* iter = NULL;
155 int vi;
156 std::string vs;
157 std::wstring vw;
159 EXPECT_TRUE(m.ReadInt(&iter, &vi));
160 EXPECT_EQ(v1, vi);
162 EXPECT_TRUE(m.ReadString(&iter, &vs));
163 EXPECT_EQ(v2, vs);
165 EXPECT_TRUE(m.ReadWString(&iter, &vw));
166 EXPECT_EQ(v3, vw);
168 // should fail
169 EXPECT_FALSE(m.ReadInt(&iter, &vi));
170 EXPECT_FALSE(m.ReadString(&iter, &vs));
171 EXPECT_FALSE(m.ReadWString(&iter, &vw));
174 static void Send(IPC::Message::Sender* sender, const char* text) {
175 static int message_index = 0;
177 IPC::Message* message = new IPC::Message(0,
179 IPC::Message::PRIORITY_NORMAL);
180 message->WriteInt(message_index++);
181 message->WriteString(std::string(text));
183 // Make sure we can handle large messages.
184 char junk[kLongMessageStringNumBytes];
185 memset(junk, 'a', sizeof(junk)-1);
186 junk[sizeof(junk)-1] = 0;
187 message->WriteString(std::string(junk));
189 // DEBUG: printf("[%u] sending message [%s]\n", GetCurrentProcessId(), text);
190 sender->Send(message);
193 class MyChannelListener : public IPC::Channel::Listener {
194 public:
195 virtual void OnMessageReceived(const IPC::Message& message) {
196 IPC::MessageIterator iter(message);
198 iter.NextInt();
199 const std::string data = iter.NextString();
200 const std::string big_string = iter.NextString();
201 EXPECT_EQ(kLongMessageStringNumBytes - 1, big_string.length());
204 if (--messages_left_ == 0) {
205 MessageLoop::current()->Quit();
206 } else {
207 Send(sender_, "Foo");
211 virtual void OnChannelError() {
212 // There is a race when closing the channel so the last message may be lost.
213 EXPECT_LE(messages_left_, 1);
214 MessageLoop::current()->Quit();
217 void Init(IPC::Message::Sender* s) {
218 sender_ = s;
219 messages_left_ = 50;
222 private:
223 IPC::Message::Sender* sender_;
224 int messages_left_;
227 TEST_F(IPCChannelTest, ChannelTest) {
228 MyChannelListener channel_listener;
229 // Setup IPC channel.
230 IPC::Channel chan(kTestClientChannel, IPC::Channel::MODE_SERVER,
231 &channel_listener);
232 chan.Connect();
234 channel_listener.Init(&chan);
236 base::ProcessHandle process_handle = SpawnChild(TEST_CLIENT, &chan);
237 ASSERT_TRUE(process_handle);
239 Send(&chan, "hello from parent");
241 // Run message loop.
242 MessageLoop::current()->Run();
244 // Close Channel so client gets its OnChannelError() callback fired.
245 chan.Close();
247 // Cleanup child process.
248 EXPECT_TRUE(base::WaitForSingleProcess(process_handle, 5000));
249 base::CloseProcessHandle(process_handle);
252 TEST_F(IPCChannelTest, ChannelProxyTest) {
253 MyChannelListener channel_listener;
255 // The thread needs to out-live the ChannelProxy.
256 base::Thread thread("ChannelProxyTestServer");
257 base::Thread::Options options;
258 options.message_loop_type = MessageLoop::TYPE_IO;
259 thread.StartWithOptions(options);
261 // setup IPC channel proxy
262 IPC::ChannelProxy chan(kTestClientChannel, IPC::Channel::MODE_SERVER,
263 &channel_listener, NULL, thread.message_loop());
265 channel_listener.Init(&chan);
267 #if defined(OS_WIN)
268 base::ProcessHandle process_handle = SpawnChild(TEST_CLIENT, NULL);
269 #elif defined(OS_POSIX)
270 bool debug_on_start = CommandLine::ForCurrentProcess()->HasSwitch(
271 switches::kDebugChildren);
272 base::file_handle_mapping_vector fds_to_map;
273 const int ipcfd = chan.GetClientFileDescriptor();
274 if (ipcfd > -1) {
275 fds_to_map.push_back(std::pair<int,int>(ipcfd, kPrimaryIPCChannel + 3));
278 base::ProcessHandle process_handle = MultiProcessTest::SpawnChild(
279 L"RunTestClient",
280 fds_to_map,
281 debug_on_start);
282 #endif // defined(OS_POSIX)
284 ASSERT_TRUE(process_handle);
286 Send(&chan, "hello from parent");
288 // run message loop
289 MessageLoop::current()->Run();
291 // cleanup child process
292 EXPECT_TRUE(base::WaitForSingleProcess(process_handle, 5000));
293 base::CloseProcessHandle(process_handle);
295 thread.Stop();
298 class ChannelListenerWithOnConnectedSend : public IPC::Channel::Listener {
299 public:
300 virtual void OnChannelConnected(int32 peer_pid) {
301 SendNextMessage();
304 virtual void OnMessageReceived(const IPC::Message& message) {
305 IPC::MessageIterator iter(message);
307 iter.NextInt();
308 const std::string data = iter.NextString();
309 const std::string big_string = iter.NextString();
310 EXPECT_EQ(kLongMessageStringNumBytes - 1, big_string.length());
311 SendNextMessage();
314 virtual void OnChannelError() {
315 // There is a race when closing the channel so the last message may be lost.
316 EXPECT_LE(messages_left_, 1);
317 MessageLoop::current()->Quit();
320 void Init(IPC::Message::Sender* s) {
321 sender_ = s;
322 messages_left_ = 50;
325 private:
326 void SendNextMessage() {
327 if (--messages_left_ == 0) {
328 MessageLoop::current()->Quit();
329 } else {
330 Send(sender_, "Foo");
334 IPC::Message::Sender* sender_;
335 int messages_left_;
338 TEST_F(IPCChannelTest, SendMessageInChannelConnected) {
339 // This tests the case of a listener sending back an event in it's
340 // OnChannelConnected handler.
342 ChannelListenerWithOnConnectedSend channel_listener;
343 // Setup IPC channel.
344 IPC::Channel channel(kTestClientChannel, IPC::Channel::MODE_SERVER,
345 &channel_listener);
346 channel_listener.Init(&channel);
347 channel.Connect();
349 base::ProcessHandle process_handle = SpawnChild(TEST_CLIENT, &channel);
350 ASSERT_TRUE(process_handle);
352 Send(&channel, "hello from parent");
354 // Run message loop.
355 MessageLoop::current()->Run();
357 // Close Channel so client gets its OnChannelError() callback fired.
358 channel.Close();
360 // Cleanup child process.
361 EXPECT_TRUE(base::WaitForSingleProcess(process_handle, 5000));
362 base::CloseProcessHandle(process_handle);
365 MULTIPROCESS_TEST_MAIN(RunTestClient) {
366 MessageLoopForIO main_message_loop;
367 MyChannelListener channel_listener;
369 // setup IPC channel
370 IPC::Channel chan(kTestClientChannel, IPC::Channel::MODE_CLIENT,
371 &channel_listener);
372 chan.Connect();
373 channel_listener.Init(&chan);
374 Send(&chan, "hello from child");
375 // run message loop
376 MessageLoop::current()->Run();
377 // return true;
378 return NULL;
381 #endif // !PERFORMANCE_TEST
383 #ifdef PERFORMANCE_TEST
385 //-----------------------------------------------------------------------------
386 // Manually performance test
388 // This test times the roundtrip IPC message cycle. It is enabled with a
389 // special preprocessor define to enable it instead of the standard IPC
390 // unit tests. This works around some funny termination conditions in the
391 // regular unit tests.
393 // This test is not automated. To test, you will want to vary the message
394 // count and message size in TEST to get the numbers you want.
396 // FIXME(brettw): Automate this test and have it run by default.
398 // This channel listener just replies to all messages with the exact same
399 // message. It assumes each message has one string parameter. When the string
400 // "quit" is sent, it will exit.
401 class ChannelReflectorListener : public IPC::Channel::Listener {
402 public:
403 explicit ChannelReflectorListener(IPC::Channel *channel) :
404 channel_(channel),
405 count_messages_(0),
406 latency_messages_(0) {
407 std::cout << "Reflector up" << std::endl;
410 ~ChannelReflectorListener() {
411 std::cout << "Client Messages: " << count_messages_ << std::endl;
412 std::cout << "Client Latency: " << latency_messages_ << std::endl;
415 virtual void OnMessageReceived(const IPC::Message& message) {
416 count_messages_++;
417 IPC::MessageIterator iter(message);
418 int time = iter.NextInt();
419 int msgid = iter.NextInt();
420 std::string payload = iter.NextString();
421 latency_messages_ += GetTickCount() - time;
423 // cout << "reflector msg received: " << msgid << endl;
424 if (payload == "quit")
425 MessageLoop::current()->Quit();
427 IPC::Message* msg = new IPC::Message(0,
429 IPC::Message::PRIORITY_NORMAL);
430 msg->WriteInt(GetTickCount());
431 msg->WriteInt(msgid);
432 msg->WriteString(payload);
433 channel_->Send(msg);
435 private:
436 IPC::Channel *channel_;
437 int count_messages_;
438 int latency_messages_;
441 class ChannelPerfListener : public IPC::Channel::Listener {
442 public:
443 ChannelPerfListener(IPC::Channel* channel, int msg_count, int msg_size) :
444 count_down_(msg_count),
445 channel_(channel),
446 count_messages_(0),
447 latency_messages_(0) {
448 payload_.resize(msg_size);
449 for (int i = 0; i < static_cast<int>(payload_.size()); i++)
450 payload_[i] = 'a';
451 std::cout << "perflistener up" << std::endl;
454 ~ChannelPerfListener() {
455 std::cout << "Server Messages: " << count_messages_ << std::endl;
456 std::cout << "Server Latency: " << latency_messages_ << std::endl;
459 virtual void OnMessageReceived(const IPC::Message& message) {
460 count_messages_++;
461 // decode the string so this gets counted in the total time
462 IPC::MessageIterator iter(message);
463 int time = iter.NextInt();
464 int msgid = iter.NextInt();
465 std::string cur = iter.NextString();
466 latency_messages_ += GetTickCount() - time;
468 // cout << "perflistener got message" << endl;
470 count_down_--;
471 if (count_down_ == 0) {
472 IPC::Message* msg = new IPC::Message(0,
474 IPC::Message::PRIORITY_NORMAL);
475 msg->WriteInt(GetTickCount());
476 msg->WriteInt(count_down_);
477 msg->WriteString("quit");
478 channel_->Send(msg);
479 SetTimer(NULL, 1, 250, (TIMERPROC) PostQuitMessage);
480 return;
483 IPC::Message* msg = new IPC::Message(0,
485 IPC::Message::PRIORITY_NORMAL);
486 msg->WriteInt(GetTickCount());
487 msg->WriteInt(count_down_);
488 msg->WriteString(payload_);
489 channel_->Send(msg);
492 private:
493 int count_down_;
494 std::string payload_;
495 IPC::Channel *channel_;
496 int count_messages_;
497 int latency_messages_;
500 TEST_F(IPCChannelTest, Performance) {
501 // setup IPC channel
502 IPC::Channel chan(kReflectorChannel, IPC::Channel::MODE_SERVER, NULL);
503 ChannelPerfListener perf_listener(&chan, 10000, 100000);
504 chan.set_listener(&perf_listener);
505 chan.Connect();
507 HANDLE process = SpawnChild(TEST_REFLECTOR, &chan);
508 ASSERT_TRUE(process);
510 PlatformThread::Sleep(1000);
512 PerfTimeLogger logger("IPC_Perf");
514 // this initial message will kick-start the ping-pong of messages
515 IPC::Message* message = new IPC::Message(0,
517 IPC::Message::PRIORITY_NORMAL);
518 message->WriteInt(GetTickCount());
519 message->WriteInt(-1);
520 message->WriteString("Hello");
521 chan.Send(message);
523 // run message loop
524 MessageLoop::current()->Run();
526 // cleanup child process
527 WaitForSingleObject(process, 5000);
528 CloseHandle(process);
531 // This message loop bounces all messages back to the sender
532 MULTIPROCESS_TEST_MAIN(RunReflector) {
533 MessageLoopForIO main_message_loop;
534 IPC::Channel chan(kReflectorChannel, IPC::Channel::MODE_CLIENT, NULL);
535 ChannelReflectorListener channel_reflector_listener(&chan);
536 chan.set_listener(&channel_reflector_listener);
537 chan.Connect();
539 MessageLoop::current()->Run();
540 return true;
543 #endif // PERFORMANCE_TEST
545 int main(int argc, char** argv) {
546 #ifdef PERFORMANCE_TEST
547 int retval = PerfTestSuite(argc, argv).Run();
548 #else
549 int retval = TestSuite(argc, argv).Run();
550 #endif
551 return retval;