| 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 "net/cert/multi_threaded_cert_verifier.h" | 5 #include "net/cert/multi_threaded_cert_verifier.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/debug/leak_annotations.h" | 10 #include "base/debug/leak_annotations.h" |
| 11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 12 #include "base/format_macros.h" | 12 #include "base/format_macros.h" |
| 13 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
| 14 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
| 15 #include "net/base/test_completion_callback.h" | 15 #include "net/base/test_completion_callback.h" |
| 16 #include "net/cert/cert_verify_proc.h" | 16 #include "net/cert/cert_verify_proc.h" |
| 17 #include "net/cert/cert_verify_result.h" | 17 #include "net/cert/cert_verify_result.h" |
| 18 #include "net/cert/x509_certificate.h" | 18 #include "net/cert/x509_certificate.h" |
| 19 #include "net/log/net_log.h" | 19 #include "net/log/net_log.h" |
| 20 #include "net/test/cert_test_util.h" | 20 #include "net/test/cert_test_util.h" |
| 21 #include "net/test/gtest_util.h" |
| 21 #include "net/test/test_data_directory.h" | 22 #include "net/test/test_data_directory.h" |
| 23 #include "testing/gmock/include/gmock/gmock.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 23 | 25 |
| 26 using net::test::IsError; |
| 27 using net::test::IsOk; |
| 28 |
| 24 namespace net { | 29 namespace net { |
| 25 | 30 |
| 26 namespace { | 31 namespace { |
| 27 | 32 |
| 28 void FailTest(int /* result */) { | 33 void FailTest(int /* result */) { |
| 29 FAIL(); | 34 FAIL(); |
| 30 } | 35 } |
| 31 | 36 |
| 32 class MockCertVerifyProc : public CertVerifyProc { | 37 class MockCertVerifyProc : public CertVerifyProc { |
| 33 public: | 38 public: |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 TestCompletionCallback callback; | 82 TestCompletionCallback callback; |
| 78 std::unique_ptr<CertVerifier::Request> request; | 83 std::unique_ptr<CertVerifier::Request> request; |
| 79 CertVerifyResult verify_result2; | 84 CertVerifyResult verify_result2; |
| 80 TestCompletionCallback callback2; | 85 TestCompletionCallback callback2; |
| 81 std::unique_ptr<CertVerifier::Request> request2; | 86 std::unique_ptr<CertVerifier::Request> request2; |
| 82 | 87 |
| 83 error = verifier_.Verify( | 88 error = verifier_.Verify( |
| 84 CertVerifier::RequestParams(test_cert, "www.example.com", 0, | 89 CertVerifier::RequestParams(test_cert, "www.example.com", 0, |
| 85 std::string(), CertificateList()), | 90 std::string(), CertificateList()), |
| 86 NULL, &verify_result, callback.callback(), &request, BoundNetLog()); | 91 NULL, &verify_result, callback.callback(), &request, BoundNetLog()); |
| 87 ASSERT_EQ(ERR_IO_PENDING, error); | 92 ASSERT_THAT(error, IsError(ERR_IO_PENDING)); |
| 88 EXPECT_TRUE(request); | 93 EXPECT_TRUE(request); |
| 89 error = verifier_.Verify( | 94 error = verifier_.Verify( |
| 90 CertVerifier::RequestParams(test_cert, "www.example.com", 0, | 95 CertVerifier::RequestParams(test_cert, "www.example.com", 0, |
| 91 std::string(), CertificateList()), | 96 std::string(), CertificateList()), |
| 92 NULL, &verify_result2, callback2.callback(), &request2, BoundNetLog()); | 97 NULL, &verify_result2, callback2.callback(), &request2, BoundNetLog()); |
| 93 EXPECT_EQ(ERR_IO_PENDING, error); | 98 EXPECT_THAT(error, IsError(ERR_IO_PENDING)); |
| 94 EXPECT_TRUE(request2); | 99 EXPECT_TRUE(request2); |
| 95 error = callback.WaitForResult(); | 100 error = callback.WaitForResult(); |
| 96 EXPECT_TRUE(IsCertificateError(error)); | 101 EXPECT_TRUE(IsCertificateError(error)); |
| 97 error = callback2.WaitForResult(); | 102 error = callback2.WaitForResult(); |
| 98 ASSERT_TRUE(IsCertificateError(error)); | 103 ASSERT_TRUE(IsCertificateError(error)); |
| 99 ASSERT_EQ(2u, verifier_.requests()); | 104 ASSERT_EQ(2u, verifier_.requests()); |
| 100 ASSERT_EQ(1u, verifier_.inflight_joins()); | 105 ASSERT_EQ(1u, verifier_.inflight_joins()); |
| 101 } | 106 } |
| 102 | 107 |
| 103 // Tests that the callback of a canceled request is never made. | 108 // Tests that the callback of a canceled request is never made. |
| 104 TEST_F(MultiThreadedCertVerifierTest, CancelRequest) { | 109 TEST_F(MultiThreadedCertVerifierTest, CancelRequest) { |
| 105 base::FilePath certs_dir = GetTestCertsDirectory(); | 110 base::FilePath certs_dir = GetTestCertsDirectory(); |
| 106 scoped_refptr<X509Certificate> test_cert( | 111 scoped_refptr<X509Certificate> test_cert( |
| 107 ImportCertFromFile(certs_dir, "ok_cert.pem")); | 112 ImportCertFromFile(certs_dir, "ok_cert.pem")); |
| 108 ASSERT_NE(static_cast<X509Certificate*>(NULL), test_cert.get()); | 113 ASSERT_NE(static_cast<X509Certificate*>(NULL), test_cert.get()); |
| 109 | 114 |
| 110 int error; | 115 int error; |
| 111 CertVerifyResult verify_result; | 116 CertVerifyResult verify_result; |
| 112 std::unique_ptr<CertVerifier::Request> request; | 117 std::unique_ptr<CertVerifier::Request> request; |
| 113 | 118 |
| 114 error = verifier_.Verify( | 119 error = verifier_.Verify( |
| 115 CertVerifier::RequestParams(test_cert, "www.example.com", 0, | 120 CertVerifier::RequestParams(test_cert, "www.example.com", 0, |
| 116 std::string(), CertificateList()), | 121 std::string(), CertificateList()), |
| 117 NULL, &verify_result, base::Bind(&FailTest), &request, BoundNetLog()); | 122 NULL, &verify_result, base::Bind(&FailTest), &request, BoundNetLog()); |
| 118 ASSERT_EQ(ERR_IO_PENDING, error); | 123 ASSERT_THAT(error, IsError(ERR_IO_PENDING)); |
| 119 ASSERT_TRUE(request); | 124 ASSERT_TRUE(request); |
| 120 request.reset(); | 125 request.reset(); |
| 121 | 126 |
| 122 // Issue a few more requests to the worker pool and wait for their | 127 // Issue a few more requests to the worker pool and wait for their |
| 123 // completion, so that the task of the canceled request (which runs on a | 128 // completion, so that the task of the canceled request (which runs on a |
| 124 // worker thread) is likely to complete by the end of this test. | 129 // worker thread) is likely to complete by the end of this test. |
| 125 TestCompletionCallback callback; | 130 TestCompletionCallback callback; |
| 126 for (int i = 0; i < 5; ++i) { | 131 for (int i = 0; i < 5; ++i) { |
| 127 error = verifier_.Verify( | 132 error = verifier_.Verify( |
| 128 CertVerifier::RequestParams(test_cert, "www2.example.com", 0, | 133 CertVerifier::RequestParams(test_cert, "www2.example.com", 0, |
| 129 std::string(), CertificateList()), | 134 std::string(), CertificateList()), |
| 130 NULL, &verify_result, callback.callback(), &request, BoundNetLog()); | 135 NULL, &verify_result, callback.callback(), &request, BoundNetLog()); |
| 131 ASSERT_EQ(ERR_IO_PENDING, error); | 136 ASSERT_THAT(error, IsError(ERR_IO_PENDING)); |
| 132 EXPECT_TRUE(request); | 137 EXPECT_TRUE(request); |
| 133 error = callback.WaitForResult(); | 138 error = callback.WaitForResult(); |
| 134 } | 139 } |
| 135 } | 140 } |
| 136 | 141 |
| 137 // Tests that a canceled request is not leaked. | 142 // Tests that a canceled request is not leaked. |
| 138 TEST_F(MultiThreadedCertVerifierTest, CancelRequestThenQuit) { | 143 TEST_F(MultiThreadedCertVerifierTest, CancelRequestThenQuit) { |
| 139 base::FilePath certs_dir = GetTestCertsDirectory(); | 144 base::FilePath certs_dir = GetTestCertsDirectory(); |
| 140 scoped_refptr<X509Certificate> test_cert( | 145 scoped_refptr<X509Certificate> test_cert( |
| 141 ImportCertFromFile(certs_dir, "ok_cert.pem")); | 146 ImportCertFromFile(certs_dir, "ok_cert.pem")); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 152 // completes. In particular MultiThreadedCertVerifier calls | 157 // completes. In particular MultiThreadedCertVerifier calls |
| 153 // base::WorkerPool::PostTaskAndReply(), which leaks its "relay" when it | 158 // base::WorkerPool::PostTaskAndReply(), which leaks its "relay" when it |
| 154 // can't post the reply back to the origin thread. See | 159 // can't post the reply back to the origin thread. See |
| 155 // https://crbug.com/522514 | 160 // https://crbug.com/522514 |
| 156 ANNOTATE_SCOPED_MEMORY_LEAK; | 161 ANNOTATE_SCOPED_MEMORY_LEAK; |
| 157 error = verifier_.Verify( | 162 error = verifier_.Verify( |
| 158 CertVerifier::RequestParams(test_cert, "www.example.com", 0, | 163 CertVerifier::RequestParams(test_cert, "www.example.com", 0, |
| 159 std::string(), CertificateList()), | 164 std::string(), CertificateList()), |
| 160 NULL, &verify_result, callback.callback(), &request, BoundNetLog()); | 165 NULL, &verify_result, callback.callback(), &request, BoundNetLog()); |
| 161 } | 166 } |
| 162 ASSERT_EQ(ERR_IO_PENDING, error); | 167 ASSERT_THAT(error, IsError(ERR_IO_PENDING)); |
| 163 EXPECT_TRUE(request); | 168 EXPECT_TRUE(request); |
| 164 request.reset(); | 169 request.reset(); |
| 165 // Destroy |verifier| by going out of scope. | 170 // Destroy |verifier| by going out of scope. |
| 166 } | 171 } |
| 167 | 172 |
| 168 // Tests de-duplication of requests. | 173 // Tests de-duplication of requests. |
| 169 // Starts up 5 requests, of which 3 are unique. | 174 // Starts up 5 requests, of which 3 are unique. |
| 170 TEST_F(MultiThreadedCertVerifierTest, MultipleInflightJoin) { | 175 TEST_F(MultiThreadedCertVerifierTest, MultipleInflightJoin) { |
| 171 base::FilePath certs_dir = GetTestCertsDirectory(); | 176 base::FilePath certs_dir = GetTestCertsDirectory(); |
| 172 scoped_refptr<X509Certificate> test_cert( | 177 scoped_refptr<X509Certificate> test_cert( |
| (...skipping 19 matching lines...) Expand all Loading... |
| 192 | 197 |
| 193 const char domain1[] = "www.example1.com"; | 198 const char domain1[] = "www.example1.com"; |
| 194 const char domain2[] = "www.exampleB.com"; | 199 const char domain2[] = "www.exampleB.com"; |
| 195 const char domain3[] = "www.example3.com"; | 200 const char domain3[] = "www.example3.com"; |
| 196 | 201 |
| 197 // Start 3 unique requests. | 202 // Start 3 unique requests. |
| 198 error = verifier_.Verify( | 203 error = verifier_.Verify( |
| 199 CertVerifier::RequestParams(test_cert, domain2, 0, std::string(), | 204 CertVerifier::RequestParams(test_cert, domain2, 0, std::string(), |
| 200 CertificateList()), | 205 CertificateList()), |
| 201 nullptr, &verify_result1, callback1.callback(), &request1, BoundNetLog()); | 206 nullptr, &verify_result1, callback1.callback(), &request1, BoundNetLog()); |
| 202 ASSERT_EQ(ERR_IO_PENDING, error); | 207 ASSERT_THAT(error, IsError(ERR_IO_PENDING)); |
| 203 EXPECT_TRUE(request1); | 208 EXPECT_TRUE(request1); |
| 204 | 209 |
| 205 error = verifier_.Verify( | 210 error = verifier_.Verify( |
| 206 CertVerifier::RequestParams(test_cert, domain2, 0, std::string(), | 211 CertVerifier::RequestParams(test_cert, domain2, 0, std::string(), |
| 207 CertificateList()), | 212 CertificateList()), |
| 208 nullptr, &verify_result2, callback2.callback(), &request2, BoundNetLog()); | 213 nullptr, &verify_result2, callback2.callback(), &request2, BoundNetLog()); |
| 209 EXPECT_EQ(ERR_IO_PENDING, error); | 214 EXPECT_THAT(error, IsError(ERR_IO_PENDING)); |
| 210 EXPECT_TRUE(request2); | 215 EXPECT_TRUE(request2); |
| 211 | 216 |
| 212 error = verifier_.Verify( | 217 error = verifier_.Verify( |
| 213 CertVerifier::RequestParams(test_cert, domain3, 0, std::string(), | 218 CertVerifier::RequestParams(test_cert, domain3, 0, std::string(), |
| 214 CertificateList()), | 219 CertificateList()), |
| 215 nullptr, &verify_result3, callback3.callback(), &request3, BoundNetLog()); | 220 nullptr, &verify_result3, callback3.callback(), &request3, BoundNetLog()); |
| 216 EXPECT_EQ(ERR_IO_PENDING, error); | 221 EXPECT_THAT(error, IsError(ERR_IO_PENDING)); |
| 217 EXPECT_TRUE(request3); | 222 EXPECT_TRUE(request3); |
| 218 | 223 |
| 219 // Start duplicate requests (which should join to existing jobs). | 224 // Start duplicate requests (which should join to existing jobs). |
| 220 error = verifier_.Verify( | 225 error = verifier_.Verify( |
| 221 CertVerifier::RequestParams(test_cert, domain1, 0, std::string(), | 226 CertVerifier::RequestParams(test_cert, domain1, 0, std::string(), |
| 222 CertificateList()), | 227 CertificateList()), |
| 223 nullptr, &verify_result4, callback4.callback(), &request4, BoundNetLog()); | 228 nullptr, &verify_result4, callback4.callback(), &request4, BoundNetLog()); |
| 224 EXPECT_EQ(ERR_IO_PENDING, error); | 229 EXPECT_THAT(error, IsError(ERR_IO_PENDING)); |
| 225 EXPECT_TRUE(request4); | 230 EXPECT_TRUE(request4); |
| 226 | 231 |
| 227 error = verifier_.Verify( | 232 error = verifier_.Verify( |
| 228 CertVerifier::RequestParams(test_cert, domain2, 0, std::string(), | 233 CertVerifier::RequestParams(test_cert, domain2, 0, std::string(), |
| 229 CertificateList()), | 234 CertificateList()), |
| 230 nullptr, &verify_result5, callback5.callback(), &request5, BoundNetLog()); | 235 nullptr, &verify_result5, callback5.callback(), &request5, BoundNetLog()); |
| 231 EXPECT_EQ(ERR_IO_PENDING, error); | 236 EXPECT_THAT(error, IsError(ERR_IO_PENDING)); |
| 232 EXPECT_TRUE(request5); | 237 EXPECT_TRUE(request5); |
| 233 | 238 |
| 234 error = callback1.WaitForResult(); | 239 error = callback1.WaitForResult(); |
| 235 EXPECT_TRUE(IsCertificateError(error)); | 240 EXPECT_TRUE(IsCertificateError(error)); |
| 236 error = callback2.WaitForResult(); | 241 error = callback2.WaitForResult(); |
| 237 ASSERT_TRUE(IsCertificateError(error)); | 242 ASSERT_TRUE(IsCertificateError(error)); |
| 238 error = callback4.WaitForResult(); | 243 error = callback4.WaitForResult(); |
| 239 ASSERT_TRUE(IsCertificateError(error)); | 244 ASSERT_TRUE(IsCertificateError(error)); |
| 240 | 245 |
| 241 // Let the other requests automatically cancel. | 246 // Let the other requests automatically cancel. |
| 242 ASSERT_EQ(5u, verifier_.requests()); | 247 ASSERT_EQ(5u, verifier_.requests()); |
| 243 ASSERT_EQ(2u, verifier_.inflight_joins()); | 248 ASSERT_EQ(2u, verifier_.inflight_joins()); |
| 244 } | 249 } |
| 245 | 250 |
| 246 } // namespace net | 251 } // namespace net |
| OLD | NEW |