• 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_short_message_callback_stub.h"
21 #include "send_short_message_callback_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<SendShortMessageCallbackStub> sendCallback = std::make_unique<SendShortMessageCallbackStub>();
60     std::unique_ptr<DeliveryShortMessageCallbackStub> deliveryCallback =
61         std::make_unique<DeliveryShortMessageCallbackStub>();
62 
63     dataParcel.WriteInt32(slotId);
64     dataParcel.WriteString16(desAddrU16);
65     dataParcel.WriteString16(scAddrU16);
66     dataParcel.WriteInt16(port);
67     if (sendCallback != nullptr) {
68         dataParcel.WriteRemoteObject(sendCallback.release()->AsObject().GetRefPtr());
69     }
70     if (deliveryCallback != nullptr) {
71         dataParcel.WriteRemoteObject(deliveryCallback.release()->AsObject().GetRefPtr());
72     }
73     dataParcel.WriteInt16(size);
74     dataParcel.WriteRawData(data, size);
75     dataParcel.RewindRead(0);
76 
77     DelayedSingleton<SmsService>::GetInstance()->OnSendSmsDataRequest(dataParcel, replyParcel, option);
78 }
79 
GetAllSimMessages(const uint8_t * data,size_t size)80 void GetAllSimMessages(const uint8_t *data, size_t size)
81 {
82     if (!IsServiceInited()) {
83         return;
84     }
85 
86     MessageParcel dataParcel;
87     MessageParcel replyParcel;
88     MessageOption option(MessageOption::TF_SYNC);
89 
90     dataParcel.WriteBuffer(data, size);
91     dataParcel.RewindRead(0);
92     DelayedSingleton<SmsService>::GetInstance()->OnGetAllSimMessages(dataParcel, replyParcel, option);
93     return;
94 }
95 
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)96 void DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
97 {
98     if (data == nullptr || size == 0) {
99         return;
100     }
101 
102     SendSmsDataRequest(data, size);
103     GetAllSimMessages(data, size);
104 }
105 }  // namespace OHOS
106 
107 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)108 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
109 {
110     /* Run your code on data */
111     OHOS::AddSmsTokenFuzzer token;
112     OHOS::DoSomethingInterestingWithMyAPI(data, size);
113     return 0;
114 }
115