blob: 07218c69297d9401106e7579096574c1cc35dec4 [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"
morritafa3ab752014-09-16 12:20:48 +090017#include "ipc/mojo/ipc_channel_mojo_readers.h"
morrita@chromium.org15996aa2014-08-05 08:44:17 +090018
19#if defined(OS_POSIX)
20#include "base/file_descriptor_posix.h"
21#endif
22
23namespace {
24
25class ListenerThatExpectsOK : public IPC::Listener {
26 public:
morrita@chromium.orge2bbe782014-08-09 06:45:13 +090027 ListenerThatExpectsOK()
28 : received_ok_(false) {}
morrita@chromium.org15996aa2014-08-05 08:44:17 +090029
dchengef7721a2014-10-22 11:29:52 +090030 ~ListenerThatExpectsOK() override {}
morrita@chromium.org15996aa2014-08-05 08:44:17 +090031
dchengef7721a2014-10-22 11:29:52 +090032 bool OnMessageReceived(const IPC::Message& message) override {
morrita@chromium.org15996aa2014-08-05 08:44:17 +090033 PickleIterator iter(message);
34 std::string should_be_ok;
35 EXPECT_TRUE(iter.ReadString(&should_be_ok));
36 EXPECT_EQ(should_be_ok, "OK");
morrita@chromium.orge2bbe782014-08-09 06:45:13 +090037 received_ok_ = true;
morrita@chromium.org15996aa2014-08-05 08:44:17 +090038 base::MessageLoop::current()->Quit();
39 return true;
40 }
41
dchengef7721a2014-10-22 11:29:52 +090042 void OnChannelError() override {
morrita@chromium.orge2bbe782014-08-09 06:45:13 +090043 // The connection should be healthy while the listener is waiting
44 // message. An error can occur after that because the peer
45 // process dies.
46 DCHECK(received_ok_);
morrita@chromium.org15996aa2014-08-05 08:44:17 +090047 }
48
49 static void SendOK(IPC::Sender* sender) {
50 IPC::Message* message = new IPC::Message(
51 0, 2, IPC::Message::PRIORITY_NORMAL);
52 message->WriteString(std::string("OK"));
53 ASSERT_TRUE(sender->Send(message));
54 }
morrita@chromium.orge2bbe782014-08-09 06:45:13 +090055
56 private:
57 bool received_ok_;
morrita@chromium.org15996aa2014-08-05 08:44:17 +090058};
59
morrita@chromium.org15996aa2014-08-05 08:44:17 +090060class ChannelClient {
61 public:
62 explicit ChannelClient(IPC::Listener* listener, const char* name) {
morrita7c48ab82014-09-24 06:16:00 +090063 channel_ = IPC::ChannelMojo::Create(NULL,
64 IPCTestBase::GetChannelName(name),
65 IPC::Channel::MODE_CLIENT,
66 listener);
morrita@chromium.org15996aa2014-08-05 08:44:17 +090067 }
68
69 void Connect() {
70 CHECK(channel_->Connect());
71 }
72
73 IPC::ChannelMojo* channel() const { return channel_.get(); }
74
75 private:
morrita@chromium.org15996aa2014-08-05 08:44:17 +090076 base::MessageLoopForIO main_message_loop_;
morrita7c48ab82014-09-24 06:16:00 +090077 scoped_ptr<IPC::ChannelMojo> channel_;
morrita@chromium.org15996aa2014-08-05 08:44:17 +090078};
79
80class IPCChannelMojoTest : public IPCTestBase {
morrita@chromium.org15996aa2014-08-05 08:44:17 +090081 protected:
dchengef7721a2014-10-22 11:29:52 +090082 scoped_ptr<IPC::ChannelFactory> CreateChannelFactory(
morritaab779702014-09-10 04:35:24 +090083 const IPC::ChannelHandle& handle,
mostynbd41cdbb2014-10-07 16:17:16 +090084 base::TaskRunner* runner) override {
morrita7c48ab82014-09-24 06:16:00 +090085 host_.reset(new IPC::ChannelMojoHost(task_runner()));
morrita98b6e4a2014-09-26 12:20:48 +090086 return IPC::ChannelMojo::CreateServerFactory(host_->channel_delegate(),
87 handle);
morrita@chromium.org15996aa2014-08-05 08:44:17 +090088 }
morrita7c48ab82014-09-24 06:16:00 +090089
dchengef7721a2014-10-22 11:29:52 +090090 bool DidStartClient() override {
morrita7c48ab82014-09-24 06:16:00 +090091 bool ok = IPCTestBase::DidStartClient();
92 DCHECK(ok);
93 host_->OnClientLaunched(client_process());
94 return ok;
95 }
96
97 private:
98 scoped_ptr<IPC::ChannelMojoHost> host_;
morrita@chromium.org15996aa2014-08-05 08:44:17 +090099};
100
101
morrita@chromium.org15996aa2014-08-05 08:44:17 +0900102class TestChannelListenerWithExtraExpectations
103 : public IPC::TestChannelListener {
104 public:
105 TestChannelListenerWithExtraExpectations()
106 : is_connected_called_(false) {
107 }
108
dchengef7721a2014-10-22 11:29:52 +0900109 void OnChannelConnected(int32 peer_pid) override {
morrita@chromium.org15996aa2014-08-05 08:44:17 +0900110 IPC::TestChannelListener::OnChannelConnected(peer_pid);
111 EXPECT_TRUE(base::kNullProcessId != peer_pid);
112 is_connected_called_ = true;
113 }
114
115 bool is_connected_called() const { return is_connected_called_; }
116
117 private:
118 bool is_connected_called_;
119};
120
121TEST_F(IPCChannelMojoTest, ConnectedFromClient) {
122 Init("IPCChannelMojoTestClient");
123
124 // Set up IPC channel and start client.
125 TestChannelListenerWithExtraExpectations listener;
morritaab779702014-09-10 04:35:24 +0900126 CreateChannel(&listener);
morrita@chromium.org15996aa2014-08-05 08:44:17 +0900127 listener.Init(sender());
128 ASSERT_TRUE(ConnectChannel());
129 ASSERT_TRUE(StartClient());
130
131 IPC::TestChannelListener::SendOneMessage(
132 sender(), "hello from parent");
133
134 base::MessageLoop::current()->Run();
135 EXPECT_TRUE(base::kNullProcessId != this->channel()->GetPeerPID());
136
137 this->channel()->Close();
138
139 EXPECT_TRUE(WaitForClientShutdown());
140 EXPECT_TRUE(listener.is_connected_called());
141 EXPECT_TRUE(listener.HasSentAll());
142
143 DestroyChannel();
144}
145
146// A long running process that connects to us
147MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoTestClient) {
148 TestChannelListenerWithExtraExpectations listener;
149 ChannelClient client(&listener, "IPCChannelMojoTestClient");
150 client.Connect();
151 listener.Init(client.channel());
152
153 IPC::TestChannelListener::SendOneMessage(
154 client.channel(), "hello from child");
155 base::MessageLoop::current()->Run();
156 EXPECT_TRUE(listener.is_connected_called());
157 EXPECT_TRUE(listener.HasSentAll());
158
159 return 0;
160}
161
morritafa3ab752014-09-16 12:20:48 +0900162class ListenerExpectingErrors : public IPC::Listener {
163 public:
164 ListenerExpectingErrors()
165 : has_error_(false) {
166 }
167
dchengef7721a2014-10-22 11:29:52 +0900168 void OnChannelConnected(int32 peer_pid) override {
morrita26b6d752014-09-25 08:38:44 +0900169 base::MessageLoop::current()->Quit();
170 }
171
dchengef7721a2014-10-22 11:29:52 +0900172 bool OnMessageReceived(const IPC::Message& message) override { return true; }
morritafa3ab752014-09-16 12:20:48 +0900173
dchengef7721a2014-10-22 11:29:52 +0900174 void OnChannelError() override {
morritafa3ab752014-09-16 12:20:48 +0900175 has_error_ = true;
176 base::MessageLoop::current()->Quit();
177 }
178
179 bool has_error() const { return has_error_; }
180
181 private:
182 bool has_error_;
183};
184
185
186class IPCChannelMojoErrorTest : public IPCTestBase {
187 protected:
dchengef7721a2014-10-22 11:29:52 +0900188 scoped_ptr<IPC::ChannelFactory> CreateChannelFactory(
morritafa3ab752014-09-16 12:20:48 +0900189 const IPC::ChannelHandle& handle,
mostynbd41cdbb2014-10-07 16:17:16 +0900190 base::TaskRunner* runner) override {
morrita7c48ab82014-09-24 06:16:00 +0900191 host_.reset(new IPC::ChannelMojoHost(task_runner()));
morrita98b6e4a2014-09-26 12:20:48 +0900192 return IPC::ChannelMojo::CreateServerFactory(host_->channel_delegate(),
193 handle);
morritafa3ab752014-09-16 12:20:48 +0900194 }
morrita7c48ab82014-09-24 06:16:00 +0900195
dchengef7721a2014-10-22 11:29:52 +0900196 bool DidStartClient() override {
morrita7c48ab82014-09-24 06:16:00 +0900197 bool ok = IPCTestBase::DidStartClient();
198 DCHECK(ok);
199 host_->OnClientLaunched(client_process());
200 return ok;
201 }
202
203 private:
204 scoped_ptr<IPC::ChannelMojoHost> host_;
morritafa3ab752014-09-16 12:20:48 +0900205};
206
207class ListenerThatQuits : public IPC::Listener {
208 public:
209 ListenerThatQuits() {
210 }
211
dchengef7721a2014-10-22 11:29:52 +0900212 bool OnMessageReceived(const IPC::Message& message) override {
dcheng9b01d242014-10-22 03:02:42 +0900213 return true;
214 }
morritafa3ab752014-09-16 12:20:48 +0900215
dchengef7721a2014-10-22 11:29:52 +0900216 void OnChannelConnected(int32 peer_pid) override {
morritafa3ab752014-09-16 12:20:48 +0900217 base::MessageLoop::current()->Quit();
218 }
219};
220
221// A long running process that connects to us.
222MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoErraticTestClient) {
223 ListenerThatQuits listener;
224 ChannelClient client(&listener, "IPCChannelMojoErraticTestClient");
225 client.Connect();
226
227 base::MessageLoop::current()->Run();
228
229 return 0;
230}
231
morrita26b6d752014-09-25 08:38:44 +0900232TEST_F(IPCChannelMojoErrorTest, SendFailWithPendingMessages) {
morritafa3ab752014-09-16 12:20:48 +0900233 Init("IPCChannelMojoErraticTestClient");
234
235 // Set up IPC channel and start client.
236 ListenerExpectingErrors listener;
237 CreateChannel(&listener);
238 ASSERT_TRUE(ConnectChannel());
239
jamesr8dddfa22014-10-03 13:26:48 +0900240 // This matches a value in mojo/edk/system/constants.h
morrita26b6d752014-09-25 08:38:44 +0900241 const int kMaxMessageNumBytes = 4 * 1024 * 1024;
242 std::string overly_large_data(kMaxMessageNumBytes, '*');
morritafa3ab752014-09-16 12:20:48 +0900243 // This messages are queued as pending.
morrita26b6d752014-09-25 08:38:44 +0900244 for (size_t i = 0; i < 10; ++i) {
morritafa3ab752014-09-16 12:20:48 +0900245 IPC::TestChannelListener::SendOneMessage(
morrita26b6d752014-09-25 08:38:44 +0900246 sender(), overly_large_data.c_str());
morritafa3ab752014-09-16 12:20:48 +0900247 }
248
249 ASSERT_TRUE(StartClient());
250 base::MessageLoop::current()->Run();
251
252 this->channel()->Close();
253
254 EXPECT_TRUE(WaitForClientShutdown());
255 EXPECT_TRUE(listener.has_error());
256
257 DestroyChannel();
258}
259
morrita7767a0d2014-10-16 03:50:19 +0900260#if defined(OS_WIN)
261class IPCChannelMojoDeadHandleTest : public IPCTestBase {
262 protected:
263 virtual scoped_ptr<IPC::ChannelFactory> CreateChannelFactory(
264 const IPC::ChannelHandle& handle,
265 base::TaskRunner* runner) override {
266 host_.reset(new IPC::ChannelMojoHost(task_runner()));
267 return IPC::ChannelMojo::CreateServerFactory(host_->channel_delegate(),
268 handle);
269 }
270
271 virtual bool DidStartClient() override {
272 IPCTestBase::DidStartClient();
273 base::ProcessHandle client = client_process();
274 // Forces GetFileHandleForProcess() fail. It happens occasionally
275 // in production, so we should exercise it somehow.
276 ::CloseHandle(client);
277 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