/src/brunsli/c/tests/fuzz_decode_streaming.cc
Line | Count | Source (jump to first uncovered line) |
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 | 694 | size_t DiscardOutputFunction(void* data, const uint8_t* buf, size_t count) { |
22 | 694 | BRUNSLI_UNUSED(data); |
23 | 694 | BRUNSLI_UNUSED(buf); |
24 | 694 | return count; |
25 | 694 | } |
26 | | |
27 | 10.4k | int DoTestOneInput(const uint8_t* data, size_t size) { |
28 | 10.4k | brunsli::JPEGOutput out(DiscardOutputFunction, nullptr); |
29 | 10.4k | brunsli::JPEGData jpg; |
30 | 10.4k | brunsli::internal::dec::State state; |
31 | 10.4k | brunsli::BrunsliStatus status; |
32 | | |
33 | 10.4k | size_t start = 0; |
34 | 8.21M | for (size_t end = 0; end <= size; ++end) { |
35 | 8.21M | state.data = data + start; |
36 | 8.21M | state.pos = 0; |
37 | 8.21M | state.len = end - start; |
38 | 8.21M | status = brunsli::internal::dec::ProcessJpeg(&state, &jpg); |
39 | 8.21M | brunsli::BrunsliStatus expected_status = |
40 | 8.21M | end < size ? brunsli::BRUNSLI_NOT_ENOUGH_DATA : brunsli::BRUNSLI_OK; |
41 | 8.21M | if (status != expected_status) return 0; |
42 | 8.20M | start += state.pos; |
43 | 8.20M | } |
44 | | |
45 | | // TODO(eustas): check that streaming and non-streaming decoders produce the |
46 | | // same output |
47 | 2.96k | brunsli::WriteJpeg(jpg, out); |
48 | 2.96k | return 0; |
49 | 10.4k | } |
50 | | |
51 | | // Entry point for LibFuzzer. |
52 | 21.7k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
53 | 21.7k | return DoTestOneInput(data, size); |
54 | 21.7k | } |
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 | } |