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 "core_manager_inner.h"
22 #include "delivery_short_message_callback_stub.h"
23 #include "send_short_message_callback_stub.h"
24 #include "sms_service.h"
25 
26 using namespace OHOS::Telephony;
27 namespace OHOS {
28 static bool g_isInited = false;
29 constexpr int32_t SLOT_NUM = 2;
30 constexpr int32_t SLEEP_TIME_SECONDS = 2;
31 
IsServiceInited()32 bool IsServiceInited()
33 {
34     if (!g_isInited) {
35         CoreManagerInner::GetInstance().isInitAllObj_ = true;
36         DelayedSingleton<SmsService>::GetInstance()->registerToService_ = true;
37         DelayedSingleton<SmsService>::GetInstance()->WaitCoreServiceToInit();
38         DelayedSingleton<SmsService>::GetInstance()->OnStart();
39         if (DelayedSingleton<SmsService>::GetInstance()->GetServiceRunningState() ==
40             static_cast<int32_t>(Telephony::ServiceRunningState::STATE_RUNNING)) {
41             g_isInited = true;
42         }
43     }
44     return g_isInited;
45 }
46 
SendSmsTextRequest(const uint8_t * data,size_t size)47 void SendSmsTextRequest(const uint8_t *data, size_t size)
48 {
49     if (!IsServiceInited()) {
50         return;
51     }
52 
53     MessageParcel dataParcel;
54     MessageParcel replyParcel;
55     MessageOption option(MessageOption::TF_SYNC);
56 
57     int32_t slotId = static_cast<int32_t>(size % SLOT_NUM);
58     std::string text(reinterpret_cast<const char *>(data), size);
59     auto desAddrU16 = Str8ToStr16("123456");
60     auto scAddrU16 = Str8ToStr16("123456");
61     auto textU16 = Str8ToStr16(text);
62 
63     std::unique_ptr<SendShortMessageCallbackStub> sendCallback = std::make_unique<SendShortMessageCallbackStub>();
64     std::unique_ptr<DeliveryShortMessageCallbackStub> deliveryCallback =
65         std::make_unique<DeliveryShortMessageCallbackStub>();
66 
67     dataParcel.WriteInt32(slotId);
68     dataParcel.WriteString16(desAddrU16);
69     dataParcel.WriteString16(scAddrU16);
70     dataParcel.WriteString16(textU16);
71     if (sendCallback != nullptr) {
72         dataParcel.WriteRemoteObject(sendCallback.release()->AsObject().GetRefPtr());
73     }
74     if (deliveryCallback != nullptr) {
75         dataParcel.WriteRemoteObject(deliveryCallback.release()->AsObject().GetRefPtr());
76     }
77     dataParcel.RewindRead(0);
78 
79     DelayedSingleton<SmsService>::GetInstance()->OnSendSmsTextRequest(dataParcel, replyParcel, option);
80 }
81 
GetDefaultSmsSlotId(const uint8_t * data,size_t size)82 void GetDefaultSmsSlotId(const uint8_t *data, size_t size)
83 {
84     if (!IsServiceInited()) {
85         return;
86     }
87 
88     MessageParcel dataParcel;
89     MessageParcel replyParcel;
90     MessageOption option(MessageOption::TF_SYNC);
91 
92     dataParcel.WriteBuffer(data, size);
93     dataParcel.RewindRead(0);
94     DelayedSingleton<SmsService>::GetInstance()->OnGetDefaultSmsSlotId(dataParcel, replyParcel, option);
95     return;
96 }
97 
SmsServiceInterfaceTest(const uint8_t * data,size_t size)98 void SmsServiceInterfaceTest(const uint8_t *data, size_t size)
99 {
100     if (!IsServiceInited()) {
101         return;
102     }
103     int32_t fd = static_cast<int32_t>(data[0]);
104     auto service = DelayedSingleton<SmsService>::GetInstance();
105     std::vector<std::u16string> args;
106     service->Dump(-1, args);
107     service->Dump(fd, args);
108     std::string argsStr(reinterpret_cast<const char *>(data), size);
109     std::u16string argsStrU16 = StringUtils::ToUtf16(argsStr);
110     args.push_back(argsStrU16);
111     service->Dump(fd, args);
112     service->GetBindTime();
113 
114     int32_t slotId = static_cast<int32_t>(size % SLOT_NUM);
115     service->InsertSessionAndDetail(slotId, argsStr, argsStr);
116     uint16_t num = static_cast<uint16_t>(size);
117     service->QuerySessionByTelephone(argsStr, num, num);
118     service->InsertSmsMmsInfo(slotId, num, argsStr, argsStr);
119     bool value = slotId == 0 ? true : false;
120     service->InsertSession(value, num, argsStr, argsStr);
121     service->IsImsSmsSupported(slotId, value);
122     service->GetImsShortMessageFormat(argsStrU16);
123     service->HasSmsCapability();
124     service->SetSmscAddr(slotId, argsStrU16);
125     service->TrimSmscAddr(argsStr);
126     service->GetSmscAddr(slotId, argsStrU16);
127     uint32_t index = static_cast<uint32_t>(size);
128     service->CheckSimMessageIndexValid(slotId, index);
129     service->SetImsSmsConfig(slotId, value);
130     service->SetDefaultSmsSlotId(slotId);
131     service->GetDefaultSmsSlotId();
132     int32_t simId;
133     service->GetDefaultSmsSimId(simId);
134     service->GetServiceRunningState();
135     service->GetEndTime();
136     service->ValidDestinationAddress(argsStr);
137     service->GetSpendTime();
138     service->GetBase64Encode(argsStr, argsStr);
139     service->GetBase64Decode(argsStr, argsStr);
140     service->GetEncodeStringFunc(argsStr, index, index, argsStr);
141     service->SendMms(slotId, argsStrU16, argsStrU16, argsStrU16, argsStrU16);
142     service->DownloadMms(slotId, argsStrU16, argsStrU16, argsStrU16, argsStrU16);
143 }
144 
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)145 void DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
146 {
147     if (data == nullptr || size == 0) {
148         return;
149     }
150 
151     SendSmsTextRequest(data, size);
152     GetDefaultSmsSlotId(data, size);
153     SmsServiceInterfaceTest(data, size);
154     DelayedSingleton<ImsSmsClient>::GetInstance()->UnInit();
155     DelayedSingleton<ImsSmsClient>::DestroyInstance();
156     sleep(SLEEP_TIME_SECONDS);
157     DelayedSingleton<SmsService>::DestroyInstance();
158 }
159 } // namespace OHOS
160 
161 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)162 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
163 {
164     /* Run your code on data */
165     OHOS::AddSmsTokenFuzzer token;
166     OHOS::DoSomethingInterestingWithMyAPI(data, size);
167     return 0;
168 }
169