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_error_code.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 (void)data;
45 (void)size;
46 g_stateCb.events = EVENT_NODE_STATE_ONLINE | EVENT_NODE_STATE_OFFLINE;
47 g_stateCb.onNodeOffline = onNodeOffline;
48 g_stateCb.onNodeOnline = onNodeOnline;
49 g_stateCb.onNodeBasicInfoChanged = onNodeBasicInfoChanged;
50 }
51
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)52 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
53 {
54 if (data == nullptr || size == 0) {
55 return true;
56 }
57 GenRanDiscInfo(data, size);
58 char tmp[65] = {0};
59 if (memcpy_s(tmp, sizeof(tmp) - 1, data, size) != EOK) {
60 return true;
61 }
62 int32_t ret = RegNodeDeviceStateCb((const char *)tmp, &g_stateCb);
63 if (ret == SOFTBUS_OK) {
64 UnregNodeDeviceStateCb(&g_stateCb);
65 }
66 return true;
67 }
68 }
69
70 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)71 extern "C" int32_t LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
72 {
73 /* Run your code on data */
74 OHOS::DoSomethingInterestingWithMyAPI(data, size);
75 return 0;
76 }
77
78