caryclark | 7da2e55 | 2015-05-12 08:36:48 -0700 | [diff] [blame] | 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) { |
halcanary | d76be9c | 2015-11-20 13:47:49 -0800 | [diff] [blame] | 14 | FILE* file = sk_fopen(filename, kRead_SkFILE_Flag); |
caryclark | 7da2e55 | 2015-05-12 08:36:48 -0700 | [diff] [blame] | 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, |
Mike Reed | 5e25717 | 2016-11-01 11:22:05 -0400 | [diff] [blame] | 30 | SkCanvas* canvas) { |
caryclark | 7da2e55 | 2015-05-12 08:36:48 -0700 | [diff] [blame] | 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 |
Mike Reed | 5e25717 | 2016-11-01 11:22:05 -0400 | [diff] [blame] | 33 | sk_sp<SkImageFilter> flattenable = SkValidatingDeserializeImageFilter(testdata.c_str(), |
| 34 | testdata.size()); |
caryclark | 7da2e55 | 2015-05-12 08:36:48 -0700 | [diff] [blame] | 35 | |
| 36 | // Adding some info, but the test passed if we got here without any trouble |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 37 | if (flattenable != nullptr) { |
caryclark | 7da2e55 | 2015-05-12 08:36:48 -0700 | [diff] [blame] | 38 | SkDebugf("Valid stream detected.\n"); |
| 39 | // Let's see if using the filters can cause any trouble... |
| 40 | SkPaint paint; |
Mike Reed | 5e25717 | 2016-11-01 11:22:05 -0400 | [diff] [blame] | 41 | paint.setImageFilter(flattenable); |
caryclark | 7da2e55 | 2015-05-12 08:36:48 -0700 | [diff] [blame] | 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 | } |