xref: /aosp_15_r20/external/webrtc/test/fuzzers/rtp_video_layers_allocation_fuzzer.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2021 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 <cstddef>
12 #include <cstdint>
13 #include <limits>
14 
15 #include "api/array_view.h"
16 #include "api/video/video_layers_allocation.h"
17 #include "modules/rtp_rtcp/source/rtp_video_layers_allocation_extension.h"
18 #include "rtc_base/checks.h"
19 
20 namespace webrtc {
21 
FuzzOneInput(const uint8_t * data,size_t size)22 void FuzzOneInput(const uint8_t* data, size_t size) {
23   // Video layers allocation is an rtp header extension.
24   // Per https://datatracker.ietf.org/doc/html/rfc8285#section-4.3
25   // rtp header extension uses up to one byte to store the size, i.e.
26   // maximum size of any rtp header extension is 255 bytes.
27   constexpr int kMaxSize = std::numeric_limits<uint8_t>::max();
28   if (size > kMaxSize) {
29     return;
30   }
31   auto raw = rtc::MakeArrayView(data, size);
32 
33   VideoLayersAllocation allocation1;
34   if (!RtpVideoLayersAllocationExtension::Parse(raw, &allocation1)) {
35     // Ignore invalid buffer and move on.
36     return;
37   }
38 
39   // Write parsed allocation back into raw buffer.
40   size_t value_size = RtpVideoLayersAllocationExtension::ValueSize(allocation1);
41   // Check `writer` use minimal number of bytes to pack the extension by
42   // checking it doesn't use more than reader consumed.
43   RTC_CHECK_LE(value_size, raw.size());
44   uint8_t some_memory[kMaxSize];
45   RTC_CHECK_LE(value_size, kMaxSize);
46   rtc::ArrayView<uint8_t> write_buffer(some_memory, value_size);
47   RTC_CHECK(
48       RtpVideoLayersAllocationExtension::Write(write_buffer, allocation1));
49 
50   // Parse what Write assembled.
51   // Unlike random input that should always succeed.
52   VideoLayersAllocation allocation2;
53   RTC_CHECK(
54       RtpVideoLayersAllocationExtension::Parse(write_buffer, &allocation2));
55 
56   RTC_CHECK_EQ(allocation1.rtp_stream_index, allocation2.rtp_stream_index);
57   RTC_CHECK_EQ(allocation1.resolution_and_frame_rate_is_valid,
58                allocation2.resolution_and_frame_rate_is_valid);
59   RTC_CHECK_EQ(allocation1.active_spatial_layers.size(),
60                allocation2.active_spatial_layers.size());
61   RTC_CHECK(allocation1 == allocation2);
62 }
63 
64 }  // namespace webrtc
65