• 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 "sendmessagedata_fuzzer.h"
17 
18 #define private public
19 #include "addsmstoken_fuzzer.h"
20 #include "delivery_send_call_back_stub.h"
21 #include "send_call_back_stub.h"
22 #include "sms_service.h"
23 
24 using namespace OHOS::Telephony;
25 namespace OHOS {
26 static bool g_isInited = false;
27 constexpr int32_t SLOT_NUM = 2;
28 static int32_t MAX_PORT = 65535;
29 
IsServiceInited()30 bool IsServiceInited()
31 {
32     if (!g_isInited) {
33         DelayedSingleton<SmsService>::GetInstance()->OnStart();
34         if (DelayedSingleton<SmsService>::GetInstance()->GetServiceRunningState() ==
35             static_cast<int32_t>(Telephony::ServiceRunningState::STATE_RUNNING)) {
36             g_isInited = true;
37         }
38     }
39     return g_isInited;
40 }
41 
SendSmsDataRequest(const uint8_t * data,size_t size)42 void SendSmsDataRequest(const uint8_t *data, size_t size)
43 {
44     if (!IsServiceInited()) {
45         return;
46     }
47 
48     MessageParcel dataParcel;
49     MessageParcel replyParcel;
50     MessageOption option(MessageOption::TF_SYNC);
51 
52     int32_t slotId = static_cast<int32_t>(size % SLOT_NUM);
53     std::string desAddr(reinterpret_cast<const char *>(data), size);
54     std::string scAddr(reinterpret_cast<const char *>(data), size);
55     auto desAddrU16 = Str8ToStr16(desAddr);
56     auto scAddrU16 = Str8ToStr16(scAddr);
57     uint16_t port = static_cast<uint16_t>(size % MAX_PORT);
58 
59     std::unique_ptr<SendCallbackStub> sendCallback = std::make_unique<SendCallbackStub>();
60     std::unique_ptr<DeliverySendCallbackStub> deliveryCallback = std::make_unique<DeliverySendCallbackStub>();
61 
62     dataParcel.WriteInt32(slotId);
63     dataParcel.WriteString16(desAddrU16);
64     dataParcel.WriteString16(scAddrU16);
65     dataParcel.WriteInt16(port);
66     if (sendCallback != nullptr) {
67         dataParcel.WriteRemoteObject(sendCallback.release()->AsObject().GetRefPtr());
68     }
69     if (deliveryCallback != nullptr) {
70         dataParcel.WriteRemoteObject(deliveryCallback.release()->AsObject().GetRefPtr());
71     }
72     dataParcel.WriteInt16(size);
73     dataParcel.WriteRawData(data, size);
74     dataParcel.RewindRead(0);
75 
76     DelayedSingleton<SmsService>::GetInstance()->OnSendSmsDataRequest(dataParcel, replyParcel, option);
77 }
78 
GetAllSimMessages(const uint8_t * data,size_t size)79 void GetAllSimMessages(const uint8_t *data, size_t size)
80 {
81     if (!IsServiceInited()) {
82         return;
83     }
84 
85     MessageParcel dataParcel;
86     MessageParcel replyParcel;
87     MessageOption option(MessageOption::TF_SYNC);
88 
89     dataParcel.WriteBuffer(data, size);
90     dataParcel.RewindRead(0);
91     DelayedSingleton<SmsService>::GetInstance()->OnGetAllSimMessages(dataParcel, replyParcel, option);
92     return;
93 }
94 
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)95 void DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
96 {
97     if (data == nullptr || size == 0) {
98         return;
99     }
100 
101     SendSmsDataRequest(data, size);
102     GetAllSimMessages(data, size);
103 }
104 }  // namespace OHOS
105 
106 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)107 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
108 {
109     /* Run your code on data */
110     OHOS::AddSmsTokenFuzzer token;
111     OHOS::DoSomethingInterestingWithMyAPI(data, size);
112     return 0;
113 }
114