• 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     Metadata vids;
42     Metadata pids;
43     vids.name = "vid";
44     vids.value = str;
45     pids.name = "pid";
46     pids.value = str;
47     vector<Metadata> metadata = {vids, pids};
48     auto ret = bus->ParseDriverInfo(metadata);
49     return true;
50 }
51 
52 using TestFuncDef = bool (*)(const uint8_t *data, size_t size);
53 
54 TestFuncDef g_allTestFunc[] = {
55     UsbDriverInfoUnSerializeFuzzer,
56     ParseDriverInfoTest,
57 };
58 
DoSomethingInterestingWithMyAPI(const uint8_t * rawData,size_t size)59 bool DoSomethingInterestingWithMyAPI(const uint8_t *rawData, size_t size)
60 {
61     if (size < sizeof(int)) {
62         return false;
63     }
64     const int index = *(static_cast<const uint8_t *>(rawData));
65     rawData += sizeof(int);
66     size -= sizeof(int);
67     int funcCount = sizeof(g_allTestFunc) / sizeof(g_allTestFunc[0]);
68 
69     auto func = g_allTestFunc[index % funcCount];
70     if (func != nullptr) {
71         auto ret = func(rawData, size);
72         return ret;
73     }
74     return false;
75 }
76 } // namespace ExtDevMgr
77 } // namespace OHOS
78 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)79 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
80 {
81     OHOS::ExternalDeviceManager::DoSomethingInterestingWithMyAPI(data, size);
82     return 0;
83 }