Henrik Lundin | 5dcbbfd | 2017-12-07 09:21:36 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 11 | #include "api/array_view.h" |
| 12 | #include "modules/audio_coding/codecs/cng/webrtc_cng.h" |
| 13 | #include "rtc_base/buffer.h" |
| 14 | #include "test/fuzzers/fuzz_data_helper.h" |
| 15 | |
| 16 | namespace webrtc { |
| 17 | namespace test { |
| 18 | namespace { |
| 19 | |
| 20 | void FuzzOneInputTest(rtc::ArrayView<const uint8_t> data) { |
| 21 | FuzzDataHelper fuzz_data(data); |
| 22 | ComfortNoiseDecoder cng_decoder; |
| 23 | |
| 24 | while (1) { |
| 25 | if (!fuzz_data.CanReadBytes(1)) |
| 26 | break; |
| 27 | const uint8_t sid_frame_len = fuzz_data.Read<uint8_t>(); |
| 28 | auto sid_frame = fuzz_data.ReadByteArray(sid_frame_len); |
| 29 | if (sid_frame.empty()) |
| 30 | break; |
| 31 | cng_decoder.UpdateSid(sid_frame); |
| 32 | if (!fuzz_data.CanReadBytes(3)) |
| 33 | break; |
| 34 | constexpr bool kTrueOrFalse[] = {true, false}; |
| 35 | const bool new_period = fuzz_data.SelectOneOf(kTrueOrFalse); |
| 36 | constexpr size_t kOutputSizes[] = {80, 160, 320, 480}; |
| 37 | const size_t output_size = fuzz_data.SelectOneOf(kOutputSizes); |
| 38 | const size_t num_generate_calls = fuzz_data.Read<uint8_t>(); |
| 39 | rtc::BufferT<int16_t> output(output_size); |
| 40 | for (size_t i = 0; i < num_generate_calls; ++i) { |
| 41 | cng_decoder.Generate(output, new_period); |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | } // namespace |
| 47 | } // namespace test |
| 48 | |
| 49 | void FuzzOneInput(const uint8_t* data, size_t size) { |
Henrik Lundin | 151be2d | 2018-02-26 08:16:33 +0100 | [diff] [blame^] | 50 | // Limit the input size to 100000 bytes to avoid fuzzer timeout. |
| 51 | if (size > 200000) |
| 52 | return; |
Henrik Lundin | 5dcbbfd | 2017-12-07 09:21:36 +0100 | [diff] [blame] | 53 | test::FuzzOneInputTest(rtc::ArrayView<const uint8_t>(data, size)); |
| 54 | } |
| 55 | |
| 56 | } // namespace webrtc |