• 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 "sendmessage_fuzzer.h"
17 
18 #define private public
19 
20 #include "addsmstoken_fuzzer.h"
21 #include "delivery_short_message_callback_stub.h"
22 #include "send_short_message_callback_stub.h"
23 #include "sms_service.h"
24 
25 using namespace OHOS::Telephony;
26 namespace OHOS {
27 static bool g_isInited = false;
28 constexpr int32_t SLOT_NUM = 2;
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 
SendSmsTextRequest(const uint8_t * data,size_t size)42 void SendSmsTextRequest(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     std::string text(reinterpret_cast<const char *>(data), size);
56     auto desAddrU16 = Str8ToStr16(desAddr);
57     auto scAddrU16 = Str8ToStr16(scAddr);
58     auto textU16 = Str8ToStr16(text);
59 
60     std::unique_ptr<SendShortMessageCallbackStub> sendCallback = std::make_unique<SendShortMessageCallbackStub>();
61     std::unique_ptr<DeliveryShortMessageCallbackStub> deliveryCallback =
62         std::make_unique<DeliveryShortMessageCallbackStub>();
63 
64     dataParcel.WriteInt32(slotId);
65     dataParcel.WriteString16(desAddrU16);
66     dataParcel.WriteString16(scAddrU16);
67     dataParcel.WriteString16(textU16);
68     if (sendCallback != nullptr) {
69         dataParcel.WriteRemoteObject(sendCallback.release()->AsObject().GetRefPtr());
70     }
71     if (deliveryCallback != nullptr) {
72         dataParcel.WriteRemoteObject(deliveryCallback.release()->AsObject().GetRefPtr());
73     }
74     dataParcel.RewindRead(0);
75 
76     DelayedSingleton<SmsService>::GetInstance()->OnSendSmsTextRequest(dataParcel, replyParcel, option);
77 }
78 
GetDefaultSmsSlotId(const uint8_t * data,size_t size)79 void GetDefaultSmsSlotId(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()->OnGetDefaultSmsSlotId(dataParcel, replyParcel, option);
92     return;
93 }
94 
SmsServiceInterfaceTest(const uint8_t * data,size_t size)95 void SmsServiceInterfaceTest(const uint8_t *data, size_t size)
96 {
97     if (!IsServiceInited()) {
98         return;
99     }
100     int32_t fd = static_cast<int32_t>(data[0]);
101     auto service = DelayedSingleton<SmsService>::GetInstance();
102     std::vector<std::u16string> args;
103     service->Dump(-1, args);
104     service->Dump(fd, args);
105     std::string argsStr(reinterpret_cast<const char *>(data), size);
106     std::u16string argsStrU16 = StringUtils::ToUtf16(argsStr);
107     args.push_back(argsStrU16);
108     service->Dump(fd, args);
109     service->GetBindTime();
110 
111     int32_t slotId = static_cast<int32_t>(size % SLOT_NUM);
112     service->InsertSessionAndDetail(slotId, argsStr, argsStr);
113     uint16_t num = static_cast<uint16_t>(size);
114     service->QuerySessionByTelephone(argsStr, num, num);
115     service->InsertSmsMmsInfo(slotId, num, argsStr, argsStr);
116     bool value = slotId == 0 ? true : false;
117     service->InsertSession(value, num, argsStr, argsStr);
118     service->IsImsSmsSupported(slotId, value);
119     service->GetImsShortMessageFormat(argsStrU16);
120     service->HasSmsCapability();
121     service->SetSmscAddr(slotId, argsStrU16);
122     service->TrimSmscAddr(argsStr);
123     service->GetSmscAddr(slotId, argsStrU16);
124     uint32_t index = static_cast<uint32_t>(size);
125     service->CheckSimMessageIndexValid(slotId, index);
126     service->SetImsSmsConfig(slotId, value);
127     service->SetDefaultSmsSlotId(slotId);
128     service->GetDefaultSmsSlotId();
129     int32_t simId;
130     service->GetDefaultSmsSimId(simId);
131     service->GetServiceRunningState();
132     service->GetEndTime();
133     service->ValidDestinationAddress(argsStr);
134     service->GetSpendTime();
135     service->GetBase64Encode(argsStr, argsStr);
136     service->GetBase64Decode(argsStr, argsStr);
137     service->GetEncodeStringFunc(argsStr, index, index, argsStr);
138     service->SendMms(slotId, argsStrU16, argsStrU16, argsStrU16, argsStrU16);
139     service->DownloadMms(slotId, argsStrU16, argsStrU16, argsStrU16, argsStrU16);
140 }
141 
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)142 void DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
143 {
144     if (data == nullptr || size == 0) {
145         return;
146     }
147 
148     SendSmsTextRequest(data, size);
149     GetDefaultSmsSlotId(data, size);
150     SmsServiceInterfaceTest(data, size);
151 }
152 } // namespace OHOS
153 
154 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)155 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
156 {
157     /* Run your code on data */
158     OHOS::AddSmsTokenFuzzer token;
159     OHOS::DoSomethingInterestingWithMyAPI(data, size);
160     return 0;
161 }
162