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

Side by Side Diff: mojo/edk/js/tests/js_to_cpp_tests.cc

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
« no previous file with comments | « mojo/edk/js/tests/js_to_cpp.mojom ('k') | mojo/edk/js/tests/js_to_cpp_tests.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 <stddef.h>
6 #include <stdint.h>
7
8 #include <string>
9 #include <utility>
10
11 #include "base/at_exit.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"
26 #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"
29 #include "testing/gtest/include/gtest/gtest.h"
30
31 namespace mojo {
32 namespace edk {
33 namespace js {
34
35 // Global value updated by some checks to prevent compilers from optimizing
36 // reads out of existence.
37 uint32_t g_waste_accumulator = 0;
38
39 namespace {
40
41 // Negative numbers with different values in each byte, the last of
42 // which can survive promotion to double and back.
43 const int8_t kExpectedInt8Value = -65;
44 const int16_t kExpectedInt16Value = -16961;
45 const int32_t kExpectedInt32Value = -1145258561;
46 const int64_t kExpectedInt64Value = -77263311946305LL;
47
48 // Positive numbers with different values in each byte, the last of
49 // which can survive promotion to double and back.
50 const uint8_t kExpectedUInt8Value = 65;
51 const uint16_t kExpectedUInt16Value = 16961;
52 const uint32_t kExpectedUInt32Value = 1145258561;
53 const uint64_t kExpectedUInt64Value = 77263311946305LL;
54
55 // Double/float values, including special case constants.
56 const double kExpectedDoubleVal = 3.14159265358979323846;
57 const double kExpectedDoubleInf = std::numeric_limits<double>::infinity();
58 const double kExpectedDoubleNan = std::numeric_limits<double>::quiet_NaN();
59 const float kExpectedFloatVal = static_cast<float>(kExpectedDoubleVal);
60 const float kExpectedFloatInf = std::numeric_limits<float>::infinity();
61 const float kExpectedFloatNan = std::numeric_limits<float>::quiet_NaN();
62
63 // NaN has the property that it is not equal to itself.
64 #define EXPECT_NAN(x) EXPECT_NE(x, x)
65
66 void CheckDataPipe(ScopedDataPipeConsumerHandle data_pipe_handle) {
67 std::string buffer;
68 bool result = common::BlockingCopyToString(std::move(data_pipe_handle),
69 &buffer);
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 }
76
77 void CheckMessagePipe(MessagePipeHandle message_pipe_handle) {
78 unsigned char buffer[100];
79 uint32_t buffer_size = static_cast<uint32_t>(sizeof(buffer));
80 MojoResult result = Wait(message_pipe_handle, MOJO_HANDLE_SIGNAL_READABLE);
81 EXPECT_EQ(MOJO_RESULT_OK, result);
82 result = ReadMessageRaw(
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
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.emplace("coming");
108 args->string_array.emplace(3);
109 (*args->string_array)[0] = "one";
110 (*args->string_array)[1] = "two";
111 (*args->string_array)[2] = "three";
112 return args;
113 }
114
115 void CheckSampleEchoArgs(js_to_cpp::EchoArgsPtr arg) {
116 EXPECT_EQ(kExpectedInt64Value, arg->si64);
117 EXPECT_EQ(kExpectedInt32Value, arg->si32);
118 EXPECT_EQ(kExpectedInt16Value, arg->si16);
119 EXPECT_EQ(kExpectedInt8Value, arg->si8);
120 EXPECT_EQ(kExpectedUInt64Value, arg->ui64);
121 EXPECT_EQ(kExpectedUInt32Value, arg->ui32);
122 EXPECT_EQ(kExpectedUInt16Value, arg->ui16);
123 EXPECT_EQ(kExpectedUInt8Value, arg->ui8);
124 EXPECT_EQ(kExpectedFloatVal, arg->float_val);
125 EXPECT_EQ(kExpectedFloatInf, arg->float_inf);
126 EXPECT_NAN(arg->float_nan);
127 EXPECT_EQ(kExpectedDoubleVal, arg->double_val);
128 EXPECT_EQ(kExpectedDoubleInf, arg->double_inf);
129 EXPECT_NAN(arg->double_nan);
130 EXPECT_EQ(std::string("coming"), *arg->name);
131 EXPECT_EQ(std::string("one"), (*arg->string_array)[0]);
132 EXPECT_EQ(std::string("two"), (*arg->string_array)[1]);
133 EXPECT_EQ(std::string("three"), (*arg->string_array)[2]);
134 CheckDataPipe(std::move(arg->data_handle));
135 CheckMessagePipe(arg->message_handle.get());
136 }
137
138 void CheckSampleEchoArgsList(const js_to_cpp::EchoArgsListPtr& list) {
139 if (list.is_null())
140 return;
141 CheckSampleEchoArgs(std::move(list->item));
142 CheckSampleEchoArgsList(list->next);
143 }
144
145 // 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
147 // bounds.
148 void CheckCorruptedString(const std::string& arg) {
149 for (size_t i = 0; i < arg.size(); ++i)
150 g_waste_accumulator += arg[i];
151 }
152
153 void CheckCorruptedString(const base::Optional<std::string>& arg) {
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)
162 return;
163 for (size_t i = 0; i < string_array->size(); ++i)
164 CheckCorruptedString((*string_array)[i]);
165 }
166
167 void CheckCorruptedDataPipe(MojoHandle data_pipe_handle) {
168 unsigned char buffer[100];
169 uint32_t buffer_size = static_cast<uint32_t>(sizeof(buffer));
170 MojoResult result = MojoReadData(
171 data_pipe_handle, buffer, &buffer_size, MOJO_READ_DATA_FLAG_NONE);
172 if (result != MOJO_RESULT_OK)
173 return;
174 for (uint32_t i = 0; i < buffer_size; ++i)
175 g_waste_accumulator += buffer[i];
176 }
177
178 void CheckCorruptedMessagePipe(MojoHandle message_pipe_handle) {
179 unsigned char buffer[100];
180 uint32_t buffer_size = static_cast<uint32_t>(sizeof(buffer));
181 MojoResult result = MojoReadMessage(
182 message_pipe_handle, buffer, &buffer_size, 0, 0, 0);
183 if (result != MOJO_RESULT_OK)
184 return;
185 for (uint32_t i = 0; i < buffer_size; ++i)
186 g_waste_accumulator += buffer[i];
187 }
188
189 void CheckCorruptedEchoArgs(const js_to_cpp::EchoArgsPtr& arg) {
190 if (arg.is_null())
191 return;
192 CheckCorruptedString(arg->name);
193 CheckCorruptedStringArray(arg->string_array);
194 if (arg->data_handle.is_valid())
195 CheckCorruptedDataPipe(arg->data_handle.get().value());
196 if (arg->message_handle.is_valid())
197 CheckCorruptedMessagePipe(arg->message_handle.get().value());
198 }
199
200 void CheckCorruptedEchoArgsList(const js_to_cpp::EchoArgsListPtr& list) {
201 if (list.is_null())
202 return;
203 CheckCorruptedEchoArgs(list->item);
204 CheckCorruptedEchoArgsList(list->next);
205 }
206
207 // Base Provider implementation class. It's expected that tests subclass and
208 // override the appropriate Provider functions. When test is done quit the
209 // run_loop().
210 class CppSideConnection : public js_to_cpp::CppSide {
211 public:
212 CppSideConnection()
213 : run_loop_(nullptr),
214 js_side_(nullptr),
215 mishandled_messages_(0),
216 binding_(this) {}
217 ~CppSideConnection() override {}
218
219 void set_run_loop(base::RunLoop* run_loop) { run_loop_ = run_loop; }
220 base::RunLoop* run_loop() { return run_loop_; }
221
222 void set_js_side(js_to_cpp::JsSide* js_side) { js_side_ = js_side; }
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));
227 // Keep the pipe open even after validation errors.
228 binding_.EnableTestingMode();
229 }
230
231 // js_to_cpp::CppSide:
232 void StartTest() override { NOTREACHED(); }
233
234 void TestFinished() override { NOTREACHED(); }
235
236 void PingResponse() override { mishandled_messages_ += 1; }
237
238 void EchoResponse(js_to_cpp::EchoArgsListPtr list) override {
239 mishandled_messages_ += 1;
240 }
241
242 void BitFlipResponse(
243 js_to_cpp::EchoArgsListPtr list,
244 js_to_cpp::ForTestingAssociatedPtrInfo not_used) override {
245 mishandled_messages_ += 1;
246 }
247
248 void BackPointerResponse(js_to_cpp::EchoArgsListPtr list) override {
249 mishandled_messages_ += 1;
250 }
251
252 protected:
253 base::RunLoop* run_loop_;
254 js_to_cpp::JsSide* js_side_;
255 int mishandled_messages_;
256 mojo::Binding<js_to_cpp::CppSide> binding_;
257
258 private:
259 DISALLOW_COPY_AND_ASSIGN(CppSideConnection);
260 };
261
262 // Trivial test to verify a message sent from JS is received.
263 class PingCppSideConnection : public CppSideConnection {
264 public:
265 PingCppSideConnection() : got_message_(false) {}
266 ~PingCppSideConnection() override {}
267
268 // js_to_cpp::CppSide:
269 void StartTest() override { js_side_->Ping(); }
270
271 void PingResponse() override {
272 got_message_ = true;
273 run_loop()->Quit();
274 }
275
276 bool DidSucceed() {
277 return got_message_ && !mishandled_messages_;
278 }
279
280 private:
281 bool got_message_;
282 DISALLOW_COPY_AND_ASSIGN(PingCppSideConnection);
283 };
284
285 // Test that parameters are passed with correct values.
286 class EchoCppSideConnection : public CppSideConnection {
287 public:
288 EchoCppSideConnection() :
289 message_count_(0),
290 termination_seen_(false) {
291 }
292 ~EchoCppSideConnection() override {}
293
294 // js_to_cpp::CppSide:
295 void StartTest() override {
296 js_side_->Echo(kExpectedMessageCount, BuildSampleEchoArgs());
297 }
298
299 void EchoResponse(js_to_cpp::EchoArgsListPtr list) override {
300 const js_to_cpp::EchoArgsPtr& special_arg = list->item;
301 message_count_ += 1;
302 EXPECT_EQ(-1, special_arg->si64);
303 EXPECT_EQ(-1, special_arg->si32);
304 EXPECT_EQ(-1, special_arg->si16);
305 EXPECT_EQ(-1, special_arg->si8);
306 EXPECT_EQ(std::string("going"), *special_arg->name);
307 CheckSampleEchoArgsList(list->next);
308 }
309
310 void TestFinished() override {
311 termination_seen_ = true;
312 run_loop()->Quit();
313 }
314
315 bool DidSucceed() {
316 return termination_seen_ &&
317 !mishandled_messages_ &&
318 message_count_ == kExpectedMessageCount;
319 }
320
321 private:
322 static const int kExpectedMessageCount = 10;
323 int message_count_;
324 bool termination_seen_;
325 DISALLOW_COPY_AND_ASSIGN(EchoCppSideConnection);
326 };
327
328 // Test that corrupted messages don't wreak havoc.
329 class BitFlipCppSideConnection : public CppSideConnection {
330 public:
331 BitFlipCppSideConnection() : termination_seen_(false) {}
332 ~BitFlipCppSideConnection() override {}
333
334 // js_to_cpp::CppSide:
335 void StartTest() override { js_side_->BitFlip(BuildSampleEchoArgs()); }
336
337 void BitFlipResponse(
338 js_to_cpp::EchoArgsListPtr list,
339 js_to_cpp::ForTestingAssociatedPtrInfo not_used) override {
340 CheckCorruptedEchoArgsList(list);
341 }
342
343 void TestFinished() override {
344 termination_seen_ = true;
345 run_loop()->Quit();
346 }
347
348 bool DidSucceed() {
349 return termination_seen_;
350 }
351
352 private:
353 bool termination_seen_;
354 DISALLOW_COPY_AND_ASSIGN(BitFlipCppSideConnection);
355 };
356
357 // Test that severely random messages don't wreak havoc.
358 class BackPointerCppSideConnection : public CppSideConnection {
359 public:
360 BackPointerCppSideConnection() : termination_seen_(false) {}
361 ~BackPointerCppSideConnection() override {}
362
363 // js_to_cpp::CppSide:
364 void StartTest() override { js_side_->BackPointer(BuildSampleEchoArgs()); }
365
366 void BackPointerResponse(js_to_cpp::EchoArgsListPtr list) override {
367 CheckCorruptedEchoArgsList(list);
368 }
369
370 void TestFinished() override {
371 termination_seen_ = true;
372 run_loop()->Quit();
373 }
374
375 bool DidSucceed() {
376 return termination_seen_;
377 }
378
379 private:
380 bool termination_seen_;
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 };
426
427 TEST_F(JsToCppTest, Ping) {
428 PingCppSideConnection cpp_side_connection;
429 RunTest("mojo/edk/js/tests/js_to_cpp_tests", &cpp_side_connection);
430 EXPECT_TRUE(cpp_side_connection.DidSucceed());
431 }
432
433 TEST_F(JsToCppTest, Echo) {
434 EchoCppSideConnection cpp_side_connection;
435 RunTest("mojo/edk/js/tests/js_to_cpp_tests", &cpp_side_connection);
436 EXPECT_TRUE(cpp_side_connection.DidSucceed());
437 }
438
439 TEST_F(JsToCppTest, BitFlip) {
440 // These tests generate a lot of expected validation errors. Suppress logging.
441 mojo::internal::ScopedSuppressValidationErrorLoggingForTests log_suppression;
442
443 BitFlipCppSideConnection cpp_side_connection;
444 RunTest("mojo/edk/js/tests/js_to_cpp_tests", &cpp_side_connection);
445 EXPECT_TRUE(cpp_side_connection.DidSucceed());
446 }
447
448 TEST_F(JsToCppTest, BackPointer) {
449 // These tests generate a lot of expected validation errors. Suppress logging.
450 mojo::internal::ScopedSuppressValidationErrorLoggingForTests log_suppression;
451
452 BackPointerCppSideConnection cpp_side_connection;
453 RunTest("mojo/edk/js/tests/js_to_cpp_tests", &cpp_side_connection);
454 EXPECT_TRUE(cpp_side_connection.DidSucceed());
455 }
456
457 } // namespace js
458 } // namespace edk
459 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/edk/js/tests/js_to_cpp.mojom ('k') | mojo/edk/js/tests/js_to_cpp_tests.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698