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

Unified 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, 2 months 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 side-by-side diff with in-line comments
Download patch
« include/core/SkReader32.h ('K') | « tests/PathTest.cpp ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tests/SerializationTest.cpp
diff --git a/tests/SerializationTest.cpp b/tests/SerializationTest.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..fa08c833e330ba95a96dae4dd574cfe87f1d9fd4
--- /dev/null
+++ b/tests/SerializationTest.cpp
@@ -0,0 +1,101 @@
+/*
+ * Copyright 2013 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "SkOrderedWriteBuffer.h"
+#include "SkValidatingReadBuffer.h"
+#include "Test.h"
+
+static void Tests(skiatest::Reporter* reporter) {
+ // Test matrix serialization
+ {
+ SkMatrix matrix = SkMatrix::I();
+ SkOrderedWriteBuffer writer(1024);
+ writer.setFlags(SkOrderedWriteBuffer::kValidation_Flag);
+ writer.writeMatrix(matrix);
+ uint32_t bytesWritten = writer.bytesWritten();
+ // This should write the length (in 4 bytes) and the array
+ REPORTER_ASSERT(reporter, (9 * sizeof(SkScalar)) == bytesWritten);
+
+ unsigned char dataWritten[1024];
+ writer.writeToMemory(dataWritten);
+
+ // Make sure this fails when it should
+ SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 1);
+ const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer.skip(0));
+ buffer.readMatrix(&matrix);
+ const unsigned char* peekAfter = static_cast<const unsigned char*>(buffer.skip(0));
+ // This should have failed, since the buffer is too small to read a matrix from it
+ REPORTER_ASSERT(reporter, peekBefore == peekAfter);
+
+ // Make sure this succeeds when it should
+ SkValidatingReadBuffer buffer2(dataWritten, bytesWritten);
+ peekBefore = static_cast<const unsigned char*>(buffer2.skip(0));
+ buffer2.readMatrix(&matrix);
+ peekAfter = static_cast<const unsigned char*>(buffer2.skip(0));
+ // This should have succeeded, since there are enough bytes to read this
+ REPORTER_ASSERT(reporter, (peekAfter - peekBefore) == bytesWritten);
+ }
+
+ // Test path serialization
+ {
+ SkPath path;
+ SkOrderedWriteBuffer writer(1024);
+ writer.setFlags(SkOrderedWriteBuffer::kValidation_Flag);
+ writer.writePath(path);
+ uint32_t bytesWritten = writer.bytesWritten();
+
+ unsigned char dataWritten[1024];
+ writer.writeToMemory(dataWritten);
+
+ // Make sure this fails when it should
+ SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 1);
+ const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer.skip(0));
+ buffer.readPath(&path);
+ const unsigned char* peekAfter = static_cast<const unsigned char*>(buffer.skip(0));
+ // This should have failed, since the buffer is too small to read a matrix from it
+ REPORTER_ASSERT(reporter, peekBefore == peekAfter);
+
+ // Make sure this succeeds when it should
+ SkValidatingReadBuffer buffer2(dataWritten, bytesWritten);
+ peekBefore = static_cast<const unsigned char*>(buffer2.skip(0));
+ buffer2.readPath(&path);
+ peekAfter = static_cast<const unsigned char*>(buffer2.skip(0));
+ // This should have succeeded, since there are enough bytes to read this
+ REPORTER_ASSERT(reporter, (peekAfter - peekBefore) == bytesWritten);
+ }
+
+ // Test region serialization
+ {
+ SkRegion region;
+ SkOrderedWriteBuffer writer(1024);
+ writer.setFlags(SkOrderedWriteBuffer::kValidation_Flag);
+ writer.writeRegion(region);
+ uint32_t bytesWritten = writer.bytesWritten();
+
+ unsigned char dataWritten[1024];
+ writer.writeToMemory(dataWritten);
+
+ // Make sure this fails when it should
+ SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 1);
+ const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer.skip(0));
+ buffer.readRegion(&region);
+ const unsigned char* peekAfter = static_cast<const unsigned char*>(buffer.skip(0));
+ // This should have failed, since the buffer is too small to read a matrix from it
+ REPORTER_ASSERT(reporter, peekBefore == peekAfter);
+
+ // Make sure this succeeds when it should
+ SkValidatingReadBuffer buffer2(dataWritten, bytesWritten);
+ peekBefore = static_cast<const unsigned char*>(buffer2.skip(0));
+ buffer2.readRegion(&region);
+ peekAfter = static_cast<const unsigned char*>(buffer2.skip(0));
+ // This should have succeeded, since there are enough bytes to read this
+ REPORTER_ASSERT(reporter, (peekAfter - peekBefore) == bytesWritten);
+ }
+}
+
+#include "TestClassDef.h"
+DEFINE_TESTCLASS("Serialization", SerializationClass, Tests)
« 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