• 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 "updateosaccountwithfullinfostub_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 MAX_TEST_ID = 10738; // Maximum test
27 const int32_t OS_ACCOUNT_TYPE_NUM = 5;
28 const std::u16string IOS_ACCOUNT_DESCRIPTOR = u"ohos.accountfwk.IOsAccount";
29 
30 namespace OHOS {
UpdateOsAccountWithFullInfoStubFuzzTest(const uint8_t * data,size_t size)31     bool UpdateOsAccountWithFullInfoStubFuzzTest(const uint8_t* data, size_t size)
32     {
33         if ((data == nullptr) || (size == 0)) {
34             return false;
35         }
36 
37         FuzzData fuzzData(data, size);
38         MessageParcel dataParcel;
39         auto useValidDescriptor = fuzzData.GenerateBool();
40         if (useValidDescriptor) {
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             int32_t localId = fuzzData.GetData<bool>() ?
50                             (fuzzData.GetData<int32_t>() % MAX_TEST_ID) : fuzzData.GetData<int32_t>();
51             osAccountInfo.SetLocalId(localId);
52             osAccountInfo.SetLocalName(fuzzData.GenerateString());
53             osAccountInfo.SetShortName(fuzzData.GenerateString());
54             osAccountInfo.SetType(static_cast<OsAccountType>(fuzzData.GetData<int32_t>() % OS_ACCOUNT_TYPE_NUM));
55             osAccountInfo.SetIsCreateCompleted(fuzzData.GetData<bool>());
56             osAccountInfo.SetIsActived(fuzzData.GetData<bool>());
57 
58             if (!dataParcel.WriteParcelable(&osAccountInfo)) {
59                 return false;
60             }
61         }
62 
63         MessageParcel reply;
64         MessageOption option;
65         auto osAccountManagerService_ = std::make_shared<OsAccountManagerService>();
66         osAccountManagerService_->OnRemoteRequest(
67             static_cast<uint32_t>(IOsAccountIpcCode::COMMAND_UPDATE_OS_ACCOUNT_WITH_FULL_INFO),
68             dataParcel, reply, option);
69 
70         return true;
71     }
72 } // namespace OHOS
73 
74 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)75 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
76 {
77     /* Run your code on data */
78     OHOS::UpdateOsAccountWithFullInfoStubFuzzTest(data, size);
79     return 0;
80 }
81