| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ipc/mojo/ipc_channel_mojo.h" | 5 #include "ipc/mojo/ipc_channel_mojo.h" |
| 6 | 6 |
| 7 #include "base/base_paths.h" | 7 #include "base/base_paths.h" |
| 8 #include "base/files/file.h" | 8 #include "base/files/file.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| 11 #include "base/pickle.h" | 11 #include "base/pickle.h" |
| 12 #include "base/threading/thread.h" | 12 #include "base/threading/thread.h" |
| 13 #include "ipc/ipc_message.h" | 13 #include "ipc/ipc_message.h" |
| 14 #include "ipc/ipc_test_base.h" | 14 #include "ipc/ipc_test_base.h" |
| 15 #include "ipc/ipc_test_channel_listener.h" | 15 #include "ipc/ipc_test_channel_listener.h" |
| 16 #include "ipc/mojo/ipc_channel_mojo_host.h" | 16 #include "ipc/mojo/ipc_channel_mojo_host.h" |
| 17 #include "ipc/mojo/ipc_channel_mojo_readers.h" | 17 #include "ipc/mojo/ipc_channel_mojo_readers.h" |
| 18 | 18 |
| 19 #if defined(OS_POSIX) | 19 #if defined(OS_POSIX) |
| 20 #include "base/file_descriptor_posix.h" | 20 #include "base/file_descriptor_posix.h" |
| 21 #endif | 21 #endif |
| 22 | 22 |
| 23 namespace { | 23 namespace { |
| 24 | 24 |
| 25 class ListenerThatExpectsOK : public IPC::Listener { | 25 class ListenerThatExpectsOK : public IPC::Listener { |
| 26 public: | 26 public: |
| 27 ListenerThatExpectsOK() | 27 ListenerThatExpectsOK() |
| 28 : received_ok_(false) {} | 28 : received_ok_(false) {} |
| 29 | 29 |
| 30 virtual ~ListenerThatExpectsOK() {} | 30 ~ListenerThatExpectsOK() override {} |
| 31 | 31 |
| 32 virtual bool OnMessageReceived(const IPC::Message& message) override { | 32 bool OnMessageReceived(const IPC::Message& message) override { |
| 33 PickleIterator iter(message); | 33 PickleIterator iter(message); |
| 34 std::string should_be_ok; | 34 std::string should_be_ok; |
| 35 EXPECT_TRUE(iter.ReadString(&should_be_ok)); | 35 EXPECT_TRUE(iter.ReadString(&should_be_ok)); |
| 36 EXPECT_EQ(should_be_ok, "OK"); | 36 EXPECT_EQ(should_be_ok, "OK"); |
| 37 received_ok_ = true; | 37 received_ok_ = true; |
| 38 base::MessageLoop::current()->Quit(); | 38 base::MessageLoop::current()->Quit(); |
| 39 return true; | 39 return true; |
| 40 } | 40 } |
| 41 | 41 |
| 42 virtual void OnChannelError() override { | 42 void OnChannelError() override { |
| 43 // The connection should be healthy while the listener is waiting | 43 // The connection should be healthy while the listener is waiting |
| 44 // message. An error can occur after that because the peer | 44 // message. An error can occur after that because the peer |
| 45 // process dies. | 45 // process dies. |
| 46 DCHECK(received_ok_); | 46 DCHECK(received_ok_); |
| 47 } | 47 } |
| 48 | 48 |
| 49 static void SendOK(IPC::Sender* sender) { | 49 static void SendOK(IPC::Sender* sender) { |
| 50 IPC::Message* message = new IPC::Message( | 50 IPC::Message* message = new IPC::Message( |
| 51 0, 2, IPC::Message::PRIORITY_NORMAL); | 51 0, 2, IPC::Message::PRIORITY_NORMAL); |
| 52 message->WriteString(std::string("OK")); | 52 message->WriteString(std::string("OK")); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 72 | 72 |
| 73 IPC::ChannelMojo* channel() const { return channel_.get(); } | 73 IPC::ChannelMojo* channel() const { return channel_.get(); } |
| 74 | 74 |
| 75 private: | 75 private: |
| 76 base::MessageLoopForIO main_message_loop_; | 76 base::MessageLoopForIO main_message_loop_; |
| 77 scoped_ptr<IPC::ChannelMojo> channel_; | 77 scoped_ptr<IPC::ChannelMojo> channel_; |
| 78 }; | 78 }; |
| 79 | 79 |
| 80 class IPCChannelMojoTest : public IPCTestBase { | 80 class IPCChannelMojoTest : public IPCTestBase { |
| 81 protected: | 81 protected: |
| 82 virtual scoped_ptr<IPC::ChannelFactory> CreateChannelFactory( | 82 scoped_ptr<IPC::ChannelFactory> CreateChannelFactory( |
| 83 const IPC::ChannelHandle& handle, | 83 const IPC::ChannelHandle& handle, |
| 84 base::TaskRunner* runner) override { | 84 base::TaskRunner* runner) override { |
| 85 host_.reset(new IPC::ChannelMojoHost(task_runner())); | 85 host_.reset(new IPC::ChannelMojoHost(task_runner())); |
| 86 return IPC::ChannelMojo::CreateServerFactory(host_->channel_delegate(), | 86 return IPC::ChannelMojo::CreateServerFactory(host_->channel_delegate(), |
| 87 handle); | 87 handle); |
| 88 } | 88 } |
| 89 | 89 |
| 90 virtual bool DidStartClient() override { | 90 bool DidStartClient() override { |
| 91 bool ok = IPCTestBase::DidStartClient(); | 91 bool ok = IPCTestBase::DidStartClient(); |
| 92 DCHECK(ok); | 92 DCHECK(ok); |
| 93 host_->OnClientLaunched(client_process()); | 93 host_->OnClientLaunched(client_process()); |
| 94 return ok; | 94 return ok; |
| 95 } | 95 } |
| 96 | 96 |
| 97 private: | 97 private: |
| 98 scoped_ptr<IPC::ChannelMojoHost> host_; | 98 scoped_ptr<IPC::ChannelMojoHost> host_; |
| 99 }; | 99 }; |
| 100 | 100 |
| 101 | 101 |
| 102 class TestChannelListenerWithExtraExpectations | 102 class TestChannelListenerWithExtraExpectations |
| 103 : public IPC::TestChannelListener { | 103 : public IPC::TestChannelListener { |
| 104 public: | 104 public: |
| 105 TestChannelListenerWithExtraExpectations() | 105 TestChannelListenerWithExtraExpectations() |
| 106 : is_connected_called_(false) { | 106 : is_connected_called_(false) { |
| 107 } | 107 } |
| 108 | 108 |
| 109 virtual void OnChannelConnected(int32 peer_pid) override { | 109 void OnChannelConnected(int32 peer_pid) override { |
| 110 IPC::TestChannelListener::OnChannelConnected(peer_pid); | 110 IPC::TestChannelListener::OnChannelConnected(peer_pid); |
| 111 EXPECT_TRUE(base::kNullProcessId != peer_pid); | 111 EXPECT_TRUE(base::kNullProcessId != peer_pid); |
| 112 is_connected_called_ = true; | 112 is_connected_called_ = true; |
| 113 } | 113 } |
| 114 | 114 |
| 115 bool is_connected_called() const { return is_connected_called_; } | 115 bool is_connected_called() const { return is_connected_called_; } |
| 116 | 116 |
| 117 private: | 117 private: |
| 118 bool is_connected_called_; | 118 bool is_connected_called_; |
| 119 }; | 119 }; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 | 158 |
| 159 return 0; | 159 return 0; |
| 160 } | 160 } |
| 161 | 161 |
| 162 class ListenerExpectingErrors : public IPC::Listener { | 162 class ListenerExpectingErrors : public IPC::Listener { |
| 163 public: | 163 public: |
| 164 ListenerExpectingErrors() | 164 ListenerExpectingErrors() |
| 165 : has_error_(false) { | 165 : has_error_(false) { |
| 166 } | 166 } |
| 167 | 167 |
| 168 virtual void OnChannelConnected(int32 peer_pid) override { | 168 void OnChannelConnected(int32 peer_pid) override { |
| 169 base::MessageLoop::current()->Quit(); | 169 base::MessageLoop::current()->Quit(); |
| 170 } | 170 } |
| 171 | 171 |
| 172 virtual bool OnMessageReceived(const IPC::Message& message) override { | 172 bool OnMessageReceived(const IPC::Message& message) override { return true; } |
| 173 return true; | |
| 174 } | |
| 175 | 173 |
| 176 virtual void OnChannelError() override { | 174 void OnChannelError() override { |
| 177 has_error_ = true; | 175 has_error_ = true; |
| 178 base::MessageLoop::current()->Quit(); | 176 base::MessageLoop::current()->Quit(); |
| 179 } | 177 } |
| 180 | 178 |
| 181 bool has_error() const { return has_error_; } | 179 bool has_error() const { return has_error_; } |
| 182 | 180 |
| 183 private: | 181 private: |
| 184 bool has_error_; | 182 bool has_error_; |
| 185 }; | 183 }; |
| 186 | 184 |
| 187 | 185 |
| 188 class IPCChannelMojoErrorTest : public IPCTestBase { | 186 class IPCChannelMojoErrorTest : public IPCTestBase { |
| 189 protected: | 187 protected: |
| 190 virtual scoped_ptr<IPC::ChannelFactory> CreateChannelFactory( | 188 scoped_ptr<IPC::ChannelFactory> CreateChannelFactory( |
| 191 const IPC::ChannelHandle& handle, | 189 const IPC::ChannelHandle& handle, |
| 192 base::TaskRunner* runner) override { | 190 base::TaskRunner* runner) override { |
| 193 host_.reset(new IPC::ChannelMojoHost(task_runner())); | 191 host_.reset(new IPC::ChannelMojoHost(task_runner())); |
| 194 return IPC::ChannelMojo::CreateServerFactory(host_->channel_delegate(), | 192 return IPC::ChannelMojo::CreateServerFactory(host_->channel_delegate(), |
| 195 handle); | 193 handle); |
| 196 } | 194 } |
| 197 | 195 |
| 198 virtual bool DidStartClient() override { | 196 bool DidStartClient() override { |
| 199 bool ok = IPCTestBase::DidStartClient(); | 197 bool ok = IPCTestBase::DidStartClient(); |
| 200 DCHECK(ok); | 198 DCHECK(ok); |
| 201 host_->OnClientLaunched(client_process()); | 199 host_->OnClientLaunched(client_process()); |
| 202 return ok; | 200 return ok; |
| 203 } | 201 } |
| 204 | 202 |
| 205 private: | 203 private: |
| 206 scoped_ptr<IPC::ChannelMojoHost> host_; | 204 scoped_ptr<IPC::ChannelMojoHost> host_; |
| 207 }; | 205 }; |
| 208 | 206 |
| 209 class ListenerThatQuits : public IPC::Listener { | 207 class ListenerThatQuits : public IPC::Listener { |
| 210 public: | 208 public: |
| 211 ListenerThatQuits() { | 209 ListenerThatQuits() { |
| 212 } | 210 } |
| 213 | 211 |
| 214 virtual bool OnMessageReceived(const IPC::Message& message) override { | 212 bool OnMessageReceived(const IPC::Message& message) override { return true; } |
| 215 return true; | |
| 216 } | |
| 217 | 213 |
| 218 virtual void OnChannelConnected(int32 peer_pid) override { | 214 void OnChannelConnected(int32 peer_pid) override { |
| 219 base::MessageLoop::current()->Quit(); | 215 base::MessageLoop::current()->Quit(); |
| 220 } | 216 } |
| 221 }; | 217 }; |
| 222 | 218 |
| 223 // A long running process that connects to us. | 219 // A long running process that connects to us. |
| 224 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoErraticTestClient) { | 220 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoErraticTestClient) { |
| 225 ListenerThatQuits listener; | 221 ListenerThatQuits listener; |
| 226 ChannelClient client(&listener, "IPCChannelMojoErraticTestClient"); | 222 ChannelClient client(&listener, "IPCChannelMojoErraticTestClient"); |
| 227 client.Connect(); | 223 client.Connect(); |
| 228 | 224 |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 317 return 0; | 313 return 0; |
| 318 } | 314 } |
| 319 #endif | 315 #endif |
| 320 | 316 |
| 321 #if defined(OS_POSIX) | 317 #if defined(OS_POSIX) |
| 322 class ListenerThatExpectsFile : public IPC::Listener { | 318 class ListenerThatExpectsFile : public IPC::Listener { |
| 323 public: | 319 public: |
| 324 ListenerThatExpectsFile() | 320 ListenerThatExpectsFile() |
| 325 : sender_(NULL) {} | 321 : sender_(NULL) {} |
| 326 | 322 |
| 327 virtual ~ListenerThatExpectsFile() {} | 323 ~ListenerThatExpectsFile() override {} |
| 328 | 324 |
| 329 virtual bool OnMessageReceived(const IPC::Message& message) override { | 325 bool OnMessageReceived(const IPC::Message& message) override { |
| 330 PickleIterator iter(message); | 326 PickleIterator iter(message); |
| 331 | 327 |
| 332 base::ScopedFD fd; | 328 base::ScopedFD fd; |
| 333 EXPECT_TRUE(message.ReadFile(&iter, &fd)); | 329 EXPECT_TRUE(message.ReadFile(&iter, &fd)); |
| 334 base::File file(fd.release()); | 330 base::File file(fd.release()); |
| 335 std::string content(GetSendingFileContent().size(), ' '); | 331 std::string content(GetSendingFileContent().size(), ' '); |
| 336 file.Read(0, &content[0], content.size()); | 332 file.Read(0, &content[0], content.size()); |
| 337 EXPECT_EQ(content, GetSendingFileContent()); | 333 EXPECT_EQ(content, GetSendingFileContent()); |
| 338 base::MessageLoop::current()->Quit(); | 334 base::MessageLoop::current()->Quit(); |
| 339 ListenerThatExpectsOK::SendOK(sender_); | 335 ListenerThatExpectsOK::SendOK(sender_); |
| 340 return true; | 336 return true; |
| 341 } | 337 } |
| 342 | 338 |
| 343 virtual void OnChannelError() override { | 339 void OnChannelError() override { NOTREACHED(); } |
| 344 NOTREACHED(); | |
| 345 } | |
| 346 | 340 |
| 347 static std::string GetSendingFileContent() { | 341 static std::string GetSendingFileContent() { |
| 348 return "Hello"; | 342 return "Hello"; |
| 349 } | 343 } |
| 350 | 344 |
| 351 static base::FilePath GetSendingFilePath() { | 345 static base::FilePath GetSendingFilePath() { |
| 352 base::FilePath path; | 346 base::FilePath path; |
| 353 bool ok = PathService::Get(base::DIR_CACHE, &path); | 347 bool ok = PathService::Get(base::DIR_CACHE, &path); |
| 354 EXPECT_TRUE(ok); | 348 EXPECT_TRUE(ok); |
| 355 return path.Append("ListenerThatExpectsFile.txt"); | 349 return path.Append("ListenerThatExpectsFile.txt"); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 399 client.Connect(); | 393 client.Connect(); |
| 400 listener.set_sender(client.channel()); | 394 listener.set_sender(client.channel()); |
| 401 | 395 |
| 402 base::MessageLoop::current()->Run(); | 396 base::MessageLoop::current()->Run(); |
| 403 | 397 |
| 404 return 0; | 398 return 0; |
| 405 } | 399 } |
| 406 #endif | 400 #endif |
| 407 | 401 |
| 408 } // namespace | 402 } // namespace |
| OLD | NEW |