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 <fuzzer/FuzzedDataProvider.h>
18 #include "device_manager_service_listener.h"
19 #include "auth_message_processor.h"
20 #include "dm_auth_manager.h"
21 #include "dm_auth_manager_fuzzer.h"
22 #include "dm_timer.h"
23 #include "hichain_connector.h"
24
25 namespace OHOS {
26 namespace DistributedHardware {
27
28 std::shared_ptr<SoftbusConnector> g_softbusConnector = std::make_shared<SoftbusConnector>();
29 std::shared_ptr<IDeviceManagerServiceListener> g_listener = std::make_shared<DeviceManagerServiceListener>();
30 std::shared_ptr<HiChainConnector> g_hiChainConnector = std::make_shared<HiChainConnector>();
31 std::shared_ptr<HiChainAuthConnector> g_hiChainAuthConnector = std::make_shared<HiChainAuthConnector>();
32 std::shared_ptr<AuthRequestState> g_authRequestState = std::make_shared<AuthRequestNetworkState>();
33 std::shared_ptr<AuthResponseState> g_authResponseState = std::make_shared<AuthResponseInitState>();
34 std::shared_ptr<DmAuthManager> g_authManager =
35 std::make_shared<DmAuthManager>(g_softbusConnector, g_hiChainConnector, g_listener, g_hiChainAuthConnector);
36
37 int32_t g_sessionId = 1;
38 int32_t g_sessionSide = 0;
39 int32_t g_result = 1;
40 int32_t g_authType = 1;
41 int32_t g_status = 1;
42 std::string g_pinCode = "111111";
43 int32_t g_action = 1;
44 int32_t g_userId = 1;
45 int32_t g_pageId = 1;
46 int32_t g_reason = 1;
47 int32_t g_state = 1;
48 int64_t g_requestId = 1;
49 int64_t g_localSessionId = 1;
50
51 std::map<std::string, std::string> g_bindParam;
52
53 PeerTargetId g_targetId = {
54 .deviceId = "deviceId",
55 .brMac = "brMac",
56 .bleMac = "bleMac",
57 .wifiIp = "wifiIp",
58 };
59
DmAuthManagerFuzzTest(const uint8_t * data,size_t size)60 void DmAuthManagerFuzzTest(const uint8_t* data, size_t size)
61 {
62 if ((data == nullptr) || (size < sizeof(int32_t))) {
63 return;
64 }
65 FuzzedDataProvider fdp(data, size);
66 std::string str(reinterpret_cast<const char*>(data), size);
67 int32_t bindLevel = fdp.ConsumeIntegral<int32_t>();
68 int32_t operationCode = fdp.ConsumeIntegral<int32_t>();
69 g_authManager->authMessageProcessor_ = std::make_shared<AuthMessageProcessor>(g_authManager);
70 g_authManager->authMessageProcessor_->authResponseContext_ = std::make_shared<DmAuthResponseContext>();
71 g_authManager->authRequestContext_ = std::make_shared<DmAuthRequestContext>();
72 g_authManager->authRequestState_ = std::make_shared<AuthRequestFinishState>();
73 g_authManager->authResponseContext_ = std::make_shared<DmAuthResponseContext>();
74 g_authManager->authResponseState_ = std::make_shared<AuthResponseConfirmState>();
75 g_authManager->hiChainAuthConnector_ = std::make_shared<HiChainAuthConnector>();
76 g_authManager->softbusConnector_ = std::make_shared<SoftbusConnector>();
77 g_authManager->softbusConnector_->GetSoftbusSession()->
78 RegisterSessionCallback(std::shared_ptr<ISoftbusSessionCallback>(g_authManager));
79 g_authManager->timer_ = std::make_shared<DmTimer>();
80
81 g_authManager->InitAuthState(str, g_authType, str, str);
82 g_authManager->OnSessionOpened(g_sessionId, g_sessionSide, g_result);
83 g_authManager->AuthenticateDevice(str, g_authType, str, str);
84 g_authManager->ImportAuthCode(str, str);
85 g_authManager->BindTarget(str, g_targetId, g_bindParam, g_sessionId, g_localSessionId);
86 g_authManager->ShowConfigDialog();
87 g_authManager->ShowAuthInfoDialog();
88 g_authManager->ShowStartAuthDialog();
89 g_authManager->OnDataReceived(g_sessionId, str);
90 g_authManager->OnGroupCreated(g_requestId, str);
91 g_authManager->OnMemberJoin(g_requestId, g_status, operationCode);
92 g_authManager->StartNegotiate(g_sessionId);
93 g_authManager->RespNegotiate(g_sessionId);
94 g_authManager->SendAuthRequest(g_sessionId);
95 g_authManager->SetAuthRequestState(g_authRequestState);
96 g_authManager->SetAuthResponseState(g_authResponseState);
97 g_authManager->StartAuthProcess(g_action);
98 g_authManager->StartRespAuthProcess();
99 g_authManager->CreateGroup();
100 g_authManager->ProcessPincode(g_pinCode);
101 g_authManager->SetPageId(g_pageId);
102 g_authManager->SetReasonAndFinish(g_reason, g_state);
103 g_authManager->IsIdenticalAccount();
104 g_authManager->OnSessionClosed(g_sessionId);
105 g_authManager->OnUserOperation(g_action, str);
106 g_authManager->GetConnectAddr(str);
107 g_authManager->HandleAuthenticateTimeout(str);
108 g_authManager->RegisterUiStateCallback(str);
109 g_authManager->UnRegisterUiStateCallback(str);
110 g_authManager->UnAuthenticateDevice(str, str, bindLevel);
111 }
112 }
113 }
114
115 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)116 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
117 {
118 /* Run your code on data */
119 OHOS::DistributedHardware::DmAuthManagerFuzzTest(data, size);
120
121 return 0;
122 }