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