blob: 47e72fc0e5ea45d44d31d8e718ec12884b25d002 [file] [log] [blame]
morrita@chromium.org15996aa2014-08-05 08:44:17 +09001// 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"
morrita7c48ab82014-09-24 06:16:00 +090016#include "ipc/mojo/ipc_channel_mojo_host.h"
morrita@chromium.org15996aa2014-08-05 08:44:17 +090017
18#if defined(OS_POSIX)
19#include "base/file_descriptor_posix.h"
20#endif
21
22namespace {
23
24class ListenerThatExpectsOK : public IPC::Listener {
25 public:
morrita@chromium.orge2bbe782014-08-09 06:45:13 +090026 ListenerThatExpectsOK()
27 : received_ok_(false) {}
morrita@chromium.org15996aa2014-08-05 08:44:17 +090028
dchengef7721a2014-10-22 11:29:52 +090029 ~ListenerThatExpectsOK() override {}
morrita@chromium.org15996aa2014-08-05 08:44:17 +090030
dchengef7721a2014-10-22 11:29:52 +090031 bool OnMessageReceived(const IPC::Message& message) override {
morrita@chromium.org15996aa2014-08-05 08:44:17 +090032 PickleIterator iter(message);
33 std::string should_be_ok;
34 EXPECT_TRUE(iter.ReadString(&should_be_ok));
35 EXPECT_EQ(should_be_ok, "OK");
morrita@chromium.orge2bbe782014-08-09 06:45:13 +090036 received_ok_ = true;
morrita@chromium.org15996aa2014-08-05 08:44:17 +090037 base::MessageLoop::current()->Quit();
38 return true;
39 }
40
dchengef7721a2014-10-22 11:29:52 +090041 void OnChannelError() override {
morrita@chromium.orge2bbe782014-08-09 06:45:13 +090042 // 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_);
morrita@chromium.org15996aa2014-08-05 08:44:17 +090046 }
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 }
morrita@chromium.orge2bbe782014-08-09 06:45:13 +090054
55 private:
56 bool received_ok_;
morrita@chromium.org15996aa2014-08-05 08:44:17 +090057};
58
morrita@chromium.org15996aa2014-08-05 08:44:17 +090059class ChannelClient {
60 public:
61 explicit ChannelClient(IPC::Listener* listener, const char* name) {
morrita7c48ab82014-09-24 06:16:00 +090062 channel_ = IPC::ChannelMojo::Create(NULL,
63 IPCTestBase::GetChannelName(name),
64 IPC::Channel::MODE_CLIENT,
65 listener);
morrita@chromium.org15996aa2014-08-05 08:44:17 +090066 }
67
68 void Connect() {
69 CHECK(channel_->Connect());
70 }
71
72 IPC::ChannelMojo* channel() const { return channel_.get(); }
73
74 private:
morrita@chromium.org15996aa2014-08-05 08:44:17 +090075 base::MessageLoopForIO main_message_loop_;
morrita7c48ab82014-09-24 06:16:00 +090076 scoped_ptr<IPC::ChannelMojo> channel_;
morrita@chromium.org15996aa2014-08-05 08:44:17 +090077};
78
79class IPCChannelMojoTest : public IPCTestBase {
morrita@chromium.org15996aa2014-08-05 08:44:17 +090080 protected:
dchengef7721a2014-10-22 11:29:52 +090081 scoped_ptr<IPC::ChannelFactory> CreateChannelFactory(
morritaab779702014-09-10 04:35:24 +090082 const IPC::ChannelHandle& handle,
mostynbd41cdbb2014-10-07 16:17:16 +090083 base::TaskRunner* runner) override {
morrita7c48ab82014-09-24 06:16:00 +090084 host_.reset(new IPC::ChannelMojoHost(task_runner()));
morrita98b6e4a2014-09-26 12:20:48 +090085 return IPC::ChannelMojo::CreateServerFactory(host_->channel_delegate(),
86 handle);
morrita@chromium.org15996aa2014-08-05 08:44:17 +090087 }
morrita7c48ab82014-09-24 06:16:00 +090088
dchengef7721a2014-10-22 11:29:52 +090089 bool DidStartClient() override {
morrita7c48ab82014-09-24 06:16:00 +090090 bool ok = IPCTestBase::DidStartClient();
91 DCHECK(ok);
rvargasf355d882015-01-13 07:23:23 +090092 host_->OnClientLaunched(client_process().Handle());
morrita7c48ab82014-09-24 06:16:00 +090093 return ok;
94 }
95
96 private:
97 scoped_ptr<IPC::ChannelMojoHost> host_;
morrita@chromium.org15996aa2014-08-05 08:44:17 +090098};
99
100
morrita@chromium.org15996aa2014-08-05 08:44:17 +0900101class TestChannelListenerWithExtraExpectations
102 : public IPC::TestChannelListener {
103 public:
104 TestChannelListenerWithExtraExpectations()
105 : is_connected_called_(false) {
106 }
107
dchengef7721a2014-10-22 11:29:52 +0900108 void OnChannelConnected(int32 peer_pid) override {
morrita@chromium.org15996aa2014-08-05 08:44:17 +0900109 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;
morritaab779702014-09-10 04:35:24 +0900125 CreateChannel(&listener);
morrita@chromium.org15996aa2014-08-05 08:44:17 +0900126 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
morritafa3ab752014-09-16 12:20:48 +0900161class ListenerExpectingErrors : public IPC::Listener {
162 public:
163 ListenerExpectingErrors()
164 : has_error_(false) {
165 }
166
dchengef7721a2014-10-22 11:29:52 +0900167 void OnChannelConnected(int32 peer_pid) override {
morrita26b6d752014-09-25 08:38:44 +0900168 base::MessageLoop::current()->Quit();
169 }
170
dchengef7721a2014-10-22 11:29:52 +0900171 bool OnMessageReceived(const IPC::Message& message) override { return true; }
morritafa3ab752014-09-16 12:20:48 +0900172
dchengef7721a2014-10-22 11:29:52 +0900173 void OnChannelError() override {
morritafa3ab752014-09-16 12:20:48 +0900174 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:
dchengef7721a2014-10-22 11:29:52 +0900187 scoped_ptr<IPC::ChannelFactory> CreateChannelFactory(
morritafa3ab752014-09-16 12:20:48 +0900188 const IPC::ChannelHandle& handle,
mostynbd41cdbb2014-10-07 16:17:16 +0900189 base::TaskRunner* runner) override {
morrita7c48ab82014-09-24 06:16:00 +0900190 host_.reset(new IPC::ChannelMojoHost(task_runner()));
morrita98b6e4a2014-09-26 12:20:48 +0900191 return IPC::ChannelMojo::CreateServerFactory(host_->channel_delegate(),
192 handle);
morritafa3ab752014-09-16 12:20:48 +0900193 }
morrita7c48ab82014-09-24 06:16:00 +0900194
dchengef7721a2014-10-22 11:29:52 +0900195 bool DidStartClient() override {
morrita7c48ab82014-09-24 06:16:00 +0900196 bool ok = IPCTestBase::DidStartClient();
197 DCHECK(ok);
rvargasf355d882015-01-13 07:23:23 +0900198 host_->OnClientLaunched(client_process().Handle());
morrita7c48ab82014-09-24 06:16:00 +0900199 return ok;
200 }
201
202 private:
203 scoped_ptr<IPC::ChannelMojoHost> host_;
morritafa3ab752014-09-16 12:20:48 +0900204};
205
206class ListenerThatQuits : public IPC::Listener {
207 public:
208 ListenerThatQuits() {
209 }
210
dchengef7721a2014-10-22 11:29:52 +0900211 bool OnMessageReceived(const IPC::Message& message) override {
dcheng9b01d242014-10-22 03:02:42 +0900212 return true;
213 }
morritafa3ab752014-09-16 12:20:48 +0900214
dchengef7721a2014-10-22 11:29:52 +0900215 void OnChannelConnected(int32 peer_pid) override {
morritafa3ab752014-09-16 12:20:48 +0900216 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
morrita26b6d752014-09-25 08:38:44 +0900231TEST_F(IPCChannelMojoErrorTest, SendFailWithPendingMessages) {
morritafa3ab752014-09-16 12:20:48 +0900232 Init("IPCChannelMojoErraticTestClient");
233
234 // Set up IPC channel and start client.
235 ListenerExpectingErrors listener;
236 CreateChannel(&listener);
237 ASSERT_TRUE(ConnectChannel());
238
jamesr8dddfa22014-10-03 13:26:48 +0900239 // This matches a value in mojo/edk/system/constants.h
morrita26b6d752014-09-25 08:38:44 +0900240 const int kMaxMessageNumBytes = 4 * 1024 * 1024;
241 std::string overly_large_data(kMaxMessageNumBytes, '*');
morritafa3ab752014-09-16 12:20:48 +0900242 // This messages are queued as pending.
morrita26b6d752014-09-25 08:38:44 +0900243 for (size_t i = 0; i < 10; ++i) {
morritafa3ab752014-09-16 12:20:48 +0900244 IPC::TestChannelListener::SendOneMessage(
morrita26b6d752014-09-25 08:38:44 +0900245 sender(), overly_large_data.c_str());
morritafa3ab752014-09-16 12:20:48 +0900246 }
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
morrita7767a0d2014-10-16 03:50:19 +0900259#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();
rvargasf355d882015-01-13 07:23:23 +0900272 const base::ProcessHandle client = client_process().Handle();
morrita7767a0d2014-10-16 03:50:19 +0900273 // Forces GetFileHandleForProcess() fail. It happens occasionally
274 // in production, so we should exercise it somehow.
rvargasf355d882015-01-13 07:23:23 +0900275 // TODO(morrita): figure out how to safely test this.
276 // ::CloseHandle(client);
morrita7767a0d2014-10-16 03:50:19 +0900277 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
morritafa3ab752014-09-16 12:20:48 +0900318
morrita@chromium.org15996aa2014-08-05 08:44:17 +0900319#if defined(OS_POSIX)
320class ListenerThatExpectsFile : public IPC::Listener {
321 public:
322 ListenerThatExpectsFile()
323 : sender_(NULL) {}
324
dchengef7721a2014-10-22 11:29:52 +0900325 ~ListenerThatExpectsFile() override {}
morrita@chromium.org15996aa2014-08-05 08:44:17 +0900326
dchengef7721a2014-10-22 11:29:52 +0900327 bool OnMessageReceived(const IPC::Message& message) override {
morrita@chromium.org15996aa2014-08-05 08:44:17 +0900328 PickleIterator iter(message);
morritaab207252014-09-25 05:11:45 +0900329
330 base::ScopedFD fd;
331 EXPECT_TRUE(message.ReadFile(&iter, &fd));
332 base::File file(fd.release());
morrita@chromium.org15996aa2014-08-05 08:44:17 +0900333 std::string content(GetSendingFileContent().size(), ' ');
morrita@chromium.org15996aa2014-08-05 08:44:17 +0900334 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
dchengef7721a2014-10-22 11:29:52 +0900341 void OnChannelError() override {
dcheng9b01d242014-10-22 03:02:42 +0900342 NOTREACHED();
343 }
morrita@chromium.org15996aa2014-08-05 08:44:17 +0900344
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);
morritaab207252014-09-25 05:11:45 +0900362 message->WriteFile(base::ScopedFD(file.TakePlatformFile()));
morrita@chromium.org15996aa2014-08-05 08:44:17 +0900363 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;
morritaab779702014-09-10 04:35:24 +0900377 CreateChannel(&listener);
morrita@chromium.org15996aa2014-08-05 08:44:17 +0900378 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