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 "createosaccountwithfullinfostub_fuzzer.h"
17 #include "os_account_stub.h"
18 #include "account_log_wrapper.h"
19 #include "fuzz_data.h"
20 #include "os_account_manager_service.h"
21 #include <string>
22 #include <vector>
23
24 using namespace OHOS::AccountSA;
25
26 const int32_t TEST_USER_ID = 1006;
27 const int32_t OS_ACCOUNT_TYPE_NUM = 5;
28 const int64_t TEST_TIME_STAMP = 1695883215000;
29 const std::u16string IOS_ACCOUNT_DESCRIPTOR = u"ohos.accountfwk.IOsAccount";
30 const std::string TEST_HAP_STRING = "test_hap1";
31
32 namespace OHOS {
CreateOsAccountWithFullInfoStubFuzzTest(const uint8_t * data,size_t size)33 bool CreateOsAccountWithFullInfoStubFuzzTest(const uint8_t* data, size_t size)
34 {
35 if ((data == nullptr) || (size == 0)) {
36 return false;
37 }
38
39 FuzzData fuzzData(data, size);
40 MessageParcel dataParcel;
41 if (!dataParcel.WriteInterfaceToken(IOS_ACCOUNT_DESCRIPTOR)) {
42 return false;
43 }
44
45 auto useOsAccountInfo = fuzzData.GenerateBool();
46 if (useOsAccountInfo) {
47 // Create OsAccountInfo object for fuzzing
48 OsAccountInfo osAccountInfo;
49 osAccountInfo.SetLocalId(fuzzData.GetData<int32_t>());
50 osAccountInfo.SetLocalName(fuzzData.GenerateString());
51 osAccountInfo.SetShortName(fuzzData.GenerateString());
52 osAccountInfo.SetType(static_cast<OsAccountType>(fuzzData.GetData<int32_t>() % OS_ACCOUNT_TYPE_NUM));
53 osAccountInfo.SetCreateTime(TEST_TIME_STAMP);
54 osAccountInfo.SetLastLoginTime(TEST_TIME_STAMP);
55
56 if (!dataParcel.WriteParcelable(&osAccountInfo)) {
57 return false;
58 }
59 }
60
61 auto useOptions = fuzzData.GenerateBool();
62 if (useOptions) {
63 CreateOsAccountOptions options;
64 options.disallowedHapList.push_back(TEST_HAP_STRING);
65 if (!options.allowedHapList.has_value()) {
66 options.allowedHapList = std::vector<std::string>();
67 options.allowedHapList->push_back(TEST_HAP_STRING);
68 }
69 if (!dataParcel.WriteParcelable(&options)) {
70 return false;
71 }
72 }
73
74 MessageParcel reply;
75 MessageOption option;
76 auto osAccountManagerService_ = std::make_shared<OsAccountManagerService>();
77 osAccountManagerService_->OnRemoteRequest(
78 static_cast<uint32_t>(IOsAccountIpcCode::COMMAND_CREATE_OS_ACCOUNT_WITH_FULL_INFO),
79 dataParcel, reply, option);
80 return true;
81 }
82
CreateOsAccountWithFullInfoInOsAccountInfoStubFuzzTest(const uint8_t * data,size_t size)83 bool CreateOsAccountWithFullInfoInOsAccountInfoStubFuzzTest(const uint8_t* data, size_t size)
84 {
85 if ((data == nullptr) || (size == 0)) {
86 return false;
87 }
88
89 FuzzData fuzzData(data, size);
90 MessageParcel dataParcel;
91 if (!dataParcel.WriteInterfaceToken(IOS_ACCOUNT_DESCRIPTOR)) {
92 return false;
93 }
94
95 auto useOsAccountInfo = fuzzData.GenerateBool();
96 if (useOsAccountInfo) {
97 // Create OsAccountInfo object for fuzzing
98 OsAccountInfo osAccountInfo;
99 osAccountInfo.SetLocalId(fuzzData.GetData<int32_t>());
100 osAccountInfo.SetLocalName(fuzzData.GenerateString());
101 osAccountInfo.SetShortName(fuzzData.GenerateString());
102 osAccountInfo.SetType(static_cast<OsAccountType>(fuzzData.GetData<int32_t>() % OS_ACCOUNT_TYPE_NUM));
103 osAccountInfo.SetCreateTime(TEST_TIME_STAMP);
104 osAccountInfo.SetLastLoginTime(TEST_TIME_STAMP);
105
106 if (!dataParcel.WriteParcelable(&osAccountInfo)) {
107 return false;
108 }
109 }
110
111 MessageParcel reply;
112 MessageOption option;
113 auto osAccountManagerService_ = std::make_shared<OsAccountManagerService>();
114 osAccountManagerService_->OnRemoteRequest(
115 static_cast<uint32_t>(IOsAccountIpcCode::COMMAND_CREATE_OS_ACCOUNT_WITH_FULL_INFO_IN_OSACCOUNTINFO),
116 dataParcel, reply, option);
117 return true;
118 }
119
SetProfilePhoto()120 void SetProfilePhoto()
121 {
122 MessageParcel datas;
123 datas.WriteInterfaceToken(IOS_ACCOUNT_DESCRIPTOR);
124 datas.WriteInt32(TEST_USER_ID);
125 string photo = "test profile photo";
126 StringRawData stringRawData;
127 stringRawData.Marshalling(photo);
128 datas.WriteUint32(stringRawData.size);
129 datas.WriteRawData(stringRawData.data, stringRawData.size);
130 MessageParcel reply;
131 MessageOption option;
132 auto osAccountManagerService_ = std::make_shared<OsAccountManagerService>();
133
134 osAccountManagerService_->OnRemoteRequest(
135 static_cast<int32_t>(IOsAccountIpcCode::COMMAND_SET_OS_ACCOUNT_PROFILE_PHOTO), datas, reply, option);
136 }
137
CleanOsAccount()138 void CleanOsAccount()
139 {
140 MessageParcel data;
141 MessageParcel reply;
142 MessageOption option;
143 data.WriteInterfaceToken(IOS_ACCOUNT_DESCRIPTOR);
144 data.WriteInt32(TEST_USER_ID);
145
146 auto osAccountManagerService_ = std::make_shared<OsAccountManagerService>();
147 osAccountManagerService_->OnRemoteRequest(
148 static_cast<uint32_t>(IOsAccountIpcCode::COMMAND_IS_OS_ACCOUNT_DEACTIVATING),
149 data, reply, option);
150 MessageParcel datas;
151 datas.WriteInterfaceToken(IOS_ACCOUNT_DESCRIPTOR);
152 datas.WriteInt32(TEST_USER_ID);
153 osAccountManagerService_->OnRemoteRequest(
154 static_cast<uint32_t>(IOsAccountIpcCode::COMMAND_REMOVE_OS_ACCOUNT),
155 datas, reply, option);
156 }
157
UpdateOsAccountWithFullInfo()158 void UpdateOsAccountWithFullInfo()
159 {
160 MessageParcel dataParcel;
161 dataParcel.WriteInterfaceToken(IOS_ACCOUNT_DESCRIPTOR);
162 OsAccountInfo osAccountInfo;
163 osAccountInfo.SetLocalId(TEST_USER_ID);
164 osAccountInfo.SetLocalName("test_full_info_name");
165 osAccountInfo.SetShortName("test_full_info_short_name");
166 osAccountInfo.SetType(OsAccountType::NORMAL);
167 osAccountInfo.SetCreateTime(TEST_TIME_STAMP);
168 osAccountInfo.SetLastLoginTime(TEST_TIME_STAMP);
169 dataParcel.WriteParcelable(&osAccountInfo);
170
171 MessageParcel reply;
172 MessageOption option;
173 auto osAccountManagerService_ = std::make_shared<OsAccountManagerService>();
174 osAccountManagerService_->OnRemoteRequest(
175 static_cast<uint32_t>(IOsAccountIpcCode::COMMAND_UPDATE_OS_ACCOUNT_WITH_FULL_INFO),
176 dataParcel, reply, option);
177 }
178
InitOsAccountWithFullInfo()179 void InitOsAccountWithFullInfo()
180 {
181 MessageParcel dataParcel;
182 dataParcel.WriteInterfaceToken(IOS_ACCOUNT_DESCRIPTOR);
183 OsAccountInfo osAccountInfo;
184 osAccountInfo.SetLocalId(TEST_USER_ID);
185 osAccountInfo.SetLocalName("test_full_info_name");
186 osAccountInfo.SetShortName("test_full_info_short_name");
187 osAccountInfo.SetType(OsAccountType::NORMAL);
188 osAccountInfo.SetCreateTime(TEST_TIME_STAMP);
189 osAccountInfo.SetLastLoginTime(TEST_TIME_STAMP);
190 dataParcel.WriteParcelable(&osAccountInfo);
191 CreateOsAccountOptions options;
192 dataParcel.WriteParcelable(&options);
193 MessageParcel reply;
194 MessageOption option;
195 auto osAccountManagerService_ = std::make_shared<OsAccountManagerService>();
196 osAccountManagerService_->OnRemoteRequest(
197 static_cast<uint32_t>(IOsAccountIpcCode::COMMAND_CREATE_OS_ACCOUNT_WITH_FULL_INFO),
198 dataParcel, reply, option);
199 }
200
InitOsAccountWithFullInfoInOsAccountInfo()201 void InitOsAccountWithFullInfoInOsAccountInfo()
202 {
203 MessageParcel dataParcel;
204 dataParcel.WriteInterfaceToken(IOS_ACCOUNT_DESCRIPTOR);
205 OsAccountInfo osAccountInfo;
206 osAccountInfo.SetLocalId(TEST_USER_ID);
207 osAccountInfo.SetLocalName("test_full_info_name");
208 osAccountInfo.SetShortName("test_full_info_short_name");
209 osAccountInfo.SetType(OsAccountType::NORMAL);
210 osAccountInfo.SetCreateTime(TEST_TIME_STAMP);
211 osAccountInfo.SetLastLoginTime(TEST_TIME_STAMP);
212 dataParcel.WriteParcelable(&osAccountInfo);
213 MessageParcel reply;
214 MessageOption option;
215 auto osAccountManagerService_ = std::make_shared<OsAccountManagerService>();
216 osAccountManagerService_->OnRemoteRequest(
217 static_cast<uint32_t>(IOsAccountIpcCode::COMMAND_CREATE_OS_ACCOUNT_WITH_FULL_INFO_IN_OSACCOUNTINFO),
218 dataParcel, reply, option);
219 }
220
GetCreatedOsAccountsCount()221 void GetCreatedOsAccountsCount()
222 {
223 MessageParcel datas;
224 datas.WriteInterfaceToken(IOS_ACCOUNT_DESCRIPTOR);
225 MessageOption option;
226 MessageParcel reply;
227 auto osAccountManagerService_ = std::make_shared<OsAccountManagerService>();
228 osAccountManagerService_->OnRemoteRequest(
229 static_cast<uint32_t>(IOsAccountIpcCode::COMMAND_GET_CREATED_OS_ACCOUNTS_COUNT),
230 datas, reply, option);
231 }
232
QueryMaxOsAccountNumber()233 void QueryMaxOsAccountNumber()
234 {
235 MessageParcel datas;
236 datas.WriteInterfaceToken(IOS_ACCOUNT_DESCRIPTOR);
237 MessageOption option;
238 MessageParcel reply;
239 auto osAccountManagerService_ = std::make_shared<OsAccountManagerService>();
240 osAccountManagerService_->OnRemoteRequest(
241 static_cast<uint32_t>(IOsAccountIpcCode::COMMAND_QUERY_MAX_OS_ACCOUNT_NUMBER),
242 datas, reply, option);
243 }
244 } // namespace OHOS
245
LLVMFuzzerInitialize(int * argc,char *** argv)246 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
247 {
248 OHOS::InitOsAccountWithFullInfo();
249 OHOS::InitOsAccountWithFullInfoInOsAccountInfo();
250 OHOS::UpdateOsAccountWithFullInfo();
251 OHOS::SetProfilePhoto();
252 OHOS::CleanOsAccount();
253 OHOS::GetCreatedOsAccountsCount();
254 OHOS::QueryMaxOsAccountNumber();
255 return 0;
256 }
257
258 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)259 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
260 {
261 /* Run your code on data */
262 OHOS::CreateOsAccountWithFullInfoStubFuzzTest(data, size);
263 OHOS::CreateOsAccountWithFullInfoInOsAccountInfoStubFuzzTest(data, size);
264 return 0;
265 }
266