• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "audio_hdi_fuzzer_common.h"
16 #include "securec.h"
17 #include "audiocreatecaptureattrs_fuzzer.h"
18 
19 using namespace OHOS::Audio;
20 namespace OHOS {
21 namespace Audio {
AudioCreateCaptureAttrsFuzzTest(const uint8_t * data,size_t size)22 bool AudioCreateCaptureAttrsFuzzTest(const uint8_t *data, size_t size)
23 {
24     bool result = false;
25     TestAudioManager *manager = nullptr;
26     int32_t ret = GetManager(manager);
27     if (ret < 0 || manager == nullptr) {
28         HDF_LOGE("%{public}s: GetManager failed \n", __func__);
29         return false;
30     }
31     struct AudioAdapter *attrsFuzzAdapter = nullptr;
32     struct AudioPort *attrsFuzzCapturePort = nullptr;
33     ret = GetLoadAdapter(manager, &attrsFuzzAdapter, attrsFuzzCapturePort);
34     if (ret < 0 || attrsFuzzAdapter == nullptr || attrsFuzzCapturePort == nullptr) {
35         HDF_LOGE("%{public}s: GetLoadAdapter failed \n", __func__);
36         return false;
37     }
38     struct AudioDeviceDescriptor devDesc = {};
39     InitDevDesc(devDesc, attrsFuzzCapturePort->portId, PIN_IN_MIC);
40     struct AudioCapture *capture = nullptr;
41     struct AudioSampleAttributes attrsFuzz = {};
42     int32_t copySize = sizeof(attrsFuzz) > size ? size : sizeof(attrsFuzz);
43     if (memcpy_s((void *)&attrsFuzz, sizeof(attrsFuzz), data, copySize) != 0) {
44         return false;
45     }
46     ret = attrsFuzzAdapter->CreateCapture(attrsFuzzAdapter, &devDesc, &attrsFuzz, &capture);
47     if (ret == HDF_SUCCESS) {
48         result = true;
49     }
50     attrsFuzzAdapter->DestroyCapture(attrsFuzzAdapter, capture);
51     manager->UnloadAdapter(manager, attrsFuzzAdapter);
52     return result;
53 }
54 }
55 }
56 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)57 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
58 {
59     OHOS::Audio::AudioCreateCaptureAttrsFuzzTest(data, size);
60     return 0;
61 }