viettrungluu@chromium.org | 7d86af2 | 2013-01-12 00:13:37 +0900 | [diff] [blame^] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "build/build_config.h" |
| 6 | |
| 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 |
| 13 | |
| 14 | #include <stdio.h> |
| 15 | #include <string> |
| 16 | #include <utility> |
| 17 | |
| 18 | #include "base/base_switches.h" |
| 19 | #include "base/command_line.h" |
| 20 | #include "base/debug/debug_on_start_win.h" |
| 21 | #include "base/perftimer.h" |
| 22 | #include "base/pickle.h" |
| 23 | #include "base/test/perf_test_suite.h" |
| 24 | #include "base/test/test_suite.h" |
| 25 | #include "base/threading/thread.h" |
| 26 | #include "base/time.h" |
| 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_multiprocess_test.h" |
| 32 | #include "ipc/ipc_sender.h" |
| 33 | #include "ipc/ipc_switches.h" |
| 34 | #include "ipc/ipc_test_base.h" |
| 35 | #include "testing/multiprocess_func_list.h" |
| 36 | |
| 37 | const size_t kLongMessageStringNumBytes = 50000; |
| 38 | |
| 39 | class IPCChannelTest : public IPCTestBase { |
| 40 | }; |
| 41 | |
| 42 | TEST_F(IPCChannelTest, BasicMessageTest) { |
| 43 | int v1 = 10; |
| 44 | std::string v2("foobar"); |
| 45 | std::wstring v3(L"hello world"); |
| 46 | |
| 47 | IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL); |
| 48 | EXPECT_TRUE(m.WriteInt(v1)); |
| 49 | EXPECT_TRUE(m.WriteString(v2)); |
| 50 | EXPECT_TRUE(m.WriteWString(v3)); |
| 51 | |
| 52 | PickleIterator iter(m); |
| 53 | |
| 54 | int vi; |
| 55 | std::string vs; |
| 56 | std::wstring vw; |
| 57 | |
| 58 | EXPECT_TRUE(m.ReadInt(&iter, &vi)); |
| 59 | EXPECT_EQ(v1, vi); |
| 60 | |
| 61 | EXPECT_TRUE(m.ReadString(&iter, &vs)); |
| 62 | EXPECT_EQ(v2, vs); |
| 63 | |
| 64 | EXPECT_TRUE(m.ReadWString(&iter, &vw)); |
| 65 | EXPECT_EQ(v3, vw); |
| 66 | |
| 67 | // should fail |
| 68 | EXPECT_FALSE(m.ReadInt(&iter, &vi)); |
| 69 | EXPECT_FALSE(m.ReadString(&iter, &vs)); |
| 70 | EXPECT_FALSE(m.ReadWString(&iter, &vw)); |
| 71 | } |
| 72 | |
| 73 | static void Send(IPC::Sender* sender, const char* text) { |
| 74 | static int message_index = 0; |
| 75 | |
| 76 | IPC::Message* message = new IPC::Message(0, |
| 77 | 2, |
| 78 | IPC::Message::PRIORITY_NORMAL); |
| 79 | message->WriteInt(message_index++); |
| 80 | message->WriteString(std::string(text)); |
| 81 | |
| 82 | // Make sure we can handle large messages. |
| 83 | char junk[kLongMessageStringNumBytes]; |
| 84 | memset(junk, 'a', sizeof(junk)-1); |
| 85 | junk[sizeof(junk)-1] = 0; |
| 86 | message->WriteString(std::string(junk)); |
| 87 | |
| 88 | // DEBUG: printf("[%u] sending message [%s]\n", GetCurrentProcessId(), text); |
| 89 | sender->Send(message); |
| 90 | } |
| 91 | |
| 92 | class MyChannelListener : public IPC::Listener { |
| 93 | public: |
| 94 | virtual bool OnMessageReceived(const IPC::Message& message) { |
| 95 | PickleIterator iter(message); |
| 96 | |
| 97 | int ignored; |
| 98 | EXPECT_TRUE(iter.ReadInt(&ignored)); |
| 99 | std::string data; |
| 100 | EXPECT_TRUE(iter.ReadString(&data)); |
| 101 | std::string big_string; |
| 102 | EXPECT_TRUE(iter.ReadString(&big_string)); |
| 103 | EXPECT_EQ(kLongMessageStringNumBytes - 1, big_string.length()); |
| 104 | |
| 105 | |
| 106 | if (--messages_left_ == 0) { |
| 107 | MessageLoop::current()->Quit(); |
| 108 | } else { |
| 109 | Send(sender_, "Foo"); |
| 110 | } |
| 111 | return true; |
| 112 | } |
| 113 | |
| 114 | virtual void OnChannelError() { |
| 115 | // There is a race when closing the channel so the last message may be lost. |
| 116 | EXPECT_LE(messages_left_, 1); |
| 117 | MessageLoop::current()->Quit(); |
| 118 | } |
| 119 | |
| 120 | void Init(IPC::Sender* s) { |
| 121 | sender_ = s; |
| 122 | messages_left_ = 50; |
| 123 | } |
| 124 | |
| 125 | private: |
| 126 | IPC::Sender* sender_; |
| 127 | int messages_left_; |
| 128 | }; |
| 129 | |
| 130 | TEST_F(IPCChannelTest, ChannelTest) { |
| 131 | MyChannelListener channel_listener; |
| 132 | // Setup IPC channel. |
| 133 | IPC::Channel chan(kTestClientChannel, IPC::Channel::MODE_SERVER, |
| 134 | &channel_listener); |
| 135 | ASSERT_TRUE(chan.Connect()); |
| 136 | |
| 137 | channel_listener.Init(&chan); |
| 138 | |
| 139 | base::ProcessHandle process_handle = SpawnChild(TEST_CLIENT, &chan); |
| 140 | ASSERT_TRUE(process_handle); |
| 141 | |
| 142 | Send(&chan, "hello from parent"); |
| 143 | |
| 144 | // Run message loop. |
| 145 | MessageLoop::current()->Run(); |
| 146 | |
| 147 | // Close Channel so client gets its OnChannelError() callback fired. |
| 148 | chan.Close(); |
| 149 | |
| 150 | // Cleanup child process. |
| 151 | EXPECT_TRUE(base::WaitForSingleProcess( |
| 152 | process_handle, base::TimeDelta::FromSeconds(5))); |
| 153 | base::CloseProcessHandle(process_handle); |
| 154 | } |
| 155 | |
| 156 | #if defined(OS_WIN) |
| 157 | TEST_F(IPCChannelTest, ChannelTestExistingPipe) { |
| 158 | MyChannelListener channel_listener; |
| 159 | // Setup IPC channel with existing pipe. Specify name in Chrome format. |
| 160 | std::string name("\\\\.\\pipe\\chrome."); |
| 161 | name.append(kTestClientChannel); |
| 162 | const DWORD open_mode = PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED | |
| 163 | FILE_FLAG_FIRST_PIPE_INSTANCE; |
| 164 | HANDLE pipe = CreateNamedPipeA(name.c_str(), |
| 165 | open_mode, |
| 166 | PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, |
| 167 | 1, |
| 168 | 4096, |
| 169 | 4096, |
| 170 | 5000, |
| 171 | NULL); |
| 172 | IPC::Channel chan(IPC::ChannelHandle(pipe), IPC::Channel::MODE_SERVER, |
| 173 | &channel_listener); |
| 174 | // Channel will duplicate the handle. |
| 175 | CloseHandle(pipe); |
| 176 | ASSERT_TRUE(chan.Connect()); |
| 177 | |
| 178 | channel_listener.Init(&chan); |
| 179 | |
| 180 | base::ProcessHandle process_handle = SpawnChild(TEST_CLIENT, &chan); |
| 181 | ASSERT_TRUE(process_handle); |
| 182 | |
| 183 | Send(&chan, "hello from parent"); |
| 184 | |
| 185 | // Run message loop. |
| 186 | MessageLoop::current()->Run(); |
| 187 | |
| 188 | // Close Channel so client gets its OnChannelError() callback fired. |
| 189 | chan.Close(); |
| 190 | |
| 191 | // Cleanup child process. |
| 192 | EXPECT_TRUE(base::WaitForSingleProcess( |
| 193 | process_handle, base::TimeDelta::FromSeconds(5))); |
| 194 | base::CloseProcessHandle(process_handle); |
| 195 | } |
| 196 | #endif // defined (OS_WIN) |
| 197 | |
| 198 | TEST_F(IPCChannelTest, ChannelProxyTest) { |
| 199 | MyChannelListener channel_listener; |
| 200 | |
| 201 | // The thread needs to out-live the ChannelProxy. |
| 202 | base::Thread thread("ChannelProxyTestServer"); |
| 203 | base::Thread::Options options; |
| 204 | options.message_loop_type = MessageLoop::TYPE_IO; |
| 205 | thread.StartWithOptions(options); |
| 206 | { |
| 207 | // setup IPC channel proxy |
| 208 | IPC::ChannelProxy chan(kTestClientChannel, IPC::Channel::MODE_SERVER, |
| 209 | &channel_listener, thread.message_loop_proxy()); |
| 210 | |
| 211 | channel_listener.Init(&chan); |
| 212 | |
| 213 | #if defined(OS_WIN) |
| 214 | base::ProcessHandle process_handle = SpawnChild(TEST_CLIENT, NULL); |
| 215 | #elif defined(OS_POSIX) |
| 216 | bool debug_on_start = CommandLine::ForCurrentProcess()->HasSwitch( |
| 217 | switches::kDebugChildren); |
| 218 | base::FileHandleMappingVector fds_to_map; |
| 219 | const int ipcfd = chan.GetClientFileDescriptor(); |
| 220 | if (ipcfd > -1) { |
| 221 | fds_to_map.push_back(std::pair<int, int>(ipcfd, kPrimaryIPCChannel + 3)); |
| 222 | } |
| 223 | |
| 224 | base::ProcessHandle process_handle = MultiProcessTest::SpawnChild( |
| 225 | "RunTestClient", |
| 226 | fds_to_map, |
| 227 | debug_on_start); |
| 228 | #endif // defined(OS_POSIX) |
| 229 | |
| 230 | ASSERT_TRUE(process_handle); |
| 231 | |
| 232 | Send(&chan, "hello from parent"); |
| 233 | |
| 234 | // run message loop |
| 235 | MessageLoop::current()->Run(); |
| 236 | |
| 237 | // cleanup child process |
| 238 | EXPECT_TRUE(base::WaitForSingleProcess( |
| 239 | process_handle, base::TimeDelta::FromSeconds(5))); |
| 240 | base::CloseProcessHandle(process_handle); |
| 241 | } |
| 242 | thread.Stop(); |
| 243 | } |
| 244 | |
| 245 | class ChannelListenerWithOnConnectedSend : public IPC::Listener { |
| 246 | public: |
| 247 | virtual void OnChannelConnected(int32 peer_pid) OVERRIDE { |
| 248 | SendNextMessage(); |
| 249 | } |
| 250 | |
| 251 | virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE { |
| 252 | PickleIterator iter(message); |
| 253 | |
| 254 | int ignored; |
| 255 | EXPECT_TRUE(iter.ReadInt(&ignored)); |
| 256 | std::string data; |
| 257 | EXPECT_TRUE(iter.ReadString(&data)); |
| 258 | std::string big_string; |
| 259 | EXPECT_TRUE(iter.ReadString(&big_string)); |
| 260 | EXPECT_EQ(kLongMessageStringNumBytes - 1, big_string.length()); |
| 261 | SendNextMessage(); |
| 262 | return true; |
| 263 | } |
| 264 | |
| 265 | virtual void OnChannelError() OVERRIDE { |
| 266 | // There is a race when closing the channel so the last message may be lost. |
| 267 | EXPECT_LE(messages_left_, 1); |
| 268 | MessageLoop::current()->Quit(); |
| 269 | } |
| 270 | |
| 271 | void Init(IPC::Sender* s) { |
| 272 | sender_ = s; |
| 273 | messages_left_ = 50; |
| 274 | } |
| 275 | |
| 276 | private: |
| 277 | void SendNextMessage() { |
| 278 | if (--messages_left_ == 0) { |
| 279 | MessageLoop::current()->Quit(); |
| 280 | } else { |
| 281 | Send(sender_, "Foo"); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | IPC::Sender* sender_; |
| 286 | int messages_left_; |
| 287 | }; |
| 288 | |
| 289 | #if defined(OS_WIN) |
| 290 | // Acting flakey in Windows. http://crbug.com/129595 |
| 291 | #define MAYBE_SendMessageInChannelConnected DISABLED_SendMessageInChannelConnected |
| 292 | #else |
| 293 | #define MAYBE_SendMessageInChannelConnected SendMessageInChannelConnected |
| 294 | #endif |
| 295 | TEST_F(IPCChannelTest, MAYBE_SendMessageInChannelConnected) { |
| 296 | // This tests the case of a listener sending back an event in it's |
| 297 | // OnChannelConnected handler. |
| 298 | |
| 299 | ChannelListenerWithOnConnectedSend channel_listener; |
| 300 | // Setup IPC channel. |
| 301 | IPC::Channel channel(kTestClientChannel, IPC::Channel::MODE_SERVER, |
| 302 | &channel_listener); |
| 303 | channel_listener.Init(&channel); |
| 304 | ASSERT_TRUE(channel.Connect()); |
| 305 | |
| 306 | base::ProcessHandle process_handle = SpawnChild(TEST_CLIENT, &channel); |
| 307 | ASSERT_TRUE(process_handle); |
| 308 | |
| 309 | Send(&channel, "hello from parent"); |
| 310 | |
| 311 | // Run message loop. |
| 312 | MessageLoop::current()->Run(); |
| 313 | |
| 314 | // Close Channel so client gets its OnChannelError() callback fired. |
| 315 | channel.Close(); |
| 316 | |
| 317 | // Cleanup child process. |
| 318 | EXPECT_TRUE(base::WaitForSingleProcess( |
| 319 | process_handle, base::TimeDelta::FromSeconds(5))); |
| 320 | base::CloseProcessHandle(process_handle); |
| 321 | } |
| 322 | |
| 323 | MULTIPROCESS_IPC_TEST_MAIN(RunTestClient) { |
| 324 | MessageLoopForIO main_message_loop; |
| 325 | MyChannelListener channel_listener; |
| 326 | |
| 327 | // setup IPC channel |
| 328 | IPC::Channel chan(kTestClientChannel, IPC::Channel::MODE_CLIENT, |
| 329 | &channel_listener); |
| 330 | CHECK(chan.Connect()); |
| 331 | channel_listener.Init(&chan); |
| 332 | Send(&chan, "hello from child"); |
| 333 | // run message loop |
| 334 | MessageLoop::current()->Run(); |
| 335 | return 0; |
| 336 | } |