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 "procauthuserstub_fuzzer.h"
17
18 #include <memory>
19 #include <string>
20 #include <vector>
21
22 #include "domain_account_callback.h"
23 #include "domain_account_callback_service.h"
24 #include "domain_account_manager_service.h"
25 #include "fuzz_data.h"
26 #include "idomain_account.h"
27
28 using namespace std;
29 using namespace OHOS::AccountSA;
30
31 namespace OHOS {
32 namespace {
33 const int32_t PASSWORD_LEN = 8;
34 const uint32_t TEST_VECTOR_MAX_SIZE = 102402;
35
36 class TestDomainAuthCallback : public OHOS::AccountSA::DomainAccountCallback {
37 public:
38 TestDomainAuthCallback() = default;
39 virtual ~TestDomainAuthCallback() = default;
OnResult(const int32_t errCode,Parcel & parcel)40 void OnResult(const int32_t errCode, Parcel &parcel) override
41 {}
42 };
43 }
44
ProcAuthUserStubFuzzTest(const uint8_t * data,size_t size)45 bool ProcAuthUserStubFuzzTest(const uint8_t* data, size_t size)
46 {
47 if ((data == nullptr) || (size == 0)) {
48 return false;
49 }
50 FuzzData fuzzData(data, size);
51 int32_t userId = fuzzData.GetData<int32_t>();
52
53 auto callbackPtr = std::make_shared<TestDomainAuthCallback>();
54 sptr<IDomainAccountCallback> callback = new (std::nothrow) DomainAccountCallbackService(callbackPtr);
55
56 MessageParcel dataTemp;
57
58 if (!dataTemp.WriteInterfaceToken(DomainAccountStub::GetDescriptor())) {
59 return false;
60 }
61 if (!dataTemp.WriteInt32(userId)) {
62 return false;
63 }
64 uint32_t passwordSize = fuzzData.GetData<bool>() ? TEST_VECTOR_MAX_SIZE : PASSWORD_LEN;
65 if (!dataTemp.WriteInt32(passwordSize)) {
66 return false;
67 }
68 for (uint32_t i = 0; i < PASSWORD_LEN; i++) {
69 if (!dataTemp.WriteUint8(fuzzData.GetData<uint8_t>())) {
70 return false;
71 }
72 }
73 if (fuzzData.GetData<bool>()) {
74 if (!dataTemp.WriteRemoteObject(callback->AsObject())) {
75 return false;
76 }
77 }
78
79 MessageParcel reply;
80 MessageOption option;
81
82 uint32_t code = static_cast<uint32_t>(IDomainAccountIpcCode::COMMAND_AUTH_USER);
83 auto domainAccountService = std::make_shared<DomainAccountManagerService>();
84 domainAccountService->OnRemoteRequest(code, dataTemp, reply, option);
85
86 return true;
87 }
88 }
89
90 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)91 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
92 {
93 /* Run your code on data */
94 OHOS::ProcAuthUserStubFuzzTest(data, size);
95 return 0;
96 }
97
98