1e1c44949Sopenharmony_ci/*
2e1c44949Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3e1c44949Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4e1c44949Sopenharmony_ci * you may not use this file except in compliance with the License.
5e1c44949Sopenharmony_ci * You may obtain a copy of the License at
6e1c44949Sopenharmony_ci *
7e1c44949Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8e1c44949Sopenharmony_ci *
9e1c44949Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10e1c44949Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11e1c44949Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12e1c44949Sopenharmony_ci * See the License for the specific language governing permissions and
13e1c44949Sopenharmony_ci * limitations under the License.
14e1c44949Sopenharmony_ci */
15e1c44949Sopenharmony_ci
16e1c44949Sopenharmony_ci#include "setaudiodevice_fuzzer.h"
17e1c44949Sopenharmony_ci
18e1c44949Sopenharmony_ci#include <cstddef>
19e1c44949Sopenharmony_ci#include <cstdint>
20e1c44949Sopenharmony_ci#define private public
21e1c44949Sopenharmony_ci#include "addcalltoken_fuzzer.h"
22e1c44949Sopenharmony_ci
23e1c44949Sopenharmony_ciusing namespace OHOS::Telephony;
24e1c44949Sopenharmony_cinamespace OHOS {
25e1c44949Sopenharmony_ciconstexpr int32_t AUDIO_DEVICE_NUM = 6;
26e1c44949Sopenharmony_ci
27e1c44949Sopenharmony_civoid SetAudioDevice(const uint8_t *data, size_t size)
28e1c44949Sopenharmony_ci{
29e1c44949Sopenharmony_ci    if (!IsServiceInited()) {
30e1c44949Sopenharmony_ci        return;
31e1c44949Sopenharmony_ci    }
32e1c44949Sopenharmony_ci    std::string address(reinterpret_cast<const char *>(data), size);
33e1c44949Sopenharmony_ci    AudioDevice audioDevice;
34e1c44949Sopenharmony_ci    if (memset_s(&audioDevice, sizeof(AudioDevice), 0, sizeof(AudioDevice)) != EOK) {
35e1c44949Sopenharmony_ci        TELEPHONY_LOGE("memset_s fail");
36e1c44949Sopenharmony_ci        return;
37e1c44949Sopenharmony_ci    }
38e1c44949Sopenharmony_ci    audioDevice.deviceType = static_cast<AudioDeviceType>(size % AUDIO_DEVICE_NUM);
39e1c44949Sopenharmony_ci    if (address.length() > kMaxAddressLen) {
40e1c44949Sopenharmony_ci        TELEPHONY_LOGE("address is not too long");
41e1c44949Sopenharmony_ci        return;
42e1c44949Sopenharmony_ci    }
43e1c44949Sopenharmony_ci    if (memcpy_s(audioDevice.address, kMaxAddressLen, address.c_str(), address.length()) != EOK) {
44e1c44949Sopenharmony_ci        return;
45e1c44949Sopenharmony_ci    }
46e1c44949Sopenharmony_ci    MessageParcel dataParcel;
47e1c44949Sopenharmony_ci    dataParcel.WriteRawData((const void *)&audioDevice, sizeof(AudioDevice));
48e1c44949Sopenharmony_ci    dataParcel.RewindRead(0);
49e1c44949Sopenharmony_ci
50e1c44949Sopenharmony_ci    MessageParcel reply;
51e1c44949Sopenharmony_ci    DelayedSingleton<CallManagerService>::GetInstance()->OnSetAudioDevice(dataParcel, reply);
52e1c44949Sopenharmony_ci}
53e1c44949Sopenharmony_ci
54e1c44949Sopenharmony_civoid DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
55e1c44949Sopenharmony_ci{
56e1c44949Sopenharmony_ci    if (data == nullptr || size == 0) {
57e1c44949Sopenharmony_ci        return;
58e1c44949Sopenharmony_ci    }
59e1c44949Sopenharmony_ci
60e1c44949Sopenharmony_ci    SetAudioDevice(data, size);
61e1c44949Sopenharmony_ci}
62e1c44949Sopenharmony_ci} // namespace OHOS
63e1c44949Sopenharmony_ci
64e1c44949Sopenharmony_ci/* Fuzzer entry point */
65e1c44949Sopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
66e1c44949Sopenharmony_ci{
67e1c44949Sopenharmony_ci    OHOS::AddCallTokenFuzzer token;
68e1c44949Sopenharmony_ci    /* Run your code on data */
69e1c44949Sopenharmony_ci    OHOS::DoSomethingInterestingWithMyAPI(data, size);
70e1c44949Sopenharmony_ci    return 0;
71e1c44949Sopenharmony_ci}
72