blob: 455a34a9bab3b6badf603eeaf608874f56da9f44 [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
Leon Scroggins III63cfb362020-04-24 13:00:48 -04008// Make sure SkUserConfig.h is included so #defines are available on
9// Android.
10#include "include/core/SkTypes.h"
11#ifdef SK_ENABLE_ANDROID_UTILS
12#include "client_utils/android/FrontBufferedStream.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/codec/SkCodec.h"
14#include "include/core/SkBitmap.h"
15#include "include/core/SkRefCnt.h"
16#include "include/core/SkStream.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/core/SkAutoMalloc.h"
18#include "tests/Test.h"
scroggo@google.com83fd2c72013-09-26 21:35:39 +000019
20static void test_read(skiatest::Reporter* reporter, SkStream* bufferedStream,
21 const void* expectations, size_t bytesToRead) {
22 // output for reading bufferedStream.
23 SkAutoMalloc storage(bytesToRead);
24
commit-bot@chromium.org74b88b72014-02-10 22:03:21 +000025 const size_t bytesRead = bufferedStream->read(storage.get(), bytesToRead);
scroggo@google.com83fd2c72013-09-26 21:35:39 +000026 REPORTER_ASSERT(reporter, bytesRead == bytesToRead || bufferedStream->isAtEnd());
27 REPORTER_ASSERT(reporter, memcmp(storage.get(), expectations, bytesRead) == 0);
28}
29
30static void test_rewind(skiatest::Reporter* reporter,
31 SkStream* bufferedStream, bool shouldSucceed) {
32 const bool success = bufferedStream->rewind();
33 REPORTER_ASSERT(reporter, success == shouldSucceed);
34}
35
commit-bot@chromium.org74b88b72014-02-10 22:03:21 +000036// Test that hasLength() returns the correct value, based on the stream
37// being wrapped. A length can only be known if the wrapped stream has a
38// length and it has a position (so its initial position can be taken into
39// account when computing the length).
40static void test_hasLength(skiatest::Reporter* reporter,
41 const SkStream& bufferedStream,
42 const SkStream& streamBeingBuffered) {
43 if (streamBeingBuffered.hasLength() && streamBeingBuffered.hasPosition()) {
44 REPORTER_ASSERT(reporter, bufferedStream.hasLength());
45 } else {
46 REPORTER_ASSERT(reporter, !bufferedStream.hasLength());
47 }
48}
49
scroggo@google.com83fd2c72013-09-26 21:35:39 +000050// All tests will buffer this string, and compare output to the original.
51// The string is long to ensure that all of our lengths being tested are
52// smaller than the string length.
53const char gAbcs[] = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwx";
54
55// Tests reading the stream across boundaries of what has been buffered so far and what
56// the total buffer size is.
57static void test_incremental_buffering(skiatest::Reporter* reporter, size_t bufferSize) {
scroggoa1193e42015-01-21 12:09:53 -080058 // NOTE: For this and other tests in this file, we cheat and continue to refer to the
59 // wrapped stream, but that's okay because we know the wrapping stream has not been
60 // deleted yet (and we only call const methods in it).
Mike Reed98c5d922017-09-15 21:39:47 -040061 SkMemoryStream* memStream = SkMemoryStream::MakeDirect(gAbcs, strlen(gAbcs)).release();
scroggo@google.com83fd2c72013-09-26 21:35:39 +000062
Leon Scroggins III63cfb362020-04-24 13:00:48 -040063 auto bufferedStream = android::skia::FrontBufferedStream::Make(
64 std::unique_ptr<SkStream>(memStream), bufferSize);
65
Ben Wagner145dbcd2016-11-03 14:40:50 -040066 test_hasLength(reporter, *bufferedStream, *memStream);
scroggo@google.com83fd2c72013-09-26 21:35:39 +000067
68 // First, test reading less than the max buffer size.
Ben Wagner145dbcd2016-11-03 14:40:50 -040069 test_read(reporter, bufferedStream.get(), gAbcs, bufferSize / 2);
scroggo@google.com83fd2c72013-09-26 21:35:39 +000070
71 // Now test rewinding back to the beginning and reading less than what was
72 // already buffered.
Ben Wagner145dbcd2016-11-03 14:40:50 -040073 test_rewind(reporter, bufferedStream.get(), true);
74 test_read(reporter, bufferedStream.get(), gAbcs, bufferSize / 4);
scroggo@google.com83fd2c72013-09-26 21:35:39 +000075
76 // Now test reading part of what was buffered, and buffering new data.
Ben Wagner145dbcd2016-11-03 14:40:50 -040077 test_read(reporter, bufferedStream.get(), gAbcs + bufferSize / 4, bufferSize / 2);
scroggo@google.com83fd2c72013-09-26 21:35:39 +000078
79 // Now test reading what was buffered, buffering new data, and
80 // reading directly from the stream.
Ben Wagner145dbcd2016-11-03 14:40:50 -040081 test_rewind(reporter, bufferedStream.get(), true);
82 test_read(reporter, bufferedStream.get(), gAbcs, bufferSize << 1);
scroggo@google.com83fd2c72013-09-26 21:35:39 +000083
84 // We have reached the end of the buffer, so rewinding will fail.
85 // This test assumes that the stream is larger than the buffer; otherwise the
86 // result of rewind should be true.
Ben Wagner145dbcd2016-11-03 14:40:50 -040087 test_rewind(reporter, bufferedStream.get(), false);
scroggo@google.com83fd2c72013-09-26 21:35:39 +000088}
89
90static void test_perfectly_sized_buffer(skiatest::Reporter* reporter, size_t bufferSize) {
Mike Reed98c5d922017-09-15 21:39:47 -040091 SkMemoryStream* memStream = SkMemoryStream::MakeDirect(gAbcs, strlen(gAbcs)).release();
Leon Scroggins III63cfb362020-04-24 13:00:48 -040092 auto bufferedStream = android::skia::FrontBufferedStream::Make(
93 std::unique_ptr<SkStream>(memStream), bufferSize);
Ben Wagner145dbcd2016-11-03 14:40:50 -040094 test_hasLength(reporter, *bufferedStream, *memStream);
scroggo@google.com83fd2c72013-09-26 21:35:39 +000095
96 // Read exactly the amount that fits in the buffer.
Ben Wagner145dbcd2016-11-03 14:40:50 -040097 test_read(reporter, bufferedStream.get(), gAbcs, bufferSize);
scroggo@google.com83fd2c72013-09-26 21:35:39 +000098
99 // Rewinding should succeed.
Ben Wagner145dbcd2016-11-03 14:40:50 -0400100 test_rewind(reporter, bufferedStream.get(), true);
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000101
102 // Once again reading buffered info should succeed
Ben Wagner145dbcd2016-11-03 14:40:50 -0400103 test_read(reporter, bufferedStream.get(), gAbcs, bufferSize);
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000104
105 // Read past the size of the buffer. At this point, we cannot return.
Ben Wagner145dbcd2016-11-03 14:40:50 -0400106 test_read(reporter, bufferedStream.get(), gAbcs + memStream->getPosition(), 1);
107 test_rewind(reporter, bufferedStream.get(), false);
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000108}
109
110static void test_skipping(skiatest::Reporter* reporter, size_t bufferSize) {
Mike Reed98c5d922017-09-15 21:39:47 -0400111 SkMemoryStream* memStream = SkMemoryStream::MakeDirect(gAbcs, strlen(gAbcs)).release();
Leon Scroggins III63cfb362020-04-24 13:00:48 -0400112 auto bufferedStream = android::skia::FrontBufferedStream::Make(
113 std::unique_ptr<SkStream>(memStream), bufferSize);
Ben Wagner145dbcd2016-11-03 14:40:50 -0400114 test_hasLength(reporter, *bufferedStream, *memStream);
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000115
116 // Skip half the buffer.
117 bufferedStream->skip(bufferSize / 2);
118
119 // Rewind, then read part of the buffer, which should have been read.
Ben Wagner145dbcd2016-11-03 14:40:50 -0400120 test_rewind(reporter, bufferedStream.get(), true);
121 test_read(reporter, bufferedStream.get(), gAbcs, bufferSize / 4);
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000122
123 // Now skip beyond the buffered piece, but still within the total buffer.
124 bufferedStream->skip(bufferSize / 2);
125
126 // Test that reading will still work.
Ben Wagner145dbcd2016-11-03 14:40:50 -0400127 test_read(reporter, bufferedStream.get(), gAbcs + memStream->getPosition(), bufferSize / 4);
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000128
Ben Wagner145dbcd2016-11-03 14:40:50 -0400129 test_rewind(reporter, bufferedStream.get(), true);
130 test_read(reporter, bufferedStream.get(), gAbcs, bufferSize);
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000131}
132
133// A custom class whose isAtEnd behaves the way Android's stream does - since it is an adaptor to a
134// Java InputStream, it does not know that it is at the end until it has attempted to read beyond
135// the end and failed. Used by test_read_beyond_buffer.
136class AndroidLikeMemoryStream : public SkMemoryStream {
137public:
138 AndroidLikeMemoryStream(void* data, size_t size, bool ownMemory)
139 : INHERITED(data, size, ownMemory)
140 , fIsAtEnd(false) {}
141
mtklein36352bf2015-03-25 18:17:31 -0700142 size_t read(void* dst, size_t requested) override {
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000143 size_t bytesRead = this->INHERITED::read(dst, requested);
144 if (bytesRead < requested) {
145 fIsAtEnd = true;
146 }
147 return bytesRead;
148 }
149
mtklein36352bf2015-03-25 18:17:31 -0700150 bool isAtEnd() const override {
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000151 return fIsAtEnd;
152 }
153
154private:
155 bool fIsAtEnd;
156 typedef SkMemoryStream INHERITED;
157};
158
159// This test ensures that buffering the exact length of the stream and attempting to read beyond it
160// does not invalidate the buffer.
161static void test_read_beyond_buffer(skiatest::Reporter* reporter, size_t bufferSize) {
162 // Use a stream that behaves like Android's stream.
halcanary385fe4d2015-08-26 13:07:48 -0700163 AndroidLikeMemoryStream* memStream =
164 new AndroidLikeMemoryStream((void*)gAbcs, bufferSize, false);
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000165
166 // Create a buffer that matches the length of the stream.
Leon Scroggins III63cfb362020-04-24 13:00:48 -0400167 auto bufferedStream = android::skia::FrontBufferedStream::Make(
168 std::unique_ptr<SkStream>(memStream), bufferSize);
scroggoa1193e42015-01-21 12:09:53 -0800169 test_hasLength(reporter, *bufferedStream.get(), *memStream);
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000170
171 // Attempt to read one more than the bufferSize
172 test_read(reporter, bufferedStream.get(), gAbcs, bufferSize + 1);
173 test_rewind(reporter, bufferedStream.get(), true);
174
175 // Ensure that the initial read did not invalidate the buffer.
Ben Wagner145dbcd2016-11-03 14:40:50 -0400176 test_read(reporter, bufferedStream.get(), gAbcs, bufferSize);
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000177}
178
commit-bot@chromium.org74b88b72014-02-10 22:03:21 +0000179// Dummy stream that optionally has a length and/or position. Tests that FrontBufferedStream's
180// length depends on the stream it's buffering having a length and position.
181class LengthOptionalStream : public SkStream {
182public:
183 LengthOptionalStream(bool hasLength, bool hasPosition)
184 : fHasLength(hasLength)
185 , fHasPosition(hasPosition)
186 {}
187
mtklein36352bf2015-03-25 18:17:31 -0700188 bool hasLength() const override {
commit-bot@chromium.org74b88b72014-02-10 22:03:21 +0000189 return fHasLength;
190 }
191
mtklein36352bf2015-03-25 18:17:31 -0700192 bool hasPosition() const override {
commit-bot@chromium.org74b88b72014-02-10 22:03:21 +0000193 return fHasPosition;
194 }
195
mtklein36352bf2015-03-25 18:17:31 -0700196 size_t read(void*, size_t) override {
commit-bot@chromium.org74b88b72014-02-10 22:03:21 +0000197 return 0;
198 }
199
mtklein36352bf2015-03-25 18:17:31 -0700200 bool isAtEnd() const override {
commit-bot@chromium.org74b88b72014-02-10 22:03:21 +0000201 return true;
202 }
203
204private:
205 const bool fHasLength;
206 const bool fHasPosition;
207};
208
209// Test all possible combinations of the wrapped stream having a length and a position.
210static void test_length_combos(skiatest::Reporter* reporter, size_t bufferSize) {
211 for (int hasLen = 0; hasLen <= 1; hasLen++) {
212 for (int hasPos = 0; hasPos <= 1; hasPos++) {
halcanary385fe4d2015-08-26 13:07:48 -0700213 LengthOptionalStream* stream =
214 new LengthOptionalStream(SkToBool(hasLen), SkToBool(hasPos));
Leon Scroggins III63cfb362020-04-24 13:00:48 -0400215 auto buffered = android::skia::FrontBufferedStream::Make(
216 std::unique_ptr<SkStream>(stream), bufferSize);
scroggoa1193e42015-01-21 12:09:53 -0800217 test_hasLength(reporter, *buffered.get(), *stream);
commit-bot@chromium.org74b88b72014-02-10 22:03:21 +0000218 }
219 }
220}
221
222// Test using a stream with an initial offset.
223static void test_initial_offset(skiatest::Reporter* reporter, size_t bufferSize) {
halcanary385fe4d2015-08-26 13:07:48 -0700224 SkMemoryStream* memStream = new SkMemoryStream(gAbcs, strlen(gAbcs), false);
commit-bot@chromium.org74b88b72014-02-10 22:03:21 +0000225
226 // Skip a few characters into the memStream, so that bufferedStream represents an offset into
227 // the stream it wraps.
228 const size_t arbitraryOffset = 17;
scroggoa1193e42015-01-21 12:09:53 -0800229 memStream->skip(arbitraryOffset);
Leon Scroggins III63cfb362020-04-24 13:00:48 -0400230 auto bufferedStream = android::skia::FrontBufferedStream::Make(
231 std::unique_ptr<SkStream>(memStream), bufferSize);
commit-bot@chromium.org74b88b72014-02-10 22:03:21 +0000232
scroggoef0fed32016-02-18 05:59:25 -0800233 // Since SkMemoryStream has a length, bufferedStream must also.
commit-bot@chromium.org74b88b72014-02-10 22:03:21 +0000234 REPORTER_ASSERT(reporter, bufferedStream->hasLength());
235
236 const size_t amountToRead = 10;
237 const size_t bufferedLength = bufferedStream->getLength();
scroggoef0fed32016-02-18 05:59:25 -0800238 size_t currentPosition = 0;
commit-bot@chromium.org74b88b72014-02-10 22:03:21 +0000239
240 // Read the stream in chunks. After each read, the position must match currentPosition,
241 // which sums the amount attempted to read, unless the end of the stream has been reached.
242 // Importantly, the end should not have been reached until currentPosition == bufferedLength.
243 while (currentPosition < bufferedLength) {
244 REPORTER_ASSERT(reporter, !bufferedStream->isAtEnd());
Ben Wagner145dbcd2016-11-03 14:40:50 -0400245 test_read(reporter, bufferedStream.get(), gAbcs + arbitraryOffset + currentPosition,
commit-bot@chromium.org74b88b72014-02-10 22:03:21 +0000246 amountToRead);
Brian Osman788b9162020-02-07 10:36:46 -0500247 currentPosition = std::min(currentPosition + amountToRead, bufferedLength);
scroggoef0fed32016-02-18 05:59:25 -0800248 REPORTER_ASSERT(reporter, memStream->getPosition() - arbitraryOffset == currentPosition);
commit-bot@chromium.org74b88b72014-02-10 22:03:21 +0000249 }
250 REPORTER_ASSERT(reporter, bufferedStream->isAtEnd());
251 REPORTER_ASSERT(reporter, bufferedLength == currentPosition);
252}
253
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000254static void test_buffers(skiatest::Reporter* reporter, size_t bufferSize) {
255 test_incremental_buffering(reporter, bufferSize);
256 test_perfectly_sized_buffer(reporter, bufferSize);
257 test_skipping(reporter, bufferSize);
258 test_read_beyond_buffer(reporter, bufferSize);
commit-bot@chromium.org74b88b72014-02-10 22:03:21 +0000259 test_length_combos(reporter, bufferSize);
260 test_initial_offset(reporter, bufferSize);
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000261}
262
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000263DEF_TEST(FrontBufferedStream, reporter) {
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000264 // Test 6 and 64, which are used by Android, as well as another arbitrary length.
265 test_buffers(reporter, 6);
266 test_buffers(reporter, 15);
267 test_buffers(reporter, 64);
268}
scroggo3ceef9a2014-10-24 06:55:07 -0700269
270// Test that a FrontBufferedStream does not allow reading after the end of a stream.
271// This class is a dummy SkStream which reports that it is at the end on the first
272// read (simulating a failure). Then it tracks whether someone calls read() again.
273class FailingStream : public SkStream {
274public:
275 FailingStream()
276 : fAtEnd(false)
scroggo3ceef9a2014-10-24 06:55:07 -0700277 {}
msarettb9e56c12016-03-01 13:29:15 -0800278
mtklein36352bf2015-03-25 18:17:31 -0700279 size_t read(void* buffer, size_t size) override {
msarettb9e56c12016-03-01 13:29:15 -0800280 SkASSERT(!fAtEnd);
281 fAtEnd = true;
scroggo3ceef9a2014-10-24 06:55:07 -0700282 return 0;
283 }
284
mtklein36352bf2015-03-25 18:17:31 -0700285 bool isAtEnd() const override {
scroggo3ceef9a2014-10-24 06:55:07 -0700286 return fAtEnd;
287 }
288
scroggo3ceef9a2014-10-24 06:55:07 -0700289private:
290 bool fAtEnd;
scroggo3ceef9a2014-10-24 06:55:07 -0700291};
292
293DEF_TEST(ShortFrontBufferedStream, reporter) {
halcanary385fe4d2015-08-26 13:07:48 -0700294 FailingStream* failingStream = new FailingStream;
Leon Scroggins III63cfb362020-04-24 13:00:48 -0400295 auto stream = android::skia::FrontBufferedStream::Make(
296 std::unique_ptr<SkStream>(failingStream), 64);
msarett7f7ec202016-03-01 12:12:27 -0800297
298 // This will fail to create a codec. However, what we really want to test is that we
299 // won't read past the end of the stream.
Mike Reedede7bac2017-07-23 15:30:02 -0400300 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream(std::move(stream)));
scroggo3ceef9a2014-10-24 06:55:07 -0700301}
Leon Scroggins III63cfb362020-04-24 13:00:48 -0400302#endif // SK_ENABLE_ANDROID_UTILS