| OLD | NEW |
| (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 <stdio.h> | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <string> | |
| 9 #include <vector> | |
| 10 | |
| 11 #include "base/message_loop/message_loop.h" | |
| 12 #include "mojo/message_pump/message_pump_mojo.h" | |
| 13 #include "mojo/public/c/system/macros.h" | |
| 14 #include "mojo/public/cpp/bindings/binding.h" | |
| 15 #include "mojo/public/cpp/bindings/interface_ptr.h" | |
| 16 #include "mojo/public/cpp/bindings/lib/connector.h" | |
| 17 #include "mojo/public/cpp/bindings/lib/filter_chain.h" | |
| 18 #include "mojo/public/cpp/bindings/lib/message_header_validator.h" | |
| 19 #include "mojo/public/cpp/bindings/lib/router.h" | |
| 20 #include "mojo/public/cpp/bindings/lib/validation_errors.h" | |
| 21 #include "mojo/public/cpp/bindings/message.h" | |
| 22 #include "mojo/public/cpp/bindings/tests/validation_test_input_parser.h" | |
| 23 #include "mojo/public/cpp/system/core.h" | |
| 24 #include "mojo/public/cpp/test_support/test_support.h" | |
| 25 #include "mojo/public/interfaces/bindings/tests/validation_test_interfaces.mojom
.h" | |
| 26 #include "testing/gtest/include/gtest/gtest.h" | |
| 27 | |
| 28 namespace mojo { | |
| 29 namespace test { | |
| 30 namespace { | |
| 31 | |
| 32 template <typename T> | |
| 33 void Append(std::vector<uint8_t>* data_vector, T data) { | |
| 34 size_t pos = data_vector->size(); | |
| 35 data_vector->resize(pos + sizeof(T)); | |
| 36 memcpy(&(*data_vector)[pos], &data, sizeof(T)); | |
| 37 } | |
| 38 | |
| 39 bool TestInputParser(const std::string& input, | |
| 40 bool expected_result, | |
| 41 const std::vector<uint8_t>& expected_data, | |
| 42 size_t expected_num_handles) { | |
| 43 std::vector<uint8_t> data; | |
| 44 size_t num_handles; | |
| 45 std::string error_message; | |
| 46 | |
| 47 bool result = | |
| 48 ParseValidationTestInput(input, &data, &num_handles, &error_message); | |
| 49 if (expected_result) { | |
| 50 if (result && error_message.empty() && expected_data == data && | |
| 51 expected_num_handles == num_handles) { | |
| 52 return true; | |
| 53 } | |
| 54 | |
| 55 // Compare with an empty string instead of checking |error_message.empty()|, | |
| 56 // so that the message will be printed out if the two are not equal. | |
| 57 EXPECT_EQ(std::string(), error_message); | |
| 58 EXPECT_EQ(expected_data, data); | |
| 59 EXPECT_EQ(expected_num_handles, num_handles); | |
| 60 return false; | |
| 61 } | |
| 62 | |
| 63 EXPECT_FALSE(error_message.empty()); | |
| 64 return !result && !error_message.empty(); | |
| 65 } | |
| 66 | |
| 67 std::vector<std::string> GetMatchingTests(const std::vector<std::string>& names, | |
| 68 const std::string& prefix) { | |
| 69 const std::string suffix = ".data"; | |
| 70 std::vector<std::string> tests; | |
| 71 for (size_t i = 0; i < names.size(); ++i) { | |
| 72 if (names[i].size() >= suffix.size() && | |
| 73 names[i].substr(0, prefix.size()) == prefix && | |
| 74 names[i].substr(names[i].size() - suffix.size()) == suffix) | |
| 75 tests.push_back(names[i].substr(0, names[i].size() - suffix.size())); | |
| 76 } | |
| 77 return tests; | |
| 78 } | |
| 79 | |
| 80 bool ReadFile(const std::string& path, std::string* result) { | |
| 81 FILE* fp = OpenSourceRootRelativeFile(path.c_str()); | |
| 82 if (!fp) { | |
| 83 ADD_FAILURE() << "File not found: " << path; | |
| 84 return false; | |
| 85 } | |
| 86 fseek(fp, 0, SEEK_END); | |
| 87 size_t size = static_cast<size_t>(ftell(fp)); | |
| 88 if (size == 0) { | |
| 89 result->clear(); | |
| 90 fclose(fp); | |
| 91 return true; | |
| 92 } | |
| 93 fseek(fp, 0, SEEK_SET); | |
| 94 result->resize(size); | |
| 95 size_t size_read = fread(&result->at(0), 1, size, fp); | |
| 96 fclose(fp); | |
| 97 return size == size_read; | |
| 98 } | |
| 99 | |
| 100 bool ReadAndParseDataFile(const std::string& path, | |
| 101 std::vector<uint8_t>* data, | |
| 102 size_t* num_handles) { | |
| 103 std::string input; | |
| 104 if (!ReadFile(path, &input)) | |
| 105 return false; | |
| 106 | |
| 107 std::string error_message; | |
| 108 if (!ParseValidationTestInput(input, data, num_handles, &error_message)) { | |
| 109 ADD_FAILURE() << error_message; | |
| 110 return false; | |
| 111 } | |
| 112 | |
| 113 return true; | |
| 114 } | |
| 115 | |
| 116 bool ReadResultFile(const std::string& path, std::string* result) { | |
| 117 if (!ReadFile(path, result)) | |
| 118 return false; | |
| 119 | |
| 120 // Result files are new-line delimited text files. Remove any CRs. | |
| 121 result->erase(std::remove(result->begin(), result->end(), '\r'), | |
| 122 result->end()); | |
| 123 | |
| 124 // Remove trailing LFs. | |
| 125 size_t pos = result->find_last_not_of('\n'); | |
| 126 if (pos == std::string::npos) | |
| 127 result->clear(); | |
| 128 else | |
| 129 result->resize(pos + 1); | |
| 130 | |
| 131 return true; | |
| 132 } | |
| 133 | |
| 134 std::string GetPath(const std::string& root, const std::string& suffix) { | |
| 135 return "mojo/public/interfaces/bindings/tests/data/validation/" + root + | |
| 136 suffix; | |
| 137 } | |
| 138 | |
| 139 // |message| should be a newly created object. | |
| 140 bool ReadTestCase(const std::string& test, | |
| 141 Message* message, | |
| 142 std::string* expected) { | |
| 143 std::vector<uint8_t> data; | |
| 144 size_t num_handles; | |
| 145 if (!ReadAndParseDataFile(GetPath(test, ".data"), &data, &num_handles) || | |
| 146 !ReadResultFile(GetPath(test, ".expected"), expected)) { | |
| 147 return false; | |
| 148 } | |
| 149 | |
| 150 message->AllocUninitializedData(static_cast<uint32_t>(data.size())); | |
| 151 if (!data.empty()) | |
| 152 memcpy(message->mutable_data(), &data[0], data.size()); | |
| 153 message->mutable_handles()->resize(num_handles); | |
| 154 | |
| 155 return true; | |
| 156 } | |
| 157 | |
| 158 void RunValidationTests(const std::string& prefix, | |
| 159 MessageReceiver* test_message_receiver) { | |
| 160 std::vector<std::string> names = | |
| 161 EnumerateSourceRootRelativeDirectory(GetPath("", "")); | |
| 162 std::vector<std::string> tests = GetMatchingTests(names, prefix); | |
| 163 | |
| 164 for (size_t i = 0; i < tests.size(); ++i) { | |
| 165 Message message; | |
| 166 std::string expected; | |
| 167 ASSERT_TRUE(ReadTestCase(tests[i], &message, &expected)); | |
| 168 | |
| 169 std::string result; | |
| 170 mojo::internal::ValidationErrorObserverForTesting observer; | |
| 171 mojo_ignore_result(test_message_receiver->Accept(&message)); | |
| 172 if (observer.last_error() == mojo::internal::VALIDATION_ERROR_NONE) | |
| 173 result = "PASS"; | |
| 174 else | |
| 175 result = mojo::internal::ValidationErrorToString(observer.last_error()); | |
| 176 | |
| 177 EXPECT_EQ(expected, result) << "failed test: " << tests[i]; | |
| 178 } | |
| 179 } | |
| 180 | |
| 181 class DummyMessageReceiver : public MessageReceiver { | |
| 182 public: | |
| 183 bool Accept(Message* message) override { | |
| 184 return true; // Any message is OK. | |
| 185 } | |
| 186 }; | |
| 187 | |
| 188 using ValidationTest = testing::Test; | |
| 189 | |
| 190 class ValidationIntegrationTest : public ValidationTest { | |
| 191 public: | |
| 192 ValidationIntegrationTest() | |
| 193 : loop_(common::MessagePumpMojo::Create()), | |
| 194 test_message_receiver_(nullptr) {} | |
| 195 | |
| 196 ~ValidationIntegrationTest() override {} | |
| 197 | |
| 198 void SetUp() override { | |
| 199 ScopedMessagePipeHandle tester_endpoint; | |
| 200 ASSERT_EQ(MOJO_RESULT_OK, | |
| 201 CreateMessagePipe(nullptr, &tester_endpoint, &testee_endpoint_)); | |
| 202 test_message_receiver_ = | |
| 203 new TestMessageReceiver(this, tester_endpoint.Pass()); | |
| 204 } | |
| 205 | |
| 206 void TearDown() override { | |
| 207 delete test_message_receiver_; | |
| 208 test_message_receiver_ = nullptr; | |
| 209 | |
| 210 // Make sure that the other end receives the OnConnectionError() | |
| 211 // notification. | |
| 212 PumpMessages(); | |
| 213 } | |
| 214 | |
| 215 MessageReceiver* test_message_receiver() { return test_message_receiver_; } | |
| 216 | |
| 217 ScopedMessagePipeHandle testee_endpoint() { return testee_endpoint_.Pass(); } | |
| 218 | |
| 219 private: | |
| 220 class TestMessageReceiver : public MessageReceiver { | |
| 221 public: | |
| 222 TestMessageReceiver(ValidationIntegrationTest* owner, | |
| 223 ScopedMessagePipeHandle handle) | |
| 224 : owner_(owner), connector_(handle.Pass()) { | |
| 225 connector_.set_enforce_errors_from_incoming_receiver(false); | |
| 226 } | |
| 227 ~TestMessageReceiver() override {} | |
| 228 | |
| 229 bool Accept(Message* message) override { | |
| 230 bool rv = connector_.Accept(message); | |
| 231 owner_->PumpMessages(); | |
| 232 return rv; | |
| 233 } | |
| 234 | |
| 235 public: | |
| 236 ValidationIntegrationTest* owner_; | |
| 237 mojo::internal::Connector connector_; | |
| 238 }; | |
| 239 | |
| 240 void PumpMessages() { loop_.RunUntilIdle(); } | |
| 241 | |
| 242 base::MessageLoop loop_; | |
| 243 TestMessageReceiver* test_message_receiver_; | |
| 244 ScopedMessagePipeHandle testee_endpoint_; | |
| 245 }; | |
| 246 | |
| 247 class IntegrationTestInterfaceImpl : public IntegrationTestInterface { | |
| 248 public: | |
| 249 ~IntegrationTestInterfaceImpl() override {} | |
| 250 | |
| 251 void Method0(BasicStructPtr param0, | |
| 252 const Method0Callback& callback) override { | |
| 253 callback.Run(Array<uint8_t>::New(0u)); | |
| 254 } | |
| 255 }; | |
| 256 | |
| 257 TEST_F(ValidationTest, InputParser) { | |
| 258 { | |
| 259 // The parser, as well as Append() defined above, assumes that this code is | |
| 260 // running on a little-endian platform. Test whether that is true. | |
| 261 uint16_t x = 1; | |
| 262 ASSERT_EQ(1, *(reinterpret_cast<char*>(&x))); | |
| 263 } | |
| 264 { | |
| 265 // Test empty input. | |
| 266 std::string input; | |
| 267 std::vector<uint8_t> expected; | |
| 268 | |
| 269 EXPECT_TRUE(TestInputParser(input, true, expected, 0)); | |
| 270 } | |
| 271 { | |
| 272 // Test input that only consists of comments and whitespaces. | |
| 273 std::string input = " \t // hello world \n\r \t// the answer is 42 "; | |
| 274 std::vector<uint8_t> expected; | |
| 275 | |
| 276 EXPECT_TRUE(TestInputParser(input, true, expected, 0)); | |
| 277 } | |
| 278 { | |
| 279 std::string input = | |
| 280 "[u1]0x10// hello world !! \n\r \t [u2]65535 \n" | |
| 281 "[u4]65536 [u8]0xFFFFFFFFFFFFFFFF 0 0Xff"; | |
| 282 std::vector<uint8_t> expected; | |
| 283 Append(&expected, static_cast<uint8_t>(0x10)); | |
| 284 Append(&expected, static_cast<uint16_t>(65535)); | |
| 285 Append(&expected, static_cast<uint32_t>(65536)); | |
| 286 Append(&expected, static_cast<uint64_t>(0xffffffffffffffff)); | |
| 287 Append(&expected, static_cast<uint8_t>(0)); | |
| 288 Append(&expected, static_cast<uint8_t>(0xff)); | |
| 289 | |
| 290 EXPECT_TRUE(TestInputParser(input, true, expected, 0)); | |
| 291 } | |
| 292 { | |
| 293 std::string input = "[s8]-0x800 [s1]-128\t[s2]+0 [s4]-40"; | |
| 294 std::vector<uint8_t> expected; | |
| 295 Append(&expected, -static_cast<int64_t>(0x800)); | |
| 296 Append(&expected, static_cast<int8_t>(-128)); | |
| 297 Append(&expected, static_cast<int16_t>(0)); | |
| 298 Append(&expected, static_cast<int32_t>(-40)); | |
| 299 | |
| 300 EXPECT_TRUE(TestInputParser(input, true, expected, 0)); | |
| 301 } | |
| 302 { | |
| 303 std::string input = "[b]00001011 [b]10000000 // hello world\r [b]00000000"; | |
| 304 std::vector<uint8_t> expected; | |
| 305 Append(&expected, static_cast<uint8_t>(11)); | |
| 306 Append(&expected, static_cast<uint8_t>(128)); | |
| 307 Append(&expected, static_cast<uint8_t>(0)); | |
| 308 | |
| 309 EXPECT_TRUE(TestInputParser(input, true, expected, 0)); | |
| 310 } | |
| 311 { | |
| 312 std::string input = "[f]+.3e9 [d]-10.03"; | |
| 313 std::vector<uint8_t> expected; | |
| 314 Append(&expected, +.3e9f); | |
| 315 Append(&expected, -10.03); | |
| 316 | |
| 317 EXPECT_TRUE(TestInputParser(input, true, expected, 0)); | |
| 318 } | |
| 319 { | |
| 320 std::string input = "[dist4]foo 0 [dist8]bar 0 [anchr]foo [anchr]bar"; | |
| 321 std::vector<uint8_t> expected; | |
| 322 Append(&expected, static_cast<uint32_t>(14)); | |
| 323 Append(&expected, static_cast<uint8_t>(0)); | |
| 324 Append(&expected, static_cast<uint64_t>(9)); | |
| 325 Append(&expected, static_cast<uint8_t>(0)); | |
| 326 | |
| 327 EXPECT_TRUE(TestInputParser(input, true, expected, 0)); | |
| 328 } | |
| 329 { | |
| 330 std::string input = "// This message has handles! \n[handles]50 [u8]2"; | |
| 331 std::vector<uint8_t> expected; | |
| 332 Append(&expected, static_cast<uint64_t>(2)); | |
| 333 | |
| 334 EXPECT_TRUE(TestInputParser(input, true, expected, 50)); | |
| 335 } | |
| 336 | |
| 337 // Test some failure cases. | |
| 338 { | |
| 339 const char* error_inputs[] = {"/ hello world", | |
| 340 "[u1]x", | |
| 341 "[u2]-1000", | |
| 342 "[u1]0x100", | |
| 343 "[s2]-0x8001", | |
| 344 "[b]1", | |
| 345 "[b]1111111k", | |
| 346 "[dist4]unmatched", | |
| 347 "[anchr]hello [dist8]hello", | |
| 348 "[dist4]a [dist4]a [anchr]a", | |
| 349 "[dist4]a [anchr]a [dist4]a [anchr]a", | |
| 350 "0 [handles]50", | |
| 351 nullptr}; | |
| 352 | |
| 353 for (size_t i = 0; error_inputs[i]; ++i) { | |
| 354 std::vector<uint8_t> expected; | |
| 355 if (!TestInputParser(error_inputs[i], false, expected, 0)) | |
| 356 ADD_FAILURE() << "Unexpected test result for: " << error_inputs[i]; | |
| 357 } | |
| 358 } | |
| 359 } | |
| 360 | |
| 361 TEST_F(ValidationTest, Conformance) { | |
| 362 DummyMessageReceiver dummy_receiver; | |
| 363 mojo::internal::FilterChain validators(&dummy_receiver); | |
| 364 validators.Append<mojo::internal::MessageHeaderValidator>(); | |
| 365 validators.Append<ConformanceTestInterface::RequestValidator_>(); | |
| 366 | |
| 367 RunValidationTests("conformance_", validators.GetHead()); | |
| 368 } | |
| 369 | |
| 370 // This test is similar to Conformance test but its goal is specifically | |
| 371 // do bounds-check testing of message validation. For example we test the | |
| 372 // detection of off-by-one errors in method ordinals. | |
| 373 TEST_F(ValidationTest, BoundsCheck) { | |
| 374 DummyMessageReceiver dummy_receiver; | |
| 375 mojo::internal::FilterChain validators(&dummy_receiver); | |
| 376 validators.Append<mojo::internal::MessageHeaderValidator>(); | |
| 377 validators.Append<BoundsCheckTestInterface::RequestValidator_>(); | |
| 378 | |
| 379 RunValidationTests("boundscheck_", validators.GetHead()); | |
| 380 } | |
| 381 | |
| 382 // This test is similar to the Conformance test but for responses. | |
| 383 TEST_F(ValidationTest, ResponseConformance) { | |
| 384 DummyMessageReceiver dummy_receiver; | |
| 385 mojo::internal::FilterChain validators(&dummy_receiver); | |
| 386 validators.Append<mojo::internal::MessageHeaderValidator>(); | |
| 387 validators.Append<ConformanceTestInterface::ResponseValidator_>(); | |
| 388 | |
| 389 RunValidationTests("resp_conformance_", validators.GetHead()); | |
| 390 } | |
| 391 | |
| 392 // This test is similar to the BoundsCheck test but for responses. | |
| 393 TEST_F(ValidationTest, ResponseBoundsCheck) { | |
| 394 DummyMessageReceiver dummy_receiver; | |
| 395 mojo::internal::FilterChain validators(&dummy_receiver); | |
| 396 validators.Append<mojo::internal::MessageHeaderValidator>(); | |
| 397 validators.Append<BoundsCheckTestInterface::ResponseValidator_>(); | |
| 398 | |
| 399 RunValidationTests("resp_boundscheck_", validators.GetHead()); | |
| 400 } | |
| 401 | |
| 402 // Test that InterfacePtr<X> applies the correct validators and they don't | |
| 403 // conflict with each other: | |
| 404 // - MessageHeaderValidator | |
| 405 // - X::ResponseValidator_ | |
| 406 TEST_F(ValidationIntegrationTest, InterfacePtr) { | |
| 407 IntegrationTestInterfacePtr interface_ptr = MakeProxy( | |
| 408 InterfacePtrInfo<IntegrationTestInterface>(testee_endpoint().Pass(), 0u)); | |
| 409 interface_ptr.internal_state()->router_for_testing()->EnableTestingMode(); | |
| 410 | |
| 411 RunValidationTests("integration_intf_resp", test_message_receiver()); | |
| 412 RunValidationTests("integration_msghdr", test_message_receiver()); | |
| 413 } | |
| 414 | |
| 415 // Test that Binding<X> applies the correct validators and they don't | |
| 416 // conflict with each other: | |
| 417 // - MessageHeaderValidator | |
| 418 // - X::RequestValidator_ | |
| 419 TEST_F(ValidationIntegrationTest, Binding) { | |
| 420 IntegrationTestInterfaceImpl interface_impl; | |
| 421 Binding<IntegrationTestInterface> binding( | |
| 422 &interface_impl, | |
| 423 MakeRequest<IntegrationTestInterface>(testee_endpoint().Pass())); | |
| 424 binding.internal_router()->EnableTestingMode(); | |
| 425 | |
| 426 RunValidationTests("integration_intf_rqst", test_message_receiver()); | |
| 427 RunValidationTests("integration_msghdr", test_message_receiver()); | |
| 428 } | |
| 429 | |
| 430 // Test pointer validation (specifically, that the encoded offset is 32-bit) | |
| 431 TEST_F(ValidationTest, ValidateEncodedPointer) { | |
| 432 uint64_t offset; | |
| 433 | |
| 434 offset = 0ULL; | |
| 435 EXPECT_TRUE(mojo::internal::ValidateEncodedPointer(&offset)); | |
| 436 | |
| 437 offset = 1ULL; | |
| 438 EXPECT_TRUE(mojo::internal::ValidateEncodedPointer(&offset)); | |
| 439 | |
| 440 // offset must be <= 32-bit. | |
| 441 offset = std::numeric_limits<uint32_t>::max() + 1ULL; | |
| 442 EXPECT_FALSE(mojo::internal::ValidateEncodedPointer(&offset)); | |
| 443 } | |
| 444 | |
| 445 // Tests the IsValidValue() function generated for BasicEnum. | |
| 446 TEST(EnumValueValidationTest, BasicEnum) { | |
| 447 // BasicEnum can have -3,0,1,10 as possible integral values. | |
| 448 EXPECT_FALSE(BasicEnum_IsValidValue(static_cast<BasicEnum>(-4))); | |
| 449 EXPECT_TRUE(BasicEnum_IsValidValue(static_cast<BasicEnum>(-3))); | |
| 450 EXPECT_FALSE(BasicEnum_IsValidValue(static_cast<BasicEnum>(-2))); | |
| 451 EXPECT_FALSE(BasicEnum_IsValidValue(static_cast<BasicEnum>(-1))); | |
| 452 EXPECT_TRUE(BasicEnum_IsValidValue(static_cast<BasicEnum>(0))); | |
| 453 EXPECT_TRUE(BasicEnum_IsValidValue(static_cast<BasicEnum>(1))); | |
| 454 EXPECT_FALSE(BasicEnum_IsValidValue(static_cast<BasicEnum>(2))); | |
| 455 EXPECT_FALSE(BasicEnum_IsValidValue(static_cast<BasicEnum>(9))); | |
| 456 // In the mojom, we represent this value as hex (0xa). | |
| 457 EXPECT_TRUE(BasicEnum_IsValidValue(static_cast<BasicEnum>(10))); | |
| 458 EXPECT_FALSE(BasicEnum_IsValidValue(static_cast<BasicEnum>(11))); | |
| 459 } | |
| 460 | |
| 461 // Tests the IsValidValue() method generated for StructWithEnum. | |
| 462 TEST(EnumValueValidationTest, EnumWithin) { | |
| 463 // StructWithEnum::EnumWithin can have [0,4] as possible integral values. | |
| 464 EXPECT_FALSE(StructWithEnum::EnumWithin_IsValidValue( | |
| 465 static_cast<StructWithEnum::EnumWithin>(-1))); | |
| 466 EXPECT_TRUE(StructWithEnum::EnumWithin_IsValidValue( | |
| 467 static_cast<StructWithEnum::EnumWithin>(0))); | |
| 468 EXPECT_TRUE(StructWithEnum::EnumWithin_IsValidValue( | |
| 469 static_cast<StructWithEnum::EnumWithin>(1))); | |
| 470 EXPECT_TRUE(StructWithEnum::EnumWithin_IsValidValue( | |
| 471 static_cast<StructWithEnum::EnumWithin>(2))); | |
| 472 EXPECT_TRUE(StructWithEnum::EnumWithin_IsValidValue( | |
| 473 static_cast<StructWithEnum::EnumWithin>(3))); | |
| 474 EXPECT_FALSE(StructWithEnum::EnumWithin_IsValidValue( | |
| 475 static_cast<StructWithEnum::EnumWithin>(4))); | |
| 476 } | |
| 477 | |
| 478 } // namespace | |
| 479 } // namespace test | |
| 480 } // namespace mojo | |
| OLD | NEW |