1 /*
2 * Copyright (c) 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 "cmdsetohosaccountinfobyuseridstub_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 }
70
CmdSetOhosAccountInfoByUserIdStubFuzzTest(const uint8_t * data,size_t size)71 bool CmdSetOhosAccountInfoByUserIdStubFuzzTest(const uint8_t *data, size_t size)
72 {
73 if ((data == nullptr) || (size == 0)) {
74 return false;
75 }
76 MessageParcel dataTemp;
77
78 if (!dataTemp.WriteInterfaceToken(ACCOUNT_TOKEN)) {
79 return false;
80 }
81
82 FuzzData fuzzData(data, size);
83 OhosAccountInfo ohosAccountInfo;
84 ohosAccountInfo.name_ = fuzzData.GenerateRandomString();
85 ohosAccountInfo.uid_ = fuzzData.GenerateRandomString();
86 ohosAccountInfo.status_ = fuzzData.GetData<int32_t>();
87 ohosAccountInfo.callingUid_ = fuzzData.GetData<int32_t>();
88 ohosAccountInfo.nickname_ = fuzzData.GenerateRandomString();
89 ohosAccountInfo.avatar_ = fuzzData.GenerateRandomString();
90
91 if (!WriteOhosAccountInfo(dataTemp, ohosAccountInfo)) {
92 ACCOUNT_LOGE("Write ohosAccountInfo failed!");
93 return false;
94 }
95
96 std::string eventStr = fuzzData.GenerateRandomString();
97 if (!dataTemp.WriteString16(Str8ToStr16(eventStr))) {
98 ACCOUNT_LOGE("Write eventStr failed!");
99 return false;
100 }
101
102 int32_t userId = fuzzData.GetData<int32_t>();
103 if (!dataTemp.WriteInt32(userId)) {
104 return false;
105 }
106
107 MessageParcel reply;
108 MessageOption option;
109 uint32_t code = static_cast<uint32_t>(AccountMgrInterfaceCode::SET_OHOS_ACCOUNT_INFO_BY_USER_ID);
110 DelayedRefSingleton<AccountMgrService>::GetInstance().state_ = ServiceRunningState::STATE_RUNNING;
111 DelayedRefSingleton<AccountMgrService>::GetInstance().OnRemoteRequest(code, dataTemp, reply, option);
112
113 return true;
114 }
115 }
116
117 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)118 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
119 {
120 /* Run your code on data */
121 OHOS::CmdSetOhosAccountInfoByUserIdStubFuzzTest(data, size);
122 return 0;
123 }
124