blob: 94479662b40e4e6e4a2898d5e4e5c88f0636420b [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
11static const int kBitmapSize = 24;
12
13static bool read_test_case(const char* filename, SkString* testdata) {
halcanaryd76be9c2015-11-20 13:47:49 -080014 FILE* file = sk_fopen(filename, kRead_SkFILE_Flag);
caryclark7da2e552015-05-12 08:36:48 -070015 if (!file) {
16 SkDebugf("couldn't open file %s\n", filename);
17 return false;
18 }
19 size_t len = sk_fgetsize(file);
20 if (!len) {
21 SkDebugf("couldn't read file %s\n", filename);
22 return false;
23 }
24 testdata->resize(len);
25 (void) sk_fread(testdata->writable_str(), len, file);
26 return true;
27}
28
29static void run_test_case(const SkString& testdata, const SkBitmap& bitmap,
Mike Reed5e257172016-11-01 11:22:05 -040030 SkCanvas* canvas) {
caryclark7da2e552015-05-12 08:36:48 -070031 // This call shouldn't crash or cause ASAN to flag any memory issues
32 // If nothing bad happens within this call, everything is fine
Mike Reed5e257172016-11-01 11:22:05 -040033 sk_sp<SkImageFilter> flattenable = SkValidatingDeserializeImageFilter(testdata.c_str(),
34 testdata.size());
caryclark7da2e552015-05-12 08:36:48 -070035
36 // Adding some info, but the test passed if we got here without any trouble
halcanary96fcdcc2015-08-27 07:41:13 -070037 if (flattenable != nullptr) {
caryclark7da2e552015-05-12 08:36:48 -070038 SkDebugf("Valid stream detected.\n");
39 // Let's see if using the filters can cause any trouble...
40 SkPaint paint;
Mike Reed5e257172016-11-01 11:22:05 -040041 paint.setImageFilter(flattenable);
caryclark7da2e552015-05-12 08:36:48 -070042 canvas->save();
43 canvas->clipRect(SkRect::MakeXYWH(
44 0, 0, SkIntToScalar(kBitmapSize), SkIntToScalar(kBitmapSize)));
45
46 // This call shouldn't crash or cause ASAN to flag any memory issues
47 // If nothing bad happens within this call, everything is fine
48 canvas->drawBitmap(bitmap, 0, 0, &paint);
49
50 SkDebugf("Filter DAG rendered successfully.\n");
51 canvas->restore();
52 } else {
53 SkDebugf("Invalid stream detected.\n");
54 }
55}
56
57static bool read_and_run_test_case(const char* filename, const SkBitmap& bitmap,
58 SkCanvas* canvas) {
59 SkString testdata;
60 SkDebugf("Test case: %s\n", filename);
61 // read_test_case will print a useful error message if it fails.
62 if (!read_test_case(filename, &testdata))
63 return false;
64 run_test_case(testdata, bitmap, canvas);
65 return true;
66}
67
68int main(int argc, char** argv) {
69 int ret = 0;
70 SkBitmap bitmap;
71 bitmap.allocN32Pixels(kBitmapSize, kBitmapSize);
72 SkCanvas canvas(bitmap);
73 canvas.clear(0x00000000);
74 for (int i = 1; i < argc; i++)
75 if (!read_and_run_test_case(argv[i], bitmap, &canvas))
76 ret = 2;
77 // Cluster-Fuzz likes "#EOF" as the last line of output to help distinguish
78 // successful runs from crashes.
79 SkDebugf("#EOF\n");
80 return ret;
81}