• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_manager_service.h"
23 #include "domain_auth_callback.h"
24 #include "domain_auth_callback_service.h"
25 #include "idomain_account.h"
26 
27 using namespace std;
28 using namespace OHOS::AccountSA;
29 
30 namespace OHOS {
31 namespace {
32 const std::u16string ACCOUNT_TOKEN = u"ohos.accountfwk.IDomainAccount";
33 const int32_t PASSWORD_LEN = 8;
34 
35 class TestDomainAuthCallback : public OHOS::AccountSA::DomainAuthCallback {
36 public:
37     TestDomainAuthCallback() = default;
38     virtual ~TestDomainAuthCallback() = default;
OnResult(int32_t resultCode,const OHOS::AccountSA::DomainAuthResult & result)39     void OnResult(int32_t resultCode, const OHOS::AccountSA::DomainAuthResult &result) override
40     {}
41 };
42 }
43 
ProcAuthUserStubFuzzTest(const uint8_t * data,size_t size)44     bool ProcAuthUserStubFuzzTest(const uint8_t* data, size_t size)
45     {
46         if ((data == nullptr) || (size == 0)) {
47             return false;
48         }
49 
50         int32_t userId = static_cast<int32_t>(size);
51         std::vector<uint8_t> password;
52 
53         for (int32_t i = 0; i < PASSWORD_LEN; i++) {
54             uint8_t bit = static_cast<uint8_t>(size);
55             password.emplace_back(bit);
56         }
57 
58         auto callbackPtr = std::make_shared<TestDomainAuthCallback>();
59         sptr<IDomainAuthCallback> callback = new (std::nothrow) DomainAuthCallbackService(callbackPtr);
60 
61         MessageParcel dataTemp;
62 
63         if (!dataTemp.WriteInterfaceToken(ACCOUNT_TOKEN)) {
64             return false;
65         }
66         if (!dataTemp.WriteInt32(userId)) {
67             return false;
68         }
69         if (!dataTemp.WriteUInt8Vector(password)) {
70             return false;
71         }
72         if (!dataTemp.WriteRemoteObject(callback->AsObject())) {
73             return false;
74         }
75 
76         MessageParcel reply;
77         MessageOption option;
78 
79         uint32_t code = static_cast<uint32_t>(DomainAccountInterfaceCode::DOMAIN_AUTH_USER);
80         auto domainAccountService = std::make_shared<DomainAccountManagerService>();
81         domainAccountService->OnRemoteRequest(code, dataTemp, reply, option);
82 
83         return true;
84     }
85 }
86 
87 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)88 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
89 {
90     /* Run your code on data */
91     OHOS::ProcAuthUserStubFuzzTest(data, size);
92     return 0;
93 }
94 
95