File: image_decoder_base_test.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (231 lines) | stat: -rw-r--r-- 8,335 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/351564777): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include "third_party/blink/renderer/platform/image-decoders/image_decoder_base_test.h"

#include <stddef.h>

#include <memory>

#include "base/files/file_enumerator.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/hash/md5.h"
#include "base/logging.h"
#include "base/path_service.h"
#include "base/strings/pattern.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "third_party/blink/renderer/platform/image-decoders/image_decoder.h"

// Uncomment this to recalculate the MD5 sums; see header comments.
// #define CALCULATE_MD5_SUMS

namespace {

const int kFirstFrameIndex = 0;

// Determine if we should test with file specified by |path| based
// on |file_selection| and the |threshold| for the file size.
bool ShouldSkipFile(const base::FilePath& path,
                    blink::ImageDecoderBaseTest::FileSelection file_selection,
                    const int64_t threshold) {
  if (file_selection == blink::ImageDecoderBaseTest::FileSelection::kAll) {
    return false;
  }

  int64_t image_size = base::GetFileSize(path).value_or(0);
  return (file_selection ==
          blink::ImageDecoderBaseTest::FileSelection::kSmaller) ==
         (image_size > threshold);
}

void ReadFileToVector(const base::FilePath& path, Vector<char>* contents) {
  std::string raw_image_data;
  base::ReadFileToString(path, &raw_image_data);
  contents->resize(raw_image_data.size());
  memcpy(&contents->front(), raw_image_data.data(), raw_image_data.size());
}

base::MD5Digest ComputeMD5Sum(const blink::ImageFrame& frame_buffer) {
  SkBitmap bitmap = frame_buffer.Bitmap();
  base::MD5Digest digest;
  base::MD5Sum(base::span(static_cast<const uint8_t*>(bitmap.getPixels()),
                          bitmap.computeByteSize()),
               &digest);
  return digest;
}

#if defined(CALCULATE_MD5_SUMS)
void SaveMD5Sum(const base::FilePath& path,
                const blink::ImageFrame* frame_buffer) {
  // Calculate MD5 sum.
  ASSERT_TRUE(frame_buffer);
  base::MD5Digest digest = ComputeMD5Sum(*frame_buffer);

  // Write sum to disk.
  ASSERT_TRUE(base::WriteFile(path, base::byte_span_from_ref(digest)));
}
#endif

#if !defined(CALCULATE_MD5_SUMS)
void VerifyImage(blink::ImageDecoder& decoder,
                 const base::FilePath& path,
                 const base::FilePath& md5_sum_path,
                 size_t frame_index) {
  SCOPED_TRACE(path.value());
  // Make sure decoding can complete successfully.
  EXPECT_TRUE(decoder.IsSizeAvailable());
  EXPECT_GE(decoder.FrameCount(), frame_index);
  blink::ImageFrame* const frame_buffer =
      decoder.DecodeFrameBufferAtIndex(frame_index);
  ASSERT_TRUE(frame_buffer);
  EXPECT_EQ(blink::ImageFrame::kFrameComplete, frame_buffer->GetStatus());
  EXPECT_FALSE(decoder.Failed());

  // Calculate MD5 sum.
  base::MD5Digest actual_digest = ComputeMD5Sum(*frame_buffer);

  // Read the MD5 sum off disk.
  std::string file_bytes;
  base::ReadFileToString(md5_sum_path, &file_bytes);
  base::MD5Digest expected_digest;
  ASSERT_EQ(sizeof expected_digest, file_bytes.size());
  memcpy(&expected_digest, file_bytes.data(), sizeof expected_digest);

  // Verify that the sums are the same.
  EXPECT_EQ(0, memcmp(&expected_digest, &actual_digest, sizeof actual_digest));
}
#endif

}  // namespace

