File: fake_network_context.h

package info (click to toggle)
chromium 139.0.7258.127-1~deb13u1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,096 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 (175 lines) | stat: -rw-r--r-- 6,976 bytes parent folder | download | duplicates (6)
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_ASH_NET_NETWORK_DIAGNOSTICS_FAKE_NETWORK_CONTEXT_H_
#define CHROME_BROWSER_ASH_NET_NETWORK_DIAGNOSTICS_FAKE_NETWORK_CONTEXT_H_

#include <memory>
#include <optional>
#include <utility>

#include "base/containers/circular_deque.h"
#include "base/containers/span.h"
#include "chrome/browser/ash/net/network_diagnostics/fake_tcp_connected_socket.h"
#include "chrome/browser/ash/net/network_diagnostics/fake_udp_socket.h"
#include "services/network/test/test_network_context.h"

namespace ash::network_diagnostics {

// Used in unit tests, the FakeNetworkContext class simulates the behavior of a
// network context.
class FakeNetworkContext : public network::TestNetworkContext {
 public:
  struct DnsResult {
   public:
    DnsResult(int32_t result,
              net::ResolveErrorInfo resolve_error_info,
              std::optional<net::AddressList> resolved_addresses,
              std::optional<net::HostResolverEndpointResults>
                  endpoint_results_with_metadata);
    ~DnsResult();

    int result_;
    net::ResolveErrorInfo resolve_error_info_;
    std::optional<net::AddressList> resolved_addresses_;
    std::optional<net::HostResolverEndpointResults>
        endpoint_results_with_metadata_;
  };
  FakeNetworkContext();
  FakeNetworkContext(const FakeNetworkContext&) = delete;
  FakeNetworkContext& operator=(const FakeNetworkContext&) = delete;
  ~FakeNetworkContext() override;

  // network::TestNetworkContext:
  void ResolveHost(
      network::mojom::HostResolverHostPtr host,
      const net::NetworkAnonymizationKey& network_anonymization_key,
      network::mojom::ResolveHostParametersPtr optional_parameters,
      mojo::PendingRemote<network::mojom::ResolveHostClient> response_client)
      override;

  void CreateTCPConnectedSocket(
      const std::optional<net::IPEndPoint>& local_addr,
      const net::AddressList& remote_addr_list,
      network::mojom::TCPConnectedSocketOptionsPtr tcp_connected_socket_options,
      const net::MutableNetworkTrafficAnnotationTag& traffic_annotation,
      mojo::PendingReceiver<network::mojom::TCPConnectedSocket> socket,
      mojo::PendingRemote<network::mojom::SocketObserver> observer,
      CreateTCPConnectedSocketCallback callback) override;

  void CreateUDPSocket(
      mojo::PendingReceiver<network::mojom::UDPSocket> receiver,
      mojo::PendingRemote<network::mojom::UDPSocketListener> listener) override;

  // Sets the fake TCP connect code. TODO(khegde): Change this to
  // SetTCPConnectCompleteCode.
  void SetTCPConnectCode(std::optional<net::Error>& tcp_connect_code);

  // Sets the fake TLS upgrade code.
  void SetTLSUpgradeCode(std::optional<net::Error>& tls_upgrade_code);

  // Sets the fake UDP connect code.
  void SetUdpConnectCode(net::Error udp_connect_code);

  // Sets the fake UDP send code.
  void SetUdpSendCode(net::Error udp_send_code);

  // Sets the state to mimic a fake disconnect during a UDP send attempt.
  void SetDisconnectDuringUdpSendAttempt(bool disconnect);

  // Sets the fake UDP on received code.
  void SetUdpOnReceivedCode(net::Error udp_on_received_code);

  // Sets the fake UDP on received data.
  void SetUdpOnReceivedData(base::span<const uint8_t> udp_on_received_data);

  // Sets the state to mimic a fake disconnect after receiving successful send
  // confirmation, but before receiving any data.
  void SetDisconnectDuringUdpReceiveAttempt(bool disconnect);

  // Sets the task environment used in testing. Used to fast forward the clock.
  void SetTaskEnvironmentForTesting(
      content::BrowserTaskEnvironment* task_environment);

  // Sets the UDP connection delay.
  void SetUdpConnectionDelay(base::TimeDelta connection_delay);

  // Sets the UDP send delay.
  void SetUdpSendDelay(base::TimeDelta send_delay);

  // Sets the UDP receive delay.
  void SetUdpReceiveDelay(base::TimeDelta receive_delay);

  // Sets the fake DNS result. Used to test a single host resolution.
  void set_fake_dns_result(std::unique_ptr<DnsResult> fake_dns_result) {
    CHECK(fake_dns_results_.empty());
    fake_dns_result_ = std::move(fake_dns_result);
  }

  // Sets the deque of fake DNS results. Used to test a sequence of host
  // resolutions.
  void set_fake_dns_results(
      base::circular_deque<std::unique_ptr<DnsResult>> fake_dns_results) {
    CHECK(!fake_dns_result_);
    fake_dns_results_ = std::move(fake_dns_results);
  }

  // If set to true, the binding pipe will be disconnected when attempting to
  // connect.
  void set_disconnect_during_host_resolution(bool disconnect) {
    host_resolution_disconnect_ = disconnect;
  }

  // If set to true, the binding pipe will be disconnected when attempting to
  // connect.
  void set_disconnect_during_tcp_connection_attempt(bool disconnect) {
    tcp_connection_attempt_disconnect_ = disconnect;
  }

  // If set to true, the binding pipe will be disconnected when attempting to
  // connect.
  void set_disconnect_during_tls_upgrade_attempt(bool disconnect) {
    tls_upgrade_attempt_disconnect_ = disconnect;
  }

  // If set to true, the binding pipe will be disconnected when attempting to
  // connect.
  void set_disconnect_during_udp_connection_attempt(bool disconnect) {
    udp_connection_attempt_disconnect_ = disconnect;
  }

 private:
  // Fake DNS lookup result. Persists across resolutions.
  // Cannot be set together with |fake_dns_results_|.
  std::unique_ptr<DnsResult> fake_dns_result_;
  // Fake DNS lookup results -- for every query the resolver pops and returns
  // the front entry.
  // Cannot be set together with |fake_dns_result_|.
  base::circular_deque<std::unique_ptr<DnsResult>> fake_dns_results_;
  // Provides the TCP socket functionality for tests.
  std::unique_ptr<FakeTCPConnectedSocket> fake_tcp_connected_socket_;
  // Provides the UDP socket functionality for tests.
  std::unique_ptr<FakeUdpSocket> fake_udp_socket_;
  // TCP connect code corresponding to the connection attempt.
  net::Error tcp_connect_code_ = net::OK;
  // UDP connect code corresponding to the connection attempt.
  net::Error udp_connect_code_ = net::OK;

  // Used to mimic the scenario where network::mojom::HostResolver receiver
  // is disconnected.
  bool host_resolution_disconnect_ = false;
  // Used to mimic the scenario where network::mojom::TCPConnectedSocket
  // receiver is disconnected.
  bool tcp_connection_attempt_disconnect_ = false;
  // Used to mimic the scenario where network::mojom::TLSClientSocket receiver
  // is disconnected.
  bool tls_upgrade_attempt_disconnect_ = false;
  // Used to mimic the scenario where network::mojom::UDPSocket receiver is
  // disconnected while connecting.
  bool udp_connection_attempt_disconnect_ = false;
};

}  // namespace ash::network_diagnostics

#endif  // CHROME_BROWSER_ASH_NET_NETWORK_DIAGNOSTICS_FAKE_NETWORK_CONTEXT_H_