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 | |
Henrik Lundin | 8fabab1 | 2018-03-09 10:25:39 +0100 | [diff] [blame^] | 11 | #include <algorithm> |
| 12 | |
Henrik Lundin | 5dcbbfd | 2017-12-07 09:21:36 +0100 | [diff] [blame] | 13 | #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 | |
| 18 | namespace webrtc { |
| 19 | namespace test { |
| 20 | namespace { |
| 21 | |
| 22 | void 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 Lundin | 8fabab1 | 2018-03-09 10:25:39 +0100 | [diff] [blame^] | 40 | const size_t num_generate_calls = |
| 41 | std::min(fuzz_data.Read<uint8_t>(), static_cast<uint8_t>(17)); |
Henrik Lundin | 5dcbbfd | 2017-12-07 09:21:36 +0100 | [diff] [blame] | 42 | 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 | |
| 52 | void FuzzOneInput(const uint8_t* data, size_t size) { |
Henrik Lundin | 151be2d | 2018-02-26 08:16:33 +0100 | [diff] [blame] | 53 | // Limit the input size to 100000 bytes to avoid fuzzer timeout. |
| 54 | if (size > 200000) |
| 55 | return; |
Henrik Lundin | 5dcbbfd | 2017-12-07 09:21:36 +0100 | [diff] [blame] | 56 | test::FuzzOneInputTest(rtc::ArrayView<const uint8_t>(data, size)); |
| 57 | } |
| 58 | |
| 59 | } // namespace webrtc |