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 ~ListenerThatExpectsOK() override {} | 30 virtual ~ListenerThatExpectsOK() {} |
31 | 31 |
32 bool OnMessageReceived(const IPC::Message& message) override { | 32 virtual 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 void OnChannelError() override { | 42 virtual 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 scoped_ptr<IPC::ChannelFactory> CreateChannelFactory( | 82 virtual 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 bool DidStartClient() override { | 90 virtual 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 void OnChannelConnected(int32 peer_pid) override { | 109 virtual 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 void OnChannelConnected(int32 peer_pid) override { | 168 virtual void OnChannelConnected(int32 peer_pid) override { |
169 base::MessageLoop::current()->Quit(); | 169 base::MessageLoop::current()->Quit(); |
170 } | 170 } |
171 | 171 |
172 bool OnMessageReceived(const IPC::Message& message) override { return true; } | 172 virtual bool OnMessageReceived(const IPC::Message& message) override { |
| 173 return true; |
| 174 } |
173 | 175 |
174 void OnChannelError() override { | 176 virtual void OnChannelError() override { |
175 has_error_ = true; | 177 has_error_ = true; |
176 base::MessageLoop::current()->Quit(); | 178 base::MessageLoop::current()->Quit(); |
177 } | 179 } |
178 | 180 |
179 bool has_error() const { return has_error_; } | 181 bool has_error() const { return has_error_; } |
180 | 182 |
181 private: | 183 private: |
182 bool has_error_; | 184 bool has_error_; |
183 }; | 185 }; |
184 | 186 |
185 | 187 |
186 class IPCChannelMojoErrorTest : public IPCTestBase { | 188 class IPCChannelMojoErrorTest : public IPCTestBase { |
187 protected: | 189 protected: |
188 scoped_ptr<IPC::ChannelFactory> CreateChannelFactory( | 190 virtual scoped_ptr<IPC::ChannelFactory> CreateChannelFactory( |
189 const IPC::ChannelHandle& handle, | 191 const IPC::ChannelHandle& handle, |
190 base::TaskRunner* runner) override { | 192 base::TaskRunner* runner) override { |
191 host_.reset(new IPC::ChannelMojoHost(task_runner())); | 193 host_.reset(new IPC::ChannelMojoHost(task_runner())); |
192 return IPC::ChannelMojo::CreateServerFactory(host_->channel_delegate(), | 194 return IPC::ChannelMojo::CreateServerFactory(host_->channel_delegate(), |
193 handle); | 195 handle); |
194 } | 196 } |
195 | 197 |
196 bool DidStartClient() override { | 198 virtual bool DidStartClient() override { |
197 bool ok = IPCTestBase::DidStartClient(); | 199 bool ok = IPCTestBase::DidStartClient(); |
198 DCHECK(ok); | 200 DCHECK(ok); |
199 host_->OnClientLaunched(client_process()); | 201 host_->OnClientLaunched(client_process()); |
200 return ok; | 202 return ok; |
201 } | 203 } |
202 | 204 |
203 private: | 205 private: |
204 scoped_ptr<IPC::ChannelMojoHost> host_; | 206 scoped_ptr<IPC::ChannelMojoHost> host_; |
205 }; | 207 }; |
206 | 208 |
207 class ListenerThatQuits : public IPC::Listener { | 209 class ListenerThatQuits : public IPC::Listener { |
208 public: | 210 public: |
209 ListenerThatQuits() { | 211 ListenerThatQuits() { |
210 } | 212 } |
211 | 213 |
212 bool OnMessageReceived(const IPC::Message& message) override { return true; } | 214 virtual bool OnMessageReceived(const IPC::Message& message) override { |
| 215 return true; |
| 216 } |
213 | 217 |
214 void OnChannelConnected(int32 peer_pid) override { | 218 virtual void OnChannelConnected(int32 peer_pid) override { |
215 base::MessageLoop::current()->Quit(); | 219 base::MessageLoop::current()->Quit(); |
216 } | 220 } |
217 }; | 221 }; |
218 | 222 |
219 // A long running process that connects to us. | 223 // A long running process that connects to us. |
220 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoErraticTestClient) { | 224 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoErraticTestClient) { |
221 ListenerThatQuits listener; | 225 ListenerThatQuits listener; |
222 ChannelClient client(&listener, "IPCChannelMojoErraticTestClient"); | 226 ChannelClient client(&listener, "IPCChannelMojoErraticTestClient"); |
223 client.Connect(); | 227 client.Connect(); |
224 | 228 |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
313 return 0; | 317 return 0; |
314 } | 318 } |
315 #endif | 319 #endif |
316 | 320 |
317 #if defined(OS_POSIX) | 321 #if defined(OS_POSIX) |
318 class ListenerThatExpectsFile : public IPC::Listener { | 322 class ListenerThatExpectsFile : public IPC::Listener { |
319 public: | 323 public: |
320 ListenerThatExpectsFile() | 324 ListenerThatExpectsFile() |
321 : sender_(NULL) {} | 325 : sender_(NULL) {} |
322 | 326 |
323 ~ListenerThatExpectsFile() override {} | 327 virtual ~ListenerThatExpectsFile() {} |
324 | 328 |
325 bool OnMessageReceived(const IPC::Message& message) override { | 329 virtual bool OnMessageReceived(const IPC::Message& message) override { |
326 PickleIterator iter(message); | 330 PickleIterator iter(message); |
327 | 331 |
328 base::ScopedFD fd; | 332 base::ScopedFD fd; |
329 EXPECT_TRUE(message.ReadFile(&iter, &fd)); | 333 EXPECT_TRUE(message.ReadFile(&iter, &fd)); |
330 base::File file(fd.release()); | 334 base::File file(fd.release()); |
331 std::string content(GetSendingFileContent().size(), ' '); | 335 std::string content(GetSendingFileContent().size(), ' '); |
332 file.Read(0, &content[0], content.size()); | 336 file.Read(0, &content[0], content.size()); |
333 EXPECT_EQ(content, GetSendingFileContent()); | 337 EXPECT_EQ(content, GetSendingFileContent()); |
334 base::MessageLoop::current()->Quit(); | 338 base::MessageLoop::current()->Quit(); |
335 ListenerThatExpectsOK::SendOK(sender_); | 339 ListenerThatExpectsOK::SendOK(sender_); |
336 return true; | 340 return true; |
337 } | 341 } |
338 | 342 |
339 void OnChannelError() override { NOTREACHED(); } | 343 virtual void OnChannelError() override { |
| 344 NOTREACHED(); |
| 345 } |
340 | 346 |
341 static std::string GetSendingFileContent() { | 347 static std::string GetSendingFileContent() { |
342 return "Hello"; | 348 return "Hello"; |
343 } | 349 } |
344 | 350 |
345 static base::FilePath GetSendingFilePath() { | 351 static base::FilePath GetSendingFilePath() { |
346 base::FilePath path; | 352 base::FilePath path; |
347 bool ok = PathService::Get(base::DIR_CACHE, &path); | 353 bool ok = PathService::Get(base::DIR_CACHE, &path); |
348 EXPECT_TRUE(ok); | 354 EXPECT_TRUE(ok); |
349 return path.Append("ListenerThatExpectsFile.txt"); | 355 return path.Append("ListenerThatExpectsFile.txt"); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
393 client.Connect(); | 399 client.Connect(); |
394 listener.set_sender(client.channel()); | 400 listener.set_sender(client.channel()); |
395 | 401 |
396 base::MessageLoop::current()->Run(); | 402 base::MessageLoop::current()->Run(); |
397 | 403 |
398 return 0; | 404 return 0; |
399 } | 405 } |
400 #endif | 406 #endif |
401 | 407 |
402 } // namespace | 408 } // namespace |
OLD | NEW |