• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "hasdomainaccount_fuzzer.h"
17 
18 #include "account_log_wrapper.h"
19 #include "domain_account_client.h"
20 #include "fuzz_data.h"
21 #include <string>
22 
23 using namespace std;
24 using namespace OHOS::AccountSA;
25 
26 namespace OHOS {
27 namespace {
28 const int ENUM_MAX = 4;
29 const int TEST_ENUM = 5;
30 class TestDomainAccountCallback : public DomainAccountCallback {
31 public:
TestDomainAccountCallback()32     TestDomainAccountCallback() {};
~TestDomainAccountCallback()33     virtual ~TestDomainAccountCallback() {}
OnResult(const int32_t errCode,Parcel & parcel)34     void OnResult(const int32_t errCode, Parcel &parcel) override {}
35 };
36 
37 class TestDomainAccountPlugin : public DomainAccountPlugin {
38 public:
TestDomainAccountPlugin()39     TestDomainAccountPlugin() {}
~TestDomainAccountPlugin()40     virtual ~TestDomainAccountPlugin() {}
Auth(const DomainAccountInfo & info,const std::vector<uint8_t> & password,const std::shared_ptr<DomainAccountCallback> & callback)41     void Auth(const DomainAccountInfo &info, const std::vector<uint8_t> &password,
42         const std::shared_ptr<DomainAccountCallback> &callback) override {}
AuthWithPopup(const AccountSA::DomainAccountInfo & info,const std::shared_ptr<AccountSA::DomainAccountCallback> & callback)43     void AuthWithPopup(const AccountSA::DomainAccountInfo &info,
44         const std::shared_ptr<AccountSA::DomainAccountCallback> &callback) override {}
AuthWithToken(const AccountSA::DomainAccountInfo & info,const std::vector<uint8_t> & token,const std::shared_ptr<AccountSA::DomainAccountCallback> & callback)45     void AuthWithToken(const AccountSA::DomainAccountInfo &info, const std::vector<uint8_t> &token,
46         const std::shared_ptr<AccountSA::DomainAccountCallback> &callback) override {}
GetAuthStatusInfo(const DomainAccountInfo & info,const std::shared_ptr<DomainAccountCallback> & callback)47     void GetAuthStatusInfo(const DomainAccountInfo &info,
48         const std::shared_ptr<DomainAccountCallback> &callback) override {}
GetDomainAccountInfo(const GetDomainAccountInfoOptions & options,const std::shared_ptr<DomainAccountCallback> & callback)49     void GetDomainAccountInfo(const GetDomainAccountInfoOptions &options,
50         const std::shared_ptr<DomainAccountCallback> &callback) override {}
OnAccountBound(const DomainAccountInfo & info,const int32_t localId,const std::shared_ptr<DomainAccountCallback> & callback)51     void OnAccountBound(const DomainAccountInfo &info, const int32_t localId,
52         const std::shared_ptr<DomainAccountCallback> &callback) override {}
OnAccountUnBound(const DomainAccountInfo & info,const std::shared_ptr<DomainAccountCallback> & callback)53     void OnAccountUnBound(const DomainAccountInfo &info,
54         const std::shared_ptr<DomainAccountCallback> &callback) override {}
IsAccountTokenValid(const DomainAccountInfo & info,const std::vector<uint8_t> & token,const std::shared_ptr<DomainAccountCallback> & callback)55     void IsAccountTokenValid(const DomainAccountInfo &info, const std::vector<uint8_t> &token,
56         const std::shared_ptr<DomainAccountCallback> &callback) override {}
GetAccessToken(const DomainAccountInfo & domainInfo,const std::vector<uint8_t> & accountToken,const GetAccessTokenOptions & option,const std::shared_ptr<DomainAccountCallback> & callback)57     void GetAccessToken(const DomainAccountInfo &domainInfo, const std::vector<uint8_t> &accountToken,
58         const GetAccessTokenOptions &option, const std::shared_ptr<DomainAccountCallback> &callback) override {}
59 };
60 }
HasDomainAccountFuzzTest(const uint8_t * data,size_t size)61 bool HasDomainAccountFuzzTest(const uint8_t* data, size_t size)
62 {
63     bool ret = true;
64     if ((data == nullptr) || (size == 0)) {
65         return false;
66     }
67     FuzzData fuzzData(data, size);
68     DomainAccountInfo info;
69     info.domain_ = fuzzData.GenerateString();
70     info.accountName_ = fuzzData.GenerateString();
71     info.accountId_ = fuzzData.GenerateString();
72     info.isAuthenticated = fuzzData.GenerateBool();
73     info.serverConfigId_ = fuzzData.GenerateString();
74     int typeNumber = fuzzData.GenerateBool() ? TEST_ENUM : fuzzData.GetData<int>() % ENUM_MAX;
75     info.status_ = static_cast<DomainAccountStatus>(typeNumber);
76     std::shared_ptr<DomainAccountCallback> callback = std::make_shared<TestDomainAccountCallback>();
77     std::shared_ptr<DomainAccountPlugin> plugin = std::make_shared<TestDomainAccountPlugin>();
78     DomainAccountClient::GetInstance().RegisterPlugin(plugin);
79     ret = DomainAccountClient::GetInstance().HasAccount(info, callback);
80     DomainAccountClient::GetInstance().UnregisterPlugin();
81     return ret == ERR_OK;
82 }
83 }
84 
85 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)86 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
87 {
88     /* Run your code on data */
89     OHOS::HasDomainAccountFuzzTest(data, size);
90     return 0;
91 }
92 
93