Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <stddef.h> | 5 #include "bindings/core/v8/ScriptController.h" |
| 6 #include <stdint.h> | 6 #include "bindings/core/v8/ScriptSourceCode.h" |
| 7 | 7 #include "bindings/core/v8/V8BindingForCore.h" |
| 8 #include <string> | 8 #include "bindings/core/v8/V8ScriptRunner.h" |
| 9 #include <utility> | 9 #include "core/frame/Settings.h" |
| 10 | 10 #include "core/mojo/MojoHandle.h" |
| 11 #include "base/at_exit.h" | 11 #include "core/testing/DummyPageHolder.h" |
| 12 #include "base/files/file_path.h" | |
| 13 #include "base/files/file_util.h" | |
| 14 #include "base/macros.h" | |
| 15 #include "base/run_loop.h" | |
| 16 #include "base/strings/utf_string_conversions.h" | |
| 17 #include "base/test/scoped_task_environment.h" | |
| 18 #include "base/threading/thread_task_runner_handle.h" | |
| 19 #include "gin/array_buffer.h" | |
| 20 #include "gin/public/isolate_holder.h" | |
| 21 #include "gin/v8_initializer.h" | |
| 22 #include "mojo/common/data_pipe_utils.h" | |
| 23 #include "mojo/edk/js/mojo_runner_delegate.h" | |
| 24 #include "mojo/edk/js/tests/js_to_cpp.mojom.h" | |
| 25 #include "mojo/public/cpp/bindings/binding.h" | 12 #include "mojo/public/cpp/bindings/binding.h" |
| 26 #include "mojo/public/cpp/bindings/lib/validation_errors.h" | 13 #include "mojo/public/cpp/bindings/lib/validation_errors.h" |
| 27 #include "mojo/public/cpp/system/core.h" | |
| 28 #include "mojo/public/cpp/system/wait.h" | 14 #include "mojo/public/cpp/system/wait.h" |
| 15 #include "platform/testing/UnitTestHelpers.h" | |
| 29 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #include "third_party/WebKit/Source/core/mojo/tests/JsToCpp.mojom-blink.h" | |
| 30 | 18 |
| 31 namespace mojo { | 19 namespace blink { |
| 32 namespace edk { | 20 namespace { |
| 33 namespace js { | |
| 34 | 21 |
| 35 // Global value updated by some checks to prevent compilers from optimizing | 22 // Global value updated by some checks to prevent compilers from optimizing |
| 36 // reads out of existence. | 23 // reads out of existence. |
| 37 uint32_t g_waste_accumulator = 0; | 24 uint32_t g_waste_accumulator = 0; |
| 38 | 25 |
| 39 namespace { | |
| 40 | |
| 41 // Negative numbers with different values in each byte, the last of | 26 // Negative numbers with different values in each byte, the last of |
| 42 // which can survive promotion to double and back. | 27 // which can survive promotion to double and back. |
| 43 const int8_t kExpectedInt8Value = -65; | 28 const int8_t kExpectedInt8Value = -65; |
| 44 const int16_t kExpectedInt16Value = -16961; | 29 const int16_t kExpectedInt16Value = -16961; |
| 45 const int32_t kExpectedInt32Value = -1145258561; | 30 const int32_t kExpectedInt32Value = -1145258561; |
| 46 const int64_t kExpectedInt64Value = -77263311946305LL; | 31 const int64_t kExpectedInt64Value = -77263311946305LL; |
| 47 | 32 |
| 48 // Positive numbers with different values in each byte, the last of | 33 // Positive numbers with different values in each byte, the last of |
| 49 // which can survive promotion to double and back. | 34 // which can survive promotion to double and back. |
| 50 const uint8_t kExpectedUInt8Value = 65; | 35 const uint8_t kExpectedUInt8Value = 65; |
| 51 const uint16_t kExpectedUInt16Value = 16961; | 36 const uint16_t kExpectedUInt16Value = 16961; |
| 52 const uint32_t kExpectedUInt32Value = 1145258561; | 37 const uint32_t kExpectedUInt32Value = 1145258561; |
| 53 const uint64_t kExpectedUInt64Value = 77263311946305LL; | 38 const uint64_t kExpectedUInt64Value = 77263311946305LL; |
| 54 | 39 |
| 55 // Double/float values, including special case constants. | 40 // Double/float values, including special case constants. |
| 56 const double kExpectedDoubleVal = 3.14159265358979323846; | 41 const double kExpectedDoubleVal = 3.14159265358979323846; |
| 57 const double kExpectedDoubleInf = std::numeric_limits<double>::infinity(); | 42 const double kExpectedDoubleInf = std::numeric_limits<double>::infinity(); |
| 58 const double kExpectedDoubleNan = std::numeric_limits<double>::quiet_NaN(); | 43 const double kExpectedDoubleNan = std::numeric_limits<double>::quiet_NaN(); |
| 59 const float kExpectedFloatVal = static_cast<float>(kExpectedDoubleVal); | 44 const float kExpectedFloatVal = static_cast<float>(kExpectedDoubleVal); |
| 60 const float kExpectedFloatInf = std::numeric_limits<float>::infinity(); | 45 const float kExpectedFloatInf = std::numeric_limits<float>::infinity(); |
| 61 const float kExpectedFloatNan = std::numeric_limits<float>::quiet_NaN(); | 46 const float kExpectedFloatNan = std::numeric_limits<float>::quiet_NaN(); |
| 62 | 47 |
| 63 // NaN has the property that it is not equal to itself. | 48 // NaN has the property that it is not equal to itself. |
| 64 #define EXPECT_NAN(x) EXPECT_NE(x, x) | 49 #define EXPECT_NAN(x) EXPECT_NE(x, x) |
| 65 | 50 |
| 66 void CheckDataPipe(ScopedDataPipeConsumerHandle data_pipe_handle) { | 51 String MojoBindingsScriptPath() { |
| 67 std::string buffer; | 52 String filepath = testing::ExecutableDir(); |
| 68 bool result = common::BlockingCopyToString(std::move(data_pipe_handle), | 53 filepath.append("/gen/mojo/public/js/mojo_bindings.js"); |
| 69 &buffer); | 54 return filepath; |
| 70 EXPECT_TRUE(result); | |
| 71 EXPECT_EQ(64u, buffer.size()); | |
| 72 for (int i = 0; i < 64; ++i) { | |
| 73 EXPECT_EQ(i, buffer[i]); | |
| 74 } | |
| 75 } | 55 } |
| 76 | 56 |
| 77 void CheckMessagePipe(MessagePipeHandle message_pipe_handle) { | 57 String TestBindingsScriptPath() { |
| 58 String filepath = testing::ExecutableDir(); | |
| 59 filepath.append( | |
| 60 "/gen/third_party/WebKit/Source/core/mojo/tests/JsToCpp.mojom.js"); | |
| 61 return filepath; | |
| 62 } | |
| 63 | |
| 64 String TestScriptPath() { | |
| 65 String filepath = testing::BlinkRootDir(); | |
| 66 filepath.append("/Source/core/mojo/tests/JsToCppTest.js"); | |
| 67 return filepath; | |
| 68 } | |
| 69 | |
| 70 void CheckDataPipe(mojo::DataPipeConsumerHandle data_pipe_handle) { | |
| 71 const void* buffer = nullptr; | |
| 72 unsigned num_bytes = 0; | |
| 73 MojoResult result = BeginReadDataRaw(data_pipe_handle, &buffer, &num_bytes, | |
| 74 MOJO_READ_DATA_FLAG_NONE); | |
| 75 EXPECT_EQ(MOJO_RESULT_OK, result); | |
| 76 EXPECT_EQ(64u, num_bytes); | |
| 77 for (unsigned i = 0; i < num_bytes; ++i) { | |
| 78 EXPECT_EQ(i, static_cast<unsigned>(static_cast<const char*>(buffer)[i])); | |
| 79 } | |
| 80 EndReadDataRaw(data_pipe_handle, num_bytes); | |
| 81 } | |
| 82 | |
| 83 void CheckMessagePipe(mojo::MessagePipeHandle message_pipe_handle) { | |
| 78 unsigned char buffer[100]; | 84 unsigned char buffer[100]; |
| 79 uint32_t buffer_size = static_cast<uint32_t>(sizeof(buffer)); | 85 uint32_t buffer_size = static_cast<uint32_t>(sizeof(buffer)); |
| 80 MojoResult result = Wait(message_pipe_handle, MOJO_HANDLE_SIGNAL_READABLE); | 86 MojoResult result = Wait(message_pipe_handle, MOJO_HANDLE_SIGNAL_READABLE); |
|
alokp
2017/06/02 23:49:58
result == MOJO_RESULT_INVALID_ARGUMENT here indica
| |
| 81 EXPECT_EQ(MOJO_RESULT_OK, result); | 87 EXPECT_EQ(MOJO_RESULT_OK, result); |
| 82 result = ReadMessageRaw( | 88 result = ReadMessageRaw(message_pipe_handle, buffer, &buffer_size, 0, 0, 0); |
| 83 message_pipe_handle, buffer, &buffer_size, 0, 0, 0); | |
| 84 EXPECT_EQ(MOJO_RESULT_OK, result); | 89 EXPECT_EQ(MOJO_RESULT_OK, result); |
| 85 EXPECT_EQ(64u, buffer_size); | 90 EXPECT_EQ(64u, buffer_size); |
| 86 for (int i = 0; i < 64; ++i) { | 91 for (int i = 0; i < 64; ++i) { |
| 87 EXPECT_EQ(255 - i, buffer[i]); | 92 EXPECT_EQ(255 - i, buffer[i]); |
| 88 } | 93 } |
| 89 } | 94 } |
| 90 | 95 |
| 91 js_to_cpp::EchoArgsPtr BuildSampleEchoArgs() { | 96 js_to_cpp::blink::EchoArgsPtr BuildSampleEchoArgs() { |
| 92 js_to_cpp::EchoArgsPtr args(js_to_cpp::EchoArgs::New()); | 97 auto args = js_to_cpp::blink::EchoArgs::New(); |
| 93 args->si64 = kExpectedInt64Value; | 98 args->si64 = kExpectedInt64Value; |
| 94 args->si32 = kExpectedInt32Value; | 99 args->si32 = kExpectedInt32Value; |
| 95 args->si16 = kExpectedInt16Value; | 100 args->si16 = kExpectedInt16Value; |
| 96 args->si8 = kExpectedInt8Value; | 101 args->si8 = kExpectedInt8Value; |
| 97 args->ui64 = kExpectedUInt64Value; | 102 args->ui64 = kExpectedUInt64Value; |
| 98 args->ui32 = kExpectedUInt32Value; | 103 args->ui32 = kExpectedUInt32Value; |
| 99 args->ui16 = kExpectedUInt16Value; | 104 args->ui16 = kExpectedUInt16Value; |
| 100 args->ui8 = kExpectedUInt8Value; | 105 args->ui8 = kExpectedUInt8Value; |
| 101 args->float_val = kExpectedFloatVal; | 106 args->float_val = kExpectedFloatVal; |
| 102 args->float_inf = kExpectedFloatInf; | 107 args->float_inf = kExpectedFloatInf; |
| 103 args->float_nan = kExpectedFloatNan; | 108 args->float_nan = kExpectedFloatNan; |
| 104 args->double_val = kExpectedDoubleVal; | 109 args->double_val = kExpectedDoubleVal; |
| 105 args->double_inf = kExpectedDoubleInf; | 110 args->double_inf = kExpectedDoubleInf; |
| 106 args->double_nan = kExpectedDoubleNan; | 111 args->double_nan = kExpectedDoubleNan; |
| 107 args->name.emplace("coming"); | 112 args->name = "coming"; |
| 108 args->string_array.emplace(3); | 113 args->string_array.emplace(3); |
| 109 (*args->string_array)[0] = "one"; | 114 (*args->string_array)[0] = "one"; |
| 110 (*args->string_array)[1] = "two"; | 115 (*args->string_array)[1] = "two"; |
| 111 (*args->string_array)[2] = "three"; | 116 (*args->string_array)[2] = "three"; |
| 112 return args; | 117 return args; |
| 113 } | 118 } |
| 114 | 119 |
| 115 void CheckSampleEchoArgs(js_to_cpp::EchoArgsPtr arg) { | 120 void CheckSampleEchoArgs(js_to_cpp::blink::EchoArgsPtr arg) { |
| 116 EXPECT_EQ(kExpectedInt64Value, arg->si64); | 121 EXPECT_EQ(kExpectedInt64Value, arg->si64); |
| 117 EXPECT_EQ(kExpectedInt32Value, arg->si32); | 122 EXPECT_EQ(kExpectedInt32Value, arg->si32); |
| 118 EXPECT_EQ(kExpectedInt16Value, arg->si16); | 123 EXPECT_EQ(kExpectedInt16Value, arg->si16); |
| 119 EXPECT_EQ(kExpectedInt8Value, arg->si8); | 124 EXPECT_EQ(kExpectedInt8Value, arg->si8); |
| 120 EXPECT_EQ(kExpectedUInt64Value, arg->ui64); | 125 EXPECT_EQ(kExpectedUInt64Value, arg->ui64); |
| 121 EXPECT_EQ(kExpectedUInt32Value, arg->ui32); | 126 EXPECT_EQ(kExpectedUInt32Value, arg->ui32); |
| 122 EXPECT_EQ(kExpectedUInt16Value, arg->ui16); | 127 EXPECT_EQ(kExpectedUInt16Value, arg->ui16); |
| 123 EXPECT_EQ(kExpectedUInt8Value, arg->ui8); | 128 EXPECT_EQ(kExpectedUInt8Value, arg->ui8); |
| 124 EXPECT_EQ(kExpectedFloatVal, arg->float_val); | 129 EXPECT_EQ(kExpectedFloatVal, arg->float_val); |
| 125 EXPECT_EQ(kExpectedFloatInf, arg->float_inf); | 130 EXPECT_EQ(kExpectedFloatInf, arg->float_inf); |
| 126 EXPECT_NAN(arg->float_nan); | 131 EXPECT_NAN(arg->float_nan); |
| 127 EXPECT_EQ(kExpectedDoubleVal, arg->double_val); | 132 EXPECT_EQ(kExpectedDoubleVal, arg->double_val); |
| 128 EXPECT_EQ(kExpectedDoubleInf, arg->double_inf); | 133 EXPECT_EQ(kExpectedDoubleInf, arg->double_inf); |
| 129 EXPECT_NAN(arg->double_nan); | 134 EXPECT_NAN(arg->double_nan); |
| 130 EXPECT_EQ(std::string("coming"), *arg->name); | 135 EXPECT_EQ(String("coming"), arg->name); |
| 131 EXPECT_EQ(std::string("one"), (*arg->string_array)[0]); | 136 EXPECT_EQ(String("one"), (*arg->string_array)[0]); |
| 132 EXPECT_EQ(std::string("two"), (*arg->string_array)[1]); | 137 EXPECT_EQ(String("two"), (*arg->string_array)[1]); |
| 133 EXPECT_EQ(std::string("three"), (*arg->string_array)[2]); | 138 EXPECT_EQ(String("three"), (*arg->string_array)[2]); |
| 134 CheckDataPipe(std::move(arg->data_handle)); | 139 CheckDataPipe(arg->data_handle.get()); |
| 135 CheckMessagePipe(arg->message_handle.get()); | 140 CheckMessagePipe(arg->message_handle.get()); |
| 136 } | 141 } |
| 137 | 142 |
| 138 void CheckSampleEchoArgsList(const js_to_cpp::EchoArgsListPtr& list) { | 143 void CheckSampleEchoArgsList(const js_to_cpp::blink::EchoArgsListPtr& list) { |
| 139 if (list.is_null()) | 144 if (list.is_null()) |
| 140 return; | 145 return; |
| 141 CheckSampleEchoArgs(std::move(list->item)); | 146 CheckSampleEchoArgs(std::move(list->item)); |
| 142 CheckSampleEchoArgsList(list->next); | 147 CheckSampleEchoArgsList(list->next); |
| 143 } | 148 } |
| 144 | 149 |
| 145 // More forgiving checks are needed in the face of potentially corrupt | 150 // More forgiving checks are needed in the face of potentially corrupt |
| 146 // messages. The values don't matter so long as all accesses are within | 151 // messages. The values don't matter so long as all accesses are within |
| 147 // bounds. | 152 // bounds. |
| 148 void CheckCorruptedString(const std::string& arg) { | 153 void CheckCorruptedString(const String& arg) { |
| 149 for (size_t i = 0; i < arg.size(); ++i) | 154 for (size_t i = 0; i < arg.length(); ++i) |
| 150 g_waste_accumulator += arg[i]; | 155 g_waste_accumulator += arg[i]; |
| 151 } | 156 } |
| 152 | 157 |
| 153 void CheckCorruptedString(const base::Optional<std::string>& arg) { | 158 void CheckCorruptedStringArray(const Optional<Vector<String>>& string_array) { |
| 154 if (!arg) | |
| 155 return; | |
| 156 CheckCorruptedString(*arg); | |
| 157 } | |
| 158 | |
| 159 void CheckCorruptedStringArray( | |
| 160 const base::Optional<std::vector<std::string>>& string_array) { | |
| 161 if (!string_array) | 159 if (!string_array) |
| 162 return; | 160 return; |
| 163 for (size_t i = 0; i < string_array->size(); ++i) | 161 for (size_t i = 0; i < string_array->size(); ++i) |
| 164 CheckCorruptedString((*string_array)[i]); | 162 CheckCorruptedString((*string_array)[i]); |
| 165 } | 163 } |
| 166 | 164 |
| 167 void CheckCorruptedDataPipe(MojoHandle data_pipe_handle) { | 165 void CheckCorruptedDataPipe(::MojoHandle data_pipe_handle) { |
| 168 unsigned char buffer[100]; | 166 unsigned char buffer[100]; |
| 169 uint32_t buffer_size = static_cast<uint32_t>(sizeof(buffer)); | 167 uint32_t buffer_size = static_cast<uint32_t>(sizeof(buffer)); |
| 170 MojoResult result = MojoReadData( | 168 MojoResult result = MojoReadData(data_pipe_handle, buffer, &buffer_size, |
| 171 data_pipe_handle, buffer, &buffer_size, MOJO_READ_DATA_FLAG_NONE); | 169 MOJO_READ_DATA_FLAG_NONE); |
| 172 if (result != MOJO_RESULT_OK) | 170 if (result != MOJO_RESULT_OK) |
| 173 return; | 171 return; |
| 174 for (uint32_t i = 0; i < buffer_size; ++i) | 172 for (uint32_t i = 0; i < buffer_size; ++i) |
| 175 g_waste_accumulator += buffer[i]; | 173 g_waste_accumulator += buffer[i]; |
| 176 } | 174 } |
| 177 | 175 |
| 178 void CheckCorruptedMessagePipe(MojoHandle message_pipe_handle) { | 176 void CheckCorruptedMessagePipe(::MojoHandle message_pipe_handle) { |
| 179 unsigned char buffer[100]; | 177 unsigned char buffer[100]; |
| 180 uint32_t buffer_size = static_cast<uint32_t>(sizeof(buffer)); | 178 uint32_t buffer_size = static_cast<uint32_t>(sizeof(buffer)); |
| 181 MojoResult result = MojoReadMessage( | 179 MojoResult result = |
| 182 message_pipe_handle, buffer, &buffer_size, 0, 0, 0); | 180 MojoReadMessage(message_pipe_handle, buffer, &buffer_size, 0, 0, 0); |
| 183 if (result != MOJO_RESULT_OK) | 181 if (result != MOJO_RESULT_OK) |
| 184 return; | 182 return; |
| 185 for (uint32_t i = 0; i < buffer_size; ++i) | 183 for (uint32_t i = 0; i < buffer_size; ++i) |
| 186 g_waste_accumulator += buffer[i]; | 184 g_waste_accumulator += buffer[i]; |
| 187 } | 185 } |
| 188 | 186 |
| 189 void CheckCorruptedEchoArgs(const js_to_cpp::EchoArgsPtr& arg) { | 187 void CheckCorruptedEchoArgs(const js_to_cpp::blink::EchoArgsPtr& arg) { |
| 190 if (arg.is_null()) | 188 if (arg.is_null()) |
| 191 return; | 189 return; |
| 192 CheckCorruptedString(arg->name); | 190 CheckCorruptedString(arg->name); |
| 193 CheckCorruptedStringArray(arg->string_array); | 191 CheckCorruptedStringArray(arg->string_array); |
| 194 if (arg->data_handle.is_valid()) | 192 if (arg->data_handle.is_valid()) |
| 195 CheckCorruptedDataPipe(arg->data_handle.get().value()); | 193 CheckCorruptedDataPipe(arg->data_handle.get().value()); |
| 196 if (arg->message_handle.is_valid()) | 194 if (arg->message_handle.is_valid()) |
| 197 CheckCorruptedMessagePipe(arg->message_handle.get().value()); | 195 CheckCorruptedMessagePipe(arg->message_handle.get().value()); |
| 198 } | 196 } |
| 199 | 197 |
| 200 void CheckCorruptedEchoArgsList(const js_to_cpp::EchoArgsListPtr& list) { | 198 void CheckCorruptedEchoArgsList(const js_to_cpp::blink::EchoArgsListPtr& list) { |
| 201 if (list.is_null()) | 199 if (list.is_null()) |
| 202 return; | 200 return; |
| 203 CheckCorruptedEchoArgs(list->item); | 201 CheckCorruptedEchoArgs(list->item); |
| 204 CheckCorruptedEchoArgsList(list->next); | 202 CheckCorruptedEchoArgsList(list->next); |
| 205 } | 203 } |
| 206 | 204 |
| 207 // Base Provider implementation class. It's expected that tests subclass and | 205 // Base Provider implementation class. It's expected that tests subclass and |
| 208 // override the appropriate Provider functions. When test is done quit the | 206 // override the appropriate Provider functions. When test is done quit the |
| 209 // run_loop(). | 207 // run_loop(). |
| 210 class CppSideConnection : public js_to_cpp::CppSide { | 208 class CppSideConnection : public js_to_cpp::blink::CppSide { |
| 211 public: | 209 public: |
| 212 CppSideConnection() | 210 CppSideConnection() |
| 213 : run_loop_(nullptr), | 211 : js_side_(nullptr), mishandled_messages_(0), binding_(this) {} |
| 214 js_side_(nullptr), | |
| 215 mishandled_messages_(0), | |
| 216 binding_(this) {} | |
| 217 ~CppSideConnection() override {} | 212 ~CppSideConnection() override {} |
| 218 | 213 |
| 219 void set_run_loop(base::RunLoop* run_loop) { run_loop_ = run_loop; } | 214 void set_js_side(js_to_cpp::blink::JsSidePtr js_side) { |
| 220 base::RunLoop* run_loop() { return run_loop_; } | 215 js_side_ = std::move(js_side); |
| 216 } | |
| 217 js_to_cpp::blink::JsSide* js_side() { return js_side_.get(); } | |
| 221 | 218 |
| 222 void set_js_side(js_to_cpp::JsSide* js_side) { js_side_ = js_side; } | 219 void Bind(mojo::InterfaceRequest<js_to_cpp::blink::CppSide> request) { |
| 223 js_to_cpp::JsSide* js_side() { return js_side_; } | |
| 224 | |
| 225 void Bind(InterfaceRequest<js_to_cpp::CppSide> request) { | |
| 226 binding_.Bind(std::move(request)); | 220 binding_.Bind(std::move(request)); |
| 227 // Keep the pipe open even after validation errors. | 221 // Keep the pipe open even after validation errors. |
| 228 binding_.EnableTestingMode(); | 222 binding_.EnableTestingMode(); |
| 229 } | 223 } |
| 230 | 224 |
| 231 // js_to_cpp::CppSide: | 225 // js_to_cpp::CppSide: |
| 232 void StartTest() override { NOTREACHED(); } | 226 void StartTest() override { NOTREACHED(); } |
| 233 | 227 |
| 234 void TestFinished() override { NOTREACHED(); } | 228 void TestFinished() override { NOTREACHED(); } |
| 235 | 229 |
| 236 void PingResponse() override { mishandled_messages_ += 1; } | 230 void PingResponse() override { mishandled_messages_ += 1; } |
| 237 | 231 |
| 238 void EchoResponse(js_to_cpp::EchoArgsListPtr list) override { | 232 void EchoResponse(js_to_cpp::blink::EchoArgsListPtr list) override { |
| 239 mishandled_messages_ += 1; | 233 mishandled_messages_ += 1; |
| 240 } | 234 } |
| 241 | 235 |
| 242 void BitFlipResponse( | 236 void BitFlipResponse( |
| 243 js_to_cpp::EchoArgsListPtr list, | 237 js_to_cpp::blink::EchoArgsListPtr list, |
| 244 js_to_cpp::ForTestingAssociatedPtrInfo not_used) override { | 238 js_to_cpp::blink::ForTestingAssociatedPtrInfo not_used) override { |
| 245 mishandled_messages_ += 1; | 239 mishandled_messages_ += 1; |
| 246 } | 240 } |
| 247 | 241 |
| 248 void BackPointerResponse(js_to_cpp::EchoArgsListPtr list) override { | 242 void BackPointerResponse(js_to_cpp::blink::EchoArgsListPtr list) override { |
| 249 mishandled_messages_ += 1; | 243 mishandled_messages_ += 1; |
| 250 } | 244 } |
| 251 | 245 |
| 252 protected: | 246 protected: |
| 253 base::RunLoop* run_loop_; | 247 js_to_cpp::blink::JsSidePtr js_side_; |
| 254 js_to_cpp::JsSide* js_side_; | |
| 255 int mishandled_messages_; | 248 int mishandled_messages_; |
| 256 mojo::Binding<js_to_cpp::CppSide> binding_; | 249 mojo::Binding<js_to_cpp::blink::CppSide> binding_; |
| 257 | |
| 258 private: | |
| 259 DISALLOW_COPY_AND_ASSIGN(CppSideConnection); | |
| 260 }; | 250 }; |
| 261 | 251 |
| 262 // Trivial test to verify a message sent from JS is received. | 252 // Trivial test to verify a message sent from JS is received. |
| 263 class PingCppSideConnection : public CppSideConnection { | 253 class PingCppSideConnection : public CppSideConnection { |
| 264 public: | 254 public: |
| 265 PingCppSideConnection() : got_message_(false) {} | 255 PingCppSideConnection() : got_message_(false) {} |
| 266 ~PingCppSideConnection() override {} | 256 ~PingCppSideConnection() override {} |
| 267 | 257 |
| 268 // js_to_cpp::CppSide: | 258 // js_to_cpp::CppSide: |
| 269 void StartTest() override { js_side_->Ping(); } | 259 void StartTest() override { js_side_->Ping(); } |
| 270 | 260 |
| 271 void PingResponse() override { | 261 void PingResponse() override { |
| 272 got_message_ = true; | 262 got_message_ = true; |
| 273 run_loop()->Quit(); | 263 testing::ExitRunLoop(); |
| 274 } | 264 } |
| 275 | 265 |
| 276 bool DidSucceed() { | 266 bool DidSucceed() { return got_message_ && !mishandled_messages_; } |
| 277 return got_message_ && !mishandled_messages_; | |
| 278 } | |
| 279 | 267 |
| 280 private: | 268 private: |
| 281 bool got_message_; | 269 bool got_message_; |
| 282 DISALLOW_COPY_AND_ASSIGN(PingCppSideConnection); | |
| 283 }; | 270 }; |
| 284 | 271 |
| 285 // Test that parameters are passed with correct values. | 272 // Test that parameters are passed with correct values. |
| 286 class EchoCppSideConnection : public CppSideConnection { | 273 class EchoCppSideConnection : public CppSideConnection { |
| 287 public: | 274 public: |
| 288 EchoCppSideConnection() : | 275 EchoCppSideConnection() : message_count_(0), termination_seen_(false) {} |
| 289 message_count_(0), | |
| 290 termination_seen_(false) { | |
| 291 } | |
| 292 ~EchoCppSideConnection() override {} | 276 ~EchoCppSideConnection() override {} |
| 293 | 277 |
| 294 // js_to_cpp::CppSide: | 278 // js_to_cpp::CppSide: |
| 295 void StartTest() override { | 279 void StartTest() override { |
| 296 js_side_->Echo(kExpectedMessageCount, BuildSampleEchoArgs()); | 280 js_side_->Echo(kExpectedMessageCount, BuildSampleEchoArgs()); |
| 297 } | 281 } |
| 298 | 282 |
| 299 void EchoResponse(js_to_cpp::EchoArgsListPtr list) override { | 283 void EchoResponse(js_to_cpp::blink::EchoArgsListPtr list) override { |
| 300 const js_to_cpp::EchoArgsPtr& special_arg = list->item; | 284 const js_to_cpp::blink::EchoArgsPtr& special_arg = list->item; |
| 301 message_count_ += 1; | 285 message_count_ += 1; |
| 302 EXPECT_EQ(-1, special_arg->si64); | 286 EXPECT_EQ(-1, special_arg->si64); |
| 303 EXPECT_EQ(-1, special_arg->si32); | 287 EXPECT_EQ(-1, special_arg->si32); |
| 304 EXPECT_EQ(-1, special_arg->si16); | 288 EXPECT_EQ(-1, special_arg->si16); |
| 305 EXPECT_EQ(-1, special_arg->si8); | 289 EXPECT_EQ(-1, special_arg->si8); |
| 306 EXPECT_EQ(std::string("going"), *special_arg->name); | 290 EXPECT_EQ(String("going"), special_arg->name); |
| 307 CheckSampleEchoArgsList(list->next); | 291 CheckSampleEchoArgsList(list->next); |
| 308 } | 292 } |
| 309 | 293 |
| 310 void TestFinished() override { | 294 void TestFinished() override { |
| 311 termination_seen_ = true; | 295 termination_seen_ = true; |
| 312 run_loop()->Quit(); | 296 testing::ExitRunLoop(); |
| 313 } | 297 } |
| 314 | 298 |
| 315 bool DidSucceed() { | 299 bool DidSucceed() { |
| 316 return termination_seen_ && | 300 return termination_seen_ && !mishandled_messages_ && |
| 317 !mishandled_messages_ && | 301 message_count_ == kExpectedMessageCount; |
| 318 message_count_ == kExpectedMessageCount; | |
| 319 } | 302 } |
| 320 | 303 |
| 321 private: | 304 private: |
| 322 static const int kExpectedMessageCount = 10; | 305 static const int kExpectedMessageCount = 10; |
| 323 int message_count_; | 306 int message_count_; |
| 324 bool termination_seen_; | 307 bool termination_seen_; |
| 325 DISALLOW_COPY_AND_ASSIGN(EchoCppSideConnection); | |
| 326 }; | 308 }; |
| 327 | 309 |
| 328 // Test that corrupted messages don't wreak havoc. | 310 // Test that corrupted messages don't wreak havoc. |
| 329 class BitFlipCppSideConnection : public CppSideConnection { | 311 class BitFlipCppSideConnection : public CppSideConnection { |
| 330 public: | 312 public: |
| 331 BitFlipCppSideConnection() : termination_seen_(false) {} | 313 BitFlipCppSideConnection() : termination_seen_(false) {} |
| 332 ~BitFlipCppSideConnection() override {} | 314 ~BitFlipCppSideConnection() override {} |
| 333 | 315 |
| 334 // js_to_cpp::CppSide: | 316 // js_to_cpp::CppSide: |
| 335 void StartTest() override { js_side_->BitFlip(BuildSampleEchoArgs()); } | 317 void StartTest() override { js_side_->BitFlip(BuildSampleEchoArgs()); } |
| 336 | 318 |
| 337 void BitFlipResponse( | 319 void BitFlipResponse( |
| 338 js_to_cpp::EchoArgsListPtr list, | 320 js_to_cpp::blink::EchoArgsListPtr list, |
| 339 js_to_cpp::ForTestingAssociatedPtrInfo not_used) override { | 321 js_to_cpp::blink::ForTestingAssociatedPtrInfo not_used) override { |
| 340 CheckCorruptedEchoArgsList(list); | 322 CheckCorruptedEchoArgsList(list); |
| 341 } | 323 } |
| 342 | 324 |
| 343 void TestFinished() override { | 325 void TestFinished() override { |
| 344 termination_seen_ = true; | 326 termination_seen_ = true; |
| 345 run_loop()->Quit(); | 327 testing::ExitRunLoop(); |
| 346 } | 328 } |
| 347 | 329 |
| 348 bool DidSucceed() { | 330 bool DidSucceed() { return termination_seen_; } |
| 349 return termination_seen_; | |
| 350 } | |
| 351 | 331 |
| 352 private: | 332 private: |
| 353 bool termination_seen_; | 333 bool termination_seen_; |
| 354 DISALLOW_COPY_AND_ASSIGN(BitFlipCppSideConnection); | |
| 355 }; | 334 }; |
| 356 | 335 |
| 357 // Test that severely random messages don't wreak havoc. | 336 // Test that severely random messages don't wreak havoc. |
| 358 class BackPointerCppSideConnection : public CppSideConnection { | 337 class BackPointerCppSideConnection : public CppSideConnection { |
| 359 public: | 338 public: |
| 360 BackPointerCppSideConnection() : termination_seen_(false) {} | 339 BackPointerCppSideConnection() : termination_seen_(false) {} |
| 361 ~BackPointerCppSideConnection() override {} | 340 ~BackPointerCppSideConnection() override {} |
| 362 | 341 |
| 363 // js_to_cpp::CppSide: | 342 // js_to_cpp::CppSide: |
| 364 void StartTest() override { js_side_->BackPointer(BuildSampleEchoArgs()); } | 343 void StartTest() override { js_side_->BackPointer(BuildSampleEchoArgs()); } |
| 365 | 344 |
| 366 void BackPointerResponse(js_to_cpp::EchoArgsListPtr list) override { | 345 void BackPointerResponse(js_to_cpp::blink::EchoArgsListPtr list) override { |
| 367 CheckCorruptedEchoArgsList(list); | 346 CheckCorruptedEchoArgsList(list); |
| 368 } | 347 } |
| 369 | 348 |
| 370 void TestFinished() override { | 349 void TestFinished() override { |
| 371 termination_seen_ = true; | 350 termination_seen_ = true; |
| 372 run_loop()->Quit(); | 351 testing::ExitRunLoop(); |
| 373 } | 352 } |
| 374 | 353 |
| 375 bool DidSucceed() { | 354 bool DidSucceed() { return termination_seen_; } |
| 376 return termination_seen_; | 355 |
| 356 private: | |
| 357 bool termination_seen_; | |
| 358 }; | |
| 359 | |
| 360 class JsToCppTest : public ::testing::Test { | |
| 361 public: | |
| 362 JsToCppTest() : page_holder_(DummyPageHolder::Create()) { | |
| 363 page_holder_->GetPage().GetSettings().SetScriptEnabled(true); | |
| 364 } | |
| 365 | |
| 366 void RunTest(CppSideConnection* cpp_side) { | |
| 367 js_to_cpp::blink::CppSidePtr cpp_side_ptr; | |
| 368 cpp_side->Bind(MakeRequest(&cpp_side_ptr)); | |
| 369 | |
| 370 js_to_cpp::blink::JsSidePtr js_side_ptr; | |
| 371 auto js_side_request = MakeRequest(&js_side_ptr); | |
| 372 js_side_ptr->SetCppSide(std::move(cpp_side_ptr)); | |
| 373 cpp_side->set_js_side(std::move(js_side_ptr)); | |
| 374 | |
| 375 ScriptState* script_state = | |
| 376 ToScriptStateForMainWorld(&page_holder_->GetFrame()); | |
| 377 ASSERT_TRUE(script_state); | |
| 378 | |
| 379 ScriptState::Scope scope(script_state); | |
| 380 ExecuteScript(MojoBindingsScriptPath()); | |
| 381 ExecuteScript(TestBindingsScriptPath()); | |
| 382 | |
| 383 v8::Local<v8::Value> start_fn = ExecuteScript(TestScriptPath()); | |
| 384 ASSERT_FALSE(start_fn.IsEmpty()); | |
| 385 ASSERT_TRUE(start_fn->IsFunction()); | |
| 386 v8::Local<v8::Object> global_proxy = script_state->GetContext()->Global(); | |
| 387 v8::Local<v8::Value> args[1] = { | |
| 388 ToV8(MojoHandle::Create( | |
| 389 mojo::ScopedHandle::From(js_side_request.PassMessagePipe())), | |
| 390 global_proxy, script_state->GetIsolate())}; | |
| 391 V8ScriptRunner::CallFunction(start_fn.As<v8::Function>(), | |
| 392 &page_holder_->GetDocument(), global_proxy, 1, | |
| 393 args, script_state->GetIsolate()); | |
| 394 testing::EnterRunLoop(); | |
| 395 } | |
| 396 | |
| 397 protected: | |
| 398 v8::Local<v8::Value> ExecuteScript(const String& script_path) { | |
| 399 RefPtr<SharedBuffer> script_src = testing::ReadFromFile(script_path); | |
| 400 return page_holder_->GetFrame() | |
| 401 .GetScriptController() | |
| 402 .ExecuteScriptInMainWorldAndReturnValue( | |
| 403 ScriptSourceCode(String(script_src->Data(), script_src->size()))); | |
| 377 } | 404 } |
| 378 | 405 |
| 379 private: | 406 private: |
| 380 bool termination_seen_; | 407 std::unique_ptr<DummyPageHolder> page_holder_; |
| 381 DISALLOW_COPY_AND_ASSIGN(BackPointerCppSideConnection); | |
| 382 }; | |
| 383 | |
| 384 } // namespace | |
| 385 | |
| 386 class JsToCppTest : public testing::Test { | |
| 387 public: | |
| 388 JsToCppTest() {} | |
| 389 | |
| 390 void RunTest(const std::string& test, CppSideConnection* cpp_side) { | |
| 391 cpp_side->set_run_loop(&run_loop_); | |
| 392 | |
| 393 js_to_cpp::JsSidePtr js_side; | |
| 394 auto js_side_proxy = MakeRequest(&js_side); | |
| 395 | |
| 396 cpp_side->set_js_side(js_side.get()); | |
| 397 js_to_cpp::CppSidePtr cpp_side_ptr; | |
| 398 cpp_side->Bind(MakeRequest(&cpp_side_ptr)); | |
| 399 | |
| 400 js_side->SetCppSide(std::move(cpp_side_ptr)); | |
| 401 | |
| 402 #ifdef V8_USE_EXTERNAL_STARTUP_DATA | |
| 403 gin::V8Initializer::LoadV8Snapshot(); | |
| 404 gin::V8Initializer::LoadV8Natives(); | |
| 405 #endif | |
| 406 | |
| 407 gin::IsolateHolder::Initialize(gin::IsolateHolder::kStrictMode, | |
| 408 gin::IsolateHolder::kStableV8Extras, | |
| 409 gin::ArrayBufferAllocator::SharedInstance()); | |
| 410 gin::IsolateHolder instance(base::ThreadTaskRunnerHandle::Get()); | |
| 411 MojoRunnerDelegate delegate; | |
| 412 gin::ShellRunner runner(&delegate, instance.isolate()); | |
| 413 delegate.Start(&runner, js_side_proxy.PassMessagePipe().release().value(), | |
| 414 test); | |
| 415 | |
| 416 run_loop_.Run(); | |
| 417 } | |
| 418 | |
| 419 private: | |
| 420 base::ShadowingAtExitManager at_exit_; | |
| 421 base::test::ScopedTaskEnvironment scoped_task_environment_; | |
| 422 base::RunLoop run_loop_; | |
| 423 | |
| 424 DISALLOW_COPY_AND_ASSIGN(JsToCppTest); | |
| 425 }; | 408 }; |
| 426 | 409 |
| 427 TEST_F(JsToCppTest, Ping) { | 410 TEST_F(JsToCppTest, Ping) { |
| 428 PingCppSideConnection cpp_side_connection; | 411 PingCppSideConnection cpp_side_connection; |
| 429 RunTest("mojo/edk/js/tests/js_to_cpp_tests", &cpp_side_connection); | 412 RunTest(&cpp_side_connection); |
| 430 EXPECT_TRUE(cpp_side_connection.DidSucceed()); | 413 EXPECT_TRUE(cpp_side_connection.DidSucceed()); |
| 431 } | 414 } |
| 432 | 415 |
| 433 TEST_F(JsToCppTest, Echo) { | 416 TEST_F(JsToCppTest, Echo) { |
| 434 EchoCppSideConnection cpp_side_connection; | 417 EchoCppSideConnection cpp_side_connection; |
| 435 RunTest("mojo/edk/js/tests/js_to_cpp_tests", &cpp_side_connection); | 418 RunTest(&cpp_side_connection); |
| 436 EXPECT_TRUE(cpp_side_connection.DidSucceed()); | 419 EXPECT_TRUE(cpp_side_connection.DidSucceed()); |
| 437 } | 420 } |
| 438 | 421 |
| 439 TEST_F(JsToCppTest, BitFlip) { | 422 TEST_F(JsToCppTest, BitFlip) { |
| 440 // These tests generate a lot of expected validation errors. Suppress logging. | 423 // These tests generate a lot of expected validation errors. Suppress logging. |
| 441 mojo::internal::ScopedSuppressValidationErrorLoggingForTests log_suppression; | 424 mojo::internal::ScopedSuppressValidationErrorLoggingForTests log_suppression; |
| 442 | 425 |
| 443 BitFlipCppSideConnection cpp_side_connection; | 426 BitFlipCppSideConnection cpp_side_connection; |
| 444 RunTest("mojo/edk/js/tests/js_to_cpp_tests", &cpp_side_connection); | 427 RunTest(&cpp_side_connection); |
| 445 EXPECT_TRUE(cpp_side_connection.DidSucceed()); | 428 EXPECT_TRUE(cpp_side_connection.DidSucceed()); |
| 446 } | 429 } |
| 447 | 430 |
| 448 TEST_F(JsToCppTest, BackPointer) { | 431 TEST_F(JsToCppTest, BackPointer) { |
| 449 // These tests generate a lot of expected validation errors. Suppress logging. | 432 // These tests generate a lot of expected validation errors. Suppress logging. |
| 450 mojo::internal::ScopedSuppressValidationErrorLoggingForTests log_suppression; | 433 mojo::internal::ScopedSuppressValidationErrorLoggingForTests log_suppression; |
| 451 | 434 |
| 452 BackPointerCppSideConnection cpp_side_connection; | 435 BackPointerCppSideConnection cpp_side_connection; |
| 453 RunTest("mojo/edk/js/tests/js_to_cpp_tests", &cpp_side_connection); | 436 RunTest(&cpp_side_connection); |
| 454 EXPECT_TRUE(cpp_side_connection.DidSucceed()); | 437 EXPECT_TRUE(cpp_side_connection.DidSucceed()); |
| 455 } | 438 } |
| 456 | 439 |
| 457 } // namespace js | 440 } // namespace |
| 458 } // namespace edk | 441 } // namespace blink |
| 459 } // namespace mojo | |
| OLD | NEW |