epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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 | */ |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 7 | |
scroggo | 028a413 | 2015-04-02 13:19:51 -0700 | [diff] [blame] | 8 | #include "Resources.h" |
reed@google.com | 8d0b577 | 2011-06-24 13:07:31 +0000 | [diff] [blame] | 9 | #include "SkData.h" |
scroggo | 028a413 | 2015-04-02 13:19:51 -0700 | [diff] [blame] | 10 | #include "SkFrontBufferedStream.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 11 | #include "SkOSFile.h" |
| 12 | #include "SkRandom.h" |
| 13 | #include "SkStream.h" |
halcanary | 48305e8 | 2015-08-18 13:30:25 -0700 | [diff] [blame] | 14 | #include "SkStreamPriv.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 15 | #include "Test.h" |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 16 | |
reed@google.com | 789c6f2 | 2013-02-25 20:24:24 +0000 | [diff] [blame] | 17 | #ifndef SK_BUILD_FOR_WIN |
| 18 | #include <unistd.h> |
| 19 | #include <fcntl.h> |
| 20 | #endif |
| 21 | |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 22 | #define MAX_SIZE (256 * 1024) |
| 23 | |
reed@google.com | 789c6f2 | 2013-02-25 20:24:24 +0000 | [diff] [blame] | 24 | static void test_loop_stream(skiatest::Reporter* reporter, SkStream* stream, |
| 25 | const void* src, size_t len, int repeat) { |
| 26 | SkAutoSMalloc<256> storage(len); |
| 27 | void* tmp = storage.get(); |
| 28 | |
| 29 | for (int i = 0; i < repeat; ++i) { |
| 30 | size_t bytes = stream->read(tmp, len); |
| 31 | REPORTER_ASSERT(reporter, bytes == len); |
| 32 | REPORTER_ASSERT(reporter, !memcmp(tmp, src, len)); |
| 33 | } |
| 34 | |
| 35 | // expect EOF |
| 36 | size_t bytes = stream->read(tmp, 1); |
| 37 | REPORTER_ASSERT(reporter, 0 == bytes); |
bungeman@google.com | 88682b7 | 2013-07-19 13:55:41 +0000 | [diff] [blame] | 38 | // isAtEnd might not return true until after the first failing read. |
| 39 | REPORTER_ASSERT(reporter, stream->isAtEnd()); |
reed@google.com | 789c6f2 | 2013-02-25 20:24:24 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | static void test_filestreams(skiatest::Reporter* reporter, const char* tmpDir) { |
tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 43 | SkString path = SkOSPath::Join(tmpDir, "wstream_test"); |
reed@google.com | 789c6f2 | 2013-02-25 20:24:24 +0000 | [diff] [blame] | 44 | |
| 45 | const char s[] = "abcdefghijklmnopqrstuvwxyz"; |
| 46 | |
| 47 | { |
| 48 | SkFILEWStream writer(path.c_str()); |
| 49 | if (!writer.isValid()) { |
halcanary@google.com | a9325fa | 2014-01-10 14:58:10 +0000 | [diff] [blame] | 50 | ERRORF(reporter, "Failed to create tmp file %s\n", path.c_str()); |
reed@google.com | 789c6f2 | 2013-02-25 20:24:24 +0000 | [diff] [blame] | 51 | return; |
| 52 | } |
| 53 | |
| 54 | for (int i = 0; i < 100; ++i) { |
| 55 | writer.write(s, 26); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | { |
| 60 | SkFILEStream stream(path.c_str()); |
| 61 | REPORTER_ASSERT(reporter, stream.isValid()); |
| 62 | test_loop_stream(reporter, &stream, s, 26, 100); |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 63 | |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 64 | SkAutoTDelete<SkStreamAsset> stream2(stream.duplicate()); |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 65 | test_loop_stream(reporter, stream2.get(), s, 26, 100); |
reed@google.com | 789c6f2 | 2013-02-25 20:24:24 +0000 | [diff] [blame] | 66 | } |
| 67 | |
reed@google.com | 789c6f2 | 2013-02-25 20:24:24 +0000 | [diff] [blame] | 68 | { |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 69 | FILE* file = ::fopen(path.c_str(), "rb"); |
| 70 | SkFILEStream stream(file, SkFILEStream::kCallerPasses_Ownership); |
reed@google.com | 789c6f2 | 2013-02-25 20:24:24 +0000 | [diff] [blame] | 71 | REPORTER_ASSERT(reporter, stream.isValid()); |
| 72 | test_loop_stream(reporter, &stream, s, 26, 100); |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 73 | |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 74 | SkAutoTDelete<SkStreamAsset> stream2(stream.duplicate()); |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 75 | test_loop_stream(reporter, stream2.get(), s, 26, 100); |
reed@google.com | 789c6f2 | 2013-02-25 20:24:24 +0000 | [diff] [blame] | 76 | } |
reed@google.com | 789c6f2 | 2013-02-25 20:24:24 +0000 | [diff] [blame] | 77 | } |
| 78 | |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 79 | static void TestWStream(skiatest::Reporter* reporter) { |
| 80 | SkDynamicMemoryWStream ds; |
| 81 | const char s[] = "abcdefghijklmnopqrstuvwxyz"; |
| 82 | int i; |
| 83 | for (i = 0; i < 100; i++) { |
| 84 | REPORTER_ASSERT(reporter, ds.write(s, 26)); |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 85 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 86 | REPORTER_ASSERT(reporter, ds.getOffset() == 100 * 26); |
bungeman@google.com | 88682b7 | 2013-07-19 13:55:41 +0000 | [diff] [blame] | 87 | |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 88 | char* dst = new char[100 * 26 + 1]; |
| 89 | dst[100*26] = '*'; |
| 90 | ds.copyTo(dst); |
| 91 | REPORTER_ASSERT(reporter, dst[100*26] == '*'); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 92 | for (i = 0; i < 100; i++) { |
| 93 | REPORTER_ASSERT(reporter, memcmp(&dst[i * 26], s, 26) == 0); |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 94 | } |
reed@google.com | 70442a6 | 2011-06-23 21:48:04 +0000 | [diff] [blame] | 95 | |
| 96 | { |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 97 | SkAutoTDelete<SkStreamAsset> stream(ds.detachAsStream()); |
bungeman@google.com | 88682b7 | 2013-07-19 13:55:41 +0000 | [diff] [blame] | 98 | REPORTER_ASSERT(reporter, 100 * 26 == stream->getLength()); |
| 99 | REPORTER_ASSERT(reporter, ds.getOffset() == 0); |
| 100 | test_loop_stream(reporter, stream.get(), s, 26, 100); |
| 101 | |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 102 | SkAutoTDelete<SkStreamAsset> stream2(stream->duplicate()); |
bungeman@google.com | 88682b7 | 2013-07-19 13:55:41 +0000 | [diff] [blame] | 103 | test_loop_stream(reporter, stream2.get(), s, 26, 100); |
| 104 | |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 105 | SkAutoTDelete<SkStreamAsset> stream3(stream->fork()); |
bungeman@google.com | 88682b7 | 2013-07-19 13:55:41 +0000 | [diff] [blame] | 106 | REPORTER_ASSERT(reporter, stream3->isAtEnd()); |
| 107 | char tmp; |
| 108 | size_t bytes = stream->read(&tmp, 1); |
| 109 | REPORTER_ASSERT(reporter, 0 == bytes); |
| 110 | stream3->rewind(); |
| 111 | test_loop_stream(reporter, stream3.get(), s, 26, 100); |
| 112 | } |
| 113 | |
| 114 | for (i = 0; i < 100; i++) { |
| 115 | REPORTER_ASSERT(reporter, ds.write(s, 26)); |
| 116 | } |
| 117 | REPORTER_ASSERT(reporter, ds.getOffset() == 100 * 26); |
| 118 | |
| 119 | { |
reed | 42943c8 | 2016-09-12 12:01:44 -0700 | [diff] [blame] | 120 | sk_sp<SkData> data = ds.snapshotAsData(); |
reed@google.com | 70442a6 | 2011-06-23 21:48:04 +0000 | [diff] [blame] | 121 | REPORTER_ASSERT(reporter, 100 * 26 == data->size()); |
| 122 | REPORTER_ASSERT(reporter, memcmp(dst, data->data(), data->size()) == 0); |
bungeman@google.com | 88682b7 | 2013-07-19 13:55:41 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | { |
reed | 42943c8 | 2016-09-12 12:01:44 -0700 | [diff] [blame] | 126 | // Test that this works after a snapshot. |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 127 | SkAutoTDelete<SkStreamAsset> stream(ds.detachAsStream()); |
bungeman@google.com | 88682b7 | 2013-07-19 13:55:41 +0000 | [diff] [blame] | 128 | REPORTER_ASSERT(reporter, ds.getOffset() == 0); |
| 129 | test_loop_stream(reporter, stream.get(), s, 26, 100); |
| 130 | |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 131 | SkAutoTDelete<SkStreamAsset> stream2(stream->duplicate()); |
bungeman@google.com | 88682b7 | 2013-07-19 13:55:41 +0000 | [diff] [blame] | 132 | test_loop_stream(reporter, stream2.get(), s, 26, 100); |
reed@google.com | 70442a6 | 2011-06-23 21:48:04 +0000 | [diff] [blame] | 133 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 134 | delete[] dst; |
reed@google.com | 789c6f2 | 2013-02-25 20:24:24 +0000 | [diff] [blame] | 135 | |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 136 | SkString tmpDir = skiatest::GetTmpDir(); |
scroggo@google.com | c76218d | 2013-06-06 14:59:56 +0000 | [diff] [blame] | 137 | if (!tmpDir.isEmpty()) { |
| 138 | test_filestreams(reporter, tmpDir.c_str()); |
reed@google.com | 789c6f2 | 2013-02-25 20:24:24 +0000 | [diff] [blame] | 139 | } |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 140 | } |
| 141 | |
reed@google.com | 19f286b | 2011-10-18 11:49:52 +0000 | [diff] [blame] | 142 | static void TestPackedUInt(skiatest::Reporter* reporter) { |
| 143 | // we know that packeduint tries to write 1, 2 or 4 bytes for the length, |
| 144 | // so we test values around each of those transitions (and a few others) |
| 145 | const size_t sizes[] = { |
| 146 | 0, 1, 2, 0xFC, 0xFD, 0xFE, 0xFF, 0x100, 0x101, 32767, 32768, 32769, |
| 147 | 0xFFFD, 0xFFFE, 0xFFFF, 0x10000, 0x10001, |
| 148 | 0xFFFFFD, 0xFFFFFE, 0xFFFFFF, 0x1000000, 0x1000001, |
| 149 | 0x7FFFFFFE, 0x7FFFFFFF, 0x80000000, 0x80000001, 0xFFFFFFFE, 0xFFFFFFFF |
| 150 | }; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 151 | |
| 152 | |
reed@google.com | 19f286b | 2011-10-18 11:49:52 +0000 | [diff] [blame] | 153 | size_t i; |
| 154 | char buffer[sizeof(sizes) * 4]; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 155 | |
reed@google.com | 19f286b | 2011-10-18 11:49:52 +0000 | [diff] [blame] | 156 | SkMemoryWStream wstream(buffer, sizeof(buffer)); |
| 157 | for (i = 0; i < SK_ARRAY_COUNT(sizes); ++i) { |
| 158 | bool success = wstream.writePackedUInt(sizes[i]); |
| 159 | REPORTER_ASSERT(reporter, success); |
| 160 | } |
| 161 | wstream.flush(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 162 | |
reed@google.com | 19f286b | 2011-10-18 11:49:52 +0000 | [diff] [blame] | 163 | SkMemoryStream rstream(buffer, sizeof(buffer)); |
| 164 | for (i = 0; i < SK_ARRAY_COUNT(sizes); ++i) { |
| 165 | size_t n = rstream.readPackedUInt(); |
| 166 | if (sizes[i] != n) { |
halcanary | 7d57124 | 2016-02-24 17:59:16 -0800 | [diff] [blame] | 167 | ERRORF(reporter, "sizes:%x != n:%x\n", i, sizes[i], n); |
reed@google.com | 19f286b | 2011-10-18 11:49:52 +0000 | [diff] [blame] | 168 | } |
reed@google.com | 19f286b | 2011-10-18 11:49:52 +0000 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 172 | // Test that setting an SkMemoryStream to a nullptr data does not result in a crash when calling |
scroggo@google.com | e490420 | 2013-01-09 22:02:58 +0000 | [diff] [blame] | 173 | // methods that access fData. |
| 174 | static void TestDereferencingData(SkMemoryStream* memStream) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 175 | memStream->read(nullptr, 0); |
scroggo@google.com | e490420 | 2013-01-09 22:02:58 +0000 | [diff] [blame] | 176 | memStream->getMemoryBase(); |
reed | 42943c8 | 2016-09-12 12:01:44 -0700 | [diff] [blame] | 177 | (void)memStream->asData(); |
scroggo@google.com | e490420 | 2013-01-09 22:02:58 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | static void TestNullData() { |
reed | 42943c8 | 2016-09-12 12:01:44 -0700 | [diff] [blame] | 181 | SkMemoryStream memStream(nullptr); |
scroggo@google.com | e490420 | 2013-01-09 22:02:58 +0000 | [diff] [blame] | 182 | TestDereferencingData(&memStream); |
| 183 | |
reed | 42943c8 | 2016-09-12 12:01:44 -0700 | [diff] [blame] | 184 | memStream.setData(nullptr); |
scroggo@google.com | e490420 | 2013-01-09 22:02:58 +0000 | [diff] [blame] | 185 | TestDereferencingData(&memStream); |
| 186 | |
| 187 | } |
| 188 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 189 | DEF_TEST(Stream, reporter) { |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 190 | TestWStream(reporter); |
reed@google.com | 19f286b | 2011-10-18 11:49:52 +0000 | [diff] [blame] | 191 | TestPackedUInt(reporter); |
scroggo@google.com | e490420 | 2013-01-09 22:02:58 +0000 | [diff] [blame] | 192 | TestNullData(); |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 193 | } |
scroggo | 028a413 | 2015-04-02 13:19:51 -0700 | [diff] [blame] | 194 | |
| 195 | /** |
| 196 | * Tests peeking and then reading the same amount. The two should provide the |
| 197 | * same results. |
scroggo | d61c384 | 2015-12-07 11:37:13 -0800 | [diff] [blame] | 198 | * Returns the amount successfully read minus the amount successfully peeked. |
scroggo | 028a413 | 2015-04-02 13:19:51 -0700 | [diff] [blame] | 199 | */ |
scroggo | d61c384 | 2015-12-07 11:37:13 -0800 | [diff] [blame] | 200 | static size_t compare_peek_to_read(skiatest::Reporter* reporter, |
| 201 | SkStream* stream, size_t bytesToPeek) { |
scroggo | 028a413 | 2015-04-02 13:19:51 -0700 | [diff] [blame] | 202 | // The rest of our tests won't be very interesting if bytesToPeek is zero. |
| 203 | REPORTER_ASSERT(reporter, bytesToPeek > 0); |
| 204 | SkAutoMalloc peekStorage(bytesToPeek); |
| 205 | SkAutoMalloc readStorage(bytesToPeek); |
| 206 | void* peekPtr = peekStorage.get(); |
| 207 | void* readPtr = peekStorage.get(); |
| 208 | |
scroggo | d61c384 | 2015-12-07 11:37:13 -0800 | [diff] [blame] | 209 | const size_t bytesPeeked = stream->peek(peekPtr, bytesToPeek); |
scroggo | 028a413 | 2015-04-02 13:19:51 -0700 | [diff] [blame] | 210 | const size_t bytesRead = stream->read(readPtr, bytesToPeek); |
| 211 | |
| 212 | // bytesRead should only be less than attempted if the stream is at the |
| 213 | // end. |
| 214 | REPORTER_ASSERT(reporter, bytesRead == bytesToPeek || stream->isAtEnd()); |
| 215 | |
| 216 | // peek and read should behave the same, except peek returned to the |
| 217 | // original position, so they read the same data. |
scroggo | d61c384 | 2015-12-07 11:37:13 -0800 | [diff] [blame] | 218 | REPORTER_ASSERT(reporter, !memcmp(peekPtr, readPtr, bytesPeeked)); |
scroggo | 028a413 | 2015-04-02 13:19:51 -0700 | [diff] [blame] | 219 | |
scroggo | d61c384 | 2015-12-07 11:37:13 -0800 | [diff] [blame] | 220 | // A stream should never be able to peek more than it can read. |
| 221 | REPORTER_ASSERT(reporter, bytesRead >= bytesPeeked); |
| 222 | |
| 223 | return bytesRead - bytesPeeked; |
scroggo | 028a413 | 2015-04-02 13:19:51 -0700 | [diff] [blame] | 224 | } |
| 225 | |
scroggo | d61c384 | 2015-12-07 11:37:13 -0800 | [diff] [blame] | 226 | static void test_fully_peekable_stream(skiatest::Reporter* r, SkStream* stream, size_t limit) { |
scroggo | 028a413 | 2015-04-02 13:19:51 -0700 | [diff] [blame] | 227 | for (size_t i = 1; !stream->isAtEnd(); i++) { |
scroggo | d61c384 | 2015-12-07 11:37:13 -0800 | [diff] [blame] | 228 | REPORTER_ASSERT(r, compare_peek_to_read(r, stream, i) == 0); |
scroggo | 028a413 | 2015-04-02 13:19:51 -0700 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | |
| 232 | static void test_peeking_front_buffered_stream(skiatest::Reporter* r, |
| 233 | const SkStream& original, |
| 234 | size_t bufferSize) { |
| 235 | SkStream* dupe = original.duplicate(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 236 | REPORTER_ASSERT(r, dupe != nullptr); |
scroggo | 028a413 | 2015-04-02 13:19:51 -0700 | [diff] [blame] | 237 | SkAutoTDelete<SkStream> bufferedStream(SkFrontBufferedStream::Create(dupe, bufferSize)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 238 | REPORTER_ASSERT(r, bufferedStream != nullptr); |
scroggo | d61c384 | 2015-12-07 11:37:13 -0800 | [diff] [blame] | 239 | |
| 240 | size_t peeked = 0; |
| 241 | for (size_t i = 1; !bufferedStream->isAtEnd(); i++) { |
| 242 | const size_t unpeekableBytes = compare_peek_to_read(r, bufferedStream, i); |
| 243 | if (unpeekableBytes > 0) { |
| 244 | // This could not have returned a number greater than i. |
| 245 | REPORTER_ASSERT(r, unpeekableBytes <= i); |
| 246 | |
| 247 | // We have reached the end of the buffer. Verify that it was at least |
| 248 | // bufferSize. |
| 249 | REPORTER_ASSERT(r, peeked + i - unpeekableBytes >= bufferSize); |
| 250 | // No more peeking is supported. |
| 251 | break; |
| 252 | } |
| 253 | peeked += i; |
| 254 | } |
| 255 | |
| 256 | // Test that attempting to peek beyond the length of the buffer does not prevent rewinding. |
| 257 | bufferedStream.reset(SkFrontBufferedStream::Create(original.duplicate(), bufferSize)); |
| 258 | REPORTER_ASSERT(r, bufferedStream != nullptr); |
| 259 | |
| 260 | const size_t bytesToPeek = bufferSize + 1; |
| 261 | SkAutoMalloc peekStorage(bytesToPeek); |
| 262 | SkAutoMalloc readStorage(bytesToPeek); |
| 263 | |
| 264 | for (size_t start = 0; start <= bufferSize; start++) { |
| 265 | // Skip to the starting point |
| 266 | REPORTER_ASSERT(r, bufferedStream->skip(start) == start); |
scroggo | d61c384 | 2015-12-07 11:37:13 -0800 | [diff] [blame] | 267 | |
| 268 | const size_t bytesPeeked = bufferedStream->peek(peekStorage.get(), bytesToPeek); |
| 269 | if (0 == bytesPeeked) { |
scroggo | ef0fed3 | 2016-02-18 05:59:25 -0800 | [diff] [blame] | 270 | // Peeking should only fail completely if we have read/skipped beyond the buffer. |
| 271 | REPORTER_ASSERT(r, start >= bufferSize); |
scroggo | d61c384 | 2015-12-07 11:37:13 -0800 | [diff] [blame] | 272 | break; |
| 273 | } |
| 274 | |
| 275 | // Only read the amount that was successfully peeked. |
| 276 | const size_t bytesRead = bufferedStream->read(readStorage.get(), bytesPeeked); |
| 277 | REPORTER_ASSERT(r, bytesRead == bytesPeeked); |
| 278 | REPORTER_ASSERT(r, !memcmp(peekStorage.get(), readStorage.get(), bytesPeeked)); |
| 279 | |
| 280 | // This should be safe to rewind. |
| 281 | REPORTER_ASSERT(r, bufferedStream->rewind()); |
| 282 | } |
scroggo | 028a413 | 2015-04-02 13:19:51 -0700 | [diff] [blame] | 283 | } |
| 284 | |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 285 | // This test uses file system operations that don't work out of the |
| 286 | // box on iOS. It's likely that we don't need them on iOS. Ignoring for now. |
| 287 | // TODO(stephana): Re-evaluate if we need this in the future. |
stephana | 195f62d | 2015-04-09 07:57:54 -0700 | [diff] [blame] | 288 | #ifndef SK_BUILD_FOR_IOS |
scroggo | 028a413 | 2015-04-02 13:19:51 -0700 | [diff] [blame] | 289 | DEF_TEST(StreamPeek, reporter) { |
| 290 | // Test a memory stream. |
| 291 | const char gAbcs[] = "abcdefghijklmnopqrstuvwxyz"; |
| 292 | SkMemoryStream memStream(gAbcs, strlen(gAbcs), false); |
scroggo | d61c384 | 2015-12-07 11:37:13 -0800 | [diff] [blame] | 293 | test_fully_peekable_stream(reporter, &memStream, memStream.getLength()); |
scroggo | 028a413 | 2015-04-02 13:19:51 -0700 | [diff] [blame] | 294 | |
| 295 | // Test an arbitrary file stream. file streams do not support peeking. |
| 296 | SkFILEStream fileStream(GetResourcePath("baby_tux.webp").c_str()); |
| 297 | REPORTER_ASSERT(reporter, fileStream.isValid()); |
caryclark | 30ac464 | 2015-04-14 06:08:04 -0700 | [diff] [blame] | 298 | if (!fileStream.isValid()) { |
| 299 | return; |
| 300 | } |
scroggo | 028a413 | 2015-04-02 13:19:51 -0700 | [diff] [blame] | 301 | SkAutoMalloc storage(fileStream.getLength()); |
| 302 | for (size_t i = 1; i < fileStream.getLength(); i++) { |
scroggo | d61c384 | 2015-12-07 11:37:13 -0800 | [diff] [blame] | 303 | REPORTER_ASSERT(reporter, fileStream.peek(storage.get(), i) == 0); |
scroggo | 028a413 | 2015-04-02 13:19:51 -0700 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | // Now test some FrontBufferedStreams |
| 307 | for (size_t i = 1; i < memStream.getLength(); i++) { |
| 308 | test_peeking_front_buffered_stream(reporter, memStream, i); |
| 309 | } |
| 310 | } |
halcanary | e797d0d | 2015-05-21 08:13:27 -0700 | [diff] [blame] | 311 | #endif |
| 312 | |
| 313 | // Asserts that asset == expected and is peekable. |
| 314 | static void stream_peek_test(skiatest::Reporter* rep, |
| 315 | SkStreamAsset* asset, |
| 316 | const SkData* expected) { |
| 317 | if (asset->getLength() != expected->size()) { |
| 318 | ERRORF(rep, "Unexpected length."); |
| 319 | return; |
| 320 | } |
| 321 | SkRandom rand; |
| 322 | uint8_t buffer[4096]; |
| 323 | const uint8_t* expect = expected->bytes(); |
| 324 | for (size_t i = 0; i < asset->getLength(); ++i) { |
| 325 | uint32_t maxSize = |
| 326 | SkToU32(SkTMin(sizeof(buffer), asset->getLength() - i)); |
| 327 | size_t size = rand.nextRangeU(1, maxSize); |
| 328 | SkASSERT(size >= 1); |
| 329 | SkASSERT(size <= sizeof(buffer)); |
| 330 | SkASSERT(size + i <= asset->getLength()); |
scroggo | d61c384 | 2015-12-07 11:37:13 -0800 | [diff] [blame] | 331 | if (asset->peek(buffer, size) < size) { |
halcanary | e797d0d | 2015-05-21 08:13:27 -0700 | [diff] [blame] | 332 | ERRORF(rep, "Peek Failed!"); |
| 333 | return; |
| 334 | } |
| 335 | if (0 != memcmp(buffer, &expect[i], size)) { |
| 336 | ERRORF(rep, "Peek returned wrong bytes!"); |
| 337 | return; |
| 338 | } |
| 339 | uint8_t value; |
| 340 | REPORTER_ASSERT(rep, 1 == asset->read(&value, 1)); |
| 341 | if (value != expect[i]) { |
| 342 | ERRORF(rep, "Read Failed!"); |
| 343 | return; |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | DEF_TEST(StreamPeek_BlockMemoryStream, rep) { |
| 349 | const static int kSeed = 1234; |
| 350 | SkRandom valueSource(kSeed); |
| 351 | SkRandom rand(kSeed << 1); |
| 352 | uint8_t buffer[4096]; |
| 353 | SkDynamicMemoryWStream dynamicMemoryWStream; |
| 354 | for (int i = 0; i < 32; ++i) { |
| 355 | // Randomize the length of the blocks. |
| 356 | size_t size = rand.nextRangeU(1, sizeof(buffer)); |
| 357 | for (size_t j = 0; j < size; ++j) { |
| 358 | buffer[j] = valueSource.nextU() & 0xFF; |
| 359 | } |
| 360 | dynamicMemoryWStream.write(buffer, size); |
| 361 | } |
| 362 | SkAutoTDelete<SkStreamAsset> asset(dynamicMemoryWStream.detachAsStream()); |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 363 | sk_sp<SkData> expected(SkData::MakeUninitialized(asset->getLength())); |
halcanary | e797d0d | 2015-05-21 08:13:27 -0700 | [diff] [blame] | 364 | uint8_t* expectedPtr = static_cast<uint8_t*>(expected->writable_data()); |
| 365 | valueSource.setSeed(kSeed); // reseed. |
| 366 | // We want the exact same same "random" string of numbers to put |
| 367 | // in expected. i.e.: don't rely on SkDynamicMemoryStream to work |
| 368 | // correctly while we are testing SkDynamicMemoryStream. |
| 369 | for (size_t i = 0; i < asset->getLength(); ++i) { |
| 370 | expectedPtr[i] = valueSource.nextU() & 0xFF; |
| 371 | } |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 372 | stream_peek_test(rep, asset, expected.get()); |
halcanary | e797d0d | 2015-05-21 08:13:27 -0700 | [diff] [blame] | 373 | } |
halcanary | 48305e8 | 2015-08-18 13:30:25 -0700 | [diff] [blame] | 374 | |
| 375 | namespace { |
| 376 | class DumbStream : public SkStream { |
| 377 | public: |
| 378 | DumbStream(const uint8_t* data, size_t n) |
| 379 | : fData(data), fCount(n), fIdx(0) {} |
| 380 | size_t read(void* buffer, size_t size) override { |
halcanary | d75e21d | 2015-08-19 07:22:04 -0700 | [diff] [blame] | 381 | size_t copyCount = SkTMin(fCount - fIdx, size); |
| 382 | if (copyCount) { |
| 383 | memcpy(buffer, &fData[fIdx], copyCount); |
| 384 | fIdx += copyCount; |
halcanary | 48305e8 | 2015-08-18 13:30:25 -0700 | [diff] [blame] | 385 | } |
halcanary | d75e21d | 2015-08-19 07:22:04 -0700 | [diff] [blame] | 386 | return copyCount; |
halcanary | 48305e8 | 2015-08-18 13:30:25 -0700 | [diff] [blame] | 387 | } |
| 388 | bool isAtEnd() const override { |
halcanary | d75e21d | 2015-08-19 07:22:04 -0700 | [diff] [blame] | 389 | return fCount == fIdx; |
halcanary | 48305e8 | 2015-08-18 13:30:25 -0700 | [diff] [blame] | 390 | } |
| 391 | private: |
| 392 | const uint8_t* fData; |
| 393 | size_t fCount, fIdx; |
| 394 | }; |
| 395 | } // namespace |
| 396 | |
| 397 | static void stream_copy_test(skiatest::Reporter* reporter, |
| 398 | const void* srcData, |
| 399 | size_t N, |
| 400 | SkStream* stream) { |
| 401 | SkDynamicMemoryWStream tgt; |
| 402 | if (!SkStreamCopy(&tgt, stream)) { |
| 403 | ERRORF(reporter, "SkStreamCopy failed"); |
| 404 | return; |
| 405 | } |
reed | 42943c8 | 2016-09-12 12:01:44 -0700 | [diff] [blame] | 406 | sk_sp<SkData> data(tgt.detachAsData()); |
halcanary | 48305e8 | 2015-08-18 13:30:25 -0700 | [diff] [blame] | 407 | if (data->size() != N) { |
| 408 | ERRORF(reporter, "SkStreamCopy incorrect size"); |
| 409 | return; |
| 410 | } |
| 411 | if (0 != memcmp(data->data(), srcData, N)) { |
| 412 | ERRORF(reporter, "SkStreamCopy bad copy"); |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | DEF_TEST(StreamCopy, reporter) { |
| 417 | SkRandom random(123456); |
halcanary | cb9241b | 2015-08-19 06:12:40 -0700 | [diff] [blame] | 418 | static const int N = 10000; |
| 419 | SkAutoTMalloc<uint8_t> src((size_t)N); |
| 420 | for (int j = 0; j < N; ++j) { |
halcanary | 48305e8 | 2015-08-18 13:30:25 -0700 | [diff] [blame] | 421 | src[j] = random.nextU() & 0xff; |
| 422 | } |
| 423 | // SkStreamCopy had two code paths; this test both. |
halcanary | cb9241b | 2015-08-19 06:12:40 -0700 | [diff] [blame] | 424 | DumbStream dumbStream(src.get(), (size_t)N); |
halcanary | 48305e8 | 2015-08-18 13:30:25 -0700 | [diff] [blame] | 425 | stream_copy_test(reporter, src, N, &dumbStream); |
halcanary | cb9241b | 2015-08-19 06:12:40 -0700 | [diff] [blame] | 426 | SkMemoryStream smartStream(src.get(), (size_t)N); |
halcanary | 48305e8 | 2015-08-18 13:30:25 -0700 | [diff] [blame] | 427 | stream_copy_test(reporter, src, N, &smartStream); |
halcanary | 48305e8 | 2015-08-18 13:30:25 -0700 | [diff] [blame] | 428 | } |
halcanary | 209c162 | 2015-09-28 07:29:39 -0700 | [diff] [blame] | 429 | |
| 430 | DEF_TEST(StreamEmptyStreamMemoryBase, r) { |
| 431 | SkDynamicMemoryWStream tmp; |
| 432 | SkAutoTDelete<SkStreamAsset> asset(tmp.detachAsStream()); |
| 433 | REPORTER_ASSERT(r, nullptr == asset->getMemoryBase()); |
| 434 | } |