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 #include "refreshlnn_fuzzer.h"
16 #include <cstddef>
17 #include <securec.h>
18 #include "softbus_access_token_test.h"
19 #include "softbus_bus_center.h"
20 #include "softbus_errcode.h"
21
22 namespace OHOS {
23 static const int32_t MAX_SIZE_DISCOVER_MODE = 2;
24 static const int32_t MAX_SIZE_EXCHANGE_MEDIUM = MEDIUM_BUTT + 1;
25 static const int32_t MAX_SIZE_EXCHANGE_FREQ = FREQ_BUTT + 1 ;
26 static const int32_t MAX_SIZE_CAPABILITYMAP = OSD_CAPABILITY_BITMAP + 1;
27
28 static SubscribeInfo g_sInfo = {
29 .subscribeId = 1,
30 .mode = DISCOVER_MODE_ACTIVE,
31 .medium = COAP,
32 .freq = MID,
33 .isSameAccount = true,
34 .isWakeRemote = false,
35 .capability = "dvKit",
36 .capabilityData = (unsigned char *)"capdata3",
37 .dataLen = sizeof("capdata3")
38 };
39
TestDeviceFound(const DeviceInfo * device)40 static void TestDeviceFound(const DeviceInfo *device)
41 {
42 (void)device;
43 }
44
TestDiscoverResult(int32_t refreshId,RefreshResult reason)45 static void TestDiscoverResult(int32_t refreshId, RefreshResult reason)
46 {
47 (void)refreshId;
48 (void)reason;
49 }
50
51 static IRefreshCallback g_refreshCb = {
52 .OnDeviceFound = TestDeviceFound,
53 .OnDiscoverResult = TestDiscoverResult
54 };
55
56
GenRanDiscInfo(char * data,size_t size)57 static void GenRanDiscInfo(char* data, size_t size)
58 {
59 g_sInfo.subscribeId = size;
60 g_sInfo.mode = (size % MAX_SIZE_DISCOVER_MODE) ? DISCOVER_MODE_ACTIVE : DISCOVER_MODE_PASSIVE;
61 g_sInfo.medium = (ExchangeMedium)(size % MAX_SIZE_EXCHANGE_MEDIUM);
62 g_sInfo.freq = (ExchangeFreq)(size % MAX_SIZE_EXCHANGE_FREQ);
63 g_sInfo.isSameAccount = (size % MAX_SIZE_DISCOVER_MODE) ? true : false;
64 g_sInfo.isWakeRemote = (size % MAX_SIZE_DISCOVER_MODE) ? true : false;
65 g_sInfo.capability = g_capabilityMap[(DataBitMap)(size % MAX_SIZE_CAPABILITYMAP)].capability;
66 g_sInfo.capabilityData = (unsigned char *)data;
67 g_sInfo.dataLen = size;
68 }
69
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)70 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
71 {
72 if (data == nullptr || size == 0) {
73 return true;
74 }
75
76 char *tmp = reinterpret_cast<char *>(malloc(size));
77 if (tmp == nullptr) {
78 return false;
79 }
80 if (memset_s(tmp, size, '\0', size) != EOK) {
81 free(tmp);
82 return false;
83 }
84 if (memcpy_s(tmp, size, data, size - 1) != EOK) {
85 free(tmp);
86 return false;
87 }
88
89 SetAceessTokenPermission("busCenterTest");
90 GenRanDiscInfo(tmp, size);
91 int32_t ret = RefreshLNN(reinterpret_cast<const char *>(tmp), &g_sInfo, &g_refreshCb);
92 if (ret == SOFTBUS_OK) {
93 StopRefreshLNN(reinterpret_cast<const char *>(tmp), g_sInfo.subscribeId);
94 }
95 return true;
96 }
97 }
98
99 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)100 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
101 {
102 /* Run your code on data */
103 OHOS::DoSomethingInterestingWithMyAPI(data, size);
104 return 0;
105 }
106
107