summaryrefslogtreecommitdiff
path: root/chromium/services/network/data_pipe_element_reader_unittest.cc
blob: d61561a805744c802d599c1c6abea7b21a46c3e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "services/network/data_pipe_element_reader.h"

#include <stdint.h>

#include <limits>
#include <memory>

#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/test/task_environment.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/system/data_pipe.h"
#include "mojo/public/cpp/system/data_pipe_utils.h"
#include "net/base/io_buffer.h"
#include "net/base/test_completion_callback.h"
#include "services/network/public/mojom/data_pipe_getter.mojom.h"
#include "testing/gtest/include/gtest/gtest.h"

// Most tests of this class are at the URLLoader layer. These tests focus on
// things too difficult to cover with integration tests.

namespace network {

namespace {

class PassThroughDataPipeGetter : public mojom::DataPipeGetter {
 public:
  explicit PassThroughDataPipeGetter() = default;

  mojo::PendingRemote<network::mojom::DataPipeGetter>
  GetDataPipeGetterRemote() {
    EXPECT_FALSE(receiver_.is_bound());
    return receiver_.BindNewPipeAndPassRemote();
  }

  void WaitForRead(mojo::ScopedDataPipeProducerHandle* write_pipe,
                   ReadCallback* read_callback) {
    DCHECK(!run_loop_);

    if (!write_pipe_.is_valid()) {
      run_loop_ = std::make_unique<base::RunLoop>();
      run_loop_->Run();
      run_loop_.reset();
    }

    EXPECT_TRUE(write_pipe_.is_valid());
    EXPECT_TRUE(read_callback_);

    *write_pipe = std::move(write_pipe_);
    *read_callback = std::move(read_callback_);
  }

 private:
  // network::mojom::DataPipeGetter implementation:
  void Read(mojo::ScopedDataPipeProducerHandle pipe,
            ReadCallback callback) override {
    EXPECT_FALSE(write_pipe_.is_valid());
    EXPECT_FALSE(read_callback_);

    write_pipe_ = std::move(pipe);
    read_callback_ = std::move(callback);

    if (run_loop_)
      run_loop_->Quit();
  }

  void Clone(
      mojo::PendingReceiver<network::mojom::DataPipeGetter> receiver) override {
    NOTIMPLEMENTED();
  }

  std::unique_ptr<base::RunLoop> run_loop_;

  mojo::Receiver<network::mojom::DataPipeGetter> receiver_{this};
  mojo::ScopedDataPipeProducerHandle write_pipe_;
  ReadCallback read_callback_;

  DISALLOW_COPY_AND_ASSIGN(PassThroughDataPipeGetter);
};

class DataPipeElementReaderTest : public testing::Test {
 public:
  DataPipeElementReaderTest()
      : element_reader_(nullptr, data_pipe_getter_.GetDataPipeGetterRemote()) {}

