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.
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"
23 // Global value updated by some checks to prevent compilers from optimizing
24 // reads out of existence.
25 uint32 g_waste_accumulator = 0;
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;
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;
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();
51 // NaN has the property that it is not equal to itself.
52 #define EXPECT_NAN(x) EXPECT_NE(x, x)
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.";
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]);
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]);
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();
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());
139 void CheckSampleEchoArgsList(const js_to_cpp::EchoArgsListPtr& list) {
142 CheckSampleEchoArgs(*list->item);
143 CheckSampleEchoArgsList(list->next);
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
149 void CheckCorruptedString(const String& arg) {
152 for (size_t i = 0; i < arg.size(); ++i)
153 g_waste_accumulator += arg[i];
156 void CheckCorruptedStringArray(const Array<String>& string_array) {
157 if (string_array.is_null())
159 for (size_t i = 0; i < string_array.size(); ++i)
160 CheckCorruptedString(string_array[i]);
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)
170 for (uint32_t i = 0; i < buffer_size; ++i)
171 g_waste_accumulator += buffer[i];
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)
181 for (uint32_t i = 0; i < buffer_size; ++i)
182 g_waste_accumulator += buffer[i];
185 void CheckCorruptedEchoArgs(const js_to_cpp::EchoArgsPtr& arg) {
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());
196 void CheckCorruptedEchoArgsList(const js_to_cpp::EchoArgsListPtr& list) {
199 CheckCorruptedEchoArgs(list->item);
200 CheckCorruptedEchoArgsList(list->next);
203 // Base Provider implementation class. It's expected that tests subclass and
204 // override the appropriate Provider functions. When test is done quit the
206 class CppSideConnection : public js_to_cpp::CppSide {
208 CppSideConnection() :
211 mishandled_messages_(0) {
213 virtual ~CppSideConnection() {}
215 void set_run_loop(base::RunLoop* run_loop) { run_loop_ = run_loop; }
216 base::RunLoop* run_loop() { return run_loop_; }
218 void set_js_side(js_to_cpp::JsSide* js_side) { js_side_ = js_side; }
219 js_to_cpp::JsSide* js_side() { return js_side_; }
221 // js_to_cpp::CppSide:
222 virtual void StartTest() OVERRIDE {
226 virtual void TestFinished() OVERRIDE {
230 virtual void PingResponse() OVERRIDE {
231 mishandled_messages_ += 1;
234 virtual void EchoResponse(js_to_cpp::EchoArgsListPtr list) OVERRIDE {
235 mishandled_messages_ += 1;
238 virtual void BitFlipResponse(js_to_cpp::EchoArgsListPtr list) OVERRIDE {
239 mishandled_messages_ += 1;
242 virtual void BackPointerResponse(
243 js_to_cpp::EchoArgsListPtr list) OVERRIDE {
244 mishandled_messages_ += 1;
248 base::RunLoop* run_loop_;
249 js_to_cpp::JsSide* js_side_;
250 int mishandled_messages_;
253 DISALLOW_COPY_AND_ASSIGN(CppSideConnection);
256 // Trivial test to verify a message sent from JS is received.
257 class PingCppSideConnection : public CppSideConnection {
259 PingCppSideConnection() : got_message_(false) {}
260 virtual ~PingCppSideConnection() {}
262 // js_to_cpp::CppSide:
263 virtual void StartTest() OVERRIDE {
267 virtual void PingResponse() OVERRIDE {
273 return got_message_ && !mishandled_messages_;
278 DISALLOW_COPY_AND_ASSIGN(PingCppSideConnection);
281 // Test that parameters are passed with correct values.
282 class EchoCppSideConnection : public CppSideConnection {
284 EchoCppSideConnection() :
286 termination_seen_(false) {
288 virtual ~EchoCppSideConnection() {}
290 // js_to_cpp::CppSide:
291 virtual void StartTest() OVERRIDE {
292 js_side_->Echo(kExpectedMessageCount, BuildSampleEchoArgs());
295 virtual void EchoResponse(js_to_cpp::EchoArgsListPtr list) OVERRIDE {
296 const js_to_cpp::EchoArgsPtr& special_arg = list->item;
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);
306 virtual void TestFinished() OVERRIDE {
307 termination_seen_ = true;
312 return termination_seen_ &&
313 !mishandled_messages_ &&
314 message_count_ == kExpectedMessageCount;
318 static const int kExpectedMessageCount = 10;
320 bool termination_seen_;
321 DISALLOW_COPY_AND_ASSIGN(EchoCppSideConnection);
324 // Test that corrupted messages don't wreak havoc.
325 class BitFlipCppSideConnection : public CppSideConnection {
327 BitFlipCppSideConnection() : termination_seen_(false) {}
328 virtual ~BitFlipCppSideConnection() {}
330 // js_to_cpp::CppSide:
331 virtual void StartTest() OVERRIDE {
332 js_side_->BitFlip(BuildSampleEchoArgs());
335 virtual void BitFlipResponse(js_to_cpp::EchoArgsListPtr list) OVERRIDE {
336 CheckCorruptedEchoArgsList(list);
339 virtual void TestFinished() OVERRIDE {
340 termination_seen_ = true;
345 return termination_seen_;
349 bool termination_seen_;
350 DISALLOW_COPY_AND_ASSIGN(BitFlipCppSideConnection);
353 // Test that severely random messages don't wreak havoc.
354 class BackPointerCppSideConnection : public CppSideConnection {
356 BackPointerCppSideConnection() : termination_seen_(false) {}
357 virtual ~BackPointerCppSideConnection() {}
359 // js_to_cpp::CppSide:
360 virtual void StartTest() OVERRIDE {
361 js_side_->BackPointer(BuildSampleEchoArgs());
364 virtual void BackPointerResponse(
365 js_to_cpp::EchoArgsListPtr list) OVERRIDE {
366 CheckCorruptedEchoArgsList(list);
369 virtual void TestFinished() OVERRIDE {
370 termination_seen_ = true;
375 return termination_seen_;
379 bool termination_seen_;
380 DISALLOW_COPY_AND_ASSIGN(BackPointerCppSideConnection);
385 class JsToCppTest : public testing::Test {
389 void RunTest(const std::string& test, CppSideConnection* cpp_side) {
390 cpp_side->set_run_loop(&run_loop_);
393 js_to_cpp::JsSidePtr js_side =
394 MakeProxy<js_to_cpp::JsSide>(pipe.handle0.Pass());
395 js_side.set_client(cpp_side);
397 js_side.internal_state()->router()->EnableTestingMode();
399 cpp_side->set_js_side(js_side.get());
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);
410 base::ShadowingAtExitManager at_exit_;
411 base::MessageLoop loop;
412 base::RunLoop run_loop_;
414 DISALLOW_COPY_AND_ASSIGN(JsToCppTest);
417 TEST_F(JsToCppTest, Ping) {
418 if (IsRunningOnIsolatedBot())
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());
426 TEST_F(JsToCppTest, Echo) {
427 if (IsRunningOnIsolatedBot())
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());
435 TEST_F(JsToCppTest, BitFlip) {
436 if (IsRunningOnIsolatedBot())
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());
444 TEST_F(JsToCppTest, BackPointer) {
445 if (IsRunningOnIsolatedBot())
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());