• 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 #include "hpae_manager_fuzzer.h"
16 
17 #include <string>
18 #include <thread>
19 #include <chrono>
20 #include <cstdio>
21 #include <fstream>
22 #include <streambuf>
23 #include <algorithm>
24 #include <unistd.h>
25 #include "audio_errors.h"
26 #include "test_case_common.h"
27 #include "hpae_audio_service_dump_callback_unit_test.h"
28 #include "hpae_capturer_manager.h"
29 
30 namespace OHOS {
31 namespace AudioStandard {
32 using namespace std;
33 using namespace OHOS::AudioStandard::HPAE;
34 
35 static const uint8_t* RAW_DATA = nullptr;
36 static size_t g_dataSize = 0;
37 static size_t g_pos;
38 static std::string g_rootCapturerPath = "/data/source_file_io_48000_2_s16le.pcm";
39 const uint32_t DEFAULT_FRAME_LENGTH = 960;
40 const size_t THRESHOLD = 10;
41 const uint8_t TESTSIZE = 1;
42 
43 typedef void (*TestFuncs)();
44 
45 template<class T>
GetData()46 T GetData()
47 {
48     T object {};
49     size_t objectSize = sizeof(object);
50 
51     if (g_dataSize <= g_pos) {
52         return object;
53     }
54 
55     if (RAW_DATA == nullptr || objectSize > g_dataSize - g_pos) {
56         return object;
57     }
58     errno_t ret = memcpy_s(&object, objectSize, RAW_DATA + g_pos, objectSize);
59     if (ret != EOK) {
60         return {};
61     }
62     g_pos += objectSize;
63     return object;
64 }
65 
66 template<class T>
GetArrLength(T & arr)67 uint32_t GetArrLength(T& arr)
68 {
69     if (arr == nullptr) {
70         AUDIO_INFO_LOG("%{public}s: The array length is equal to 0", __func__);
71         return 0;
72     }
73     return sizeof(arr) / sizeof(arr[0]);
74 }
75 
InitSourceInfo(HpaeSourceInfo & sourceInfo)76 static void InitSourceInfo(HpaeSourceInfo &sourceInfo)
77 {
78     sourceInfo.deviceNetId = DEFAULT_TEST_DEVICE_NETWORKID;
79     sourceInfo.deviceClass = DEFAULT_TEST_DEVICE_CLASS;
80     sourceInfo.sourceType = SOURCE_TYPE_MIC;
81     sourceInfo.filePath = g_rootCapturerPath;
82 
83     sourceInfo.samplingRate = SAMPLE_RATE_48000;
84     sourceInfo.channels = STEREO;
85     sourceInfo.format = SAMPLE_S16LE;
86     sourceInfo.frameLen = DEFAULT_FRAME_LENGTH;
87     sourceInfo.ecType = HPAE_EC_TYPE_NONE;
88     sourceInfo.micRef = HPAE_REF_OFF;
89 }
90 
UploadDumpSourceInfoFuzzTest()91 void UploadDumpSourceInfoFuzzTest()
92 {
93     HpaeSourceInfo sourceInfo;
94     InitSourceInfo(sourceInfo);
95     std::shared_ptr<IHpaeCapturerManager> capturerManager = std::make_shared<HpaeCapturerManager>(sourceInfo);
96     std::string deviceName = "";
97     if (capturerManager == nullptr) {
98         return;
99     }
100     capturerManager->UploadDumpSourceInfo(deviceName);
101 }
102 
103 TestFuncs g_testFuncs[TESTSIZE] = {
104     UploadDumpSourceInfoFuzzTest,
105 };
106 
FuzzTest(const uint8_t * rawData,size_t size)107 bool FuzzTest(const uint8_t* rawData, size_t size)
108 {
109     if (rawData == nullptr) {
110         return false;
111     }
112 
113     // initialize data
114     RAW_DATA = rawData;
115     g_dataSize = size;
116     g_pos = 0;
117 
118     uint32_t code = GetData<uint32_t>();
119     uint32_t len = GetArrLength(g_testFuncs);
120     if (len > 0) {
121         g_testFuncs[code % len]();
122     } else {
123         AUDIO_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
124     }
125 
126     return true;
127 }
128 } // namespace AudioStandard
129 } // namesapce OHOS
130 
131 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)132 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
133 {
134     if (size < OHOS::AudioStandard::THRESHOLD) {
135         return 0;
136     }
137 
138     OHOS::AudioStandard::FuzzTest(data, size);
139     return 0;
140 }
141