blob: acf02309625d3ff1a0d2085f21890fefca62ac5d [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"
14#include "net/log/net_log.h"
15#include "net/socket/client_socket_handle.h"
16#include "net/socket/connection_attempts.h"
17#include "net/socket/fuzzed_socket.h"
18#include "net/socket/ssl_client_socket.h"
mmenke91c17162016-06-02 16:03:2319#include "net/udp/fuzzed_datagram_client_socket.h"
mmenkec951d412016-04-28 19:05:2220
21namespace net {
22
23namespace {
24
mmenkec951d412016-04-28 19:05:2225// SSLClientSocket implementation that always fails to connect.
26class FailingSSLClientSocket : public SSLClientSocket {
27 public:
28 FailingSSLClientSocket() {}
29 ~FailingSSLClientSocket() override {}
30
31 // Socket implementation:
32 int Read(IOBuffer* buf,
33 int buf_len,
34 const CompletionCallback& callback) override {
35 NOTREACHED();
36 return ERR_UNEXPECTED;
37 }
38
39 int Write(IOBuffer* buf,
40 int buf_len,
41 const CompletionCallback& callback) override {
42 NOTREACHED();
43 return ERR_UNEXPECTED;
44 }
45
46 int SetReceiveBufferSize(int32_t size) override { return OK; }
47 int SetSendBufferSize(int32_t size) override { return OK; }
48
49 // StreamSocket implementation:
50 int Connect(const CompletionCallback& callback) override {
51 return ERR_FAILED;
52 }
53
54 void Disconnect() override {}
55 bool IsConnected() const override { return false; }
56 bool IsConnectedAndIdle() const override { return false; }
57
58 int GetPeerAddress(IPEndPoint* address) const override {
59 return ERR_SOCKET_NOT_CONNECTED;
60 }
61 int GetLocalAddress(IPEndPoint* address) const override {
62 return ERR_SOCKET_NOT_CONNECTED;
63 }
64
tfarina42834112016-09-22 13:38:2065 const NetLogWithSource& NetLog() const override { return net_log_; }
mmenkec951d412016-04-28 19:05:2266
67 void SetSubresourceSpeculation() override {}
68 void SetOmniboxSpeculation() override {}
69
70 bool WasEverUsed() const override { return false; }
71
72 void EnableTCPFastOpenIfSupported() override {}
73
74 bool WasNpnNegotiated() const override { return false; }
75
76 NextProto GetNegotiatedProtocol() const override { return kProtoUnknown; }
77
78 bool GetSSLInfo(SSLInfo* ssl_info) override { return false; }
79
80 void GetConnectionAttempts(ConnectionAttempts* out) const override {
81 out->clear();
82 }
83
84 void ClearConnectionAttempts() override {}
85
86 void AddConnectionAttempts(const ConnectionAttempts& attempts) override {}
87
88 int64_t GetTotalReceivedBytes() const override { return 0; }
89
90 // SSLSocket implementation:
91 int ExportKeyingMaterial(const base::StringPiece& label,
92 bool has_context,
93 const base::StringPiece& context,
94 unsigned char* out,
95 unsigned int outlen) override {
96 NOTREACHED();
97 return 0;
98 }
99
100 // SSLClientSocket implementation:
101 void GetSSLCertRequestInfo(SSLCertRequestInfo* cert_request_info) override {}
102
mmenkec951d412016-04-28 19:05:22103 ChannelIDService* GetChannelIDService() const override {
104 NOTREACHED();
105 return nullptr;
106 }
107
nharper78e6d2b2016-09-21 05:42:35108 Error GetTokenBindingSignature(crypto::ECPrivateKey* key,
109 TokenBindingType tb_type,
110 std::vector<uint8_t>* out) override {
mmenkec951d412016-04-28 19:05:22111 NOTREACHED();
112 return ERR_UNEXPECTED;
113 }
114
115 crypto::ECPrivateKey* GetChannelIDKey() const override {
116 NOTREACHED();
117 return nullptr;
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)
mmenkec951d412016-04-28 19:05:22130 : data_provider_(data_provider) {}
131
132FuzzedSocketFactory::~FuzzedSocketFactory() {}
133
134std::unique_ptr<DatagramClientSocket>
135FuzzedSocketFactory::CreateDatagramClientSocket(
136 DatagramSocket::BindType bind_type,
137 const RandIntCallback& rand_int_cb,
138 NetLog* net_log,
139 const NetLog::Source& source) {
ricea2deef682016-09-09 08:04:07140 return base::MakeUnique<FuzzedDatagramClientSocket>(data_provider_);
mmenkec951d412016-04-28 19:05:22141}
142
143std::unique_ptr<StreamSocket> FuzzedSocketFactory::CreateTransportClientSocket(
144 const AddressList& addresses,
145 std::unique_ptr<SocketPerformanceWatcher> socket_performance_watcher,
146 NetLog* net_log,
147 const NetLog::Source& source) {
148 std::unique_ptr<FuzzedSocket> socket(
149 new FuzzedSocket(data_provider_, net_log));
150 socket->set_fuzz_connect_result(true);
151 // 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) {
ricea2deef682016-09-09 08:04:07161 return base::MakeUnique<FailingSSLClientSocket>();
mmenkec951d412016-04-28 19:05:22162}
163
164void FuzzedSocketFactory::ClearSSLSessionCache() {}
165
166} // namespace net