morlovich | 5e6e19b | 2017-01-30 14:38:16 | [diff] [blame] | 1 | // Copyright (c) 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 | #ifndef NET_SOCKET_FUZZED_SERVER_SOCKET_H_ |
| 6 | #define NET_SOCKET_FUZZED_SERVER_SOCKET_H_ |
| 7 | |
| 8 | #include <stdint.h> |
| 9 | |
| 10 | #include <memory> |
| 11 | #include <string> |
| 12 | |
| 13 | #include "base/macros.h" |
| 14 | #include "base/memory/weak_ptr.h" |
Bence Béky | 3796f16 | 2018-07-31 14:11:22 | [diff] [blame] | 15 | #include "net/base/completion_once_callback.h" |
morlovich | 5e6e19b | 2017-01-30 14:38:16 | [diff] [blame] | 16 | #include "net/base/ip_endpoint.h" |
| 17 | #include "net/socket/server_socket.h" |
| 18 | |
| 19 | namespace base { |
| 20 | class FuzzedDataProvider; |
| 21 | } |
| 22 | |
| 23 | namespace net { |
| 24 | |
| 25 | class NetLog; |
| 26 | class StreamSocket; |
| 27 | |
| 28 | // A ServerSocket that uses a FuzzedDataProvider to generate the input the |
| 29 | // server receives. It succeeds in Accept()ing, asynchronously, a single |
| 30 | // connection with that input; later calls to Accept will just return |
| 31 | // ERR_IO_PENDING but will not invoke the callback. |
| 32 | class FuzzedServerSocket : public ServerSocket { |
| 33 | public: |
| 34 | // |data_provider| is used as to determine behavior of the socket. It |
| 35 | // must remain valid until after both this object and the StreamSocket |
| 36 | // produced by Accept are destroyed. |
| 37 | FuzzedServerSocket(base::FuzzedDataProvider* data_provider, |
| 38 | net::NetLog* net_log); |
| 39 | ~FuzzedServerSocket() override; |
| 40 | |
| 41 | int Listen(const IPEndPoint& address, int backlog) override; |
| 42 | int GetLocalAddress(IPEndPoint* address) const override; |
| 43 | |
| 44 | int Accept(std::unique_ptr<StreamSocket>* socket, |
Bence Béky | 3796f16 | 2018-07-31 14:11:22 | [diff] [blame] | 45 | CompletionOnceCallback callback) override; |
morlovich | 5e6e19b | 2017-01-30 14:38:16 | [diff] [blame] | 46 | |
| 47 | private: |
| 48 | void DispatchAccept(std::unique_ptr<StreamSocket>* socket, |
Bence Béky | 3796f16 | 2018-07-31 14:11:22 | [diff] [blame] | 49 | CompletionOnceCallback callback); |
morlovich | 5e6e19b | 2017-01-30 14:38:16 | [diff] [blame] | 50 | |
| 51 | base::FuzzedDataProvider* data_provider_; |
| 52 | net::NetLog* net_log_; |
| 53 | |
| 54 | IPEndPoint listening_on_; |
| 55 | bool first_accept_; |
| 56 | bool listen_called_; |
| 57 | |
| 58 | base::WeakPtrFactory<FuzzedServerSocket> weak_factory_; |
| 59 | DISALLOW_COPY_AND_ASSIGN(FuzzedServerSocket); |
| 60 | }; |
| 61 | |
| 62 | } // namespace net |
| 63 | |
| 64 | #endif // NET_SOCKET_FUZZED_SERVER_SOCKET_H_ |