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 "prochasdomainaccountstub_fuzzer.h"
17
18 #include <memory>
19 #include <string>
20 #include <vector>
21 #include "account_log_wrapper.h"
22 #include "domain_account_callback_stub.h"
23 #include "domain_account_common.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;
30 using namespace OHOS::AccountSA;
31 class TestHasDomainInfoCallback : public DomainAccountCallbackStub {
32 public:
TestHasDomainInfoCallback()33 TestHasDomainInfoCallback() {};
34 virtual ~TestHasDomainInfoCallback();
35 ErrCode OnResult(int32_t errCode, const DomainAccountParcel &parcel) override;
36 };
37
~TestHasDomainInfoCallback()38 TestHasDomainInfoCallback::~TestHasDomainInfoCallback()
39 {}
40
OnResult(int32_t errCode,const DomainAccountParcel & parcel)41 ErrCode TestHasDomainInfoCallback::OnResult(int32_t errCode, const DomainAccountParcel &parcel)
42 {
43 return ERR_OK;
44 }
45
46 namespace OHOS {
ProcHasDomainAccountStubFuzzTest(const uint8_t * data,size_t size)47 bool ProcHasDomainAccountStubFuzzTest(const uint8_t* data, size_t size)
48 {
49 if ((data == nullptr) || (size == 0)) {
50 return false;
51 }
52 MessageParcel dataTemp;
53 if (!dataTemp.WriteInterfaceToken(DomainAccountStub::GetDescriptor())) {
54 return false;
55 }
56
57 DomainAccountInfo info;
58 FuzzData fuzzData(data, size);
59 info.domain_ = fuzzData.GenerateString();
60 info.accountName_ = fuzzData.GenerateString();
61 info.accountId_ = fuzzData.GenerateString();
62 if (fuzzData.GetData<bool>()) {
63 if (!dataTemp.WriteParcelable(&info)) {
64 return false;
65 }
66 }
67
68 auto testCallback = new TestHasDomainInfoCallback();
69
70 if (testCallback == nullptr) {
71 ACCOUNT_LOGI("AppAccountStub ProcHasDomainAccount testCallback is null");
72 return false;
73 }
74 if (fuzzData.GetData<bool>()) {
75 if (!dataTemp.WriteRemoteObject(testCallback->AsObject())) {
76 return false;
77 }
78 }
79 MessageParcel reply;
80 MessageOption option;
81 uint32_t code = static_cast<uint32_t>(IDomainAccountIpcCode::COMMAND_HAS_DOMAIN_ACCOUNT);
82 auto domainAccountService = std::make_shared<DomainAccountManagerService>();
83 domainAccountService->OnRemoteRequest(code, dataTemp, reply, option);
84
85 return true;
86 }
87 }
88
89 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)90 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
91 {
92 /* Run your code on data */
93 OHOS::ProcHasDomainAccountStubFuzzTest(data, size);
94 return 0;
95 }
96
97