File: transport_client_socket_pool_test_util.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (159 lines) | stat: -rw-r--r-- 5,949 bytes parent folder | download | duplicates (9)
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Test methods and classes common to transport_client_socket_pool_unittest.cc
// and websocket_transport_client_socket_pool_unittest.cc. If you find you need
// to use these for another purpose, consider moving them to socket_test_util.h.

#ifndef NET_SOCKET_TRANSPORT_CLIENT_SOCKET_POOL_TEST_UTIL_H_
#define NET_SOCKET_TRANSPORT_CLIENT_SOCKET_POOL_TEST_UTIL_H_

#include <memory>
#include <optional>
#include <string>

#include "base/compiler_specific.h"
#include "base/containers/queue.h"
#include "base/containers/span.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/raw_span.h"
#include "base/time/time.h"
#include "net/base/address_list.h"
#include "net/socket/client_socket_factory.h"
#include "net/socket/client_socket_handle.h"
#include "net/socket/socket_performance_watcher.h"
#include "net/socket/stream_socket.h"

namespace net {

class ClientSocketHandle;
class IPEndPoint;
class NetLog;

// Make sure |handle| sets load times correctly when it has been assigned a
// reused socket. Uses gtest expectations.
void TestLoadTimingInfoConnectedReused(const ClientSocketHandle& handle);

// Make sure |handle| sets load times correctly when it has been assigned a
// fresh socket.  Also runs TestLoadTimingInfoConnectedReused, since the owner
// of a connection where |is_reused| is false may consider the connection
// reused. Uses gtest expectations.
void TestLoadTimingInfoConnectedNotReused(const ClientSocketHandle& handle);

// Set |address| to 1.1.1.1:80
void SetIPv4Address(IPEndPoint* address);

// Set |address| to [1:abcd::3:4:ff]:80
void SetIPv6Address(IPEndPoint* address);

// A ClientSocketFactory that produces sockets with the specified connection
// behaviours.
class MockTransportClientSocketFactory : public ClientSocketFactory {
 public:
  // The type of socket to create.
  enum class Type {
    // An unexpected socket. Causes a test failure if run.
    kUnexpected,
    // Connects successfully, synchronously.
    kSynchronous,
    // Fails to connect, synchronously.
    kFailing,
    // Connects successfully, asynchronously.
    kPending,
    // Fails to connect, asynchronously.
    kPendingFailing,
    // A delayed socket will pause before connecting through the message loop.
    kDelayed,
    // A delayed socket that fails.
    kDelayedFailing,
    // A stalled socket that never connects at all.
    kStalled,
    // A socket that can be triggered to connect explicitly, asynchronously.
    kTriggerable,
  };

  // A rule describing a mock `TransportClientSocket` to create.
  struct Rule {
    explicit Rule(Type type,
                  std::optional<std::vector<IPEndPoint>> expected_addresses =
                      std::nullopt,
                  Error connect_error = ERR_CONNECTION_FAILED);
    ~Rule();
    Rule(const Rule&);
    Rule& operator=(const Rule&);

    Type type;
    // If specified, the addresses that should be passed into
    // `CreateTransportClientSocket`.
    std::optional<std::vector<IPEndPoint>> expected_addresses;
    // The error to use if `type` specifies a failing connection. Ignored
    // otherwise.
    Error connect_error;
  };

  explicit MockTransportClientSocketFactory(NetLog* net_log);

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

  ~MockTransportClientSocketFactory() override;

  std::unique_ptr<DatagramClientSocket> CreateDatagramClientSocket(
      DatagramSocket::BindType bind_type,
      NetLog* net_log,
      const NetLogSource& source) override;

  std::unique_ptr<TransportClientSocket> CreateTransportClientSocket(
      const AddressList& addresses,
      std::unique_ptr<
          SocketPerformanceWatcher> /* socket_performance_watcher */,
      NetworkQualityEstimator* /* network_quality_estimator */,
      NetLog* /* net_log */,
      const NetLogSource& /* source */) override;

  std::unique_ptr<SSLClientSocket> CreateSSLClientSocket(
      SSLClientContext* context,
      std::unique_ptr<StreamSocket> nested_socket,
      const HostPortPair& host_and_port,
      const SSLConfig& ssl_config) override;

  int allocation_count() const { return allocation_count_; }

  // Set the default type for `CreateTransportClientSocket` calls, if all rules
  // (see `SetRules`) are consumed.
  void set_default_client_socket_type(Type type) { client_socket_type_ = type; }

  // Configures a list of rules for `CreateTransportClientSocket`. `rules` must
  // outlive the `MockTransportClientSocketFactory`. If
  // `CreateTransportClientSocket` is called more than `rules.size()` times,
  // excess calls will be treated as test failures, but this can be changed by
  // calling `set_default_client_socket_type` after this method.
  void SetRules(base::span<const Rule> rules);

  void set_delay(base::TimeDelta delay) { delay_ = delay; }

  // If one or more `kTriggerable` socket has already been created, then returns
  // a `OnceClosure` that can be called to cause the first not-yet-connected one
  // to connect. If no `kTriggerable` sockets have been created yet, wait for
  // one to be created before returning the `OnceClosure`. This method should be
  // called the same number of times as `kTriggerable` sockets are created in
  // the test.
  base::OnceClosure WaitForTriggerableSocketCreation();

 private:
  raw_ptr<NetLog> net_log_;
  int allocation_count_ = 0;
  Type client_socket_type_ = Type::kSynchronous;
  base::raw_span<const Rule, DanglingUntriaged> rules_;
  base::TimeDelta delay_;
  base::queue<base::OnceClosure> triggerable_sockets_;
  base::OnceClosure run_loop_quit_closure_;
};

}  // namespace net

#endif  // NET_SOCKET_TRANSPORT_CLIENT_SOCKET_POOL_TEST_UTIL_H_