blob: e3f43521d9d99367c9d55463337245121418d3c1 [file] [log] [blame]
scroggo@google.com83fd2c72013-09-26 21:35:39 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/codec/SkCodec.h"
9#include "include/core/SkBitmap.h"
10#include "include/core/SkRefCnt.h"
11#include "include/core/SkStream.h"
12#include "include/utils/SkFrontBufferedStream.h"
13#include "src/core/SkAutoMalloc.h"
14#include "tests/Test.h"
scroggo@google.com83fd2c72013-09-26 21:35:39 +000015
16static void test_read(skiatest::Reporter* reporter, SkStream* bufferedStream,
17 const void* expectations, size_t bytesToRead) {
18 // output for reading bufferedStream.
19 SkAutoMalloc storage(bytesToRead);
20
commit-bot@chromium.org74b88b72014-02-10 22:03:21 +000021 const size_t bytesRead = bufferedStream->read(storage.get(), bytesToRead);
scroggo@google.com83fd2c72013-09-26 21:35:39 +000022 REPORTER_ASSERT(reporter, bytesRead == bytesToRead || bufferedStream->isAtEnd());
23 REPORTER_ASSERT(reporter, memcmp(storage.get(), expectations, bytesRead) == 0);
24}
25
26static void test_rewind(skiatest::Reporter* reporter,
27 SkStream* bufferedStream, bool shouldSucceed) {
28 const bool success = bufferedStream->rewind();
29 REPORTER_ASSERT(reporter, success == shouldSucceed);
30}
31
commit-bot@chromium.org74b88b72014-02-10 22:03:21 +000032// Test that hasLength() returns the correct value, based on the stream
33// being wrapped. A length can only be known if the wrapped stream has a
34// length and it has a position (so its initial position can be taken into
35// account when computing the length).
36static void test_hasLength(skiatest::Reporter* reporter,
37 const SkStream& bufferedStream,
38 const SkStream& streamBeingBuffered) {
39 if (streamBeingBuffered.hasLength() && streamBeingBuffered.hasPosition()) {
40 REPORTER_ASSERT(reporter, bufferedStream.hasLength());
41 } else {
42 REPORTER_ASSERT(reporter, !bufferedStream.hasLength());
43 }
44}
45
scroggo@google.com83fd2c72013-09-26 21:35:39 +000046// All tests will buffer this string, and compare output to the original.
47// The string is long to ensure that all of our lengths being tested are
48// smaller than the string length.
49const char gAbcs[] = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwx";
50
51// Tests reading the stream across boundaries of what has been buffered so far and what
52// the total buffer size is.
53static void test_incremental_buffering(skiatest::Reporter* reporter, size_t bufferSize) {
scroggoa1193e42015-01-21 12:09:53 -080054 // NOTE: For this and other tests in this file, we cheat and continue to refer to the
55 // wrapped stream, but that's okay because we know the wrapping stream has not been
56 // deleted yet (and we only call const methods in it).
Mike Reed98c5d922017-09-15 21:39:47 -040057 SkMemoryStream* memStream = SkMemoryStream::MakeDirect(gAbcs, strlen(gAbcs)).release();
scroggo@google.com83fd2c72013-09-26 21:35:39 +000058
Mike Reed98c5d922017-09-15 21:39:47 -040059 auto bufferedStream = SkFrontBufferedStream::Make(std::unique_ptr<SkStream>(memStream),
60 bufferSize);
Ben Wagner145dbcd2016-11-03 14:40:50 -040061 test_hasLength(reporter, *bufferedStream, *memStream);
scroggo@google.com83fd2c72013-09-26 21:35:39 +000062
63 // First, test reading less than the max buffer size.
Ben Wagner145dbcd2016-11-03 14:40:50 -040064 test_read(reporter, bufferedStream.get(), gAbcs, bufferSize / 2);
scroggo@google.com83fd2c72013-09-26 21:35:39 +000065
66 // Now test rewinding back to the beginning and reading less than what was
67 // already buffered.
Ben Wagner145dbcd2016-11-03 14:40:50 -040068 test_rewind(reporter, bufferedStream.get(), true);
69 test_read(reporter, bufferedStream.get(), gAbcs, bufferSize / 4);
scroggo@google.com83fd2c72013-09-26 21:35:39 +000070
71 // Now test reading part of what was buffered, and buffering new data.
Ben Wagner145dbcd2016-11-03 14:40:50 -040072 test_read(reporter, bufferedStream.get(), gAbcs + bufferSize / 4, bufferSize / 2);
scroggo@google.com83fd2c72013-09-26 21:35:39 +000073
74 // Now test reading what was buffered, buffering new data, and
75 // reading directly from the stream.
Ben Wagner145dbcd2016-11-03 14:40:50 -040076 test_rewind(reporter, bufferedStream.get(), true);
77 test_read(reporter, bufferedStream.get(), gAbcs, bufferSize << 1);
scroggo@google.com83fd2c72013-09-26 21:35:39 +000078
79 // We have reached the end of the buffer, so rewinding will fail.
80 // This test assumes that the stream is larger than the buffer; otherwise the
81 // result of rewind should be true.
Ben Wagner145dbcd2016-11-03 14:40:50 -040082 test_rewind(reporter, bufferedStream.get(), false);
scroggo@google.com83fd2c72013-09-26 21:35:39 +000083}
84
85static void test_perfectly_sized_buffer(skiatest::Reporter* reporter, size_t bufferSize) {
Mike Reed98c5d922017-09-15 21:39:47 -040086 SkMemoryStream* memStream = SkMemoryStream::MakeDirect(gAbcs, strlen(gAbcs)).release();
87 auto bufferedStream = SkFrontBufferedStream::Make(std::unique_ptr<SkStream>(memStream),
88 bufferSize);
Ben Wagner145dbcd2016-11-03 14:40:50 -040089 test_hasLength(reporter, *bufferedStream, *memStream);
scroggo@google.com83fd2c72013-09-26 21:35:39 +000090
91 // Read exactly the amount that fits in the buffer.
Ben Wagner145dbcd2016-11-03 14:40:50 -040092 test_read(reporter, bufferedStream.get(), gAbcs, bufferSize);
scroggo@google.com83fd2c72013-09-26 21:35:39 +000093
94 // Rewinding should succeed.
Ben Wagner145dbcd2016-11-03 14:40:50 -040095 test_rewind(reporter, bufferedStream.get(), true);
scroggo@google.com83fd2c72013-09-26 21:35:39 +000096
97 // Once again reading buffered info should succeed
Ben Wagner145dbcd2016-11-03 14:40:50 -040098 test_read(reporter, bufferedStream.get(), gAbcs, bufferSize);
scroggo@google.com83fd2c72013-09-26 21:35:39 +000099
100 // Read past the size of the buffer. At this point, we cannot return.
Ben Wagner145dbcd2016-11-03 14:40:50 -0400101 test_read(reporter, bufferedStream.get(), gAbcs + memStream->getPosition(), 1);
102 test_rewind(reporter, bufferedStream.get(), false);
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000103}
104
105static void test_skipping(skiatest::Reporter* reporter, size_t bufferSize) {
Mike Reed98c5d922017-09-15 21:39:47 -0400106 SkMemoryStream* memStream = SkMemoryStream::MakeDirect(gAbcs, strlen(gAbcs)).release();
107 auto bufferedStream = SkFrontBufferedStream::Make(std::unique_ptr<SkStream>(memStream),
108 bufferSize);
Ben Wagner145dbcd2016-11-03 14:40:50 -0400109 test_hasLength(reporter, *bufferedStream, *memStream);
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000110
111 // Skip half the buffer.
112 bufferedStream->skip(bufferSize / 2);
113
114 // Rewind, then read part of the buffer, which should have been read.
Ben Wagner145dbcd2016-11-03 14:40:50 -0400115 test_rewind(reporter, bufferedStream.get(), true);
116 test_read(reporter, bufferedStream.get(), gAbcs, bufferSize / 4);
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000117
118 // Now skip beyond the buffered piece, but still within the total buffer.
119 bufferedStream->skip(bufferSize / 2);
120
121 // Test that reading will still work.
Ben Wagner145dbcd2016-11-03 14:40:50 -0400122 test_read(reporter, bufferedStream.get(), gAbcs + memStream->getPosition(), bufferSize / 4);
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000123
Ben Wagner145dbcd2016-11-03 14:40:50 -0400124 test_rewind(reporter, bufferedStream.get(), true);
125 test_read(reporter, bufferedStream.get(), gAbcs, bufferSize);
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000126}
127
128// A custom class whose isAtEnd behaves the way Android's stream does - since it is an adaptor to a
129// Java InputStream, it does not know that it is at the end until it has attempted to read beyond
130// the end and failed. Used by test_read_beyond_buffer.
131class AndroidLikeMemoryStream : public SkMemoryStream {
132public:
133 AndroidLikeMemoryStream(void* data, size_t size, bool ownMemory)
134 : INHERITED(data, size, ownMemory)
135 , fIsAtEnd(false) {}
136
mtklein36352bf2015-03-25 18:17:31 -0700137 size_t read(void* dst, size_t requested) override {
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000138 size_t bytesRead = this->INHERITED::read(dst, requested);
139 if (bytesRead < requested) {
140 fIsAtEnd = true;
141 }
142 return bytesRead;
143 }
144
mtklein36352bf2015-03-25 18:17:31 -0700145 bool isAtEnd() const override {
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000146 return fIsAtEnd;
147 }
148
149private:
150 bool fIsAtEnd;
151 typedef SkMemoryStream INHERITED;
152};
153
154// This test ensures that buffering the exact length of the stream and attempting to read beyond it
155// does not invalidate the buffer.
156static void test_read_beyond_buffer(skiatest::Reporter* reporter, size_t bufferSize) {
157 // Use a stream that behaves like Android's stream.
halcanary385fe4d2015-08-26 13:07:48 -0700158 AndroidLikeMemoryStream* memStream =
159 new AndroidLikeMemoryStream((void*)gAbcs, bufferSize, false);
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000160
161 // Create a buffer that matches the length of the stream.
Mike Reed98c5d922017-09-15 21:39:47 -0400162 auto bufferedStream = SkFrontBufferedStream::Make(std::unique_ptr<SkStream>(memStream),
163 bufferSize);
scroggoa1193e42015-01-21 12:09:53 -0800164 test_hasLength(reporter, *bufferedStream.get(), *memStream);
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000165
166 // Attempt to read one more than the bufferSize
167 test_read(reporter, bufferedStream.get(), gAbcs, bufferSize + 1);
168 test_rewind(reporter, bufferedStream.get(), true);
169
170 // Ensure that the initial read did not invalidate the buffer.
Ben Wagner145dbcd2016-11-03 14:40:50 -0400171 test_read(reporter, bufferedStream.get(), gAbcs, bufferSize);
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000172}
173
commit-bot@chromium.org74b88b72014-02-10 22:03:21 +0000174// Dummy stream that optionally has a length and/or position. Tests that FrontBufferedStream's
175// length depends on the stream it's buffering having a length and position.
176class LengthOptionalStream : public SkStream {
177public:
178 LengthOptionalStream(bool hasLength, bool hasPosition)
179 : fHasLength(hasLength)
180 , fHasPosition(hasPosition)
181 {}
182
mtklein36352bf2015-03-25 18:17:31 -0700183 bool hasLength() const override {
commit-bot@chromium.org74b88b72014-02-10 22:03:21 +0000184 return fHasLength;
185 }
186
mtklein36352bf2015-03-25 18:17:31 -0700187 bool hasPosition() const override {
commit-bot@chromium.org74b88b72014-02-10 22:03:21 +0000188 return fHasPosition;
189 }
190
mtklein36352bf2015-03-25 18:17:31 -0700191 size_t read(void*, size_t) override {
commit-bot@chromium.org74b88b72014-02-10 22:03:21 +0000192 return 0;
193 }
194
mtklein36352bf2015-03-25 18:17:31 -0700195 bool isAtEnd() const override {
commit-bot@chromium.org74b88b72014-02-10 22:03:21 +0000196 return true;
197 }
198
199private:
200 const bool fHasLength;
201 const bool fHasPosition;
202};
203
204// Test all possible combinations of the wrapped stream having a length and a position.
205static void test_length_combos(skiatest::Reporter* reporter, size_t bufferSize) {
206 for (int hasLen = 0; hasLen <= 1; hasLen++) {
207 for (int hasPos = 0; hasPos <= 1; hasPos++) {
halcanary385fe4d2015-08-26 13:07:48 -0700208 LengthOptionalStream* stream =
209 new LengthOptionalStream(SkToBool(hasLen), SkToBool(hasPos));
Mike Reed98c5d922017-09-15 21:39:47 -0400210 auto buffered = SkFrontBufferedStream::Make(std::unique_ptr<SkStream>(stream),
211 bufferSize);
scroggoa1193e42015-01-21 12:09:53 -0800212 test_hasLength(reporter, *buffered.get(), *stream);
commit-bot@chromium.org74b88b72014-02-10 22:03:21 +0000213 }
214 }
215}
216
217// Test using a stream with an initial offset.
218static void test_initial_offset(skiatest::Reporter* reporter, size_t bufferSize) {
halcanary385fe4d2015-08-26 13:07:48 -0700219 SkMemoryStream* memStream = new SkMemoryStream(gAbcs, strlen(gAbcs), false);
commit-bot@chromium.org74b88b72014-02-10 22:03:21 +0000220
221 // Skip a few characters into the memStream, so that bufferedStream represents an offset into
222 // the stream it wraps.
223 const size_t arbitraryOffset = 17;
scroggoa1193e42015-01-21 12:09:53 -0800224 memStream->skip(arbitraryOffset);
Mike Reed98c5d922017-09-15 21:39:47 -0400225 auto bufferedStream = SkFrontBufferedStream::Make(std::unique_ptr<SkStream>(memStream),
226 bufferSize);
commit-bot@chromium.org74b88b72014-02-10 22:03:21 +0000227
scroggoef0fed32016-02-18 05:59:25 -0800228 // Since SkMemoryStream has a length, bufferedStream must also.
commit-bot@chromium.org74b88b72014-02-10 22:03:21 +0000229 REPORTER_ASSERT(reporter, bufferedStream->hasLength());
230
231 const size_t amountToRead = 10;
232 const size_t bufferedLength = bufferedStream->getLength();
scroggoef0fed32016-02-18 05:59:25 -0800233 size_t currentPosition = 0;
commit-bot@chromium.org74b88b72014-02-10 22:03:21 +0000234
235 // Read the stream in chunks. After each read, the position must match currentPosition,
236 // which sums the amount attempted to read, unless the end of the stream has been reached.
237 // Importantly, the end should not have been reached until currentPosition == bufferedLength.
238 while (currentPosition < bufferedLength) {
239 REPORTER_ASSERT(reporter, !bufferedStream->isAtEnd());
Ben Wagner145dbcd2016-11-03 14:40:50 -0400240 test_read(reporter, bufferedStream.get(), gAbcs + arbitraryOffset + currentPosition,
commit-bot@chromium.org74b88b72014-02-10 22:03:21 +0000241 amountToRead);
242 currentPosition = SkTMin(currentPosition + amountToRead, bufferedLength);
scroggoef0fed32016-02-18 05:59:25 -0800243 REPORTER_ASSERT(reporter, memStream->getPosition() - arbitraryOffset == currentPosition);
commit-bot@chromium.org74b88b72014-02-10 22:03:21 +0000244 }
245 REPORTER_ASSERT(reporter, bufferedStream->isAtEnd());
246 REPORTER_ASSERT(reporter, bufferedLength == currentPosition);
247}
248
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000249static void test_buffers(skiatest::Reporter* reporter, size_t bufferSize) {
250 test_incremental_buffering(reporter, bufferSize);
251 test_perfectly_sized_buffer(reporter, bufferSize);
252 test_skipping(reporter, bufferSize);
253 test_read_beyond_buffer(reporter, bufferSize);
commit-bot@chromium.org74b88b72014-02-10 22:03:21 +0000254 test_length_combos(reporter, bufferSize);
255 test_initial_offset(reporter, bufferSize);
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000256}
257
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000258DEF_TEST(FrontBufferedStream, reporter) {
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000259 // Test 6 and 64, which are used by Android, as well as another arbitrary length.
260 test_buffers(reporter, 6);
261 test_buffers(reporter, 15);
262 test_buffers(reporter, 64);
263}
scroggo3ceef9a2014-10-24 06:55:07 -0700264
265// Test that a FrontBufferedStream does not allow reading after the end of a stream.
266// This class is a dummy SkStream which reports that it is at the end on the first
267// read (simulating a failure). Then it tracks whether someone calls read() again.
268class FailingStream : public SkStream {
269public:
270 FailingStream()
271 : fAtEnd(false)
scroggo3ceef9a2014-10-24 06:55:07 -0700272 {}
msarettb9e56c12016-03-01 13:29:15 -0800273
mtklein36352bf2015-03-25 18:17:31 -0700274 size_t read(void* buffer, size_t size) override {
msarettb9e56c12016-03-01 13:29:15 -0800275 SkASSERT(!fAtEnd);
276 fAtEnd = true;
scroggo3ceef9a2014-10-24 06:55:07 -0700277 return 0;
278 }
279
mtklein36352bf2015-03-25 18:17:31 -0700280 bool isAtEnd() const override {
scroggo3ceef9a2014-10-24 06:55:07 -0700281 return fAtEnd;
282 }
283
scroggo3ceef9a2014-10-24 06:55:07 -0700284private:
285 bool fAtEnd;
scroggo3ceef9a2014-10-24 06:55:07 -0700286};
287
288DEF_TEST(ShortFrontBufferedStream, reporter) {
halcanary385fe4d2015-08-26 13:07:48 -0700289 FailingStream* failingStream = new FailingStream;
Mike Reed98c5d922017-09-15 21:39:47 -0400290 auto stream = SkFrontBufferedStream::Make(std::unique_ptr<SkStream>(failingStream), 64);
msarett7f7ec202016-03-01 12:12:27 -0800291
292 // This will fail to create a codec. However, what we really want to test is that we
293 // won't read past the end of the stream.
Mike Reedede7bac2017-07-23 15:30:02 -0400294 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream(std::move(stream)));
scroggo3ceef9a2014-10-24 06:55:07 -0700295}