1 /*
2  * Copyright (c) 2021-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 #include "audioadmdispatchercontrolcmdid_fuzzer.h"
16 #include "hdf_log.h"
17 #include "audio_hdi_common.h"
18 #include "audio_adm_common.h"
19 
20 using namespace OHOS::Audio;
21 namespace OHOS {
22 namespace Audio {
AudioAdmDispatcherControlCmdidFuzzTest(const uint8_t * data,size_t size)23     bool AudioAdmDispatcherControlCmdidFuzzTest(const uint8_t *data, size_t size)
24     {
25         bool result = false;
26         struct HdfIoService *service = nullptr;
27         struct HdfSBuf *writeBuf = nullptr;
28         struct HdfSBuf *writeReply = nullptr;
29 
30         struct AudioCtlElemValue writeElemValue = {
31             .id.cardServiceName = CARD_SEVICE_NAME.c_str(),
32             .id.iface = AUDIODRV_CTL_ELEM_IFACE_MIXER,
33             .id.itemName = "Mic Left Gain",
34             .value[0] = 5,
35         };
36 
37         service = HdfIoServiceBind(HDF_CONTROL_SERVICE.c_str());
38         if (service == nullptr || service->dispatcher == nullptr) {
39             HDF_LOGE("%{public}s: HdfIoServiceBind failed\n", __func__);
40             return false;
41         }
42 
43         writeBuf = HdfSbufObtainDefaultSize();
44         if (writeBuf == nullptr) {
45             HDF_LOGE("%{public}s: writeBuf is NULL\n", __func__);
46             HdfIoServiceRecycle(service);
47             return false;
48         }
49         int32_t ret = WriteEleValueToBuf(writeBuf, writeElemValue);
50         if (ret < 0) {
51             HDF_LOGE("%{public}s: Write eleValue to buffer failed\n", __func__);
52             return false;
53         }
54         int32_t cmdId = *(reinterpret_cast<int32_t *>(const_cast<uint8_t *>(data)));
55         ret = service->dispatcher->Dispatch(&service->object, cmdId, writeBuf, writeReply);
56         if (ret == HDF_SUCCESS) {
57             HDF_LOGE("%{public}s: Dispatch sucess\n", __func__);
58             result = true;
59         }
60         HdfSbufRecycle(writeBuf);
61         HdfIoServiceRecycle(service);
62         return result;
63     }
64 }
65 }
66 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)67 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
68 {
69     OHOS::Audio::AudioAdmDispatcherControlCmdidFuzzTest(data, size);
70     return 0;
71 }