blob: c096af6ccee6614c8e115cefc0eb16f082353b82 [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"
8#include "base/memory/ptr_util.h"
csharrisonf30fc95f2016-08-19 21:43:449#include "base/test/fuzzed_data_provider.h"
mmenkec951d412016-04-28 19:05:2210#include "net/base/address_list.h"
11#include "net/base/ip_endpoint.h"
12#include "net/base/net_errors.h"
13#include "net/base/network_change_notifier.h"
mikecironef22f9812016-10-04 03:40:1914#include "net/log/net_log_with_source.h"
mmenkec951d412016-04-28 19:05:2215#include "net/socket/client_socket_handle.h"
16#include "net/socket/connection_attempts.h"
tfarina5dd13c22016-11-16 12:08:2617#include "net/socket/fuzzed_datagram_client_socket.h"
mmenkec951d412016-04-28 19:05:2218#include "net/socket/fuzzed_socket.h"
19#include "net/socket/ssl_client_socket.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:
30 FailingSSLClientSocket() {}
31 ~FailingSSLClientSocket() override {}
32
33 // Socket implementation:
34 int Read(IOBuffer* buf,
35 int buf_len,
36 const CompletionCallback& callback) override {
37 NOTREACHED();
38 return ERR_UNEXPECTED;
39 }
40
41 int Write(IOBuffer* buf,
42 int buf_len,
43 const CompletionCallback& callback) override {
44 NOTREACHED();
45 return ERR_UNEXPECTED;
46 }
47
48 int SetReceiveBufferSize(int32_t size) override { return OK; }
49 int SetSendBufferSize(int32_t size) override { return OK; }
50
51 // StreamSocket implementation:
52 int Connect(const CompletionCallback& callback) override {
53 return ERR_FAILED;
54 }
55
56 void Disconnect() override {}
57 bool IsConnected() const override { return false; }
58 bool IsConnectedAndIdle() const override { return false; }
59
60 int GetPeerAddress(IPEndPoint* address) const override {
61 return ERR_SOCKET_NOT_CONNECTED;
62 }
63 int GetLocalAddress(IPEndPoint* address) const override {
64 return ERR_SOCKET_NOT_CONNECTED;
65 }
66
tfarina42834112016-09-22 13:38:2067 const NetLogWithSource& NetLog() const override { return net_log_; }
mmenkec951d412016-04-28 19:05:2268
69 void SetSubresourceSpeculation() override {}
70 void SetOmniboxSpeculation() override {}
71
72 bool WasEverUsed() const override { return false; }
73
74 void EnableTCPFastOpenIfSupported() override {}
75
tfarina2846404c2016-12-25 14:31:3776 bool WasAlpnNegotiated() const override { return false; }
mmenkec951d412016-04-28 19:05:2277
78 NextProto GetNegotiatedProtocol() const override { return kProtoUnknown; }
79
80 bool GetSSLInfo(SSLInfo* ssl_info) override { return false; }
81
82 void GetConnectionAttempts(ConnectionAttempts* out) const override {
83 out->clear();
84 }
85
86 void ClearConnectionAttempts() override {}
87
88 void AddConnectionAttempts(const ConnectionAttempts& attempts) override {}
89
90 int64_t GetTotalReceivedBytes() const override { return 0; }
91
92 // SSLSocket implementation:
93 int ExportKeyingMaterial(const base::StringPiece& label,
94 bool has_context,
95 const base::StringPiece& context,
96 unsigned char* out,
97 unsigned int outlen) override {
98 NOTREACHED();
99 return 0;
100 }
101
102 // SSLClientSocket implementation:
103 void GetSSLCertRequestInfo(SSLCertRequestInfo* cert_request_info) override {}
104
mmenkec951d412016-04-28 19:05:22105 ChannelIDService* GetChannelIDService() const override {
106 NOTREACHED();
107 return nullptr;
108 }
109
nharper78e6d2b2016-09-21 05:42:35110 Error GetTokenBindingSignature(crypto::ECPrivateKey* key,
111 TokenBindingType tb_type,
112 std::vector<uint8_t>* out) override {
mmenkec951d412016-04-28 19:05:22113 NOTREACHED();
114 return ERR_UNEXPECTED;
115 }
116
117 crypto::ECPrivateKey* GetChannelIDKey() const override {
118 NOTREACHED();
119 return nullptr;
120 }
121
mmenkec951d412016-04-28 19:05:22122 private:
tfarina42834112016-09-22 13:38:20123 NetLogWithSource net_log_;
mmenkec951d412016-04-28 19:05:22124
125 DISALLOW_COPY_AND_ASSIGN(FailingSSLClientSocket);
126};
127
128} // namespace
129
csharrisonf30fc95f2016-08-19 21:43:44130FuzzedSocketFactory::FuzzedSocketFactory(
131 base::FuzzedDataProvider* data_provider)
morlovich833190ec2017-02-10 16:53:04132 : data_provider_(data_provider), fuzz_connect_result_(true) {}
mmenkec951d412016-04-28 19:05:22133
134FuzzedSocketFactory::~FuzzedSocketFactory() {}
135
136std::unique_ptr<DatagramClientSocket>
137FuzzedSocketFactory::CreateDatagramClientSocket(
138 DatagramSocket::BindType bind_type,
139 const RandIntCallback& rand_int_cb,
140 NetLog* net_log,
mikecironef22f9812016-10-04 03:40:19141 const NetLogSource& source) {
Jeremy Roman0579ed62017-08-29 15:56:19142 return std::make_unique<FuzzedDatagramClientSocket>(data_provider_);
mmenkec951d412016-04-28 19:05:22143}
144
145std::unique_ptr<StreamSocket> FuzzedSocketFactory::CreateTransportClientSocket(
146 const AddressList& addresses,
147 std::unique_ptr<SocketPerformanceWatcher> socket_performance_watcher,
148 NetLog* net_log,
mikecironef22f9812016-10-04 03:40:19149 const NetLogSource& source) {
mmenkec951d412016-04-28 19:05:22150 std::unique_ptr<FuzzedSocket> socket(
151 new FuzzedSocket(data_provider_, net_log));
morlovich833190ec2017-02-10 16:53:04152 socket->set_fuzz_connect_result(fuzz_connect_result_);
mmenkec951d412016-04-28 19:05:22153 // Just use the first address.
154 socket->set_remote_address(*addresses.begin());
155 return std::move(socket);
156}
157
158std::unique_ptr<SSLClientSocket> FuzzedSocketFactory::CreateSSLClientSocket(
159 std::unique_ptr<ClientSocketHandle> transport_socket,
160 const HostPortPair& host_and_port,
161 const SSLConfig& ssl_config,
162 const SSLClientSocketContext& context) {
Jeremy Roman0579ed62017-08-29 15:56:19163 return std::make_unique<FailingSSLClientSocket>();
mmenkec951d412016-04-28 19:05:22164}
165
166void FuzzedSocketFactory::ClearSSLSessionCache() {}
167
168} // namespace net