fuzzed_server_socket.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. #ifndef NET_SOCKET_FUZZED_SERVER_SOCKET_H_
  5. #define NET_SOCKET_FUZZED_SERVER_SOCKET_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include "base/memory/weak_ptr.h"
  9. #include "net/base/completion_once_callback.h"
  10. #include "net/base/ip_endpoint.h"
  11. #include "net/socket/server_socket.h"
  12. class FuzzedDataProvider;
  13. namespace net {
  14. class NetLog;
  15. class StreamSocket;
  16. // A ServerSocket that uses a FuzzedDataProvider to generate the input the
  17. // server receives. It succeeds in Accept()ing, asynchronously, a single
  18. // connection with that input; later calls to Accept will just return
  19. // ERR_IO_PENDING but will not invoke the callback.
  20. class FuzzedServerSocket : public ServerSocket {
  21. public:
  22. // |data_provider| is used as to determine behavior of the socket. It
  23. // must remain valid until after both this object and the StreamSocket
  24. // produced by Accept are destroyed.
  25. FuzzedServerSocket(FuzzedDataProvider* data_provider, net::NetLog* net_log);
  26. FuzzedServerSocket(const FuzzedServerSocket&) = delete;
  27. FuzzedServerSocket& operator=(const FuzzedServerSocket&) = delete;
  28. ~FuzzedServerSocket() override;
  29. int Listen(const IPEndPoint& address, int backlog) override;
  30. int GetLocalAddress(IPEndPoint* address) const override;
  31. int Accept(std::unique_ptr<StreamSocket>* socket,
  32. CompletionOnceCallback callback) override;
  33. private:
  34. void DispatchAccept(std::unique_ptr<StreamSocket>* socket,
  35. CompletionOnceCallback callback);
  36. FuzzedDataProvider* data_provider_;
  37. net::NetLog* net_log_;
  38. IPEndPoint listening_on_;
  39. bool first_accept_ = true;
  40. bool listen_called_ = false;
  41. base::WeakPtrFactory<FuzzedServerSocket> weak_factory_{this};
  42. };
  43. } // namespace net
  44. #endif // NET_SOCKET_FUZZED_SERVER_SOCKET_H_