1 // Copyright 2016 The Chromium Authors. All rights reserved.
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/macros.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   ~FuzzedSocketFactory() override;
36 
37   std::unique_ptr<DatagramClientSocket> CreateDatagramClientSocket(
38       DatagramSocket::BindType bind_type,
39       NetLog* net_log,
40       const NetLogSource& source) override;
41 
42   std::unique_ptr<TransportClientSocket> CreateTransportClientSocket(
43       const AddressList& addresses,
44       std::unique_ptr<SocketPerformanceWatcher> socket_performance_watcher,
45       NetworkQualityEstimator* network_quality_estimator,
46       NetLog* net_log,
47       const NetLogSource& source) override;
48 
49   std::unique_ptr<SSLClientSocket> CreateSSLClientSocket(
50       SSLClientContext* context,
51       std::unique_ptr<StreamSocket> stream_socket,
52       const HostPortPair& host_and_port,
53       const SSLConfig& ssl_config) override;
54 
55   std::unique_ptr<ProxyClientSocket> CreateProxyClientSocket(
56       std::unique_ptr<StreamSocket> stream_socket,
57       const std::string& user_agent,
58       const HostPortPair& endpoint,
59       const ProxyServer& proxy_server,
60       HttpAuthController* http_auth_controller,
61       bool tunnel,
62       bool using_spdy,
63       NextProto negotiated_protocol,
64       ProxyDelegate* proxy_delegate,
65       const NetworkTrafficAnnotationTag& traffic_annotation) override;
66 
67   // Sets whether Connect()ions on returned sockets can be asynchronously
68   // delayed or outright fail. Defaults to true.
set_fuzz_connect_result(bool v)69   void set_fuzz_connect_result(bool v) { fuzz_connect_result_ = v; }
70 
71  private:
72   FuzzedDataProvider* data_provider_;
73   bool fuzz_connect_result_;
74 
75   DISALLOW_COPY_AND_ASSIGN(FuzzedSocketFactory);
76 };
77 
78 }  // namespace net
79 
80 #endif  // NET_SOCKET_FUZZED_SOCKET_FACTORY_H_
81