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 "dialcall_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 ACTIVE_NUM = 2;
30 constexpr int32_t VEDIO_STATE_NUM = 2;
31 constexpr int32_t DIAL_SCENE_NUM = 3;
32 constexpr int32_t DIAL_TYPE_NUM = 3;
33 constexpr int32_t CALL_TYPE_NUM = 3;
34
IsServiceInited()35 bool IsServiceInited()
36 {
37 if (!g_isInited) {
38 DelayedSingleton<CallManagerService>::GetInstance()->OnStart();
39 if (DelayedSingleton<CallManagerService>::GetInstance()->GetServiceRunningState() ==
40 static_cast<int32_t>(CallManagerService::ServiceRunningState::STATE_RUNNING)) {
41 g_isInited = true;
42 }
43 }
44 return g_isInited;
45 }
46
HasCall(const uint8_t * data,size_t size)47 bool HasCall(const uint8_t *data, size_t size)
48 {
49 if (!IsServiceInited()) {
50 return TELEPHONY_ERROR;
51 }
52 MessageParcel dataParcel;
53 dataParcel.WriteBuffer(data, size);
54 dataParcel.RewindRead(0);
55 MessageParcel reply;
56 return DelayedSingleton<CallManagerService>::GetInstance()->OnHasCall(dataParcel, reply);
57 }
58
GetCallState(const uint8_t * data,size_t size)59 int32_t GetCallState(const uint8_t *data, size_t size)
60 {
61 if (!IsServiceInited()) {
62 return TELEPHONY_ERROR;
63 }
64 MessageParcel dataParcel;
65 dataParcel.WriteBuffer(data, size);
66 dataParcel.RewindRead(0);
67 MessageParcel reply;
68 return DelayedSingleton<CallManagerService>::GetInstance()->OnGetCallState(dataParcel, reply);
69 }
70
GetCallWaiting(const uint8_t * data,size_t size)71 int32_t GetCallWaiting(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 size_t dataSize = size - sizeof(int32_t);
80 dataParcel.WriteBuffer(data + sizeof(int32_t), dataSize);
81 dataParcel.RewindRead(0);
82 MessageParcel reply;
83 return DelayedSingleton<CallManagerService>::GetInstance()->OnGetCallWaiting(dataParcel, reply);
84 }
85
IsRinging(const uint8_t * data,size_t size)86 bool IsRinging(const uint8_t *data, size_t size)
87 {
88 if (!IsServiceInited()) {
89 return false;
90 }
91 MessageParcel dataParcel;
92 dataParcel.WriteBuffer(data, size);
93 dataParcel.RewindRead(0);
94 MessageParcel reply;
95 return DelayedSingleton<CallManagerService>::GetInstance()->OnIsRinging(dataParcel, reply);
96 }
97
IsInEmergencyCall(const uint8_t * data,size_t size)98 bool IsInEmergencyCall(const uint8_t *data, size_t size)
99 {
100 if (!IsServiceInited()) {
101 return false;
102 }
103 MessageParcel dataParcel;
104 dataParcel.WriteBuffer(data, size);
105 dataParcel.RewindRead(0);
106 MessageParcel reply;
107 return DelayedSingleton<CallManagerService>::GetInstance()->OnIsInEmergencyCall(dataParcel, reply);
108 }
109
DialCall(const uint8_t * data,size_t size)110 int32_t DialCall(const uint8_t *data, size_t size)
111 {
112 if (!IsServiceInited()) {
113 return TELEPHONY_ERROR;
114 }
115 std::string number(reinterpret_cast<const char *>(data), size);
116 auto numberU16 = Str8ToStr16(number);
117 int32_t accountId = static_cast<int32_t>(size % SLOT_NUM);
118 int32_t videoState = static_cast<int32_t>(size % VEDIO_STATE_NUM);
119 int32_t dialScene = static_cast<int32_t>(size % DIAL_SCENE_NUM);
120 int32_t dialType = static_cast<int32_t>(size % DIAL_TYPE_NUM);
121 int32_t callType = static_cast<int32_t>(size % CALL_TYPE_NUM);
122 MessageParcel dataParcel;
123 dataParcel.WriteString16(numberU16);
124 dataParcel.WriteInt32(accountId);
125 dataParcel.WriteInt32(videoState);
126 dataParcel.WriteInt32(dialScene);
127 dataParcel.WriteInt32(dialType);
128 dataParcel.WriteInt32(callType);
129 dataParcel.RewindRead(0);
130
131 MessageParcel reply;
132 return DelayedSingleton<CallManagerService>::GetInstance()->OnDialCall(dataParcel, reply);
133 }
134
SetCallWaiting(const uint8_t * data,size_t size)135 int32_t SetCallWaiting(const uint8_t *data, size_t size)
136 {
137 if (!IsServiceInited()) {
138 return TELEPHONY_ERROR;
139 }
140 int32_t slotId = static_cast<int32_t>(size % SLOT_NUM);
141 int32_t activate = static_cast<int32_t>(size % ACTIVE_NUM);
142 MessageParcel dataParcel;
143 dataParcel.WriteInt32(slotId);
144 dataParcel.WriteBool(activate);
145 dataParcel.RewindRead(0);
146 MessageParcel reply;
147 return DelayedSingleton<CallManagerService>::GetInstance()->OnSetCallWaiting(dataParcel, reply);
148 }
149
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)150 void DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
151 {
152 if (data == nullptr || size == 0) {
153 return;
154 }
155
156 HasCall(data, size);
157 GetCallState(data, size);
158 GetCallWaiting(data, size);
159 IsRinging(data, size);
160 IsInEmergencyCall(data, size);
161 DialCall(data, size);
162 SetCallWaiting(data, size);
163 }
164 } // namespace OHOS
165
166 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)167 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
168 {
169 OHOS::AddCallTokenFuzzer token;
170 /* Run your code on data */
171 OHOS::DoSomethingInterestingWithMyAPI(data, size);
172 return 0;
173 }
174