File: fake_udp_socket.h

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (135 lines) | stat: -rw-r--r-- 5,085 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
// 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_UDP_SOCKET_H_
#define CHROME_BROWSER_ASH_NET_NETWORK_DIAGNOSTICS_FAKE_UDP_SOCKET_H_

#include "base/memory/raw_ptr.h"
#include "base/memory/raw_span.h"
#include "base/time/time.h"
#include "content/public/test/browser_task_environment.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "net/base/net_errors.h"
#include "services/network/public/mojom/udp_socket.mojom.h"

namespace ash {
namespace network_diagnostics {

// Provides some UDP socket functionality in tests. Most methods, unless
// otherwise noted, are not expected to be called in tests or in a non Chrome OS
// environment.
class FakeUdpSocket : public network::mojom::UDPSocket {
 public:
  FakeUdpSocket();
  FakeUdpSocket(const FakeUdpSocket&) = delete;
  FakeUdpSocket& operator=(const FakeUdpSocket&) = delete;
  ~FakeUdpSocket() override;

  // network::mojom::UDPSocket:
  // Used in the fake.
  void Connect(const net::IPEndPoint& remote_addr,
               network::mojom::UDPSocketOptionsPtr options,
               ConnectCallback callback) override;
  void Bind(const net::IPEndPoint& local_addr,
            network::mojom::UDPSocketOptionsPtr options,
            BindCallback callback) override;
  void SetBroadcast(bool broadcast, SetBroadcastCallback callback) override;
  void SetSendBufferSize(int32_t send_buffer_size,
                         SetSendBufferSizeCallback callback) override;
  void SetReceiveBufferSize(int32_t receive_buffer_size,
                            SetSendBufferSizeCallback callback) override;
  void JoinGroup(const net::IPAddress& group_address,
                 JoinGroupCallback callback) override;
  void LeaveGroup(const net::IPAddress& group_address,
                  LeaveGroupCallback callback) override;
  // Used in the fake.
  void ReceiveMore(uint32_t num_additional_datagrams) override;
  void ReceiveMoreWithBufferSize(uint32_t num_additional_datagrams,
                                 uint32_t buffer_size) override;
  void SendTo(const net::IPEndPoint& dest_addr,
              base::span<const uint8_t> data,
              const net::MutableNetworkTrafficAnnotationTag& traffic_annotation,
              SendToCallback callback) override;
  // Used in the fake.
  void Send(base::span<const uint8_t> data,
            const net::MutableNetworkTrafficAnnotationTag& traffic_annotation,
            SendCallback callback) override;
  void Close() override;

  // Binds the pending receiver to |this|.
  void BindReceiver(mojo::PendingReceiver<network::mojom::UDPSocket> socket);

  // Binds the pending remote to a remote held by |this|.
  void BindRemote(
      mojo::PendingRemote<network::mojom::UDPSocketListener> socket_listener);

  void set_udp_connect_code(net::Error udp_connect_code) {
    udp_connect_code_ = udp_connect_code;
  }

  void set_udp_send_code(net::Error udp_send_code) {
    udp_send_code_ = udp_send_code;
  }

  void set_udp_on_received_code(net::Error udp_on_received_code) {
    udp_on_received_code_ = udp_on_received_code;
  }

  void set_udp_on_received_data(
      base::span<const uint8_t> udp_on_received_data) {
    udp_on_received_data_ = std::move(udp_on_received_data);
  }

  void set_disconnect_during_udp_connection_attempt(bool disconnect) {
    mojo_disconnect_on_connect_ = disconnect;
  }

  void set_disconnect_during_udp_send_attempt(bool disconnect) {
    mojo_disconnect_on_send_ = disconnect;
  }

  void set_disconnect_during_udp_receive_attempt(bool disconnect) {
    mojo_disconnect_on_receive_ = disconnect;
  }

  void set_task_environment_for_testing(
      content::BrowserTaskEnvironment* task_environment) {
    task_environment_ = task_environment;
  }

  void set_udp_connection_delay(base::TimeDelta connection_delay) {
    connection_delay_ = connection_delay;
  }

  void set_udp_send_delay(base::TimeDelta send_delay) {
    send_delay_ = send_delay;
  }

  void set_udp_receive_delay(base::TimeDelta receive_delay) {
    receive_delay_ = receive_delay;
  }

 private:
  mojo::Receiver<network::mojom::UDPSocket> receiver_{this};
  mojo::Remote<network::mojom::UDPSocketListener> remote_;
  net::Error udp_connect_code_ = net::ERR_FAILED;
  net::Error udp_send_code_ = net::ERR_FAILED;
  net::Error udp_on_received_code_ = net::ERR_FAILED;
  base::raw_span<const uint8_t> udp_on_received_data_ = {};
  bool mojo_disconnect_on_connect_ = false;
  bool mojo_disconnect_on_send_ = false;
  bool mojo_disconnect_on_receive_ = false;
  raw_ptr<content::BrowserTaskEnvironment> task_environment_;
  base::TimeDelta connection_delay_;
  base::TimeDelta send_delay_;
  base::TimeDelta receive_delay_;
};

}  // namespace network_diagnostics
}  // namespace ash

#endif  // CHROME_BROWSER_ASH_NET_NETWORK_DIAGNOSTICS_FAKE_UDP_SOCKET_H_