blob: a3862741e3713f50066bdc8302a30ed594288b0e [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
scroggo3ceef9a2014-10-24 06:55:07 -07008#include "SkBitmap.h"
msarett7f7ec202016-03-01 12:12:27 -08009#include "SkCodec.h"
scroggo@google.com83fd2c72013-09-26 21:35:39 +000010#include "SkFrontBufferedStream.h"
11#include "SkRefCnt.h"
scroggo@google.com09a53832013-11-12 20:53:05 +000012#include "SkStream.h"
scroggo@google.com83fd2c72013-09-26 21:35:39 +000013#include "SkTypes.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000014#include "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).
halcanary385fe4d2015-08-26 13:07:48 -070057 SkMemoryStream* memStream = new SkMemoryStream(gAbcs, strlen(gAbcs), false);
scroggo@google.com83fd2c72013-09-26 21:35:39 +000058
Ben Wagner145dbcd2016-11-03 14:40:50 -040059 std::unique_ptr<SkStream> bufferedStream(SkFrontBufferedStream::Create(memStream, bufferSize));
60 test_hasLength(reporter, *bufferedStream, *memStream);
scroggo@google.com83fd2c72013-09-26 21:35:39 +000061
62 // First, test reading less than the max buffer size.
Ben Wagner145dbcd2016-11-03 14:40:50 -040063 test_read(reporter, bufferedStream.get(), gAbcs, bufferSize / 2);
scroggo@google.com83fd2c72013-09-26 21:35:39 +000064
65 // Now test rewinding back to the beginning and reading less than what was
66 // already buffered.
Ben Wagner145dbcd2016-11-03 14:40:50 -040067 test_rewind(reporter, bufferedStream.get(), true);
68 test_read(reporter, bufferedStream.get(), gAbcs, bufferSize / 4);
scroggo@google.com83fd2c72013-09-26 21:35:39 +000069
70 // Now test reading part of what was buffered, and buffering new data.
Ben Wagner145dbcd2016-11-03 14:40:50 -040071 test_read(reporter, bufferedStream.get(), gAbcs + bufferSize / 4, bufferSize / 2);
scroggo@google.com83fd2c72013-09-26 21:35:39 +000072
73 // Now test reading what was buffered, buffering new data, and
74 // reading directly from the stream.
Ben Wagner145dbcd2016-11-03 14:40:50 -040075 test_rewind(reporter, bufferedStream.get(), true);
76 test_read(reporter, bufferedStream.get(), gAbcs, bufferSize << 1);
scroggo@google.com83fd2c72013-09-26 21:35:39 +000077
78 // We have reached the end of the buffer, so rewinding will fail.
79 // This test assumes that the stream is larger than the buffer; otherwise the
80 // result of rewind should be true.
Ben Wagner145dbcd2016-11-03 14:40:50 -040081 test_rewind(reporter, bufferedStream.get(), false);
scroggo@google.com83fd2c72013-09-26 21:35:39 +000082}
83
84static void test_perfectly_sized_buffer(skiatest::Reporter* reporter, size_t bufferSize) {
halcanary385fe4d2015-08-26 13:07:48 -070085 SkMemoryStream* memStream = new SkMemoryStream(gAbcs, strlen(gAbcs), false);
Ben Wagner145dbcd2016-11-03 14:40:50 -040086 std::unique_ptr<SkStream> bufferedStream(SkFrontBufferedStream::Create(memStream, bufferSize));
87 test_hasLength(reporter, *bufferedStream, *memStream);
scroggo@google.com83fd2c72013-09-26 21:35:39 +000088
89 // Read exactly the amount that fits in the buffer.
Ben Wagner145dbcd2016-11-03 14:40:50 -040090 test_read(reporter, bufferedStream.get(), gAbcs, bufferSize);
scroggo@google.com83fd2c72013-09-26 21:35:39 +000091
92 // Rewinding should succeed.
Ben Wagner145dbcd2016-11-03 14:40:50 -040093 test_rewind(reporter, bufferedStream.get(), true);
scroggo@google.com83fd2c72013-09-26 21:35:39 +000094
95 // Once again reading buffered info should succeed
Ben Wagner145dbcd2016-11-03 14:40:50 -040096 test_read(reporter, bufferedStream.get(), gAbcs, bufferSize);
scroggo@google.com83fd2c72013-09-26 21:35:39 +000097
98 // Read past the size of the buffer. At this point, we cannot return.
Ben Wagner145dbcd2016-11-03 14:40:50 -040099 test_read(reporter, bufferedStream.get(), gAbcs + memStream->getPosition(), 1);
100 test_rewind(reporter, bufferedStream.get(), false);
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000101}
102
103static void test_skipping(skiatest::Reporter* reporter, size_t bufferSize) {
halcanary385fe4d2015-08-26 13:07:48 -0700104 SkMemoryStream* memStream = new SkMemoryStream(gAbcs, strlen(gAbcs), false);
Ben Wagner145dbcd2016-11-03 14:40:50 -0400105 std::unique_ptr<SkStream> bufferedStream(SkFrontBufferedStream::Create(memStream, bufferSize));
106 test_hasLength(reporter, *bufferedStream, *memStream);
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000107
108 // Skip half the buffer.
109 bufferedStream->skip(bufferSize / 2);
110
111 // Rewind, then read part of the buffer, which should have been read.
Ben Wagner145dbcd2016-11-03 14:40:50 -0400112 test_rewind(reporter, bufferedStream.get(), true);
113 test_read(reporter, bufferedStream.get(), gAbcs, bufferSize / 4);
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000114
115 // Now skip beyond the buffered piece, but still within the total buffer.
116 bufferedStream->skip(bufferSize / 2);
117
118 // Test that reading will still work.
Ben Wagner145dbcd2016-11-03 14:40:50 -0400119 test_read(reporter, bufferedStream.get(), gAbcs + memStream->getPosition(), bufferSize / 4);
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000120
Ben Wagner145dbcd2016-11-03 14:40:50 -0400121 test_rewind(reporter, bufferedStream.get(), true);
122 test_read(reporter, bufferedStream.get(), gAbcs, bufferSize);
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000123}
124
125// A custom class whose isAtEnd behaves the way Android's stream does - since it is an adaptor to a
126// Java InputStream, it does not know that it is at the end until it has attempted to read beyond
127// the end and failed. Used by test_read_beyond_buffer.
128class AndroidLikeMemoryStream : public SkMemoryStream {
129public:
130 AndroidLikeMemoryStream(void* data, size_t size, bool ownMemory)
131 : INHERITED(data, size, ownMemory)
132 , fIsAtEnd(false) {}
133
mtklein36352bf2015-03-25 18:17:31 -0700134 size_t read(void* dst, size_t requested) override {
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000135 size_t bytesRead = this->INHERITED::read(dst, requested);
136 if (bytesRead < requested) {
137 fIsAtEnd = true;
138 }
139 return bytesRead;
140 }
141
mtklein36352bf2015-03-25 18:17:31 -0700142 bool isAtEnd() const override {
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000143 return fIsAtEnd;
144 }
145
146private:
147 bool fIsAtEnd;
148 typedef SkMemoryStream INHERITED;
149};
150
151// This test ensures that buffering the exact length of the stream and attempting to read beyond it
152// does not invalidate the buffer.
153static void test_read_beyond_buffer(skiatest::Reporter* reporter, size_t bufferSize) {
154 // Use a stream that behaves like Android's stream.
halcanary385fe4d2015-08-26 13:07:48 -0700155 AndroidLikeMemoryStream* memStream =
156 new AndroidLikeMemoryStream((void*)gAbcs, bufferSize, false);
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000157
158 // Create a buffer that matches the length of the stream.
Ben Wagner145dbcd2016-11-03 14:40:50 -0400159 std::unique_ptr<SkStream> bufferedStream(SkFrontBufferedStream::Create(memStream, bufferSize));
scroggoa1193e42015-01-21 12:09:53 -0800160 test_hasLength(reporter, *bufferedStream.get(), *memStream);
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000161
162 // Attempt to read one more than the bufferSize
163 test_read(reporter, bufferedStream.get(), gAbcs, bufferSize + 1);
164 test_rewind(reporter, bufferedStream.get(), true);
165
166 // Ensure that the initial read did not invalidate the buffer.
Ben Wagner145dbcd2016-11-03 14:40:50 -0400167 test_read(reporter, bufferedStream.get(), gAbcs, bufferSize);
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000168}
169
commit-bot@chromium.org74b88b72014-02-10 22:03:21 +0000170// Dummy stream that optionally has a length and/or position. Tests that FrontBufferedStream's
171// length depends on the stream it's buffering having a length and position.
172class LengthOptionalStream : public SkStream {
173public:
174 LengthOptionalStream(bool hasLength, bool hasPosition)
175 : fHasLength(hasLength)
176 , fHasPosition(hasPosition)
177 {}
178
mtklein36352bf2015-03-25 18:17:31 -0700179 bool hasLength() const override {
commit-bot@chromium.org74b88b72014-02-10 22:03:21 +0000180 return fHasLength;
181 }
182
mtklein36352bf2015-03-25 18:17:31 -0700183 bool hasPosition() const override {
commit-bot@chromium.org74b88b72014-02-10 22:03:21 +0000184 return fHasPosition;
185 }
186
mtklein36352bf2015-03-25 18:17:31 -0700187 size_t read(void*, size_t) override {
commit-bot@chromium.org74b88b72014-02-10 22:03:21 +0000188 return 0;
189 }
190
mtklein36352bf2015-03-25 18:17:31 -0700191 bool isAtEnd() const override {
commit-bot@chromium.org74b88b72014-02-10 22:03:21 +0000192 return true;
193 }
194
195private:
196 const bool fHasLength;
197 const bool fHasPosition;
198};
199
200// Test all possible combinations of the wrapped stream having a length and a position.
201static void test_length_combos(skiatest::Reporter* reporter, size_t bufferSize) {
202 for (int hasLen = 0; hasLen <= 1; hasLen++) {
203 for (int hasPos = 0; hasPos <= 1; hasPos++) {
halcanary385fe4d2015-08-26 13:07:48 -0700204 LengthOptionalStream* stream =
205 new LengthOptionalStream(SkToBool(hasLen), SkToBool(hasPos));
Ben Wagner145dbcd2016-11-03 14:40:50 -0400206 std::unique_ptr<SkStream> buffered(SkFrontBufferedStream::Create(stream, bufferSize));
scroggoa1193e42015-01-21 12:09:53 -0800207 test_hasLength(reporter, *buffered.get(), *stream);
commit-bot@chromium.org74b88b72014-02-10 22:03:21 +0000208 }
209 }
210}
211
212// Test using a stream with an initial offset.
213static void test_initial_offset(skiatest::Reporter* reporter, size_t bufferSize) {
halcanary385fe4d2015-08-26 13:07:48 -0700214 SkMemoryStream* memStream = new SkMemoryStream(gAbcs, strlen(gAbcs), false);
commit-bot@chromium.org74b88b72014-02-10 22:03:21 +0000215
216 // Skip a few characters into the memStream, so that bufferedStream represents an offset into
217 // the stream it wraps.
218 const size_t arbitraryOffset = 17;
scroggoa1193e42015-01-21 12:09:53 -0800219 memStream->skip(arbitraryOffset);
Ben Wagner145dbcd2016-11-03 14:40:50 -0400220 std::unique_ptr<SkStream> bufferedStream(SkFrontBufferedStream::Create(memStream, bufferSize));
commit-bot@chromium.org74b88b72014-02-10 22:03:21 +0000221
scroggoef0fed32016-02-18 05:59:25 -0800222 // Since SkMemoryStream has a length, bufferedStream must also.
commit-bot@chromium.org74b88b72014-02-10 22:03:21 +0000223 REPORTER_ASSERT(reporter, bufferedStream->hasLength());
224
225 const size_t amountToRead = 10;
226 const size_t bufferedLength = bufferedStream->getLength();
scroggoef0fed32016-02-18 05:59:25 -0800227 size_t currentPosition = 0;
commit-bot@chromium.org74b88b72014-02-10 22:03:21 +0000228
229 // Read the stream in chunks. After each read, the position must match currentPosition,
230 // which sums the amount attempted to read, unless the end of the stream has been reached.
231 // Importantly, the end should not have been reached until currentPosition == bufferedLength.
232 while (currentPosition < bufferedLength) {
233 REPORTER_ASSERT(reporter, !bufferedStream->isAtEnd());
Ben Wagner145dbcd2016-11-03 14:40:50 -0400234 test_read(reporter, bufferedStream.get(), gAbcs + arbitraryOffset + currentPosition,
commit-bot@chromium.org74b88b72014-02-10 22:03:21 +0000235 amountToRead);
236 currentPosition = SkTMin(currentPosition + amountToRead, bufferedLength);
scroggoef0fed32016-02-18 05:59:25 -0800237 REPORTER_ASSERT(reporter, memStream->getPosition() - arbitraryOffset == currentPosition);
commit-bot@chromium.org74b88b72014-02-10 22:03:21 +0000238 }
239 REPORTER_ASSERT(reporter, bufferedStream->isAtEnd());
240 REPORTER_ASSERT(reporter, bufferedLength == currentPosition);
241}
242
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000243static void test_buffers(skiatest::Reporter* reporter, size_t bufferSize) {
244 test_incremental_buffering(reporter, bufferSize);
245 test_perfectly_sized_buffer(reporter, bufferSize);
246 test_skipping(reporter, bufferSize);
247 test_read_beyond_buffer(reporter, bufferSize);
commit-bot@chromium.org74b88b72014-02-10 22:03:21 +0000248 test_length_combos(reporter, bufferSize);
249 test_initial_offset(reporter, bufferSize);
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000250}
251
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000252DEF_TEST(FrontBufferedStream, reporter) {
scroggo@google.com83fd2c72013-09-26 21:35:39 +0000253 // Test 6 and 64, which are used by Android, as well as another arbitrary length.
254 test_buffers(reporter, 6);
255 test_buffers(reporter, 15);
256 test_buffers(reporter, 64);
257}
scroggo3ceef9a2014-10-24 06:55:07 -0700258
259// Test that a FrontBufferedStream does not allow reading after the end of a stream.
260// This class is a dummy SkStream which reports that it is at the end on the first
261// read (simulating a failure). Then it tracks whether someone calls read() again.
262class FailingStream : public SkStream {
263public:
264 FailingStream()
265 : fAtEnd(false)
scroggo3ceef9a2014-10-24 06:55:07 -0700266 {}
msarettb9e56c12016-03-01 13:29:15 -0800267
mtklein36352bf2015-03-25 18:17:31 -0700268 size_t read(void* buffer, size_t size) override {
msarettb9e56c12016-03-01 13:29:15 -0800269 SkASSERT(!fAtEnd);
270 fAtEnd = true;
scroggo3ceef9a2014-10-24 06:55:07 -0700271 return 0;
272 }
273
mtklein36352bf2015-03-25 18:17:31 -0700274 bool isAtEnd() const override {
scroggo3ceef9a2014-10-24 06:55:07 -0700275 return fAtEnd;
276 }
277
scroggo3ceef9a2014-10-24 06:55:07 -0700278private:
279 bool fAtEnd;
scroggo3ceef9a2014-10-24 06:55:07 -0700280};
281
282DEF_TEST(ShortFrontBufferedStream, reporter) {
halcanary385fe4d2015-08-26 13:07:48 -0700283 FailingStream* failingStream = new FailingStream;
Ben Wagner145dbcd2016-11-03 14:40:50 -0400284 std::unique_ptr<SkStreamRewindable> stream(SkFrontBufferedStream::Create(failingStream, 64));
msarett7f7ec202016-03-01 12:12:27 -0800285
286 // This will fail to create a codec. However, what we really want to test is that we
287 // won't read past the end of the stream.
Ben Wagner145dbcd2016-11-03 14:40:50 -0400288 std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
scroggo3ceef9a2014-10-24 06:55:07 -0700289}