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