blob: 65a11fc5fe7ff624ca53a2015ab10a5d6844602b [file] [log] [blame]
mmenkec951d412016-04-28 19:05:221// 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#include "net/socket/fuzzed_socket_factory.h"
6
7#include "base/logging.h"
csharrisonf30fc95f2016-08-19 21:43:448#include "base/test/fuzzed_data_provider.h"
mmenkec951d412016-04-28 19:05:229#include "net/base/address_list.h"
10#include "net/base/ip_endpoint.h"
11#include "net/base/net_errors.h"
12#include "net/base/network_change_notifier.h"
mikecironef22f9812016-10-04 03:40:1913#include "net/log/net_log_with_source.h"
mmenkec951d412016-04-28 19:05:2214#include "net/socket/client_socket_handle.h"
15#include "net/socket/connection_attempts.h"
tfarina5dd13c22016-11-16 12:08:2616#include "net/socket/fuzzed_datagram_client_socket.h"
mmenkec951d412016-04-28 19:05:2217#include "net/socket/fuzzed_socket.h"
18#include "net/socket/ssl_client_socket.h"
[email protected]a2b2cfc2017-12-06 09:06:0819#include "net/traffic_annotation/network_traffic_annotation.h"
mmenkec951d412016-04-28 19:05:2220
21namespace net {
22
mikecironef22f9812016-10-04 03:40:1923class NetLog;
24
mmenkec951d412016-04-28 19:05:2225namespace {
26
mmenkec951d412016-04-28 19:05:2227// SSLClientSocket implementation that always fails to connect.
28class FailingSSLClientSocket : public SSLClientSocket {
29 public:
Chris Watkins7a41d3552017-12-01 02:13:2730 FailingSSLClientSocket() = default;
31 ~FailingSSLClientSocket() override = default;
mmenkec951d412016-04-28 19:05:2232
33 // Socket implementation:
34 int Read(IOBuffer* buf,
35 int buf_len,
Brad Lassey3a814172018-04-26 03:30:2136 CompletionOnceCallback callback) override {
mmenkec951d412016-04-28 19:05:2237 NOTREACHED();
38 return ERR_UNEXPECTED;
39 }
40
41 int Write(IOBuffer* buf,
42 int buf_len,
Brad Lassey3a814172018-04-26 03:30:2143 CompletionOnceCallback callback,
[email protected]578968d42017-12-13 15:39:3244 const NetworkTrafficAnnotationTag& traffic_annotation) override {
mmenkec951d412016-04-28 19:05:2245 NOTREACHED();
46 return ERR_UNEXPECTED;
47 }
48
49 int SetReceiveBufferSize(int32_t size) override { return OK; }
50 int SetSendBufferSize(int32_t size) override { return OK; }
51
52 // StreamSocket implementation:
Brad Lassey3a814172018-04-26 03:30:2153 int Connect(CompletionOnceCallback callback) override { return ERR_FAILED; }
mmenkec951d412016-04-28 19:05:2254
55 void Disconnect() override {}
56 bool IsConnected() const override { return false; }
57 bool IsConnectedAndIdle() const override { return false; }
58
59 int GetPeerAddress(IPEndPoint* address) const override {
60 return ERR_SOCKET_NOT_CONNECTED;
61 }
62 int GetLocalAddress(IPEndPoint* address) const override {
63 return ERR_SOCKET_NOT_CONNECTED;
64 }
65
tfarina42834112016-09-22 13:38:2066 const NetLogWithSource& NetLog() const override { return net_log_; }
mmenkec951d412016-04-28 19:05:2267
mmenkec951d412016-04-28 19:05:2268 bool WasEverUsed() const override { return false; }
69
70 void EnableTCPFastOpenIfSupported() override {}
71
tfarina2846404c2016-12-25 14:31:3772 bool WasAlpnNegotiated() const override { return false; }
mmenkec951d412016-04-28 19:05:2273
74 NextProto GetNegotiatedProtocol() const override { return kProtoUnknown; }
75
76 bool GetSSLInfo(SSLInfo* ssl_info) override { return false; }
77
78 void GetConnectionAttempts(ConnectionAttempts* out) const override {
79 out->clear();
80 }
81
82 void ClearConnectionAttempts() override {}
83
84 void AddConnectionAttempts(const ConnectionAttempts& attempts) override {}
85
86 int64_t GetTotalReceivedBytes() const override { return 0; }
87
Bence Békydae8af5f2018-04-13 08:53:1788 void GetSSLCertRequestInfo(
89 SSLCertRequestInfo* cert_request_info) const override {}
mmenkec951d412016-04-28 19:05:2290
mmenkec951d412016-04-28 19:05:2291 ChannelIDService* GetChannelIDService() const override {
92 NOTREACHED();
93 return nullptr;
94 }
95
nharper78e6d2b2016-09-21 05:42:3596 Error GetTokenBindingSignature(crypto::ECPrivateKey* key,
97 TokenBindingType tb_type,
98 std::vector<uint8_t>* out) override {
mmenkec951d412016-04-28 19:05:2299 NOTREACHED();
100 return ERR_UNEXPECTED;
101 }
102
103 crypto::ECPrivateKey* GetChannelIDKey() const override {
104 NOTREACHED();
105 return nullptr;
106 }
107
Bence Békydae8af5f2018-04-13 08:53:17108 void ApplySocketTag(const net::SocketTag& tag) override {}
109
110 // SSLSocket implementation:
111 int ExportKeyingMaterial(const base::StringPiece& label,
112 bool has_context,
113 const base::StringPiece& context,
114 unsigned char* out,
115 unsigned int outlen) override {
116 NOTREACHED();
117 return 0;
118 }
119
mmenkec951d412016-04-28 19:05:22120 private:
tfarina42834112016-09-22 13:38:20121 NetLogWithSource net_log_;
mmenkec951d412016-04-28 19:05:22122
123 DISALLOW_COPY_AND_ASSIGN(FailingSSLClientSocket);
124};
125
126} // namespace
127
csharrisonf30fc95f2016-08-19 21:43:44128FuzzedSocketFactory::FuzzedSocketFactory(
129 base::FuzzedDataProvider* data_provider)
morlovich833190ec2017-02-10 16:53:04130 : data_provider_(data_provider), fuzz_connect_result_(true) {}
mmenkec951d412016-04-28 19:05:22131
Chris Watkins7a41d3552017-12-01 02:13:27132FuzzedSocketFactory::~FuzzedSocketFactory() = default;
mmenkec951d412016-04-28 19:05:22133
134std::unique_ptr<DatagramClientSocket>
135FuzzedSocketFactory::CreateDatagramClientSocket(
136 DatagramSocket::BindType bind_type,
mmenkec951d412016-04-28 19:05:22137 NetLog* net_log,
mikecironef22f9812016-10-04 03:40:19138 const NetLogSource& source) {
Jeremy Roman0579ed62017-08-29 15:56:19139 return std::make_unique<FuzzedDatagramClientSocket>(data_provider_);
mmenkec951d412016-04-28 19:05:22140}
141
Helen Lid5bb9222018-04-12 15:33:09142std::unique_ptr<TransportClientSocket>
143FuzzedSocketFactory::CreateTransportClientSocket(
mmenkec951d412016-04-28 19:05:22144 const AddressList& addresses,
145 std::unique_ptr<SocketPerformanceWatcher> socket_performance_watcher,
146 NetLog* net_log,
mikecironef22f9812016-10-04 03:40:19147 const NetLogSource& source) {
mmenkec951d412016-04-28 19:05:22148 std::unique_ptr<FuzzedSocket> socket(
149 new FuzzedSocket(data_provider_, net_log));
morlovich833190ec2017-02-10 16:53:04150 socket->set_fuzz_connect_result(fuzz_connect_result_);
mmenkec951d412016-04-28 19:05:22151 // Just use the first address.
152 socket->set_remote_address(*addresses.begin());
153 return std::move(socket);
154}
155
156std::unique_ptr<SSLClientSocket> FuzzedSocketFactory::CreateSSLClientSocket(
157 std::unique_ptr<ClientSocketHandle> transport_socket,
158 const HostPortPair& host_and_port,
159 const SSLConfig& ssl_config,
160 const SSLClientSocketContext& context) {
Jeremy Roman0579ed62017-08-29 15:56:19161 return std::make_unique<FailingSSLClientSocket>();
mmenkec951d412016-04-28 19:05:22162}
163
Helen Liac3c51e2018-04-24 00:02:13164std::unique_ptr<ProxyClientSocket> FuzzedSocketFactory::CreateProxyClientSocket(
165 std::unique_ptr<ClientSocketHandle> transport_socket,
166 const std::string& user_agent,
167 const HostPortPair& endpoint,
168 HttpAuthController* http_auth_controller,
169 bool tunnel,
170 bool using_spdy,
171 NextProto negotiated_protocol,
172 bool is_https_proxy,
173 const NetworkTrafficAnnotationTag& traffic_annotation) {
174 NOTIMPLEMENTED();
175 return nullptr;
176}
177
mmenkec951d412016-04-28 19:05:22178void FuzzedSocketFactory::ClearSSLSessionCache() {}
179
180} // namespace net