Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(261)

Side by Side Diff: ipc/mojo/ipc_channel_mojo_unittest.cc

Issue 382333002: Introduce ChannelMojo (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixing Mac build failure Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "ipc/mojo/ipc_channel_mojo.h"
6
7 #include "base/base_paths.h"
8 #include "base/files/file.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/path_service.h"
11 #include "base/pickle.h"
12 #include "base/threading/thread.h"
13 #include "ipc/ipc_message.h"
14 #include "ipc/ipc_test_base.h"
15 #include "ipc/ipc_test_channel_listener.h"
16 #include "mojo/embedder/embedder.h"
17
18 #if defined(OS_POSIX)
19 #include "base/file_descriptor_posix.h"
20 #endif
21
22 namespace {
23
24 class ListenerThatExpectsOK : public IPC::Listener {
25 public:
26 ListenerThatExpectsOK() {}
27
28 virtual ~ListenerThatExpectsOK() {}
29
30 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE {
31 PickleIterator iter(message);
32 std::string should_be_ok;
33 EXPECT_TRUE(iter.ReadString(&should_be_ok));
34 EXPECT_EQ(should_be_ok, "OK");
35 base::MessageLoop::current()->Quit();
36 return true;
37 }
38
39 virtual void OnChannelError() OVERRIDE {
40 NOTREACHED();
41 }
42
43 static void SendOK(IPC::Sender* sender) {
44 IPC::Message* message = new IPC::Message(
45 0, 2, IPC::Message::PRIORITY_NORMAL);
46 message->WriteString(std::string("OK"));
47 ASSERT_TRUE(sender->Send(message));
48 }
49 };
50
51 class ListenerThatShouldBeNeverCalled : public IPC::Listener {
52 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE {
53 NOTREACHED();
54 return true;
55 }
56
57 virtual void OnChannelError() OVERRIDE {
58 NOTREACHED();
59 }
60
61 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE {
62 NOTREACHED();
63 }
64
65 virtual void OnBadMessageReceived(const IPC::Message& message) OVERRIDE {
66 NOTREACHED();
67 }
68 };
69
70 class ChannelClient {
71 public:
72 explicit ChannelClient(IPC::Listener* listener, const char* name) {
73 scoped_ptr<IPC::Channel> bootstrap(IPC::Channel::CreateClient(
74 IPCTestBase::GetChannelName(name),
75 &never_called_));
76 channel_ = IPC::ChannelMojo::Create(
77 bootstrap.Pass(), IPC::Channel::MODE_CLIENT, listener,
78 main_message_loop_.message_loop_proxy());
79 }
80
81 void Connect() {
82 CHECK(channel_->Connect());
83 }
84
85 IPC::ChannelMojo* channel() const { return channel_.get(); }
86
87 private:
88 scoped_ptr<IPC::ChannelMojo> channel_;
89 ListenerThatShouldBeNeverCalled never_called_;
90 base::MessageLoopForIO main_message_loop_;
91 };
92
93 class IPCChannelMojoTest : public IPCTestBase {
94 public:
95 void CreateMojoChannel(IPC::Listener* listener);
96
97 protected:
98 virtual void SetUp() OVERRIDE {
99 IPCTestBase::SetUp();
100 mojo::embedder::InitIfNeeded();
101 }
102
103 ListenerThatShouldBeNeverCalled never_called_;
104 };
105
106
107 void IPCChannelMojoTest::CreateMojoChannel(IPC::Listener* listener) {
108 CreateChannel(&never_called_);
109 scoped_ptr<IPC::Channel> mojo_channel = IPC::ChannelMojo::Create(
110 ReleaseChannel(), IPC::Channel::MODE_SERVER, listener,
111 io_thread_task_runner()).PassAs<IPC::Channel>();
112 SetChannel(mojo_channel.PassAs<IPC::Channel>());
113 }
114
115 class TestChannelListenerWithExtraExpectations
116 : public IPC::TestChannelListener {
117 public:
118 TestChannelListenerWithExtraExpectations()
119 : is_connected_called_(false) {
120 }
121
122 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE {
123 IPC::TestChannelListener::OnChannelConnected(peer_pid);
124 EXPECT_TRUE(base::kNullProcessId != peer_pid);
125 is_connected_called_ = true;
126 }
127
128 bool is_connected_called() const { return is_connected_called_; }
129
130 private:
131 bool is_connected_called_;
132 };
133
134 TEST_F(IPCChannelMojoTest, ConnectedFromClient) {
135 Init("IPCChannelMojoTestClient");
136
137 // Set up IPC channel and start client.
138 TestChannelListenerWithExtraExpectations listener;
139 CreateMojoChannel(&listener);
140 listener.Init(sender());
141 ASSERT_TRUE(ConnectChannel());
142 ASSERT_TRUE(StartClient());
143
144 IPC::TestChannelListener::SendOneMessage(
145 sender(), "hello from parent");
146
147 base::MessageLoop::current()->Run();
148 EXPECT_TRUE(base::kNullProcessId != this->channel()->GetPeerPID());
149
150 this->channel()->Close();
151
152 EXPECT_TRUE(WaitForClientShutdown());
153 EXPECT_TRUE(listener.is_connected_called());
154 EXPECT_TRUE(listener.HasSentAll());
155
156 DestroyChannel();
157 }
158
159 // A long running process that connects to us
160 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoTestClient) {
161 mojo::embedder::InitIfNeeded();
162
163 TestChannelListenerWithExtraExpectations listener;
164 ChannelClient client(&listener, "IPCChannelMojoTestClient");
165 client.Connect();
166 listener.Init(client.channel());
167
168 IPC::TestChannelListener::SendOneMessage(
169 client.channel(), "hello from child");
170 base::MessageLoop::current()->Run();
171 EXPECT_TRUE(listener.is_connected_called());
172 EXPECT_TRUE(listener.HasSentAll());
173
174 return 0;
175 }
176
177 #if defined(OS_POSIX)
178 class ListenerThatExpectsFile : public IPC::Listener {
179 public:
180 ListenerThatExpectsFile()
181 : sender_(NULL) {}
182
183 virtual ~ListenerThatExpectsFile() {}
184
185 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE {
186 PickleIterator iter(message);
187 base::FileDescriptor desc;
188 EXPECT_TRUE(message.ReadFileDescriptor(&iter, &desc));
189 std::string content(GetSendingFileContent().size(), ' ');
190 base::File file(desc.fd);
191 file.Read(0, &content[0], content.size());
192 EXPECT_EQ(content, GetSendingFileContent());
193 base::MessageLoop::current()->Quit();
194 ListenerThatExpectsOK::SendOK(sender_);
195 return true;
196 }
197
198 virtual void OnChannelError() OVERRIDE {
199 NOTREACHED();
200 }
201
202 static std::string GetSendingFileContent() {
203 return "Hello";
204 }
205
206 static base::FilePath GetSendingFilePath() {
207 base::FilePath path;
208 bool ok = PathService::Get(base::DIR_CACHE, &path);
209 EXPECT_TRUE(ok);
210 return path.Append("ListenerThatExpectsFile.txt");
211 }
212
213 static void WriteAndSendFile(IPC::Sender* sender, base::File& file) {
214 std::string content = GetSendingFileContent();
215 file.WriteAtCurrentPos(content.data(), content.size());
216 file.Flush();
217 IPC::Message* message = new IPC::Message(
218 0, 2, IPC::Message::PRIORITY_NORMAL);
219 message->WriteFileDescriptor(
220 base::FileDescriptor(file.TakePlatformFile(), false));
221 ASSERT_TRUE(sender->Send(message));
222 }
223
224 void set_sender(IPC::Sender* sender) { sender_ = sender; }
225
226 private:
227 IPC::Sender* sender_;
228 };
229
230
231 TEST_F(IPCChannelMojoTest, SendPlatformHandle) {
232 Init("IPCChannelMojoTestSendPlatformHandleClient");
233
234 ListenerThatExpectsOK listener;
235 CreateMojoChannel(&listener);
236 ASSERT_TRUE(ConnectChannel());
237 ASSERT_TRUE(StartClient());
238
239 base::File file(ListenerThatExpectsFile::GetSendingFilePath(),
240 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE |
241 base::File::FLAG_READ);
242 ListenerThatExpectsFile::WriteAndSendFile(channel(), file);
243 base::MessageLoop::current()->Run();
244
245 this->channel()->Close();
246
247 EXPECT_TRUE(WaitForClientShutdown());
248 DestroyChannel();
249 }
250
251 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(IPCChannelMojoTestSendPlatformHandleClient) {
252 mojo::embedder::InitIfNeeded();
253 ListenerThatExpectsFile listener;
254 ChannelClient client(
255 &listener, "IPCChannelMojoTestSendPlatformHandleClient");
256 client.Connect();
257 listener.set_sender(client.channel());
258
259 base::MessageLoop::current()->Run();
260
261 return 0;
262 }
263 #endif
264
265 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698