blob: ac31f8329cec9f4342359716cce32282217bae4e [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
Nasko Oskovee48dfb42024-06-08 05:13:065#ifdef UNSAFE_BUFFERS_BUILD
6// TODO(crbug.com/342213636): Remove this and spanify to fix the errors.
7#pragma allow_unsafe_buffers
8#endif
9
[email protected]f8e92b5d2013-03-21 18:35:4610#include "content/browser/byte_stream.h"
[email protected]b03507862012-05-23 17:11:5011
avib7348942015-12-25 20:57:1012#include <stddef.h>
13
[email protected]07516262013-08-22 07:43:2414#include <limits>
[email protected]b03507862012-05-23 17:11:5015
Brett Wilsoncc8623d2017-09-12 03:28:1016#include "base/containers/circular_deque.h"
Avi Drissmanadac21992023-01-11 23:46:3917#include "base/functional/bind.h"
18#include "base/functional/callback.h"
[email protected]b03507862012-05-23 17:11:5019#include "base/memory/ref_counted.h"
fdoraye716a902016-07-05 16:05:4920#include "base/run_loop.h"
Sean Maher5b9af51f2022-11-21 15:32:4721#include "base/task/single_thread_task_runner.h"
Gabriel Charettec7108742019-08-23 03:31:4022#include "base/test/task_environment.h"
[email protected]a8582b12012-12-19 22:18:2923#include "base/test/test_simple_task_runner.h"
[email protected]b03507862012-05-23 17:11:5024#include "net/base/io_buffer.h"
[email protected]b03507862012-05-23 17:11:5025#include "testing/gtest/include/gtest/gtest.h"
26
[email protected]35869622012-10-26 23:23:5527namespace content {
[email protected]b03507862012-05-23 17:11:5028namespace {
29
[email protected]b03507862012-05-23 17:11:5030void CountCallbacks(int* counter) {
31 ++*counter;
32}
33
34} // namespace
35
36class ByteStreamTest : public testing::Test {
37 public:
38 ByteStreamTest();
39
40 // Create a new IO buffer of the given |buffer_size|. Details of the
41 // contents of the created buffer will be kept, and can be validated
42 // by ValidateIOBuffer.
43 scoped_refptr<net::IOBuffer> NewIOBuffer(size_t buffer_size) {
Tom Sepez0156eb22023-11-01 18:34:0144 auto buffer = base::MakeRefCounted<net::IOBufferWithSize>(buffer_size);
[email protected]b03507862012-05-23 17:11:5045 char *bufferp = buffer->data();
46 for (size_t i = 0; i < buffer_size; i++)
47 bufferp[i] = (i + producing_seed_key_) % (1 << sizeof(char));
48 pointer_queue_.push_back(bufferp);
49 length_queue_.push_back(buffer_size);
50 ++producing_seed_key_;
51 return buffer;
52 }
53
54 // Create an IOBuffer of the appropriate size and add it to the
55 // ByteStream, returning the result of the ByteStream::Write.
56 // Separate function to avoid duplication of buffer_size in test
57 // calls.
[email protected]35869622012-10-26 23:23:5558 bool Write(ByteStreamWriter* byte_stream_input, size_t buffer_size) {
[email protected]b03507862012-05-23 17:11:5059 return byte_stream_input->Write(NewIOBuffer(buffer_size), buffer_size);
60 }
61
62 // Validate that we have the IOBuffer we expect. This routine must be
63 // called on buffers that were allocated from NewIOBuffer, and in the
64 // order that they were allocated. Calls to NewIOBuffer &&
65 // ValidateIOBuffer may be interleaved.
66 bool ValidateIOBuffer(
67 scoped_refptr<net::IOBuffer> buffer, size_t buffer_size) {
68 char *bufferp = buffer->data();
69
70 char *expected_ptr = pointer_queue_.front();
71 size_t expected_length = length_queue_.front();
72 pointer_queue_.pop_front();
73 length_queue_.pop_front();
74 ++consuming_seed_key_;
75
76 EXPECT_EQ(expected_ptr, bufferp);
77 if (expected_ptr != bufferp)
78 return false;
79
80 EXPECT_EQ(expected_length, buffer_size);
81 if (expected_length != buffer_size)
82 return false;
83
84 for (size_t i = 0; i < buffer_size; i++) {
85 // Already incremented, so subtract one from the key.
86 EXPECT_EQ(static_cast<int>((i + consuming_seed_key_ - 1)
87 % (1 << sizeof(char))),
88 bufferp[i]);
89 if (static_cast<int>((i + consuming_seed_key_ - 1) %
90 (1 << sizeof(char))) != bufferp[i]) {
91 return false;
92 }
93 }
94 return true;
95 }
96
97 protected:
Gabriel Charettea0e08462019-09-05 13:48:2398 base::test::SingleThreadTaskEnvironment task_environment_;
[email protected]b03507862012-05-23 17:11:5099
100 private:
101 int producing_seed_key_;
102 int consuming_seed_key_;
Brett Wilsoncc8623d2017-09-12 03:28:10103 base::circular_deque<char*> pointer_queue_;
104 base::circular_deque<size_t> length_queue_;
[email protected]b03507862012-05-23 17:11:50105};
106
107ByteStreamTest::ByteStreamTest()
108 : producing_seed_key_(0),
109 consuming_seed_key_(0) { }
110
[email protected]d7db4f622012-06-04 18:20:56111// Confirm that filling and emptying the stream works properly, and that
[email protected]b03507862012-05-23 17:11:50112// we get full triggers when we expect.
113TEST_F(ByteStreamTest, ByteStream_PushBack) {
dcheng59716272016-04-09 05:19:08114 std::unique_ptr<ByteStreamWriter> byte_stream_input;
115 std::unique_ptr<ByteStreamReader> byte_stream_output;
Sean Maher5b9af51f2022-11-21 15:32:47116 CreateByteStream(base::SingleThreadTaskRunner::GetCurrentDefault(),
117 base::SingleThreadTaskRunner::GetCurrentDefault(), 3 * 1024,
Alexander Timin58ee05f2018-10-05 11:44:33118 &byte_stream_input, &byte_stream_output);
[email protected]b03507862012-05-23 17:11:50119
120 // Push a series of IO buffers on; test pushback happening and
121 // that it's advisory.
122 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
123 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
124 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
125 EXPECT_FALSE(Write(byte_stream_input.get(), 1));
126 EXPECT_FALSE(Write(byte_stream_input.get(), 1024));
127 // Flush
[email protected]8d0c23e2013-08-02 11:02:30128 byte_stream_input->Close(0);
[email protected]07516262013-08-22 07:43:24129 EXPECT_EQ(4 * 1024U + 1U, byte_stream_input->GetTotalBufferedBytes());
fdoraye716a902016-07-05 16:05:49130 base::RunLoop().RunUntilIdle();
[email protected]07516262013-08-22 07:43:24131 // Data already sent to reader is also counted in.
132 EXPECT_EQ(4 * 1024U + 1U, byte_stream_input->GetTotalBufferedBytes());
[email protected]b03507862012-05-23 17:11:50133
134 // Pull the IO buffers out; do we get the same buffers and do they
135 // have the same contents?
136 scoped_refptr<net::IOBuffer> output_io_buffer;
137 size_t output_length;
[email protected]35869622012-10-26 23:23:55138 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50139 byte_stream_output->Read(&output_io_buffer, &output_length));
140 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
141
[email protected]35869622012-10-26 23:23:55142 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50143 byte_stream_output->Read(&output_io_buffer, &output_length));
144 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
145
[email protected]35869622012-10-26 23:23:55146 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50147 byte_stream_output->Read(&output_io_buffer, &output_length));
148 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
149
[email protected]35869622012-10-26 23:23:55150 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50151 byte_stream_output->Read(&output_io_buffer, &output_length));
152 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
153
[email protected]35869622012-10-26 23:23:55154 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50155 byte_stream_output->Read(&output_io_buffer, &output_length));
156 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
157
[email protected]35869622012-10-26 23:23:55158 EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE,
[email protected]b03507862012-05-23 17:11:50159 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]07516262013-08-22 07:43:24160
fdoraye716a902016-07-05 16:05:49161 base::RunLoop().RunUntilIdle();
[email protected]07516262013-08-22 07:43:24162 // Reader now knows that all data is read out.
163 EXPECT_EQ(1024U, byte_stream_input->GetTotalBufferedBytes());
[email protected]b03507862012-05-23 17:11:50164}
165
[email protected]566357e2013-07-31 03:59:36166// Confirm that Flush() method makes the writer to send written contents to
167// the reader.
168TEST_F(ByteStreamTest, ByteStream_Flush) {
dcheng59716272016-04-09 05:19:08169 std::unique_ptr<ByteStreamWriter> byte_stream_input;
170 std::unique_ptr<ByteStreamReader> byte_stream_output;
Sean Maher5b9af51f2022-11-21 15:32:47171 CreateByteStream(base::SingleThreadTaskRunner::GetCurrentDefault(),
172 base::SingleThreadTaskRunner::GetCurrentDefault(), 1024,
Alexander Timin58ee05f2018-10-05 11:44:33173 &byte_stream_input, &byte_stream_output);
[email protected]566357e2013-07-31 03:59:36174
175 EXPECT_TRUE(Write(byte_stream_input.get(), 1));
fdoraye716a902016-07-05 16:05:49176 base::RunLoop().RunUntilIdle();
[email protected]566357e2013-07-31 03:59:36177
178 scoped_refptr<net::IOBuffer> output_io_buffer;
179 size_t output_length = 0;
180 // Check that data is not sent to the reader yet.
181 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
182 byte_stream_output->Read(&output_io_buffer, &output_length));
183
184 byte_stream_input->Flush();
fdoraye716a902016-07-05 16:05:49185 base::RunLoop().RunUntilIdle();
[email protected]566357e2013-07-31 03:59:36186
187 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
188 byte_stream_output->Read(&output_io_buffer, &output_length));
189 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
190
191 // Check that it's ok to Flush() an empty writer.
192 byte_stream_input->Flush();
fdoraye716a902016-07-05 16:05:49193 base::RunLoop().RunUntilIdle();
[email protected]566357e2013-07-31 03:59:36194
195 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
196 byte_stream_output->Read(&output_io_buffer, &output_length));
197
[email protected]8d0c23e2013-08-02 11:02:30198 byte_stream_input->Close(0);
fdoraye716a902016-07-05 16:05:49199 base::RunLoop().RunUntilIdle();
[email protected]566357e2013-07-31 03:59:36200
201 EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE,
202 byte_stream_output->Read(&output_io_buffer, &output_length));
203}
204
[email protected]b03507862012-05-23 17:11:50205// Same as above, only use knowledge of the internals to confirm
206// that we're getting pushback even when data's split across the two
207// objects
208TEST_F(ByteStreamTest, ByteStream_PushBackSplit) {
dcheng59716272016-04-09 05:19:08209 std::unique_ptr<ByteStreamWriter> byte_stream_input;
210 std::unique_ptr<ByteStreamReader> byte_stream_output;
Sean Maher5b9af51f2022-11-21 15:32:47211 CreateByteStream(base::SingleThreadTaskRunner::GetCurrentDefault(),
212 base::SingleThreadTaskRunner::GetCurrentDefault(), 9 * 1024,
Alexander Timin58ee05f2018-10-05 11:44:33213 &byte_stream_input, &byte_stream_output);
[email protected]b03507862012-05-23 17:11:50214
215 // Push a series of IO buffers on; test pushback happening and
216 // that it's advisory.
217 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
fdoraye716a902016-07-05 16:05:49218 base::RunLoop().RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50219 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
fdoraye716a902016-07-05 16:05:49220 base::RunLoop().RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50221 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
fdoraye716a902016-07-05 16:05:49222 base::RunLoop().RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50223 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
fdoraye716a902016-07-05 16:05:49224 base::RunLoop().RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50225 EXPECT_FALSE(Write(byte_stream_input.get(), 6 * 1024));
fdoraye716a902016-07-05 16:05:49226 base::RunLoop().RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50227
228 // Pull the IO buffers out; do we get the same buffers and do they
229 // have the same contents?
230 scoped_refptr<net::IOBuffer> output_io_buffer;
231 size_t output_length;
[email protected]35869622012-10-26 23:23:55232 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50233 byte_stream_output->Read(&output_io_buffer, &output_length));
234 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
235
[email protected]35869622012-10-26 23:23:55236 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50237 byte_stream_output->Read(&output_io_buffer, &output_length));
238 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
239
[email protected]35869622012-10-26 23:23:55240 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50241 byte_stream_output->Read(&output_io_buffer, &output_length));
242 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
243
[email protected]35869622012-10-26 23:23:55244 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50245 byte_stream_output->Read(&output_io_buffer, &output_length));
246 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
247
[email protected]35869622012-10-26 23:23:55248 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50249 byte_stream_output->Read(&output_io_buffer, &output_length));
250 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
251
[email protected]35869622012-10-26 23:23:55252 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50253 byte_stream_output->Read(&output_io_buffer, &output_length));
254}
255
256// Confirm that a Close() notification transmits in-order
[email protected]d7db4f622012-06-04 18:20:56257// with data on the stream.
[email protected]b03507862012-05-23 17:11:50258TEST_F(ByteStreamTest, ByteStream_CompleteTransmits) {
dcheng59716272016-04-09 05:19:08259 std::unique_ptr<ByteStreamWriter> byte_stream_input;
260 std::unique_ptr<ByteStreamReader> byte_stream_output;
[email protected]b03507862012-05-23 17:11:50261
262 scoped_refptr<net::IOBuffer> output_io_buffer;
263 size_t output_length;
264
265 // Empty stream, non-error case.
Sean Maher5b9af51f2022-11-21 15:32:47266 CreateByteStream(base::SingleThreadTaskRunner::GetCurrentDefault(),
267 base::SingleThreadTaskRunner::GetCurrentDefault(), 3 * 1024,
Alexander Timin58ee05f2018-10-05 11:44:33268 &byte_stream_input, &byte_stream_output);
[email protected]35869622012-10-26 23:23:55269 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50270 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]8d0c23e2013-08-02 11:02:30271 byte_stream_input->Close(0);
fdoraye716a902016-07-05 16:05:49272 base::RunLoop().RunUntilIdle();
[email protected]35869622012-10-26 23:23:55273 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE,
[email protected]b03507862012-05-23 17:11:50274 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]8d0c23e2013-08-02 11:02:30275 EXPECT_EQ(0, byte_stream_output->GetStatus());
[email protected]b03507862012-05-23 17:11:50276
277 // Non-empty stream, non-error case.
Sean Maher5b9af51f2022-11-21 15:32:47278 CreateByteStream(base::SingleThreadTaskRunner::GetCurrentDefault(),
279 base::SingleThreadTaskRunner::GetCurrentDefault(), 3 * 1024,
Alexander Timin58ee05f2018-10-05 11:44:33280 &byte_stream_input, &byte_stream_output);
[email protected]35869622012-10-26 23:23:55281 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50282 byte_stream_output->Read(&output_io_buffer, &output_length));
283 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
[email protected]8d0c23e2013-08-02 11:02:30284 byte_stream_input->Close(0);
fdoraye716a902016-07-05 16:05:49285 base::RunLoop().RunUntilIdle();
[email protected]35869622012-10-26 23:23:55286 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50287 byte_stream_output->Read(&output_io_buffer, &output_length));
288 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
[email protected]35869622012-10-26 23:23:55289 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE,
[email protected]b03507862012-05-23 17:11:50290 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]8d0c23e2013-08-02 11:02:30291 EXPECT_EQ(0, byte_stream_output->GetStatus());
[email protected]b03507862012-05-23 17:11:50292
[email protected]8d0c23e2013-08-02 11:02:30293 const int kFakeErrorCode = 22;
294
295 // Empty stream, error case.
Sean Maher5b9af51f2022-11-21 15:32:47296 CreateByteStream(base::SingleThreadTaskRunner::GetCurrentDefault(),
297 base::SingleThreadTaskRunner::GetCurrentDefault(), 3 * 1024,
Alexander Timin58ee05f2018-10-05 11:44:33298 &byte_stream_input, &byte_stream_output);
[email protected]35869622012-10-26 23:23:55299 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50300 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]8d0c23e2013-08-02 11:02:30301 byte_stream_input->Close(kFakeErrorCode);
fdoraye716a902016-07-05 16:05:49302 base::RunLoop().RunUntilIdle();
[email protected]35869622012-10-26 23:23:55303 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE,
[email protected]b03507862012-05-23 17:11:50304 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]8d0c23e2013-08-02 11:02:30305 EXPECT_EQ(kFakeErrorCode, byte_stream_output->GetStatus());
[email protected]b03507862012-05-23 17:11:50306
[email protected]8d0c23e2013-08-02 11:02:30307 // Non-empty stream, error case.
Sean Maher5b9af51f2022-11-21 15:32:47308 CreateByteStream(base::SingleThreadTaskRunner::GetCurrentDefault(),
309 base::SingleThreadTaskRunner::GetCurrentDefault(), 3 * 1024,
Alexander Timin58ee05f2018-10-05 11:44:33310 &byte_stream_input, &byte_stream_output);
[email protected]35869622012-10-26 23:23:55311 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50312 byte_stream_output->Read(&output_io_buffer, &output_length));
313 EXPECT_TRUE(Write(byte_stream_input.get(), 1024));
[email protected]8d0c23e2013-08-02 11:02:30314 byte_stream_input->Close(kFakeErrorCode);
fdoraye716a902016-07-05 16:05:49315 base::RunLoop().RunUntilIdle();
[email protected]35869622012-10-26 23:23:55316 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50317 byte_stream_output->Read(&output_io_buffer, &output_length));
318 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
[email protected]35869622012-10-26 23:23:55319 ASSERT_EQ(ByteStreamReader::STREAM_COMPLETE,
[email protected]b03507862012-05-23 17:11:50320 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]8d0c23e2013-08-02 11:02:30321 EXPECT_EQ(kFakeErrorCode, byte_stream_output->GetStatus());
[email protected]b03507862012-05-23 17:11:50322}
323
324// Confirm that callbacks on the sink side are triggered when they should be.
325TEST_F(ByteStreamTest, ByteStream_SinkCallback) {
[email protected]a8582b12012-12-19 22:18:29326 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
327 new base::TestSimpleTaskRunner());
[email protected]b03507862012-05-23 17:11:50328
dcheng59716272016-04-09 05:19:08329 std::unique_ptr<ByteStreamWriter> byte_stream_input;
330 std::unique_ptr<ByteStreamReader> byte_stream_output;
Sean Maher5b9af51f2022-11-21 15:32:47331 CreateByteStream(base::SingleThreadTaskRunner::GetCurrentDefault(),
332 task_runner, 10000, &byte_stream_input, &byte_stream_output);
[email protected]b03507862012-05-23 17:11:50333
334 scoped_refptr<net::IOBuffer> output_io_buffer;
335 size_t output_length;
[email protected]b03507862012-05-23 17:11:50336
337 // Note that the specifics of when the callbacks are called with regard
[email protected]d7db4f622012-06-04 18:20:56338 // to how much data is pushed onto the stream is not (currently) part
[email protected]b03507862012-05-23 17:11:50339 // of the interface contract. If it becomes part of the contract, the
340 // tests below should get much more precise.
341
342 // Confirm callback called when you add more than 33% of the buffer.
343
344 // Setup callback
345 int num_callbacks = 0;
346 byte_stream_output->RegisterCallback(
Mark Pilgrimec8bce32018-05-17 18:55:23347 base::BindRepeating(CountCallbacks, &num_callbacks));
[email protected]b03507862012-05-23 17:11:50348
349 EXPECT_TRUE(Write(byte_stream_input.get(), 4000));
fdoraye716a902016-07-05 16:05:49350 base::RunLoop().RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50351
[email protected]b03507862012-05-23 17:11:50352 EXPECT_EQ(0, num_callbacks);
[email protected]a8582b12012-12-19 22:18:29353 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50354 EXPECT_EQ(1, num_callbacks);
355
356 // Check data and stream state.
[email protected]35869622012-10-26 23:23:55357 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50358 byte_stream_output->Read(&output_io_buffer, &output_length));
359 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
[email protected]35869622012-10-26 23:23:55360 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50361 byte_stream_output->Read(&output_io_buffer, &output_length));
362
363 // Confirm callback *isn't* called at less than 33% (by lack of
364 // unexpected call on task runner).
365 EXPECT_TRUE(Write(byte_stream_input.get(), 3000));
fdoraye716a902016-07-05 16:05:49366 base::RunLoop().RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50367
368 // This reflects an implementation artifact that data goes with callbacks,
369 // which should not be considered part of the interface guarantee.
[email protected]35869622012-10-26 23:23:55370 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50371 byte_stream_output->Read(&output_io_buffer, &output_length));
372}
373
374// Confirm that callbacks on the source side are triggered when they should
375// be.
376TEST_F(ByteStreamTest, ByteStream_SourceCallback) {
[email protected]a8582b12012-12-19 22:18:29377 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
378 new base::TestSimpleTaskRunner());
[email protected]b03507862012-05-23 17:11:50379
dcheng59716272016-04-09 05:19:08380 std::unique_ptr<ByteStreamWriter> byte_stream_input;
381 std::unique_ptr<ByteStreamReader> byte_stream_output;
Sean Maher5b9af51f2022-11-21 15:32:47382 CreateByteStream(task_runner,
383 base::SingleThreadTaskRunner::GetCurrentDefault(), 10000,
skyostil95082a62015-06-05 19:53:07384 &byte_stream_input, &byte_stream_output);
[email protected]b03507862012-05-23 17:11:50385
386 scoped_refptr<net::IOBuffer> output_io_buffer;
387 size_t output_length;
[email protected]b03507862012-05-23 17:11:50388
389 // Note that the specifics of when the callbacks are called with regard
[email protected]d7db4f622012-06-04 18:20:56390 // to how much data is pulled from the stream is not (currently) part
[email protected]b03507862012-05-23 17:11:50391 // of the interface contract. If it becomes part of the contract, the
392 // tests below should get much more precise.
393
394 // Confirm callback called when about 33% space available, and not
395 // at other transitions.
396
[email protected]a8582b12012-12-19 22:18:29397 // Add data.
[email protected]b03507862012-05-23 17:11:50398 int num_callbacks = 0;
399 byte_stream_input->RegisterCallback(
Mark Pilgrimec8bce32018-05-17 18:55:23400 base::BindRepeating(CountCallbacks, &num_callbacks));
[email protected]b03507862012-05-23 17:11:50401 EXPECT_TRUE(Write(byte_stream_input.get(), 2000));
402 EXPECT_TRUE(Write(byte_stream_input.get(), 2001));
403 EXPECT_FALSE(Write(byte_stream_input.get(), 6000));
404
405 // Allow bytes to transition (needed for message passing implementation),
406 // and get and validate the data.
fdoraye716a902016-07-05 16:05:49407 base::RunLoop().RunUntilIdle();
[email protected]35869622012-10-26 23:23:55408 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50409 byte_stream_output->Read(&output_io_buffer, &output_length));
410 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
411
[email protected]b03507862012-05-23 17:11:50412 // Grab data, triggering callback. Recorded on dispatch, but doesn't
413 // happen because it's caught by the mock.
[email protected]35869622012-10-26 23:23:55414 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50415 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]b03507862012-05-23 17:11:50416 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
417
418 // Confirm that the callback passed to the mock does what we expect.
419 EXPECT_EQ(0, num_callbacks);
[email protected]a8582b12012-12-19 22:18:29420 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50421 EXPECT_EQ(1, num_callbacks);
422
423 // Same drill with final buffer.
[email protected]35869622012-10-26 23:23:55424 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50425 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]b03507862012-05-23 17:11:50426 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
[email protected]35869622012-10-26 23:23:55427 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50428 byte_stream_output->Read(&output_io_buffer, &output_length));
429 EXPECT_EQ(1, num_callbacks);
[email protected]a8582b12012-12-19 22:18:29430 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50431 // Should have updated the internal structures but not called the
432 // callback.
433 EXPECT_EQ(1, num_callbacks);
434}
435
436// Confirm that racing a change to a sink callback with a post results
437// in the new callback being called.
438TEST_F(ByteStreamTest, ByteStream_SinkInterrupt) {
[email protected]a8582b12012-12-19 22:18:29439 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
440 new base::TestSimpleTaskRunner());
[email protected]b03507862012-05-23 17:11:50441
dcheng59716272016-04-09 05:19:08442 std::unique_ptr<ByteStreamWriter> byte_stream_input;
443 std::unique_ptr<ByteStreamReader> byte_stream_output;
Sean Maher5b9af51f2022-11-21 15:32:47444 CreateByteStream(base::SingleThreadTaskRunner::GetCurrentDefault(),
445 task_runner, 10000, &byte_stream_input, &byte_stream_output);
[email protected]b03507862012-05-23 17:11:50446
447 scoped_refptr<net::IOBuffer> output_io_buffer;
448 size_t output_length;
[email protected]b03507862012-05-23 17:11:50449
[email protected]a8582b12012-12-19 22:18:29450 // Record initial state.
[email protected]b03507862012-05-23 17:11:50451 int num_callbacks = 0;
452 byte_stream_output->RegisterCallback(
Mark Pilgrimec8bce32018-05-17 18:55:23453 base::BindRepeating(CountCallbacks, &num_callbacks));
[email protected]b03507862012-05-23 17:11:50454
455 // Add data, and pass it across.
456 EXPECT_TRUE(Write(byte_stream_input.get(), 4000));
fdoraye716a902016-07-05 16:05:49457 base::RunLoop().RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50458
459 // The task runner should have been hit, but the callback count
460 // isn't changed until we actually run the callback.
[email protected]b03507862012-05-23 17:11:50461 EXPECT_EQ(0, num_callbacks);
462
463 // If we change the callback now, the new one should be run
464 // (simulates race with post task).
465 int num_alt_callbacks = 0;
466 byte_stream_output->RegisterCallback(
Mark Pilgrimec8bce32018-05-17 18:55:23467 base::BindRepeating(CountCallbacks, &num_alt_callbacks));
[email protected]a8582b12012-12-19 22:18:29468 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50469 EXPECT_EQ(0, num_callbacks);
470 EXPECT_EQ(1, num_alt_callbacks);
471
472 // Final cleanup.
[email protected]35869622012-10-26 23:23:55473 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50474 byte_stream_output->Read(&output_io_buffer, &output_length));
475 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
[email protected]35869622012-10-26 23:23:55476 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50477 byte_stream_output->Read(&output_io_buffer, &output_length));
478
479}
480
481// Confirm that racing a change to a source callback with a post results
482// in the new callback being called.
483TEST_F(ByteStreamTest, ByteStream_SourceInterrupt) {
[email protected]a8582b12012-12-19 22:18:29484 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
485 new base::TestSimpleTaskRunner());
[email protected]b03507862012-05-23 17:11:50486
dcheng59716272016-04-09 05:19:08487 std::unique_ptr<ByteStreamWriter> byte_stream_input;
488 std::unique_ptr<ByteStreamReader> byte_stream_output;
Sean Maher5b9af51f2022-11-21 15:32:47489 CreateByteStream(task_runner,
490 base::SingleThreadTaskRunner::GetCurrentDefault(), 10000,
skyostil95082a62015-06-05 19:53:07491 &byte_stream_input, &byte_stream_output);
[email protected]b03507862012-05-23 17:11:50492
493 scoped_refptr<net::IOBuffer> output_io_buffer;
494 size_t output_length;
[email protected]b03507862012-05-23 17:11:50495
[email protected]a8582b12012-12-19 22:18:29496 // Setup state for test.
[email protected]b03507862012-05-23 17:11:50497 int num_callbacks = 0;
498 byte_stream_input->RegisterCallback(
Mark Pilgrimec8bce32018-05-17 18:55:23499 base::BindRepeating(CountCallbacks, &num_callbacks));
[email protected]b03507862012-05-23 17:11:50500 EXPECT_TRUE(Write(byte_stream_input.get(), 2000));
501 EXPECT_TRUE(Write(byte_stream_input.get(), 2001));
502 EXPECT_FALSE(Write(byte_stream_input.get(), 6000));
fdoraye716a902016-07-05 16:05:49503 base::RunLoop().RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50504
505 // Initial get should not trigger callback.
[email protected]35869622012-10-26 23:23:55506 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50507 byte_stream_output->Read(&output_io_buffer, &output_length));
508 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
fdoraye716a902016-07-05 16:05:49509 base::RunLoop().RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50510
[email protected]b03507862012-05-23 17:11:50511 // Second get *should* trigger callback.
[email protected]35869622012-10-26 23:23:55512 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50513 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]b03507862012-05-23 17:11:50514 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
515
516 // Which should do the right thing when it's run.
517 int num_alt_callbacks = 0;
518 byte_stream_input->RegisterCallback(
Mark Pilgrimec8bce32018-05-17 18:55:23519 base::BindRepeating(CountCallbacks, &num_alt_callbacks));
[email protected]a8582b12012-12-19 22:18:29520 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50521 EXPECT_EQ(0, num_callbacks);
522 EXPECT_EQ(1, num_alt_callbacks);
523
524 // Third get should also trigger callback.
[email protected]35869622012-10-26 23:23:55525 EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
[email protected]b03507862012-05-23 17:11:50526 byte_stream_output->Read(&output_io_buffer, &output_length));
[email protected]b03507862012-05-23 17:11:50527 EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
[email protected]35869622012-10-26 23:23:55528 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
[email protected]b03507862012-05-23 17:11:50529 byte_stream_output->Read(&output_io_buffer, &output_length));
530}
531
532// Confirm that callback is called on zero data transfer but source
533// complete.
534TEST_F(ByteStreamTest, ByteStream_ZeroCallback) {
[email protected]a8582b12012-12-19 22:18:29535 scoped_refptr<base::TestSimpleTaskRunner> task_runner(
536 new base::TestSimpleTaskRunner());
[email protected]b03507862012-05-23 17:11:50537
dcheng59716272016-04-09 05:19:08538 std::unique_ptr<ByteStreamWriter> byte_stream_input;
539 std::unique_ptr<ByteStreamReader> byte_stream_output;
Sean Maher5b9af51f2022-11-21 15:32:47540 CreateByteStream(base::SingleThreadTaskRunner::GetCurrentDefault(),
541 task_runner, 10000, &byte_stream_input, &byte_stream_output);
[email protected]b03507862012-05-23 17:11:50542
[email protected]a8582b12012-12-19 22:18:29543 // Record initial state.
[email protected]b03507862012-05-23 17:11:50544 int num_callbacks = 0;
545 byte_stream_output->RegisterCallback(
Mark Pilgrimec8bce32018-05-17 18:55:23546 base::BindRepeating(CountCallbacks, &num_callbacks));
[email protected]b03507862012-05-23 17:11:50547
548 // Immediately close the stream.
[email protected]8d0c23e2013-08-02 11:02:30549 byte_stream_input->Close(0);
[email protected]a8582b12012-12-19 22:18:29550 task_runner->RunUntilIdle();
[email protected]b03507862012-05-23 17:11:50551 EXPECT_EQ(1, num_callbacks);
552}
553
[email protected]6a14c192013-08-06 20:18:42554TEST_F(ByteStreamTest, ByteStream_CloseWithoutAnyWrite) {
dcheng59716272016-04-09 05:19:08555 std::unique_ptr<ByteStreamWriter> byte_stream_input;
556 std::unique_ptr<ByteStreamReader> byte_stream_output;
Sean Maher5b9af51f2022-11-21 15:32:47557 CreateByteStream(base::SingleThreadTaskRunner::GetCurrentDefault(),
558 base::SingleThreadTaskRunner::GetCurrentDefault(), 3 * 1024,
Alexander Timin58ee05f2018-10-05 11:44:33559 &byte_stream_input, &byte_stream_output);
[email protected]6a14c192013-08-06 20:18:42560
561 byte_stream_input->Close(0);
fdoraye716a902016-07-05 16:05:49562 base::RunLoop().RunUntilIdle();
[email protected]6a14c192013-08-06 20:18:42563
564 scoped_refptr<net::IOBuffer> output_io_buffer;
565 size_t output_length;
566 EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE,
567 byte_stream_output->Read(&output_io_buffer, &output_length));
568}
569
570TEST_F(ByteStreamTest, ByteStream_FlushWithoutAnyWrite) {
dcheng59716272016-04-09 05:19:08571 std::unique_ptr<ByteStreamWriter> byte_stream_input;
572 std::unique_ptr<ByteStreamReader> byte_stream_output;
Sean Maher5b9af51f2022-11-21 15:32:47573 CreateByteStream(base::SingleThreadTaskRunner::GetCurrentDefault(),
574 base::SingleThreadTaskRunner::GetCurrentDefault(), 3 * 1024,
Alexander Timin58ee05f2018-10-05 11:44:33575 &byte_stream_input, &byte_stream_output);
[email protected]6a14c192013-08-06 20:18:42576
577 byte_stream_input->Flush();
fdoraye716a902016-07-05 16:05:49578 base::RunLoop().RunUntilIdle();
[email protected]6a14c192013-08-06 20:18:42579
580 scoped_refptr<net::IOBuffer> output_io_buffer;
581 size_t output_length;
582 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
583 byte_stream_output->Read(&output_io_buffer, &output_length));
584
585 byte_stream_input->Close(0);
fdoraye716a902016-07-05 16:05:49586 base::RunLoop().RunUntilIdle();
[email protected]6a14c192013-08-06 20:18:42587
588 EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE,
589 byte_stream_output->Read(&output_io_buffer, &output_length));
590}
591
[email protected]07516262013-08-22 07:43:24592TEST_F(ByteStreamTest, ByteStream_WriteOverflow) {
dcheng59716272016-04-09 05:19:08593 std::unique_ptr<ByteStreamWriter> byte_stream_input;
594 std::unique_ptr<ByteStreamReader> byte_stream_output;
Sean Maher5b9af51f2022-11-21 15:32:47595 CreateByteStream(base::SingleThreadTaskRunner::GetCurrentDefault(),
596 base::SingleThreadTaskRunner::GetCurrentDefault(),
skyostil95082a62015-06-05 19:53:07597 std::numeric_limits<size_t>::max(), &byte_stream_input,
598 &byte_stream_output);
[email protected]07516262013-08-22 07:43:24599
600 EXPECT_TRUE(Write(byte_stream_input.get(), 1));
601 // 1 + size_t max -> Overflow.
602 scoped_refptr<net::IOBuffer> empty_io_buffer;
603 EXPECT_FALSE(byte_stream_input->Write(empty_io_buffer,
604 std::numeric_limits<size_t>::max()));
fdoraye716a902016-07-05 16:05:49605 base::RunLoop().RunUntilIdle();
[email protected]07516262013-08-22 07:43:24606
607 // The first write is below PostToPeer threshold. We shouldn't get anything
608 // from the output.
609 scoped_refptr<net::IOBuffer> output_io_buffer;
610 size_t output_length;
611 EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
612 byte_stream_output->Read(&output_io_buffer, &output_length));
613}
614
[email protected]35869622012-10-26 23:23:55615} // namespace content