• 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 "getdomainaccountinfo_fuzzer.h"
17 
18 #include "access_token.h"
19 #include "access_token_error.h"
20 #include "accesstoken_kit.h"
21 #include "domain_account_client.h"
22 #include "fuzz_data.h"
23 #include "nativetoken_kit.h"
24 #include "token_setproc.h"
25 
26 using namespace std;
27 using namespace OHOS::AccountSA;
28 using namespace OHOS::Security::AccessToken;
29 
30 namespace OHOS {
31 namespace {
32 const int ENUM_MAX = 4;
33 class TestDomainAccountCallback : public DomainAccountCallback {
34 public:
35     TestDomainAccountCallback() = default;
36     virtual ~TestDomainAccountCallback() = default;
OnResult(const int32_t errCode,Parcel & parcel)37     void OnResult(const int32_t errCode, Parcel &parcel) override {}
38 };
39 }
40 
GetDomainAccountInfoFuzzTest(const uint8_t * data,size_t size)41 bool GetDomainAccountInfoFuzzTest(const uint8_t* data, size_t size)
42 {
43     bool result = false;
44     if ((data == nullptr) || (size == 0)) {
45         return false;
46     }
47     FuzzData fuzzData(data, size);
48     DomainAccountInfo info;
49     info.domain_ = fuzzData.GenerateString();
50     info.accountName_ = fuzzData.GenerateString();
51     info.accountId_ = fuzzData.GenerateString();
52     info.isAuthenticated = fuzzData.GenerateBool();
53     info.serverConfigId_ = fuzzData.GenerateString();
54     int typeNumber = fuzzData.GetData<int>() % ENUM_MAX;
55     info.status_ = static_cast<DomainAccountStatus>(typeNumber);
56     std::shared_ptr<DomainAccountCallback> callback = nullptr;
57     auto useCallback = fuzzData.GenerateBool();
58     if (useCallback) {
59         callback = std::make_shared<TestDomainAccountCallback>();
60     }
61 
62     result = DomainAccountClient::GetInstance().GetDomainAccountInfo(info, callback);
63     return result == ERR_OK;
64 }
65 }
66 
NativeTokenGet()67 void NativeTokenGet()
68 {
69     uint64_t tokenId;
70     const char **perms = new const char *[1];
71     perms[0] = "ohos.permission.GET_DOMAIN_ACCOUNTS";
72     NativeTokenInfoParams infoInstance = {
73         .dcapsNum = 0,
74         .permsNum = 1,
75         .aclsNum = 0,
76         .perms = perms,
77         .acls = nullptr,
78         .aplStr = "system_core",
79     };
80     infoInstance.processName = "RegisterInputer";
81     tokenId = GetAccessTokenId(&infoInstance);
82     SetSelfTokenID(tokenId);
83     AccessTokenKit::ReloadNativeTokenInfo();
84     delete[] perms;
85 }
86 
87 /* Fuzzer entry point */
LLVMFuzzerInitialize(int * argc,char *** argv)88 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
89 {
90     NativeTokenGet();
91     return 0;
92 }
93 
94 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)95 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
96 {
97     /* Run your code on data */
98     OHOS::GetDomainAccountInfoFuzzTest(data, size);
99     return 0;
100 }
101 
102