• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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,int32_t operationCode)40     void OnMemberJoin(int64_t requestId, int32_t status, int32_t operationCode) override
41     {
42         (void)requestId;
43         (void)status;
44         (void)operationCode;
45     }
GetConnectAddr(std::string deviceId)46     std::string GetConnectAddr(std::string deviceId) override
47     {
48         (void)deviceId;
49         return "";
50     }
GetPinCode(std::string & code)51     int32_t GetPinCode(std::string &code) override
52     {
53         (void)code;
54         return DM_OK;
55     }
56 };
57 
OnFinishFuzzTest(const uint8_t * data,size_t size)58 void OnFinishFuzzTest(const uint8_t* data, size_t size)
59 {
60     if ((data == nullptr) || (size < sizeof(int64_t) + sizeof(int32_t))) {
61         return;
62     }
63 
64     std::shared_ptr<HiChainConnector> hichainConnector = std::make_shared<HiChainConnector>();
65     hichainConnector->RegisterHiChainCallback(std::make_shared<HiChainConnectorCallbackTest>());
66 
67     FuzzedDataProvider fdp(data, size);
68     int64_t requestId = fdp.ConsumeIntegral<int64_t>();
69     int operationCode = fdp.ConsumeIntegral<int32_t>();
70     std::string str(reinterpret_cast<const char*>(data), size);
71     const char *returnData = str.data();
72     hichainConnector->onFinish(requestId, operationCode, returnData);
73 }
74 }
75 }
76 
77 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)78 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
79 {
80     /* Run your code on data */
81     OHOS::DistributedHardware::OnFinishFuzzTest(data, size);
82 
83     return 0;
84 }