• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 <cstddef>
18 #include <cstdint>
19 
20 #include "message_parcel.h"
21 
22 #include "audio_manager_base.h"
23 #include "audio_server.h"
24 using namespace std;
25 
26 namespace OHOS {
27 namespace AudioStandard {
28 const int32_t SYSTEM_ABILITY_ID = 3001;
29 const bool RUN_ON_CREATE = false;
30 const int32_t LIMITSIZE = 4;
31 
Convert2Float(const uint8_t * ptr)32 float Convert2Float(const uint8_t *ptr)
33 {
34     float floatValue = static_cast<float>(*ptr);
35     return floatValue / 128.0f - 1.0f;
36 }
37 
AudioServerBalanceFuzzer(const uint8_t * rawData,size_t size,std::shared_ptr<AudioServer> AudioServerPtr)38 void AudioServerBalanceFuzzer(const uint8_t *rawData, size_t size, std::shared_ptr<AudioServer> AudioServerPtr)
39 {
40     float balanceValue = Convert2Float(rawData);
41     MessageParcel data;
42     data.WriteFloat(balanceValue);
43     MessageParcel reply;
44     MessageOption option;
45     AudioServerPtr->OnRemoteRequest(
46         static_cast<uint32_t>(AudioServerInterfaceCode::SET_AUDIO_BALANCE_VALUE), data, reply, option);
47 }
48 
Convert2Bool(const uint8_t * ptr)49 bool Convert2Bool(const uint8_t *ptr)
50 {
51     return (ptr[0] & 1) ? true : false;
52 }
53 
AudioServerMonoFuzzer(const uint8_t * rawData,size_t size,std::shared_ptr<AudioServer> AudioServerPtr)54 void AudioServerMonoFuzzer(const uint8_t *rawData, size_t size, std::shared_ptr<AudioServer> AudioServerPtr)
55 {
56     bool monoState = Convert2Bool(rawData);
57     MessageParcel data;
58     data.WriteBool(monoState);
59     MessageParcel reply;
60     MessageOption option;
61     AudioServerPtr->OnRemoteRequest(
62         static_cast<uint32_t>(AudioServerInterfaceCode::SET_AUDIO_MONO_STATE), data, reply, option);
63 }
64 
AudioServerBalanceFuzzTest(const uint8_t * rawData,size_t size)65 void AudioServerBalanceFuzzTest(const uint8_t *rawData, size_t size)
66 {
67     if (rawData == nullptr || size < LIMITSIZE) {
68         return;
69     }
70     std::shared_ptr<AudioServer> AudioServerPtr =
71         std::make_shared<AudioServer>(SYSTEM_ABILITY_ID, RUN_ON_CREATE);
72     AudioServerBalanceFuzzer(rawData, size, AudioServerPtr);
73     AudioServerMonoFuzzer(rawData, size, AudioServerPtr);
74 }
75 } // namespace AudioStandard
76 } // namesapce OHOS
77 
78 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)79 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
80 {
81     /* Run your code on data */
82     OHOS::AudioStandard::AudioServerBalanceFuzzTest(data, size);
83     return 0;
84 }
85