scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
scroggo | 3ceef9a | 2014-10-24 06:55:07 -0700 | [diff] [blame] | 8 | #include "SkBitmap.h" |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 9 | #include "SkFrontBufferedStream.h" |
scroggo | 3ceef9a | 2014-10-24 06:55:07 -0700 | [diff] [blame] | 10 | #include "SkImageDecoder.h" |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 11 | #include "SkRefCnt.h" |
scroggo@google.com | 09a5383 | 2013-11-12 20:53:05 +0000 | [diff] [blame] | 12 | #include "SkStream.h" |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 13 | #include "SkTypes.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 14 | #include "Test.h" |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 15 | |
| 16 | static 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.org | 74b88b7 | 2014-02-10 22:03:21 +0000 | [diff] [blame] | 21 | const size_t bytesRead = bufferedStream->read(storage.get(), bytesToRead); |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 22 | REPORTER_ASSERT(reporter, bytesRead == bytesToRead || bufferedStream->isAtEnd()); |
| 23 | REPORTER_ASSERT(reporter, memcmp(storage.get(), expectations, bytesRead) == 0); |
| 24 | } |
| 25 | |
| 26 | static 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.org | 74b88b7 | 2014-02-10 22:03:21 +0000 | [diff] [blame] | 32 | // 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). |
| 36 | static 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.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 46 | // 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. |
| 49 | const 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. |
| 53 | static void test_incremental_buffering(skiatest::Reporter* reporter, size_t bufferSize) { |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 54 | // 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). |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 57 | SkMemoryStream* memStream = new SkMemoryStream(gAbcs, strlen(gAbcs), false); |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 58 | |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 59 | SkAutoTDelete<SkStream> bufferedStream(SkFrontBufferedStream::Create(memStream, bufferSize)); |
| 60 | test_hasLength(reporter, *bufferedStream.get(), *memStream); |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 61 | |
| 62 | // First, test reading less than the max buffer size. |
| 63 | test_read(reporter, bufferedStream, gAbcs, bufferSize / 2); |
| 64 | |
| 65 | // Now test rewinding back to the beginning and reading less than what was |
| 66 | // already buffered. |
| 67 | test_rewind(reporter, bufferedStream, true); |
| 68 | test_read(reporter, bufferedStream, gAbcs, bufferSize / 4); |
| 69 | |
| 70 | // Now test reading part of what was buffered, and buffering new data. |
| 71 | test_read(reporter, bufferedStream, gAbcs + bufferedStream->getPosition(), bufferSize / 2); |
| 72 | |
| 73 | // Now test reading what was buffered, buffering new data, and |
| 74 | // reading directly from the stream. |
| 75 | test_rewind(reporter, bufferedStream, true); |
| 76 | test_read(reporter, bufferedStream, gAbcs, bufferSize << 1); |
| 77 | |
| 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. |
| 81 | test_rewind(reporter, bufferedStream, false); |
| 82 | } |
| 83 | |
| 84 | static void test_perfectly_sized_buffer(skiatest::Reporter* reporter, size_t bufferSize) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 85 | SkMemoryStream* memStream = new SkMemoryStream(gAbcs, strlen(gAbcs), false); |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 86 | SkAutoTDelete<SkStream> bufferedStream(SkFrontBufferedStream::Create(memStream, bufferSize)); |
| 87 | test_hasLength(reporter, *bufferedStream.get(), *memStream); |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 88 | |
| 89 | // Read exactly the amount that fits in the buffer. |
| 90 | test_read(reporter, bufferedStream, gAbcs, bufferSize); |
| 91 | |
| 92 | // Rewinding should succeed. |
| 93 | test_rewind(reporter, bufferedStream, true); |
| 94 | |
| 95 | // Once again reading buffered info should succeed |
| 96 | test_read(reporter, bufferedStream, gAbcs, bufferSize); |
| 97 | |
| 98 | // Read past the size of the buffer. At this point, we cannot return. |
| 99 | test_read(reporter, bufferedStream, gAbcs + bufferedStream->getPosition(), 1); |
| 100 | test_rewind(reporter, bufferedStream, false); |
| 101 | } |
| 102 | |
| 103 | static void test_skipping(skiatest::Reporter* reporter, size_t bufferSize) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 104 | SkMemoryStream* memStream = new SkMemoryStream(gAbcs, strlen(gAbcs), false); |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 105 | SkAutoTDelete<SkStream> bufferedStream(SkFrontBufferedStream::Create(memStream, bufferSize)); |
| 106 | test_hasLength(reporter, *bufferedStream.get(), *memStream); |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 107 | |
| 108 | // Skip half the buffer. |
| 109 | bufferedStream->skip(bufferSize / 2); |
| 110 | |
| 111 | // Rewind, then read part of the buffer, which should have been read. |
| 112 | test_rewind(reporter, bufferedStream, true); |
| 113 | test_read(reporter, bufferedStream, gAbcs, bufferSize / 4); |
| 114 | |
| 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. |
| 119 | test_read(reporter, bufferedStream, gAbcs + bufferedStream->getPosition(), bufferSize / 4); |
| 120 | |
| 121 | test_rewind(reporter, bufferedStream, true); |
| 122 | test_read(reporter, bufferedStream, gAbcs, bufferSize); |
| 123 | } |
| 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. |
| 128 | class AndroidLikeMemoryStream : public SkMemoryStream { |
| 129 | public: |
| 130 | AndroidLikeMemoryStream(void* data, size_t size, bool ownMemory) |
| 131 | : INHERITED(data, size, ownMemory) |
| 132 | , fIsAtEnd(false) {} |
| 133 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 134 | size_t read(void* dst, size_t requested) override { |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 135 | size_t bytesRead = this->INHERITED::read(dst, requested); |
| 136 | if (bytesRead < requested) { |
| 137 | fIsAtEnd = true; |
| 138 | } |
| 139 | return bytesRead; |
| 140 | } |
| 141 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 142 | bool isAtEnd() const override { |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 143 | return fIsAtEnd; |
| 144 | } |
| 145 | |
| 146 | private: |
| 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. |
| 153 | static void test_read_beyond_buffer(skiatest::Reporter* reporter, size_t bufferSize) { |
| 154 | // Use a stream that behaves like Android's stream. |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 155 | AndroidLikeMemoryStream* memStream = |
| 156 | new AndroidLikeMemoryStream((void*)gAbcs, bufferSize, false); |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 157 | |
| 158 | // Create a buffer that matches the length of the stream. |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 159 | SkAutoTDelete<SkStream> bufferedStream(SkFrontBufferedStream::Create(memStream, bufferSize)); |
| 160 | test_hasLength(reporter, *bufferedStream.get(), *memStream); |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 161 | |
| 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. |
| 167 | test_read(reporter, bufferedStream, gAbcs, bufferSize); |
| 168 | } |
| 169 | |
commit-bot@chromium.org | 74b88b7 | 2014-02-10 22:03:21 +0000 | [diff] [blame] | 170 | // 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. |
| 172 | class LengthOptionalStream : public SkStream { |
| 173 | public: |
| 174 | LengthOptionalStream(bool hasLength, bool hasPosition) |
| 175 | : fHasLength(hasLength) |
| 176 | , fHasPosition(hasPosition) |
| 177 | {} |
| 178 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 179 | bool hasLength() const override { |
commit-bot@chromium.org | 74b88b7 | 2014-02-10 22:03:21 +0000 | [diff] [blame] | 180 | return fHasLength; |
| 181 | } |
| 182 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 183 | bool hasPosition() const override { |
commit-bot@chromium.org | 74b88b7 | 2014-02-10 22:03:21 +0000 | [diff] [blame] | 184 | return fHasPosition; |
| 185 | } |
| 186 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 187 | size_t read(void*, size_t) override { |
commit-bot@chromium.org | 74b88b7 | 2014-02-10 22:03:21 +0000 | [diff] [blame] | 188 | return 0; |
| 189 | } |
| 190 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 191 | bool isAtEnd() const override { |
commit-bot@chromium.org | 74b88b7 | 2014-02-10 22:03:21 +0000 | [diff] [blame] | 192 | return true; |
| 193 | } |
| 194 | |
| 195 | private: |
| 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. |
| 201 | static 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++) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 204 | LengthOptionalStream* stream = |
| 205 | new LengthOptionalStream(SkToBool(hasLen), SkToBool(hasPos)); |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 206 | SkAutoTDelete<SkStream> buffered(SkFrontBufferedStream::Create(stream, bufferSize)); |
| 207 | test_hasLength(reporter, *buffered.get(), *stream); |
commit-bot@chromium.org | 74b88b7 | 2014-02-10 22:03:21 +0000 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | // Test using a stream with an initial offset. |
| 213 | static void test_initial_offset(skiatest::Reporter* reporter, size_t bufferSize) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 214 | SkMemoryStream* memStream = new SkMemoryStream(gAbcs, strlen(gAbcs), false); |
commit-bot@chromium.org | 74b88b7 | 2014-02-10 22:03:21 +0000 | [diff] [blame] | 215 | |
| 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; |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 219 | memStream->skip(arbitraryOffset); |
| 220 | SkAutoTDelete<SkStream> bufferedStream(SkFrontBufferedStream::Create(memStream, bufferSize)); |
commit-bot@chromium.org | 74b88b7 | 2014-02-10 22:03:21 +0000 | [diff] [blame] | 221 | |
| 222 | // Since SkMemoryStream has a length and a position, bufferedStream must also. |
| 223 | REPORTER_ASSERT(reporter, bufferedStream->hasLength()); |
| 224 | |
| 225 | const size_t amountToRead = 10; |
| 226 | const size_t bufferedLength = bufferedStream->getLength(); |
| 227 | size_t currentPosition = bufferedStream->getPosition(); |
| 228 | REPORTER_ASSERT(reporter, 0 == currentPosition); |
| 229 | |
| 230 | // Read the stream in chunks. After each read, the position must match currentPosition, |
| 231 | // which sums the amount attempted to read, unless the end of the stream has been reached. |
| 232 | // Importantly, the end should not have been reached until currentPosition == bufferedLength. |
| 233 | while (currentPosition < bufferedLength) { |
| 234 | REPORTER_ASSERT(reporter, !bufferedStream->isAtEnd()); |
| 235 | test_read(reporter, bufferedStream, gAbcs + arbitraryOffset + currentPosition, |
| 236 | amountToRead); |
| 237 | currentPosition = SkTMin(currentPosition + amountToRead, bufferedLength); |
| 238 | REPORTER_ASSERT(reporter, bufferedStream->getPosition() == currentPosition); |
| 239 | } |
| 240 | REPORTER_ASSERT(reporter, bufferedStream->isAtEnd()); |
| 241 | REPORTER_ASSERT(reporter, bufferedLength == currentPosition); |
| 242 | } |
| 243 | |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 244 | static void test_buffers(skiatest::Reporter* reporter, size_t bufferSize) { |
| 245 | test_incremental_buffering(reporter, bufferSize); |
| 246 | test_perfectly_sized_buffer(reporter, bufferSize); |
| 247 | test_skipping(reporter, bufferSize); |
| 248 | test_read_beyond_buffer(reporter, bufferSize); |
commit-bot@chromium.org | 74b88b7 | 2014-02-10 22:03:21 +0000 | [diff] [blame] | 249 | test_length_combos(reporter, bufferSize); |
| 250 | test_initial_offset(reporter, bufferSize); |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 251 | } |
| 252 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 253 | DEF_TEST(FrontBufferedStream, reporter) { |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 254 | // Test 6 and 64, which are used by Android, as well as another arbitrary length. |
| 255 | test_buffers(reporter, 6); |
| 256 | test_buffers(reporter, 15); |
| 257 | test_buffers(reporter, 64); |
| 258 | } |
scroggo | 3ceef9a | 2014-10-24 06:55:07 -0700 | [diff] [blame] | 259 | |
| 260 | // Test that a FrontBufferedStream does not allow reading after the end of a stream. |
| 261 | // This class is a dummy SkStream which reports that it is at the end on the first |
| 262 | // read (simulating a failure). Then it tracks whether someone calls read() again. |
| 263 | class FailingStream : public SkStream { |
| 264 | public: |
| 265 | FailingStream() |
| 266 | : fAtEnd(false) |
| 267 | , fReadAfterEnd(false) |
| 268 | {} |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 269 | size_t read(void* buffer, size_t size) override { |
scroggo | 3ceef9a | 2014-10-24 06:55:07 -0700 | [diff] [blame] | 270 | if (fAtEnd) { |
| 271 | fReadAfterEnd = true; |
| 272 | } else { |
| 273 | fAtEnd = true; |
| 274 | } |
| 275 | return 0; |
| 276 | } |
| 277 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 278 | bool isAtEnd() const override { |
scroggo | 3ceef9a | 2014-10-24 06:55:07 -0700 | [diff] [blame] | 279 | return fAtEnd; |
| 280 | } |
| 281 | |
| 282 | bool readAfterEnd() const { |
| 283 | return fReadAfterEnd; |
| 284 | } |
| 285 | private: |
| 286 | bool fAtEnd; |
| 287 | bool fReadAfterEnd; |
| 288 | }; |
| 289 | |
| 290 | DEF_TEST(ShortFrontBufferedStream, reporter) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 291 | FailingStream* failingStream = new FailingStream; |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 292 | SkAutoTDelete<SkStreamRewindable> stream(SkFrontBufferedStream::Create(failingStream, 64)); |
scroggo | 3ceef9a | 2014-10-24 06:55:07 -0700 | [diff] [blame] | 293 | SkBitmap bm; |
| 294 | // The return value of DecodeStream is not important. We are just using DecodeStream because |
| 295 | // it simulates a bug. DecodeStream will read the stream, then rewind, then attempt to read |
| 296 | // again. FrontBufferedStream::read should not continue to read its underlying stream beyond |
| 297 | // its end. |
| 298 | SkImageDecoder::DecodeStream(stream, &bm); |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 299 | REPORTER_ASSERT(reporter, !failingStream->readAfterEnd()); |
scroggo | 3ceef9a | 2014-10-24 06:55:07 -0700 | [diff] [blame] | 300 | } |