OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkOrderedWriteBuffer.h" | 8 #include "SkOrderedWriteBuffer.h" |
9 #include "SkValidatingReadBuffer.h" | 9 #include "SkValidatingReadBuffer.h" |
10 #include "Test.h" | 10 #include "Test.h" |
11 | 11 |
12 static const uint32_t kArraySize = 64; | |
13 | |
14 template<typename T> | |
15 static void TestAlignment(T* testObj, skiatest::Reporter* reporter) { | |
16 // Test memory read/write functions directly | |
17 unsigned char dataWritten[1024]; | |
18 size_t bytesWrittenToMemory = testObj->writeToMemory(dataWritten); | |
19 REPORTER_ASSERT(reporter, SkAlign4(bytesWrittenToMemory) == bytesWrittenToMe mory); | |
20 size_t bytesReadFromMemory = testObj->readFromMemory(dataWritten, bytesWritt enToMemory); | |
21 REPORTER_ASSERT(reporter, SkAlign4(bytesReadFromMemory) == bytesReadFromMemo ry); | |
22 } | |
23 | |
24 template<typename T> struct SerializationUtils { | |
25 }; | |
26 | |
27 template<> struct SerializationUtils<SkMatrix> { | |
28 static void Write(SkOrderedWriteBuffer& writer, const SkMatrix* matrix) { | |
29 writer.writeMatrix(*matrix); | |
30 } | |
31 static void Read(SkValidatingReadBuffer& reader, SkMatrix* matrix) { | |
32 reader.readMatrix(matrix); | |
33 } | |
34 }; | |
35 | |
36 template<> struct SerializationUtils<SkPath> { | |
37 static void Write(SkOrderedWriteBuffer& writer, const SkPath* path) { | |
38 writer.writePath(*path); | |
39 } | |
40 static void Read(SkValidatingReadBuffer& reader, SkPath* path) { | |
41 reader.readPath(path); | |
42 } | |
43 }; | |
44 | |
45 template<> struct SerializationUtils<SkRegion> { | |
46 static void Write(SkOrderedWriteBuffer& writer, const SkRegion* region) { | |
47 writer.writeRegion(*region); | |
48 } | |
49 static void Read(SkValidatingReadBuffer& reader, SkRegion* region) { | |
50 reader.readRegion(region); | |
51 } | |
52 }; | |
53 | |
54 template<> struct SerializationUtils<unsigned char> { | |
55 static void Write(SkOrderedWriteBuffer& writer, unsigned char* data, uint32_ t arraySize) { | |
56 writer.writeByteArray(data, arraySize); | |
57 } | |
58 static bool Read(SkValidatingReadBuffer& reader, unsigned char* data, uint32 _t arraySize) { | |
59 return reader.readByteArray(data, arraySize); | |
60 } | |
61 }; | |
62 | |
63 template<> struct SerializationUtils<SkColor> { | |
64 static void Write(SkOrderedWriteBuffer& writer, SkColor* data, uint32_t arra ySize) { | |
65 writer.writeColorArray(data, arraySize); | |
66 } | |
67 static bool Read(SkValidatingReadBuffer& reader, SkColor* data, uint32_t arr aySize) { | |
68 return reader.readColorArray(data, arraySize); | |
69 } | |
70 }; | |
71 | |
72 template<> struct SerializationUtils<int32_t> { | |
73 static void Write(SkOrderedWriteBuffer& writer, int32_t* data, uint32_t arra ySize) { | |
74 writer.writeIntArray(data, arraySize); | |
75 } | |
76 static bool Read(SkValidatingReadBuffer& reader, int32_t* data, uint32_t arr aySize) { | |
77 return reader.readIntArray(data, arraySize); | |
78 } | |
79 }; | |
80 | |
81 template<> struct SerializationUtils<SkPoint> { | |
82 static void Write(SkOrderedWriteBuffer& writer, SkPoint* data, uint32_t arra ySize) { | |
83 writer.writePointArray(data, arraySize); | |
84 } | |
85 static bool Read(SkValidatingReadBuffer& reader, SkPoint* data, uint32_t arr aySize) { | |
86 return reader.readPointArray(data, arraySize); | |
87 } | |
88 }; | |
89 | |
90 template<> struct SerializationUtils<SkScalar> { | |
91 static void Write(SkOrderedWriteBuffer& writer, SkScalar* data, uint32_t arr aySize) { | |
92 writer.writeScalarArray(data, arraySize); | |
93 } | |
94 static bool Read(SkValidatingReadBuffer& reader, SkScalar* data, uint32_t ar raySize) { | |
95 return reader.readScalarArray(data, arraySize); | |
96 } | |
97 }; | |
98 | |
99 template<typename T> | |
100 static void TestObjectSerialization(T* testObj, skiatest::Reporter* reporter) { | |
101 SkOrderedWriteBuffer writer(1024); | |
102 writer.setFlags(SkOrderedWriteBuffer::kValidation_Flag); | |
103 SerializationUtils<T>::Write(writer, testObj); | |
104 size_t bytesWritten = writer.bytesWritten(); | |
105 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten); | |
106 | |
107 unsigned char dataWritten[1024]; | |
108 writer.writeToMemory(dataWritten); | |
109 | |
110 // Make sure this fails when it should (test with smaller size, but still mu ltiple of 4) | |
111 SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 4); | |
112 const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer.s kip(0)); | |
113 SerializationUtils<T>::Read(buffer, testObj); | |
114 const unsigned char* peekAfter = static_cast<const unsigned char*>(buffer.sk ip(0)); | |
115 // This should have failed, since the buffer is too small to read a matrix f rom it | |
116 REPORTER_ASSERT(reporter, peekBefore == peekAfter); | |
117 | |
118 // Make sure this succeeds when it should | |
119 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten); | |
120 peekBefore = static_cast<const unsigned char*>(buffer2.skip(0)); | |
121 SerializationUtils<T>::Read(buffer2, testObj); | |
122 peekAfter = static_cast<const unsigned char*>(buffer2.skip(0)); | |
123 // This should have succeeded, since there are enough bytes to read this | |
124 REPORTER_ASSERT(reporter, static_cast<size_t>(peekAfter - peekBefore) == byt esWritten); | |
125 | |
126 TestAlignment(testObj, reporter); | |
127 } | |
128 | |
129 template<typename T> | |
130 static void TestArraySerialization(T* data, skiatest::Reporter* reporter) { | |
131 SkOrderedWriteBuffer writer(1024); | |
132 writer.setFlags(SkOrderedWriteBuffer::kValidation_Flag); | |
133 SerializationUtils<T>::Write(writer, data, kArraySize); | |
134 size_t bytesWritten = writer.bytesWritten(); | |
135 // This should write the length (in 4 bytes) and the array | |
136 REPORTER_ASSERT(reporter, (4 + kArraySize * sizeof(T)) == bytesWritten); | |
137 | |
138 unsigned char dataWritten[1024]; | |
139 writer.writeToMemory(dataWritten); | |
140 | |
141 // Make sure this fails when it should | |
142 SkValidatingReadBuffer buffer(dataWritten, bytesWritten); | |
143 T dataRead[kArraySize]; | |
144 bool success = SerializationUtils<T>::Read(buffer, dataRead, kArraySize / 2) ; | |
145 // This should have failed, since the provided size was too small | |
146 REPORTER_ASSERT(reporter, !success); | |
147 | |
148 // Make sure this succeeds when it should | |
149 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten); | |
150 success = SerializationUtils<T>::Read(buffer2, dataRead, kArraySize); | |
151 // This should have succeeded, since there are enough bytes to read this | |
152 REPORTER_ASSERT(reporter, success); | |
153 } | |
154 | |
12 static void Tests(skiatest::Reporter* reporter) { | 155 static void Tests(skiatest::Reporter* reporter) { |
13 { | 156 // Test matrix serialization |
14 static const uint32_t arraySize = 512; | 157 { |
15 unsigned char data[arraySize] = {0}; | 158 SkMatrix matrix = SkMatrix::I(); |
16 SkOrderedWriteBuffer writer(1024); | 159 TestObjectSerialization(&matrix, reporter); |
17 writer.setFlags(SkOrderedWriteBuffer::kValidation_Flag); | 160 } |
18 writer.writeByteArray(data, arraySize); | 161 |
19 uint32_t bytesWritten = writer.bytesWritten(); | 162 // Test path serialization |
20 // This should write the length (in 4 bytes) and the array | 163 { |
21 REPORTER_ASSERT(reporter, (4 + arraySize) == bytesWritten); | 164 SkPath path; |
22 | 165 TestObjectSerialization(&path, reporter); |
23 unsigned char dataWritten[1024]; | 166 } |
24 writer.writeToMemory(dataWritten); | 167 |
25 | 168 // Test region serialization |
26 // Make sure this fails when it should | 169 { |
27 SkValidatingReadBuffer buffer(dataWritten, bytesWritten); | 170 SkRegion region; |
28 unsigned char dataRead[arraySize]; | 171 TestObjectSerialization(®ion, reporter); |
29 bool success = buffer.readByteArray(dataRead, 256); | 172 } |
30 // This should have failed, since 256 < sizeInBytes | 173 |
31 REPORTER_ASSERT(reporter, !success); | 174 // Test rrect serialization |
32 | 175 { |
33 // Make sure this succeeds when it should | 176 SkRRect rrect; |
34 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten); | 177 TestAlignment(&rrect, reporter); |
bungeman-skia
2013/11/27 17:11:26
This test is causing Valgrind to complain about Sk
| |
35 success = buffer2.readByteArray(dataRead, arraySize); | 178 } |
36 // This should have succeeded, since there are enough bytes to read this | 179 |
37 REPORTER_ASSERT(reporter, success); | 180 // Test readByteArray |
38 } | 181 { |
39 | 182 unsigned char data[kArraySize] = {0}; |
40 { | 183 TestArraySerialization(data, reporter); |
41 static const uint32_t arraySize = 64; | 184 } |
42 SkColor data[arraySize]; | 185 |
43 SkOrderedWriteBuffer writer(1024); | 186 // Test readColorArray |
44 writer.setFlags(SkOrderedWriteBuffer::kValidation_Flag); | 187 { |
45 writer.writeColorArray(data, arraySize); | 188 SkColor data[kArraySize]; |
46 uint32_t bytesWritten = writer.bytesWritten(); | 189 TestArraySerialization(data, reporter); |
47 // This should write the length (in 4 bytes) and the array | 190 } |
48 REPORTER_ASSERT(reporter, (4 + arraySize * sizeof(SkColor)) == bytesWrit ten); | 191 |
49 | 192 // Test readIntArray |
50 unsigned char dataWritten[1024]; | 193 { |
51 writer.writeToMemory(dataWritten); | 194 int32_t data[kArraySize]; |
52 | 195 TestArraySerialization(data, reporter); |
53 // Make sure this fails when it should | 196 } |
54 SkValidatingReadBuffer buffer(dataWritten, bytesWritten); | 197 |
55 SkColor dataRead[arraySize]; | 198 // Test readPointArray |
56 bool success = buffer.readColorArray(dataRead, 32); | 199 { |
57 // This should have failed, since 256 < sizeInBytes | 200 SkPoint data[kArraySize]; |
58 REPORTER_ASSERT(reporter, !success); | 201 TestArraySerialization(data, reporter); |
59 | 202 } |
60 // Make sure this succeeds when it should | 203 |
61 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten); | 204 // Test readScalarArray |
62 success = buffer2.readColorArray(dataRead, arraySize); | 205 { |
63 // This should have succeeded, since there are enough bytes to read this | 206 SkScalar data[kArraySize]; |
64 REPORTER_ASSERT(reporter, success); | 207 TestArraySerialization(data, reporter); |
65 } | 208 } |
66 | 209 } |
67 { | 210 |
68 static const uint32_t arraySize = 64; | |
69 int32_t data[arraySize]; | |
70 SkOrderedWriteBuffer writer(1024); | |
71 writer.setFlags(SkOrderedWriteBuffer::kValidation_Flag); | |
72 writer.writeIntArray(data, arraySize); | |
73 uint32_t bytesWritten = writer.bytesWritten(); | |
74 // This should write the length (in 4 bytes) and the array | |
75 REPORTER_ASSERT(reporter, (4 + arraySize * sizeof(int32_t)) == bytesWrit ten); | |
76 | |
77 unsigned char dataWritten[1024]; | |
78 writer.writeToMemory(dataWritten); | |
79 | |
80 // Make sure this fails when it should | |
81 SkValidatingReadBuffer buffer(dataWritten, bytesWritten); | |
82 int32_t dataRead[arraySize]; | |
83 bool success = buffer.readIntArray(dataRead, 32); | |
84 // This should have failed, since 256 < sizeInBytes | |
85 REPORTER_ASSERT(reporter, !success); | |
86 | |
87 // Make sure this succeeds when it should | |
88 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten); | |
89 success = buffer2.readIntArray(dataRead, arraySize); | |
90 // This should have succeeded, since there are enough bytes to read this | |
91 REPORTER_ASSERT(reporter, success); | |
92 } | |
93 | |
94 { | |
95 static const uint32_t arraySize = 64; | |
96 SkPoint data[arraySize]; | |
97 SkOrderedWriteBuffer writer(1024); | |
98 writer.setFlags(SkOrderedWriteBuffer::kValidation_Flag); | |
99 writer.writePointArray(data, arraySize); | |
100 uint32_t bytesWritten = writer.bytesWritten(); | |
101 // This should write the length (in 4 bytes) and the array | |
102 REPORTER_ASSERT(reporter, (4 + arraySize * sizeof(SkPoint)) == bytesWrit ten); | |
103 | |
104 unsigned char dataWritten[1024]; | |
105 writer.writeToMemory(dataWritten); | |
106 | |
107 // Make sure this fails when it should | |
108 SkValidatingReadBuffer buffer(dataWritten, bytesWritten); | |
109 SkPoint dataRead[arraySize]; | |
110 bool success = buffer.readPointArray(dataRead, 32); | |
111 // This should have failed, since 256 < sizeInBytes | |
112 REPORTER_ASSERT(reporter, !success); | |
113 | |
114 // Make sure this succeeds when it should | |
115 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten); | |
116 success = buffer2.readPointArray(dataRead, arraySize); | |
117 // This should have succeeded, since there are enough bytes to read this | |
118 REPORTER_ASSERT(reporter, success); | |
119 } | |
120 | |
121 { | |
122 static const uint32_t arraySize = 64; | |
123 SkScalar data[arraySize]; | |
124 SkOrderedWriteBuffer writer(1024); | |
125 writer.setFlags(SkOrderedWriteBuffer::kValidation_Flag); | |
126 writer.writeScalarArray(data, arraySize); | |
127 uint32_t bytesWritten = writer.bytesWritten(); | |
128 // This should write the length (in 4 bytes) and the array | |
129 REPORTER_ASSERT(reporter, (4 + arraySize * sizeof(SkScalar)) == bytesWri tten); | |
130 | |
131 unsigned char dataWritten[1024]; | |
132 writer.writeToMemory(dataWritten); | |
133 | |
134 // Make sure this fails when it should | |
135 SkValidatingReadBuffer buffer(dataWritten, bytesWritten); | |
136 SkScalar dataRead[arraySize]; | |
137 bool success = buffer.readScalarArray(dataRead, 32); | |
138 // This should have failed, since 256 < sizeInBytes | |
139 REPORTER_ASSERT(reporter, !success); | |
140 | |
141 // Make sure this succeeds when it should | |
142 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten); | |
143 success = buffer2.readScalarArray(dataRead, arraySize); | |
144 // This should have succeeded, since there are enough bytes to read this | |
145 REPORTER_ASSERT(reporter, success); | |
146 } | |
147 } | |
148 | |
149 #include "TestClassDef.h" | 211 #include "TestClassDef.h" |
150 DEFINE_TESTCLASS("Serialization", SerializationClass, Tests) | 212 DEFINE_TESTCLASS("Serialization", SerializationClass, Tests) |
OLD | NEW |