fuzzed_source_stream.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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_FILTER_FUZZED_SOURCE_STREAM_H_
  5. #define NET_FILTER_FUZZED_SOURCE_STREAM_H_
  6. #include <string>
  7. #include "base/memory/ref_counted.h"
  8. #include "net/base/completion_once_callback.h"
  9. #include "net/filter/source_stream.h"
  10. class FuzzedDataProvider;
  11. namespace net {
  12. class IOBuffer;
  13. // A SourceStream implementation used in tests. This allows tests to specify
  14. // what data to return for each Read() call.
  15. class FuzzedSourceStream : public SourceStream {
  16. public:
  17. // |data_provider| is used to determine behavior of the FuzzedSourceStream.
  18. // It must remain valid until after the FuzzedSocket is destroyed.
  19. explicit FuzzedSourceStream(FuzzedDataProvider* data_provider);
  20. FuzzedSourceStream(const FuzzedSourceStream&) = delete;
  21. FuzzedSourceStream& operator=(const FuzzedSourceStream&) = delete;
  22. ~FuzzedSourceStream() override;
  23. // SourceStream implementation
  24. int Read(IOBuffer* dest_buffer,
  25. int buffer_size,
  26. CompletionOnceCallback callback) override;
  27. std::string Description() const override;
  28. bool MayHaveMoreBytes() const override;
  29. private:
  30. void OnReadComplete(CompletionOnceCallback callback,
  31. const std::string& fuzzed_data,
  32. scoped_refptr<IOBuffer> read_buf,
  33. int result);
  34. FuzzedDataProvider* data_provider_;
  35. // Whether there is a pending Read().
  36. bool read_pending_ = false;
  37. // Last result returned by Read() is an error or 0.
  38. bool end_returned_ = false;
  39. };
  40. } // namespace net
  41. #endif // NET_FILTER_FUZZED_SOURCE_STREAM_H_