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