• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "iostream"
16 #include "string"
17 #include "securec.h"
18 #include "usb_bus_extension.h"
19 #include "usb_driver_info.h"
20 #include "hilog_wrapper.h"
21 #include "usbextension_fuzzer.h"
22 namespace OHOS {
23 namespace ExternalDeviceManager {
24 using namespace std;
25 
UsbDriverInfoUnSerializeFuzzer(const uint8_t * data,size_t size)26 bool UsbDriverInfoUnSerializeFuzzer(const uint8_t *data, size_t size)
27 {
28     string drvInfoStr(reinterpret_cast<const char *>(data));
29     if (drvInfoStr.size() == 0) {
30         return false;
31     }
32 
33     UsbDriverInfo usbDevInfo;
34     usbDevInfo.UnSerialize(drvInfoStr);
35     return true;
36 }
ParseDriverInfoTest(const uint8_t * data,size_t size)37 bool ParseDriverInfoTest(const uint8_t *data, size_t size)
38 {
39     auto bus = make_shared<UsbBusExtension>();
40     string str(reinterpret_cast<const char *>(data));
41     map<string, string> metadata = {
42         {"vid", str},
43         {"pid", str}
44     };
45     auto ret = bus->ParseDriverInfo(metadata);
46     return true;
47 }
48 
49 using TestFuncDef = bool (*)(const uint8_t *data, size_t size);
50 
51 TestFuncDef g_allTestFunc[] = {
52     UsbDriverInfoUnSerializeFuzzer,
53     ParseDriverInfoTest,
54 };
55 
DoSomethingInterestingWithMyAPI(const uint8_t * rawData,size_t size)56 bool DoSomethingInterestingWithMyAPI(const uint8_t *rawData, size_t size)
57 {
58     if (size < sizeof(int)) {
59         return false;
60     }
61     const int index = *(static_cast<const uint8_t *>(rawData));
62     rawData += sizeof(int);
63     size -= sizeof(int);
64     int funcCount = sizeof(g_allTestFunc) / sizeof(g_allTestFunc[0]);
65 
66     auto func = g_allTestFunc[index % funcCount];
67     if (func != nullptr) {
68         auto ret = func(rawData, size);
69         return ret;
70     }
71     return false;
72 }
73 } // namespace ExtDevMgr
74 } // namespace OHOS
75 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)76 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
77 {
78     OHOS::ExternalDeviceManager::DoSomethingInterestingWithMyAPI(data, size);
79     return 0;
80 }