Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(215)

Side by Side Diff: tests/SerializationTest.cpp

Issue 61913002: Adding error checks to SkRBuffer (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Fixed serialization tests Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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"
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 writer.setFlags(SkOrderedWriteBuffer::kValidation_Flag); 102 writer.setFlags(SkOrderedWriteBuffer::kValidation_Flag);
103 SerializationUtils<T>::Write(writer, testObj); 103 SerializationUtils<T>::Write(writer, testObj);
104 size_t bytesWritten = writer.bytesWritten(); 104 size_t bytesWritten = writer.bytesWritten();
105 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten); 105 REPORTER_ASSERT(reporter, SkAlign4(bytesWritten) == bytesWritten);
106 106
107 unsigned char dataWritten[1024]; 107 unsigned char dataWritten[1024];
108 writer.writeToMemory(dataWritten); 108 writer.writeToMemory(dataWritten);
109 109
110 // Make sure this fails when it should (test with smaller size, but still mu ltiple of 4) 110 // Make sure this fails when it should (test with smaller size, but still mu ltiple of 4)
111 SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 4); 111 SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 4);
112 const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer.s kip(0)); 112 T obj;
113 SerializationUtils<T>::Read(buffer, testObj); 113 SerializationUtils<T>::Read(buffer, &obj);
114 const unsigned char* peekAfter = static_cast<const unsigned char*>(buffer.sk ip(0)); 114 REPORTER_ASSERT(reporter, !buffer.validate(true));
115 // This should have failed, since the buffer is too small to read a matrix f rom it
116 REPORTER_ASSERT(reporter, peekBefore == peekAfter);
sugoi1 2013/11/08 16:20:06 This test wasn't working properly. In this scenari
117 115
118 // Make sure this succeeds when it should 116 // Make sure this succeeds when it should
119 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten); 117 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten);
120 peekBefore = static_cast<const unsigned char*>(buffer2.skip(0)); 118 const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer2. skip(0));
121 SerializationUtils<T>::Read(buffer2, testObj); 119 T obj2;
122 peekAfter = static_cast<const unsigned char*>(buffer2.skip(0)); 120 SerializationUtils<T>::Read(buffer2, &obj2);
121 const unsigned char* peekAfter = static_cast<const unsigned char*>(buffer2.s kip(0));
123 // This should have succeeded, since there are enough bytes to read this 122 // This should have succeeded, since there are enough bytes to read this
123 REPORTER_ASSERT(reporter, buffer2.validate(true));
124 REPORTER_ASSERT(reporter, static_cast<size_t>(peekAfter - peekBefore) == byt esWritten); 124 REPORTER_ASSERT(reporter, static_cast<size_t>(peekAfter - peekBefore) == byt esWritten);
125 125
126 TestAlignment(testObj, reporter); 126 TestAlignment(testObj, reporter);
127 } 127 }
128 128
129 template<typename T> 129 template<typename T>
130 static void TestArraySerialization(T* data, skiatest::Reporter* reporter) { 130 static void TestArraySerialization(T* data, skiatest::Reporter* reporter) {
131 SkOrderedWriteBuffer writer(1024); 131 SkOrderedWriteBuffer writer(1024);
132 writer.setFlags(SkOrderedWriteBuffer::kValidation_Flag); 132 writer.setFlags(SkOrderedWriteBuffer::kValidation_Flag);
133 SerializationUtils<T>::Write(writer, data, kArraySize); 133 SerializationUtils<T>::Write(writer, data, kArraySize);
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 203
204 // Test readScalarArray 204 // Test readScalarArray
205 { 205 {
206 SkScalar data[kArraySize]; 206 SkScalar data[kArraySize];
207 TestArraySerialization(data, reporter); 207 TestArraySerialization(data, reporter);
208 } 208 }
209 } 209 }
210 210
211 #include "TestClassDef.h" 211 #include "TestClassDef.h"
212 DEFINE_TESTCLASS("Serialization", SerializationClass, Tests) 212 DEFINE_TESTCLASS("Serialization", SerializationClass, Tests)
OLDNEW
« src/core/SkValidatingReadBuffer.cpp ('K') | « src/ports/SkFontConfigInterface_direct.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698