blob: 0d9cd16f89af598b0a71bf9e743f4f17e50645c7 [file] [log] [blame]
morlovich5e6e19b2017-01-30 14:38:161// 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éky3796f162018-07-31 14:11:2215#include "net/base/completion_once_callback.h"
morlovich5e6e19b2017-01-30 14:38:1616#include "net/base/ip_endpoint.h"
17#include "net/socket/server_socket.h"
18
19namespace base {
20class FuzzedDataProvider;
21}
22
23namespace net {
24
25class NetLog;
26class 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.
32class 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éky3796f162018-07-31 14:11:2245 CompletionOnceCallback callback) override;
morlovich5e6e19b2017-01-30 14:38:1646
47 private:
48 void DispatchAccept(std::unique_ptr<StreamSocket>* socket,
Bence Béky3796f162018-07-31 14:11:2249 CompletionOnceCallback callback);
morlovich5e6e19b2017-01-30 14:38:1650
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_