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/file_util.h"
6 #include "base/logging.h"
7 #include "third_party/skia/include/core/SkBitmapDevice.h"
8 #include "third_party/skia/include/core/SkCanvas.h"
9 #include "third_party/skia/include/core/SkFlattenableSerialization.h"
10 #include "third_party/skia/include/core/SkImageFilter.h"
14 static const int BitmapSize = 24;
16 bool ReadTestCase(const char* filename, std::string* ipc_filter_message) {
17 base::FilePath filepath = base::FilePath::FromUTF8Unsafe(filename);
19 if (!base::ReadFileToString(filepath, ipc_filter_message)) {
20 LOG(ERROR) << filename << ": couldn't read file.";
27 void RunTestCase(std::string& ipc_filter_message, SkBitmap& bitmap,
29 // This call shouldn't crash or cause ASAN to flag any memory issues
30 // If nothing bad happens within this call, everything is fine
31 SkFlattenable* flattenable = SkValidatingDeserializeFlattenable(
32 ipc_filter_message.c_str(), ipc_filter_message.size(),
33 SkImageFilter::GetFlattenableType());
35 // Adding some info, but the test passed if we got here without any trouble
36 if (flattenable != NULL) {
37 LOG(INFO) << "Valid stream detected.";
38 // Let's see if using the filters can cause any trouble...
40 paint.setImageFilter(static_cast<SkImageFilter*>(flattenable))->unref();
42 canvas->clipRect(SkRect::MakeXYWH(
43 0, 0, SkIntToScalar(BitmapSize), SkIntToScalar(BitmapSize)));
45 // This call shouldn't crash or cause ASAN to flag any memory issues
46 // If nothing bad happens within this call, everything is fine
47 canvas->drawBitmap(bitmap, 0, 0, &paint);
49 LOG(INFO) << "Filter DAG rendered successfully";
52 LOG(INFO) << "Invalid stream detected.";
56 bool ReadAndRunTestCase(const char* filename, SkBitmap& bitmap,
58 std::string ipc_filter_message;
60 LOG(INFO) << "Test case: " << filename;
62 // ReadTestCase will print a useful error message if it fails.
63 if (!ReadTestCase(filename, &ipc_filter_message))
66 RunTestCase(ipc_filter_message, bitmap, canvas);
73 int main(int argc, char** argv) {
77 bitmap.setConfig(SkBitmap::kARGB_8888_Config, BitmapSize, BitmapSize);
79 SkBitmapDevice device(bitmap);
80 SkCanvas canvas(&device);
81 canvas.clear(0x00000000);
83 for (int i = 1; i < argc; i++)
84 if (!ReadAndRunTestCase(argv[i], bitmap, &canvas))
87 // Cluster-Fuzz likes "#EOF" as the last line of output to help distinguish
88 // successful runs from crashes.