 protected:
  base::test::SingleThreadTaskEnvironment task_environment_{
      base::test::SingleThreadTaskEnvironment::MainThreadType::IO};
  PassThroughDataPipeGetter data_pipe_getter_;
  DataPipeElementReader element_reader_;
};

// Test the case where a second Init() call occurs when there's a pending Init()
// call in progress. The first call should be dropped, in favor of the second
// one.
TEST_F(DataPipeElementReaderTest, InitInterruptsInit) {
  // Value deliberately outside of the range of an uint32_t, to catch any
  // accidental conversions to an int.
  const uint64_t kResponseBodySize = std::numeric_limits<uint32_t>::max();

  // The network stack calls Init.
  net::TestCompletionCallback first_init_callback;
  EXPECT_EQ(net::ERR_IO_PENDING,
            element_reader_.Init(first_init_callback.callback()));

  // Wait for DataPipeGetter::Read() to be called.
  mojo::ScopedDataPipeProducerHandle first_write_pipe;
  network::mojom::DataPipeGetter::ReadCallback first_read_pipe_callback;
  data_pipe_getter_.WaitForRead(&first_write_pipe, &first_read_pipe_callback);

  // The network stack calls Init again, interrupting the previous call.
  net::TestCompletionCallback second_init_callback;
  EXPECT_EQ(net::ERR_IO_PENDING,
            element_reader_.Init(second_init_callback.callback()));

  // Wait for DataPipeGetter::Read() to be called again.
  mojo::ScopedDataPipeProducerHandle second_write_pipe;
  network::mojom::DataPipeGetter::ReadCallback second_read_pipe_callback;
  data_pipe_getter_.WaitForRead(&second_write_pipe, &second_read_pipe_callback);

  // Sending data on the first read pipe should do nothing.
  std::move(first_read_pipe_callback)
      .Run(net::ERR_FAILED, kResponseBodySize - 1);
  // Run any pending tasks, to make sure nothing unexpected is queued.
  base::RunLoop().RunUntilIdle();
  EXPECT_FALSE(first_init_callback.have_result());
  EXPECT_FALSE(second_init_callback.have_result());

  // Sending data on the second pipe should result in the second init callback
  // being invoked.
  std::move(second_read_pipe_callback).Run(net::OK, kResponseBodySize);
  EXPECT_EQ(net::OK, second_init_callback.WaitForResult());
  EXPECT_FALSE(first_init_callback.have_result());

  EXPECT_EQ(kResponseBodySize, element_reader_.GetContentLength());
  EXPECT_EQ(kResponseBodySize, element_reader_.BytesRemaining());
  EXPECT_FALSE(element_reader_.IsInMemory());

  // Try to read from the body.
  auto io_buffer = base::MakeRefCounted<net::IOBufferWithSize>(10);
  net::TestCompletionCallback read_callback;
  EXPECT_EQ(net::ERR_IO_PENDING,
            element_reader_.Read(io_buffer.get(), io_buffer->size(),
                                 read_callback.callback()));

  // Writes to the first write pipe should either fail, or succeed but be
  // ignored.
  mojo::BlockingCopyFromString("foo", first_write_pipe);
  base::RunLoop().RunUntilIdle();
  EXPECT_FALSE(read_callback.have_result());
}

// Test the case where a second Init() call occurs when there's a pending Read()
// call in progress. The old Read() should be dropped, in favor of the new
// Init().
TEST_F(DataPipeElementReaderTest, InitInterruptsRead) {
  // Value deliberately outside of the range of an uint32_t, to catch any
  // accidental conversions to an int.
  const uint64_t kResponseBodySize = std::numeric_limits<uint32_t>::max();

  // The network stack calls Init.
  net::TestCompletionCallback first_init_callback;
  EXPECT_EQ(net::ERR_IO_PENDING,
            element_reader_.Init(first_init_callback.callback()));

  // Wait for DataPipeGetter::Read() to be called.
  mojo::ScopedDataPipeProducerHandle first_write_pipe;
  network::mojom::DataPipeGetter::ReadCallback first_read_pipe_callback;
  data_pipe_getter_.WaitForRead(&first_write_pipe, &first_read_pipe_callback);
  std::move(first_read_pipe_callback).Run(net::OK, kResponseBodySize);

  ASSERT_EQ(net::OK, first_init_callback.WaitForResult());

  auto first_io_buffer = base::MakeRefCounted<net::IOBufferWithSize>(10);
  net::TestCompletionCallback first_read_callback;
  EXPECT_EQ(net::ERR_IO_PENDING,
            element_reader_.Read(first_io_buffer.get(), first_io_buffer->size(),
                                 first_read_callback.callback()));

  // The network stack calls Init again, interrupting the previous read.
  net::TestCompletionCallback second_init_callback;
  EXPECT_EQ(net::ERR_IO_PENDING,
            element_reader_.Init(second_init_callback.callback()));

  // Wait for DataPipeGetter::Read() to be called again.
  mojo::ScopedDataPipeProducerHandle second_write_pipe;
  network::mojom::DataPipeGetter::ReadCallback second_read_pipe_callback;
  data_pipe_getter_.WaitForRead(&second_write_pipe, &second_read_pipe_callback);

  // Run any pending tasks, to make sure nothing unexpected is queued.
  base::RunLoop().RunUntilIdle();
  EXPECT_FALSE(first_read_callback.have_result());
  EXPECT_FALSE(second_init_callback.have_result());

  // Sending data on the second pipe should result in the second init callback
  // being invoked.
  std::move(second_read_pipe_callback).Run(net::OK, kResponseBodySize);
  EXPECT_EQ(net::OK, second_init_callback.WaitForResult());

  EXPECT_EQ(kResponseBodySize, element_reader_.GetContentLength());
  EXPECT_EQ(kResponseBodySize, element_reader_.BytesRemaining());
  EXPECT_FALSE(element_reader_.IsInMemory());

  // Try to read from the body.
  auto io_buffer = base::MakeRefCounted<net::IOBufferWithSize>(10);
  net::TestCompletionCallback second_read_callback;
  EXPECT_EQ(net::ERR_IO_PENDING,
            element_reader_.Read(io_buffer.get(), io_buffer->size(),
                                 second_read_callback.callback()));

  // Writes to the first write pipe should either fail, or succeed but be
  // ignored.
  mojo::BlockingCopyFromString("foo", first_write_pipe);
  base::RunLoop().RunUntilIdle();
  EXPECT_FALSE(second_read_callback.have_result());
}

}  // namespace

}  // namespace network