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 | |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 12 | static void Tests(skiatest::Reporter* reporter) { |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 13 | { |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame^] | 14 | static const uint32_t arraySize = 512; |
| 15 | unsigned char data[arraySize] = {0}; |
| 16 | SkOrderedWriteBuffer writer(1024); |
| 17 | writer.setFlags(SkOrderedWriteBuffer::kValidation_Flag); |
| 18 | writer.writeByteArray(data, arraySize); |
| 19 | uint32_t bytesWritten = writer.bytesWritten(); |
| 20 | // This should write the length (in 4 bytes) and the array |
| 21 | REPORTER_ASSERT(reporter, (4 + arraySize) == bytesWritten); |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 22 | |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame^] | 23 | unsigned char dataWritten[1024]; |
| 24 | writer.writeToMemory(dataWritten); |
| 25 | |
| 26 | // Make sure this fails when it should |
| 27 | SkValidatingReadBuffer buffer(dataWritten, bytesWritten); |
| 28 | unsigned char dataRead[arraySize]; |
| 29 | bool success = buffer.readByteArray(dataRead, 256); |
| 30 | // This should have failed, since 256 < sizeInBytes |
| 31 | REPORTER_ASSERT(reporter, !success); |
| 32 | |
| 33 | // Make sure this succeeds when it should |
| 34 | SkValidatingReadBuffer buffer2(dataWritten, bytesWritten); |
| 35 | success = buffer2.readByteArray(dataRead, arraySize); |
| 36 | // This should have succeeded, since there are enough bytes to read this |
| 37 | REPORTER_ASSERT(reporter, success); |
sugoi@google.com | 305f78e | 2013-11-04 16:18:15 +0000 | [diff] [blame] | 38 | } |
commit-bot@chromium.org | 0251288 | 2013-10-31 18:37:50 +0000 | [diff] [blame] | 39 | |
sugoi@google.com | 305f78e | 2013-11-04 16:18:15 +0000 | [diff] [blame] | 40 | { |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame^] | 41 | static const uint32_t arraySize = 64; |
| 42 | SkColor data[arraySize]; |
| 43 | SkOrderedWriteBuffer writer(1024); |
| 44 | writer.setFlags(SkOrderedWriteBuffer::kValidation_Flag); |
| 45 | writer.writeColorArray(data, arraySize); |
| 46 | uint32_t bytesWritten = writer.bytesWritten(); |
| 47 | // This should write the length (in 4 bytes) and the array |
| 48 | REPORTER_ASSERT(reporter, (4 + arraySize * sizeof(SkColor)) == bytesWritten); |
| 49 | |
| 50 | unsigned char dataWritten[1024]; |
| 51 | writer.writeToMemory(dataWritten); |
| 52 | |
| 53 | // Make sure this fails when it should |
| 54 | SkValidatingReadBuffer buffer(dataWritten, bytesWritten); |
| 55 | SkColor dataRead[arraySize]; |
| 56 | bool success = buffer.readColorArray(dataRead, 32); |
| 57 | // This should have failed, since 256 < sizeInBytes |
| 58 | REPORTER_ASSERT(reporter, !success); |
| 59 | |
| 60 | // Make sure this succeeds when it should |
| 61 | SkValidatingReadBuffer buffer2(dataWritten, bytesWritten); |
| 62 | success = buffer2.readColorArray(dataRead, arraySize); |
| 63 | // This should have succeeded, since there are enough bytes to read this |
| 64 | REPORTER_ASSERT(reporter, success); |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 65 | } |
sugoi@google.com | 305f78e | 2013-11-04 16:18:15 +0000 | [diff] [blame] | 66 | |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 67 | { |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame^] | 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)) == bytesWritten); |
| 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); |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 92 | } |
sugoi@google.com | 305f78e | 2013-11-04 16:18:15 +0000 | [diff] [blame] | 93 | |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 94 | { |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame^] | 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)) == bytesWritten); |
| 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); |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 119 | } |
sugoi@google.com | 305f78e | 2013-11-04 16:18:15 +0000 | [diff] [blame] | 120 | |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 121 | { |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame^] | 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)) == bytesWritten); |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 130 | |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame^] | 131 | unsigned char dataWritten[1024]; |
| 132 | writer.writeToMemory(dataWritten); |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 133 | |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame^] | 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); |
sugoi@google.com | b48a59a | 2013-11-04 20:28:23 +0000 | [diff] [blame] | 140 | |
reed@google.com | 12a2386 | 2013-11-04 21:35:55 +0000 | [diff] [blame^] | 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); |
commit-bot@chromium.org | 0251288 | 2013-10-31 18:37:50 +0000 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
| 149 | #include "TestClassDef.h" |
| 150 | DEFINE_TESTCLASS("Serialization", SerializationClass, Tests) |