1 // Copyright 2017 The Chromium Authors
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_server_socket.h"
6
7 #include <utility>
8
9 #include "base/functional/bind.h"
10 #include "base/location.h"
11 #include "base/task/single_thread_task_runner.h"
12 #include "net/socket/fuzzed_socket.h"
13
14 namespace net {
15
FuzzedServerSocket(FuzzedDataProvider * data_provider,net::NetLog * net_log)16 FuzzedServerSocket::FuzzedServerSocket(FuzzedDataProvider* data_provider,
17 net::NetLog* net_log)
18 : data_provider_(data_provider), net_log_(net_log) {}
19
20 FuzzedServerSocket::~FuzzedServerSocket() = default;
21
Listen(const IPEndPoint & address,int backlog,std::optional<bool> ipv6_only)22 int FuzzedServerSocket::Listen(const IPEndPoint& address,
23 int backlog,
24 std::optional<bool> ipv6_only) {
25 DCHECK(!listen_called_);
26 listening_on_ = address;
27 listen_called_ = true;
28 return OK;
29 }
30
GetLocalAddress(IPEndPoint * address) const31 int FuzzedServerSocket::GetLocalAddress(IPEndPoint* address) const {
32 *address = listening_on_;
33 return OK;
34 }
35
Accept(std::unique_ptr<StreamSocket> * socket,CompletionOnceCallback callback)36 int FuzzedServerSocket::Accept(std::unique_ptr<StreamSocket>* socket,
37 CompletionOnceCallback callback) {
38 if (first_accept_) {
39 base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
40 FROM_HERE, base::BindOnce(&FuzzedServerSocket::DispatchAccept,
41 weak_factory_.GetWeakPtr(), socket,
42 std::move(callback)));
43 }
44 first_accept_ = false;
45
46 return ERR_IO_PENDING;
47 }
48
DispatchAccept(std::unique_ptr<StreamSocket> * socket,CompletionOnceCallback callback)49 void FuzzedServerSocket::DispatchAccept(std::unique_ptr<StreamSocket>* socket,
50 CompletionOnceCallback callback) {
51 std::unique_ptr<FuzzedSocket> connected_socket(
52 std::make_unique<FuzzedSocket>(data_provider_, net_log_));
53 // The Connect call should always succeed synchronously, without using the
54 // callback, since connected_socket->set_fuzz_connect_result(true) has not
55 // been called.
56 CHECK_EQ(net::OK, connected_socket->Connect(CompletionOnceCallback()));
57 *socket = std::move(connected_socket);
58 std::move(callback).Run(OK);
59 }
60
61 } // namespace net
62