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