fuzzed_socket_factory.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright 2016 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_SOCKET_FACTORY_H_
  5. #define NET_SOCKET_FUZZED_SOCKET_FACTORY_H_
  6. #include <memory>
  7. #include <string>
  8. #include "net/socket/client_socket_factory.h"
  9. class FuzzedDataProvider;
  10. namespace net {
  11. // A socket factory that creates FuzzedSockets that share the same
  12. // FuzzedDataProvider. To behave consistently, the read operations on all
  13. // sockets must be the same, and in the same order (both on each socket, and
  14. // between sockets).
  15. //
  16. // Currently doesn't support SSL sockets - just returns sockets that
  17. // synchronously fail to connect when trying to create either type of socket.
  18. // TODO(mmenke): Add support for ssl sockets.
  19. // TODO(mmenke): add fuzzing for generation of valid cryptographically signed
  20. // messages.
  21. class FuzzedSocketFactory : public ClientSocketFactory {
  22. public:
  23. // |data_provider| must outlive the FuzzedSocketFactory, and all sockets it
  24. // creates. Other objects can also continue to consume |data_provider|, as
  25. // long as their calls into it are made on the CLientSocketFactory's thread
  26. // and the calls are deterministic.
  27. explicit FuzzedSocketFactory(FuzzedDataProvider* data_provider);
  28. FuzzedSocketFactory(const FuzzedSocketFactory&) = delete;
  29. FuzzedSocketFactory& operator=(const FuzzedSocketFactory&) = delete;
  30. ~FuzzedSocketFactory() override;
  31. std::unique_ptr<DatagramClientSocket> CreateDatagramClientSocket(
  32. DatagramSocket::BindType bind_type,
  33. NetLog* net_log,
  34. const NetLogSource& source) override;
  35. std::unique_ptr<TransportClientSocket> CreateTransportClientSocket(
  36. const AddressList& addresses,
  37. std::unique_ptr<SocketPerformanceWatcher> socket_performance_watcher,
  38. NetworkQualityEstimator* network_quality_estimator,
  39. NetLog* net_log,
  40. const NetLogSource& source) override;
  41. std::unique_ptr<SSLClientSocket> CreateSSLClientSocket(
  42. SSLClientContext* context,
  43. std::unique_ptr<StreamSocket> stream_socket,
  44. const HostPortPair& host_and_port,
  45. const SSLConfig& ssl_config) override;
  46. // Sets whether Connect()ions on returned sockets can be asynchronously
  47. // delayed or outright fail. Defaults to true.
  48. void set_fuzz_connect_result(bool v) { fuzz_connect_result_ = v; }
  49. private:
  50. FuzzedDataProvider* data_provider_;
  51. bool fuzz_connect_result_ = true;
  52. };
  53. } // namespace net
  54. #endif // NET_SOCKET_FUZZED_SOCKET_FACTORY_H_