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 #include "audio_control_manager.h"
23
24 using namespace OHOS::Telephony;
25 namespace OHOS {
26 constexpr int32_t AUDIO_DEVICE_NUM = 6;
27
SetAudioDevice(const uint8_t * data,size_t size)28 void SetAudioDevice(const uint8_t *data, size_t size)
29 {
30 if (!IsServiceInited()) {
31 return;
32 }
33 std::string address(reinterpret_cast<const char *>(data), size);
34 AudioDevice audioDevice;
35 if (memset_s(&audioDevice, sizeof(AudioDevice), 0, sizeof(AudioDevice)) != EOK) {
36 TELEPHONY_LOGE("memset_s fail");
37 return;
38 }
39 audioDevice.deviceType = static_cast<AudioDeviceType>(size % AUDIO_DEVICE_NUM);
40 if (address.length() > kMaxAddressLen) {
41 TELEPHONY_LOGE("address is not too long");
42 return;
43 }
44 if (memcpy_s(audioDevice.address, kMaxAddressLen, address.c_str(), address.length()) != EOK) {
45 return;
46 }
47 MessageParcel messageParcel;
48 messageParcel.WriteRawData((const void *)&audioDevice, sizeof(AudioDevice));
49 messageParcel.RewindRead(0);
50
51 MessageParcel reply;
52 DelayedSingleton<CallManagerService>::GetInstance()->OnSetAudioDevice(messageParcel, reply);
53 }
54
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)55 void DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
56 {
57 if (data == nullptr || size == 0) {
58 return;
59 }
60 DelayedSingleton<AudioProxy>::GetInstance()->SetAudioMicStateChangeCallback();
61 DelayedSingleton<AudioProxy>::GetInstance()->SetAudioDeviceChangeCallback();
62 DelayedSingleton<AudioProxy>::GetInstance()->SetAudioPreferDeviceChangeCallback();
63 SetAudioDevice(data, size);
64 }
65 } // namespace 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 OHOS::AddCallTokenFuzzer token;
71 /* Run your code on data */
72 OHOS::DoSomethingInterestingWithMyAPI(data, size);
73 return 0;
74 }
75