• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <iostream>
17 #include "channel_converter.h"
18 #include "audio_stream_info.h"
19 #include "audio_log.h"
20 #include "../fuzz_utils.h"
21 
22 namespace OHOS {
23 namespace AudioStandard {
24 using namespace OHOS::AudioStandard::HPAE;
25 using namespace std;
26 using namespace HPAE;
27 
28 FuzzUtils &g_fuzzUtils = FuzzUtils::GetInstance();
29 const size_t THRESHOLD = 10;
30 
SetParamFuzzTest()31 void SetParamFuzzTest()
32 {
33     std::shared_ptr<ChannelConverter> channelConverter = std::make_shared<ChannelConverter>();
34     CHECK_AND_RETURN(channelConverter != nullptr);
35     AudioChannelInfo inChannelInfo = {
36         .channelLayout = g_fuzzUtils.GetData<AudioChannelLayout>(),
37         .numChannels = g_fuzzUtils.GetData<uint32_t>(),
38     };
39     AudioChannelInfo outChannelInfo = {
40         .channelLayout = g_fuzzUtils.GetData<AudioChannelLayout>(),
41         .numChannels = g_fuzzUtils.GetData<uint32_t>(),
42     };
43     AudioSampleFormat format = g_fuzzUtils.GetData<AudioSampleFormat>();
44     bool mixLfe = g_fuzzUtils.GetData<bool>();
45 
46     channelConverter->SetParam(inChannelInfo, outChannelInfo, format, mixLfe);
47 }
48 
SetInChannelInfoFuzzTest()49 void SetInChannelInfoFuzzTest()
50 {
51     std::shared_ptr<ChannelConverter> channelConverter = std::make_shared<ChannelConverter>();
52     CHECK_AND_RETURN(channelConverter != nullptr);
53     AudioChannelInfo inChannelInfo = {
54         .channelLayout = g_fuzzUtils.GetData<AudioChannelLayout>(),
55         .numChannels = g_fuzzUtils.GetData<uint32_t>(),
56     };
57     channelConverter->SetInChannelInfo(inChannelInfo);
58 }
59 
SetOutChannelInfoFuzzTest()60 void SetOutChannelInfoFuzzTest()
61 {
62     std::shared_ptr<ChannelConverter> channelConverter = std::make_shared<ChannelConverter>();
63     CHECK_AND_RETURN(channelConverter != nullptr);
64     AudioChannelInfo outChannelInfo = {
65         .channelLayout = g_fuzzUtils.GetData<AudioChannelLayout>(),
66         .numChannels = g_fuzzUtils.GetData<uint32_t>(),
67     };
68     channelConverter->SetOutChannelInfo(outChannelInfo);
69 }
70 
SetParamsecondFuzzTest()71 void SetParamsecondFuzzTest()
72 {
73     std::shared_ptr<ChannelConverter> channelConverter = std::make_shared<ChannelConverter>();
74     CHECK_AND_RETURN(channelConverter != nullptr);
75     AudioChannelInfo inChannelInfo;
76     AudioChannelInfo outChannelInfo;
77     inChannelInfo.numChannels = g_fuzzUtils.GetData<int32_t>();
78     outChannelInfo.numChannels = g_fuzzUtils.GetData<int32_t>();
79     AudioSampleFormat workFormat = g_fuzzUtils.GetData<AudioSampleFormat>();
80     bool mixLfe = g_fuzzUtils.GetData<bool>();
81     channelConverter->SetParam(inChannelInfo, outChannelInfo, workFormat, mixLfe);
82 }
83 
84 vector <TestFuncs> g_testFuncs = {
85     SetParamFuzzTest,
86     SetInChannelInfoFuzzTest,
87     SetOutChannelInfoFuzzTest,
88     SetParamsecondFuzzTest,
89 };
90 
91 } // namespace AudioStandard
92 } // namesapce OHOS
93 
94 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)95 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
96 {
97     if (size < OHOS::AudioStandard::THRESHOLD) {
98         return 0;
99     }
100 
101     OHOS::AudioStandard::g_fuzzUtils.fuzzTest(data, size, OHOS::AudioStandard::g_testFuncs);
102     return 0;
103 }
104