• 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_send_call_back_stub.h"
22 #include "napi_util.h"
23 #include "send_call_back_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 
IsServiceInited()31 bool IsServiceInited()
32 {
33     if (!g_isInited) {
34         DelayedSingleton<SmsService>::GetInstance()->OnStart();
35         if (DelayedSingleton<SmsService>::GetInstance()->GetServiceRunningState() ==
36             static_cast<int32_t>(Telephony::ServiceRunningState::STATE_RUNNING)) {
37             g_isInited = true;
38         }
39     }
40     return g_isInited;
41 }
42 
SendSmsTextRequest(const uint8_t * data,size_t size)43 void SendSmsTextRequest(const uint8_t *data, size_t size)
44 {
45     if (!IsServiceInited()) {
46         return;
47     }
48 
49     MessageParcel dataParcel;
50     MessageParcel replyParcel;
51     MessageOption option(MessageOption::TF_SYNC);
52 
53     int32_t slotId = static_cast<int32_t>(size % SLOT_NUM);
54     std::string desAddr(reinterpret_cast<const char *>(data), size);
55     std::string scAddr(reinterpret_cast<const char *>(data), size);
56     std::string text(reinterpret_cast<const char *>(data), size);
57     auto desAddrU16 = Str8ToStr16(desAddr);
58     auto scAddrU16 = Str8ToStr16(scAddr);
59     auto textU16 = Str8ToStr16(text);
60 
61     std::unique_ptr<SendCallbackStub> sendCallback = std::make_unique<SendCallbackStub>();
62     std::unique_ptr<DeliverySendCallbackStub> deliveryCallback = std::make_unique<DeliverySendCallbackStub>();
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 
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     SendSmsTextRequest(data, size);
102     GetDefaultSmsSlotId(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