1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/files/file_util.h"
6 #include "base/logging.h"
7 #include "third_party/skia/include/core/SkCanvas.h"
8 #include "third_party/skia/include/core/SkFlattenableSerialization.h"
9 #include "third_party/skia/include/core/SkImageFilter.h"
13 static const int BitmapSize = 24;
15 bool ReadTestCase(const char* filename, std::string* ipc_filter_message) {
16 base::FilePath filepath = base::FilePath::FromUTF8Unsafe(filename);
18 if (!base::ReadFileToString(filepath, ipc_filter_message)) {
19 LOG(ERROR) << filename << ": couldn't read file.";
26 void RunTestCase(std::string& ipc_filter_message, SkBitmap& bitmap,
28 // This call shouldn't crash or cause ASAN to flag any memory issues
29 // If nothing bad happens within this call, everything is fine
30 SkFlattenable* flattenable = SkValidatingDeserializeFlattenable(
31 ipc_filter_message.c_str(), ipc_filter_message.size(),
32 SkImageFilter::GetFlattenableType());
34 // Adding some info, but the test passed if we got here without any trouble
35 if (flattenable != NULL) {
36 LOG(INFO) << "Valid stream detected.";
37 // Let's see if using the filters can cause any trouble...
39 paint.setImageFilter(static_cast<SkImageFilter*>(flattenable))->unref();
41 canvas->clipRect(SkRect::MakeXYWH(
42 0, 0, SkIntToScalar(BitmapSize), SkIntToScalar(BitmapSize)));
44 // This call shouldn't crash or cause ASAN to flag any memory issues
45 // If nothing bad happens within this call, everything is fine
46 canvas->drawBitmap(bitmap, 0, 0, &paint);
48 LOG(INFO) << "Filter DAG rendered successfully";
51 LOG(INFO) << "Invalid stream detected.";
55 bool ReadAndRunTestCase(const char* filename, SkBitmap& bitmap,
57 std::string ipc_filter_message;
59 LOG(INFO) << "Test case: " << filename;
61 // ReadTestCase will print a useful error message if it fails.
62 if (!ReadTestCase(filename, &ipc_filter_message))
65 RunTestCase(ipc_filter_message, bitmap, canvas);
72 int main(int argc, char** argv) {
76 bitmap.allocN32Pixels(BitmapSize, BitmapSize);
77 SkCanvas canvas(bitmap);
78 canvas.clear(0x00000000);
80 for (int i = 1; i < argc; i++)
81 if (!ReadAndRunTestCase(argv[i], bitmap, &canvas))
84 // Cluster-Fuzz likes "#EOF" as the last line of output to help distinguish
85 // successful runs from crashes.