mock_source_stream.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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_MOCK_SOURCE_STREAM_H_
  5. #define NET_FILTER_MOCK_SOURCE_STREAM_H_
  6. #include <string>
  7. #include "base/containers/queue.h"
  8. #include "base/memory/ref_counted.h"
  9. #include "net/base/completion_once_callback.h"
  10. #include "net/base/net_errors.h"
  11. #include "net/filter/source_stream.h"
  12. namespace net {
  13. class IOBuffer;
  14. // A SourceStream implementation used in tests. This allows tests to specify
  15. // what data to return for each Read() call.
  16. class MockSourceStream : public SourceStream {
  17. public:
  18. enum Mode {
  19. SYNC,
  20. ASYNC,
  21. };
  22. MockSourceStream();
  23. MockSourceStream(const MockSourceStream&) = delete;
  24. MockSourceStream& operator=(const MockSourceStream&) = delete;
  25. // The destructor will crash in debug build if there is any pending read.
  26. ~MockSourceStream() override;
  27. // SourceStream implementation
  28. int Read(IOBuffer* dest_buffer,
  29. int buffer_size,
  30. CompletionOnceCallback callback) override;
  31. std::string Description() const override;
  32. bool MayHaveMoreBytes() const override;
  33. // Enqueues a result to be returned by |Read|. This method does not make a
  34. // copy of |data|, so |data| must outlive this object. If |mode| is SYNC,
  35. // |Read| will return the supplied data synchronously; otherwise, consumer
  36. // needs to call |CompleteNextRead|
  37. void AddReadResult(const char* data, int len, Error error, Mode mode);
  38. // Completes a pending Read() call. Crash in debug build if there is no
  39. // pending read.
  40. void CompleteNextRead();
  41. // Affects behavior or AddReadResult. When set to true, each character in
  42. // |data| passed to AddReadResult will be read as an individual byte, instead
  43. // of all at once. Default to false.
  44. // Note that setting it only affects future calls to AddReadResult, not
  45. // previous ones.
  46. void set_read_one_byte_at_a_time(bool read_one_byte_at_a_time) {
  47. read_one_byte_at_a_time_ = read_one_byte_at_a_time;
  48. }
  49. void set_always_report_has_more_bytes(bool always_report_has_more_bytes) {
  50. always_report_has_more_bytes_ = always_report_has_more_bytes;
  51. }
  52. // Returns true if a read is waiting to be completed.
  53. bool awaiting_completion() const { return awaiting_completion_; }
  54. private:
  55. struct QueuedResult {
  56. QueuedResult(const char* data, int len, Error error, Mode mode);
  57. const char* data;
  58. const int len;
  59. const Error error;
  60. const Mode mode;
  61. };
  62. bool read_one_byte_at_a_time_ = false;
  63. bool always_report_has_more_bytes_ = true;
  64. base::queue<QueuedResult> results_;
  65. bool awaiting_completion_ = false;
  66. scoped_refptr<IOBuffer> dest_buffer_;
  67. CompletionOnceCallback callback_;
  68. int dest_buffer_size_ = 0;
  69. };
  70. } // namespace net
  71. #endif // NET_FILTER_MOCK_SOURCE_STREAM_H_