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 
21 #include "hril_data.h"
22 #include "hril_manager.h"
23 #include "hril_notification.h"
24 #include "system_ability_definition.h"
25 
26 using namespace OHOS::Telephony;
27 namespace OHOS {
28 constexpr int32_t SLOT_NUM = 2;
29 constexpr int32_t REASON_TYPE = 2;
30 constexpr int32_t RETRY_TIME = 2;
31 constexpr int32_t ACTIVE_NUM = 2;
32 constexpr int32_t KILO_BIT = 1000;
33 constexpr const char *NUMBER = "123";
34 
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)35 void DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
36 {
37     if (data == nullptr || size == 0) {
38         return;
39     }
40 
41     int32_t slotId = static_cast<int32_t>(*data % SLOT_NUM);
42 
43     HRilDataCallResponse response;
44     int32_t offset = 0;
45     response.reason = static_cast<int32_t>(*data + offset) % REASON_TYPE;
46     offset += sizeof(int32_t);
47     response.retryTime = static_cast<int32_t>(*data + offset) % RETRY_TIME;
48     offset += sizeof(int32_t);
49     response.cid = static_cast<int32_t>(*data + offset);
50     offset += sizeof(int32_t);
51     response.active = static_cast<int32_t>(*data + offset) % ACTIVE_NUM;
52     offset += sizeof(int32_t);
53     response.type = const_cast<char *>(NUMBER);
54     response.netPortName = const_cast<char *>(NUMBER);
55     response.address = const_cast<char *>(NUMBER);
56     response.dns = const_cast<char *>(NUMBER);
57     response.dnsSec = const_cast<char *>(NUMBER);
58     response.gateway = const_cast<char *>(NUMBER);
59     response.maxTransferUnit = static_cast<int32_t>(*data + offset);
60     offset += sizeof(int32_t);
61     response.pCscfPrimAddr = const_cast<char *>(NUMBER);
62     response.pCscfSecAddr = const_cast<char *>(NUMBER);
63     response.pduSessionId = static_cast<int32_t>(*data + offset);
64     offset += sizeof(int32_t);
65 
66     struct ReportInfo report;
67     report.error = static_cast<HRilErrNumber>(size);
68     report.notifyId = HNOTI_DATA_PDP_CONTEXT_LIST_UPDATED;
69     report.type = HRIL_NOTIFICATION;
70     HRilManager::GetInstance().OnDataReport(slotId, &report, (const uint8_t *)&response, sizeof(HRilDataCallResponse));
71 
72     HRilDataLinkCapability linkCapability;
73     linkCapability.primaryDownlinkKbps = static_cast<int32_t>(*data + offset) * KILO_BIT;
74     offset += sizeof(int32_t);
75     linkCapability.primaryUplinkKbps = static_cast<int32_t>(*data + offset) * KILO_BIT;
76     offset += sizeof(int32_t);
77     linkCapability.secondaryDownlinkKbps = static_cast<int32_t>(*data + offset) * KILO_BIT;
78     offset += sizeof(int32_t);
79     linkCapability.secondaryUplinkKbps = static_cast<int32_t>(*data + offset) * KILO_BIT;
80     report.notifyId = HNOTI_DATA_LINK_CAPABILITY_UPDATED;
81     HRilManager::GetInstance().OnDataReport(
82         slotId, &report, (const uint8_t *)&linkCapability, sizeof(HRilDataLinkCapability));
83     return;
84 }
85 } // namespace OHOS
86 
87 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)88 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
89 {
90     /* Run your code on data */
91     OHOS::DoSomethingInterestingWithMyAPI(data, size);
92     return 0;
93 }
94