Danil Chapovalov | ef83cc5 | 2019-09-20 12:24:56 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2019 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 <algorithm> |
| 12 | #include <cstddef> |
| 13 | #include <cstdint> |
| 14 | #include <memory> |
| 15 | #include <utility> |
| 16 | |
| 17 | #include "api/array_view.h" |
| 18 | #include "common_video/generic_frame_descriptor/generic_frame_info.h" |
| 19 | #include "modules/rtp_rtcp/source/rtp_dependency_descriptor_extension.h" |
| 20 | #include "rtc_base/checks.h" |
| 21 | #include "test/fuzzers/fuzz_data_helper.h" |
| 22 | |
| 23 | namespace webrtc { |
Danil Chapovalov | ef83cc5 | 2019-09-20 12:24:56 +0200 | [diff] [blame] | 24 | |
| 25 | void FuzzOneInput(const uint8_t* data, size_t size) { |
| 26 | FrameDependencyStructure structure1; |
| 27 | // nullptr during 1st while loop, after that should point to structure1. |
| 28 | const FrameDependencyStructure* structure1_ptr = nullptr; |
| 29 | std::unique_ptr<const FrameDependencyStructure> structure2; |
| 30 | |
| 31 | test::FuzzDataHelper fuzz_data(rtc::MakeArrayView(data, size)); |
| 32 | while (fuzz_data.CanReadBytes(1)) { |
| 33 | // Treat next byte as size of the next extension. That aligns how |
| 34 | // two-byte rtp header extension sizes are written. |
| 35 | size_t next_size = fuzz_data.Read<uint8_t>(); |
| 36 | auto raw = |
| 37 | fuzz_data.ReadByteArray(std::min(next_size, fuzz_data.BytesLeft())); |
| 38 | |
| 39 | // Read the random input. |
| 40 | DependencyDescriptor descriptor1; |
| 41 | if (!RtpDependencyDescriptorExtension::Parse(raw, structure1_ptr, |
| 42 | &descriptor1)) { |
| 43 | // Ignore invalid buffer and move on. |
| 44 | continue; |
| 45 | } |
| 46 | if (descriptor1.attached_structure) { |
| 47 | structure1 = *descriptor1.attached_structure; |
| 48 | structure1_ptr = &structure1; |
| 49 | } |
| 50 | RTC_CHECK(structure1_ptr); |
| 51 | |
| 52 | // Write parsed descriptor back into raw buffer. |
| 53 | size_t value_size = |
| 54 | RtpDependencyDescriptorExtension::ValueSize(structure1, descriptor1); |
| 55 | // Check |writer| use minimal number of bytes to pack the descriptor by |
| 56 | // checking it doesn't use more than reader consumed. |
| 57 | RTC_CHECK_LE(value_size, raw.size()); |
| 58 | uint8_t some_memory[256]; |
| 59 | // That should be true because value_size <= next_size < 256 |
| 60 | RTC_CHECK_LT(value_size, 256); |
| 61 | rtc::ArrayView<uint8_t> write_buffer(some_memory, value_size); |
| 62 | RTC_CHECK(RtpDependencyDescriptorExtension::Write(write_buffer, structure1, |
| 63 | descriptor1)); |
| 64 | |
| 65 | // Parse what Write assembled. |
| 66 | // Unlike random input that should always succeed. |
| 67 | DependencyDescriptor descriptor2; |
| 68 | RTC_CHECK(RtpDependencyDescriptorExtension::Parse( |
| 69 | write_buffer, structure2.get(), &descriptor2)); |
Danil Chapovalov | e9f663c | 2019-11-07 12:00:45 +0100 | [diff] [blame] | 70 | // Check descriptor1 and descriptor2 have same values. |
| 71 | RTC_CHECK_EQ(descriptor1.first_packet_in_frame, |
| 72 | descriptor2.first_packet_in_frame); |
| 73 | RTC_CHECK_EQ(descriptor1.last_packet_in_frame, |
| 74 | descriptor2.last_packet_in_frame); |
| 75 | RTC_CHECK_EQ(descriptor1.attached_structure != nullptr, |
| 76 | descriptor2.attached_structure != nullptr); |
| 77 | // Using value_or would miss invalid corner case when one value is nullopt |
| 78 | // while another one is 0, but for other errors would produce much nicer |
| 79 | // error message than using RTC_CHECK(optional1 == optional2); |
| 80 | // If logger would support pretty printing optional values, value_or can be |
| 81 | // removed. |
| 82 | RTC_CHECK_EQ(descriptor1.active_decode_targets_bitmask.value_or(0), |
| 83 | descriptor2.active_decode_targets_bitmask.value_or(0)); |
| 84 | RTC_CHECK_EQ(descriptor1.frame_number, descriptor2.frame_number); |
| 85 | RTC_CHECK(descriptor1.resolution == descriptor2.resolution); |
| 86 | RTC_CHECK(descriptor1.frame_dependencies == descriptor2.frame_dependencies); |
Danil Chapovalov | ef83cc5 | 2019-09-20 12:24:56 +0200 | [diff] [blame] | 87 | |
| 88 | if (descriptor2.attached_structure) { |
| 89 | structure2 = std::move(descriptor2.attached_structure); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | } // namespace webrtc |