OLD | NEW |
| (Empty) |
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 "base/at_exit.h" | |
6 #include "base/files/file_path.h" | |
7 #include "base/files/file_util.h" | |
8 #include "base/macros.h" | |
9 #include "base/message_loop/message_loop.h" | |
10 #include "base/run_loop.h" | |
11 #include "base/strings/utf_string_conversions.h" | |
12 #include "gin/array_buffer.h" | |
13 #include "gin/public/isolate_holder.h" | |
14 #include "mojo/apps/js/mojo_runner_delegate.h" | |
15 #include "mojo/apps/js/test/js_to_cpp.mojom.h" | |
16 #include "mojo/common/common_type_converters.h" | |
17 #include "mojo/edk/test/test_utils.h" | |
18 #include "mojo/public/cpp/system/core.h" | |
19 #include "mojo/public/cpp/system/macros.h" | |
20 #include "testing/gtest/include/gtest/gtest.h" | |
21 | |
22 namespace mojo { | |
23 namespace js { | |
24 | |
25 // Global value updated by some checks to prevent compilers from optimizing | |
26 // reads out of existence. | |
27 uint32 g_waste_accumulator = 0; | |
28 | |
29 namespace { | |
30 | |
31 // Negative numbers with different values in each byte, the last of | |
32 // which can survive promotion to double and back. | |
33 const int8 kExpectedInt8Value = -65; | |
34 const int16 kExpectedInt16Value = -16961; | |
35 const int32 kExpectedInt32Value = -1145258561; | |
36 const int64 kExpectedInt64Value = -77263311946305LL; | |
37 | |
38 // Positive numbers with different values in each byte, the last of | |
39 // which can survive promotion to double and back. | |
40 const uint8 kExpectedUInt8Value = 65; | |
41 const uint16 kExpectedUInt16Value = 16961; | |
42 const uint32 kExpectedUInt32Value = 1145258561; | |
43 const uint64 kExpectedUInt64Value = 77263311946305LL; | |
44 | |
45 // Double/float values, including special case constants. | |
46 const double kExpectedDoubleVal = 3.14159265358979323846; | |
47 const double kExpectedDoubleInf = std::numeric_limits<double>::infinity(); | |
48 const double kExpectedDoubleNan = std::numeric_limits<double>::quiet_NaN(); | |
49 const float kExpectedFloatVal = static_cast<float>(kExpectedDoubleVal); | |
50 const float kExpectedFloatInf = std::numeric_limits<float>::infinity(); | |
51 const float kExpectedFloatNan = std::numeric_limits<float>::quiet_NaN(); | |
52 | |
53 // NaN has the property that it is not equal to itself. | |
54 #define EXPECT_NAN(x) EXPECT_NE(x, x) | |
55 | |
56 bool IsRunningOnIsolatedBot() { | |
57 // TODO(yzshen): Remove this check once isolated tests are supported on the | |
58 // Chromium waterfall. (http://crbug.com/351214) | |
59 const base::FilePath test_file_path( | |
60 test::GetFilePathForJSResource( | |
61 "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom")); | |
62 if (!base::PathExists(test_file_path)) { | |
63 LOG(WARNING) << "Mojom binding files don't exist. Skipping the test."; | |
64 return true; | |
65 } | |
66 return false; | |
67 } | |
68 | |
69 void CheckDataPipe(MojoHandle data_pipe_handle) { | |
70 unsigned char buffer[100]; | |
71 uint32_t buffer_size = static_cast<uint32_t>(sizeof(buffer)); | |
72 MojoResult result = MojoReadData( | |
73 data_pipe_handle, buffer, &buffer_size, MOJO_READ_DATA_FLAG_NONE); | |
74 EXPECT_EQ(MOJO_RESULT_OK, result); | |
75 EXPECT_EQ(64u, buffer_size); | |
76 for (int i = 0; i < 64; ++i) { | |
77 EXPECT_EQ(i, buffer[i]); | |
78 } | |
79 } | |
80 | |
81 void CheckMessagePipe(MojoHandle message_pipe_handle) { | |
82 unsigned char buffer[100]; | |
83 uint32_t buffer_size = static_cast<uint32_t>(sizeof(buffer)); | |
84 MojoResult result = MojoReadMessage( | |
85 message_pipe_handle, buffer, &buffer_size, 0, 0, 0); | |
86 EXPECT_EQ(MOJO_RESULT_OK, result); | |
87 EXPECT_EQ(64u, buffer_size); | |
88 for (int i = 0; i < 64; ++i) { | |
89 EXPECT_EQ(255 - i, buffer[i]); | |
90 } | |
91 } | |
92 | |
93 js_to_cpp::EchoArgsPtr BuildSampleEchoArgs() { | |
94 js_to_cpp::EchoArgsPtr args(js_to_cpp::EchoArgs::New()); | |
95 args->si64 = kExpectedInt64Value; | |
96 args->si32 = kExpectedInt32Value; | |
97 args->si16 = kExpectedInt16Value; | |
98 args->si8 = kExpectedInt8Value; | |
99 args->ui64 = kExpectedUInt64Value; | |
100 args->ui32 = kExpectedUInt32Value; | |
101 args->ui16 = kExpectedUInt16Value; | |
102 args->ui8 = kExpectedUInt8Value; | |
103 args->float_val = kExpectedFloatVal; | |
104 args->float_inf = kExpectedFloatInf; | |
105 args->float_nan = kExpectedFloatNan; | |
106 args->double_val = kExpectedDoubleVal; | |
107 args->double_inf = kExpectedDoubleInf; | |
108 args->double_nan = kExpectedDoubleNan; | |
109 args->name = "coming"; | |
110 Array<String> string_array(3); | |
111 string_array[0] = "one"; | |
112 string_array[1] = "two"; | |
113 string_array[2] = "three"; | |
114 args->string_array = string_array.Pass(); | |
115 return args.Pass(); | |
116 } | |
117 | |
118 void CheckSampleEchoArgs(const js_to_cpp::EchoArgs& arg) { | |
119 EXPECT_EQ(kExpectedInt64Value, arg.si64); | |
120 EXPECT_EQ(kExpectedInt32Value, arg.si32); | |
121 EXPECT_EQ(kExpectedInt16Value, arg.si16); | |
122 EXPECT_EQ(kExpectedInt8Value, arg.si8); | |
123 EXPECT_EQ(kExpectedUInt64Value, arg.ui64); | |
124 EXPECT_EQ(kExpectedUInt32Value, arg.ui32); | |
125 EXPECT_EQ(kExpectedUInt16Value, arg.ui16); | |
126 EXPECT_EQ(kExpectedUInt8Value, arg.ui8); | |
127 EXPECT_EQ(kExpectedFloatVal, arg.float_val); | |
128 EXPECT_EQ(kExpectedFloatInf, arg.float_inf); | |
129 EXPECT_NAN(arg.float_nan); | |
130 EXPECT_EQ(kExpectedDoubleVal, arg.double_val); | |
131 EXPECT_EQ(kExpectedDoubleInf, arg.double_inf); | |
132 EXPECT_NAN(arg.double_nan); | |
133 EXPECT_EQ(std::string("coming"), arg.name.get()); | |
134 EXPECT_EQ(std::string("one"), arg.string_array[0].get()); | |
135 EXPECT_EQ(std::string("two"), arg.string_array[1].get()); | |
136 EXPECT_EQ(std::string("three"), arg.string_array[2].get()); | |
137 CheckDataPipe(arg.data_handle.get().value()); | |
138 CheckMessagePipe(arg.message_handle.get().value()); | |
139 } | |
140 | |
141 void CheckSampleEchoArgsList(const js_to_cpp::EchoArgsListPtr& list) { | |
142 if (list.is_null()) | |
143 return; | |
144 CheckSampleEchoArgs(*list->item); | |
145 CheckSampleEchoArgsList(list->next); | |
146 } | |
147 | |
148 // More forgiving checks are needed in the face of potentially corrupt | |
149 // messages. The values don't matter so long as all accesses are within | |
150 // bounds. | |
151 void CheckCorruptedString(const String& arg) { | |
152 if (arg.is_null()) | |
153 return; | |
154 for (size_t i = 0; i < arg.size(); ++i) | |
155 g_waste_accumulator += arg[i]; | |
156 } | |
157 | |
158 void CheckCorruptedStringArray(const Array<String>& string_array) { | |
159 if (string_array.is_null()) | |
160 return; | |
161 for (size_t i = 0; i < string_array.size(); ++i) | |
162 CheckCorruptedString(string_array[i]); | |
163 } | |
164 | |
165 void CheckCorruptedDataPipe(MojoHandle data_pipe_handle) { | |
166 unsigned char buffer[100]; | |
167 uint32_t buffer_size = static_cast<uint32_t>(sizeof(buffer)); | |
168 MojoResult result = MojoReadData( | |
169 data_pipe_handle, buffer, &buffer_size, MOJO_READ_DATA_FLAG_NONE); | |
170 if (result != MOJO_RESULT_OK) | |
171 return; | |
172 for (uint32_t i = 0; i < buffer_size; ++i) | |
173 g_waste_accumulator += buffer[i]; | |
174 } | |
175 | |
176 void CheckCorruptedMessagePipe(MojoHandle message_pipe_handle) { | |
177 unsigned char buffer[100]; | |
178 uint32_t buffer_size = static_cast<uint32_t>(sizeof(buffer)); | |
179 MojoResult result = MojoReadMessage( | |
180 message_pipe_handle, buffer, &buffer_size, 0, 0, 0); | |
181 if (result != MOJO_RESULT_OK) | |
182 return; | |
183 for (uint32_t i = 0; i < buffer_size; ++i) | |
184 g_waste_accumulator += buffer[i]; | |
185 } | |
186 | |
187 void CheckCorruptedEchoArgs(const js_to_cpp::EchoArgsPtr& arg) { | |
188 if (arg.is_null()) | |
189 return; | |
190 CheckCorruptedString(arg->name); | |
191 CheckCorruptedStringArray(arg->string_array); | |
192 if (arg->data_handle.is_valid()) | |
193 CheckCorruptedDataPipe(arg->data_handle.get().value()); | |
194 if (arg->message_handle.is_valid()) | |
195 CheckCorruptedMessagePipe(arg->message_handle.get().value()); | |
196 } | |
197 | |
198 void CheckCorruptedEchoArgsList(const js_to_cpp::EchoArgsListPtr& list) { | |
199 if (list.is_null()) | |
200 return; | |
201 CheckCorruptedEchoArgs(list->item); | |
202 CheckCorruptedEchoArgsList(list->next); | |
203 } | |
204 | |
205 // Base Provider implementation class. It's expected that tests subclass and | |
206 // override the appropriate Provider functions. When test is done quit the | |
207 // run_loop(). | |
208 class CppSideConnection : public js_to_cpp::CppSide { | |
209 public: | |
210 CppSideConnection() : | |
211 run_loop_(NULL), | |
212 js_side_(NULL), | |
213 mishandled_messages_(0) { | |
214 } | |
215 ~CppSideConnection() override {} | |
216 | |
217 void set_run_loop(base::RunLoop* run_loop) { run_loop_ = run_loop; } | |
218 base::RunLoop* run_loop() { return run_loop_; } | |
219 | |
220 void set_js_side(js_to_cpp::JsSide* js_side) { js_side_ = js_side; } | |
221 js_to_cpp::JsSide* js_side() { return js_side_; } | |
222 | |
223 // js_to_cpp::CppSide: | |
224 void StartTest() override { NOTREACHED(); } | |
225 | |
226 void TestFinished() override { NOTREACHED(); } | |
227 | |
228 void PingResponse() override { mishandled_messages_ += 1; } | |
229 | |
230 void EchoResponse(js_to_cpp::EchoArgsListPtr list) override { | |
231 mishandled_messages_ += 1; | |
232 } | |
233 | |
234 void BitFlipResponse(js_to_cpp::EchoArgsListPtr list) override { | |
235 mishandled_messages_ += 1; | |
236 } | |
237 | |
238 void BackPointerResponse(js_to_cpp::EchoArgsListPtr list) override { | |
239 mishandled_messages_ += 1; | |
240 } | |
241 | |
242 protected: | |
243 base::RunLoop* run_loop_; | |
244 js_to_cpp::JsSide* js_side_; | |
245 int mishandled_messages_; | |
246 | |
247 private: | |
248 DISALLOW_COPY_AND_ASSIGN(CppSideConnection); | |
249 }; | |
250 | |
251 // Trivial test to verify a message sent from JS is received. | |
252 class PingCppSideConnection : public CppSideConnection { | |
253 public: | |
254 PingCppSideConnection() : got_message_(false) {} | |
255 ~PingCppSideConnection() override {} | |
256 | |
257 // js_to_cpp::CppSide: | |
258 void StartTest() override { js_side_->Ping(); } | |
259 | |
260 void PingResponse() override { | |
261 got_message_ = true; | |
262 run_loop()->Quit(); | |
263 } | |
264 | |
265 bool DidSucceed() { | |
266 return got_message_ && !mishandled_messages_; | |
267 } | |
268 | |
269 private: | |
270 bool got_message_; | |
271 DISALLOW_COPY_AND_ASSIGN(PingCppSideConnection); | |
272 }; | |
273 | |
274 // Test that parameters are passed with correct values. | |
275 class EchoCppSideConnection : public CppSideConnection { | |
276 public: | |
277 EchoCppSideConnection() : | |
278 message_count_(0), | |
279 termination_seen_(false) { | |
280 } | |
281 ~EchoCppSideConnection() override {} | |
282 | |
283 // js_to_cpp::CppSide: | |
284 void StartTest() override { | |
285 js_side_->Echo(kExpectedMessageCount, BuildSampleEchoArgs()); | |
286 } | |
287 | |
288 void EchoResponse(js_to_cpp::EchoArgsListPtr list) override { | |
289 const js_to_cpp::EchoArgsPtr& special_arg = list->item; | |
290 message_count_ += 1; | |
291 EXPECT_EQ(-1, special_arg->si64); | |
292 EXPECT_EQ(-1, special_arg->si32); | |
293 EXPECT_EQ(-1, special_arg->si16); | |
294 EXPECT_EQ(-1, special_arg->si8); | |
295 EXPECT_EQ(std::string("going"), special_arg->name.To<std::string>()); | |
296 CheckSampleEchoArgsList(list->next); | |
297 } | |
298 | |
299 void TestFinished() override { | |
300 termination_seen_ = true; | |
301 run_loop()->Quit(); | |
302 } | |
303 | |
304 bool DidSucceed() { | |
305 return termination_seen_ && | |
306 !mishandled_messages_ && | |
307 message_count_ == kExpectedMessageCount; | |
308 } | |
309 | |
310 private: | |
311 static const int kExpectedMessageCount = 10; | |
312 int message_count_; | |
313 bool termination_seen_; | |
314 DISALLOW_COPY_AND_ASSIGN(EchoCppSideConnection); | |
315 }; | |
316 | |
317 // Test that corrupted messages don't wreak havoc. | |
318 class BitFlipCppSideConnection : public CppSideConnection { | |
319 public: | |
320 BitFlipCppSideConnection() : termination_seen_(false) {} | |
321 ~BitFlipCppSideConnection() override {} | |
322 | |
323 // js_to_cpp::CppSide: | |
324 void StartTest() override { js_side_->BitFlip(BuildSampleEchoArgs()); } | |
325 | |
326 void BitFlipResponse(js_to_cpp::EchoArgsListPtr list) override { | |
327 CheckCorruptedEchoArgsList(list); | |
328 } | |
329 | |
330 void TestFinished() override { | |
331 termination_seen_ = true; | |
332 run_loop()->Quit(); | |
333 } | |
334 | |
335 bool DidSucceed() { | |
336 return termination_seen_; | |
337 } | |
338 | |
339 private: | |
340 bool termination_seen_; | |
341 DISALLOW_COPY_AND_ASSIGN(BitFlipCppSideConnection); | |
342 }; | |
343 | |
344 // Test that severely random messages don't wreak havoc. | |
345 class BackPointerCppSideConnection : public CppSideConnection { | |
346 public: | |
347 BackPointerCppSideConnection() : termination_seen_(false) {} | |
348 ~BackPointerCppSideConnection() override {} | |
349 | |
350 // js_to_cpp::CppSide: | |
351 void StartTest() override { js_side_->BackPointer(BuildSampleEchoArgs()); } | |
352 | |
353 void BackPointerResponse(js_to_cpp::EchoArgsListPtr list) override { | |
354 CheckCorruptedEchoArgsList(list); | |
355 } | |
356 | |
357 void TestFinished() override { | |
358 termination_seen_ = true; | |
359 run_loop()->Quit(); | |
360 } | |
361 | |
362 bool DidSucceed() { | |
363 return termination_seen_; | |
364 } | |
365 | |
366 private: | |
367 bool termination_seen_; | |
368 DISALLOW_COPY_AND_ASSIGN(BackPointerCppSideConnection); | |
369 }; | |
370 | |
371 } // namespace | |
372 | |
373 class JsToCppTest : public testing::Test { | |
374 public: | |
375 JsToCppTest() {} | |
376 | |
377 void RunTest(const std::string& test, CppSideConnection* cpp_side) { | |
378 cpp_side->set_run_loop(&run_loop_); | |
379 | |
380 MessagePipe pipe; | |
381 js_to_cpp::JsSidePtr js_side = | |
382 MakeProxy<js_to_cpp::JsSide>(pipe.handle0.Pass()); | |
383 js_side.set_client(cpp_side); | |
384 | |
385 js_side.internal_state()->router_for_testing()->EnableTestingMode(); | |
386 | |
387 cpp_side->set_js_side(js_side.get()); | |
388 | |
389 gin::IsolateHolder::Initialize(gin::IsolateHolder::kStrictMode, | |
390 gin::ArrayBufferAllocator::SharedInstance()); | |
391 gin::IsolateHolder instance; | |
392 apps::MojoRunnerDelegate delegate; | |
393 gin::ShellRunner runner(&delegate, instance.isolate()); | |
394 delegate.Start(&runner, pipe.handle1.release().value(), test); | |
395 | |
396 run_loop_.Run(); | |
397 } | |
398 | |
399 private: | |
400 base::ShadowingAtExitManager at_exit_; | |
401 base::MessageLoop loop; | |
402 base::RunLoop run_loop_; | |
403 | |
404 DISALLOW_COPY_AND_ASSIGN(JsToCppTest); | |
405 }; | |
406 | |
407 TEST_F(JsToCppTest, Ping) { | |
408 if (IsRunningOnIsolatedBot()) | |
409 return; | |
410 | |
411 PingCppSideConnection cpp_side_connection; | |
412 RunTest("mojo/apps/js/test/js_to_cpp_unittest", &cpp_side_connection); | |
413 EXPECT_TRUE(cpp_side_connection.DidSucceed()); | |
414 } | |
415 | |
416 TEST_F(JsToCppTest, Echo) { | |
417 if (IsRunningOnIsolatedBot()) | |
418 return; | |
419 | |
420 EchoCppSideConnection cpp_side_connection; | |
421 RunTest("mojo/apps/js/test/js_to_cpp_unittest", &cpp_side_connection); | |
422 EXPECT_TRUE(cpp_side_connection.DidSucceed()); | |
423 } | |
424 | |
425 TEST_F(JsToCppTest, BitFlip) { | |
426 if (IsRunningOnIsolatedBot()) | |
427 return; | |
428 | |
429 BitFlipCppSideConnection cpp_side_connection; | |
430 RunTest("mojo/apps/js/test/js_to_cpp_unittest", &cpp_side_connection); | |
431 EXPECT_TRUE(cpp_side_connection.DidSucceed()); | |
432 } | |
433 | |
434 TEST_F(JsToCppTest, BackPointer) { | |
435 if (IsRunningOnIsolatedBot()) | |
436 return; | |
437 | |
438 BackPointerCppSideConnection cpp_side_connection; | |
439 RunTest("mojo/apps/js/test/js_to_cpp_unittest", &cpp_side_connection); | |
440 EXPECT_TRUE(cpp_side_connection.DidSucceed()); | |
441 } | |
442 | |
443 } // namespace js | |
444 } // namespace mojo | |
OLD | NEW |