1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93

content / public / test / content_mock_cert_verifier.h [blame]

// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_PUBLIC_TEST_CONTENT_MOCK_CERT_VERIFIER_H_
#define CONTENT_PUBLIC_TEST_CONTENT_MOCK_CERT_VERIFIER_H_

#include <memory>

#include "base/memory/raw_ptr.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "net/cert/mock_cert_verifier.h"
#include "services/network/public/mojom/network_service_test.mojom.h"

namespace base {
class CommandLine;
}

namespace net {
class MockCertVerifier;
}

namespace content {

// ContentMockCertVerifier allows tests to force certificate verification
// results for requests made with any BrowserContext's main request context
// (such as navigations). To do so, tests can use the MockCertVerifier exposed
// via ContentMockCertVerifier::mock_cert_verifier(). This class ensures the
// mocking works correctly regardless of whether the network service is enabled
// or disabled, or running in-process.
class ContentMockCertVerifier {
 public:
  ContentMockCertVerifier();

  ContentMockCertVerifier(const ContentMockCertVerifier&) = delete;
  ContentMockCertVerifier& operator=(const ContentMockCertVerifier&) = delete;

  virtual ~ContentMockCertVerifier();

  // Be sure to call these method from the relevant BrowserTestBase methods.
  virtual void SetUpCommandLine(base::CommandLine* command_line);
  virtual void SetUpInProcessBrowserTestFixture();
  virtual void TearDownInProcessBrowserTestFixture();

  // Has the same methods as net::MockCertVerifier and updates the network
  // service as well if it's in use. See the documentation of the net class
  // for documentation on the methods.
  // Once all requests use the NetworkContext, even when network service is not
  // enabled, we can stop also updating net::MockCertVerifier here and always
  // go through the NetworkServiceTest mojo interface.
  class CertVerifier {
   public:
    explicit CertVerifier(net::MockCertVerifier* verifier);

    CertVerifier(const CertVerifier&) = delete;
    CertVerifier& operator=(const CertVerifier&) = delete;

    ~CertVerifier();
    void set_default_result(int default_result);
    void AddResultForCert(scoped_refptr<net::X509Certificate> cert,
                          const net::CertVerifyResult& verify_result,
                          int rv);
    void AddResultForCertAndHost(scoped_refptr<net::X509Certificate> cert,
                                 const std::string& host_pattern,
                                 const net::CertVerifyResult& verify_result,
                                 int rv);

   private:
    void EnsureNetworkServiceTestInitialized();

    raw_ptr<net::MockCertVerifier> verifier_;
    mojo::Remote<network::mojom::NetworkServiceTest> network_service_test_;
  };

  // Returns a pointer to the MockCertVerifier used by all profiles in
  // this test. This is what test code should use to mock certificate
  // verification.
  CertVerifier* mock_cert_verifier();

  // An internal getter to be used by test harness that wraps this class.
  net::MockCertVerifier* mock_cert_verifier_internal() {
    return mock_cert_verifier_.get();
  }

 private:
  std::unique_ptr<net::MockCertVerifier> mock_cert_verifier_;

  CertVerifier cert_verifier_;
};

}  // namespace content

#endif  // CONTENT_PUBLIC_TEST_CONTENT_MOCK_CERT_VERIFIER_H_