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

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

Issue 250713003: Test sending corrupt mojo messages back from javascript. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase. 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 | « mojo/apps/js/test/js_to_cpp.mojom ('k') | 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"
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
54 const base::FilePath test_file_path( 54 const base::FilePath test_file_path(
55 test::GetFilePathForJSResource( 55 test::GetFilePathForJSResource(
56 "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom")); 56 "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom"));
57 if (!base::PathExists(test_file_path)) { 57 if (!base::PathExists(test_file_path)) {
58 LOG(WARNING) << "Mojom binding files don't exist. Skipping the test."; 58 LOG(WARNING) << "Mojom binding files don't exist. Skipping the test.";
59 return true; 59 return true;
60 } 60 }
61 return false; 61 return false;
62 } 62 }
63 63
64 // NOTE: Callers will need to have established an AllocationScope, or you're
65 // gonna have a bad time.
66 js_to_cpp::EchoArgs BuildSampleEchoArgs() {
67 js_to_cpp::EchoArgs::Builder builder;
68 builder.set_si64(kExpectedInt64Value);
69 builder.set_si32(kExpectedInt32Value);
70 builder.set_si16(kExpectedInt16Value);
71 builder.set_si8(kExpectedInt8Value);
72 builder.set_ui64(kExpectedUInt64Value);
73 builder.set_ui32(kExpectedUInt32Value);
74 builder.set_ui16(kExpectedUInt16Value);
75 builder.set_ui8(kExpectedUInt8Value);
76 builder.set_float_val(kExpectedFloatVal);
77 builder.set_float_inf(kExpectedFloatInf);
78 builder.set_float_nan(kExpectedFloatNan);
79 builder.set_double_val(kExpectedDoubleVal);
80 builder.set_double_inf(kExpectedDoubleInf);
81 builder.set_double_nan(kExpectedDoubleNan);
82 builder.set_name("coming");
83 mojo::Array<mojo::String>::Builder string_array(3);
84 string_array[0] = "one";
85 string_array[1] = "two";
86 string_array[2] = "three";
87 builder.set_string_array(string_array.Finish());
88 return builder.Finish();
89 }
90
91 void CheckSampleEchoArgs(const js_to_cpp::EchoArgs& arg) {
92 EXPECT_EQ(kExpectedInt64Value, arg.si64());
93 EXPECT_EQ(kExpectedInt32Value, arg.si32());
94 EXPECT_EQ(kExpectedInt16Value, arg.si16());
95 EXPECT_EQ(kExpectedInt8Value, arg.si8());
96 EXPECT_EQ(kExpectedUInt64Value, arg.ui64());
97 EXPECT_EQ(kExpectedUInt32Value, arg.ui32());
98 EXPECT_EQ(kExpectedUInt16Value, arg.ui16());
99 EXPECT_EQ(kExpectedUInt8Value, arg.ui8());
100 EXPECT_EQ(kExpectedFloatVal, arg.float_val());
101 EXPECT_EQ(kExpectedFloatInf, arg.float_inf());
102 EXPECT_NAN(arg.float_nan());
103 EXPECT_EQ(kExpectedDoubleVal, arg.double_val());
104 EXPECT_EQ(kExpectedDoubleInf, arg.double_inf());
105 EXPECT_NAN(arg.double_nan());
106 EXPECT_EQ(std::string("coming"), arg.name().To<std::string>());
107 EXPECT_EQ(std::string("one"), arg.string_array()[0].To<std::string>());
108 EXPECT_EQ(std::string("two"), arg.string_array()[1].To<std::string>());
109 EXPECT_EQ(std::string("three"), arg.string_array()[2].To<std::string>());
110 }
111
64 // Base Provider implementation class. It's expected that tests subclass and 112 // Base Provider implementation class. It's expected that tests subclass and
65 // override the appropriate Provider functions. When test is done quit the 113 // override the appropriate Provider functions. When test is done quit the
66 // run_loop(). 114 // run_loop().
67 class CppSideConnection : public js_to_cpp::CppSide { 115 class CppSideConnection : public js_to_cpp::CppSide {
68 public: 116 public:
69 CppSideConnection() : run_loop_(NULL), client_(NULL) { 117 CppSideConnection() : run_loop_(NULL), client_(NULL) {
70 } 118 }
71 virtual ~CppSideConnection() {} 119 virtual ~CppSideConnection() {}
72 120
73 void set_run_loop(base::RunLoop* run_loop) { run_loop_ = run_loop; } 121 void set_run_loop(base::RunLoop* run_loop) { run_loop_ = run_loop; }
74 base::RunLoop* run_loop() { return run_loop_; } 122 base::RunLoop* run_loop() { return run_loop_; }
75 123
76 void set_client(js_to_cpp::JsSide* client) { client_ = client; } 124 void set_client(js_to_cpp::JsSide* client) { client_ = client; }
77 js_to_cpp::JsSide* client() { return client_; } 125 js_to_cpp::JsSide* client() { return client_; }
78 126
79 // js_to_cpp::CppSide: 127 // js_to_cpp::CppSide:
80 virtual void StartTest() OVERRIDE { 128 virtual void StartTest() OVERRIDE {
81 NOTREACHED(); 129 NOTREACHED();
82 } 130 }
83 131
132 virtual void TestFinished() OVERRIDE {
133 NOTREACHED();
134 }
135
84 virtual void PingResponse() OVERRIDE { 136 virtual void PingResponse() OVERRIDE {
85 NOTREACHED(); 137 NOTREACHED();
86 } 138 }
87 139
88 virtual void EchoResponse(const js_to_cpp::EchoArgs& arg1, 140 virtual void EchoResponse(const js_to_cpp::EchoArgs& arg1,
89 const js_to_cpp::EchoArgs& arg2) OVERRIDE { 141 const js_to_cpp::EchoArgs& arg2) OVERRIDE {
90 NOTREACHED(); 142 NOTREACHED();
91 } 143 }
92 144
145 virtual void BitFlipResponse(const js_to_cpp::EchoArgs& arg1) OVERRIDE {
146 NOTREACHED();
147 }
148
93 protected: 149 protected:
94 base::RunLoop* run_loop_; 150 base::RunLoop* run_loop_;
95 js_to_cpp::JsSide* client_; 151 js_to_cpp::JsSide* client_;
96 152
97 private: 153 private:
98 Environment environment; 154 Environment environment;
99 DISALLOW_COPY_AND_ASSIGN(CppSideConnection); 155 DISALLOW_COPY_AND_ASSIGN(CppSideConnection);
100 }; 156 };
101 157
102 // Trivial test to verify a message sent from JS is received. 158 // Trivial test to verify a message sent from JS is received.
(...skipping 17 matching lines...) Expand all
120 } 176 }
121 177
122 private: 178 private:
123 bool got_message_; 179 bool got_message_;
124 DISALLOW_COPY_AND_ASSIGN(PingCppSideConnection); 180 DISALLOW_COPY_AND_ASSIGN(PingCppSideConnection);
125 }; 181 };
126 182
127 // Test that parameters are passed with correct values. 183 // Test that parameters are passed with correct values.
128 class EchoCppSideConnection : public CppSideConnection { 184 class EchoCppSideConnection : public CppSideConnection {
129 public: 185 public:
130 explicit EchoCppSideConnection() : message_count_(0) {} 186 explicit EchoCppSideConnection() :
187 message_count_(0),
188 termination_seen_(false) {
189 }
131 virtual ~EchoCppSideConnection() {} 190 virtual ~EchoCppSideConnection() {}
132 191
133 // js_to_cpp::CppSide: 192 // js_to_cpp::CppSide:
134 virtual void StartTest() OVERRIDE { 193 virtual void StartTest() OVERRIDE {
135 AllocationScope scope; 194 AllocationScope scope;
136 js_to_cpp::EchoArgs::Builder builder; 195 client_->Echo(kExpectedMessageCount, BuildSampleEchoArgs());
137 builder.set_si64(kExpectedInt64Value);
138 builder.set_si32(kExpectedInt32Value);
139 builder.set_si16(kExpectedInt16Value);
140 builder.set_si8(kExpectedInt8Value);
141 builder.set_ui64(kExpectedUInt64Value);
142 builder.set_ui32(kExpectedUInt32Value);
143 builder.set_ui16(kExpectedUInt16Value);
144 builder.set_ui8(kExpectedUInt8Value);
145 builder.set_float_val(kExpectedFloatVal);
146 builder.set_float_inf(kExpectedFloatInf);
147 builder.set_float_nan(kExpectedFloatNan);
148 builder.set_double_val(kExpectedDoubleVal);
149 builder.set_double_inf(kExpectedDoubleInf);
150 builder.set_double_nan(kExpectedDoubleNan);
151 builder.set_name("coming");
152 mojo::Array<mojo::String>::Builder string_array(3);
153 string_array[0] = "one";
154 string_array[1] = "two";
155 string_array[2] = "three";
156 builder.set_string_array(string_array.Finish());
157 client_->Echo(builder.Finish());
158 } 196 }
159 197
160 virtual void EchoResponse(const js_to_cpp::EchoArgs& arg1, 198 virtual void EchoResponse(const js_to_cpp::EchoArgs& arg1,
161 const js_to_cpp::EchoArgs& arg2) OVERRIDE { 199 const js_to_cpp::EchoArgs& arg2) OVERRIDE {
162 EXPECT_EQ(kExpectedInt64Value, arg1.si64()); 200 message_count_ += 1;
163 EXPECT_EQ(kExpectedInt32Value, arg1.si32()); 201 CheckSampleEchoArgs(arg1);
164 EXPECT_EQ(kExpectedInt16Value, arg1.si16());
165 EXPECT_EQ(kExpectedInt8Value, arg1.si8());
166 EXPECT_EQ(kExpectedUInt64Value, arg1.ui64());
167 EXPECT_EQ(kExpectedUInt32Value, arg1.ui32());
168 EXPECT_EQ(kExpectedUInt16Value, arg1.ui16());
169 EXPECT_EQ(kExpectedUInt8Value, arg1.ui8());
170 EXPECT_EQ(kExpectedFloatVal, arg1.float_val());
171 EXPECT_EQ(kExpectedFloatInf, arg1.float_inf());
172 EXPECT_NAN(arg1.float_nan());
173 EXPECT_EQ(kExpectedDoubleVal, arg1.double_val());
174 EXPECT_EQ(kExpectedDoubleInf, arg1.double_inf());
175 EXPECT_NAN(arg1.double_nan());
176 EXPECT_EQ(std::string("coming"), arg1.name().To<std::string>());
177 EXPECT_EQ(std::string("one"), arg1.string_array()[0].To<std::string>());
178 EXPECT_EQ(std::string("two"), arg1.string_array()[1].To<std::string>());
179 EXPECT_EQ(std::string("three"), arg1.string_array()[2].To<std::string>());
180
181 EXPECT_EQ(-1, arg2.si64()); 202 EXPECT_EQ(-1, arg2.si64());
182 EXPECT_EQ(-1, arg2.si32()); 203 EXPECT_EQ(-1, arg2.si32());
183 EXPECT_EQ(-1, arg2.si16()); 204 EXPECT_EQ(-1, arg2.si16());
184 EXPECT_EQ(-1, arg2.si8()); 205 EXPECT_EQ(-1, arg2.si8());
185 EXPECT_EQ(std::string("going"), arg2.name().To<std::string>()); 206 EXPECT_EQ(std::string("going"), arg2.name().To<std::string>());
207 }
186 208
187 message_count_ += 1; 209 virtual void TestFinished() OVERRIDE {
188 if (message_count_ == kExpectedMessageCount) 210 termination_seen_ = true;
189 run_loop_->Quit(); 211 run_loop()->Quit();
190 } 212 }
191 213
192 bool DidSucceed() { 214 bool DidSucceed() {
193 return message_count_ == kExpectedMessageCount; 215 return termination_seen_ && message_count_ == kExpectedMessageCount;
194 } 216 }
195 217
196 private: 218 private:
197 static const int kExpectedMessageCount = 100; 219 static const int kExpectedMessageCount = 100;
198 int message_count_; 220 int message_count_;
221 bool termination_seen_;
199 DISALLOW_COPY_AND_ASSIGN(EchoCppSideConnection); 222 DISALLOW_COPY_AND_ASSIGN(EchoCppSideConnection);
200 }; 223 };
201 224
225 // Test that corrupted messages don't wreak havoc.
226 class BitFlipCppSideConnection : public CppSideConnection {
227 public:
228 explicit BitFlipCppSideConnection() : termination_seen_(false) {}
229 virtual ~BitFlipCppSideConnection() {}
230
231 // js_to_cpp::CppSide:
232 virtual void StartTest() OVERRIDE {
233 AllocationScope scope;
234 client_->BitFlip(BuildSampleEchoArgs());
235 }
236
237 virtual void BitFlipResponse(const js_to_cpp::EchoArgs& arg1) OVERRIDE {
238 // TODO(tsepez): How to check, may be corrupt in various ways.
239 }
240
241 virtual void TestFinished() OVERRIDE {
242 termination_seen_ = true;
243 run_loop()->Quit();
244 }
245
246 bool DidSucceed() {
247 return termination_seen_;
248 }
249
250 private:
251 bool termination_seen_;
252 DISALLOW_COPY_AND_ASSIGN(BitFlipCppSideConnection);
253 };
254
255 } // namespace
256
202 class JsToCppTest : public testing::Test { 257 class JsToCppTest : public testing::Test {
203 public: 258 public:
204 JsToCppTest() {} 259 JsToCppTest() {}
205 260
206 void RunTest(const std::string& test, CppSideConnection* cpp_side) { 261 void RunTest(const std::string& test, CppSideConnection* cpp_side) {
207 cpp_side->set_run_loop(&run_loop_); 262 cpp_side->set_run_loop(&run_loop_);
208 InterfacePipe<js_to_cpp::CppSide, js_to_cpp::JsSide> pipe; 263 InterfacePipe<js_to_cpp::CppSide, js_to_cpp::JsSide> pipe;
209 RemotePtr<js_to_cpp::JsSide> js_side; 264 RemotePtr<js_to_cpp::JsSide> js_side;
210 js_side.reset(pipe.handle_to_peer.Pass(), cpp_side); 265 js_side.reset(pipe.handle_to_peer.Pass(), cpp_side);
211 cpp_side->set_client(js_side.get()); 266 cpp_side->set_client(js_side.get());
(...skipping 25 matching lines...) Expand all
237 292
238 TEST_F(JsToCppTest, Echo) { 293 TEST_F(JsToCppTest, Echo) {
239 if (IsRunningOnIsolatedBot()) 294 if (IsRunningOnIsolatedBot())
240 return; 295 return;
241 296
242 EchoCppSideConnection cpp_side_connection; 297 EchoCppSideConnection cpp_side_connection;
243 RunTest("mojo/apps/js/test/js_to_cpp_unittest", &cpp_side_connection); 298 RunTest("mojo/apps/js/test/js_to_cpp_unittest", &cpp_side_connection);
244 EXPECT_TRUE(cpp_side_connection.DidSucceed()); 299 EXPECT_TRUE(cpp_side_connection.DidSucceed());
245 } 300 }
246 301
247 } // namespace 302 // TODO(tsepez): Disabled due to http://crbug.com/366797.
303 TEST_F(JsToCppTest, DISABLED_BitFlip) {
304 if (IsRunningOnIsolatedBot())
305 return;
306
307 BitFlipCppSideConnection cpp_side_connection;
308 RunTest("mojo/apps/js/test/js_to_cpp_unittest", &cpp_side_connection);
309 EXPECT_TRUE(cpp_side_connection.DidSucceed());
310 }
311
248 } // namespace js 312 } // namespace js
249 } // namespace mojo 313 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/apps/js/test/js_to_cpp.mojom ('k') | mojo/apps/js/test/js_to_cpp_unittest.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698