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 "dfx_utils.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
SerializeToJSONString1FuzzTest()31 void SerializeToJSONString1FuzzTest()
32 {
33 DfxUtils dfxUtils;
34 RendererStats data;
35 dfxUtils.SerializeToJSONString(data);
36 }
37
SerializeToJSONString2FuzzTest()38 void SerializeToJSONString2FuzzTest()
39 {
40 DfxUtils dfxUtils;
41 CapturerStats data;
42 dfxUtils.SerializeToJSONString(data);
43 }
44
SerializeToJSONString3FuzzTest()45 void SerializeToJSONString3FuzzTest()
46 {
47 DfxUtils dfxUtils;
48 std::vector<InterruptEffect> data;
49 InterruptEffect gameEffect;
50 gameEffect.bundleName = "com.example.game";
51 gameEffect.streamUsage = g_fuzzUtils.GetData<uint8_t>();
52 gameEffect.appState = g_fuzzUtils.GetData<uint8_t>();
53 gameEffect.interruptEvent = g_fuzzUtils.GetData<uint8_t>();
54 data.push_back(gameEffect);
55 dfxUtils.SerializeToJSONString(data);
56 }
57
58 vector<TestFuncs> g_testFuncs = {
59 SerializeToJSONString1FuzzTest,
60 SerializeToJSONString2FuzzTest,
61 SerializeToJSONString3FuzzTest,
62 };
63
64 } // namespace AudioStandard
65 } // namesapce OHOS
66
67 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)68 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
69 {
70 if (size < OHOS::AudioStandard::FUZZ_INPUT_SIZE_THRESHOLD) {
71 return 0;
72 }
73
74 OHOS::AudioStandard::g_fuzzUtils.fuzzTest(data, size, OHOS::AudioStandard::g_testFuncs);
75 return 0;
76 }
77