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_request_fuzzer.h"
27
28 namespace OHOS {
29 namespace DistributedHardware {
30 class HiChainConnectorCallbackTest : public IHiChainConnectorCallback {
31 public:
HiChainConnectorCallbackTest()32 HiChainConnectorCallbackTest() {}
~HiChainConnectorCallbackTest()33 virtual ~HiChainConnectorCallbackTest() {}
OnGroupCreated(int64_t requestId,const std::string & groupId)34 void OnGroupCreated(int64_t requestId, const std::string &groupId) override
35 {
36 (void)requestId;
37 (void)groupId;
38 }
OnMemberJoin(int64_t requestId,int32_t status)39 void OnMemberJoin(int64_t requestId, int32_t status) override
40 {
41 (void)requestId;
42 (void)status;
43 }
GetConnectAddr(std::string deviceId)44 std::string GetConnectAddr(std::string deviceId) override
45 {
46 (void)deviceId;
47 return "";
48 }
GetPinCode(int32_t & code)49 int32_t GetPinCode(int32_t &code) override
50 {
51 (void)code;
52 return DM_OK;
53 }
54 };
55
OnRequestFuzzTest(const uint8_t * data,size_t size)56 void OnRequestFuzzTest(const uint8_t* data, size_t size)
57 {
58 if ((data == nullptr) || (size < sizeof(int64_t) + sizeof(int32_t))) {
59 return;
60 }
61
62 FuzzedDataProvider fdp(data, size);
63 int64_t requestId = fdp.ConsumeIntegral<int64_t>();
64 int operationCode = fdp.ConsumeIntegral<int32_t>();
65 const char *reqParams = reinterpret_cast<const char*>(data);
66
67 std::shared_ptr<HiChainConnector> hichainConnector = std::make_shared<HiChainConnector>();
68 hichainConnector->RegisterHiChainCallback(std::make_shared<HiChainConnectorCallbackTest>());
69 hichainConnector->onRequest(requestId, operationCode, reqParams);
70 }
71 }
72 }
73
74 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)75 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
76 {
77 /* Run your code on data */
78 OHOS::DistributedHardware::OnRequestFuzzTest(data, size);
79
80 return 0;
81 }