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 #include "audioadmdispatchercapturecmdid_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 {
AudioAdmDispatcherCaptureCmdidFuzzTest(const uint8_t * data,size_t size)23 bool AudioAdmDispatcherCaptureCmdidFuzzTest(const uint8_t *data, size_t size)
24 {
25 bool result = false;
26 struct HdfIoService *admDisCapFuzzService = nullptr;
27 struct HdfSBuf *sBuf = nullptr;
28 struct HdfSBuf *reply = nullptr;
29 struct AudioPcmHwParams hwParams {
30 .streamType = AUDIO_CAPTURE_STREAM, .channels = 2, .rate = 11025, .periodSize = 8192,
31 .periodCount = 32, .format = AUDIO_FORMAT_TYPE_PCM_24_BIT, .cardServiceName = CARD_SEVICE_NAME.c_str(),
32 .isBigEndian = 0, .isSignedData = 1, .silenceThreshold = 16384
33 };
34
35 admDisCapFuzzService = HdfIoServiceBind(HDF_CAPTURE_SERVICE.c_str());
36 if (admDisCapFuzzService == nullptr || admDisCapFuzzService->dispatcher == nullptr) {
37 HDF_LOGE("%{public}s: HdfIoServiceBind failed\n", __func__);
38 return false;
39 }
40 sBuf = HdfSbufObtainDefaultSize();
41 if (sBuf == nullptr) {
42 HDF_LOGE("%{public}s: sBuf is NULL\n", __func__);
43 return false;
44 }
45 int32_t ret = WriteHwParamsToBuf(sBuf, hwParams);
46 if (ret < 0) {
47 HDF_LOGE("%{public}s: Write HwParams to buf failed\n", __func__);
48 return false;
49 }
50 int32_t cmdId = *(reinterpret_cast<int32_t *>(const_cast<uint8_t *>(data)));
51 ret = admDisCapFuzzService->dispatcher->Dispatch(&admDisCapFuzzService->object, cmdId, sBuf, reply);
52 if (ret == HDF_SUCCESS) {
53 HDF_LOGE("%{public}s: Dispatch success\n", __func__);
54 result = true;
55 }
56 HdfSbufRecycle(sBuf);
57 HdfIoServiceRecycle(admDisCapFuzzService);
58 return result;
59 }
60 }
61 }
62
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)63 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
64 {
65 OHOS::Audio::AudioAdmDispatcherCaptureCmdidFuzzTest(data, size);
66 return 0;
67 }