1 /*
2 * Copyright (c) 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 "auth_pin_auth_fuzzer.h"
17
18 #include <fuzzer/FuzzedDataProvider.h>
19 #include <string>
20
21 #include "device_manager_service_listener.h"
22 #include "dm_auth_state.h"
23 #include "dm_auth_state_machine.h"
24 #include "dm_freeze_process.h"
25
26 namespace OHOS {
27 namespace DistributedHardware {
AuthPinAuthFuzzTest(const uint8_t * data,size_t size)28 void AuthPinAuthFuzzTest(const uint8_t* data, size_t size)
29 {
30 if ((data == nullptr) || (size < sizeof(int32_t))) {
31 return;
32 }
33 FreezeProcess freezeProcess;
34 FuzzedDataProvider fdp(data, size);
35 std::shared_ptr<DmAuthContext> context = std::make_shared<DmAuthContext>();
36 context->transmitData = fdp.ConsumeRandomLengthString();
37 context->requestId = fdp.ConsumeIntegral<int64_t>();
38 context->accesser.userId = fdp.ConsumeIntegral<int32_t>();
39
40 std::shared_ptr<DmAuthState> auth1 = std::make_shared<AuthSrcPinAuthStartState>();
41 std::shared_ptr<DmAuthState> auth2 = std::make_shared<AuthSinkPinAuthDoneState>();
42 std::shared_ptr<AuthSrcPinNegotiateStartState> auth3 = std::make_shared<AuthSrcPinNegotiateStartState>();
43 std::shared_ptr<AuthSrcPinInputState> auth4 = std::make_shared<AuthSrcPinInputState>();
44 std::shared_ptr<DmAuthState> auth5 = std::make_shared<AuthSrcReverseUltrasonicStartState>();
45 std::shared_ptr<DmAuthState> auth6 = std::make_shared<AuthSrcForwardUltrasonicStartState>();
46 std::shared_ptr<DmAuthState> auth7 = std::make_shared<AuthSinkReverseUltrasonicDoneState>();
47 std::shared_ptr<DmAuthState> auth8 = std::make_shared<AuthSinkForwardUltrasonicDoneState>();
48 auth1->GetStateType();
49 auth2->GetStateType();
50 int32_t credType = fdp.ConsumeIntegral<int32_t>();
51 auth3->GetCredIdByCredType(context, credType);
52 context->pinNegotiateStarted = false;
53 auth3->ProcessPinBind(context);
54 context->isAppCredentialVerified = true;
55 context->accesser.isGenerateLnnCredential = false;
56 context->listener = std::make_shared<DeviceManagerServiceListener>();
57 auth4->ShowStartAuthDialog(context);
58 auth5->GetStateType();
59 auth6->GetStateType();
60 auth7->GetStateType();
61 auth8->GetStateType();
62 }
63 }
64 }
65
66 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)67 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
68 {
69 /* Run your code on data */
70 OHOS::DistributedHardware::AuthPinAuthFuzzTest(data, size);
71 return 0;
72 }