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) { |
| 54 | SkMemoryStream memStream(gAbcs, strlen(gAbcs), false); |
| 55 | |
| 56 | SkAutoTUnref<SkStream> bufferedStream(SkFrontBufferedStream::Create(&memStream, bufferSize)); |
commit-bot@chromium.org | 74b88b7 | 2014-02-10 22:03:21 +0000 | [diff] [blame] | 57 | test_hasLength(reporter, *bufferedStream.get(), memStream); |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 58 | |
| 59 | // First, test reading less than the max buffer size. |
| 60 | test_read(reporter, bufferedStream, gAbcs, bufferSize / 2); |
| 61 | |
| 62 | // Now test rewinding back to the beginning and reading less than what was |
| 63 | // already buffered. |
| 64 | test_rewind(reporter, bufferedStream, true); |
| 65 | test_read(reporter, bufferedStream, gAbcs, bufferSize / 4); |
| 66 | |
| 67 | // Now test reading part of what was buffered, and buffering new data. |
| 68 | test_read(reporter, bufferedStream, gAbcs + bufferedStream->getPosition(), bufferSize / 2); |
| 69 | |
| 70 | // Now test reading what was buffered, buffering new data, and |
| 71 | // reading directly from the stream. |
| 72 | test_rewind(reporter, bufferedStream, true); |
| 73 | test_read(reporter, bufferedStream, gAbcs, bufferSize << 1); |
| 74 | |
| 75 | // We have reached the end of the buffer, so rewinding will fail. |
| 76 | // This test assumes that the stream is larger than the buffer; otherwise the |
| 77 | // result of rewind should be true. |
| 78 | test_rewind(reporter, bufferedStream, false); |
| 79 | } |
| 80 | |
| 81 | static void test_perfectly_sized_buffer(skiatest::Reporter* reporter, size_t bufferSize) { |
| 82 | SkMemoryStream memStream(gAbcs, strlen(gAbcs), false); |
| 83 | SkAutoTUnref<SkStream> bufferedStream(SkFrontBufferedStream::Create(&memStream, bufferSize)); |
commit-bot@chromium.org | 74b88b7 | 2014-02-10 22:03:21 +0000 | [diff] [blame] | 84 | test_hasLength(reporter, *bufferedStream.get(), memStream); |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 85 | |
| 86 | // Read exactly the amount that fits in the buffer. |
| 87 | test_read(reporter, bufferedStream, gAbcs, bufferSize); |
| 88 | |
| 89 | // Rewinding should succeed. |
| 90 | test_rewind(reporter, bufferedStream, true); |
| 91 | |
| 92 | // Once again reading buffered info should succeed |
| 93 | test_read(reporter, bufferedStream, gAbcs, bufferSize); |
| 94 | |
| 95 | // Read past the size of the buffer. At this point, we cannot return. |
| 96 | test_read(reporter, bufferedStream, gAbcs + bufferedStream->getPosition(), 1); |
| 97 | test_rewind(reporter, bufferedStream, false); |
| 98 | } |
| 99 | |
| 100 | static void test_skipping(skiatest::Reporter* reporter, size_t bufferSize) { |
| 101 | SkMemoryStream memStream(gAbcs, strlen(gAbcs), false); |
| 102 | SkAutoTUnref<SkStream> bufferedStream(SkFrontBufferedStream::Create(&memStream, bufferSize)); |
commit-bot@chromium.org | 74b88b7 | 2014-02-10 22:03:21 +0000 | [diff] [blame] | 103 | test_hasLength(reporter, *bufferedStream.get(), memStream); |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 104 | |
| 105 | // Skip half the buffer. |
| 106 | bufferedStream->skip(bufferSize / 2); |
| 107 | |
| 108 | // Rewind, then read part of the buffer, which should have been read. |
| 109 | test_rewind(reporter, bufferedStream, true); |
| 110 | test_read(reporter, bufferedStream, gAbcs, bufferSize / 4); |
| 111 | |
| 112 | // Now skip beyond the buffered piece, but still within the total buffer. |
| 113 | bufferedStream->skip(bufferSize / 2); |
| 114 | |
| 115 | // Test that reading will still work. |
| 116 | test_read(reporter, bufferedStream, gAbcs + bufferedStream->getPosition(), bufferSize / 4); |
| 117 | |
| 118 | test_rewind(reporter, bufferedStream, true); |
| 119 | test_read(reporter, bufferedStream, gAbcs, bufferSize); |
| 120 | } |
| 121 | |
| 122 | // A custom class whose isAtEnd behaves the way Android's stream does - since it is an adaptor to a |
| 123 | // Java InputStream, it does not know that it is at the end until it has attempted to read beyond |
| 124 | // the end and failed. Used by test_read_beyond_buffer. |
| 125 | class AndroidLikeMemoryStream : public SkMemoryStream { |
| 126 | public: |
| 127 | AndroidLikeMemoryStream(void* data, size_t size, bool ownMemory) |
| 128 | : INHERITED(data, size, ownMemory) |
| 129 | , fIsAtEnd(false) {} |
| 130 | |
| 131 | size_t read(void* dst, size_t requested) SK_OVERRIDE { |
| 132 | size_t bytesRead = this->INHERITED::read(dst, requested); |
| 133 | if (bytesRead < requested) { |
| 134 | fIsAtEnd = true; |
| 135 | } |
| 136 | return bytesRead; |
| 137 | } |
| 138 | |
| 139 | bool isAtEnd() const SK_OVERRIDE { |
| 140 | return fIsAtEnd; |
| 141 | } |
| 142 | |
| 143 | private: |
| 144 | bool fIsAtEnd; |
| 145 | typedef SkMemoryStream INHERITED; |
| 146 | }; |
| 147 | |
| 148 | // This test ensures that buffering the exact length of the stream and attempting to read beyond it |
| 149 | // does not invalidate the buffer. |
| 150 | static void test_read_beyond_buffer(skiatest::Reporter* reporter, size_t bufferSize) { |
| 151 | // Use a stream that behaves like Android's stream. |
| 152 | AndroidLikeMemoryStream memStream((void*)gAbcs, bufferSize, false); |
| 153 | |
| 154 | // Create a buffer that matches the length of the stream. |
| 155 | SkAutoTUnref<SkStream> bufferedStream(SkFrontBufferedStream::Create(&memStream, bufferSize)); |
commit-bot@chromium.org | 74b88b7 | 2014-02-10 22:03:21 +0000 | [diff] [blame] | 156 | test_hasLength(reporter, *bufferedStream.get(), memStream); |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 157 | |
| 158 | // Attempt to read one more than the bufferSize |
| 159 | test_read(reporter, bufferedStream.get(), gAbcs, bufferSize + 1); |
| 160 | test_rewind(reporter, bufferedStream.get(), true); |
| 161 | |
| 162 | // Ensure that the initial read did not invalidate the buffer. |
| 163 | test_read(reporter, bufferedStream, gAbcs, bufferSize); |
| 164 | } |
| 165 | |
commit-bot@chromium.org | 74b88b7 | 2014-02-10 22:03:21 +0000 | [diff] [blame] | 166 | // Dummy stream that optionally has a length and/or position. Tests that FrontBufferedStream's |
| 167 | // length depends on the stream it's buffering having a length and position. |
| 168 | class LengthOptionalStream : public SkStream { |
| 169 | public: |
| 170 | LengthOptionalStream(bool hasLength, bool hasPosition) |
| 171 | : fHasLength(hasLength) |
| 172 | , fHasPosition(hasPosition) |
| 173 | {} |
| 174 | |
| 175 | virtual bool hasLength() const SK_OVERRIDE { |
| 176 | return fHasLength; |
| 177 | } |
| 178 | |
| 179 | virtual bool hasPosition() const SK_OVERRIDE { |
| 180 | return fHasPosition; |
| 181 | } |
| 182 | |
| 183 | virtual size_t read(void*, size_t) SK_OVERRIDE { |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | virtual bool isAtEnd() const SK_OVERRIDE { |
| 188 | return true; |
| 189 | } |
| 190 | |
| 191 | private: |
| 192 | const bool fHasLength; |
| 193 | const bool fHasPosition; |
| 194 | }; |
| 195 | |
| 196 | // Test all possible combinations of the wrapped stream having a length and a position. |
| 197 | static void test_length_combos(skiatest::Reporter* reporter, size_t bufferSize) { |
| 198 | for (int hasLen = 0; hasLen <= 1; hasLen++) { |
| 199 | for (int hasPos = 0; hasPos <= 1; hasPos++) { |
commit-bot@chromium.org | 6d254ee | 2014-02-10 22:46:08 +0000 | [diff] [blame] | 200 | LengthOptionalStream stream(SkToBool(hasLen), SkToBool(hasPos)); |
commit-bot@chromium.org | 74b88b7 | 2014-02-10 22:03:21 +0000 | [diff] [blame] | 201 | SkAutoTUnref<SkStream> buffered(SkFrontBufferedStream::Create(&stream, bufferSize)); |
| 202 | test_hasLength(reporter, *buffered.get(), stream); |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | // Test using a stream with an initial offset. |
| 208 | static void test_initial_offset(skiatest::Reporter* reporter, size_t bufferSize) { |
| 209 | SkMemoryStream memStream(gAbcs, strlen(gAbcs), false); |
| 210 | |
| 211 | // Skip a few characters into the memStream, so that bufferedStream represents an offset into |
| 212 | // the stream it wraps. |
| 213 | const size_t arbitraryOffset = 17; |
| 214 | memStream.skip(arbitraryOffset); |
| 215 | SkAutoTUnref<SkStream> bufferedStream(SkFrontBufferedStream::Create(&memStream, bufferSize)); |
| 216 | |
| 217 | // Since SkMemoryStream has a length and a position, bufferedStream must also. |
| 218 | REPORTER_ASSERT(reporter, bufferedStream->hasLength()); |
| 219 | |
| 220 | const size_t amountToRead = 10; |
| 221 | const size_t bufferedLength = bufferedStream->getLength(); |
| 222 | size_t currentPosition = bufferedStream->getPosition(); |
| 223 | REPORTER_ASSERT(reporter, 0 == currentPosition); |
| 224 | |
| 225 | // Read the stream in chunks. After each read, the position must match currentPosition, |
| 226 | // which sums the amount attempted to read, unless the end of the stream has been reached. |
| 227 | // Importantly, the end should not have been reached until currentPosition == bufferedLength. |
| 228 | while (currentPosition < bufferedLength) { |
| 229 | REPORTER_ASSERT(reporter, !bufferedStream->isAtEnd()); |
| 230 | test_read(reporter, bufferedStream, gAbcs + arbitraryOffset + currentPosition, |
| 231 | amountToRead); |
| 232 | currentPosition = SkTMin(currentPosition + amountToRead, bufferedLength); |
| 233 | REPORTER_ASSERT(reporter, bufferedStream->getPosition() == currentPosition); |
| 234 | } |
| 235 | REPORTER_ASSERT(reporter, bufferedStream->isAtEnd()); |
| 236 | REPORTER_ASSERT(reporter, bufferedLength == currentPosition); |
| 237 | } |
| 238 | |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 239 | static void test_buffers(skiatest::Reporter* reporter, size_t bufferSize) { |
| 240 | test_incremental_buffering(reporter, bufferSize); |
| 241 | test_perfectly_sized_buffer(reporter, bufferSize); |
| 242 | test_skipping(reporter, bufferSize); |
| 243 | test_read_beyond_buffer(reporter, bufferSize); |
commit-bot@chromium.org | 74b88b7 | 2014-02-10 22:03:21 +0000 | [diff] [blame] | 244 | test_length_combos(reporter, bufferSize); |
| 245 | test_initial_offset(reporter, bufferSize); |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 246 | } |
| 247 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 248 | DEF_TEST(FrontBufferedStream, reporter) { |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 249 | // Test 6 and 64, which are used by Android, as well as another arbitrary length. |
| 250 | test_buffers(reporter, 6); |
| 251 | test_buffers(reporter, 15); |
| 252 | test_buffers(reporter, 64); |
| 253 | } |
scroggo | 3ceef9a | 2014-10-24 06:55:07 -0700 | [diff] [blame] | 254 | |
| 255 | // Test that a FrontBufferedStream does not allow reading after the end of a stream. |
| 256 | // This class is a dummy SkStream which reports that it is at the end on the first |
| 257 | // read (simulating a failure). Then it tracks whether someone calls read() again. |
| 258 | class FailingStream : public SkStream { |
| 259 | public: |
| 260 | FailingStream() |
| 261 | : fAtEnd(false) |
| 262 | , fReadAfterEnd(false) |
| 263 | {} |
| 264 | virtual size_t read(void* buffer, size_t size) SK_OVERRIDE { |
| 265 | if (fAtEnd) { |
| 266 | fReadAfterEnd = true; |
| 267 | } else { |
| 268 | fAtEnd = true; |
| 269 | } |
| 270 | return 0; |
| 271 | } |
| 272 | |
| 273 | virtual bool isAtEnd() const SK_OVERRIDE { |
| 274 | return fAtEnd; |
| 275 | } |
| 276 | |
| 277 | bool readAfterEnd() const { |
| 278 | return fReadAfterEnd; |
| 279 | } |
| 280 | private: |
| 281 | bool fAtEnd; |
| 282 | bool fReadAfterEnd; |
| 283 | }; |
| 284 | |
| 285 | DEF_TEST(ShortFrontBufferedStream, reporter) { |
| 286 | FailingStream failingStream; |
| 287 | SkAutoTUnref<SkStreamRewindable> stream(SkFrontBufferedStream::Create(&failingStream, 64)); |
| 288 | SkBitmap bm; |
| 289 | // The return value of DecodeStream is not important. We are just using DecodeStream because |
| 290 | // it simulates a bug. DecodeStream will read the stream, then rewind, then attempt to read |
| 291 | // again. FrontBufferedStream::read should not continue to read its underlying stream beyond |
| 292 | // its end. |
| 293 | SkImageDecoder::DecodeStream(stream, &bm); |
| 294 | REPORTER_ASSERT(reporter, !failingStream.readAfterEnd()); |
| 295 | } |