1 // Copyright 2013 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 <stdint.h>
6 #include <utility>
7 
8 #include "base/memory/ptr_util.h"
9 #include "base/run_loop.h"
10 #include "mojo/public/cpp/bindings/binding.h"
11 #include "mojo/public/cpp/bindings/strong_binding.h"
12 #include "mojo/public/cpp/bindings/tests/bindings_test_base.h"
13 #include "mojo/public/cpp/system/wait.h"
14 #include "mojo/public/cpp/test_support/test_utils.h"
15 #include "mojo/public/interfaces/bindings/tests/sample_factory.mojom.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 
18 namespace mojo {
19 namespace test {
20 namespace {
21 
22 const char kText1[] = "hello";
23 const char kText2[] = "world";
24 
RecordString(std::string * storage,const base::Closure & closure,const std::string & str)25 void RecordString(std::string* storage,
26                   const base::Closure& closure,
27                   const std::string& str) {
28   *storage = str;
29   closure.Run();
30 }
31 
MakeStringRecorder(std::string * storage,const base::Closure & closure)32 base::Callback<void(const std::string&)> MakeStringRecorder(
33     std::string* storage,
34     const base::Closure& closure) {
35   return base::Bind(&RecordString, storage, closure);
36 }
37 
38 class ImportedInterfaceImpl : public imported::ImportedInterface {
39  public:
ImportedInterfaceImpl(InterfaceRequest<imported::ImportedInterface> request,const base::Closure & closure)40   ImportedInterfaceImpl(
41       InterfaceRequest<imported::ImportedInterface> request,
42       const base::Closure& closure)
43       : binding_(this, std::move(request)), closure_(closure) {}
44 
DoSomething()45   void DoSomething() override {
46     do_something_count_++;
47     closure_.Run();
48   }
49 
do_something_count()50   static int do_something_count() { return do_something_count_; }
51 
52  private:
53   static int do_something_count_;
54   Binding<ImportedInterface> binding_;
55   base::Closure closure_;
56 };
57 int ImportedInterfaceImpl::do_something_count_ = 0;
58 
59 class SampleNamedObjectImpl : public sample::NamedObject {
60  public:
SampleNamedObjectImpl()61   SampleNamedObjectImpl() {}
62 
SetName(const std::string & name)63   void SetName(const std::string& name) override { name_ = name; }
64 
GetName(const GetNameCallback & callback)65   void GetName(const GetNameCallback& callback) override {
66     callback.Run(name_);
67   }
68 
69  private:
70   std::string name_;
71 };
72 
73 class SampleFactoryImpl : public sample::Factory {
74  public:
SampleFactoryImpl(InterfaceRequest<sample::Factory> request)75   explicit SampleFactoryImpl(InterfaceRequest<sample::Factory> request)
76       : binding_(this, std::move(request)) {}
77 
DoStuff(sample::RequestPtr request,ScopedMessagePipeHandle pipe,const DoStuffCallback & callback)78   void DoStuff(sample::RequestPtr request,
79                ScopedMessagePipeHandle pipe,
80                const DoStuffCallback& callback) override {
81     std::string text1;
82     if (pipe.is_valid())
83       EXPECT_TRUE(ReadTextMessage(pipe.get(), &text1));
84 
85     std::string text2;
86     if (request->pipe.is_valid()) {
87       EXPECT_TRUE(ReadTextMessage(request->pipe.get(), &text2));
88 
89       // Ensure that simply accessing request->pipe does not close it.
90       EXPECT_TRUE(request->pipe.is_valid());
91     }
92 
93     ScopedMessagePipeHandle pipe0;
94     if (!text2.empty()) {
95       CreateMessagePipe(nullptr, &pipe0, &pipe1_);
96       EXPECT_TRUE(WriteTextMessage(pipe1_.get(), text2));
97     }
98 
99     sample::ResponsePtr response(sample::Response::New(2, std::move(pipe0)));
100     callback.Run(std::move(response), text1);
101 
102     if (request->obj) {
103       imported::ImportedInterfacePtr proxy(std::move(request->obj));
104       proxy->DoSomething();
105     }
106   }
107 
DoStuff2(ScopedDataPipeConsumerHandle pipe,const DoStuff2Callback & callback)108   void DoStuff2(ScopedDataPipeConsumerHandle pipe,
109                 const DoStuff2Callback& callback) override {
110     // Read the data from the pipe, writing the response (as a string) to
111     // DidStuff2().
112     ASSERT_TRUE(pipe.is_valid());
113     uint32_t data_size = 0;
114 
115     MojoHandleSignalsState state;
116     ASSERT_EQ(MOJO_RESULT_OK,
117               mojo::Wait(pipe.get(), MOJO_HANDLE_SIGNAL_READABLE, &state));
118     ASSERT_TRUE(state.satisfied_signals & MOJO_HANDLE_SIGNAL_READABLE);
119     ASSERT_EQ(MOJO_RESULT_OK,
120               pipe->ReadData(nullptr, &data_size, MOJO_READ_DATA_FLAG_QUERY));
121     ASSERT_NE(0, static_cast<int>(data_size));
122     char data[64];
123     ASSERT_LT(static_cast<int>(data_size), 64);
124     ASSERT_EQ(MOJO_RESULT_OK, pipe->ReadData(data, &data_size,
125                                              MOJO_READ_DATA_FLAG_ALL_OR_NONE));
126 
127     callback.Run(data);
128   }
129 
CreateNamedObject(InterfaceRequest<sample::NamedObject> object_request)130   void CreateNamedObject(
131       InterfaceRequest<sample::NamedObject> object_request) override {
132     EXPECT_TRUE(object_request.is_pending());
133     MakeStrongBinding(std::make_unique<SampleNamedObjectImpl>(),
134                       std::move(object_request));
135   }
136 
137   // These aren't called or implemented, but exist here to test that the
138   // methods are generated with the correct argument types for imported
139   // interfaces.
RequestImportedInterface(InterfaceRequest<imported::ImportedInterface> imported,const RequestImportedInterfaceCallback & callback)140   void RequestImportedInterface(
141       InterfaceRequest<imported::ImportedInterface> imported,
142       const RequestImportedInterfaceCallback& callback) override {}
TakeImportedInterface(imported::ImportedInterfacePtr imported,const TakeImportedInterfaceCallback & callback)143   void TakeImportedInterface(
144       imported::ImportedInterfacePtr imported,
145       const TakeImportedInterfaceCallback& callback) override {}
146 
147  private:
148   ScopedMessagePipeHandle pipe1_;
149   Binding<sample::Factory> binding_;
150 };
151 
152 class HandlePassingTest : public BindingsTestBase {
153  public:
HandlePassingTest()154   HandlePassingTest() {}
155 
TearDown()156   void TearDown() override { PumpMessages(); }
157 
PumpMessages()158   void PumpMessages() { base::RunLoop().RunUntilIdle(); }
159 
160 };
161 
DoStuff(bool * got_response,std::string * got_text_reply,const base::Closure & closure,sample::ResponsePtr response,const std::string & text_reply)162 void DoStuff(bool* got_response,
163              std::string* got_text_reply,
164              const base::Closure& closure,
165              sample::ResponsePtr response,
166              const std::string& text_reply) {
167   *got_text_reply = text_reply;
168 
169   if (response->pipe.is_valid()) {
170     std::string text2;
171     EXPECT_TRUE(ReadTextMessage(response->pipe.get(), &text2));
172 
173     // Ensure that simply accessing response.pipe does not close it.
174     EXPECT_TRUE(response->pipe.is_valid());
175 
176     EXPECT_EQ(std::string(kText2), text2);
177 
178     // Do some more tests of handle passing:
179     ScopedMessagePipeHandle p = std::move(response->pipe);
180     EXPECT_TRUE(p.is_valid());
181     EXPECT_FALSE(response->pipe.is_valid());
182   }
183 
184   *got_response = true;
185   closure.Run();
186 }
187 
DoStuff2(bool * got_response,std::string * got_text_reply,const base::Closure & closure,const std::string & text_reply)188 void DoStuff2(bool* got_response,
189               std::string* got_text_reply,
190               const base::Closure& closure,
191               const std::string& text_reply) {
192   *got_response = true;
193   *got_text_reply = text_reply;
194   closure.Run();
195 }
196 
TEST_P(HandlePassingTest,Basic)197 TEST_P(HandlePassingTest, Basic) {
198   sample::FactoryPtr factory;
199   SampleFactoryImpl factory_impl(MakeRequest(&factory));
200 
201   MessagePipe pipe0;
202   EXPECT_TRUE(WriteTextMessage(pipe0.handle1.get(), kText1));
203 
204   MessagePipe pipe1;
205   EXPECT_TRUE(WriteTextMessage(pipe1.handle1.get(), kText2));
206 
207   imported::ImportedInterfacePtrInfo imported;
208   base::RunLoop run_loop;
209   ImportedInterfaceImpl imported_impl(MakeRequest(&imported),
210                                       run_loop.QuitClosure());
211 
212   sample::RequestPtr request(sample::Request::New(
213       1, std::move(pipe1.handle0), base::nullopt, std::move(imported)));
214   bool got_response = false;
215   std::string got_text_reply;
216   base::RunLoop run_loop2;
217   factory->DoStuff(std::move(request), std::move(pipe0.handle0),
218                    base::Bind(&DoStuff, &got_response, &got_text_reply,
219                               run_loop2.QuitClosure()));
220 
221   EXPECT_FALSE(got_response);
222   int count_before = ImportedInterfaceImpl::do_something_count();
223 
224   run_loop.Run();
225   run_loop2.Run();
226 
227   EXPECT_TRUE(got_response);
228   EXPECT_EQ(kText1, got_text_reply);
229   EXPECT_EQ(1, ImportedInterfaceImpl::do_something_count() - count_before);
230 }
231 
TEST_P(HandlePassingTest,PassInvalid)232 TEST_P(HandlePassingTest, PassInvalid) {
233   sample::FactoryPtr factory;
234   SampleFactoryImpl factory_impl(MakeRequest(&factory));
235 
236   sample::RequestPtr request(sample::Request::New(1, ScopedMessagePipeHandle(),
237                                                   base::nullopt, nullptr));
238 
239   bool got_response = false;
240   std::string got_text_reply;
241   base::RunLoop run_loop;
242   factory->DoStuff(std::move(request), ScopedMessagePipeHandle(),
243                    base::Bind(&DoStuff, &got_response, &got_text_reply,
244                               run_loop.QuitClosure()));
245 
246   EXPECT_FALSE(got_response);
247 
248   run_loop.Run();
249 
250   EXPECT_TRUE(got_response);
251 }
252 
253 // Verifies DataPipeConsumer can be passed and read from.
TEST_P(HandlePassingTest,DataPipe)254 TEST_P(HandlePassingTest, DataPipe) {
255   sample::FactoryPtr factory;
256   SampleFactoryImpl factory_impl(MakeRequest(&factory));
257 
258   // Writes a string to a data pipe and passes the data pipe (consumer) to the
259   // factory.
260   ScopedDataPipeProducerHandle producer_handle;
261   ScopedDataPipeConsumerHandle consumer_handle;
262   MojoCreateDataPipeOptions options = {sizeof(MojoCreateDataPipeOptions),
263                                        MOJO_CREATE_DATA_PIPE_FLAG_NONE, 1,
264                                        1024};
265   ASSERT_EQ(MOJO_RESULT_OK,
266             CreateDataPipe(&options, &producer_handle, &consumer_handle));
267   std::string expected_text_reply = "got it";
268   // +1 for \0.
269   uint32_t data_size = static_cast<uint32_t>(expected_text_reply.size() + 1);
270   ASSERT_EQ(MOJO_RESULT_OK,
271             producer_handle->WriteData(expected_text_reply.c_str(), &data_size,
272                                        MOJO_WRITE_DATA_FLAG_ALL_OR_NONE));
273 
274   bool got_response = false;
275   std::string got_text_reply;
276   base::RunLoop run_loop;
277   factory->DoStuff2(std::move(consumer_handle),
278                     base::Bind(&DoStuff2, &got_response, &got_text_reply,
279                                run_loop.QuitClosure()));
280 
281   EXPECT_FALSE(got_response);
282 
283   run_loop.Run();
284 
285   EXPECT_TRUE(got_response);
286   EXPECT_EQ(expected_text_reply, got_text_reply);
287 }
288 
TEST_P(HandlePassingTest,CreateNamedObject)289 TEST_P(HandlePassingTest, CreateNamedObject) {
290   sample::FactoryPtr factory;
291   SampleFactoryImpl factory_impl(MakeRequest(&factory));
292 
293   sample::NamedObjectPtr object1;
294   EXPECT_FALSE(object1);
295 
296   auto object1_request = mojo::MakeRequest(&object1);
297   EXPECT_TRUE(object1_request.is_pending());
298   factory->CreateNamedObject(std::move(object1_request));
299   EXPECT_FALSE(object1_request.is_pending());  // We've passed the request.
300 
301   ASSERT_TRUE(object1);
302   object1->SetName("object1");
303 
304   sample::NamedObjectPtr object2;
305   factory->CreateNamedObject(MakeRequest(&object2));
306   object2->SetName("object2");
307 
308   base::RunLoop run_loop, run_loop2;
309   std::string name1;
310   object1->GetName(MakeStringRecorder(&name1, run_loop.QuitClosure()));
311 
312   std::string name2;
313   object2->GetName(MakeStringRecorder(&name2, run_loop2.QuitClosure()));
314 
315   run_loop.Run();
316   run_loop2.Run();
317 
318   EXPECT_EQ(std::string("object1"), name1);
319   EXPECT_EQ(std::string("object2"), name2);
320 }
321 
322 INSTANTIATE_MOJO_BINDINGS_TEST_CASE_P(HandlePassingTest);
323 
324 }  // namespace
325 }  // namespace test
326 }  // namespace mojo
327