Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(150)

Side by Side Diff: third_party/WebKit/Source/core/mojo/tests/JsToCppTest.cpp

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

Powered by Google App Engine
This is Rietveld 408576698