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 <securec.h>
17
18 #include "audio_log.h"
19 #include "microphone_descriptor.h"
20 #include "../fuzz_utils.h"
21
22 namespace OHOS {
23 namespace AudioStandard {
24 using namespace std;
25
26 FuzzUtils &g_fuzzUtils = FuzzUtils::GetInstance();
27 const size_t FUZZ_INPUT_SIZE_THRESHOLD = 10;
28
29 typedef void (*TestFuncs)();
30
MicrophoneDescriptor1FuzzTest()31 void MicrophoneDescriptor1FuzzTest()
32 {
33 int32_t id = g_fuzzUtils.GetData<int32_t>();
34 DeviceType deviceType = g_fuzzUtils.GetData<DeviceType>();
35 int32_t groupId = g_fuzzUtils.GetData<int32_t>();
36 int32_t sensitivity = g_fuzzUtils.GetData<int32_t>();
37 MicrophoneDescriptor microphoneDescriptor(id, deviceType, groupId, sensitivity);
38 }
39
MicrophoneDescriptor2FuzzTest()40 void MicrophoneDescriptor2FuzzTest()
41 {
42 sptr<MicrophoneDescriptor> micDesc = new (std::nothrow) MicrophoneDescriptor();
43 if (micDesc == nullptr) {
44 return;
45 }
46 micDesc->micId_ = g_fuzzUtils.GetData<int32_t>();
47 MicrophoneDescriptor microphoneDescriptor(micDesc);
48 }
49
OperatorFuzzTest()50 void OperatorFuzzTest()
51 {
52 int32_t id = g_fuzzUtils.GetData<int32_t>();
53 DeviceType deviceType = g_fuzzUtils.GetData<DeviceType>();
54
55 int32_t groupId = g_fuzzUtils.GetData<int32_t>();
56 int32_t sensitivity = g_fuzzUtils.GetData<int32_t>();
57 MicrophoneDescriptor microphoneDescriptor(id, deviceType, groupId, sensitivity);
58 MicrophoneDescriptor microphoneDescriptor2(microphoneDescriptor);
59 }
60
MarshallingFuzzTest()61 void MarshallingFuzzTest()
62 {
63 int32_t id = g_fuzzUtils.GetData<int32_t>();
64 DeviceType deviceType = g_fuzzUtils.GetData<DeviceType>();
65
66 int32_t groupId = g_fuzzUtils.GetData<int32_t>();
67 int32_t sensitivity = g_fuzzUtils.GetData<int32_t>();
68 MicrophoneDescriptor microphoneDescriptor(id, deviceType, groupId, sensitivity);
69 Parcel parcel;
70 microphoneDescriptor.Marshalling(parcel);
71 }
72
UnmarshallingFuzzTest()73 void UnmarshallingFuzzTest()
74 {
75 int32_t id = g_fuzzUtils.GetData<int32_t>();
76 DeviceType deviceType = g_fuzzUtils.GetData<DeviceType>();
77
78 int32_t groupId = g_fuzzUtils.GetData<int32_t>();
79 int32_t sensitivity = g_fuzzUtils.GetData<int32_t>();
80 MicrophoneDescriptor microphoneDescriptor(id, deviceType, groupId, sensitivity);
81 Parcel parcel;
82 microphoneDescriptor.Marshalling(parcel);
83 if (MicrophoneDescriptor::Unmarshalling(parcel) == nullptr) {
84 return;
85 }
86 auto micDescShared = std::shared_ptr<MicrophoneDescriptor>(MicrophoneDescriptor::Unmarshalling(parcel));
87 }
88
89 vector<TestFuncs> g_testFuncs = {
90 MicrophoneDescriptor1FuzzTest,
91 MicrophoneDescriptor2FuzzTest,
92 OperatorFuzzTest,
93 MarshallingFuzzTest,
94 UnmarshallingFuzzTest,
95 };
96
97 } // namespace AudioStandard
98 } // namesapce OHOS
99
100 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)101 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
102 {
103 if (size < OHOS::AudioStandard::FUZZ_INPUT_SIZE_THRESHOLD) {
104 return 0;
105 }
106
107 OHOS::AudioStandard::g_fuzzUtils.fuzzTest(data, size, OHOS::AudioStandard::g_testFuncs);
108 return 0;
109 }
110