blob: eb8ae861edd9799d02d4035c359032d08b700950 [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
65 const BoundNetLog& NetLog() const override { return net_log_; }
66
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
108 Error GetSignedEKMForTokenBinding(crypto::ECPrivateKey* key,
109 std::vector<uint8_t>* out) override {
110 NOTREACHED();
111 return ERR_UNEXPECTED;
112 }
113
114 crypto::ECPrivateKey* GetChannelIDKey() const override {
115 NOTREACHED();
116 return nullptr;
117 }
118
mmenkec951d412016-04-28 19:05:22119 private:
120 BoundNetLog net_log_;
121
122 DISALLOW_COPY_AND_ASSIGN(FailingSSLClientSocket);
123};
124
125} // namespace
126
csharrisonf30fc95f2016-08-19 21:43:44127FuzzedSocketFactory::FuzzedSocketFactory(
128 base::FuzzedDataProvider* data_provider)
mmenkec951d412016-04-28 19:05:22129 : data_provider_(data_provider) {}
130
131FuzzedSocketFactory::~FuzzedSocketFactory() {}
132
133std::unique_ptr<DatagramClientSocket>
134FuzzedSocketFactory::CreateDatagramClientSocket(
135 DatagramSocket::BindType bind_type,
136 const RandIntCallback& rand_int_cb,
137 NetLog* net_log,
138 const NetLog::Source& source) {
mmenke91c17162016-06-02 16:03:23139 return base::WrapUnique(new FuzzedDatagramClientSocket(data_provider_));
mmenkec951d412016-04-28 19:05:22140}
141
142std::unique_ptr<StreamSocket> FuzzedSocketFactory::CreateTransportClientSocket(
143 const AddressList& addresses,
144 std::unique_ptr<SocketPerformanceWatcher> socket_performance_watcher,
145 NetLog* net_log,
146 const NetLog::Source& source) {
147 std::unique_ptr<FuzzedSocket> socket(
148 new FuzzedSocket(data_provider_, net_log));
149 socket->set_fuzz_connect_result(true);
150 // Just use the first address.
151 socket->set_remote_address(*addresses.begin());
152 return std::move(socket);
153}
154
155std::unique_ptr<SSLClientSocket> FuzzedSocketFactory::CreateSSLClientSocket(
156 std::unique_ptr<ClientSocketHandle> transport_socket,
157 const HostPortPair& host_and_port,
158 const SSLConfig& ssl_config,
159 const SSLClientSocketContext& context) {
160 return base::WrapUnique(new FailingSSLClientSocket());
161}
162
163void FuzzedSocketFactory::ClearSSLSessionCache() {}
164
165} // namespace net