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 "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 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 CheckMessagePipe(arg.message_handle().get().value()); | 139 CheckMessagePipe(arg.message_handle().get().value()); |
140 } | 140 } |
141 | 141 |
142 void CheckSampleEchoArgsList(const js_to_cpp::EchoArgsList& list) { | 142 void CheckSampleEchoArgsList(const js_to_cpp::EchoArgsList& list) { |
143 if (list.is_null()) | 143 if (list.is_null()) |
144 return; | 144 return; |
145 CheckSampleEchoArgs(list.item()); | 145 CheckSampleEchoArgs(list.item()); |
146 CheckSampleEchoArgsList(list.next()); | 146 CheckSampleEchoArgsList(list.next()); |
147 } | 147 } |
148 | 148 |
| 149 // More forgiving checks are needed in the face of potentially corrupt |
| 150 // messages. The values don't matter so long as all accesses are within |
| 151 // bounds. |
149 void CheckCorruptedString(const mojo::String& arg) { | 152 void CheckCorruptedString(const mojo::String& arg) { |
150 // The values don't matter so long as all accesses are within bounds. | |
151 if (arg.is_null()) | 153 if (arg.is_null()) |
152 return; | 154 return; |
153 for (size_t i = 0; i < arg.size(); ++i) | 155 for (size_t i = 0; i < arg.size(); ++i) |
154 g_waste_accumulator += arg[i]; | 156 g_waste_accumulator += arg[i]; |
155 } | 157 } |
156 | 158 |
157 void CheckCorruptedStringArray(const mojo::Array<mojo::String>& string_array) { | 159 void CheckCorruptedStringArray(const mojo::Array<mojo::String>& string_array) { |
158 if (string_array.is_null()) | 160 if (string_array.is_null()) |
159 return; | 161 return; |
160 for (size_t i = 0; i < string_array.size(); ++i) | 162 for (size_t i = 0; i < string_array.size(); ++i) |
161 CheckCorruptedString(string_array[i]); | 163 CheckCorruptedString(string_array[i]); |
162 } | 164 } |
163 | 165 |
| 166 void CheckCorruptedDataPipe(MojoHandle data_pipe_handle) { |
| 167 unsigned char buffer[100]; |
| 168 uint32_t buffer_size = static_cast<uint32_t>(sizeof(buffer)); |
| 169 MojoResult result = MojoReadData( |
| 170 data_pipe_handle, buffer, &buffer_size, MOJO_READ_DATA_FLAG_NONE); |
| 171 if (result != MOJO_RESULT_OK) |
| 172 return; |
| 173 for (uint32_t i = 0; i < buffer_size; ++i) |
| 174 g_waste_accumulator += buffer[i]; |
| 175 } |
| 176 |
| 177 void CheckCorruptedMessagePipe(MojoHandle message_pipe_handle) { |
| 178 unsigned char buffer[100]; |
| 179 uint32_t buffer_size = static_cast<uint32_t>(sizeof(buffer)); |
| 180 MojoResult result = MojoReadMessage( |
| 181 message_pipe_handle, buffer, &buffer_size, 0, 0, 0); |
| 182 if (result != MOJO_RESULT_OK) |
| 183 return; |
| 184 for (uint32_t i = 0; i < buffer_size; ++i) |
| 185 g_waste_accumulator += buffer[i]; |
| 186 } |
| 187 |
164 void CheckCorruptedEchoArgs(const js_to_cpp::EchoArgs& arg) { | 188 void CheckCorruptedEchoArgs(const js_to_cpp::EchoArgs& arg) { |
165 if (arg.is_null()) | 189 if (arg.is_null()) |
166 return; | 190 return; |
167 CheckCorruptedString(arg.name()); | 191 CheckCorruptedString(arg.name()); |
168 CheckCorruptedStringArray(arg.string_array()); | 192 CheckCorruptedStringArray(arg.string_array()); |
169 if (arg.data_handle().is_valid()) | 193 if (arg.data_handle().is_valid()) |
170 CheckDataPipe(arg.data_handle().get().value()); | 194 CheckCorruptedDataPipe(arg.data_handle().get().value()); |
171 if (arg.message_handle().is_valid()) | 195 if (arg.message_handle().is_valid()) |
172 CheckMessagePipe(arg.message_handle().get().value()); | 196 CheckCorruptedMessagePipe(arg.message_handle().get().value()); |
173 } | 197 } |
174 | 198 |
175 void CheckCorruptedEchoArgsList(const js_to_cpp::EchoArgsList& list) { | 199 void CheckCorruptedEchoArgsList(const js_to_cpp::EchoArgsList& list) { |
176 if (list.is_null()) | 200 if (list.is_null()) |
177 return; | 201 return; |
178 CheckCorruptedEchoArgs(list.item()); | 202 CheckCorruptedEchoArgs(list.item()); |
179 CheckCorruptedEchoArgsList(list.next()); | 203 CheckCorruptedEchoArgsList(list.next()); |
180 } | 204 } |
181 | 205 |
182 // Base Provider implementation class. It's expected that tests subclass and | 206 // Base Provider implementation class. It's expected that tests subclass and |
183 // override the appropriate Provider functions. When test is done quit the | 207 // override the appropriate Provider functions. When test is done quit the |
184 // run_loop(). | 208 // run_loop(). |
185 class CppSideConnection : public js_to_cpp::CppSide { | 209 class CppSideConnection : public js_to_cpp::CppSide { |
186 public: | 210 public: |
187 CppSideConnection() : run_loop_(NULL), js_side_(NULL) { | 211 CppSideConnection() : |
| 212 run_loop_(NULL), |
| 213 js_side_(NULL), |
| 214 mishandled_messages_(0) { |
188 } | 215 } |
189 virtual ~CppSideConnection() {} | 216 virtual ~CppSideConnection() {} |
190 | 217 |
191 void set_run_loop(base::RunLoop* run_loop) { run_loop_ = run_loop; } | 218 void set_run_loop(base::RunLoop* run_loop) { run_loop_ = run_loop; } |
192 base::RunLoop* run_loop() { return run_loop_; } | 219 base::RunLoop* run_loop() { return run_loop_; } |
193 | 220 |
194 void set_js_side(js_to_cpp::JsSide* js_side) { js_side_ = js_side; } | 221 void set_js_side(js_to_cpp::JsSide* js_side) { js_side_ = js_side; } |
195 js_to_cpp::JsSide* js_side() { return js_side_; } | 222 js_to_cpp::JsSide* js_side() { return js_side_; } |
196 | 223 |
197 // js_to_cpp::CppSide: | 224 // js_to_cpp::CppSide: |
198 virtual void StartTest() OVERRIDE { | 225 virtual void StartTest() OVERRIDE { |
199 NOTREACHED(); | 226 NOTREACHED(); |
200 } | 227 } |
201 | 228 |
202 virtual void TestFinished() OVERRIDE { | 229 virtual void TestFinished() OVERRIDE { |
203 NOTREACHED(); | 230 NOTREACHED(); |
204 } | 231 } |
205 | 232 |
206 virtual void PingResponse() OVERRIDE { | 233 virtual void PingResponse() OVERRIDE { |
207 NOTREACHED(); | 234 mishandled_messages_ += 1; |
208 } | 235 } |
209 | 236 |
210 virtual void EchoResponse(const js_to_cpp::EchoArgsList& list) OVERRIDE { | 237 virtual void EchoResponse(const js_to_cpp::EchoArgsList& list) OVERRIDE { |
211 NOTREACHED(); | 238 mishandled_messages_ += 1; |
212 } | 239 } |
213 | 240 |
214 virtual void BitFlipResponse(const js_to_cpp::EchoArgsList& list) OVERRIDE { | 241 virtual void BitFlipResponse(const js_to_cpp::EchoArgsList& list) OVERRIDE { |
215 NOTREACHED(); | 242 mishandled_messages_ += 1; |
216 } | 243 } |
217 | 244 |
218 virtual void BackPointerResponse( | 245 virtual void BackPointerResponse( |
219 const js_to_cpp::EchoArgsList& list) OVERRIDE { | 246 const js_to_cpp::EchoArgsList& list) OVERRIDE { |
220 NOTREACHED(); | 247 mishandled_messages_ += 1; |
221 } | 248 } |
| 249 |
222 protected: | 250 protected: |
223 base::RunLoop* run_loop_; | 251 base::RunLoop* run_loop_; |
224 js_to_cpp::JsSide* js_side_; | 252 js_to_cpp::JsSide* js_side_; |
| 253 int mishandled_messages_; |
225 | 254 |
226 private: | 255 private: |
227 DISALLOW_COPY_AND_ASSIGN(CppSideConnection); | 256 DISALLOW_COPY_AND_ASSIGN(CppSideConnection); |
228 }; | 257 }; |
229 | 258 |
230 // Trivial test to verify a message sent from JS is received. | 259 // Trivial test to verify a message sent from JS is received. |
231 class PingCppSideConnection : public CppSideConnection { | 260 class PingCppSideConnection : public CppSideConnection { |
232 public: | 261 public: |
233 PingCppSideConnection() : got_message_(false) {} | 262 PingCppSideConnection() : got_message_(false) {} |
234 virtual ~PingCppSideConnection() {} | 263 virtual ~PingCppSideConnection() {} |
235 | 264 |
236 // js_to_cpp::CppSide: | 265 // js_to_cpp::CppSide: |
237 virtual void StartTest() OVERRIDE { | 266 virtual void StartTest() OVERRIDE { |
238 js_side_->Ping(); | 267 js_side_->Ping(); |
239 } | 268 } |
240 | 269 |
241 virtual void PingResponse() OVERRIDE { | 270 virtual void PingResponse() OVERRIDE { |
242 got_message_ = true; | 271 got_message_ = true; |
243 run_loop()->Quit(); | 272 run_loop()->Quit(); |
244 } | 273 } |
245 | 274 |
246 bool DidSucceed() { | 275 bool DidSucceed() { |
247 return got_message_; | 276 return got_message_ && !mishandled_messages_; |
248 } | 277 } |
249 | 278 |
250 private: | 279 private: |
251 bool got_message_; | 280 bool got_message_; |
252 DISALLOW_COPY_AND_ASSIGN(PingCppSideConnection); | 281 DISALLOW_COPY_AND_ASSIGN(PingCppSideConnection); |
253 }; | 282 }; |
254 | 283 |
255 // Test that parameters are passed with correct values. | 284 // Test that parameters are passed with correct values. |
256 class EchoCppSideConnection : public CppSideConnection { | 285 class EchoCppSideConnection : public CppSideConnection { |
257 public: | 286 public: |
(...skipping 19 matching lines...) Expand all Loading... |
277 EXPECT_EQ(std::string("going"), special_arg.name().To<std::string>()); | 306 EXPECT_EQ(std::string("going"), special_arg.name().To<std::string>()); |
278 CheckSampleEchoArgsList(list.next()); | 307 CheckSampleEchoArgsList(list.next()); |
279 } | 308 } |
280 | 309 |
281 virtual void TestFinished() OVERRIDE { | 310 virtual void TestFinished() OVERRIDE { |
282 termination_seen_ = true; | 311 termination_seen_ = true; |
283 run_loop()->Quit(); | 312 run_loop()->Quit(); |
284 } | 313 } |
285 | 314 |
286 bool DidSucceed() { | 315 bool DidSucceed() { |
287 return termination_seen_ && message_count_ == kExpectedMessageCount; | 316 return termination_seen_ && |
| 317 !mishandled_messages_ && |
| 318 message_count_ == kExpectedMessageCount; |
288 } | 319 } |
289 | 320 |
290 private: | 321 private: |
291 static const int kExpectedMessageCount = 10; | 322 static const int kExpectedMessageCount = 10; |
292 int message_count_; | 323 int message_count_; |
293 bool termination_seen_; | 324 bool termination_seen_; |
294 DISALLOW_COPY_AND_ASSIGN(EchoCppSideConnection); | 325 DISALLOW_COPY_AND_ASSIGN(EchoCppSideConnection); |
295 }; | 326 }; |
296 | 327 |
297 // Test that corrupted messages don't wreak havoc. | 328 // Test that corrupted messages don't wreak havoc. |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
423 if (IsRunningOnIsolatedBot()) | 454 if (IsRunningOnIsolatedBot()) |
424 return; | 455 return; |
425 | 456 |
426 BackPointerCppSideConnection cpp_side_connection; | 457 BackPointerCppSideConnection cpp_side_connection; |
427 RunTest("mojo/apps/js/test/js_to_cpp_unittest", &cpp_side_connection); | 458 RunTest("mojo/apps/js/test/js_to_cpp_unittest", &cpp_side_connection); |
428 EXPECT_TRUE(cpp_side_connection.DidSucceed()); | 459 EXPECT_TRUE(cpp_side_connection.DidSucceed()); |
429 } | 460 } |
430 | 461 |
431 } // namespace js | 462 } // namespace js |
432 } // namespace mojo | 463 } // namespace mojo |
OLD | NEW |