| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 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_server_socket.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/location.h" |
| 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/threading/thread_task_runner_handle.h" |
| 11 #include "net/socket/fuzzed_socket.h" |
| 12 |
| 13 namespace net { |
| 14 |
| 15 FuzzedServerSocket::FuzzedServerSocket(base::FuzzedDataProvider* data_provider, |
| 16 net::NetLog* net_log) |
| 17 : data_provider_(data_provider), |
| 18 net_log_(net_log), |
| 19 first_accept_(true), |
| 20 listen_called_(false), |
| 21 weak_factory_(this) {} |
| 22 |
| 23 FuzzedServerSocket::~FuzzedServerSocket() {} |
| 24 |
| 25 int FuzzedServerSocket::Listen(const IPEndPoint& address, int backlog) { |
| 26 DCHECK(!listen_called_); |
| 27 listening_on_ = address; |
| 28 listen_called_ = true; |
| 29 return OK; |
| 30 } |
| 31 |
| 32 int FuzzedServerSocket::GetLocalAddress(IPEndPoint* address) const { |
| 33 *address = listening_on_; |
| 34 return OK; |
| 35 } |
| 36 |
| 37 int FuzzedServerSocket::Accept(std::unique_ptr<StreamSocket>* socket, |
| 38 const CompletionCallback& callback) { |
| 39 if (first_accept_) { |
| 40 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 41 FROM_HERE, base::Bind(&FuzzedServerSocket::DispatchAccept, |
| 42 weak_factory_.GetWeakPtr(), socket, callback)); |
| 43 } |
| 44 first_accept_ = false; |
| 45 |
| 46 return ERR_IO_PENDING; |
| 47 } |
| 48 |
| 49 void FuzzedServerSocket::DispatchAccept(std::unique_ptr<StreamSocket>* socket, |
| 50 const CompletionCallback& callback) { |
| 51 std::unique_ptr<FuzzedSocket> connected_socket( |
| 52 base::MakeUnique<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(callback)); |
| 57 *socket = std::move(connected_socket); |
| 58 callback.Run(OK); |
| 59 } |
| 60 |
| 61 } // namespace net |
| OLD | NEW |