FrontBufferedStreamTest.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. #include "include/codec/SkCodec.h"
  8. #include "include/core/SkBitmap.h"
  9. #include "include/core/SkRefCnt.h"
  10. #include "include/core/SkStream.h"
  11. #include "include/utils/SkFrontBufferedStream.h"
  12. #include "src/core/SkAutoMalloc.h"
  13. #include "tests/Test.h"
  14. static void test_read(skiatest::Reporter* reporter, SkStream* bufferedStream,
  15. const void* expectations, size_t bytesToRead) {
  16. // output for reading bufferedStream.
  17. SkAutoMalloc storage(bytesToRead);
  18. const size_t bytesRead = bufferedStream->read(storage.get(), bytesToRead);
  19. REPORTER_ASSERT(reporter, bytesRead == bytesToRead || bufferedStream->isAtEnd());
  20. REPORTER_ASSERT(reporter, memcmp(storage.get(), expectations, bytesRead) == 0);
  21. }
  22. static void test_rewind(skiatest::Reporter* reporter,
  23. SkStream* bufferedStream, bool shouldSucceed) {
  24. const bool success = bufferedStream->rewind();
  25. REPORTER_ASSERT(reporter, success == shouldSucceed);
  26. }
  27. // Test that hasLength() returns the correct value, based on the stream
  28. // being wrapped. A length can only be known if the wrapped stream has a
  29. // length and it has a position (so its initial position can be taken into
  30. // account when computing the length).
  31. static void test_hasLength(skiatest::Reporter* reporter,
  32. const SkStream& bufferedStream,
  33. const SkStream& streamBeingBuffered) {
  34. if (streamBeingBuffered.hasLength() && streamBeingBuffered.hasPosition()) {
  35. REPORTER_ASSERT(reporter, bufferedStream.hasLength());
  36. } else {
  37. REPORTER_ASSERT(reporter, !bufferedStream.hasLength());
  38. }
  39. }
  40. // All tests will buffer this string, and compare output to the original.
  41. // The string is long to ensure that all of our lengths being tested are
  42. // smaller than the string length.
  43. const char gAbcs[] = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwx";
  44. // Tests reading the stream across boundaries of what has been buffered so far and what
  45. // the total buffer size is.
  46. static void test_incremental_buffering(skiatest::Reporter* reporter, size_t bufferSize) {
  47. // NOTE: For this and other tests in this file, we cheat and continue to refer to the
  48. // wrapped stream, but that's okay because we know the wrapping stream has not been
  49. // deleted yet (and we only call const methods in it).
  50. SkMemoryStream* memStream = SkMemoryStream::MakeDirect(gAbcs, strlen(gAbcs)).release();
  51. auto bufferedStream = SkFrontBufferedStream::Make(std::unique_ptr<SkStream>(memStream),
  52. bufferSize);
  53. test_hasLength(reporter, *bufferedStream, *memStream);
  54. // First, test reading less than the max buffer size.
  55. test_read(reporter, bufferedStream.get(), gAbcs, bufferSize / 2);
  56. // Now test rewinding back to the beginning and reading less than what was
  57. // already buffered.
  58. test_rewind(reporter, bufferedStream.get(), true);
  59. test_read(reporter, bufferedStream.get(), gAbcs, bufferSize / 4);
  60. // Now test reading part of what was buffered, and buffering new data.
  61. test_read(reporter, bufferedStream.get(), gAbcs + bufferSize / 4, bufferSize / 2);
  62. // Now test reading what was buffered, buffering new data, and
  63. // reading directly from the stream.
  64. test_rewind(reporter, bufferedStream.get(), true);
  65. test_read(reporter, bufferedStream.get(), gAbcs, bufferSize << 1);
  66. // We have reached the end of the buffer, so rewinding will fail.
  67. // This test assumes that the stream is larger than the buffer; otherwise the
  68. // result of rewind should be true.
  69. test_rewind(reporter, bufferedStream.get(), false);
  70. }
  71. static void test_perfectly_sized_buffer(skiatest::Reporter* reporter, size_t bufferSize) {
  72. SkMemoryStream* memStream = SkMemoryStream::MakeDirect(gAbcs, strlen(gAbcs)).release();
  73. auto bufferedStream = SkFrontBufferedStream::Make(std::unique_ptr<SkStream>(memStream),
  74. bufferSize);
  75. test_hasLength(reporter, *bufferedStream, *memStream);
  76. // Read exactly the amount that fits in the buffer.
  77. test_read(reporter, bufferedStream.get(), gAbcs, bufferSize);
  78. // Rewinding should succeed.
  79. test_rewind(reporter, bufferedStream.get(), true);
  80. // Once again reading buffered info should succeed
  81. test_read(reporter, bufferedStream.get(), gAbcs, bufferSize);
  82. // Read past the size of the buffer. At this point, we cannot return.
  83. test_read(reporter, bufferedStream.get(), gAbcs + memStream->getPosition(), 1);
  84. test_rewind(reporter, bufferedStream.get(), false);
  85. }
  86. static void test_skipping(skiatest::Reporter* reporter, size_t bufferSize) {
  87. SkMemoryStream* memStream = SkMemoryStream::MakeDirect(gAbcs, strlen(gAbcs)).release();
  88. auto bufferedStream = SkFrontBufferedStream::Make(std::unique_ptr<SkStream>(memStream),
  89. bufferSize);
  90. test_hasLength(reporter, *bufferedStream, *memStream);
  91. // Skip half the buffer.
  92. bufferedStream->skip(bufferSize / 2);
  93. // Rewind, then read part of the buffer, which should have been read.
  94. test_rewind(reporter, bufferedStream.get(), true);
  95. test_read(reporter, bufferedStream.get(), gAbcs, bufferSize / 4);
  96. // Now skip beyond the buffered piece, but still within the total buffer.
  97. bufferedStream->skip(bufferSize / 2);
  98. // Test that reading will still work.
  99. test_read(reporter, bufferedStream.get(), gAbcs + memStream->getPosition(), bufferSize / 4);
  100. test_rewind(reporter, bufferedStream.get(), true);
  101. test_read(reporter, bufferedStream.get(), gAbcs, bufferSize);
  102. }
  103. // A custom class whose isAtEnd behaves the way Android's stream does - since it is an adaptor to a
  104. // Java InputStream, it does not know that it is at the end until it has attempted to read beyond
  105. // the end and failed. Used by test_read_beyond_buffer.
  106. class AndroidLikeMemoryStream : public SkMemoryStream {
  107. public:
  108. AndroidLikeMemoryStream(void* data, size_t size, bool ownMemory)
  109. : INHERITED(data, size, ownMemory)
  110. , fIsAtEnd(false) {}
  111. size_t read(void* dst, size_t requested) override {
  112. size_t bytesRead = this->INHERITED::read(dst, requested);
  113. if (bytesRead < requested) {
  114. fIsAtEnd = true;
  115. }
  116. return bytesRead;
  117. }
  118. bool isAtEnd() const override {
  119. return fIsAtEnd;
  120. }
  121. private:
  122. bool fIsAtEnd;
  123. typedef SkMemoryStream INHERITED;
  124. };
  125. // This test ensures that buffering the exact length of the stream and attempting to read beyond it
  126. // does not invalidate the buffer.
  127. static void test_read_beyond_buffer(skiatest::Reporter* reporter, size_t bufferSize) {
  128. // Use a stream that behaves like Android's stream.
  129. AndroidLikeMemoryStream* memStream =
  130. new AndroidLikeMemoryStream((void*)gAbcs, bufferSize, false);
  131. // Create a buffer that matches the length of the stream.
  132. auto bufferedStream = SkFrontBufferedStream::Make(std::unique_ptr<SkStream>(memStream),
  133. bufferSize);
  134. test_hasLength(reporter, *bufferedStream.get(), *memStream);
  135. // Attempt to read one more than the bufferSize
  136. test_read(reporter, bufferedStream.get(), gAbcs, bufferSize + 1);
  137. test_rewind(reporter, bufferedStream.get(), true);
  138. // Ensure that the initial read did not invalidate the buffer.
  139. test_read(reporter, bufferedStream.get(), gAbcs, bufferSize);
  140. }
  141. // Dummy stream that optionally has a length and/or position. Tests that FrontBufferedStream's
  142. // length depends on the stream it's buffering having a length and position.
  143. class LengthOptionalStream : public SkStream {
  144. public:
  145. LengthOptionalStream(bool hasLength, bool hasPosition)
  146. : fHasLength(hasLength)
  147. , fHasPosition(hasPosition)
  148. {}
  149. bool hasLength() const override {
  150. return fHasLength;
  151. }
  152. bool hasPosition() const override {
  153. return fHasPosition;
  154. }
  155. size_t read(void*, size_t) override {
  156. return 0;
  157. }
  158. bool isAtEnd() const override {
  159. return true;
  160. }
  161. private:
  162. const bool fHasLength;
  163. const bool fHasPosition;
  164. };
  165. // Test all possible combinations of the wrapped stream having a length and a position.
  166. static void test_length_combos(skiatest::Reporter* reporter, size_t bufferSize) {
  167. for (int hasLen = 0; hasLen <= 1; hasLen++) {
  168. for (int hasPos = 0; hasPos <= 1; hasPos++) {
  169. LengthOptionalStream* stream =
  170. new LengthOptionalStream(SkToBool(hasLen), SkToBool(hasPos));
  171. auto buffered = SkFrontBufferedStream::Make(std::unique_ptr<SkStream>(stream),
  172. bufferSize);
  173. test_hasLength(reporter, *buffered.get(), *stream);
  174. }
  175. }
  176. }
  177. // Test using a stream with an initial offset.
  178. static void test_initial_offset(skiatest::Reporter* reporter, size_t bufferSize) {
  179. SkMemoryStream* memStream = new SkMemoryStream(gAbcs, strlen(gAbcs), false);
  180. // Skip a few characters into the memStream, so that bufferedStream represents an offset into
  181. // the stream it wraps.
  182. const size_t arbitraryOffset = 17;
  183. memStream->skip(arbitraryOffset);
  184. auto bufferedStream = SkFrontBufferedStream::Make(std::unique_ptr<SkStream>(memStream),
  185. bufferSize);
  186. // Since SkMemoryStream has a length, bufferedStream must also.
  187. REPORTER_ASSERT(reporter, bufferedStream->hasLength());
  188. const size_t amountToRead = 10;
  189. const size_t bufferedLength = bufferedStream->getLength();
  190. size_t currentPosition = 0;
  191. // Read the stream in chunks. After each read, the position must match currentPosition,
  192. // which sums the amount attempted to read, unless the end of the stream has been reached.
  193. // Importantly, the end should not have been reached until currentPosition == bufferedLength.
  194. while (currentPosition < bufferedLength) {
  195. REPORTER_ASSERT(reporter, !bufferedStream->isAtEnd());
  196. test_read(reporter, bufferedStream.get(), gAbcs + arbitraryOffset + currentPosition,
  197. amountToRead);
  198. currentPosition = SkTMin(currentPosition + amountToRead, bufferedLength);
  199. REPORTER_ASSERT(reporter, memStream->getPosition() - arbitraryOffset == currentPosition);
  200. }
  201. REPORTER_ASSERT(reporter, bufferedStream->isAtEnd());
  202. REPORTER_ASSERT(reporter, bufferedLength == currentPosition);
  203. }
  204. static void test_buffers(skiatest::Reporter* reporter, size_t bufferSize) {
  205. test_incremental_buffering(reporter, bufferSize);
  206. test_perfectly_sized_buffer(reporter, bufferSize);
  207. test_skipping(reporter, bufferSize);
  208. test_read_beyond_buffer(reporter, bufferSize);
  209. test_length_combos(reporter, bufferSize);
  210. test_initial_offset(reporter, bufferSize);
  211. }
  212. DEF_TEST(FrontBufferedStream, reporter) {
  213. // Test 6 and 64, which are used by Android, as well as another arbitrary length.
  214. test_buffers(reporter, 6);
  215. test_buffers(reporter, 15);
  216. test_buffers(reporter, 64);
  217. }
  218. // Test that a FrontBufferedStream does not allow reading after the end of a stream.
  219. // This class is a dummy SkStream which reports that it is at the end on the first
  220. // read (simulating a failure). Then it tracks whether someone calls read() again.
  221. class FailingStream : public SkStream {
  222. public:
  223. FailingStream()
  224. : fAtEnd(false)
  225. {}
  226. size_t read(void* buffer, size_t size) override {
  227. SkASSERT(!fAtEnd);
  228. fAtEnd = true;
  229. return 0;
  230. }
  231. bool isAtEnd() const override {
  232. return fAtEnd;
  233. }
  234. private:
  235. bool fAtEnd;
  236. };
  237. DEF_TEST(ShortFrontBufferedStream, reporter) {
  238. FailingStream* failingStream = new FailingStream;
  239. auto stream = SkFrontBufferedStream::Make(std::unique_ptr<SkStream>(failingStream), 64);
  240. // This will fail to create a codec. However, what we really want to test is that we
  241. // won't read past the end of the stream.
  242. std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream(std::move(stream)));
  243. }