blob: 47e72fc0e5ea45d44d31d8e718ec12884b25d002 [file] [log] [blame]
[email protected]64860882014-08-04 23:44:171// Copyright 2014 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 "ipc/mojo/ipc_channel_mojo.h"
6
7#include "base/base_paths.h"
8#include "base/files/file.h"
9#include "base/message_loop/message_loop.h"
10#include "base/path_service.h"
11#include "base/pickle.h"
12#include "base/threading/thread.h"
13#include "ipc/ipc_message.h"
14#include "ipc/ipc_test_base.h"
15#include "ipc/ipc_test_channel_listener.h"
morrita54f6f80c2014-09-23 21:16:0016#include "ipc/mojo/ipc_channel_mojo_host.h"
[email protected]64860882014-08-04 23:44:1717
18#if defined(OS_POSIX)
19#include "base/file_descriptor_posix.h"
20#endif
21
22namespace {
23
24class ListenerThatExpectsOK : public IPC::Listener {
25 public:
[email protected]e5c27752014-08-08 21:45:1326 ListenerThatExpectsOK()
27 : received_ok_(false) {}
[email protected]64860882014-08-04 23:44:1728
dchengfe61fca2014-10-22 02:29:5229 ~ListenerThatExpectsOK() override {}
[email protected]64860882014-08-04 23:44:1730
dchengfe61fca2014-10-22 02:29:5231 bool OnMessageReceived(const IPC::Message& message) override {
[email protected]64860882014-08-04 23:44:1732 PickleIterator iter(message);
33 std::string should_be_ok;
34 EXPECT_TRUE(iter.ReadString(&should_be_ok));
35 EXPECT_EQ(should_be_ok, "OK");
[email protected]e5c27752014-08-08 21:45:1336 received_ok_ = true;
[email protected]64860882014-08-04 23:44:1737 base::MessageLoop::current()->Quit();
38 return true;
39 }
40
dchengfe61fca2014-10-22 02:29:5241 void OnChannelError() override {
[email protected]e5c27752014-08-08 21:45:1342 // The connection should be healthy while the listener is waiting
43 // message. An error can occur after that because the peer
44 // process dies.
45 DCHECK(received_ok_);
[email protected]64860882014-08-04 23:44:1746 }
47
48 static void SendOK(IPC::Sender* sender) {
49 IPC::Message* message = new IPC::Message(
50 0, 2, IPC::Message::PRIORITY_NORMAL);
51 message->WriteString(std::string("OK"));
52 ASSERT_TRUE(sender->Send(message));
53 }
[email protected]e5c27752014-08-08 21:45:1354
55 private:
56 bool received_ok_;
[email protected]64860882014-08-04 23:44:1757};
58
[email protected]64860882014-08-04 23:44:1759class ChannelClient {
60 public:
61 explicit ChannelClient(IPC::Listener* listener, const char* name) {
morrita54f6f80c2014-09-23 21:16:0062 channel_ = IPC::ChannelMojo::Create(NULL,
63 IPCTestBase::GetChannelName(name),
64 IPC::Channel::MODE_CLIENT,
65 listener);
[email protected]64860882014-08-04 23:44:1766 }
67
68 void Connect() {
69 CHECK(channel_->Connect());
70 }
71
72 IPC::ChannelMojo* channel() const { return channel_.get(); }
73
74 private:
[email protected]64860882014-08-04 23:44:1775 base::MessageLoopForIO main_message_loop_;
morrita54f6f80c2014-09-23 21:16:0076 scoped_ptr<IPC::ChannelMojo> channel_;
[email protected]64860882014-08-04 23:44:1777};
78
79class IPCChannelMojoTest : public IPCTestBase {
[email protected]64860882014-08-04 23:44:1780 protected:
dchengfe61fca2014-10-22 02:29:5281 scoped_ptr<IPC::ChannelFactory> CreateChannelFactory(
morrita373af03b2014-09-09 19:35:2482 const IPC::ChannelHandle& handle,
mostynb50a41f32014-10-07 07:17:1683 base::TaskRunner* runner) override {
morrita54f6f80c2014-09-23 21:16:0084 host_.reset(new IPC::ChannelMojoHost(task_runner()));
morritae9453ea2014-09-26 03:20:4885 return IPC::ChannelMojo::CreateServerFactory(host_->channel_delegate(),
86 handle);
[email protected]64860882014-08-04 23:44:1787 }
morrita54f6f80c2014-09-23 21:16:0088
dchengfe61fca2014-10-22 02:29:5289 bool DidStartClient() override {
morrita54f6f80c2014-09-23 21:16:0090 bool ok = IPCTestBase::DidStartClient();
91 DCHECK(ok);
rvargas07b589c2015-01-12 22:23:2392 host_->OnClientLaunched(client_process().Handle());
morrita54f6f80c2014-09-23 21:16:0093 return ok;
94 }
95
96 private:
97 scoped_ptr<IPC::ChannelMojoHost> host_;
[email protected]64860882014-08-04 23:44:1798};
99
100
[email protected]64860882014-08-04 23:44:17101class TestChannelListenerWithExtraExpectations
102 : public IPC::TestChannelListener {
103 public:
104 TestChannelListenerWithExtraExpectations()
105 : is_connected_called_(false) {
106 }
107
dchengfe61fca2014-10-22 02:29:52108 void OnChannelConnected(int32 peer_pid) override {
[email protected]64860882014-08-04 23:44:17109 IPC::TestChannelListener::OnChannelConnected(peer_pid);
110 EXPECT_TRUE(base::kNullProcessId != peer_pid);
111 is_connected_called_ = true;
112 }
113
114 bool is_connected_called() const { return is_connected_called_; }
115
116 private:
117 bool is_connected_called_;
118};
119
120TEST_F(IPCChannelMojoTest, ConnectedFromClient) {
121 Init("IPCChannelMojoTestClient");
122
123 // Set up IPC channel and start client.
124 TestChannelListenerWithExtraExpectations listener;
morrita373af03b2014-09-09 19:35:24125 CreateChannel(&listener);
[email protected]64860882014-08-04 23:44:17126 listener.Init(sender());
127 ASSERT_TRUE(ConnectChannel());
128 ASSERT_TRUE(StartClient());
129
130 IPC::TestChannelListener::SendOneMessage(
131 sender(), "hello from parent");
132
133 base::MessageLoop::current()->Run();
134 EXPECT_TRUE(base::kNullProcessId != this->channel()->GetPeerPID());
135
136 this->channel()->Close();
137
138 EXPECT_TRUE(WaitForClientShutdown());
139 EXPECT_TRUE(listener.is_connected_called());
140 EXPECT_TRUE(listener.HasSentAll());
141
142 DestroyChannel();
143}
144
145// A long running process that connects to us
146MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoTestClient) {
147 TestChannelListenerWithExtraExpectations listener;
148 ChannelClient client(&listener, "IPCChannelMojoTestClient");
149 client.Connect();
150 listener.Init(client.channel());
151
152 IPC::TestChannelListener::SendOneMessage(
153 client.channel(), "hello from child");
154 base::MessageLoop::current()->Run();
155 EXPECT_TRUE(listener.is_connected_called());
156 EXPECT_TRUE(listener.HasSentAll());
157
158 return 0;
159}
160
morrita0a24cfc92014-09-16 03:20:48161class ListenerExpectingErrors : public IPC::Listener {
162 public:
163 ListenerExpectingErrors()
164 : has_error_(false) {
165 }
166
dchengfe61fca2014-10-22 02:29:52167 void OnChannelConnected(int32 peer_pid) override {
morritabe6c4cc2014-09-24 23:38:44168 base::MessageLoop::current()->Quit();
169 }
170
dchengfe61fca2014-10-22 02:29:52171 bool OnMessageReceived(const IPC::Message& message) override { return true; }
morrita0a24cfc92014-09-16 03:20:48172
dchengfe61fca2014-10-22 02:29:52173 void OnChannelError() override {
morrita0a24cfc92014-09-16 03:20:48174 has_error_ = true;
175 base::MessageLoop::current()->Quit();
176 }
177
178 bool has_error() const { return has_error_; }
179
180 private:
181 bool has_error_;
182};
183
184
185class IPCChannelMojoErrorTest : public IPCTestBase {
186 protected:
dchengfe61fca2014-10-22 02:29:52187 scoped_ptr<IPC::ChannelFactory> CreateChannelFactory(
morrita0a24cfc92014-09-16 03:20:48188 const IPC::ChannelHandle& handle,
mostynb50a41f32014-10-07 07:17:16189 base::TaskRunner* runner) override {
morrita54f6f80c2014-09-23 21:16:00190 host_.reset(new IPC::ChannelMojoHost(task_runner()));
morritae9453ea2014-09-26 03:20:48191 return IPC::ChannelMojo::CreateServerFactory(host_->channel_delegate(),
192 handle);
morrita0a24cfc92014-09-16 03:20:48193 }
morrita54f6f80c2014-09-23 21:16:00194
dchengfe61fca2014-10-22 02:29:52195 bool DidStartClient() override {
morrita54f6f80c2014-09-23 21:16:00196 bool ok = IPCTestBase::DidStartClient();
197 DCHECK(ok);
rvargas07b589c2015-01-12 22:23:23198 host_->OnClientLaunched(client_process().Handle());
morrita54f6f80c2014-09-23 21:16:00199 return ok;
200 }
201
202 private:
203 scoped_ptr<IPC::ChannelMojoHost> host_;
morrita0a24cfc92014-09-16 03:20:48204};
205
206class ListenerThatQuits : public IPC::Listener {
207 public:
208 ListenerThatQuits() {
209 }
210
dchengfe61fca2014-10-22 02:29:52211 bool OnMessageReceived(const IPC::Message& message) override {
dchengf3076af2014-10-21 18:02:42212 return true;
213 }
morrita0a24cfc92014-09-16 03:20:48214
dchengfe61fca2014-10-22 02:29:52215 void OnChannelConnected(int32 peer_pid) override {
morrita0a24cfc92014-09-16 03:20:48216 base::MessageLoop::current()->Quit();
217 }
218};
219
220// A long running process that connects to us.
221MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoErraticTestClient) {
222 ListenerThatQuits listener;
223 ChannelClient client(&listener, "IPCChannelMojoErraticTestClient");
224 client.Connect();
225
226 base::MessageLoop::current()->Run();
227
228 return 0;
229}
230
morritabe6c4cc2014-09-24 23:38:44231TEST_F(IPCChannelMojoErrorTest, SendFailWithPendingMessages) {
morrita0a24cfc92014-09-16 03:20:48232 Init("IPCChannelMojoErraticTestClient");
233
234 // Set up IPC channel and start client.
235 ListenerExpectingErrors listener;
236 CreateChannel(&listener);
237 ASSERT_TRUE(ConnectChannel());
238
jamesra03ae492014-10-03 04:26:48239 // This matches a value in mojo/edk/system/constants.h
morritabe6c4cc2014-09-24 23:38:44240 const int kMaxMessageNumBytes = 4 * 1024 * 1024;
241 std::string overly_large_data(kMaxMessageNumBytes, '*');
morrita0a24cfc92014-09-16 03:20:48242 // This messages are queued as pending.
morritabe6c4cc2014-09-24 23:38:44243 for (size_t i = 0; i < 10; ++i) {
morrita0a24cfc92014-09-16 03:20:48244 IPC::TestChannelListener::SendOneMessage(
morritabe6c4cc2014-09-24 23:38:44245 sender(), overly_large_data.c_str());
morrita0a24cfc92014-09-16 03:20:48246 }
247
248 ASSERT_TRUE(StartClient());
249 base::MessageLoop::current()->Run();
250
251 this->channel()->Close();
252
253 EXPECT_TRUE(WaitForClientShutdown());
254 EXPECT_TRUE(listener.has_error());
255
256 DestroyChannel();
257}
258
morrita25803672014-10-15 18:50:19259#if defined(OS_WIN)
260class IPCChannelMojoDeadHandleTest : public IPCTestBase {
261 protected:
262 virtual scoped_ptr<IPC::ChannelFactory> CreateChannelFactory(
263 const IPC::ChannelHandle& handle,
264 base::TaskRunner* runner) override {
265 host_.reset(new IPC::ChannelMojoHost(task_runner()));
266 return IPC::ChannelMojo::CreateServerFactory(host_->channel_delegate(),
267 handle);
268 }
269
270 virtual bool DidStartClient() override {
271 IPCTestBase::DidStartClient();
rvargas07b589c2015-01-12 22:23:23272 const base::ProcessHandle client = client_process().Handle();
morrita25803672014-10-15 18:50:19273 // Forces GetFileHandleForProcess() fail. It happens occasionally
274 // in production, so we should exercise it somehow.
rvargas07b589c2015-01-12 22:23:23275 // TODO(morrita): figure out how to safely test this.
276 // ::CloseHandle(client);
morrita25803672014-10-15 18:50:19277 host_->OnClientLaunched(client);
278 return true;
279 }
280
281 private:
282 scoped_ptr<IPC::ChannelMojoHost> host_;
283};
284
285TEST_F(IPCChannelMojoDeadHandleTest, InvalidClientHandle) {
286 // Any client type is fine as it is going to be killed anyway.
287 Init("IPCChannelMojoTestDoNothingClient");
288
289 // Set up IPC channel and start client.
290 ListenerExpectingErrors listener;
291 CreateChannel(&listener);
292 ASSERT_TRUE(ConnectChannel());
293
294 ASSERT_TRUE(StartClient());
295 base::MessageLoop::current()->Run();
296
297 this->channel()->Close();
298
299 // WaitForClientShutdown() fails as client_hanadle() is already
300 // closed.
301 EXPECT_FALSE(WaitForClientShutdown());
302 EXPECT_TRUE(listener.has_error());
303
304 DestroyChannel();
305}
306
307MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoTestDoNothingClient) {
308 ListenerThatQuits listener;
309 ChannelClient client(&listener, "IPCChannelMojoTestDoNothingClient");
310 client.Connect();
311
312 // Quits without running the message loop as this client won't
313 // receive any messages from the server.
314
315 return 0;
316}
317#endif
morrita0a24cfc92014-09-16 03:20:48318
[email protected]64860882014-08-04 23:44:17319#if defined(OS_POSIX)
320class ListenerThatExpectsFile : public IPC::Listener {
321 public:
322 ListenerThatExpectsFile()
323 : sender_(NULL) {}
324
dchengfe61fca2014-10-22 02:29:52325 ~ListenerThatExpectsFile() override {}
[email protected]64860882014-08-04 23:44:17326
dchengfe61fca2014-10-22 02:29:52327 bool OnMessageReceived(const IPC::Message& message) override {
[email protected]64860882014-08-04 23:44:17328 PickleIterator iter(message);
morrita96693852014-09-24 20:11:45329
330 base::ScopedFD fd;
331 EXPECT_TRUE(message.ReadFile(&iter, &fd));
332 base::File file(fd.release());
[email protected]64860882014-08-04 23:44:17333 std::string content(GetSendingFileContent().size(), ' ');
[email protected]64860882014-08-04 23:44:17334 file.Read(0, &content[0], content.size());
335 EXPECT_EQ(content, GetSendingFileContent());
336 base::MessageLoop::current()->Quit();
337 ListenerThatExpectsOK::SendOK(sender_);
338 return true;
339 }
340
dchengfe61fca2014-10-22 02:29:52341 void OnChannelError() override {
dchengf3076af2014-10-21 18:02:42342 NOTREACHED();
343 }
[email protected]64860882014-08-04 23:44:17344
345 static std::string GetSendingFileContent() {
346 return "Hello";
347 }
348
349 static base::FilePath GetSendingFilePath() {
350 base::FilePath path;
351 bool ok = PathService::Get(base::DIR_CACHE, &path);
352 EXPECT_TRUE(ok);
353 return path.Append("ListenerThatExpectsFile.txt");
354 }
355
356 static void WriteAndSendFile(IPC::Sender* sender, base::File& file) {
357 std::string content = GetSendingFileContent();
358 file.WriteAtCurrentPos(content.data(), content.size());
359 file.Flush();
360 IPC::Message* message = new IPC::Message(
361 0, 2, IPC::Message::PRIORITY_NORMAL);
morrita96693852014-09-24 20:11:45362 message->WriteFile(base::ScopedFD(file.TakePlatformFile()));
[email protected]64860882014-08-04 23:44:17363 ASSERT_TRUE(sender->Send(message));
364 }
365
366 void set_sender(IPC::Sender* sender) { sender_ = sender; }
367
368 private:
369 IPC::Sender* sender_;
370};
371
372
373TEST_F(IPCChannelMojoTest, SendPlatformHandle) {
374 Init("IPCChannelMojoTestSendPlatformHandleClient");
375
376 ListenerThatExpectsOK listener;
morrita373af03b2014-09-09 19:35:24377 CreateChannel(&listener);
[email protected]64860882014-08-04 23:44:17378 ASSERT_TRUE(ConnectChannel());
379 ASSERT_TRUE(StartClient());
380
381 base::File file(ListenerThatExpectsFile::GetSendingFilePath(),
382 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE |
383 base::File::FLAG_READ);
384 ListenerThatExpectsFile::WriteAndSendFile(channel(), file);
385 base::MessageLoop::current()->Run();
386
387 this->channel()->Close();
388
389 EXPECT_TRUE(WaitForClientShutdown());
390 DestroyChannel();
391}
392
393MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoTestSendPlatformHandleClient) {
394 ListenerThatExpectsFile listener;
395 ChannelClient client(
396 &listener, "IPCChannelMojoTestSendPlatformHandleClient");
397 client.Connect();
398 listener.set_sender(client.channel());
399
400 base::MessageLoop::current()->Run();
401
402 return 0;
403}
404#endif
405
406} // namespace