• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "net/filter/mock_source_stream.h"
6 
7 #include <utility>
8 
9 #include "base/check_op.h"
10 #include "net/base/io_buffer.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 
13 namespace net {
14 
MockSourceStream()15 MockSourceStream::MockSourceStream() : SourceStream(SourceStream::TYPE_NONE) {}
16 
~MockSourceStream()17 MockSourceStream::~MockSourceStream() {
18   DCHECK(!awaiting_completion_);
19   // All data should have been consumed.
20   EXPECT_TRUE(results_.empty());
21 }
22 
Read(IOBuffer * dest_buffer,int buffer_size,CompletionOnceCallback callback)23 int MockSourceStream::Read(IOBuffer* dest_buffer,
24                            int buffer_size,
25                            CompletionOnceCallback callback) {
26   DCHECK(!awaiting_completion_);
27   DCHECK(!results_.empty());
28 
29   if (results_.empty())
30     return ERR_UNEXPECTED;
31 
32   QueuedResult r = results_.front();
33   DCHECK_GE(buffer_size, r.len);
34   if (r.mode == ASYNC) {
35     awaiting_completion_ = true;
36     dest_buffer_ = dest_buffer;
37     dest_buffer_size_ = buffer_size;
38     callback_ = std::move(callback);
39     return ERR_IO_PENDING;
40   }
41 
42   results_.pop();
43   memcpy(dest_buffer->data(), r.data, r.len);
44   return r.error == OK ? r.len : r.error;
45 }
46 
Description() const47 std::string MockSourceStream::Description() const {
48   return "";
49 }
50 
MayHaveMoreBytes() const51 bool MockSourceStream::MayHaveMoreBytes() const {
52   if (always_report_has_more_bytes_)
53     return true;
54   return !results_.empty();
55 }
56 
QueuedResult(const char * data,int len,Error error,Mode mode)57 MockSourceStream::QueuedResult::QueuedResult(const char* data,
58                                              int len,
59                                              Error error,
60                                              Mode mode)
61     : data(data), len(len), error(error), mode(mode) {}
62 
AddReadResult(const char * data,int len,Error error,Mode mode)63 void MockSourceStream::AddReadResult(const char* data,
64                                      int len,
65                                      Error error,
66                                      Mode mode) {
67   if (error != OK) {
68     // Doesn't make any sense to have both an error and data.
69     DCHECK_EQ(len, 0);
70   } else {
71     // The read result must be between 0 and 32k (inclusive) because the read
72     // buffer used in FilterSourceStream is 32k.
73     DCHECK_GE(32 * 1024, len);
74     DCHECK_LE(0, len);
75   }
76 
77   if (len > 0 && read_one_byte_at_a_time_) {
78     for (int i = 0; i < len; ++i) {
79       QueuedResult result(data + i, 1, OK, mode);
80       results_.push(result);
81     }
82     return;
83   }
84 
85   QueuedResult result(data, len, error, mode);
86   results_.push(result);
87 }
88 
CompleteNextRead()89 void MockSourceStream::CompleteNextRead() {
90   DCHECK(awaiting_completion_);
91 
92   awaiting_completion_ = false;
93   QueuedResult r = results_.front();
94   DCHECK_EQ(ASYNC, r.mode);
95   results_.pop();
96   DCHECK_GE(dest_buffer_size_, r.len);
97   memcpy(dest_buffer_->data(), r.data, r.len);
98   dest_buffer_ = nullptr;
99   std::move(callback_).Run(r.error == OK ? r.len : r.error);
100 }
101 
102 }  // namespace net
103