• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.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>(OsAccountInterfaceCode::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 
62     MessageParcel datas;
63     if (!datas.WriteInterfaceToken(IOS_ACCOUNT_DESCRIPTOR)) {
64         return false;
65     }
66 
67     FuzzData fuzzData(data, size);
68 
69     OsAccountInfo osAccountInfo;
70     osAccountInfo.SetLocalId(fuzzData.GetData<int>());
71     osAccountInfo.SetLocalName(fuzzData.GenerateString());
72     osAccountInfo.SetShortName(fuzzData.GenerateString());
73     int typeNumber = fuzzData.GetData<int>() % ENUM_MAX;
74     if (typeNumber == PRIVATE_NUMBER) {
75         osAccountInfo.SetType(PRIVATE);
76     } else if (typeNumber == END_NUMBER) {
77         osAccountInfo.SetType(END);
78     } else {
79         OsAccountType testType = static_cast<OsAccountType>(typeNumber);
80         osAccountInfo.SetType(testType);
81     }
82     osAccountInfo.SetSerialNumber(fuzzData.GetData<int64_t>());
83 
84     if (!datas.WriteParcelable(&osAccountInfo)) {
85         return false;
86     }
87 
88     MessageParcel reply;
89     MessageOption option;
90     auto osAccountManagerService_ = std::make_shared<OsAccountManagerService>();
91     osAccountManagerService_->OnRemoteRequest(
92         static_cast<int32_t>(OsAccountInterfaceCode::UPDATE_OS_ACCOUNT_WITH_FULL_INFO), datas, reply, option);
93 
94     return true;
95 }
96 } // namespace OHOS
97 
98 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)99 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
100 {
101     /* Run your code on data */
102     OHOS::CheckOsAccountConstraintEnabledStubFuzzTest(data, size);
103     OHOS::ProcUpdateOsAccountWithFullInfoStubFuzzTest(data, size);
104     return 0;
105 }
106