[email protected] | b7243c4 | 2010-07-23 05:23:13 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 645aaa5 | 2009-02-07 01:03:05 | [diff] [blame] | 5 | #include "build/build_config.h" |
| 6 | |
[email protected] | d4651ff | 2008-12-02 16:51:58 | [diff] [blame] | 7 | #if defined(OS_WIN) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 8 | #include <windows.h> |
[email protected] | 43cf815 | 2009-02-07 00:57:45 | [diff] [blame] | 9 | #elif defined(OS_POSIX) |
| 10 | #include <sys/types.h> |
[email protected] | 43cf815 | 2009-02-07 00:57:45 | [diff] [blame] | 11 | #include <unistd.h> |
[email protected] | d4651ff | 2008-12-02 16:51:58 | [diff] [blame] | 12 | #endif |
[email protected] | 43cf815 | 2009-02-07 00:57:45 | [diff] [blame] | 13 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 14 | #include <stdio.h> |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 15 | #include <string> |
[email protected] | b7243c4 | 2010-07-23 05:23:13 | [diff] [blame] | 16 | #include <utility> |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 17 | |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 18 | #include "ipc/ipc_tests.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 19 | |
| 20 | #include "base/base_switches.h" |
| 21 | #include "base/command_line.h" |
| 22 | #include "base/debug_on_start.h" |
| 23 | #include "base/perftimer.h" |
[email protected] | fb895c6 | 2009-10-09 18:20:30 | [diff] [blame] | 24 | #include "base/test/perf_test_suite.h" |
| 25 | #include "base/test/test_suite.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 26 | #include "base/thread.h" |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 27 | #include "ipc/ipc_descriptors.h" |
| 28 | #include "ipc/ipc_channel.h" |
| 29 | #include "ipc/ipc_channel_proxy.h" |
| 30 | #include "ipc/ipc_message_utils.h" |
| 31 | #include "ipc/ipc_switches.h" |
[email protected] | 95cb7fb9 | 2008-12-09 22:00:47 | [diff] [blame] | 32 | #include "testing/multiprocess_func_list.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 33 | |
| 34 | // Define to enable IPC performance testing instead of the regular unit tests |
| 35 | // #define PERFORMANCE_TEST |
| 36 | |
[email protected] | 9a3a293b | 2009-06-04 22:28:16 | [diff] [blame] | 37 | const char kTestClientChannel[] = "T1"; |
| 38 | const char kReflectorChannel[] = "T2"; |
| 39 | const char kFuzzerChannel[] = "F3"; |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 40 | const char kSyncSocketChannel[] = "S4"; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 41 | |
[email protected] | c2f10ed | 2009-02-10 00:52:57 | [diff] [blame] | 42 | const size_t kLongMessageStringNumBytes = 50000; |
| 43 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 44 | #ifndef PERFORMANCE_TEST |
| 45 | |
[email protected] | 95cb7fb9 | 2008-12-09 22:00:47 | [diff] [blame] | 46 | void IPCChannelTest::SetUp() { |
| 47 | MultiProcessTest::SetUp(); |
| 48 | |
| 49 | // Construct a fresh IO Message loop for the duration of each test. |
| 50 | message_loop_ = new MessageLoopForIO(); |
| 51 | } |
| 52 | |
| 53 | void IPCChannelTest::TearDown() { |
| 54 | delete message_loop_; |
| 55 | message_loop_ = NULL; |
| 56 | |
| 57 | MultiProcessTest::TearDown(); |
| 58 | } |
| 59 | |
[email protected] | df3c1ca1 | 2008-12-19 21:37:01 | [diff] [blame] | 60 | #if defined(OS_WIN) |
| 61 | base::ProcessHandle IPCChannelTest::SpawnChild(ChildType child_type, |
| 62 | IPC::Channel *channel) { |
[email protected] | 95cb7fb9 | 2008-12-09 22:00:47 | [diff] [blame] | 63 | // kDebugChildren support. |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 64 | bool debug_on_start = |
| 65 | CommandLine::ForCurrentProcess()->HasSwitch(switches::kDebugChildren); |
[email protected] | 95cb7fb9 | 2008-12-09 22:00:47 | [diff] [blame] | 66 | |
| 67 | switch (child_type) { |
| 68 | case TEST_CLIENT: |
[email protected] | d420c31e | 2010-07-30 02:14:22 | [diff] [blame] | 69 | return MultiProcessTest::SpawnChild("RunTestClient", debug_on_start); |
[email protected] | 95cb7fb9 | 2008-12-09 22:00:47 | [diff] [blame] | 70 | case TEST_REFLECTOR: |
[email protected] | d420c31e | 2010-07-30 02:14:22 | [diff] [blame] | 71 | return MultiProcessTest::SpawnChild("RunReflector", debug_on_start); |
[email protected] | 95cb7fb9 | 2008-12-09 22:00:47 | [diff] [blame] | 72 | case FUZZER_SERVER: |
[email protected] | d420c31e | 2010-07-30 02:14:22 | [diff] [blame] | 73 | return MultiProcessTest::SpawnChild("RunFuzzServer", debug_on_start); |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 74 | case SYNC_SOCKET_SERVER: |
[email protected] | d420c31e | 2010-07-30 02:14:22 | [diff] [blame] | 75 | return MultiProcessTest::SpawnChild("RunSyncSocketServer", debug_on_start); |
[email protected] | 95cb7fb9 | 2008-12-09 22:00:47 | [diff] [blame] | 76 | default: |
| 77 | return NULL; |
[email protected] | 95cb7fb9 | 2008-12-09 22:00:47 | [diff] [blame] | 78 | } |
| 79 | } |
[email protected] | df3c1ca1 | 2008-12-19 21:37:01 | [diff] [blame] | 80 | #elif defined(OS_POSIX) |
| 81 | base::ProcessHandle IPCChannelTest::SpawnChild(ChildType child_type, |
| 82 | IPC::Channel *channel) { |
| 83 | // kDebugChildren support. |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 84 | bool debug_on_start = |
| 85 | CommandLine::ForCurrentProcess()->HasSwitch(switches::kDebugChildren); |
[email protected] | df3c1ca1 | 2008-12-19 21:37:01 | [diff] [blame] | 86 | |
| 87 | base::file_handle_mapping_vector fds_to_map; |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 88 | const int ipcfd = channel->GetClientFileDescriptor(); |
| 89 | if (ipcfd > -1) { |
[email protected] | b7243c4 | 2010-07-23 05:23:13 | [diff] [blame] | 90 | fds_to_map.push_back(std::pair<int, int>(ipcfd, kPrimaryIPCChannel + 3)); |
[email protected] | df3c1ca1 | 2008-12-19 21:37:01 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | base::ProcessHandle ret = NULL; |
| 94 | switch (child_type) { |
| 95 | case TEST_CLIENT: |
[email protected] | d420c31e | 2010-07-30 02:14:22 | [diff] [blame] | 96 | ret = MultiProcessTest::SpawnChild("RunTestClient", |
[email protected] | df3c1ca1 | 2008-12-19 21:37:01 | [diff] [blame] | 97 | fds_to_map, |
| 98 | debug_on_start); |
[email protected] | df3c1ca1 | 2008-12-19 21:37:01 | [diff] [blame] | 99 | break; |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 100 | case TEST_DESCRIPTOR_CLIENT: |
[email protected] | d420c31e | 2010-07-30 02:14:22 | [diff] [blame] | 101 | ret = MultiProcessTest::SpawnChild("RunTestDescriptorClient", |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 102 | fds_to_map, |
| 103 | debug_on_start); |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 104 | break; |
[email protected] | e8351b7e | 2009-02-10 22:25:39 | [diff] [blame] | 105 | case TEST_DESCRIPTOR_CLIENT_SANDBOXED: |
[email protected] | d420c31e | 2010-07-30 02:14:22 | [diff] [blame] | 106 | ret = MultiProcessTest::SpawnChild("RunTestDescriptorClientSandboxed", |
[email protected] | e8351b7e | 2009-02-10 22:25:39 | [diff] [blame] | 107 | fds_to_map, |
| 108 | debug_on_start); |
[email protected] | e8351b7e | 2009-02-10 22:25:39 | [diff] [blame] | 109 | break; |
[email protected] | df3c1ca1 | 2008-12-19 21:37:01 | [diff] [blame] | 110 | case TEST_REFLECTOR: |
[email protected] | d420c31e | 2010-07-30 02:14:22 | [diff] [blame] | 111 | ret = MultiProcessTest::SpawnChild("RunReflector", |
[email protected] | df3c1ca1 | 2008-12-19 21:37:01 | [diff] [blame] | 112 | fds_to_map, |
| 113 | debug_on_start); |
[email protected] | df3c1ca1 | 2008-12-19 21:37:01 | [diff] [blame] | 114 | break; |
| 115 | case FUZZER_SERVER: |
[email protected] | d420c31e | 2010-07-30 02:14:22 | [diff] [blame] | 116 | ret = MultiProcessTest::SpawnChild("RunFuzzServer", |
[email protected] | df3c1ca1 | 2008-12-19 21:37:01 | [diff] [blame] | 117 | fds_to_map, |
| 118 | debug_on_start); |
[email protected] | df3c1ca1 | 2008-12-19 21:37:01 | [diff] [blame] | 119 | break; |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 120 | case SYNC_SOCKET_SERVER: |
[email protected] | d420c31e | 2010-07-30 02:14:22 | [diff] [blame] | 121 | ret = MultiProcessTest::SpawnChild("RunSyncSocketServer", |
[email protected] | 0840cc7 | 2009-11-24 16:14:53 | [diff] [blame] | 122 | fds_to_map, |
| 123 | debug_on_start); |
| 124 | break; |
[email protected] | df3c1ca1 | 2008-12-19 21:37:01 | [diff] [blame] | 125 | default: |
| 126 | return NULL; |
| 127 | break; |
| 128 | } |
| 129 | return ret; |
| 130 | } |
| 131 | #endif // defined(OS_POSIX) |
[email protected] | 95cb7fb9 | 2008-12-09 22:00:47 | [diff] [blame] | 132 | |
| 133 | TEST_F(IPCChannelTest, BasicMessageTest) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 134 | int v1 = 10; |
| 135 | std::string v2("foobar"); |
| 136 | std::wstring v3(L"hello world"); |
| 137 | |
| 138 | IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL); |
| 139 | EXPECT_TRUE(m.WriteInt(v1)); |
| 140 | EXPECT_TRUE(m.WriteString(v2)); |
| 141 | EXPECT_TRUE(m.WriteWString(v3)); |
| 142 | |
| 143 | void* iter = NULL; |
| 144 | |
| 145 | int vi; |
| 146 | std::string vs; |
| 147 | std::wstring vw; |
| 148 | |
| 149 | EXPECT_TRUE(m.ReadInt(&iter, &vi)); |
| 150 | EXPECT_EQ(v1, vi); |
| 151 | |
| 152 | EXPECT_TRUE(m.ReadString(&iter, &vs)); |
| 153 | EXPECT_EQ(v2, vs); |
| 154 | |
| 155 | EXPECT_TRUE(m.ReadWString(&iter, &vw)); |
| 156 | EXPECT_EQ(v3, vw); |
| 157 | |
| 158 | // should fail |
| 159 | EXPECT_FALSE(m.ReadInt(&iter, &vi)); |
| 160 | EXPECT_FALSE(m.ReadString(&iter, &vs)); |
| 161 | EXPECT_FALSE(m.ReadWString(&iter, &vw)); |
| 162 | } |
| 163 | |
| 164 | static void Send(IPC::Message::Sender* sender, const char* text) { |
| 165 | static int message_index = 0; |
| 166 | |
| 167 | IPC::Message* message = new IPC::Message(0, |
| 168 | 2, |
| 169 | IPC::Message::PRIORITY_NORMAL); |
| 170 | message->WriteInt(message_index++); |
| 171 | message->WriteString(std::string(text)); |
| 172 | |
[email protected] | 3d1b666 | 2009-01-29 17:03:11 | [diff] [blame] | 173 | // Make sure we can handle large messages. |
[email protected] | c2f10ed | 2009-02-10 00:52:57 | [diff] [blame] | 174 | char junk[kLongMessageStringNumBytes]; |
[email protected] | 3d1b666 | 2009-01-29 17:03:11 | [diff] [blame] | 175 | memset(junk, 'a', sizeof(junk)-1); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 176 | junk[sizeof(junk)-1] = 0; |
| 177 | message->WriteString(std::string(junk)); |
| 178 | |
| 179 | // DEBUG: printf("[%u] sending message [%s]\n", GetCurrentProcessId(), text); |
| 180 | sender->Send(message); |
| 181 | } |
| 182 | |
| 183 | class MyChannelListener : public IPC::Channel::Listener { |
| 184 | public: |
| 185 | virtual void OnMessageReceived(const IPC::Message& message) { |
| 186 | IPC::MessageIterator iter(message); |
| 187 | |
[email protected] | d4651ff | 2008-12-02 16:51:58 | [diff] [blame] | 188 | iter.NextInt(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 189 | const std::string data = iter.NextString(); |
[email protected] | c2f10ed | 2009-02-10 00:52:57 | [diff] [blame] | 190 | const std::string big_string = iter.NextString(); |
| 191 | EXPECT_EQ(kLongMessageStringNumBytes - 1, big_string.length()); |
| 192 | |
| 193 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 194 | if (--messages_left_ == 0) { |
| 195 | MessageLoop::current()->Quit(); |
| 196 | } else { |
| 197 | Send(sender_, "Foo"); |
| 198 | } |
| 199 | } |
| 200 | |
[email protected] | b6be588 | 2008-11-07 21:53:03 | [diff] [blame] | 201 | virtual void OnChannelError() { |
| 202 | // There is a race when closing the channel so the last message may be lost. |
| 203 | EXPECT_LE(messages_left_, 1); |
| 204 | MessageLoop::current()->Quit(); |
| 205 | } |
| 206 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 207 | void Init(IPC::Message::Sender* s) { |
| 208 | sender_ = s; |
| 209 | messages_left_ = 50; |
| 210 | } |
| 211 | |
| 212 | private: |
| 213 | IPC::Message::Sender* sender_; |
| 214 | int messages_left_; |
| 215 | }; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 216 | |
[email protected] | 95cb7fb9 | 2008-12-09 22:00:47 | [diff] [blame] | 217 | TEST_F(IPCChannelTest, ChannelTest) { |
[email protected] | c2f10ed | 2009-02-10 00:52:57 | [diff] [blame] | 218 | MyChannelListener channel_listener; |
[email protected] | 3d1b666 | 2009-01-29 17:03:11 | [diff] [blame] | 219 | // Setup IPC channel. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 220 | IPC::Channel chan(kTestClientChannel, IPC::Channel::MODE_SERVER, |
| 221 | &channel_listener); |
[email protected] | 39703fb | 2010-10-19 19:11:15 | [diff] [blame^] | 222 | ASSERT_TRUE(chan.Connect()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 223 | |
| 224 | channel_listener.Init(&chan); |
| 225 | |
[email protected] | df3c1ca1 | 2008-12-19 21:37:01 | [diff] [blame] | 226 | base::ProcessHandle process_handle = SpawnChild(TEST_CLIENT, &chan); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 227 | ASSERT_TRUE(process_handle); |
| 228 | |
| 229 | Send(&chan, "hello from parent"); |
| 230 | |
[email protected] | 3d1b666 | 2009-01-29 17:03:11 | [diff] [blame] | 231 | // Run message loop. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 232 | MessageLoop::current()->Run(); |
| 233 | |
[email protected] | 3d1b666 | 2009-01-29 17:03:11 | [diff] [blame] | 234 | // Close Channel so client gets its OnChannelError() callback fired. |
| 235 | chan.Close(); |
| 236 | |
| 237 | // Cleanup child process. |
[email protected] | 95cb7fb9 | 2008-12-09 22:00:47 | [diff] [blame] | 238 | EXPECT_TRUE(base::WaitForSingleProcess(process_handle, 5000)); |
[email protected] | cd4fd15 | 2009-02-09 19:28:41 | [diff] [blame] | 239 | base::CloseProcessHandle(process_handle); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 240 | } |
| 241 | |
[email protected] | 95cb7fb9 | 2008-12-09 22:00:47 | [diff] [blame] | 242 | TEST_F(IPCChannelTest, ChannelProxyTest) { |
[email protected] | c2f10ed | 2009-02-10 00:52:57 | [diff] [blame] | 243 | MyChannelListener channel_listener; |
| 244 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 245 | // The thread needs to out-live the ChannelProxy. |
[email protected] | ab820df | 2008-08-26 05:55:10 | [diff] [blame] | 246 | base::Thread thread("ChannelProxyTestServer"); |
| 247 | base::Thread::Options options; |
| 248 | options.message_loop_type = MessageLoop::TYPE_IO; |
| 249 | thread.StartWithOptions(options); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 250 | { |
| 251 | // setup IPC channel proxy |
| 252 | IPC::ChannelProxy chan(kTestClientChannel, IPC::Channel::MODE_SERVER, |
| 253 | &channel_listener, NULL, thread.message_loop()); |
| 254 | |
| 255 | channel_listener.Init(&chan); |
| 256 | |
[email protected] | e74488c | 2008-12-22 22:43:41 | [diff] [blame] | 257 | #if defined(OS_WIN) |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 258 | base::ProcessHandle process_handle = SpawnChild(TEST_CLIENT, NULL); |
[email protected] | e74488c | 2008-12-22 22:43:41 | [diff] [blame] | 259 | #elif defined(OS_POSIX) |
[email protected] | bb97536 | 2009-01-21 01:00:22 | [diff] [blame] | 260 | bool debug_on_start = CommandLine::ForCurrentProcess()->HasSwitch( |
| 261 | switches::kDebugChildren); |
[email protected] | e74488c | 2008-12-22 22:43:41 | [diff] [blame] | 262 | base::file_handle_mapping_vector fds_to_map; |
[email protected] | cc8f146 | 2009-06-12 17:36:55 | [diff] [blame] | 263 | const int ipcfd = chan.GetClientFileDescriptor(); |
| 264 | if (ipcfd > -1) { |
[email protected] | b7243c4 | 2010-07-23 05:23:13 | [diff] [blame] | 265 | fds_to_map.push_back(std::pair<int, int>(ipcfd, kPrimaryIPCChannel + 3)); |
[email protected] | e74488c | 2008-12-22 22:43:41 | [diff] [blame] | 266 | } |
| 267 | |
[email protected] | df3c1ca1 | 2008-12-19 21:37:01 | [diff] [blame] | 268 | base::ProcessHandle process_handle = MultiProcessTest::SpawnChild( |
[email protected] | d420c31e | 2010-07-30 02:14:22 | [diff] [blame] | 269 | "RunTestClient", |
[email protected] | e74488c | 2008-12-22 22:43:41 | [diff] [blame] | 270 | fds_to_map, |
[email protected] | df3c1ca1 | 2008-12-19 21:37:01 | [diff] [blame] | 271 | debug_on_start); |
[email protected] | 1db97ce7 | 2009-08-21 21:53:54 | [diff] [blame] | 272 | #endif // defined(OS_POSIX) |
[email protected] | e74488c | 2008-12-22 22:43:41 | [diff] [blame] | 273 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 274 | ASSERT_TRUE(process_handle); |
| 275 | |
| 276 | Send(&chan, "hello from parent"); |
| 277 | |
| 278 | // run message loop |
| 279 | MessageLoop::current()->Run(); |
| 280 | |
| 281 | // cleanup child process |
[email protected] | df3c1ca1 | 2008-12-19 21:37:01 | [diff] [blame] | 282 | EXPECT_TRUE(base::WaitForSingleProcess(process_handle, 5000)); |
[email protected] | cd4fd15 | 2009-02-09 19:28:41 | [diff] [blame] | 283 | base::CloseProcessHandle(process_handle); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 284 | } |
| 285 | thread.Stop(); |
| 286 | } |
| 287 | |
[email protected] | 052fd45 | 2009-09-23 17:56:25 | [diff] [blame] | 288 | class ChannelListenerWithOnConnectedSend : public IPC::Channel::Listener { |
| 289 | public: |
| 290 | virtual void OnChannelConnected(int32 peer_pid) { |
| 291 | SendNextMessage(); |
| 292 | } |
| 293 | |
| 294 | virtual void OnMessageReceived(const IPC::Message& message) { |
| 295 | IPC::MessageIterator iter(message); |
| 296 | |
| 297 | iter.NextInt(); |
| 298 | const std::string data = iter.NextString(); |
| 299 | const std::string big_string = iter.NextString(); |
| 300 | EXPECT_EQ(kLongMessageStringNumBytes - 1, big_string.length()); |
| 301 | SendNextMessage(); |
| 302 | } |
| 303 | |
| 304 | virtual void OnChannelError() { |
| 305 | // There is a race when closing the channel so the last message may be lost. |
| 306 | EXPECT_LE(messages_left_, 1); |
| 307 | MessageLoop::current()->Quit(); |
| 308 | } |
| 309 | |
| 310 | void Init(IPC::Message::Sender* s) { |
| 311 | sender_ = s; |
| 312 | messages_left_ = 50; |
| 313 | } |
| 314 | |
| 315 | private: |
| 316 | void SendNextMessage() { |
| 317 | if (--messages_left_ == 0) { |
| 318 | MessageLoop::current()->Quit(); |
| 319 | } else { |
| 320 | Send(sender_, "Foo"); |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | IPC::Message::Sender* sender_; |
| 325 | int messages_left_; |
| 326 | }; |
| 327 | |
| 328 | TEST_F(IPCChannelTest, SendMessageInChannelConnected) { |
| 329 | // This tests the case of a listener sending back an event in it's |
| 330 | // OnChannelConnected handler. |
| 331 | |
| 332 | ChannelListenerWithOnConnectedSend channel_listener; |
| 333 | // Setup IPC channel. |
| 334 | IPC::Channel channel(kTestClientChannel, IPC::Channel::MODE_SERVER, |
| 335 | &channel_listener); |
| 336 | channel_listener.Init(&channel); |
[email protected] | 39703fb | 2010-10-19 19:11:15 | [diff] [blame^] | 337 | ASSERT_TRUE(channel.Connect()); |
[email protected] | 052fd45 | 2009-09-23 17:56:25 | [diff] [blame] | 338 | |
| 339 | base::ProcessHandle process_handle = SpawnChild(TEST_CLIENT, &channel); |
| 340 | ASSERT_TRUE(process_handle); |
| 341 | |
| 342 | Send(&channel, "hello from parent"); |
| 343 | |
| 344 | // Run message loop. |
| 345 | MessageLoop::current()->Run(); |
| 346 | |
| 347 | // Close Channel so client gets its OnChannelError() callback fired. |
| 348 | channel.Close(); |
| 349 | |
| 350 | // Cleanup child process. |
| 351 | EXPECT_TRUE(base::WaitForSingleProcess(process_handle, 5000)); |
| 352 | base::CloseProcessHandle(process_handle); |
| 353 | } |
| 354 | |
[email protected] | 95cb7fb9 | 2008-12-09 22:00:47 | [diff] [blame] | 355 | MULTIPROCESS_TEST_MAIN(RunTestClient) { |
| 356 | MessageLoopForIO main_message_loop; |
[email protected] | c2f10ed | 2009-02-10 00:52:57 | [diff] [blame] | 357 | MyChannelListener channel_listener; |
[email protected] | 95cb7fb9 | 2008-12-09 22:00:47 | [diff] [blame] | 358 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 359 | // setup IPC channel |
| 360 | IPC::Channel chan(kTestClientChannel, IPC::Channel::MODE_CLIENT, |
| 361 | &channel_listener); |
[email protected] | 39703fb | 2010-10-19 19:11:15 | [diff] [blame^] | 362 | CHECK(chan.Connect()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 363 | channel_listener.Init(&chan); |
| 364 | Send(&chan, "hello from child"); |
| 365 | // run message loop |
| 366 | MessageLoop::current()->Run(); |
[email protected] | 95cb7fb9 | 2008-12-09 22:00:47 | [diff] [blame] | 367 | // return true; |
| 368 | return NULL; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 369 | } |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 370 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 371 | #endif // !PERFORMANCE_TEST |
| 372 | |
| 373 | #ifdef PERFORMANCE_TEST |
| 374 | |
| 375 | //----------------------------------------------------------------------------- |
| 376 | // Manually performance test |
| 377 | // |
| 378 | // This test times the roundtrip IPC message cycle. It is enabled with a |
| 379 | // special preprocessor define to enable it instead of the standard IPC |
| 380 | // unit tests. This works around some funny termination conditions in the |
| 381 | // regular unit tests. |
| 382 | // |
| 383 | // This test is not automated. To test, you will want to vary the message |
| 384 | // count and message size in TEST to get the numbers you want. |
| 385 | // |
| 386 | // FIXME(brettw): Automate this test and have it run by default. |
| 387 | |
| 388 | // This channel listener just replies to all messages with the exact same |
| 389 | // message. It assumes each message has one string parameter. When the string |
| 390 | // "quit" is sent, it will exit. |
| 391 | class ChannelReflectorListener : public IPC::Channel::Listener { |
| 392 | public: |
[email protected] | 95cb7fb9 | 2008-12-09 22:00:47 | [diff] [blame] | 393 | explicit ChannelReflectorListener(IPC::Channel *channel) : |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 394 | channel_(channel), |
| 395 | count_messages_(0), |
| 396 | latency_messages_(0) { |
| 397 | std::cout << "Reflector up" << std::endl; |
| 398 | } |
| 399 | |
| 400 | ~ChannelReflectorListener() { |
| 401 | std::cout << "Client Messages: " << count_messages_ << std::endl; |
| 402 | std::cout << "Client Latency: " << latency_messages_ << std::endl; |
| 403 | } |
| 404 | |
| 405 | virtual void OnMessageReceived(const IPC::Message& message) { |
| 406 | count_messages_++; |
| 407 | IPC::MessageIterator iter(message); |
| 408 | int time = iter.NextInt(); |
| 409 | int msgid = iter.NextInt(); |
| 410 | std::string payload = iter.NextString(); |
| 411 | latency_messages_ += GetTickCount() - time; |
| 412 | |
| 413 | // cout << "reflector msg received: " << msgid << endl; |
| 414 | if (payload == "quit") |
| 415 | MessageLoop::current()->Quit(); |
| 416 | |
| 417 | IPC::Message* msg = new IPC::Message(0, |
| 418 | 2, |
| 419 | IPC::Message::PRIORITY_NORMAL); |
| 420 | msg->WriteInt(GetTickCount()); |
| 421 | msg->WriteInt(msgid); |
| 422 | msg->WriteString(payload); |
| 423 | channel_->Send(msg); |
| 424 | } |
| 425 | private: |
| 426 | IPC::Channel *channel_; |
| 427 | int count_messages_; |
| 428 | int latency_messages_; |
| 429 | }; |
| 430 | |
| 431 | class ChannelPerfListener : public IPC::Channel::Listener { |
| 432 | public: |
| 433 | ChannelPerfListener(IPC::Channel* channel, int msg_count, int msg_size) : |
| 434 | count_down_(msg_count), |
| 435 | channel_(channel), |
| 436 | count_messages_(0), |
| 437 | latency_messages_(0) { |
| 438 | payload_.resize(msg_size); |
| 439 | for (int i = 0; i < static_cast<int>(payload_.size()); i++) |
| 440 | payload_[i] = 'a'; |
| 441 | std::cout << "perflistener up" << std::endl; |
| 442 | } |
| 443 | |
| 444 | ~ChannelPerfListener() { |
| 445 | std::cout << "Server Messages: " << count_messages_ << std::endl; |
| 446 | std::cout << "Server Latency: " << latency_messages_ << std::endl; |
| 447 | } |
| 448 | |
| 449 | virtual void OnMessageReceived(const IPC::Message& message) { |
| 450 | count_messages_++; |
| 451 | // decode the string so this gets counted in the total time |
| 452 | IPC::MessageIterator iter(message); |
| 453 | int time = iter.NextInt(); |
| 454 | int msgid = iter.NextInt(); |
| 455 | std::string cur = iter.NextString(); |
| 456 | latency_messages_ += GetTickCount() - time; |
| 457 | |
| 458 | // cout << "perflistener got message" << endl; |
| 459 | |
| 460 | count_down_--; |
| 461 | if (count_down_ == 0) { |
| 462 | IPC::Message* msg = new IPC::Message(0, |
| 463 | 2, |
| 464 | IPC::Message::PRIORITY_NORMAL); |
| 465 | msg->WriteInt(GetTickCount()); |
| 466 | msg->WriteInt(count_down_); |
| 467 | msg->WriteString("quit"); |
| 468 | channel_->Send(msg); |
| 469 | SetTimer(NULL, 1, 250, (TIMERPROC) PostQuitMessage); |
| 470 | return; |
| 471 | } |
| 472 | |
| 473 | IPC::Message* msg = new IPC::Message(0, |
| 474 | 2, |
| 475 | IPC::Message::PRIORITY_NORMAL); |
| 476 | msg->WriteInt(GetTickCount()); |
| 477 | msg->WriteInt(count_down_); |
| 478 | msg->WriteString(payload_); |
| 479 | channel_->Send(msg); |
| 480 | } |
| 481 | |
| 482 | private: |
| 483 | int count_down_; |
| 484 | std::string payload_; |
| 485 | IPC::Channel *channel_; |
| 486 | int count_messages_; |
| 487 | int latency_messages_; |
| 488 | }; |
| 489 | |
[email protected] | 95cb7fb9 | 2008-12-09 22:00:47 | [diff] [blame] | 490 | TEST_F(IPCChannelTest, Performance) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 491 | // setup IPC channel |
| 492 | IPC::Channel chan(kReflectorChannel, IPC::Channel::MODE_SERVER, NULL); |
| 493 | ChannelPerfListener perf_listener(&chan, 10000, 100000); |
| 494 | chan.set_listener(&perf_listener); |
[email protected] | 39703fb | 2010-10-19 19:11:15 | [diff] [blame^] | 495 | ASSERT_TRUE(chan.Connect()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 496 | |
[email protected] | df3c1ca1 | 2008-12-19 21:37:01 | [diff] [blame] | 497 | HANDLE process = SpawnChild(TEST_REFLECTOR, &chan); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 498 | ASSERT_TRUE(process); |
| 499 | |
[email protected] | ec7690f | 2009-04-28 00:11:27 | [diff] [blame] | 500 | PlatformThread::Sleep(1000); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 501 | |
| 502 | PerfTimeLogger logger("IPC_Perf"); |
| 503 | |
| 504 | // this initial message will kick-start the ping-pong of messages |
| 505 | IPC::Message* message = new IPC::Message(0, |
| 506 | 2, |
| 507 | IPC::Message::PRIORITY_NORMAL); |
| 508 | message->WriteInt(GetTickCount()); |
| 509 | message->WriteInt(-1); |
| 510 | message->WriteString("Hello"); |
| 511 | chan.Send(message); |
| 512 | |
| 513 | // run message loop |
| 514 | MessageLoop::current()->Run(); |
| 515 | |
| 516 | // cleanup child process |
| 517 | WaitForSingleObject(process, 5000); |
| 518 | CloseHandle(process); |
| 519 | } |
| 520 | |
| 521 | // This message loop bounces all messages back to the sender |
[email protected] | 95cb7fb9 | 2008-12-09 22:00:47 | [diff] [blame] | 522 | MULTIPROCESS_TEST_MAIN(RunReflector) { |
| 523 | MessageLoopForIO main_message_loop; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 524 | IPC::Channel chan(kReflectorChannel, IPC::Channel::MODE_CLIENT, NULL); |
| 525 | ChannelReflectorListener channel_reflector_listener(&chan); |
| 526 | chan.set_listener(&channel_reflector_listener); |
[email protected] | 39703fb | 2010-10-19 19:11:15 | [diff] [blame^] | 527 | ASSERT_TRUE(chan.Connect()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 528 | |
| 529 | MessageLoop::current()->Run(); |
| 530 | return true; |
| 531 | } |
| 532 | |
| 533 | #endif // PERFORMANCE_TEST |
| 534 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 535 | int main(int argc, char** argv) { |
[email protected] | 95cb7fb9 | 2008-12-09 22:00:47 | [diff] [blame] | 536 | #ifdef PERFORMANCE_TEST |
[email protected] | 99cc51e | 2010-10-10 00:21:35 | [diff] [blame] | 537 | int retval = base::PerfTestSuite(argc, argv).Run(); |
[email protected] | 302831b | 2009-01-13 22:35:10 | [diff] [blame] | 538 | #else |
[email protected] | 20e1491 | 2010-08-17 19:40:11 | [diff] [blame] | 539 | int retval = base::TestSuite(argc, argv).Run(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 540 | #endif |
[email protected] | 95cb7fb9 | 2008-12-09 22:00:47 | [diff] [blame] | 541 | return retval; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 542 | } |