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 "residentnetworkupdated_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include <fuzzer/FuzzedDataProvider.h>
21
22 #include "hril_network.h"
23 #include "hril_manager.h"
24 #include "hril_notification.h"
25 #include "system_ability_definition.h"
26
27 using namespace OHOS::Telephony;
28 namespace OHOS {
29 constexpr int32_t MIN_SLOT_ID = -1;
30 constexpr int32_t MAX_SLOT_ID = 4;
31
GetRandomInt(int min,int max,const uint8_t * data,size_t size)32 int32_t GetRandomInt(int min, int max, const uint8_t *data, size_t size)
33 {
34 FuzzedDataProvider fdp(data, size);
35 return fdp.ConsumeIntegralInRange<int32_t>(min, max);
36 }
37
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)38 void DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
39 {
40 if (data == nullptr || size == 0) {
41 return;
42 }
43 int32_t slotId = GetRandomInt(MIN_SLOT_ID, MAX_SLOT_ID, data, size);
44 const char *plmn = reinterpret_cast<const char *>(data);
45
46 int32_t minErrCode = -1;
47 int32_t maxErrCode = static_cast<int32_t>(HRIL_ERR_HDF_IPC_FAILURE) + 1;
48 HRilErrNumber error = static_cast<HRilErrNumber>(GetRandomInt(minErrCode, maxErrCode, data, size));
49 struct ReportInfo report;
50 report.error = error;
51 report.notifyId = HNOTI_NETWORK_RESIDENT_NETWORK_UPDATED;
52 report.type = HRIL_NOTIFICATION;
53 HRilManager::GetInstance().OnNetworkReport(slotId, &report, (const uint8_t *)plmn, size);
54 return;
55 }
56 } // namespace OHOS
57
58 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)59 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
60 {
61 /* Run your code on data */
62 OHOS::DoSomethingInterestingWithMyAPI(data, size);
63 return 0;
64 }
65