[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 1 | // 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 | |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame] | 7 | #include <stddef.h> |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 8 | #include <stdint.h> |
dcheng | e4860045 | 2015-12-28 02:24:50 | [diff] [blame] | 9 | #include <utility> |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 10 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 11 | #include "base/base_paths.h" |
| 12 | #include "base/files/file.h" |
skyostil | e687bdff | 2015-05-12 11:29:21 | [diff] [blame] | 13 | #include "base/location.h" |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 14 | #include "base/path_service.h" |
| 15 | #include "base/pickle.h" |
rockot | cbca72f | 2015-03-03 16:31:04 | [diff] [blame] | 16 | #include "base/run_loop.h" |
skyostil | e687bdff | 2015-05-12 11:29:21 | [diff] [blame] | 17 | #include "base/single_thread_task_runner.h" |
morrita | 0bd20bd | 2015-02-25 20:11:27 | [diff] [blame] | 18 | #include "base/test/test_timeouts.h" |
skyostil | e687bdff | 2015-05-12 11:29:21 | [diff] [blame] | 19 | #include "base/thread_task_runner_handle.h" |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 20 | #include "base/threading/thread.h" |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame] | 21 | #include "build/build_config.h" |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 22 | #include "ipc/ipc_message.h" |
| 23 | #include "ipc/ipc_test_base.h" |
| 24 | #include "ipc/ipc_test_channel_listener.h" |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 25 | #include "ipc/mojo/ipc_mojo_handle_attachment.h" |
| 26 | #include "ipc/mojo/ipc_mojo_message_helper.h" |
morrita | 438a2ee | 2015-04-03 05:28:21 | [diff] [blame] | 27 | #include "ipc/mojo/ipc_mojo_param_traits.h" |
rockot | cbca72f | 2015-03-03 16:31:04 | [diff] [blame] | 28 | #include "ipc/mojo/scoped_ipc_support.h" |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 29 | |
| 30 | #if defined(OS_POSIX) |
| 31 | #include "base/file_descriptor_posix.h" |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 32 | #include "ipc/ipc_platform_file_attachment_posix.h" |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 33 | #endif |
| 34 | |
| 35 | namespace { |
| 36 | |
| 37 | class ListenerThatExpectsOK : public IPC::Listener { |
| 38 | public: |
[email protected] | e5c2775 | 2014-08-08 21:45:13 | [diff] [blame] | 39 | ListenerThatExpectsOK() |
| 40 | : received_ok_(false) {} |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 41 | |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 42 | ~ListenerThatExpectsOK() override {} |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 43 | |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 44 | bool OnMessageReceived(const IPC::Message& message) override { |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 45 | base::PickleIterator iter(message); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 46 | std::string should_be_ok; |
| 47 | EXPECT_TRUE(iter.ReadString(&should_be_ok)); |
| 48 | EXPECT_EQ(should_be_ok, "OK"); |
[email protected] | e5c2775 | 2014-08-08 21:45:13 | [diff] [blame] | 49 | received_ok_ = true; |
ki.stfu | a21ed8c | 2015-10-12 17:26:00 | [diff] [blame] | 50 | base::MessageLoop::current()->QuitWhenIdle(); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 51 | return true; |
| 52 | } |
| 53 | |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 54 | void OnChannelError() override { |
[email protected] | e5c2775 | 2014-08-08 21:45:13 | [diff] [blame] | 55 | // The connection should be healthy while the listener is waiting |
| 56 | // message. An error can occur after that because the peer |
| 57 | // process dies. |
| 58 | DCHECK(received_ok_); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | static void SendOK(IPC::Sender* sender) { |
| 62 | IPC::Message* message = new IPC::Message( |
| 63 | 0, 2, IPC::Message::PRIORITY_NORMAL); |
| 64 | message->WriteString(std::string("OK")); |
| 65 | ASSERT_TRUE(sender->Send(message)); |
| 66 | } |
[email protected] | e5c2775 | 2014-08-08 21:45:13 | [diff] [blame] | 67 | |
| 68 | private: |
| 69 | bool received_ok_; |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 70 | }; |
| 71 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 72 | class ChannelClient { |
| 73 | public: |
| 74 | explicit ChannelClient(IPC::Listener* listener, const char* name) { |
erikchen | 30dc281 | 2015-09-24 03:26:38 | [diff] [blame] | 75 | channel_ = IPC::ChannelMojo::Create(main_message_loop_.task_runner(), |
| 76 | IPCTestBase::GetChannelName(name), |
| 77 | IPC::Channel::MODE_CLIENT, listener); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | void Connect() { |
| 81 | CHECK(channel_->Connect()); |
| 82 | } |
| 83 | |
rockot | cbca72f | 2015-03-03 16:31:04 | [diff] [blame] | 84 | void Close() { |
| 85 | channel_->Close(); |
| 86 | |
| 87 | base::RunLoop run_loop; |
skyostil | e687bdff | 2015-05-12 11:29:21 | [diff] [blame] | 88 | base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 89 | run_loop.QuitClosure()); |
rockot | cbca72f | 2015-03-03 16:31:04 | [diff] [blame] | 90 | run_loop.Run(); |
| 91 | } |
| 92 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 93 | IPC::ChannelMojo* channel() const { return channel_.get(); } |
| 94 | |
| 95 | private: |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 96 | base::MessageLoopForIO main_message_loop_; |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 97 | scoped_ptr<IPC::ChannelMojo> channel_; |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 98 | }; |
| 99 | |
rockot | cbca72f | 2015-03-03 16:31:04 | [diff] [blame] | 100 | class IPCChannelMojoTestBase : public IPCTestBase { |
| 101 | public: |
| 102 | void InitWithMojo(const std::string& test_client_name) { |
| 103 | Init(test_client_name); |
rockot | cbca72f | 2015-03-03 16:31:04 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | void TearDown() override { |
| 107 | // Make sure Mojo IPC support is properly shutdown on the I/O loop before |
| 108 | // TearDown continues. |
rockot | cbca72f | 2015-03-03 16:31:04 | [diff] [blame] | 109 | base::RunLoop run_loop; |
| 110 | task_runner()->PostTask(FROM_HERE, run_loop.QuitClosure()); |
| 111 | run_loop.Run(); |
| 112 | |
| 113 | IPCTestBase::TearDown(); |
| 114 | } |
rockot | cbca72f | 2015-03-03 16:31:04 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | class IPCChannelMojoTest : public IPCChannelMojoTestBase { |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 118 | protected: |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 119 | scoped_ptr<IPC::ChannelFactory> CreateChannelFactory( |
morrita | 373af03b | 2014-09-09 19:35:24 | [diff] [blame] | 120 | const IPC::ChannelHandle& handle, |
morrita | c4db547 | 2015-03-13 20:44:39 | [diff] [blame] | 121 | base::SequencedTaskRunner* runner) override { |
erikchen | 30dc281 | 2015-09-24 03:26:38 | [diff] [blame] | 122 | return IPC::ChannelMojo::CreateServerFactory(task_runner(), handle); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 123 | } |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 124 | |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 125 | bool DidStartClient() override { |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 126 | bool ok = IPCTestBase::DidStartClient(); |
| 127 | DCHECK(ok); |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 128 | return ok; |
| 129 | } |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 133 | class TestChannelListenerWithExtraExpectations |
| 134 | : public IPC::TestChannelListener { |
| 135 | public: |
| 136 | TestChannelListenerWithExtraExpectations() |
| 137 | : is_connected_called_(false) { |
| 138 | } |
| 139 | |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 140 | void OnChannelConnected(int32_t peer_pid) override { |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 141 | IPC::TestChannelListener::OnChannelConnected(peer_pid); |
| 142 | EXPECT_TRUE(base::kNullProcessId != peer_pid); |
| 143 | is_connected_called_ = true; |
| 144 | } |
| 145 | |
| 146 | bool is_connected_called() const { return is_connected_called_; } |
| 147 | |
| 148 | private: |
| 149 | bool is_connected_called_; |
| 150 | }; |
| 151 | |
msw | 8f7f5390 | 2015-06-19 16:26:47 | [diff] [blame] | 152 | // Times out on Android; see http://crbug.com/502290 |
| 153 | #if defined(OS_ANDROID) |
| 154 | #define MAYBE_ConnectedFromClient DISABLED_ConnectedFromClient |
| 155 | #else |
| 156 | #define MAYBE_ConnectedFromClient ConnectedFromClient |
| 157 | #endif |
| 158 | TEST_F(IPCChannelMojoTest, MAYBE_ConnectedFromClient) { |
rockot | cbca72f | 2015-03-03 16:31:04 | [diff] [blame] | 159 | InitWithMojo("IPCChannelMojoTestClient"); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 160 | |
| 161 | // Set up IPC channel and start client. |
| 162 | TestChannelListenerWithExtraExpectations listener; |
morrita | 373af03b | 2014-09-09 19:35:24 | [diff] [blame] | 163 | CreateChannel(&listener); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 164 | listener.Init(sender()); |
| 165 | ASSERT_TRUE(ConnectChannel()); |
| 166 | ASSERT_TRUE(StartClient()); |
| 167 | |
| 168 | IPC::TestChannelListener::SendOneMessage( |
| 169 | sender(), "hello from parent"); |
| 170 | |
| 171 | base::MessageLoop::current()->Run(); |
| 172 | EXPECT_TRUE(base::kNullProcessId != this->channel()->GetPeerPID()); |
| 173 | |
| 174 | this->channel()->Close(); |
| 175 | |
| 176 | EXPECT_TRUE(WaitForClientShutdown()); |
| 177 | EXPECT_TRUE(listener.is_connected_called()); |
| 178 | EXPECT_TRUE(listener.HasSentAll()); |
| 179 | |
| 180 | DestroyChannel(); |
| 181 | } |
| 182 | |
| 183 | // A long running process that connects to us |
| 184 | MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoTestClient) { |
| 185 | TestChannelListenerWithExtraExpectations listener; |
| 186 | ChannelClient client(&listener, "IPCChannelMojoTestClient"); |
| 187 | client.Connect(); |
| 188 | listener.Init(client.channel()); |
| 189 | |
| 190 | IPC::TestChannelListener::SendOneMessage( |
| 191 | client.channel(), "hello from child"); |
| 192 | base::MessageLoop::current()->Run(); |
| 193 | EXPECT_TRUE(listener.is_connected_called()); |
| 194 | EXPECT_TRUE(listener.HasSentAll()); |
| 195 | |
rockot | cbca72f | 2015-03-03 16:31:04 | [diff] [blame] | 196 | client.Close(); |
| 197 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 198 | return 0; |
| 199 | } |
| 200 | |
morrita | 0a24cfc9 | 2014-09-16 03:20:48 | [diff] [blame] | 201 | class ListenerExpectingErrors : public IPC::Listener { |
| 202 | public: |
| 203 | ListenerExpectingErrors() |
| 204 | : has_error_(false) { |
| 205 | } |
| 206 | |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 207 | void OnChannelConnected(int32_t peer_pid) override { |
ki.stfu | a21ed8c | 2015-10-12 17:26:00 | [diff] [blame] | 208 | base::MessageLoop::current()->QuitWhenIdle(); |
morrita | be6c4cc | 2014-09-24 23:38:44 | [diff] [blame] | 209 | } |
| 210 | |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 211 | bool OnMessageReceived(const IPC::Message& message) override { return true; } |
morrita | 0a24cfc9 | 2014-09-16 03:20:48 | [diff] [blame] | 212 | |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 213 | void OnChannelError() override { |
morrita | 0a24cfc9 | 2014-09-16 03:20:48 | [diff] [blame] | 214 | has_error_ = true; |
ki.stfu | a21ed8c | 2015-10-12 17:26:00 | [diff] [blame] | 215 | base::MessageLoop::current()->QuitWhenIdle(); |
morrita | 0a24cfc9 | 2014-09-16 03:20:48 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | bool has_error() const { return has_error_; } |
| 219 | |
| 220 | private: |
| 221 | bool has_error_; |
| 222 | }; |
| 223 | |
| 224 | |
rockot | cbca72f | 2015-03-03 16:31:04 | [diff] [blame] | 225 | class IPCChannelMojoErrorTest : public IPCChannelMojoTestBase { |
morrita | 0a24cfc9 | 2014-09-16 03:20:48 | [diff] [blame] | 226 | protected: |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 227 | scoped_ptr<IPC::ChannelFactory> CreateChannelFactory( |
morrita | 0a24cfc9 | 2014-09-16 03:20:48 | [diff] [blame] | 228 | const IPC::ChannelHandle& handle, |
morrita | c4db547 | 2015-03-13 20:44:39 | [diff] [blame] | 229 | base::SequencedTaskRunner* runner) override { |
erikchen | 30dc281 | 2015-09-24 03:26:38 | [diff] [blame] | 230 | return IPC::ChannelMojo::CreateServerFactory(task_runner(), handle); |
morrita | 0a24cfc9 | 2014-09-16 03:20:48 | [diff] [blame] | 231 | } |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 232 | |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 233 | bool DidStartClient() override { |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 234 | bool ok = IPCTestBase::DidStartClient(); |
| 235 | DCHECK(ok); |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 236 | return ok; |
| 237 | } |
morrita | 0a24cfc9 | 2014-09-16 03:20:48 | [diff] [blame] | 238 | }; |
| 239 | |
| 240 | class ListenerThatQuits : public IPC::Listener { |
| 241 | public: |
| 242 | ListenerThatQuits() { |
| 243 | } |
| 244 | |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 245 | bool OnMessageReceived(const IPC::Message& message) override { |
dcheng | f3076af | 2014-10-21 18:02:42 | [diff] [blame] | 246 | return true; |
| 247 | } |
morrita | 0a24cfc9 | 2014-09-16 03:20:48 | [diff] [blame] | 248 | |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 249 | void OnChannelConnected(int32_t peer_pid) override { |
ki.stfu | a21ed8c | 2015-10-12 17:26:00 | [diff] [blame] | 250 | base::MessageLoop::current()->QuitWhenIdle(); |
morrita | 0a24cfc9 | 2014-09-16 03:20:48 | [diff] [blame] | 251 | } |
| 252 | }; |
| 253 | |
| 254 | // A long running process that connects to us. |
| 255 | MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoErraticTestClient) { |
| 256 | ListenerThatQuits listener; |
| 257 | ChannelClient client(&listener, "IPCChannelMojoErraticTestClient"); |
| 258 | client.Connect(); |
| 259 | |
| 260 | base::MessageLoop::current()->Run(); |
| 261 | |
rockot | cbca72f | 2015-03-03 16:31:04 | [diff] [blame] | 262 | client.Close(); |
| 263 | |
morrita | 0a24cfc9 | 2014-09-16 03:20:48 | [diff] [blame] | 264 | return 0; |
| 265 | } |
| 266 | |
msw | 8f7f5390 | 2015-06-19 16:26:47 | [diff] [blame] | 267 | // Times out on Android; see http://crbug.com/502290 |
| 268 | #if defined(OS_ANDROID) |
| 269 | #define MAYBE_SendFailWithPendingMessages DISABLED_SendFailWithPendingMessages |
| 270 | #else |
| 271 | #define MAYBE_SendFailWithPendingMessages SendFailWithPendingMessages |
| 272 | #endif |
| 273 | TEST_F(IPCChannelMojoErrorTest, MAYBE_SendFailWithPendingMessages) { |
rockot | cbca72f | 2015-03-03 16:31:04 | [diff] [blame] | 274 | InitWithMojo("IPCChannelMojoErraticTestClient"); |
morrita | 0a24cfc9 | 2014-09-16 03:20:48 | [diff] [blame] | 275 | |
| 276 | // Set up IPC channel and start client. |
| 277 | ListenerExpectingErrors listener; |
| 278 | CreateChannel(&listener); |
| 279 | ASSERT_TRUE(ConnectChannel()); |
| 280 | |
jamesr | a03ae49 | 2014-10-03 04:26:48 | [diff] [blame] | 281 | // This matches a value in mojo/edk/system/constants.h |
morrita | be6c4cc | 2014-09-24 23:38:44 | [diff] [blame] | 282 | const int kMaxMessageNumBytes = 4 * 1024 * 1024; |
| 283 | std::string overly_large_data(kMaxMessageNumBytes, '*'); |
morrita | 0a24cfc9 | 2014-09-16 03:20:48 | [diff] [blame] | 284 | // This messages are queued as pending. |
morrita | be6c4cc | 2014-09-24 23:38:44 | [diff] [blame] | 285 | for (size_t i = 0; i < 10; ++i) { |
morrita | 0a24cfc9 | 2014-09-16 03:20:48 | [diff] [blame] | 286 | IPC::TestChannelListener::SendOneMessage( |
morrita | be6c4cc | 2014-09-24 23:38:44 | [diff] [blame] | 287 | sender(), overly_large_data.c_str()); |
morrita | 0a24cfc9 | 2014-09-16 03:20:48 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | ASSERT_TRUE(StartClient()); |
| 291 | base::MessageLoop::current()->Run(); |
| 292 | |
| 293 | this->channel()->Close(); |
| 294 | |
| 295 | EXPECT_TRUE(WaitForClientShutdown()); |
| 296 | EXPECT_TRUE(listener.has_error()); |
| 297 | |
| 298 | DestroyChannel(); |
| 299 | } |
| 300 | |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 301 | struct TestingMessagePipe { |
| 302 | TestingMessagePipe() { |
| 303 | EXPECT_EQ(MOJO_RESULT_OK, mojo::CreateMessagePipe(nullptr, &self, &peer)); |
| 304 | } |
| 305 | |
| 306 | mojo::ScopedMessagePipeHandle self; |
| 307 | mojo::ScopedMessagePipeHandle peer; |
| 308 | }; |
| 309 | |
| 310 | class HandleSendingHelper { |
| 311 | public: |
| 312 | static std::string GetSendingFileContent() { return "Hello"; } |
| 313 | |
| 314 | static void WritePipe(IPC::Message* message, TestingMessagePipe* pipe) { |
| 315 | std::string content = HandleSendingHelper::GetSendingFileContent(); |
| 316 | EXPECT_EQ(MOJO_RESULT_OK, |
| 317 | mojo::WriteMessageRaw(pipe->self.get(), &content[0], |
| 318 | static_cast<uint32_t>(content.size()), |
| 319 | nullptr, 0, 0)); |
dcheng | e4860045 | 2015-12-28 02:24:50 | [diff] [blame] | 320 | EXPECT_TRUE(IPC::MojoMessageHelper::WriteMessagePipeTo( |
| 321 | message, std::move(pipe->peer))); |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | static void WritePipeThenSend(IPC::Sender* sender, TestingMessagePipe* pipe) { |
| 325 | IPC::Message* message = |
| 326 | new IPC::Message(0, 2, IPC::Message::PRIORITY_NORMAL); |
| 327 | WritePipe(message, pipe); |
| 328 | ASSERT_TRUE(sender->Send(message)); |
| 329 | } |
| 330 | |
| 331 | static void ReadReceivedPipe(const IPC::Message& message, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 332 | base::PickleIterator* iter) { |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 333 | mojo::ScopedMessagePipeHandle pipe; |
| 334 | EXPECT_TRUE( |
| 335 | IPC::MojoMessageHelper::ReadMessagePipeFrom(&message, iter, &pipe)); |
| 336 | std::string content(GetSendingFileContent().size(), ' '); |
| 337 | |
| 338 | uint32_t num_bytes = static_cast<uint32_t>(content.size()); |
| 339 | EXPECT_EQ(MOJO_RESULT_OK, |
| 340 | mojo::ReadMessageRaw(pipe.get(), &content[0], &num_bytes, nullptr, |
| 341 | nullptr, 0)); |
| 342 | EXPECT_EQ(content, GetSendingFileContent()); |
| 343 | } |
| 344 | |
| 345 | #if defined(OS_POSIX) |
| 346 | static base::FilePath GetSendingFilePath() { |
| 347 | base::FilePath path; |
| 348 | bool ok = PathService::Get(base::DIR_CACHE, &path); |
| 349 | EXPECT_TRUE(ok); |
| 350 | return path.Append("ListenerThatExpectsFile.txt"); |
| 351 | } |
| 352 | |
| 353 | static void WriteFile(IPC::Message* message, base::File& file) { |
| 354 | std::string content = GetSendingFileContent(); |
| 355 | file.WriteAtCurrentPos(content.data(), content.size()); |
| 356 | file.Flush(); |
| 357 | message->WriteAttachment(new IPC::internal::PlatformFileAttachment( |
| 358 | base::ScopedFD(file.TakePlatformFile()))); |
| 359 | } |
| 360 | |
| 361 | static void WriteFileThenSend(IPC::Sender* sender, base::File& file) { |
| 362 | IPC::Message* message = |
| 363 | new IPC::Message(0, 2, IPC::Message::PRIORITY_NORMAL); |
| 364 | WriteFile(message, file); |
| 365 | ASSERT_TRUE(sender->Send(message)); |
| 366 | } |
| 367 | |
| 368 | static void WriteFileAndPipeThenSend(IPC::Sender* sender, |
| 369 | base::File& file, |
| 370 | TestingMessagePipe* pipe) { |
| 371 | IPC::Message* message = |
| 372 | new IPC::Message(0, 2, IPC::Message::PRIORITY_NORMAL); |
| 373 | WriteFile(message, file); |
| 374 | WritePipe(message, pipe); |
| 375 | ASSERT_TRUE(sender->Send(message)); |
| 376 | } |
| 377 | |
| 378 | static void ReadReceivedFile(const IPC::Message& message, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 379 | base::PickleIterator* iter) { |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 380 | base::ScopedFD fd; |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame^] | 381 | scoped_refptr<base::Pickle::Attachment> attachment; |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 382 | EXPECT_TRUE(message.ReadAttachment(iter, &attachment)); |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame^] | 383 | base::File file(static_cast<IPC::MessageAttachment*>(attachment.get()) |
| 384 | ->TakePlatformFile()); |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 385 | std::string content(GetSendingFileContent().size(), ' '); |
| 386 | file.Read(0, &content[0], content.size()); |
| 387 | EXPECT_EQ(content, GetSendingFileContent()); |
| 388 | } |
| 389 | #endif |
| 390 | }; |
| 391 | |
| 392 | class ListenerThatExpectsMessagePipe : public IPC::Listener { |
| 393 | public: |
| 394 | ListenerThatExpectsMessagePipe() : sender_(NULL) {} |
| 395 | |
| 396 | ~ListenerThatExpectsMessagePipe() override {} |
| 397 | |
| 398 | bool OnMessageReceived(const IPC::Message& message) override { |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 399 | base::PickleIterator iter(message); |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 400 | HandleSendingHelper::ReadReceivedPipe(message, &iter); |
ki.stfu | a21ed8c | 2015-10-12 17:26:00 | [diff] [blame] | 401 | base::MessageLoop::current()->QuitWhenIdle(); |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 402 | ListenerThatExpectsOK::SendOK(sender_); |
| 403 | return true; |
| 404 | } |
| 405 | |
| 406 | void OnChannelError() override { NOTREACHED(); } |
| 407 | |
| 408 | void set_sender(IPC::Sender* sender) { sender_ = sender; } |
| 409 | |
| 410 | private: |
| 411 | IPC::Sender* sender_; |
| 412 | }; |
| 413 | |
msw | 8f7f5390 | 2015-06-19 16:26:47 | [diff] [blame] | 414 | // Times out on Android; see http://crbug.com/502290 |
| 415 | #if defined(OS_ANDROID) |
| 416 | #define MAYBE_SendMessagePipe DISABLED_SendMessagePipe |
| 417 | #else |
| 418 | #define MAYBE_SendMessagePipe SendMessagePipe |
| 419 | #endif |
| 420 | TEST_F(IPCChannelMojoTest, MAYBE_SendMessagePipe) { |
rockot | cbca72f | 2015-03-03 16:31:04 | [diff] [blame] | 421 | InitWithMojo("IPCChannelMojoTestSendMessagePipeClient"); |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 422 | |
| 423 | ListenerThatExpectsOK listener; |
| 424 | CreateChannel(&listener); |
| 425 | ASSERT_TRUE(ConnectChannel()); |
| 426 | ASSERT_TRUE(StartClient()); |
| 427 | |
| 428 | TestingMessagePipe pipe; |
| 429 | HandleSendingHelper::WritePipeThenSend(channel(), &pipe); |
| 430 | |
| 431 | base::MessageLoop::current()->Run(); |
| 432 | this->channel()->Close(); |
| 433 | |
| 434 | EXPECT_TRUE(WaitForClientShutdown()); |
| 435 | DestroyChannel(); |
| 436 | } |
| 437 | |
| 438 | MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoTestSendMessagePipeClient) { |
| 439 | ListenerThatExpectsMessagePipe listener; |
morrita | e73c0b4 | 2015-03-05 02:55:19 | [diff] [blame] | 440 | ChannelClient client(&listener, "IPCChannelMojoTestSendMessagePipeClient"); |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 441 | client.Connect(); |
| 442 | listener.set_sender(client.channel()); |
| 443 | |
| 444 | base::MessageLoop::current()->Run(); |
| 445 | |
rockot | cbca72f | 2015-03-03 16:31:04 | [diff] [blame] | 446 | client.Close(); |
| 447 | |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 448 | return 0; |
| 449 | } |
| 450 | |
morrita | 438a2ee | 2015-04-03 05:28:21 | [diff] [blame] | 451 | void ReadOK(mojo::MessagePipeHandle pipe) { |
| 452 | std::string should_be_ok("xx"); |
| 453 | uint32_t num_bytes = static_cast<uint32_t>(should_be_ok.size()); |
| 454 | CHECK_EQ(MOJO_RESULT_OK, |
| 455 | mojo::ReadMessageRaw(pipe, &should_be_ok[0], &num_bytes, nullptr, |
| 456 | nullptr, 0)); |
| 457 | EXPECT_EQ(should_be_ok, std::string("OK")); |
| 458 | } |
| 459 | |
| 460 | void WriteOK(mojo::MessagePipeHandle pipe) { |
| 461 | std::string ok("OK"); |
| 462 | CHECK_EQ(MOJO_RESULT_OK, |
| 463 | mojo::WriteMessageRaw(pipe, &ok[0], static_cast<uint32_t>(ok.size()), |
| 464 | nullptr, 0, 0)); |
| 465 | } |
| 466 | |
| 467 | class ListenerThatExpectsMessagePipeUsingParamTrait : public IPC::Listener { |
| 468 | public: |
| 469 | explicit ListenerThatExpectsMessagePipeUsingParamTrait(bool receiving_valid) |
| 470 | : sender_(NULL), receiving_valid_(receiving_valid) {} |
| 471 | |
| 472 | ~ListenerThatExpectsMessagePipeUsingParamTrait() override {} |
| 473 | |
| 474 | bool OnMessageReceived(const IPC::Message& message) override { |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 475 | base::PickleIterator iter(message); |
morrita | 438a2ee | 2015-04-03 05:28:21 | [diff] [blame] | 476 | mojo::MessagePipeHandle handle; |
| 477 | EXPECT_TRUE(IPC::ParamTraits<mojo::MessagePipeHandle>::Read(&message, &iter, |
| 478 | &handle)); |
| 479 | EXPECT_EQ(handle.is_valid(), receiving_valid_); |
| 480 | if (receiving_valid_) { |
| 481 | ReadOK(handle); |
| 482 | MojoClose(handle.value()); |
| 483 | } |
| 484 | |
ki.stfu | a21ed8c | 2015-10-12 17:26:00 | [diff] [blame] | 485 | base::MessageLoop::current()->QuitWhenIdle(); |
morrita | 438a2ee | 2015-04-03 05:28:21 | [diff] [blame] | 486 | ListenerThatExpectsOK::SendOK(sender_); |
| 487 | return true; |
| 488 | } |
| 489 | |
| 490 | void OnChannelError() override { NOTREACHED(); } |
| 491 | void set_sender(IPC::Sender* sender) { sender_ = sender; } |
| 492 | |
| 493 | private: |
| 494 | IPC::Sender* sender_; |
| 495 | bool receiving_valid_; |
| 496 | }; |
| 497 | |
| 498 | void ParamTraitMessagePipeClient(bool receiving_valid_handle, |
| 499 | const char* channel_name) { |
| 500 | ListenerThatExpectsMessagePipeUsingParamTrait listener( |
| 501 | receiving_valid_handle); |
| 502 | ChannelClient client(&listener, channel_name); |
| 503 | client.Connect(); |
| 504 | listener.set_sender(client.channel()); |
| 505 | |
| 506 | base::MessageLoop::current()->Run(); |
| 507 | |
| 508 | client.Close(); |
| 509 | } |
| 510 | |
msw | 8f7f5390 | 2015-06-19 16:26:47 | [diff] [blame] | 511 | // Times out on Android; see http://crbug.com/502290 |
| 512 | #if defined(OS_ANDROID) |
| 513 | #define MAYBE_ParamTraitValidMessagePipe DISABLED_ParamTraitValidMessagePipe |
| 514 | #else |
| 515 | #define MAYBE_ParamTraitValidMessagePipe ParamTraitValidMessagePipe |
| 516 | #endif |
| 517 | TEST_F(IPCChannelMojoTest, MAYBE_ParamTraitValidMessagePipe) { |
morrita | 438a2ee | 2015-04-03 05:28:21 | [diff] [blame] | 518 | InitWithMojo("ParamTraitValidMessagePipeClient"); |
| 519 | |
| 520 | ListenerThatExpectsOK listener; |
| 521 | CreateChannel(&listener); |
| 522 | ASSERT_TRUE(ConnectChannel()); |
| 523 | ASSERT_TRUE(StartClient()); |
| 524 | |
| 525 | TestingMessagePipe pipe; |
| 526 | |
| 527 | scoped_ptr<IPC::Message> message(new IPC::Message()); |
| 528 | IPC::ParamTraits<mojo::MessagePipeHandle>::Write(message.get(), |
| 529 | pipe.peer.release()); |
| 530 | WriteOK(pipe.self.get()); |
| 531 | |
| 532 | this->channel()->Send(message.release()); |
| 533 | base::MessageLoop::current()->Run(); |
| 534 | this->channel()->Close(); |
| 535 | |
| 536 | EXPECT_TRUE(WaitForClientShutdown()); |
| 537 | DestroyChannel(); |
| 538 | } |
| 539 | |
| 540 | MULTIPROCESS_IPC_TEST_CLIENT_MAIN(ParamTraitValidMessagePipeClient) { |
| 541 | ParamTraitMessagePipeClient(true, "ParamTraitValidMessagePipeClient"); |
| 542 | return 0; |
| 543 | } |
| 544 | |
msw | 8f7f5390 | 2015-06-19 16:26:47 | [diff] [blame] | 545 | // Times out on Android; see http://crbug.com/502290 |
| 546 | #if defined(OS_ANDROID) |
| 547 | #define MAYBE_ParamTraitInvalidMessagePipe DISABLED_ParamTraitInvalidMessagePipe |
| 548 | #else |
| 549 | #define MAYBE_ParamTraitInvalidMessagePipe ParamTraitInvalidMessagePipe |
| 550 | #endif |
| 551 | TEST_F(IPCChannelMojoTest, MAYBE_ParamTraitInvalidMessagePipe) { |
morrita | 438a2ee | 2015-04-03 05:28:21 | [diff] [blame] | 552 | InitWithMojo("ParamTraitInvalidMessagePipeClient"); |
| 553 | |
| 554 | ListenerThatExpectsOK listener; |
| 555 | CreateChannel(&listener); |
| 556 | ASSERT_TRUE(ConnectChannel()); |
| 557 | ASSERT_TRUE(StartClient()); |
| 558 | |
| 559 | mojo::MessagePipeHandle invalid_handle; |
| 560 | scoped_ptr<IPC::Message> message(new IPC::Message()); |
| 561 | IPC::ParamTraits<mojo::MessagePipeHandle>::Write(message.get(), |
| 562 | invalid_handle); |
| 563 | |
| 564 | this->channel()->Send(message.release()); |
| 565 | base::MessageLoop::current()->Run(); |
| 566 | this->channel()->Close(); |
| 567 | |
| 568 | EXPECT_TRUE(WaitForClientShutdown()); |
| 569 | DestroyChannel(); |
| 570 | } |
| 571 | |
| 572 | MULTIPROCESS_IPC_TEST_CLIENT_MAIN(ParamTraitInvalidMessagePipeClient) { |
| 573 | ParamTraitMessagePipeClient(false, "ParamTraitInvalidMessagePipeClient"); |
| 574 | return 0; |
| 575 | } |
| 576 | |
morrita | 17137e6 | 2015-06-23 22:29:36 | [diff] [blame] | 577 | TEST_F(IPCChannelMojoTest, SendFailAfterClose) { |
| 578 | InitWithMojo("IPCChannelMojoTestSendOkClient"); |
| 579 | |
| 580 | ListenerThatExpectsOK listener; |
| 581 | CreateChannel(&listener); |
| 582 | ASSERT_TRUE(ConnectChannel()); |
| 583 | ASSERT_TRUE(StartClient()); |
| 584 | |
| 585 | base::MessageLoop::current()->Run(); |
| 586 | this->channel()->Close(); |
| 587 | ASSERT_FALSE(this->channel()->Send(new IPC::Message())); |
| 588 | |
| 589 | EXPECT_TRUE(WaitForClientShutdown()); |
| 590 | DestroyChannel(); |
| 591 | } |
| 592 | |
| 593 | class ListenerSendingOneOk : public IPC::Listener { |
| 594 | public: |
| 595 | ListenerSendingOneOk() { |
| 596 | } |
| 597 | |
| 598 | bool OnMessageReceived(const IPC::Message& message) override { |
| 599 | return true; |
| 600 | } |
| 601 | |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 602 | void OnChannelConnected(int32_t peer_pid) override { |
morrita | 17137e6 | 2015-06-23 22:29:36 | [diff] [blame] | 603 | ListenerThatExpectsOK::SendOK(sender_); |
ki.stfu | a21ed8c | 2015-10-12 17:26:00 | [diff] [blame] | 604 | base::MessageLoop::current()->QuitWhenIdle(); |
morrita | 17137e6 | 2015-06-23 22:29:36 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | void set_sender(IPC::Sender* sender) { sender_ = sender; } |
| 608 | |
| 609 | private: |
| 610 | IPC::Sender* sender_; |
| 611 | }; |
| 612 | |
| 613 | MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoTestSendOkClient) { |
| 614 | ListenerSendingOneOk listener; |
| 615 | ChannelClient client(&listener, "IPCChannelMojoTestSendOkClient"); |
| 616 | client.Connect(); |
| 617 | listener.set_sender(client.channel()); |
| 618 | |
| 619 | base::MessageLoop::current()->Run(); |
| 620 | |
| 621 | client.Close(); |
| 622 | |
| 623 | return 0; |
| 624 | } |
| 625 | |
morrita | 2580367 | 2014-10-15 18:50:19 | [diff] [blame] | 626 | #if defined(OS_WIN) |
rockot | cbca72f | 2015-03-03 16:31:04 | [diff] [blame] | 627 | class IPCChannelMojoDeadHandleTest : public IPCChannelMojoTestBase { |
morrita | 2580367 | 2014-10-15 18:50:19 | [diff] [blame] | 628 | protected: |
nick | d60f717 | 2015-04-23 16:42:48 | [diff] [blame] | 629 | scoped_ptr<IPC::ChannelFactory> CreateChannelFactory( |
morrita | 2580367 | 2014-10-15 18:50:19 | [diff] [blame] | 630 | const IPC::ChannelHandle& handle, |
morrita | c4db547 | 2015-03-13 20:44:39 | [diff] [blame] | 631 | base::SequencedTaskRunner* runner) override { |
erikchen | 30dc281 | 2015-09-24 03:26:38 | [diff] [blame] | 632 | return IPC::ChannelMojo::CreateServerFactory(task_runner(), handle); |
morrita | 2580367 | 2014-10-15 18:50:19 | [diff] [blame] | 633 | } |
| 634 | |
nick | d60f717 | 2015-04-23 16:42:48 | [diff] [blame] | 635 | bool DidStartClient() override { |
morrita | 2580367 | 2014-10-15 18:50:19 | [diff] [blame] | 636 | IPCTestBase::DidStartClient(); |
leon.han | d20a6c4c | 2015-06-19 02:25:48 | [diff] [blame] | 637 | // const base::ProcessHandle client = client_process().Handle(); |
morrita | 2580367 | 2014-10-15 18:50:19 | [diff] [blame] | 638 | // Forces GetFileHandleForProcess() fail. It happens occasionally |
| 639 | // in production, so we should exercise it somehow. |
morrita | e73c0b4 | 2015-03-05 02:55:19 | [diff] [blame] | 640 | // TODO(morrita): figure out how to safely test this. See crbug.com/464109. |
rvargas | 07b589c | 2015-01-12 22:23:23 | [diff] [blame] | 641 | // ::CloseHandle(client); |
morrita | 2580367 | 2014-10-15 18:50:19 | [diff] [blame] | 642 | return true; |
| 643 | } |
morrita | 2580367 | 2014-10-15 18:50:19 | [diff] [blame] | 644 | }; |
| 645 | |
| 646 | TEST_F(IPCChannelMojoDeadHandleTest, InvalidClientHandle) { |
| 647 | // Any client type is fine as it is going to be killed anyway. |
rockot | cbca72f | 2015-03-03 16:31:04 | [diff] [blame] | 648 | InitWithMojo("IPCChannelMojoTestDoNothingClient"); |
morrita | 2580367 | 2014-10-15 18:50:19 | [diff] [blame] | 649 | |
| 650 | // Set up IPC channel and start client. |
| 651 | ListenerExpectingErrors listener; |
| 652 | CreateChannel(&listener); |
| 653 | ASSERT_TRUE(ConnectChannel()); |
| 654 | |
| 655 | ASSERT_TRUE(StartClient()); |
| 656 | base::MessageLoop::current()->Run(); |
| 657 | |
| 658 | this->channel()->Close(); |
| 659 | |
morrita | e73c0b4 | 2015-03-05 02:55:19 | [diff] [blame] | 660 | // TODO(morrita): We need CloseHandle() call in DidStartClient(), |
| 661 | // which has been disabled since crrev.com/843113003, to |
| 662 | // make this fail. See crbug.com/464109. |
| 663 | // EXPECT_FALSE(WaitForClientShutdown()); |
| 664 | WaitForClientShutdown(); |
morrita | 2580367 | 2014-10-15 18:50:19 | [diff] [blame] | 665 | EXPECT_TRUE(listener.has_error()); |
| 666 | |
| 667 | DestroyChannel(); |
| 668 | } |
| 669 | |
| 670 | MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoTestDoNothingClient) { |
| 671 | ListenerThatQuits listener; |
| 672 | ChannelClient client(&listener, "IPCChannelMojoTestDoNothingClient"); |
| 673 | client.Connect(); |
| 674 | |
| 675 | // Quits without running the message loop as this client won't |
| 676 | // receive any messages from the server. |
| 677 | |
| 678 | return 0; |
| 679 | } |
| 680 | #endif |
morrita | 0a24cfc9 | 2014-09-16 03:20:48 | [diff] [blame] | 681 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 682 | #if defined(OS_POSIX) |
| 683 | class ListenerThatExpectsFile : public IPC::Listener { |
| 684 | public: |
| 685 | ListenerThatExpectsFile() |
| 686 | : sender_(NULL) {} |
| 687 | |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 688 | ~ListenerThatExpectsFile() override {} |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 689 | |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 690 | bool OnMessageReceived(const IPC::Message& message) override { |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 691 | base::PickleIterator iter(message); |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 692 | HandleSendingHelper::ReadReceivedFile(message, &iter); |
ki.stfu | a21ed8c | 2015-10-12 17:26:00 | [diff] [blame] | 693 | base::MessageLoop::current()->QuitWhenIdle(); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 694 | ListenerThatExpectsOK::SendOK(sender_); |
| 695 | return true; |
| 696 | } |
| 697 | |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 698 | void OnChannelError() override { |
dcheng | f3076af | 2014-10-21 18:02:42 | [diff] [blame] | 699 | NOTREACHED(); |
| 700 | } |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 701 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 702 | void set_sender(IPC::Sender* sender) { sender_ = sender; } |
| 703 | |
| 704 | private: |
| 705 | IPC::Sender* sender_; |
| 706 | }; |
| 707 | |
msw | 8f7f5390 | 2015-06-19 16:26:47 | [diff] [blame] | 708 | // Times out on Android; see http://crbug.com/502290 |
| 709 | #if defined(OS_ANDROID) |
| 710 | #define MAYBE_SendPlatformHandle DISABLED_SendPlatformHandle |
| 711 | #else |
| 712 | #define MAYBE_SendPlatformHandle SendPlatformHandle |
| 713 | #endif |
| 714 | TEST_F(IPCChannelMojoTest, MAYBE_SendPlatformHandle) { |
rockot | cbca72f | 2015-03-03 16:31:04 | [diff] [blame] | 715 | InitWithMojo("IPCChannelMojoTestSendPlatformHandleClient"); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 716 | |
| 717 | ListenerThatExpectsOK listener; |
morrita | 373af03b | 2014-09-09 19:35:24 | [diff] [blame] | 718 | CreateChannel(&listener); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 719 | ASSERT_TRUE(ConnectChannel()); |
| 720 | ASSERT_TRUE(StartClient()); |
| 721 | |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 722 | base::File file(HandleSendingHelper::GetSendingFilePath(), |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 723 | base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE | |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 724 | base::File::FLAG_READ); |
| 725 | HandleSendingHelper::WriteFileThenSend(channel(), file); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 726 | base::MessageLoop::current()->Run(); |
| 727 | |
| 728 | this->channel()->Close(); |
| 729 | |
| 730 | EXPECT_TRUE(WaitForClientShutdown()); |
| 731 | DestroyChannel(); |
| 732 | } |
| 733 | |
| 734 | MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoTestSendPlatformHandleClient) { |
| 735 | ListenerThatExpectsFile listener; |
| 736 | ChannelClient client( |
| 737 | &listener, "IPCChannelMojoTestSendPlatformHandleClient"); |
| 738 | client.Connect(); |
| 739 | listener.set_sender(client.channel()); |
| 740 | |
| 741 | base::MessageLoop::current()->Run(); |
| 742 | |
rockot | cbca72f | 2015-03-03 16:31:04 | [diff] [blame] | 743 | client.Close(); |
| 744 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 745 | return 0; |
| 746 | } |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 747 | |
| 748 | class ListenerThatExpectsFileAndPipe : public IPC::Listener { |
| 749 | public: |
| 750 | ListenerThatExpectsFileAndPipe() : sender_(NULL) {} |
| 751 | |
| 752 | ~ListenerThatExpectsFileAndPipe() override {} |
| 753 | |
| 754 | bool OnMessageReceived(const IPC::Message& message) override { |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 755 | base::PickleIterator iter(message); |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 756 | HandleSendingHelper::ReadReceivedFile(message, &iter); |
| 757 | HandleSendingHelper::ReadReceivedPipe(message, &iter); |
ki.stfu | a21ed8c | 2015-10-12 17:26:00 | [diff] [blame] | 758 | base::MessageLoop::current()->QuitWhenIdle(); |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 759 | ListenerThatExpectsOK::SendOK(sender_); |
| 760 | return true; |
| 761 | } |
| 762 | |
| 763 | void OnChannelError() override { NOTREACHED(); } |
| 764 | |
| 765 | void set_sender(IPC::Sender* sender) { sender_ = sender; } |
| 766 | |
| 767 | private: |
| 768 | IPC::Sender* sender_; |
| 769 | }; |
| 770 | |
msw | 8f7f5390 | 2015-06-19 16:26:47 | [diff] [blame] | 771 | // Times out on Android; see http://crbug.com/502290 |
| 772 | #if defined(OS_ANDROID) |
| 773 | #define MAYBE_SendPlatformHandleAndPipe DISABLED_SendPlatformHandleAndPipe |
| 774 | #else |
| 775 | #define MAYBE_SendPlatformHandleAndPipe SendPlatformHandleAndPipe |
| 776 | #endif |
| 777 | TEST_F(IPCChannelMojoTest, MAYBE_SendPlatformHandleAndPipe) { |
rockot | cbca72f | 2015-03-03 16:31:04 | [diff] [blame] | 778 | InitWithMojo("IPCChannelMojoTestSendPlatformHandleAndPipeClient"); |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 779 | |
| 780 | ListenerThatExpectsOK listener; |
| 781 | CreateChannel(&listener); |
| 782 | ASSERT_TRUE(ConnectChannel()); |
| 783 | ASSERT_TRUE(StartClient()); |
| 784 | |
| 785 | base::File file(HandleSendingHelper::GetSendingFilePath(), |
| 786 | base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE | |
| 787 | base::File::FLAG_READ); |
| 788 | TestingMessagePipe pipe; |
| 789 | HandleSendingHelper::WriteFileAndPipeThenSend(channel(), file, &pipe); |
| 790 | |
| 791 | base::MessageLoop::current()->Run(); |
| 792 | this->channel()->Close(); |
| 793 | |
| 794 | EXPECT_TRUE(WaitForClientShutdown()); |
| 795 | DestroyChannel(); |
| 796 | } |
| 797 | |
| 798 | MULTIPROCESS_IPC_TEST_CLIENT_MAIN( |
| 799 | IPCChannelMojoTestSendPlatformHandleAndPipeClient) { |
| 800 | ListenerThatExpectsFileAndPipe listener; |
| 801 | ChannelClient client(&listener, |
| 802 | "IPCChannelMojoTestSendPlatformHandleAndPipeClient"); |
| 803 | client.Connect(); |
| 804 | listener.set_sender(client.channel()); |
| 805 | |
| 806 | base::MessageLoop::current()->Run(); |
| 807 | |
rockot | cbca72f | 2015-03-03 16:31:04 | [diff] [blame] | 808 | client.Close(); |
| 809 | |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 810 | return 0; |
| 811 | } |
| 812 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 813 | #endif |
| 814 | |
morrita | 0bd20bd | 2015-02-25 20:11:27 | [diff] [blame] | 815 | #if defined(OS_LINUX) |
| 816 | |
| 817 | const base::ProcessId kMagicChildId = 54321; |
| 818 | |
| 819 | class ListenerThatVerifiesPeerPid : public IPC::Listener { |
| 820 | public: |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 821 | void OnChannelConnected(int32_t peer_pid) override { |
morrita | 0bd20bd | 2015-02-25 20:11:27 | [diff] [blame] | 822 | EXPECT_EQ(peer_pid, kMagicChildId); |
ki.stfu | a21ed8c | 2015-10-12 17:26:00 | [diff] [blame] | 823 | base::MessageLoop::current()->QuitWhenIdle(); |
morrita | 0bd20bd | 2015-02-25 20:11:27 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | bool OnMessageReceived(const IPC::Message& message) override { |
| 827 | NOTREACHED(); |
| 828 | return true; |
| 829 | } |
| 830 | }; |
| 831 | |
| 832 | TEST_F(IPCChannelMojoTest, VerifyGlobalPid) { |
rockot | cbca72f | 2015-03-03 16:31:04 | [diff] [blame] | 833 | InitWithMojo("IPCChannelMojoTestVerifyGlobalPidClient"); |
morrita | 0bd20bd | 2015-02-25 20:11:27 | [diff] [blame] | 834 | |
| 835 | ListenerThatVerifiesPeerPid listener; |
| 836 | CreateChannel(&listener); |
| 837 | ASSERT_TRUE(ConnectChannel()); |
| 838 | ASSERT_TRUE(StartClient()); |
| 839 | |
| 840 | base::MessageLoop::current()->Run(); |
rockot | cbca72f | 2015-03-03 16:31:04 | [diff] [blame] | 841 | channel()->Close(); |
morrita | 0bd20bd | 2015-02-25 20:11:27 | [diff] [blame] | 842 | |
| 843 | EXPECT_TRUE(WaitForClientShutdown()); |
| 844 | DestroyChannel(); |
| 845 | } |
| 846 | |
| 847 | MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoTestVerifyGlobalPidClient) { |
| 848 | IPC::Channel::SetGlobalPid(kMagicChildId); |
| 849 | ListenerThatQuits listener; |
| 850 | ChannelClient client(&listener, |
| 851 | "IPCChannelMojoTestVerifyGlobalPidClient"); |
| 852 | client.Connect(); |
| 853 | |
| 854 | base::MessageLoop::current()->Run(); |
| 855 | |
rockot | cbca72f | 2015-03-03 16:31:04 | [diff] [blame] | 856 | client.Close(); |
| 857 | |
morrita | 0bd20bd | 2015-02-25 20:11:27 | [diff] [blame] | 858 | return 0; |
| 859 | } |
| 860 | |
| 861 | #endif // OS_LINUX |
| 862 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 863 | } // namespace |