• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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 "createosaccountfordomainstub_fuzzer.h"
17 #include <string>
18 #include <thread>
19 #include <vector>
20 
21 #include "domain_account_callback_service.h"
22 #include "fuzz_data.h"
23 #include "ios_account.h"
24 #include "os_account_info_json_parser.h"
25 #include "os_account_manager_service.h"
26 
27 using namespace std;
28 using namespace OHOS::AccountSA;
29 
30 namespace OHOS {
31 const int CONSTANTS_NUMBER_FIVE = 5;
32 const std::u16string IOS_ACCOUNT_DESCRIPTOR = u"ohos.accountfwk.IOsAccount";
33 const std::string TEST_SHORT_NAME = "test_short_name";
CreateOsAccountForDomainStubFuzzTest(const uint8_t * data,size_t size)34 bool CreateOsAccountForDomainStubFuzzTest(const uint8_t *data, size_t size)
35 {
36     if ((data == nullptr) || (size == 0)) {
37         return false;
38     }
39 
40     FuzzData fuzzData(data, size);
41     MessageParcel datas;
42     datas.WriteInterfaceToken(IOS_ACCOUNT_DESCRIPTOR);
43     OsAccountType testType = static_cast<OsAccountType>(fuzzData.GetData<size_t>() % CONSTANTS_NUMBER_FIVE);
44     if (!datas.WriteInt32(testType)) {
45         return false;
46     }
47     auto useDomainAccountInfo = fuzzData.GenerateBool();
48     if (useDomainAccountInfo) {
49         DomainAccountInfo domainInfo(fuzzData.GenerateString(), fuzzData.GenerateString());
50         if (!datas.WriteParcelable(&domainInfo)) {
51             return false;
52         }
53     }
54     auto useDomainAccountCallback = fuzzData.GenerateBool();
55     if (useDomainAccountCallback) {
56         std::shared_ptr<DomainAccountCallback> callbackPtr = nullptr;
57         sptr<DomainAccountCallbackService> callbackService =
58             new (std::nothrow) DomainAccountCallbackService(callbackPtr);
59         if ((callbackService == nullptr) || (!datas.WriteRemoteObject(callbackService->AsObject()))) {
60             return false;
61         }
62     }
63     auto useCreateOsAccountForDomainOptions = fuzzData.GenerateBool();
64     if (useCreateOsAccountForDomainOptions) {
65         CreateOsAccountForDomainOptions options;
66         options.shortName = TEST_SHORT_NAME;
67         options.hasShortName = true;
68         if (!datas.WriteParcelable(&options)) {
69             return false;
70         }
71     }
72 
73     MessageParcel reply;
74     MessageOption option;
75 
76     auto osAccountManagerService_ = std::make_shared<OsAccountManagerService>();
77 
78     osAccountManagerService_ ->OnRemoteRequest(
79         static_cast<int32_t>(IOsAccountIpcCode::COMMAND_CREATE_OS_ACCOUNT_FOR_DOMAIN), datas, reply, option);
80 
81     return true;
82 }
83 
CreateOsAccountForDomainWithoutOptionsStubFuzzTest(const uint8_t * data,size_t size)84 bool CreateOsAccountForDomainWithoutOptionsStubFuzzTest(const uint8_t *data, size_t size)
85 {
86     if ((data == nullptr) || (size == 0)) {
87         return false;
88     }
89 
90     FuzzData fuzzData(data, size);
91     MessageParcel datas;
92     datas.WriteInterfaceToken(IOS_ACCOUNT_DESCRIPTOR);
93     OsAccountType testType = static_cast<OsAccountType>(fuzzData.GetData<size_t>() % CONSTANTS_NUMBER_FIVE);
94     if (!datas.WriteInt32(testType)) {
95         return false;
96     }
97     auto useDomainAccountInfo = fuzzData.GenerateBool();
98     if (useDomainAccountInfo) {
99         DomainAccountInfo domainInfo(fuzzData.GenerateString(), fuzzData.GenerateString());
100         if (!datas.WriteParcelable(&domainInfo)) {
101             return false;
102         }
103     }
104     auto useDomainAccountCallback = fuzzData.GenerateBool();
105     if (useDomainAccountCallback) {
106         std::shared_ptr<DomainAccountCallback> callbackPtr = nullptr;
107         sptr<DomainAccountCallbackService> callbackService =
108             new (std::nothrow) DomainAccountCallbackService(callbackPtr);
109         if ((callbackService == nullptr) || (!datas.WriteRemoteObject(callbackService->AsObject()))) {
110             return false;
111         }
112     }
113 
114     MessageParcel reply;
115     MessageOption option;
116 
117     auto osAccountManagerService_ = std::make_shared<OsAccountManagerService>();
118 
119     osAccountManagerService_ ->OnRemoteRequest(static_cast<int32_t>(
120         IOsAccountIpcCode::COMMAND_CREATE_OS_ACCOUNT_FOR_DOMAIN_IN_INT_IN_DOMAINACCOUNTINFO_IN_IDOMAINACCOUNTCALLBACK),
121         datas, reply, option);
122 
123     return true;
124 }
125 
SendRequestWithAccountId(int32_t code,int id)126 void SendRequestWithAccountId(int32_t code, int id)
127 {
128     MessageParcel datas;
129     datas.WriteInterfaceToken(IOS_ACCOUNT_DESCRIPTOR);
130     if (!datas.WriteInt32(id)) {
131         return;
132     }
133     MessageOption option;
134     MessageParcel reply;
135     auto osAccountManagerService_ = std::make_shared<OsAccountManagerService>();
136     osAccountManagerService_->OnRemoteRequest(code, datas, reply, option);
137 }
138 
SendRequestWithCode(int32_t code)139 void SendRequestWithCode(int32_t code)
140 {
141     MessageParcel datas;
142     datas.WriteInterfaceToken(IOS_ACCOUNT_DESCRIPTOR);
143     MessageOption option;
144     MessageParcel reply;
145     auto osAccountManagerService_ = std::make_shared<OsAccountManagerService>();
146     osAccountManagerService_->OnRemoteRequest(code, datas, reply, option);
147 }
148 
ReadOsAccountInfo(MessageParcel & data,OsAccountInfo & accountInfo)149 bool ReadOsAccountInfo(MessageParcel &data, OsAccountInfo &accountInfo)
150 {
151     StringRawData stringRawData;
152 
153     data.ReadInt32();
154     if (!data.ReadUint32(stringRawData.size)) {
155         return false;
156     }
157     auto readstringRawData = data.ReadRawData(stringRawData.size);
158     if (readstringRawData == nullptr) {
159         return false;
160     }
161     ErrCode stringRawDataoutError = stringRawData.RawDataCpy(readstringRawData);
162     if (stringRawDataoutError) {
163         return false;
164     }
165 
166     std::string accountStr;
167     stringRawData.Unmarshalling(accountStr);
168     auto jsonObject = CreateJsonFromString(accountStr);
169     if (jsonObject == nullptr) {
170         return false;
171     }
172     FromJson(jsonObject.get(), accountInfo);
173     return true;
174 }
175 
CheckOsAccountStatus()176 void CheckOsAccountStatus()
177 {
178     OsAccountInfo osAccountInfoOne;
179     OsAccountType testType = OsAccountType::NORMAL;
180     std::string accountName = "fordomainstub_test_account";
181     MessageParcel data;
182     MessageParcel reply;
183     MessageOption option;
184     data.WriteInterfaceToken(IOS_ACCOUNT_DESCRIPTOR);
185     data.WriteString16(Str8ToStr16(accountName));
186     data.WriteInt32(static_cast<int32_t>(testType));
187     auto osAccountManagerService_ = std::make_shared<OsAccountManagerService>();
188     osAccountManagerService_->OnRemoteRequest(
189         static_cast<int32_t>(IOsAccountIpcCode::COMMAND_CREATE_OS_ACCOUNT), data, reply, option);
190     if (!ReadOsAccountInfo(reply, osAccountInfoOne)) {
191         return;
192     }
193     int32_t localId = osAccountInfoOne.GetLocalId();
194     SendRequestWithAccountId(static_cast<int32_t>(IOsAccountIpcCode::COMMAND_ACTIVATE_OS_ACCOUNT), localId);
195     SendRequestWithAccountId(static_cast<int32_t>(IOsAccountIpcCode::COMMAND_DEACTIVATE_OS_ACCOUNT), localId);
196     SendRequestWithCode(static_cast<int32_t>(IOsAccountIpcCode::COMMAND_QUERY_CURRENT_OS_ACCOUNT));
197 }
198 } // namespace OHOS
199 
LLVMFuzzerInitialize(int * argc,char *** argv)200 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
201 {
202     OHOS::CheckOsAccountStatus();
203     return 0;
204 }
205 
206 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)207 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
208 {
209     /* Run your code on data */
210     OHOS::CreateOsAccountForDomainStubFuzzTest(data, size);
211     OHOS::CreateOsAccountForDomainWithoutOptionsStubFuzzTest(data, size);
212     return 0;
213 }
214