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 "checkosaccountconstraintenabledstub_fuzzer.h"
17 #include <string>
18 #include <thread>
19 #include <vector>
20 
21 #include "fuzz_data.h"
22 #include "ios_account.h"
23 #include "os_account_manager_service.h"
24 
25 using namespace std;
26 using namespace OHOS::AccountSA;
27 
28 namespace OHOS {
29 const int ENUM_MAX = 5;
30 const int PRIVATE_NUMBER = 3;
31 const int END_NUMBER = 4;
32 const std::u16string IOS_ACCOUNT_DESCRIPTOR = u"ohos.accountfwk.IOsAccount";
CheckOsAccountConstraintEnabledStubFuzzTest(const uint8_t * data,size_t size)33 bool CheckOsAccountConstraintEnabledStubFuzzTest(const uint8_t *data, size_t size)
34 {
35     MessageParcel datas;
36     if ((data == nullptr) || (size == 0) || (!datas.WriteInterfaceToken(IOS_ACCOUNT_DESCRIPTOR))) {
37         return false;
38     }
39     FuzzData fuzzData(data, size);
40     if (!datas.WriteInt32(fuzzData.GetData<int32_t>())) {
41         return false;
42     }
43     if (!datas.WriteString(fuzzData.GenerateRandomString())) {
44         return false;
45     }
46 
47     MessageParcel reply;
48     MessageOption option;
49     auto osAccountManagerService_ = std::make_shared<OsAccountManagerService>();
50     osAccountManagerService_ ->OnRemoteRequest(
51         static_cast<int32_t>(OsAccountInterfaceCode::CHECK_OS_ACCOUNT_CONSTRAINT_ENABLED), datas, reply, option);
52 
53     return true;
54 }
55 
ProcQueryActiveOsAccountIdsStubFuzzTest(const uint8_t * data,size_t size)56 bool ProcQueryActiveOsAccountIdsStubFuzzTest(const uint8_t *data, size_t size)
57 {
58     if ((data == nullptr) || (size == 0)) {
59         return false;
60     }
61 
62     MessageParcel datas;
63     if (!datas.WriteInterfaceToken(IOS_ACCOUNT_DESCRIPTOR)) {
64         return false;
65     }
66 
67     MessageParcel reply;
68     MessageOption option;
69     auto osAccountManagerService_ = std::make_shared<OsAccountManagerService>();
70     osAccountManagerService_->OnRemoteRequest(
71         static_cast<int32_t>(OsAccountInterfaceCode::QUERY_ACTIVE_OS_ACCOUNT_IDS), datas, reply, option);
72 
73     return true;
74 }
75 
ProcQueryAllCreatedOsAccountsStubFuzzTest(const uint8_t * data,size_t size)76 bool ProcQueryAllCreatedOsAccountsStubFuzzTest(const uint8_t *data, size_t size)
77 {
78     if ((data == nullptr) || (size == 0)) {
79         return false;
80     }
81 
82     MessageParcel datas;
83     if (!datas.WriteInterfaceToken(IOS_ACCOUNT_DESCRIPTOR)) {
84         return false;
85     }
86 
87     MessageParcel reply;
88     MessageOption option;
89     auto osAccountManagerService_ = std::make_shared<OsAccountManagerService>();
90     osAccountManagerService_->OnRemoteRequest(
91         static_cast<int32_t>(OsAccountInterfaceCode::QUERY_ALL_CREATED_OS_ACCOUNTS), datas, reply, option);
92 
93     return true;
94 }
95 
ProcQueryCurrentOsAccountStubFuzzTest(const uint8_t * data,size_t size)96 bool ProcQueryCurrentOsAccountStubFuzzTest(const uint8_t *data, size_t size)
97 {
98     if ((data == nullptr) || (size == 0)) {
99         return false;
100     }
101 
102     MessageParcel datas;
103     if (!datas.WriteInterfaceToken(IOS_ACCOUNT_DESCRIPTOR)) {
104         return false;
105     }
106 
107     MessageParcel reply;
108     MessageOption option;
109     auto osAccountManagerService_ = std::make_shared<OsAccountManagerService>();
110     osAccountManagerService_->OnRemoteRequest(
111         static_cast<int32_t>(OsAccountInterfaceCode::QUERY_CURRENT_OS_ACCOUNT), datas, reply, option);
112 
113     return true;
114 }
115 
ProcQueryMaxLoggedInOsAccountNumberStubFuzzTest(const uint8_t * data,size_t size)116 bool ProcQueryMaxLoggedInOsAccountNumberStubFuzzTest(const uint8_t *data, size_t size)
117 {
118     if ((data == nullptr) || (size == 0)) {
119         return false;
120     }
121 
122     MessageParcel datas;
123     if (!datas.WriteInterfaceToken(IOS_ACCOUNT_DESCRIPTOR)) {
124         return false;
125     }
126 
127     MessageParcel reply;
128     MessageOption option;
129     auto osAccountManagerService_ = std::make_shared<OsAccountManagerService>();
130     osAccountManagerService_->OnRemoteRequest(
131         static_cast<int32_t>(OsAccountInterfaceCode::QUERY_MAX_LOGGED_IN_OS_ACCOUNT_NUMBER), datas, reply, option);
132 
133     return true;
134 }
135 
ProcUpdateOsAccountWithFullInfoStubFuzzTest(const uint8_t * data,size_t size)136 bool ProcUpdateOsAccountWithFullInfoStubFuzzTest(const uint8_t *data, size_t size)
137 {
138     if ((data == nullptr) || (size == 0)) {
139         return false;
140     }
141 
142     MessageParcel datas;
143     if (!datas.WriteInterfaceToken(IOS_ACCOUNT_DESCRIPTOR)) {
144         return false;
145     }
146 
147     FuzzData fuzzData(data, size);
148 
149     OsAccountInfo osAccountInfo;
150     osAccountInfo.SetLocalId(fuzzData.GetData<int>());
151     osAccountInfo.SetLocalName(fuzzData.GenerateRandomString());
152     osAccountInfo.SetShortName(fuzzData.GenerateRandomString());
153     int typeNumber = fuzzData.GetData<int>() % ENUM_MAX;
154     if (typeNumber == PRIVATE_NUMBER) {
155         osAccountInfo.SetType(PRIVATE);
156     } else if (typeNumber == END_NUMBER) {
157         osAccountInfo.SetType(END);
158     } else {
159         OsAccountType testType = static_cast<OsAccountType>(typeNumber);
160         osAccountInfo.SetType(testType);
161     }
162     osAccountInfo.SetSerialNumber(fuzzData.GetData<int64_t>());
163 
164     if (!datas.WriteParcelable(&osAccountInfo)) {
165         return false;
166     }
167 
168     MessageParcel reply;
169     MessageOption option;
170     auto osAccountManagerService_ = std::make_shared<OsAccountManagerService>();
171     osAccountManagerService_->OnRemoteRequest(
172         static_cast<int32_t>(OsAccountInterfaceCode::UPDATE_OS_ACCOUNT_WITH_FULL_INFO), datas, reply, option);
173 
174     return true;
175 }
176 } // namespace OHOS
177 
178 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)179 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
180 {
181     /* Run your code on data */
182     OHOS::CheckOsAccountConstraintEnabledStubFuzzTest(data, size);
183     OHOS::ProcQueryActiveOsAccountIdsStubFuzzTest(data, size);
184     OHOS::ProcQueryAllCreatedOsAccountsStubFuzzTest(data, size);
185     OHOS::ProcQueryCurrentOsAccountStubFuzzTest(data, size);
186     OHOS::ProcQueryMaxLoggedInOsAccountNumberStubFuzzTest(data, size);
187     OHOS::ProcUpdateOsAccountWithFullInfoStubFuzzTest(data, size);
188     return 0;
189 }
190