estark | b1ee167 | 2015-08-06 01:15:24 | [diff] [blame] | 1 | // Copyright 2015 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 | #ifndef CHROME_BROWSER_SSL_CERT_VERIFIER_BROWSER_TEST_H_ |
| 6 | #define CHROME_BROWSER_SSL_CERT_VERIFIER_BROWSER_TEST_H_ |
| 7 | |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 8 | #include <memory> |
| 9 | |
estark | b1ee167 | 2015-08-06 01:15:24 | [diff] [blame] | 10 | #include "chrome/test/base/in_process_browser_test.h" |
John Abd-El-Malek | 9672128 | 2018-01-09 01:35:17 | [diff] [blame] | 11 | #include "content/public/common/network_service_test.mojom.h" |
| 12 | #include "net/cert/mock_cert_verifier.h" |
estark | b1ee167 | 2015-08-06 01:15:24 | [diff] [blame] | 13 | |
| 14 | namespace net { |
| 15 | class MockCertVerifier; |
| 16 | } // namespace net |
| 17 | |
| 18 | // CertVerifierBrowserTest allows tests to force certificate |
| 19 | // verification results for requests made with any profile's main |
| 20 | // request context (such as navigations). To do so, tests can use the |
| 21 | // MockCertVerifier exposed via |
| 22 | // CertVerifierBrowserTest::mock_cert_verifier(). |
| 23 | class CertVerifierBrowserTest : public InProcessBrowserTest { |
| 24 | public: |
| 25 | CertVerifierBrowserTest(); |
| 26 | ~CertVerifierBrowserTest() override; |
| 27 | |
| 28 | // InProcessBrowserTest: |
John Abd-El-Malek | e9cf288 | 2018-01-17 16:59:06 | [diff] [blame^] | 29 | void SetUpCommandLine(base::CommandLine* command_line) override; |
estark | b1ee167 | 2015-08-06 01:15:24 | [diff] [blame] | 30 | void SetUpInProcessBrowserTestFixture() override; |
| 31 | void TearDownInProcessBrowserTestFixture() override; |
| 32 | |
John Abd-El-Malek | 9672128 | 2018-01-09 01:35:17 | [diff] [blame] | 33 | // Has the same methods as net::MockCertVerifier and updates the network |
| 34 | // service as well if it's in use. See the documentation of the net class |
| 35 | // for documentation on the methods. |
| 36 | // Once all requests use the NetworkContext even when network service is not |
| 37 | // enabled, we can stop also updating net::MockCertVerifier here and always |
| 38 | // go through the NetworkServiceTest mojo interface. |
| 39 | class CertVerifier { |
| 40 | public: |
| 41 | explicit CertVerifier(net::MockCertVerifier* verifier); |
| 42 | ~CertVerifier(); |
| 43 | void set_default_result(int default_result); |
| 44 | void AddResultForCert(scoped_refptr<net::X509Certificate> cert, |
| 45 | const net::CertVerifyResult& verify_result, |
| 46 | int rv); |
| 47 | void AddResultForCertAndHost(scoped_refptr<net::X509Certificate> cert, |
| 48 | const std::string& host_pattern, |
| 49 | const net::CertVerifyResult& verify_result, |
| 50 | int rv); |
| 51 | |
| 52 | private: |
| 53 | void EnsureNetworkServiceTestInitialized(); |
| 54 | |
| 55 | net::MockCertVerifier* verifier_; |
| 56 | content::mojom::NetworkServiceTestPtr network_service_test_; |
| 57 | }; |
| 58 | |
estark | b1ee167 | 2015-08-06 01:15:24 | [diff] [blame] | 59 | // Returns a pointer to the MockCertVerifier used by all profiles in |
| 60 | // this test. |
John Abd-El-Malek | 9672128 | 2018-01-09 01:35:17 | [diff] [blame] | 61 | CertVerifier* mock_cert_verifier(); |
estark | b1ee167 | 2015-08-06 01:15:24 | [diff] [blame] | 62 | |
| 63 | private: |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 64 | std::unique_ptr<net::MockCertVerifier> mock_cert_verifier_; |
John Abd-El-Malek | 9672128 | 2018-01-09 01:35:17 | [diff] [blame] | 65 | |
| 66 | CertVerifier cert_verifier_; |
estark | b1ee167 | 2015-08-06 01:15:24 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | #endif // CHROME_BROWSER_SSL_CERT_VERIFIER_BROWSER_TEST_H_ |