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 "setaudiodevice_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 #define private public
21 #include "addcalltoken_fuzzer.h"
22 
23 using namespace OHOS::Telephony;
24 namespace OHOS {
25 constexpr int32_t AUDIO_DEVICE_NUM = 6;
26 
SetAudioDevice(const uint8_t * data,size_t size)27 void SetAudioDevice(const uint8_t *data, size_t size)
28 {
29     if (!IsServiceInited()) {
30         return;
31     }
32     std::string address(reinterpret_cast<const char *>(data), size);
33     AudioDevice audioDevice;
34     if (memset_s(&audioDevice, sizeof(AudioDevice), 0, sizeof(AudioDevice)) != EOK) {
35         TELEPHONY_LOGE("memset_s fail");
36         return;
37     }
38     audioDevice.deviceType = static_cast<AudioDeviceType>(size % AUDIO_DEVICE_NUM);
39     if (address.length() > kMaxAddressLen) {
40         TELEPHONY_LOGE("address is not too long");
41         return;
42     }
43     if (memcpy_s(audioDevice.address, kMaxAddressLen, address.c_str(), address.length()) != EOK) {
44         return;
45     }
46     MessageParcel dataParcel;
47     dataParcel.WriteRawData((const void *)&audioDevice, sizeof(AudioDevice));
48     dataParcel.RewindRead(0);
49 
50     MessageParcel reply;
51     DelayedSingleton<CallManagerService>::GetInstance()->OnSetAudioDevice(dataParcel, reply);
52 }
53 
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)54 void DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
55 {
56     if (data == nullptr || size == 0) {
57         return;
58     }
59 
60     SetAudioDevice(data, size);
61 }
62 } // namespace OHOS
63 
64 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)65 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
66 {
67     OHOS::AddCallTokenFuzzer token;
68     /* Run your code on data */
69     OHOS::DoSomethingInterestingWithMyAPI(data, size);
70     return 0;
71 }
72