Coverage Report

Created: 2025-10-13 06:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/brunsli/c/tests/fuzz_decode_streaming.cc
Line
Count
Source
1
// Copyright (c) Google LLC 2019
2
//
3
// Use of this source code is governed by an MIT-style
4
// license that can be found in the LICENSE file or at
5
// https://opensource.org/licenses/MIT.
6
7
#include <cstddef>
8
#include <cstdint>
9
#include <vector>
10
11
// #include "gtest/gtest.h"
12
// #include "testing/fuzzing/fuzztest.h"
13
#include <brunsli/jpeg_data.h>
14
#include "../common/platform.h"
15
#include <brunsli/status.h>
16
#include <brunsli/brunsli_decode.h>
17
#include <brunsli/jpeg_data_writer.h>
18
#include "../dec/state.h"
19
#include "./test_utils.h"
20
21
840
size_t DiscardOutputFunction(void* data, const uint8_t* buf, size_t count) {
22
840
  BRUNSLI_UNUSED(data);
23
840
  BRUNSLI_UNUSED(buf);
24
840
  return count;
25
840
}
26
27
14.6k
int DoTestOneInput(const uint8_t* data, size_t size) {
28
14.6k
  brunsli::JPEGOutput out(DiscardOutputFunction, nullptr);
29
14.6k
  brunsli::JPEGData jpg;
30
14.6k
  brunsli::internal::dec::State state;
31
14.6k
  brunsli::BrunsliStatus status;
32
33
14.6k
  size_t start = 0;
34
3.07M
  for (size_t end = 0; end <= size; ++end) {
35
3.06M
    state.data = data + start;
36
3.06M
    state.pos = 0;
37
3.06M
    state.len = end - start;
38
3.06M
    status = brunsli::internal::dec::ProcessJpeg(&state, &jpg);
39
3.06M
    brunsli::BrunsliStatus expected_status =
40
3.06M
        end < size ? brunsli::BRUNSLI_NOT_ENOUGH_DATA : brunsli::BRUNSLI_OK;
41
3.06M
    if (status != expected_status) return 0;
42
3.05M
    start += state.pos;
43
3.05M
  }
44
45
  // TODO(eustas): check that streaming and non-streaming decoders produce the
46
  //               same output
47
4.12k
  brunsli::WriteJpeg(jpg, out);
48
4.12k
  return 0;
49
14.6k
}
50
51
// Entry point for LibFuzzer.
52
31.1k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
53
31.1k
  return DoTestOneInput(data, size);
54
31.1k
}
55
56
0
void TestOneInput(const std::vector<uint8_t>& data) {
57
0
  DoTestOneInput(data.data(), data.size());
58
0
}
59
60
0
std::vector<std::tuple<std::vector<uint8_t>>> ReadSeeds() {
61
0
  const std::vector<uint8_t> data = brunsli::ReadTestData("fuzz-decode.mar");
62
0
  return brunsli::ParseMar(data.data(), data.size());
63
0
}
64
65
FUZZ_TEST(BrunsliDecodeStreamingFuzz, TestOneInput).WithSeeds(&ReadSeeds);
66
67
// TODO(eustas): Add existing cases.
68
0
TEST(BrunsliDecodeStreamingFuzz, Empty) {
69
0
  DoTestOneInput(nullptr, 0);
70
0
}