commit-bot@chromium.org | 0251288 | 2013-10-31 18:37:50 +0000 | [diff] [blame] | 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 | |
| 8 | #include "SkOrderedWriteBuffer.h" |
| 9 | #include "SkValidatingReadBuffer.h" |
| 10 | #include "Test.h" |
| 11 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame^] | 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) == bytesWrittenToMemory); |
| 20 | size_t bytesReadFromMemory = testObj->readFromMemory(dataWritten, bytesWrittenToMemory); |
| 21 | REPORTER_ASSERT(reporter, SkAlign4(bytesReadFromMemory) == bytesReadFromMemory); |
| 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) { |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame] | 56 | writer.writeByteArray(data, arraySize); |
sugoi@google.com | 305f78e | 2013-11-04 16:18:15 +0000 | [diff] [blame] | 57 | } |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame^] | 58 | static bool Read(SkValidatingReadBuffer& reader, unsigned char* data, uint32_t arraySize) { |
| 59 | return reader.readByteArray(data, arraySize); |
| 60 | } |
| 61 | }; |
commit-bot@chromium.org | 0251288 | 2013-10-31 18:37:50 +0000 | [diff] [blame] | 62 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame^] | 63 | template<> struct SerializationUtils<SkColor> { |
| 64 | static void Write(SkOrderedWriteBuffer& writer, SkColor* data, uint32_t arraySize) { |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame] | 65 | writer.writeColorArray(data, arraySize); |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 66 | } |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame^] | 67 | static bool Read(SkValidatingReadBuffer& reader, SkColor* data, uint32_t arraySize) { |
| 68 | return reader.readColorArray(data, arraySize); |
| 69 | } |
| 70 | }; |
sugoi@google.com | 305f78e | 2013-11-04 16:18:15 +0000 | [diff] [blame] | 71 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame^] | 72 | template<> struct SerializationUtils<int32_t> { |
| 73 | static void Write(SkOrderedWriteBuffer& writer, int32_t* data, uint32_t arraySize) { |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame] | 74 | writer.writeIntArray(data, arraySize); |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 75 | } |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame^] | 76 | static bool Read(SkValidatingReadBuffer& reader, int32_t* data, uint32_t arraySize) { |
| 77 | return reader.readIntArray(data, arraySize); |
| 78 | } |
| 79 | }; |
sugoi@google.com | 305f78e | 2013-11-04 16:18:15 +0000 | [diff] [blame] | 80 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame^] | 81 | template<> struct SerializationUtils<SkPoint> { |
| 82 | static void Write(SkOrderedWriteBuffer& writer, SkPoint* data, uint32_t arraySize) { |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame] | 83 | writer.writePointArray(data, arraySize); |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame^] | 84 | } |
| 85 | static bool Read(SkValidatingReadBuffer& reader, SkPoint* data, uint32_t arraySize) { |
| 86 | return reader.readPointArray(data, arraySize); |
| 87 | } |
| 88 | }; |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame] | 89 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame^] | 90 | template<> struct SerializationUtils<SkScalar> { |
| 91 | static void Write(SkOrderedWriteBuffer& writer, SkScalar* data, uint32_t arraySize) { |
| 92 | writer.writeScalarArray(data, arraySize); |
| 93 | } |
| 94 | static bool Read(SkValidatingReadBuffer& reader, SkScalar* data, uint32_t arraySize) { |
| 95 | return reader.readScalarArray(data, arraySize); |
| 96 | } |
| 97 | }; |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame] | 98 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame^] | 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); |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame] | 106 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame^] | 107 | unsigned char dataWritten[1024]; |
| 108 | writer.writeToMemory(dataWritten); |
| 109 | |
| 110 | // Make sure this fails when it should (test with smaller size, but still multiple of 4) |
| 111 | SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 4); |
| 112 | const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer.skip(0)); |
| 113 | SerializationUtils<T>::Read(buffer, testObj); |
| 114 | const unsigned char* peekAfter = static_cast<const unsigned char*>(buffer.skip(0)); |
| 115 | // This should have failed, since the buffer is too small to read a matrix from 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) == bytesWritten); |
| 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 | |
| 155 | static void Tests(skiatest::Reporter* reporter) { |
| 156 | // Test matrix serialization |
| 157 | { |
| 158 | SkMatrix matrix = SkMatrix::I(); |
| 159 | TestObjectSerialization(&matrix, reporter); |
| 160 | } |
| 161 | |
| 162 | // Test path serialization |
| 163 | { |
| 164 | SkPath path; |
| 165 | TestObjectSerialization(&path, reporter); |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 166 | } |
sugoi@google.com | 305f78e | 2013-11-04 16:18:15 +0000 | [diff] [blame] | 167 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame^] | 168 | // Test region serialization |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 169 | { |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame^] | 170 | SkRegion region; |
| 171 | TestObjectSerialization(®ion, reporter); |
| 172 | } |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 173 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame^] | 174 | // Test rrect serialization |
| 175 | { |
| 176 | SkRRect rrect; |
| 177 | TestAlignment(&rrect, reporter); |
| 178 | } |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 179 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame^] | 180 | // Test readByteArray |
| 181 | { |
| 182 | unsigned char data[kArraySize] = {0}; |
| 183 | TestArraySerialization(data, reporter); |
| 184 | } |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 185 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame^] | 186 | // Test readColorArray |
| 187 | { |
| 188 | SkColor data[kArraySize]; |
| 189 | TestArraySerialization(data, reporter); |
| 190 | } |
| 191 | |
| 192 | // Test readIntArray |
| 193 | { |
| 194 | int32_t data[kArraySize]; |
| 195 | TestArraySerialization(data, reporter); |
| 196 | } |
| 197 | |
| 198 | // Test readPointArray |
| 199 | { |
| 200 | SkPoint data[kArraySize]; |
| 201 | TestArraySerialization(data, reporter); |
| 202 | } |
| 203 | |
| 204 | // Test readScalarArray |
| 205 | { |
| 206 | SkScalar data[kArraySize]; |
| 207 | TestArraySerialization(data, reporter); |
commit-bot@chromium.org | 0251288 | 2013-10-31 18:37:50 +0000 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | |
| 211 | #include "TestClassDef.h" |
| 212 | DEFINE_TESTCLASS("Serialization", SerializationClass, Tests) |