namespace blink {

void ImageDecoderBaseTest::SetUp() {
  base::FilePath data_dir;
  ASSERT_TRUE(base::PathService::Get(base::DIR_SRC_TEST_DATA_ROOT, &data_dir));
  data_dir_ = data_dir.AppendASCII("webkit").AppendASCII("data").AppendASCII(
      format_.Utf8() + "_decoder");
}

base::FilePath ImageDecoderBaseTest::GetMD5SumPath(const base::FilePath& path) {
  static const base::FilePath::StringType kDecodedDataExtension(
      FILE_PATH_LITERAL(".md5sum"));
  return base::FilePath(path.value() + kDecodedDataExtension);
}

Vector<base::FilePath> ImageDecoderBaseTest::GetImageFiles() const {
  Vector<base::FilePath> image_files;
  if (!base::PathExists(data_dir_)) {
    return image_files;
  }
  std::string pattern = "*." + format_.Utf8();
  base::FileEnumerator enumerator(data_dir_, false,
                                  base::FileEnumerator::FILES);
  for (base::FilePath next_file_name = enumerator.Next();
       !next_file_name.empty(); next_file_name = enumerator.Next()) {
    base::FilePath base_name = next_file_name.BaseName();
    std::string base_name_ascii = base_name.MaybeAsASCII();
    if (base::MatchPattern(base_name_ascii, pattern)) {
      image_files.push_back(next_file_name);
    }
  }

  return image_files;
}

bool ImageDecoderBaseTest::ShouldImageFail(const base::FilePath& path) const {
  const base::FilePath::StringType kBadSuffix(FILE_PATH_LITERAL(".bad."));
  return (path.value().length() > (kBadSuffix.length() + format_.length()) &&
          !path.value().compare(
              path.value().length() - format_.length() - kBadSuffix.length(),
              kBadSuffix.length(), kBadSuffix));
}

void ImageDecoderBaseTest::TestDecoding(
    blink::ImageDecoderBaseTest::FileSelection file_selection,
    const int64_t threshold) {
  const Vector<base::FilePath> image_files = GetImageFiles();
  if (image_files.empty()) {
    const testing::TestInfo* const test_info =
        testing::UnitTest::GetInstance()->current_test_info();
    VLOG(0) << "TestDecoding() in " << test_info->test_suite_name() << "."
            << test_info->name()
            << " not running because test data wasn't found.";
    return;
  }
  for (const base::FilePath& file : image_files) {
    if (!ShouldSkipFile(file, file_selection, threshold)) {
      TestImageDecoder(file, GetMD5SumPath(file), kFirstFrameIndex);
    }
  }
}

void ImageDecoderBaseTest::TestImageDecoder(const base::FilePath& image_path,
                                            const base::FilePath& md5_sum_path,
                                            int desired_frame_index) const {
#if defined(CALCULATE_MD5_SUMS)
  // If we're just calculating the MD5 sums, skip failing images quickly.
  if (ShouldImageFail(image_path)) {
    return;
  }
#endif

  CHECK(base::PathExists(image_path)) << image_path;
  CHECK(ShouldImageFail(image_path) || base::PathExists(md5_sum_path))
      << md5_sum_path;
  Vector<char> image_contents;
  ReadFileToVector(image_path, &image_contents);
  EXPECT_TRUE(image_contents.size());
  std::unique_ptr<ImageDecoder> decoder(CreateImageDecoder());
  EXPECT_FALSE(decoder->Failed());

#if !defined(CALCULATE_MD5_SUMS)
  // Test chunking file into half.
  const size_t partial_size = image_contents.size() / 2;

  scoped_refptr<SharedBuffer> partial_data =
      SharedBuffer::Create(base::span(image_contents).first(partial_size));

  // Make Sure the image decoder doesn't fail when we ask for the frame
  // buffer for this partial image.
  // NOTE: We can't check that frame 0 is non-NULL, because if this is an
  // ICO and we haven't yet supplied enough data to read the directory,
  // there is no framecount and thus no first frame.
  decoder->SetData(partial_data, false);
  EXPECT_FALSE(decoder->Failed()) << image_path.value();
#endif

  // Make sure passing the complete image results in successful decoding.
  scoped_refptr<SharedBuffer> data = SharedBuffer::Create(image_contents);
  decoder->SetData(data, true);
  if (ShouldImageFail(image_path)) {
    blink::ImageFrame* const frame_buffer =
        decoder->DecodeFrameBufferAtIndex(kFirstFrameIndex);
    if (kFirstFrameIndex < decoder->FrameCount()) {
      EXPECT_TRUE(frame_buffer);
      EXPECT_NE(blink::ImageFrame::kFrameComplete, frame_buffer->GetStatus());
    } else {
      EXPECT_FALSE(frame_buffer);
    }
    EXPECT_TRUE(decoder->Failed());
  } else {
    EXPECT_FALSE(decoder->Failed()) << image_path.value();
#if defined(CALCULATE_MD5_SUMS)
    SaveMD5Sum(md5_sum_path,
               decoder->DecodeFrameBufferAtIndex(desired_frame_index));
#else
    VerifyImage(*decoder, image_path, md5_sum_path, desired_frame_index);
#endif
  }
}

}  // namespace blink