1 /*
2 * Copyright (c) 2023-2024 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 <string>
17 #include <cstdlib>
18 #include <random>
19 #include <vector>
20 #include <fuzzer/FuzzedDataProvider.h>
21
22 #include "device_manager_service_listener.h"
23 #include "dm_auth_manager.h"
24 #include "hichain_connector.h"
25
26 #include "on_finish_fuzzer.h"
27
28 namespace OHOS {
29 namespace DistributedHardware {
30
31 class HiChainConnectorCallbackTest : public IHiChainConnectorCallback {
32 public:
HiChainConnectorCallbackTest()33 HiChainConnectorCallbackTest() {}
~HiChainConnectorCallbackTest()34 virtual ~HiChainConnectorCallbackTest() {}
OnGroupCreated(int64_t requestId,const std::string & groupId)35 void OnGroupCreated(int64_t requestId, const std::string &groupId) override
36 {
37 (void)requestId;
38 (void)groupId;
39 }
OnMemberJoin(int64_t requestId,int32_t status)40 void OnMemberJoin(int64_t requestId, int32_t status) override
41 {
42 (void)requestId;
43 (void)status;
44 }
GetConnectAddr(std::string deviceId)45 std::string GetConnectAddr(std::string deviceId) override
46 {
47 (void)deviceId;
48 return "";
49 }
GetPinCode(int32_t & code)50 int32_t GetPinCode(int32_t &code) override
51 {
52 (void)code;
53 return DM_OK;
54 }
55 };
56
OnFinishFuzzTest(const uint8_t * data,size_t size)57 void OnFinishFuzzTest(const uint8_t* data, size_t size)
58 {
59 if ((data == nullptr) || (size < sizeof(int64_t) + sizeof(int32_t))) {
60 return;
61 }
62
63 std::shared_ptr<HiChainConnector> hichainConnector = std::make_shared<HiChainConnector>();
64 hichainConnector->RegisterHiChainCallback(std::make_shared<HiChainConnectorCallbackTest>());
65
66 FuzzedDataProvider fdp(data, size);
67 int64_t requestId = fdp.ConsumeIntegral<int64_t>();
68 int operationCode = fdp.ConsumeIntegral<int32_t>();
69 std::string str(reinterpret_cast<const char*>(data), size);
70 const char *returnData = str.data();
71 hichainConnector->onFinish(requestId, operationCode, returnData);
72 }
73 }
74 }
75
76 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)77 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
78 {
79 /* Run your code on data */
80 OHOS::DistributedHardware::OnFinishFuzzTest(data, size);
81
82 return 0;
83 }