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 "regnodedevicestatecb_fuzzer.h"
17 #include <cstddef>
18 #include <securec.h>
19 #include "softbus_bus_center.h"
20 #include "softbus_errcode.h"
21 
22 namespace OHOS {
23     static INodeStateCb g_stateCb;
24 
onNodeOnline(NodeBasicInfo * info)25     void onNodeOnline(NodeBasicInfo *info)
26     {
27         (void)info;
28     }
29 
onNodeOffline(NodeBasicInfo * info)30     void onNodeOffline(NodeBasicInfo *info)
31     {
32         (void)info;
33     }
34 
onNodeBasicInfoChanged(NodeBasicInfoType type,NodeBasicInfo * info)35     void onNodeBasicInfoChanged(NodeBasicInfoType type, NodeBasicInfo *info)
36     {
37         (void)type;
38         (void)info;
39     }
40 
41 
GenRanDiscInfo(const uint8_t * data,size_t size)42     static void GenRanDiscInfo(const uint8_t* data, size_t size)
43     {
44         g_stateCb.events = EVENT_NODE_STATE_ONLINE | EVENT_NODE_STATE_OFFLINE;
45         g_stateCb.onNodeOffline = onNodeOffline;
46         g_stateCb.onNodeOnline = onNodeOnline;
47         g_stateCb.onNodeBasicInfoChanged = onNodeBasicInfoChanged;
48     }
49 
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)50     bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
51     {
52         if (data == nullptr || size == 0) {
53             return true;
54         }
55         GenRanDiscInfo(data, size);
56         char tmp[65] = {0};
57         if (memcpy_s(tmp, sizeof(tmp) - 1, data, size) != EOK) {
58             return true;
59         }
60         int32_t ret = RegNodeDeviceStateCb((const char *)tmp, &g_stateCb);
61         if (ret == SOFTBUS_OK) {
62             UnregNodeDeviceStateCb(&g_stateCb);
63         }
64         return true;
65     }
66 }
67 
68 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)69 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
70 {
71     /* Run your code on data */
72     OHOS::DoSomethingInterestingWithMyAPI(data, size);
73     return 0;
74 }
75 
76