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