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

Side by Side Diff: mojo/apps/js/test/js_to_cpp_unittest.cc

Issue 267343006: Pass data down pipes in mojo fuzzing tests. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase on clean branch. Created 6 years, 7 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | mojo/apps/js/test/js_to_cpp_unittest.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "base/file_util.h" 5 #include "base/file_util.h"
6 #include "base/files/file_path.h" 6 #include "base/files/file_path.h"
7 #include "base/message_loop/message_loop.h" 7 #include "base/message_loop/message_loop.h"
8 #include "base/run_loop.h" 8 #include "base/run_loop.h"
9 #include "base/strings/utf_string_conversions.h" 9 #include "base/strings/utf_string_conversions.h"
10 #include "gin/public/isolate_holder.h" 10 #include "gin/public/isolate_holder.h"
11 #include "mojo/apps/js/mojo_runner_delegate.h" 11 #include "mojo/apps/js/mojo_runner_delegate.h"
12 #include "mojo/apps/js/test/js_to_cpp.mojom.h" 12 #include "mojo/apps/js/test/js_to_cpp.mojom.h"
13 #include "mojo/common/common_type_converters.h" 13 #include "mojo/common/common_type_converters.h"
14 #include "mojo/common/test/test_utils.h" 14 #include "mojo/common/test/test_utils.h"
15 #include "mojo/public/cpp/bindings/allocation_scope.h" 15 #include "mojo/public/cpp/bindings/allocation_scope.h"
16 #include "mojo/public/cpp/environment/environment.h" 16 #include "mojo/public/cpp/environment/environment.h"
17 #include "mojo/public/cpp/system/core.h" 17 #include "mojo/public/cpp/system/core.h"
18 #include "mojo/public/cpp/system/macros.h" 18 #include "mojo/public/cpp/system/macros.h"
19 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
20 20
21 namespace mojo { 21 namespace mojo {
22 namespace js { 22 namespace js {
23
24 // Global value updated by some checks to prevent compilers from optimizing
25 // reads out of existence.
26 uint32 g_waste_accumulator = 0;
27
23 namespace { 28 namespace {
24 29
25 // Negative numbers with different values in each byte, the last of 30 // Negative numbers with different values in each byte, the last of
26 // which can survive promotion to double and back. 31 // which can survive promotion to double and back.
27 const int8 kExpectedInt8Value = -65; 32 const int8 kExpectedInt8Value = -65;
28 const int16 kExpectedInt16Value = -16961; 33 const int16 kExpectedInt16Value = -16961;
29 const int32 kExpectedInt32Value = -1145258561; 34 const int32 kExpectedInt32Value = -1145258561;
30 const int64 kExpectedInt64Value = -77263311946305LL; 35 const int64 kExpectedInt64Value = -77263311946305LL;
31 36
32 // Positive numbers with different values in each byte, the last of 37 // Positive numbers with different values in each byte, the last of
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
101 EXPECT_NAN(arg.float_nan()); 106 EXPECT_NAN(arg.float_nan());
102 EXPECT_EQ(kExpectedDoubleVal, arg.double_val()); 107 EXPECT_EQ(kExpectedDoubleVal, arg.double_val());
103 EXPECT_EQ(kExpectedDoubleInf, arg.double_inf()); 108 EXPECT_EQ(kExpectedDoubleInf, arg.double_inf());
104 EXPECT_NAN(arg.double_nan()); 109 EXPECT_NAN(arg.double_nan());
105 EXPECT_EQ(std::string("coming"), arg.name().To<std::string>()); 110 EXPECT_EQ(std::string("coming"), arg.name().To<std::string>());
106 EXPECT_EQ(std::string("one"), arg.string_array()[0].To<std::string>()); 111 EXPECT_EQ(std::string("one"), arg.string_array()[0].To<std::string>());
107 EXPECT_EQ(std::string("two"), arg.string_array()[1].To<std::string>()); 112 EXPECT_EQ(std::string("two"), arg.string_array()[1].To<std::string>());
108 EXPECT_EQ(std::string("three"), arg.string_array()[2].To<std::string>()); 113 EXPECT_EQ(std::string("three"), arg.string_array()[2].To<std::string>());
109 } 114 }
110 115
116 void CheckDataPipe(MojoHandle data_pipe_handle) {
117 char buffer[100];
118 uint32_t buffer_size = static_cast<uint32_t>(sizeof(buffer));
119 MojoResult result = MojoReadData(
120 data_pipe_handle, buffer, &buffer_size, MOJO_READ_DATA_FLAG_NONE);
121 EXPECT_EQ(MOJO_RESULT_OK, result);
122 EXPECT_EQ(64u, buffer_size);
123 for (int i = 0; i < 64; ++i) {
124 EXPECT_EQ(i, buffer[i]);
125 }
126 }
127
128 void CheckCorruptedString(const mojo::String& arg) {
129 // The values don't matter so long as all accesses are within bounds.
130 std::string name = arg.To<std::string>();
131 for (size_t i = 0; i < name.length(); ++i)
132 g_waste_accumulator += name[i];
133 }
134
135 void CheckCorruptedStringArray(const mojo::Array<mojo::String>& string_array) {
136 for (size_t i = 0; i < string_array.size(); ++i)
137 CheckCorruptedString(string_array[i]);
138 }
139
111 // Base Provider implementation class. It's expected that tests subclass and 140 // Base Provider implementation class. It's expected that tests subclass and
112 // override the appropriate Provider functions. When test is done quit the 141 // override the appropriate Provider functions. When test is done quit the
113 // run_loop(). 142 // run_loop().
114 class CppSideConnection : public js_to_cpp::CppSide { 143 class CppSideConnection : public js_to_cpp::CppSide {
115 public: 144 public:
116 CppSideConnection() : run_loop_(NULL), js_side_(NULL) { 145 CppSideConnection() : run_loop_(NULL), js_side_(NULL) {
117 } 146 }
118 virtual ~CppSideConnection() {} 147 virtual ~CppSideConnection() {}
119 148
120 void set_run_loop(base::RunLoop* run_loop) { run_loop_ = run_loop; } 149 void set_run_loop(base::RunLoop* run_loop) { run_loop_ = run_loop; }
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
196 225
197 virtual void EchoResponse(const js_to_cpp::EchoArgs& arg1, 226 virtual void EchoResponse(const js_to_cpp::EchoArgs& arg1,
198 const js_to_cpp::EchoArgs& arg2) OVERRIDE { 227 const js_to_cpp::EchoArgs& arg2) OVERRIDE {
199 message_count_ += 1; 228 message_count_ += 1;
200 CheckSampleEchoArgs(arg1); 229 CheckSampleEchoArgs(arg1);
201 EXPECT_EQ(-1, arg2.si64()); 230 EXPECT_EQ(-1, arg2.si64());
202 EXPECT_EQ(-1, arg2.si32()); 231 EXPECT_EQ(-1, arg2.si32());
203 EXPECT_EQ(-1, arg2.si16()); 232 EXPECT_EQ(-1, arg2.si16());
204 EXPECT_EQ(-1, arg2.si8()); 233 EXPECT_EQ(-1, arg2.si8());
205 EXPECT_EQ(std::string("going"), arg2.name().To<std::string>()); 234 EXPECT_EQ(std::string("going"), arg2.name().To<std::string>());
235 CheckDataPipe(arg2.data_handle().get().value());
206 } 236 }
207 237
208 virtual void TestFinished() OVERRIDE { 238 virtual void TestFinished() OVERRIDE {
209 termination_seen_ = true; 239 termination_seen_ = true;
210 run_loop()->Quit(); 240 run_loop()->Quit();
211 } 241 }
212 242
213 bool DidSucceed() { 243 bool DidSucceed() {
214 return termination_seen_ && message_count_ == kExpectedMessageCount; 244 return termination_seen_ && message_count_ == kExpectedMessageCount;
215 } 245 }
(...skipping 10 matching lines...) Expand all
226 public: 256 public:
227 explicit BitFlipCppSideConnection() : termination_seen_(false) {} 257 explicit BitFlipCppSideConnection() : termination_seen_(false) {}
228 virtual ~BitFlipCppSideConnection() {} 258 virtual ~BitFlipCppSideConnection() {}
229 259
230 // js_to_cpp::CppSide: 260 // js_to_cpp::CppSide:
231 virtual void StartTest() OVERRIDE { 261 virtual void StartTest() OVERRIDE {
232 AllocationScope scope; 262 AllocationScope scope;
233 js_side_->BitFlip(BuildSampleEchoArgs()); 263 js_side_->BitFlip(BuildSampleEchoArgs());
234 } 264 }
235 265
236 virtual void BitFlipResponse(const js_to_cpp::EchoArgs& arg1) OVERRIDE { 266 virtual void BitFlipResponse(const js_to_cpp::EchoArgs& arg) OVERRIDE {
237 // TODO(tsepez): How to check, may be corrupt in various ways. 267 if (arg.is_null())
268 return;
269 CheckCorruptedString(arg.name());
270 CheckCorruptedStringArray(arg.string_array());
271 if (arg.data_handle().is_valid())
272 CheckDataPipe(arg.data_handle().get().value());
238 } 273 }
239 274
240 virtual void TestFinished() OVERRIDE { 275 virtual void TestFinished() OVERRIDE {
241 termination_seen_ = true; 276 termination_seen_ = true;
242 run_loop()->Quit(); 277 run_loop()->Quit();
243 } 278 }
244 279
245 bool DidSucceed() { 280 bool DidSucceed() {
246 return termination_seen_; 281 return termination_seen_;
247 } 282 }
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
308 if (IsRunningOnIsolatedBot()) 343 if (IsRunningOnIsolatedBot())
309 return; 344 return;
310 345
311 BitFlipCppSideConnection cpp_side_connection; 346 BitFlipCppSideConnection cpp_side_connection;
312 RunTest("mojo/apps/js/test/js_to_cpp_unittest", &cpp_side_connection); 347 RunTest("mojo/apps/js/test/js_to_cpp_unittest", &cpp_side_connection);
313 EXPECT_TRUE(cpp_side_connection.DidSucceed()); 348 EXPECT_TRUE(cpp_side_connection.DidSucceed());
314 } 349 }
315 350
316 } // namespace js 351 } // namespace js
317 } // namespace mojo 352 } // namespace mojo
OLDNEW
« no previous file with comments | « no previous file | mojo/apps/js/test/js_to_cpp_unittest.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698