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 "joinlnn_fuzzer.h"
17 #include <cstddef>
18 #include <cstring>
19 #include <securec.h>
20 #include "softbus_access_token_test.h"
21 #include "softbus_bus_center.h"
22 #include "softbus_def.h"
23 #include "softbus_errcode.h"
24
25 namespace OHOS {
OnJoinLNNResult(ConnectionAddr * addr,const char * networkId,int32_t retCode)26 void OnJoinLNNResult(ConnectionAddr *addr, const char *networkId, int32_t retCode)
27 {
28 (void)addr;
29 (void)networkId;
30 (void)retCode;
31 }
32
33 static ConnectionAddr addr;
34 static const int32_t MAX_CONNECT_TYPE = CONNECTION_ADDR_MAX;
35 static const char *IP = "192.168.43.16";
36 static const int32_t port = 6007;
37
GenRandAddr(const uint8_t * data,size_t size)38 void GenRandAddr(const uint8_t *data, size_t size)
39 {
40 addr.type = (ConnectionAddrType)(size % MAX_CONNECT_TYPE);
41 memcpy_s(addr.peerUid, MAX_ACCOUNT_HASH_LEN, data, size);
42 memcpy_s(addr.info.ip.ip, IP_STR_MAX_LEN, IP, strlen(IP));
43 addr.info.ip.port = port + size;
44 }
45
OnLeaveLNNResult(const char * networkId,int32_t retCode)46 static void OnLeaveLNNResult(const char *networkId, int32_t retCode)
47 {
48 (void)networkId;
49 (void)retCode;
50 }
51
JoinLnnFuzzerTest(const uint8_t * data,size_t size)52 bool JoinLnnFuzzerTest(const uint8_t* data, size_t size)
53 {
54 if (data == nullptr || size == 0) {
55 return false;
56 }
57
58 char *tmp = reinterpret_cast<char *>(malloc(size));
59 if (tmp == nullptr) {
60 return false;
61 }
62 if (memset_s(tmp, size, '\0', size) != EOK) {
63 free(tmp);
64 return false;
65 }
66 if (memcpy_s(tmp, size, data, size - 1) != EOK) {
67 free(tmp);
68 return false;
69 }
70
71 SetAceessTokenPermission("busCenterTest");
72 GenRandAddr(data, size);
73 JoinLNN(reinterpret_cast<const char *>(tmp), &addr, OnJoinLNNResult);
74 LeaveLNN(reinterpret_cast<const char *>(tmp), reinterpret_cast<const char *>(tmp), OnLeaveLNNResult);
75 free(tmp);
76 return true;
77 }
78 }
79
80 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)81 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
82 {
83 /* Run your code on data */
84 OHOS::JoinLnnFuzzerTest(data, size);
85 return 0;
86 }