1 /*
2 * Copyright (c) 2023-2024 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 "cmdsetohosaccountinfostub_fuzzer.h"
17
18 #include <string>
19 #include <vector>
20
21 #define private public
22 #include "account_mgr_service.h"
23 #undef private
24 #include "fuzz_data.h"
25 #include "iaccount.h"
26
27 using namespace std;
28 using namespace OHOS::AccountSA;
29
30 namespace OHOS {
31 namespace {
32 const std::u16string ACCOUNT_TOKEN = u"ohos.accountfwk.IAccount";
WriteOhosAccountInfo(MessageParcel & data,const OhosAccountInfo & ohosAccountInfo)33 bool WriteOhosAccountInfo(MessageParcel &data, const OhosAccountInfo &ohosAccountInfo)
34 {
35 if (!data.WriteString16(Str8ToStr16(ohosAccountInfo.name_))) {
36 ACCOUNT_LOGE("write name failed!");
37 return false;
38 }
39 if (!data.WriteString16(Str8ToStr16(ohosAccountInfo.uid_))) {
40 ACCOUNT_LOGE("write uid failed!");
41 return false;
42 }
43 if (!data.WriteString16(Str8ToStr16(ohosAccountInfo.GetRawUid()))) {
44 ACCOUNT_LOGE("write rawUid failed!");
45 return false;
46 }
47 if (!data.WriteInt32(ohosAccountInfo.status_)) {
48 ACCOUNT_LOGE("write status failed!");
49 return false;
50 }
51 if (!data.WriteString16(Str8ToStr16(ohosAccountInfo.nickname_))) {
52 ACCOUNT_LOGE("write nickname failed!");
53 return false;
54 }
55 if (!data.WriteInt32(ohosAccountInfo.avatar_.size() + 1)) {
56 ACCOUNT_LOGE("write avatarSize failed!");
57 return false;
58 }
59 if (!data.WriteRawData(ohosAccountInfo.avatar_.c_str(), ohosAccountInfo.avatar_.size() + 1)) {
60 ACCOUNT_LOGE("write avatar failed!");
61 return false;
62 }
63 if (!data.WriteParcelable(&(ohosAccountInfo.scalableData_))) {
64 ACCOUNT_LOGE("write scalableData failed!");
65 return false;
66 }
67 return true;
68 }
69 }
CmdSetOhosAccountInfoStubFuzzTest(const uint8_t * data,size_t size)70 bool CmdSetOhosAccountInfoStubFuzzTest(const uint8_t* data, size_t size)
71 {
72 if ((data == nullptr) || (size == 0)) {
73 return false;
74 }
75 MessageParcel dataTemp;
76 if (!dataTemp.WriteInterfaceToken(ACCOUNT_TOKEN)) {
77 return false;
78 }
79 FuzzData fuzzData(data, size);
80 OhosAccountInfo ohosAccountInfo;
81 ohosAccountInfo.name_ = fuzzData.GenerateRandomString();
82 ohosAccountInfo.uid_ = fuzzData.GenerateRandomString();
83 ohosAccountInfo.status_ = fuzzData.GetData<int32_t>();
84 ohosAccountInfo.callingUid_ = fuzzData.GetData<int32_t>();
85 ohosAccountInfo.nickname_ = fuzzData.GenerateRandomString();
86 ohosAccountInfo.avatar_ = fuzzData.GenerateRandomString();
87 if (!WriteOhosAccountInfo(dataTemp, ohosAccountInfo)) {
88 ACCOUNT_LOGE("Write ohosAccountInfo failed!");
89 return false;
90 }
91 std::string eventStr = fuzzData.GenerateRandomString();
92 if (!dataTemp.WriteString16(Str8ToStr16(eventStr))) {
93 ACCOUNT_LOGE("Write eventStr failed!");
94 return false;
95 }
96 MessageParcel reply;
97 MessageOption option;
98 uint32_t code = static_cast<uint32_t>(AccountMgrInterfaceCode::SET_OHOS_ACCOUNT_INFO);
99 DelayedRefSingleton<AccountMgrService>::GetInstance().state_ = ServiceRunningState::STATE_RUNNING;
100 DelayedRefSingleton<AccountMgrService>::GetInstance().OnRemoteRequest(code, dataTemp, reply, option);
101
102 return true;
103 }
104 }
105
106 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)107 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
108 {
109 /* Run your code on data */
110 OHOS::CmdSetOhosAccountInfoStubFuzzTest(data, size);
111 return 0;
112 }
113
114