• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "imsregcallback_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 #include "addcoreservicetoken_fuzzer.h"
22 #include <fuzzer/FuzzedDataProvider.h>
23 #include "ims_reg_info_callback_stub.h"
24 #include "napi_ims_reg_info_callback.h"
25 #include "napi_ims_reg_info_callback_manager.h"
26 #include "napi_util.h"
27 #include "system_ability_definition.h"
28 
29 using namespace OHOS::Telephony;
30 namespace OHOS {
31 constexpr int32_t BOOL_NUM = 2;
32 constexpr int32_t TECH_NUM = 4;
33 constexpr int32_t IMS_SERVICE_TYPE_NUM = 4;
34 constexpr int32_t MIN_SLOT_ID = -1;
35 constexpr int32_t MAX_SLOT_ID = 4;
36 
GetRandomInt(int min,int max,const uint8_t * data,size_t size)37 int32_t GetRandomInt(int min, int max, const uint8_t *data, size_t size)
38 {
39     FuzzedDataProvider fdp(data, size);
40     return fdp.ConsumeIntegralInRange<int32_t>(min, max);
41 }
42 
OnRemoteRequest(const uint8_t * data,size_t size)43 void OnRemoteRequest(const uint8_t *data, size_t size)
44 {
45     MessageParcel dataMessageParcel;
46     if (!dataMessageParcel.WriteInterfaceToken(ImsRegInfoCallbackStub::GetDescriptor())) {
47         return;
48     }
49     int32_t slotId = GetRandomInt(MIN_SLOT_ID, MAX_SLOT_ID, data, size);
50     int32_t reg = GetRandomInt(0, BOOL_NUM, data, size);
51     int32_t tech = GetRandomInt(0, TECH_NUM, data, size);
52     dataMessageParcel.WriteInt32(slotId);
53     dataMessageParcel.WriteInt32(reg);
54     dataMessageParcel.WriteInt32(tech);
55 
56     uint32_t code = static_cast<uint32_t>(GetRandomInt(0, IMS_SERVICE_TYPE_NUM, data, size));
57     MessageParcel reply;
58     MessageOption option;
59     sptr<NapiImsRegInfoCallback> imsCallback = new NapiImsRegInfoCallback();
60     imsCallback->OnRemoteRequest(code, dataMessageParcel, reply, option);
61 }
62 
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)63 void DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
64 {
65     if (data == nullptr || size == 0) {
66         return;
67     }
68 
69     OnRemoteRequest(data, size);
70 }
71 } // namespace OHOS
72 
73 /* Fuzzer entry point */
LLVMFuzzerInitialize(int * argc,char *** argv)74 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
75 {
76     OHOS::AddCoreServiceTokenFuzzer token;
77     return 0;
78 }
79 
80 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)81 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
82 {
83     /* Run your code on data */
84     OHOS::DoSomethingInterestingWithMyAPI(data, size);
85     return 0;
86 }
87