OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <memory> | 5 #include <memory> |
6 #include <utility> | 6 #include <utility> |
7 | 7 |
8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
(...skipping 9229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
9240 ImportCertFromFile(GetTestCertsDirectory(), "ocsp-test-root.pem"); | 9240 ImportCertFromFile(GetTestCertsDirectory(), "ocsp-test-root.pem"); |
9241 CHECK_NE(static_cast<X509Certificate*>(NULL), root_cert.get()); | 9241 CHECK_NE(static_cast<X509Certificate*>(NULL), root_cert.get()); |
9242 test_root_.reset(new ScopedTestRoot(root_cert.get())); | 9242 test_root_.reset(new ScopedTestRoot(root_cert.get())); |
9243 | 9243 |
9244 #if defined(USE_NSS_CERTS) | 9244 #if defined(USE_NSS_CERTS) |
9245 SetURLRequestContextForNSSHttpIO(&context_); | 9245 SetURLRequestContextForNSSHttpIO(&context_); |
9246 EnsureNSSHttpIOInit(); | 9246 EnsureNSSHttpIOInit(); |
9247 #endif | 9247 #endif |
9248 } | 9248 } |
9249 | 9249 |
9250 void DoConnection(const SpawnedTestServer::SSLOptions& ssl_options, | 9250 ::testing::AssertionResult DoConnection( |
svaldez
2016/06/29 14:41:23
Still unclear whether you need the return change.
dadrian
2016/06/30 21:52:43
I got flak for not making this change on another r
Ryan Sleevi
2016/06/30 22:14:51
It may not have been clear why the change.
It mat
dadrian
2016/07/08 22:17:30
Reverted, but not sure I follow. You can't use an
| |
9251 CertStatus* out_cert_status) { | 9251 const SpawnedTestServer::SSLOptions& ssl_options, |
9252 // We always overwrite out_cert_status. | 9252 TestDelegate* delegate, |
9253 *out_cert_status = 0; | 9253 SSLInfo* out_ssl_info) { |
9254 // Always overwrite |out_ssl_info|. | |
9255 out_ssl_info->Reset(); | |
9256 | |
9254 SpawnedTestServer test_server( | 9257 SpawnedTestServer test_server( |
9255 SpawnedTestServer::TYPE_HTTPS, | 9258 SpawnedTestServer::TYPE_HTTPS, |
9256 ssl_options, | 9259 ssl_options, |
9257 base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); | 9260 base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
9258 ASSERT_TRUE(test_server.Start()); | 9261 EXPECT_TRUE(test_server.Start()); |
9259 | 9262 |
9260 TestDelegate d; | 9263 delegate->set_allow_certificate_errors(true); |
9261 d.set_allow_certificate_errors(true); | 9264 std::unique_ptr<URLRequest> r(context_.CreateRequest( |
9262 std::unique_ptr<URLRequest> r( | 9265 test_server.GetURL("/"), DEFAULT_PRIORITY, delegate)); |
9263 context_.CreateRequest(test_server.GetURL("/"), DEFAULT_PRIORITY, &d)); | |
9264 r->Start(); | 9266 r->Start(); |
9265 | 9267 |
9266 base::RunLoop().Run(); | 9268 base::RunLoop().Run(); |
9269 EXPECT_EQ(1, delegate->response_started_count()); | |
9267 | 9270 |
9268 EXPECT_EQ(1, d.response_started_count()); | 9271 *out_ssl_info = r->ssl_info(); |
9269 *out_cert_status = r->ssl_info().cert_status; | 9272 return ::testing::AssertionSuccess(); |
9273 } | |
9274 | |
9275 ::testing::AssertionResult DoConnection( | |
9276 const SpawnedTestServer::SSLOptions& ssl_options, | |
9277 SSLInfo* out_ssl_info) { | |
9278 TestDelegate d; | |
9279 return DoConnection(ssl_options, &d, out_ssl_info); | |
9280 } | |
9281 | |
9282 ::testing::AssertionResult DoConnection( | |
9283 const SpawnedTestServer::SSLOptions& ssl_options, | |
9284 CertStatus* out_cert_status) { | |
9285 // Always overwrite |out_cert_status|. | |
9286 *out_cert_status = 0; | |
9287 | |
9288 SSLInfo ssl_info; | |
9289 EXPECT_TRUE(DoConnection(ssl_options, &ssl_info)); | |
9290 | |
9291 *out_cert_status = ssl_info.cert_status; | |
9292 return ::testing::AssertionSuccess(); | |
9270 } | 9293 } |
9271 | 9294 |
9272 ~HTTPSOCSPTest() override { | 9295 ~HTTPSOCSPTest() override { |
9273 #if defined(USE_NSS_CERTS) | 9296 #if defined(USE_NSS_CERTS) |
9274 ShutdownNSSHttpIO(); | 9297 ShutdownNSSHttpIO(); |
9275 #endif | 9298 #endif |
9276 } | 9299 } |
9277 | 9300 |
9278 protected: | 9301 protected: |
9279 class AllowAnyCertCTPolicyEnforcer : public CTPolicyEnforcer { | 9302 class AllowAnyCertCTPolicyEnforcer : public CTPolicyEnforcer { |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
9478 ssl_options.ocsp_server_unavailable = true; | 9501 ssl_options.ocsp_server_unavailable = true; |
9479 | 9502 |
9480 CertStatus cert_status; | 9503 CertStatus cert_status; |
9481 DoConnection(ssl_options, &cert_status); | 9504 DoConnection(ssl_options, &cert_status); |
9482 | 9505 |
9483 EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); | 9506 EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
9484 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); | 9507 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
9485 EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); | 9508 EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
9486 } | 9509 } |
9487 | 9510 |
9511 struct OCSPVerifyTestData { | |
9512 SpawnedTestServer::SSLOptions::OCSPStatus ocsp_status; | |
9513 SpawnedTestServer::SSLOptions::OCSPDate ocsp_date; | |
9514 OCSPVerifyResult::ResponseStatus response_status; | |
9515 bool has_cert_status; | |
9516 OCSPCertStatus::Status cert_status; | |
9517 }; | |
9518 | |
9519 static const OCSPVerifyTestData kOCSPVerifyData[] = { | |
Ryan Sleevi
2016/06/30 22:14:51
We tend to just combine these definitions
static
dadrian
2016/07/08 22:17:30
I had kept them separate since the type was used i
| |
9520 { | |
9521 SpawnedTestServer::SSLOptions::OCSP_OK, | |
9522 SpawnedTestServer::SSLOptions::OCSP_VALID, OCSPVerifyResult::PROVIDED, | |
9523 true, OCSPCertStatus::Status::GOOD, | |
9524 }, | |
9525 { | |
9526 SpawnedTestServer::SSLOptions::OCSP_OK, | |
9527 SpawnedTestServer::SSLOptions::OCSP_OLD, OCSPVerifyResult::INVALID_DATE, | |
9528 false, OCSPCertStatus::Status::GOOD, | |
9529 }, | |
9530 { | |
9531 SpawnedTestServer::SSLOptions::OCSP_OK, | |
9532 SpawnedTestServer::SSLOptions::OCSP_EARLY, | |
9533 OCSPVerifyResult::INVALID_DATE, false, OCSPCertStatus::Status::GOOD, | |
9534 }, | |
9535 { | |
9536 SpawnedTestServer::SSLOptions::OCSP_OK, | |
9537 SpawnedTestServer::SSLOptions::OCSP_LONG, | |
9538 OCSPVerifyResult::INVALID_DATE, false, OCSPCertStatus::Status::GOOD, | |
9539 }, | |
9540 { | |
9541 SpawnedTestServer::SSLOptions::OCSP_INVALID, | |
9542 SpawnedTestServer::SSLOptions::OCSP_VALID, | |
9543 OCSPVerifyResult::PARSE_RESPONSE, false, | |
9544 OCSPCertStatus::Status::UNKNOWN, | |
9545 }, | |
9546 { | |
9547 SpawnedTestServer::SSLOptions::OCSP_REVOKED, | |
9548 SpawnedTestServer::SSLOptions::OCSP_EARLY, | |
9549 OCSPVerifyResult::INVALID_DATE, false, OCSPCertStatus::Status::UNKNOWN, | |
9550 }, | |
9551 { | |
9552 SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, | |
9553 SpawnedTestServer::SSLOptions::OCSP_VALID, OCSPVerifyResult::PROVIDED, | |
9554 true, OCSPCertStatus::Status::UNKNOWN, | |
9555 }, | |
9556 { | |
9557 SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, | |
9558 SpawnedTestServer::SSLOptions::OCSP_OLD, OCSPVerifyResult::INVALID_DATE, | |
9559 false, OCSPCertStatus::Status::UNKNOWN, | |
9560 }, | |
9561 { | |
9562 SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, | |
9563 SpawnedTestServer::SSLOptions::OCSP_EARLY, | |
9564 OCSPVerifyResult::INVALID_DATE, false, OCSPCertStatus::Status::UNKNOWN, | |
9565 }, | |
9566 { | |
9567 SpawnedTestServer::SSLOptions::OCSP_UNKNOWN, | |
9568 SpawnedTestServer::SSLOptions::OCSP_LONG, | |
9569 OCSPVerifyResult::INVALID_DATE, false, OCSPCertStatus::Status::UNKNOWN, | |
9570 }, | |
9571 }; | |
9572 | |
9573 class HTTPSOCSPVerifyTest | |
9574 : public HTTPSOCSPTest, | |
9575 public testing::WithParamInterface<OCSPVerifyTestData> { | |
9576 public: | |
9577 HTTPSOCSPVerifyTest() = default; | |
9578 virtual ~HTTPSOCSPVerifyTest() {} | |
9579 }; | |
9580 | |
9581 TEST_P(HTTPSOCSPVerifyTest, SingleResponse) { | |
9582 SpawnedTestServer::SSLOptions ssl_options( | |
9583 SpawnedTestServer::SSLOptions::CERT_AUTO); | |
9584 OCSPVerifyTestData test = GetParam(); | |
9585 ssl_options.ocsp_status = test.ocsp_status; | |
9586 ssl_options.ocsp_date = test.ocsp_date; | |
9587 ssl_options.staple_ocsp_response = true; | |
9588 | |
9589 SSLInfo ssl_info; | |
9590 ASSERT_TRUE(DoConnection(ssl_options, &ssl_info)); | |
9591 | |
9592 EXPECT_EQ(0u, ssl_info.cert_status & CERT_STATUS_ALL_ERRORS); | |
9593 EXPECT_EQ(test.response_status, ssl_info.ocsp.response_status); | |
9594 | |
9595 if (test.has_cert_status) { | |
9596 ASSERT_TRUE(ssl_info.ocsp.cert_status); | |
9597 EXPECT_EQ(test.cert_status, *ssl_info.ocsp.cert_status); | |
9598 } else { | |
9599 EXPECT_FALSE(ssl_info.ocsp.cert_status); | |
9600 } | |
9601 }; | |
9602 | |
9603 INSTANTIATE_TEST_CASE_P(OCSPVerify, | |
9604 HTTPSOCSPVerifyTest, | |
9605 testing::ValuesIn(kOCSPVerifyData)); | |
9606 | |
9607 // OCSPErrorTestDelegate caches the SSLInfo passed to OnSSLCertificateError. | |
9608 // This is needed because after the certificate failure, the URLRequest will | |
9609 // retry the connection, and return a partial SSLInfo with a cached cert status. | |
9610 // The partial SSLInfo does not have the OCSP information filled out. | |
9611 class OCSPErrorTestDelegate : public TestDelegate { | |
dadrian
2016/06/27 22:43:03
This approach is definitely less than ideal, and n
svaldez
2016/06/29 14:41:23
Arguably, we might actually want a flag on the cer
Ryan Sleevi
2016/06/30 22:14:50
I'm not sure I understand what you're proposing?
Ryan Sleevi
2016/06/30 22:14:51
Have you traced through with gdb to figure out who
dadrian
2016/07/08 22:17:30
I worked around it, but it was getting modified by
| |
9612 public: | |
9613 void OnSSLCertificateError(URLRequest* request, | |
9614 const SSLInfo& ssl_info, | |
9615 bool fatal) override { | |
9616 ssl_info_ = ssl_info; | |
9617 on_ssl_certificate_error_called_ = true; | |
9618 TestDelegate::OnSSLCertificateError(request, ssl_info, fatal); | |
9619 } | |
9620 | |
9621 bool on_ssl_certificate_error_called() { | |
9622 return on_ssl_certificate_error_called_; | |
9623 } | |
9624 | |
9625 SSLInfo ssl_info() { return ssl_info_; } | |
9626 | |
9627 private: | |
9628 bool on_ssl_certificate_error_called_ = false; | |
9629 SSLInfo ssl_info_; | |
9630 }; | |
9631 | |
9632 static const OCSPVerifyTestData kOCSPFailData[] = { | |
9633 { | |
9634 SpawnedTestServer::SSLOptions::OCSP_REVOKED, | |
9635 SpawnedTestServer::SSLOptions::OCSP_VALID, OCSPVerifyResult::PROVIDED, | |
9636 true, OCSPCertStatus::Status::REVOKED, | |
9637 }, | |
9638 { | |
9639 SpawnedTestServer::SSLOptions::OCSP_REVOKED, | |
9640 SpawnedTestServer::SSLOptions::OCSP_OLD, OCSPVerifyResult::INVALID_DATE, | |
dadrian
2016/06/27 22:43:03
Arguably, this test case should not cause a failur
| |
9641 false, OCSPCertStatus::Status::UNKNOWN, | |
9642 }, | |
9643 { | |
9644 SpawnedTestServer::SSLOptions::OCSP_REVOKED, | |
9645 SpawnedTestServer::SSLOptions::OCSP_LONG, | |
dadrian
2016/06/27 22:43:03
Similar comment about this test case.
| |
9646 OCSPVerifyResult::INVALID_DATE, false, OCSPCertStatus::Status::UNKNOWN, | |
9647 }, | |
9648 }; | |
9649 | |
9650 class HTTPSOCSPFailTest | |
9651 : public HTTPSOCSPTest, | |
9652 public testing::WithParamInterface<OCSPVerifyTestData> { | |
9653 public: | |
9654 HTTPSOCSPFailTest() = default; | |
9655 virtual ~HTTPSOCSPFailTest() {} | |
9656 }; | |
9657 | |
9658 TEST_P(HTTPSOCSPFailTest, SingleResponse) { | |
9659 SpawnedTestServer::SSLOptions ssl_options( | |
9660 SpawnedTestServer::SSLOptions::CERT_AUTO); | |
9661 OCSPVerifyTestData test = GetParam(); | |
9662 ssl_options.ocsp_status = test.ocsp_status; | |
9663 ssl_options.ocsp_date = test.ocsp_date; | |
9664 ssl_options.staple_ocsp_response = true; | |
9665 | |
9666 SSLInfo unused; | |
9667 OCSPErrorTestDelegate d; | |
9668 ASSERT_TRUE(DoConnection(ssl_options, &d, &unused)); | |
9669 ASSERT_TRUE(d.on_ssl_certificate_error_called()); | |
9670 SSLInfo ssl_info = d.ssl_info(); | |
9671 | |
9672 EXPECT_EQ(CERT_STATUS_REVOKED, ssl_info.cert_status & CERT_STATUS_REVOKED); | |
9673 EXPECT_EQ(test.response_status, ssl_info.ocsp.response_status); | |
9674 | |
9675 if (test.has_cert_status) { | |
9676 ASSERT_TRUE(ssl_info.ocsp.cert_status); | |
9677 EXPECT_EQ(test.cert_status, *ssl_info.ocsp.cert_status); | |
9678 } else { | |
9679 EXPECT_FALSE(ssl_info.ocsp.cert_status); | |
9680 } | |
9681 }; | |
9682 | |
9683 INSTANTIATE_TEST_CASE_P(OCSPVerify, | |
9684 HTTPSOCSPFailTest, | |
9685 testing::ValuesIn(kOCSPFailData)); | |
9686 | |
9488 class HTTPSHardFailTest : public HTTPSOCSPTest { | 9687 class HTTPSHardFailTest : public HTTPSOCSPTest { |
9489 protected: | 9688 protected: |
9490 void SetupContext() override { | 9689 void SetupContext() override { |
9491 context_.set_ssl_config_service(new TestSSLConfigService( | 9690 context_.set_ssl_config_service(new TestSSLConfigService( |
9492 false /* check for EV */, false /* online revocation checking */, | 9691 false /* check for EV */, false /* online revocation checking */, |
9493 true /* require rev. checking for local | 9692 true /* require rev. checking for local |
9494 anchors */, | 9693 anchors */, |
9495 false /* token binding enabled */)); | 9694 false /* token binding enabled */)); |
9496 } | 9695 } |
9497 }; | 9696 }; |
(...skipping 650 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
10148 AddTestInterceptor()->set_main_intercept_job(std::move(job)); | 10347 AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
10149 | 10348 |
10150 req->Start(); | 10349 req->Start(); |
10151 req->Cancel(); | 10350 req->Cancel(); |
10152 base::RunLoop().RunUntilIdle(); | 10351 base::RunLoop().RunUntilIdle(); |
10153 EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); | 10352 EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
10154 EXPECT_EQ(0, d.received_redirect_count()); | 10353 EXPECT_EQ(0, d.received_redirect_count()); |
10155 } | 10354 } |
10156 | 10355 |
10157 } // namespace net | 10356 } // namespace net |
OLD | NEW |