Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 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 "net/ocsp/nss_ocsp.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/file_util.h" | |
| 10 #include "base/files/file_path.h" | |
| 11 #include "base/logging.h" | |
| 12 #include "base/memory/ref_counted.h" | |
| 13 #include "net/base/net_errors.h" | |
| 14 #include "net/base/test_completion_callback.h" | |
| 15 #include "net/base/test_data_directory.h" | |
| 16 #include "net/cert/cert_status_flags.h" | |
| 17 #include "net/cert/cert_verifier.h" | |
| 18 #include "net/cert/cert_verify_proc.h" | |
| 19 #include "net/cert/cert_verify_proc_nss.h" | |
| 20 #include "net/cert/cert_verify_result.h" | |
| 21 #include "net/cert/multi_threaded_cert_verifier.h" | |
| 22 #include "net/cert/test_root_certs.h" | |
| 23 #include "net/cert/x509_certificate.h" | |
| 24 #include "net/test/cert_test_util.h" | |
| 25 #include "net/url_request/url_request_filter.h" | |
| 26 #include "net/url_request/url_request_test_job.h" | |
| 27 #include "net/url_request/url_request_test_util.h" | |
| 28 #include "testing/gtest/include/gtest/gtest.h" | |
| 29 | |
| 30 namespace net { | |
| 31 | |
| 32 namespace { | |
| 33 | |
| 34 // Matches the caIssuers hostname from the generated certificate. | |
| 35 const char kAiaHost[] = "aia-test.invalid"; | |
| 36 // Returning a single DER-encoded cert, so the mime-type must be | |
| 37 // application/pkix-cert per RFC 5280. | |
| 38 const char kAiaHeaders[] = "HTTP/1.1 200 OK\0" | |
| 39 "Content-type: application/pkix-cert\0" | |
| 40 "\0"; | |
| 41 | |
| 42 class AiaResponseHandler : public net::URLRequestJobFactory::ProtocolHandler { | |
| 43 public: | |
| 44 AiaResponseHandler(const std::string& headers, const std::string& cert_data) | |
| 45 : headers_(headers), cert_data_(cert_data), request_count_(0) {} | |
| 46 virtual ~AiaResponseHandler() {} | |
| 47 | |
| 48 // net::URLRequestJobFactory::ProtocolHandler implementation: | |
| 49 virtual net::URLRequestJob* MaybeCreateJob( | |
| 50 net::URLRequest* request, | |
| 51 net::NetworkDelegate* network_delegate) const OVERRIDE { | |
| 52 ++const_cast<AiaResponseHandler*>(this)->request_count_; | |
| 53 | |
| 54 return new net::URLRequestTestJob( | |
| 55 request, network_delegate, headers_, cert_data_, true); | |
| 56 } | |
| 57 | |
| 58 int request_count() const { return request_count_; } | |
| 59 | |
| 60 private: | |
| 61 std::string headers_; | |
| 62 std::string cert_data_; | |
| 63 int request_count_; | |
| 64 | |
| 65 DISALLOW_COPY_AND_ASSIGN(AiaResponseHandler); | |
| 66 }; | |
| 67 | |
| 68 } // namespace | |
| 69 | |
| 70 class NssHttpTest : public ::testing::Test { | |
| 71 public: | |
| 72 NssHttpTest() | |
| 73 : context_(false), | |
| 74 verify_proc_(new CertVerifyProcNSS), | |
| 75 verifier_(new MultiThreadedCertVerifier(verify_proc_)) { | |
| 76 } | |
| 77 virtual ~NssHttpTest() {} | |
| 78 | |
| 79 virtual void SetUp() { | |
| 80 std::string file_contents; | |
| 81 ASSERT_TRUE(file_util::ReadFileToString( | |
| 82 GetTestCertsDirectory().AppendASCII("aia-intermediate.der"), | |
| 83 &file_contents)); | |
| 84 // Ownership of |handler| is transferred to the URLRequestFilter, but | |
|
erikwright (departed)
2013/05/22 23:01:12
Thanks. I think this comment is optional at this p
| |
| 85 // hold onto the original pointer in order to access |request_count()|. | |
| 86 scoped_ptr<AiaResponseHandler> handler( | |
| 87 new AiaResponseHandler(kAiaHeaders, file_contents)); | |
| 88 handler_ = handler.get(); | |
| 89 | |
| 90 URLRequestFilter::GetInstance()->AddHostnameProtocolHandler( | |
| 91 "http", | |
| 92 kAiaHost, | |
| 93 handler.PassAs<URLRequestJobFactory::ProtocolHandler>()); | |
| 94 | |
| 95 SetURLRequestContextForNSSHttpIO(&context_); | |
| 96 EnsureNSSHttpIOInit(); | |
| 97 } | |
| 98 | |
| 99 virtual void TearDown() { | |
| 100 ShutdownNSSHttpIO(); | |
| 101 | |
| 102 URLRequestFilter::GetInstance()->RemoveHostnameHandler("http", kAiaHost); | |
| 103 } | |
| 104 | |
| 105 CertVerifier* verifier() const { | |
| 106 return verifier_.get(); | |
| 107 } | |
| 108 | |
| 109 int request_count() const { | |
| 110 return handler_->request_count(); | |
| 111 } | |
| 112 | |
| 113 protected: | |
| 114 const CertificateList empty_cert_list_; | |
| 115 | |
| 116 private: | |
| 117 TestURLRequestContext context_; | |
| 118 AiaResponseHandler* handler_; | |
| 119 scoped_refptr<CertVerifyProc> verify_proc_; | |
| 120 scoped_ptr<CertVerifier> verifier_; | |
| 121 }; | |
| 122 | |
| 123 // Tests that when using NSS to verify certificates, and IO is enabled, | |
| 124 // that a request to fetch missing intermediate certificates is | |
| 125 // made successfully. | |
| 126 TEST_F(NssHttpTest, TestAia) { | |
| 127 scoped_refptr<X509Certificate> test_cert( | |
| 128 ImportCertFromFile(GetTestCertsDirectory(), "aia-cert.pem")); | |
| 129 ASSERT_TRUE(test_cert.get()); | |
| 130 | |
| 131 scoped_refptr<X509Certificate> test_root( | |
| 132 ImportCertFromFile(GetTestCertsDirectory(), "aia-root.pem")); | |
| 133 ASSERT_TRUE(test_root.get()); | |
| 134 | |
| 135 ScopedTestRoot scoped_root(test_root); | |
| 136 | |
| 137 CertVerifyResult verify_result; | |
| 138 TestCompletionCallback test_callback; | |
| 139 CertVerifier::RequestHandle request_handle; | |
| 140 | |
| 141 int flags = CertVerifier::VERIFY_CERT_IO_ENABLED; | |
| 142 int error = verifier()->Verify(test_cert, "aia-host.invalid", flags, NULL, | |
| 143 &verify_result, test_callback.callback(), | |
| 144 &request_handle, BoundNetLog()); | |
| 145 ASSERT_EQ(ERR_IO_PENDING, error); | |
| 146 | |
| 147 error = test_callback.WaitForResult(); | |
| 148 | |
| 149 EXPECT_EQ(OK, error); | |
| 150 | |
| 151 // Ensure that NSS made an AIA request for the missing intermediate. | |
| 152 EXPECT_LT(0, request_count()); | |
| 153 } | |
| 154 | |
| 155 } // namespace net | |
| OLD | NEW |