blob: 5ec9705747c98eab8b06c8eebabc8be5a16eb38b [file] [log] [blame]
Avi Drissman4e1b7bc32022-09-15 14:03:501// Copyright 2012 The Chromium Authors
[email protected]b03507862012-05-23 17:11:502// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]f8e92b5d2013-03-21 18:35:465#include "content/browser/byte_stream.h"
[email protected]b03507862012-05-23 17:11:506
avib7348942015-12-25 20:57:107#include <stddef.h>
8
[email protected]07516262013-08-22 07:43:249#include <limits>
Arthur Sonzognic6113982025-08-11 16:01:5910#include <vector>
[email protected]b03507862012-05-23 17:11:5011
Tom Sepez3e425102025-07-23 12:16:2012#include "base/compiler_specific.h"
Brett Wilsoncc8623d2017-09-12 03:28:1013#include "base/containers/circular_deque.h"
Arthur Sonzognic6113982025-08-11 16:01:5914#include "base/containers/to_vector.h"
Avi Drissmanadac21992023-01-11 23:46:3915#include "base/functional/bind.h"
16#include "base/functional/callback.h"
[email protected]b03507862012-05-23 17:11:5017#include "base/memory/ref_counted.h"
fdoraye716a902016-07-05 16:05:4918#include "base/run_loop.h"
Sean Maher5b9af51f2022-11-21 15:32:4719#include "base/task/single_thread_task_runner.h"
Gabriel Charettec7108742019-08-23 03:31:4020#include "base/test/task_environment.h"
[email protected]a8582b12012-12-19 22:18:2921#include "base/test/test_simple_task_runner.h"
[email protected]b03507862012-05-23 17:11:5022#include "net/base/io_buffer.h"
[email protected]b03507862012-05-23 17:11:5023#include "testing/gtest/include/gtest/gtest.h"
24
[email protected]35869622012-10-26 23:23:5525namespace content {
[email protected]b03507862012-05-23 17:11:5026namespace {
27
[email protected]b03507862012-05-23 17:11:5028void CountCallbacks(int* counter) {
29 ++*counter;
30}
31
32} // namespace
33
34class ByteStreamTest : public testing::Test {
35 public:
[email protected]b03507862012-05-23 17:11:5036 // Create a new IO buffer of the given |buffer_size|. Details of the
37 // contents of the created buffer will be kept, and can be validated
38 // by ValidateIOBuffer.
39 scoped_refptr<net::IOBuffer> NewIOBuffer(size_t buffer_size) {
Arthur Sonzognic6113982025-08-11 16:01:5940 static uint8_t seed = 0;
Tom Sepez0156eb22023-11-01 18:34:0141 auto buffer = base::MakeRefCounted<net::IOBufferWithSize>(buffer_size);
Arthur Sonzognic6113982025-08-11 16:01:5942 base::span<uint8_t> span = buffer->span();
43 for (size_t i = 0; i < buffer_size; i++) {
44 span[i] = ++seed;
45 }
46 data_queue_.emplace_back(base::ToVector(span));
[email protected]b03507862012-05-23 17:11:5047 return buffer;
48 }
49
50 // Create an IOBuffer of the appropriate size and add it to the
51 // ByteStream, returning the result of the ByteStream::Write.
52 // Separate function to avoid duplication of buffer_size in test
53 // calls.
[email protected]35869622012-10-26 23:23:5554 bool Write(ByteStreamWriter* byte_stream_input, size_t buffer_size) {
[email protected]b03507862012-05-23 17:11:5055 return byte_stream_input->Write(NewIOBuffer(buffer_size), buffer_size);
56 }
57
58 // Validate that we have the IOBuffer we expect. This routine must be
59 // called on buffers that were allocated from NewIOBuffer, and in the
60 // order that they were allocated. Calls to NewIOBuffer &&
61 // ValidateIOBuffer may be interleaved.
Arthur Sonzognic6113982025-08-11 16:01:5962 bool ValidateIOBuffer(scoped_refptr<net::IOBuffer> buffer) {
63 base::span<const uint8_t> buffer_span = buffer->span();
64 std::vector<uint8_t> expected_data = data_queue_.front();
65 data_queue_.pop_front();
[email protected]b03507862012-05-23 17:11:5066
Arthur Sonzognic6113982025-08-11 16:01:5967 EXPECT_EQ(buffer_span, expected_data);
68 return buffer_span == expected_data;
[email protected]b03507862012-05-23 17:11:5069 }
70
[email protected]b03507862012-05-23 17:11:5071 private:
Arthur Sonzognic6113982025-08-11 16:01:5972 base::test::SingleThreadTaskEnvironment task_environment_;
73 base::circular_deque<std::vector<uint8_t>> data_queue_;
[email protected]b03507862012-05-23 17:11:5074};
75
[email protected]d7db4f622012-06-04 18:20:5676// Confirm that filling and emptying the stream works properly, and that
[email protected]b03507862012-05-23 17:11:5077// we get full triggers when we expect.
78TEST_F(ByteStreamTest, ByteStream_PushBack) {
dcheng59716272016-04-09 05:19:0879 std::unique_ptr<ByteStreamWriter> byte_stream_input;
80 std::unique_ptr<ByteStreamReader> byte_stream_output;
Sean Maher5b9af51f2022-11-21 15:32:4781 CreateByteStream(base::SingleThreadTaskRunner::GetCurrentDefault(),
82 base::SingleThreadTaskRunner::GetCurrentDefault(), 3 * 1024,
Alexander Timin58ee05f2018-10-05 11:44:3383 &byte_stream_input, &byte_stream_output);
[email protected]b03507862012-05-23 17:11:5084
85 // Push a series of IO buffers on; test pushback happening and
86 // that it's advisory.
87 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
88 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
89 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
90 EXPECT_FALSE(Write(byte_stream_input.get(), 1));
91 EXPECT_FALSE(Write(byte_stream_input.get(), 1024));
92 // Flush
[email protected]8d0c23e2013-08-02 11:02:3093 byte_stream_input->Close(0);
[email protected]07516262013-08-22 07:43:2494 EXPECT_EQ(4 * 1024U + 1U, byte_stream_input->GetTotalBufferedBytes());
fdoraye716a902016-07-05 16:05:4995 base::RunLoop().RunUntilIdle();
[email protected]07516262013-08-22 07:43:2496 // Data already sent to reader is also counted in.
97 EXPECT_EQ(4 * 1024U + 1U, byte_stream_input->GetTotalBufferedBytes());
[email protected]b03507862012-05-23 17:11:5098
99 // Pull the IO buffers out; do we get the same buffers and do they
100 // have the same contents?
101 scoped_refptr<net::IOBuffer> output_io_buffer;
102 size_t output_length;
[email protected]35869622012-10-26 23:23:55103 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50104 byte_stream_output->Read(&output_io_buffer, &output_length));
Arthur Sonzognic6113982025-08-11 16:01:59105 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer));
[email protected]b03507862012-05-23 17:11:50106
[email protected]35869622012-10-26 23:23:55107 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50108 byte_stream_output->Read(&output_io_buffer, &output_length));
Arthur Sonzognic6113982025-08-11 16:01:59109 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer));
[email protected]b03507862012-05-23 17:11:50110
[email protected]35869622012-10-26 23:23:55111 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50112 byte_stream_output->Read(&output_io_buffer, &output_length));
Arthur Sonzognic6113982025-08-11 16:01:59113 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer));
[email protected]b03507862012-05-23 17:11:50114
[email protected]35869622012-10-26 23:23:55115 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50116 byte_stream_output->Read(&output_io_buffer, &output_length));
Arthur Sonzognic6113982025-08-11 16:01:59117 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer));
[email protected]b03507862012-05-23 17:11:50118
[email protected]35869622012-10-26 23:23:55119 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50120 byte_stream_output->Read(&output_io_buffer, &output_length));
Arthur Sonzognic6113982025-08-11 16:01:59121 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer));
[email protected]b03507862012-05-23 17:11:50122
[email protected]35869622012-10-26 23:23:55123 EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE,
[email protected]b03507862012-05-23 17:11:50124 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]07516262013-08-22 07:43:24125
fdoraye716a902016-07-05 16:05:49126 base::RunLoop().RunUntilIdle();
[email protected]07516262013-08-22 07:43:24127 // Reader now knows that all data is read out.
128 EXPECT_EQ(1024U, byte_stream_input->GetTotalBufferedBytes());
[email protected]b03507862012-05-23 17:11:50129}
130
[email protected]566357e2013-07-31 03:59:36131// Confirm that Flush() method makes the writer to send written contents to
132// the reader.
133TEST_F(ByteStreamTest, ByteStream_Flush) {
dcheng59716272016-04-09 05:19:08134 std::unique_ptr<ByteStreamWriter> byte_stream_input;
135 std::unique_ptr<ByteStreamReader> byte_stream_output;
Sean Maher5b9af51f2022-11-21 15:32:47136 CreateByteStream(base::SingleThreadTaskRunner::GetCurrentDefault(),
137 base::SingleThreadTaskRunner::GetCurrentDefault(), 1024,
Alexander Timin58ee05f2018-10-05 11:44:33138 &byte_stream_input, &byte_stream_output);
[email protected]566357e2013-07-31 03:59:36139
140 EXPECT_TRUE(Write(byte_stream_input.get(), 1));
fdoraye716a902016-07-05 16:05:49141 base::RunLoop().RunUntilIdle();
[email protected]566357e2013-07-31 03:59:36142
143 scoped_refptr<net::IOBuffer> output_io_buffer;
144 size_t output_length = 0;
145 // Check that data is not sent to the reader yet.
146 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
147 byte_stream_output->Read(&output_io_buffer, &output_length));
148
149 byte_stream_input->Flush();
fdoraye716a902016-07-05 16:05:49150 base::RunLoop().RunUntilIdle();
[email protected]566357e2013-07-31 03:59:36151
152 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
153 byte_stream_output->Read(&output_io_buffer, &output_length));
Arthur Sonzognic6113982025-08-11 16:01:59154 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer));
[email protected]566357e2013-07-31 03:59:36155
156 // Check that it's ok to Flush() an empty writer.
157 byte_stream_input->Flush();
fdoraye716a902016-07-05 16:05:49158 base::RunLoop().RunUntilIdle();
[email protected]566357e2013-07-31 03:59:36159
160 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
161 byte_stream_output->Read(&output_io_buffer, &output_length));
162
[email protected]8d0c23e2013-08-02 11:02:30163 byte_stream_input->Close(0);
fdoraye716a902016-07-05 16:05:49164 base::RunLoop().RunUntilIdle();
[email protected]566357e2013-07-31 03:59:36165
166 EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE,
167 byte_stream_output->Read(&output_io_buffer, &output_length));
168}
169
[email protected]b03507862012-05-23 17:11:50170// Same as above, only use knowledge of the internals to confirm
171// that we're getting pushback even when data's split across the two
172// objects
173TEST_F(ByteStreamTest, ByteStream_PushBackSplit) {
dcheng59716272016-04-09 05:19:08174 std::unique_ptr<ByteStreamWriter> byte_stream_input;
175 std::unique_ptr<ByteStreamReader> byte_stream_output;
Sean Maher5b9af51f2022-11-21 15:32:47176 CreateByteStream(base::SingleThreadTaskRunner::GetCurrentDefault(),
177 base::SingleThreadTaskRunner::GetCurrentDefault(), 9 * 1024,
Alexander Timin58ee05f2018-10-05 11:44:33178 &byte_stream_input, &byte_stream_output);
[email protected]b03507862012-05-23 17:11:50179
180 // Push a series of IO buffers on; test pushback happening and
181 // that it's advisory.
182 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
fdoraye716a902016-07-05 16:05:49183 base::RunLoop().RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50184 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
fdoraye716a902016-07-05 16:05:49185 base::RunLoop().RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50186 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
fdoraye716a902016-07-05 16:05:49187 base::RunLoop().RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50188 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
fdoraye716a902016-07-05 16:05:49189 base::RunLoop().RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50190 EXPECT_FALSE(Write(byte_stream_input.get(), 6 * 1024));
fdoraye716a902016-07-05 16:05:49191 base::RunLoop().RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50192
193 // Pull the IO buffers out; do we get the same buffers and do they
194 // have the same contents?
195 scoped_refptr<net::IOBuffer> output_io_buffer;
196 size_t output_length;
[email protected]35869622012-10-26 23:23:55197 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50198 byte_stream_output->Read(&output_io_buffer, &output_length));
Arthur Sonzognic6113982025-08-11 16:01:59199 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer));
[email protected]b03507862012-05-23 17:11:50200
[email protected]35869622012-10-26 23:23:55201 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50202 byte_stream_output->Read(&output_io_buffer, &output_length));
Arthur Sonzognic6113982025-08-11 16:01:59203 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer));
[email protected]b03507862012-05-23 17:11:50204
[email protected]35869622012-10-26 23:23:55205 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50206 byte_stream_output->Read(&output_io_buffer, &output_length));
Arthur Sonzognic6113982025-08-11 16:01:59207 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer));
[email protected]b03507862012-05-23 17:11:50208
[email protected]35869622012-10-26 23:23:55209 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50210 byte_stream_output->Read(&output_io_buffer, &output_length));
Arthur Sonzognic6113982025-08-11 16:01:59211 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer));
[email protected]b03507862012-05-23 17:11:50212
[email protected]35869622012-10-26 23:23:55213 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50214 byte_stream_output->Read(&output_io_buffer, &output_length));
Arthur Sonzognic6113982025-08-11 16:01:59215 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer));
[email protected]b03507862012-05-23 17:11:50216
[email protected]35869622012-10-26 23:23:55217 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50218 byte_stream_output->Read(&output_io_buffer, &output_length));
219}
220
221// Confirm that a Close() notification transmits in-order
[email protected]d7db4f622012-06-04 18:20:56222// with data on the stream.
[email protected]b03507862012-05-23 17:11:50223TEST_F(ByteStreamTest, ByteStream_CompleteTransmits) {
dcheng59716272016-04-09 05:19:08224 std::unique_ptr<ByteStreamWriter> byte_stream_input;
225 std::unique_ptr<ByteStreamReader> byte_stream_output;
[email protected]b03507862012-05-23 17:11:50226
227 scoped_refptr<net::IOBuffer> output_io_buffer;
228 size_t output_length;
229
230 // Empty stream, non-error case.
Sean Maher5b9af51f2022-11-21 15:32:47231 CreateByteStream(base::SingleThreadTaskRunner::GetCurrentDefault(),
232 base::SingleThreadTaskRunner::GetCurrentDefault(), 3 * 1024,
Alexander Timin58ee05f2018-10-05 11:44:33233 &byte_stream_input, &byte_stream_output);
[email protected]35869622012-10-26 23:23:55234 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50235 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]8d0c23e2013-08-02 11:02:30236 byte_stream_input->Close(0);
fdoraye716a902016-07-05 16:05:49237 base::RunLoop().RunUntilIdle();
[email protected]35869622012-10-26 23:23:55238 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE,
[email protected]b03507862012-05-23 17:11:50239 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]8d0c23e2013-08-02 11:02:30240 EXPECT_EQ(0, byte_stream_output->GetStatus());
[email protected]b03507862012-05-23 17:11:50241
242 // Non-empty stream, non-error case.
Sean Maher5b9af51f2022-11-21 15:32:47243 CreateByteStream(base::SingleThreadTaskRunner::GetCurrentDefault(),
244 base::SingleThreadTaskRunner::GetCurrentDefault(), 3 * 1024,
Alexander Timin58ee05f2018-10-05 11:44:33245 &byte_stream_input, &byte_stream_output);
[email protected]35869622012-10-26 23:23:55246 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50247 byte_stream_output->Read(&output_io_buffer, &output_length));
248 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
[email protected]8d0c23e2013-08-02 11:02:30249 byte_stream_input->Close(0);
fdoraye716a902016-07-05 16:05:49250 base::RunLoop().RunUntilIdle();
[email protected]35869622012-10-26 23:23:55251 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50252 byte_stream_output->Read(&output_io_buffer, &output_length));
Arthur Sonzognic6113982025-08-11 16:01:59253 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer));
[email protected]35869622012-10-26 23:23:55254 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE,
[email protected]b03507862012-05-23 17:11:50255 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]8d0c23e2013-08-02 11:02:30256 EXPECT_EQ(0, byte_stream_output->GetStatus());
[email protected]b03507862012-05-23 17:11:50257
[email protected]8d0c23e2013-08-02 11:02:30258 const int kFakeErrorCode = 22;
259
260 // Empty stream, error case.
Sean Maher5b9af51f2022-11-21 15:32:47261 CreateByteStream(base::SingleThreadTaskRunner::GetCurrentDefault(),
262 base::SingleThreadTaskRunner::GetCurrentDefault(), 3 * 1024,
Alexander Timin58ee05f2018-10-05 11:44:33263 &byte_stream_input, &byte_stream_output);
[email protected]35869622012-10-26 23:23:55264 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50265 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]8d0c23e2013-08-02 11:02:30266 byte_stream_input->Close(kFakeErrorCode);
fdoraye716a902016-07-05 16:05:49267 base::RunLoop().RunUntilIdle();
[email protected]35869622012-10-26 23:23:55268 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE,
[email protected]b03507862012-05-23 17:11:50269 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]8d0c23e2013-08-02 11:02:30270 EXPECT_EQ(kFakeErrorCode, byte_stream_output->GetStatus());
[email protected]b03507862012-05-23 17:11:50271
[email protected]8d0c23e2013-08-02 11:02:30272 // Non-empty stream, error case.
Sean Maher5b9af51f2022-11-21 15:32:47273 CreateByteStream(base::SingleThreadTaskRunner::GetCurrentDefault(),
274 base::SingleThreadTaskRunner::GetCurrentDefault(), 3 * 1024,
Alexander Timin58ee05f2018-10-05 11:44:33275 &byte_stream_input, &byte_stream_output);
[email protected]35869622012-10-26 23:23:55276 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50277 byte_stream_output->Read(&output_io_buffer, &output_length));
278 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
[email protected]8d0c23e2013-08-02 11:02:30279 byte_stream_input->Close(kFakeErrorCode);
fdoraye716a902016-07-05 16:05:49280 base::RunLoop().RunUntilIdle();
[email protected]35869622012-10-26 23:23:55281 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50282 byte_stream_output->Read(&output_io_buffer, &output_length));
Arthur Sonzognic6113982025-08-11 16:01:59283 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer));
[email protected]35869622012-10-26 23:23:55284 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE,
[email protected]b03507862012-05-23 17:11:50285 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]8d0c23e2013-08-02 11:02:30286 EXPECT_EQ(kFakeErrorCode, byte_stream_output->GetStatus());
[email protected]b03507862012-05-23 17:11:50287}
288
289// Confirm that callbacks on the sink side are triggered when they should be.
290TEST_F(ByteStreamTest, ByteStream_SinkCallback) {
[email protected]a8582b12012-12-19 22:18:29291 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
292 new base::TestSimpleTaskRunner());
[email protected]b03507862012-05-23 17:11:50293
dcheng59716272016-04-09 05:19:08294 std::unique_ptr<ByteStreamWriter> byte_stream_input;
295 std::unique_ptr<ByteStreamReader> byte_stream_output;
Sean Maher5b9af51f2022-11-21 15:32:47296 CreateByteStream(base::SingleThreadTaskRunner::GetCurrentDefault(),
297 task_runner, 10000, &byte_stream_input, &byte_stream_output);
[email protected]b03507862012-05-23 17:11:50298
299 scoped_refptr<net::IOBuffer> output_io_buffer;
300 size_t output_length;
[email protected]b03507862012-05-23 17:11:50301
302 // Note that the specifics of when the callbacks are called with regard
[email protected]d7db4f622012-06-04 18:20:56303 // to how much data is pushed onto the stream is not (currently) part
[email protected]b03507862012-05-23 17:11:50304 // of the interface contract. If it becomes part of the contract, the
305 // tests below should get much more precise.
306
307 // Confirm callback called when you add more than 33% of the buffer.
308
309 // Setup callback
310 int num_callbacks = 0;
311 byte_stream_output->RegisterCallback(
Mark Pilgrimec8bce32018-05-17 18:55:23312 base::BindRepeating(CountCallbacks, &num_callbacks));
[email protected]b03507862012-05-23 17:11:50313
314 EXPECT_TRUE(Write(byte_stream_input.get(), 4000));
fdoraye716a902016-07-05 16:05:49315 base::RunLoop().RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50316
[email protected]b03507862012-05-23 17:11:50317 EXPECT_EQ(0, num_callbacks);
[email protected]a8582b12012-12-19 22:18:29318 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50319 EXPECT_EQ(1, num_callbacks);
320
321 // Check data and stream state.
[email protected]35869622012-10-26 23:23:55322 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50323 byte_stream_output->Read(&output_io_buffer, &output_length));
Arthur Sonzognic6113982025-08-11 16:01:59324 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer));
[email protected]35869622012-10-26 23:23:55325 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50326 byte_stream_output->Read(&output_io_buffer, &output_length));
327
328 // Confirm callback *isn't* called at less than 33% (by lack of
329 // unexpected call on task runner).
330 EXPECT_TRUE(Write(byte_stream_input.get(), 3000));
fdoraye716a902016-07-05 16:05:49331 base::RunLoop().RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50332
333 // This reflects an implementation artifact that data goes with callbacks,
334 // which should not be considered part of the interface guarantee.
[email protected]35869622012-10-26 23:23:55335 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50336 byte_stream_output->Read(&output_io_buffer, &output_length));
337}
338
339// Confirm that callbacks on the source side are triggered when they should
340// be.
341TEST_F(ByteStreamTest, ByteStream_SourceCallback) {
[email protected]a8582b12012-12-19 22:18:29342 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
343 new base::TestSimpleTaskRunner());
[email protected]b03507862012-05-23 17:11:50344
dcheng59716272016-04-09 05:19:08345 std::unique_ptr<ByteStreamWriter> byte_stream_input;
346 std::unique_ptr<ByteStreamReader> byte_stream_output;
Sean Maher5b9af51f2022-11-21 15:32:47347 CreateByteStream(task_runner,
348 base::SingleThreadTaskRunner::GetCurrentDefault(), 10000,
skyostil95082a62015-06-05 19:53:07349 &byte_stream_input, &byte_stream_output);
[email protected]b03507862012-05-23 17:11:50350
351 scoped_refptr<net::IOBuffer> output_io_buffer;
352 size_t output_length;
[email protected]b03507862012-05-23 17:11:50353
354 // Note that the specifics of when the callbacks are called with regard
[email protected]d7db4f622012-06-04 18:20:56355 // to how much data is pulled from the stream is not (currently) part
[email protected]b03507862012-05-23 17:11:50356 // of the interface contract. If it becomes part of the contract, the
357 // tests below should get much more precise.
358
359 // Confirm callback called when about 33% space available, and not
360 // at other transitions.
361
[email protected]a8582b12012-12-19 22:18:29362 // Add data.
[email protected]b03507862012-05-23 17:11:50363 int num_callbacks = 0;
364 byte_stream_input->RegisterCallback(
Mark Pilgrimec8bce32018-05-17 18:55:23365 base::BindRepeating(CountCallbacks, &num_callbacks));
[email protected]b03507862012-05-23 17:11:50366 EXPECT_TRUE(Write(byte_stream_input.get(), 2000));
367 EXPECT_TRUE(Write(byte_stream_input.get(), 2001));
368 EXPECT_FALSE(Write(byte_stream_input.get(), 6000));
369
370 // Allow bytes to transition (needed for message passing implementation),
371 // and get and validate the data.
fdoraye716a902016-07-05 16:05:49372 base::RunLoop().RunUntilIdle();
[email protected]35869622012-10-26 23:23:55373 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50374 byte_stream_output->Read(&output_io_buffer, &output_length));
Arthur Sonzognic6113982025-08-11 16:01:59375 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer));
[email protected]b03507862012-05-23 17:11:50376
[email protected]b03507862012-05-23 17:11:50377 // Grab data, triggering callback. Recorded on dispatch, but doesn't
378 // happen because it's caught by the mock.
[email protected]35869622012-10-26 23:23:55379 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50380 byte_stream_output->Read(&output_io_buffer, &output_length));
Arthur Sonzognic6113982025-08-11 16:01:59381 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer));
[email protected]b03507862012-05-23 17:11:50382
383 // Confirm that the callback passed to the mock does what we expect.
384 EXPECT_EQ(0, num_callbacks);
[email protected]a8582b12012-12-19 22:18:29385 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50386 EXPECT_EQ(1, num_callbacks);
387
388 // Same drill with final buffer.
[email protected]35869622012-10-26 23:23:55389 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50390 byte_stream_output->Read(&output_io_buffer, &output_length));
Arthur Sonzognic6113982025-08-11 16:01:59391 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer));
[email protected]35869622012-10-26 23:23:55392 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50393 byte_stream_output->Read(&output_io_buffer, &output_length));
394 EXPECT_EQ(1, num_callbacks);
[email protected]a8582b12012-12-19 22:18:29395 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50396 // Should have updated the internal structures but not called the
397 // callback.
398 EXPECT_EQ(1, num_callbacks);
399}
400
401// Confirm that racing a change to a sink callback with a post results
402// in the new callback being called.
403TEST_F(ByteStreamTest, ByteStream_SinkInterrupt) {
[email protected]a8582b12012-12-19 22:18:29404 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
405 new base::TestSimpleTaskRunner());
[email protected]b03507862012-05-23 17:11:50406
dcheng59716272016-04-09 05:19:08407 std::unique_ptr<ByteStreamWriter> byte_stream_input;
408 std::unique_ptr<ByteStreamReader> byte_stream_output;
Sean Maher5b9af51f2022-11-21 15:32:47409 CreateByteStream(base::SingleThreadTaskRunner::GetCurrentDefault(),
410 task_runner, 10000, &byte_stream_input, &byte_stream_output);
[email protected]b03507862012-05-23 17:11:50411
412 scoped_refptr<net::IOBuffer> output_io_buffer;
413 size_t output_length;
[email protected]b03507862012-05-23 17:11:50414
[email protected]a8582b12012-12-19 22:18:29415 // Record initial state.
[email protected]b03507862012-05-23 17:11:50416 int num_callbacks = 0;
417 byte_stream_output->RegisterCallback(
Mark Pilgrimec8bce32018-05-17 18:55:23418 base::BindRepeating(CountCallbacks, &num_callbacks));
[email protected]b03507862012-05-23 17:11:50419
420 // Add data, and pass it across.
421 EXPECT_TRUE(Write(byte_stream_input.get(), 4000));
fdoraye716a902016-07-05 16:05:49422 base::RunLoop().RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50423
424 // The task runner should have been hit, but the callback count
425 // isn't changed until we actually run the callback.
[email protected]b03507862012-05-23 17:11:50426 EXPECT_EQ(0, num_callbacks);
427
428 // If we change the callback now, the new one should be run
429 // (simulates race with post task).
430 int num_alt_callbacks = 0;
431 byte_stream_output->RegisterCallback(
Mark Pilgrimec8bce32018-05-17 18:55:23432 base::BindRepeating(CountCallbacks, &num_alt_callbacks));
[email protected]a8582b12012-12-19 22:18:29433 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50434 EXPECT_EQ(0, num_callbacks);
435 EXPECT_EQ(1, num_alt_callbacks);
436
437 // Final cleanup.
[email protected]35869622012-10-26 23:23:55438 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50439 byte_stream_output->Read(&output_io_buffer, &output_length));
Arthur Sonzognic6113982025-08-11 16:01:59440 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer));
[email protected]35869622012-10-26 23:23:55441 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50442 byte_stream_output->Read(&output_io_buffer, &output_length));
443
444}
445
446// Confirm that racing a change to a source callback with a post results
447// in the new callback being called.
448TEST_F(ByteStreamTest, ByteStream_SourceInterrupt) {
[email protected]a8582b12012-12-19 22:18:29449 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
450 new base::TestSimpleTaskRunner());
[email protected]b03507862012-05-23 17:11:50451
dcheng59716272016-04-09 05:19:08452 std::unique_ptr<ByteStreamWriter> byte_stream_input;
453 std::unique_ptr<ByteStreamReader> byte_stream_output;
Sean Maher5b9af51f2022-11-21 15:32:47454 CreateByteStream(task_runner,
455 base::SingleThreadTaskRunner::GetCurrentDefault(), 10000,
skyostil95082a62015-06-05 19:53:07456 &byte_stream_input, &byte_stream_output);
[email protected]b03507862012-05-23 17:11:50457
458 scoped_refptr<net::IOBuffer> output_io_buffer;
459 size_t output_length;
[email protected]b03507862012-05-23 17:11:50460
[email protected]a8582b12012-12-19 22:18:29461 // Setup state for test.
[email protected]b03507862012-05-23 17:11:50462 int num_callbacks = 0;
463 byte_stream_input->RegisterCallback(
Mark Pilgrimec8bce32018-05-17 18:55:23464 base::BindRepeating(CountCallbacks, &num_callbacks));
[email protected]b03507862012-05-23 17:11:50465 EXPECT_TRUE(Write(byte_stream_input.get(), 2000));
466 EXPECT_TRUE(Write(byte_stream_input.get(), 2001));
467 EXPECT_FALSE(Write(byte_stream_input.get(), 6000));
fdoraye716a902016-07-05 16:05:49468 base::RunLoop().RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50469
470 // Initial get should not trigger callback.
[email protected]35869622012-10-26 23:23:55471 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50472 byte_stream_output->Read(&output_io_buffer, &output_length));
Arthur Sonzognic6113982025-08-11 16:01:59473 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer));
fdoraye716a902016-07-05 16:05:49474 base::RunLoop().RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50475
[email protected]b03507862012-05-23 17:11:50476 // Second get *should* trigger callback.
[email protected]35869622012-10-26 23:23:55477 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50478 byte_stream_output->Read(&output_io_buffer, &output_length));
Arthur Sonzognic6113982025-08-11 16:01:59479 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer));
[email protected]b03507862012-05-23 17:11:50480
481 // Which should do the right thing when it's run.
482 int num_alt_callbacks = 0;
483 byte_stream_input->RegisterCallback(
Mark Pilgrimec8bce32018-05-17 18:55:23484 base::BindRepeating(CountCallbacks, &num_alt_callbacks));
[email protected]a8582b12012-12-19 22:18:29485 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50486 EXPECT_EQ(0, num_callbacks);
487 EXPECT_EQ(1, num_alt_callbacks);
488
489 // Third get should also trigger callback.
[email protected]35869622012-10-26 23:23:55490 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50491 byte_stream_output->Read(&output_io_buffer, &output_length));
Arthur Sonzognic6113982025-08-11 16:01:59492 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer));
[email protected]35869622012-10-26 23:23:55493 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50494 byte_stream_output->Read(&output_io_buffer, &output_length));
495}
496
497// Confirm that callback is called on zero data transfer but source
498// complete.
499TEST_F(ByteStreamTest, ByteStream_ZeroCallback) {
[email protected]a8582b12012-12-19 22:18:29500 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
501 new base::TestSimpleTaskRunner());
[email protected]b03507862012-05-23 17:11:50502
dcheng59716272016-04-09 05:19:08503 std::unique_ptr<ByteStreamWriter> byte_stream_input;
504 std::unique_ptr<ByteStreamReader> byte_stream_output;
Sean Maher5b9af51f2022-11-21 15:32:47505 CreateByteStream(base::SingleThreadTaskRunner::GetCurrentDefault(),
506 task_runner, 10000, &byte_stream_input, &byte_stream_output);
[email protected]b03507862012-05-23 17:11:50507
[email protected]a8582b12012-12-19 22:18:29508 // Record initial state.
[email protected]b03507862012-05-23 17:11:50509 int num_callbacks = 0;
510 byte_stream_output->RegisterCallback(
Mark Pilgrimec8bce32018-05-17 18:55:23511 base::BindRepeating(CountCallbacks, &num_callbacks));
[email protected]b03507862012-05-23 17:11:50512
513 // Immediately close the stream.
[email protected]8d0c23e2013-08-02 11:02:30514 byte_stream_input->Close(0);
[email protected]a8582b12012-12-19 22:18:29515 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50516 EXPECT_EQ(1, num_callbacks);
517}
518
[email protected]6a14c192013-08-06 20:18:42519TEST_F(ByteStreamTest, ByteStream_CloseWithoutAnyWrite) {
dcheng59716272016-04-09 05:19:08520 std::unique_ptr<ByteStreamWriter> byte_stream_input;
521 std::unique_ptr<ByteStreamReader> byte_stream_output;
Sean Maher5b9af51f2022-11-21 15:32:47522 CreateByteStream(base::SingleThreadTaskRunner::GetCurrentDefault(),
523 base::SingleThreadTaskRunner::GetCurrentDefault(), 3 * 1024,
Alexander Timin58ee05f2018-10-05 11:44:33524 &byte_stream_input, &byte_stream_output);
[email protected]6a14c192013-08-06 20:18:42525
526 byte_stream_input->Close(0);
fdoraye716a902016-07-05 16:05:49527 base::RunLoop().RunUntilIdle();
[email protected]6a14c192013-08-06 20:18:42528
529 scoped_refptr<net::IOBuffer> output_io_buffer;
530 size_t output_length;
531 EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE,
532 byte_stream_output->Read(&output_io_buffer, &output_length));
533}
534
535TEST_F(ByteStreamTest, ByteStream_FlushWithoutAnyWrite) {
dcheng59716272016-04-09 05:19:08536 std::unique_ptr<ByteStreamWriter> byte_stream_input;
537 std::unique_ptr<ByteStreamReader> byte_stream_output;
Sean Maher5b9af51f2022-11-21 15:32:47538 CreateByteStream(base::SingleThreadTaskRunner::GetCurrentDefault(),
539 base::SingleThreadTaskRunner::GetCurrentDefault(), 3 * 1024,
Alexander Timin58ee05f2018-10-05 11:44:33540 &byte_stream_input, &byte_stream_output);
[email protected]6a14c192013-08-06 20:18:42541
542 byte_stream_input->Flush();
fdoraye716a902016-07-05 16:05:49543 base::RunLoop().RunUntilIdle();
[email protected]6a14c192013-08-06 20:18:42544
545 scoped_refptr<net::IOBuffer> output_io_buffer;
546 size_t output_length;
547 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
548 byte_stream_output->Read(&output_io_buffer, &output_length));
549
550 byte_stream_input->Close(0);
fdoraye716a902016-07-05 16:05:49551 base::RunLoop().RunUntilIdle();
[email protected]6a14c192013-08-06 20:18:42552
553 EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE,
554 byte_stream_output->Read(&output_io_buffer, &output_length));
555}
556
[email protected]07516262013-08-22 07:43:24557TEST_F(ByteStreamTest, ByteStream_WriteOverflow) {
dcheng59716272016-04-09 05:19:08558 std::unique_ptr<ByteStreamWriter> byte_stream_input;
559 std::unique_ptr<ByteStreamReader> byte_stream_output;
Sean Maher5b9af51f2022-11-21 15:32:47560 CreateByteStream(base::SingleThreadTaskRunner::GetCurrentDefault(),
561 base::SingleThreadTaskRunner::GetCurrentDefault(),
skyostil95082a62015-06-05 19:53:07562 std::numeric_limits<size_t>::max(), &byte_stream_input,
563 &byte_stream_output);
[email protected]07516262013-08-22 07:43:24564
565 EXPECT_TRUE(Write(byte_stream_input.get(), 1));
566 // 1 + size_t max -> Overflow.
567 scoped_refptr<net::IOBuffer> empty_io_buffer;
568 EXPECT_FALSE(byte_stream_input->Write(empty_io_buffer,
569 std::numeric_limits<size_t>::max()));
fdoraye716a902016-07-05 16:05:49570 base::RunLoop().RunUntilIdle();
[email protected]07516262013-08-22 07:43:24571
572 // The first write is below PostToPeer threshold. We shouldn't get anything
573 // from the output.
574 scoped_refptr<net::IOBuffer> output_io_buffer;
575 size_t output_length;
576 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
577 byte_stream_output->Read(&output_io_buffer, &output_length));
578}
579
[email protected]35869622012-10-26 23:23:55580} // namespace content