• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "rejectcall_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 "call_manager_service_stub.h"
24 #include "system_ability_definition.h"
25 
26 using namespace OHOS::Telephony;
27 namespace OHOS {
28 static bool g_isInited = false;
29 constexpr int32_t SLOT_NUM = 2;
30 constexpr int32_t CHOICE_NUM = 2;
31 
IsServiceInited()32 bool IsServiceInited()
33 {
34     if (!g_isInited) {
35         DelayedSingleton<CallManagerService>::GetInstance()->OnStart();
36         if (DelayedSingleton<CallManagerService>::GetInstance()->GetServiceRunningState() ==
37             static_cast<int32_t>(CallManagerService::ServiceRunningState::STATE_RUNNING)) {
38             g_isInited = true;
39         }
40     }
41     return g_isInited;
42 }
43 
RejectCall(const uint8_t * data,size_t size)44 void RejectCall(const uint8_t *data, size_t size)
45 {
46     if (!IsServiceInited()) {
47         return;
48     }
49 
50     int32_t slotId = static_cast<int32_t>(size % SLOT_NUM);
51     int32_t rejectWithMessage = static_cast<int32_t>(size % CHOICE_NUM);
52     std::string message(reinterpret_cast<const char *>(data), size);
53     MessageParcel dataMessageParcel;
54     dataMessageParcel.WriteInt32(slotId);
55     dataMessageParcel.WriteInt32(rejectWithMessage);
56     dataMessageParcel.WriteString16(Str8ToStr16(message));
57     dataMessageParcel.RewindRead(0);
58     MessageParcel reply;
59     DelayedSingleton<CallManagerService>::GetInstance()->OnRejectCall(dataMessageParcel, reply);
60 }
61 
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)62 void DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
63 {
64     if (data == nullptr || size == 0) {
65         return;
66     }
67 
68     RejectCall(data, size);
69 }
70 } // namespace OHOS
71 
72 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)73 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
74 {
75     OHOS::AddCallTokenFuzzer token;
76     /* Run your code on data */
77     OHOS::DoSomethingInterestingWithMyAPI(data, size);
78     return 0;
79 }
80