Coverage Report

Created: 2025-08-26 06:59

/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
22.0k
            DecoderPtr decoder) {
24
22.0k
  ASSERT_FALSE(GetSeedDataDirs().empty());  // Make sure seeds are available.
25
26
22.0k
  ImagePtr decoded(avifImageCreateEmpty());
27
22.0k
  ASSERT_NE(decoded, nullptr);
28
29
22.0k
  const uint8_t* data =
30
22.0k
      reinterpret_cast<const uint8_t*>(arbitrary_bytes.data());
31
22.0k
  avifIO* const io = avifIOCreateMemoryReader(data, arbitrary_bytes.size());
32
22.0k
  if (io == nullptr) return;
33
  // The Chrome's avifIO object is not persistent.
34
22.0k
  io->persistent = is_persistent;
35
22.0k
  avifDecoderSetIO(decoder.get(), io);
36
37
22.0k
  if (avifDecoderParse(decoder.get()) != AVIF_RESULT_OK) return;
38
39
36.5k
  for (size_t i = 0; i < decoder->image->numProperties; ++i) {
40
17.3k
    const avifRWData& box_payload = decoder->image->properties[i].boxPayload;
41
    // Each custom property should be found as is in the input bitstream.
42
17.3k
    EXPECT_NE(std::search(data, data + arbitrary_bytes.size(), box_payload.data,
43
17.3k
                          box_payload.data + box_payload.size),
44
17.3k
              data + arbitrary_bytes.size());
45
17.3k
  }
46
47
23.9k
  while (avifDecoderNextImage(decoder.get()) == AVIF_RESULT_OK) {
48
4.82k
    EXPECT_GT(decoder->image->width, 0u);
49
4.82k
    EXPECT_GT(decoder->image->height, 0u);
50
4.82k
  }
51
52
  // Loop once.
53
19.1k
  if (avifDecoderReset(decoder.get()) != AVIF_RESULT_OK) return;
54
21.0k
  while (avifDecoderNextImage(decoder.get()) == AVIF_RESULT_OK) {
55
3.50k
  }
56
17.5k
}
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