1 /*
2 * Copyright (c) 2024, Alliance for Open Media. All rights reserved
3 *
4 * This source code is subject to the terms of the BSD 3-Clause Clear License
5 * and the Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear
6 * License was not distributed with this source code in the LICENSE file, you
7 * can obtain it at www.aomedia.org/license/software-license/bsd-3-c-c. If the
8 * Alliance for Open Media Patent License 1.0 was not distributed with this
9 * source code in the PATENTS file, you can obtain it at
10 * www.aomedia.org/license/patent.
11 */
12
13 #include <cstdint>
14 #include <string>
15 #include <vector>
16
17 #include "absl/status/status_matchers.h"
18 #include "absl/types/span.h"
19 #include "fuzztest/fuzztest.h"
20 #include "gmock/gmock.h"
21 // [internal] Placeholder for FLAC fuzzing include.
22 #include "iamf/api/decoder/iamf_decoder.h"
23 #include "iamf/api/types.h"
24
25 namespace iamf_tools {
26 namespace {
27
28 using api::OutputLayout;
29 using ::fuzztest::Arbitrary;
30 using ::fuzztest::ElementOf;
31
32 constexpr OutputLayout kStereoLayout =
33 OutputLayout::kItu2051_SoundSystemA_0_2_0;
34
DoesNotDieWithBasicDecode(const std::string & data)35 void DoesNotDieWithBasicDecode(const std::string& data) {
36 absl::StatusOr<api::IamfDecoder> iamf_decoder =
37 api::IamfDecoder::Create(kStereoLayout);
38 std::vector<uint8_t> bitstream(data.begin(), data.end());
39 ASSERT_THAT(iamf_decoder, ::absl_testing::IsOk());
40
41 auto decode_status = iamf_decoder->Decode(bitstream);
42 }
43
44 FUZZ_TEST(IamfDecoderFuzzTest_ArbitraryBytes, DoesNotDieWithBasicDecode);
45
DoesNotDieCreateFromDescriptors(const std::string & data)46 void DoesNotDieCreateFromDescriptors(const std::string& data) {
47 std::vector<uint8_t> bitstream(data.begin(), data.end());
48
49 absl::StatusOr<api::IamfDecoder> iamf_decoder =
50 api::IamfDecoder::CreateFromDescriptors(kStereoLayout, bitstream);
51
52 if (iamf_decoder.ok()) {
53 auto decoder_status = iamf_decoder->Decode(bitstream);
54 }
55 }
56
57 FUZZ_TEST(IamfDecoderFuzzTest_ArbitraryBytesToDescriptors,
58 DoesNotDieCreateFromDescriptors);
59
DoesNotDieAllParams(api::OutputLayout output_layout,api::OutputSampleType output_sample_type,uint32_t mix_presentation_id,std::string data)60 void DoesNotDieAllParams(api::OutputLayout output_layout,
61 api::OutputSampleType output_sample_type,
62 uint32_t mix_presentation_id, std::string data) {
63 std::vector<uint8_t> bitstream(data.begin(), data.end());
64 absl::StatusOr<api::IamfDecoder> iamf_decoder =
65 api::IamfDecoder::Create(kStereoLayout);
66 ASSERT_THAT(iamf_decoder, ::absl_testing::IsOk());
67
68 auto decode_status = iamf_decoder->Decode(bitstream);
69 iamf_decoder->ConfigureOutputSampleType(output_sample_type);
70 }
71
72 // // TODO(b/378912426): Update this to support all output layouts.
AnyOutputLayout()73 auto AnyOutputLayout() {
74 return ElementOf<api::OutputLayout>({
75 api::OutputLayout::kItu2051_SoundSystemA_0_2_0,
76 api::OutputLayout::kItu2051_SoundSystemB_0_5_0,
77 api::OutputLayout::kItu2051_SoundSystemC_2_5_0,
78 api::OutputLayout::kItu2051_SoundSystemD_4_5_0,
79 api::OutputLayout::kItu2051_SoundSystemE_4_5_1,
80 api::OutputLayout::kItu2051_SoundSystemF_3_7_0,
81 api::OutputLayout::kItu2051_SoundSystemG_4_9_0,
82 api::OutputLayout::kItu2051_SoundSystemH_9_10_3,
83 api::OutputLayout::kItu2051_SoundSystemI_0_7_0,
84 api::OutputLayout::kItu2051_SoundSystemJ_4_7_0,
85 api::OutputLayout::kIAMF_SoundSystemExtension_2_7_0,
86 api::OutputLayout::kIAMF_SoundSystemExtension_2_3_0,
87 api::OutputLayout::kIAMF_SoundSystemExtension_0_1_0,
88 api::OutputLayout::kIAMF_SoundSystemExtension_6_9_0,
89 });
90 }
91
AnyOutputSampleType()92 auto AnyOutputSampleType() {
93 return ElementOf<api::OutputSampleType>({
94 api::OutputSampleType::kInt16LittleEndian,
95 api::OutputSampleType::kInt32LittleEndian,
96 });
97 }
98
99 FUZZ_TEST(IamfDecoderFuzzTest_AllArbitraryParams, DoesNotDieAllParams)
100 .WithDomains(AnyOutputLayout(), // output_layout,
101 AnyOutputSampleType(), // output_sample_type,
102 Arbitrary<uint32_t>(), // mix_presentation_id,
103 Arbitrary<std::string>()); // data
104
105 } // namespace
106 } // namespace iamf_tools
107