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 "ims_reg_info_callback_stub.h"
23 #include "napi_ims_reg_info_callback.h"
24 #include "napi_ims_reg_info_callback_manager.h"
25 #include "napi_util.h"
26 #include "system_ability_definition.h"
27
28 using namespace OHOS::Telephony;
29 namespace OHOS {
30 constexpr int32_t SLOT_NUM = 2;
31 constexpr int32_t BOOL_NUM = 2;
32 constexpr int32_t TECH_NUM = 4;
33 constexpr int32_t IMS_SERVICE_TYPE_NUM = 4;
OnRemoteRequest(const uint8_t * data,size_t size)34 void OnRemoteRequest(const uint8_t *data, size_t size)
35 {
36 MessageParcel dataMessageParcel;
37 if (!dataMessageParcel.WriteInterfaceToken(ImsRegInfoCallbackStub::GetDescriptor())) {
38 return;
39 }
40 int32_t slotId = static_cast<int32_t>(*data % SLOT_NUM);
41 int32_t reg = static_cast<int32_t>(*data % BOOL_NUM);
42 int32_t tech = static_cast<int32_t>(*data % TECH_NUM);
43 dataMessageParcel.WriteInt32(slotId);
44 dataMessageParcel.WriteInt32(reg);
45 dataMessageParcel.WriteInt32(tech);
46
47 uint32_t code = static_cast<uint32_t>(*data % IMS_SERVICE_TYPE_NUM);
48 MessageParcel reply;
49 MessageOption option;
50 sptr<NapiImsRegInfoCallback> imsCallback = new NapiImsRegInfoCallback();
51 imsCallback->OnRemoteRequest(code, dataMessageParcel, reply, option);
52 }
53
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)54 void DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
55 {
56 if (data == nullptr || size == 0) {
57 return;
58 }
59
60 OnRemoteRequest(data, size);
61 }
62 } // namespace OHOS
63
64 /* Fuzzer entry point */
LLVMFuzzerInitialize(int * argc,char *** argv)65 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
66 {
67 OHOS::AddCoreServiceTokenFuzzer token;
68 return 0;
69 }
70
71 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)72 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
73 {
74 /* Run your code on data */
75 OHOS::DoSomethingInterestingWithMyAPI(data, size);
76 return 0;
77 }
78