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 TWO_INT_NUM = 2;
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 size_t dataSize = size - sizeof(int32_t) * TWO_INT_NUM;
54 dataParcel.WriteBuffer(data + sizeof(int32_t) * TWO_INT_NUM, dataSize);
55 dataParcel.RewindRead(0);
56 MessageParcel reply;
57 return DelayedSingleton<CallManagerService>::GetInstance()->OnGetMainCallId(dataParcel, reply);
58 }
59
GetSubCallIdList(const uint8_t * data,size_t size)60 int32_t GetSubCallIdList(const uint8_t *data, size_t size)
61 {
62 if (!IsServiceInited()) {
63 return TELEPHONY_ERROR;
64 }
65 MessageParcel dataParcel;
66 int32_t callId = static_cast<int32_t>(size);
67 dataParcel.WriteInt32(callId);
68 size_t dataSize = size - sizeof(int32_t);
69 dataParcel.WriteBuffer(data + sizeof(int32_t), dataSize);
70 dataParcel.RewindRead(0);
71 MessageParcel reply;
72 return DelayedSingleton<CallManagerService>::GetInstance()->OnGetSubCallIdList(dataParcel, reply);
73 }
74
GetCallIdListForConference(const uint8_t * data,size_t size)75 int32_t GetCallIdListForConference(const uint8_t *data, size_t size)
76 {
77 if (!IsServiceInited()) {
78 return TELEPHONY_ERROR;
79 }
80 MessageParcel dataParcel;
81 int32_t callId = static_cast<int32_t>(size);
82 dataParcel.WriteInt32(callId);
83 size_t dataSize = size - sizeof(int32_t);
84 dataParcel.WriteBuffer(data + sizeof(int32_t), dataSize);
85 dataParcel.RewindRead(0);
86 MessageParcel reply;
87 return DelayedSingleton<CallManagerService>::GetInstance()->OnGetCallIdListForConference(dataParcel, reply);
88 }
89
GetCallRestriction(const uint8_t * data,size_t size)90 int32_t GetCallRestriction(const uint8_t *data, size_t size)
91 {
92 if (!IsServiceInited()) {
93 return TELEPHONY_ERROR;
94 }
95 int32_t slotId = static_cast<int32_t>(size % SLOT_NUM);
96 MessageParcel dataParcel;
97 dataParcel.WriteInt32(slotId);
98 dataParcel.WriteInt32(static_cast<int32_t>(size));
99 size_t dataSize = size - sizeof(int32_t) * TWO_INT_NUM;
100 dataParcel.WriteBuffer(data + sizeof(int32_t) * TWO_INT_NUM, dataSize);
101 dataParcel.RewindRead(0);
102 MessageParcel reply;
103 return DelayedSingleton<CallManagerService>::GetInstance()->OnGetCallRestriction(dataParcel, reply);
104 }
105
FormatPhoneNumber(const uint8_t * data,size_t size)106 int32_t FormatPhoneNumber(const uint8_t *data, size_t size)
107 {
108 if (!IsServiceInited()) {
109 return TELEPHONY_ERROR;
110 }
111 std::string number(reinterpret_cast<const char *>(data), size);
112 auto numberU16 = Str8ToStr16(number);
113 std::string countryCode(reinterpret_cast<const char *>(data), size);
114 auto countryCodeU16 = Str8ToStr16(countryCode);
115 MessageParcel dataParcel;
116 dataParcel.WriteString16(numberU16);
117 dataParcel.WriteString16(countryCodeU16);
118 dataParcel.RewindRead(0);
119 MessageParcel reply;
120 return DelayedSingleton<CallManagerService>::GetInstance()->OnFormatPhoneNumber(dataParcel, reply);
121 }
122
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)123 void DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
124 {
125 if (data == nullptr || size == 0) {
126 return;
127 }
128
129 GetMainCallId(data, size);
130 GetSubCallIdList(data, size);
131 GetCallIdListForConference(data, size);
132 GetCallRestriction(data, size);
133 FormatPhoneNumber(data, size);
134 }
135 } // namespace OHOS
136
137 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)138 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
139 {
140 OHOS::AddCallTokenFuzzer token;
141 /* Run your code on data */
142 OHOS::DoSomethingInterestingWithMyAPI(data, size);
143 return 0;
144 }
145