Coverage Report

Created: 2025-07-12 06:45

/src/libavif/tests/gtest/avif_fuzztest_dec.cc
Line
Count
Source (jump to first uncovered line)
1
// Copyright 2023 Google LLC
2
// SPDX-License-Identifier: BSD-2-Clause
3
// Decodes an arbitrary sequence of bytes.
4
5
#include <algorithm>
6
#include <cstdint>
7
8
#include "avif/avif.h"
9
#include "avif_fuzztest_helpers.h"
10
#include "aviftest_helpers.h"
11
#include "fuzztest/fuzztest.h"
12
#include "gtest/gtest.h"
13
14
using ::fuzztest::Arbitrary;
15
16
namespace avif {
17
namespace testutil {
18
namespace {
19
20
//------------------------------------------------------------------------------
21
22
void Decode(const std::string& arbitrary_bytes, bool is_persistent,
23
17.1k
            DecoderPtr decoder) {
24
17.1k
  ASSERT_FALSE(GetSeedDataDirs().empty());  // Make sure seeds are available.
25
26
17.1k
  ImagePtr decoded(avifImageCreateEmpty());
27
17.1k
  ASSERT_NE(decoded, nullptr);
28
29
17.1k
  const uint8_t* data =
30
17.1k
      reinterpret_cast<const uint8_t*>(arbitrary_bytes.data());
31
17.1k
  avifIO* const io = avifIOCreateMemoryReader(data, arbitrary_bytes.size());
32
17.1k
  if (io == nullptr) return;
33
  // The Chrome's avifIO object is not persistent.
34
17.1k
  io->persistent = is_persistent;
35
17.1k
  avifDecoderSetIO(decoder.get(), io);
36
37
17.1k
  if (avifDecoderParse(decoder.get()) != AVIF_RESULT_OK) return;
38
39
28.6k
  for (size_t i = 0; i < decoder->image->numProperties; ++i) {
40
13.8k
    const avifRWData& box_payload = decoder->image->properties[i].boxPayload;
41
    // Each custom property should be found as is in the input bitstream.
42
13.8k
    EXPECT_NE(std::search(data, data + arbitrary_bytes.size(), box_payload.data,
43
13.8k
                          box_payload.data + box_payload.size),
44
13.8k
              data + arbitrary_bytes.size());
45
13.8k
  }
46
47
18.3k
  while (avifDecoderNextImage(decoder.get()) == AVIF_RESULT_OK) {
48
3.65k
    EXPECT_GT(decoder->image->width, 0u);
49
3.65k
    EXPECT_GT(decoder->image->height, 0u);
50
3.65k
  }
51
52
  // Loop once.
53
14.7k
  if (avifDecoderReset(decoder.get()) != AVIF_RESULT_OK) return;
54
15.9k
  while (avifDecoderNextImage(decoder.get()) == AVIF_RESULT_OK) {
55
2.80k
  }
56
13.1k
}
57
58
FUZZ_TEST(DecodeAvifTest, Decode)
59
    .WithDomains(ArbitraryImageWithSeeds({AVIF_APP_FILE_FORMAT_AVIF}),
60
                 Arbitrary<bool>(), ArbitraryAvifDecoder());
61
62
//------------------------------------------------------------------------------
63
64
}  // namespace
65
}  // namespace testutil
66
}  // namespace avif