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 "joinmetanode_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_errcode.h"
23
24 namespace OHOS {
JoinMetaNodeCb(ConnectionAddr * addr,const char * networkId,int32_t retCode)25 void JoinMetaNodeCb(ConnectionAddr *addr, const char *networkId, int32_t retCode)
26 {
27 (void) addr;
28 (void) networkId;
29 (void) retCode;
30 }
31
LeaveMetaNodeCb(const char * networkId,int32_t retCode)32 void LeaveMetaNodeCb(const char *networkId, int32_t retCode)
33 {
34 (void) networkId;
35 (void) retCode;
36 }
37
38 ConnectionAddr addr;
39 static const int32_t MAX_CONNECT_TYPE = CONNECTION_ADDR_MAX;
40 static const char *g_ip = "192.168.43.16";
41 static const int32_t port = 6007;
42
GenRandAddr(const uint8_t * data,size_t size)43 void GenRandAddr(const uint8_t *data, size_t size)
44 {
45 addr.type = (ConnectionAddrType)(size % MAX_CONNECT_TYPE);
46 memcpy_s(addr.peerUid, MAX_ACCOUNT_HASH_LEN, data, size);
47 memcpy_s(addr.info.ip.ip, IP_STR_MAX_LEN, g_ip, strlen(g_ip));
48 addr.info.ip.port = port + size;
49 }
50
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)51 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
52 {
53 if (data == nullptr || size == 0) {
54 return false;
55 }
56
57 CustomData customData;
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
73 GenRandAddr(data, size);
74 JoinMetaNode(reinterpret_cast<const char *>(tmp), &addr, &customData, JoinMetaNodeCb);
75 LeaveMetaNode(reinterpret_cast<const char *>(tmp),
76 reinterpret_cast<const char *>(tmp), LeaveMetaNodeCb);
77 free(tmp);
78 return true;
79 }
80 }
81
82 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)83 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
84 {
85 /* Run your code on data */
86 OHOS::DoSomethingInterestingWithMyAPI(data, size);
87 return 0;
88 }
89
90