• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "newsmsnotify_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_sms.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     HRilSmsResponse response;
47     int32_t offset = 0;
48     response.msgRef = static_cast<int32_t>(*data + offset);
49     offset += sizeof(int32_t);
50     response.pdu = const_cast<char *>(NUMBER);
51     response.errCode = static_cast<int32_t>(*data + offset);
52 
53     int32_t minErrCode = -1;
54     int32_t maxErrCode = static_cast<int32_t>(HRIL_ERR_HDF_IPC_FAILURE) + 1;
55     HRilErrNumber error = static_cast<HRilErrNumber>(GetRandomInt(minErrCode, maxErrCode, data, size));
56     struct ReportInfo report;
57     report.error = error;
58     report.notifyId = HNOTI_SMS_NEW_SMS;
59     report.type = HRIL_NOTIFICATION;
60     HRilManager::GetInstance().OnSmsReport(slotId, &report, (const uint8_t *)&response, sizeof(HRilSmsResponse));
61     return;
62 }
63 } // namespace OHOS
64 
65 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)66 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
67 {
68     /* Run your code on data */
69     OHOS::DoSomethingInterestingWithMyAPI(data, size);
70     return 0;
71 }
72