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" |
| 14 | #include "Test.h" |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 15 | |
reed@google.com | 789c6f2 | 2013-02-25 20:24:24 +0000 | [diff] [blame] | 16 | #ifndef SK_BUILD_FOR_WIN |
| 17 | #include <unistd.h> |
| 18 | #include <fcntl.h> |
| 19 | #endif |
| 20 | |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 21 | #define MAX_SIZE (256 * 1024) |
| 22 | |
reed@google.com | 789c6f2 | 2013-02-25 20:24:24 +0000 | [diff] [blame] | 23 | static void test_loop_stream(skiatest::Reporter* reporter, SkStream* stream, |
| 24 | const void* src, size_t len, int repeat) { |
| 25 | SkAutoSMalloc<256> storage(len); |
| 26 | void* tmp = storage.get(); |
| 27 | |
| 28 | for (int i = 0; i < repeat; ++i) { |
| 29 | size_t bytes = stream->read(tmp, len); |
| 30 | REPORTER_ASSERT(reporter, bytes == len); |
| 31 | REPORTER_ASSERT(reporter, !memcmp(tmp, src, len)); |
| 32 | } |
| 33 | |
| 34 | // expect EOF |
| 35 | size_t bytes = stream->read(tmp, 1); |
| 36 | REPORTER_ASSERT(reporter, 0 == bytes); |
bungeman@google.com | 88682b7 | 2013-07-19 13:55:41 +0000 | [diff] [blame] | 37 | // isAtEnd might not return true until after the first failing read. |
| 38 | REPORTER_ASSERT(reporter, stream->isAtEnd()); |
reed@google.com | 789c6f2 | 2013-02-25 20:24:24 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | static void test_filestreams(skiatest::Reporter* reporter, const char* tmpDir) { |
tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 42 | SkString path = SkOSPath::Join(tmpDir, "wstream_test"); |
reed@google.com | 789c6f2 | 2013-02-25 20:24:24 +0000 | [diff] [blame] | 43 | |
| 44 | const char s[] = "abcdefghijklmnopqrstuvwxyz"; |
| 45 | |
| 46 | { |
| 47 | SkFILEWStream writer(path.c_str()); |
| 48 | if (!writer.isValid()) { |
halcanary@google.com | a9325fa | 2014-01-10 14:58:10 +0000 | [diff] [blame] | 49 | 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] | 50 | return; |
| 51 | } |
| 52 | |
| 53 | for (int i = 0; i < 100; ++i) { |
| 54 | writer.write(s, 26); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | { |
| 59 | SkFILEStream stream(path.c_str()); |
| 60 | REPORTER_ASSERT(reporter, stream.isValid()); |
| 61 | test_loop_stream(reporter, &stream, s, 26, 100); |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 62 | |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 63 | SkAutoTDelete<SkStreamAsset> stream2(stream.duplicate()); |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 64 | test_loop_stream(reporter, stream2.get(), s, 26, 100); |
reed@google.com | 789c6f2 | 2013-02-25 20:24:24 +0000 | [diff] [blame] | 65 | } |
| 66 | |
reed@google.com | 789c6f2 | 2013-02-25 20:24:24 +0000 | [diff] [blame] | 67 | { |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 68 | FILE* file = ::fopen(path.c_str(), "rb"); |
| 69 | SkFILEStream stream(file, SkFILEStream::kCallerPasses_Ownership); |
reed@google.com | 789c6f2 | 2013-02-25 20:24:24 +0000 | [diff] [blame] | 70 | REPORTER_ASSERT(reporter, stream.isValid()); |
| 71 | test_loop_stream(reporter, &stream, s, 26, 100); |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 72 | |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 73 | SkAutoTDelete<SkStreamAsset> stream2(stream.duplicate()); |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 74 | test_loop_stream(reporter, stream2.get(), s, 26, 100); |
reed@google.com | 789c6f2 | 2013-02-25 20:24:24 +0000 | [diff] [blame] | 75 | } |
reed@google.com | 789c6f2 | 2013-02-25 20:24:24 +0000 | [diff] [blame] | 76 | } |
| 77 | |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 78 | static void TestWStream(skiatest::Reporter* reporter) { |
| 79 | SkDynamicMemoryWStream ds; |
| 80 | const char s[] = "abcdefghijklmnopqrstuvwxyz"; |
| 81 | int i; |
| 82 | for (i = 0; i < 100; i++) { |
| 83 | REPORTER_ASSERT(reporter, ds.write(s, 26)); |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 84 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 85 | REPORTER_ASSERT(reporter, ds.getOffset() == 100 * 26); |
bungeman@google.com | 88682b7 | 2013-07-19 13:55:41 +0000 | [diff] [blame] | 86 | |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 87 | char* dst = new char[100 * 26 + 1]; |
| 88 | dst[100*26] = '*'; |
| 89 | ds.copyTo(dst); |
| 90 | REPORTER_ASSERT(reporter, dst[100*26] == '*'); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 91 | for (i = 0; i < 100; i++) { |
| 92 | REPORTER_ASSERT(reporter, memcmp(&dst[i * 26], s, 26) == 0); |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 93 | } |
reed@google.com | 70442a6 | 2011-06-23 21:48:04 +0000 | [diff] [blame] | 94 | |
| 95 | { |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 96 | SkAutoTDelete<SkStreamAsset> stream(ds.detachAsStream()); |
bungeman@google.com | 88682b7 | 2013-07-19 13:55:41 +0000 | [diff] [blame] | 97 | REPORTER_ASSERT(reporter, 100 * 26 == stream->getLength()); |
| 98 | REPORTER_ASSERT(reporter, ds.getOffset() == 0); |
| 99 | test_loop_stream(reporter, stream.get(), s, 26, 100); |
| 100 | |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 101 | SkAutoTDelete<SkStreamAsset> stream2(stream->duplicate()); |
bungeman@google.com | 88682b7 | 2013-07-19 13:55:41 +0000 | [diff] [blame] | 102 | test_loop_stream(reporter, stream2.get(), s, 26, 100); |
| 103 | |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 104 | SkAutoTDelete<SkStreamAsset> stream3(stream->fork()); |
bungeman@google.com | 88682b7 | 2013-07-19 13:55:41 +0000 | [diff] [blame] | 105 | REPORTER_ASSERT(reporter, stream3->isAtEnd()); |
| 106 | char tmp; |
| 107 | size_t bytes = stream->read(&tmp, 1); |
| 108 | REPORTER_ASSERT(reporter, 0 == bytes); |
| 109 | stream3->rewind(); |
| 110 | test_loop_stream(reporter, stream3.get(), s, 26, 100); |
| 111 | } |
| 112 | |
| 113 | for (i = 0; i < 100; i++) { |
| 114 | REPORTER_ASSERT(reporter, ds.write(s, 26)); |
| 115 | } |
| 116 | REPORTER_ASSERT(reporter, ds.getOffset() == 100 * 26); |
| 117 | |
| 118 | { |
| 119 | SkAutoTUnref<SkData> data(ds.copyToData()); |
reed@google.com | 70442a6 | 2011-06-23 21:48:04 +0000 | [diff] [blame] | 120 | REPORTER_ASSERT(reporter, 100 * 26 == data->size()); |
| 121 | REPORTER_ASSERT(reporter, memcmp(dst, data->data(), data->size()) == 0); |
bungeman@google.com | 88682b7 | 2013-07-19 13:55:41 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | { |
| 125 | // Test that this works after a copyToData. |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 126 | SkAutoTDelete<SkStreamAsset> stream(ds.detachAsStream()); |
bungeman@google.com | 88682b7 | 2013-07-19 13:55:41 +0000 | [diff] [blame] | 127 | REPORTER_ASSERT(reporter, ds.getOffset() == 0); |
| 128 | test_loop_stream(reporter, stream.get(), s, 26, 100); |
| 129 | |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 130 | SkAutoTDelete<SkStreamAsset> stream2(stream->duplicate()); |
bungeman@google.com | 88682b7 | 2013-07-19 13:55:41 +0000 | [diff] [blame] | 131 | test_loop_stream(reporter, stream2.get(), s, 26, 100); |
reed@google.com | 70442a6 | 2011-06-23 21:48:04 +0000 | [diff] [blame] | 132 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 133 | delete[] dst; |
reed@google.com | 789c6f2 | 2013-02-25 20:24:24 +0000 | [diff] [blame] | 134 | |
halcanary | 87f3ba4 | 2015-01-20 09:30:20 -0800 | [diff] [blame] | 135 | SkString tmpDir = skiatest::GetTmpDir(); |
scroggo@google.com | c76218d | 2013-06-06 14:59:56 +0000 | [diff] [blame] | 136 | if (!tmpDir.isEmpty()) { |
| 137 | test_filestreams(reporter, tmpDir.c_str()); |
reed@google.com | 789c6f2 | 2013-02-25 20:24:24 +0000 | [diff] [blame] | 138 | } |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 139 | } |
| 140 | |
reed@google.com | 19f286b | 2011-10-18 11:49:52 +0000 | [diff] [blame] | 141 | static void TestPackedUInt(skiatest::Reporter* reporter) { |
| 142 | // we know that packeduint tries to write 1, 2 or 4 bytes for the length, |
| 143 | // so we test values around each of those transitions (and a few others) |
| 144 | const size_t sizes[] = { |
| 145 | 0, 1, 2, 0xFC, 0xFD, 0xFE, 0xFF, 0x100, 0x101, 32767, 32768, 32769, |
| 146 | 0xFFFD, 0xFFFE, 0xFFFF, 0x10000, 0x10001, |
| 147 | 0xFFFFFD, 0xFFFFFE, 0xFFFFFF, 0x1000000, 0x1000001, |
| 148 | 0x7FFFFFFE, 0x7FFFFFFF, 0x80000000, 0x80000001, 0xFFFFFFFE, 0xFFFFFFFF |
| 149 | }; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 150 | |
| 151 | |
reed@google.com | 19f286b | 2011-10-18 11:49:52 +0000 | [diff] [blame] | 152 | size_t i; |
| 153 | char buffer[sizeof(sizes) * 4]; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 154 | |
reed@google.com | 19f286b | 2011-10-18 11:49:52 +0000 | [diff] [blame] | 155 | SkMemoryWStream wstream(buffer, sizeof(buffer)); |
| 156 | for (i = 0; i < SK_ARRAY_COUNT(sizes); ++i) { |
| 157 | bool success = wstream.writePackedUInt(sizes[i]); |
| 158 | REPORTER_ASSERT(reporter, success); |
| 159 | } |
| 160 | wstream.flush(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 161 | |
reed@google.com | 19f286b | 2011-10-18 11:49:52 +0000 | [diff] [blame] | 162 | SkMemoryStream rstream(buffer, sizeof(buffer)); |
| 163 | for (i = 0; i < SK_ARRAY_COUNT(sizes); ++i) { |
| 164 | size_t n = rstream.readPackedUInt(); |
| 165 | if (sizes[i] != n) { |
| 166 | SkDebugf("-- %d: sizes:%x n:%x\n", i, sizes[i], n); |
| 167 | } |
| 168 | REPORTER_ASSERT(reporter, sizes[i] == n); |
| 169 | } |
| 170 | } |
| 171 | |
scroggo@google.com | e490420 | 2013-01-09 22:02:58 +0000 | [diff] [blame] | 172 | // Test that setting an SkMemoryStream to a NULL data does not result in a crash when calling |
| 173 | // methods that access fData. |
| 174 | static void TestDereferencingData(SkMemoryStream* memStream) { |
| 175 | memStream->read(NULL, 0); |
| 176 | memStream->getMemoryBase(); |
| 177 | SkAutoDataUnref data(memStream->copyToData()); |
| 178 | } |
| 179 | |
| 180 | static void TestNullData() { |
| 181 | SkData* nullData = NULL; |
| 182 | SkMemoryStream memStream(nullData); |
| 183 | TestDereferencingData(&memStream); |
| 184 | |
| 185 | memStream.setData(nullData); |
| 186 | TestDereferencingData(&memStream); |
| 187 | |
| 188 | } |
| 189 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 190 | DEF_TEST(Stream, reporter) { |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 191 | TestWStream(reporter); |
reed@google.com | 19f286b | 2011-10-18 11:49:52 +0000 | [diff] [blame] | 192 | TestPackedUInt(reporter); |
scroggo@google.com | e490420 | 2013-01-09 22:02:58 +0000 | [diff] [blame] | 193 | TestNullData(); |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 194 | } |
scroggo | 028a413 | 2015-04-02 13:19:51 -0700 | [diff] [blame] | 195 | |
| 196 | /** |
| 197 | * Tests peeking and then reading the same amount. The two should provide the |
| 198 | * same results. |
| 199 | * Returns whether the stream could peek. |
| 200 | */ |
| 201 | static bool compare_peek_to_read(skiatest::Reporter* reporter, |
| 202 | SkStream* stream, size_t bytesToPeek) { |
| 203 | // The rest of our tests won't be very interesting if bytesToPeek is zero. |
| 204 | REPORTER_ASSERT(reporter, bytesToPeek > 0); |
| 205 | SkAutoMalloc peekStorage(bytesToPeek); |
| 206 | SkAutoMalloc readStorage(bytesToPeek); |
| 207 | void* peekPtr = peekStorage.get(); |
| 208 | void* readPtr = peekStorage.get(); |
| 209 | |
| 210 | if (!stream->peek(peekPtr, bytesToPeek)) { |
| 211 | return false; |
| 212 | } |
| 213 | const size_t bytesRead = stream->read(readPtr, bytesToPeek); |
| 214 | |
| 215 | // bytesRead should only be less than attempted if the stream is at the |
| 216 | // end. |
| 217 | REPORTER_ASSERT(reporter, bytesRead == bytesToPeek || stream->isAtEnd()); |
| 218 | |
| 219 | // peek and read should behave the same, except peek returned to the |
| 220 | // original position, so they read the same data. |
| 221 | REPORTER_ASSERT(reporter, !memcmp(peekPtr, readPtr, bytesRead)); |
| 222 | |
| 223 | return true; |
| 224 | } |
| 225 | |
| 226 | static void test_peeking_stream(skiatest::Reporter* r, SkStream* stream, size_t limit) { |
| 227 | size_t peeked = 0; |
| 228 | for (size_t i = 1; !stream->isAtEnd(); i++) { |
| 229 | const bool couldPeek = compare_peek_to_read(r, stream, i); |
| 230 | if (!couldPeek) { |
| 231 | REPORTER_ASSERT(r, peeked + i > limit); |
| 232 | // No more peeking is supported. |
| 233 | break; |
| 234 | } |
| 235 | peeked += i; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | static void test_peeking_front_buffered_stream(skiatest::Reporter* r, |
| 240 | const SkStream& original, |
| 241 | size_t bufferSize) { |
| 242 | SkStream* dupe = original.duplicate(); |
| 243 | REPORTER_ASSERT(r, dupe != NULL); |
| 244 | SkAutoTDelete<SkStream> bufferedStream(SkFrontBufferedStream::Create(dupe, bufferSize)); |
| 245 | REPORTER_ASSERT(r, bufferedStream != NULL); |
| 246 | test_peeking_stream(r, bufferedStream, bufferSize); |
| 247 | } |
| 248 | |
stephana | 195f62d | 2015-04-09 07:57:54 -0700 | [diff] [blame] | 249 | // This test uses file system operations that don't work out of the |
| 250 | // box on iOS. It's likely that we don't need them on iOS. Ignoring for now. |
| 251 | // TODO(stephana): Re-evaluate if we need this in the future. |
| 252 | #ifndef SK_BUILD_FOR_IOS |
scroggo | 028a413 | 2015-04-02 13:19:51 -0700 | [diff] [blame] | 253 | DEF_TEST(StreamPeek, reporter) { |
| 254 | // Test a memory stream. |
| 255 | const char gAbcs[] = "abcdefghijklmnopqrstuvwxyz"; |
| 256 | SkMemoryStream memStream(gAbcs, strlen(gAbcs), false); |
| 257 | test_peeking_stream(reporter, &memStream, memStream.getLength()); |
| 258 | |
| 259 | // Test an arbitrary file stream. file streams do not support peeking. |
| 260 | SkFILEStream fileStream(GetResourcePath("baby_tux.webp").c_str()); |
| 261 | REPORTER_ASSERT(reporter, fileStream.isValid()); |
caryclark | 30ac464 | 2015-04-14 06:08:04 -0700 | [diff] [blame] | 262 | if (!fileStream.isValid()) { |
| 263 | return; |
| 264 | } |
scroggo | 028a413 | 2015-04-02 13:19:51 -0700 | [diff] [blame] | 265 | SkAutoMalloc storage(fileStream.getLength()); |
| 266 | for (size_t i = 1; i < fileStream.getLength(); i++) { |
| 267 | REPORTER_ASSERT(reporter, !fileStream.peek(storage.get(), i)); |
| 268 | } |
| 269 | |
| 270 | // Now test some FrontBufferedStreams |
| 271 | for (size_t i = 1; i < memStream.getLength(); i++) { |
| 272 | test_peeking_front_buffered_stream(reporter, memStream, i); |
| 273 | } |
| 274 | } |
stephana | 195f62d | 2015-04-09 07:57:54 -0700 | [diff] [blame] | 275 | #endif |