xref: /aosp_15_r20/external/webrtc/test/fuzzers/sdp_integration_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 <stddef.h>
12 #include <stdint.h>
13 
14 #include "absl/strings/string_view.h"
15 #include "pc/test/integration_test_helpers.h"
16 
17 namespace webrtc {
18 
19 class FuzzerTest : public PeerConnectionIntegrationBaseTest {
20  public:
FuzzerTest()21   FuzzerTest()
22       : PeerConnectionIntegrationBaseTest(SdpSemantics::kUnifiedPlan) {}
23 
RunNegotiateCycle(absl::string_view message)24   void RunNegotiateCycle(absl::string_view message) {
25     CreatePeerConnectionWrappers();
26     // Note - we do not do test.ConnectFakeSignaling(); all signals
27     // generated are discarded.
28 
29     auto srd_observer =
30         rtc::make_ref_counted<FakeSetRemoteDescriptionObserver>();
31 
32     SdpParseError error;
33     std::unique_ptr<SessionDescriptionInterface> sdp(
34         CreateSessionDescription("offer", std::string(message), &error));
35     caller()->pc()->SetRemoteDescription(std::move(sdp), srd_observer);
36     // Wait a short time for observer to be called. Timeout is short
37     // because the fuzzer should be trying many branches.
38     EXPECT_TRUE_WAIT(srd_observer->called(), 100);
39 
40     // If set-remote-description was successful, try to answer.
41     auto sld_observer =
42         rtc::make_ref_counted<FakeSetLocalDescriptionObserver>();
43     if (srd_observer->error().ok()) {
44       caller()->pc()->SetLocalDescription(sld_observer);
45       EXPECT_TRUE_WAIT(sld_observer->called(), 100);
46     }
47     // If there is an EXPECT failure, die here.
48     RTC_CHECK(!HasFailure());
49   }
50 
51   // This test isn't using the test definition macros, so we have to
52   // define the TestBody() function, even though we don't need it.
TestBody()53   void TestBody() override {}
54 };
55 
FuzzOneInput(const uint8_t * data,size_t size)56 void FuzzOneInput(const uint8_t* data, size_t size) {
57   if (size > 16384) {
58     return;
59   }
60 
61   FuzzerTest test;
62   test.RunNegotiateCycle(
63       absl::string_view(reinterpret_cast<const char*>(data), size));
64 }
65 
66 }  // namespace webrtc
67