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