1cb93a386Sopenharmony_ci// Copyright 2013 The Chromium Authors. All rights reserved.
2cb93a386Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
3cb93a386Sopenharmony_ci// found in the LICENSE file.
4cb93a386Sopenharmony_ci
5cb93a386Sopenharmony_ci#include "include/core/SkCanvas.h"
6cb93a386Sopenharmony_ci#include "SkFlattenableSerialization.h"
7cb93a386Sopenharmony_ci#include "include/core/SkImageFilter.h"
8cb93a386Sopenharmony_ci#include "include/core/SkString.h"
9cb93a386Sopenharmony_ci#include "src/core/SkOSFile.h"
10cb93a386Sopenharmony_ci
11cb93a386Sopenharmony_ci#include <stdio.h>
12cb93a386Sopenharmony_ci
13cb93a386Sopenharmony_cistatic const int kBitmapSize = 24;
14cb93a386Sopenharmony_ci
15cb93a386Sopenharmony_cistatic bool read_test_case(const char* filename, SkString* testdata) {
16cb93a386Sopenharmony_ci  FILE* file = sk_fopen(filename, kRead_SkFILE_Flag);
17cb93a386Sopenharmony_ci  if (!file) {
18cb93a386Sopenharmony_ci    SkDebugf("couldn't open file %s\n", filename);
19cb93a386Sopenharmony_ci    return false;
20cb93a386Sopenharmony_ci  }
21cb93a386Sopenharmony_ci  size_t len = sk_fgetsize(file);
22cb93a386Sopenharmony_ci  if (!len) {
23cb93a386Sopenharmony_ci    SkDebugf("couldn't read file %s\n", filename);
24cb93a386Sopenharmony_ci    return false;
25cb93a386Sopenharmony_ci  }
26cb93a386Sopenharmony_ci  testdata->resize(len);
27cb93a386Sopenharmony_ci  (void) fread(testdata->writable_str(), len, file);
28cb93a386Sopenharmony_ci  return true;
29cb93a386Sopenharmony_ci}
30cb93a386Sopenharmony_ci
31cb93a386Sopenharmony_cistatic void run_test_case(const SkString& testdata, const SkBitmap& bitmap,
32cb93a386Sopenharmony_ci                          SkCanvas* canvas) {
33cb93a386Sopenharmony_ci  // This call shouldn't crash or cause ASAN to flag any memory issues
34cb93a386Sopenharmony_ci  // If nothing bad happens within this call, everything is fine
35cb93a386Sopenharmony_ci  sk_sp<SkImageFilter> flattenable = SkValidatingDeserializeImageFilter(testdata.c_str(),
36cb93a386Sopenharmony_ci                                                                        testdata.size());
37cb93a386Sopenharmony_ci
38cb93a386Sopenharmony_ci  // Adding some info, but the test passed if we got here without any trouble
39cb93a386Sopenharmony_ci  if (flattenable != nullptr) {
40cb93a386Sopenharmony_ci    SkDebugf("Valid stream detected.\n");
41cb93a386Sopenharmony_ci    // Let's see if using the filters can cause any trouble...
42cb93a386Sopenharmony_ci    SkPaint paint;
43cb93a386Sopenharmony_ci    paint.setImageFilter(flattenable);
44cb93a386Sopenharmony_ci    canvas->save();
45cb93a386Sopenharmony_ci    canvas->clipRect(SkRect::MakeXYWH(
46cb93a386Sopenharmony_ci        0, 0, SkIntToScalar(kBitmapSize), SkIntToScalar(kBitmapSize)));
47cb93a386Sopenharmony_ci
48cb93a386Sopenharmony_ci    // This call shouldn't crash or cause ASAN to flag any memory issues
49cb93a386Sopenharmony_ci    // If nothing bad happens within this call, everything is fine
50cb93a386Sopenharmony_ci    canvas->drawBitmap(bitmap, 0, 0, &paint);
51cb93a386Sopenharmony_ci
52cb93a386Sopenharmony_ci    SkDebugf("Filter DAG rendered successfully.\n");
53cb93a386Sopenharmony_ci    canvas->restore();
54cb93a386Sopenharmony_ci  } else {
55cb93a386Sopenharmony_ci    SkDebugf("Invalid stream detected.\n");
56cb93a386Sopenharmony_ci  }
57cb93a386Sopenharmony_ci}
58cb93a386Sopenharmony_ci
59cb93a386Sopenharmony_cistatic bool read_and_run_test_case(const char* filename, const SkBitmap& bitmap,
60cb93a386Sopenharmony_ci                        SkCanvas* canvas) {
61cb93a386Sopenharmony_ci  SkString testdata;
62cb93a386Sopenharmony_ci  SkDebugf("Test case: %s\n", filename);
63cb93a386Sopenharmony_ci  // read_test_case will print a useful error message if it fails.
64cb93a386Sopenharmony_ci  if (!read_test_case(filename, &testdata))
65cb93a386Sopenharmony_ci    return false;
66cb93a386Sopenharmony_ci  run_test_case(testdata, bitmap, canvas);
67cb93a386Sopenharmony_ci  return true;
68cb93a386Sopenharmony_ci}
69cb93a386Sopenharmony_ci
70cb93a386Sopenharmony_ciint main(int argc, char** argv) {
71cb93a386Sopenharmony_ci  int ret = 0;
72cb93a386Sopenharmony_ci  SkBitmap bitmap;
73cb93a386Sopenharmony_ci  bitmap.allocN32Pixels(kBitmapSize, kBitmapSize);
74cb93a386Sopenharmony_ci  SkCanvas canvas(bitmap);
75cb93a386Sopenharmony_ci  canvas.clear(0x00000000);
76cb93a386Sopenharmony_ci  for (int i = 1; i < argc; i++)
77cb93a386Sopenharmony_ci    if (!read_and_run_test_case(argv[i], bitmap, &canvas))
78cb93a386Sopenharmony_ci      ret = 2;
79cb93a386Sopenharmony_ci  // Cluster-Fuzz likes "#EOF" as the last line of output to help distinguish
80cb93a386Sopenharmony_ci  // successful runs from crashes.
81cb93a386Sopenharmony_ci  SkDebugf("#EOF\n");
82cb93a386Sopenharmony_ci  return ret;
83cb93a386Sopenharmony_ci}
84