1 /*
2  * Copyright (c) 2023 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 std::u16string ACCOUNT_TOKEN = u"ohos.accountfwk.IDomainAccount";
34 const int32_t PASSWORD_LEN = 8;
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         std::vector<uint8_t> password;
53 
54         for (int32_t i = 0; i < PASSWORD_LEN; i++) {
55             uint8_t bit = fuzzData.GetData<uint8_t>();
56             password.emplace_back(bit);
57         }
58 
59         auto callbackPtr = std::make_shared<TestDomainAuthCallback>();
60         sptr<IDomainAccountCallback> callback = new (std::nothrow) DomainAccountCallbackService(callbackPtr);
61 
62         MessageParcel dataTemp;
63 
64         if (!dataTemp.WriteInterfaceToken(ACCOUNT_TOKEN)) {
65             return false;
66         }
67         if (!dataTemp.WriteInt32(userId)) {
68             return false;
69         }
70         if (!dataTemp.WriteUInt8Vector(password)) {
71             return false;
72         }
73         if (!dataTemp.WriteRemoteObject(callback->AsObject())) {
74             return false;
75         }
76 
77         MessageParcel reply;
78         MessageOption option;
79 
80         uint32_t code = static_cast<uint32_t>(DomainAccountInterfaceCode::DOMAIN_AUTH_USER);
81         auto domainAccountService = std::make_shared<DomainAccountManagerService>();
82         domainAccountService->OnRemoteRequest(code, dataTemp, reply, option);
83 
84         return true;
85     }
86 }
87 
88 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)89 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
90 {
91     /* Run your code on data */
92     OHOS::ProcAuthUserStubFuzzTest(data, size);
93     return 0;
94 }
95 
96