• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "include/core/SkCanvas.h"
6 #include "SkFlattenableSerialization.h"
7 #include "include/core/SkImageFilter.h"
8 #include "include/core/SkString.h"
9 #include "src/core/SkOSFile.h"
10 
11 #include <stdio.h>
12 
13 static const int kBitmapSize = 24;
14 
read_test_case(const char * filename,SkString * testdata)15 static bool read_test_case(const char* filename, SkString* testdata) {
16   FILE* file = sk_fopen(filename, kRead_SkFILE_Flag);
17   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);
27   (void) fread(testdata->writable_str(), len, file);
28   return true;
29 }
30 
run_test_case(const SkString & testdata,const SkBitmap & bitmap,SkCanvas * canvas)31 static void run_test_case(const SkString& testdata, const SkBitmap& bitmap,
32                           SkCanvas* canvas) {
33   // This call shouldn't crash or cause ASAN to flag any memory issues
34   // If nothing bad happens within this call, everything is fine
35   sk_sp<SkImageFilter> flattenable = SkValidatingDeserializeImageFilter(testdata.c_str(),
36                                                                         testdata.size());
37 
38   // Adding some info, but the test passed if we got here without any trouble
39   if (flattenable != nullptr) {
40     SkDebugf("Valid stream detected.\n");
41     // Let's see if using the filters can cause any trouble...
42     SkPaint paint;
43     paint.setImageFilter(flattenable);
44     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 
read_and_run_test_case(const char * filename,const SkBitmap & bitmap,SkCanvas * canvas)59 static 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 
main(int argc,char ** argv)70 int 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 }
84