blob: 504ee2aaf951b76edee17cb9259407c7031077f6 [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
mmenkec951d412016-04-28 19:05:2296 crypto::ECPrivateKey* GetChannelIDKey() const override {
97 NOTREACHED();
98 return nullptr;
99 }
100
Bence Békydae8af5f2018-04-13 08:53:17101 void ApplySocketTag(const net::SocketTag& tag) override {}
102
103 // SSLSocket implementation:
104 int ExportKeyingMaterial(const base::StringPiece& label,
105 bool has_context,
106 const base::StringPiece& context,
107 unsigned char* out,
108 unsigned int outlen) override {
109 NOTREACHED();
110 return 0;
111 }
112
mmenkec951d412016-04-28 19:05:22113 private:
tfarina42834112016-09-22 13:38:20114 NetLogWithSource net_log_;
mmenkec951d412016-04-28 19:05:22115
116 DISALLOW_COPY_AND_ASSIGN(FailingSSLClientSocket);
117};
118
119} // namespace
120
csharrisonf30fc95f2016-08-19 21:43:44121FuzzedSocketFactory::FuzzedSocketFactory(
122 base::FuzzedDataProvider* data_provider)
morlovich833190ec2017-02-10 16:53:04123 : data_provider_(data_provider), fuzz_connect_result_(true) {}
mmenkec951d412016-04-28 19:05:22124
Chris Watkins7a41d3552017-12-01 02:13:27125FuzzedSocketFactory::~FuzzedSocketFactory() = default;
mmenkec951d412016-04-28 19:05:22126
127std::unique_ptr<DatagramClientSocket>
128FuzzedSocketFactory::CreateDatagramClientSocket(
129 DatagramSocket::BindType bind_type,
mmenkec951d412016-04-28 19:05:22130 NetLog* net_log,
mikecironef22f9812016-10-04 03:40:19131 const NetLogSource& source) {
Jeremy Roman0579ed62017-08-29 15:56:19132 return std::make_unique<FuzzedDatagramClientSocket>(data_provider_);
mmenkec951d412016-04-28 19:05:22133}
134
Helen Lid5bb9222018-04-12 15:33:09135std::unique_ptr<TransportClientSocket>
136FuzzedSocketFactory::CreateTransportClientSocket(
mmenkec951d412016-04-28 19:05:22137 const AddressList& addresses,
138 std::unique_ptr<SocketPerformanceWatcher> socket_performance_watcher,
139 NetLog* net_log,
mikecironef22f9812016-10-04 03:40:19140 const NetLogSource& source) {
mmenkec951d412016-04-28 19:05:22141 std::unique_ptr<FuzzedSocket> socket(
142 new FuzzedSocket(data_provider_, net_log));
morlovich833190ec2017-02-10 16:53:04143 socket->set_fuzz_connect_result(fuzz_connect_result_);
mmenkec951d412016-04-28 19:05:22144 // Just use the first address.
145 socket->set_remote_address(*addresses.begin());
146 return std::move(socket);
147}
148
149std::unique_ptr<SSLClientSocket> FuzzedSocketFactory::CreateSSLClientSocket(
150 std::unique_ptr<ClientSocketHandle> transport_socket,
151 const HostPortPair& host_and_port,
152 const SSLConfig& ssl_config,
153 const SSLClientSocketContext& context) {
Jeremy Roman0579ed62017-08-29 15:56:19154 return std::make_unique<FailingSSLClientSocket>();
mmenkec951d412016-04-28 19:05:22155}
156
Helen Liac3c51e2018-04-24 00:02:13157std::unique_ptr<ProxyClientSocket> FuzzedSocketFactory::CreateProxyClientSocket(
158 std::unique_ptr<ClientSocketHandle> transport_socket,
159 const std::string& user_agent,
160 const HostPortPair& endpoint,
161 HttpAuthController* http_auth_controller,
162 bool tunnel,
163 bool using_spdy,
164 NextProto negotiated_protocol,
165 bool is_https_proxy,
166 const NetworkTrafficAnnotationTag& traffic_annotation) {
167 NOTIMPLEMENTED();
168 return nullptr;
169}
170
mmenkec951d412016-04-28 19:05:22171void FuzzedSocketFactory::ClearSSLSessionCache() {}
172
173} // namespace net