Home | History | Annotate | Download | only in tools
      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 "SkCanvas.h"
      6 #include "SkFlattenableSerialization.h"
      7 #include "SkImageFilter.h"
      8 #include "SkOSFile.h"
      9 #include "SkString.h"
     10 
     11 static const int kBitmapSize = 24;
     12 
     13 static bool read_test_case(const char* filename, SkString* testdata) {
     14   FILE* file = sk_fopen(filename, kRead_SkFILE_Flag);
     15   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 
     29 static void run_test_case(const SkString& testdata, const SkBitmap& bitmap,
     30                  SkCanvas* canvas) {
     31   // This call shouldn't crash or cause ASAN to flag any memory issues
     32   // If nothing bad happens within this call, everything is fine
     33   SkFlattenable* flattenable = SkValidatingDeserializeFlattenable(
     34         testdata.c_str(), testdata.size(), SkImageFilter::GetFlattenableType());
     35 
     36   // Adding some info, but the test passed if we got here without any trouble
     37   if (flattenable != nullptr) {
     38     SkDebugf("Valid stream detected.\n");
     39     // Let's see if using the filters can cause any trouble...
     40     SkPaint paint;
     41     paint.setImageFilter(static_cast<SkImageFilter*>(flattenable))->unref();
     42     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 
     57 static 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 
     68 int 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 }
     82 
     83