• 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 "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.GenerateString())) {
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>(IOsAccountIpcCode::COMMAND_CHECK_OS_ACCOUNT_CONSTRAINT_ENABLED), datas, reply, option);
52 
53     return true;
54 }
55 
ProcUpdateOsAccountWithFullInfoStubFuzzTest(const uint8_t * data,size_t size)56 bool ProcUpdateOsAccountWithFullInfoStubFuzzTest(const uint8_t *data, size_t size)
57 {
58     if ((data == nullptr) || (size == 0)) {
59         return false;
60     }
61     MessageParcel datas;
62     if (!datas.WriteInterfaceToken(IOS_ACCOUNT_DESCRIPTOR)) {
63         return false;
64     }
65     FuzzData fuzzData(data, size);
66     bool isInitWithSerialNumber = fuzzData.GetData<bool>();
67     auto localId = fuzzData.GetData<int>();
68     auto localName = fuzzData.GenerateString();
69     auto serialNumber = fuzzData.GetData<int64_t>();
70     OsAccountInfo osAccountInfo;
71     if (isInitWithSerialNumber) {
72         osAccountInfo = OsAccountInfo(localId, localName, PRIVATE, serialNumber);
73     } else {
74         osAccountInfo = OsAccountInfo(localId, localName, PRIVATE);
75     }
76     osAccountInfo.SetLocalId(fuzzData.GetData<int>());
77     osAccountInfo.SetLocalName(fuzzData.GenerateString());
78     osAccountInfo.SetShortName(fuzzData.GenerateString());
79     int typeNumber = fuzzData.GetData<int>() % ENUM_MAX;
80     if (typeNumber == PRIVATE_NUMBER) {
81         osAccountInfo.SetType(PRIVATE);
82     } else if (typeNumber == END_NUMBER) {
83         osAccountInfo.SetType(END);
84     } else {
85         OsAccountType testType = static_cast<OsAccountType>(typeNumber);
86         osAccountInfo.SetType(testType);
87     }
88     osAccountInfo.SetSerialNumber(fuzzData.GetData<int64_t>());
89     osAccountInfo.SetCredentialId(fuzzData.GetData<uint64_t>());
90     osAccountInfo.SetIsDataRemovable(fuzzData.GetData<bool>());
91     osAccountInfo.SetCreatorType(fuzzData.GetData<int32_t>());
92     osAccountInfo.SetToBeRemoved(fuzzData.GetData<bool>());
93     if (!datas.WriteParcelable(&osAccountInfo)) {
94         return false;
95     }
96     MessageParcel reply;
97     MessageOption option;
98     auto osAccountManagerService_ = std::make_shared<OsAccountManagerService>();
99     osAccountManagerService_->OnRemoteRequest(
100         static_cast<int32_t>(IOsAccountIpcCode::COMMAND_UPDATE_OS_ACCOUNT_WITH_FULL_INFO), datas, reply, option);
101     return true;
102 }
103 } // namespace OHOS
104 
105 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)106 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
107 {
108     /* Run your code on data */
109     OHOS::CheckOsAccountConstraintEnabledStubFuzzTest(data, size);
110     OHOS::ProcUpdateOsAccountWithFullInfoStubFuzzTest(data, size);
111     return 0;
112 }
113