1 /*
2 * Copyright (c) 2022 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 "formatphonenumber_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #define private public
21 #include "addcalltoken_fuzzer.h"
22 #include "call_manager_service.h"
23 #include "system_ability_definition.h"
24
25 using namespace OHOS::Telephony;
26 namespace OHOS {
27 static bool g_isInited = false;
28 constexpr int32_t SLOT_NUM = 2;
29 constexpr int32_t CALLS_NUM = 5;
30
IsServiceInited()31 bool IsServiceInited()
32 {
33 if (!g_isInited) {
34 DelayedSingleton<CallManagerService>::GetInstance()->OnStart();
35 if (DelayedSingleton<CallManagerService>::GetInstance()->GetServiceRunningState() ==
36 static_cast<int32_t>(CallManagerService::ServiceRunningState::STATE_RUNNING)) {
37 g_isInited = true;
38 }
39 }
40 return g_isInited;
41 }
42
GetMainCallId(const uint8_t * data,size_t size)43 int32_t GetMainCallId(const uint8_t *data, size_t size)
44 {
45 if (!IsServiceInited()) {
46 return TELEPHONY_ERROR;
47 }
48 MessageParcel dataParcel;
49 int32_t callId = static_cast<int32_t>(size);
50 dataParcel.WriteInt32(callId);
51 size_t mainCallId = static_cast<int32_t>(size);
52 dataParcel.WriteInt32(mainCallId);
53 dataParcel.WriteBuffer(data, size);
54 dataParcel.RewindRead(0);
55 MessageParcel reply;
56 return DelayedSingleton<CallManagerService>::GetInstance()->OnGetMainCallId(dataParcel, reply);
57 }
58
GetSubCallIdList(const uint8_t * data,size_t size)59 int32_t GetSubCallIdList(const uint8_t *data, size_t size)
60 {
61 if (!IsServiceInited()) {
62 return TELEPHONY_ERROR;
63 }
64 MessageParcel dataParcel;
65 int32_t callId = static_cast<int32_t>(size);
66 dataParcel.WriteInt32(callId);
67 dataParcel.WriteBuffer(data, size);
68 dataParcel.RewindRead(0);
69 MessageParcel reply;
70 return DelayedSingleton<CallManagerService>::GetInstance()->OnGetSubCallIdList(dataParcel, reply);
71 }
72
GetCallIdListForConference(const uint8_t * data,size_t size)73 int32_t GetCallIdListForConference(const uint8_t *data, size_t size)
74 {
75 if (!IsServiceInited()) {
76 return TELEPHONY_ERROR;
77 }
78 MessageParcel dataParcel;
79 int32_t callId = static_cast<int32_t>(size);
80 dataParcel.WriteInt32(callId);
81 dataParcel.WriteBuffer(data, size);
82 dataParcel.RewindRead(0);
83 MessageParcel reply;
84 return DelayedSingleton<CallManagerService>::GetInstance()->OnGetCallIdListForConference(dataParcel, reply);
85 }
86
GetCallRestriction(const uint8_t * data,size_t size)87 int32_t GetCallRestriction(const uint8_t *data, size_t size)
88 {
89 if (!IsServiceInited()) {
90 return TELEPHONY_ERROR;
91 }
92 int32_t slotId = static_cast<int32_t>(size % SLOT_NUM);
93 MessageParcel dataParcel;
94 dataParcel.WriteInt32(slotId);
95 dataParcel.WriteInt32(static_cast<int32_t>(size));
96 dataParcel.WriteBuffer(data, size);
97 dataParcel.RewindRead(0);
98 MessageParcel reply;
99 return DelayedSingleton<CallManagerService>::GetInstance()->OnGetCallRestriction(dataParcel, reply);
100 }
101
ReportOttCallDetailsInfo(const uint8_t * data,size_t size)102 int32_t ReportOttCallDetailsInfo(const uint8_t *data, size_t size)
103 {
104 if (!IsServiceInited()) {
105 return TELEPHONY_ERROR;
106 }
107 int32_t vecCnt = static_cast<int32_t>(size % CALLS_NUM);
108 OttCallDetailsInfo info;
109 std::string msg(reinterpret_cast<const char *>(data), size);
110 int32_t phoneLength = msg.length() > kMaxNumberLen ? kMaxNumberLen : msg.length();
111 int32_t bundleLength = msg.length() > kMaxBundleNameLen ? kMaxBundleNameLen : msg.length();
112 memcpy_s(info.phoneNum, kMaxNumberLen, msg.c_str(), phoneLength);
113 memcpy_s(info.bundleName, kMaxBundleNameLen, msg.c_str(), bundleLength);
114 info.callState = TelCallState::CALL_STATUS_DIALING;
115 info.videoState = VideoStateType::TYPE_VOICE;
116 MessageParcel dataParcel;
117 dataParcel.WriteInt32(vecCnt);
118 dataParcel.WriteRawData((const void *)&info, sizeof(OttCallDetailsInfo));
119 dataParcel.RewindRead(0);
120 MessageParcel reply;
121 return DelayedSingleton<CallManagerService>::GetInstance()->OnReportOttCallDetailsInfo(dataParcel, reply);
122 }
123
ReportOttCallEventInfo(const uint8_t * data,size_t size)124 int32_t ReportOttCallEventInfo(const uint8_t *data, size_t size)
125 {
126 if (!IsServiceInited()) {
127 return TELEPHONY_ERROR;
128 }
129 OttCallEventInfo info;
130 info.ottCallEventId = OttCallEventId::OTT_CALL_EVENT_FUNCTION_UNSUPPORTED;
131 std::string msg(reinterpret_cast<const char *>(data), size);
132 int32_t bundleLength = msg.length() > kMaxBundleNameLen ? kMaxBundleNameLen : msg.length();
133 memcpy_s(info.bundleName, kMaxBundleNameLen, msg.c_str(), bundleLength);
134 MessageParcel dataParcel;
135 dataParcel.WriteRawData((const void *)&info, sizeof(OttCallEventInfo));
136 dataParcel.RewindRead(0);
137 MessageParcel reply;
138 return DelayedSingleton<CallManagerService>::GetInstance()->OnReportOttCallEventInfo(dataParcel, reply);
139 }
140
CloseUnFinishedUssd(const uint8_t * data,size_t size)141 int32_t CloseUnFinishedUssd(const uint8_t *data, size_t size)
142 {
143 if (!IsServiceInited()) {
144 return TELEPHONY_ERROR;
145 }
146 int32_t slotId = static_cast<int32_t>(size % SLOT_NUM);
147 MessageParcel dataParcel;
148 dataParcel.WriteInt32(slotId);
149 dataParcel.RewindRead(0);
150 MessageParcel reply;
151 return DelayedSingleton<CallManagerService>::GetInstance()->OnCloseUnFinishedUssd(dataParcel, reply);
152 }
153
FormatPhoneNumber(const uint8_t * data,size_t size)154 int32_t FormatPhoneNumber(const uint8_t *data, size_t size)
155 {
156 if (!IsServiceInited()) {
157 return TELEPHONY_ERROR;
158 }
159 std::string number(reinterpret_cast<const char *>(data), size);
160 auto numberU16 = Str8ToStr16(number);
161 std::string countryCode(reinterpret_cast<const char *>(data), size);
162 auto countryCodeU16 = Str8ToStr16(countryCode);
163 MessageParcel dataParcel;
164 dataParcel.WriteString16(numberU16);
165 dataParcel.WriteString16(countryCodeU16);
166 dataParcel.RewindRead(0);
167 MessageParcel reply;
168 return DelayedSingleton<CallManagerService>::GetInstance()->OnFormatPhoneNumber(dataParcel, reply);
169 }
170
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)171 void DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
172 {
173 if (data == nullptr || size == 0) {
174 return;
175 }
176
177 GetMainCallId(data, size);
178 GetSubCallIdList(data, size);
179 GetCallIdListForConference(data, size);
180 GetCallRestriction(data, size);
181 ReportOttCallDetailsInfo(data, size);
182 ReportOttCallEventInfo(data, size);
183 CloseUnFinishedUssd(data, size);
184 FormatPhoneNumber(data, size);
185 }
186 } // namespace OHOS
187
188 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)189 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
190 {
191 OHOS::AddCallTokenFuzzer token;
192 /* Run your code on data */
193 OHOS::DoSomethingInterestingWithMyAPI(data, size);
194 return 0;
195 }
196