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 "pdpcontextlistupdated_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include <fuzzer/FuzzedDataProvider.h>
21
22 #include "hril_data.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 constexpr int32_t REASON_TYPE = 2;
32 constexpr int32_t RETRY_TIME = 2;
33 constexpr int32_t ACTIVE_NUM = 2;
34 constexpr int32_t KILO_BIT = 1000;
35 constexpr const char *NUMBER = "123";
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
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)43 void DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
44 {
45 if (data == nullptr || size == 0) {
46 return;
47 }
48
49 int32_t slotId = GetRandomInt(MIN_SLOT_ID, MAX_SLOT_ID, data, size);
50
51 HRilDataCallResponse response;
52 int32_t offset = 0;
53 response.reason = static_cast<int32_t>(*data + offset) % REASON_TYPE;
54 offset += sizeof(int32_t);
55 response.retryTime = static_cast<int32_t>(*data + offset) % RETRY_TIME;
56 offset += sizeof(int32_t);
57 response.cid = static_cast<int32_t>(*data + offset);
58 offset += sizeof(int32_t);
59 response.active = static_cast<int32_t>(*data + offset) % ACTIVE_NUM;
60 offset += sizeof(int32_t);
61 response.type = const_cast<char *>(NUMBER);
62 response.netPortName = const_cast<char *>(NUMBER);
63 response.address = const_cast<char *>(NUMBER);
64 response.dns = const_cast<char *>(NUMBER);
65 response.dnsSec = const_cast<char *>(NUMBER);
66 response.gateway = const_cast<char *>(NUMBER);
67 response.maxTransferUnit = static_cast<int32_t>(*data + offset);
68 offset += sizeof(int32_t);
69 response.pCscfPrimAddr = const_cast<char *>(NUMBER);
70 response.pCscfSecAddr = const_cast<char *>(NUMBER);
71 response.pduSessionId = static_cast<int32_t>(*data + offset);
72 offset += sizeof(int32_t);
73
74 int32_t minErrCode = -1;
75 int32_t maxErrCode = static_cast<int32_t>(HRIL_ERR_HDF_IPC_FAILURE) + 1;
76 HRilErrNumber error = static_cast<HRilErrNumber>(GetRandomInt(minErrCode, maxErrCode, data, size));
77 struct ReportInfo report;
78 report.error = error;
79 report.notifyId = HNOTI_DATA_PDP_CONTEXT_LIST_UPDATED;
80 report.type = HRIL_NOTIFICATION;
81 HRilManager::GetInstance().OnDataReport(slotId, &report, (const uint8_t *)&response, sizeof(HRilDataCallResponse));
82
83 HRilDataLinkCapability linkCapability;
84 linkCapability.primaryDownlinkKbps = static_cast<int32_t>(*data + offset) * KILO_BIT;
85 offset += sizeof(int32_t);
86 linkCapability.primaryUplinkKbps = static_cast<int32_t>(*data + offset) * KILO_BIT;
87 offset += sizeof(int32_t);
88 linkCapability.secondaryDownlinkKbps = static_cast<int32_t>(*data + offset) * KILO_BIT;
89 offset += sizeof(int32_t);
90 linkCapability.secondaryUplinkKbps = static_cast<int32_t>(*data + offset) * KILO_BIT;
91 report.notifyId = HNOTI_DATA_LINK_CAPABILITY_UPDATED;
92 HRilManager::GetInstance().OnDataReport(
93 slotId, &report, (const uint8_t *)&linkCapability, sizeof(HRilDataLinkCapability));
94 return;
95 }
96 } // namespace OHOS
97
98 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)99 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
100 {
101 /* Run your code on data */
102 OHOS::DoSomethingInterestingWithMyAPI(data, size);
103 return 0;
104 }
105