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  */
7 
8 #include "include/utils/SkRandom.h"
9 #include "src/core/SkAutoMalloc.h"
10 #include "src/core/SkReadBuffer.h"
11 #include "src/core/SkWriter32.h"
12 #include "tests/Test.h"
13 
check_contents(skiatest::Reporter * reporter,const SkWriter32 & writer,const void * expected,size_t size)14 static void check_contents(skiatest::Reporter* reporter, const SkWriter32& writer,
15                            const void* expected, size_t size) {
16     SkAutoSMalloc<256> storage(size);
17     REPORTER_ASSERT(reporter, writer.bytesWritten() == size);
18     writer.flatten(storage.get());
19     REPORTER_ASSERT(reporter, !memcmp(storage.get(), expected, size));
20 }
21 
22 
test_reserve(skiatest::Reporter * reporter)23 static void test_reserve(skiatest::Reporter* reporter) {
24     // There used to be a bug where we'd assert your first reservation had to
25     // fit in external storage if you used it.  This would crash in debug mode.
26     uint8_t storage[4];
27     SkWriter32 writer(storage, sizeof(storage));
28     writer.reserve(40);
29 }
30 
test_string_null(skiatest::Reporter * reporter)31 static void test_string_null(skiatest::Reporter* reporter) {
32     uint8_t storage[8];
33     SkWriter32 writer(storage, sizeof(storage));
34 
35     // Can we write nullptr?
36     writer.writeString(nullptr);
37     const int32_t expected[] = { 0x0, 0x0 };
38     check_contents(reporter, writer, expected, sizeof(expected));
39 }
40 
test_rewind(skiatest::Reporter * reporter)41 static void test_rewind(skiatest::Reporter* reporter) {
42     SkSWriter32<32> writer;
43     int32_t array[3] = { 1, 2, 4 };
44 
45     REPORTER_ASSERT(reporter, 0 == writer.bytesWritten());
46     for (size_t i = 0; i < SK_ARRAY_COUNT(array); ++i) {
47         writer.writeInt(array[i]);
48     }
49     check_contents(reporter, writer, array, sizeof(array));
50 
51     writer.rewindToOffset(2*sizeof(int32_t));
52     REPORTER_ASSERT(reporter, sizeof(array) - 4 == writer.bytesWritten());
53     writer.writeInt(3);
54     REPORTER_ASSERT(reporter, sizeof(array) == writer.bytesWritten());
55     array[2] = 3;
56     check_contents(reporter, writer, array, sizeof(array));
57 
58     // test rewinding past allocated chunks. This used to crash because we
59     // didn't truncate our link-list after freeing trailing blocks
60     {
61         SkWriter32 writer;
62         for (int i = 0; i < 100; ++i) {
63             writer.writeInt(i);
64         }
65         REPORTER_ASSERT(reporter, 100*4 == writer.bytesWritten());
66         for (int j = 100*4; j >= 0; j -= 16) {
67             writer.rewindToOffset(j);
68         }
69         REPORTER_ASSERT(reporter, writer.bytesWritten() < 16);
70     }
71 }
72 
test1(skiatest::Reporter * reporter,SkWriter32 * writer)73 static void test1(skiatest::Reporter* reporter, SkWriter32* writer) {
74     const uint32_t data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
75     for (size_t i = 0; i < SK_ARRAY_COUNT(data); ++i) {
76         REPORTER_ASSERT(reporter, i*4 == writer->bytesWritten());
77         writer->write32(data[i]);
78         REPORTER_ASSERT(reporter, data[i] == writer->readTAt<uint32_t>(i * 4));
79     }
80 
81     char buffer[sizeof(data)];
82     REPORTER_ASSERT(reporter, sizeof(buffer) == writer->bytesWritten());
83     writer->flatten(buffer);
84     REPORTER_ASSERT(reporter, !memcmp(data, buffer, sizeof(buffer)));
85 }
86 
testWritePad(skiatest::Reporter * reporter,SkWriter32 * writer)87 static void testWritePad(skiatest::Reporter* reporter, SkWriter32* writer) {
88     // Create some random data to write.
89     const size_t dataSize = 10;
90 
91     SkAutoTMalloc<uint32_t> originalData(dataSize);
92     {
93         SkRandom rand(0);
94         for (size_t i = 0; i < dataSize; i++) {
95             originalData[(int) i] = rand.nextU();
96         }
97 
98         // Write  the random data to the writer at different lengths for
99         // different alignments.
100         for (size_t len = 0; len < dataSize; len++) {
101             writer->writePad(originalData.get(), len);
102         }
103     }
104 
105     size_t totalBytes = writer->bytesWritten();
106 
107     SkAutoMalloc readStorage(totalBytes);
108     writer->flatten(readStorage.get());
109 
110     SkReadBuffer reader(readStorage.get(), totalBytes);
111 
112     for (size_t len = 0; len < dataSize; len++) {
113         const char* readPtr = static_cast<const char*>(reader.skip(len));
114         // Ensure that the data read is the same as what was written.
115         REPORTER_ASSERT(reporter, memcmp(readPtr, originalData.get(), len) == 0);
116         // Ensure that the rest is padded with zeroes.
117         const char* stop = readPtr + SkAlign4(len);
118         readPtr += len;
119         while (readPtr < stop) {
120             REPORTER_ASSERT(reporter, *readPtr++ == 0);
121         }
122     }
123 }
124 
testOverwriteT(skiatest::Reporter * reporter,SkWriter32 * writer)125 static void testOverwriteT(skiatest::Reporter* reporter, SkWriter32* writer) {
126     const size_t padding = 64;
127 
128     const uint32_t uint1 = 0x12345678;
129     const uint32_t uint2 = 0x98765432;
130     const SkScalar scalar1 = 1234.5678f;
131     const SkScalar scalar2 = 9876.5432f;
132     const SkRect rect1 = SkRect::MakeXYWH(1, 2, 3, 4);
133     const SkRect rect2 = SkRect::MakeXYWH(5, 6, 7, 8);
134 
135     for (size_t i = 0; i < (padding / 4); ++i) {
136         writer->write32(0);
137     }
138 
139     writer->write32(uint1);
140     writer->writeRect(rect1);
141     writer->writeScalar(scalar1);
142 
143     for (size_t i = 0; i < (padding / 4); ++i) {
144         writer->write32(0);
145     }
146 
147     REPORTER_ASSERT(reporter, writer->readTAt<uint32_t>(padding) == uint1);
148     REPORTER_ASSERT(reporter, writer->readTAt<SkRect>(padding + sizeof(uint32_t)) == rect1);
149     REPORTER_ASSERT(reporter, writer->readTAt<SkScalar>(
150                         padding + sizeof(uint32_t) + sizeof(SkRect)) == scalar1);
151 
152     writer->overwriteTAt(padding, uint2);
153     writer->overwriteTAt(padding + sizeof(uint32_t), rect2);
154     writer->overwriteTAt(padding + sizeof(uint32_t) + sizeof(SkRect), scalar2);
155 
156     REPORTER_ASSERT(reporter, writer->readTAt<uint32_t>(padding) == uint2);
157     REPORTER_ASSERT(reporter, writer->readTAt<SkRect>(padding + sizeof(uint32_t)) == rect2);
158     REPORTER_ASSERT(reporter, writer->readTAt<SkScalar>(
159                         padding + sizeof(uint32_t) + sizeof(SkRect)) == scalar2);
160 }
161 
DEF_TEST(Writer32_dynamic,reporter)162 DEF_TEST(Writer32_dynamic, reporter) {
163     SkWriter32 writer;
164     test1(reporter, &writer);
165 
166     writer.reset();
167     testWritePad(reporter, &writer);
168 
169     writer.reset();
170     testOverwriteT(reporter, &writer);
171 }
172 
DEF_TEST(Writer32_small,reporter)173 DEF_TEST(Writer32_small, reporter) {
174     SkSWriter32<8 * sizeof(intptr_t)> writer;
175     test1(reporter, &writer);
176 
177     writer.reset(); // should just rewind our storage
178     testWritePad(reporter, &writer);
179 
180     writer.reset();
181     testOverwriteT(reporter, &writer);
182 }
183 
DEF_TEST(Writer32_large,reporter)184 DEF_TEST(Writer32_large, reporter) {
185     SkSWriter32<1024 * sizeof(intptr_t)> writer;
186     test1(reporter, &writer);
187 
188     writer.reset(); // should just rewind our storage
189     testWritePad(reporter, &writer);
190 
191     writer.reset();
192     testOverwriteT(reporter, &writer);
193 }
194 
DEF_TEST(Writer32_misc,reporter)195 DEF_TEST(Writer32_misc, reporter) {
196     test_reserve(reporter);
197     test_string_null(reporter);
198     test_rewind(reporter);
199 }
200 
DEF_TEST(Writer32_data,reporter)201 DEF_TEST(Writer32_data, reporter) {
202     const char* str = "0123456789";
203     sk_sp<SkData> data0(SkData::MakeWithCString(str));
204     sk_sp<SkData> data1(SkData::MakeEmpty());
205 
206     const size_t sizes[] = {
207         SkWriter32::WriteDataSize(nullptr),
208         SkWriter32::WriteDataSize(data0.get()),
209         SkWriter32::WriteDataSize(data1.get()),
210     };
211 
212     SkSWriter32<1000> writer;
213     size_t sizeWritten = 0;
214 
215     writer.writeData(nullptr);
216     sizeWritten += sizes[0];
217     REPORTER_ASSERT(reporter, sizeWritten == writer.bytesWritten());
218 
219     writer.writeData(data0.get());
220     sizeWritten += sizes[1];
221     REPORTER_ASSERT(reporter, sizeWritten == writer.bytesWritten());
222 
223     writer.writeData(data1.get());
224     sizeWritten += sizes[2];
225     REPORTER_ASSERT(reporter, sizeWritten == writer.bytesWritten());
226 
227     auto result(writer.snapshotAsData());
228 
229     SkReadBuffer reader(result->data(), result->size());
230     auto d0(reader.readByteArrayAsData()),
231          d1(reader.readByteArrayAsData()),
232          d2(reader.readByteArrayAsData());
233 
234     REPORTER_ASSERT(reporter, 0 == d0->size());
235     REPORTER_ASSERT(reporter, strlen(str)+1 == d1->size());
236     REPORTER_ASSERT(reporter, !memcmp(str, d1->data(), strlen(str)+1));
237     REPORTER_ASSERT(reporter, 0 == d2->size());
238 
239     REPORTER_ASSERT(reporter, reader.offset() == sizeWritten);
240     REPORTER_ASSERT(reporter, reader.eof());
241 }
242