xref: /aosp_15_r20/external/cronet/net/socket/fuzzed_socket_factory.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2016 The Chromium Authors
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 NET_SOCKET_FUZZED_SOCKET_FACTORY_H_
6 #define NET_SOCKET_FUZZED_SOCKET_FACTORY_H_
7 
8 #include <memory>
9 #include <string>
10 
11 #include "base/memory/raw_ptr.h"
12 #include "net/socket/client_socket_factory.h"
13 
14 class FuzzedDataProvider;
15 
16 namespace net {
17 
18 // A socket factory that creates FuzzedSockets that share the same
19 // FuzzedDataProvider. To behave consistently, the read operations on all
20 // sockets must be the same, and in the same order (both on each socket, and
21 // between sockets).
22 //
23 // Currently doesn't support SSL sockets - just returns sockets that
24 // synchronously fail to connect when trying to create either type of socket.
25 // TODO(mmenke): Add support for ssl sockets.
26 // TODO(mmenke): add fuzzing for generation of valid cryptographically signed
27 // messages.
28 class FuzzedSocketFactory : public ClientSocketFactory {
29  public:
30   // |data_provider| must outlive the FuzzedSocketFactory, and all sockets it
31   // creates. Other objects can also continue to consume |data_provider|, as
32   // long as their calls into it are made on the CLientSocketFactory's thread
33   // and the calls are deterministic.
34   explicit FuzzedSocketFactory(FuzzedDataProvider* data_provider);
35 
36   FuzzedSocketFactory(const FuzzedSocketFactory&) = delete;
37   FuzzedSocketFactory& operator=(const FuzzedSocketFactory&) = delete;
38 
39   ~FuzzedSocketFactory() override;
40 
41   std::unique_ptr<DatagramClientSocket> CreateDatagramClientSocket(
42       DatagramSocket::BindType bind_type,
43       NetLog* net_log,
44       const NetLogSource& source) override;
45 
46   std::unique_ptr<TransportClientSocket> CreateTransportClientSocket(
47       const AddressList& addresses,
48       std::unique_ptr<SocketPerformanceWatcher> socket_performance_watcher,
49       NetworkQualityEstimator* network_quality_estimator,
50       NetLog* net_log,
51       const NetLogSource& source) override;
52 
53   std::unique_ptr<SSLClientSocket> CreateSSLClientSocket(
54       SSLClientContext* context,
55       std::unique_ptr<StreamSocket> stream_socket,
56       const HostPortPair& host_and_port,
57       const SSLConfig& ssl_config) override;
58 
59   // Sets whether Connect()ions on returned sockets can be asynchronously
60   // delayed or outright fail. Defaults to true.
set_fuzz_connect_result(bool v)61   void set_fuzz_connect_result(bool v) { fuzz_connect_result_ = v; }
62 
63  private:
64   raw_ptr<FuzzedDataProvider> data_provider_;
65   bool fuzz_connect_result_ = true;
66 };
67 
68 }  // namespace net
69 
70 #endif  // NET_SOCKET_FUZZED_SOCKET_FACTORY_H_
71