blob: fcc0db2f4c0a7eeb0deae4f8213c060cfa87b070 [file] [log] [blame]
caryclark7da2e552015-05-12 08:36:48 -07001// 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.
4
5#include "SkCanvas.h"
6#include "SkFlattenableSerialization.h"
7#include "SkImageFilter.h"
8#include "SkOSFile.h"
9#include "SkString.h"
10
Ben Wagner4d1955c2017-03-10 13:08:15 -050011#include <stdio.h>
12
caryclark7da2e552015-05-12 08:36:48 -070013static const int kBitmapSize = 24;
14
15static bool read_test_case(const char* filename, SkString* testdata) {
halcanaryd76be9c2015-11-20 13:47:49 -080016 FILE* file = sk_fopen(filename, kRead_SkFILE_Flag);
caryclark7da2e552015-05-12 08:36:48 -070017 if (!file) {
18 SkDebugf("couldn't open file %s\n", filename);
19 return false;
20 }
21 size_t len = sk_fgetsize(file);
22 if (!len) {
23 SkDebugf("couldn't read file %s\n", filename);
24 return false;
25 }
26 testdata->resize(len);
Ben Wagner4d1955c2017-03-10 13:08:15 -050027 (void) fread(testdata->writable_str(), len, file);
caryclark7da2e552015-05-12 08:36:48 -070028 return true;
29}
30
31static void run_test_case(const SkString& testdata, const SkBitmap& bitmap,
Mike Reed5e257172016-11-01 11:22:05 -040032 SkCanvas* canvas) {
caryclark7da2e552015-05-12 08:36:48 -070033 // This call shouldn't crash or cause ASAN to flag any memory issues
34 // If nothing bad happens within this call, everything is fine
Mike Reed5e257172016-11-01 11:22:05 -040035 sk_sp<SkImageFilter> flattenable = SkValidatingDeserializeImageFilter(testdata.c_str(),
36 testdata.size());
caryclark7da2e552015-05-12 08:36:48 -070037
38 // Adding some info, but the test passed if we got here without any trouble
halcanary96fcdcc2015-08-27 07:41:13 -070039 if (flattenable != nullptr) {
caryclark7da2e552015-05-12 08:36:48 -070040 SkDebugf("Valid stream detected.\n");
41 // Let's see if using the filters can cause any trouble...
42 SkPaint paint;
Mike Reed5e257172016-11-01 11:22:05 -040043 paint.setImageFilter(flattenable);
caryclark7da2e552015-05-12 08:36:48 -070044 canvas->save();
45 canvas->clipRect(SkRect::MakeXYWH(
46 0, 0, SkIntToScalar(kBitmapSize), SkIntToScalar(kBitmapSize)));
47
48 // This call shouldn't crash or cause ASAN to flag any memory issues
49 // If nothing bad happens within this call, everything is fine
50 canvas->drawBitmap(bitmap, 0, 0, &paint);
51
52 SkDebugf("Filter DAG rendered successfully.\n");
53 canvas->restore();
54 } else {
55 SkDebugf("Invalid stream detected.\n");
56 }
57}
58
59static bool read_and_run_test_case(const char* filename, const SkBitmap& bitmap,
60 SkCanvas* canvas) {
61 SkString testdata;
62 SkDebugf("Test case: %s\n", filename);
63 // read_test_case will print a useful error message if it fails.
64 if (!read_test_case(filename, &testdata))
65 return false;
66 run_test_case(testdata, bitmap, canvas);
67 return true;
68}
69
70int main(int argc, char** argv) {
71 int ret = 0;
72 SkBitmap bitmap;
73 bitmap.allocN32Pixels(kBitmapSize, kBitmapSize);
74 SkCanvas canvas(bitmap);
75 canvas.clear(0x00000000);
76 for (int i = 1; i < argc; i++)
77 if (!read_and_run_test_case(argv[i], bitmap, &canvas))
78 ret = 2;
79 // Cluster-Fuzz likes "#EOF" as the last line of output to help distinguish
80 // successful runs from crashes.
81 SkDebugf("#EOF\n");
82 return ret;
83}