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 <stdio.h> | 5 #include <stdio.h> |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 #include <utility> | 9 #include <utility> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "mojo/public/cpp/bindings/binding.h" | 12 #include "mojo/public/cpp/bindings/binding.h" |
13 #include "mojo/public/cpp/bindings/interface_ptr.h" | 13 #include "mojo/public/cpp/bindings/interface_ptr.h" |
14 #include "mojo/public/cpp/bindings/lib/connector.h" | 14 #include "mojo/public/cpp/bindings/lib/connector.h" |
15 #include "mojo/public/cpp/bindings/lib/message_header_validator.h" | 15 #include "mojo/public/cpp/bindings/lib/message_header_validator.h" |
16 #include "mojo/public/cpp/bindings/lib/router.h" | 16 #include "mojo/public/cpp/bindings/lib/router.h" |
17 #include "mojo/public/cpp/bindings/lib/validation_errors.h" | 17 #include "mojo/public/cpp/bindings/lib/validation_errors.h" |
18 #include "mojo/public/cpp/bindings/message.h" | 18 #include "mojo/public/cpp/bindings/message.h" |
19 #include "mojo/public/cpp/bindings/tests/validation_test_input_parser.h" | 19 #include "mojo/public/cpp/bindings/tests/validation_test_input_parser.h" |
20 #include "mojo/public/cpp/environment/environment.h" | |
21 #include "mojo/public/cpp/system/macros.h" | 20 #include "mojo/public/cpp/system/macros.h" |
22 #include "mojo/public/cpp/system/message_pipe.h" | 21 #include "mojo/public/cpp/system/message_pipe.h" |
23 #include "mojo/public/cpp/test_support/test_support.h" | 22 #include "mojo/public/cpp/test_support/test_support.h" |
24 #include "mojo/public/cpp/utility/run_loop.h" | 23 #include "mojo/public/cpp/utility/run_loop.h" |
25 #include "mojo/public/interfaces/bindings/tests/validation_test_interfaces.mojom
.h" | 24 #include "mojo/public/interfaces/bindings/tests/validation_test_interfaces.mojom
.h" |
26 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
27 | 26 |
28 namespace mojo { | 27 namespace mojo { |
29 | 28 |
30 using internal::MessageValidator; | 29 using internal::MessageValidator; |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
186 } | 185 } |
187 } | 186 } |
188 | 187 |
189 class DummyMessageReceiver : public MessageReceiver { | 188 class DummyMessageReceiver : public MessageReceiver { |
190 public: | 189 public: |
191 bool Accept(Message* message) override { | 190 bool Accept(Message* message) override { |
192 return true; // Any message is OK. | 191 return true; // Any message is OK. |
193 } | 192 } |
194 }; | 193 }; |
195 | 194 |
196 class ValidationTest : public testing::Test { | 195 class ValidationIntegrationTest : public testing::Test { |
197 public: | |
198 ~ValidationTest() override {} | |
199 | |
200 private: | |
201 Environment env_; | |
202 }; | |
203 | |
204 class ValidationIntegrationTest : public ValidationTest { | |
205 public: | 196 public: |
206 ValidationIntegrationTest() : test_message_receiver_(nullptr) {} | 197 ValidationIntegrationTest() : test_message_receiver_(nullptr) {} |
207 | 198 |
208 ~ValidationIntegrationTest() override {} | 199 ~ValidationIntegrationTest() override {} |
209 | 200 |
210 void SetUp() override { | 201 void SetUp() override { |
211 ScopedMessagePipeHandle tester_endpoint; | 202 ScopedMessagePipeHandle tester_endpoint; |
212 ASSERT_EQ(MOJO_RESULT_OK, | 203 ASSERT_EQ(MOJO_RESULT_OK, |
213 CreateMessagePipe(nullptr, &tester_endpoint, &testee_endpoint_)); | 204 CreateMessagePipe(nullptr, &tester_endpoint, &testee_endpoint_)); |
214 test_message_receiver_ = | 205 test_message_receiver_ = |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
270 public: | 261 public: |
271 explicit FailingValidator(ValidationError err) : err_(err) {} | 262 explicit FailingValidator(ValidationError err) : err_(err) {} |
272 ValidationError Validate(const Message* message, std::string* err) override { | 263 ValidationError Validate(const Message* message, std::string* err) override { |
273 return err_; | 264 return err_; |
274 } | 265 } |
275 | 266 |
276 private: | 267 private: |
277 ValidationError err_; | 268 ValidationError err_; |
278 }; | 269 }; |
279 | 270 |
280 TEST_F(ValidationTest, InputParser) { | 271 TEST(ValidationTest, InputParser) { |
281 { | 272 { |
282 // The parser, as well as Append() defined above, assumes that this code is | 273 // The parser, as well as Append() defined above, assumes that this code is |
283 // running on a little-endian platform. Test whether that is true. | 274 // running on a little-endian platform. Test whether that is true. |
284 uint16_t x = 1; | 275 uint16_t x = 1; |
285 ASSERT_EQ(1, *(reinterpret_cast<char*>(&x))); | 276 ASSERT_EQ(1, *(reinterpret_cast<char*>(&x))); |
286 } | 277 } |
287 { | 278 { |
288 // Test empty input. | 279 // Test empty input. |
289 std::string input; | 280 std::string input; |
290 std::vector<uint8_t> expected; | 281 std::vector<uint8_t> expected; |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
374 nullptr}; | 365 nullptr}; |
375 | 366 |
376 for (size_t i = 0; error_inputs[i]; ++i) { | 367 for (size_t i = 0; error_inputs[i]; ++i) { |
377 std::vector<uint8_t> expected; | 368 std::vector<uint8_t> expected; |
378 if (!TestInputParser(error_inputs[i], false, expected, 0)) | 369 if (!TestInputParser(error_inputs[i], false, expected, 0)) |
379 ADD_FAILURE() << "Unexpected test result for: " << error_inputs[i]; | 370 ADD_FAILURE() << "Unexpected test result for: " << error_inputs[i]; |
380 } | 371 } |
381 } | 372 } |
382 } | 373 } |
383 | 374 |
384 TEST_F(ValidationTest, Conformance) { | 375 TEST(ValidationTest, Conformance) { |
385 DummyMessageReceiver dummy_receiver; | 376 DummyMessageReceiver dummy_receiver; |
386 MessageValidatorList validators; | 377 MessageValidatorList validators; |
387 validators.push_back(std::unique_ptr<MessageValidator>( | 378 validators.push_back(std::unique_ptr<MessageValidator>( |
388 new mojo::internal::MessageHeaderValidator)); | 379 new mojo::internal::MessageHeaderValidator)); |
389 validators.push_back(std::unique_ptr<MessageValidator>( | 380 validators.push_back(std::unique_ptr<MessageValidator>( |
390 new ConformanceTestInterface::RequestValidator_)); | 381 new ConformanceTestInterface::RequestValidator_)); |
391 | 382 |
392 RunValidationTests("conformance_", validators, &dummy_receiver); | 383 RunValidationTests("conformance_", validators, &dummy_receiver); |
393 } | 384 } |
394 | 385 |
395 // This test is similar to Conformance test but its goal is specifically | 386 // This test is similar to Conformance test but its goal is specifically |
396 // do bounds-check testing of message validation. For example we test the | 387 // do bounds-check testing of message validation. For example we test the |
397 // detection of off-by-one errors in method ordinals. | 388 // detection of off-by-one errors in method ordinals. |
398 TEST_F(ValidationTest, BoundsCheck) { | 389 TEST(ValidationTest, BoundsCheck) { |
399 DummyMessageReceiver dummy_receiver; | 390 DummyMessageReceiver dummy_receiver; |
400 MessageValidatorList validators; | 391 MessageValidatorList validators; |
401 validators.push_back(std::unique_ptr<MessageValidator>( | 392 validators.push_back(std::unique_ptr<MessageValidator>( |
402 new mojo::internal::MessageHeaderValidator)); | 393 new mojo::internal::MessageHeaderValidator)); |
403 validators.push_back(std::unique_ptr<MessageValidator>( | 394 validators.push_back(std::unique_ptr<MessageValidator>( |
404 new BoundsCheckTestInterface::RequestValidator_)); | 395 new BoundsCheckTestInterface::RequestValidator_)); |
405 | 396 |
406 RunValidationTests("boundscheck_", validators, &dummy_receiver); | 397 RunValidationTests("boundscheck_", validators, &dummy_receiver); |
407 } | 398 } |
408 | 399 |
409 // This test is similar to the Conformance test but for responses. | 400 // This test is similar to the Conformance test but for responses. |
410 TEST_F(ValidationTest, ResponseConformance) { | 401 TEST(ValidationTest, ResponseConformance) { |
411 DummyMessageReceiver dummy_receiver; | 402 DummyMessageReceiver dummy_receiver; |
412 MessageValidatorList validators; | 403 MessageValidatorList validators; |
413 validators.push_back(std::unique_ptr<MessageValidator>( | 404 validators.push_back(std::unique_ptr<MessageValidator>( |
414 new mojo::internal::MessageHeaderValidator)); | 405 new mojo::internal::MessageHeaderValidator)); |
415 validators.push_back(std::unique_ptr<MessageValidator>( | 406 validators.push_back(std::unique_ptr<MessageValidator>( |
416 new ConformanceTestInterface::ResponseValidator_)); | 407 new ConformanceTestInterface::ResponseValidator_)); |
417 | 408 |
418 RunValidationTests("resp_conformance_", validators, &dummy_receiver); | 409 RunValidationTests("resp_conformance_", validators, &dummy_receiver); |
419 } | 410 } |
420 | 411 |
421 // This test is similar to the BoundsCheck test but for responses. | 412 // This test is similar to the BoundsCheck test but for responses. |
422 TEST_F(ValidationTest, ResponseBoundsCheck) { | 413 TEST(ValidationTest, ResponseBoundsCheck) { |
423 DummyMessageReceiver dummy_receiver; | 414 DummyMessageReceiver dummy_receiver; |
424 MessageValidatorList validators; | 415 MessageValidatorList validators; |
425 validators.push_back(std::unique_ptr<MessageValidator>( | 416 validators.push_back(std::unique_ptr<MessageValidator>( |
426 new mojo::internal::MessageHeaderValidator)); | 417 new mojo::internal::MessageHeaderValidator)); |
427 validators.push_back(std::unique_ptr<MessageValidator>( | 418 validators.push_back(std::unique_ptr<MessageValidator>( |
428 new BoundsCheckTestInterface::ResponseValidator_)); | 419 new BoundsCheckTestInterface::ResponseValidator_)); |
429 | 420 |
430 RunValidationTests("resp_boundscheck_", validators, &dummy_receiver); | 421 RunValidationTests("resp_boundscheck_", validators, &dummy_receiver); |
431 } | 422 } |
432 | 423 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
467 new mojo::internal::MessageHeaderValidator)); | 458 new mojo::internal::MessageHeaderValidator)); |
468 validators.push_back(std::unique_ptr<MessageValidator>( | 459 validators.push_back(std::unique_ptr<MessageValidator>( |
469 new typename IntegrationTestInterface::RequestValidator_)); | 460 new typename IntegrationTestInterface::RequestValidator_)); |
470 | 461 |
471 RunValidationTests("integration_intf_rqst", validators, | 462 RunValidationTests("integration_intf_rqst", validators, |
472 test_message_receiver()); | 463 test_message_receiver()); |
473 RunValidationTests("integration_msghdr", validators, test_message_receiver()); | 464 RunValidationTests("integration_msghdr", validators, test_message_receiver()); |
474 } | 465 } |
475 | 466 |
476 // Test pointer validation (specifically, that the encoded offset is 32-bit) | 467 // Test pointer validation (specifically, that the encoded offset is 32-bit) |
477 TEST_F(ValidationTest, ValidateEncodedPointer) { | 468 TEST(ValidationTest, ValidateEncodedPointer) { |
478 uint64_t offset; | 469 uint64_t offset; |
479 | 470 |
480 offset = 0ULL; | 471 offset = 0ULL; |
481 EXPECT_TRUE(mojo::internal::ValidateEncodedPointer(&offset)); | 472 EXPECT_TRUE(mojo::internal::ValidateEncodedPointer(&offset)); |
482 | 473 |
483 offset = 1ULL; | 474 offset = 1ULL; |
484 EXPECT_TRUE(mojo::internal::ValidateEncodedPointer(&offset)); | 475 EXPECT_TRUE(mojo::internal::ValidateEncodedPointer(&offset)); |
485 | 476 |
486 // offset must be <= 32-bit. | 477 // offset must be <= 32-bit. |
487 offset = std::numeric_limits<uint32_t>::max() + 1ULL; | 478 offset = std::numeric_limits<uint32_t>::max() + 1ULL; |
488 EXPECT_FALSE(mojo::internal::ValidateEncodedPointer(&offset)); | 479 EXPECT_FALSE(mojo::internal::ValidateEncodedPointer(&offset)); |
489 } | 480 } |
490 | 481 |
491 TEST_F(ValidationTest, RunValidatorsOnMessageTest) { | 482 TEST(ValidationTest, RunValidatorsOnMessageTest) { |
492 Message msg; | 483 Message msg; |
493 mojo::internal::MessageValidatorList validators; | 484 mojo::internal::MessageValidatorList validators; |
494 | 485 |
495 validators.push_back(std::unique_ptr<MessageValidator>( | 486 validators.push_back(std::unique_ptr<MessageValidator>( |
496 new mojo::internal::PassThroughValidator)); | 487 new mojo::internal::PassThroughValidator)); |
497 EXPECT_EQ(ValidationError::NONE, | 488 EXPECT_EQ(ValidationError::NONE, |
498 RunValidatorsOnMessage(validators, &msg, nullptr)); | 489 RunValidatorsOnMessage(validators, &msg, nullptr)); |
499 | 490 |
500 validators.push_back(std::unique_ptr<MessageValidator>( | 491 validators.push_back(std::unique_ptr<MessageValidator>( |
501 new FailingValidator(ValidationError::MESSAGE_HEADER_INVALID_FLAGS))); | 492 new FailingValidator(ValidationError::MESSAGE_HEADER_INVALID_FLAGS))); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
538 static_cast<StructWithEnum::EnumWithin>(2))); | 529 static_cast<StructWithEnum::EnumWithin>(2))); |
539 EXPECT_TRUE(StructWithEnum::EnumWithin_IsValidValue( | 530 EXPECT_TRUE(StructWithEnum::EnumWithin_IsValidValue( |
540 static_cast<StructWithEnum::EnumWithin>(3))); | 531 static_cast<StructWithEnum::EnumWithin>(3))); |
541 EXPECT_FALSE(StructWithEnum::EnumWithin_IsValidValue( | 532 EXPECT_FALSE(StructWithEnum::EnumWithin_IsValidValue( |
542 static_cast<StructWithEnum::EnumWithin>(4))); | 533 static_cast<StructWithEnum::EnumWithin>(4))); |
543 } | 534 } |
544 | 535 |
545 } // namespace | 536 } // namespace |
546 } // namespace test | 537 } // namespace test |
547 } // namespace mojo | 538 } // namespace mojo |
OLD | NEW |