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 
16 #include "setlocalname_fuzzer.h"
17 #include "bluetooth_host.h"
18 #include "bluetooth_log.h"
19 #include "securec.h"
20 
21 using namespace std;
22 using namespace OHOS::Bluetooth;
23 const size_t FUZZ_MAX_NAME_LEN = 10;
24 namespace OHOS {
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)25     bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
26     {
27         HILOGI("start");
28         if ((data == nullptr) || (size < sizeof(int32_t))) {
29             return false;
30         }
31         bool result = false;
32         int len = (size > FUZZ_MAX_NAME_LEN) ? FUZZ_MAX_NAME_LEN : size;
33         std::string localName(reinterpret_cast<const char*>(data), len);
34         for (int i = 0; i < len; i++) {
35             char c = localName[i];
36             bool ret = (c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f');
37             if (!ret) {
38                 localName[i] = 'a';
39             }
40         }
41         BluetoothHost *host = &BluetoothHost::GetDefaultHost();
42         HILOGI("name:%{public}s", localName.c_str());
43         result = host->SetLocalName(localName);
44         HILOGI("result:%{public}d", result);
45         return result;
46     }
47 }
48 
49 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)50 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
51 {
52     /* Run your code on data */
53     OHOS::DoSomethingInterestingWithMyAPI(data, size);
54     return 0;
55 }
56 
57