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

Side by Side Diff: tests/SerializationTest.cpp

Issue 41253002: Checking structure sizes before reading them from memory to avoid overflowing the buffer's stream. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Fixed comments and added 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
« include/core/SkReader32.h ('K') | « tests/PathTest.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
12 static void Tests(skiatest::Reporter* reporter) {
13 // Test matrix serialization
14 {
15 SkMatrix matrix = SkMatrix::I();
16 SkOrderedWriteBuffer writer(1024);
17 writer.setFlags(SkOrderedWriteBuffer::kValidation_Flag);
18 writer.writeMatrix(matrix);
19 uint32_t bytesWritten = writer.bytesWritten();
20 // This should write the length (in 4 bytes) and the array
21 REPORTER_ASSERT(reporter, (9 * sizeof(SkScalar)) == bytesWritten);
22
23 unsigned char dataWritten[1024];
24 writer.writeToMemory(dataWritten);
25
26 // Make sure this fails when it should
27 SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 1);
28 const unsigned char* peekBefore = static_cast<const unsigned char*>(buff er.skip(0));
29 buffer.readMatrix(&matrix);
30 const unsigned char* peekAfter = static_cast<const unsigned char*>(buffe r.skip(0));
31 // This should have failed, since the buffer is too small to read a matr ix from it
32 REPORTER_ASSERT(reporter, peekBefore == peekAfter);
33
34 // Make sure this succeeds when it should
35 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten);
36 peekBefore = static_cast<const unsigned char*>(buffer2.skip(0));
37 buffer2.readMatrix(&matrix);
38 peekAfter = static_cast<const unsigned char*>(buffer2.skip(0));
39 // This should have succeeded, since there are enough bytes to read this
40 REPORTER_ASSERT(reporter, (peekAfter - peekBefore) == bytesWritten);
41 }
42
43 // Test path serialization
44 {
45 SkPath path;
46 SkOrderedWriteBuffer writer(1024);
47 writer.setFlags(SkOrderedWriteBuffer::kValidation_Flag);
48 writer.writePath(path);
49 uint32_t bytesWritten = writer.bytesWritten();
50
51 unsigned char dataWritten[1024];
52 writer.writeToMemory(dataWritten);
53
54 // Make sure this fails when it should
55 SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 1);
56 const unsigned char* peekBefore = static_cast<const unsigned char*>(buff er.skip(0));
57 buffer.readPath(&path);
58 const unsigned char* peekAfter = static_cast<const unsigned char*>(buffe r.skip(0));
59 // This should have failed, since the buffer is too small to read a matr ix from it
60 REPORTER_ASSERT(reporter, peekBefore == peekAfter);
61
62 // Make sure this succeeds when it should
63 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten);
64 peekBefore = static_cast<const unsigned char*>(buffer2.skip(0));
65 buffer2.readPath(&path);
66 peekAfter = static_cast<const unsigned char*>(buffer2.skip(0));
67 // This should have succeeded, since there are enough bytes to read this
68 REPORTER_ASSERT(reporter, (peekAfter - peekBefore) == bytesWritten);
69 }
70
71 // Test region serialization
72 {
73 SkRegion region;
74 SkOrderedWriteBuffer writer(1024);
75 writer.setFlags(SkOrderedWriteBuffer::kValidation_Flag);
76 writer.writeRegion(region);
77 uint32_t bytesWritten = writer.bytesWritten();
78
79 unsigned char dataWritten[1024];
80 writer.writeToMemory(dataWritten);
81
82 // Make sure this fails when it should
83 SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 1);
84 const unsigned char* peekBefore = static_cast<const unsigned char*>(buff er.skip(0));
85 buffer.readRegion(&region);
86 const unsigned char* peekAfter = static_cast<const unsigned char*>(buffe r.skip(0));
87 // This should have failed, since the buffer is too small to read a matr ix from it
88 REPORTER_ASSERT(reporter, peekBefore == peekAfter);
89
90 // Make sure this succeeds when it should
91 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten);
92 peekBefore = static_cast<const unsigned char*>(buffer2.skip(0));
93 buffer2.readRegion(&region);
94 peekAfter = static_cast<const unsigned char*>(buffer2.skip(0));
95 // This should have succeeded, since there are enough bytes to read this
96 REPORTER_ASSERT(reporter, (peekAfter - peekBefore) == bytesWritten);
97 }
98 }
99
100 #include "TestClassDef.h"
101 DEFINE_TESTCLASS("Serialization", SerializationClass, Tests)
OLDNEW
« include/core/SkReader32.h ('K') | « tests/PathTest.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698