blob: a7eeb71889b69982752415d37fc4bf28e30346ca [file] [log] [blame]
Henrik Lundin5dcbbfd2017-12-07 09:21:36 +01001/*
2 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Henrik Lundin8fabab12018-03-09 10:25:39 +010011#include <algorithm>
12
Henrik Lundin5dcbbfd2017-12-07 09:21:36 +010013#include "api/array_view.h"
14#include "modules/audio_coding/codecs/cng/webrtc_cng.h"
15#include "rtc_base/buffer.h"
16#include "test/fuzzers/fuzz_data_helper.h"
17
18namespace webrtc {
19namespace test {
20namespace {
21
22void FuzzOneInputTest(rtc::ArrayView<const uint8_t> data) {
23 FuzzDataHelper fuzz_data(data);
24 ComfortNoiseDecoder cng_decoder;
25
26 while (1) {
27 if (!fuzz_data.CanReadBytes(1))
28 break;
29 const uint8_t sid_frame_len = fuzz_data.Read<uint8_t>();
30 auto sid_frame = fuzz_data.ReadByteArray(sid_frame_len);
31 if (sid_frame.empty())
32 break;
33 cng_decoder.UpdateSid(sid_frame);
34 if (!fuzz_data.CanReadBytes(3))
35 break;
36 constexpr bool kTrueOrFalse[] = {true, false};
37 const bool new_period = fuzz_data.SelectOneOf(kTrueOrFalse);
38 constexpr size_t kOutputSizes[] = {80, 160, 320, 480};
39 const size_t output_size = fuzz_data.SelectOneOf(kOutputSizes);
Henrik Lundin8fabab12018-03-09 10:25:39 +010040 const size_t num_generate_calls =
41 std::min(fuzz_data.Read<uint8_t>(), static_cast<uint8_t>(17));
Henrik Lundin5dcbbfd2017-12-07 09:21:36 +010042 rtc::BufferT<int16_t> output(output_size);
43 for (size_t i = 0; i < num_generate_calls; ++i) {
44 cng_decoder.Generate(output, new_period);
45 }
46 }
47}
48
49} // namespace
50} // namespace test
51
52void FuzzOneInput(const uint8_t* data, size_t size) {
Henrik Lundin151be2d2018-02-26 08:16:33 +010053 // Limit the input size to 100000 bytes to avoid fuzzer timeout.
54 if (size > 200000)
55 return;
Henrik Lundin5dcbbfd2017-12-07 09:21:36 +010056 test::FuzzOneInputTest(rtc::ArrayView<const uint8_t>(data, size));
57}
58
59} // namespace webrtc